# -*- coding: utf-8 -*- # Copyright 2026 Nexa Systems Inc. # License OPL-1 (Odoo Proprietary License v1.0) # Part of the Fusion Plating product family. import logging from odoo import models _logger = logging.getLogger(__name__) class FpDelivery(models.Model): """Fire the 'final balance' invoice for Progress Billing / Net Terms when a delivery is marked delivered. """ _inherit = 'fusion.plating.delivery' def action_mark_delivered(self): res = super().action_mark_delivered() SaleOrder = self.env['sale.order'] MrpProduction = self.env['mrp.production'] for delivery in self: # Resolve the sale order via delivery.job_ref → MO.name → MO.origin so = False if delivery.job_ref: mo = MrpProduction.search( [('name', '=', delivery.job_ref)], limit=1, ) if mo and mo.origin: so = SaleOrder.search( [('name', '=', mo.origin)], limit=1, ) if not so: # Fallback: find by partner + recently-confirmed with matching strategy continue strategy = so.x_fc_invoice_strategy if strategy not in ('progress', 'net_terms'): continue # Skip if already billed in full if so.invoice_status == 'invoiced': continue so._create_final_balance_invoice() return res