diff --git a/stock_available_mrp/__openerp__.py b/stock_available_mrp/__openerp__.py index 64ad57aaa..2d6afa640 100644 --- a/stock_available_mrp/__openerp__.py +++ b/stock_available_mrp/__openerp__.py @@ -3,7 +3,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). { 'name': 'Consider the production potential is available to promise', - 'version': '8.0.3.1.0', + 'version': '8.0.3.1.1', "author": u"Numérigraphe," u"Odoo Community Association (OCA)", 'category': 'Hidden', diff --git a/stock_available_mrp/models/product_product.py b/stock_available_mrp/models/product_product.py index 74be352c6..c6440f31a 100644 --- a/stock_available_mrp/models/product_product.py +++ b/stock_available_mrp/models/product_product.py @@ -19,6 +19,14 @@ class ProductProduct(models.Model): help="Quantity of this Product that could be produced using " "the materials already at hand.") + # Needed for fields dependencies + # When self.potential_qty is compute, we want to force the ORM + # to compute all the components potential_qty too. + component_ids = fields.Many2many( + comodel_name='product.product', + compute='_get_component_ids', + ) + @api.multi @api.depends('potential_qty') def _immediately_usable_qty(self): @@ -30,6 +38,7 @@ class ProductProduct(models.Model): product.immediately_usable_qty += product.potential_qty @api.multi + @api.depends('component_ids.potential_qty') def _get_potential_qty(self): """Compute the potential qty based on the available components.""" bom_obj = self.env['mrp.bom'] @@ -104,3 +113,15 @@ class ProductProduct(models.Model): ) return needs + + def _get_component_ids(self): + """ Compute component_ids by getting all the components for + this product. + """ + bom_obj = self.env['mrp.bom'] + + bom_id = bom_obj._bom_find(product_id=self.id) + if bom_id: + bom = bom_obj.browse(bom_id) + for bom_component in bom_obj._bom_explode(bom, self, 1.0)[0]: + self.component_ids |= self.browse(bom_component['product_id']) diff --git a/stock_available_mrp/tests/test_potential_qty.py b/stock_available_mrp/tests/test_potential_qty.py index 1157d29d7..98eb9a545 100644 --- a/stock_available_mrp/tests/test_potential_qty.py +++ b/stock_available_mrp/tests/test_potential_qty.py @@ -85,6 +85,24 @@ class TestPotentialQty(TransactionCase): }) inventory.action_done() + def create_simple_bom(self, product, sub_product, + product_qty=1, sub_product_qty=1): + bom = self.bom_model.create({ + 'product_tmpl_id': product.product_tmpl_id.id, + 'product_id': product.id, + 'product_qty': product_qty, + 'product_uom': self.ref('product.product_uom_unit'), + + }) + self.bom_line_model.create({ + 'bom_id': bom.id, + 'product_id': sub_product.id, + 'product_qty': sub_product_qty, + 'product_uom': self.ref('product.product_uom_unit'), + }) + + return bom + def assertPotentialQty(self, record, qty, msg): record.refresh() # Check the potential @@ -465,3 +483,33 @@ class TestPotentialQty(TransactionCase): 'virtual_available') p1.refresh() self.assertEqual(5.0, p1.potential_qty) + + def test_potential_qty__list(self): + # Try to highlight a bug when _get_potential_qty is called on + # a recordset with multiple products + # Recursive compute is not working + + p1 = self.product_model.create({'name': 'Test P1'}) + p2 = self.product_model.create({'name': 'Test P2'}) + p3 = self.product_model.create({'name': 'Test P3'}) + + self.config.set_param('stock_available_mrp_based_on', + 'immediately_usable_qty') + + # P1 need one P2 + self.create_simple_bom(p1, p2) + # P2 need one P3 + self.create_simple_bom(p2, p3) + + self.create_inventory(p3.id, 3) + + self.product_model.invalidate_cache() + + products = self.product_model.search( + [('id', 'in', [p1.id, p2.id, p3.id])] + ) + + self.assertEqual( + {p1.id: 3.0, p2.id: 3.0, p3.id: 0.0}, + {p.id: p.potential_qty for p in products} + )