"use client"; import { Check as CheckIcon, Plus, Trash2, X } from "lucide-react"; import { amountsEqual, describe, formatAmount, parseAmount, SUMMARY_FIELDS, type Check, type Item, type Receipt, type SummaryField, } from "@/lib/consistency"; /** Shared receipt form and rule list, used by the scan page (users) and the model view (evaluation). */ export const FIELD_LABEL: Record = { subtotal: "Subtotal", discount: "Discount", service: "Service charge", tax: "Tax", total: "Total", cash: "Cash paid", change: "Change", card: "Card charge", }; const ITEM_COLS: { key: keyof Item; label: string; money: boolean }[] = [ { key: "name", label: "Item", money: false }, { key: "qty", label: "Qty", money: true }, { key: "unit_price", label: "Unit price", money: true }, { key: "amount", label: "Amount", money: true }, ]; export const cloneReceipt = (r: Receipt | null | undefined): Receipt => JSON.parse(JSON.stringify(r ?? { items: [] })); function norm(money: boolean, v: string | undefined): string { if (!v) return ""; if (money) { const p = parseAmount(v); if (p !== null) return Math.abs(p).toFixed(2); } return v.replace(/\s+/g, " ").trim().toLowerCase(); } /** * Editable items table and summary fields. `update` applies a mutation to a copy of the receipt. * When `gold` is given and `compare` is on, cells that differ from the human label are marked. */ export function ReceiptFields({ value, update, gold, compare = false, }: { value: Receipt; update: (fn: (r: Receipt) => void) => void; gold?: Receipt; compare?: boolean; }) { const items = value.items ?? []; const goldItems = gold?.items ?? []; const marking = compare && !!gold; return ( <>
{ITEM_COLS.map((c) => ( ))} {items.map((it, i) => ( {ITEM_COLS.map((c) => { const v = it[c.key] ?? ""; const g = goldItems[i]?.[c.key]; const differs = marking && norm(c.money, v) !== norm(c.money, g); return ( ); })} ))} {items.length === 0 && ( )}
{c.label} Remove
update((r) => { (r.items ??= [])[i] = { ...r.items![i], [c.key]: e.target.value, }; }) } className={`tabular h-8 w-full rounded-md border bg-transparent px-2 text-[13.5px] text-ink focus:border-accent focus:outline-none ${ differs ? "border-warn/60 bg-warn-soft/40" : "border-transparent hover:border-line" }`} />
No items found. Add them with the button below.
{SUMMARY_FIELDS.map((f) => { const v = value[f] ?? ""; const g = gold?.[f]; const differs = marking && norm(true, v) !== norm(true, g); return ( ); })}
); } /** One row per accounting rule: what the receipt should say, and what it says. */ export function CheckList({ checks }: { checks: Check[] }) { return ( ); }