Skip to content

Commit

Permalink
fix(exchange): show HR rate instead of inverted
Browse files Browse the repository at this point in the history
This commit fixes the exchange rate to show a human readable rate
instead of the inverted rate.  It now works correctly with both FC and
USD enterprise currencies.
  • Loading branch information
jniles committed Feb 28, 2018
1 parent 3b13bfb commit 8cc6626
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
17 changes: 11 additions & 6 deletions server/controllers/finance/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

const db = require('../../lib/db');
const NotFound = require('../../lib/errors/NotFound');
const util = require('../../lib/util');

exports.getExchangeRate = getExchangeRate;
exports.formatExchangeRateForDisplay = formatExchangeRateForDisplay;

// uses the mysql function getExchangeRate() to find
// the correct exchange rate
Expand All @@ -19,6 +21,11 @@ function getExchangeRate(enterpriseId, currencyId, date) {
.then(rows => rows[0]);
}

// gets a positive number for the exchange rate display.
function formatExchangeRateForDisplay(value) {
return (value < 1) ? util.roundDecimal(1 / value, 2) : value;
}

/**
* @method list
*
Expand All @@ -29,7 +36,7 @@ function getExchangeRate(enterpriseId, currencyId, date) {
* URL: /exchange
*/
exports.list = function list(req, res, next) {
const enterprise = req.session.enterprise;
const { enterprise } = req.session;
const options = req.query;

getExchangeRateList(enterprise.id, options)
Expand All @@ -54,7 +61,7 @@ function getExchangeRateList(enterpriseId, opts) {
const limitQuery = Number.isNaN(limit) ? '' : `LIMIT ${limit}`;

const sql = `
SELECT exchange_rate.id, exchange_rate.enterprise_id, exchange_rate.currency_id,
SELECT exchange_rate.id, exchange_rate.enterprise_id, exchange_rate.currency_id,
exchange_rate.rate, exchange_rate.date, enterprise.currency_id AS 'enterprise_currency_id'
FROM exchange_rate
JOIN enterprise ON enterprise.id = exchange_rate.enterprise_id
Expand Down Expand Up @@ -90,9 +97,7 @@ exports.create = function create(req, res, next) {

// PUT /exchange/:id
exports.update = function update(req, res, next) {
var sql;

sql =
let sql =
'UPDATE exchange_rate SET ? WHERE id = ?;';

// should we even be changed the date?
Expand All @@ -104,7 +109,7 @@ exports.update = function update(req, res, next) {
.then(() => {
sql =
`SELECT
exchange_rate.id, exchange_rate.enterprise_id, exchange_rate.currency_id,
exchange_rate.id, exchange_rate.enterprise_id, exchange_rate.currency_id,
exchange_rate.rate, exchange_rate.date, enterprise.currency_id AS enterprise_currency_id
FROM exchange_rate
JOIN enterprise ON enterprise.id = exchange_rate.enterprise_id
Expand Down
7 changes: 3 additions & 4 deletions server/controllers/finance/reports/reportAccounts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const Exchange = require('../../exchange');
const Currency = require('../../currencies');
const Fiscal = require('../../fiscal');
const FilterParser = require('../../../../lib/filter');
const util = require('../../../../lib/util');

const TEMPLATE = './server/controllers/finance/reports/reportAccounts/report.handlebars';

Expand Down Expand Up @@ -52,7 +51,7 @@ function document(req, res, next) {
})
.then(rate => {
bundle.rate = rate.rate || 1;
bundle.invertedRate = util.roundDecimal(1 / bundle.rate, 2);
bundle.invertedRate = Exchange.formatExchangeRateForDisplay(bundle.rate);
return AccountsExtra.getOpeningBalanceForDate(params.account_id, params.dateFrom, false);
})
.then(balance => {
Expand Down Expand Up @@ -138,7 +137,7 @@ function getGeneralLedgerSQL(options) {

const sql = `
SELECT trans_id, description, trans_date, document_reference, debit, credit,
debit_equiv, credit_equiv, currency_id, rate, (1 / rate) AS invertedRate,
debit_equiv, credit_equiv, currency_id, rate, IF(rate < 1, (1 / rate), rate) AS invertedRate,
${columns}
FROM (
SELECT trans_id, description, trans_date, document_map.text AS document_reference,
Expand Down Expand Up @@ -248,7 +247,7 @@ function getAccountTransactions(options, openingBalance = 0) {
exchangedBalance : totals.balance * totals.rate,
exchangedCumSum : lastCumSum,
exchangedDate : new Date(),
invertedRate : util.roundDecimal(1 / totals.rate, 2),
invertedRate : Exchange.formatExchangeRateForDisplay(totals.rate),
shouldDisplayDebitCredit,
transactionCurrencyId : lastCurrencyId,
};
Expand Down

0 comments on commit 8cc6626

Please sign in to comment.