mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-20 06:40:31 +00:00
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:
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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'),
|
||||||
|
|||||||
Reference in New Issue
Block a user