import * as plugins from './plugins.js'; import { assertSkrType } from './core/core.chart.js'; import { booksChartOf } from './core/core.booksaccounts.js'; import { closeDb } from './skr.database.js'; import { getDb } from './skr.storage.js'; import { Account } from './skr.classes.account.js'; import { JournalEntry } from './skr.classes.journalentry.js'; import { listTransactionViews, postJournalInput, postTransactionInput, reverseTransactionInput, } from './skr.journalbooking.js'; import { accountLineViews } from './skr.txview.js'; import { SKR03_ACCOUNTS, SKR03_ACCOUNT_CLASSES } from './skr03.data.js'; import { SKR04_ACCOUNTS, SKR04_ACCOUNT_CLASSES } from './skr04.data.js'; import type { TDatabaseConfig, TSKRType, IAccountData, IAccountFilter, ITransactionFilter, ITransactionData, ITransactionView, IJournalEntry, } from './skr.types.js'; export class ChartOfAccounts { private logger: plugins.smartlog.Smartlog; private initialized: boolean = false; private skrType: TSKRType | null = null; constructor(private config?: TDatabaseConfig) { this.logger = new plugins.smartlog.Smartlog({ logContext: { company: 'fin.cx', companyunit: 'skr', containerName: 'ChartOfAccounts', environment: 'local', runtime: 'node', zone: 'local', }, }); this.logger.enableConsole(); } /** * Initialize the database connection */ public async init(): Promise { if (this.initialized) { this.logger.log('info', 'ChartOfAccounts already initialized'); return; } if (!this.config) { throw new Error('Database configuration required for initialization'); } await getDb(this.config); this.initialized = true; this.logger.log('info', 'ChartOfAccounts initialized successfully'); } /** * Initialize SKR03 chart of accounts */ public async initializeSKR03(): Promise { await this.init(); this.logger.log('info', 'Initializing SKR03 chart of accounts'); // Check if SKR03 accounts already exist const existingAccounts = await Account.getInstances({ skrType: 'SKR03' }); if (existingAccounts.length > 0) { this.logger.log( 'info', `SKR03 already initialized with ${existingAccounts.length} accounts`, ); this.skrType = 'SKR03'; return; } // Create all SKR03 accounts const accounts: Account[] = []; for (const accountData of SKR03_ACCOUNTS) { const account = await Account.createAccount(accountData); accounts.push(account); } this.skrType = 'SKR03'; this.logger.log( 'info', `Successfully initialized SKR03 with ${accounts.length} accounts`, ); } /** * Initialize SKR04 chart of accounts */ public async initializeSKR04(): Promise { await this.init(); this.logger.log('info', 'Initializing SKR04 chart of accounts'); // Check if SKR04 accounts already exist const existingAccounts = await Account.getInstances({ skrType: 'SKR04' }); if (existingAccounts.length > 0) { this.logger.log( 'info', `SKR04 already initialized with ${existingAccounts.length} accounts`, ); this.skrType = 'SKR04'; return; } // Create all SKR04 accounts const accounts: Account[] = []; for (const accountData of SKR04_ACCOUNTS) { const account = await Account.createAccount(accountData); accounts.push(account); } this.skrType = 'SKR04'; this.logger.log( 'info', `Successfully initialized SKR04 with ${accounts.length} accounts`, ); } /** * Get the current SKR type */ public getSKRType(): TSKRType | null { return this.skrType; } /** * Set the active SKR type */ public setSKRType(skrType: TSKRType): void { assertSkrType(skrType); this.skrType = skrType; } /** * Get account by number */ public async getAccountByNumber( accountNumber: string, ): Promise { if (!this.skrType) { throw new Error('SKR type not set. Initialize SKR03 or SKR04 first.'); } return await Account.getAccountByNumber(accountNumber, this.skrType); } /** * Get accounts by class */ public async getAccountsByClass(accountClass: number): Promise { if (!this.skrType) { throw new Error('SKR type not set. Initialize SKR03 or SKR04 first.'); } return await Account.getAccountsByClass(accountClass, this.skrType); } /** * Get accounts by type */ public async getAccountsByType( accountType: IAccountData['accountType'], ): Promise { if (!this.skrType) { throw new Error('SKR type not set. Initialize SKR03 or SKR04 first.'); } return await Account.getAccountsByType(accountType, this.skrType); } /** * Create a custom account */ public async createCustomAccount( accountData: Partial, ): Promise { if (!this.skrType) { throw new Error('SKR type not set. Initialize SKR03 or SKR04 first.'); } // Ensure the account uses the current SKR type const fullAccountData: IAccountData = { accountNumber: accountData.accountNumber || '', accountName: accountData.accountName || '', accountClass: accountData.accountClass || 0, accountType: accountData.accountType || 'asset', skrType: this.skrType, description: accountData.description, vatRate: accountData.vatRate, isActive: accountData.isActive !== undefined ? accountData.isActive : true, // handed on so Account.createAccount refuses an individual account: it is declared (declareIndividualAccount) parentAccountNumber: accountData.parentAccountNumber, securitiesRole: accountData.securitiesRole, overrides: accountData.overrides, }; // Validate account number doesn't already exist const existing = await this.getAccountByNumber( fullAccountData.accountNumber, ); if (existing) { throw new Error( `Account ${fullAccountData.accountNumber} already exists`, ); } return await Account.createAccount(fullAccountData); } /** * Declare an individual account of the books (Individualkonto) on a number * DATEV leaves free after the account it belongs to. @fin.cx/chartdata * checks it together with the individual accounts already declared and * refuses it with its `IndividualAccountsError` (`problems`); nothing is * stored then. The account takes its parent's type. */ public async declareIndividualAccount( declaration: plugins.chartdata.IIndividualAccountDeclaration, ): Promise { return await Account.declareIndividualAccount(this.requireSkrType(), declaration); } /** * The declarations of the books' individual accounts, by account number: * what `IAccountPolicy.individualAccounts` takes. */ public async getIndividualAccounts(): Promise { return await Account.getIndividualAccountDeclarations(this.requireSkrType()); } /** * Update an existing account */ public async updateAccount( accountNumber: string, updates: Partial, ): Promise { const account = await this.getAccountByNumber(accountNumber); if (!account) { throw new Error(`Account ${accountNumber} not found`); } if (updates.accountName !== undefined && account.parentAccountNumber !== undefined) { // an individual account's name is part of its declaration, which @fin.cx/chartdata checks (`name_missing`) const declared = await Account.getIndividualAccountDeclarations(account.skrType); booksChartOf({ skrType: account.skrType, individualAccounts: declared.map((declarationArg) => declarationArg.accountNumber === accountNumber ? { ...declarationArg, accountName: updates.accountName! } : declarationArg), }); } // Apply updates if (updates.accountName !== undefined) account.accountName = updates.accountName; if (updates.description !== undefined) account.description = updates.description; if (updates.vatRate !== undefined) account.vatRate = updates.vatRate; if (updates.isActive !== undefined) account.isActive = updates.isActive; account.updatedAt = new Date(); await account.save(); return account; } /** * Delete a custom account (only non-system accounts) */ public async deleteAccount(accountNumber: string): Promise { const account = await this.getAccountByNumber(accountNumber); if (!account) { throw new Error(`Account ${accountNumber} not found`); } if (account.isSystemAccount) { throw new Error(`Cannot delete system account ${accountNumber}`); } // Check if any journal entry books on the account const hasEntries = await JournalEntry.exists({ skrType: account.skrType, 'lines.accountNumber': accountNumber, }); if (hasEntries) { throw new Error( `Cannot delete account ${accountNumber} with existing transactions`, ); } await account.delete(); } /** * Search accounts */ public async searchAccounts(searchTerm: string): Promise { if (!this.skrType) { throw new Error('SKR type not set. Initialize SKR03 or SKR04 first.'); } return await Account.searchAccounts(searchTerm, this.skrType); } /** * Get all accounts */ public async getAllAccounts(filter?: IAccountFilter): Promise { const query: plugins.nosqldb.MongoFilter = {}; if (this.skrType) { query.skrType = this.skrType; } if (filter) { if (filter.accountClass !== undefined) query.accountClass = filter.accountClass; if (filter.accountType !== undefined) query.accountType = filter.accountType; if (filter.isActive !== undefined) query.isActive = filter.isActive; } const accounts = await Account.getInstances(query); // Apply text search if provided if (filter?.searchTerm) { const searchTerm = filter.searchTerm; const lowerSearchTerm = searchTerm.toLowerCase(); return accounts.filter( (account) => account.accountNumber.includes(searchTerm) || account.accountName.toLowerCase().includes(lowerSearchTerm) || account.description.toLowerCase().includes(lowerSearchTerm), ); } return accounts; } /** * Post a simple transaction as a hashed two-line journal entry. */ public async postTransaction( transactionData: ITransactionData, ): Promise { return postTransactionInput(this.requireSkrType(), transactionData); } /** * Post a journal entry on the hashed journal. */ public async postJournalEntry( journalData: IJournalEntry, ): Promise { return postJournalInput(this.requireSkrType(), journalData); } /** * Get per-line transaction views for an account. */ public async getAccountTransactions( accountNumber: string, ): Promise { return accountLineViews(accountNumber, this.requireSkrType()); } /** * Get transaction views (two-line entries) by filter. */ public async getTransactions( filter?: ITransactionFilter, ): Promise { return listTransactionViews(this.requireSkrType(), filter); } /** * Reverse a transaction via Storno; the original is never mutated (GoBD). */ public async reverseTransaction(transactionId: string): Promise { return reverseTransactionInput(this.requireSkrType(), transactionId); } private requireSkrType(): TSKRType { if (!this.skrType) { throw new Error('SKR type not set. Initialize SKR03 or SKR04 first.'); } return this.skrType; } /** * Get account class description */ public getAccountClassDescription(accountClass: number): string { if (!this.skrType) { throw new Error('SKR type not set. Initialize SKR03 or SKR04 first.'); } const classes = this.skrType === 'SKR03' ? SKR03_ACCOUNT_CLASSES : SKR04_ACCOUNT_CLASSES; return ( classes[accountClass as keyof typeof classes] || `Class ${accountClass}` ); } /** * Import accounts from CSV */ public async importAccountsFromCSV(csvContent: string): Promise { if (!this.skrType) { throw new Error('SKR type not set. Initialize SKR03 or SKR04 first.'); } const lines = csvContent.split('\n').filter((line) => line.trim()); let importedCount = 0; for (const line of lines) { // Parse CSV line (expecting format: "account";"name";"description";"type";"active") const parts = line .split(';') .map((part) => part.replace(/"/g, '').trim()); if (parts.length >= 5) { const accountData: IAccountData = { accountNumber: parts[0], accountName: parts[1], accountClass: parseInt(parts[0][0]), accountType: parts[3] as IAccountData['accountType'], skrType: this.skrType, description: parts[2], isActive: parts[4].toLowerCase() === 'standard' || parts[4].toLowerCase() === 'active', }; try { await this.createCustomAccount(accountData); importedCount++; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); this.logger.log( 'warn', `Failed to import account ${parts[0]}: ${errorMessage}`, ); } } } return importedCount; } /** * Export accounts to CSV */ public async exportAccountsToCSV(): Promise { const accounts = await this.getAllAccounts(); const csvLines: string[] = []; csvLines.push('"Account";"Name";"Description";"Type";"Active"'); for (const account of accounts) { csvLines.push( `"${account.accountNumber}";"${account.accountName}";"${account.description}";"${account.accountType}";"${account.isActive ? 'Active' : 'Inactive'}"`, ); } return csvLines.join('\n'); } /** * Close the database connection */ public async close(): Promise { await closeDb(); this.initialized = false; this.logger.log('info', 'ChartOfAccounts closed'); } }