Skip to content

Commit

Permalink
fix(cash): fix ui-grid invoice selection
Browse files Browse the repository at this point in the history
This commit fixes the debtor invoice selection on the cash page, by
making sure the `selectInvoices()` is called twice.  This should ensure
that both the grid's API and the invoices are present when the client
attempts to restore previous invoices.
  • Loading branch information
Jonathan Niles authored and jniles committed Jan 9, 2017
1 parent e311f0b commit 88962b9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
7 changes: 1 addition & 6 deletions client/src/js/services/ModalService.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,7 @@ function ModalService(Modal) {
controller : 'CashInvoiceModalController as CashInvoiceModalCtrl',
resolve : {
debtorId : function debtorIdProvider() { return request.debtorUuid; },
invoiceIds : function invoiceIdsProvider() {
if (!request.invoices) { return []; }
return request.invoices.map(function (invoice) {
return invoice.invoice_uuid;
});
}
invoices : function invoicesProvider() { return request.invoices; }
}
});

Expand Down
11 changes: 9 additions & 2 deletions client/src/partials/cash/cash.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function CashController(Cash, Cashboxes, AppCache, Currencies, Exchange, Session
Patient.balance(vm.patient.debtor_uuid)
.then(function (balance) {
/**
* balance < 0 means that enterprise must money to the patient (creditor balance)
* balance < 0 means that enterprise owes money to the patient (creditor balance)
* multiply by -1 means we want work with positive value if the patient has a creditor balance
*/
vm.patientBalance = balance * -1;
Expand All @@ -115,7 +115,13 @@ function CashController(Cash, Cashboxes, AppCache, Currencies, Exchange, Session

/* Debtor Invoices Modal */
function openInvoicesModal() {
Modals.openDebtorInvoices({ debtorUuid: vm.payment.debtor_uuid, invoices: vm.payment.invoices })

var invoices = angular.copy(vm.payment.invoices || [])
.map(function (invoice) {
return invoice.uuid;
});

Modals.openDebtorInvoices({ debtorUuid: vm.payment.debtor_uuid, invoices: invoices })
.then(function (result) {

// bind the selected invoices
Expand Down Expand Up @@ -176,6 +182,7 @@ function CashController(Cash, Cashboxes, AppCache, Currencies, Exchange, Session
}

function setupPayment() {

// load payment type from cache
var DEFAULT_PAYMENT_TYPE = 0;
var paymentType = cache.is_caution || DEFAULT_PAYMENT_TYPE;
Expand Down
48 changes: 31 additions & 17 deletions client/src/partials/cash/modals/invoices.modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ angular.module('bhima.controllers')
.controller('CashInvoiceModalController', CashInvoiceModalController);

CashInvoiceModalController.$inject = [
'DebtorService', 'debtorId', 'invoiceIds', '$uibModalInstance', 'SessionService',
'DebtorService', 'debtorId', 'invoices', '$uibModalInstance', 'SessionService',
'$timeout', 'NotifyService'
];

/**
* @module cash/modals/CashInvoiceModalController
*
* @description This controller is responsible for retrieving a list of debtor invoices
* @description
* This controller is responsible for retrieving a list of debtor invoices
* from the server, and allowing selection of any number of invoices.
*/
function CashInvoiceModalController(Debtors, debtorId, invoiceIds, ModalInstance, Session, $timeout, Notify) {
function CashInvoiceModalController(Debtors, debtorId, invoices, ModalInstance, Session, $timeout, Notify) {
var vm = this;

// we start in a neutral state
Expand Down Expand Up @@ -47,13 +48,33 @@ function CashInvoiceModalController(Debtors, debtorId, invoiceIds, ModalInstance

// in order to use controllerAs syntax, we need to import the entire grid API
// into the controller scope to bind the getSelectedRows method.
function onRegisterApi(api) {
vm.getSelectedRows = api.selection.getSelectedRows;
vm.selectRow = api.selection.selectRow;
function onRegisterApi(gridApi) {
vm.getSelectedRows = gridApi.selection.getSelectedRows;

// set up callbacks
api.selection.on.rowSelectionChanged(null, selectionChangeCallback);
api.selection.on.rowSelectionChangedBatch(null, selectionChangeCallback);
gridApi.selection.on.rowSelectionChanged(null, selectionChangeCallback);
gridApi.selection.on.rowSelectionChangedBatch(null, selectionChangeCallback);

// bind the grid API
vm.gridApi = gridApi;

selectPreviouslySelectedInvoices();
}

// toggles previously selected rows
function selectPreviouslySelectedInvoices() {
if (!vm.gridApi) { return; }

var rows = vm.gridApi.grid.rows;

// loop through each invoice id passed in and reselect those that have
// previously been selected
rows.forEach(function (row) {
console.log('row', row);
if (invoices.indexOf(row.entity.uuid) > -1) {
vm.gridApi.selection.selectRow(row.entity);
}
});
}

// starts up the modal
Expand All @@ -69,15 +90,8 @@ function CashInvoiceModalController(Debtors, debtorId, invoiceIds, ModalInstance

// requires timeout to bind angular ids to each row before selecting them.
$timeout(function () {

// loop through each invoice id passed in and reselect those that have
// previously been selected
vm.gridOptions.data.forEach(function (invoice) {
if (invoiceIds.indexOf(invoice.invoice_uuid) > -1) {
vm.selectRow(invoice);
}
});
});
selectPreviouslySelectedInvoices();
}, 0, false);
})
.catch(function (error) {
vm.hasError = true;
Expand Down

0 comments on commit 88962b9

Please sign in to comment.