1 dojo.require('openils.CGI');
2 dojo.require('openils.Util');
3 dojo.require('openils.User');
4 dojo.require('openils.Event');
6 const SET_BARCODE_REGEX = 'opac.barcode_regex';
7 const SET_PATRON_TIMEOUT = 'circ.selfcheck.patron_login_timeout';
8 const SET_ALERT_ON_CHECKOUT_EVENT = 'circ.selfcheck.alert_on_checkout_event';
9 const SET_AUTO_OVERRIDE_EVENTS = 'circ.selfcheck.auto_override_checkout_events';
10 const SET_PATRON_PASSWORD_REQUIRED = 'circ.selfcheck.patron_password_required';
12 function SelfCheckManager() {
14 this.cgi = new openils.CGI();
16 this.workstation = null;
17 this.authtoken = null;
20 this.patronBarcodeRegex = null;
22 // current item barcode
23 this.itemBarcode = null;
25 // are we currently performing a renewal?
26 this.isRenewal = false;
28 // is a transaction pending?
29 this.pendingXact = false;
31 // dict of org unit settings for "here"
32 this.orgSettings = {};
36 * Fetch the org-unit settings, initialize the display, etc.
38 SelfCheckManager.prototype.init = function() {
40 this.staff = openils.User.user;
41 this.workstation = openils.User.workstation;
42 this.authtoken = openils.User.authtoken;
43 this.loadOrgSettings();
45 if(this.cgi.param('patron')) {
46 // Patron barcode via cgi param. Mainly used for debugging.
47 this.loginPatron(this.cgi.param('patron'));
54 * Loads the org unit settings
56 SelfCheckManager.prototype.loadOrgSettings = function() {
58 var settings = fieldmapper.aou.fetchOrgSettingBatch(
62 SET_ALERT_ON_CHECKOUT_EVENT,
63 SET_AUTO_OVERRIDE_EVENTS,
69 this.orgSettings[k] = settings[k].value;
72 if(settings[SET_BARCODE_REGEX])
73 this.patronBarcodeRegex = new RegExp(settings[SET_BARCODE_REGEX].value);
76 SelfCheckManager.prototype.drawLoginPage = function() {
79 var bcHandler = function(barcode) {
80 // handle patron barcode entry
82 if(self.orgSettings[SET_PATRON_PASSWORD_REQUIRED]) {
84 // password is required. wire up the scan box to read it
86 'Please enter your password', // TODO i18n
88 function(pw) { self.loginPatron(barcode, ps); }
91 dojo.connect(selfckScanBox, 'onKeyDown', pwHandler);
94 // password is not required, go ahead and login
95 self.loginPatron(barcode);
100 'Please log in with your library barcode.', // TODO
109 SelfCheckManager.prototype.loginPatron = function(barcode, passwd) {
111 if(this.orgSettings[SET_PATRON_PASSWORD_REQUIRED]) {
113 // patron password is required. Verify it.
115 var res = fieldmapper.standardRequest(
116 ['open-ils.actor', 'open-ils.actor.verify_user_password'],
117 {params : [this.authtoken, barcode, null, hex_md5(passwd)]}
121 return alert('login failed'); // TODO
125 // retrieve the fleshed user by barcode
126 this.patron = fieldmapper.standardRequest(
127 ['open-ils.actor', 'open-ils.actor.user.fleshed.retrieve_by_barcode'],
128 {params : [this.authtoken, barcode]}
131 var evt = openils.Event.parse(this.patron);
134 // User login failed, why?
136 switch(evt.textcode) {
138 case 'ACTOR_USER_NOT_FOUND':
139 return alert('user not found'); // TODO
142 return alert('staff login timed out'); // TODO
145 return alert('unexpected patron login error occured: ' + evt.textcode); // TODO
149 // patron login succeeded
150 dojo.byId('oils-selfck-user-banner').innerHTML = 'Welcome, ' + this.patron.usrname(); // TODO i18n
156 * Manages the main input box
157 * @param str The context message to display with the box
158 * @param clearOnly Don't update the context message, just clear the value and re-focus
159 * @param handler Optional "on-enter" handler.
161 SelfCheckManager.prototype.updateScanBox = function(str, clearOnly, handler) {
164 dojo.byId('oils-selfck-scan-text').innerHTML = str;
165 selfckScanBox.attr('value', '');
166 selfckScanBox.focus();
169 dojo.connect(selfckScanBox, 'onKeyDown',
171 if(e.keyCode != dojo.keys.ENTER)
173 handler(selfckScanBox.attr('value'));
180 * Sets up the checkout/renewal interface
182 SelfCheckManager.prototype.drawCircPage = function() {
186 'Please enter an item barcode', // TODO i18n
188 function(barcode) { self.checkout(barcode); }
191 openils.Util.show('oils-selfck-circ-page');
193 this.circTbody = dojo.byId('oils-selfck-circ-tbody');
194 if(!this.circTemplate)
195 this.circTemplate = this.circTbody.removeChild(dojo.byId('oils-selfck-circ-row'));
201 * Check out a single item. If the item is already checked
202 * out to the patron, redirect to renew()
204 SelfCheckManager.prototype.checkout = function(barcode, override) {
207 this.updateScanbox(null, true);
211 // TODO see if it's a patron barcode
212 // TODO see if this item has already been checked out in this session
214 var method = 'open-ils.circ.checkout.full';
215 if(override) method += '.override';
217 var result = fieldmapper.standardRequest(
218 ['open-ils.circ', 'open-ils.circ.checkout.full'],
221 patron_id : this.patron.id(),
222 copy_barcode : barcode
228 if(dojo.isArray(result)) {
229 // list of results. See if we can override all of them.
232 var evt = openils.Event.parse(result);
234 switch(evt.textcode) {
235 // standard result events
238 this.displayCheckout(evt);
241 case 'OPEN_CIRCULATION_EXISTS':
251 console.log("Circ resulted in " + js2JSON(result));
257 SelfCheckManager.prototype.renew = function() {
261 * Display the result of a checkout or renewal in the items out table
263 SelfCheckManager.prototype.displayCheckout = function(evt) {
264 var copy = evt.payload.copy;
265 var record = evt.payload.record;
266 var circ = evt.payload.circ;
267 var row = this.circTemplate.cloneNode(true);
271 var pic = $n(template, 'jacket');
272 pic.setAttribute('src', '/opac/ac/jacket/small/' + cleanISBN(record.isbn()));
276 this.byName('barcode', row).innerHTML = copy.barcode();
277 this.byName('title', row).innerHTML = record.title();
278 this.byName('author', row).innerHTML = record.author();
279 this.circTbody.appendChild(row);
283 SelfCheckManager.prototype.byName = function(node, name) {
284 return dojo.query('[name=' + name+']', node)[0];
290 SelfCheckManager.prototype.printReceipt = function() {
294 * Build the patron holds table
296 SelfCheckManager.prototype.displayHolds = function() {
301 * Logout the patron and return to the login page
303 SelfCheckManager.prototype.logoutPatron = function() {
308 * Fire up the manager on page load
310 openils.Util.addOnLoad(
312 new SelfCheckManager().init();