import * as plugins from './plugins.js'; import { getDbSync } from './skr.database.js'; import type { TAccountType, TSKRType, IAccountData } from './skr.types.js'; import { assertSkrType } from './core/core.chart.js'; import { booksChartOf } from './core/core.booksaccounts.js'; @plugins.nosqldb.Collection(() => getDbSync(), { collectionName: 'Account' }) export class Account extends plugins.nosqldb.SmartDataDbDoc { @plugins.nosqldb.unI() public id!: string; @plugins.nosqldb.svDb() @plugins.nosqldb.index() public accountNumber!: string; @plugins.nosqldb.svDb() public accountName!: string; @plugins.nosqldb.svDb() @plugins.nosqldb.index() public accountClass!: number; @plugins.nosqldb.svDb() public accountGroup!: number; @plugins.nosqldb.svDb() public accountSubgroup!: number; @plugins.nosqldb.svDb() public accountType!: TAccountType; @plugins.nosqldb.svDb() @plugins.nosqldb.index() public skrType!: TSKRType; @plugins.nosqldb.svDb() public description!: string; @plugins.nosqldb.svDb() public vatRate!: number; @plugins.nosqldb.svDb() public balance!: number; @plugins.nosqldb.svDb() public debitTotal!: number; @plugins.nosqldb.svDb() public creditTotal!: number; @plugins.nosqldb.svDb() public isActive!: boolean; @plugins.nosqldb.svDb() public isSystemAccount!: boolean; @plugins.nosqldb.svDb() public isAutomaticAccount!: boolean; /** * An individual account of the books (Individualkonto, * `Account.declareIndividualAccount`): the DATEV account it belongs to and * takes its type from; unset = an account of the chart or a custom account. */ @plugins.nosqldb.svDb() public parentAccountNumber?: string; /** the securities role an individual account's declaration names; unset = none */ @plugins.nosqldb.svDb() public securitiesRole?: plugins.chartdata.TSecuritiesRole; /** what an individual account's declaration changes of what it takes from its parent; unset = nothing */ @plugins.nosqldb.svDb() public overrides?: plugins.chartdata.IIndividualAccountOverrides; @plugins.nosqldb.svDb() public createdAt!: Date; @plugins.nosqldb.svDb() public updatedAt!: Date; constructor(data?: Partial) { super(); if (data) { this.id = plugins.smartunique.shortId(); this.accountNumber = data.accountNumber || ''; this.accountName = data.accountName || ''; this.accountClass = data.accountClass || 0; this.accountType = data.accountType || 'asset'; this.skrType = data.skrType || 'SKR03'; this.description = data.description || ''; this.vatRate = data.vatRate || 0; this.isActive = data.isActive !== undefined ? data.isActive : true; // Parse account structure from number if (this.accountNumber && this.accountNumber.length === 4) { this.accountClass = parseInt(this.accountNumber[0]); this.accountGroup = parseInt(this.accountNumber[1]); this.accountSubgroup = parseInt(this.accountNumber[2]); } else { this.accountGroup = 0; this.accountSubgroup = 0; } this.balance = 0; this.debitTotal = 0; this.creditTotal = 0; this.isSystemAccount = true; this.isAutomaticAccount = data.isAutomaticAccount || false; this.createdAt = new Date(); this.updatedAt = new Date(); } } public static async createAccount(data: IAccountData): Promise { if (data.parentAccountNumber !== undefined || data.securitiesRole !== undefined || data.overrides !== undefined) { // an individual account is what chartdata makes of its declaration, never data handed in throw new Error( `Account ${data.accountNumber} is an individual account; declare it with declareIndividualAccount`, ); } const account = new Account(data); await account.save(); return account; } public static async getAccountByNumber( accountNumber: string, skrType: TSKRType, ): Promise { const account = await Account.getInstance({ accountNumber, skrType, }); return account; } /** * Declare an individual account of the books (Individualkonto) on a number * DATEV leaves free after the account it belongs to. @fin.cx/chartdata * checks the declaration together with the individual accounts already * declared for the chart (`booksChartOf`) and refuses it with its * `IndividualAccountsError` (`problems`: a number DATEV assigns, one that * does not follow its parent, one declared already, …); a number another * account of the chart's books holds is refused too. Nothing is stored for * a refused declaration. The account takes its parent's type and is no * system account; the declaration is stored on it * (`getIndividualAccountDeclarations`). */ public static async declareIndividualAccount( skrType: TSKRType, declaration: plugins.chartdata.IIndividualAccountDeclaration, ): Promise { assertSkrType(skrType); const declared = await Account.getIndividualAccountDeclarations(skrType); const books = booksChartOf({ skrType, individualAccounts: [...declared, declaration] }); const chartAccount = books.getAccount(declaration.accountNumber); if (chartAccount === null) { // BooksChart.of took the declaration, so its chart holds the account throw new Error(`Individual account ${declaration.accountNumber} is no account of the ${skrType} books chart`); } if (await Account.getAccountByNumber(declaration.accountNumber, skrType)) { throw new Error(`Account ${declaration.accountNumber} already exists in ${skrType}`); } const account = new Account({ accountNumber: declaration.accountNumber, accountName: declaration.accountName, accountClass: chartAccount.accountClass, accountType: chartAccount.accountType, skrType, isActive: true, }); account.isSystemAccount = false; account.parentAccountNumber = declaration.parentAccountNumber; if (declaration.securitiesRole !== undefined) account.securitiesRole = declaration.securitiesRole; if (declaration.overrides !== undefined) account.overrides = declaration.overrides; await account.save(); return account; } /** * The declarations of the individual accounts stored for a chart, by * account number: what `IAccountPolicy.individualAccounts` takes. Inactive * individual accounts are declarations too — their numbers stay taken. */ public static async getIndividualAccountDeclarations( skrType: TSKRType, ): Promise { assertSkrType(skrType); const accounts = await Account.getInstances({ skrType, parentAccountNumber: { $ne: null }, }); const declarations: plugins.chartdata.IIndividualAccountDeclaration[] = []; for (const account of accounts) { if (account.parentAccountNumber === undefined) continue; declarations.push({ accountNumber: account.accountNumber, accountName: account.accountName, parentAccountNumber: account.parentAccountNumber, ...(account.securitiesRole !== undefined ? { securitiesRole: account.securitiesRole } : {}), ...(account.overrides !== undefined ? { overrides: account.overrides } : {}), }); } return declarations.sort((left, right) => left.accountNumber.localeCompare(right.accountNumber)); } public static async getAccountsByClass( accountClass: number, skrType: TSKRType, ): Promise { const accounts = await Account.getInstances({ accountClass, skrType, isActive: true, }); return accounts; } public static async getAccountsByType( accountType: TAccountType, skrType: TSKRType, ): Promise { const accounts = await Account.getInstances({ accountType, skrType, isActive: true, }); return accounts; } public static async searchAccounts( searchTerm: string, skrType?: TSKRType, ): Promise { const query: plugins.nosqldb.MongoFilter = {}; if (skrType) { query.skrType = skrType; } const accounts = await Account.getInstances(query); // Filter by search term const lowerSearchTerm = searchTerm.toLowerCase(); return accounts.filter( (account) => account.accountNumber.includes(searchTerm) || account.accountName.toLowerCase().includes(lowerSearchTerm) || account.description.toLowerCase().includes(lowerSearchTerm), ); } /** * Check if account number is in debtor range (10000-69999) * Debtor accounts (Debitorenkonten) are individual customer accounts */ public static isInDebtorRange(accountNumber: string): boolean { const num = parseInt(accountNumber); return num >= 10000 && num <= 69999; } /** * Check if account number is in creditor range (70000-99999) * Creditor accounts (Kreditorenkonten) are individual vendor accounts */ public static isInCreditorRange(accountNumber: string): boolean { const num = parseInt(accountNumber); return num >= 70000 && num <= 99999; } /** * Check if account is an automatic account (Automatikkonto) * Automatic accounts like 1400/1600 cannot be posted to directly */ public static isAutomaticAccount(accountNumber: string, skrType: TSKRType): boolean { assertSkrType(skrType); // Sammelkonten (control accounts) must be addressed via personal accounts // SKR03: 1400 (Forderungen), 1600 (Verbindlichkeiten) // SKR04: 1200 (Forderungen), 3300 (Verbindlichkeiten) if (skrType === 'SKR03') { return accountNumber === '1400' || accountNumber === '1600'; } return accountNumber === '1200' || accountNumber === '3300'; } /** * Validate account for posting - throws error if account cannot be posted to */ public static async validateAccountForPosting( accountNumber: string, skrType: TSKRType, ): Promise { // Check if automatic account if (Account.isAutomaticAccount(accountNumber, skrType)) { throw new Error( `Account ${accountNumber} is an automatic account (Automatikkonto) and cannot be posted to directly. ` + `Use debtor accounts (10000-69999) or creditor accounts (70000-99999) instead.` ); } // Get account to verify it exists const account = await Account.getAccountByNumber(accountNumber, skrType); if (!account) { throw new Error( `Account ${accountNumber} not found in ${skrType}. ` + `Please create the account before posting.` ); } // Check if account is active if (!account.isActive) { throw new Error( `Account ${accountNumber} is inactive and cannot be posted to.` ); } } /** * Check if this account instance is a debtor account */ public isDebtorAccount(): boolean { return Account.isInDebtorRange(this.accountNumber); } /** * Check if this account instance is a creditor account */ public isCreditorAccount(): boolean { return Account.isInCreditorRange(this.accountNumber); } public async updateBalance( debitAmount: number = 0, creditAmount: number = 0, ): Promise { this.debitTotal += debitAmount; this.creditTotal += creditAmount; // Calculate balance based on account type switch (this.accountType) { case 'asset': case 'expense': // Normal debit accounts this.balance = this.debitTotal - this.creditTotal; break; case 'liability': case 'equity': case 'revenue': // Normal credit accounts this.balance = this.creditTotal - this.debitTotal; break; } this.updatedAt = new Date(); await this.save(); } public async deactivate(): Promise { this.isActive = false; this.updatedAt = new Date(); await this.save(); } public async activate(): Promise { this.isActive = true; this.updatedAt = new Date(); await this.save(); } public getNormalBalance(): 'debit' | 'credit' { switch (this.accountType) { case 'asset': case 'expense': return 'debit'; case 'liability': case 'equity': case 'revenue': return 'credit'; } } public async beforeSave(): Promise { // Validate account number format const accountLength = this.accountNumber?.length || 0; if (!this.accountNumber || (accountLength !== 4 && accountLength !== 5)) { throw new Error( `Invalid account number format: ${this.accountNumber}. Must be 4 digits (standard SKR) or 5 digits (debtor/creditor).`, ); } // Validate account number is numeric if (!/^\d{4,5}$/.test(this.accountNumber)) { throw new Error( `Account number must contain only digits: ${this.accountNumber}`, ); } // For 5-digit accounts, validate they are in debtor (10000-69999) or creditor (70000-99999) ranges if (accountLength === 5) { const accountNum = parseInt(this.accountNumber); const isDebtor = accountNum >= 10000 && accountNum <= 69999; const isCreditor = accountNum >= 70000 && accountNum <= 99999; if (!isDebtor && !isCreditor) { throw new Error( `5-digit account number ${this.accountNumber} must be in debtor range (10000-69999) or creditor range (70000-99999).`, ); } } // Validate account class matches first digit const firstDigit = parseInt(this.accountNumber[0]); if (this.accountClass !== firstDigit) { throw new Error( `Account class ${this.accountClass} does not match account number ${this.accountNumber}`, ); } // Validate SKR type if (this.skrType !== 'SKR03' && this.skrType !== 'SKR04') { throw new Error(`Invalid SKR type: ${this.skrType}`); } // Mark automatic accounts (Automatikkonten) // These are summary accounts that cannot be posted to directly if (Account.isAutomaticAccount(this.accountNumber, this.skrType)) { this.isAutomaticAccount = true; } } }