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

241 lines
7.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 FpNcr(models.Model):
"""Non-Conformance Report.
The NCR is the entry point of the Fusion Plating QMS. Anything that
falls outside of spec — a chemistry deviation, a customer return, an
inspection failure, an audit observation — is opened as an NCR and
walked through containment, disposition, and closure.
"""
_name = 'fusion.plating.ncr'
_description = 'Fusion Plating — Non-Conformance Report'
_inherit = ['mail.thread', 'mail.activity.mixin', 'fp.parent.numbered.mixin']
_order = 'reported_date desc, 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'),
('open', 'Open'),
('containment', 'Containment'),
('disposition', 'Disposition'),
('closed', 'Closed'),
],
string='Status',
default='draft',
required=True,
tracking=True,
)
facility_id = fields.Many2one(
'fusion.plating.facility',
string='Facility',
required=True,
tracking=True,
)
company_id = fields.Many2one(
'res.company',
related='facility_id.company_id',
store=True,
readonly=True,
)
reported_by_id = fields.Many2one(
'res.users',
string='Reported By',
default=lambda self: self.env.user,
tracking=True,
)
reported_date = fields.Datetime(
string='Reported On',
default=lambda self: fields.Datetime.now(),
tracking=True,
)
closed_date = fields.Datetime(
string='Closed On',
readonly=True,
tracking=True,
)
source = fields.Selection(
[
('shop_floor', 'Shop Floor'),
('inspection', 'Inspection'),
('customer', 'Customer'),
('audit', 'Audit'),
('supplier', 'Supplier'),
('other', 'Other'),
],
string='Source',
default='shop_floor',
tracking=True,
)
severity = fields.Selection(
[
('low', 'Low'),
('medium', 'Medium'),
('high', 'High'),
('critical', 'Critical'),
],
string='Severity',
default='medium',
tracking=True,
)
part_ref = fields.Char(string='Part / Lot')
quantity_affected = fields.Float(string='Quantity Affected')
description = fields.Html(string='Description')
root_cause = fields.Html(string='Root Cause')
containment = fields.Html(string='Containment Actions')
disposition = fields.Selection(
[
('use_as_is', 'Use as Is'),
('rework', 'Rework'),
('scrap', 'Scrap'),
('return_to_customer', 'Return to Customer'),
('pending', 'Pending'),
],
string='Disposition',
default='pending',
tracking=True,
)
bath_id = fields.Many2one(
'fusion.plating.bath',
string='Bath',
help='If the non-conformance was caused by a specific chemistry bath.',
)
customer_partner_id = fields.Many2one(
'res.partner',
string='Customer',
)
capa_ids = fields.One2many(
'fusion.plating.capa',
'ncr_id',
string='CAPAs',
)
capa_count = fields.Integer(
string='# CAPAs',
compute='_compute_capa_count',
)
active = fields.Boolean(default=True)
# ------------------------------------------------------------------
# Parent-numbered mixin hooks
# NCRs don't have a direct SO/job link in core yet — falls back to
# legacy fusion.plating.ncr sequence. When a future module adds a
# link, it can override _fp_parent_sale_order to enable parent
# naming retroactively without further changes here.
# ------------------------------------------------------------------
def _fp_parent_sale_order(self):
return self.env['sale.order']
def _fp_name_prefix(self):
return 'NCR'
def _fp_parent_counter_field(self):
return 'x_fc_pn_ncr_count'
@api.model
def _default_name(self):
seq = self.env['ir.sequence'].next_by_code('fusion.plating.ncr')
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.ncr') or 'New'
self.env.cr.execute(
"UPDATE fusion_plating_ncr SET name = %s WHERE id = %s",
(seq, rec.id),
)
rec.invalidate_recordset(['name'])
return records
@api.depends('capa_ids')
def _compute_capa_count(self):
for rec in self:
rec.capa_count = len(rec.capa_ids)
def action_open(self):
self.write({'state': 'open'})
def action_containment(self):
self.write({'state': 'containment'})
def action_disposition(self):
self.write({'state': 'disposition'})
def action_close(self):
"""Block close unless root_cause + containment + disposition are set.
A closed NCR without these three is useless for AS9100 audits:
the whole point of the NCR is to document what went wrong
(containment), why (root_cause), and what we decided to do
with the affected parts (disposition).
"""
for rec in self:
missing = []
# Strip HTML-empty strings like "<p><br></p>" before checking
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.description):
missing.append(_('Description'))
if is_empty_html(rec.containment):
missing.append(_('Containment Actions'))
if is_empty_html(rec.root_cause):
missing.append(_('Root Cause'))
if not rec.disposition:
missing.append(_('Disposition (use-as-is / rework / scrap / RTV)'))
if missing:
raise UserError(_(
'Cannot close NCR "%(name)s" — these fields must be '
'filled in first:\n%(fields)s\n\n'
'AS9100 / Nadcap auditors will reject a closed NCR '
'that doesn\'t document what happened, why, and how '
'we responded.'
) % {
'name': rec.name or rec.display_name,
'fields': '\n'.join(missing),
})
self.write({
'state': 'closed',
'closed_date': fields.Datetime.now(),
})
def action_reset_to_draft(self):
self.write({'state': 'draft', 'closed_date': False})
def action_view_capas(self):
self.ensure_one()
return {
'name': 'CAPAs',
'type': 'ir.actions.act_window',
'res_model': 'fusion.plating.capa',
'view_mode': 'list,form',
'domain': [('ncr_id', '=', self.id)],
'context': {'default_ncr_id': self.id},
}