From 05ae07c1a77c8aea68c3a1e6086f82fb56561287 Mon Sep 17 00:00:00 2001 From: Arnold Moyaux Date: Wed, 2 Feb 2022 16:00:57 +0000 Subject: [PATCH] [FIX] (purchase_)mrp: Correct links with MO and PO Fine tuning of commit 6acfb2e It correctly merge the purchase.order in case of direct link and avoid to relaunch the procurement. It also correctly set the created_production_id if the demand come from a SO closes odoo/odoo#85301 X-original-commit: 9b6fa6c3db55daa3533c0d5cb13da7c98fc3e936 Signed-off-by: Arnold Moyaux --- addons/mrp/models/mrp_production.py | 29 ++++++++++++++++---- addons/purchase_mrp/models/mrp_production.py | 7 +++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/addons/mrp/models/mrp_production.py b/addons/mrp/models/mrp_production.py index 637b7387472..245f3f7b2b5 100644 --- a/addons/mrp/models/mrp_production.py +++ b/addons/mrp/models/mrp_production.py @@ -1965,9 +1965,7 @@ class MrpProduction(models.Model): else: user_id = self.env.user - origs = {} - for move in self.move_raw_ids: - origs.setdefault(move.bom_line_id.id, []).extend(move.move_orig_ids.ids) + origs = self._prepare_merge_orig_links() dests = {} for move in self.move_finished_ids: dests.setdefault(move.byproduct_id.id, []).extend(move.move_dest_ids.ids) @@ -1986,17 +1984,24 @@ class MrpProduction(models.Model): production._create_workorder() for move in production.move_raw_ids: - move.move_orig_ids = [Command.set(origs[move.bom_line_id.id])] + for field, vals in origs[move.bom_line_id.id].items(): + move[field] = vals for move in production.move_finished_ids: move.move_dest_ids = [Command.set(dests[move.byproduct_id.id])] - production.move_dest_ids = [Command.set(sum(list(dests.values()), []))] + + self.move_dest_ids.created_production_id = production.id self.procurement_group_id.stock_move_ids.group_id = production.procurement_group_id if 'confirmed' in self.mapped('state'): - production.action_confirm() + production.move_raw_ids._adjust_procure_method() + (production.move_raw_ids | production.move_finished_ids).write({'state': 'confirmed'}) + production.workorder_ids._action_confirm() + production.state = 'confirmed' self.with_context(skip_activity=True)._action_cancel() + for p in self: + p._message_log(body=_('This production has been merge in %s', production.display_name)) return { 'type': 'ir.actions.act_window', @@ -2133,3 +2138,15 @@ class MrpProduction(models.Model): raise UserError(_('You can only merge manufacturing with the same operation type')) # TODO explode and check no quantity has been edited return True + + def _prepare_merge_orig_links(self): + origs = defaultdict(dict) + for move in self.move_raw_ids: + if not move.move_orig_ids: + continue + origs[move.bom_line_id.id].setdefault('move_orig_ids', set()).update(move.move_orig_ids.ids) + for vals in origs.values(): + if not vals.get('move_orig_ids'): + continue + vals['move_orig_ids'] = [Command.set(vals['move_orig_ids'])] + return origs diff --git a/addons/purchase_mrp/models/mrp_production.py b/addons/purchase_mrp/models/mrp_production.py index c979c4adda3..ec09e681de6 100644 --- a/addons/purchase_mrp/models/mrp_production.py +++ b/addons/purchase_mrp/models/mrp_production.py @@ -43,3 +43,10 @@ class MrpProduction(models.Model): if not iterate_key and move_raw_id.created_purchase_line_id: iterate_key = 'created_purchase_line_id' return iterate_key + + def _prepare_merge_orig_links(self): + origs = super()._prepare_merge_orig_links() + for move in self.move_raw_ids: + if move.created_purchase_line_id: + origs[move.bom_line_id.id]['created_purchase_line_id'] = move.created_purchase_line_id + return origs