Files
Odoo-Modules/fusion_plating/fusion_plating_invoicing/models/account_move.py
gsinghpal c118b7c6b5 feat(plating): close compliance gaps 7-9 — NCR + CAPA + discharge + invoice ref
**7a. NCR close gate** (fusion.plating.ncr.action_close)
Block close unless these are filled in:
  • Description (what happened)
  • Containment Actions (immediate response)
  • Root Cause (why it happened)
  • Disposition (use-as-is / rework / scrap / RTV decision)

A closed NCR without these is useless for AS9100 audits — it's
the entire point of an NCR to document what went wrong, why, and
how we responded. Empty-HTML strings like "<p><br></p>" are
detected as empty too.

**7b. CAPA close gate** (fusion.plating.capa.action_close)
Block close unless:
  • Root Cause Analysis filled in
  • Action Plan filled in
  • Verification (date + verifier) recorded
  • Effectiveness Notes filled when CAPA was marked Not Effective

AS9100 §10.2 / Nadcap require evidence of root-cause analysis,
the corrective/preventive action plan, AND that effectiveness
was verified before the loop is closed.

**8. Invoice ref defensive default** (account.move.create)
Auto-fills `ref` from the source SO's client_order_ref or
x_fc_po_number when the invoice is created with invoice_origin set
but no ref. Already populated on the SO confirm path; this catches
manually-created invoices that would otherwise miss it. Customer
AP teams reject invoices that don't quote their PO# back.

**9. Discharge sample close gate** (fusion.plating.discharge.sample.action_close)
Block close unless:
  • Lab Report # set
  • Results Received Date set
  • At least one parameter reading on file
  • Lab certificate/report attached

Without lab evidence the record fails any environmental compliance
audit — the whole point is to document the test was performed and
what the lab said.

**Simulator** (scripts/fp_e2e_workforce.py)
Adds 4 new negative tests (Test 8-11), all wrapped in savepoints:
  ✓ Test 8 : NCR close without RC/containment/disposition → blocked
  ✓ Test 9 : CAPA close without analysis/plan/verification → blocked
  ✓ Test 10: Discharge sample close without lab evidence → blocked
  ✓ Test 11: Invoice ref auto-fills from SO.client_order_ref → asserted

**Final E2E**: 52 PASS / 2 WARN / 0 FAIL out of 54 checks.
Both remaining WARNs are expected (bake-window auto-create,
first-piece gate — coating-driven, this coating doesn't trigger them).

11 negative tests in total now, every gate fires when triggered.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 10:35:27 -04:00

77 lines
3.6 KiB
Python

# -*- coding: utf-8 -*-
# Copyright 2026 Nexa Systems Inc.
# License OPL-1 (Odoo Proprietary License v1.0)
# Part of the Fusion Plating product family.
from odoo import api, models, _
from odoo.exceptions import UserError
class AccountMove(models.Model):
_inherit = 'account.move'
@api.model_create_multi
def create(self, vals_list):
"""Auto-inherit payment terms + customer PO# at creation time.
Two defensive defaults so newly-created invoices come out
compliant out of the box:
1. **invoice_payment_term_id** — pulled from the customer's
property_payment_term_id (Net-30, COD, etc.). Without this
the due date silently becomes "immediate", wrong for B2B.
2. **ref** (customer reference / PO#) — pulled from the source
sale order's client_order_ref or x_fc_po_number. Customer
AP teams reject invoices that don't quote their PO# back.
We already populate this on the SO confirm path, but a
manually-created invoice would miss it without this default.
"""
Partner = self.env['res.partner']
SO = self.env['sale.order']
for vals in vals_list:
if vals.get('move_type') in ('out_invoice', 'out_refund'):
if not vals.get('invoice_payment_term_id') and vals.get('partner_id'):
partner = Partner.browse(vals['partner_id'])
if partner.property_payment_term_id:
vals['invoice_payment_term_id'] = partner.property_payment_term_id.id
# Defensive PO#: invoice_origin links to the SO; pull the
# customer ref from there if the caller didn't pass one.
if not vals.get('ref') and vals.get('invoice_origin'):
so = SO.search([('name', '=', vals['invoice_origin'])], limit=1)
if so:
vals['ref'] = (
so.client_order_ref
or (so.x_fc_po_number if 'x_fc_po_number' in so._fields else False)
or False
)
return super().create(vals_list)
def action_post(self):
"""Block post when:
• customer is on account hold (existing rule), or
• the invoice has no payment term (auto-fill missed it AND
partner had no default — accountant must pick one).
"""
for move in self:
if move.move_type in ('out_invoice', 'out_refund') and move.partner_id:
if move.partner_id.x_fc_account_hold:
is_manager = self.env.user.has_group(
'fusion_plating.group_fusion_plating_manager'
)
if not is_manager:
raise UserError(_(
'Cannot post invoice — customer "%s" is on account hold.\n'
'Reason: %s\n\n'
'Contact a manager to override.'
) % (move.partner_id.name,
move.partner_id.x_fc_account_hold_reason or 'No reason specified'))
if not move.invoice_payment_term_id:
raise UserError(_(
'Cannot post invoice "%s" — no payment terms set.\n\n'
'Pick payment terms (Net-30, COD, etc.) on the invoice, '
'or set a default on the customer "%s" so future '
'invoices inherit it automatically.'
) % (move.name or move.display_name, move.partner_id.name))
return super().action_post()