diff --git a/addons/mrp/models/stock_move.py b/addons/mrp/models/stock_move.py index 3bd6a83c9b6..058765a1ae1 100644 --- a/addons/mrp/models/stock_move.py +++ b/addons/mrp/models/stock_move.py @@ -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( diff --git a/addons/mrp/tests/test_order.py b/addons/mrp/tests/test_order.py index ef0f71a0f51..7a5a701b32e 100644 --- a/addons/mrp/tests/test_order.py +++ b/addons/mrp/tests/test_order.py @@ -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): """