Skip to content

Commit

Permalink
fix(cash): rework transfer modal with ui-router
Browse files Browse the repository at this point in the history
This commit fixes the cash transfer modal to work with the newer journal
vouchers interface.  The cash transfers now display the account names
and the currency is dependent on the account being selected.

The cash transfer modal can now be deep-linked via ui-router.  This
allows for a nice cleanup of states, but complicates the receipt modal a
little bit.  A receipt has not been implemented for cash transfers yet.
  • Loading branch information
Jonathan Niles committed Aug 24, 2016
1 parent 263107f commit ada250d
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 561 deletions.
2 changes: 1 addition & 1 deletion client/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
},
"TITLE" : "Cash Window",
"TRANSFER": {
"TITLE" : "Transfers"
"TITLE" : "New Cash Transfer"
},
"VOUCHER": {
"CASHBOXES": {
Expand Down
8 changes: 4 additions & 4 deletions client/src/js/components/bhCurrencyInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ function CurrencyInputController(Currencies, $scope) {

// load currency from the currency service
Currencies.detail(ctrl.currencyId)
.then(function (currency) {
.then(function (currency) {

// bind the currency to the controller
ctrl.currency = currency;
});
// bind the currency to the controller
ctrl.currency = currency;
});
}
}
61 changes: 35 additions & 26 deletions client/src/js/services/CashService.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ function CashService(Api, Exchange, Session, moment) {

// custom methods
service.create = create;
service.reference = reference;
service.getTransferRecord = getTransferRecord;
service.calculateDisabledIds = calculateDisabledIds;

/**
* Cash Payments can be made to multiple invoices. This function loops
Expand Down Expand Up @@ -91,37 +91,19 @@ function CashService(Api, Exchange, Session, moment) {
return Api.create.call(service, { payment : data });
}

/**
* Searches for a cash payment by its reference.
*
* @method reference
* @param {String} reference
* @returns {Promise} promise - a resolved or rejected promise with the
* result sent from the server.
*/
function reference(ref) {
var target = service.url.concat('references/', ref);
return this.$http.get(target)
.then(this.util.unwrapHttpResponse);
}

/**
* This method is responsible to create a voucher object and it back
*/
function getTransferRecord(cashAccountCurrency, amount, currencyId) {
/**
* The date field is set at the server side
* @todo the date in timestamp type in the database
*/
var voucher = {
project_id : Session.project.id,
currency_id : currencyId,
amount : amount,
description : generateTransferDescription(),
user_id : Session.user.id,
project_id: Session.project.id,
currency_id: currencyId,
amount: amount,
description: generateTransferDescription(),
user_id: Session.user.id,

// two lines (debit and credit) to be recorded in the database
items : [{
items: [{
account_id : cashAccountCurrency.account_id,
debit : 0,
credit : amount,
Expand All @@ -132,7 +114,34 @@ function CashService(Api, Exchange, Session, moment) {
}]
};

return { voucher : voucher };
return voucher;
}

/**
* @method calculateDisabledIds
*
* @description
* For a given cashbox, determine which currencies should be unsupported and
* therefore disabled from selection.
*
* @param {Object} cashbox - the cashbox to read from.
* @param {Array} currencies - a list of application currencies
* @returns {Array} - the array of currency ids to disable
*/
function calculateDisabledIds(cashbox, currencies) {

// collect cashbox ids in an array
var cashboxCurrencyIds = cashbox.currencies.reduce(function (array, currency) {
return array.concat(currency.currency_id);
}, []);

// find all ids that are not cashbox ids, to disable them
var disabledCurrencyIds = currencies.reduce(function (array, currency) {
var bool = (cashboxCurrencyIds.indexOf(currency.id) === -1);
return array.concat(bool ? currency.id : []);
}, []);

return disabledCurrencyIds;
}

/**
Expand Down
15 changes: 10 additions & 5 deletions client/src/js/services/VoucherService.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
angular.module('bhima.services')
.service('VoucherService', VoucherService);
.service('VoucherService', VoucherService);

VoucherService.$inject = ['PrototypeApiService', '$http', 'util'];
VoucherService.$inject = [
'PrototypeApiService', '$http', 'util'
];

/**
* @class VoucherService
Expand All @@ -12,6 +14,8 @@ VoucherService.$inject = ['PrototypeApiService', '$http', 'util'];
*/
function VoucherService(Api, $http, util) {
var service = new Api('/vouchers/');

// @tdoo - remove this reference to baseUrl
var baseUrl = '/journal/';

// transfer type
Expand Down Expand Up @@ -47,6 +51,7 @@ function VoucherService(Api, $http, util) {
* accounting.
*
* @param {object} voucher - the raw journal voucher
* @returns {Promise} - the $http promise object
*/
function createSimple(voucher) {

Expand Down Expand Up @@ -75,9 +80,9 @@ function VoucherService(Api, $http, util) {
}

/**
* This method facilitate annulling a transaction,
* bhima should automatically be able to reverse
* any transaction in the posting_journal by creating a
* This method facilitate annulling a transaction,
* bhima should automatically be able to reverse
* any transaction in the posting_journal by creating a
* new transaction that is an exact duplicate of the original transaction with sign minous.
*/
function reverse(creditNote) {
Expand Down
3 changes: 2 additions & 1 deletion client/src/js/services/receipts/ReceiptService.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ function ReceiptService($http, util, Language) {

// print a cash (point-of-sale) receipt
function cash(uuid, options) {
/* noop */
var route = '/reports/cash/'.concat(uuid);
return fetch(route, options);
}

// print a generic transaction receipt
Expand Down
6 changes: 3 additions & 3 deletions client/src/partials/cash/cash.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
<!-- Switch Cashbox Form and Transfer -->
<div class="col-md-2">

<button class="btn btn-warning btn-block" ui-sref="cash.select({id : CashCtrl.cashbox.id })" data-change-cashbox>
<button class="btn btn-warning btn-block" ui-sref="cash.select({ id : CashCtrl.cashbox.id })" data-change-cashbox>
<i class="fa fa-inbox"></i> {{ "CASH.VOUCHER.CASHBOXES.CHANGE_CASHBOX" | translate }}
</button>

<button class="btn btn-primary btn-block" ng-click="CashCtrl.openTransferModal()" data-perform-transfer>
<span class="glyphicon glyphicon-export"></span> {{ "CASH.VOUCHER.CASHBOXES.TRANSFER" | translate }}
<button class="btn btn-primary btn-block" ui-sref="cash.transfer({ id: CashCtrl.cashbox.id })" data-perform-transfer>
<i class="fa fa-exchange"></i> {{ "CASH.VOUCHER.CASHBOXES.TRANSFER" | translate }}
</button>
</div>

Expand Down
52 changes: 17 additions & 35 deletions client/src/partials/cash/cash.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
angular.module('bhima.controllers')
.controller('CashController', CashController);
.controller('CashController', CashController);

CashController.$inject = [
'CashService', 'CashboxService', 'AppCache', 'CurrencyService',
'ExchangeRateService', 'SessionService', 'ModalService',
'NotifyService', '$state'
'NotifyService', '$state', 'ReceiptModal'
];

/**
Expand All @@ -16,7 +16,7 @@ CashController.$inject = [
* against previous invoices. The cash payments module provides
* functionality to pay both in multiple currencies.
*/
function CashController(Cash, Cashboxes, AppCache, Currencies, Exchange, Session, Modals, Notify, $state) {
function CashController(Cash, Cashboxes, AppCache, Currencies, Exchange, Session, Modals, Notify, $state, Receipts) {
var vm = this;

// persist cash data across sessions
Expand All @@ -29,8 +29,8 @@ function CashController(Cash, Cashboxes, AppCache, Currencies, Exchange, Session
if (!cashboxId) {
$state.go('^.select', {}, { notify : false });

// if there is no URL id but one in localstorage, the state with the
// localstorage params
// if there is no URL id but one in localstorage, the state with the
// localstorage params
} else if (cashboxId && !$state.params.id) {
$state.go('^.window', { id : cashboxId }, { location: 'replace'});
}
Expand Down Expand Up @@ -79,20 +79,6 @@ function CashController(Cash, Cashboxes, AppCache, Currencies, Exchange, Session
.catch(Notify.handleError);
}

function calculateDisabledIds() {

// collect cashbox ids in an array
var cashboxCurrencyIds = vm.cashbox.currencies.reduce(function (array, currency) {
return array.concat(currency.currency_id);
}, []);

// find all ids that are not cashbox ids, to disable them
vm.disabledCurrencyIds = vm.currencies.reduce(function (array, currency) {
var bool = (cashboxCurrencyIds.indexOf(currency.id) === -1);
return array.concat(bool ? currency.id : []);
}, []);
}

// clears the invoices field whenever the voucher type changes for a better UX
function togglePaymentType() {
delete vm.payment.invoices;
Expand All @@ -113,21 +99,22 @@ function CashController(Cash, Cashboxes, AppCache, Currencies, Exchange, Session
function setCashboxSelection(cashbox) {
vm.cashbox = cashbox;
cache.cashbox = cashbox;
calculateDisabledIds();
vm.disabledCurrencyIds = Cash.calculateDisabledIds(vm.cashbox, vm.currencies);
}

/* Debtor Invoices Modal */
function openInvoicesModal() {
Modals.openDebtorInvoices({ debtorUuid: vm.payment.debtor_uuid, invoices: vm.payment.invoices })
.then(function (result) {
// bind the selected invoices
vm.payment.invoices = result.invoices;

// the table of invoices shown to the client is name-spaced by 'slip'
vm.slip = {};
vm.slip.rawTotal = result.total;
digestExchangeRate();
});
.then(function (result) {

// bind the selected invoices
vm.payment.invoices = result.invoices;

// the table of invoices shown to the client is name-spaced by 'slip'
vm.slip = {};
vm.slip.rawTotal = result.total;
digestExchangeRate();
});
}

// exchanges the payment at the bottom of the previous invoice slip.
Expand Down Expand Up @@ -155,12 +142,7 @@ function CashController(Cash, Cashboxes, AppCache, Currencies, Exchange, Session
// submit the cash payment
return Cash.create(vm.payment)
.then(function (response) {

// open cash receipt
Modals.openPatientReceipt({
uuid: response.uuid,
patientUuid: vm.patient.uuid
});
return Receipts.cash(response.uuid, true);
})
.catch(Notify.handleError);
}
Expand Down
21 changes: 19 additions & 2 deletions client/src/partials/cash/cash.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,40 @@ angular.module('bhima.routes')
.state('cash.select', {
url : '/selection',
params : { id : { value: null } },
onEnter :['$state', '$uibModal', cashboxSelectionModal]
onEnter :['$uibModal', cashboxSelectionModal]
})

.state('cash.window', {
url : '/:id?',
params : { id : { squash: true, value: null } },
controller: 'CashController as CashCtrl',
templateUrl: '/partials/cash/cash.html'
})

.state('cash.transfer', {
url : '/:id/transfer',
params : { id : { squash: true, value: null } },
onEnter :['$state', '$uibModal', transferModal]
});
}]);


function cashboxSelectionModal($state, Modal) {
function cashboxSelectionModal(Modal) {
Modal.open({
templateUrl: 'partials/cash/modals/selectCashbox.modal.html',
controller: 'SelectCashboxModalController as $ctrl',
backdrop: 'static',
keyboard: false
});
}

function transferModal($state, Modal) {
Modal.open({
controller: 'CashTransferModalController as TransferCtrl',
templateUrl: 'partials/cash/modals/transfer.modal.html',
backdrop: 'static',
keyboard: false
});
}


46 changes: 0 additions & 46 deletions client/src/partials/cash/cashboxSelect.html

This file was deleted.

Loading

0 comments on commit ada250d

Please sign in to comment.