-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathcomplex-voucher.ctrl.js
289 lines (242 loc) · 8.25 KB
/
complex-voucher.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
angular.module('bhima.controllers')
.controller('ComplexJournalVoucherController', ComplexJournalVoucherController);
ComplexJournalVoucherController.$inject = [
'VoucherService', 'CurrencyService', 'SessionService', 'NotifyService',
'VoucherToolkitService', 'ReceiptModal', 'bhConstants', 'uiGridConstants',
'VoucherForm', '$timeout', 'ExchangeRateService', 'TransactionTypeService',
];
/**
* @overview ComplexJournalVoucherController
*
* @description
* This module implements complex journal vouchers. It allows users to quickly create transactions by
* specifying two or more lines of transactions and all relative document references
*
* @constructor
*/
function ComplexJournalVoucherController(
Vouchers, Currencies, Session, Notify, Toolkit, Receipts, bhConstants,
uiGridConstants, VoucherForm, $timeout, Rates, TransactionTypes,
) {
const vm = this;
// help to group transaction_types
vm.groupTransactionByType = Vouchers.groupTransactionByType;
// bind constants
vm.bhConstants = bhConstants;
vm.enterprise = Session.enterprise;
vm.itemIncrement = 1;
vm.timestamp = new Date();
// bind the complex voucher form
vm.Voucher = new VoucherForm('ComplexVouchers');
// fired to clear the grid
vm.clear = function clear(form) {
vm.Voucher.clear();
form.$setPristine();
};
// fired on changes
vm.onChanges = function onChanges() {
vm.Voucher.onChanges();
vm.gridApi.core.notifyDataChange(uiGridConstants.dataChange.EDIT);
};
vm.onCurrencyChange = function onCurrencyChange(currency) {
vm.Voucher.handleCurrencyChange(currency.id, true);
vm.gridApi.core.notifyDataChange(uiGridConstants.dataChange.EDIT);
};
// ui-grid options
vm.gridOptions = {
appScopeProvider : vm,
flatEntityAccess : true,
fastWatch : true,
enableSorting : false,
enableColumnMenus : false,
showColumnFooter : true,
virtualizationThreshold : 200,
onRegisterApi,
};
function onRegisterApi(api) {
vm.gridApi = api;
}
vm.openConventionPaymentModal = function openConventionPaymentModal() {
gridManager(Toolkit.openConventionPaymentModal);
};
vm.openPatientFledModal = function openPatientFledModal() {
gridManager(Toolkit.openPatientFledModal);
};
vm.openGenericIncomeModal = function openGenericIncomeModal() {
gridManager(Toolkit.openGenericIncomeModal);
};
vm.openGenericExpenseModal = function openGenericExpenseModal() {
gridManager(Toolkit.openGenericExpenseModal);
};
vm.openCashTransferModal = function openCashTransferModal() {
gridManager(Toolkit.openCashTransferModal);
};
vm.openSupportPatientModal = function openSupportPatientModal() {
gridManager(Toolkit.openSupportPatientModal);
};
vm.openPaymentEmployees = function openPaymentEmployees() {
gridManager(Toolkit.openPaymentEmployees);
};
vm.openAdvancesLoansInstallments = function openAdvancesLoansInstallments() {
gridManager(Toolkit.openAdvancesLoansInstallments);
};
// @TODO fixed me to display correctly selected items(invoices, ..) accounts
// without adding empty items before
function gridManager(modal) {
modal()
.then(result => {
if (!result) {
removeNullRows();
if (vm.Voucher.store.data.length === 0) {
vm.Voucher.addItems(2);
}
return;
}
processVoucherToolRows(result);
});
}
/**
* @function processVoucherToolRows
*
* @description this function handle the result of the tool modal
*/
function processVoucherToolRows(result) {
if (!result) { return; }
// force updating details
vm.Voucher.replaceFormRows(result.rows);
updateView(result);
}
/**
* @method updateView
*
* @description
* this function force to update details of the voucher
* and remove unnecessary rows
*
* @param {object} result
*/
function updateView(result) {
$timeout(() => {
// transaction type
vm.Voucher.details.type_id = result.type_id || vm.Voucher.details.type_id;
// description
vm.Voucher.description(result.description || vm.Voucher.details.description);
// currency
vm.Voucher.details.currency_id = result.currency_id || vm.Voucher.details.currency_id;
removeNullRows();
}, 0);
}
/**
* @function removeNullRows
*
* @description remove null rows
*/
function removeNullRows() {
const gridData = JSON.parse(JSON.stringify(vm.gridOptions.data));
gridData.forEach(item => {
if (!item.account_id) {
vm.Voucher.store.remove(item.uuid);
}
});
}
/** ======================== end voucher tools ======================= */
// bind the startup method as a reset method
vm.submit = submit;
vm.currencySymbol = currencySymbol;
/** Get the selected currency symbol */
function currencySymbol(currencyId) {
if (!currencyId) { return ''; }
return Currencies.symbol(currencyId);
}
/** run the module on startup and refresh */
function startup() {
vm.gridOptions.data = vm.Voucher.store.data;
// NOTE(@jniles) - is this really a good idea?!
// queue the exchange rate download
Rates.read(true);
}
/* ============================= Transaction Type ============================= */
TransactionTypes.read()
.then(types => {
vm.types = types;
})
.catch(Notify.handleError);
/* ============================= Grid ====================================== */
// grid default options
vm.gridOptions.columnDefs = [{
field : 'isValid',
displayName : '...',
cellTemplate : 'modules/vouchers/templates/status.grid.tmpl.html',
aggregationType : uiGridConstants.aggregationTypes.count,
aggregationHideLabel : true,
footerCellClass : 'text-center',
width : 40,
}, {
field : 'account',
displayName : 'FORM.LABELS.ACCOUNT',
headerCellFilter : 'translate',
cellTemplate : 'modules/vouchers/templates/account.grid.tmpl.html',
width : '35%',
}, {
field : 'debit',
displayName : 'FORM.LABELS.DEBIT',
headerCellFilter : 'translate',
cellTemplate : 'modules/vouchers/templates/debit.grid.tmpl.html',
aggregationType : uiGridConstants.aggregationTypes.sum,
aggregationHideLabel : true,
footerCellFilter : 'currency:grid.appScope.Voucher.details.currency_id',
footerCellClass : 'text-right',
}, {
field : 'credit',
displayName : 'FORM.LABELS.CREDIT',
headerCellFilter : 'translate',
cellTemplate : 'modules/vouchers/templates/credit.grid.tmpl.html',
aggregationType : uiGridConstants.aggregationTypes.sum,
aggregationHideLabel : true,
footerCellFilter : 'currency:grid.appScope.Voucher.details.currency_id',
footerCellClass : 'text-right',
}, {
field : 'entity',
displayName : 'FORM.LABELS.DEBTOR_CREDITOR',
headerCellFilter : 'translate',
cellTemplate : 'modules/vouchers/templates/entity.grid.tmpl.html',
}, {
field : 'reference',
displayName : 'FORM.LABELS.REFERENCE',
headerCellFilter : 'translate',
cellTemplate : 'modules/vouchers/templates/reference.grid.tmpl.html',
}, {
field : 'action',
displayName : '...',
width : 25,
cellTemplate : 'modules/vouchers/templates/remove.grid.tmpl.html',
}];
/* ============================= End Grid ================================== */
/** submit data */
function submit(form) {
// stop submission if the form is invalid
if (form.$invalid) {
return Notify.danger('VOUCHERS.COMPLEX.INVALID_VALUES');
}
const valid = vm.Voucher.validate();
if (!valid) {
return Notify.danger(vm.Voucher._error);
}
const voucher = vm.Voucher.details;
voucher.items = vm.Voucher.store.data;
return Vouchers.create(voucher)
.then(result => {
Receipts.voucher(result.uuid, true);
vm.Voucher.clear();
form.$setPristine();
})
.catch(Notify.handleError);
}
vm.onSelectEntity = (row, entity) => {
row.entity.entity_uuid = entity.uuid;
};
vm.onSelectRecord = (row, record) => {
row.entity.document_uuid = record.uuid;
};
startup();
}