From 1edfd7be0d4435ade6dff559ec4cb300262de583 Mon Sep 17 00:00:00 2001 From: Chris Lomame Date: Tue, 30 Jul 2019 15:38:10 +0100 Subject: [PATCH 1/3] fix (Invoicing Bug) - resolution of the problem when calling the Debtors.balance function with rounding to two ranks after the decimal point of the total credit and debit values closes #3822 --- server/controllers/finance/debtors/index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/server/controllers/finance/debtors/index.js b/server/controllers/finance/debtors/index.js index f83acdb2ee..2df550322e 100644 --- a/server/controllers/finance/debtors/index.js +++ b/server/controllers/finance/debtors/index.js @@ -261,9 +261,15 @@ function balance(debtorUuid, excludeCautionLinks = false) { const excludeCautionLinkStatement = `AND transaction_type_id <> ${CAUTION_LINK_TYPE_ID}`; + /** + * resolution of the problem when calling the Debtors.balance function with + * rounding to two ranks after the decimal point of the total credit and + * debit values + * + */ const sql = ` - SELECT IFNULL(SUM(ledger.debit_equiv), 0) AS debit, IFNULL(SUM(ledger.credit_equiv), 0) AS credit, - IFNULL(SUM(ledger.debit_equiv - ledger.credit_equiv), 0) AS balance, MIN(trans_date) AS since, + SELECT ROUND(IFNULL(SUM(ledger.debit_equiv), 0), 2) AS debit, ROUND(IFNULL(SUM(ledger.credit_equiv), 0),2) AS credit, + ROUND(IFNULL(SUM(ledger.debit_equiv - ledger.credit_equiv), 0),2) AS balance, MIN(trans_date) AS since, MAX(trans_date) AS until FROM ( SELECT debit_equiv, credit_equiv, entity_uuid, trans_date FROM posting_journal From b613b29915cf03110c4263f90a952c30c4720728 Mon Sep 17 00:00:00 2001 From: Chris Lomame Date: Tue, 30 Jul 2019 15:43:32 +0100 Subject: [PATCH 2/3] Improve and sanitaze code --- server/controllers/finance/debtors/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/server/controllers/finance/debtors/index.js b/server/controllers/finance/debtors/index.js index 2df550322e..25e90243d0 100644 --- a/server/controllers/finance/debtors/index.js +++ b/server/controllers/finance/debtors/index.js @@ -268,9 +268,10 @@ function balance(debtorUuid, excludeCautionLinks = false) { * */ const sql = ` - SELECT ROUND(IFNULL(SUM(ledger.debit_equiv), 0), 2) AS debit, ROUND(IFNULL(SUM(ledger.credit_equiv), 0),2) AS credit, - ROUND(IFNULL(SUM(ledger.debit_equiv - ledger.credit_equiv), 0),2) AS balance, MIN(trans_date) AS since, - MAX(trans_date) AS until + SELECT ROUND(IFNULL(SUM(ledger.debit_equiv), 0), 2) AS debit, + ROUND(IFNULL(SUM(ledger.credit_equiv), 0),2) AS credit, + ROUND(IFNULL(SUM(ledger.debit_equiv - ledger.credit_equiv), 0),2) AS balance, + MIN(trans_date) AS since, MAX(trans_date) AS until FROM ( SELECT debit_equiv, credit_equiv, entity_uuid, trans_date FROM posting_journal WHERE entity_uuid = ? ${excludeCautionLinks ? excludeCautionLinkStatement : ''} From b289a4e9d6a6fe431f48f63060ab888b442b6044 Mon Sep 17 00:00:00 2001 From: Chris Lomame Date: Wed, 31 Jul 2019 18:35:13 +0100 Subject: [PATCH 3/3] improvement(Invoicing Bug) - Restoring the old debtor balance request and modifying the hasCreditorBalance calculation by analyzing whether the balance is greater than 0.01 --- server/controllers/finance/debtors/index.js | 7 +++---- server/controllers/finance/patientInvoice.js | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/server/controllers/finance/debtors/index.js b/server/controllers/finance/debtors/index.js index 25e90243d0..1882facbaa 100644 --- a/server/controllers/finance/debtors/index.js +++ b/server/controllers/finance/debtors/index.js @@ -268,10 +268,9 @@ function balance(debtorUuid, excludeCautionLinks = false) { * */ const sql = ` - SELECT ROUND(IFNULL(SUM(ledger.debit_equiv), 0), 2) AS debit, - ROUND(IFNULL(SUM(ledger.credit_equiv), 0),2) AS credit, - ROUND(IFNULL(SUM(ledger.debit_equiv - ledger.credit_equiv), 0),2) AS balance, - MIN(trans_date) AS since, MAX(trans_date) AS until + SELECT IFNULL(SUM(ledger.debit_equiv), 0) AS debit, IFNULL(SUM(ledger.credit_equiv), 0) AS credit, + IFNULL(SUM(ledger.debit_equiv - ledger.credit_equiv), 0) AS balance, MIN(trans_date) AS since, + MAX(trans_date) AS until FROM ( SELECT debit_equiv, credit_equiv, entity_uuid, trans_date FROM posting_journal WHERE entity_uuid = ? ${excludeCautionLinks ? excludeCautionLinkStatement : ''} diff --git a/server/controllers/finance/patientInvoice.js b/server/controllers/finance/patientInvoice.js index 38b02aba94..a0c7665a63 100644 --- a/server/controllers/finance/patientInvoice.js +++ b/server/controllers/finance/patientInvoice.js @@ -204,7 +204,7 @@ function create(req, res, next) { // so, we will use their caution balance to link to the invoice for payment. Debtors.balance(invoice.debtor_uuid) .then(([pBalance]) => { - const hasCreditorBalance = hasPrepaymentSupport && pBalance && (pBalance.credit > pBalance.debit); + const hasCreditorBalance = hasPrepaymentSupport && pBalance && ((pBalance.credit - pBalance.debit) > 0.01); const preparedTransaction = createInvoice(invoice, hasCreditorBalance, prepaymentDescription); return preparedTransaction.execute(); })