Add API Q1 CAPA: root cause, corrective action gate, effectiveness verification

Implements tester feedback against API Q1 §5.9.1.2 / §6.4.2:

- Root Cause field + 6M Root Cause Category lookup (Man/Machine/Method/
  Material/Measurement/Environment), separate from Deviation Detail/Category
- "Corrective Action Required?" Yes/No gate on every NCR with a required
  justification
- Corrective action plan with owner + due date; owner is notified by email
- Effectiveness verification (result, notes, server-stamped verifier/date)
  required before an NCR can close when corrective action is required —
  costing returns 409 listing the missing pieces
- Recurring-issue flag with bidirectional NCR-to-NCR links; prior NCRs show
  a warning when later NCRs reference them
- Dashboard metrics: % root cause completed, % CAPA verified effective,
  avg CAPA close time, overdue CAPA count, NCRs by root cause category
- CAPA section in the NCR detail UI, printable PDF, CSV export, and the
  vw_ncr_full Power BI view; admin list manager for root cause categories
- Migrations 0003 (schema + seeded 6M lookup) and 0004 (view refresh);
  demo seed data exercises every metric

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
ang3l12
2026-08-04 13:49:08 -06:00
parent 8d48f774bb
commit c1cd1bc9df
28 changed files with 1830 additions and 61 deletions

View File

@@ -21,3 +21,4 @@ class LookupPatchIn(BaseModel):
class LookupsOut(BaseModel):
departments: list[NamedLookupOut]
deviation_categories: list[NamedLookupOut]
root_cause_categories: list[NamedLookupOut]

View File

@@ -1,3 +1,4 @@
from datetime import date
from decimal import Decimal
from typing import Annotated, Literal
@@ -43,6 +44,27 @@ class InspectionIn(BaseModel):
qc_closed: bool = False
class CapaIn(BaseModel):
"""Corrective & Preventive Action section (API Q1 §5.9.1.2 / §6.4.2).
Only fields present in the request body are updated (exclude_unset), so
partial saves from different roles never clobber each other's entries.
"""
root_cause: str | None = Field(default=None, max_length=20000)
root_cause_category_id: int | None = None
corrective_action_required: bool | None = None
corrective_action_justification: str | None = Field(default=None, max_length=2000)
corrective_action_plan: str | None = Field(default=None, max_length=20000)
corrective_action_owner_id: int | None = None
corrective_action_due_date: date | None = None
effectiveness_result: Literal["effective", "not_effective"] | None = None
effectiveness_notes: str | None = Field(default=None, max_length=20000)
is_recurring: bool | None = None
# Replaces the full set of linked prior NCRs when present.
related_ncr_ids: list[int] | None = None
class CostingIn(BaseModel):
labor_cost: Money
material_cost: Money
@@ -91,6 +113,16 @@ class JobInfoOut(AppModel):
source: str
class NcrLinkOut(BaseModel):
"""Light projection of a linked NCR (recurring-issue tracking)."""
id: int
ncr_number: str
job_number: str
stage: str
stage_label: str
class NcrListItem(BaseModel):
id: int
ncr_number: str
@@ -146,6 +178,23 @@ class NcrDetailOut(BaseModel):
qc_closed_at: UTCDateTime | None
qc_closed_by: UserRef | None
root_cause: str | None
root_cause_category: str | None
root_cause_category_id: int | None
corrective_action_required: bool | None
corrective_action_justification: str | None
corrective_action_plan: str | None
corrective_action_owner: UserRef | None
corrective_action_due_date: date | None
corrective_action_opened_at: UTCDateTime | None
effectiveness_result: str | None
effectiveness_notes: str | None
effectiveness_verified_at: UTCDateTime | None
effectiveness_verified_by: UserRef | None
is_recurring: bool
related_ncrs: list[NcrLinkOut]
referenced_by: list[NcrLinkOut]
labor_cost: Decimal | None
material_cost: Decimal | None
service_cost: Decimal | None

View File

@@ -44,6 +44,12 @@ class ReportsSummaryOut(BaseModel):
open_ncrs: int
closed_ncrs: int
total_cost: Decimal
# ── CAPA metrics (API Q1 §6.4.2) ─────────────────────────────────────────
root_cause_pct: float | None # % of NCRs with a completed root cause
effectiveness_verified_pct: float | None # % of CA-required NCRs verified effective
avg_capa_close_days: float | None # CA opened → verified effective
overdue_capa_count: int # CA required, past due, not yet verified effective
by_root_cause_category: list[CountByName]
by_department: list[CountByName]
by_category: list[CountByName]
by_month: list[CountByMonth]