feat(reports): reskontra per valfritt datum + PDF-export (#1039)

Kundreskontra and leverantörsreskontra were effectively always "as of
today": the UI never passed a date, the xlsx export ignored the chosen
fiscal year, and no PDF existed.

- Both ledger generators reconstruct the ledger as it stood on a
  backdated as-of date: invoices dated on or before it (including ones
  fully paid since) with outstanding recomputed from the payment-row
  history; paid_at dates row-less full payments; undateable legacy
  amounts degrade to the live values. Today/future dates keep the live
  computation byte-identical.
- New shared reskontra PDF template (aging per counterparty + invoice
  detail for kundreskontra) with PDF routes for both ledgers.
- Both report views get a "Per datum" date control; the export menu
  offers PDF + Excel and passes the chosen date through.

Note: the PDF template deliberately avoids react-pdf's `break` prop:
it deadlocks layout when the section spills across pages (reproduced
at 40+ rows, documented in the template).

Fixes #1020
Fixes #1021

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-07-16 18:14:45 +02:00
committed by GitHub
co-authored by Claude Fable 5
parent f8611f2e89
commit 14f7478abb
12 changed files with 1318 additions and 32 deletions
+122 -1
View File
@@ -9,7 +9,7 @@ let results: Array<{ data?: unknown; error?: unknown }>
function makeBuilder() {
const b: Record<string, unknown> = {}
for (const m of ['select', 'eq', 'in', 'order', 'range']) {
for (const m of ['select', 'eq', 'in', 'lte', 'order', 'range']) {
b[m] = vi.fn().mockReturnValue(b)
}
b.single = vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null })
@@ -447,3 +447,124 @@ describe('generateARLedger', () => {
expect(report.entries[0].customer_name).toBe('Okänd kund')
})
})
describe('generateARLedger: historical as-of reconstruction (#1020)', () => {
const invoiceBase = {
customer_id: 'cust-a',
customer: { id: 'cust-a', name: 'Acme AB' },
invoice_date: '2024-05-01',
due_date: '2024-06-01',
currency: 'SEK',
}
it('reopens an invoice whose payment came after the as-of date', async () => {
results = [
// Query 1: invoices (historical path also fetches status='paid')
{
data: [
{ ...invoiceBase, id: 'inv-1', invoice_number: 'F001', total: 5000, paid_amount: 5000, paid_at: '2024-07-01T10:00:00Z', status: 'paid' },
],
error: null,
},
// Query 2: payment rows: the payment is dated after the as-of date
{
data: [{ invoice_id: 'inv-1', amount: 5000, payment_date: '2024-07-01' }],
error: null,
},
]
const report = await generateARLedger(supabase, 'company-1', '2024-06-15')
expect(report.entries).toHaveLength(1)
expect(report.entries[0].invoices[0].outstanding).toBe(5000)
expect(report.entries[0].invoices[0].paid_amount).toBe(0)
expect(report.total_outstanding).toBe(5000)
expect(report.unpaid_count).toBe(1)
})
it('reduces outstanding by payments made on or before the as-of date only', async () => {
results = [
{
data: [
{ ...invoiceBase, id: 'inv-1', invoice_number: 'F001', total: 10000, paid_amount: 10000, paid_at: '2024-07-05T10:00:00Z', status: 'paid' },
],
error: null,
},
{
data: [
{ invoice_id: 'inv-1', amount: 4000, payment_date: '2024-06-10' },
{ invoice_id: 'inv-1', amount: 6000, payment_date: '2024-07-05' },
],
error: null,
},
]
const report = await generateARLedger(supabase, 'company-1', '2024-06-15')
expect(report.entries[0].invoices[0].paid_amount).toBe(4000)
expect(report.entries[0].invoices[0].outstanding).toBe(6000)
expect(report.total_outstanding).toBe(6000)
})
it('skips invoices already settled by the as-of date', async () => {
results = [
{
data: [
// Settled before the as-of date: must not appear at all.
{ ...invoiceBase, id: 'inv-1', invoice_number: 'F001', total: 1000, paid_amount: 1000, paid_at: '2024-06-01T10:00:00Z', status: 'paid' },
// Still open: the only row in the report.
{ ...invoiceBase, id: 'inv-2', invoice_number: 'F002', total: 2000, paid_amount: 0, status: 'sent' },
],
error: null,
},
{ data: [], error: null },
]
const report = await generateARLedger(supabase, 'company-1', '2024-06-15')
expect(report.entries).toHaveLength(1)
expect(report.entries[0].invoices).toHaveLength(1)
expect(report.entries[0].invoices[0].invoice_number).toBe('F002')
expect(report.total_outstanding).toBe(2000)
expect(report.unpaid_count).toBe(1)
})
it('falls back to paid_at for fully paid invoices without payment rows', async () => {
results = [
{
data: [
// No payment rows, but paid_at says the payment came after the
// as-of date: the invoice was open on that date.
{ ...invoiceBase, id: 'inv-1', invoice_number: 'F001', total: 3000, paid_amount: 3000, paid_at: '2024-08-01T10:00:00Z', status: 'paid' },
],
error: null,
},
{ data: [], error: null },
]
const report = await generateARLedger(supabase, 'company-1', '2024-06-15')
expect(report.entries).toHaveLength(1)
expect(report.entries[0].invoices[0].outstanding).toBe(3000)
expect(report.total_outstanding).toBe(3000)
})
it('keeps stored paid_amount for undateable legacy partial payments', async () => {
results = [
{
data: [
// No payment rows and no paid_at: the stored partial amount cannot
// be dated, so it is assumed to have stood at the as-of date.
{ ...invoiceBase, id: 'inv-1', invoice_number: 'F001', total: 3000, paid_amount: 1000, status: 'sent' },
],
error: null,
},
{ data: [], error: null },
]
const report = await generateARLedger(supabase, 'company-1', '2024-06-15')
expect(report.entries[0].invoices[0].outstanding).toBe(2000)
expect(report.total_outstanding).toBe(2000)
})
})
+96 -1
View File
@@ -9,7 +9,7 @@ let results: Array<{ data?: unknown; error?: unknown }>
function makeBuilder() {
const b: Record<string, unknown> = {}
for (const m of ['select', 'eq', 'in', 'order', 'range']) {
for (const m of ['select', 'eq', 'in', 'lte', 'order', 'range']) {
b[m] = vi.fn().mockReturnValue(b)
}
b.single = vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null })
@@ -332,3 +332,98 @@ describe('generateSupplierLedger', () => {
expect(report.total_current).toBe(100)
})
})
describe('generateSupplierLedger: historical as-of reconstruction (#1021)', () => {
const invoiceBase = {
supplier_id: 'sup-1',
supplier: { id: 'sup-1', name: 'Leverantören AB' },
invoice_date: '2024-05-01',
due_date: '2024-06-01',
currency: 'SEK',
}
it('reopens an invoice whose payment came after the as-of date', async () => {
results = [
// Query 1: invoices (historical path also fetches status='paid')
{
data: [
{ ...invoiceBase, id: 'si-1', total: 8000, paid_amount: 8000, remaining_amount: 0, paid_at: '2024-07-01T10:00:00Z', status: 'paid' },
],
error: null,
},
// Query 2: payment rows dated after the as-of date
{
data: [{ supplier_invoice_id: 'si-1', amount: 8000, payment_date: '2024-07-01' }],
error: null,
},
]
const report = await generateSupplierLedger(supabase, 'company-1', '2024-06-15')
expect(report.entries).toHaveLength(1)
expect(report.entries[0].total_outstanding).toBe(8000)
expect(report.total_outstanding).toBe(8000)
expect(report.unpaid_count).toBe(1)
})
it('reduces outstanding by payments on or before the as-of date and skips settled invoices', async () => {
results = [
{
data: [
// Partially paid at the as-of date: 4 000 of 10 000 paid.
{ ...invoiceBase, id: 'si-1', total: 10000, paid_amount: 10000, remaining_amount: 0, paid_at: '2024-07-05T10:00:00Z', status: 'paid' },
// Fully settled before the as-of date: must not appear.
{ ...invoiceBase, id: 'si-2', total: 500, paid_amount: 500, remaining_amount: 0, paid_at: '2024-06-01T10:00:00Z', status: 'paid' },
],
error: null,
},
{
data: [
{ supplier_invoice_id: 'si-1', amount: 4000, payment_date: '2024-06-10' },
{ supplier_invoice_id: 'si-1', amount: 6000, payment_date: '2024-07-05' },
],
error: null,
},
]
const report = await generateSupplierLedger(supabase, 'company-1', '2024-06-15')
expect(report.entries).toHaveLength(1)
expect(report.total_outstanding).toBe(6000)
expect(report.unpaid_count).toBe(1)
})
it('falls back to paid_at for fully paid invoices without payment rows', async () => {
results = [
{
data: [
{ ...invoiceBase, id: 'si-1', total: 3000, paid_amount: 3000, remaining_amount: 0, paid_at: '2024-08-01T10:00:00Z', status: 'paid' },
],
error: null,
},
{ data: [], error: null },
]
const report = await generateSupplierLedger(supabase, 'company-1', '2024-06-15')
expect(report.entries).toHaveLength(1)
expect(report.total_outstanding).toBe(3000)
})
it('keeps the live remaining_amount for the live (non-backdated) view', async () => {
// No asOfDate: single query, stored remaining_amount trusted as-is.
results = [
{
data: [
{ ...invoiceBase, id: 'si-1', total: 3000, paid_amount: 1000, remaining_amount: 2000, status: 'partially_paid' },
],
error: null,
},
]
const report = await generateSupplierLedger(supabase, 'company-1')
expect(report.total_outstanding).toBe(2000)
expect(report.unpaid_count).toBe(1)
})
})