diff --git a/fusion_plating/fusion_plating_notifications/data/fp_notification_template_data.xml b/fusion_plating/fusion_plating_notifications/data/fp_notification_template_data.xml index 76cad532..867c45d2 100644 --- a/fusion_plating/fusion_plating_notifications/data/fp_notification_template_data.xml +++ b/fusion_plating/fusion_plating_notifications/data/fp_notification_template_data.xml @@ -1,12 +1,17 @@ + + Quotation Sent quote_sent - @@ -14,7 +19,6 @@ so_confirmed - @@ -45,7 +49,6 @@ invoice_posted - diff --git a/fusion_plating/fusion_plating_notifications/hooks.py b/fusion_plating/fusion_plating_notifications/hooks.py index 663eff85..799d2424 100644 --- a/fusion_plating/fusion_plating_notifications/hooks.py +++ b/fusion_plating/fusion_plating_notifications/hooks.py @@ -9,15 +9,19 @@ _logger = logging.getLogger(__name__) def post_init_hook(env): - """Force-populate report_template_ids on the mail templates. + """Wire reports onto mail templates + clean up double-attach flags. - The mail-template XML records are tagged noupdate="1" so - customer-edited templates aren't overwritten on module update. - That means report_template_ids added to the XML AFTER the - templates were first installed won't propagate via the usual - -u reload. This hook wires the reports onto the branded - templates on install/upgrade and is safe to re-run. + Both the mail-template and fp.notification.template XML records + are tagged noupdate="1" so user customizations aren't overwritten + on module update. This hook runs once to: + + 1. Point report_template_ids at the current branded reports. + 2. Turn off attach_quotation / attach_sale_order / attach_invoice + flags on fp.notification.template — those would duplicate the + PDF that's now auto-attached via the mail template's + report_template_ids. """ + # 1. Attach correct reports to mail templates _apply_report_template( env, 'fusion_plating_notifications.fp_mail_template_quote_sent', @@ -34,6 +38,28 @@ def post_init_hook(env): 'fusion_plating_reports.action_report_fp_invoice_portrait', ) + # 2. Clear duplicate-attach flags on the auto-dispatch records. + # The PDF is already auto-attached by the mail.template's + # report_template_ids; leaving these flags on produces 2 PDFs. + for xmlid, flags in [ + ('fusion_plating_notifications.fp_notif_quote_sent', + {'attach_quotation': False}), + ('fusion_plating_notifications.fp_notif_so_confirmed', + {'attach_sale_order': False}), + ('fusion_plating_notifications.fp_notif_invoice_posted', + {'attach_invoice': False}), + ]: + rec = env.ref(xmlid, raise_if_not_found=False) + if not rec: + continue + needs_write = any(rec[k] != v for k, v in flags.items()) + if needs_write: + rec.write(flags) + _logger.info( + 'fusion_plating_notifications: cleared duplicate-attach ' + 'flags on %s (%s)', xmlid, flags, + ) + def _apply_report_template(env, mail_template_xmlid, report_xmlid): """Replace the template's report_template_ids with exactly [report]. diff --git a/fusion_plating/fusion_plating_notifications/models/sale_order.py b/fusion_plating/fusion_plating_notifications/models/sale_order.py index e6943eb9..3e154376 100644 --- a/fusion_plating/fusion_plating_notifications/models/sale_order.py +++ b/fusion_plating/fusion_plating_notifications/models/sale_order.py @@ -43,15 +43,18 @@ class SaleOrder(models.Model): ) return fp_tpl or super()._find_mail_template() - def action_quotation_send(self): - """Fire the quote_sent trigger when a quotation is emailed.""" - res = super().action_quotation_send() - Dispatch = self.env['fp.notification.template'] - for order in self: - Dispatch._dispatch( - 'quote_sent', order, order.partner_id, sale_order=order, - ) - return res + # NOTE: we intentionally do NOT override action_quotation_send to + # fire the quote_sent trigger. action_quotation_send returns a + # compose-dialog action synchronously — if we dispatch here the + # email is already sent by the time the user sees the dialog and + # decides to Cancel. + # + # The Send button itself IS the manual-send path; Odoo's compose + # dialog (populated with our FP: Quotation Sent template via the + # _find_mail_template override) handles the send-or-cancel choice + # cleanly. If an auto-quote-sent notification is ever desired, + # wire it from a cron job that scans SOs that just transitioned + # from draft → sent, not from the button handler. def action_confirm(self): res = super().action_confirm()