* fix(db): enforce balance check on directly inserted posted journal entries check_balance_on_post only fires on the draft-to-posted UPDATE transition, so any code path that INSERTs a row with status 'posted' directly skipped balance validation entirely. The invariant sum(debit) = sum(credit) on every posted entry was DB-enforced only for the engine's commit lifecycle. Add check_balance_on_posted_insert, a deferred constraint trigger on AFTER INSERT WHEN (NEW.status = 'posted') reusing the existing check_journal_entry_balance() function, which already handles the journal_entries INSERT context via NEW.id/NEW.status. Deferred semantics let an atomic transaction insert header and lines together; zero-line and unbalanced posted inserts are rejected at constraint evaluation. All existing checks stay intact; this only adds coverage. The one first-party posted-INSERT path outside an RPC, the sandbox seed, now books through the bookkeeping engine (createJournalEntry) instead of raw inserts. SIE import already inserts header and lines in a single transaction via its structured RPC and passes unchanged. pg tests cover the new path (zero-line rejected, unbalanced rejected at SET CONSTRAINTS IMMEDIATE, balanced same-transaction insert accepted) and existing posted-entry fixtures move to a transactional insertPostedJournalEntry helper so they stay valid setup. Fixes #327 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(tests): insert list-filters pg fixtures in one transaction The list-filters suite (landed via a sibling merge) inserted posted headers with getPool().query, where each query autocommits: the deferred check_balance_on_posted_insert constraint fired at the header's own commit with zero lines and correctly rejected the fixture. Header and balanced lines now share one BEGIN/COMMIT so the constraint evaluates the complete entry, mirroring the insertPostedJournalEntry helper. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(seed): insert journal headers as drafts, post after lines land check_balance_on_posted_insert (renamed to apply-time version 20260806130000) rejects a posted header whose transaction has no lines. PostgREST autocommits each request, so every seed path that inserted posted headers first would die with "has zero total": the sandbox seed (ledger history, invoice vouchers, salary vouchers), seed-demo-account and seed-export-data. All now insert draft headers, insert lines, then flip to posted so check_balance_on_post validates the finished verifikat. The sandbox seed keeps its documented no-events design. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(db): preserve a preset committed_at on draft-to-posted transition set_committed_at() stamped now() unconditionally, so the seed flows that post backdated drafts lost their historical booking timestamps and every demo verifikat read as booked today (CodeRabbit finding on PR 1439). Stamp only when committed_at is NULL: the engine path (drafts carry no committed_at) behaves exactly as before and a posted entry still always has a committed_at; an explicitly supplied value now survives posting. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(db): preserve preset committed_at only for trusted roles The IS NULL guard alone (20260806150000, never shipped; replaced by 20260806160000) let any RLS-permitted member backdate committed_at through PostgREST by presetting it on a draft and posting, which the Swedish accounting review flagged: committed_at is what the BFL 5 kap timeliness checks and behandlingshistorik treat as the genuine transition time. Preset values now survive posting only for service_role/postgres/supabase_admin; authenticated and anon writers always get the now() stamp. Consequence: the sandbox seed (runs as the requesting user) gets committed_at = posting time, accepted and documented in the route; the demo scripts run as service_role and keep their backdated history. pg tests cover all four paths, with the upper timestamp bound CodeRabbit asked for. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(db): restore superseded migration so the preview tracker stays consistent The preview branch had already applied 20260806150000 when the previous commit deleted the file, orphaning the preview's migration tracker ("Remote migration versions not found in local migrations directory"). Restored with a header explaining it is superseded in the same deploy by 20260806160000, so the unguarded semantics are never live on their own. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(db): decide committed_at trust by JWT claims, not current_user The Swedish review found the current_user guard bypassable: commit_journal_entry is SECURITY DEFINER and granted to authenticated, so inside it current_user is the function owner and a member could preset a backdated committed_at on a direct-inserted draft and launder it through the RPC. The guard now reads the JWT claims role (same primitive as the RPC's own tenant guard): preset values survive only for service_role or claim-less backend connections; authenticated and anon callers are always stamped now(), on both the direct UPDATE and the RPC path (new pg test). Both migration files now carry the identical final body so no unguarded intermediate exists as a standalone applyable unit. Behandlingshistorik logging of trusted overrides is follow-up #1444. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
741 lines
23 KiB
TypeScript
741 lines
23 KiB
TypeScript
/**
|
|
* pg-real test for migration
|
|
* 20260726140000_link_voucher_rpcs_resolve_amount_in_invoice_currency.sql,
|
|
* whose final RPC bodies (NULL-safe tenant guard) are restated by
|
|
* 20260727130000_link_voucher_rpcs_null_safe_tenant_guard.sql.
|
|
*
|
|
* `journal_entry_lines.debit_amount` / `credit_amount` are ALWAYS SEK;
|
|
* `currency` + `amount_in_currency` describe the underlying DOCUMENT. Both
|
|
* commit RPCs summed the SEK column and compared it against
|
|
* `remaining_amount`, which is quoted in the invoice's own currency, while the
|
|
* guard meant to make that safe (`COALESCE(v_line_currency, ...) IS DISTINCT
|
|
* FROM v_invoice.currency`) passes on precisely the FX rows it exists to catch.
|
|
*
|
|
* These are the RPCs' own guards, so they cannot be tested with mocks: the
|
|
* TypeScript matchers do not gate this path at all
|
|
* (`linkInvoiceToVoucher` / `linkSupplierInvoiceToVoucher` call the RPC
|
|
* directly from the web routes and from bulk reconcile).
|
|
*
|
|
* The SEK cases are here for the same reason as the FX ones: the 95% path must
|
|
* be provably unchanged by the migration.
|
|
*/
|
|
import { describe, it, expect } from 'vitest'
|
|
import { randomUUID } from 'node:crypto'
|
|
import { getClient, getPool } from '@/tests/pg/setup'
|
|
import {
|
|
insertAuthUser,
|
|
insertCompany,
|
|
insertCompanyMember,
|
|
insertFiscalPeriod,
|
|
} from '@/tests/pg/fixtures'
|
|
|
|
let seq = 0
|
|
function nextSeq(): number {
|
|
return (Date.now() % 1_000_000) * 1000 + seq++
|
|
}
|
|
|
|
async function seedCustomer(params: { userId: string; companyId: string }): Promise<string> {
|
|
const id = randomUUID()
|
|
await getPool().query(
|
|
`INSERT INTO public.customers (id, user_id, company_id, name, customer_type)
|
|
VALUES ($1, $2, $3, 'Utländsk Kund AB', 'swedish_business')`,
|
|
[id, params.userId, params.companyId],
|
|
)
|
|
return id
|
|
}
|
|
|
|
/**
|
|
* A customer invoice quoted in `currency`. `total` / `remaining_amount` are in
|
|
* that currency; `total_sek` carries the kronor equivalent, exactly as the
|
|
* columns are defined.
|
|
*/
|
|
async function seedInvoice(params: {
|
|
userId: string
|
|
companyId: string
|
|
customerId: string
|
|
// invoices.currency is `text default 'SEK'` and NULLABLE; explicit null
|
|
// seeds the legacy row shape that has always meant kronor.
|
|
currency: string | null
|
|
total: number
|
|
totalSek?: number | null
|
|
exchangeRate?: number | null
|
|
}): Promise<string> {
|
|
const id = randomUUID()
|
|
await getPool().query(
|
|
`INSERT INTO public.invoices
|
|
(id, user_id, company_id, customer_id, invoice_number, invoice_date, due_date,
|
|
currency, exchange_rate, subtotal, vat_amount, total, total_sek,
|
|
vat_treatment, vat_rate, status, paid_amount, remaining_amount)
|
|
VALUES ($1, $2, $3, $4, $5, '2026-04-01', '2026-05-01',
|
|
$6, $7, $8, 0, $8, $9, 'standard_25', 25, 'sent', 0, $8)`,
|
|
[
|
|
id,
|
|
params.userId,
|
|
params.companyId,
|
|
params.customerId,
|
|
`F-${nextSeq()}`,
|
|
params.currency,
|
|
params.exchangeRate ?? null,
|
|
params.total,
|
|
params.totalSek ?? null,
|
|
],
|
|
)
|
|
return id
|
|
}
|
|
|
|
async function seedSupplierInvoice(params: {
|
|
userId: string
|
|
companyId: string
|
|
currency: string
|
|
total: number
|
|
}): Promise<string> {
|
|
const supplierId = randomUUID()
|
|
await getPool().query(
|
|
`INSERT INTO public.suppliers
|
|
(id, user_id, company_id, name, supplier_type, country, default_payment_terms, default_currency)
|
|
VALUES ($1, $2, $3, 'Utlandsleverantör AB', 'swedish_business', 'SE', 30, $4)`,
|
|
[supplierId, params.userId, params.companyId, params.currency],
|
|
)
|
|
const id = randomUUID()
|
|
const arrivalNumber = nextSeq()
|
|
await getPool().query(
|
|
`INSERT INTO public.supplier_invoices
|
|
(id, user_id, company_id, supplier_id, arrival_number, supplier_invoice_number,
|
|
invoice_date, due_date, received_date, status, currency,
|
|
subtotal, vat_amount, total, paid_amount, remaining_amount,
|
|
vat_treatment, reverse_charge, is_credit_note)
|
|
VALUES ($1, $2, $3, $4, $5, $6, '2026-04-01', '2026-05-01', '2026-04-01', 'approved', $7,
|
|
$8, 0, $8, 0, $8, 'standard_25', false, false)`,
|
|
[
|
|
id,
|
|
params.userId,
|
|
params.companyId,
|
|
supplierId,
|
|
arrivalNumber,
|
|
`LF-${arrivalNumber}`,
|
|
params.currency,
|
|
params.total,
|
|
],
|
|
)
|
|
return id
|
|
}
|
|
|
|
/**
|
|
* A posted, balanced voucher. `sekAmount` goes in the debit/credit columns (the
|
|
* ledger is always kronor); `lineCurrency` + `amountInCurrency` are the document
|
|
* metadata that the fix reads on a foreign invoice. Pass `amountInCurrency:
|
|
* null` to model an FX row that carries no rate.
|
|
*/
|
|
async function seedVoucher(params: {
|
|
userId: string
|
|
companyId: string
|
|
fiscalPeriodId: string
|
|
debitAccount: string
|
|
creditAccount: string
|
|
sekAmount: number
|
|
lineCurrency?: string | null
|
|
amountInCurrency?: number | null
|
|
}): Promise<string> {
|
|
const id = randomUUID()
|
|
const client = await getClient()
|
|
try {
|
|
await client.query('BEGIN')
|
|
await client.query(
|
|
`INSERT INTO public.journal_entries
|
|
(id, user_id, company_id, fiscal_period_id, voucher_number, voucher_series,
|
|
entry_date, description, source_type, status)
|
|
VALUES ($1, $2, $3, $4, $5, 'A', '2026-05-05', 'Betalning', 'manual', 'posted')`,
|
|
[id, params.userId, params.companyId, params.fiscalPeriodId, nextSeq() % 2_000_000_000],
|
|
)
|
|
await client.query(
|
|
`INSERT INTO public.journal_entry_lines
|
|
(journal_entry_id, account_number, debit_amount, credit_amount, currency, amount_in_currency)
|
|
VALUES ($1, $2, $3, 0, $5, $6),
|
|
($1, $4, 0, $3, $5, $6)`,
|
|
[
|
|
id,
|
|
params.debitAccount,
|
|
params.sekAmount,
|
|
params.creditAccount,
|
|
params.lineCurrency ?? 'SEK',
|
|
params.amountInCurrency ?? null,
|
|
],
|
|
)
|
|
await client.query('COMMIT')
|
|
} catch (error) {
|
|
await client.query('ROLLBACK').catch(() => {})
|
|
throw error
|
|
} finally {
|
|
client.release()
|
|
}
|
|
return id
|
|
}
|
|
|
|
type RpcResult = {
|
|
ok: boolean
|
|
code?: string
|
|
invoice_status?: string
|
|
paid_amount?: number
|
|
remaining_amount?: number
|
|
payment_amount?: number
|
|
details?: Record<string, unknown>
|
|
}
|
|
|
|
async function callLinkInvoice(args: {
|
|
invoiceId: string
|
|
voucherId: string
|
|
userId: string
|
|
companyId: string
|
|
}): Promise<RpcResult> {
|
|
const { rows } = await getPool().query<{ result: RpcResult }>(
|
|
`SELECT public.link_invoice_to_voucher($1, $2, $3, $4, NULL) AS result`,
|
|
[args.invoiceId, args.voucherId, args.userId, args.companyId],
|
|
)
|
|
return rows[0].result
|
|
}
|
|
|
|
async function callLinkSupplierInvoice(args: {
|
|
supplierInvoiceId: string
|
|
voucherId: string
|
|
userId: string
|
|
companyId: string
|
|
}): Promise<RpcResult> {
|
|
const { rows } = await getPool().query<{ result: RpcResult }>(
|
|
`SELECT public.link_supplier_invoice_to_voucher($1, $2, $3, $4, NULL) AS result`,
|
|
[args.supplierInvoiceId, args.voucherId, args.userId, args.companyId],
|
|
)
|
|
return rows[0].result
|
|
}
|
|
|
|
async function seedTenant() {
|
|
const userId = await insertAuthUser()
|
|
const companyId = await insertCompany({ createdBy: userId })
|
|
await insertCompanyMember({ companyId, userId })
|
|
const fiscalPeriodId = await insertFiscalPeriod({ userId, companyId })
|
|
return { userId, companyId, fiscalPeriodId }
|
|
}
|
|
|
|
// ============================================================
|
|
// link_invoice_to_voucher
|
|
// ============================================================
|
|
|
|
describe('link_invoice_to_voucher: amount resolved in the invoice currency', () => {
|
|
it('links a 1000 EUR invoice to its 11 500 kr voucher instead of EXCEEDS_REMAINING', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedTenant()
|
|
const customerId = await seedCustomer({ userId, companyId })
|
|
// 1000 EUR outstanding, booked at 11.50 SEK/EUR.
|
|
const invoiceId = await seedInvoice({
|
|
userId,
|
|
companyId,
|
|
customerId,
|
|
currency: 'EUR',
|
|
total: 1000,
|
|
totalSek: 11500,
|
|
exchangeRate: 11.5,
|
|
})
|
|
const voucherId = await seedVoucher({
|
|
userId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
debitAccount: '1930',
|
|
creditAccount: '1510',
|
|
sekAmount: 11500,
|
|
lineCurrency: 'EUR',
|
|
amountInCurrency: 1000,
|
|
})
|
|
|
|
const result = await callLinkInvoice({ invoiceId, voucherId, userId, companyId })
|
|
|
|
// Before the fix: 11 500 > 1 000 → LINK_VOUCHER_AMOUNT_EXCEEDS_REMAINING.
|
|
expect(result).toMatchObject({ ok: true, invoice_status: 'paid' })
|
|
expect(Number(result.payment_amount)).toBe(1000)
|
|
expect(Number(result.remaining_amount)).toBe(0)
|
|
})
|
|
|
|
it('writes the payment row in the invoice currency, not in kronor', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedTenant()
|
|
const customerId = await seedCustomer({ userId, companyId })
|
|
const invoiceId = await seedInvoice({
|
|
userId,
|
|
companyId,
|
|
customerId,
|
|
currency: 'EUR',
|
|
total: 1000,
|
|
totalSek: 11500,
|
|
exchangeRate: 11.5,
|
|
})
|
|
// 400 EUR of the 1000: 4600 kr in the ledger.
|
|
const voucherId = await seedVoucher({
|
|
userId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
debitAccount: '1930',
|
|
creditAccount: '1510',
|
|
sekAmount: 4600,
|
|
lineCurrency: 'EUR',
|
|
amountInCurrency: 400,
|
|
})
|
|
|
|
const result = await callLinkInvoice({ invoiceId, voucherId, userId, companyId })
|
|
|
|
expect(result).toMatchObject({ ok: true, invoice_status: 'partially_paid' })
|
|
expect(Number(result.payment_amount)).toBe(400)
|
|
expect(Number(result.remaining_amount)).toBe(600)
|
|
|
|
const { rows } = await getPool().query(
|
|
`SELECT amount, currency FROM public.invoice_payments
|
|
WHERE invoice_id = $1 AND journal_entry_id = $2`,
|
|
[invoiceId, voucherId],
|
|
)
|
|
expect(rows).toHaveLength(1)
|
|
expect(Number(rows[0].amount)).toBe(400)
|
|
expect(rows[0].currency).toBe('EUR')
|
|
})
|
|
|
|
it('still rejects a voucher that genuinely exceeds the remainder', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedTenant()
|
|
const customerId = await seedCustomer({ userId, companyId })
|
|
const invoiceId = await seedInvoice({
|
|
userId,
|
|
companyId,
|
|
customerId,
|
|
currency: 'EUR',
|
|
total: 1000,
|
|
totalSek: 11500,
|
|
exchangeRate: 11.5,
|
|
})
|
|
// 1500 EUR against a 1000 EUR remainder.
|
|
const voucherId = await seedVoucher({
|
|
userId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
debitAccount: '1930',
|
|
creditAccount: '1510',
|
|
sekAmount: 17250,
|
|
lineCurrency: 'EUR',
|
|
amountInCurrency: 1500,
|
|
})
|
|
|
|
const result = await callLinkInvoice({ invoiceId, voucherId, userId, companyId })
|
|
|
|
expect(result.ok).toBe(false)
|
|
expect(result.code).toBe('LINK_VOUCHER_AMOUNT_EXCEEDS_REMAINING')
|
|
expect(Number(result.details?.ar_credit)).toBe(1500)
|
|
expect(Number(result.details?.remaining)).toBe(1000)
|
|
})
|
|
|
|
it('refuses an AR credit that carries no amount in the invoice currency', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedTenant()
|
|
const customerId = await seedCustomer({ userId, companyId })
|
|
const invoiceId = await seedInvoice({
|
|
userId,
|
|
companyId,
|
|
customerId,
|
|
currency: 'EUR',
|
|
total: 1000,
|
|
totalSek: 11500,
|
|
exchangeRate: 11.5,
|
|
})
|
|
// Labelled EUR but with no rate: nothing to convert with. Reading the
|
|
// 11 500 as euro would advance the invoice by eleven times its value.
|
|
const voucherId = await seedVoucher({
|
|
userId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
debitAccount: '1930',
|
|
creditAccount: '1510',
|
|
sekAmount: 11500,
|
|
lineCurrency: 'EUR',
|
|
amountInCurrency: null,
|
|
})
|
|
|
|
const result = await callLinkInvoice({ invoiceId, voucherId, userId, companyId })
|
|
|
|
expect(result.ok).toBe(false)
|
|
expect(result.code).toBe('LINK_VOUCHER_CURRENCY_MISMATCH')
|
|
|
|
const { rows } = await getPool().query(
|
|
`SELECT status, paid_amount, remaining_amount FROM public.invoices WHERE id = $1`,
|
|
[invoiceId],
|
|
)
|
|
expect(rows[0].status).toBe('sent')
|
|
expect(Number(rows[0].paid_amount)).toBe(0)
|
|
expect(Number(rows[0].remaining_amount)).toBe(1000)
|
|
})
|
|
|
|
it('refuses a domestic SEK credit as settlement of a EUR invoice', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedTenant()
|
|
const customerId = await seedCustomer({ userId, companyId })
|
|
const invoiceId = await seedInvoice({
|
|
userId,
|
|
companyId,
|
|
customerId,
|
|
currency: 'EUR',
|
|
total: 1000,
|
|
totalSek: 11500,
|
|
exchangeRate: 11.5,
|
|
})
|
|
// Same magnitude, wrong unit: 1000 kr is not 1000 EUR.
|
|
const voucherId = await seedVoucher({
|
|
userId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
debitAccount: '1930',
|
|
creditAccount: '1510',
|
|
sekAmount: 1000,
|
|
lineCurrency: 'SEK',
|
|
})
|
|
|
|
const result = await callLinkInvoice({ invoiceId, voucherId, userId, companyId })
|
|
|
|
expect(result.ok).toBe(false)
|
|
expect(result.code).toBe('LINK_VOUCHER_CURRENCY_MISMATCH')
|
|
})
|
|
|
|
it('SEK: a plain domestic link is unchanged', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedTenant()
|
|
const customerId = await seedCustomer({ userId, companyId })
|
|
const invoiceId = await seedInvoice({
|
|
userId,
|
|
companyId,
|
|
customerId,
|
|
currency: 'SEK',
|
|
total: 1000,
|
|
})
|
|
const voucherId = await seedVoucher({
|
|
userId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
debitAccount: '1930',
|
|
creditAccount: '1510',
|
|
sekAmount: 1000,
|
|
lineCurrency: 'SEK',
|
|
})
|
|
|
|
const result = await callLinkInvoice({ invoiceId, voucherId, userId, companyId })
|
|
|
|
expect(result).toMatchObject({ ok: true, invoice_status: 'paid' })
|
|
expect(Number(result.payment_amount)).toBe(1000)
|
|
})
|
|
|
|
it('SEK: a partial domestic link is unchanged', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedTenant()
|
|
const customerId = await seedCustomer({ userId, companyId })
|
|
const invoiceId = await seedInvoice({
|
|
userId,
|
|
companyId,
|
|
customerId,
|
|
currency: 'SEK',
|
|
total: 1000,
|
|
})
|
|
const voucherId = await seedVoucher({
|
|
userId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
debitAccount: '1930',
|
|
creditAccount: '1510',
|
|
sekAmount: 400,
|
|
lineCurrency: 'SEK',
|
|
})
|
|
|
|
const result = await callLinkInvoice({ invoiceId, voucherId, userId, companyId })
|
|
|
|
expect(result).toMatchObject({ ok: true, invoice_status: 'partially_paid' })
|
|
expect(Number(result.payment_amount)).toBe(400)
|
|
expect(Number(result.remaining_amount)).toBe(600)
|
|
})
|
|
|
|
it('links a legacy NULL-currency invoice (meaning SEK) with a SEK payment line', async () => {
|
|
// The label guard used to compare against the RAW nullable column:
|
|
// COALESCE('SEK', NULL) IS DISTINCT FROM NULL is true, so an ordinary
|
|
// domestic payment against a NULL-currency invoice raised
|
|
// LINK_VOUCHER_CURRENCY_MISMATCH forever, and the payment row would have
|
|
// inherited the NULL. Both now use the resolved currency.
|
|
const { userId, companyId, fiscalPeriodId } = await seedTenant()
|
|
const customerId = await seedCustomer({ userId, companyId })
|
|
const invoiceId = await seedInvoice({
|
|
userId,
|
|
companyId,
|
|
customerId,
|
|
currency: null,
|
|
total: 1000,
|
|
})
|
|
const voucherId = await seedVoucher({
|
|
userId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
debitAccount: '1930',
|
|
creditAccount: '1510',
|
|
sekAmount: 1000,
|
|
lineCurrency: 'SEK',
|
|
})
|
|
|
|
const result = await callLinkInvoice({ invoiceId, voucherId, userId, companyId })
|
|
|
|
expect(result).toMatchObject({ ok: true, invoice_status: 'paid' })
|
|
expect(Number(result.payment_amount)).toBe(1000)
|
|
|
|
// The payment row carries the resolved unit, not the raw NULL.
|
|
const { rows } = await getPool().query(
|
|
`SELECT amount, currency FROM public.invoice_payments
|
|
WHERE invoice_id = $1 AND journal_entry_id = $2`,
|
|
[invoiceId, voucherId],
|
|
)
|
|
expect(rows).toHaveLength(1)
|
|
expect(Number(rows[0].amount)).toBe(1000)
|
|
expect(rows[0].currency).toBe('SEK')
|
|
})
|
|
|
|
it('SEK: the ledger column wins even when the line carries FX metadata', async () => {
|
|
// A SEK invoice paid by a voucher whose lines happen to carry a rate: the
|
|
// debit/credit columns are kronor regardless, so the SEK branch must ignore
|
|
// amount_in_currency entirely.
|
|
const { userId, companyId, fiscalPeriodId } = await seedTenant()
|
|
const customerId = await seedCustomer({ userId, companyId })
|
|
const invoiceId = await seedInvoice({
|
|
userId,
|
|
companyId,
|
|
customerId,
|
|
currency: 'SEK',
|
|
total: 1000,
|
|
})
|
|
const voucherId = await seedVoucher({
|
|
userId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
debitAccount: '1930',
|
|
creditAccount: '1510',
|
|
sekAmount: 1000,
|
|
lineCurrency: 'SEK',
|
|
amountInCurrency: 87,
|
|
})
|
|
|
|
const result = await callLinkInvoice({ invoiceId, voucherId, userId, companyId })
|
|
|
|
expect(result).toMatchObject({ ok: true })
|
|
expect(Number(result.payment_amount)).toBe(1000)
|
|
})
|
|
})
|
|
|
|
// ============================================================
|
|
// link_supplier_invoice_to_voucher
|
|
// ============================================================
|
|
|
|
describe('link_supplier_invoice_to_voucher: amount resolved in the invoice currency', () => {
|
|
it('links a 1000 EUR supplier invoice to its 11 500 kr 2440 debit', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedTenant()
|
|
const supplierInvoiceId = await seedSupplierInvoice({
|
|
userId,
|
|
companyId,
|
|
currency: 'EUR',
|
|
total: 1000,
|
|
})
|
|
const voucherId = await seedVoucher({
|
|
userId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
debitAccount: '2440',
|
|
creditAccount: '1930',
|
|
sekAmount: 11500,
|
|
lineCurrency: 'EUR',
|
|
amountInCurrency: 1000,
|
|
})
|
|
|
|
const result = await callLinkSupplierInvoice({
|
|
supplierInvoiceId,
|
|
voucherId,
|
|
userId,
|
|
companyId,
|
|
})
|
|
|
|
expect(result).toMatchObject({ ok: true, invoice_status: 'paid' })
|
|
expect(Number(result.payment_amount)).toBe(1000)
|
|
|
|
const { rows } = await getPool().query(
|
|
`SELECT amount, currency FROM public.supplier_invoice_payments
|
|
WHERE supplier_invoice_id = $1 AND journal_entry_id = $2`,
|
|
[supplierInvoiceId, voucherId],
|
|
)
|
|
expect(rows).toHaveLength(1)
|
|
expect(Number(rows[0].amount)).toBe(1000)
|
|
expect(rows[0].currency).toBe('EUR')
|
|
})
|
|
|
|
it('sums a 244x debit across accounts in the invoice currency', async () => {
|
|
// BAS reserves 2440-2449; a samlingsverifikat can debit 2440 and 2441 in the
|
|
// same voucher. 600 + 400 EUR must settle a 1000 EUR invoice exactly.
|
|
const { userId, companyId, fiscalPeriodId } = await seedTenant()
|
|
const supplierInvoiceId = await seedSupplierInvoice({
|
|
userId,
|
|
companyId,
|
|
currency: 'EUR',
|
|
total: 1000,
|
|
})
|
|
const voucherId = randomUUID()
|
|
const client = await getClient()
|
|
try {
|
|
await client.query('BEGIN')
|
|
await client.query(
|
|
`INSERT INTO public.journal_entries
|
|
(id, user_id, company_id, fiscal_period_id, voucher_number, voucher_series,
|
|
entry_date, description, source_type, status)
|
|
VALUES ($1, $2, $3, $4, $5, 'A', '2026-05-05', 'Betalning', 'manual', 'posted')`,
|
|
[voucherId, userId, companyId, fiscalPeriodId, nextSeq() % 2_000_000_000],
|
|
)
|
|
await client.query(
|
|
`INSERT INTO public.journal_entry_lines
|
|
(journal_entry_id, account_number, debit_amount, credit_amount, currency, amount_in_currency)
|
|
VALUES ($1, '2440', 6900, 0, 'EUR', 600),
|
|
($1, '2441', 4600, 0, 'EUR', 400),
|
|
($1, '1930', 0, 11500, 'EUR', 1000)`,
|
|
[voucherId],
|
|
)
|
|
await client.query('COMMIT')
|
|
} catch (error) {
|
|
await client.query('ROLLBACK').catch(() => {})
|
|
throw error
|
|
} finally {
|
|
client.release()
|
|
}
|
|
|
|
const result = await callLinkSupplierInvoice({
|
|
supplierInvoiceId,
|
|
voucherId,
|
|
userId,
|
|
companyId,
|
|
})
|
|
|
|
expect(result).toMatchObject({ ok: true, invoice_status: 'paid' })
|
|
expect(Number(result.payment_amount)).toBe(1000)
|
|
})
|
|
|
|
it('refuses a 244x debit that carries no amount in the invoice currency', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedTenant()
|
|
const supplierInvoiceId = await seedSupplierInvoice({
|
|
userId,
|
|
companyId,
|
|
currency: 'EUR',
|
|
total: 1000,
|
|
})
|
|
const voucherId = await seedVoucher({
|
|
userId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
debitAccount: '2440',
|
|
creditAccount: '1930',
|
|
sekAmount: 11500,
|
|
lineCurrency: 'EUR',
|
|
amountInCurrency: null,
|
|
})
|
|
|
|
const result = await callLinkSupplierInvoice({
|
|
supplierInvoiceId,
|
|
voucherId,
|
|
userId,
|
|
companyId,
|
|
})
|
|
|
|
expect(result.ok).toBe(false)
|
|
expect(result.code).toBe('LINK_SI_VOUCHER_CURRENCY_MISMATCH')
|
|
|
|
const { rows } = await getPool().query(
|
|
`SELECT status, paid_amount, remaining_amount FROM public.supplier_invoices WHERE id = $1`,
|
|
[supplierInvoiceId],
|
|
)
|
|
expect(rows[0].status).toBe('approved')
|
|
expect(Number(rows[0].paid_amount)).toBe(0)
|
|
expect(Number(rows[0].remaining_amount)).toBe(1000)
|
|
})
|
|
|
|
it('still rejects a 244x debit that genuinely exceeds the remainder', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedTenant()
|
|
const supplierInvoiceId = await seedSupplierInvoice({
|
|
userId,
|
|
companyId,
|
|
currency: 'EUR',
|
|
total: 1000,
|
|
})
|
|
const voucherId = await seedVoucher({
|
|
userId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
debitAccount: '2440',
|
|
creditAccount: '1930',
|
|
sekAmount: 17250,
|
|
lineCurrency: 'EUR',
|
|
amountInCurrency: 1500,
|
|
})
|
|
|
|
const result = await callLinkSupplierInvoice({
|
|
supplierInvoiceId,
|
|
voucherId,
|
|
userId,
|
|
companyId,
|
|
})
|
|
|
|
expect(result.ok).toBe(false)
|
|
expect(result.code).toBe('LINK_SI_VOUCHER_AMOUNT_EXCEEDS_REMAINING')
|
|
expect(Number(result.details?.ap_debit)).toBe(1500)
|
|
expect(Number(result.details?.remaining)).toBe(1000)
|
|
})
|
|
|
|
it('SEK: a plain domestic supplier link is unchanged', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedTenant()
|
|
const supplierInvoiceId = await seedSupplierInvoice({
|
|
userId,
|
|
companyId,
|
|
currency: 'SEK',
|
|
total: 1000,
|
|
})
|
|
const voucherId = await seedVoucher({
|
|
userId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
debitAccount: '2440',
|
|
creditAccount: '1930',
|
|
sekAmount: 1000,
|
|
lineCurrency: 'SEK',
|
|
})
|
|
|
|
const result = await callLinkSupplierInvoice({
|
|
supplierInvoiceId,
|
|
voucherId,
|
|
userId,
|
|
companyId,
|
|
})
|
|
|
|
expect(result).toMatchObject({ ok: true, invoice_status: 'paid' })
|
|
expect(Number(result.payment_amount)).toBe(1000)
|
|
})
|
|
|
|
it('SEK: a partial domestic supplier link is unchanged', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedTenant()
|
|
const supplierInvoiceId = await seedSupplierInvoice({
|
|
userId,
|
|
companyId,
|
|
currency: 'SEK',
|
|
total: 1000,
|
|
})
|
|
const voucherId = await seedVoucher({
|
|
userId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
debitAccount: '2440',
|
|
creditAccount: '1930',
|
|
sekAmount: 400,
|
|
lineCurrency: 'SEK',
|
|
})
|
|
|
|
const result = await callLinkSupplierInvoice({
|
|
supplierInvoiceId,
|
|
voucherId,
|
|
userId,
|
|
companyId,
|
|
})
|
|
|
|
expect(result).toMatchObject({ ok: true, invoice_status: 'partially_paid' })
|
|
expect(Number(result.payment_amount)).toBe(400)
|
|
expect(Number(result.remaining_amount)).toBe(600)
|
|
})
|
|
})
|