# -*- 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 AccountMove(models.Model): _inherit = 'account.move' def action_post(self): res = super().action_post() for move in self: if move.move_type == 'out_invoice' and move.partner_id: # Find linked SO so = False if move.invoice_origin: so = self.env['sale.order'].search( [('name', '=', move.invoice_origin)], limit=1, ) self._send_fp_notification( 'invoice_posted', move, move.partner_id, sale_order=so, ) return res def _send_fp_notification(self, trigger_event, record, partner, sale_order=None): """Send a notification email and log it.""" template = self.env['fp.notification.template'].search( [('trigger_event', '=', trigger_event), ('active', '=', True)], limit=1, ) if not template or not template.mail_template_id: return try: template.mail_template_id.send_mail(record.id, force_send=False) self.env['fp.notification.log'].create({ 'template_id': template.id, 'trigger_event': trigger_event, 'sale_order_id': sale_order.id if sale_order else False, 'partner_id': partner.id if partner else False, 'recipient_email': partner.email if partner else '', 'status': 'sent', }) except Exception as e: _logger.warning('FP notification failed (%s): %s', trigger_event, e) self.env['fp.notification.log'].create({ 'template_id': template.id, 'trigger_event': trigger_event, 'sale_order_id': sale_order.id if sale_order else False, 'partner_id': partner.id if partner else False, 'recipient_email': partner.email if partner else '', 'status': 'failed', 'error_message': str(e), })