/** * The pure part of OPOS (Offene-Posten-Buchhaltung): open items derived from * personal-account lines, their aging and how a bank movement settles one. * Nothing here touches a database; `skr.opos.ts` loads the lines of the v2 * ledger and adds the dunning metadata around these functions. * * A line on a personal account (debtors and creditors by the policy's ranges) * belongs to the open item keyed by (account, Belegfeld 1); the item is open * while its signed sum is non-zero. The key is DATEV's: Belegfeld 1 is the * "Rechnungs-/Belegnummer[, w]ird als 'Schlüssel' für den Ausgleich offener * Rechnungen genutzt" (DATEV-Format, Buchungsstapel field 11), so every * settlement — a payment, a credit note, a refund — repeats the INVOICE number * there and lands on the invoice's item. */ import type { IAccountPolicy } from './core.accountpolicy.js'; export type TOposKind = 'debtor' | 'creditor'; export interface IOpenItem { accountNumber: string; kind: TOposKind; /** belegfeld1 of the originating lines — the invoice number */ reference: string; /** original claim (sum of the claim-side lines) */ invoiceCents: number; /** settled so far (sum of the settlement-side lines, credit notes included) */ clearedCents: number; /** * invoiceCents - clearedCents. > 0 is an open claim; < 0 is a credit in the * counterparty's favour — an over-settled item or a credit note beyond what * was still open — which a refund settles (see planOpenItemSettlement). */ openCents: number; firstDate: string; lastDate: string; entryIds: string[]; dunningLevel: number; dueDate?: string; } export interface IAgingBucket { label: string; maxAgeDays: number | null; items: IOpenItem[]; totalCents: number; } /** One personal-account line of a posted entry, the input of the derivation. */ export interface IOposLineRow { entryId: string; accountNumber: string; belegfeld1?: string; side: 'debit' | 'credit'; /** negative for the line of a Generalumkehr: it takes back what its side booked, a claim or a settlement */ amountCents: number; date: Date; } /** Whether an account is a debtor or a creditor account of the policy, or neither. */ export declare function oposKindOf(accountNumber: string, policy: IAccountPolicy): TOposKind | null; /** * Group personal-account lines into open items. Pure. Debtors: claims are * debits, settlements credits; creditors inverse — so a customer credit note * (debtor credit) settles exactly like an incoming payment, which is why it * has to repeat the invoice number in Belegfeld 1. Items with openCents === 0 * are settled and omitted unless includeSettled is set. */ export declare function deriveOpenItems(rows: IOposLineRow[], policy: IAccountPolicy, options?: { kind?: TOposKind; includeSettled?: boolean; }): IOpenItem[]; /** Aging buckets relative to asOf, by days since dueDate (falling back to the item's first posting date). */ export declare function bucketOpenItems(items: IOpenItem[], asOf: Date, bucketDays?: number[]): IAgingBucket[]; /** How a bank movement settles an open item (see planOpenItemSettlement). */ export interface IOpenItemSettlementPlan { /** the movement on the bank account: money in or money out */ direction: 'incoming' | 'outgoing'; /** amount the personal account is cleared by */ expectedCents: number; /** amount that actually moves on the bank account */ paidCents: number; /** the movement covers only part of the outstanding amount */ partial: boolean; } /** * Decide direction and amounts for settling an open item with a bank * movement. Pure — the posting is done by the caller. * * The sign of `openCents` picks the direction, not the kind of the account * alone: a debtor's open claim is collected (incoming), but a debtor item that * a credit note pushed into a CREDIT is money we owe the customer, so it is * settled by a refund (outgoing) — and a creditor works the other way round. * The refund repeats the item's reference, so it clears the same Belegfeld 1 * key and the item disappears instead of turning into a second one. * * A plain underpayment (no Skonto, beyond tolerance) is a PARTIAL settlement: * only the paid amount is booked and the rest stays open. */ export declare function planOpenItemSettlement(params: { item: IOpenItem; paidCents: number; diffToleranceCents?: number; /** a Skonto branch consumes the whole outstanding amount, never partial */ skonto?: boolean; }): IOpenItemSettlementPlan;