diff --git a/stock_mts_mto_mrp_rule/__manifest__.py b/stock_mts_mto_mrp_rule/__manifest__.py index 0ad27b239f7d..16aaddb687f4 100644 --- a/stock_mts_mto_mrp_rule/__manifest__.py +++ b/stock_mts_mto_mrp_rule/__manifest__.py @@ -1,7 +1,7 @@ { "name": "Stock MTS+MTO MRP Rule", "summary": "Stock MTS+MTO MRP Rule", - "version": "14.0.1.0.1", + "version": "17.0.1.0.0", "category": "Warehouse", "website": "https://github.com/OCA/stock-logistics-warehouse", "author": "Cetmix, Odoo Community Association (OCA)", diff --git a/stock_mts_mto_mrp_rule/models/stock_move.py b/stock_mts_mto_mrp_rule/models/stock_move.py index e209d16c2ad6..87cf6dc4b133 100644 --- a/stock_mts_mto_mrp_rule/models/stock_move.py +++ b/stock_mts_mto_mrp_rule/models/stock_move.py @@ -6,15 +6,13 @@ class StockMove(models.Model): _inherit = "stock.move" def _prepare_move_split_vals(self, qty): - vals = super(StockMove, self)._prepare_move_split_vals(qty) + vals = super()._prepare_move_split_vals(qty) if self._context.get("changed_purchase_method"): vals.update(procure_method="make_to_order") return vals def _split(self, qty, restrict_partner_id=False): - new_move_vals = super(StockMove, self)._split( - qty, restrict_partner_id=restrict_partner_id - ) + new_move_vals = super()._split(qty, restrict_partner_id=restrict_partner_id) if self._context.get("changed_purchase_method"): self.write({"procure_method": "make_to_stock"}) return new_move_vals @@ -27,21 +25,19 @@ def _adjust_procure_method(self): product_id = move.product_id domain = [ ("location_src_id", "=", move.location_id.id), - ("location_id", "=", move.location_dest_id.id), + ("location_dest_id", "=", move.location_dest_id.id), ("action", "!=", "push"), ] rules = self.env["procurement.group"]._search_rule( - False, product_id, move.warehouse_id, domain + False, move.product_packaging_id, product_id, move.warehouse_id, domain ) if not rules or rules and rules.action != "split_procurement": - super(StockMove, move)._adjust_procure_method() + return super(StockMove, move)._adjust_procure_method() else: needed_qty = rules.get_mto_qty_to_order( product_id, move.product_qty, move.product_uom, {} ) - if float_is_zero(needed_qty, precision_digits=precision): - move.procure_method = "make_to_stock" - elif ( + if ( float_compare( needed_qty, move.product_qty, precision_digits=precision ) @@ -49,9 +45,49 @@ def _adjust_procure_method(self): ): move.procure_method = "make_to_order" else: - self.create( - move.with_context(changed_purchase_method=True)._split( - needed_qty - ) - ) - move._action_assign() + move.procure_method = "make_to_stock" + + def _action_confirm(self, merge=True, merge_into=False): + moves_to_split = {} + for move in self: + if ( + move.state == "draft" + and move.procure_method == "make_to_stock" + and move.raw_material_production_id + ): + domain = [ + ("location_src_id", "=", move.location_id.id), + ("location_dest_id", "=", move.location_dest_id.id), + ("action", "!=", "push"), + ] + rules = self.env["procurement.group"]._search_rule( + False, + move.product_packaging_id, + move.product_id, + move.warehouse_id, + domain, + ) + if not rules or rules and rules.action != "split_procurement": + continue + needed_qty = rules.get_mto_qty_to_order( + move.product_id, move.product_qty, move.product_uom, {} + ) + if not float_is_zero( + needed_qty, precision_digits=move.product_id.uom_id.rounding + ): + moves_to_split[move] = needed_qty + res = super()._action_confirm(merge=merge, merge_into=merge_into) + for move, qty_to_split in moves_to_split.items(): + if ( + not move.exists() + or move.state in ["draft", "cancel", "done"] + or move.procure_method != "make_to_stock" + or not move.raw_material_production_id + ): + continue + move._do_unreserve() + self.create( + move.with_context(changed_purchase_method=True)._split(qty_to_split) + )._action_confirm() + move._action_assign() + return res \ No newline at end of file diff --git a/stock_mts_mto_mrp_rule/tests/common.py b/stock_mts_mto_mrp_rule/tests/common.py index 210bb7f5f94d..87ce74f3b744 100644 --- a/stock_mts_mto_mrp_rule/tests/common.py +++ b/stock_mts_mto_mrp_rule/tests/common.py @@ -1,14 +1,14 @@ -from odoo.tests import SavepointCase +from odoo.tests.common import TransactionCase -class TestCommon(SavepointCase): +class TestCommon(TransactionCase): @classmethod def setUpClass(cls): - super(TestCommon, cls).setUpClass() + super().setUpClass() Product = cls.env["product.product"] ProcurementGroup = cls.env["procurement.group"] - StockLocationRoute = cls.env["stock.location.route"] + StockLocationRoute = cls.env["stock.route"] StockRule = cls.env["stock.rule"] StockQuant = cls.env["stock.quant"] @@ -41,7 +41,7 @@ def setUpClass(cls): cls.dummy_rule = StockRule.create( { "name": "dummy rule", - "location_id": location_stock_id, + "location_dest_id": location_stock_id, "location_src_id": cls.env.ref("stock.stock_location_suppliers").id, "action": "pull", "warehouse_id": cls.warehouse.id,