Files
accounted/lib/sales-orders/__tests__/create-invoice-from-order.test.ts
T
MattssonandClaude Fable 5.1 c0818bb2d2 feat(sales-orders): kundorder with partial delivery and partial invoicing (#2166)
* feat(sales-orders): kundorder with partial delivery and partial invoicing

Adds sales orders (kundorder) as their own non-ledger document between
agreement and invoice, for companies that deliver or invoice in parts.

Schema (20260902130000): sales_orders + sales_order_items with RLS via
user_company_ids(), OR-<n> numbering RPC (membership-gated, no anon
execute), company_settings.sales_orders_enabled UI gate, and back-links
invoices.sales_order_id / invoice_items.sales_order_item_id. The invoiced
quantity per order line is DERIVED from the linked invoice lines on
non-cancelled, non-credited invoices and enforced by a BEFORE trigger, so
no counter can drift and a credited invoice frees its quantity. Header
status is draft / confirmed / completed / cancelled; completion is kept
by DB triggers from the same derived quantity. Delivery and invoicing
progress are derived per line, never stored as status.

Service + API: lib/sales-orders (create/update with id-preserving line
replace, transitions with compare-and-set, cumulative delivery
registration, invoice-from-order through buildInvoiceWriteData so
booking stays in the engine, proforma -> order conversion), routes under
/api/sales-orders and /api/invoices/[id]/convert-to-order, structured
SALES_ORDER_* error codes, archive classification of the new tables.
The invoice editor round-trips sales_order_item_id so a draft edit
cannot drop the link; GET /api/invoices gains ?sales_order_id=.

UI: /sales-orders list, create/edit form reusing the invoice line
conventions, detail with deliver and create-invoice dialogs and linked
invoices; nav row behind the settings toggle; the webshop row is
relabelled webshop_orders; "Skapa order" on proformas.

MCP (20260902141000/141001): list/get reads plus four staged writes
(create, transition, register delivery, create invoice from order) whose
executors call the lib services; op types added to the pending
operations CHECK.

Tests: route tests for every route (401/400/404/happy), service unit
tests, executor and tool tests, and tests/pg/sales-orders.pg.test.ts
(16 cases, green on staging) covering RLS, numbering guards, the
over-invoice trigger incl. release on cancel/credit and cross-company
refusal, the quantity floor, and completion maintenance.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RQW7mXvbAPgjUHq7dSEamr

* fix(sales-orders): harden kundorder after skeptic and security review

Resolves every finding from the PR #2166 review pass in one batch.

Order link integrity: replaceInvoiceItems now refuses a line set that
drops an existing sales_order_item_id (INVOICE_UPDATE_DROPS_ORDER_LINK),
closing the MCP update_invoice header-only edit and the v1 PATCH path
that severed the link and freed the quantity for double invoicing. The
update_invoice re-fetch, gnubok_get_invoice and the v1 item projection
now carry sales_order_item_id so well-behaved clients round-trip it.

Quantity math: derived remaining/invoiced quantities are rounded to six
decimals and compared with an epsilon (roundQty, qtyGreater) so a float
remainder such as 0.5999999999999996 can neither refuse the final partial
invoice nor land as an invoice quantity; duplicate explicit picks are
summed before validation.

Leveransdatum: per-line last_delivery_date (migration 20260902160000);
an invoice takes the latest date over the lines it covers and only when
the covered quantity was delivered, never the header date and never for
an advance invoice (ML 17 kap 24 p.7, FX anchor per ML 8 kap 21-23).

VAT drift: the order stores the customer type and VAT-validation flag its
lines were priced under; invoicing refuses with
SALES_ORDER_CUSTOMER_VAT_CHANGED when they differ, and re-saving the
order re-validates the lines. Customer and currency are frozen once
invoices exist.

Tenant and role gates: composite FK (sales_order_id, company_id) ties a
line to its parent's company (Superagent P2); aa_enforce_company_writer_role
on both tables so a viewer cannot write through the browser client.

Proforma -> order refuses proformas with ROT/RUT, periodisering or
negative-quantity lines instead of dropping those fields. RESTRICT FK
errors on delete map to SALES_ORDER_LINE_LOCKED / SALES_ORDER_HAS_INVOICES.

Also: schema-guard literal payloads in lib/sales-orders (ceiling +2 with
reason), regenerated skills/accounted-api (sales_order_item_id on invoice
items), pg tests for the composite FK, the viewer gate and the new
columns, unit tests for every changed path.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XzFmmH82hCJNmZbPqycDiW

* fix(sales-orders): resolve CodeRabbit round on PR #2166

Quick wins from the review, all in one pass:

- replaceInvoiceItems fails closed when the invoice_items snapshot cannot
  be read (it is both the restore source and the input to the kundorder
  link guard); the guard branch is explicit in both PATCH routes.
- Cumulative delivery registration carries an optimistic predicate on the
  quantity it read, so two concurrent registrations cannot regress each
  other; DELETE of an order keeps its allowed status in the predicate and
  answers a conflict when zero rows match.
- Business dates (order date, delivery date, invoice date) default to the
  Europe/Stockholm calendar day (todayIsoStockholm), never UTC: the
  delivery date is also the Riksbanken rate anchor.
- The invoice-from-order executor treats an event emit failure as
  non-blocking: the draft already exists.
- sales_order_items are archived through their parent with the order
  currency denormalised, like invoice_items.
- Proforma "Skapa order" tolerates a 2xx without a parsable body; the
  settings toggle refreshes the server-rendered nav.
- List route doc states that q matches the order number (customer names
  are matched client-side).

Declined (out of scope for this PR): moving header + line writes and the
delivery loop into transactional RPCs (same PostgREST pattern as the
invoice PATCH path, tracked as a follow-up), the MCP approval handler's
error message shape (pre-existing code outside this change), and the
docstring-coverage warning (no repo convention).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XzFmmH82hCJNmZbPqycDiW

* fix(sales-orders): move hardening migration off a colliding version; archive contract; ceiling

- 20260902160000_sales_orders_hardening.sql collided with main's
  20260902160000_parties_substrate.sql after the third sync; renamed to
  20260902180000 and made idempotent (DROP ... IF EXISTS before each
  ADD CONSTRAINT) so a preview branch that applied it under the old
  version replays it cleanly. Staging's schema_migrations row renamed.
- sales_order_items goes back to a direct archive dump: the coverage
  contract (tests/pg/full-archive-coverage.pg.test.ts) requires it for a
  table with its own company_id; the currency lives on the parent order
  one file over, joined by sales_order_id.
- Scanner ceiling re-baselined after merging main (parties phase 1): 397.
- v1 PATCH test queues a real empty invoice_items snapshot now that
  replaceInvoiceItems fails closed on an unreadable one.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XzFmmH82hCJNmZbPqycDiW

* fix(sales-orders): drop the composite FK before its unique index on replay

The idempotent guard in 20260902180000_sales_orders_hardening.sql dropped
the unique (id, company_id) before the FK that depends on its index, so
the preview branch replay (which had applied the file under its former
version) failed with SQLSTATE 2BP01. Order swapped; replay verified on
staging.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XzFmmH82hCJNmZbPqycDiW

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-02 18:14:49 +02:00

572 lines
23 KiB
TypeScript

/**
* pickLines (pure) and createInvoiceFromSalesOrder (queued mock, with the
* invoice builder mocked).
*
* createInvoiceFromSalesOrder queue order: loadSalesOrder (sales_orders
* select, invoiced rpc), customers select, invoices insert, invoice_items
* insert, then loadSalesOrder again. On an items-insert failure the two
* rollback deletes (invoice_items, invoices) come before the failure returns.
*/
import { describe, it, expect, vi, beforeEach } from 'vitest'
import type { SupabaseClient } from '@supabase/supabase-js'
import { createQueuedMockSupabase } from '@/tests/helpers'
import { IDS, invoicedRow, makeOrderCustomer, makeSalesOrder, makeSalesOrderItem } from './fixtures'
const mockBuildInvoiceWriteData = vi.fn()
vi.mock('@/lib/invoices/build-invoice-write', () => ({
buildInvoiceWriteData: (...args: unknown[]) => mockBuildInvoiceWriteData(...args),
}))
import { createInvoiceFromSalesOrder, deliveryDateFor, pickLines } from '../create-invoice-from-order'
const { supabase, enqueue, reset, findCall } = createQueuedMockSupabase()
const sb = supabase as unknown as SupabaseClient
function orderWith(items = [makeSalesOrderItem()], overrides = {}) {
return makeSalesOrder({ status: 'confirmed', confirmed_at: '2026-09-01T10:00:00Z', items, ...overrides })
}
describe('pickLines', () => {
const order = makeSalesOrder({
items: [
makeSalesOrderItem({ id: IDS.item1, quantity: 10, delivered_qty: 5, invoiced_qty: 2, remaining_qty: 8 }),
makeSalesOrderItem({ id: IDS.item2, sort_order: 1, quantity: 4, delivered_qty: 0, invoiced_qty: 4, remaining_qty: 0 }),
makeSalesOrderItem({ id: IDS.item3, sort_order: 2, line_type: 'text', quantity: 0, delivered_qty: 0 }),
],
})
it('refuses an explicit pick above the remaining quantity', () => {
const result = pickLines(order, { lines: [{ sales_order_item_id: IDS.item1, quantity: 9 }] })
expect(result).toMatchObject({
ok: false,
code: 'SALES_ORDER_OVER_INVOICED',
details: { sales_order_item_id: IDS.item1, remaining_qty: 8, requested_qty: 9 },
})
})
it('accepts an explicit pick at exactly the remaining quantity', () => {
const result = pickLines(order, { lines: [{ sales_order_item_id: IDS.item1, quantity: 8 }] })
expect(result.ok).toBe(true)
if (!result.ok) return
expect(result.picked).toHaveLength(1)
expect(result.picked[0]).toMatchObject({ quantity: 8, item: { id: IDS.item1 } })
})
it('derives remaining from quantity - invoiced_qty when remaining_qty is absent', () => {
const bare = makeSalesOrder({ items: [makeSalesOrderItem({ id: IDS.item1, quantity: 10, invoiced_qty: 7 })] })
expect(pickLines(bare, { lines: [{ sales_order_item_id: IDS.item1, quantity: 4 }] })).toMatchObject({
ok: false,
code: 'SALES_ORDER_OVER_INVOICED',
details: { remaining_qty: 3 },
})
})
it('refuses an explicit pick of a text row or an unknown line', () => {
expect(pickLines(order, { lines: [{ sales_order_item_id: IDS.item3, quantity: 1 }] })).toMatchObject({
ok: false,
code: 'SALES_ORDER_LINE_NOT_FOUND',
})
expect(pickLines(order, { lines: [{ sales_order_item_id: IDS.unknownItem, quantity: 1 }] })).toMatchObject({
ok: false,
code: 'SALES_ORDER_LINE_NOT_FOUND',
details: { sales_order_item_id: IDS.unknownItem },
})
})
it('mode remaining (default) picks everything not yet invoiced', () => {
const result = pickLines(order, {})
expect(result.ok).toBe(true)
if (!result.ok) return
// item2 is fully invoiced, the text row never counts.
expect(result.picked.map((p) => [p.item.id, p.quantity])).toEqual([[IDS.item1, 8]])
})
it('mode delivered picks delivered minus invoiced, capped at remaining', () => {
const result = pickLines(order, { mode: 'delivered' })
expect(result.ok).toBe(true)
if (!result.ok) return
// item1: delivered 5 - invoiced 2 = 3
expect(result.picked.map((p) => [p.item.id, p.quantity])).toEqual([[IDS.item1, 3]])
})
it('mode delivered never goes negative when more is invoiced than delivered', () => {
const advance = makeSalesOrder({
items: [
makeSalesOrderItem({ id: IDS.item1, quantity: 10, delivered_qty: 1, invoiced_qty: 5 }),
makeSalesOrderItem({ id: IDS.item2, sort_order: 1, quantity: 10, delivered_qty: 6, invoiced_qty: 5 }),
],
})
const result = pickLines(advance, { mode: 'delivered' })
expect(result.ok).toBe(true)
if (!result.ok) return
expect(result.picked.map((p) => [p.item.id, p.quantity])).toEqual([[IDS.item2, 1]])
})
it('returns SALES_ORDER_NOTHING_TO_INVOICE when every line is fully invoiced', () => {
const done = makeSalesOrder({
items: [makeSalesOrderItem({ id: IDS.item1, quantity: 4, invoiced_qty: 4, remaining_qty: 0 })],
})
expect(pickLines(done, {})).toMatchObject({ ok: false, code: 'SALES_ORDER_NOTHING_TO_INVOICE' })
expect(pickLines(done, { mode: 'delivered' })).toMatchObject({ ok: false, code: 'SALES_ORDER_NOTHING_TO_INVOICE' })
})
it('returns SALES_ORDER_NOTHING_TO_INVOICE in delivered mode when nothing was delivered', () => {
const undelivered = makeSalesOrder({ items: [makeSalesOrderItem({ quantity: 4, delivered_qty: 0 })] })
expect(pickLines(undelivered, { mode: 'delivered' })).toMatchObject({
ok: false,
code: 'SALES_ORDER_NOTHING_TO_INVOICE',
})
})
it('treats an empty explicit lines array like no picks', () => {
const result = pickLines(order, { lines: [] })
expect(result.ok).toBe(true)
if (!result.ok) return
expect(result.picked.map((p) => p.item.id)).toEqual([IDS.item1])
})
it('sums duplicate picks of the same line into one invoice line', () => {
const result = pickLines(order, {
lines: [
{ sales_order_item_id: IDS.item1, quantity: 3 },
{ sales_order_item_id: IDS.item1, quantity: 5 },
],
})
expect(result.ok).toBe(true)
if (!result.ok) return
expect(result.picked).toHaveLength(1)
expect(result.picked[0]).toMatchObject({ quantity: 8, item: { id: IDS.item1 } })
})
it('refuses duplicate picks whose SUM exceeds the remaining quantity', () => {
// 5 and 4 each fit within the remaining 8; together they do not.
const result = pickLines(order, {
lines: [
{ sales_order_item_id: IDS.item1, quantity: 5 },
{ sales_order_item_id: IDS.item1, quantity: 4 },
],
})
expect(result).toMatchObject({
ok: false,
code: 'SALES_ORDER_OVER_INVOICED',
details: { sales_order_item_id: IDS.item1, remaining_qty: 8, requested_qty: 9 },
})
})
it('accepts a pick equal to a float remainder (7.5 ordered, 6.9 invoiced, pick 0.6)', () => {
// 7.5 - 6.9 is 0.5999999999999996 in doubles; the pick must still fit.
const fractional = makeSalesOrder({
items: [makeSalesOrderItem({ id: IDS.item1, quantity: 7.5, invoiced_qty: 6.9 })],
})
const explicit = pickLines(fractional, { lines: [{ sales_order_item_id: IDS.item1, quantity: 0.6 }] })
expect(explicit.ok).toBe(true)
if (!explicit.ok) return
expect(explicit.picked[0].quantity).toBe(0.6)
const remaining = pickLines(fractional, {})
expect(remaining.ok).toBe(true)
if (!remaining.ok) return
expect(remaining.picked[0].quantity).toBe(0.6)
})
it('rounds a summed fractional pick before comparing it to the remainder', () => {
const fractional = makeSalesOrder({
items: [makeSalesOrderItem({ id: IDS.item1, quantity: 1, invoiced_qty: 0.7 })],
})
// 0.1 + 0.2 = 0.30000000000000004 in doubles; remaining is exactly 0.3.
const result = pickLines(fractional, {
lines: [
{ sales_order_item_id: IDS.item1, quantity: 0.1 },
{ sales_order_item_id: IDS.item1, quantity: 0.2 },
],
})
expect(result.ok).toBe(true)
if (!result.ok) return
expect(result.picked[0].quantity).toBe(0.3)
})
})
describe('deliveryDateFor', () => {
it('is the latest per-line last_delivery_date when every pick is covered by deliveries', () => {
const picked = [
{
item: makeSalesOrderItem({ id: IDS.item1, quantity: 10, delivered_qty: 5, invoiced_qty: 2, last_delivery_date: '2026-08-20' }),
quantity: 3,
},
{
item: makeSalesOrderItem({ id: IDS.item2, quantity: 4, delivered_qty: 4, invoiced_qty: 0, last_delivery_date: '2026-08-30' }),
quantity: 4,
},
]
expect(deliveryDateFor(picked)).toBe('2026-08-30')
})
it('is null when a pick exceeds what was delivered but not yet invoiced (advance invoice)', () => {
const picked = [
{
// delivered 5 - invoiced 2 = 3 available; picking 4 reaches undelivered quantity.
item: makeSalesOrderItem({ id: IDS.item1, quantity: 10, delivered_qty: 5, invoiced_qty: 2, last_delivery_date: '2026-08-20' }),
quantity: 4,
},
]
expect(deliveryDateFor(picked)).toBeNull()
expect(
deliveryDateFor([
{ item: makeSalesOrderItem({ quantity: 10, delivered_qty: 0, last_delivery_date: null }), quantity: 1 },
]),
).toBeNull()
})
it('is null when any covered line lacks a per-line delivery date', () => {
const picked = [
{
item: makeSalesOrderItem({ id: IDS.item1, quantity: 10, delivered_qty: 5, invoiced_qty: 0, last_delivery_date: '2026-08-20' }),
quantity: 5,
},
{
// Delivered before per-line dates existed: no date to anchor on.
item: makeSalesOrderItem({ id: IDS.item2, quantity: 4, delivered_qty: 4, invoiced_qty: 0, last_delivery_date: null }),
quantity: 4,
},
]
expect(deliveryDateFor(picked)).toBeNull()
})
it('tolerates float drift in the covered check (7.5 delivered, 6.9 invoiced, pick 0.6)', () => {
const picked = [
{
item: makeSalesOrderItem({ id: IDS.item1, quantity: 7.5, delivered_qty: 7.5, invoiced_qty: 6.9, last_delivery_date: '2026-08-30' }),
quantity: 0.6,
},
]
expect(deliveryDateFor(picked)).toBe('2026-08-30')
})
})
const okBuild = {
ok: true,
invoiceFields: {
customer_id: IDS.customer,
invoice_date: '2026-09-02',
due_date: '2026-10-02',
currency: 'SEK',
subtotal: 800,
vat_amount: 200,
total: 1000,
vat_treatment: 'standard_25',
},
items: [
{
sort_order: 0,
line_type: 'product',
description: 'Konsulttimme',
quantity: 8,
unit: 'h',
unit_price: 100,
line_total: 800,
vat_rate: 25,
vat_amount: 200,
sales_order_item_id: IDS.item1,
},
],
}
describe('createInvoiceFromSalesOrder', () => {
beforeEach(() => {
vi.clearAllMocks()
reset()
mockBuildInvoiceWriteData.mockResolvedValue(okBuild)
})
const params = { companyId: IDS.company, userId: IDS.user, orderId: IDS.order }
it('returns SALES_ORDER_NOT_FOUND for a missing order', async () => {
enqueue({ data: null })
const result = await createInvoiceFromSalesOrder(sb, { ...params, input: {} })
expect(result).toMatchObject({ ok: false, code: 'SALES_ORDER_NOT_FOUND' })
expect(mockBuildInvoiceWriteData).not.toHaveBeenCalled()
})
it('refuses invoicing an order that is not confirmed', async () => {
enqueue({ data: makeSalesOrder({ status: 'draft' }) })
enqueue({ data: [] })
const result = await createInvoiceFromSalesOrder(sb, { ...params, input: {} })
expect(result).toMatchObject({
ok: false,
code: 'SALES_ORDER_INVALID_STATE',
details: { status: 'draft', action: 'invoice' },
})
expect(mockBuildInvoiceWriteData).not.toHaveBeenCalled()
})
it('refuses when nothing remains to invoice', async () => {
enqueue({ data: orderWith([makeSalesOrderItem({ id: IDS.item1, quantity: 4 })]) })
enqueue({ data: [invoicedRow(IDS.item1, 4)] })
const result = await createInvoiceFromSalesOrder(sb, { ...params, input: {} })
expect(result).toMatchObject({ ok: false, code: 'SALES_ORDER_NOTHING_TO_INVOICE' })
expect(findCall('invoices', 'insert')).toBeUndefined()
})
it('returns CUSTOMER_NOT_FOUND when the raw customer row is gone', async () => {
enqueue({ data: orderWith() })
enqueue({ data: [] })
enqueue({ data: null })
const result = await createInvoiceFromSalesOrder(sb, { ...params, input: {} })
expect(result).toMatchObject({ ok: false, code: 'CUSTOMER_NOT_FOUND', details: { customerId: IDS.customer } })
})
it('creates an unnumbered draft linked to the order, each line carrying its sales_order_item_id', async () => {
enqueue({
data: orderWith(
[
makeSalesOrderItem({ id: IDS.item1, quantity: 10, delivered_qty: 0, discount_percent: 10 }),
makeSalesOrderItem({ id: IDS.item3, sort_order: 1, line_type: 'text', description: 'Tack', quantity: 0 }),
],
{ order_number: 'OR-7', your_reference: 'Anna', last_delivery_date: '2026-08-30' },
),
})
enqueue({ data: [invoicedRow(IDS.item1, 2)] })
enqueue({ data: makeOrderCustomer({ default_payment_terms: 20 }) })
enqueue({ data: { id: IDS.invoice, status: 'draft', invoice_number: null, sales_order_id: IDS.order } })
enqueue({ data: null }) // invoice_items insert
enqueue({ data: orderWith() }) // reload
enqueue({ data: [invoicedRow(IDS.item1, 10)] })
const result = await createInvoiceFromSalesOrder(sb, { ...params, input: { invoice_date: '2026-09-02' } })
expect(result.ok).toBe(true)
if (!result.ok) return
expect(result.invoice.id).toBe(IDS.invoice)
expect(result.order.invoicing_progress).toBe('full')
// Builder input: the remaining 8 of item1, the text row carried along,
// sales_order_item_id on the product line, due date from customer terms,
// no delivery date because nothing was delivered.
const buildArg = mockBuildInvoiceWriteData.mock.calls[0][0] as {
documentType: string
input: { items: Record<string, unknown>[]; due_date: string; delivery_date: unknown; notes?: string; your_reference?: string }
}
expect(buildArg.documentType).toBe('invoice')
expect(buildArg.input.due_date).toBe('2026-09-22')
expect(buildArg.input.delivery_date).toBeNull()
expect(buildArg.input.notes).toBe('Kundorder OR-7')
expect(buildArg.input.your_reference).toBe('Anna')
expect(buildArg.input.items).toEqual([
expect.objectContaining({
line_type: 'product',
quantity: 8,
unit_price: 100,
discount_percent: 10,
vat_rate: 25,
sales_order_item_id: IDS.item1,
}),
expect.objectContaining({ line_type: 'text', description: 'Tack', quantity: 0 }),
])
const invoiceInsert = findCall('invoices', 'insert')![0] as Record<string, unknown>
expect(invoiceInsert).toMatchObject({
user_id: IDS.user,
company_id: IDS.company,
invoice_number: null,
status: 'draft',
sales_order_id: IDS.order,
subtotal: 800,
total: 1000,
})
const itemRows = findCall('invoice_items', 'insert')![0] as Record<string, unknown>[]
expect(itemRows).toHaveLength(1)
for (const row of itemRows) {
expect(row.invoice_id).toBe(IDS.invoice)
expect(row.sales_order_item_id).toBe(IDS.item1)
}
expect(findCall('invoices', 'delete')).toBeUndefined()
})
it('sets delivery_date from the picked lines when the pick is covered by deliveries', async () => {
// The header last_delivery_date is display-only and deliberately later
// than the line's date: the invoice must take the LINE date.
enqueue({
data: orderWith(
[makeSalesOrderItem({ id: IDS.item1, quantity: 10, delivered_qty: 4, last_delivery_date: '2026-08-30' })],
{ last_delivery_date: '2026-09-01' },
),
})
enqueue({ data: [] })
enqueue({ data: makeOrderCustomer() })
enqueue({ data: { id: IDS.invoice, status: 'draft' } })
enqueue({ data: null })
enqueue({ data: orderWith() })
enqueue({ data: [] })
const result = await createInvoiceFromSalesOrder(sb, {
...params,
input: { mode: 'delivered', due_date: '2026-09-30' },
})
expect(result.ok).toBe(true)
const buildArg = mockBuildInvoiceWriteData.mock.calls[0][0] as { input: { delivery_date: unknown; due_date: string; items: { quantity: number }[] } }
expect(buildArg.input.delivery_date).toBe('2026-08-30')
expect(buildArg.input.due_date).toBe('2026-09-30')
expect(buildArg.input.items[0].quantity).toBe(4)
})
it('leaves delivery_date null when the pick reaches undelivered quantity (advance invoice)', async () => {
enqueue({
data: orderWith(
[makeSalesOrderItem({ id: IDS.item1, quantity: 10, delivered_qty: 4, last_delivery_date: '2026-08-30' })],
{ last_delivery_date: '2026-08-30' },
),
})
enqueue({ data: [] })
enqueue({ data: makeOrderCustomer() })
enqueue({ data: { id: IDS.invoice, status: 'draft' } })
enqueue({ data: null })
enqueue({ data: orderWith() })
enqueue({ data: [] })
// mode remaining: all 10, of which only 4 were delivered.
const result = await createInvoiceFromSalesOrder(sb, { ...params, input: { due_date: '2026-09-30' } })
expect(result.ok).toBe(true)
const buildArg = mockBuildInvoiceWriteData.mock.calls[0][0] as { input: { delivery_date: unknown; items: { quantity: number }[] } }
expect(buildArg.input.delivery_date).toBeNull()
expect(buildArg.input.items[0].quantity).toBe(10)
})
it('refuses with SALES_ORDER_CUSTOMER_VAT_CHANGED when the customer type differs from the order snapshot', async () => {
enqueue({
data: orderWith([makeSalesOrderItem()], {
customer_type_snapshot: 'swedish_business',
customer_vat_validated_snapshot: false,
}),
})
enqueue({ data: [] })
// Since validated as an EU business: the frozen 25 % line would pass the
// permitted-set gate but is no longer what the customer should be charged.
enqueue({
data: makeOrderCustomer({ customer_type: 'eu_business', vat_number: 'DE123456789', vat_number_validated: true }),
})
const result = await createInvoiceFromSalesOrder(sb, { ...params, input: {} })
expect(result).toMatchObject({
ok: false,
code: 'SALES_ORDER_CUSTOMER_VAT_CHANGED',
details: {
snapshot: { customer_type: 'swedish_business', vat_number_validated: false },
current: { customer_type: 'eu_business', vat_number_validated: true },
},
})
expect(mockBuildInvoiceWriteData).not.toHaveBeenCalled()
expect(findCall('invoices', 'insert')).toBeUndefined()
})
it('refuses when only the VAT-number validation flag changed since the snapshot', async () => {
enqueue({
data: orderWith([makeSalesOrderItem()], {
customer_type_snapshot: 'eu_business',
customer_vat_validated_snapshot: false,
}),
})
enqueue({ data: [] })
enqueue({ data: makeOrderCustomer({ customer_type: 'eu_business', vat_number_validated: true }) })
const result = await createInvoiceFromSalesOrder(sb, { ...params, input: {} })
expect(result).toMatchObject({ ok: false, code: 'SALES_ORDER_CUSTOMER_VAT_CHANGED' })
expect(mockBuildInvoiceWriteData).not.toHaveBeenCalled()
})
it('treats a null validation snapshot as false and passes when the customer still matches', async () => {
enqueue({
data: orderWith([makeSalesOrderItem()], {
customer_type_snapshot: 'swedish_business',
customer_vat_validated_snapshot: null,
}),
})
enqueue({ data: [] })
enqueue({ data: makeOrderCustomer({ customer_type: 'swedish_business', vat_number_validated: null as unknown as boolean }) })
enqueue({ data: { id: IDS.invoice, status: 'draft' } })
enqueue({ data: null })
enqueue({ data: orderWith() })
enqueue({ data: [] })
const result = await createInvoiceFromSalesOrder(sb, { ...params, input: {} })
expect(result.ok).toBe(true)
expect(mockBuildInvoiceWriteData).toHaveBeenCalledTimes(1)
})
it('passes without a snapshot check when the order carries no customer_type_snapshot (pre-snapshot orders)', async () => {
enqueue({
data: orderWith([makeSalesOrderItem()], { customer_type_snapshot: null, customer_vat_validated_snapshot: null }),
})
enqueue({ data: [] })
enqueue({ data: makeOrderCustomer({ customer_type: 'eu_business', vat_number_validated: true }) })
enqueue({ data: { id: IDS.invoice, status: 'draft' } })
enqueue({ data: null })
enqueue({ data: orderWith() })
enqueue({ data: [] })
const result = await createInvoiceFromSalesOrder(sb, { ...params, input: {} })
expect(result.ok).toBe(true)
expect(mockBuildInvoiceWriteData).toHaveBeenCalledTimes(1)
expect(findCall('invoices', 'insert')).toBeDefined()
})
it('propagates a builder domain failure without inserting', async () => {
enqueue({ data: orderWith() })
enqueue({ data: [] })
enqueue({ data: makeOrderCustomer() })
mockBuildInvoiceWriteData.mockResolvedValue({
ok: false,
code: 'INVOICE_CREATE_VAT_RULE_VIOLATION',
details: { attemptedRate: 25 },
})
const result = await createInvoiceFromSalesOrder(sb, { ...params, input: {} })
expect(result).toMatchObject({ ok: false, code: 'INVOICE_CREATE_VAT_RULE_VIOLATION', details: { attemptedRate: 25 } })
expect(findCall('invoices', 'insert')).toBeUndefined()
})
it('deletes the draft and maps the over-invoice trigger when the line insert fails', async () => {
enqueue({ data: orderWith() })
enqueue({ data: [] })
enqueue({ data: makeOrderCustomer() })
enqueue({ data: { id: IDS.invoice, status: 'draft' } })
enqueue({
data: null,
error: { message: 'SALES_ORDER_OVER_INVOICED: line d1000000 would exceed ordered quantity', code: 'P0001' },
})
enqueue({ data: null }) // invoice_items delete
enqueue({ data: null }) // invoices delete
const result = await createInvoiceFromSalesOrder(sb, { ...params, input: {} })
expect(result).toMatchObject({ ok: false, code: 'SALES_ORDER_OVER_INVOICED' })
expect(findCall('invoice_items', 'delete')).toBeDefined()
expect(findCall('invoice_items', 'eq')).toEqual(['invoice_id', IDS.invoice])
expect(findCall('invoices', 'delete')).toBeDefined()
expect(findCall('invoices', 'eq')).toEqual(['id', IDS.invoice])
})
it('deletes the draft and returns the raw DB error when the line insert fails for another reason', async () => {
enqueue({ data: orderWith() })
enqueue({ data: [] })
enqueue({ data: makeOrderCustomer() })
enqueue({ data: { id: IDS.invoice, status: 'draft' } })
enqueue({ data: null, error: { message: 'null value in column "description"', code: '23502' } })
enqueue({ data: null })
enqueue({ data: null })
const result = await createInvoiceFromSalesOrder(sb, { ...params, input: {} })
expect(result.ok).toBe(false)
expect('dbError' in result && result.dbError).toMatchObject({ code: '23502' })
expect(findCall('invoices', 'delete')).toBeDefined()
})
})