diff --git a/packages/server/src/services/Accounting/Ledger.ts b/packages/server/src/services/Accounting/Ledger.ts index ab241ebe2..e8bbcdfa4 100644 --- a/packages/server/src/services/Accounting/Ledger.ts +++ b/packages/server/src/services/Accounting/Ledger.ts @@ -49,6 +49,15 @@ export default class Ledger implements ILedger { return this.filter((entry) => entry.accountId === accountId); } + /** + * Filters entries by the given accounts ids then returns a new ledger. + * @param {number[]} accountsIds - Accounts ids. + * @returns {ILedger} + */ + public whereAccountsIds(accountsIds: number[]): ILedger { + return this.filter((entry) => accountsIds.indexOf(entry.accountId) !== -1); + } + /** * Filters entries that before or same the given date and returns a new ledger. * @param {Date|string} fromDate diff --git a/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheet.ts b/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheet.ts index 61ec665ec..38ed3a944 100644 --- a/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheet.ts +++ b/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheet.ts @@ -59,8 +59,11 @@ export default class TrialBalanceSheet extends FinancialSheet { * @returns {number} */ public getClosingAccountCredit(accountId: number) { + const depsAccountsIds = + this.repository.accountsDepGraph.dependenciesOf(accountId); + return this.repository.totalAccountsLedger - .whereAccountId(accountId) + .whereAccountsIds([accountId, ...depsAccountsIds]) .getClosingCredit(); } @@ -70,8 +73,11 @@ export default class TrialBalanceSheet extends FinancialSheet { * @returns {number} */ public getClosingAccountDebit(accountId: number) { + const depsAccountsIds = + this.repository.accountsDepGraph.dependenciesOf(accountId); + return this.repository.totalAccountsLedger - .whereAccountId(accountId) + .whereAccountsIds([accountId, ...depsAccountsIds]) .getClosingDebit(); } diff --git a/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetRepository.ts b/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetRepository.ts index cffc71c7a..1c37edd4d 100644 --- a/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetRepository.ts +++ b/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetRepository.ts @@ -8,11 +8,8 @@ import { Service } from 'typedi'; export class TrialBalanceSheetRepository { private query: ITrialBalanceSheetQuery; private models: any; - - /** - * - */ public accounts: any; + public accountsDepGraph; /** * Total closing accounts ledger. @@ -25,8 +22,9 @@ export class TrialBalanceSheetRepository { * @param {number} tenantId * @param {IBalanceSheetQuery} query */ - constructor(models: any, query: ITrialBalanceSheetQuery) { + constructor(models: any, repos: any, query: ITrialBalanceSheetQuery) { this.query = query; + this.repos = repos; this.models = models; } @@ -48,7 +46,10 @@ export class TrialBalanceSheetRepository { */ public initAccounts = async () => { const accounts = await this.getAccounts(); + const accountsDepGraph = + await this.repos.accountRepository.getDependencyGraph(); + this.accountsDepGraph = accountsDepGraph; this.accounts = accounts; }; diff --git a/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetService.ts b/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetService.ts index 1b7789d62..150f160b0 100644 --- a/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetService.ts +++ b/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetService.ts @@ -95,9 +95,11 @@ export default class TrialBalanceSheetService extends FinancialSheet { .withGraphFetched('metadata'); const models = this.tenancy.models(tenantId); + const repos = this.tenancy.repositories(tenantId); const trialBalanceSheetRepos = new TrialBalanceSheetRepository( models, + repos, filter ); await trialBalanceSheetRepos.asyncInitialize(); diff --git a/packages/webapp/src/containers/FinancialStatements/TrialBalanceSheet/dynamicColumns.ts b/packages/webapp/src/containers/FinancialStatements/TrialBalanceSheet/dynamicColumns.ts index 0413c41a0..c7f23f2aa 100644 --- a/packages/webapp/src/containers/FinancialStatements/TrialBalanceSheet/dynamicColumns.ts +++ b/packages/webapp/src/containers/FinancialStatements/TrialBalanceSheet/dynamicColumns.ts @@ -1,7 +1,11 @@ // @ts-nocheck +import * as R from 'ramda'; import { Align } from '@/constants'; import { getColumnWidth } from '@/utils'; -import * as R from 'ramda'; + +const ACCOUNT_NAME_COLUMN_WIDTH = 320; +const AMOUNT_COLUMNS_MIN_WIDTH = 120; +const AMOUNT_COLUMNS_MAGIC_SPACING = 10; const getTableCellValueAccessor = (index: number) => `cells[${index}].value`; @@ -13,7 +17,7 @@ const accountNameAccessor = R.curry((data, column) => { id: column.key, accessor, className: column.key, - width: 350, + width: ACCOUNT_NAME_COLUMN_WIDTH, }; }); @@ -25,7 +29,10 @@ const amountAccessor = R.curry((data, column) => { id: column.key, accessor, className: column.key, - width: getColumnWidth(data, accessor, { minWidth: 120 }), + width: getColumnWidth(data, accessor, { + magicSpacing: AMOUNT_COLUMNS_MAGIC_SPACING, + minWidth: AMOUNT_COLUMNS_MIN_WIDTH, + }), align: Align.Right, }; });