Merge pull request #722 from nklmantey/BIG-257-not-balanced-if-decimal

Fix: Credit and debit totals not balancing when decimal values are used
This commit is contained in:
Ahmed Bouhuolia
2024-10-24 16:49:52 +02:00
committed by GitHub
2 changed files with 17 additions and 17 deletions

View File

@@ -1,4 +1,4 @@
import { difference, isEmpty } from 'lodash'; import { difference, isEmpty, round, sumBy } from 'lodash';
import { Service, Inject } from 'typedi'; import { Service, Inject } from 'typedi';
import { ServiceError } from '@/exceptions'; import { ServiceError } from '@/exceptions';
import { import {
@@ -23,20 +23,18 @@ export class CommandManualJournalValidators {
* @param {IManualJournalDTO} manualJournalDTO * @param {IManualJournalDTO} manualJournalDTO
*/ */
public valdiateCreditDebitTotalEquals(manualJournalDTO: IManualJournalDTO) { public valdiateCreditDebitTotalEquals(manualJournalDTO: IManualJournalDTO) {
let totalCredit = 0; const totalCredit = round(
let totalDebit = 0; sumBy(manualJournalDTO.entries, (entry) => entry.credit || 0),
2
manualJournalDTO.entries.forEach((entry) => { );
if (entry.credit > 0) { const totalDebit = round(
totalCredit += entry.credit; sumBy(manualJournalDTO.entries, (entry) => entry.debit || 0),
} 2
if (entry.debit > 0) { );
totalDebit += entry.debit;
}
});
if (totalCredit <= 0 || totalDebit <= 0) { if (totalCredit <= 0 || totalDebit <= 0) {
throw new ServiceError(ERRORS.CREDIT_DEBIT_NOT_EQUAL_ZERO); throw new ServiceError(ERRORS.CREDIT_DEBIT_NOT_EQUAL_ZERO);
} }
if (totalCredit !== totalDebit) { if (totalCredit !== totalDebit) {
throw new ServiceError(ERRORS.CREDIT_DEBIT_NOT_EQUAL); throw new ServiceError(ERRORS.CREDIT_DEBIT_NOT_EQUAL);
} }

View File

@@ -4,7 +4,7 @@ import { Formik, Form } from 'formik';
import { Intent } from '@blueprintjs/core'; import { Intent } from '@blueprintjs/core';
import intl from 'react-intl-universal'; import intl from 'react-intl-universal';
import * as R from 'ramda'; import * as R from 'ramda';
import { isEmpty, omit } from 'lodash'; import { sumBy, round, isEmpty, omit } from 'lodash';
import classNames from 'classnames'; import classNames from 'classnames';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
@@ -88,15 +88,17 @@ function MakeJournalEntriesForm({
const entries = values.entries.filter( const entries = values.entries.filter(
(entry) => entry.debit || entry.credit, (entry) => entry.debit || entry.credit,
); );
// Updated getTotal function using lodash
const getTotal = (type = 'credit') => { const getTotal = (type = 'credit') => {
return entries.reduce((total, item) => { return round(
return item[type] ? item[type] + total : total; sumBy(entries, (entry) => parseFloat(entry[type] || 0)),
}, 0); 2,
);
}; };
const totalCredit = getTotal('credit'); const totalCredit = getTotal('credit');
const totalDebit = getTotal('debit'); const totalDebit = getTotal('debit');
// Validate the total credit should be eqials total debit. // Validate the total credit should be equals total debit.
if (totalCredit !== totalDebit) { if (totalCredit !== totalDebit) {
AppToaster.show({ AppToaster.show({
message: intl.get('should_total_of_credit_and_debit_be_equal'), message: intl.get('should_total_of_credit_and_debit_be_equal'),