Files
Odoo-Modules/fusion_plating/fusion_plating/models/fp_operator_certification.py
gsinghpal a623c6684d 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>
2026-04-17 01:18:22 -04:00

146 lines
5.5 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, _
class FpOperatorCertification(models.Model):
"""A signed-off training record that certifies an operator on a
specific process type.
Used to gate shop-floor work orders: an operator cannot start a
plating WO unless they hold a current (non-expired) certification
for that process.
"""
_name = 'fp.operator.certification'
_description = 'Fusion Plating — Operator Certification'
_inherit = ['mail.thread', 'mail.activity.mixin']
_order = 'employee_id, process_type_id'
name = fields.Char(
string='Certification Ref',
compute='_compute_name', store=True,
)
employee_id = fields.Many2one(
'hr.employee', string='Operator', required=True,
ondelete='cascade', tracking=True,
)
process_type_id = fields.Many2one(
'fusion.plating.process.type', string='Process Type',
required=True, ondelete='restrict', tracking=True,
)
issued_date = fields.Date(
string='Issued', default=fields.Date.today, required=True,
)
expires_date = fields.Date(
string='Expires',
help='Blank = no expiry. Set a date for re-certification tracking.',
)
issued_by_id = fields.Many2one(
'res.users', string='Certified By', default=lambda self: self.env.user,
)
training_record_attachment_id = fields.Many2one(
'ir.attachment', string='Training Record',
)
notes = fields.Text(string='Notes')
revoked = fields.Boolean(string='Revoked', tracking=True)
revoked_reason = fields.Text(string='Revoked Reason')
state = fields.Selection(
[('active', 'Active'),
('expired', 'Expired'),
('revoked', 'Revoked')],
string='Status',
compute='_compute_state', store=True, tracking=True,
# NOT readonly=False — this is purely derived from revoked + expires_date
# so the nightly recompute never fights with manual edits.
)
@api.depends('employee_id', 'process_type_id')
def _compute_name(self):
for rec in self:
if rec.employee_id and rec.process_type_id:
rec.name = f'{rec.employee_id.name} / {rec.process_type_id.name}'
else:
rec.name = ''
@api.depends('expires_date', 'revoked')
def _compute_state(self):
today = fields.Date.today()
for rec in self:
if rec.revoked:
rec.state = 'revoked'
elif rec.expires_date and rec.expires_date < today:
rec.state = 'expired'
else:
rec.state = 'active'
@api.constrains('employee_id', 'process_type_id', 'revoked', 'expires_date')
def _check_single_active(self):
"""At most one active certification per (employee, process_type)."""
today = fields.Date.today()
for rec in self:
if rec.revoked:
continue
if rec.expires_date and rec.expires_date < today:
continue
# This record is active — look for another active sibling
dupes = self.search_count([
('id', '!=', rec.id),
('employee_id', '=', rec.employee_id.id),
('process_type_id', '=', rec.process_type_id.id),
('revoked', '=', False),
'|', ('expires_date', '=', False),
('expires_date', '>=', today),
])
if dupes:
from odoo.exceptions import ValidationError
raise ValidationError(_(
'Operator %s already has an active certification for "%s". '
'Revoke or expire the existing one before adding another.'
) % (rec.employee_id.name, rec.process_type_id.name))
def action_revoke(self):
for rec in self:
rec.revoked = True
rec.message_post(body=_('Certification revoked.'))
@api.model
def has_active_cert(self, employee_id, process_type_id):
"""Utility — True if this employee holds a current certification.
Checks revoked + expires_date directly instead of the computed
`state` column, so even a certification that expired yesterday
is caught immediately (no wait for nightly recompute).
"""
if not employee_id or not process_type_id:
return False
today = fields.Date.today()
return bool(self.search_count([
('employee_id', '=', employee_id),
('process_type_id', '=', process_type_id),
('revoked', '=', False),
'|', ('expires_date', '=', False),
('expires_date', '>=', today),
]))
class HrEmployee(models.Model):
_inherit = 'hr.employee'
x_fc_certification_ids = fields.One2many(
'fp.operator.certification', 'employee_id',
string='Plating Certifications',
)
x_fc_certified_process_ids = fields.Many2many(
'fusion.plating.process.type', compute='_compute_certified_processes',
string='Certified Processes',
)
@api.depends('x_fc_certification_ids.state', 'x_fc_certification_ids.process_type_id')
def _compute_certified_processes(self):
for emp in self:
active = emp.x_fc_certification_ids.filtered(lambda c: c.state == 'active')
emp.x_fc_certified_process_ids = active.mapped('process_type_id')