From 1ac00874a55db900453e7c9b7bbdd5425c08161c Mon Sep 17 00:00:00 2001 From: Arnold Moyaux Date: Tue, 1 Oct 2019 09:12:16 +0000 Subject: [PATCH] [FIX] stock: manual inter-company flow Usecase: - Create a route without company - Create a push rule for company B from intercompany to stock - Apply the push rule on product and set in on a product. - Connect with a user in company A - Create a move from stock(company A) to intercompany. - Confirm the move -> The receipt for company B is not created. Multiple issues: - The rule is not found because it belong to the other company and is filtered by the record rules. -> Aplly the seach as sudo if the move is going to a location without company. - Record rule error because a move from intercompany to company B is created with a user from company A that has no access to company A. -> Search and read company values and create the move as super user. Based on work from Simon Lejeune closes odoo/odoo#38124 X-original-commit: a80262e8b97256cac4bc5c406bc74a05f7189a00 Signed-off-by: Simon Goffin (sig) --- addons/stock/models/stock_move.py | 6 ++++-- addons/stock/models/stock_rule.py | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/addons/stock/models/stock_move.py b/addons/stock/models/stock_move.py index b70ff1354c9..064584db81b 100644 --- a/addons/stock/models/stock_move.py +++ b/addons/stock/models/stock_move.py @@ -661,8 +661,10 @@ class StockMove(models.Model): domain = [('location_src_id', '=', move.location_dest_id.id), ('action', 'in', ('push', 'pull_push'))] # first priority goes to the preferred routes defined on the move itself (e.g. coming from a SO line) warehouse_id = move.warehouse_id or move.picking_id.picking_type_id.warehouse_id - rules = self.env['procurement.group']._search_rule(move.route_ids, move.product_id, warehouse_id, domain) - + if not self.env.context.get('force_company', False) and move.location_dest_id.company_id == self.env.user.company_id: + rules = self.env['procurement.group']._search_rule(move.route_ids, move.product_id, warehouse_id, domain) + else: + rules = self.sudo().env['procurement.group']._search_rule(move.route_ids, move.product_id, warehouse_id, domain) # Make sure it is not returning the return if rules and (not move.origin_returned_move_id or move.origin_returned_move_id.location_dest_id.id != rules.location_id.id): rules._run_push(move) diff --git a/addons/stock/models/stock_rule.py b/addons/stock/models/stock_rule.py index c82fcc603ad..d810c2655f4 100644 --- a/addons/stock/models/stock_rule.py +++ b/addons/stock/models/stock_rule.py @@ -169,18 +169,21 @@ class StockRule(models.Model): move._push_apply() else: new_move_vals = self._push_prepare_move_copy_values(move, new_date) - new_move = move.copy(new_move_vals) + new_move = move.sudo().copy(new_move_vals) move.write({'move_dest_ids': [(4, new_move.id)]}) new_move._action_confirm() def _push_prepare_move_copy_values(self, move_to_copy, new_date): + company_id = self.company_id.id + if not company_id: + company_id = self.sudo().warehouse_id and self.sudo().warehouse_id.company_id.id or self.sudo().picking_type_id.warehouse_id.company_id.id new_move_vals = { 'origin': move_to_copy.origin or move_to_copy.picking_id.name or "/", 'location_id': move_to_copy.location_dest_id.id, 'location_dest_id': self.location_id.id, 'date': new_date, 'date_expected': new_date, - 'company_id': self.company_id.id, + 'company_id': company_id, 'picking_id': False, 'picking_type_id': self.picking_type_id.id, 'propagate_cancel': self.propagate_cancel,