Skip to content

Commit

Permalink
feat(exchange): add exchange rate modal
Browse files Browse the repository at this point in the history
This commit adds the exchange rate modal to the enterprise configuration
page.  This allows a user to set a new exchange rate for a given
currency on a given date.
  • Loading branch information
Jonathan Niles authored and sfount committed Nov 23, 2016
1 parent 1f2edc4 commit 80a1137
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 12 deletions.
2 changes: 0 additions & 2 deletions client/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,6 @@
"ADDING_RATE" : "Adding an exchange Rate",
"ENTERPRISE_CURRENCY" : "Enterprise Currency",
"EXCHANGE_RATES" : "Exchange Rate(s)",
"REVIEW" : "Review Rates ",
"REVIEW_RATE" : "Review the exchange Rate",
"TITLE" : "Configure Exchange Rates",
"RATE_SET" : "rate last set"
},
Expand Down
2 changes: 1 addition & 1 deletion client/src/js/services/ExchangeRateService.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function ExchangeRateService($http, util, Currencies, Session) {
//if (cache) { return $q.resolve(cache); }

// query the exchange_rate table on the backend
return $http.get('/exchange', options)
return $http.get('/exchange', { params: options })
.then(util.unwrapHttpResponse)
.then(function (data) {

Expand Down
8 changes: 4 additions & 4 deletions client/src/partials/enterprises/exchange/exchange.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<div class="panel panel-default">

<div class="panel-heading clearfix">
<div class="panel-heading">
{{ "EXCHANGE.TITLE" | translate }}
</div>

<div class="panel-body" style="height : 300px; overflow : hidden">
<div class="panel-body" style="height : 315px; overflow : hidden">

<!-- show a loading indicator while loading -->
<p
ng-if="ExchangeCtrl.loading"
class="text-muted">
class="text-muted text-center">
<i class="fa fa-circle-o-notch fa-spin"></i>
{{ "FORM.INFO.LOADING" | translate }}
</p>
Expand All @@ -33,7 +33,7 @@
<p>{{ "EXCHANGE.ENTERPRISE_CURRENCY" | translate}}: <i>{{ ExchangeCtrl.formatCurrency(ExchangeCtrl.enterpriseCurrencyId) }}</i></p>

<ul class="list-unstyled">
<li data-exchange-rate-element ng-repeat="rate in ExchangeCtrl.rates">
<li data-exchange-rate-element ng-repeat="rate in ExchangeCtrl.rates | orderBy:'-date'">
<i class="fa fa-clock-o"></i> <b>{{ rate.rate }} {{ ExchangeCtrl.formatCurrency(rate.currency_id) }} </b> {{ "EXCHANGE.RATE_SET" | translate }} <span am-time-ago="rate.date"></span>.
<br />
<span class="icon-spacer text-muted"><i>{{rate.date | date : 'medium'}}</i></span>
Expand Down
4 changes: 1 addition & 3 deletions client/src/partials/enterprises/exchange/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ function ExchangeController(Session, Exchange, Notify, Currencies, $uibModal) {
}

// formats the currency nicely
vm.formatCurrency = function formatCurrency(id) {
return Currencies.name(id) + ' (' + Currencies.symbol(id) + ')';
};
vm.formatCurrency = Currencies.format;

// opens a modal to set a new exchange rate
vm.setNewExchangeRate = function setNewExchangeRate() {
Expand Down
64 changes: 64 additions & 0 deletions client/src/partials/enterprises/exchange/exchange.modal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<form name="ModalForm" bh-submit="ModalCtrl.submit(ModalForm)" bh-form-defaults novalidate>
<div class="modal-header">
<ol class="headercrumb">
<li class="static">{{ "EXCHANGE.TITLE" | translate }}</li>
<li class="title">{{ "EXCHANGE.ADDING_RATE" | translate }}</li>
</ol>
</div>

<div class="modal-body">

<bh-date-editor
date-value="ModalCtrl.rate.date"
validation-trigger="ModalForm.$submitted"
max-date="ModalCtrl.timestamp">
</bh-date-editor>

<div
class="form-group"
ng-class="{ 'has-error' : ModalForm.$submitted && ModalForm.currency_id.$invalid }">
<label class="control-label">
{{ "FORM.LABELS.CURRENCY" | translate }}
</label>

<ui-select
name="currency_id"
ng-model="ModalCtrl.rate.currency_id"
ng-disabled="ModalCtrl.currencies.length === 1"
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">
<span ng-bind-html="ModalCtrl.format(currency.id) | highlight:$select.search"></span>
</ui-select-choices>
</ui-select>

<div class="help-block" ng-messages="ModalForm.currency_id.$error" ng-show="ModalForm.$submitted">
<div ng-messages-include="partials/templates/messages.tmpl.html"></div>
</div>
</div>

<div class="form-group"
ng-class="{ 'has-error' : ModalForm.$submitted && ModalForm.rate.$invalid }"
>
<label class="control-label">{{ "FORM.LABELS.EXCHANGE_RATE" | translate }}</label>
<input class="form-control" type="number" name="rate" ng-model="ModalCtrl.rate.rate" required>
<div class="help-block" ng-messages="ModalForm.rate.$error" ng-show="ModalForm.$submitted">
<div ng-messages-include="partials/templates/messages.tmpl.html"></div>
</div>
</div>
</div>

<div class="modal-footer">
<button type="button" class="btn btn-default" ng-click="ModalCtrl.cancel()" data-method="cancel">
{{ "FORM.BUTTONS.CANCEL" | translate }}
</button>

<bh-loading-button loading-state="ModalForm.$loading">
{{ "FORM.BUTTONS.SUBMIT" | translate }}
</bh-loading-button>
</div>
</form>
56 changes: 56 additions & 0 deletions client/src/partials/enterprises/exchange/exchange.modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
angular.module('bhima.controllers')
.controller('ExchangeRateModalController', ExchangeRateModalController);

ExchangeRateModalController.$inject = [
'$uibModalInstance', 'ExchangeRateService', 'CurrencyService', 'SessionService', 'NotifyService'
];

/**
* This modal is a generic exchange rate modal that allows a user to
* set the exchange rate from virtually anywhere in the application.
*
*/
function ExchangeRateModalController(ModalInstance, Exchange, Currencies, Session, Notify) {
var vm = this;

// bind defaults
vm.timestamp = new Date();
vm.date = new Date();
vm.enterprise = Session.enterprise;

vm.rate = {
date : new Date(),
};

vm.submit = submit;
vm.format = Currencies.format;
vm.cancel = function () { ModalInstance.dismiss(); };

Currencies.read()
.then(function (currencies) {
vm.currencies = currencies
.filter(function (currency) {
return currency.id !== Session.enterprise.currency_id;
});

// use the first currency in the list
vm.rate.currency_id = vm.currencies[0];
})
.catch(Notify.handleError);

function submit(form) {
if (form.$invalid) { return; }

vm.rate.enterprise_id = Session.enterprise.id;

// TODO clean this up with proper Ui-select syntax when internet available
var currency = vm.rate.currency_id;
vm.rate.currency_id = currency.id;

return Exchange.create(vm.rate)
.then(function () {
ModalInstance.close();
});
}
}

4 changes: 2 additions & 2 deletions client/src/partials/exchange/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function ExchangeModalController(Rates, Currencies, Session, $uibModalInstance,
} else {
vm.text = 'EXCHANGE.NEW_RATE';
vm.action = 'create';
}
}


// bind methods
Expand All @@ -47,7 +47,7 @@ function ExchangeModalController(Rates, Currencies, Session, $uibModalInstance,

if(creation){
delete vm.data.id;
}
}

promise = (creation) ?
Rates.create(rate) :
Expand Down

0 comments on commit 80a1137

Please sign in to comment.