/
findPatient.modal.js
89 lines (73 loc) · 2.04 KB
/
findPatient.modal.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
angular.module('bhima.controllers')
.controller('StockFindPatientModalController', StockFindPatientModalController);
StockFindPatientModalController.$inject = [
'$uibModalInstance', 'PatientService', 'NotifyService', 'data', 'AppCache',
'BarcodeService',
];
function StockFindPatientModalController(Instance, Patients, Notify, Data, AppCache, Barcodes) {
const vm = this;
const cache = new AppCache('StockFindPatient');
cache.joinInvoice = cache.joinInvoice || 0;
// join invoice as default behavior
vm.joinInvoice = cache.joinInvoice;
// global
vm.selected = {};
// bind methods
vm.setPatient = setPatient;
vm.setInvoice = setInvoice;
vm.submit = submit;
vm.cancel = cancel;
vm.openBarcodeScanner = openBarcodeScanner;
if (Data.entity_uuid) {
Patients.read(Data.entity_uuid)
.then(patient => {
setPatient(patient);
})
.catch(err => {
if (err.statusCode === 404) {
setPatient({});
} else {
Notify.handleError(err);
}
});
}
// set patient
function setPatient(patient) {
vm.selected = patient;
}
function setInvoice(invoice) {
vm.invoice = invoice;
}
// submit
function submit() {
vm.selected.invoice = vm.invoice;
Instance.close(vm.selected);
}
// cancel
function cancel() {
Instance.close();
}
/**
* @function openBarcodeScanner
*
* @description
* Opens the barcode scanner component and receives the invoice from the
* modal. Sets both the patient and the invoice based on the scan.
*/
function openBarcodeScanner() {
let invoice;
Barcodes.modal()
.then(record => {
invoice = record;
return Patients.read(record.patient_uuid);
})
.then(patient => {
setPatient(patient);
// we need to wait for the bh-find-invoice component to call the setInvoice()
// since the invoice details have to be formatted in a particular way.
vm.joinInvoice = 1;
vm.scannedInvoice = invoice;
})
.catch(angular.noop);
}
}