import type { IEntryIssue, IInputTaxUse, IJournalLineDraft, TSKRType } from './core.types.js'; import { type IAccountPolicy } from './core.accountpolicy.js'; /** * Where an exchange rate comes from: the BMF's monthly average for VAT * (§ 16 Abs. 6 Satz 1 UStG), the ECB reference rate of the day, what the bank * converted, or a rate a person stated with its Beleg. */ export type TExchangeRateSource = 'bmf_monthly' | 'ecb_daily' | 'bank' | 'stated'; /** A foreign amount behind an entry in euro (GoBD Rz. 77, 94). */ export interface IPostedEntryForeignAmount { /** ISO 4217, not EUR */ currency: string; amountCents: number; /** units of `currency` for one euro, the decimal with a point as published */ rate: string; rateDate: string; rateSource: TExchangeRateSource; } /** The record of the ledger's owner an entry was built from, and its revision. */ export interface IPostedEntrySource { /** opaque to the core */ kind: string; id: string; revision: number; } /** * What caused an entry to be posted, for example the statement that made a * repost (Storno and Neubuchung) necessary, and when it was recorded. Opaque * to the core, as `IPostedEntrySource` is. */ export interface IPostedEntryCause { kind: string; id: string; /** epoch milliseconds */ recordedAt: number; } /** One posted entry as the hash chain covers it. */ export interface IPostedEntryPayload { /** the version of the stored shape, the owner's */ schemaVersion: number; /** the chain the entry belongs to: one per organization and fiscal year */ ledgerId: string; /** 1, 2, 3 … per ledger, without gaps */ seq: number; /** the number people read, opaque to the core */ entryNumber: string; /** Belegdatum, `YYYY-MM-DD` */ documentDate: string; /** Buchungsdatum, `YYYY-MM-DD`: places the entry in its period and its fiscal year */ postingDate: string; /** Erfassungsdatum: when it was recorded, epoch milliseconds */ recordedAt: number; /** Buchungsperiode, `YYYY-MM`: the month of `postingDate` */ period: string; /** Voranmeldungszeitraum, `YYYY-MM` */ vatPeriod?: string; /** Leistungsdatum (`on`) or Leistungszeitraum (`from` and `to`) */ performance?: { on?: string; from?: string; to?: string; }; source: IPostedEntrySource; /** unique per ledger owner: a source's revision is posted once, and its reversal once */ sourceKey: string; /** the canonical hash of the source's content the entry was built from */ sourceDigest: string; /** the Belege (GoBD Rz. 64, 71) */ documentIds: string[]; /** * The bank movements that are the entry's Beleg themselves: the electronic * account statement data, kept unchanged, of a movement nothing else * explains (GoBD Rz. 76-78), opaque ids of the owner's store. Covered by the * hash where given; an entry without it hashes as one made before it existed. */ movementIds?: string[]; description: string; party?: { name: string; contactId?: string; vatId?: string; taxNumber?: string; }; /** as posted: positive amounts; the entry's `generalumkehr` makes each count negative on its side */ lines: IJournalLineDraft[]; currency: 'EUR'; foreign?: IPostedEntryForeignAmount; /** a general reversal; absent (or false) when it is none */ generalumkehr?: boolean; /** the entry this one reverses */ reversalOf?: string; /** the Autorisierung of Rz. 94: a person, a part of the application, a job */ recordedBy: { userId?: string; system?: string; jobId?: string; }; /** the person who confirmed the source before it was posted */ confirmedBy?: { userId: string; at: number; }; chart: TSKRType; accountLength: number; /** the version of the rules that built the entry from its source (GoBD Rz. 80) */ rulesVersion: string; /** * What the entry's supply is used for, as far as its input tax is concerned * (`IInputTaxUse`, § 15 Abs. 2 and 4 UStG), as a person stated it: one per * entry, as a Beleg has one. A statement, never a computation: the amounts * are the lines'. Covered by the hash where given; an entry without it * hashes as one made before it existed. Refused: a kind that is none of * `TInputTaxUseKind`, a key beside another kind than `mixed` or one that is * no decimal above 0 and below 100 with up to two places * (`input_tax_use_invalid`), `mixed` without its key * (`input_tax_use_key_missing`), and, with a policy, `exempt` on an entry * whose lines on any of the policy's input-tax accounts (`vat.inputStandard`, * `…Reduced`, `intraEuAcqInput`, `reverseChargeInput`) do not net to zero, * each account apart (`input_tax_use_exempt_deducts`), a line counting as * the account it declares as (`IJournalLineDraft.declaresAs`): a § 13b or * intra-Community pair whose deduction a correction takes back in full * passes, as does its reversal. */ inputTaxUse?: IInputTaxUse; /** * What caused this entry to be posted, for example the statement that made * a repost necessary; absent on entries posted for their own source. * Covered by the hash where given; an entry without it hashes as one made * before it existed. Refused (`cause_invalid`): no object, a `kind` or `id` * that is no non-empty text, a `recordedAt` that is no whole number of * epoch milliseconds from 0, and any other key. */ cause?: IPostedEntryCause; /** * The history this entry was decided on, read through these times (epoch * milliseconds) per named store. The consuming app's provenance: the core * keeps it and hashes it and gives it no meaning. Covered by the hash where * given; an entry without it hashes as one made before it existed. Refused * (`read_through_invalid`): no plain object, an empty one (it would hash * otherwise than an absent one), a key that is no letter followed by letters * and digits (keys become document field names: no `.` or `$`), and a time * that is no whole number of epoch milliseconds from 0. */ readThrough?: Record; } /** A posted entry with its place in the chain. */ export interface IPostedEntry extends IPostedEntryPayload { prevHash: string; entryHash: string; } /** The last entry of a ledger, which the next one follows. */ export interface ILedgerChainHead { seq: number; entryHash: string; /** when given, it must be the ledger of the next entry */ ledgerId?: string; } export interface IPostedEntryValidationOptions { /** the Automatikkonto and control-account rules of `validateJournalDraft`, and the VAT of each Automatikkonto line (`validatePostedEntry`) */ policy?: IAccountPolicy; /** the fiscal year the ledger is for: the posting date must lie in it */ fiscalYear?: { start: string; end: string; }; } export type TLedgerChainBreakReason = /** a seq that is not the previous one plus 1, or a first seq that is not 1 */ 'gap' /** a seq given twice in a row */ | 'duplicate' /** an entry of another ledger */ | 'ledger' /** a prevHash that is not the previous entry's hash (the genesis for the first) */ | 'prev_hash' /** a hash that no longer matches what the entry records */ | 'entry_hash'; export interface ILedgerChainBreak { /** the position in the list handed in */ index: number; seq: number; reason: TLedgerChainBreakReason; } /** * What the hash of a posted entry covers: exactly the fields of * `IPostedEntryPayload`, an absent field and one read back as `null` alike, * `generalumkehr: false` as absent. Fields beyond them (a store's own id, an * organization id, the two hashes) are left out, so a stored entry can be * handed in as it is. */ export declare function postedEntryPayloadOf(entry: IPostedEntryPayload): IPostedEntryPayload; /** * The hash of a posted entry: `entryHash(prevHash, payload)` — SHA-256 over * the predecessor's hash and the canonical form (`canonicalize`: keys sorted, * arrays in order, integers only) of `postedEntryPayloadOf(entry)`. */ export declare function postedEntryHash(prevHash: string, entry: IPostedEntryPayload): string; /** The hash the first entry of a ledger chains to: SHA-256 of `GENESIS:LEDGER:`. */ export declare function ledgerGenesisHash(ledgerId: string): string; /** * The number and predecessor hash of a ledger's next entry: 1 and the genesis * after no entry, else one more than the last entry's number and its hash. * Refuses a head that is no entry, and one of another ledger. */ export declare function planNextEntry(head: ILedgerChainHead | null, ledgerId: string): { seq: number; prevHash: string; }; /** * Validate a posted entry. Unlike `validateJournalDraft` it normalises * nothing, and what is a warning for a draft is an error here, because a * posted entry can never be put right afterwards. It refuses: fewer than two * lines; an amount that is not a positive whole number of cents; debits and * credits that differ; an account equal to its counter account, or either * missing; an unknown BU key; a Buchungstext over 60 characters; a Belegfeld * that is empty, over 36 characters or with characters DATEV does not take; a currency other * than EUR; dates that are no days; a period that is not the posting date's * month; a posting date outside the fiscal year when one is given; a foreign * amount in EUR, not in whole cents, or with a rate that is no positive * decimal; a missing ledger, number, source, digest or authorisation; and with * a policy the Automatikkonto and control-account rules, and a line on an * Automatikkonto whose German VAT does not fit the tax the chart ties the * account to (`automatikRateIssuesOf`); a `reverseChargeParagraph` beside a * BU key that is none of § 13b; a `declaresAs` that is no non-empty text; * and a use statement (`inputTaxUse`) of another shape, or, with a policy, an * `exempt` one whose input-tax accounts * do not each net to zero; a `cause` of another shape; and a `readThrough` * that is no non-empty plain object of named epoch milliseconds. Each error has its issue * (`IEntryIssue`) in `issues`, in the same order: the rule's code, the field * and the line. */ export declare function validatePostedEntry(payload: IPostedEntryPayload, options?: IPostedEntryValidationOptions): { valid: boolean; errors: string[]; issues: IEntryIssue[]; }; /** * Verify the entries of one ledger, in their order: each belongs to the * ledger, is numbered one more than the entry before it (1 for the first), * names that entry's hash (the genesis for the first) and still has the hash * of what it records. Answers the first break, or null when the chain is * whole. Hand in every entry of the ledger: a chain cut short at its end is * whole as far as it goes. */ export declare function verifyLedgerChain(ledgerId: string, entries: readonly IPostedEntry[]): ILedgerChainBreak | null;