Skip to content

Commit

Permalink
Add server-side utils to display current exchange rate in reports
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcameron committed Jan 13, 2022
1 parent cddd292 commit c9c5ebb
Show file tree
Hide file tree
Showing 40 changed files with 179 additions and 214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ <h3 class="text-capitalize" translate>REPORT.CLIENTS.TITLE</h3>
required="true">
</bh-fiscal-year-select>

<bh-currency-select
currency-id="ReportConfigCtrl.reportDetails.currencyId"
on-change="ReportConfigCtrl.onSelectCurrency(currency)">
</bh-currency-select>

<bh-yes-no-radios
label="REPORT.CLIENTS.HIDE_LOCKED_CLIENTS"
value="ReportConfigCtrl.reportDetails.hideLockedClients"
Expand All @@ -51,6 +46,11 @@ <h3 class="text-capitalize" translate>REPORT.CLIENTS.TITLE</h3>
on-change-callback="ReportConfigCtrl.onIncludeCashClientsToggle(value)">
</bh-yes-no-radios>

<bh-currency-select
currency-id="ReportConfigCtrl.reportDetails.currencyId"
on-change="ReportConfigCtrl.onSelectCurrency(currency)">
</bh-currency-select>

<!--preview-->
<bh-loading-button loading-state="ConfigForm.$loading">
<span translate>REPORT.UTIL.PREVIEW</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ function OpenDebtorsConfigController(

vm.onSelectCurrency = currency => {
vm.reportDetails.currencyId = currency.id;
vm.reportDetails.currencySymbol = currency.symbol;
};

vm.requestSaveAs = function requestSaveAs() {
Expand Down
12 changes: 5 additions & 7 deletions client/src/modules/reports/generate/stock_entry/stock_entry.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ <h3 translate>REPORT.STOCK.ENTRY_REPORT</h3>
required="true">
</bh-date-interval>

<bh-currency-select
currency-id="ReportConfigCtrl.reportDetails.currencyId"
on-change="ReportConfigCtrl.onSelectCurrency(currency)" >
</bh-currency-select>

<!-- stock entry type -->
<div ng-class="{'has-error': ConfigForm.$submitted && !ReportConfigCtrl.hasOneChecked}">
<div class="checkbox">
Expand Down Expand Up @@ -76,15 +71,18 @@ <h3 translate>REPORT.STOCK.ENTRY_REPORT</h3>
<em ng-if="ConfigForm.$submitted && !ReportConfigCtrl.hasOneChecked" class="help-block" translate>STOCK.AT_LEAST_ONE_CHECKED</em>
</div>

<hr>

<div class="checkbox">
<label>
<input type="checkbox" ng-true-value="1" ng-false-value="0" ng-model="ReportConfigCtrl.reportDetails.showDetails">
<span translate>REPORT.STOCK.SHOW_DETAILS</span>
</label>
</div>

<bh-currency-select
currency-id="ReportConfigCtrl.reportDetails.currencyId"
on-change="ReportConfigCtrl.onSelectCurrency(currency)" >
</bh-currency-select>

<!-- preview -->
<bh-loading-button loading-state="ConfigForm.$loading">
<span translate>REPORT.UTIL.PREVIEW</span>
Expand Down
12 changes: 5 additions & 7 deletions client/src/modules/reports/generate/stock_exit/stock_exit.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ <h3 translate>REPORT.STOCK.EXIT_REPORT</h3>
required="true">
</bh-date-interval>

<bh-currency-select
currency-id="ReportConfigCtrl.currency_id"
on-change="ReportConfigCtrl.onSelectCurrency(currency)" >
</bh-currency-select>

<!-- stock exit type -->
<div ng-class="{'has-error': ConfigForm.$submitted && !ReportConfigCtrl.hasOneChecked}">
<div class="checkbox">
Expand Down Expand Up @@ -90,15 +85,18 @@ <h3 translate>REPORT.STOCK.EXIT_REPORT</h3>
<em ng-if="ConfigForm.$submitted && !ReportConfigCtrl.hasOneChecked" class="help-block" translate>STOCK.AT_LEAST_ONE_CHECKED</em>
</div>

<hr>

<div class="checkbox">
<label>
<input type="checkbox" ng-true-value="1" ng-false-value="0" ng-model="ReportConfigCtrl.showDetails">
<span translate>REPORT.STOCK.SHOW_DETAILS</span>
</label>
</div>

<bh-currency-select
currency-id="ReportConfigCtrl.currency_id"
on-change="ReportConfigCtrl.onSelectCurrency(currency)" >
</bh-currency-select>

<!-- preview -->
<bh-loading-button loading-state="ConfigForm.$loading">
<span translate>REPORT.UTIL.PREVIEW</span>
Expand Down
13 changes: 6 additions & 7 deletions client/src/modules/reports/generate/stock_value/stock_value.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ <h3 translate>REPORT.STOCK_VALUE.TITLE</h3>
on-change="ReportConfigCtrl.onDateChange(date)">
</bh-date-editor>

<!-- the currency to be used in the footer -->
<bh-currency-select
currency-id="ReportConfigCtrl.currency_id"
on-change="ReportConfigCtrl.onSelectCurrency(currency)">
</bh-currency-select>

<hr>
<div class="checkbox">
<label>
<input type="checkbox" ng-true-value="1" ng-false-value="0"
Expand All @@ -56,6 +49,12 @@ <h3 translate>REPORT.STOCK_VALUE.TITLE</h3>
</label>
</div>

<!-- the currency to be used in the footer -->
<bh-currency-select
currency-id="ReportConfigCtrl.currency_id"
on-change="ReportConfigCtrl.onSelectCurrency(currency)">
</bh-currency-select>

<!-- preview -->
<bh-loading-button loading-state="ConfigForm.$loading">
<span translate>REPORT.UTIL.PREVIEW</span>
Expand Down
4 changes: 2 additions & 2 deletions server/controllers/finance/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
</tr>
</thead>


<tbody>
<tr>
<th colspan="2">{{translate 'REPORT.CLIENT_SUMMARY.NOT_EMPLOYEES_DEBTS' }} ({{patients.length}})</th>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<strong class="text-capitalize">{{date dateTo "MMMM YYYY"}}</strong>
</h4>

{{> exchangeRate lastRateUsed=lastRateUsed firstCurrency=firstCurrency secondCurrency=secondCurrency dateTo=dateTo}}
{{> exchangeRate rate=lastRateUsed currencyId=currencyId date=dateTo}}

<table style="page-break-after: always;" class="table table-striped table-condensed table-report table-bordered">
<thead>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
(
Expand All @@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
{{ fiscalYear.label }}
</h4>

{{#unless isEnterpriseCurrency}}
<div class="alert alert-warning">
<p>{{translate "REPORT.REPORT_ACCOUNTS.WARN_CURRENCY"}}</p>
</div>
{{/unless}}
{{> exchangeRate rate=exchangeRate currencyId=currencyId}}

<section>
<table class="table table-condensed table-report table-bordered">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,7 +34,7 @@ async function setupAnnualClientsReport(options, enterpriseCurrencyId) {
]);

return {
rows, footer, fiscalYear, rate, isEnterpriseCurrency,
rows, footer, fiscalYear, exchangeRate : rate, currencyId,
};
}

Expand All @@ -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) {
Expand Down
13 changes: 2 additions & 11 deletions server/controllers/finance/reports/debtors/openDebtors.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,8 @@
{{#if details.limitDate}}{{date details.reportDateLimit}} - {{/if}}
{{date this.timestamp }}
</h4>
{{#unless enterpriseCurrency}}
{{#if showReciprocalRate}}
<h5 class="text-center">
{{ translate 'FORM.LABELS.EXCHANGE_RATE' }}: {{currency reciprocalRate enterpriseCurrencyId}} {{translate 'FORM.INFO.PER'}} {{ currencySymbol }}
</h5>
{{else}}
<h5 class="text-center">
{{ translate 'FORM.LABELS.EXCHANGE_RATE' }}: {{currency rate currencyId}} {{translate 'FORM.INFO.PER'}} {{ enterpriseCurrencySymbol }}
</h5>
{{/if}}
{{/unless}}

{{> exchangeRate rate=exchangeRate currencyId=currencyId}}

<!-- margin is the cell size -->
<section>
Expand Down
18 changes: 3 additions & 15 deletions server/controllers/finance/reports/debtors/openDebtors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
</span>
</h3>

{{#unless isEnterpriseCurrency}}
<div class="alert alert-warning">
<p>{{translate "REPORT.REPORT_ACCOUNTS.WARN_CURRENCY"}}</p>
</div>
{{/unless}}

{{#if limitTimeInterval}}
<div class="text-center">
<strong>
Expand All @@ -24,6 +18,8 @@
</div>
{{/if}}

{{> exchangeRate rate=exchangeRate currencyId=currencyId date=dateExchangeRate}}

<section>
<table class="table table-condensed table-bordered table-report">
{{#if summary}}
Expand Down
8 changes: 5 additions & 3 deletions server/controllers/finance/reports/financial.all_employees.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit c9c5ebb

Please sign in to comment.