feat(woo): selectable revenue accounts per VAT rate in the bulk order sweep (#1908)

* feat(woo): selectable revenue accounts per VAT rate in the bulk order sweep

The bulk sweep hardcoded the revenue side to the standard 3001-series, so
a store selling both goods and services could not route tjansteordrar to
its own revenue accounts (user request, follow-up to #1900). The bulk
dialog now has a "bokforingsmall" section: per-VAT-rate revenue account
inputs, shown only for rates present in the selection, prefilled with the
effective defaults; only diffs from the default map are sent.

Server side, BulkBookWebshopOrdersSchema gains an optional
revenue_accounts map (class 3 accounts only) that buildOrderBookingLines
routes each rate bucket's revenue line through; output VAT accounts stay
derived from the rate and are not overridable. User-chosen accounts are
never auto-created: the route verifies them against the company chart up
front and aborts the whole sweep with
WEBSHOP_ORDER_REVENUE_ACCOUNT_UNKNOWN naming the offenders, while
accounts in the closed prefill set keep riding the existing chart
repair. No hardcoded varor/tjanster preset on purpose: BAS 2026 has no
standard 30xx goods/services subdivision (see DECISIONS.md).

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

* fix(woo): harden the bulk revenue template per skeptic and review findings

Three findings from the adversarial review of the revenue-template
commit, fixed in one pass:

- Build breaker: revenueAccountByRate was typed Partial<Record<...>>,
  making Object.values() return (string | undefined)[] and failing the
  production build's type-check (Vitest and ESLint both miss it). Typed
  as Record<number, string>; only truthy strings are ever inserted.

- 3740 template collision (two skeptics, independently): choosing 3740
  as a revenue account passed the class-3 gate, skipped the chart guard
  (it is in the closed prefill set), and made the residual bound read
  the templated revenue line instead of the residual, so a mangled
  gift-card order the sweep must refuse could book a ~499 kr gap as
  "oresavrundning" in an immutable verifikat. 3740 is now banned by the
  schema and the dialog mirror, and the residual line is identified
  structurally (always the last line) instead of by account lookup,
  which also fixes the pre-existing misdiagnosis when 3740 is used as
  payment_account.

- Rate-classification guard (Swedish accounting review): output VAT
  books 2611/2621/2631 per rate regardless of template, but a custom
  account counts toward ruta 05 only when configured for that rate
  (explicit momssats, rate-mapped treatment, or rate-conforming
  30x1/2/3 number + name, i.e. exactly inferDomesticSalesRate, now
  exported and reused). A mismatched pair is refused up front with
  WEBSHOP_ORDER_REVENUE_ACCOUNT_RATE_MISMATCH naming the offenders;
  default-set accounts are valid only for the rate they are the default
  for; rate-0 buckets are exempt (no output VAT, legitimate momsfri/
  export accounts).

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

* fix(woo): explicit momssats wins over name inference in the revenue-template guard

Two Swedish accounting review findings on the rate-classification guard:

- Precedence: the OR check let number+name inference qualify an account
  whose explicit default_vat_rate says a DIFFERENT rate (6%-configured
  account passing a 25% slot on its name). The guard now resolves ONE
  effective rate exactly like fetchDynamicVatAccounts does (explicit
  momssats, then rate-mapped treatment, inference only when nothing is
  configured) and compares that.

- Rate 0 slots no longer skip the check entirely: an account whose
  resolved rate is TAXABLE contradicts the 0% bucket and is refused,
  while unconfigured momsfri/export/EU accounts stay accepted (no
  contradicting configuration required, not positive proof of 0%).

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

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Mattsson
2026-08-25 21:04:06 +02:00
committed by GitHub
co-authored by Claude Fable 5
parent 85e039035d
commit d035d283ef
11 changed files with 773 additions and 13 deletions
+33
View File
@@ -1630,6 +1630,24 @@ export const BookWebshopOrderSchema = z.object({
* through the same server-side flow as the single-order endpoint (never one
* combined journal write). Max 50 = one orders-page of selection.
*/
/**
* Revenue account for the bulk revenue template: class 3 only, and never
* 3740. The template routes the revenue side of the sweep; a non-revenue
* account here would put sales on a balance or cost account with no
* reviewing user per line. 3740 (öresavrundning) is excluded because the
* bulk route bounds the rounding residual by that account: a templated
* revenue line on 3740 would both misbook real revenue as rounding and
* blind that guard (skeptic finding). Orders needing an off-class-3 revenue
* leg go through the single-order dialog, which is fully line-editable.
*/
const webshopRevenueAccount = accountNumber
.refine((n) => n.startsWith('3'), {
message: 'Intäktskontot måste vara ett konto i klass 3 (3000-3999)',
})
.refine((n) => n !== '3740', {
message: 'Öresavrundningskontot 3740 kan inte användas som intäktskonto',
})
export const BulkBookWebshopOrdersSchema = z.object({
order_ids: z.array(uuid).min(1).max(50),
/**
@@ -1637,6 +1655,21 @@ export const BulkBookWebshopOrdersSchema = z.object({
* account instead of the per-store payment-method mapping.
*/
payment_account: accountNumber.optional(),
/**
* Optional revenue template: revenue account per Swedish VAT rate, keyed
* by the rate as a string. A missing rate falls back to the standard
* 3001-series map. Output VAT accounts are never overridable: they are
* derived from the rate.
*/
revenue_accounts: z
.object({
'25': webshopRevenueAccount.optional(),
'12': webshopRevenueAccount.optional(),
'6': webshopRevenueAccount.optional(),
'0': webshopRevenueAccount.optional(),
})
.strict()
.optional(),
})
export const CreateInvoiceFromWebshopOrderSchema = z.object({