fix(GeneralLedger).

This commit is contained in:
a.bouhuolia
2021-03-29 16:31:15 +02:00
parent 5f573c095e
commit f2851d24c6
2 changed files with 23 additions and 24 deletions

View File

@@ -1,4 +1,4 @@
import { isEmpty, get, last } from 'lodash'; import { isEmpty, get, last, sumBy } from 'lodash';
import { import {
IGeneralLedgerSheetQuery, IGeneralLedgerSheetQuery,
IGeneralLedgerSheetAccount, IGeneralLedgerSheetAccount,
@@ -19,7 +19,6 @@ export default class GeneralLedgerSheet extends FinancialSheet {
accounts: IAccount[]; accounts: IAccount[];
query: IGeneralLedgerSheetQuery; query: IGeneralLedgerSheetQuery;
openingBalancesJournal: IJournalPoster; openingBalancesJournal: IJournalPoster;
closingBalancesJournal: IJournalPoster;
transactions: IJournalPoster; transactions: IJournalPoster;
contactsMap: Map<number, IContact>; contactsMap: Map<number, IContact>;
baseCurrency: string; baseCurrency: string;
@@ -39,7 +38,7 @@ export default class GeneralLedgerSheet extends FinancialSheet {
contactsByIdMap: Map<number, IContact>, contactsByIdMap: Map<number, IContact>,
transactions: IJournalPoster, transactions: IJournalPoster,
openingBalancesJournal: IJournalPoster, openingBalancesJournal: IJournalPoster,
closingBalancesJournal: IJournalPoster,
baseCurrency: string baseCurrency: string
) { ) {
super(); super();
@@ -51,7 +50,6 @@ export default class GeneralLedgerSheet extends FinancialSheet {
this.contactsMap = contactsByIdMap; this.contactsMap = contactsByIdMap;
this.transactions = transactions; this.transactions = transactions;
this.openingBalancesJournal = openingBalancesJournal; this.openingBalancesJournal = openingBalancesJournal;
this.closingBalancesJournal = closingBalancesJournal;
this.baseCurrency = baseCurrency; this.baseCurrency = baseCurrency;
} }
@@ -162,9 +160,10 @@ export default class GeneralLedgerSheet extends FinancialSheet {
* @return {IGeneralLedgerSheetAccountBalance} * @return {IGeneralLedgerSheetAccountBalance}
*/ */
private accountClosingBalance( private accountClosingBalance(
account: IAccount openingBalance: number,
transactions: IGeneralLedgerSheetAccountTransaction[]
): IGeneralLedgerSheetAccountBalance { ): IGeneralLedgerSheetAccountBalance {
const amount = this.closingBalancesJournal.getAccountBalance(account.id); const amount = this.calcClosingBalance(openingBalance, transactions);
const formattedAmount = this.formatTotalNumber(amount); const formattedAmount = this.formatTotalNumber(amount);
const currencyCode = this.baseCurrency; const currencyCode = this.baseCurrency;
const date = this.query.toDate; const date = this.query.toDate;
@@ -172,6 +171,13 @@ export default class GeneralLedgerSheet extends FinancialSheet {
return { amount, formattedAmount, currencyCode, date }; return { amount, formattedAmount, currencyCode, date };
} }
private calcClosingBalance(
openingBalance: number,
transactions: IGeneralLedgerSheetAccountTransaction[]
) {
return openingBalance + sumBy(transactions, (trans) => trans.amount);
}
/** /**
* Retreive general ledger accounts sections. * Retreive general ledger accounts sections.
* @param {IAccount} account * @param {IAccount} account
@@ -179,7 +185,15 @@ export default class GeneralLedgerSheet extends FinancialSheet {
*/ */
private accountMapper(account: IAccount): IGeneralLedgerSheetAccount { private accountMapper(account: IAccount): IGeneralLedgerSheetAccount {
const openingBalance = this.accountOpeningBalance(account); 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 { return {
id: account.id, id: account.id,
@@ -188,10 +202,7 @@ export default class GeneralLedgerSheet extends FinancialSheet {
index: account.index, index: account.index,
parentAccountId: account.parentAccountId, parentAccountId: account.parentAccountId,
openingBalance, openingBalance,
transactions: this.accountTransactionsMapper( transactions,
account,
openingBalance.amount
),
closingBalance, closingBalance,
}; };
} }

View File

@@ -134,12 +134,7 @@ export default class GeneralLedgerService {
}); });
// Retreive opening balance credit/debit sumation. // Retreive opening balance credit/debit sumation.
const openingBalanceTrans = await transactionsRepository.journal({ const openingBalanceTrans = await transactionsRepository.journal({
toDate: filter.fromDate, toDate: moment(filter.fromDate).subtract(1, 'day'),
sumationCreditDebit: true,
});
// Retreive closing balance credit/debit sumation.
const closingBalanceTrans = await transactionsRepository.journal({
toDate: filter.toDate,
sumationCreditDebit: true, sumationCreditDebit: true,
}); });
// Transform array transactions to journal collection. // Transform array transactions to journal collection.
@@ -154,12 +149,6 @@ export default class GeneralLedgerService {
tenantId, tenantId,
accountsGraph accountsGraph
); );
// Accounts closing transactions.
const closingTransJournal = Journal.fromTransactions(
closingBalanceTrans,
tenantId,
accountsGraph
);
// General ledger report instance. // General ledger report instance.
const generalLedgerInstance = new GeneralLedgerSheet( const generalLedgerInstance = new GeneralLedgerSheet(
tenantId, tenantId,
@@ -168,7 +157,6 @@ export default class GeneralLedgerService {
contactsByIdMap, contactsByIdMap,
transactionsJournal, transactionsJournal,
openingTransJournal, openingTransJournal,
closingTransJournal,
baseCurrency baseCurrency
); );
// Retrieve general ledger report data. // Retrieve general ledger report data.