Files
jorgecuadros-platform/apps/web/src/components/ChildCollection.tsx
T
rmancinasandClaude Opus 5 48e01ddd21
Build and Push Images / Build jorgecuadros-api (push) Successful in 2m19s
Build and Push Images / Build jorgecuadros-web (push) Successful in 2m1s
feat(policies): capture the full premium breakdown
The capture form only ever had prima neta, derecho de póliza and comisión.
The Access form it replaces has seven figures, and the four that were missing
are the ones that make a policy paid in installments add up.

Adds recargo, IVA, prima total and forma de pago to the policy header, the
same breakdown per installment, and a per-line-of-business IVA rate.

IVA and prima total are the only derived figures:

    base  = prima neta + recargo + derecho de póliza
    IVA   = round(base * tasa)
    total = base + IVA

The recargo is inside the taxable base. That is not a guess — policy 7006785
prints IVA 52.03 on 610.86 + 8.55 + 31.00, and leaving the recargo out gives
51.35, which matches nothing on the page. Both of its money rows are asserted
in premium.spec.ts. The recargo itself is never derived: the carrier quotes it,
so staff key it in, and the field is disabled on ANNUAL/SINGLE. Both derived
figures are stored rather than recomputed on read, and stay editable, because
the printed policy is the record of truth and a later rate change must not
silently restate what was issued.

The rate lives on PolicyType (seeded to 0.08, editable in Catálogos), which is
the legacy one-row IMPUESTOS / IMPUESTOS_AUTOS tables made configurable. The
rate applied is stamped on the policy so an old one reads back at its original
rate.

Per-installment, not two fixed slots on the header: a policy split into several
exhibiciones prices each payment separately — that is why the Access form drew
the money row twice — and a trimestral policy needs four, which the Access
layout could not hold.

Also fixes two losses in the ETL, which is how these went missing:

  - `forma_pago` was marked consumed by the coverage sweep and then never
    written to any column, so FORMA PAGO existed nowhere in the platform.
  - `recargo` and the whole second money row fell into `coveragesJson` as
    loose strings, mislabeled as coverage amounts.

transform_policies.py now writes all of it directly;
backfill_policy_premium_breakdown.py recovers it on a database that must not be
re-imported, and strips the migrated keys back out of coveragesJson. Both are
COALESCE-only, so a figure a human has corrected in the app wins.

IVA and TOTAL are NOT backfilled: they were unbound calculated controls on the
Access form, never columns, so there is nothing to recover and every migrated
policy reads null until it is edited.

The backfill warns on 5 annual policies that carry a non-zero recargo — a
contradiction that predates this change and is left for a human, not silently
corrected.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-18 00:24:22 -07:00

263 lines
8.1 KiB
TypeScript

