[FIX] mrp: raise validation error only if total cost share exceeds 100

Steps to reproduce the bug:
- Enable the "by-product" option in the mrp settings.
- Create a product A with the following Bill of Materials:
    - Component: 1 unit of Product B.
    - By-product: 2 units of Product C.
- Create a Manufacturing Order to produce one unit of Product A.
- Confirm the MO.
- Set the quantity of Product A to 1.
- Set the quantity of the by-product "C" to 1 instead of 2.
- Attempt to validate the MO.

Problem:
When validating the MO, we check if the sum of the cost share in the
“move_byproduct” does not exceed 100. However, we do not exclude the
cancelled move, leading to incorrect calculations.

opw-3375388

closes odoo/odoo#127178

X-original-commit: 7fb1c9ff07f5ec05fc313283a497137327d9ce3a
Signed-off-by: William Henrotin (whe) <whe@odoo.com>
Signed-off-by: Djamel Touati (otd) <otd@odoo.com>
This commit is contained in:
Touati Djamel (otd)
2023-07-05 10:12:50 +02:00
parent 0c50cbe18c
commit 5b1bc2e1da
2 changed files with 22 additions and 1 deletions
+1 -1
View File
@@ -794,7 +794,7 @@ class MrpProduction(models.Model):
for order in self:
if any(move.cost_share < 0 for move in order.move_byproduct_ids):
raise ValidationError(_("By-products cost shares must be positive."))
if sum(order.move_byproduct_ids.mapped('cost_share')) > 100:
if sum(order.move_byproduct_ids.filtered(lambda m: m.state != 'cancel').mapped('cost_share')) > 100:
raise ValidationError(_("The total cost share for a manufacturing order's by-products cannot exceed 100."))
def write(self, vals):
+21
View File
@@ -386,3 +386,24 @@ class TestMrpByProduct(common.TransactionCase):
byproduct_1.cost_share = 60
byproduct_2.cost_share = 70
mo.write({'move_byproduct_ids': [(6, 0, [byproduct_1.id, byproduct_2.id])]})
def test_check_byproducts_cost_share_02(self):
"""
Test that byproducts with total cost_share < 100% with a cancelled moves will don't throw a ValidationError
"""
self.bom_byproduct.byproduct_ids[0].cost_share = 70
self.bom_byproduct.byproduct_ids[0].product_qty = 2
mo = self.env["mrp.production"].create({
'product_id': self.product_a.id,
'product_qty': 1.0,
'bom_id': self.bom_byproduct.id,
})
mo.action_confirm()
self.assertEqual(mo.state, 'confirmed')
mo_form = Form(mo)
mo_form.qty_producing = 1
mo = mo_form.save()
self.assertEqual(mo.state, 'to_close')
mo.move_byproduct_ids[0].quantity_done = 1
mo.button_mark_done()
self.assertEqual(mo.state, 'done')