From 58688edb9a0e9349d68eb7d3f90b20fcdf1a8b5c Mon Sep 17 00:00:00 2001 From: Deivis Laya Date: Sat, 19 Sep 2020 18:34:23 +0000 Subject: [PATCH] [MIG] stock_mts_mto_rule: Migration to 13.0 --- stock_mts_mto_rule/README.rst | 2 +- stock_mts_mto_rule/__manifest__.py | 6 +- .../i18n/stock_mts_mto_rule.pot | 2 +- stock_mts_mto_rule/models/stock_rule.py | 73 ++++++++---- stock_mts_mto_rule/models/stock_warehouse.py | 4 +- .../tests/test_mto_mts_route.py | 111 +++++++++++------- 6 files changed, 123 insertions(+), 75 deletions(-) diff --git a/stock_mts_mto_rule/README.rst b/stock_mts_mto_rule/README.rst index 098c2209e..38fb4659a 100644 --- a/stock_mts_mto_rule/README.rst +++ b/stock_mts_mto_rule/README.rst @@ -47,7 +47,7 @@ You should not select both the mts+mto route and the mto route. .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas :alt: Try me on Runbot - :target: https://runbot.odoo-community.org/runbot/153/12.0 + :target: https://runbot.odoo-community.org/runbot/153/13.0 Known issues ============ diff --git a/stock_mts_mto_rule/__manifest__.py b/stock_mts_mto_rule/__manifest__.py index 661ee8950..138d0b159 100644 --- a/stock_mts_mto_rule/__manifest__.py +++ b/stock_mts_mto_rule/__manifest__.py @@ -3,7 +3,7 @@ { "name": "Stock MTS+MTO Rule", "summary": "Add a MTS+MTO route", - "version": "12.0.1.0.1", + "version": "13.0.1.0.1", "development_status": "Mature", "category": "Warehouse", "website": "https://github.com/OCA/stock-logistics-warehouse", @@ -11,6 +11,6 @@ "license": "AGPL-3", "application": False, "installable": True, - "depends": ["stock",], - "data": ["data/stock_data.xml", "view/pull_rule.xml", "view/warehouse.xml",], + "depends": ["stock"], + "data": ["data/stock_data.xml", "view/pull_rule.xml", "view/warehouse.xml"], } diff --git a/stock_mts_mto_rule/i18n/stock_mts_mto_rule.pot b/stock_mts_mto_rule/i18n/stock_mts_mto_rule.pot index 8b2a8120c..8a75668c9 100644 --- a/stock_mts_mto_rule/i18n/stock_mts_mto_rule.pot +++ b/stock_mts_mto_rule/i18n/stock_mts_mto_rule.pot @@ -4,7 +4,7 @@ # msgid "" msgstr "" -"Project-Id-Version: Odoo Server 12.0\n" +"Project-Id-Version: Odoo Server 13.0\n" "Report-Msgid-Bugs-To: \n" "Last-Translator: <>\n" "Language-Team: \n" diff --git a/stock_mts_mto_rule/models/stock_rule.py b/stock_mts_mto_rule/models/stock_rule.py index 6d86fcc05..e122a0cb8 100644 --- a/stock_mts_mto_rule/models/stock_rule.py +++ b/stock_mts_mto_rule/models/stock_rule.py @@ -2,6 +2,7 @@ from odoo import _, api, fields, models from odoo.exceptions import ValidationError +from odoo.osv import expression from odoo.tools import float_compare, float_is_zero @@ -11,8 +12,8 @@ class StockRule(models.Model): action = fields.Selection( selection_add=[("split_procurement", "Choose between MTS and MTO")] ) - mts_rule_id = fields.Many2one("stock.rule", string="MTS Rule") - mto_rule_id = fields.Many2one("stock.rule", string="MTO Rule") + mts_rule_id = fields.Many2one("stock.rule", string="MTS Rule", check_company=True) + mto_rule_id = fields.Many2one("stock.rule", string="MTO Rule", check_company=True) @api.constrains("action", "mts_rule_id", "mto_rule_id") def _check_mts_mto_rule(self): @@ -34,7 +35,6 @@ class StockRule(models.Model): ) % (rule.name,) raise ValidationError(msg) - @api.multi def get_mto_qty_to_order(self, product, product_qty, product_uom, values): self.ensure_one() precision = self.env["decimal.precision"].precision_get( @@ -54,30 +54,53 @@ class StockRule(models.Model): return product_qty - qty_available return product_qty - def _run_split_procurement( - self, product_id, product_qty, product_uom, location_id, name, origin, values - ): + def _run_split_procurement(self, procurements): precision = self.env["decimal.precision"].precision_get( "Product Unit of Measure" ) - needed_qty = self.get_mto_qty_to_order( - product_id, product_qty, product_uom, values - ) + for procurement, rule in procurements: + domain = self.env["procurement.group"]._get_moves_to_assign_domain( + procurement.company_id.id + ) + needed_qty = rule.get_mto_qty_to_order( + procurement.product_id, + procurement.product_qty, + procurement.product_uom, + procurement.values, + ) + if float_is_zero(needed_qty, precision_digits=precision): + getattr(self.env["stock.rule"], "_run_%s" % rule.mts_rule_id.action)( + [(procurement, rule.mts_rule_id)] + ) + elif ( + float_compare( + needed_qty, procurement.product_qty, precision_digits=precision + ) + == 0.0 + ): + getattr(self.env["stock.rule"], "_run_%s" % rule.mto_rule_id.action)( + [(procurement, rule.mto_rule_id)] + ) + else: + mts_qty = procurement.product_qty - needed_qty + mts_procurement = procurement._replace(product_qty=mts_qty) + getattr(self.env["stock.rule"], "_run_%s" % rule.mts_rule_id.action)( + [(mts_procurement, rule.mts_rule_id)] + ) - if float_is_zero(needed_qty, precision_digits=precision): - getattr(self.mts_rule_id, "_run_%s" % self.mts_rule_id.action)( - product_id, product_qty, product_uom, location_id, name, origin, values - ) - elif float_compare(needed_qty, product_qty, precision_digits=precision) == 0.0: - getattr(self.mto_rule_id, "_run_%s" % self.mto_rule_id.action)( - product_id, product_qty, product_uom, location_id, name, origin, values - ) - else: - mts_qty = product_qty - needed_qty - getattr(self.mts_rule_id, "_run_%s" % self.mts_rule_id.action)( - product_id, mts_qty, product_uom, location_id, name, origin, values - ) - getattr(self.mto_rule_id, "_run_%s" % self.mto_rule_id.action)( - product_id, needed_qty, product_uom, location_id, name, origin, values - ) + # Search all confirmed stock_moves of mts_procuremet and assign them + # to adjust the product's free qty + group_id = mts_procurement.values.get("group_id") + group_domain = expression.AND( + [domain, [("group_id", "=", group_id.id)]] + ) + moves_to_assign = self.env["stock.move"].search( + group_domain, order="priority desc, date_expected asc" + ) + moves_to_assign._action_assign() + + mto_procurement = procurement._replace(product_qty=needed_qty) + getattr(self.env["stock.rule"], "_run_%s" % rule.mto_rule_id.action)( + [(mto_procurement, rule.mto_rule_id)] + ) return True diff --git a/stock_mts_mto_rule/models/stock_warehouse.py b/stock_mts_mto_rule/models/stock_warehouse.py index 5b7bc40f5..c63675fab 100644 --- a/stock_mts_mto_rule/models/stock_warehouse.py +++ b/stock_mts_mto_rule/models/stock_warehouse.py @@ -12,7 +12,7 @@ class StockWarehouse(models.Model): "purchase order will be created only if the virtual stock is " "less than 0 else, the product will be taken from stocks", ) - mts_mto_rule_id = fields.Many2one("stock.rule", "MTO+MTS rule") + mts_mto_rule_id = fields.Many2one("stock.rule", "MTO+MTS rule", check_company=True) def _get_all_routes(self): routes = super(StockWarehouse, self)._get_all_routes() @@ -54,7 +54,7 @@ class StockWarehouse(models.Model): "procure_method": "make_to_order", "company_id": self.company_id.id, "auto": "manual", - "propagate": True, + "propagate_cancel": True, "route_id": self._find_global_route( "stock_mts_mto_rule.route_mto_mts", _("Make To Order + Make To Stock"), diff --git a/stock_mts_mto_rule/tests/test_mto_mts_route.py b/stock_mts_mto_rule/tests/test_mto_mts_route.py index 13ae60a11..0f4609192 100644 --- a/stock_mts_mto_rule/tests/test_mto_mts_route.py +++ b/stock_mts_mto_rule/tests/test_mto_mts_route.py @@ -18,27 +18,37 @@ class TestMtoMtsRoute(TransactionCase): def test_standard_mto_route(self): mto_route = self.env.ref("stock.route_warehouse0_mto") self.product.route_ids = [(6, 0, [mto_route.id])] - self.group.run( - self.product, - 2.0, - self.uom, - self.customer_loc, - self.product.name, - "test", - self.procurement_vals, + self.env["procurement.group"].run( + [ + self.group.Procurement( + self.product, + 2.0, + self.uom, + self.customer_loc, + self.product.name, + "test", + self.warehouse.company_id, + self.procurement_vals, + ) + ] ) moves = self.move_obj.search([("group_id", "=", self.group.id)]) self.assertEqual(len(moves), 2) def test_standard_mts_route(self): - self.group.run( - self.product, - 2.0, - self.uom, - self.customer_loc, - self.product.name, - "test", - self.procurement_vals, + self.env["procurement.group"].run( + [ + self.group.Procurement( + self.product, + 2.0, + self.uom, + self.customer_loc, + self.product.name, + "test", + self.warehouse.company_id, + self.procurement_vals, + ) + ] ) moves = self.move_obj.search([("group_id", "=", self.group.id)]) self.assertEqual(len(moves), 1) @@ -47,14 +57,19 @@ class TestMtoMtsRoute(TransactionCase): mto_mts_route = self.env.ref("stock_mts_mto_rule.route_mto_mts") self.product.route_ids = [(6, 0, [mto_mts_route.id])] self._create_quant(1.0) - self.group.run( - self.product, - 2.0, - self.uom, - self.customer_loc, - self.product.name, - "test", - self.procurement_vals, + self.env["procurement.group"].run( + [ + self.group.Procurement( + self.product, + 2.0, + self.uom, + self.customer_loc, + self.product.name, + "test", + self.warehouse.company_id, + self.procurement_vals, + ) + ] ) moves = self.env["stock.move"].search([("group_id", "=", self.group.id)]) self.assertEqual(3, len(moves)) @@ -67,7 +82,7 @@ class TestMtoMtsRoute(TransactionCase): ) self.assertEqual(1, len(move_mts)) self.assertEqual(1.0, move_mts.product_uom_qty) - self.assertEqual("confirmed", move_mts.state) + self.assertEqual("assigned", move_mts.state) move_mto = self.env["stock.move"].search( [ ("group_id", "=", self.group.id), @@ -81,14 +96,19 @@ class TestMtoMtsRoute(TransactionCase): def test_mts_mto_route_mto_only(self): mto_mts_route = self.env.ref("stock_mts_mto_rule.route_mto_mts") self.product.route_ids = [(6, 0, [mto_mts_route.id])] - self.group.run( - self.product, - 2.0, - self.uom, - self.customer_loc, - self.product.name, - "test", - self.procurement_vals, + self.env["procurement.group"].run( + [ + self.group.Procurement( + self.product, + 2.0, + self.uom, + self.customer_loc, + self.product.name, + "test", + self.warehouse.company_id, + self.procurement_vals, + ) + ] ) moves = self.env["stock.move"].search( [ @@ -104,14 +124,19 @@ class TestMtoMtsRoute(TransactionCase): mto_mts_route = self.env.ref("stock_mts_mto_rule.route_mto_mts") self.product.route_ids = [(6, 0, [mto_mts_route.id])] self._create_quant(3.0) - self.group.run( - self.product, - 2.0, - self.uom, - self.customer_loc, - self.product.name, - "test", - self.procurement_vals, + self.env["procurement.group"].run( + [ + self.group.Procurement( + self.product, + 2.0, + self.uom, + self.customer_loc, + self.product.name, + "test", + self.warehouse.company_id, + self.procurement_vals, + ) + ] ) moves = self.env["stock.move"].search([("group_id", "=", self.group.id)]) self.assertEqual(1, len(moves)) @@ -195,10 +220,10 @@ class TestMtoMtsRoute(TransactionCase): self.warehouse.mto_mts_management = True self.customer_loc = self.env.ref("stock.stock_location_customers") self.product = self.env["product.product"].create( - {"name": "Test product", "type": "product",} + {"name": "Test product", "type": "product"} ) self.company_partner = self.env.ref("base.main_partner") - self.group = self.env["procurement.group"].create({"name": "test",}) + self.group = self.env["procurement.group"].create({"name": "test"}) self.procurement_vals = {"warehouse_id": self.warehouse, "group_id": self.group} # Since mrp and purchase modules may not be installed, we need to # create a dummy step to show that mts, mto, and mts+mto flows work.