[FIX] mrp: update MO qty reserves components
Steps to reproduce: - Create and confirm an MO with extra components in stock - Update the quantity to produce Bug: the reserved qty is not updated (it actually is but to the previous value if you update MO quantity multiple times) since this commit[1] moves are now assigned during the write before the new product_uom_qty is written on them opw-3489226 [1]:https://github.com/odoo/odoo/pull/93712 closes odoo/odoo#135548 X-original-commit: cc23ebefb4a15d163ec508cd535bf757cf626fa5 Signed-off-by: Adrien Widart (awt) <awt@odoo.com> Signed-off-by: Walid Hanniche (waha) <waha@odoo.com>
This commit is contained in:
@@ -298,26 +298,29 @@ class StockMove(models.Model):
|
||||
# so possibly unlink lines
|
||||
move_line_vals = vals.pop('move_line_ids')
|
||||
super().write({'move_line_ids': move_line_vals})
|
||||
if 'product_uom_qty' in vals and self.raw_material_production_id.state == 'confirmed' and not self.env.context.get('no_procurement', False):
|
||||
old_demand = {move.id: move.product_uom_qty for move in self}
|
||||
res = super().write(vals)
|
||||
if 'product_uom_qty' in vals and not self.env.context.get('no_procurement', False):
|
||||
# when updating consumed qty need to update related pickings
|
||||
# context no_procurement means we don't want the qty update to modify stock i.e create new pickings
|
||||
# ex. when spliting MO to backorders we don't want to move qty from pre prod to stock in 2/3 step config
|
||||
self._run_procurement(vals['product_uom_qty'], self.product_uom_qty)
|
||||
return super().write(vals)
|
||||
self.filtered(lambda m: m.raw_material_production_id.state == 'confirmed')._run_procurement(old_demand)
|
||||
return res
|
||||
|
||||
def _run_procurement(self, new_qty, old_qty):
|
||||
def _run_procurement(self, old_qties=False):
|
||||
procurements = []
|
||||
old_qties = old_qties or {}
|
||||
to_assign = self.env['stock.move']
|
||||
self._adjust_procure_method()
|
||||
for move in self:
|
||||
if new_qty > 0:
|
||||
if float_compare(move.product_uom_qty, 0, precision_rounding=move.product_uom.rounding) > 0:
|
||||
if move._should_bypass_reservation() \
|
||||
or move.picking_type_id.reservation_method == 'at_confirm' \
|
||||
or (move.reservation_date and move.reservation_date <= fields.Date.today()):
|
||||
to_assign |= move
|
||||
|
||||
if move.procure_method == 'make_to_order':
|
||||
procurement_qty = new_qty - old_qty
|
||||
procurement_qty = move.product_uom_qty - old_qties.get(move.id, 0)
|
||||
values = move._prepare_procurement_values()
|
||||
origin = move._prepare_procurement_origin()
|
||||
procurements.append(self.env['procurement.group'].Procurement(
|
||||
|
||||
@@ -3089,19 +3089,29 @@ class TestMrpOrder(TestMrpCommon):
|
||||
(0, 0, {'product_id': self.product_2.id, 'product_qty': 1.0, 'product_uom_id': self.box250.id}),
|
||||
]
|
||||
})
|
||||
self.env['stock.quant'].create({
|
||||
'location_id':self.env.ref('stock.stock_location_stock').id,
|
||||
'product_id': self.product_2.id,
|
||||
'inventory_quantity': 500
|
||||
}).action_apply_inventory()
|
||||
|
||||
mo_form = Form(self.env['mrp.production'])
|
||||
mo_form.bom_id = test_bom
|
||||
mo = mo_form.save()
|
||||
mo.action_confirm()
|
||||
|
||||
self.assertEqual(mo.move_raw_ids.product_uom_qty, 1)
|
||||
self.assertEqual(mo.move_raw_ids.move_line_ids.reserved_uom_qty, mo.move_raw_ids.product_uom_qty)
|
||||
self.assertEqual(mo.move_raw_ids.availability, 250)
|
||||
update_quantity_wizard = self.env['change.production.qty'].create({
|
||||
'mo_id': mo.id,
|
||||
'product_qty': 300,
|
||||
})
|
||||
update_quantity_wizard.change_prod_qty()
|
||||
|
||||
self.assertEqual(mo.move_raw_ids[0].product_uom_qty, 2)
|
||||
self.assertEqual(mo.move_raw_ids.product_uom_qty, 2)
|
||||
self.assertEqual(mo.move_raw_ids.move_line_ids.reserved_uom_qty, mo.move_raw_ids.product_uom_qty)
|
||||
self.assertEqual(mo.move_raw_ids.availability, 0)
|
||||
|
||||
def test_update_qty_to_consume_of_component(self):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user