Skip to content

Commit

Permalink
feat: trial balance sheet adjusted total
Browse files Browse the repository at this point in the history
  • Loading branch information
abouolia committed Oct 24, 2023
1 parent ae6688a commit 1f1db41
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
9 changes: 9 additions & 0 deletions packages/server/src/services/Accounting/Ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

Expand All @@ -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;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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`;

Expand All @@ -13,7 +17,7 @@ const accountNameAccessor = R.curry((data, column) => {
id: column.key,
accessor,
className: column.key,
width: 350,
width: ACCOUNT_NAME_COLUMN_WIDTH,
};
});

Expand All @@ -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,
};
});
Expand Down

0 comments on commit 1f1db41

Please sign in to comment.