* fix(reconciliation): exclude opening balance from unmatched-1930 set (#443) Reconciliation incorrectly counted IB (source_type='opening_balance') vouchers on 1930 as unmatched bank transactions and included them in the GL period total. Result: an SIE-imported book always showed a phantom unmatched voucher and a difference equal to the IB amount, even when every real bank transaction was matched. This was the root of the "reconciliation says broken but numbers look right" complaint. The fix is two-sided: - get_unlinked_1930_lines RPC now skips source_type='opening_balance' rows. IB has no counterpart in the bank feed by definition. - getReconciliationStatus splits glOpeningBalance out of the period movement and computes difference = bankTotal − glPeriodMovement (was bankTotal − glBalance). gl_1930_balance is preserved unchanged for back-compat; new gl_1930_period_movement and gl_1930_opening_balance fields let the UI show both perspectives. - BankReconciliationView shows period movement vs bank in the diff and folds IB into a small caption ("Ingående balans … räknas inte i avstämningen") only when non-zero. Mirrors Fortnox's "Ingående saldo vid avstämningsstart" pattern — IB is verified separately, never surfaced as unmatched. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(reconciliation): use IS DISTINCT FROM for NULL-safe source_type filter `source_type <> 'opening_balance'` evaluates to NULL (not TRUE) when `source_type IS NULL` under SQL three-valued logic. journal_entries.source_type is NOT NULL today so the practical difference is zero, but if that constraint is ever relaxed `<>` would silently drop NULL rows from the unmatched-1930 set, making them invisible to reconciliation. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(test): use valid source_type 'bank_transaction' in pg-real fixture CI pg-real failed because 'bank_import' is not in the journal_entries source_type CHECK constraint. The valid value for bank-imported transactions is 'bank_transaction' (per migration 20260513170001 + earlier). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * docs(reconciliation): deprecate gl_1930_balance on ReconciliationStatus Mark the IB-inclusive balance field as @deprecated on both the server-side ReconciliationStatus interface and the component-side mirror. Downstream consumers that read this field expecting it to drive the diff will be off by the IB amount whenever a SIE-imported opening balance exists on 1930. The new gl_1930_period_movement field is the correct basis for the diff and is what the difference field is computed against. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * refactor(reconciliation): tighten review feedback on PR #485 Three tweaks from the Swedish-accounting bot review: 1. Drop the gl_1930_period_movement ?? gl_1930_balance fallback in BankReconciliationView. gnubok has no persisted reconciliation snapshots and Vercel deploys atomically, so the old-server/new-client case can't occur — the fallback was defensive code for an impossible state. Per CLAUDE.md: don't add back-compat shims when you can just change the code. gl_1930_period_movement and gl_1930_opening_balance are now required on the type. 2. Clarify in the migration comment that source_type='opening_balance' is reserved for the fiscal-year IB voucher (always at period_start). The bot questioned whether the unconditional filter could hide a mid-year corrective IB entry; document the invariant — mid-year corrections use source_type='correction' or 'manual', never 'opening_balance'. 3. Test fixture 2099 → 2091. 2099 is årets resultat (current year); 2091 is balanserad vinst/förlust, the realistic counterpart for a carried-forward bank IB. RPC filters on 1930 only so this is cosmetic but matches what a real SIE import would produce. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
cad83d180d
commit
5eee06a56d
@@ -0,0 +1,168 @@
|
||||
/**
|
||||
* pg-real test for get_unlinked_1930_lines (PR 3 of erp-mafia/gnubok#443).
|
||||
*
|
||||
* Verifies the RPC excludes opening_balance vouchers from the unmatched-1930
|
||||
* set, while preserving the existing behavior for posted bank-import vouchers,
|
||||
* date-range filtering, and company scoping.
|
||||
*
|
||||
* Migration: 20260514132534_unlinked_1930_lines_exclude_opening_balance.sql
|
||||
*/
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { getPool } from './setup'
|
||||
import { insertAuthUser, insertCompany, insertFiscalPeriod } from './fixtures'
|
||||
|
||||
async function insertPostedJournalEntry(params: {
|
||||
userId: string
|
||||
companyId: string
|
||||
fiscalPeriodId: string
|
||||
entryDate: string
|
||||
sourceType: 'opening_balance' | 'manual' | 'bank_transaction' | 'import'
|
||||
voucherNumber: number
|
||||
amount?: number
|
||||
}): Promise<string> {
|
||||
const id = randomUUID()
|
||||
const amount = params.amount ?? 1000
|
||||
// Insert as posted directly. This bypasses commit_journal_entry's voucher
|
||||
// sequencing; that's fine for testing the read-side RPC, which only cares
|
||||
// about (account_number, status, source_type, date_range, link presence).
|
||||
await getPool().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', $6, $7, $8, 'posted')`,
|
||||
[
|
||||
id,
|
||||
params.userId,
|
||||
params.companyId,
|
||||
params.fiscalPeriodId,
|
||||
params.voucherNumber,
|
||||
params.entryDate,
|
||||
`Test ${params.sourceType}`,
|
||||
params.sourceType,
|
||||
],
|
||||
)
|
||||
// Balanced pair on 1930 + 2091 (balanserad vinst/förlust — the realistic
|
||||
// carried-forward counterpart for an IB on a bank account; harmless for the
|
||||
// other source_types where the test only cares about the 1930 side).
|
||||
await getPool().query(
|
||||
`INSERT INTO public.journal_entry_lines
|
||||
(journal_entry_id, account_number, debit_amount, credit_amount)
|
||||
VALUES ($1, '1930', $2, 0),
|
||||
($1, '2091', 0, $2)`,
|
||||
[id, amount],
|
||||
)
|
||||
return id
|
||||
}
|
||||
|
||||
describe('get_unlinked_1930_lines RPC — opening_balance exclusion', () => {
|
||||
it('excludes opening_balance vouchers from the unmatched-1930 set', async () => {
|
||||
const userId = await insertAuthUser()
|
||||
const companyId = await insertCompany({ createdBy: userId })
|
||||
const fiscalPeriodId = await insertFiscalPeriod({
|
||||
userId,
|
||||
companyId,
|
||||
periodStart: '2026-01-01',
|
||||
periodEnd: '2026-12-31',
|
||||
})
|
||||
|
||||
// Three vouchers on 1930, all posted, none linked to a transaction:
|
||||
// IB voucher (source_type='opening_balance') — should be EXCLUDED
|
||||
// Bank import voucher — should be RETURNED
|
||||
// Manual voucher — should be RETURNED
|
||||
await insertPostedJournalEntry({
|
||||
userId, companyId, fiscalPeriodId,
|
||||
entryDate: '2026-01-01',
|
||||
sourceType: 'opening_balance',
|
||||
voucherNumber: 1,
|
||||
amount: 50000,
|
||||
})
|
||||
const bankEntryId = await insertPostedJournalEntry({
|
||||
userId, companyId, fiscalPeriodId,
|
||||
entryDate: '2026-03-15',
|
||||
sourceType: 'bank_transaction',
|
||||
voucherNumber: 2,
|
||||
amount: 1500,
|
||||
})
|
||||
const manualEntryId = await insertPostedJournalEntry({
|
||||
userId, companyId, fiscalPeriodId,
|
||||
entryDate: '2026-04-20',
|
||||
sourceType: 'manual',
|
||||
voucherNumber: 3,
|
||||
amount: 200,
|
||||
})
|
||||
|
||||
const { rows } = await getPool().query(
|
||||
`SELECT journal_entry_id, source_type FROM public.get_unlinked_1930_lines($1)`,
|
||||
[companyId],
|
||||
)
|
||||
|
||||
const returnedIds = new Set(rows.map((r) => r.journal_entry_id))
|
||||
expect(returnedIds.has(bankEntryId)).toBe(true)
|
||||
expect(returnedIds.has(manualEntryId)).toBe(true)
|
||||
// IB voucher should NOT be returned regardless of company/date scope.
|
||||
expect(rows.find((r) => r.source_type === 'opening_balance')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('still applies date_from / date_to filtering', async () => {
|
||||
const userId = await insertAuthUser()
|
||||
const companyId = await insertCompany({ createdBy: userId })
|
||||
const fiscalPeriodId = await insertFiscalPeriod({
|
||||
userId,
|
||||
companyId,
|
||||
periodStart: '2026-01-01',
|
||||
periodEnd: '2026-12-31',
|
||||
})
|
||||
|
||||
await insertPostedJournalEntry({
|
||||
userId, companyId, fiscalPeriodId,
|
||||
entryDate: '2026-02-01',
|
||||
sourceType: 'bank_transaction',
|
||||
voucherNumber: 10,
|
||||
})
|
||||
await insertPostedJournalEntry({
|
||||
userId, companyId, fiscalPeriodId,
|
||||
entryDate: '2026-08-01',
|
||||
sourceType: 'bank_transaction',
|
||||
voucherNumber: 11,
|
||||
})
|
||||
|
||||
// Window covers only the second voucher.
|
||||
const { rows } = await getPool().query(
|
||||
`SELECT entry_date FROM public.get_unlinked_1930_lines($1, $2, $3) ORDER BY entry_date`,
|
||||
[companyId, '2026-07-01', '2026-12-31'],
|
||||
)
|
||||
|
||||
expect(rows).toHaveLength(1)
|
||||
expect(rows[0].entry_date.toISOString().slice(0, 10)).toBe('2026-08-01')
|
||||
})
|
||||
|
||||
it('scopes to the requested company only', async () => {
|
||||
const userA = await insertAuthUser()
|
||||
const userB = await insertAuthUser()
|
||||
const companyA = await insertCompany({ createdBy: userA, name: 'A' })
|
||||
const companyB = await insertCompany({ createdBy: userB, name: 'B' })
|
||||
const fpA = await insertFiscalPeriod({ userId: userA, companyId: companyA })
|
||||
const fpB = await insertFiscalPeriod({ userId: userB, companyId: companyB })
|
||||
|
||||
await insertPostedJournalEntry({
|
||||
userId: userA, companyId: companyA, fiscalPeriodId: fpA,
|
||||
entryDate: '2026-03-01', sourceType: 'bank_transaction', voucherNumber: 1,
|
||||
})
|
||||
await insertPostedJournalEntry({
|
||||
userId: userB, companyId: companyB, fiscalPeriodId: fpB,
|
||||
entryDate: '2026-03-01', sourceType: 'bank_transaction', voucherNumber: 1,
|
||||
})
|
||||
|
||||
const { rows: rowsA } = await getPool().query(
|
||||
`SELECT 1 FROM public.get_unlinked_1930_lines($1)`,
|
||||
[companyA],
|
||||
)
|
||||
const { rows: rowsB } = await getPool().query(
|
||||
`SELECT 1 FROM public.get_unlinked_1930_lines($1)`,
|
||||
[companyB],
|
||||
)
|
||||
expect(rowsA).toHaveLength(1)
|
||||
expect(rowsB).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user