Skip to content

Commit

Permalink
feat(barcodes): implement barcode scanning modal
Browse files Browse the repository at this point in the history
This commit implements the barcode scanning modal for the cash payments
page.  This commit only adds the rough HTML and JS outline - it is a
work in progress.
  • Loading branch information
Jonathan Niles authored and sfount committed Jan 14, 2017
1 parent 6a275a1 commit 228678a
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 10 deletions.
30 changes: 25 additions & 5 deletions client/src/js/components/bhFindPatient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
});
};

Expand All @@ -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
*
Expand All @@ -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);
}

/**
Expand Down
18 changes: 18 additions & 0 deletions client/src/js/services/BarcodeService.js
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion client/src/partials/cash/cash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 16 additions & 3 deletions client/src/partials/cash/cash.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
});
}]);


Expand All @@ -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();
}


47 changes: 47 additions & 0 deletions client/src/partials/cash/modals/scanBarcode.modal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<form name="BarcodeForm" bh-form-defaults novalidate style="min-height:400px;">

<div class="modal-header">
<ol class="headercrumb">
<li class="static">{{ ::CashBarCtrl.cashbox.label }}</li>
<li class="title" translate>CASH.BARCODE.TITLE</li>
</ol>
</div>

<div class="modal-body text-center">

<!-- state: awaiting barcode input -->
<h1 class="text-muted">
Scan Barcode <br />
<i class="fa fa-barcode fa-3x"></i>
</h1>

<p>
<span class="text-muted" ng-if="vm.step === vm.INITIALIZED">
<span>Waiting for Input</span>
</span>

<span class="text-primary" ng-if="vm.step === vm.LOADING">
<i class="fa fa-info-circle"></i>
<span>Barcode Read! Looking up the Invoice ...</span>
</span>

<span class="text-success" ng-if="vm.step === vm.READ_SUCCESS">
<i class="fa fa-check-circle-o"></i> Found Invoice IV.HEV.12!
</span>

<span class="text-danger" ng-if="vm.step === vm.READ_ERROR">
<i class="fa fa-danger-sign"></i> Unreadable Barcode! Please enter the barcode manually.
</span>
</p>

<!-- state: gathering required information -->
<h1 hidden>
<i class="fa fa-barcode"></i> Scanned Invoice Barcode!
<br />
<small>Gathering required information...</small>
</h1>

<!-- hidden barcode input -->
<input ng-model="CashBarCtrl.barcode" ng-change="CashBarCtrl.triggerBarcodeRead()" hidden autofocus>
</div>
</form>
83 changes: 83 additions & 0 deletions client/src/partials/cash/modals/scanBarcode.modal.js
Original file line number Diff line number Diff line change
@@ -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();
}
2 changes: 1 addition & 1 deletion test/integration/patients.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 228678a

Please sign in to comment.