diff --git a/addons/mrp/models/stock_move.py b/addons/mrp/models/stock_move.py index c327ffa03ff..cffa9188282 100644 --- a/addons/mrp/models/stock_move.py +++ b/addons/mrp/models/stock_move.py @@ -305,6 +305,10 @@ class StockMove(models.Model): to_assign = self.env['stock.move'] self._adjust_procure_method() for move in self: + if float_compare(move.product_uom_qty - old_qties.get(move.id, 0), 0, precision_rounding=move.product_uom.rounding) < 0\ + and move.procure_method == 'make_to_order'\ + and all(m.state == 'done' for m in move.move_orig_ids): + continue 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' \ @@ -313,6 +317,8 @@ class StockMove(models.Model): if move.procure_method == 'make_to_order': procurement_qty = move.product_uom_qty - old_qties.get(move.id, 0) + possible_reduceable_qty = -sum(move.move_orig_ids.filtered(lambda m: m.state not in ('done', 'cancel') and m.product_uom_qty).mapped('product_uom_qty')) + procurement_qty = max(procurement_qty, possible_reduceable_qty) 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 b7fb45dea88..a97a65aed21 100644 --- a/addons/mrp/tests/test_order.py +++ b/addons/mrp/tests/test_order.py @@ -3572,3 +3572,29 @@ class TestMrpOrder(TestMrpCommon): # Force the warning consumption_warning.action_confirm() self.assertEqual(mo.state, 'done') + + def test_cancel_return(self): + """ + check that the return picking is not created on done state transfer when reducing MO quantity. + """ + self.stock_location = self.env.ref('stock.stock_location_stock') + warehouse = self.env['stock.warehouse'].search([('company_id', '=', self.env.company.id)], limit=1) + warehouse.manufacture_steps = 'pbm' + + mo, _bom, _p_final, p1, p2 = self.generate_mo(qty_final=5.0, qty_base_1=1.0, qty_base_2=1.0) + mo.action_confirm() + + self.env['stock.quant']._update_available_quantity(p1, self.stock_location, 5.0) + self.env['stock.quant']._update_available_quantity(p2, self.stock_location, 5.0) + + mo.picking_ids.move_ids[0].quantity = 5.0 + mo.picking_ids.move_ids[1].quantity = 5.0 + mo.picking_ids.button_validate() + + update_quantity_wizard = self.env['change.production.qty'].create({ + 'mo_id': mo.id, + 'product_qty': 4.0, + }) + update_quantity_wizard.change_prod_qty() + new_picking = mo.picking_ids + self.assertEqual(len(new_picking), 1, "Return picking should not be created in done Transfer")