Files
gsinghpal 95cb73d91a feat(numbering): wire NCR, CAPA, Hold, RMA into parent-numbered mixin
Hold derives parent via job_id.sale_order_id; RMA via sale_order_id
directly — both get HOLD-<parent> / RMA-<parent> names. NCR and CAPA
have no SO link in core, so they fall back to their legacy sequences
(NCR/YYYY/NNN, CAPA/YYYY/NNN); future modules can override the
_fp_parent_sale_order hook to enable parent naming.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 13:36:29 -04:00

233 lines
8.0 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', 'fp.parent.numbered.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)
# ------------------------------------------------------------------
# Parent-numbered mixin hooks
# CAPAs reach SO via ncr_id → fp.job link if present (jobless NCRs
# fall back to legacy sequence).
# ------------------------------------------------------------------
def _fp_parent_sale_order(self):
# CAPA usually flows from an NCR. If the NCR has a job-back-link
# (added by future modules), we can reach SO through it. For now
# there's no link in core — falls back to legacy seq.
return self.env['sale.order']
def _fp_name_prefix(self):
return 'CAPA'
def _fp_parent_counter_field(self):
return 'x_fc_pn_capa_count'
@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'] = 'New'
records = super().create(vals_list)
for rec in records:
if rec.name and rec.name != 'New':
continue
if not rec._fp_assign_parent_name():
seq = self.env['ir.sequence'].next_by_code('fusion.plating.capa') or 'New'
self.env.cr.execute(
"UPDATE fusion_plating_capa SET name = %s WHERE id = %s",
(seq, rec.id),
)
rec.invalidate_recordset(['name'])
return records
@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'})