1 dojo.require('dojo.date.locale');
2 dojo.require('dojo.date.stamp');
3 dojo.require('openils.CGI');
4 dojo.require('openils.Util');
5 dojo.require('openils.User');
6 dojo.require('openils.Event');
7 dojo.require('openils.widget.ProgressDialog');
9 dojo.requireLocalization('openils.circ', 'selfcheck');
10 var localeStrings = dojo.i18n.getLocalization('openils.circ', 'selfcheck');
13 const SET_BARCODE_REGEX = 'opac.barcode_regex';
14 const SET_PATRON_TIMEOUT = 'circ.selfcheck.patron_login_timeout';
15 const SET_ALERT_ON_CHECKOUT_EVENT = 'circ.selfcheck.alert_on_checkout_event';
16 const SET_AUTO_OVERRIDE_EVENTS = 'circ.selfcheck.auto_override_checkout_events';
17 const SET_PATRON_PASSWORD_REQUIRED = 'circ.selfcheck.patron_password_required';
19 function SelfCheckManager() {
21 this.cgi = new openils.CGI();
23 this.workstation = null;
24 this.authtoken = null;
27 this.patronBarcodeRegex = null;
29 // current item barcode
30 this.itemBarcode = null;
32 // are we currently performing a renewal?
33 this.isRenewal = false;
35 // is a transaction pending?
36 this.pendingXact = false;
38 // dict of org unit settings for "here"
39 this.orgSettings = {};
42 // Construct a mock checkout for debugging purposes
43 if(this.mockCheckouts = this.cgi.param('mock-circ')) {
47 record : new fieldmapper.mvr(),
48 copy : new fieldmapper.acp(),
49 circ : new fieldmapper.circ()
53 this.mockCheckout.payload.record.title('Jazz improvisation for guitar');
54 this.mockCheckout.payload.record.author('Wise, Les');
55 this.mockCheckout.payload.record.isbn('0634033565');
56 this.mockCheckout.payload.copy.barcode('123456789');
57 this.mockCheckout.payload.circ.renewal_remaining(1);
58 this.mockCheckout.payload.circ.parent_circ(1);
59 this.mockCheckout.payload.circ.due_date('2012-12-21');
66 * Fetch the org-unit settings, initialize the display, etc.
68 SelfCheckManager.prototype.init = function() {
70 this.staff = openils.User.user;
71 this.workstation = openils.User.workstation;
72 this.authtoken = openils.User.authtoken;
73 this.loadOrgSettings();
77 // connect onclick handlers to the various navigation links
79 'oils-selfck-hold-details-link' : function() { self.drawHoldsPage(); },
80 'oils-selfck-nav-holds' : function() { self.drawHoldsPage(); },
81 'oils-selfck-pay-fines-link' : function() { self.drawFinesPage(); },
82 'oils-selfck-nav-fines' : function() { self.drawFinesPage(); },
83 'oils-selfck-nav-home' : function() { self.drawCircPage(); },
84 'oils-selfck-nav-logout' : function() { self.logoutPatron(); }
87 for(var id in linkHandlers)
88 dojo.connect(dojo.byId(id), 'onclick', linkHandlers[id]);
91 if(this.cgi.param('patron')) {
93 // Patron barcode via cgi param. Mainly used for debugging and
94 // only works if password is not required by policy
95 this.loginPatron(this.cgi.param('patron'));
103 * Loads the org unit settings
105 SelfCheckManager.prototype.loadOrgSettings = function() {
107 var settings = fieldmapper.aou.fetchOrgSettingBatch(
108 this.staff.ws_ou(), [
111 SET_ALERT_ON_CHECKOUT_EVENT,
112 SET_AUTO_OVERRIDE_EVENTS,
118 this.orgSettings[k] = settings[k].value;
121 if(settings[SET_BARCODE_REGEX])
122 this.patronBarcodeRegex = new RegExp(settings[SET_BARCODE_REGEX].value);
125 SelfCheckManager.prototype.drawLoginPage = function() {
128 var bcHandler = function(barcode) {
129 // handle patron barcode entry
131 if(self.orgSettings[SET_PATRON_PASSWORD_REQUIRED]) {
133 // password is required. wire up the scan box to read it
135 msg : 'Please enter your password', // TODO i18n
136 handler : function(pw) { self.loginPatron(barcode, ps); }
139 dojo.connect(selfckScanBox, 'onKeyDown', pwHandler);
142 // password is not required, go ahead and login
143 self.loginPatron(barcode);
148 msg : 'Please log in with your library barcode.', // TODO
156 SelfCheckManager.prototype.loginPatron = function(barcode, passwd) {
158 if(this.orgSettings[SET_PATRON_PASSWORD_REQUIRED]) {
160 // patron password is required. Verify it.
162 var res = fieldmapper.standardRequest(
163 ['open-ils.actor', 'open-ils.actor.verify_user_password'],
164 {params : [this.authtoken, barcode, null, hex_md5(passwd)]}
168 return alert('login failed'); // TODO
172 // retrieve the fleshed user by barcode
173 this.patron = fieldmapper.standardRequest(
174 ['open-ils.actor', 'open-ils.actor.user.fleshed.retrieve_by_barcode'],
175 {params : [this.authtoken, barcode]}
178 var evt = openils.Event.parse(this.patron);
181 // User login failed, why?
183 switch(evt.textcode) {
185 case 'ACTOR_USER_NOT_FOUND':
186 return alert('user not found'); // TODO
189 return alert('staff login timed out'); // TODO
192 return alert('unexpected patron login error occured: ' + evt.textcode); // TODO
196 // patron login succeeded
197 dojo.byId('oils-selfck-user-banner').innerHTML = 'Welcome, ' + this.patron.usrname(); // TODO i18n
203 * Manages the main input box
204 * @param msg The context message to display with the box
205 * @param clearOnly Don't update the context message, just clear the value and re-focus
206 * @param handler Optional "on-enter" handler.
208 SelfCheckManager.prototype.updateScanBox = function(args) {
210 selfckScanBox.attr('value', '');
213 selfckScanBox.attr('value', args.value);
216 dojo.byId('oils-selfck-scan-text').innerHTML = args.msg;
218 if(selfckScanBox._lastHandler && (args.handler || args.clearHandler)) {
219 dojo.disconnect(selfckScanBox._lastHandler);
223 selfckScanBox._lastHandler = dojo.connect(
227 if(e.keyCode != dojo.keys.ENTER)
229 args.handler(selfckScanBox.attr('value'));
234 selfckScanBox.focus();
238 * Sets up the checkout/renewal interface
240 SelfCheckManager.prototype.drawCircPage = function() {
244 msg : 'Please enter an item barcode', // TODO i18n
245 handler : function(barcode) { self.checkout(barcode); }
248 openils.Util.hide('oils-selfck-payment-page');
249 openils.Util.hide('oils-selfck-holds-page');
250 openils.Util.show('oils-selfck-circ-page');
252 this.circTbody = dojo.byId('oils-selfck-circ-tbody');
253 if(!this.circTemplate)
254 this.circTemplate = this.circTbody.removeChild(dojo.byId('oils-selfck-circ-row'));
256 // items out, holds, and fines summaries
259 fieldmapper.standardRequest(
260 ['open-ils.actor', 'open-ils.actor.user.fines.summary'],
262 params : [this.authtoken, this.patron.id()],
263 oncomplete : function(r) {
264 var summary = openils.Util.readResponse(r);
265 dojo.byId('oils-selfck-fines-total').innerHTML =
266 dojo.string.substitute(
267 localeStrings.TOTAL_FINES_ACCOUNT,
268 [summary.balance_owed()]
275 this.updateHoldsSummary();
278 this.updateCircSummary();
280 // render mock checkouts for debugging?
281 if(this.mockCheckouts) {
282 for(var i in [1,2,3])
283 this.displayCheckout(this.mockCheckout);
287 SelfCheckManager.prototype.updateHoldsSummary = function(decrement) {
289 if(!this.holdsSummary) {
290 var summary = fieldmapper.standardRequest(
291 ['open-ils.circ', 'open-ils.circ.holds.user_summary'],
292 {params : [this.authtoken, this.patron.id()]}
295 this.holdsSummary = {};
296 this.holdsSummary.ready = Number(summary['4']);
297 this.holdsSummary.total = 0;
299 for(var i in summary)
300 this.holdsSummary.total += Number(summary[i]);
304 this.holdsSummary.ready -= 1;
306 dojo.byId('oils-selfck-holds-total').innerHTML =
307 dojo.string.substitute(
308 localeStrings.TOTAL_HOLDS,
309 [this.holdsSummary.total]
312 dojo.byId('oils-selfck-holds-ready').innerHTML =
313 dojo.string.substitute(
314 localeStrings.HOLDS_READY_FOR_PICKUP,
315 [this.holdsSummary.ready]
320 SelfCheckManager.prototype.updateCircSummary = function(increment) {
322 if(!this.circSummary) {
324 var summary = fieldmapper.standardRequest(
325 ['open-ils.actor', 'open-ils.actor.user.checked_out.count'],
326 {params : [this.authtoken, this.patron.id()]}
330 total : Number(summary.out) + Number(summary.overdue),
331 overdue : Number(summary.overdue),
337 // local checkout occurred. Add to the total and the session.
338 this.circSummary.total += 1;
339 this.circSummary.session += 1;
342 dojo.byId('oils-selfck-circ-account-total').innerHTML =
343 dojo.string.substitute(
344 localeStrings.TOTAL_ITEMS_ACCOUNT,
345 [this.circSummary.total]
348 dojo.byId('oils-selfck-circ-session-total').innerHTML =
349 dojo.string.substitute(
350 localeStrings.TOTAL_ITEMS_SESSION,
351 [this.circSummary.session]
356 SelfCheckManager.prototype.drawHoldsPage = function() {
358 // TODO add option to hid scanBox
359 // this.updateScanBox(...)
361 openils.Util.hide('oils-selfck-circ-page');
362 openils.Util.hide('oils-selfck-payment-page');
363 openils.Util.show('oils-selfck-holds-page');
365 this.holdTbody = dojo.byId('oils-selfck-hold-tbody');
366 if(!this.holdTemplate)
367 this.holdTemplate = this.holdTbody.removeChild(dojo.byId('oils-selfck-hold-row'));
368 while(this.holdTbody.childNodes[0])
369 this.holdTbody.removeChild(this.holdTbody.childNodes[0]);
371 progressDialog.show(true);
374 fieldmapper.standardRequest( // fetch the hold IDs
375 ['open-ils.circ', 'open-ils.circ.holds.id_list.retrieve'],
377 params : [this.authtoken, this.patron.id()],
379 oncomplete : function(r) {
380 var ids = openils.Util.readResponse(r);
381 if(!ids || ids.length == 0) return;
383 fieldmapper.standardRequest( // fetch the hold objects with fleshed details
384 ['open-ils.circ', 'open-ils.circ.hold.details.batch.retrieve.atomic'],
386 params : [self.authtoken, ids],
387 oncomplete : function(rr) {
388 self.drawHolds(openils.Util.readResponse(rr));
398 * Fetch and add a single hold to the list of holds
400 SelfCheckManager.prototype.drawHolds = function(holds) {
403 // sort available holds to the top of the list
404 // followed by queue position order
406 if(a.status == 4) return -1;
407 if(a.queue_position < b.queue_position) return -1;
412 progressDialog.hide();
414 for(var i in holds) {
417 var row = this.holdTemplate.cloneNode(true);
419 if(data.mvr.isbn()) {
420 this.byName(row, 'jacket').setAttribute('src', '/opac/extras/ac/jacket/small/' + data.mvr.isbn());
423 this.byName(row, 'title').innerHTML = data.mvr.title();
424 this.byName(row, 'author').innerHTML = data.mvr.author();
426 if(data.status == 4) {
428 // hold is ready for pickup
429 this.byName(row, 'status').innerHTML = localeStrings.HOLD_STATUS_READY;
433 // hold is still pending
434 this.byName(row, 'status').innerHTML =
435 dojo.string.substitute(
436 localeStrings.HOLD_STATUS_WAITING,
437 [data.queue_position, data.potential_copies]
441 this.holdTbody.appendChild(row);
448 * Check out a single item. If the item is already checked
449 * out to the patron, redirect to renew()
451 SelfCheckManager.prototype.checkout = function(barcode, override) {
454 this.updateScanbox(null, true);
458 if(this.mockCheckouts) {
459 // if we're in mock-checkout mode, just insert another
460 // fake circ into the table and get out of here.
461 this.displayCheckout(this.mockCheckout);
465 // TODO see if it's a patron barcode
466 // TODO see if this item has already been checked out in this session
468 var method = 'open-ils.circ.checkout.full';
469 if(override) method += '.override';
471 var result = fieldmapper.standardRequest(
472 ['open-ils.circ', 'open-ils.circ.checkout.full'],
475 patron_id : this.patron.id(),
476 copy_barcode : barcode
482 if(dojo.isArray(result)) {
483 // list of results. See if we can override all of them.
486 var evt = openils.Event.parse(result);
488 switch(evt.textcode) {
489 // standard result events
492 this.displayCheckout(evt);
495 case 'OPEN_CIRCULATION_EXISTS':
505 console.log("Circ resulted in " + js2JSON(result));
511 SelfCheckManager.prototype.renew = function() {
515 * Display the result of a checkout or renewal in the items out table
517 SelfCheckManager.prototype.displayCheckout = function(evt) {
519 var copy = evt.payload.copy;
520 var record = evt.payload.record;
521 var circ = evt.payload.circ;
522 var row = this.circTemplate.cloneNode(true);
525 this.byName(row, 'jacket').setAttribute('src', '/opac/extras/ac/jacket/small/' + record.isbn());
528 this.byName(row, 'barcode').innerHTML = copy.barcode();
529 this.byName(row, 'title').innerHTML = record.title();
530 this.byName(row, 'author').innerHTML = record.author();
531 this.byName(row, 'remaining').innerHTML = circ.renewal_remaining();
533 var date = dojo.date.stamp.fromISOString(circ.due_date());
534 this.byName(row, 'due_date').innerHTML =
535 dojo.date.locale.format(date, {selector : 'date'});
537 // put new circs at the top of the list
538 this.circTbody.insertBefore(row, this.circTbody.getElementsByTagName('tr')[0]);
542 SelfCheckManager.prototype.byName = function(node, name) {
543 return dojo.query('[name=' + name+']', node)[0];
547 SelfCheckManager.prototype.drawFinesPage = function() {
549 openils.Util.hide('oils-selfck-circ-page');
550 openils.Util.hide('oils-selfck-holds-page');
551 openils.Util.show('oils-selfck-payment-page');
558 SelfCheckManager.prototype.printReceipt = function() {
563 * Logout the patron and return to the login page
565 SelfCheckManager.prototype.logoutPatron = function() {
570 * Fire up the manager on page load
572 openils.Util.addOnLoad(
574 new SelfCheckManager().init();