mrp_shared_operation/models/mrp_production.py
2026-02-11 11:56:58 +07:00

62 lines
3.0 KiB
Python

from odoo import fields, models, api, Command
class MrpProduction(models.Model):
_inherit = 'mrp.production'
@api.depends('bom_id', 'product_id', 'product_qty', 'product_uom_id', 'never_product_template_attribute_value_ids')
def _compute_workorder_ids(self):
"""
Override to include shared operations from bom_id.shared_operation_ids.
This reuses the original logic but injects the shared operations effectively into the BOM
structure considered for work orders.
"""
super()._compute_workorder_ids()
for production in self:
if production.state != 'draft' or not production.bom_id or not production.bom_id.shared_operation_ids:
continue
# Identify shared operations that are not already in workorder_ids
# The super method might not pick them up because they are not directly in bom.operation_ids if they are only in shared_operation_ids
# However, we need to be careful. If shared_operation_ids are just a link, we need to treat them as if they are part of the BOM.
# Strategy:
# 1. Inspect existing workorders.
# 2. Iterate shared operations.
# 3. Create workorders for missing shared operations.
workorders_values = []
deleted_workorders_ids = [] # We probably don't want to delete what super created, only add.
# We need to respect the same filtering logic (skip if not applicable to variant)
# The shared operations are mrp.routing.workcenter records.
# They have 'bom_product_template_attribute_value_ids'.
product = production.product_id
for operation in production.bom_id.shared_operation_ids:
if operation._skip_operation_line(product, production.never_product_template_attribute_value_ids):
continue
# Check if this operation is already present in workorders
# Note: The same operation record might be used in multiple BOMs.
# If it's already there (e.g. from super if we modify BOM structure implicitly), we skip.
# But here, shared_operation_ids are likely NOT in bom.operation_ids (One2many inverse of bom_id).
existing_wo = production.workorder_ids.filtered(lambda wo: wo.operation_id == operation)
if existing_wo:
continue
workorders_values += [{
'name': operation.name,
'production_id': production.id,
'workcenter_id': operation.workcenter_id.id,
'product_uom_id': production.product_uom_id.id,
'operation_id': operation.id,
'state': 'ready',
}]
if workorders_values:
production.workorder_ids = [Command.create(vals) for vals in workorders_values]