Files
Odoo-Modules/fusion_plating/fusion_plating_quality/models/fp_capa.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

205 lines
6.8 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, fields, models
from odoo.exceptions import UserError
class FpCapa(models.Model):
"""Corrective and Preventive Action.
A CAPA carries an issue from "we found a problem" all the way to
"we proved the fix worked". Each CAPA has an owner, a due date, an
action plan, and an effectiveness verification step.
"""
_name = 'fusion.plating.capa'
_description = 'Fusion Plating — Corrective / Preventive Action'
_inherit = ['mail.thread', 'mail.activity.mixin']
_order = 'due_date asc, id desc'
name = fields.Char(
string='Reference',
required=True,
copy=False,
readonly=True,
default=lambda self: self._default_name(),
tracking=True,
)
state = fields.Selection(
[
('draft', 'Draft'),
('analysis', 'Analysis'),
('implementation', 'Implementation'),
('verification', 'Verification'),
('effective', 'Effective'),
('not_effective', 'Not Effective'),
('closed', 'Closed'),
],
string='Status',
default='draft',
required=True,
tracking=True,
)
type = fields.Selection(
[
('corrective', 'Corrective'),
('preventive', 'Preventive'),
],
string='Type',
default='corrective',
required=True,
tracking=True,
)
ncr_id = fields.Many2one(
'fusion.plating.ncr',
string='Source NCR',
ondelete='set null',
tracking=True,
)
facility_id = fields.Many2one(
'fusion.plating.facility',
string='Facility',
related='ncr_id.facility_id',
store=True,
readonly=False,
)
company_id = fields.Many2one(
'res.company',
related='facility_id.company_id',
store=True,
readonly=True,
)
description = fields.Html(string='Description')
root_cause_analysis = fields.Html(
string='Root Cause Analysis',
help='Use 5 Whys, fishbone, or any structured method.',
)
action_plan = fields.Html(string='Action Plan')
owner_id = fields.Many2one(
'res.users',
string='Owner',
required=True,
default=lambda self: self.env.user,
tracking=True,
)
due_date = fields.Date(string='Due Date', tracking=True)
verification_date = fields.Date(string='Verification Date', tracking=True)
verification_by_id = fields.Many2one(
'res.users',
string='Verified By',
tracking=True,
)
is_effective = fields.Boolean(string='Effective', tracking=True)
effectiveness_notes = fields.Html(string='Effectiveness Notes')
is_overdue = fields.Boolean(
string='Overdue',
compute='_compute_is_overdue',
search='_search_is_overdue',
)
active = fields.Boolean(default=True)
@api.model
def _default_name(self):
seq = self.env['ir.sequence'].next_by_code('fusion.plating.capa')
return seq or '/'
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
if not vals.get('name') or vals.get('name') == '/':
vals['name'] = self._default_name()
return super().create(vals_list)
@api.depends('due_date', 'state')
def _compute_is_overdue(self):
today = fields.Date.context_today(self)
for rec in self:
rec.is_overdue = bool(
rec.due_date
and rec.state not in ('effective', 'closed')
and rec.due_date < today
)
def _search_is_overdue(self, operator, value):
today = fields.Date.context_today(self)
if (operator == '=' and value) or (operator == '!=' and not value):
return [
('due_date', '<', today),
('state', 'not in', ['effective', 'closed']),
]
return [
'|',
('due_date', '>=', today),
('state', 'in', ['effective', 'closed']),
]
def action_start_analysis(self):
self.write({'state': 'analysis'})
def action_start_implementation(self):
self.write({'state': 'implementation'})
def action_start_verification(self):
self.write({'state': 'verification'})
def action_mark_effective(self):
self.write({
'state': 'effective',
'is_effective': True,
'verification_date': fields.Date.context_today(self),
'verification_by_id': self.env.user.id,
})
def action_mark_not_effective(self):
self.write({
'state': 'not_effective',
'is_effective': False,
'verification_date': fields.Date.context_today(self),
'verification_by_id': self.env.user.id,
})
def action_close(self):
"""Block close unless root_cause + action_plan + verification are set.
A CAPA without these is just an open ticket — the AS9100 §10.2
/ Nadcap loop requires evidence of the root cause analysis,
the corrective/preventive action plan, AND that effectiveness
was verified before the loop is closed.
"""
for rec in self:
missing = []
def is_empty_html(val):
if not val:
return True
s = str(val).replace('<p>', '').replace('</p>', '')
s = s.replace('<br>', '').replace('<br/>', '').strip()
return not s
if is_empty_html(rec.root_cause_analysis):
missing.append(_('Root Cause Analysis'))
if is_empty_html(rec.action_plan):
missing.append(_('Action Plan'))
if not rec.verification_date or not rec.verification_by_id:
missing.append(_('Verification (date + verifier)'))
if rec.is_effective is False and is_empty_html(rec.effectiveness_notes):
# If marked not-effective, demand a note explaining the
# follow-up plan — otherwise the loop never actually closes.
missing.append(_('Effectiveness Notes (required when "Not Effective")'))
if missing:
raise UserError(_(
'Cannot close CAPA "%(name)s" — these fields must be '
'filled in first:\n%(fields)s\n\n'
'A CAPA without root cause + action plan + verified '
'effectiveness fails AS9100 §10.2 / Nadcap on audit.'
) % {
'name': rec.name or rec.display_name,
'fields': '\n'.join(missing),
})
self.write({'state': 'closed'})
def action_reset_to_draft(self):
self.write({'state': 'draft'})