# -*- coding: utf-8 -*- # Copyright 2026 Nexa Systems Inc. # License OPL-1 (Odoo Proprietary License v1.0) # # When an invoice is posted, find the linked fp.job (via origin) and # update the portal job state to 'complete' + stamp invoice_ref. import logging from odoo import models _logger = logging.getLogger(__name__) class AccountMove(models.Model): _inherit = 'account.move' def action_post(self): result = super().action_post() for invoice in self.filtered( lambda m: m.move_type in ('out_invoice', 'out_refund') ): invoice._fp_link_to_job() return result def _fp_link_to_job(self): self.ensure_one() if not self.invoice_origin: return Job = self.env['fp.job'].sudo() # Walk SO -> fp.job SO = self.env['sale.order'].sudo() so = SO.search([('name', '=', self.invoice_origin)], limit=1) if not so: return job = Job.search([('sale_order_id', '=', so.id)], limit=1) if not job or not job.portal_job_id: return portal = job.portal_job_id if 'state' in portal._fields: portal.state = 'complete' if 'invoice_ref' in portal._fields: portal.invoice_ref = self.name _logger.info( 'Invoice %s linked to fp.job %s portal %s', self.name, job.name, portal.name, )