From 36757cde6d00a9e9059ce0bfb73d768c26743ab8 Mon Sep 17 00:00:00 2001
From: svs-odoo <svs@odoo.com>
Date: Thu, 5 Sep 2019 10:05:22 +0000
Subject: [PATCH] [FIX] stock: domain on quant lot_id

Adds a domain on quant `lot_id` when user changes quantity on hand of a
specific product, so user can select lot only for this product (or its
product variants if user come from a product template).

closes odoo/odoo#36467

Signed-off-by: Simon Lejeune (sle) <sle@openerp.com>
---
 addons/stock/models/stock_quant.py | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/addons/stock/models/stock_quant.py b/addons/stock/models/stock_quant.py
index 0adc8e4f7ddc..ea7e26eec2fe 100644
--- a/addons/stock/models/stock_quant.py
+++ b/addons/stock/models/stock_quant.py
@@ -23,6 +23,22 @@ class StockQuant(models.Model):
             return
         return [('usage', 'in', ['internal', 'transit'])]
 
+    def _domain_lot_id(self):
+        if not self._is_inventory_mode():
+            return
+        domain = [
+            "'|'",
+                "('company_id', '=', company_id)",
+                "('company_id', '=', False)"
+        ]
+        if self.env.context.get('active_model') == 'product.product':
+            domain.insert(0, "('product_id', '=', %s)" % self.env.context.get('active_id'))
+        if self.env.context.get('active_model') == 'product.template':
+            product_template = self.env['product.template'].browse(self.env.context.get('active_id'))
+            if product_template.exists():
+                domain.insert(0, "('product_id', 'in', %s)" % product_template.product_variant_ids.ids)
+        return '[' + ', '.join(domain) + ']'
+
     def _domain_product_id(self):
         if not self._is_inventory_mode():
             return
@@ -50,7 +66,8 @@ class StockQuant(models.Model):
         auto_join=True, ondelete='restrict', readonly=True, required=True, check_company=True)
     lot_id = fields.Many2one(
         'stock.production.lot', 'Lot/Serial Number',
-        ondelete='restrict', readonly=True, check_company=True)
+        ondelete='restrict', readonly=True, check_company=True,
+        domain=lambda self: self._domain_lot_id())
     package_id = fields.Many2one(
         'stock.quant.package', 'Package',
         domain="[('location_id', '=', location_id)]",
-- 
GitLab