* 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
@@ -10,6 +10,7 @@ import {
|
||||
runReconciliation,
|
||||
manualLink,
|
||||
unlinkReconciliation,
|
||||
getReconciliationStatus,
|
||||
} from '../bank-reconciliation'
|
||||
import type { UnlinkedGLLine } from '../bank-reconciliation'
|
||||
import { makeTransaction } from '@/tests/helpers'
|
||||
@@ -554,3 +555,134 @@ describe('unlinkReconciliation', () => {
|
||||
expect(result.success).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
// ============================================================
|
||||
// getReconciliationStatus — IB exclusion (PR 3 of #443)
|
||||
// ============================================================
|
||||
|
||||
describe('getReconciliationStatus', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
eventBus.clear()
|
||||
})
|
||||
|
||||
function createQueueMockSupabase() {
|
||||
const resultQueue: { data: unknown; error: unknown }[] = []
|
||||
const enqueue = (...results: { data?: unknown; error?: unknown }[]) => {
|
||||
for (const r of results) resultQueue.push({ data: r.data ?? null, error: r.error ?? null })
|
||||
}
|
||||
const buildChain = (): unknown => {
|
||||
const handler: ProxyHandler<object> = {
|
||||
get(_target, prop) {
|
||||
if (prop === 'then') {
|
||||
const next = resultQueue.shift() ?? { data: null, error: null }
|
||||
return (resolve: (v: unknown) => void) => resolve(next)
|
||||
}
|
||||
return (..._args: unknown[]) => buildChain()
|
||||
},
|
||||
}
|
||||
return new Proxy({}, handler)
|
||||
}
|
||||
const supabase = {
|
||||
from: vi.fn().mockImplementation(() => buildChain()),
|
||||
rpc: vi.fn().mockImplementation(() => buildChain()),
|
||||
}
|
||||
return { supabase, enqueue }
|
||||
}
|
||||
|
||||
it('reports is_reconciled=true when only the IB voucher is unmatched on 1930', async () => {
|
||||
const { supabase, enqueue } = createQueueMockSupabase()
|
||||
|
||||
// 1) transactions: 1000 SEK matched (journal_entry_id set)
|
||||
enqueue({
|
||||
data: [{ amount: 1000, journal_entry_id: 'je-tx', reconciliation_method: 'auto_exact' }],
|
||||
})
|
||||
// 2) journal_entry_lines: 50,000 IB debit + 1000 matched debit on 1930
|
||||
enqueue({
|
||||
data: [
|
||||
{ debit_amount: 50000, credit_amount: 0, journal_entries: { source_type: 'opening_balance' } },
|
||||
{ debit_amount: 1000, credit_amount: 0, journal_entries: { source_type: 'bank_import' } },
|
||||
],
|
||||
})
|
||||
// 3) RPC get_unlinked_1930_lines: returns empty (RPC excludes IB after migration)
|
||||
enqueue({ data: [] })
|
||||
|
||||
const status = await getReconciliationStatus(supabase as never, 'company-1')
|
||||
|
||||
expect(status.gl_1930_balance).toBe(51000) // includes IB
|
||||
expect(status.gl_1930_period_movement).toBe(1000) // excludes IB
|
||||
expect(status.gl_1930_opening_balance).toBe(50000)
|
||||
expect(status.bank_transaction_total).toBe(1000)
|
||||
expect(status.difference).toBe(0)
|
||||
expect(status.is_reconciled).toBe(true)
|
||||
expect(status.unmatched_gl_line_count).toBe(0)
|
||||
})
|
||||
|
||||
it('reports is_reconciled=false and a non-zero difference when a real bank tx is unmatched', async () => {
|
||||
const { supabase, enqueue } = createQueueMockSupabase()
|
||||
|
||||
// 1) transactions: 1500 total, only 1000 matched
|
||||
enqueue({
|
||||
data: [
|
||||
{ amount: 1000, journal_entry_id: 'je-1', reconciliation_method: null },
|
||||
{ amount: 500, journal_entry_id: null, reconciliation_method: null },
|
||||
],
|
||||
})
|
||||
// 2) GL lines: 50,000 IB + 1000 booked
|
||||
enqueue({
|
||||
data: [
|
||||
{ debit_amount: 50000, credit_amount: 0, journal_entries: { source_type: 'opening_balance' } },
|
||||
{ debit_amount: 1000, credit_amount: 0, journal_entries: { source_type: 'bank_import' } },
|
||||
],
|
||||
})
|
||||
// 3) RPC: empty
|
||||
enqueue({ data: [] })
|
||||
|
||||
const status = await getReconciliationStatus(supabase as never, 'company-1')
|
||||
|
||||
expect(status.bank_transaction_total).toBe(1500)
|
||||
expect(status.gl_1930_period_movement).toBe(1000)
|
||||
expect(status.gl_1930_opening_balance).toBe(50000)
|
||||
expect(status.difference).toBe(500) // bank > GL period movement
|
||||
expect(status.is_reconciled).toBe(false)
|
||||
expect(status.unmatched_transaction_count).toBe(1)
|
||||
})
|
||||
|
||||
it('handles companies with no IB on 1930 (period_movement === gl_balance)', async () => {
|
||||
const { supabase, enqueue } = createQueueMockSupabase()
|
||||
|
||||
enqueue({ data: [{ amount: 100, journal_entry_id: 'je-1', reconciliation_method: 'auto_exact' }] })
|
||||
enqueue({
|
||||
data: [{ debit_amount: 100, credit_amount: 0, journal_entries: { source_type: 'bank_import' } }],
|
||||
})
|
||||
enqueue({ data: [] })
|
||||
|
||||
const status = await getReconciliationStatus(supabase as never, 'company-1')
|
||||
|
||||
expect(status.gl_1930_opening_balance).toBe(0)
|
||||
expect(status.gl_1930_period_movement).toBe(100)
|
||||
expect(status.gl_1930_balance).toBe(100)
|
||||
expect(status.difference).toBe(0)
|
||||
expect(status.is_reconciled).toBe(true)
|
||||
})
|
||||
|
||||
it('handles array-shaped journal_entries embed (Supabase wide typing)', async () => {
|
||||
// Supabase typings sometimes widen embedded relations to arrays. The
|
||||
// implementation handles both shapes — verify here.
|
||||
const { supabase, enqueue } = createQueueMockSupabase()
|
||||
|
||||
enqueue({ data: [] })
|
||||
enqueue({
|
||||
data: [
|
||||
{ debit_amount: 1000, credit_amount: 0, journal_entries: [{ source_type: 'opening_balance' }] },
|
||||
{ debit_amount: 200, credit_amount: 0, journal_entries: [{ source_type: 'bank_import' }] },
|
||||
],
|
||||
})
|
||||
enqueue({ data: [] })
|
||||
|
||||
const status = await getReconciliationStatus(supabase as never, 'company-1')
|
||||
|
||||
expect(status.gl_1930_opening_balance).toBe(1000)
|
||||
expect(status.gl_1930_period_movement).toBe(200)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -36,7 +36,21 @@ export interface ReconciliationRunResult {
|
||||
|
||||
export interface ReconciliationStatus {
|
||||
bank_transaction_total: number
|
||||
/**
|
||||
* @deprecated Use `gl_1930_period_movement` for the reconciliation diff. This
|
||||
* field is preserved for back-compat with persisted status snapshots produced
|
||||
* before the IB-exclusion change; new consumers reading this to compute the
|
||||
* "real" difference will be off by the IB amount whenever a SIE-imported
|
||||
* opening balance exists on 1930. The `difference` field on this interface
|
||||
* is computed against `gl_1930_period_movement`, not this.
|
||||
*/
|
||||
gl_1930_balance: number
|
||||
/** Ledger movement on 1930 excluding source_type='opening_balance' lines. */
|
||||
gl_1930_period_movement: number
|
||||
/** IB on 1930 within the date range — surfaced separately so reconciliation
|
||||
* doesn't treat it as an unmatched bank transaction. */
|
||||
gl_1930_opening_balance: number
|
||||
/** bankTotal − gl_1930_period_movement. Zero when every period transaction is matched. */
|
||||
difference: number
|
||||
is_reconciled: boolean
|
||||
matched_count: number
|
||||
@@ -232,10 +246,13 @@ export async function getReconciliationStatus(
|
||||
|
||||
const { data: transactions } = await txQuery
|
||||
|
||||
// Get GL bank account lines (all, not just unlinked)
|
||||
// Get GL bank account lines (all, not just unlinked). Pull source_type
|
||||
// from the join so we can split IB out of the period-movement comparison —
|
||||
// an opening_balance line on 1930 is the prior year's closing balance, not
|
||||
// a bank transaction we should expect to match.
|
||||
let glQuery = supabase
|
||||
.from('journal_entry_lines')
|
||||
.select('debit_amount, credit_amount, journal_entries!inner(company_id, entry_date, status)')
|
||||
.select('debit_amount, credit_amount, journal_entries!inner(company_id, entry_date, status, source_type)')
|
||||
.eq('account_number', bankAccount)
|
||||
.eq('journal_entries.company_id', companyId)
|
||||
.eq('journal_entries.status', 'posted')
|
||||
@@ -245,16 +262,38 @@ export async function getReconciliationStatus(
|
||||
|
||||
const { data: glLines } = await glQuery
|
||||
|
||||
type GlLineRow = {
|
||||
debit_amount: number | string | null
|
||||
credit_amount: number | string | null
|
||||
journal_entries: { source_type?: string | null } | { source_type?: string | null }[] | null
|
||||
}
|
||||
function isOpeningBalance(line: GlLineRow): boolean {
|
||||
const je = line.journal_entries
|
||||
if (!je) return false
|
||||
// Supabase typings sometimes widen embedded relations to arrays even when
|
||||
// the join is one-to-one. Handle both shapes defensively.
|
||||
const sourceType = Array.isArray(je) ? je[0]?.source_type : je.source_type
|
||||
return sourceType === 'opening_balance'
|
||||
}
|
||||
|
||||
// Calculate totals
|
||||
const bankTotal = (transactions || []).reduce(
|
||||
(sum, tx) => sum + (Number(tx.amount) || 0),
|
||||
0
|
||||
)
|
||||
|
||||
const glBalance = (glLines || []).reduce(
|
||||
const allLines = (glLines || []) as GlLineRow[]
|
||||
const glBalance = allLines.reduce(
|
||||
(sum, line) => sum + (Number(line.debit_amount) || 0) - (Number(line.credit_amount) || 0),
|
||||
0
|
||||
)
|
||||
const glOpeningBalance = allLines
|
||||
.filter(isOpeningBalance)
|
||||
.reduce(
|
||||
(sum, line) => sum + (Number(line.debit_amount) || 0) - (Number(line.credit_amount) || 0),
|
||||
0
|
||||
)
|
||||
const glPeriodMovement = glBalance - glOpeningBalance
|
||||
|
||||
const matchedCount = (transactions || []).filter(
|
||||
(tx) => tx.journal_entry_id !== null
|
||||
@@ -264,14 +303,17 @@ export async function getReconciliationStatus(
|
||||
(tx) => tx.journal_entry_id === null
|
||||
).length
|
||||
|
||||
// Unlinked GL lines count
|
||||
// Unlinked GL lines count (RPC excludes source_type='opening_balance' since
|
||||
// 20260514132534_unlinked_1930_lines_exclude_opening_balance.sql)
|
||||
const unlinkedLines = await fetchUnlinkedGLLines(supabase, companyId, dateFrom, dateTo)
|
||||
|
||||
const difference = Math.round((bankTotal - glBalance) * 100) / 100
|
||||
const difference = Math.round((bankTotal - glPeriodMovement) * 100) / 100
|
||||
|
||||
return {
|
||||
bank_transaction_total: Math.round(bankTotal * 100) / 100,
|
||||
gl_1930_balance: Math.round(glBalance * 100) / 100,
|
||||
gl_1930_period_movement: Math.round(glPeriodMovement * 100) / 100,
|
||||
gl_1930_opening_balance: Math.round(glOpeningBalance * 100) / 100,
|
||||
difference,
|
||||
is_reconciled: Math.abs(difference) < 0.01,
|
||||
matched_count: matchedCount,
|
||||
|
||||
Reference in New Issue
Block a user