# -*- 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 models class AccountMove(models.Model): """Extend invoice to auto-complete portal job when posted. GAP 7: Invoice posted → find portal job for same customer/SO → set state "complete" + invoice_ref. """ _inherit = 'account.move' def action_post(self): """Override to cascade invoice posting to portal job completion.""" res = super().action_post() PortalJob = self.env.get('fusion.plating.portal.job') if PortalJob is None: return res for invoice in self: if invoice.move_type != 'out_invoice': continue # Find portal jobs for this customer that are shipped but not complete # Match by SO origin from the invoice lines origin = invoice.invoice_origin or '' jobs = PortalJob.browse() if origin: # Try to find MO linked to this SO, then its portal job mos = self.env['mrp.production'].search( [('origin', '=', origin)], ) for mo in mos: if mo.x_fc_portal_job_id and mo.x_fc_portal_job_id.state == 'shipped': jobs |= mo.x_fc_portal_job_id # Fallback: find shipped jobs for same partner if not jobs: jobs = PortalJob.search([ ('partner_id', '=', invoice.partner_id.id), ('state', '=', 'shipped'), ('invoice_ref', '=', False), ], limit=1) for job in jobs: job.write({ 'state': 'complete', 'invoice_ref': invoice.name, }) job.message_post( body='Invoice %s posted — job complete.' % invoice.name, ) return res