feat(worklist): the next Skatteverket payment with bankgiro, OCR and due date under Att göra on Hem (#2435)

Part (a) of #2187. A twelfth worklist category, skattekonto_payment_due:
the earliest upcoming skattekonto charge whose sum exceeds the last synced
saldo, computed once in lib/worklist (server-side twin of the /skattekonto
page's Nästa dragning math) and rendered as one Betala row on Hem with the
shortfall, bankgiro 5050-1055, the OCR reference and the due date.

The row appears only when money has to move: a saldo that covers the charge
yields nothing, and no upcoming charge yields nothing. Ignored rows take
part, since Skatteverket draws them regardless of our flag. Without a
balance snapshot the full charge is the amount. Without an org number the
row keeps its bankgiro and date and drops the OCR.

No table, route or migration: /api/worklist/counts picks the category up
through getWorklistCounts, and Hem passes the computed row into the same
options wave as the expense payouts.

Part (b), a betalfil for the skattekonto payment, stays a follow-up: the
existing payment-file route is AGI-scoped.


Claude-Session: https://claude.ai/code/session_0179bdetHyofL6ATfQxB5wP5

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-09-08 21:25:01 +02:00
committed by GitHub
co-authored by Jakob Wennberg Claude Fable 5.1
parent a7dcaac6ad
commit 20925f6c65
10 changed files with 353 additions and 9 deletions
+18 -3
View File
@@ -13,6 +13,7 @@ vi.mock('../categories', () => ({
countPendingOperations: vi.fn().mockResolvedValue(2),
countReconciliationDue: vi.fn().mockResolvedValue(1),
countExpensePayoutsDue: vi.fn().mockResolvedValue(2),
countSkattekontoPaymentDue: vi.fn().mockResolvedValue(1),
}))
import { getWorklistCounts } from '../aggregate'
@@ -38,6 +39,7 @@ describe('getWorklistCounts', () => {
pending_operations: 2,
reconciliation_due: 1,
expense_payout: 2,
skattekonto_payment_due: 1,
})
})
@@ -61,10 +63,23 @@ describe('getWorklistCounts', () => {
expect(countExpensePayoutsDue).not.toHaveBeenCalled()
})
it('takes the skattekonto payment count from a caller-supplied value instead of rescanning', async () => {
const { countSkattekontoPaymentDue } = await import('../categories')
const due = { due: '2026-09-12', amount: 1000 } as never
const withDue = await getWorklistCounts(supabase, 'company-1', {
skattekontoPaymentDue: Promise.resolve(due),
})
expect(withDue.counts.skattekonto_payment_due).toBe(1)
// null is a value ("nothing to pay in"), not an absent option.
const without = await getWorklistCounts(supabase, 'company-1', { skattekontoPaymentDue: null })
expect(without.counts.skattekonto_payment_due).toBe(0)
expect(countSkattekontoPaymentDue).not.toHaveBeenCalled()
})
it('excludes suggested_match from the total (subset of book_transaction)', async () => {
const { total } = await getWorklistCounts(supabase, 'company-1')
// 4 + 7 + 6 + 1 + 3 + 5 + 1 + 2 + 1 + 2 (people owed for utlägg), without
// the 2 suggested matches.
expect(total).toBe(32)
// 4 + 7 + 6 + 1 + 3 + 5 + 1 + 2 + 1 + 2 (people owed for utlägg) + 1
// (skattekonto payment), without the 2 suggested matches.
expect(total).toBe(33)
})
})
+113
View File
@@ -14,6 +14,8 @@ import {
countVerifikatMissingDocument,
listExpensePayoutsDue,
listExpensePayoutSuggestions,
listSkattekontoPaymentDue,
countSkattekontoPaymentDue,
listRotRutPayoutSetSuggestions,
listSuggestedMatches,
} from '../categories'
@@ -699,3 +701,114 @@ describe('listExpensePayoutSuggestions', () => {
expect(mockSupabase.from).not.toHaveBeenCalledWith('transactions')
})
})
describe('listSkattekontoPaymentDue', () => {
const TODAY = '2026-09-08'
const company = { org_number: '559547-0021', entity_type: 'aktiebolag' }
// Queue order follows the function's `from` calls: upcoming rows and the
// balance snapshot (one parallel wave), then the company, then the OCR
// resolver's own snapshot read.
const snapshot = (saldoSkatteverket: number) => ({
data: { value: { saldo: { saldoSkatteverket }, fetchedAt: 1757300000000 } },
})
it('returns null when Skatteverket has no upcoming charge', async () => {
enqueue({ data: [] })
enqueue({ data: null })
await expect(listSkattekontoPaymentDue(supabase, COMPANY, TODAY)).resolves.toBeNull()
expect(mockSupabase.from).toHaveBeenCalledWith('skattekonto_transactions')
expect(findCalls('skattekonto_transactions', 'eq')).toContainEqual(['status', 'upcoming'])
// Ignored rows are not filtered out: Skatteverket draws them regardless.
expect(findCalls('skattekonto_transactions', 'eq')).not.toContainEqual(['is_ignored', false])
})
it('returns null when the synced saldo covers the next charge', async () => {
enqueue({
data: [
{ transaktionsdatum: '2026-09-12', forfallodatum: '2026-09-12', belopp_skatteverket: -12000 },
{ transaktionsdatum: '2026-09-12', forfallodatum: '2026-09-12', belopp_skatteverket: -3000 },
],
})
enqueue(snapshot(15000))
await expect(listSkattekontoPaymentDue(supabase, COMPANY, TODAY)).resolves.toBeNull()
// No company or OCR read once nothing has to be paid in.
expect(mockSupabase.from).not.toHaveBeenCalledWith('companies')
})
it('reports the shortfall on the earliest due date with bankgiro and OCR', async () => {
enqueue({
data: [
// A later charge: not part of the next payment.
{ transaktionsdatum: '2026-10-12', forfallodatum: '2026-10-12', belopp_skatteverket: -9000 },
{ transaktionsdatum: '2026-09-12', forfallodatum: '2026-09-12', belopp_skatteverket: '-12000.50' },
{ transaktionsdatum: '2026-09-12', forfallodatum: '2026-09-12', belopp_skatteverket: -3000 },
// Already drawn: a due date before today belongs to book_skattekonto.
{ transaktionsdatum: '2026-09-01', forfallodatum: '2026-09-01', belopp_skatteverket: -500 },
],
})
enqueue(snapshot(4000.25))
enqueue({ data: company })
// OCR resolver: no reported OCR in the snapshot, so it is derived.
enqueue({ data: null })
await expect(listSkattekontoPaymentDue(supabase, COMPANY, TODAY)).resolves.toEqual({
due: '2026-09-12',
charge: 15000.5,
balance: 4000.25,
amount: 11000.25,
count: 2,
ocr: '1655954700217',
bankgiro: '5050-1055',
})
})
it('shows the full charge when no balance snapshot exists', async () => {
enqueue({
data: [{ transaktionsdatum: '2026-09-12', forfallodatum: null, belopp_skatteverket: -2500 }],
})
enqueue({ data: null })
enqueue({ data: company })
enqueue({ data: null })
const due = await listSkattekontoPaymentDue(supabase, COMPANY, TODAY)
expect(due).toMatchObject({ due: '2026-09-12', charge: 2500, balance: null, amount: 2500, count: 1 })
})
it('adds a negative saldo (a debt) to the amount to pay in', async () => {
enqueue({
data: [{ transaktionsdatum: '2026-09-12', forfallodatum: '2026-09-12', belopp_skatteverket: -1000 }],
})
enqueue(snapshot(-250))
enqueue({ data: company })
enqueue({ data: null })
const due = await listSkattekontoPaymentDue(supabase, COMPANY, TODAY)
expect(due).toMatchObject({ charge: 1000, balance: -250, amount: 1250 })
})
it('keeps the row without an OCR when the company has no org number', async () => {
enqueue({
data: [{ transaktionsdatum: '2026-09-12', forfallodatum: '2026-09-12', belopp_skatteverket: -1000 }],
})
enqueue({ data: null })
enqueue({ data: { org_number: null, entity_type: 'aktiebolag' } })
const due = await listSkattekontoPaymentDue(supabase, COMPANY, TODAY)
expect(due).toMatchObject({ amount: 1000, ocr: null, bankgiro: '5050-1055' })
})
it('soft-fails to null on query error', async () => {
enqueue({ error: { message: 'boom' } })
enqueue({ data: null })
await expect(listSkattekontoPaymentDue(supabase, COMPANY, TODAY)).resolves.toBeNull()
})
it('counts 1 when a payment is due and 0 otherwise', async () => {
enqueue({
data: [{ transaktionsdatum: '2026-09-12', forfallodatum: '2026-09-12', belopp_skatteverket: -1000 }],
})
enqueue({ data: null })
enqueue({ data: company })
enqueue({ data: null })
await expect(countSkattekontoPaymentDue(supabase, COMPANY)).resolves.toBe(1)
enqueue({ data: [] })
enqueue({ data: null })
await expect(countSkattekontoPaymentDue(supabase, COMPANY)).resolves.toBe(0)
})
})