feat(dimensions): PR7 producers — auto-tagged documents (invoices, supplier invoices, bulk-book, templates, MCP) (#868)

* feat(dimensions): PR7 producers — invoices/supplier invoices carry dims, generators propagate, BulkBook + templates + MCP bags

Source documents now carry dimension tags and every entry generator
propagates them onto journal lines (dev_docs/dimensions_implementation_plan.md PR7):

- invoices/supplier_invoices.default_dimensions + per-item dimensions
  (migration 20260702200000; jsonb DEFAULT '{}' + object CHECK)
- invoice-entries: issuance/payment/cash/credit propagate — item bags merge
  over the invoice default per revenue line (account+bag aggregation
  identity), payment vouchers re-propagate the linked invoice's bag onto
  every leg incl. FX result lines; ROT/RUT 1513 carries the item bag
- supplier-invoice-entries: registration/payment/cash/privately-paid/credit
  propagate with the same merge rules (expense buckets keyed account+bag)
- bulk_book_transactions RPC persists per-line bags + derives
  cost_center/project mirrors in SQL (migration 20260702201000; malformed
  bags rejected with BULK_BOOK_INVALID_DIMENSIONS); route merges the header
  default into template/manual lines
- counterparty templates: LinePatternEntry.dimensions learned from SIE
  voucher history (kept only when every occurrence agrees), applied to
  business lines on booking; QuickReviewDialog shows a dims badge
- categorize: staged dimensions bag tags business lines only (bank/VAT
  untagged); credit/convert/inbox copy paths carry bags forward
- propose-payment/send-lines stamp the invoice default so the editable
  payment grid books what the preview shows; mark-paid override lines
  accept dimensions
- UI: InvoiceEditor + NewSupplierInvoiceForm header KS/Projekt pair with
  per-row override; BulkBookDialog header default pair (both tabs)
- MCP: default_dimensions/items[].dimensions on create_invoice +
  create_supplier_invoice_from_inbox, dimensions on categorize_transaction,
  per-line bags on bulk_book_transactions — resolve-don't-select via the
  shared registry helpers, resolutions echoed

32 new propagation unit tests + 4 pg-real tests for the RPC migration.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test: use roundOre in new dims rounding assertions (ratchet)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix: copy dimension bag per payment line, document dimensionsBagKey normalization contract (review)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-07-02 21:21:01 +02:00
committed by GitHub
co-authored by Claude Fable 5
parent 816b1769c8
commit 755e0f7e47
41 changed files with 3574 additions and 112 deletions
+24
View File
@@ -291,6 +291,9 @@ export const CreateInvoiceItemSchema = z
accrual_period_start: isoDate.nullable().optional(),
accrual_period_end: isoDate.nullable().optional(),
accrual_balance_account: deferredRevenueAccount.nullable().optional(),
// Dimensions PR7: per-item bag merged over the invoice's
// default_dimensions on the revenue line this item books to.
dimensions: DimensionsBagSchema.optional(),
})
.superRefine((item, ctx) => {
validateAccrualPeriod(item, ctx)
@@ -359,6 +362,9 @@ export const CreateInvoiceSchema = z.object({
// Per-invoice öresavrundning toggle (display-only). Omitted → stored as null,
// which inherits company_settings.ore_rounding when rendering totals.
ore_rounding: z.boolean().optional(),
// Dimensions PR7: invoice-level bag applied to every generated journal line;
// items[].dimensions merge over it per revenue line.
default_dimensions: DimensionsBagSchema.optional(),
items: z.array(CreateInvoiceItemSchema).min(1, 'At least one item is required'),
})
@@ -491,6 +497,9 @@ export const MarkInvoicePaidSchema = z.object({
debit_amount: nonNegativeAmount.default(0),
credit_amount: nonNegativeAmount.default(0),
line_description: z.string().optional(),
// Dimensions PR7: user-edited payment lines keep their tags (the
// no-override path re-propagates the invoice's default_dimensions).
dimensions: DimensionsBagSchema.optional(),
})).min(2).optional(),
// Bypass the duplicate-payment guard. Set after the user reviews the
// candidate list returned by INVOICE_PAID_LIKELY_DUPLICATE and confirms
@@ -591,6 +600,9 @@ export const CreateSupplierInvoiceItemSchema = z.object({
accrual_period_start: isoDate.nullable().optional(),
accrual_period_end: isoDate.nullable().optional(),
accrual_balance_account: prepaidExpenseAccount.nullable().optional(),
// Dimensions PR7: per-item bag merged over the invoice's
// default_dimensions on the expense line this item books to.
dimensions: DimensionsBagSchema.optional(),
}).refine(
(item) => {
if (item.vat_amount == null) return true
@@ -641,6 +653,9 @@ export const CreateSupplierInvoiceSchema = z.object({
// For paid_with_private_funds: the date the owner paid out-of-pocket.
// Defaults to invoice_date (common for kvitto where the two coincide).
payment_date: isoDate.optional(),
// Dimensions PR7: invoice-level bag applied to every generated journal line;
// items[].dimensions merge over it per expense line.
default_dimensions: DimensionsBagSchema.optional(),
items: z.array(CreateSupplierInvoiceItemSchema).min(1, 'At least one item is required'),
})
@@ -662,6 +677,9 @@ export const MarkSupplierInvoicePaidSchema = z.object({
debit_amount: nonNegativeAmount.default(0),
credit_amount: nonNegativeAmount.default(0),
line_description: z.string().optional(),
// Dimensions PR7: user-edited payment lines keep their tags (the
// no-override path re-propagates the invoice's default_dimensions).
dimensions: DimensionsBagSchema.optional(),
})).min(2).optional(),
})
@@ -1015,11 +1033,17 @@ export const BulkBookSchema = z
credit_amount: nonNegativeAmount.max(99_999_999, 'Line amount exceeds maximum'),
currency: z.string().min(3).max(3).default('SEK'),
line_description: z.string().max(200).optional(),
// Dimensions PR7: per-line bag, wins over default_dimensions.
dimensions: DimensionsBagSchema.optional(),
})
)
.min(2, 'A verifikat needs at least two lines')
.max(200)
.optional(),
// Dimensions PR7: header-level bag applied to every generated line in
// BOTH the template and manual paths (per-line bags win per key). The
// route merges before calling the RPC.
default_dimensions: DimensionsBagSchema.optional(),
})
.superRefine((data, ctx) => {
const hasExisting = !!data.existing_journal_entry_id