mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 15:20:34 +00:00
fix: using lodash utils for decimal rounding
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,13 +23,14 @@ export class CommandManualJournalValidators {
|
|||||||
* @param {IManualJournalDTO} manualJournalDTO
|
* @param {IManualJournalDTO} manualJournalDTO
|
||||||
*/
|
*/
|
||||||
public valdiateCreditDebitTotalEquals(manualJournalDTO: IManualJournalDTO) {
|
public valdiateCreditDebitTotalEquals(manualJournalDTO: IManualJournalDTO) {
|
||||||
const totalCredit = this.roundToTwoDecimals(
|
const totalCredit = round(
|
||||||
manualJournalDTO.entries.reduce((sum, entry) => sum + (entry.credit || 0), 0)
|
sumBy(manualJournalDTO.entries, (entry) => entry.credit || 0),
|
||||||
|
2
|
||||||
);
|
);
|
||||||
const totalDebit = this.roundToTwoDecimals(
|
const totalDebit = round(
|
||||||
manualJournalDTO.entries.reduce((sum, entry) => sum + (entry.debit || 0), 0)
|
sumBy(manualJournalDTO.entries, (entry) => entry.debit || 0),
|
||||||
|
2
|
||||||
);
|
);
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
@@ -39,10 +40,6 @@ export class CommandManualJournalValidators {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private roundToTwoDecimals(value: number): number {
|
|
||||||
return Math.round(value * 100) / 100;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate manual entries accounts existance on the storage.
|
* Validate manual entries accounts existance on the storage.
|
||||||
* @param {number} tenantId -
|
* @param {number} tenantId -
|
||||||
@@ -309,4 +306,3 @@ export class CommandManualJournalValidators {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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';
|
||||||
|
|
||||||
@@ -67,17 +67,17 @@ function MakeJournalEntriesForm({
|
|||||||
() => ({
|
() => ({
|
||||||
...(!isEmpty(manualJournal)
|
...(!isEmpty(manualJournal)
|
||||||
? {
|
? {
|
||||||
...transformToEditForm(manualJournal),
|
...transformToEditForm(manualJournal),
|
||||||
}
|
}
|
||||||
: {
|
: {
|
||||||
...defaultManualJournal,
|
...defaultManualJournal,
|
||||||
// If the auto-increment mode is enabled, take the next journal
|
// If the auto-increment mode is enabled, take the next journal
|
||||||
// number from the settings.
|
// number from the settings.
|
||||||
...(journalAutoIncrement && {
|
...(journalAutoIncrement && {
|
||||||
journal_number: journalNumber,
|
journal_number: journalNumber,
|
||||||
|
}),
|
||||||
|
currency_code: base_currency,
|
||||||
}),
|
}),
|
||||||
currency_code: base_currency,
|
|
||||||
}),
|
|
||||||
}),
|
}),
|
||||||
[manualJournal, base_currency, journalNumber, journalAutoIncrement],
|
[manualJournal, base_currency, journalNumber, journalAutoIncrement],
|
||||||
);
|
);
|
||||||
@@ -88,15 +88,13 @@ 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
|
||||||
// Updated getTotal function
|
|
||||||
const getTotal = (type = 'credit') => {
|
const getTotal = (type = 'credit') => {
|
||||||
return entries.reduce((total, item) => {
|
return round(
|
||||||
const value = item[type] ? parseFloat(item[type]) : 0;
|
sumBy(entries, (entry) => parseFloat(entry[type] || 0)),
|
||||||
return Math.round((total + value) * 100) / 100;
|
2,
|
||||||
}, 0);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const totalCredit = getTotal('credit');
|
const totalCredit = getTotal('credit');
|
||||||
const totalDebit = getTotal('debit');
|
const totalDebit = getTotal('debit');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user