[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 <arm@odoo.com>
This commit is contained in:
Arnold Moyaux
2022-02-25 09:22:52 +00:00
parent c4158bec64
commit 05ae07c1a7
2 changed files with 30 additions and 6 deletions
+23 -6
View File
@@ -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
@@ -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