# -*- coding: utf-8 -*- # Copyright 2026 Nexa Systems Inc. # License OPL-1 (Odoo Proprietary License v1.0) # Part of the Fusion Plating product family. from odoo import api, fields, models class FpCertificate(models.Model): """Add Specification linkage + auto-fill spec_reference from it. Lives in fusion_plating_quality because customer.spec lives here. Quality already depends on certificates, so the inverse direction works. """ _inherit = 'fp.certificate' customer_spec_id = fields.Many2one( 'fusion.plating.customer.spec', string='Specification', help='Snapshot of the specification the cert was issued against. ' 'Drives the spec_reference printed on the CoC.', ) @api.model_create_multi def create(self, vals_list): """Auto-fill spec_reference from the SO line's customer_spec_id. Resolution order (first match wins): 1. Explicit spec_reference passed in vals. 2. customer_spec_id (this field) → format "code Rev rev". 3. SO line x_fc_customer_spec_id (with print_on_cert=True). 4. Existing legacy fall-back lives in the parent module (reads x_fc_coating_config_id.spec_reference). Untouched. """ SaleOrder = self.env['sale.order'] for vals in vals_list: if vals.get('spec_reference'): continue spec = False # 2. Explicit spec on the cert. if vals.get('customer_spec_id'): spec = self.env['fusion.plating.customer.spec'].browse( vals['customer_spec_id'], ).exists() # 3. SO line's spec. if not spec and vals.get('sale_order_id'): so = SaleOrder.browse(vals['sale_order_id']) if 'x_fc_customer_spec_id' in so.order_line._fields: spec = so.order_line.mapped( 'x_fc_customer_spec_id', ).filtered('print_on_cert')[:1] if spec and not vals.get('customer_spec_id'): vals['customer_spec_id'] = spec.id if spec: ref = spec.code or '' if spec.revision: ref = f'{ref} Rev {spec.revision}' if ref else f'Rev {spec.revision}' if ref: vals['spec_reference'] = ref return super().create(vals_list)