Skip to content

Commit

Permalink
Add infrasructure for receipt currency component
Browse files Browse the repository at this point in the history
This commit sets up the infrastructure for the receipt currency
component. This component will provide an on update callback that
updates the controller when the user has selected a new currency for
the receipt. It will also cache the user's previous choice.
  • Loading branch information
sfount committed Nov 27, 2016
1 parent dfb4645 commit e504038
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 30 deletions.
48 changes: 48 additions & 0 deletions client/src/js/components/bhReceiptCurrency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
angular.module('bhima.components').component('bhReceiptCurrency', {
templateUrl : 'partials/templates/bhReceiptCurrency.tmpl.html',
controller : ReceiptCurrencyController,
bindings : {
onUpdate : '&'
}
});

ReceiptCurrencyController.$inject = ['CurrencyService', 'SessionService', 'AppCache', 'Store'];

/**
* Receipt Currency Component
*
*/
function ReceiptCurrencyController(Currencies, Session, AppCache, Store) {
var ctrl = this;
var cache = new AppCache('ReceiptCurrencyComponent');

Currencies.read()
.then(function (currencies) {
ctrl.currencies = new Store();
ctrl.currencies.setData(currencies);
loadDefaultCurrency();
});

ctrl.update = function (currency) {
// update view with currency object (id and symbol)
ctrl.selectedCurrency = currency;

// update cache with id to select this currency object on next load
cache.selectedCurrencyId = currency.id;

// update bindings
ctrl.onUpdate({ currencyId : currency.id });
};

function loadDefaultCurrency() {
// if the cache exists - use that
var cached = cache.selectedCurrencyId;
if (cached) {
ctrl.update(ctrl.currencies.get(cached));
return;
}

// no cached value - use the enterprise currency
ctrl.update(ctrl.currencies.get(Session.enterprise.currency_id));
}
}
11 changes: 2 additions & 9 deletions client/src/partials/patient_invoice/registry/registry.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,8 @@


<div class="toolbar-item">
<div class="btn-group" uib-dropdown dropdown-append-to-body>
<button class="btn btn-default" uib-dropdown-toggle>{{ "INVOICE_REGISTRY.RECEIPT_CURRENCY" | translate }}</button>
<button style="min-width : 30px" class="btn btn-default">
<span>{{InvoiceRegistryCtrl.selectedCurrency.symbol}}</span>
</button>
<ul class="dropdown-menu" uib-dropdown-menu role="menu">
<li ng-repeat="currency in InvoiceRegistryCtrl.currencies track by currency.id" role="menuitem"><a ng-click="InvoiceRegistryCtrl.setCurrency(currency)" href>{{currency.name}} (<b>{{currency.symbol}}</b>)</a></li>
</ul>
</div>
<bh-receipt-currency
on-update="InvoiceRegistryCtrl.setReceiptCurrency(currencyId)" />
</div>
</div>
</div>
Expand Down
27 changes: 6 additions & 21 deletions client/src/partials/patient_invoice/registry/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ angular.module('bhima.controllers')
InvoiceRegistryController.$inject = [
'PatientInvoiceService', 'bhConstants', 'NotifyService',
'SessionService', 'util', 'ReceiptModal', 'appcache',
'uiGridConstants', 'CurrencyService'
'uiGridConstants'
];

/**
* Invoice Registry Controller
*
* This module is responsible for the management of Invoice Registry.
*/
function InvoiceRegistryController(Invoices, bhConstants, Notify, Session, util, Receipt, AppCache, uiGridConstants, Currencies) {
function InvoiceRegistryController(Invoices, bhConstants, Notify, Session, util, Receipt, AppCache, uiGridConstants) {
var vm = this;

var cache = AppCache('InvoiceRegistry');

vm.search = search;
vm.openReceiptModal = Receipt.invoice;
vm.setCurrency = setCurrency;
vm.onRemoveFilter = onRemoveFilter;
vm.clearFilters = clearFilters;
vm.creditNote = creditNote;
Expand Down Expand Up @@ -57,22 +56,13 @@ function InvoiceRegistryController(Invoices, bhConstants, Notify, Session, util,
enableSorting : true,
rowTemplate : '/partials/patient_invoice/templates/grid.creditNote.tmpl.html'
};
vm.receiptOptions = {};

// receipt options
vm.selectedCurrency = {
id : vm.enterprise.currency_id,
symbol : vm.enterprise.currencySymbol
// receiptOptions are used in the bh-print directive under the receipt-action template
vm.setReceiptCurrency = function setReceiptCurrency(currencyId) {
vm.receiptOptions.currency = currencyId;
};

vm.receiptOptions = {
currency : vm.selectedCurrency.id
};

function setCurrency(currency) {
vm.selectedCurrency = currency;
vm.receiptOptions.currency = vm.selectedCurrency.id;
}

function handler(error) {
vm.hasError = true;
Notify.handleError(error);
Expand All @@ -82,11 +72,6 @@ function InvoiceRegistryController(Invoices, bhConstants, Notify, Session, util,
vm.loading = !vm.loading;
}

Currencies.read()
.then(function (currencies) {
vm.currencies = currencies;
});

// this function loads invoices from the database with search parameters
// if passed in.
function load(parameters) {
Expand Down
11 changes: 11 additions & 0 deletions client/src/partials/templates/bhReceiptCurrency.tmpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div class="btn-group" uib-dropdown dropdown-append-to-body>
<button class="btn btn-default" uib-dropdown-toggle>{{ "INVOICE_REGISTRY.RECEIPT_CURRENCY" | translate }}</button>
<button style="min-width : 30px" class="btn btn-default">
<span>{{$ctrl.selectedCurrency.symbol || '-'}}</span>
</button>
<ul class="dropdown-menu" uib-dropdown-menu role="menu">
<li ng-repeat="currency in $ctrl.currencies.data track by currency.id" role="menuitem">
<a ng-click="$ctrl.update(currency)" href>{{currency.name}} (<b>{{currency.symbol}}</b>)</a>
</li>
</ul>
</div>

0 comments on commit e504038

Please sign in to comment.