fix: Closing balance in general ledger report does not sum the negative figures.

This commit is contained in:
Ahmed Bouhuolia
2024-06-04 21:26:46 +02:00
parent 909a70e2c5
commit 6afe1a09c6
4 changed files with 152 additions and 97 deletions

View File

@@ -1,18 +1,10 @@
import { Service, Inject } from 'typedi';
import moment from 'moment';
import { ServiceError } from '@/exceptions';
import { difference } from 'lodash';
import { IGeneralLedgerSheetQuery, IGeneralLedgerMeta } from '@/interfaces';
import TenancyService from '@/services/Tenancy/TenancyService';
import Journal from '@/services/Accounting/JournalPoster';
import GeneralLedgerSheet from '@/services/FinancialStatements/GeneralLedger/GeneralLedger';
import { transformToMap } from 'utils';
import { Tenant } from '@/system/models';
import { GeneralLedgerMeta } from './GeneralLedgerMeta';
const ERRORS = {
ACCOUNTS_NOT_FOUND: 'ACCOUNTS_NOT_FOUND',
};
import { GeneralLedgerRepository } from './GeneralLedgerRepository';
@Service()
export class GeneralLedgerService {
@@ -40,29 +32,13 @@ export class GeneralLedgerService {
};
}
/**
* Validates accounts existance on the storage.
* @param {number} tenantId
* @param {number[]} accountsIds
*/
async validateAccountsExistance(tenantId: number, accountsIds: number[]) {
const { Account } = this.tenancy.models(tenantId);
const storedAccounts = await Account.query().whereIn('id', accountsIds);
const storedAccountsIds = storedAccounts.map((a) => a.id);
if (difference(accountsIds, storedAccountsIds).length > 0) {
throw new ServiceError(ERRORS.ACCOUNTS_NOT_FOUND);
}
}
/**
* Retrieve general ledger report statement.
* @param {number} tenantId
* @param {IGeneralLedgerSheetQuery} query
* @return {IGeneralLedgerStatement}
* @return {Promise<IGeneralLedgerStatement>}
*/
async generalLedger(
public async generalLedger(
tenantId: number,
query: IGeneralLedgerSheetQuery
): Promise<{
@@ -70,60 +46,25 @@ export class GeneralLedgerService {
query: IGeneralLedgerSheetQuery;
meta: IGeneralLedgerMeta;
}> {
const { accountRepository, transactionsRepository, contactRepository } =
this.tenancy.repositories(tenantId);
const repositories = this.tenancy.repositories(tenantId);
const i18n = this.tenancy.i18n(tenantId);
const tenant = await Tenant.query()
.findById(tenantId)
.withGraphFetched('metadata');
const filter = {
...this.defaultQuery,
...query,
};
// Retrieve all accounts with associated type from the storage.
const accounts = await accountRepository.all();
const accountsGraph = await accountRepository.getDependencyGraph();
// Retrieve all contacts on the storage.
const contacts = await contactRepository.all();
const contactsByIdMap = transformToMap(contacts, 'id');
// Retreive journal transactions from/to the given date.
const transactions = await transactionsRepository.journal({
fromDate: filter.fromDate,
toDate: filter.toDate,
branchesIds: filter.branchesIds,
});
// Retreive opening balance credit/debit sumation.
const openingBalanceTrans = await transactionsRepository.journal({
toDate: moment(filter.fromDate).subtract(1, 'day'),
sumationCreditDebit: true,
branchesIds: filter.branchesIds,
});
// Transform array transactions to journal collection.
const transactionsJournal = Journal.fromTransactions(
transactions,
tenantId,
accountsGraph
);
// Accounts opening transactions.
const openingTransJournal = Journal.fromTransactions(
openingBalanceTrans,
tenantId,
accountsGraph
const genealLedgerRepository = new GeneralLedgerRepository(
repositories,
query,
tenantId
);
await genealLedgerRepository.asyncInitialize();
// General ledger report instance.
const generalLedgerInstance = new GeneralLedgerSheet(
tenantId,
filter,
accounts,
contactsByIdMap,
transactionsJournal,
openingTransJournal,
tenant.metadata.baseCurrency,
genealLedgerRepository,
i18n
);
// Retrieve general ledger report data.