From c9c5ebbd425099d8dd3e9b3907c62818c0e38491 Mon Sep 17 00:00:00 2001 From: Jonathan Cameron Date: Fri, 7 Jan 2022 11:00:31 -0800 Subject: [PATCH] Add server-side utils to display current exchange rate in reports --- .../annual_clients_report.html | 10 +++---- .../open_debtors/open_debtors.config.js | 1 - .../generate/stock_entry/stock_entry.html | 12 ++++---- .../generate/stock_exit/stock_exit.html | 12 ++++---- .../generate/stock_value/stock_value.html | 13 ++++---- server/controllers/finance/exchange.js | 4 +-- .../reports/client_debts/report.handlebars | 1 - .../income_and_expense.handlebars | 2 +- .../income_and_expense.js | 12 ++++---- .../debtors/annual_clients_report.handlebars | 6 +--- .../reports/debtors/annual_clients_report.js | 13 ++++---- .../reports/debtors/openDebtors.handlebars | 13 ++------ .../finance/reports/debtors/openDebtors.js | 18 ++--------- .../financial.all_employees.handlebars | 8 ++--- .../reports/financial.all_employees.js | 8 +++-- .../reports/financial.employee.handlebars | 17 +++++------ .../finance/reports/financial.employee.js | 7 +++-- .../finance/reports/monthly_balance/index.js | 11 ++++--- .../reports/monthly_balance/report.handlebars | 2 ++ .../finance/reports/reportAccounts/index.js | 2 +- .../reports/unpaid_invoice_payments/index.js | 17 +++++++++-- .../unpaid_invoice_payments/report.handlebars | 8 ++--- server/controllers/stock/index.js | 2 +- .../stock/avg_med_costs_per_patient_report.js | 12 +++++--- .../stock/reports/stock/entry_report.js | 9 +++--- .../stock/reports/stock/exit_report.js | 9 +++--- .../stock/reports/stock/expiration_report.js | 13 +++----- .../stock/reports/stock/lost_stock_report.js | 10 ++++--- .../stock/reports/stock/stock_sheet.js | 8 ++--- .../controllers/stock/reports/stock/value.js | 11 ++++--- ...vg_med_costs_per_patient.report.handlebars | 6 ++-- .../reports/stock_entry.report.handlebars | 27 +++++------------ .../reports/stock_exit.report.handlebars | 30 ++++++------------- .../stock_expiration_report.handlebars | 12 +++----- .../stock_lost_stock.report.handlebars | 4 +-- .../reports/stock_sheet.report.handlebars | 8 +---- .../reports/stock_value.report.handlebars | 2 +- server/lib/template/helpers/math.js | 9 ++++-- server/lib/template/index.js | 3 +- .../template/partials/exchangeRate.handlebars | 21 +++++++++---- 40 files changed, 179 insertions(+), 214 deletions(-) diff --git a/client/src/modules/reports/generate/annual_clients_report/annual_clients_report.html b/client/src/modules/reports/generate/annual_clients_report/annual_clients_report.html index 15971dfe42..fdfbcde54e 100644 --- a/client/src/modules/reports/generate/annual_clients_report/annual_clients_report.html +++ b/client/src/modules/reports/generate/annual_clients_report/annual_clients_report.html @@ -30,11 +30,6 @@

REPORT.CLIENTS.TITLE

