/** * The books' accounts: the chart's, and the individual accounts the books * declare on the numbers DATEV leaves free after one of its accounts * (`IAccountPolicy.individualAccounts`), resolved through @fin.cx/chartdata's * `BooksChart`. An individual account is what its parent is — its type, its * HGB position, its UStVA and Anlage EÜR mapping, its tax rule — without the * parent's DATEV tax automatism. The core's checks of what an account is ask * here, so a booking on an individual account is checked as one on its parent. */ import * as plugins from './core.plugins.js'; import type { IAccountPolicy } from './core.accountpolicy.js'; import type { TSKRType } from './core.types.js'; import { chartOf } from './core.chart.js'; import { CHART_PROFIT_AND_LOSS_ACCOUNTS, CHART_VAT_ACCOUNTS } from './core.chartaccounts.js'; /** The year of the @fin.cx/chartdata datasets the core's chart tables are generated from (`tools/generate-chart-data.ts`). */ export const CHART_DATA_YEAR = 2026; const STANDARD_CHARTS = new Map(); /** * The chart of the policy with the books' individual accounts. Throws * chartdata's `IndividualAccountsError` for declarations it refuses (a number * DATEV assigns, a parent the number does not follow, …): the books' settings * are wrong, not a booking. */ export function booksChartOf(policy: Pick): plugins.chartdata.BooksChart { const chart = chartOf(policy); const declarations = policy.individualAccounts ?? []; if (declarations.length > 0) { return plugins.chartdata.BooksChart.of(chart, CHART_DATA_YEAR, declarations); } let standard = STANDARD_CHARTS.get(chart); if (standard === undefined) { standard = plugins.chartdata.BooksChart.of(chart, CHART_DATA_YEAR); STANDARD_CHARTS.set(chart, standard); } return standard; } /** The books' individual accounts by number; empty for a policy that declares none. */ export function individualAccountsOf(policy: Pick): ReadonlyMap { if ((policy.individualAccounts ?? []).length === 0) return new Map(); return new Map(booksChartOf(policy).individualAccounts.map((account) => [account.accountNumber, account])); } /** * Whether an account is an expense (`expense`) or income (`income`) account of * the books: one of the chart's profit-and-loss accounts * (`CHART_PROFIT_AND_LOSS_ACCOUNTS`: type `expense` or `revenue` with an * income-statement position), or an individual account under one. */ export function isProfitAndLossAccount(policy: IAccountPolicy, accountNumber: string, kind: 'expense' | 'income'): boolean { const table = CHART_PROFIT_AND_LOSS_ACCOUNTS[chartOf(policy)]; if ((kind === 'expense' ? table.expense : table.income).includes(accountNumber)) return true; const own = individualAccountsOf(policy).get(accountNumber); return own !== undefined && own.hgb275Position !== undefined && own.accountType === (kind === 'expense' ? 'expense' : 'revenue'); } /** * The account of the chart whose tie to a tax a booking on an account is * checked against (`accountRateMismatchOf`, the 0 % revenue, the revenue taxed * in another member state) and whose VAT a closed year reclassifies with * (`vatOfPastYearsReclassified`): the account itself, or, for an individual account * that keeps its parent's UStVA mapping, its parent — the account it is under, * whatever its own automatism. An individual account whose declaration changes * its UStVA mapping stands for itself. */ export function vatCheckAccountOf(policy: IAccountPolicy, accountNumber: string): string { const declaration = (policy.individualAccounts ?? []).find((declarationArg) => declarationArg.accountNumber === accountNumber); if (declaration === undefined || declaration.overrides?.ustva !== undefined) return accountNumber; // the declaration is the books' only where chartdata takes it return individualAccountsOf(policy).get(accountNumber)?.parentAccountNumber ?? accountNumber; } /** * Whether the chart marks an account for the UStVA or with a DATEV tax * automatism (`CHART_VAT_ACCOUNTS`), or it is an individual account that takes * its parent's UStVA mapping. */ export function isVatMarkedAccount(policy: IAccountPolicy, accountNumber: string): boolean { return CHART_VAT_ACCOUNTS[chartOf(policy)].includes(accountNumber) || individualAccountsOf(policy).get(accountNumber)?.ustva !== undefined; }