fix: Balance sheet and P/L nested accounts

This commit is contained in:
Ahmed Bouhuolia
2024-06-12 13:05:02 +02:00
parent 858f347fd4
commit cfd37f8894
4 changed files with 89 additions and 16 deletions

View File

@@ -20,6 +20,8 @@ import { BalanceSheetPercentage } from './BalanceSheetPercentage';
import { BalanceSheetSchema } from './BalanceSheetSchema';
import { BalanceSheetBase } from './BalanceSheetBase';
import { BalanceSheetQuery } from './BalanceSheetQuery';
import { flatToNestedArray } from '@/utils';
import BalanceSheetRepository from './BalanceSheetRepository';
export const BalanceSheetAccounts = (Base: any) =>
class extends R.compose(
@@ -56,6 +58,11 @@ export const BalanceSheetAccounts = (Base: any) =>
*/
readonly i18n: any;
/**
* Balance sheet repository.
*/
readonly repository: BalanceSheetRepository;
/**
* Retrieve the accounts node of accounts types.
* @param {string} accountsTypes
@@ -78,8 +85,12 @@ export const BalanceSheetAccounts = (Base: any) =>
private reportSchemaAccountNodeMapper = (
account: IAccount
): IBalanceSheetAccountNode => {
const childrenAccountsIds = this.repository.accountsGraph.dependenciesOf(
account.id
);
const accountIds = R.uniq(R.append(account.id, childrenAccountsIds));
const total = this.repository.totalAccountsLedger
.whereAccountId(account.id)
.whereAccountsIds(accountIds)
.getClosingBalance();
return {
@@ -128,8 +139,19 @@ export const BalanceSheetAccounts = (Base: any) =>
private getAccountsNodesByAccountTypes = (
accountsTypes: string[]
): IBalanceSheetAccountNode[] => {
// Retrieves accounts from the given defined node account types.
const accounts = this.getAccountsByAccountTypes(accountsTypes);
return R.map(this.reportSchemaAccountNodeComposer, accounts);
// Converts the flatten accounts to tree.
const accountsTree = flatToNestedArray(accounts, {
id: 'id',
parentId: 'parentAccountId',
});
// Maps over the accounts tree.
return this.mapNodesDeep(
accountsTree,
this.reportSchemaAccountNodeComposer
);
};
/**

View File

@@ -3,7 +3,6 @@ import * as R from 'ramda';
import { Knex } from 'knex';
import { isEmpty } from 'lodash';
import {
IAccount,
IAccountTransactionsGroupBy,
IBalanceSheetQuery,
ILedger,
@@ -12,7 +11,6 @@ import { transformToMapBy } from 'utils';
import Ledger from '@/services/Accounting/Ledger';
import { BalanceSheetQuery } from './BalanceSheetQuery';
import { FinancialDatePeriods } from '../FinancialDatePeriods';
import { ACCOUNT_PARENT_TYPE, ACCOUNT_TYPE } from '@/data/AccountTypes';
import { BalanceSheetRepositoryNetIncome } from './BalanceSheetRepositoryNetIncome';
@Service()
@@ -40,6 +38,11 @@ export default class BalanceSheetRepository extends R.compose(
*/
public accounts: any;
/**
* @param {}
*/
public accountsGraph: any;
/**
*
*/
@@ -163,6 +166,8 @@ export default class BalanceSheetRepository extends R.compose(
*/
public asyncInitialize = async () => {
await this.initAccounts();
await this.initAccountsGraph();
await this.initAccountsTotalLedger();
// Date periods.
@@ -204,6 +209,15 @@ export default class BalanceSheetRepository extends R.compose(
this.accountsByParentType = transformToMapBy(accounts, 'accountParentType');
};
/**
* Initialize accounts graph.
*/
public initAccountsGraph = async () => {
const { Account } = this.models;
this.accountsGraph = Account.toDependencyGraph(this.accounts);
};
// ----------------------------
// # Closing Total
// ----------------------------