import type { IAccountPolicy } from './core.accountpolicy.js'; import { isAutomatikAccount } from './core.accountpolicy.js'; import { booksChartOf } from './core.booksaccounts.js'; import { chartOf } from './core.chart.js'; import { RecipeRefusalError } from './core.errors.js'; import type { TSKRType } from './core.types.js'; export type TShareholderLiabilityKind = 'shareholder' | 'gmbh_shareholder' | 'general_partner' | 'limited_partner'; export const SHAREHOLDER_LIABILITY_KINDS: readonly TShareholderLiabilityKind[] = [ 'shareholder', 'gmbh_shareholder', 'general_partner', 'limited_partner', ]; export const SHAREHOLDER_LIABILITY_ACCOUNTS: Readonly>>> = { SKR03: { shareholder: '0730', gmbh_shareholder: '1665', general_partner: '1670', limited_partner: '1675' }, SKR04: { shareholder: '3510', gmbh_shareholder: '3640', general_partner: '3645', limited_partner: '3650' }, }; export interface IShareholderLiabilityAccountInput { kind: TShareholderLiabilityKind; account: string; } export function shareholderLiabilityAccountOf(policy: IAccountPolicy, input: IShareholderLiabilityAccountInput): string { const chart = chartOf(policy); if (!SHAREHOLDER_LIABILITY_KINDS.includes(input?.kind)) { throw new RecipeRefusalError( 'shareholder_liability_kind_required', 'State the established shareholder or partner creditor category; legal form alone does not establish the funding treatment', { field: 'kind' }, ); } if (typeof input.account !== 'string' || input.account.trim() === '') { throw new RecipeRefusalError( 'shareholder_liability_account_required', 'Choose the shareholder or partner liability account in the books settings', { field: 'account' }, ); } const account = booksChartOf(policy).getAccount(input.account); const parents = SHAREHOLDER_LIABILITY_ACCOUNTS[chart]; const allowed = [parents.shareholder, parents[input.kind]]; if (account === null || !allowed.includes(account.parentAccountNumber ?? account.accountNumber) || account.accountType !== 'liability' || account.hgb266Position !== 'Passiva C.8' || account.euerExclusion !== 'balance_sheet' || account.euer !== undefined || account.ustva !== undefined || account.automatikFunction !== undefined || isAutomatikAccount(policy, input.account) || Object.values(policy.vat).includes(input.account)) { throw new RecipeRefusalError( 'shareholder_liability_account_not_allowed', `Choose a tax-free liability account of ${input.kind} in ${chart}, under ${[...new Set(allowed)].join(' or ')}; ${input.account} is not one`, { field: 'account', account: input.account, accounts: [...new Set(allowed)] }, ); } return account.accountNumber; }