/** * The reversal (Storno) of a journal entry, in the two forms DATEV knows: * * - `generalumkehr` (Buchungsstapel field 118): the entry's lines again, on * the same accounts and sides with the same amounts, marked so that they * count negative. Each account's turnover stays as if the reversed entry * had never been booked. * - `gegenbuchung`: the lines with their sides swapped. The balances are the * same; the turnover of both sides grows by the amount. * * Both keep Belegfeld 1 and 2, the cost centres, the document link and the * VAT facts, so the reversal stays on the Beleg and on the open item of the * entry it reverses (GoBD Rz. 64: "Korrektur- bzw. Stornobuchungen müssen auf * die ursprüngliche Buchung rückbeziehbar sein"); correcting by reversal and * new entry is what GoBD Rz. 93 names ("durch Stornierungen oder * Neubuchungen"). * * Pure: the caller posts the draft. `JournalPoster.postStorno` of the * database ledger is unchanged: a Gegenbuchung whose Belegfeld 1 is replaced * by the journal number. */ import type { IJournalDraft, IJournalLineDraft } from './core.types.js'; import { DATEV_MAX_BUCHUNGSTEXT, validateJournalDraft } from './core.validate.js'; import { RecipeRefusalError } from './core.errors.js'; export type TReversalForm = 'generalumkehr' | 'gegenbuchung'; /** An entry that can be reversed: a journal draft with the id it was posted under. */ export interface IReversibleEntry extends IJournalDraft { id: string; } export interface IReversalOptions { /** unset: `generalumkehr` */ form?: TReversalForm; /** the reversal's description; unset: `Storno: ` and the entry's description */ description?: string; } const stornoText = (textArg: string): string => `Storno: ${textArg}`.slice(0, DATEV_MAX_BUCHUNGSTEXT); /** * The lines of a reversal, and whether it is marked Generalumkehr. The form * `generalumkehr` keeps sides and amounts and flips the mark (the reversal of * a Generalumkehr is a plain entry); `gegenbuchung` swaps the sides and keeps * the mark. Every other field of a line is kept; the Buchungstext becomes * `Storno: `, cut to 60 characters. The lines are taken as they are * posted: positive amounts (a negative amount is refused). */ export function reversalLinesOf( lines: readonly IJournalLineDraft[], generalumkehr: boolean, form: TReversalForm, ): { lines: IJournalLineDraft[]; generalumkehr: boolean } { if (form !== 'generalumkehr' && form !== 'gegenbuchung') { // TReversalForm has no other form: a caller that reaches here broke the type, an invariant, not a refusal throw new Error(`the form of a reversal is generalumkehr or gegenbuchung, not ${String(form)}`); } const negative = lines.findIndex((lineArg) => !Number.isSafeInteger(lineArg.amountCents) || lineArg.amountCents <= 0); if (negative >= 0) { throw new RecipeRefusalError( 'reversal_line_not_positive', `line ${negative + 1} states ${String(lines[negative].amountCents)}: the lines of a reversal are taken as posted, with positive amounts of whole cents`, { field: `lines[${negative}].amountCents`, amountCents: lines[negative].amountCents }, ); } return { lines: lines.map((lineArg) => ({ ...lineArg, ...(form === 'gegenbuchung' ? { side: lineArg.side === 'debit' ? ('credit' as const) : ('debit' as const) } : {}), buchungstext: stornoText(lineArg.buchungstext), })), generalumkehr: form === 'generalumkehr' ? !generalumkehr : generalumkehr, }; } /** * The reversal of an entry: the same date (the Belegdatum of the same Beleg), * reference, chart and tax scenario, the lines of `reversalLinesOf`, linked by * `reversalOf` to the entry's id. A line the entry states negative (without * the mark) is read on its other side first, as the validator reads it. * Refuses an entry that is not valid, and one without an id. */ export function reversalOf(entry: IReversibleEntry, options?: IReversalOptions): IJournalDraft { if (typeof entry.id !== 'string' || entry.id.length === 0) { throw new RecipeRefusalError('reversal_entry_without_id', 'the entry to reverse needs its id', { field: 'id' }); } const { id, ...draft } = entry; const validation = validateJournalDraft(draft); if (!validation.valid) { throw new RecipeRefusalError('reversal_entry_invalid', `the entry ${id} cannot be reversed: ${validation.errors.join('; ')}`, { errors: validation.errors, }); } const { generalumkehr: _mark, reversalOf: _reversed, lines, ...rest } = validation.normalized; const reversal = reversalLinesOf(lines, validation.normalized.generalumkehr === true, options?.form ?? 'generalumkehr'); return { ...rest, description: options?.description ?? `Storno: ${entry.description}`, reversalOf: id, ...(reversal.generalumkehr ? { generalumkehr: true } : {}), lines: reversal.lines, }; }