Skip to content

Commit

Permalink
Added Euros and fixed exchange rate selection page.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcameron committed Apr 8, 2021
1 parent 6852ebf commit b0ff201
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 23 deletions.
29 changes: 29 additions & 0 deletions client/src/i18n/currency/eur.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"CURRENCY_SYM": "",
"DECIMAL_SEP": ".",
"GROUP_SEP": ",",
"PATTERNS": [
{
"gSize": 3,
"lgSize": 3,
"maxFrac": 3,
"minFrac": 0,
"minInt": 1,
"negPre": "-",
"negSuf": "",
"posPre": "",
"posSuf": ""
},
{
"gSize": 3,
"lgSize": 3,
"maxFrac": 2,
"minFrac": 2,
"minInt": 1,
"negPre": "-\u00a4",
"negSuf": "",
"posPre": "\u00a4",
"posSuf": ""
}
]
}
1 change: 1 addition & 0 deletions client/src/i18n/en/form.json
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
"PATIENT_INVOICE_FOUND": "Invoice found",
"PATIENT_INVOICE_NOT_FOUND": "No invoices found for the patient: {{name}}",
"PAYMENT": "Cash Payment",
"PER": "per",
"PREVIOUS": "Previous",
"RECORD_SAME":"No changes have been made to the form",
"REQUISITION": {
Expand Down
1 change: 1 addition & 0 deletions client/src/i18n/fr/form.json
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
"PATIENT_INVOICE_FOUND": "La facture a été trouvée",
"PATIENT_INVOICE_NOT_FOUND": "La facture est inexistante pour le patient: {{name}}",
"PAYMENT": "Référence du Paiement",
"PER": "par",
"PREVIOUS": "Précédent",
"RECORD_SAME":"Aucun changement n'a été apporté sur le formulaire",
"REQUISITION": {
Expand Down
2 changes: 1 addition & 1 deletion client/src/js/services/currencyFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function currencyFormat(Currencies, $http, Store) {
var formatObject = null;
fetchingKeys[key] = true;

$http.get(currencyConfigurationPath.concat(key, '.json'))
$http.get(currencyConfigurationPath.concat(key.toLowerCase(), '.json'))
.then(function (response) {

// Add configuration to local cache
Expand Down
7 changes: 5 additions & 2 deletions client/src/modules/exchange/exchange.modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
<ui-select
name="currency"
ng-model="ModalCtrl.rate.currency"
ng-change="ModalCtrl.selectCurrency()"
required>
<ui-select-match placeholder="{{ 'FORM.PLACEHOLDERS.CURRENCY' | translate }}">
<span>{{ ModalCtrl.format($select.selected.id) }}</span>
</ui-select-match>
<ui-select-choices
ui-select-focus-patch
repeat="currency.id as currency in ModalCtrl.currencies | filter:$select.search">
repeat="currency as currency in ModalCtrl.currencies | filter:$select.search">
<span ng-bind-html="ModalCtrl.format(currency.id) | highlight:$select.search"></span>
</ui-select-choices>
</ui-select>
Expand All @@ -39,7 +40,9 @@
</bh-date-editor>

<p>
<span translate>EXCHANGE.CURRENT_RATE</span>: <i>{{ ModalCtrl.currentExchangeRate | currency:ModalCtrl.rate.currency.id }}</i>
<span translate>EXCHANGE.CURRENT_RATE</span>:
<i>{{ ModalCtrl.currentExchangeRate | currency:ModalCtrl.rate.currency.id }}</i>
<i><span translate>FORM.INFO.PER</span> {{ModalCtrl.enterprise.currencySymbol}}</i>
</p>

<div class="form-group"
Expand Down
4 changes: 4 additions & 0 deletions client/src/modules/exchange/exchange.modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ function ExchangeRateModalController(ModalInstance, Exchange, Currencies, Sessio
vm.symbol = Currencies.symbol;
vm.cancel = function cancel() { ModalInstance.dismiss(); };

vm.selectCurrency = () => {
vm.currentExchangeRate = Exchange.getCurrentRate(vm.rate.currency.id);
};

// this turns on and off the currency select input
vm.hasMultipleCurrencies = false;

Expand Down
2 changes: 1 addition & 1 deletion server/config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ exports.configure = function configure(app) {
app.get('/invoices/stats', stats.invoices);

// exchange rate modules
app.get('/exchange', exchange.list);
app.get('/exchange/:id?', exchange.list);
app.post('/exchange', exchange.create);
app.put('/exchange/:id', exchange.update);
app.delete('/exchange/:id', exchange.delete);
Expand Down
7 changes: 3 additions & 4 deletions server/controllers/finance/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function formatExchangeRateForDisplay(value) {
*/
exports.list = function list(req, res, next) {
const { enterprise } = req.session;
const options = req.query;
const options = { ...req.query, ...req.params };

getExchangeRateList(enterprise.id, options)
.then(rows => {
Expand All @@ -60,17 +60,17 @@ function getExchangeRateList(enterpriseId, opts) {

const limit = Number(options.limit);
const limitQuery = Number.isNaN(limit) ? '' : `LIMIT ${limit}`;
const whereQuery = options.id ? `AND exchange_rate.id = ${options.id}` : '';

const sql = `
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
WHERE exchange_rate.enterprise_id = ?
WHERE exchange_rate.enterprise_id = ? ${whereQuery}
ORDER BY date DESC
${limitQuery};
`;

return db.exec(sql, [enterpriseId]);
}

Expand Down Expand Up @@ -103,7 +103,6 @@ exports.update = function update(req, res, next) {
if (req.body.date) {
req.body.date = new Date(req.body.date);
}

db.exec(sql, [req.body, req.params.id])
.then(() => {
sql = `SELECT
Expand Down
3 changes: 2 additions & 1 deletion server/models/bhima.sql
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ INSERT INTO `language` VALUES
-- Currencies
INSERT INTO `currency` (`id`, `name`, `format_key`, `symbol`, `note`, `min_monentary_unit`) VALUES
(1,'Congolese Francs','fc','Fc',NULL,50.00),
(2,'United States Dollars','usd','$',NULL,0.01);
(2,'United States Dollars','usd','$',NULL,0.01),
(3,'Euro','EUR','',NULL,0.01);

INSERT INTO `inventory_type` VALUES (1,'Article'),(2,'Assembly'),(3,'Service');
INSERT INTO `inventory_unit` VALUES
Expand Down
11 changes: 11 additions & 0 deletions server/models/migrations/next/migrate.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ CALL drop_column_if_exists('lots', 'initial_quantity');
CALL drop_column_if_exists('lots', 'quantity');
CALL drop_column_if_exists('lots', 'entry_date');

/**
* @author: jmcameron
* @date: 2021-04-08
* @desc: Add support for Euros
*/
INSERT IGNORE INTO `currency` (`id`, `name`, `format_key`, `symbol`, `note`, `min_monentary_unit`)
VALUES
(3,'Euro','EUR','',NULL,0.01);
INSERT IGNORE INTO `exchange_rate`
VALUES
(3, 1, @EUR, 0.84, NOW());
4 changes: 3 additions & 1 deletion test/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,13 @@ INSERT INTO `project_permission` VALUES (1, 1, 1),(2, 1, 2),(3, 2, 1),(4, 4, 1);

SET @USD = 1;
SET @FC = 2;
SET @EUR = 3;

-- exchange rate for the current date
INSERT INTO `exchange_rate` VALUES
(1, 1, @USD, 900.0000, DATE('2016-01-01')),
(2, 1, @USD, 930.0000, NOW());
(2, 1, @USD, 930.0000, NOW()),
(3, 1, @EUR, 0.8400, NOW());

INSERT INTO `cash_box` (id, label, project_id, is_auxiliary) VALUES
(1, 'Caisse Principale', 1, 0),
Expand Down
20 changes: 16 additions & 4 deletions test/integration/currencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,41 @@
const helpers = require('./helpers');

describe('(/currencies) currencies API routes', () => {
const currencyId = 1;
const currencyIdFc = 1;
const currencyIdEur = 3;
const keys = [
'id', 'name', 'note', 'format_key', 'symbol', 'min_monentary_unit',
];

it('GET /currencies should return a list of currencies', () => {
return agent.get('/currencies')
.then((res) => {
helpers.api.listed(res, 2);
helpers.api.listed(res, 3);
})
.catch(helpers.handler);
});

it('GET /currencies/:id should return a single currency', () => {
return agent.get('/currencies/'.concat(currencyId))
it('GET /currencies/:id should return a single currency for Fc', () => {
return agent.get('/currencies/'.concat(currencyIdFc))
.then((res) => {
expect(res).to.have.status(200);
expect(res).to.be.json;
expect(res.body).to.have.keys(keys);
expect(res.body.name).to.equal('Congolese Francs');
})
.catch(helpers.handler);
});

it('GET /currencies/:id should return a single currency for EUR', () => {
return agent.get('/currencies/'.concat(currencyIdEur))
.then((res) => {
expect(res).to.have.status(200);
expect(res).to.be.json;
expect(res.body).to.have.keys(keys);
expect(res.body.name).to.equal('Euro');
})
.catch(helpers.handler);
});
it('GET /currencies/:id should return an error for unknown id', () => {
return agent.get('/currencies/123456789')
.then((res) => {
Expand Down
74 changes: 65 additions & 9 deletions test/integration/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@ const helpers = require('./helpers');
describe('(/exchange) The /exchange API endpoint', () => {

// constants
const RATE = {
const FcRATE = {
enterprise_id : 1, // Enterprise ID
currency_id : 1, // FC in test database
rate : 930,
date : new Date('2015-10-10'),
};

const EuroRATE = {
enterprise_id : 1, // Enterprise ID
currency_id : 3, // Euro in test database
rate : 0.84,
date : new Date('2021-04-08'),
};

const RATE_KEY = [
'id', 'enterprise_id', 'currency_id', 'enterprise_currency_id', 'rate', 'date',
];

it('GET /exchange returns a list of exchange rates', () => {
return agent.get('/exchange')
.then((res) => {
helpers.api.listed(res, 2);
helpers.api.listed(res, 3);
})
.catch(helpers.handler);
});
Expand All @@ -38,15 +45,15 @@ describe('(/exchange) The /exchange API endpoint', () => {
.catch(helpers.handler);
});

it('POST /exchange creates a new exchange rate', () => {
it('POST /exchange creates a new exchange rate for Fc', () => {
return agent.post('/exchange')
.send({ rate : RATE })
.send({ rate : FcRATE })
.then((res) => {

// make sure the API conforms to app standards
helpers.api.created(res);

RATE.id = res.body.id;
FcRATE.id = res.body.id;
return agent.get('/exchange');
})
.then((res) => {
Expand All @@ -59,13 +66,62 @@ describe('(/exchange) The /exchange API endpoint', () => {
.catch(helpers.handler);
});

it('PUT /exchange should update an existing exchange rate', () => {
return agent.put(`/exchange/${RATE.id}`)
it('PUT /exchange should update an existing exchange rate for Fc', () => {
return agent.put(`/exchange/${FcRATE.id}`)
.send({ rate : 925 })
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.have.keys(RATE_KEY);
expect(res.body.rate).to.not.equal(RATE.rate);
expect(res.body.rate).to.not.equal(FcRATE.rate);
})
.catch(helpers.handler);
});

it('POST /exchange creates a new exchange rate for Euros', () => {
return agent.post('/exchange')
.send({ rate : EuroRATE })
.then((res) => {

// make sure the API conforms to app standards
helpers.api.created(res);

EuroRATE.id = res.body.id;
return agent.get('/exchange');
})
.then((res) => {
expect(res).to.have.status(200);
expect(res).to.be.json;
expect(res.body).to.not.be.empty;
expect(res.body[0]).to.not.be.empty;
expect(res.body[0]).to.contain.keys('currency_id', 'date', 'rate');
})
.catch(helpers.handler);
});

it('PUT /exchange should update an existing exchange rate for Euros', () => {
return agent.put(`/exchange/${EuroRATE.id}`)
.send({ rate : 0.86 })
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.have.keys(RATE_KEY);
expect(res.body.rate).to.not.equal(EuroRATE.rate);
})
.catch(helpers.handler);
});

it('GET /exchange returns a list of the updated exchange rates', () => {
return agent.get('/exchange')
.then((res) => {
helpers.api.listed(res, 5);
})
.catch(helpers.handler);
});

it('GET /exchange/id get updated exchange rate for Euros', () => {
return agent.get(`/exchange/${EuroRATE.id}`)
.then((res) => {
helpers.api.listed(res, 1);
expect(res.body[0].rate).to.equal(0.86);
})
.catch(helpers.handler);
});
Expand Down Expand Up @@ -111,7 +167,7 @@ describe('(/exchange) The /exchange API endpoint', () => {
});

it('DELETE /exchange/:id should delete an exchange rate ', () => {
return agent.delete(`/exchange/${RATE.id}`)
return agent.delete(`/exchange/${FcRATE.id}`)
.then((res) => {
helpers.api.deleted(res);
})
Expand Down

0 comments on commit b0ff201

Please sign in to comment.