feat(mcp): article-aware invoice updates with gnubok_get_invoice round trip and rebooking preview (#1993)
* feat(mcp): article-aware invoice updates with gnubok_get_invoice round trip and rebooking preview gnubok_update_invoice items are a FULL REPLACE, had no article fields, and no MCP tool returned invoice lines, so a quantity fix rebuilt from memory wrote article_id/revenue_account null and reverted vat_rate to the customer default: revenue silently moved from the article account (3041) to the VAT-derived default, invisible in the approval preview. - gnubok_get_invoice (invoices:read, search-only): header plus every line with article_id, revenue_account, vat_rate, dimensions, editable_draft - gnubok_update_invoice lines accept article_id with the same prefill and default-set VAT adoption guard as create; permitted-set VAT gate at staging; preview carries the new lines' effective booking and a snapshot of the lines being replaced - commitUpdateInvoice scope-checks staged article ids like create does - OperationPreview: update_invoice preview (current vs new lines, header diffs, totals); create_invoice lines show VAT rate and posting account - invoicing skill points at the read-before-replace round trip Closes #1642 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FkUfWtuFCUkNtRAgMQCse2 * fix(mcp): use roundOre for update-invoice preview totals so the ore ratchet stays at baseline The preview-building code in gnubok_update_invoice introduced five naive Math.round(x * 100) / 100 occurrences, tripping check:guards (naive-ore-round 627 vs baseline 622) and failing Core Build on PR #1993. roundOre from @/lib/money is the sanctioned helper and was already imported in this file. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FkUfWtuFCUkNtRAgMQCse2 * fix(mcp): make the invoice round trip lossless for text, ROT/RUT and accrual lines Skeptic review of #1993 found three round-trip breaks for web-created drafts edited via MCP (the exact silent-loss class issue #1642 reports): - Text rows: the update pre-gate and resolveInvoiceLineFromArticle rejected quantity <= 0 before looking at line_type, so any draft with a free-text spacer row could not be edited at all, and the natural agent recovery (drop the row and retry the FULL REPLACE) silently deleted invoice content. Text rows are now exempt from the quantity/description/unit/price gates (CreateInvoiceItemSchema parity), normalized to the zeroed stored shape, excluded from the staged totals and the VAT gate (commitCreateInvoice billableItems parity), and line_type is declared on both the create and update item schemas. - ROT/RUT: gnubok_get_invoice omitted housing_designation, apartment_number and brf_org_number, so an items replace on a ROT draft either failed AFTER approval ('Fastighetsbeteckning krävs för ROT-avdrag') or, for a schema-conformant agent, silently stripped the avdrag and the stored personnummer. The three property columns (property identifiers, never the personnummer ciphertext) are now returned per line, the deduction fields are declared on the update item schema, deduction_type rides on the current_items snapshot and the new-lines preview, and a staging-time completeness gate (arbetstyp/timmar via validateDeductionLines, fastighetsbeteckning for ROT, personnummer availability on the invoice or the individual's kundkort) surfaces the failure to the agent instead of the approver. - Declared-schema gap: revenue_account and the accrual fields were accepted on pass-through but undeclared, so a schema-conformant agent dropped a manual posting-account override or a periodisering on pass-back. They are now declared on the update item schema (revenue_account also on create; create deliberately does NOT declare deduction/accrual fields because commitCreateInvoice drops them), and the approval preview shows ROT/RUT-avdrag and the periodisering period per line. tools/list ceiling check after the two new create-schema properties: 63337 of 63400. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FkUfWtuFCUkNtRAgMQCse2 --------- Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
Jakob Wennberg
parent
273af39994
commit
17caf9d80a
@@ -193,8 +193,66 @@ function CustomerPreview({ data }: { data: Record<string, unknown> }) {
|
||||
)
|
||||
}
|
||||
|
||||
// One staged (or replaced) invoice line as the staging tools put it in
|
||||
// preview_data: create_invoice and update_invoice both carry the effective
|
||||
// vat_rate and any posting-account override, so a rebooking is visible to the
|
||||
// approver, not only the amount (issue #1642).
|
||||
interface PreviewInvoiceLine {
|
||||
description: string
|
||||
quantity: number
|
||||
unit: string
|
||||
unit_price?: number
|
||||
line_total: number
|
||||
vat_rate?: number
|
||||
revenue_account?: string | null
|
||||
article_id?: string | null
|
||||
line_type?: string
|
||||
// ROT/RUT and periodisering markers: a full replace that drops one of
|
||||
// these must be visible to the approver, not only the amounts.
|
||||
deduction_type?: string | null
|
||||
accrual_period_start?: string | null
|
||||
accrual_period_end?: string | null
|
||||
}
|
||||
|
||||
function isPreviewInvoiceLines(value: unknown): value is PreviewInvoiceLine[] {
|
||||
return (
|
||||
Array.isArray(value) &&
|
||||
value.every((row) => row != null && typeof row === 'object' && typeof (row as PreviewInvoiceLine).description === 'string')
|
||||
)
|
||||
}
|
||||
|
||||
function InvoiceLineRows({ items, currency }: { items: PreviewInvoiceLine[]; currency: string }) {
|
||||
return (
|
||||
<div className="space-y-1">
|
||||
{items.map((item, i) => (
|
||||
<div key={i} className="flex justify-between text-xs">
|
||||
<span className="truncate mr-4">
|
||||
{item.description}
|
||||
{item.line_type === 'text' ? null : ` (${item.quantity} ${item.unit})`}
|
||||
{typeof item.vat_rate === 'number' && item.line_type !== 'text' && (
|
||||
<span className="text-muted-foreground"> · {item.vat_rate} % moms</span>
|
||||
)}
|
||||
{item.revenue_account && (
|
||||
<span className="text-muted-foreground font-mono"> · {item.revenue_account}</span>
|
||||
)}
|
||||
{item.deduction_type && (
|
||||
<span className="text-muted-foreground"> · {item.deduction_type === 'rot' ? 'ROT-avdrag' : 'RUT-avdrag'}</span>
|
||||
)}
|
||||
{item.accrual_period_start && item.accrual_period_end && (
|
||||
<span className="text-muted-foreground"> · periodiseras {item.accrual_period_start} till {item.accrual_period_end}</span>
|
||||
)}
|
||||
</span>
|
||||
<span className="font-mono tabular-nums whitespace-nowrap">
|
||||
{item.line_type === 'text' ? '' : money(item.line_total, currency)}
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function InvoicePreview({ data }: { data: Record<string, unknown> }) {
|
||||
const items = (data.items as Array<{ description: string; quantity: number; unit: string; unit_price: number; line_total: number; vat_rate: number }>) || []
|
||||
const items = isPreviewInvoiceLines(data.items) ? data.items : []
|
||||
|
||||
return (
|
||||
<div className="space-y-3 text-sm">
|
||||
@@ -207,15 +265,8 @@ function InvoicePreview({ data }: { data: Record<string, unknown> }) {
|
||||
<span>{String(data.due_date ?? '')}</span>
|
||||
</div>
|
||||
{items.length > 0 && (
|
||||
<div className="border-t pt-2 space-y-1">
|
||||
{items.map((item, i) => (
|
||||
<div key={i} className="flex justify-between text-xs">
|
||||
<span className="truncate mr-4">{item.description} ({item.quantity} {item.unit})</span>
|
||||
<span className="font-mono tabular-nums whitespace-nowrap">
|
||||
{formatCurrency(item.line_total, (data.currency as string) || 'SEK')}
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
<div className="border-t pt-2">
|
||||
<InvoiceLineRows items={items} currency={(data.currency as string) || 'SEK'} />
|
||||
</div>
|
||||
)}
|
||||
<div className="border-t pt-2 grid grid-cols-2 gap-x-4 gap-y-1">
|
||||
@@ -230,6 +281,82 @@ function InvoicePreview({ data }: { data: Record<string, unknown> }) {
|
||||
)
|
||||
}
|
||||
|
||||
const UPDATE_INVOICE_FIELD_LABELS: Record<string, string> = {
|
||||
notes: 'Anteckningar',
|
||||
invoice_date: 'Fakturadatum',
|
||||
due_date: 'Förfallodatum',
|
||||
delivery_date: 'Leveransdatum',
|
||||
your_reference: 'Er referens',
|
||||
our_reference: 'Vår referens',
|
||||
}
|
||||
|
||||
function UpdateInvoicePreview({ data }: { data: Record<string, unknown> }) {
|
||||
const currency = (data.currency as string) || 'SEK'
|
||||
const changes = (data.changes && typeof data.changes === 'object' ? (data.changes as Record<string, unknown>) : {})
|
||||
const headerEntries = Object.entries(changes).filter(
|
||||
([key, value]) => key !== 'items' && key !== 'default_dimensions' && value !== undefined,
|
||||
)
|
||||
const hasDimensionChange = 'default_dimensions' in changes
|
||||
const dimensionBag = changes.default_dimensions as Record<string, string> | undefined
|
||||
const newItems = isPreviewInvoiceLines(data.items) ? data.items : null
|
||||
const currentItems = isPreviewInvoiceLines(data.current_items) ? data.current_items : null
|
||||
|
||||
return (
|
||||
<div className="space-y-3 text-sm">
|
||||
<div className="grid grid-cols-2 gap-x-4 gap-y-1">
|
||||
<span className="text-muted-foreground">Kund</span>
|
||||
<span>{String(data.customer_name ?? '')}</span>
|
||||
<span className="text-muted-foreground">Faktura</span>
|
||||
<span>{data.invoice_number ? String(data.invoice_number) : 'utkast'}</span>
|
||||
{headerEntries.map(([key, value]) => (
|
||||
<Fragment key={key}>
|
||||
<span className="text-muted-foreground">{UPDATE_INVOICE_FIELD_LABELS[key] ?? key.replace(/_/g, ' ')}</span>
|
||||
{/* null is an explicit clear (delivery_date: null), not a missing value */}
|
||||
<span>{value === null ? 'rensas' : renderPrimitive(value)}</span>
|
||||
</Fragment>
|
||||
))}
|
||||
{hasDimensionChange && (
|
||||
<>
|
||||
<span className="text-muted-foreground">Dimensioner</span>
|
||||
<span className="font-mono text-xs">
|
||||
{dimensionBag && Object.keys(dimensionBag).length > 0
|
||||
? Object.entries(dimensionBag).map(([dim, code]) => `${dim}: ${code}`).join(', ')
|
||||
: 'rensas'}
|
||||
</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
{/* Full replace: show what goes away next to what comes in, so a
|
||||
quantity fix that also moves revenue off the article's account
|
||||
(3041 to 3001) or changes the VAT rate is visible before approval. */}
|
||||
{currentItems && (
|
||||
<div className="border-t pt-2 space-y-1">
|
||||
<div className="text-xs text-muted-foreground">
|
||||
{currentItems.length > 0 ? 'Nuvarande rader (ersätts)' : 'Nuvarande rader: inga'}
|
||||
</div>
|
||||
{currentItems.length > 0 && <InvoiceLineRows items={currentItems} currency={currency} />}
|
||||
</div>
|
||||
)}
|
||||
{newItems && (
|
||||
<div className="border-t pt-2 space-y-1">
|
||||
<div className="text-xs text-muted-foreground">Nya rader</div>
|
||||
<InvoiceLineRows items={newItems} currency={currency} />
|
||||
</div>
|
||||
)}
|
||||
{newItems && typeof data.total === 'number' && (
|
||||
<div className="border-t pt-2 grid grid-cols-2 gap-x-4 gap-y-1">
|
||||
<span className="text-muted-foreground">Netto</span>
|
||||
<span className="tabular-nums text-right">{money(data.subtotal, currency)}</span>
|
||||
<span className="text-muted-foreground">Moms</span>
|
||||
<span className="tabular-nums text-right">{money(data.vat_amount, currency)}</span>
|
||||
<span className="font-medium">Totalt</span>
|
||||
<span className="tabular-nums font-medium text-right">{money(data.total, currency)}</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function CreateTransactionPreview({ data }: { data: Record<string, unknown> }) {
|
||||
const currency = (data.currency as string) || 'SEK'
|
||||
|
||||
@@ -571,6 +698,8 @@ export function OperationPreview({ op }: { op: OperationPreviewInput }) {
|
||||
return <CustomerPreview data={op.preview_data} />
|
||||
case 'create_invoice':
|
||||
return <InvoicePreview data={op.preview_data} />
|
||||
case 'update_invoice':
|
||||
return <UpdateInvoicePreview data={op.preview_data} />
|
||||
case 'create_transaction':
|
||||
return <CreateTransactionPreview data={op.preview_data} />
|
||||
case 'create_voucher':
|
||||
|
||||
Reference in New Issue
Block a user