required="true"> - - - REPORT.CLIENTS.TITLE on-change-callback="ReportConfigCtrl.onIncludeCashClientsToggle(value)"> + + + REPORT.UTIL.PREVIEW diff --git a/client/src/modules/reports/generate/open_debtors/open_debtors.config.js b/client/src/modules/reports/generate/open_debtors/open_debtors.config.js index f3fbd6cc36..4ed4b8e164 100644 --- a/client/src/modules/reports/generate/open_debtors/open_debtors.config.js +++ b/client/src/modules/reports/generate/open_debtors/open_debtors.config.js @@ -54,7 +54,6 @@ function OpenDebtorsConfigController( vm.onSelectCurrency = currency => { vm.reportDetails.currencyId = currency.id; - vm.reportDetails.currencySymbol = currency.symbol; }; vm.requestSaveAs = function requestSaveAs() { diff --git a/client/src/modules/reports/generate/stock_entry/stock_entry.html b/client/src/modules/reports/generate/stock_entry/stock_entry.html index a0ac53e0a4..90fadff145 100644 --- a/client/src/modules/reports/generate/stock_entry/stock_entry.html +++ b/client/src/modules/reports/generate/stock_entry/stock_entry.html @@ -39,11 +39,6 @@

REPORT.STOCK.ENTRY_REPORT

required="true"> - - -
@@ -76,8 +71,6 @@

REPORT.STOCK.ENTRY_REPORT

STOCK.AT_LEAST_ONE_CHECKED
-
-
+ + + REPORT.UTIL.PREVIEW diff --git a/client/src/modules/reports/generate/stock_exit/stock_exit.html b/client/src/modules/reports/generate/stock_exit/stock_exit.html index e67d108a24..8fea0838e4 100644 --- a/client/src/modules/reports/generate/stock_exit/stock_exit.html +++ b/client/src/modules/reports/generate/stock_exit/stock_exit.html @@ -39,11 +39,6 @@

REPORT.STOCK.EXIT_REPORT

required="true"> - - -
@@ -90,8 +85,6 @@

REPORT.STOCK.EXIT_REPORT

STOCK.AT_LEAST_ONE_CHECKED
-
-
+ + + REPORT.UTIL.PREVIEW diff --git a/client/src/modules/reports/generate/stock_value/stock_value.html b/client/src/modules/reports/generate/stock_value/stock_value.html index 077afa2254..754293155a 100644 --- a/client/src/modules/reports/generate/stock_value/stock_value.html +++ b/client/src/modules/reports/generate/stock_value/stock_value.html @@ -40,13 +40,6 @@

REPORT.STOCK_VALUE.TITLE

on-change="ReportConfigCtrl.onDateChange(date)"> - - - - -
+ + + + REPORT.UTIL.PREVIEW diff --git a/server/controllers/finance/exchange.js b/server/controllers/finance/exchange.js index 8253d0373f..a74e29e8e9 100644 --- a/server/controllers/finance/exchange.js +++ b/server/controllers/finance/exchange.js @@ -6,8 +6,8 @@ */ const db = require('../../lib/db'); -const NotFound = require('../../lib/errors/NotFound'); const util = require('../../lib/util'); +const NotFound = require('../../lib/errors/NotFound'); exports.getExchangeRate = getExchangeRate; exports.formatExchangeRateForDisplay = formatExchangeRateForDisplay; @@ -22,7 +22,7 @@ function getExchangeRate(enterpriseId, currencyId, date) { .then(rows => rows[0]); } -// gets a positive number for the exchange rate display. +// gets a non-fractional number for the exchange rate display. function formatExchangeRateForDisplay(value) { return (value < 1) ? util.roundDecimal(1 / value, 2) : value; } diff --git a/server/controllers/finance/reports/client_debts/report.handlebars b/server/controllers/finance/reports/client_debts/report.handlebars index 8b5658c526..e5ac73f9af 100644 --- a/server/controllers/finance/reports/client_debts/report.handlebars +++ b/server/controllers/finance/reports/client_debts/report.handlebars @@ -24,7 +24,6 @@ - {{translate 'REPORT.CLIENT_SUMMARY.NOT_EMPLOYEES_DEBTS' }} ({{patients.length}}) diff --git a/server/controllers/finance/reports/cost_center_step_down/income_and_expense.handlebars b/server/controllers/finance/reports/cost_center_step_down/income_and_expense.handlebars index 29a7a81c63..b00f2c06cc 100644 --- a/server/controllers/finance/reports/cost_center_step_down/income_and_expense.handlebars +++ b/server/controllers/finance/reports/cost_center_step_down/income_and_expense.handlebars @@ -20,7 +20,7 @@ {{date dateTo "MMMM YYYY"}} - {{> exchangeRate lastRateUsed=lastRateUsed firstCurrency=firstCurrency secondCurrency=secondCurrency dateTo=dateTo}} + {{> exchangeRate rate=lastRateUsed currencyId=currencyId date=dateTo}} diff --git a/server/controllers/finance/reports/cost_center_step_down/income_and_expense.js b/server/controllers/finance/reports/cost_center_step_down/income_and_expense.js index f696bf414f..3c16f3f0ec 100644 --- a/server/controllers/finance/reports/cost_center_step_down/income_and_expense.js +++ b/server/controllers/finance/reports/cost_center_step_down/income_and_expense.js @@ -40,9 +40,9 @@ async function buildAccountsReport(params, session) { const queryTotals = ` SELECT - z.label, - SUM(z.income * IFNULL(GetExchangeRate(?, ?, ?), 1)) income, - SUM(z.expense * IFNULL(GetExchangeRate(?, ?, ?), 1)) expense, + z.label, + SUM(z.income * IFNULL(GetExchangeRate(?, ?, ?), 1)) income, + SUM(z.expense * IFNULL(GetExchangeRate(?, ?, ?), 1)) expense, SUM((z.income - expense) * IFNULL(GetExchangeRate(?, ?, ?), 1)) AS balance FROM ( @@ -51,9 +51,9 @@ async function buildAccountsReport(params, session) { SUM(IF(a.type_id = 4, gl.credit_equiv - gl.debit_equiv, 0)) AS income, SUM(IF(a.type_id = 5, gl.debit_equiv - gl.credit_equiv, 0)) AS expense, gl.cost_center_id AS ccId - FROM general_ledger gl + FROM general_ledger gl JOIN cost_center cc ON cc.id = gl.cost_center_id - JOIN account a ON a.id = gl.account_id + JOIN account a ON a.id = gl.account_id WHERE gl.period_id >= ? AND gl.period_id <= ? GROUP BY cc.id )z @@ -78,7 +78,7 @@ async function buildAccountsReport(params, session) { const totals = await db.exec(queryTotals, parameters); const context = { - currencyId : options.currency_id, + currencyId : Number(options.currency_id), dateFrom, dateTo, data, diff --git a/server/controllers/finance/reports/debtors/annual_clients_report.handlebars b/server/controllers/finance/reports/debtors/annual_clients_report.handlebars index 27613fc4ff..41bd15ebd3 100644 --- a/server/controllers/finance/reports/debtors/annual_clients_report.handlebars +++ b/server/controllers/finance/reports/debtors/annual_clients_report.handlebars @@ -12,11 +12,7 @@ {{ fiscalYear.label }} - {{#unless isEnterpriseCurrency}} -
-

{{translate "REPORT.REPORT_ACCOUNTS.WARN_CURRENCY"}}

-
- {{/unless}} + {{> exchangeRate rate=exchangeRate currencyId=currencyId}}
diff --git a/server/controllers/finance/reports/debtors/annual_clients_report.js b/server/controllers/finance/reports/debtors/annual_clients_report.js index 6093303eee..d8258f0d97 100644 --- a/server/controllers/finance/reports/debtors/annual_clients_report.js +++ b/server/controllers/finance/reports/debtors/annual_clients_report.js @@ -11,20 +11,19 @@ const TEMPLATE = './server/controllers/finance/reports/debtors/annual_clients_re exports.annualClientsReport = annualClientsReport; exports.reporting = reporting; -async function setupAnnualClientsReport(options, enterpriseCurrencyId) { +async function setupAnnualClientsReport(options, enterprise) { const { - fiscalId, currencyId, hideLockedClients, includeCashClients, + fiscalId, hideLockedClients, includeCashClients, } = options; + const currencyId = Number(options.currencyId); // convert to an integer const shouldHideLockedClients = Number(hideLockedClients); const shouldIncludeCashClients = Number(includeCashClients); - const isEnterpriseCurrency = parseInt(currencyId, 10) === enterpriseCurrencyId; - const [fiscalYear, exchange] = await Promise.all([ Fiscal.lookupFiscalYear(fiscalId), - Exchange.getExchangeRate(enterpriseCurrencyId, currencyId, new Date()), + Exchange.getExchangeRate(enterprise.id, currencyId, new Date()), ]); const rate = exchange.rate || 1; @@ -35,7 +34,7 @@ async function setupAnnualClientsReport(options, enterpriseCurrencyId) { ]); return { - rows, footer, fiscalYear, rate, isEnterpriseCurrency, + rows, footer, fiscalYear, exchangeRate : rate, currencyId, }; } @@ -54,7 +53,7 @@ async function annualClientsReport(req, res, next) { try { const reportManager = new ReportManager(TEMPLATE, req.session, options); - const data = await setupAnnualClientsReport(req.query, req.session.enterprise.currency_id); + const data = await setupAnnualClientsReport(req.query, req.session.enterprise); const { headers, report } = await reportManager.render(data); res.set(headers).send(report); } catch (e) { diff --git a/server/controllers/finance/reports/debtors/openDebtors.handlebars b/server/controllers/finance/reports/debtors/openDebtors.handlebars index a644fc384c..a0b2ee01ed 100644 --- a/server/controllers/finance/reports/debtors/openDebtors.handlebars +++ b/server/controllers/finance/reports/debtors/openDebtors.handlebars @@ -12,17 +12,8 @@ {{#if details.limitDate}}{{date details.reportDateLimit}} - {{/if}} {{date this.timestamp }} - {{#unless enterpriseCurrency}} - {{#if showReciprocalRate}} -
- {{ translate 'FORM.LABELS.EXCHANGE_RATE' }}: {{currency reciprocalRate enterpriseCurrencyId}} {{translate 'FORM.INFO.PER'}} {{ currencySymbol }} -
- {{else}} -
- {{ translate 'FORM.LABELS.EXCHANGE_RATE' }}: {{currency rate currencyId}} {{translate 'FORM.INFO.PER'}} {{ enterpriseCurrencySymbol }} -
- {{/if}} - {{/unless}} + + {{> exchangeRate rate=exchangeRate currencyId=currencyId}}
diff --git a/server/controllers/finance/reports/debtors/openDebtors.js b/server/controllers/finance/reports/debtors/openDebtors.js index 5e81bf6d8e..944c9d8f4e 100644 --- a/server/controllers/finance/reports/debtors/openDebtors.js +++ b/server/controllers/finance/reports/debtors/openDebtors.js @@ -33,6 +33,7 @@ const _ = require('lodash'); const ReportManager = require('../../../../lib/ReportManager'); const db = require('../../../../lib/db'); +const Exchange = require('../../exchange'); // path to the template to render const TEMPLATE = './server/controllers/finance/reports/debtors/openDebtors.handlebars'; @@ -61,7 +62,6 @@ function build(req, res, next) { qs.enterpriseCurrencySymbol = metadata.enterprise.currencySymbol; let report; - try { report = new ReportManager(TEMPLATE, metadata, qs); } catch (e) { @@ -92,16 +92,10 @@ function requestOpenDebtors(params) { const reportDateLimit = new Date(params.reportDateLimit); const currencyId = parseInt(params.currencyId, 10); const enterpriseId = parseInt(params.enterpriseId, 10); - const enterpriseCurrencyId = parseInt(params.enterpriseCurrencyId, 10); - const enterpriseCurrency = params.currencyId === params.enterpriseCurrencyId; // TODO(@jniles) respect the ordering in the open debtors field. const ordering = parseOrdering(params.order); - const exchangeRateQuery = ` - SELECT GetExchangeRate(?, ?, NOW()) AS rate - `; - const unverifiedSource = ` (SELECT entity_uuid, reference_uuid, trans_date, credit_equiv, debit_equiv from general_ledger UNION ALL @@ -130,17 +124,11 @@ function requestOpenDebtors(params) { reportDateLimit, }, currencyId, - currencySymbol : params.currencySymbol, - enterpriseCurrencyId, - enterpriseCurrencySymbol : params.enterpriseCurrencySymbol, - enterpriseCurrency, }; - return db.one(exchangeRateQuery, [enterpriseId, currencyId]) + return Exchange.getExchangeRate(enterpriseId, currencyId, new Date()) .then((exRate) => { - debtorReport.rate = exRate.rate ? exRate.rate : 1; - debtorReport.reciprocalRate = 1.0 / debtorReport.rate; - debtorReport.showReciprocalRate = debtorReport.rate < 1.0; + debtorReport.exchangeRate = exRate.rate ? exRate.rate : 1; return db.exec(debtorQuery); }) .then((debtorsDebts) => { diff --git a/server/controllers/finance/reports/financial.all_employees.handlebars b/server/controllers/finance/reports/financial.all_employees.handlebars index d4278dc61b..78183ab40f 100644 --- a/server/controllers/finance/reports/financial.all_employees.handlebars +++ b/server/controllers/finance/reports/financial.all_employees.handlebars @@ -10,12 +10,6 @@ - {{#unless isEnterpriseCurrency}} -
-

{{translate "REPORT.REPORT_ACCOUNTS.WARN_CURRENCY"}}

-
- {{/unless}} - {{#if limitTimeInterval}}
@@ -24,6 +18,8 @@
{{/if}} + {{> exchangeRate rate=exchangeRate currencyId=currencyId date=dateExchangeRate}} +
{{#if summary}} diff --git a/server/controllers/finance/reports/financial.all_employees.js b/server/controllers/finance/reports/financial.all_employees.js index 90a3557138..15a803bfee 100644 --- a/server/controllers/finance/reports/financial.all_employees.js +++ b/server/controllers/finance/reports/financial.all_employees.js @@ -157,18 +157,20 @@ async function build(req, res, next) { `; } + const currencyId = Number(options.currency_id); + const [financialData, exchange] = await Promise.all([ db.exec(sql), Exchange.getExchangeRate( req.session.enterprise.id, - Number(options.currency_id), + currencyId, dateExchangeRate, ), ]); + data.currencyId = currencyId; data.exchangeRate = exchange.rate || 1; - data.currencyId = options.currency_id; - data.isEnterpriseCurrency = req.session.enterprise.currency_id === Number(options.currency_id); + data.dateExchangeRate = dateExchangeRate; let sumDebit = 0; let sumCredit = 0; diff --git a/server/controllers/finance/reports/financial.employee.handlebars b/server/controllers/finance/reports/financial.employee.handlebars index 812a8f38e0..a521e63f43 100644 --- a/server/controllers/finance/reports/financial.employee.handlebars +++ b/server/controllers/finance/reports/financial.employee.handlebars @@ -9,20 +9,17 @@ {{translate "REPORT.EMPLOYEE_STANDING.REPORT"}} -

{{translate "FORM.LABELS.EMPLOYEE_NAME"}} : {{ employee.display_name }}

-

{{translate "FORM.LABELS.REFERENCE"}} : {{ employee.reference }}

-
{{translate "FORM.LABELS.REGISTRATION_NUMBER"}} : {{ employee.code }}
-

+ +

{{translate "FORM.LABELS.EMPLOYEE_NAME"}} : {{ employee.display_name }}

+

{{translate "FORM.LABELS.REFERENCE"}} : {{ employee.reference }}

+
{{translate "FORM.LABELS.REGISTRATION_NUMBER"}} : {{ employee.code }}
+

{{translate balanceCreditorText}} = {{debcred (multiply ./creditorAggregates.balance exchangeRate) currencyId}}

- {{#unless isEnterpriseCurrency}} -
-

{{translate "REPORT.REPORT_ACCOUNTS.WARN_CURRENCY"}}

-
- {{/unless}} + {{> exchangeRate rate=exchangeRate currencyId=currencyId date=dateExchangeRate}} {{#if includeMedicalCare }}
@@ -30,8 +27,8 @@ {{translate "TABLE.COLUMNS.TOTAL_MEDICAL_CARE_EMPLOYEE"}} = {{debcred (multiply ./debtorAggregates.balance exchangeRate) currencyId}}
- {{/if}}
+ {{/if}}
{{#if includeMedicalCare }} diff --git a/server/controllers/finance/reports/financial.employee.js b/server/controllers/finance/reports/financial.employee.js index 28a0260358..f8fd06dec6 100644 --- a/server/controllers/finance/reports/financial.employee.js +++ b/server/controllers/finance/reports/financial.employee.js @@ -56,8 +56,7 @@ async function build(req, res, next) { const data = {}; - data.currencyId = options.currency_id; - data.isEnterpriseCurrency = req.session.enterprise.currency_id === Number(options.currency_id); + const currencyId = Number(options.currency_id); const sql = ` SELECT BUID(p.debtor_uuid) as debtor_uuid @@ -70,12 +69,14 @@ async function build(req, res, next) { db.one(sql, db.bid(options.employee_uuid)), Exchange.getExchangeRate( req.session.enterprise.id, - Number(options.currency_id), + currencyId, dateExchangeRate, ), ]); + data.currencyId = currencyId; data.exchangeRate = exchange.rate || 1; + data.dateExchangeRate = dateExchangeRate; // get debtor/creditor information const [creditorOperations, debtorOperations] = await Promise.all([ diff --git a/server/controllers/finance/reports/monthly_balance/index.js b/server/controllers/finance/reports/monthly_balance/index.js index e74b29d9ce..24905d4e51 100644 --- a/server/controllers/finance/reports/monthly_balance/index.js +++ b/server/controllers/finance/reports/monthly_balance/index.js @@ -3,10 +3,10 @@ const Tree = require('@ima-worldhealth/tree'); const db = require('../../../../lib/db'); const util = require('../../../../lib/util'); -const ReportManager = require('../../../../lib/ReportManager'); +const fiscal = require('../../fiscal'); const Exchange = require('../../../finance/exchange'); -const fiscal = require('../../fiscal'); +const ReportManager = require('../../../../lib/ReportManager'); const TEMPLATE = './server/controllers/finance/reports/monthly_balance/report.handlebars'; @@ -24,12 +24,14 @@ const DECIMAL_PRECISION = 2; // ex: 12.4567 => 12.46 */ async function reporting(opts, session) { const params = opts; + const { enterprise } = session; + const currencyId = Number(opts.currencyId); params.allAccount = parseInt(params.allAccount, 10); const accountNumber = params.allAccount ? `` : params.accountNumber; const accountLabel = params.allAccount ? `` : params.accountLabel; - const enterpriseId = session.enterprise.id; + const enterpriseId = enterprise.id; const exchangeRate = await Exchange.getExchangeRate(enterpriseId, params.currencyId, new Date()); const rate = exchangeRate.rate || 1; @@ -112,10 +114,11 @@ async function reporting(opts, session) { dateFrom : range.dateFrom, dateTo : range.dateTo, periodLabel : params.periodLabel, - currencyId : params.currencyId, accountLabel, accountNumber, allAccount : params.allAccount, + currencyId, + exchangeRate : rate, }; formatData(context.exploitation, context.totalExploitation, DECIMAL_PRECISION); diff --git a/server/controllers/finance/reports/monthly_balance/report.handlebars b/server/controllers/finance/reports/monthly_balance/report.handlebars index 08c01b07f0..b50a9088e7 100644 --- a/server/controllers/finance/reports/monthly_balance/report.handlebars +++ b/server/controllers/finance/reports/monthly_balance/report.handlebars @@ -13,6 +13,8 @@

{{this.accountNumber}}: {{this.accountLabel}}

{{/if}} + {{> exchangeRate rate=exchangeRate currencyId=currencyId}} +
diff --git a/server/controllers/finance/reports/reportAccounts/index.js b/server/controllers/finance/reports/reportAccounts/index.js index faeeb5e1b6..70d661a85e 100644 --- a/server/controllers/finance/reports/reportAccounts/index.js +++ b/server/controllers/finance/reports/reportAccounts/index.js @@ -27,7 +27,7 @@ function document(req, res, next) { const params = req.query; params.user = req.session.user; params.enterprise_id = req.session.enterprise.id; - params.isEnterpriseCurrency = (req.session.enterprise.currency_id === Number(params.currency_id)); + params.isEnterpriseCurrency = req.session.enterprise.currency_id === Number(params.currency_id); params.includeUnpostedValues = params.includeUnpostedValues ? Number(params.includeUnpostedValues) : 0; try { diff --git a/server/controllers/finance/reports/unpaid_invoice_payments/index.js b/server/controllers/finance/reports/unpaid_invoice_payments/index.js index 7288cdf1bf..92a50c647b 100644 --- a/server/controllers/finance/reports/unpaid_invoice_payments/index.js +++ b/server/controllers/finance/reports/unpaid_invoice_payments/index.js @@ -1,7 +1,10 @@ const _ = require('lodash'); -const ReportManager = require('../../../../lib/ReportManager'); const db = require('../../../../lib/db'); const util = require('../../../../lib/util'); +const Exchange = require('../../exchange'); + +const ReportManager = require('../../../../lib/ReportManager'); + // path to the template to render const TEMPLATE = './server/controllers/finance/reports/unpaid_invoice_payments/report.handlebars'; @@ -14,12 +17,14 @@ exports.document = build; exports.reporting = reporting; async function build(req, res, next) { + const { dateTo } = req.query; + const { enterprise } = req.session; + const currencyId = Number(req.query.currencyId); + const qs = _.extend(req.query, DEFAULT_OPTIONS); const metadata = _.clone(req.session); - qs.isEnterpriseCurrency = !!(parseInt(qs.currencyId, 10) === metadata.enterprise.currency_id); - let report; let results; @@ -50,6 +55,12 @@ async function build(req, res, next) { const data = _.extend({}, qs, results); + const exchangeRate = await Exchange.getExchangeRate(enterprise.id, currencyId, new Date(dateTo)); + + data.dateTo = dateTo; + data.currencyId = currencyId; + data.exchangeRate = exchangeRate.rate || 1; + const compiled = await report.render(data); res.set(compiled.headers).send(compiled.report); } diff --git a/server/controllers/finance/reports/unpaid_invoice_payments/report.handlebars b/server/controllers/finance/reports/unpaid_invoice_payments/report.handlebars index cf88e41924..5a2a3c0355 100644 --- a/server/controllers/finance/reports/unpaid_invoice_payments/report.handlebars +++ b/server/controllers/finance/reports/unpaid_invoice_payments/report.handlebars @@ -21,11 +21,7 @@ {{date dateFrom "DD/MM/YYYY"}} - {{date dateTo "DD/MM/YYYY"}} - {{#unless isEnterpriseCurrency}} -
-

{{translate "REPORT.REPORT_ACCOUNTS.WARN_CURRENCY"}}

-
- {{/unless}} + {{> exchangeRate rate=exchangeRate currencyId=currencyId date=dateTo}}
{{translate 'FORM.LABELS.ACCOUNT_NUMBER'}}
@@ -64,7 +60,7 @@ {{/each}} {{/unless}} - + {{/unless}} diff --git a/server/controllers/stock/index.js b/server/controllers/stock/index.js index a793721b74..041abe4e9f 100644 --- a/server/controllers/stock/index.js +++ b/server/controllers/stock/index.js @@ -358,7 +358,7 @@ async function createInventoryAdjustment(req, res, next) { if (difference < 0) { // we have to do a stock exit movement - // we must substract the |difference| to the current quantity + // we must subtract the |difference| to the current quantity countNeedDecrease++; adjustmentMovement.is_exit = 1; } else { diff --git a/server/controllers/stock/reports/stock/avg_med_costs_per_patient_report.js b/server/controllers/stock/reports/stock/avg_med_costs_per_patient_report.js index 10a72f1abb..783000adfa 100644 --- a/server/controllers/stock/reports/stock/avg_med_costs_per_patient_report.js +++ b/server/controllers/stock/reports/stock/avg_med_costs_per_patient_report.js @@ -15,7 +15,8 @@ const Exchange = require('../../../finance/exchange'); * GET /reports/stock/avg_med_costs_per_patient' */ async function stockAvgMedCostsPerPatientReport(req, res, next) { - let report; + const { enterprise } = req.session; + const options = req.query; const { dateFrom, dateTo, @@ -28,10 +29,12 @@ async function stockAvgMedCostsPerPatientReport(req, res, next) { title : 'REPORT.AVG_MED_COST_PER_PATIENT.TITLE', }); + let report; + const data = {}; - const enterpriseId = req.session.enterprise.id; - const exchangeRate = await Exchange.getExchangeRate(enterpriseId, options.currencyId, new Date()); + const currencyId = Number(options.currencyId); + const exchangeRate = await Exchange.getExchangeRate(enterprise.id, currencyId, new Date()); const rate = exchangeRate.rate || 1; try { @@ -97,7 +100,8 @@ async function stockAvgMedCostsPerPatientReport(req, res, next) { return db.exec(sql, params) .then((results) => { - data.currencyId = Number(options.currencyId); + data.currencyId = currencyId; + data.exchangeRate = rate; data.dateFrom = dateFrom; data.dateTo = dateTo; data.depotName = depotName; diff --git a/server/controllers/stock/reports/stock/entry_report.js b/server/controllers/stock/reports/stock/entry_report.js index 3352760df8..04cd0d6fdd 100644 --- a/server/controllers/stock/reports/stock/entry_report.js +++ b/server/controllers/stock/reports/stock/entry_report.js @@ -2,6 +2,8 @@ const { _, db, util, ReportManager, STOCK_ENTRY_REPORT_TEMPLATE, } = require('../common'); +const Exchange = require('../../../finance/exchange'); + const StockEntryFromPurchase = require('./entry/entryFromPurchase'); const StockEntryFromIntegration = require('./entry/entryFromIntegration'); const StockEntryFromDonation = require('./entry/entryFromDonation'); @@ -29,13 +31,12 @@ async function stockEntryReport(req, res, next) { try { const report = new ReportManager(STOCK_ENTRY_REPORT_TEMPLATE, req.session, optionReport); - const [depot, [{ rate }]] = await Promise.all([ + const [depot, exchange] = await Promise.all([ fetchDepotDetails(params.depotUuid), - db.exec('SELECT GetExchangeRate(?, ?, NOW()) as rate;', [req.session.enterprise.id, params.currencyId]), + Exchange.getExchangeRate(req.session.enterprise.id, params.currencyId, new Date()), ]); - params.isEnterpriseCurrency = params.currencyId === req.session.enterprise.currency_id; - params.exchangeRate = params.isEnterpriseCurrency ? 1 : rate; + params.exchangeRate = exchange.rate || 1; params.depotName = depot.text; const collection = await collect(params); diff --git a/server/controllers/stock/reports/stock/exit_report.js b/server/controllers/stock/reports/stock/exit_report.js index a5ff5e0cd6..eb5c6ed827 100644 --- a/server/controllers/stock/reports/stock/exit_report.js +++ b/server/controllers/stock/reports/stock/exit_report.js @@ -2,6 +2,8 @@ const { _, db, util, ReportManager, STOCK_EXIT_REPORT_TEMPLATE, } = require('../common'); +const Exchange = require('../../../finance/exchange'); + const StockExitToPatient = require('./exit/exitToPatient'); const StockExitToService = require('./exit/exitToService'); const StockExitToDepot = require('./exit/exitToDepot'); @@ -29,13 +31,12 @@ async function stockExitReport(req, res, next) { try { const report = new ReportManager(STOCK_EXIT_REPORT_TEMPLATE, req.session, optionReport); - const [depot, [{ rate }]] = await Promise.all([ + const [depot, exchange] = await Promise.all([ fetchDepotDetails(params.depotUuid), - db.exec('SELECT GetExchangeRate(?, ?, NOW()) as rate;', [req.session.enterprise.id, params.currencyId]), + Exchange.getExchangeRate(req.session.enterprise.id, params.currencyId, new Date()), ]); - params.isEnterpriseCurrency = params.currencyId === req.session.enterprise.currency_id; - params.exchangeRate = params.isEnterpriseCurrency ? 1 : rate; + params.exchangeRate = exchange.rate || 1; params.depotName = depot.text; const collection = await collect(params); diff --git a/server/controllers/stock/reports/stock/expiration_report.js b/server/controllers/stock/reports/stock/expiration_report.js index 6f4f8f3e00..dc2b009c14 100644 --- a/server/controllers/stock/reports/stock/expiration_report.js +++ b/server/controllers/stock/reports/stock/expiration_report.js @@ -3,6 +3,7 @@ const { } = require('../common'); const stockCore = require('../../core'); +const Exchange = require('../../../finance/exchange'); function exchange(rows, exchangeRate) { @@ -44,14 +45,8 @@ async function stockExpirationReport(req, res, next) { depot = await db.one(depotSql, db.bid(options.depot_uuid)); } - const isEnterpriseCurrency = currencyId === req.session.enterprise.currency_id; - - const [{ rate }] = await db.exec( - 'SELECT GetExchangeRate(?, ?, NOW()) AS rate;', - [req.session.enterprise.id, currencyId], - ); - - const exchangeRate = isEnterpriseCurrency ? 1 : rate; + const exRate = await Exchange.getExchangeRate(req.session.enterprise.id, currencyId, new Date()); + const exchangeRate = exRate.rate || 1; // clean off the label if it exists so it doesn't mess up the PDF export delete options.label; @@ -113,8 +108,8 @@ async function stockExpirationReport(req, res, next) { const reportResult = await report.render({ result : values, - isEnterpriseCurrency, currencyId, + exchangeRate, depot, totals, today, diff --git a/server/controllers/stock/reports/stock/lost_stock_report.js b/server/controllers/stock/reports/stock/lost_stock_report.js index 39cb8d04fa..594694f178 100644 --- a/server/controllers/stock/reports/stock/lost_stock_report.js +++ b/server/controllers/stock/reports/stock/lost_stock_report.js @@ -14,12 +14,13 @@ const Exchange = require('../../../finance/exchange'); * GET /reports/stock/lost */ async function lostStockReport(req, res, next) { - const params = req.query; - const { depotRole } = params; + const { depotRole } = req.query; + const { enterprise } = req.session; + const currencyId = Number(params.currencyId); - const enterpriseId = req.session.enterprise.id; - const exchangeRate = await Exchange.getExchangeRate(enterpriseId, params.currencyId, new Date()); + const enterpriseId = enterprise.id; + const exchangeRate = await Exchange.getExchangeRate(enterpriseId, currencyId, new Date()); const rate = exchangeRate.rate || 1; // set up the report with report manager @@ -31,6 +32,7 @@ async function lostStockReport(req, res, next) { const data = {}; const [key] = rows; data.currencyId = Number(params.currencyId); + data.exchangeRate = rate; data.dateTo = params.dateTo; data.dateFrom = params.dateFrom; data.isDestDepot = null; diff --git a/server/controllers/stock/reports/stock/stock_sheet.js b/server/controllers/stock/reports/stock/stock_sheet.js index 419fec7d90..5f295b9517 100644 --- a/server/controllers/stock/reports/stock/stock_sheet.js +++ b/server/controllers/stock/reports/stock/stock_sheet.js @@ -4,6 +4,7 @@ const { const PeriodService = require('../../../../lib/period'); const Inventory = require('../../../inventory'); +const Exchange = require('../../../finance/exchange'); /** * @method stockSheetReport @@ -46,14 +47,11 @@ async function stockSheetReport(req, res, next) { const report = new ReportManager(STOCK_SHEET_REPORT_TEMPLATE, req.session, optionReport); - const [{ rate }] = await db.exec( - 'SELECT GetExchangeRate(?, ?, NOW()) as rate;', [req.session.enterprise.id, options.currencyId], - ); + const exchangeRate = await Exchange.getExchangeRate(req.session.enterprise.id, options.currencyId, new Date()); const data = {}; - data.isEnterpriseCurrency = options.currencyId === req.session.enterprise.currency_id; - data.exchangeRate = data.isEnterpriseCurrency ? 1 : rate; + data.exchangeRate = exchangeRate.rate || 1; data.currencyId = options.currencyId; options.exchangeRate = data.exchangeRate; diff --git a/server/controllers/stock/reports/stock/value.js b/server/controllers/stock/reports/stock/value.js index 66f4267aec..c8b71a8b47 100644 --- a/server/controllers/stock/reports/stock/value.js +++ b/server/controllers/stock/reports/stock/value.js @@ -1,9 +1,9 @@ -const Exchange = require('../../../finance/exchange'); - const { _, db, ReportManager, STOCK_VALUE_REPORT_TEMPLATE, } = require('../common'); +const Exchange = require('../../../finance/exchange'); + /** * @method stockInventoryReport * @@ -32,6 +32,7 @@ async function reporting(_options, session) { const options = (typeof (_options.params) === 'string') ? JSON.parse(_options.params) : _options.params; data.dateTo = options.dateTo; data.depot = await db.one('SELECT * FROM depot WHERE uuid=?', [db.bid(options.depot_uuid)]); + const currencyId = Number(options.currency_id); // Get inventories movemented const sqlGetInventories = ` @@ -83,7 +84,7 @@ async function reporting(_options, session) { let stockTotalValue = 0; let stockTotalSaleValue = 0; - const exchangeRate = await Exchange.getExchangeRate(enterpriseId, options.currency_id, new Date()); + const exchangeRate = await Exchange.getExchangeRate(enterpriseId, currencyId, new Date()); const rate = exchangeRate.rate || 1; // calculate quantity in stock since wac is globally calculated @@ -122,9 +123,11 @@ async function reporting(_options, session) { data.stockTotalSaleValue = stockTotalSaleValue; data.emptyResult = data.stockValues.length === 0; - data.currency_id = options.currency_id; data.exclude_zero_value = options.exclude_zero_value; + data.currencyId = currencyId; + data.exchangeRate = rate; + return report.render(data); } diff --git a/server/controllers/stock/reports/stock_avg_med_costs_per_patient.report.handlebars b/server/controllers/stock/reports/stock_avg_med_costs_per_patient.report.handlebars index e39f3ea773..54eb6400b1 100644 --- a/server/controllers/stock/reports/stock_avg_med_costs_per_patient.report.handlebars +++ b/server/controllers/stock/reports/stock_avg_med_costs_per_patient.report.handlebars @@ -11,19 +11,21 @@ {{translate 'REPORT.AVG_MED_COST_PER_PATIENT.TITLE'}} {{#if depotOrService}} -

+

{{#if depotName}} {{ depotName }}{{#if depotAndService}}, {{/if}} {{/if}} {{#if serviceName}} {{translate 'STOCK.SERVICE'}}: {{ serviceName }} {{/if}} -

+ {{/if}}

{{date dateFrom}} - {{date dateTo}}

+ {{> exchangeRate rate=exchangeRate currencyId=currencyId}} +
{{debcred (look row service) ../../currencyId}}{{debcred row.Total ../currencyId}}
diff --git a/server/controllers/stock/reports/stock_entry.report.handlebars b/server/controllers/stock/reports/stock_entry.report.handlebars index 9438d2da43..919a4fff11 100644 --- a/server/controllers/stock/reports/stock_entry.report.handlebars +++ b/server/controllers/stock/reports/stock_entry.report.handlebars @@ -12,21 +12,15 @@ {{translate 'REPORT.STOCK.ENTRY_REPORT'}} -

+

{{ depotName }} -

+ -

+

{{date dateFrom}} - {{date dateTo}}

- {{#unless isEnterpriseCurrency}} -
-

{{translate "REPORT.REPORT_ACCOUNTS.WARN_CURRENCY"}}

-
- {{/unless}} - -
+ {{> exchangeRate rate=exchangeRate currencyId=currencyId}} {{#if includePurchaseEntry}} @@ -73,10 +67,9 @@
{{/if}}
{{translate 'STOCK.NO_DATA'}}
+
{{/if}} -
- {{#if includeIntegrationEntry}} @@ -122,10 +115,9 @@ {{/if}}
{{translate 'STOCK.NO_DATA'}}
+
{{/if}} -
- {{#if includeDonationEntry}} @@ -167,10 +159,9 @@ {{/if}}
{{translate 'STOCK.NO_DATA'}}
+
{{/if}} -
- {{#if includeTransferEntry}} @@ -212,11 +203,9 @@ {{/if}}
{{translate 'STOCK.NO_DATA'}}
+
{{/if}} -
- -

*{{translate 'STOCK.STOCK_COST_DESCRIPTION'}}

*{{translate 'STOCK.STOCK_UNIT_COST_ROUNDED'}}

diff --git a/server/controllers/stock/reports/stock_exit.report.handlebars b/server/controllers/stock/reports/stock_exit.report.handlebars index 8d8fb8a401..9daac4a237 100644 --- a/server/controllers/stock/reports/stock_exit.report.handlebars +++ b/server/controllers/stock/reports/stock_exit.report.handlebars @@ -12,21 +12,15 @@ {{translate 'REPORT.STOCK.EXIT_REPORT'}} -

+

{{ depotName }} -

+

{{date dateFrom}} - {{date dateTo}}

- {{#unless isEnterpriseCurrency}} -
-

{{translate "REPORT.REPORT_ACCOUNTS.WARN_CURRENCY"}}

-
- {{/unless}} - -
+ {{> exchangeRate rate=exchangeRate currencyId=currencyId}} {{#if includePatientExit}} @@ -73,10 +67,9 @@ {{translate 'STOCK.NO_DATA'}} {{/if}} +
{{/if}} -
- {{#if includeServiceExit}} @@ -118,10 +111,9 @@ {{/if}}
{{translate 'STOCK.NO_DATA'}}
+
{{/if}} -
- {{#if includeGroupedServiceExit}} @@ -172,10 +164,9 @@ {{/if}}
{{translate 'STOCK.NO_DATA'}}
+
{{/if}} -
- {{#if includeDepotExit}} @@ -217,10 +208,9 @@ {{/if}}
{{translate 'STOCK.NO_DATA'}}
+
{{/if}} -
- {{#if includeLossExit}} @@ -262,10 +252,9 @@ {{/if}}
{{translate 'STOCK.NO_DATA'}}
+
{{/if}} -
- {{#if includeAggregateConsumption}} @@ -307,10 +296,9 @@ {{/if}}
{{translate 'STOCK.NO_DATA'}}
+
{{/if}} -
-

*{{translate 'STOCK.STOCK_COST_DESCRIPTION'}}

*{{translate 'STOCK.STOCK_UNIT_COST_ROUNDED'}}

diff --git a/server/controllers/stock/reports/stock_expiration_report.handlebars b/server/controllers/stock/reports/stock_expiration_report.handlebars index 833de399ba..54348dacc7 100644 --- a/server/controllers/stock/reports/stock_expiration_report.handlebars +++ b/server/controllers/stock/reports/stock_expiration_report.handlebars @@ -16,7 +16,7 @@ margin-bottom: 5px; } .card .value { - font-size: 3em; + font-size: 2em; } @@ -29,15 +29,11 @@

{{depot.text}}

{{/if}} -

+

{{date today}} -

+ - {{#unless isEnterpriseCurrency}} -
-

{{translate "REPORT.REPORT_ACCOUNTS.WARN_CURRENCY"}}

-
- {{/unless}} + {{> exchangeRate rate=exchangeRate currencyId=currencyId}}
diff --git a/server/controllers/stock/reports/stock_lost_stock.report.handlebars b/server/controllers/stock/reports/stock_lost_stock.report.handlebars index 08ceb5d5f4..e455546195 100644 --- a/server/controllers/stock/reports/stock_lost_stock.report.handlebars +++ b/server/controllers/stock/reports/stock_lost_stock.report.handlebars @@ -13,12 +13,12 @@ {{date dateFrom}} - {{date dateTo}} -

+

{{#if isDestDepot}}{{translate 'REPORT.LOST_STOCK_REPORT.DESTINATION_DEPOT'}}: {{depotName}}{{/if}} {{#if isSrcDepot}}{{translate 'REPORT.LOST_STOCK_REPORT.SOURCE_DEPOT'}}: {{depotName}}{{/if}}

-
+ {{> exchangeRate rate=exchangeRate currencyId=currencyId}} diff --git a/server/controllers/stock/reports/stock_sheet.report.handlebars b/server/controllers/stock/reports/stock_sheet.report.handlebars index 5e466cff4d..5901685f7b 100644 --- a/server/controllers/stock/reports/stock_sheet.report.handlebars +++ b/server/controllers/stock/reports/stock_sheet.report.handlebars @@ -23,13 +23,7 @@ {{#if dateTo}}{{date dateTo}}{{/if}} -
- - {{#unless isEnterpriseCurrency}} -
-

{{translate "REPORT.REPORT_ACCOUNTS.WARN_CURRENCY"}}

-
- {{/unless}} + {{> exchangeRate rate=exchangeRate currencyId=currencyId}}
diff --git a/server/controllers/stock/reports/stock_value.report.handlebars b/server/controllers/stock/reports/stock_value.report.handlebars index 072add49e8..1af976bbcc 100644 --- a/server/controllers/stock/reports/stock_value.report.handlebars +++ b/server/controllers/stock/reports/stock_value.report.handlebars @@ -21,7 +21,7 @@ {{date dateTo}} -
+ {{> exchangeRate rate=exchangeRate currencyId=currencyId}}
diff --git a/server/lib/template/helpers/math.js b/server/lib/template/helpers/math.js index 4a528b97a4..2726d51a74 100644 --- a/server/lib/template/helpers/math.js +++ b/server/lib/template/helpers/math.js @@ -2,7 +2,7 @@ function add(a, b) { return a + b; } -function substract(a, b) { +function subtract(a, b) { return a - b; } @@ -10,6 +10,10 @@ function multiply(a, b) { return (a * b) || 0; } +function divide(a, b) { + return (a / b) || 0; +} + /** * @function sum * @desc This function is responsible to calculate the sum of an array given @@ -37,6 +41,7 @@ function sum(array, column, ponderation) { } exports.multiply = multiply; +exports.divide = divide; exports.add = add; exports.sum = sum; -exports.substract = substract; +exports.subtract = subtract; diff --git a/server/lib/template/index.js b/server/lib/template/index.js index 4180f56602..376c8849bf 100644 --- a/server/lib/template/index.js +++ b/server/lib/template/index.js @@ -30,9 +30,10 @@ const hbs = exphbs.create({ timestamp : dates.timestamp, age : dates.age, multiply : math.multiply, + divide : math.divide, sum : math.sum, add : math.add, - substract : math.substract, + subtract : math.subtract, currency : finance.currency, numberToText : finance.numberToText, indentAccount : finance.indentAccount, diff --git a/server/lib/template/partials/exchangeRate.handlebars b/server/lib/template/partials/exchangeRate.handlebars index d5f96544d1..9e39d3d1a2 100644 --- a/server/lib/template/partials/exchangeRate.handlebars +++ b/server/lib/template/partials/exchangeRate.handlebars @@ -1,7 +1,18 @@ +{{#equal currencyId metadata.enterprise.currency_id}} +{{else}}
- {{#if this.lastRateUsed}} - {{translate 'EXCHANGE.EXCHANGE_RATES'}} : - {{currency 1 this.firstCurrency}} = {{currency this.lastRateUsed this.secondCurrency}} - ({{translate 'EXCHANGE.AT_THE_DATE'}} {{date this.dateTo}}) + {{#gt rate 1}} + {{translate 'FORM.LABELS.EXCHANGE_RATE'}}: {{currency rate currencyId 4}} + {{translate 'FORM.INFO.PER'}} {{currency 1 metadata.enterprise.currency_id 0}} + {{else}} + {{translate 'FORM.LABELS.EXCHANGE_RATE'}}: {{currency (divide 1 rate) metadata.enterprise.currency_id 4}} + {{translate 'FORM.INFO.PER'}} {{currency 1 currencyId 0}} + {{/gt}} + {{#if date}} + ({{translate 'EXCHANGE.AT_THE_DATE'}} {{date date}}) {{/if}} -
\ No newline at end of file + +
+

{{translate "REPORT.REPORT_ACCOUNTS.WARN_CURRENCY"}}

+
+{{/equal}} \ No newline at end of file