"use client";
import { useState } from "react";
/** A single editable field in a child row. */
export type FieldDef = {
key: string;
label: string;
type?: "text" | "number" | "date" | "checkbox" | "select";
options?: { value: string; label: string }[];
width?: number;
/** Numeric granularity. Defaults to money (0.01); a tax rate stored as a
* fraction needs finer, or the browser rejects 0.0825 as off-step. */
step?: string;
};
export type ChildConfig = {
/** URL segment: installments | vehicles | drivers | beneficiaries | claims */
apiKind: string;
title: string;
fields: FieldDef[];
};
type RowValues = Record<string, string | boolean>;
function toDateInput(v: unknown): string {
if (!v || typeof v !== "string") return "";
const d = new Date(v);
return isNaN(d.getTime()) ? "" : d.toISOString().slice(0, 10);
}
/** Build editable values for a field from an existing row (or blank). */
function rowToValues(fields: FieldDef[], row?: Record<string, unknown>): RowValues {
const v: RowValues = {};
for (const f of fields) {
const raw = row?.[f.key];
if (f.type === "checkbox") v[f.key] = !!raw;
else if (f.type === "date") v[f.key] = toDateInput(raw);
else v[f.key] = raw == null ? "" : String(raw);
}
return v;
}
/** Coerce editable values into an API payload (numbers/blanks handled). */
function valuesToPayload(fields: FieldDef[], v: RowValues): Record<string, unknown> {
const out: Record<string, unknown> = {};
for (const f of fields) {
const val = v[f.key];
if (f.type === "checkbox") out[f.key] = !!val;
else if (f.type === "number") {
const s = String(val).trim();
out[f.key] = s === "" ? undefined : Number(s);
} else {
const s = String(val).trim();
out[f.key] = s === "" ? undefined : s;
}
}
return out;
}
/**
* Generic add/edit/remove editor for a policy's child collection. The parent
* owns the API calls (so it can reload the policy afterward); this component is
* pure UI over `rows` plus add/save/remove callbacks.
*/
export function ChildCollection({
config,
rows,
canEdit,
onAdd,
onSave,
onRemove,
}: {
config: ChildConfig;
rows: Record<string, unknown>[];
canEdit: boolean;
onAdd: (payload: Record<string, unknown>) => Promise<void>;
onSave: (id: string, payload: Record<string, unknown>) => Promise<void>;
onRemove: (id: string) => Promise<void>;
}) {
const [editingId, setEditingId] = useState<string | null>(null);
const [adding, setAdding] = useState(false);
const [values, setValues] = useState<RowValues>({});
const [busy, setBusy] = useState(false);
function startAdd() {
setEditingId(null);
setAdding(true);
setValues(rowToValues(config.fields));
}
function startEdit(row: Record<string, unknown>) {
setAdding(false);
setEditingId(String(row.id));
setValues(rowToValues(config.fields, row));
}
function cancel() {
setAdding(false);
setEditingId(null);
}
async function submit() {
setBusy(true);
try {
const payload = valuesToPayload(config.fields, values);
if (editingId) await onSave(editingId, payload);
else await onAdd(payload);
cancel();
} catch (e) {
window.alert((e as Error)?.message ?? "No se pudo guardar.");
} finally {
setBusy(false);
}
}
async function remove(id: string) {
if (!window.confirm("¿Eliminar este registro?")) return;
try {
await onRemove(id);
} catch (e) {
window.alert((e as Error)?.message ?? "No se pudo eliminar.");
}
}
const colCount = config.fields.length + (canEdit ? 1 : 0);
function editorRow() {
return (
<tr>
<td colSpan={colCount}>{editor()}</td>
</tr>
);
}
function editor() {
return (
<div className="child-editor">
<div className="form-grid">
{config.fields.map((f) => (
<label className="field" key={f.key}>
<span className="field-label">{f.label}</span>
{f.type === "checkbox" ? (
<input
type="checkbox"
checked={!!values[f.key]}
onChange={(e) => setValues({ ...values, [f.key]: e.target.checked })}
/>
) : f.type === "select" ? (
<select
className="select"
value={String(values[f.key] ?? "")}
onChange={(e) => setValues({ ...values, [f.key]: e.target.value })}
>
<option value=""></option>
{f.options?.map((o) => (
<option key={o.value} value={o.value}>
{o.label}
</option>
))}
</select>
) : (
<input
className="input"
type={f.type === "number" ? "number" : f.type === "date" ? "date" : "text"}
step={f.type === "number" ? f.step ?? "0.01" : undefined}
value={String(values[f.key] ?? "")}
onChange={(e) => setValues({ ...values, [f.key]: e.target.value })}
/>
)}
</label>
))}
</div>
<div className="form-actions">
<button type="button" className="btn btn-primary" onClick={submit} disabled={busy}>
{busy ? "Guardando…" : editingId ? "Guardar" : "Agregar"}
</button>
<button type="button" className="btn btn-ghost" onClick={cancel}>
Cancelar
</button>
</div>
</div>
);
}
return (
<div className="card" style={{ padding: 16, marginBottom: 14 }}>
<div className="child-head">
<h3 className="section-title" style={{ margin: 0 }}>
{config.title}
<span className="section-count"> {rows.length}</span>
</h3>
{canEdit && !adding && editingId === null && (
<button type="button" className="btn btn-outline" onClick={startAdd}>
+ Agregar
</button>
)}
</div>
{rows.length === 0 && !adding ? (
<div className="empty-inline">Sin registros.</div>
) : (
<div className="tx-scroll">
<table className="tx-table">
<thead>
<tr>
{config.fields.map((f) => (
<th key={f.key}>{f.label}</th>
))}
{canEdit && <th className="num">Acciones</th>}
</tr>
</thead>
<tbody>
{adding && editorRow()}
{rows.map((row) =>
editingId === String(row.id) ? (
<tr key={String(row.id)}>
<td colSpan={colCount}>{editor()}</td>
</tr>
) : (
<tr key={String(row.id)}>
{config.fields.map((f) => (
<td key={f.key}>{cellText(f, row[f.key])}</td>
))}
{canEdit && (
<td>
<div className="row-actions">
<button
type="button"
className="btn btn-ghost"
onClick={() => startEdit(row)}
>
Editar
</button>
<button
type="button"
className="btn btn-ghost"
onClick={() => remove(String(row.id))}
>
Eliminar
</button>
</div>
</td>
)}
</tr>
),
)}
</tbody>
</table>
</div>
)}
</div>
);
}
function cellText(f: FieldDef, raw: unknown): string {
if (f.type === "checkbox") return raw ? "Sí" : "No";
if (f.type === "date") return toDateInput(raw) || "—";
if (f.type === "select") {
const opt = f.options?.find((o) => o.value === String(raw));
return opt ? opt.label : "—";
}
return raw == null || raw === "" ? "—" : String(raw);
}