Files
accounted/lib/reports/trial-balance.ts
T
Jakob WennbergandClaude Opus 4.6 3e82295cce feat: semi-manual invoice payment booking dialog (#37)
* fix: include reversed entries in all reports (general ledger, trial balance, VAT, SIE, NE, INK2)

Reversed entries (storno) must appear alongside their original posted entries
in reports for a complete audit trail. Previously, filtering by status='posted'
excluded them, causing discrepancies when corrections had been made.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* feat: semi-manual invoice payment booking with editable journal lines

When marking an invoice as paid, users now see a dialog where they can:
- Choose which bank/cash account the payment goes to (1910, 1920, 1930, etc.)
- Review and edit the proposed journal entry lines before committing
- The happy path remains fast — lines are pre-filled correctly

Implementation:
- Pure proposePaymentLines() function for line computation (accrual + cash)
- PaymentBookingDialog with AccountCombobox, balance validation, date picker
- API accepts optional custom lines, falls back to auto-generation without them
- 18 tests (8 unit + 10 API) all passing

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: address Greptile review — validation fallback, balance check, error handling

- P1: Return 400 on invalid body instead of silently falling back to
  auto-generated lines (split JSON parse from schema validation)
- P1: Add server-side balance check for custom lines before committing
  (debit must equal credit, totalDebit > 0)
- P2: Wrap PaymentBookingDialog init() in try/catch with toast on
  failure and auto-close instead of silent empty state
- Add 2 new tests: unbalanced lines → 400, invalid schema → 400

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 11:20:29 +01:00

150 lines
4.4 KiB
TypeScript

import type { SupabaseClient } from '@supabase/supabase-js'
import { fetchAllRows } from '@/lib/supabase/fetch-all'
import type { TrialBalanceRow } from '@/types'
/**
* Generate trial balance (Saldobalans) for a fiscal period
*
* Aggregates all posted journal entry lines grouped by account number,
* filtered by fiscal period. Verifies total debits = total credits.
*/
export async function generateTrialBalance(
supabase: SupabaseClient,
userId: string,
fiscalPeriodId: string
): Promise<{
rows: TrialBalanceRow[]
totalDebit: number
totalCredit: number
isBalanced: boolean
}> {
// Get all posted journal entry lines for this period, grouped by account
const { data, error } = await supabase.rpc('generate_trial_balance', {
p_user_id: userId,
p_fiscal_period_id: fiscalPeriodId,
})
if (error) {
// Fallback: manual aggregation via SQL
return generateTrialBalanceManual(supabase, userId, fiscalPeriodId)
}
if (data) {
const rows = data as TrialBalanceRow[]
const totalDebit = rows.reduce((sum, r) => sum + r.closing_debit, 0)
const totalCredit = rows.reduce((sum, r) => sum + r.closing_credit, 0)
return {
rows,
totalDebit: Math.round(totalDebit * 100) / 100,
totalCredit: Math.round(totalCredit * 100) / 100,
isBalanced: Math.abs(totalDebit - totalCredit) < 0.01,
}
}
return generateTrialBalanceManual(supabase, userId, fiscalPeriodId)
}
/**
* Manual trial balance generation using direct queries
*/
async function generateTrialBalanceManual(
supabase: SupabaseClient,
userId: string,
fiscalPeriodId: string
): Promise<{
rows: TrialBalanceRow[]
totalDebit: number
totalCredit: number
isBalanced: boolean
}> {
// Get all journal entry lines for posted entries in this period
const { data: entries, error: entriesError } = await supabase
.from('journal_entries')
.select('id')
.eq('user_id', userId)
.eq('fiscal_period_id', fiscalPeriodId)
.in('status', ['posted', 'reversed'])
if (entriesError || !entries || entries.length === 0) {
return { rows: [], totalDebit: 0, totalCredit: 0, isBalanced: true }
}
const entryIds = entries.map((e) => e.id)
const { data: lines, error: linesError } = await supabase
.from('journal_entry_lines')
.select('account_number, debit_amount, credit_amount')
.in('journal_entry_id', entryIds)
if (linesError || !lines) {
return { rows: [], totalDebit: 0, totalCredit: 0, isBalanced: true }
}
// Get account names
const accounts = await fetchAllRows<{ account_number: string; account_name: string; account_class: number }>(({ from, to }) =>
supabase
.from('chart_of_accounts')
.select('account_number, account_name, account_class')
.eq('user_id', userId)
.range(from, to)
)
const accountMap = new Map<string, { name: string; class: number }>()
for (const acc of accounts) {
accountMap.set(acc.account_number, {
name: acc.account_name,
class: acc.account_class,
})
}
// Aggregate by account
const balances = new Map<
string,
{ debit: number; credit: number }
>()
for (const line of lines) {
const existing = balances.get(line.account_number) || { debit: 0, credit: 0 }
existing.debit += Number(line.debit_amount) || 0
existing.credit += Number(line.credit_amount) || 0
balances.set(line.account_number, existing)
}
// Build rows
const rows: TrialBalanceRow[] = []
for (const [accountNumber, balance] of balances) {
const accountInfo = accountMap.get(accountNumber) || {
name: `Konto ${accountNumber}`,
class: parseInt(accountNumber[0]) || 0,
}
rows.push({
account_number: accountNumber,
account_name: accountInfo.name,
account_class: accountInfo.class,
opening_debit: 0,
opening_credit: 0,
period_debit: Math.round(balance.debit * 100) / 100,
period_credit: Math.round(balance.credit * 100) / 100,
closing_debit: Math.round(balance.debit * 100) / 100,
closing_credit: Math.round(balance.credit * 100) / 100,
})
}
// Sort by account number
rows.sort((a, b) => a.account_number.localeCompare(b.account_number))
const totalDebit = Math.round(rows.reduce((sum, r) => sum + r.closing_debit, 0) * 100) / 100
const totalCredit = Math.round(rows.reduce((sum, r) => sum + r.closing_credit, 0) * 100) / 100
return {
rows,
totalDebit,
totalCredit,
isBalanced: Math.abs(totalDebit - totalCredit) < 0.01,
}
}