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 { ServiceError } from '@/exceptions';
import {
@@ -23,20 +23,18 @@ export class CommandManualJournalValidators {
* @param {IManualJournalDTO} manualJournalDTO
*/
public valdiateCreditDebitTotalEquals(manualJournalDTO: IManualJournalDTO) {
let totalCredit = 0;
let totalDebit = 0;
manualJournalDTO.entries.forEach((entry) => {
if (entry.credit > 0) {
totalCredit += entry.credit;
}
if (entry.debit > 0) {
totalDebit += entry.debit;
}
});
const totalCredit = round(
sumBy(manualJournalDTO.entries, (entry) => entry.credit || 0),
2
);
const totalDebit = round(
sumBy(manualJournalDTO.entries, (entry) => entry.debit || 0),
2
);
if (totalCredit <= 0 || totalDebit <= 0) {
throw new ServiceError(ERRORS.CREDIT_DEBIT_NOT_EQUAL_ZERO);
}
if (totalCredit !== totalDebit) {
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 intl from 'react-intl-universal';
import * as R from 'ramda';
import { isEmpty, omit } from 'lodash';
import { sumBy, round, isEmpty, omit } from 'lodash';
import classNames from 'classnames';
import { useHistory } from 'react-router-dom';
@@ -88,15 +88,17 @@ function MakeJournalEntriesForm({
const entries = values.entries.filter(
(entry) => entry.debit || entry.credit,
);
// Updated getTotal function using lodash
const getTotal = (type = 'credit') => {
return entries.reduce((total, item) => {
return item[type] ? item[type] + total : total;
}, 0);
return round(
sumBy(entries, (entry) => parseFloat(entry[type] || 0)),
2,
);
};
const totalCredit = getTotal('credit');
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) {
AppToaster.show({
message: intl.get('should_total_of_credit_and_debit_be_equal'),