diff --git a/client/src/js/components/bhFindPatient.js b/client/src/js/components/bhFindPatient.js index 1bb4346fb8..cd8a494821 100644 --- a/client/src/js/components/bhFindPatient.js +++ b/client/src/js/components/bhFindPatient.js @@ -67,7 +67,7 @@ function FindPatientComponent(Patients, AppCache, Notify) { // call the onRegisterApi() callback with the vm.onRegisterApi({ - api : { reset : vm.reset } + api : { reset : vm.reset, searchByUuid : searchByUuid } }); }; @@ -83,6 +83,26 @@ function FindPatientComponent(Patients, AppCache, Notify) { vm.reset = reset; vm.onKeyPress = onKeyPress; + /** + * @method searchByUuid + * + * @public + * + * @param {String} uuid - the patient's UUID to be loaded programmatically from by an + * API call. + * + * @description + * This method exists to be called from the bhFindPatient API, initializing the component + * with a patient's uuid. + */ + function searchByUuid(uuid) { + Patients.read(uuid) + .then(function (patient) { + selectPatient(patient); + }) + .catch(Notify.handleError); + } + /** * @method searchByReference * @@ -102,10 +122,10 @@ function FindPatientComponent(Patients, AppCache, Notify) { // query the patient's search endpoint for the // reference Patients.search(options) - .then(function (patients) { - selectPatient(patients[0]); - }) - .catch(Notify.handleError); + .then(function (patients) { + selectPatient(patients[0]); + }) + .catch(Notify.handleError); } /** diff --git a/client/src/js/services/BarcodeService.js b/client/src/js/services/BarcodeService.js new file mode 100644 index 0000000000..883e0be727 --- /dev/null +++ b/client/src/js/services/BarcodeService.js @@ -0,0 +1,18 @@ +angular.module('bhima.services') + .service('BarcodeService', BarcodeService); + +BarcodeService.$inject = [ '$http', 'util' ]; + +function BarcodeService($http, util) { + var service = this; + + // TODO - barcode redirection + service.redirect = angular.noop; + + service.search = function search(code) { + return $http.get('/barcode/'.concat(code)) + .then(util.unwrapHTTPResponse); + }; + + return service; +} diff --git a/client/src/partials/cash/cash.js b/client/src/partials/cash/cash.js index ad451fac3d..7fa79bb6d6 100644 --- a/client/src/partials/cash/cash.js +++ b/client/src/partials/cash/cash.js @@ -23,7 +23,7 @@ function CashController(Cash, Cashboxes, AppCache, Currencies, Exchange, Session var cache = AppCache('CashPayments'); /* id of the currently select cashbox */ - var cashboxId = $state.params.id || (cache.cashbox && cache.cashbox.id) || null; + var cashboxId = $state.params.id || (cache.cashbox && cache.cashbox.id); // if no id, re-route to 'cash.select' if (!cashboxId) { diff --git a/client/src/partials/cash/cash.routes.js b/client/src/partials/cash/cash.routes.js index 1c4b4d5af7..52c73fc57d 100644 --- a/client/src/partials/cash/cash.routes.js +++ b/client/src/partials/cash/cash.routes.js @@ -34,8 +34,14 @@ angular.module('bhima.routes') params : { id : { squash: true, value: null } }, onEnter :['$state', '$uibModal', transferModal], onExit : ['$uibModalStack', closeModal] - }); + }) + .state('cash.scan', { + url : '/:id/scan', + params : { id : { squash: true, value: null } }, + onEnter :['$state', '$uibModal', scanBarcodeModal], + onExit : ['$uibModalStack', closeModal] + }); }]); @@ -57,8 +63,15 @@ function transferModal($state, Modal) { }); } +function scanBarcodeModal($state, Modal) { + Modal.open({ + controller: 'CashBarcodeScannerModalController as CashBarCtrl', + templateUrl: 'partials/cash/modals/scanBarcode.modal.html', + backdrop: 'static', + keyboard: true + }); +} + function closeModal($uibModalStack) { $uibModalStack.dismissAll(); } - - diff --git a/client/src/partials/cash/modals/scanBarcode.modal.html b/client/src/partials/cash/modals/scanBarcode.modal.html new file mode 100644 index 0000000000..2706a839ad --- /dev/null +++ b/client/src/partials/cash/modals/scanBarcode.modal.html @@ -0,0 +1,47 @@ +
+ + + + +
diff --git a/client/src/partials/cash/modals/scanBarcode.modal.js b/client/src/partials/cash/modals/scanBarcode.modal.js new file mode 100644 index 0000000000..910d568e65 --- /dev/null +++ b/client/src/partials/cash/modals/scanBarcode.modal.js @@ -0,0 +1,83 @@ +angular.module('bhima.controllers') + .controller('CashBarcodeScannerModalController', CashBarController); + +CashBarController.$inject = [ + '$state', 'CashboxService', 'NotifyService', 'BarcodeService', 'PatientService' +]; + +/** + * @module cash/modals/CashBarController + * + * @description + * This controller is responsible for scanning barcodes and then configuring the CashForm with the barcode +*/ +function CashBarController($state, Cashboxes, Notify, Barcodes, Patients) { + var vm = this; + var id = $state.params.id; + + vm.triggerBarcodeRead = triggerBarcodeRead; + + vm.loading = true; + + vm.INITIALIZED = 'initialized'; + vm.LOADING = 'loading'; + vm.READ_ERROR = 'read-error'; + vm.READ_SUCCESS = 'found'; + + // the first step is initialized + vm.step = vm.INITILIZED; + + // determine if the input was a valid barcode + function isValidBarcode(input) { + return false; + } + + function triggerBarcodeRead() { + + console.log('TriggerBarcodeRead with:', vm.barcode); + + if (!isValidBarcode(vm.barcode)) { + vm.step = vm.READ_ERROR; + } else { + searchForBarcode(vm.barcode); + } + } + + // send an HTTP request based on the barcode to get the invoice in question, + // then load the patient information in the background + function searchForBarcode(barcode) { + + // set the loading step + vm.step = vm.LOADING; + + Barcodes.search(barcode) + .then(function (invoice) { + vm.invoice = invoice; + return Patients.search({ debtor_uuid : invoice.debtor_uuid }); + }).then(function (patients) { + + // destructure search array + var patient = patients[0]; + + vm.patient = patient; + + // signal + vm.step = vm.READ_SUCCESS; + }) + .catch(function (error) { + vm.step = vm.READ_ERROR; + }); + } + + // fired on state startup + function startup() { + + Cashboxes.read(id) + .then(function (cashbox) { + vm.cashbox = cashbox; + }) + .catch(Notify.handleError); + } + + startup(); +} diff --git a/test/integration/patients.js b/test/integration/patients.js index 64cfb230d1..a07ef48139 100644 --- a/test/integration/patients.js +++ b/test/integration/patients.js @@ -4,7 +4,7 @@ const helpers = require('./helpers'); const q = require('q'); -describe.only('(/patients) Patients', function () { +describe('(/patients) Patients', function () { 'use strict'; const patientUuid = '81af634f-321a-40de-bc6f-ceb1167a9f65';