diff --git a/server/src/services/FinancialStatements/GeneralLedger/GeneralLedger.ts b/server/src/services/FinancialStatements/GeneralLedger/GeneralLedger.ts index 608030b8e..fd6984ab0 100644 --- a/server/src/services/FinancialStatements/GeneralLedger/GeneralLedger.ts +++ b/server/src/services/FinancialStatements/GeneralLedger/GeneralLedger.ts @@ -1,4 +1,4 @@ -import { isEmpty, get, last } from 'lodash'; +import { isEmpty, get, last, sumBy } from 'lodash'; import { IGeneralLedgerSheetQuery, IGeneralLedgerSheetAccount, @@ -19,7 +19,6 @@ export default class GeneralLedgerSheet extends FinancialSheet { accounts: IAccount[]; query: IGeneralLedgerSheetQuery; openingBalancesJournal: IJournalPoster; - closingBalancesJournal: IJournalPoster; transactions: IJournalPoster; contactsMap: Map; baseCurrency: string; @@ -39,7 +38,7 @@ export default class GeneralLedgerSheet extends FinancialSheet { contactsByIdMap: Map, transactions: IJournalPoster, openingBalancesJournal: IJournalPoster, - closingBalancesJournal: IJournalPoster, + baseCurrency: string ) { super(); @@ -51,7 +50,6 @@ export default class GeneralLedgerSheet extends FinancialSheet { this.contactsMap = contactsByIdMap; this.transactions = transactions; this.openingBalancesJournal = openingBalancesJournal; - this.closingBalancesJournal = closingBalancesJournal; this.baseCurrency = baseCurrency; } @@ -162,9 +160,10 @@ export default class GeneralLedgerSheet extends FinancialSheet { * @return {IGeneralLedgerSheetAccountBalance} */ private accountClosingBalance( - account: IAccount + openingBalance: number, + transactions: IGeneralLedgerSheetAccountTransaction[] ): IGeneralLedgerSheetAccountBalance { - const amount = this.closingBalancesJournal.getAccountBalance(account.id); + const amount = this.calcClosingBalance(openingBalance, transactions); const formattedAmount = this.formatTotalNumber(amount); const currencyCode = this.baseCurrency; const date = this.query.toDate; @@ -172,6 +171,13 @@ export default class GeneralLedgerSheet extends FinancialSheet { return { amount, formattedAmount, currencyCode, date }; } + private calcClosingBalance( + openingBalance: number, + transactions: IGeneralLedgerSheetAccountTransaction[] + ) { + return openingBalance + sumBy(transactions, (trans) => trans.amount); + } + /** * Retreive general ledger accounts sections. * @param {IAccount} account @@ -179,7 +185,15 @@ export default class GeneralLedgerSheet extends FinancialSheet { */ private accountMapper(account: IAccount): IGeneralLedgerSheetAccount { const openingBalance = this.accountOpeningBalance(account); - const closingBalance = this.accountClosingBalance(account); + + const transactions = this.accountTransactionsMapper( + account, + openingBalance.amount + ); + const closingBalance = this.accountClosingBalance( + openingBalance.amount, + transactions + ); return { id: account.id, @@ -188,10 +202,7 @@ export default class GeneralLedgerSheet extends FinancialSheet { index: account.index, parentAccountId: account.parentAccountId, openingBalance, - transactions: this.accountTransactionsMapper( - account, - openingBalance.amount - ), + transactions, closingBalance, }; } diff --git a/server/src/services/FinancialStatements/GeneralLedger/GeneralLedgerService.ts b/server/src/services/FinancialStatements/GeneralLedger/GeneralLedgerService.ts index 0878305e1..cd9309e95 100644 --- a/server/src/services/FinancialStatements/GeneralLedger/GeneralLedgerService.ts +++ b/server/src/services/FinancialStatements/GeneralLedger/GeneralLedgerService.ts @@ -134,12 +134,7 @@ export default class GeneralLedgerService { }); // Retreive opening balance credit/debit sumation. const openingBalanceTrans = await transactionsRepository.journal({ - toDate: filter.fromDate, - sumationCreditDebit: true, - }); - // Retreive closing balance credit/debit sumation. - const closingBalanceTrans = await transactionsRepository.journal({ - toDate: filter.toDate, + toDate: moment(filter.fromDate).subtract(1, 'day'), sumationCreditDebit: true, }); // Transform array transactions to journal collection. @@ -154,12 +149,6 @@ export default class GeneralLedgerService { tenantId, accountsGraph ); - // Accounts closing transactions. - const closingTransJournal = Journal.fromTransactions( - closingBalanceTrans, - tenantId, - accountsGraph - ); // General ledger report instance. const generalLedgerInstance = new GeneralLedgerSheet( tenantId, @@ -168,7 +157,6 @@ export default class GeneralLedgerService { contactsByIdMap, transactionsJournal, openingTransJournal, - closingTransJournal, baseCurrency ); // Retrieve general ledger report data.