From 81519aa84b039225faa8c356ffb8214bcd5ae39b Mon Sep 17 00:00:00 2001 From: "Hubert (huvw)" Date: Tue, 27 Jul 2021 11:12:30 +0000 Subject: [PATCH] [FIX] stock,purchase_stock: multi-company issue on routes with a single company Step to follow Do not activate multiple companies, keep it standard to 1 Enable multi-locations and routes Create a second location for raw materials and a new Receipt operation type with the new location as destination location Go to routes -> edit the buy route -> add a second rule with "Buy" and save. Cause of the issue When a stock rule is created from a stock route, the default company cannot be found as there is none assigned to the stock route. Solution Override the default_get method for stock rules in order to use the current company opw-2578984 closes odoo/odoo#74380 X-original-commit: 2b8d7d76adceeca16f97e394a6734bb5be633c3b Signed-off-by: Victor Feyens (vfe) --- addons/purchase_stock/tests/__init__.py | 1 + addons/purchase_stock/tests/test_routes.py | 53 ++++++++++++++++++++++ addons/stock/models/stock_rule.py | 7 +++ 3 files changed, 61 insertions(+) create mode 100644 addons/purchase_stock/tests/test_routes.py diff --git a/addons/purchase_stock/tests/__init__.py b/addons/purchase_stock/tests/__init__.py index aa9e156fd88..31ce9c7181a 100644 --- a/addons/purchase_stock/tests/__init__.py +++ b/addons/purchase_stock/tests/__init__.py @@ -17,3 +17,4 @@ from . import test_replenish_wizard from . import test_reordering_rule from . import test_move_cancel_propagation from . import test_product_template +from . import test_routes diff --git a/addons/purchase_stock/tests/test_routes.py b/addons/purchase_stock/tests/test_routes.py new file mode 100644 index 00000000000..7510011b0bc --- /dev/null +++ b/addons/purchase_stock/tests/test_routes.py @@ -0,0 +1,53 @@ +from odoo.tests.common import TransactionCase, Form + + +class TestRoutes(TransactionCase): + + def test_allow_rule_creation_for_route_without_company(self): + self.env['res.config.settings'].write({ + 'group_stock_adv_location': True, + 'group_stock_multi_locations': True, + }) + + warehouse = self.env['stock.warehouse'].search([('company_id', '=', self.env.company.id)], limit=1) + + location_1 = self.env['stock.location'].create({ + 'name': 'loc1', + 'location_id': warehouse.id + }) + + location_2 = self.env['stock.location'].create({ + 'name': 'loc2', + 'location_id': warehouse.id + }) + + receipt_1 = self.env['stock.picking.type'].create({ + 'name': 'Receipts from loc1', + 'sequence_code': 'IN1', + 'code': 'incoming', + 'warehouse_id': warehouse.id, + 'default_location_dest_id': location_1.id, + }) + + receipt_2 = self.env['stock.picking.type'].create({ + 'name': 'Receipts from loc2', + 'sequence_code': 'IN2', + 'code': 'incoming', + 'warehouse_id': warehouse.id, + 'default_location_dest_id': location_2.id, + }) + + route = self.env['stock.location.route'].create({ + 'name': 'Buy', + 'company_id': False + }) + + with Form(route) as r: + with r.rule_ids.new() as line: + line.name = 'first rule' + line.action = 'buy' + line.picking_type_id = receipt_1 + with r.rule_ids.new() as line: + line.name = 'second rule' + line.action = 'buy' + line.picking_type_id = receipt_2 diff --git a/addons/stock/models/stock_rule.py b/addons/stock/models/stock_rule.py index 79d718d6e8f..789981b627f 100644 --- a/addons/stock/models/stock_rule.py +++ b/addons/stock/models/stock_rule.py @@ -34,6 +34,13 @@ class StockRule(models.Model): _order = "sequence, id" _check_company_auto = True + @api.model + def default_get(self, fields_list): + res = super().default_get(fields_list) + if 'company_id' in fields_list and not res['company_id']: + res['company_id'] = self.env.company.id + return res + name = fields.Char( 'Name', required=True, translate=True, help="This field will fill the packing origin and the name of its moves")