fix(fusion_plating): bug review fixes + progress/net-terms invoicing + formal CoC rebuild
Bug review fixes (found by code review + live QWeb error):
- report_fp_sale.xml: product_uom → product_uom_id (Odoo 19 renamed;
was raising KeyError during PDF render, blocking all sale-order prints)
- mrp_production.button_mark_done: add idempotency guard on delivery
auto-create (was duplicating on every re-close)
- fp.certificate._compute_batch_ids: use empty recordset instead of
False for Many2many computed fields
- fp_notification_template._collect_attachments: collapse attach_quotation
+ attach_sale_order into a single render so email doesn't double-attach
the same PDF
- fp.operator.certification: SQL unique on computed state was unreliable;
added explicit `revoked` boolean, made state pure-compute, replaced
SQL constraint with @api.constrains that checks active-only uniqueness;
has_active_cert now reads revoked + expires_date directly (no stale
stored state between nightly recomputes)
Two missing invoice strategies implemented + 1 pre-existing deposit bug fix:
- Progress Billing: new x_fc_progress_initial_percent field on sale.order;
_create_progress_initial_invoice bills the configured % on SO confirm
via down-payment wizard, _create_final_balance_invoice bills the
remainder on delivery
- Net Terms: no invoice on confirm; full invoice auto-created when
fusion.plating.delivery.action_mark_delivered fires
- Fix for deposit (pre-existing, silent): sale.advance.payment.inv
reads active_ids at wizard-create time, not on create_invoices();
context was being set on the wrong call, so every deposit attempt
raised "Expected singleton" and message-posted to chatter instead
of actually invoicing
- New fusion_plating_invoicing/models/fp_delivery.py hooks
action_mark_delivered to dispatch final invoice for progress/net_terms
- fp.direct.order.wizard + SO form surface the progress_initial_percent
field (conditional on strategy)
Report styling cleanup:
- Hide DISCOUNT column from sale + invoice landscape reports unless at
least one line has a non-zero discount; colspan auto-adjusts
- Replace hardcoded #0066a1 in all reports with company.primary_color
driven by doc.company_id → company → user.company_id fallback chain,
with #1d1f1e as ultimate fallback; new .fp-header-primary class
exposes the colour for inline section headers (CARGO DESCRIPTION,
PAYMENT DETAILS, OPERATOR SIGN-OFF, etc.) so they retint with the
company theme without template edits
Certificate of Conformance — formal ENTECH-style rebuild:
- New res.company fields: x_fc_owner_user_id (default signer, sig from
hr.employee.signature), x_fc_coc_signature_override (manual upload),
x_fc_{nadcap,as9100,cgp}_logo + _active toggles for accreditation
badges
- New res.config.settings section "Fusion Plating" exposing the above
as configurable blocks; manager-only menu under Configuration →
Fusion Plating Settings
- New fp.certificate fields: nc_quantity, customer_job_no,
contact_partner_id (child contact for Name / Email / Phone block)
- New report_coc_en + report_coc_fr templates (primary): custom header
(company contact | accreditations | company logo), bilingual labels
per variant, customer info block with customer logo, 3-column cert
info table, 6-column line-item table (Part # | Process | Customer
PO | Shipped | NC Qty | Customer Job No.), signature image + bordered
certification statement, footer "Fusion Plating by Nexa Systems"
- Legacy report_coc + report_coc_portrait kept for existing portal-job
bindings (no behaviour change)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
|
||||
{
|
||||
'name': 'Fusion Plating — Invoicing',
|
||||
'version': '19.0.1.0.0',
|
||||
'version': '19.0.2.0.0',
|
||||
'category': 'Manufacturing/Plating',
|
||||
'summary': 'Invoice strategy engine with deposit, progress billing, net terms, COD/prepay, and account holds.',
|
||||
'description': """
|
||||
@@ -29,6 +29,7 @@ Provides:
|
||||
'currency': 'CAD',
|
||||
'depends': [
|
||||
'fusion_plating_configurator',
|
||||
'fusion_plating_logistics',
|
||||
'sale_management',
|
||||
'account',
|
||||
],
|
||||
|
||||
@@ -7,3 +7,4 @@ from . import fp_invoice_strategy_default
|
||||
from . import res_partner
|
||||
from . import sale_order
|
||||
from . import account_move
|
||||
from . import fp_delivery
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
# -*- 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
|
||||
@@ -43,7 +43,6 @@ class SaleOrder(models.Model):
|
||||
) % (order.partner_id.name,
|
||||
order.partner_id.x_fc_account_hold_reason or 'No reason specified'))
|
||||
else:
|
||||
# Manager gets a warning in chatter but can proceed
|
||||
order.message_post(
|
||||
body=_(
|
||||
'Warning: Customer "%s" is on account hold (reason: %s). '
|
||||
@@ -54,35 +53,45 @@ class SaleOrder(models.Model):
|
||||
|
||||
res = super().action_confirm()
|
||||
|
||||
# --- Invoice strategy automation ---
|
||||
# --- Invoice strategy automation (on confirm) ---
|
||||
for order in self:
|
||||
strategy = order.x_fc_invoice_strategy
|
||||
if not strategy:
|
||||
continue
|
||||
|
||||
if strategy == 'deposit' and order.x_fc_deposit_percent:
|
||||
order._create_deposit_invoice()
|
||||
elif strategy == 'cod_prepay':
|
||||
order._create_full_invoice()
|
||||
elif strategy == 'progress' and order.x_fc_progress_initial_percent:
|
||||
order._create_progress_initial_invoice()
|
||||
# 'net_terms' — no action on confirm; invoiced when delivery is marked delivered
|
||||
|
||||
return res
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Strategy implementations
|
||||
# ------------------------------------------------------------------
|
||||
def _create_deposit_invoice(self):
|
||||
"""Create a deposit (down payment) invoice for the deposit percentage."""
|
||||
"""Deposit strategy: down-payment invoice for the deposit %."""
|
||||
self.ensure_one()
|
||||
percent = self.x_fc_deposit_percent
|
||||
if not percent or percent <= 0:
|
||||
return
|
||||
|
||||
try:
|
||||
# Use Odoo's standard down payment mechanism
|
||||
wizard = self.env['sale.advance.payment.inv'].create({
|
||||
# The wizard's sale_order_ids default reads active_ids AT CREATE
|
||||
# time — context must be set on .with_context(), not on the
|
||||
# subsequent create_invoices() call.
|
||||
wizard = self.env['sale.advance.payment.inv'].with_context(
|
||||
active_ids=self.ids,
|
||||
active_model='sale.order',
|
||||
active_id=self.id,
|
||||
).create({
|
||||
'advance_payment_method': 'percentage',
|
||||
'amount': percent,
|
||||
})
|
||||
wizard.with_context(active_ids=self.ids, active_model='sale.order').create_invoices()
|
||||
wizard.create_invoices()
|
||||
self.message_post(
|
||||
body=_('Deposit invoice (%.0f%%) created automatically — strategy: Deposit.') % percent,
|
||||
body=_('Deposit invoice (%.0f%%) created — strategy: Deposit.') % percent,
|
||||
)
|
||||
except Exception as e:
|
||||
_logger.warning('Failed to create deposit invoice for SO %s: %s', self.name, e)
|
||||
@@ -91,16 +100,83 @@ class SaleOrder(models.Model):
|
||||
)
|
||||
|
||||
def _create_full_invoice(self):
|
||||
"""Create a full invoice immediately (COD/Prepay strategy)."""
|
||||
"""COD / Prepay: invoice the entire order immediately."""
|
||||
self.ensure_one()
|
||||
try:
|
||||
invoices = self._create_invoices()
|
||||
if invoices:
|
||||
self.message_post(
|
||||
body=_('Full invoice created automatically — strategy: COD / Prepay.'),
|
||||
body=_('Full invoice created — strategy: COD / Prepay.'),
|
||||
)
|
||||
except Exception as e:
|
||||
_logger.warning('Failed to create COD invoice for SO %s: %s', self.name, e)
|
||||
self.message_post(
|
||||
body=_('Failed to auto-create invoice: %s. Create manually.') % str(e),
|
||||
)
|
||||
|
||||
def _create_progress_initial_invoice(self):
|
||||
"""Progress Billing — first invoice at SO confirm.
|
||||
|
||||
Uses Odoo's down-payment mechanism to bill the initial percentage.
|
||||
The remainder is billed on delivery via `_create_final_balance_invoice`.
|
||||
"""
|
||||
self.ensure_one()
|
||||
percent = self.x_fc_progress_initial_percent
|
||||
if not percent or percent <= 0:
|
||||
return
|
||||
try:
|
||||
wizard = self.env['sale.advance.payment.inv'].with_context(
|
||||
active_ids=self.ids,
|
||||
active_model='sale.order',
|
||||
active_id=self.id,
|
||||
).create({
|
||||
'advance_payment_method': 'percentage',
|
||||
'amount': percent,
|
||||
})
|
||||
wizard.create_invoices()
|
||||
self.message_post(
|
||||
body=_(
|
||||
'Progress invoice — initial %.0f%% created — strategy: Progress Billing. '
|
||||
'Final balance will be invoiced on delivery.'
|
||||
) % percent,
|
||||
)
|
||||
except Exception as e:
|
||||
_logger.warning('Failed progress-initial invoice for SO %s: %s', self.name, e)
|
||||
self.message_post(
|
||||
body=_('Failed to auto-create progress invoice: %s') % str(e),
|
||||
)
|
||||
|
||||
def _create_final_balance_invoice(self):
|
||||
"""Create the closing invoice for Progress Billing / Net Terms.
|
||||
|
||||
Called when delivery is marked delivered. Uses the standard
|
||||
`_create_invoices()` method which bills the remainder (net of any
|
||||
previously-posted down payments).
|
||||
"""
|
||||
self.ensure_one()
|
||||
if self.x_fc_final_invoice_id:
|
||||
return self.x_fc_final_invoice_id # Already invoiced — don't double
|
||||
if self.invoice_status == 'invoiced':
|
||||
return False # Nothing more to bill
|
||||
try:
|
||||
invoices = self._create_invoices(final=True)
|
||||
if invoices:
|
||||
self.x_fc_final_invoice_id = invoices[:1].id
|
||||
strategy_label = dict(
|
||||
self._fields['x_fc_invoice_strategy'].selection
|
||||
).get(self.x_fc_invoice_strategy, self.x_fc_invoice_strategy)
|
||||
self.message_post(
|
||||
body=_(
|
||||
'Final invoice created on delivery — strategy: %s.'
|
||||
) % strategy_label,
|
||||
)
|
||||
return invoices
|
||||
except Exception as e:
|
||||
_logger.warning('Failed final invoice for SO %s: %s', self.name, e)
|
||||
self.message_post(
|
||||
body=_(
|
||||
'Failed to auto-create final invoice: %s. '
|
||||
'Create manually from the SO.'
|
||||
) % str(e),
|
||||
)
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user