-
Notifications
You must be signed in to change notification settings - Fork 105
/
invoice-modal.ctrl.js
126 lines (100 loc) · 3.41 KB
/
invoice-modal.ctrl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
angular.module('bhima.controllers')
.controller('CashInvoiceModalController', CashInvoiceModalController);
CashInvoiceModalController.$inject = [
'DebtorService', 'SessionService', '$timeout', 'NotifyService', '$state',
'$rootScope', '$uibModalInstance',
];
/**
* @module cash/modals/CashInvoiceModalController
*
* @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, Session, $timeout, Notify, $state, $rootScope, Instance) {
var vm = this;
var debtorId = $state.params.debtor_uuid;
var invoices = $state.params.invoices;
vm.$params = $state.params;
// defaults to value
vm.missingId = !angular.isDefined(debtorId);
// bind methods
vm.submit = submit;
vm.cancel = Instance.dismiss;
vm.gridOptions = {
appScopeProvider : vm,
multiSelect : true,
fastWatch : true,
flatEntityAccess : true,
onRegisterApi : onRegisterApi,
enableColumnMenus : false,
columnDefs : [
{ name: 'reference' },
{ name: 'balance', cellFilter: 'currency:' + Session.enterprise.currencyId},
{ name: 'date', cellFilter: 'date' },
],
minRowsToShow : 10,
};
function selectionChangeCallback() {
vm.rows = vm.getSelectedRows();
}
// 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(gridApi) {
vm.getSelectedRows = gridApi.selection.getSelectedRows;
// set up callbacks
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) {
if (invoices.indexOf(row.entity.uuid) > -1) {
vm.gridApi.selection.selectRow(row.entity);
}
});
}
// starts up the modal
function startup() {
// start up the loading indicator
toggleLoadingState();
// load debtor invoices
Debtors.invoices(debtorId, { balanced : 0 })
.then(function (invoices) {
vm.gridOptions.data = invoices;
// requires timeout to bind angular ids to each row before selecting them.
$timeout(function () {
selectPreviouslySelectedInvoices();
}, 0, false);
})
.catch(function (error) {
vm.hasError = true;
Notify.handleError(error);
})
.finally(toggleLoadingState);
}
/* toggles loading state (boolean) */
function toggleLoadingState() {
vm.loading = !vm.loading;
}
// resolve the modal with the selected invoices to add to the cash payment bills
function submit() {
var invoices;
// we start in a neutral state
vm.loading = false;
vm.hasError = false;
// retrieve the outstanding patient invoices from the ui grid
invoices = vm.getSelectedRows();
$rootScope.$broadcast('cash:configure', { invoices: invoices });
return Instance.close();
}
// start up the module
startup();
}