From d79f26f1b50a1690a1f0e6f0c3c54b5f49ed4673 Mon Sep 17 00:00:00 2001 From: Nana Kofi Larbi Mantey Date: Sun, 20 Oct 2024 17:44:39 +0000 Subject: [PATCH 1/4] refactors backend validator for credit-debit equals --- .../CommandManualJournalValidators.ts | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/server/src/services/ManualJournals/CommandManualJournalValidators.ts b/packages/server/src/services/ManualJournals/CommandManualJournalValidators.ts index 3e3e6c7b1..40c8dd509 100644 --- a/packages/server/src/services/ManualJournals/CommandManualJournalValidators.ts +++ b/packages/server/src/services/ManualJournals/CommandManualJournalValidators.ts @@ -23,23 +23,20 @@ export class CommandManualJournalValidators { * @param {IManualJournalDTO} manualJournalDTO */ public valdiateCreditDebitTotalEquals(manualJournalDTO: IManualJournalDTO) { - let totalCredit = 0; - let totalDebit = 0; + const totalCredit = this.roundToTwoDecimals( + manualJournalDTO.entries.reduce((sum, entry) => sum + (entry.credit || 0), 0) + ); + const totalDebit = this.roundToTwoDecimals( + manualJournalDTO.entries.reduce((sum, entry) => sum + (entry.debit || 0), 0) + ); - manualJournalDTO.entries.forEach((entry) => { - if (entry.credit > 0) { - totalCredit += entry.credit; - } - if (entry.debit > 0) { - totalDebit += entry.debit; - } - }); 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); - } + } + + private roundToTwoDecimals(value: number): number { + return Math.round(value * 100) / 100; } /** @@ -308,3 +305,4 @@ export class CommandManualJournalValidators { } }; } + From c8c27868932bd321ff75c1320d678d7b42938351 Mon Sep 17 00:00:00 2001 From: Nana Kofi Larbi Mantey Date: Sun, 20 Oct 2024 17:45:44 +0000 Subject: [PATCH 2/4] refactors getTotal function loginc --- .../MakeJournal/MakeJournalEntriesForm.tsx | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/webapp/src/containers/Accounting/MakeJournal/MakeJournalEntriesForm.tsx b/packages/webapp/src/containers/Accounting/MakeJournal/MakeJournalEntriesForm.tsx index a47b3b6aa..fd5bf7781 100644 --- a/packages/webapp/src/containers/Accounting/MakeJournal/MakeJournalEntriesForm.tsx +++ b/packages/webapp/src/containers/Accounting/MakeJournal/MakeJournalEntriesForm.tsx @@ -67,17 +67,17 @@ function MakeJournalEntriesForm({ () => ({ ...(!isEmpty(manualJournal) ? { - ...transformToEditForm(manualJournal), - } + ...transformToEditForm(manualJournal), + } : { - ...defaultManualJournal, - // If the auto-increment mode is enabled, take the next journal - // number from the settings. - ...(journalAutoIncrement && { - journal_number: journalNumber, - }), - currency_code: base_currency, + ...defaultManualJournal, + // If the auto-increment mode is enabled, take the next journal + // number from the settings. + ...(journalAutoIncrement && { + journal_number: journalNumber, }), + currency_code: base_currency, + }), }), [manualJournal, base_currency, journalNumber, journalAutoIncrement], ); @@ -88,15 +88,19 @@ function MakeJournalEntriesForm({ const entries = values.entries.filter( (entry) => entry.debit || entry.credit, ); + + // Updated getTotal function const getTotal = (type = 'credit') => { return entries.reduce((total, item) => { - return item[type] ? item[type] + total : total; + const value = item[type] ? parseFloat(item[type]) : 0; + return Math.round((total + value) * 100) / 100; }, 0); }; + 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'), From bbc19df6b435fb21aa0535c756de3b748e356932 Mon Sep 17 00:00:00 2001 From: Nana Kofi Larbi Mantey Date: Sun, 20 Oct 2024 19:04:10 +0000 Subject: [PATCH 3/4] adds CREDIT_DEBIT_NOT_EQUAL error --- .../services/ManualJournals/CommandManualJournalValidators.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/server/src/services/ManualJournals/CommandManualJournalValidators.ts b/packages/server/src/services/ManualJournals/CommandManualJournalValidators.ts index 40c8dd509..ebb8321b0 100644 --- a/packages/server/src/services/ManualJournals/CommandManualJournalValidators.ts +++ b/packages/server/src/services/ManualJournals/CommandManualJournalValidators.ts @@ -33,6 +33,10 @@ export class CommandManualJournalValidators { 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); + } } private roundToTwoDecimals(value: number): number { From 4c0f9a0aef714c9afd5831cde69b9147613a553b Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Thu, 24 Oct 2024 16:47:29 +0200 Subject: [PATCH 4/4] fix: using lodash utils for decimal rounding --- .../CommandManualJournalValidators.ts | 18 ++++------- .../MakeJournal/MakeJournalEntriesForm.tsx | 32 +++++++++---------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/packages/server/src/services/ManualJournals/CommandManualJournalValidators.ts b/packages/server/src/services/ManualJournals/CommandManualJournalValidators.ts index ebb8321b0..d165fbca9 100644 --- a/packages/server/src/services/ManualJournals/CommandManualJournalValidators.ts +++ b/packages/server/src/services/ManualJournals/CommandManualJournalValidators.ts @@ -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,13 +23,14 @@ export class CommandManualJournalValidators { * @param {IManualJournalDTO} manualJournalDTO */ public valdiateCreditDebitTotalEquals(manualJournalDTO: IManualJournalDTO) { - const totalCredit = this.roundToTwoDecimals( - manualJournalDTO.entries.reduce((sum, entry) => sum + (entry.credit || 0), 0) + const totalCredit = round( + sumBy(manualJournalDTO.entries, (entry) => entry.credit || 0), + 2 ); - const totalDebit = this.roundToTwoDecimals( - manualJournalDTO.entries.reduce((sum, entry) => sum + (entry.debit || 0), 0) + 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); } @@ -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. * @param {number} tenantId - @@ -309,4 +306,3 @@ export class CommandManualJournalValidators { } }; } - diff --git a/packages/webapp/src/containers/Accounting/MakeJournal/MakeJournalEntriesForm.tsx b/packages/webapp/src/containers/Accounting/MakeJournal/MakeJournalEntriesForm.tsx index fd5bf7781..02a347b13 100644 --- a/packages/webapp/src/containers/Accounting/MakeJournal/MakeJournalEntriesForm.tsx +++ b/packages/webapp/src/containers/Accounting/MakeJournal/MakeJournalEntriesForm.tsx @@ -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'; @@ -67,17 +67,17 @@ function MakeJournalEntriesForm({ () => ({ ...(!isEmpty(manualJournal) ? { - ...transformToEditForm(manualJournal), - } + ...transformToEditForm(manualJournal), + } : { - ...defaultManualJournal, - // If the auto-increment mode is enabled, take the next journal - // number from the settings. - ...(journalAutoIncrement && { - journal_number: journalNumber, + ...defaultManualJournal, + // If the auto-increment mode is enabled, take the next journal + // number from the settings. + ...(journalAutoIncrement && { + journal_number: journalNumber, + }), + currency_code: base_currency, }), - currency_code: base_currency, - }), }), [manualJournal, base_currency, journalNumber, journalAutoIncrement], ); @@ -88,15 +88,13 @@ function MakeJournalEntriesForm({ const entries = values.entries.filter( (entry) => entry.debit || entry.credit, ); - - // Updated getTotal function + // Updated getTotal function using lodash const getTotal = (type = 'credit') => { - return entries.reduce((total, item) => { - const value = item[type] ? parseFloat(item[type]) : 0; - return Math.round((total + value) * 100) / 100; - }, 0); + return round( + sumBy(entries, (entry) => parseFloat(entry[type] || 0)), + 2, + ); }; - const totalCredit = getTotal('credit'); const totalDebit = getTotal('debit');