Skip to content
Permalink
Browse files

[FIX] mrp: update quantity after post inventory

Usecase to reproduce:
- Create a BoM with 2 components for a final product
- Create a MO for 100 final products
- Produce 50
- Post Inventory
- Update the MO quantity to 200
- Produce 50 more

The total quantity for components consumed is 175 (instead of 200)

It happens because the update quantity do not take care of existing
produced unit when it updates the raw material moves and their unit
factor. (FYI unit factor is used later in order to know the quantity
to consume depending the produced quantity)

Fix #39866

closes #39963

Signed-off-by: Arnold Moyaux <amoyaux@users.noreply.github.com>
  • Loading branch information...
amoyaux committed Nov 7, 2019
1 parent 80f22f9 commit 77ed1900fec14c6052ffdec73b897377e0333f37
Showing with 41 additions and 1 deletion.
  1. +3 −1 addons/mrp/models/mrp_production.py
  2. +38 −0 addons/mrp/tests/test_order.py
@@ -451,11 +451,13 @@ def _update_raw_move(self, bom_line, line_data):
move = self.move_raw_ids.filtered(lambda x: x.bom_line_id.id == bom_line.id and x.state not in ('done', 'cancel'))
if move:
if quantity > 0:
production = move[0].raw_material_production_id
production_qty = production.product_qty - production.qty_produced
move[0]._decrease_reserved_quanity(quantity)
move[0].with_context(do_not_unreserve=True).write({'product_uom_qty': quantity})
move[0]._recompute_state()
move[0]._action_assign()
move[0].unit_factor = quantity / move[0].raw_material_production_id.product_qty
move[0].unit_factor = production_qty and quantity / production_qty or 0.0
elif quantity < 0: # Do not remove 0 lines
if move[0].quantity_done > 0:
raise UserError(_('Lines need to be deleted, but can not as you still have some quantities to consume in them. '))
@@ -663,3 +663,41 @@ def test_product_produce_5(self):

self.assertEqual(mo.move_raw_ids.filtered(lambda m: m.product_id == p1).quantity_done, 20, 'Update the produce quantity should not impact already produced quantity.')
mo.button_mark_done()

def test_product_produce_6(self):
""" Plan 100 products to produce. Produce 50. Do a 'Post Inventory'.
Update the quantity to produce to 200. Produce 50 again. Check that
the components has been consumed correctly.
"""
self.stock_location = self.env.ref('stock.stock_location_stock')
mo, bom, p_final, p1, p2 = self.generate_mo(qty_base_1=2, qty_base_2=2, qty_final=100)
self.assertEqual(len(mo), 1, 'MO should have been created')

self.env['stock.quant']._update_available_quantity(p1, self.stock_location, 200)
self.env['stock.quant']._update_available_quantity(p2, self.stock_location, 200)

produce_wizard = self.env['mrp.product.produce'].with_context({
'active_id': mo.id,
'active_ids': [mo.id],
}).create({
'product_qty': 50.0,
})
produce_wizard.do_produce()

mo.post_inventory()
update_quantity_wizard = self.env['change.production.qty'].create({
'mo_id': mo.id,
'product_qty': 200,
})
update_quantity_wizard.change_prod_qty()
produce_wizard = self.env['mrp.product.produce'].with_context({
'active_id': mo.id,
'active_ids': [mo.id],
}).create({
'product_qty': 50.0,
})
produce_wizard.do_produce()

self.assertEqual(sum(mo.move_raw_ids.filtered(lambda m: m.product_id == p1).mapped('quantity_done')), 200)
self.assertEqual(sum(mo.move_raw_ids.filtered(lambda m: m.product_id == p2).mapped('quantity_done')), 200)
self.assertEqual(sum(mo.move_finished_ids.mapped('quantity_done')), 100)

0 comments on commit 77ed190

Please sign in to comment.
You can’t perform that action at this time.