/** * The ledger as pure functions over journal entries: the lines of every * entry, the trial balance (Summen- und Saldenliste), the sheet of one * account (Kontenblatt) and the open items of the personal accounts. Nothing * here is stored or read from a database: the caller hands in the entries — * journal drafts of the recipes, or posted entries of any ledger — and gets * the figures back, in exact integer cents. * * What the functions refuse, because a figure built from it would be false: * an entry whose debits and credits differ, an amount that is not a whole * number of cents, an entry id given twice (it would be counted twice), a * date that is no date, a period that is no day or ends before it begins. * * Days are UTC calendar days (`date.toISOString().slice(0, 10)`), as the * recipes write them and as the rest of the core reads them. */ import type { IAccountPolicy } from './core.accountpolicy.js'; import { type IOpenItem, type TOposKind } from './core.openitems.js'; import type { IJournalLineDraft, TBuKey, TLineSide } from './core.types.js'; /** One journal entry as the ledger reads it: a draft of the recipes with an id, or a posted entry. */ export interface ILedgerEntryInput { /** unique across the entries handed in */ id: string; date: Date; description?: string; /** the entry's reference; a line without its own Belegfeld 1 falls back to it for its open item */ reference?: string; /** a general reversal: every line keeps its side and counts negative there (see `IJournalDraft.generalumkehr`) */ generalumkehr?: boolean; /** an opening entry (EB-Werte): its lines are the accounts' opening balances, not turnover */ opening?: boolean; /** * Voranmeldungszeitraum, `YYYY-MM` as `IPostedEntryPayload.vatPeriod`: the * VAT month the entry is declared in, which a correction of an earlier * return names (§ 16 Abs. 1 Satz 2 UStG) though its date lies later. * Absent: the month of `date`. Read by `computeUstVerprobung` only. */ vatPeriod?: string; lines: IJournalLineDraft[]; } /** One line of the ledger: a line of an entry, on the side it books, with its amount positive. */ export interface ILedgerLine { entryId: string; /** `YYYY-MM-DD`, UTC */ date: string; accountNumber: string; counterAccount: string; side: TLineSide; /** positive */ amountCents: number; /** the line of a Generalumkehr: it stays on its side and counts negative there */ generalumkehr: boolean; /** the line of an opening entry */ opening: boolean; /** debit positive, credit negative; a Generalumkehr line the other way round */ signedCents: number; buKey: TBuKey; buchungstext: string; belegfeld1?: string; belegfeld2?: string; /** KOST1, the line's cost centre, as the entry states it */ costCenter1?: string; /** KOST2, the line's second cost centre (a project, for example), as the entry states it */ costCenter2?: string; reference?: string; docLink?: string; } /** An opening balance (Saldovortrag / EB-Wert) of one account: debit positive. */ export interface IOpeningBalance { accountNumber: string; signedCents: number; } /** Which days the figures are for. Both ends are optional and inclusive, as `YYYY-MM-DD`. */ export interface ILedgerPeriodOptions { /** lines before this day count as brought forward (`prior…`); unset: none are */ periodFrom?: string; /** lines after this day are left out; unset: none are */ periodTo?: string; } export interface ITrialBalanceOptions extends ILedgerPeriodOptions { /** * the opening balances; with none given and no opening entry among the * entries, none are recorded, and every account's `openingCents` is `null`. * Given both, they add up. */ opening?: IOpeningBalance[]; } /** One row of the Summen- und Saldenliste. */ export interface ITrialBalanceAccount { accountNumber: string; /** `null` when no opening balances were handed in at all; `0` for an account without one among them */ openingCents: number | null; /** lines before the period (within the entries handed in) */ priorDebitCents: number; priorCreditCents: number; periodDebitCents: number; periodCreditCents: number; /** prior + period */ cumulativeDebitCents: number; cumulativeCreditCents: number; /** opening + cumulative debit − cumulative credit: debit positive */ balanceCents: number; } export interface ITrialBalance { periodFrom?: string; periodTo?: string; /** by account number: numbers of digits by their value, of equal value by their text; other numbers after them, by their text */ accounts: ITrialBalanceAccount[]; hasOpeningBalances: boolean; /** the sum of the opening balances; a set that balances sums to 0 */ openingDifferenceCents: number; /** over the cumulative columns */ totalDebitCents: number; totalCreditCents: number; /** debits equal credits and the opening balances sum to 0 */ isBalanced: boolean; } export interface IAccountSheetOptions extends ILedgerPeriodOptions { /** * the account's opening balance, debit positive; with none given and no * opening entry among the entries, none is recorded. Given both, they add up. */ openingCents?: number; } export interface IAccountSheetLine { entryId: string; date: string; counterAccount: string; buchungstext: string; belegfeld1?: string; reference?: string; docLink?: string; buKey: TBuKey; /** negative on a Generalumkehr line booked in Soll */ debitCents: number; /** negative on a Generalumkehr line booked in Haben */ creditCents: number; /** the running balance after this line, debit positive */ balanceCents: number; generalumkehr: boolean; } /** The Kontenblatt of one account for a period. */ export interface IAccountSheet { accountNumber: string; periodFrom?: string; periodTo?: string; /** `null` when no opening balance is recorded */ openingCents: number | null; /** the signed sum of the lines before the period, within the entries handed in */ carriedCents: number; /** the lines of the period, by day and then in the order the entries came in */ lines: IAccountSheetLine[]; debitCents: number; creditCents: number; /** opening (or 0) + carried + period debit − period credit */ closingCents: number; } export interface IOpenItemsOptions { kind?: TOposKind; includeSettled?: boolean; /** only lines up to and including this day, `YYYY-MM-DD` */ asOf?: string; } /** The period of the options, its days checked: a day as `YYYY-MM-DD` that exists, and an end on or after the beginning. */ export declare const ledgerPeriodOf: (optionsArg: ILedgerPeriodOptions | undefined) => { from?: string; to?: string; }; /** * Every line of every entry, in the order the entries and their lines came * in. A negative amount books on the other side, as DATEV reads it; a line of * 0 is no line. The lines of a Generalumkehr keep their sides and count * negative there. Refuses an entry that does not balance, an amount that is * not a whole number of cents, an id given twice, a date that is no date and * a Generalumkehr with a negative amount. */ export declare function ledgerLinesOf(entries: readonly ILedgerEntryInput[]): ILedgerLine[]; /** * The Summen- und Saldenliste of the entries: per account the opening * balance, what was booked before the period and in it, the cumulated sums * and the balance. Accounts that carry only an opening balance are listed. * The accounts are listed by account number, a number of digits by its * value: the general-ledger accounts (four digits) first, then the debtors * (10000–69999) and the creditors (70000–99999); of equal value (`0027`, * `27`) by the text, and a number with other characters after them all. * A Generalumkehr line lowers the sum of its side, so an entry and its * Generalumkehr leave the turnover as if neither had been booked. The lines * of an opening entry (up to the period's end) are the opening balances, not * turnover. */ export declare function trialBalanceOf(entries: readonly ILedgerEntryInput[], options?: ITrialBalanceOptions): ITrialBalance; /** * The Kontenblatt of one account: its lines in the period, by day and then in * the order the entries came in, each with the running balance, which starts * from the opening balance and what was booked before the period. A * Generalumkehr line stands on its side with a negative amount. The lines of * an opening entry (up to the period's end) are the opening balance. */ export declare function accountSheetOf(entries: readonly ILedgerEntryInput[], accountNumber: string, options?: IAccountSheetOptions): IAccountSheet; /** * The open items of the personal accounts in the entries (see * `deriveOpenItems`): a line without its own Belegfeld 1 falls back to its * entry's reference, as in the database ledger's OPOS. The lines are read by * day and within a day in the order the entries came in, as the database * ledger reads them by date, so an item's first and last day and its aging do * not depend on the order the entries are handed in; its entry ids follow the * days, and within a day the order the entries came in. */ export declare function openItemsOf(entries: readonly ILedgerEntryInput[], policy: IAccountPolicy, options?: IOpenItemsOptions): IOpenItem[];