import type { SubledgerReport, AccountPosting, } from '@brazil-swift-ops/types'; import { getPostingStore } from './posting'; import { getAccountStore } from './accounts'; export function generateSubledgerReport( subledgerId: string, periodStart: Date, periodEnd: Date ): SubledgerReport { const accountStore = getAccountStore(); const postingStore = getPostingStore(); const subledger = accountStore.get(subledgerId); if (!subledger || subledger.type !== 'subledger') { throw new Error(`Subledger account ${subledgerId} not found`); } const postings = postingStore .getByAccount(subledgerId) .filter( (p) => p.postedAt >= periodStart && p.postedAt <= periodEnd ); const openingBalance = subledger.balance - postings.reduce((sum, p) => { return p.postingType === 'debit' ? sum - p.amount : sum + p.amount; }, 0); const totalDebits = postings .filter((p) => p.postingType === 'debit') .reduce((sum, p) => sum + p.amount, 0); const totalCredits = postings .filter((p) => p.postingType === 'credit') .reduce((sum, p) => sum + p.amount, 0); const closingBalance = openingBalance + totalCredits - totalDebits; const netPosition = totalCredits - totalDebits; return { subledgerId, periodStart, periodEnd, openingBalance, closingBalance, totalDebits, totalCredits, netPosition, currency: subledger.currency, transactionCount: new Set(postings.map((p) => p.transactionId)).size, postings, }; }