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_AUTO_OVERRIDE_EVENTS = 'circ.selfcheck.auto_override_checkout_events';
16 const SET_PATRON_PASSWORD_REQUIRED = 'circ.selfcheck.patron_password_required';
17 const SET_AUTO_RENEW_INTERVAL = 'circ.checkout_auto_renew_age';
18 const SET_WORKSTATION_REQUIRED = 'circ.selfcheck.workstation_required';
19 const SET_ALERT_POPUP = 'circ.selfcheck.alert.popup';
20 const SET_ALERT_SOUND = 'circ.selfcheck.alert.sound';
22 function SelfCheckManager() {
24 this.cgi = new openils.CGI();
26 this.workstation = null;
27 this.authtoken = null;
30 this.patronBarcodeRegex = null;
34 // During renewals, keep track of the ID of the previous circulation.
35 // Previous circ is used for tracking failed renewals (for receipts).
38 // current item barcode
39 this.itemBarcode = null;
41 // are we currently performing a renewal?
42 this.isRenewal = false;
44 // dict of org unit settings for "here"
45 this.orgSettings = {};
47 // Construct a mock checkout for debugging purposes
48 if(this.mockCheckouts = this.cgi.param('mock-circ')) {
52 record : new fieldmapper.mvr(),
53 copy : new fieldmapper.acp(),
54 circ : new fieldmapper.circ()
58 this.mockCheckout.payload.record.title('Jazz improvisation for guitar');
59 this.mockCheckout.payload.record.author('Wise, Les');
60 this.mockCheckout.payload.record.isbn('0634033565');
61 this.mockCheckout.payload.copy.barcode('123456789');
62 this.mockCheckout.payload.circ.renewal_remaining(1);
63 this.mockCheckout.payload.circ.parent_circ(1);
64 this.mockCheckout.payload.circ.due_date('2012-12-21');
71 * Fetch the org-unit settings, initialize the display, etc.
73 SelfCheckManager.prototype.init = function() {
75 this.staff = openils.User.user;
76 this.workstation = openils.User.workstation;
77 this.authtoken = openils.User.authtoken;
78 this.loadOrgSettings();
80 this.circTbody = dojo.byId('oils-selfck-circ-tbody');
81 this.itemsOutTbody = dojo.byId('oils-selfck-circ-out-tbody');
83 // workstation is required but none provided
84 if(this.orgSettings[SET_WORKSTATION_REQUIRED] && !this.workstation) {
85 alert(dojo.string.substitute(localeStrings.WORKSTATION_REQUIRED));
90 // connect onclick handlers to the various navigation links
92 'oils-selfck-hold-details-link' : function() { self.drawHoldsPage(); },
93 //'oils-selfck-nav-holds' : function() { self.drawHoldsPage(); },
94 'oils-selfck-pay-fines-link' : function() { self.drawFinesPage(); },
95 //'oils-selfck-nav-fines' : function() { self.drawFinesPage(); },
96 'oils-selfck-nav-home' : function() { self.drawCircPage(); },
97 'oils-selfck-nav-logout' : function() { self.logoutPatron(); },
98 'oils-selfck-nav-logout-print' : function() { self.logoutPatron(true); },
99 'oils-selfck-items-out-details-link' : function() { self.drawItemsOutPage(); }
102 for(var id in linkHandlers)
103 dojo.connect(dojo.byId(id), 'onclick', linkHandlers[id]);
106 if(this.cgi.param('patron')) {
108 // Patron barcode via cgi param. Mainly used for debugging and
109 // only works if password is not required by policy
110 this.loginPatron(this.cgi.param('patron'));
113 this.drawLoginPage();
117 * To test printing, pass a URL param of 'testprint'. The value for the param
118 * should be a JSON string like so: [{circ:<circ_id>}, ...]
120 var testPrint = this.cgi.param('testprint');
122 this.checkouts = JSON2js(testPrint);
129 * Loads the org unit settings
131 SelfCheckManager.prototype.loadOrgSettings = function() {
133 var settings = fieldmapper.aou.fetchOrgSettingBatch(
134 this.staff.ws_ou(), [
139 SET_AUTO_OVERRIDE_EVENTS,
140 SET_PATRON_PASSWORD_REQUIRED,
141 SET_AUTO_RENEW_INTERVAL,
142 SET_WORKSTATION_REQUIRED
148 this.orgSettings[k] = settings[k].value;
151 if(settings[SET_BARCODE_REGEX])
152 this.patronBarcodeRegex = new RegExp(settings[SET_BARCODE_REGEX].value);
155 SelfCheckManager.prototype.drawLoginPage = function() {
158 var bcHandler = function(barcode) {
159 // handle patron barcode entry
161 if(self.orgSettings[SET_PATRON_PASSWORD_REQUIRED]) {
163 // password is required. wire up the scan box to read it
165 msg : 'Please enter your password', // TODO i18n
166 handler : function(pw) { self.loginPatron(barcode, pw); },
171 // password is not required, go ahead and login
172 self.loginPatron(barcode);
177 msg : 'Please log in with your library barcode.', // TODO
185 SelfCheckManager.prototype.loginPatron = function(barcode, passwd) {
187 if(this.orgSettings[SET_PATRON_PASSWORD_REQUIRED]) {
190 // would only happen in dev/debug mode when using the patron= param
191 alert('password required by org setting. remove patron= from URL');
195 // patron password is required. Verify it.
197 var res = fieldmapper.standardRequest(
198 ['open-ils.actor', 'open-ils.actor.verify_user_password'],
199 {params : [this.authtoken, barcode, null, hex_md5(passwd)]}
203 // user-not-found results in login failure
205 dojo.string.substitute(localeStrings.LOGIN_FAILED, [barcode]),
206 false, 'login-failure'
208 this.drawLoginPage();
213 // retrieve the fleshed user by barcode
214 this.patron = fieldmapper.standardRequest(
215 ['open-ils.actor', 'open-ils.actor.user.fleshed.retrieve_by_barcode'],
216 {params : [this.authtoken, barcode]}
219 var evt = openils.Event.parse(this.patron);
222 dojo.string.substitute(localeStrings.LOGIN_FAILED, [barcode]),
223 false, 'login-failure'
225 this.drawLoginPage();
229 this.handleAlert('', false, 'login-success');
230 dojo.byId('oils-selfck-user-banner').innerHTML = 'Welcome, ' + this.patron.usrname(); // TODO i18n
236 SelfCheckManager.prototype.handleAlert = function(message, shouldPopup, sound) {
238 console.log("Handling alert " + message);
240 dojo.byId('oils-selfck-status-div').innerHTML = message;
242 if(shouldPopup && this.orgSettings[SET_ALERT_POPUP])
245 if(sound && this.orgSettings[SET_ALERT_SOUND])
246 openils.Util.playAudioUrl(SelfCheckManager.audioConfig[sound]);
251 * Manages the main input box
252 * @param msg The context message to display with the box
253 * @param clearOnly Don't update the context message, just clear the value and re-focus
254 * @param handler Optional "on-enter" handler.
256 SelfCheckManager.prototype.updateScanBox = function(args) {
260 selfckScanBox.domNode.select();
262 selfckScanBox.attr('value', '');
266 selfckScanBox.domNode.setAttribute('type', 'password');
268 selfckScanBox.domNode.setAttribute('type', '');
272 selfckScanBox.attr('value', args.value);
275 dojo.byId('oils-selfck-scan-text').innerHTML = args.msg;
277 if(selfckScanBox._lastHandler && (args.handler || args.clearHandler)) {
278 dojo.disconnect(selfckScanBox._lastHandler);
282 selfckScanBox._lastHandler = dojo.connect(
286 if(e.keyCode != dojo.keys.ENTER)
288 args.handler(selfckScanBox.attr('value'));
293 selfckScanBox.focus();
297 * Sets up the checkout/renewal interface
299 SelfCheckManager.prototype.drawCircPage = function() {
301 openils.Util.show('oils-selfck-circ-tbody');
303 while(this.itemsOutTbody.childNodes[0])
304 this.itemsOutTbody.removeChild(this.itemsOutTbody.childNodes[0]);
308 msg : 'Please enter an item barcode', // TODO i18n
309 handler : function(barcode) { self.checkout(barcode); }
312 openils.Util.hide('oils-selfck-payment-page');
313 openils.Util.hide('oils-selfck-holds-page');
314 openils.Util.show('oils-selfck-circ-page');
316 if(!this.circTemplate)
317 this.circTemplate = this.circTbody.removeChild(dojo.byId('oils-selfck-circ-row'));
319 // items out, holds, and fines summaries
322 fieldmapper.standardRequest(
323 ['open-ils.actor', 'open-ils.actor.user.fines.summary'],
325 params : [this.authtoken, this.patron.id()],
326 oncomplete : function(r) {
327 var summary = openils.Util.readResponse(r);
328 dojo.byId('oils-selfck-fines-total').innerHTML =
329 dojo.string.substitute(
330 localeStrings.TOTAL_FINES_ACCOUNT,
331 [summary.balance_owed()]
338 this.updateHoldsSummary();
341 this.updateCircSummary();
343 // render mock checkouts for debugging?
344 if(this.mockCheckouts) {
345 for(var i in [1,2,3])
346 this.displayCheckout(this.mockCheckout, 'checkout');
350 SelfCheckManager.prototype.drawItemsOutPage = function() {
351 openils.Util.hide('oils-selfck-circ-tbody');
352 openils.Util.hide('oils-selfck-payment-page');
353 openils.Util.hide('oils-selfck-holds-page');
354 openils.Util.show('oils-selfck-circ-page');
356 while(this.itemsOutTbody.childNodes[0])
357 this.itemsOutTbody.removeChild(this.itemsOutTbody.childNodes[0]);
359 progressDialog.show(true);
362 fieldmapper.standardRequest(
363 ['open-ils.circ', 'open-ils.circ.actor.user.checked_out.atomic'],
366 params : [this.authtoken, this.patron.id()],
367 oncomplete : function(r) {
369 var resp = openils.Util.readResponse(r);
371 var circs = resp.sort(
373 if(a.circ.due_date() > b.circ.due_date())
379 progressDialog.hide();
383 self.displayCheckout(
385 (circ.circ.parent_circ()) ? 'renew' : 'checkout',
395 SelfCheckManager.prototype.updateHoldsSummary = function() {
397 if(!this.holdsSummary) {
398 var summary = fieldmapper.standardRequest(
399 ['open-ils.circ', 'open-ils.circ.holds.user_summary'],
400 {params : [this.authtoken, this.patron.id()]}
403 this.holdsSummary = {};
404 this.holdsSummary.ready = Number(summary['4']);
405 this.holdsSummary.total = 0;
407 for(var i in summary)
408 this.holdsSummary.total += Number(summary[i]);
411 dojo.byId('oils-selfck-holds-total').innerHTML =
412 dojo.string.substitute(
413 localeStrings.TOTAL_HOLDS,
414 [this.holdsSummary.total]
417 dojo.byId('oils-selfck-holds-ready').innerHTML =
418 dojo.string.substitute(
419 localeStrings.HOLDS_READY_FOR_PICKUP,
420 [this.holdsSummary.ready]
425 SelfCheckManager.prototype.updateCircSummary = function(increment) {
427 if(!this.circSummary) {
429 var summary = fieldmapper.standardRequest(
430 ['open-ils.actor', 'open-ils.actor.user.checked_out.count'],
431 {params : [this.authtoken, this.patron.id()]}
435 total : Number(summary.out) + Number(summary.overdue),
436 overdue : Number(summary.overdue),
442 // local checkout occurred. Add to the total and the session.
443 this.circSummary.total += 1;
444 this.circSummary.session += 1;
447 dojo.byId('oils-selfck-circ-account-total').innerHTML =
448 dojo.string.substitute(
449 localeStrings.TOTAL_ITEMS_ACCOUNT,
450 [this.circSummary.total]
453 dojo.byId('oils-selfck-circ-session-total').innerHTML =
454 dojo.string.substitute(
455 localeStrings.TOTAL_ITEMS_SESSION,
456 [this.circSummary.session]
461 SelfCheckManager.prototype.drawHoldsPage = function() {
463 // TODO add option to hid scanBox
464 // this.updateScanBox(...)
466 openils.Util.hide('oils-selfck-circ-page');
467 openils.Util.hide('oils-selfck-payment-page');
468 openils.Util.show('oils-selfck-holds-page');
470 this.holdTbody = dojo.byId('oils-selfck-hold-tbody');
471 if(!this.holdTemplate)
472 this.holdTemplate = this.holdTbody.removeChild(dojo.byId('oils-selfck-hold-row'));
473 while(this.holdTbody.childNodes[0])
474 this.holdTbody.removeChild(this.holdTbody.childNodes[0]);
476 progressDialog.show(true);
479 fieldmapper.standardRequest( // fetch the hold IDs
481 ['open-ils.circ', 'open-ils.circ.holds.id_list.retrieve'],
483 params : [this.authtoken, this.patron.id()],
485 oncomplete : function(r) {
486 var ids = openils.Util.readResponse(r);
487 if(!ids || ids.length == 0) {
488 progressDialog.hide();
492 fieldmapper.standardRequest( // fetch the hold objects with fleshed details
493 ['open-ils.circ', 'open-ils.circ.hold.details.batch.retrieve.atomic'],
495 params : [self.authtoken, ids],
497 oncomplete : function(rr) {
498 self.drawHolds(openils.Util.readResponse(rr));
508 * Fetch and add a single hold to the list of holds
510 SelfCheckManager.prototype.drawHolds = function(holds) {
513 // sort available holds to the top of the list
514 // followed by queue position order
516 if(a.status == 4) return -1;
517 if(a.queue_position < b.queue_position) return -1;
522 progressDialog.hide();
524 for(var i in holds) {
527 var row = this.holdTemplate.cloneNode(true);
529 if(data.mvr.isbn()) {
530 this.byName(row, 'jacket').setAttribute('src', '/opac/extras/ac/jacket/small/' + data.mvr.isbn());
533 this.byName(row, 'title').innerHTML = data.mvr.title();
534 this.byName(row, 'author').innerHTML = data.mvr.author();
536 if(data.status == 4) {
538 // hold is ready for pickup
539 this.byName(row, 'status').innerHTML = localeStrings.HOLD_STATUS_READY;
543 // hold is still pending
544 this.byName(row, 'status').innerHTML =
545 dojo.string.substitute(
546 localeStrings.HOLD_STATUS_WAITING,
547 [data.queue_position, data.potential_copies]
551 this.holdTbody.appendChild(row);
558 * Check out a single item. If the item is already checked
559 * out to the patron, redirect to renew()
561 SelfCheckManager.prototype.checkout = function(barcode, override) {
563 this.prevCirc = null;
566 this.updateScanbox(null, true);
570 if(this.mockCheckouts) {
571 // if we're in mock-checkout mode, just insert another
572 // fake circ into the table and get out of here.
573 this.displayCheckout(this.mockCheckout, 'checkout');
577 // TODO see if it's a patron barcode
578 // TODO see if this item has already been checked out in this session
580 var method = 'open-ils.circ.checkout.full';
581 if(override) method += '.override';
583 console.log("Checkout out item " + barcode + " with method " + method);
585 var result = fieldmapper.standardRequest(
586 ['open-ils.circ', 'open-ils.circ.checkout.full'],
589 patron_id : this.patron.id(),
590 copy_barcode : barcode
595 console.log(js2JSON(result));
597 var stat = this.handleXactResult('checkout', barcode, result);
600 this.checkout(barcode, true);
601 } else if(stat.renew) {
607 SelfCheckManager.prototype.handleXactResult = function(action, item, result) {
609 var displayText = '';
611 // If true, the display message is important enough to pop up. Whether or not
612 // an alert() actually occurs, depends on org unit settings
617 // TODO handle lost/missing/etc checkin+checkout override steps
619 var payload = result.payload || {};
621 if(result.textcode == 'NO_SESSION') {
623 return this.logoutStaff();
625 } else if(result.textcode == 'SUCCESS') {
627 if(action == 'checkout') {
629 displayText = dojo.string.substitute(localeStrings.CHECKOUT_SUCCESS, [item]);
630 this.displayCheckout(result, 'checkout');
632 if(payload.holds_fulfilled && payload.holds_fulfilled.length) {
633 // A hold was fulfilled, update the hold numbers in the circ summary
634 console.log("fulfilled hold " + payload.holds_fulfilled + " during checkout");
635 this.holdsSummary = null;
636 this.updateHoldsSummary();
639 this.updateCircSummary(true);
641 } else if(action == 'renew') {
643 displayText = dojo.string.substitute(localeStrings.RENEW_SUCCESS, [item]);
644 this.displayCheckout(result, 'renew');
647 this.checkouts.push({circ : result.payload.circ.id()});
648 sound = 'checkout-success';
649 this.updateScanBox();
651 } else if(result.textcode == 'OPEN_CIRCULATION_EXISTS' && action == 'checkout') {
653 // Server says the item is already checked out. If it's checked out to the
654 // current user, we may need to renew it.
656 if(payload.old_circ) {
659 old_circ refers to the previous checkout IFF it's for the same user.
660 If no auto-renew interval is not defined, assume we should renew it
661 If an auto-renew interval is defined and the payload comes back with
662 auto_renew set to true, do the renewal. Otherwise, let the patron know
663 the item is already checked out to them. */
665 if( !this.orgSettings[SET_AUTO_RENEW_INTERVAL] ||
666 (this.orgSettings[SET_AUTO_RENEW_INTERVAL] && payload.auto_renew) ) {
667 this.prevCirc = payload.old_circ.id();
668 return { renew : true };
672 sound = 'checkout-failure';
673 displayText = dojo.string.substitute(localeStrings.ALREADY_OUT, [item]);
677 // item is checked out to some other user
679 sound = 'checkout-failure';
680 displayText = dojo.string.substitute(localeStrings.OPEN_CIRCULATION_EXISTS, [item]);
683 this.updateScanBox({select:true});
687 var overrideEvents = this.orgSettings[SET_AUTO_OVERRIDE_EVENTS];
689 if(overrideEvents && overrideEvents.length) {
691 // see if the events we received are all in the list of
692 // events to override
694 if(!result.length) result = [result];
697 for(var i = 0; i < result.length; i++) {
698 var match = overrideEvents.filter(
699 function(e) { return (e == result[i].textcode); })[0];
707 return { override : true };
710 this.updateScanBox({select : true});
712 sound = 'checkout-failure';
714 if(action == 'renew')
715 this.checkouts.push({circ : this.prevCirc, renewal_failure : true});
720 switch(result.textcode) {
722 // TODO custom handler for blocking penalties
724 case 'MAX_RENEWALS_REACHED' :
725 displayText = dojo.string.substitute(
726 localeStrings.MAX_RENEWALS, [item]);
729 case 'ITEM_NOT_CATALOGED' :
730 displayText = dojo.string.substitute(
731 localeStrings.ITEM_NOT_CATALOGED, [item]);
734 case 'OPEN_CIRCULATION_EXISTS' :
735 displayText = dojo.string.substitute(
736 localeStrings.OPEN_CIRCULATION_EXISTS, [item]);
740 console.error('Unhandled event ' + result.textcode);
742 if(action == 'checkout' || action == 'renew') {
743 displayText = dojo.string.substitute(
744 localeStrings.GENERIC_CIRC_FAILURE, [item]);
746 displayText = dojo.string.substitute(
747 localeStrings.UNKNOWN_ERROR, [result.textcode]);
752 this.handleAlert(displayText, popup, sound);
760 SelfCheckManager.prototype.renew = function(barcode, override) {
762 var method = 'open-ils.circ.renew';
763 if(override) method += '.override';
765 console.log("Renewing item " + barcode + " with method " + method);
767 var result = fieldmapper.standardRequest(
768 ['open-ils.circ', method],
771 patron_id : this.patron.id(),
772 copy_barcode : barcode
777 console.log(js2JSON(result));
779 var stat = this.handleXactResult('renew', barcode, result);
782 this.renew(barcode, true);
786 * Display the result of a checkout or renewal in the items out table
788 SelfCheckManager.prototype.displayCheckout = function(evt, type, itemsOut) {
790 var copy = evt.payload.copy;
791 var record = evt.payload.record;
792 var circ = evt.payload.circ;
793 var row = this.circTemplate.cloneNode(true);
796 this.byName(row, 'jacket').setAttribute('src', '/opac/extras/ac/jacket/small/' + record.isbn());
799 this.byName(row, 'barcode').innerHTML = copy.barcode();
800 this.byName(row, 'title').innerHTML = record.title();
801 this.byName(row, 'author').innerHTML = record.author();
802 this.byName(row, 'remaining').innerHTML = circ.renewal_remaining();
803 openils.Util.show(this.byName(row, type));
805 var date = dojo.date.stamp.fromISOString(circ.due_date());
806 this.byName(row, 'due_date').innerHTML =
807 dojo.date.locale.format(date, {selector : 'date'});
809 // put new circs at the top of the list
810 var tbody = this.circTbody;
811 if(itemsOut) tbody = this.itemsOutTbody;
812 tbody.insertBefore(row, tbody.getElementsByTagName('tr')[0]);
816 SelfCheckManager.prototype.byName = function(node, name) {
817 return dojo.query('[name=' + name+']', node)[0];
821 SelfCheckManager.prototype.drawFinesPage = function() {
822 openils.Util.hide('oils-selfck-circ-page');
823 openils.Util.hide('oils-selfck-holds-page');
824 openils.Util.show('oils-selfck-payment-page');
828 SelfCheckManager.prototype.initPrinter = function() {
829 try { // Mozilla only
830 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
831 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
832 netscape.security.PrivilegeManager.enablePrivilege('UniversalPreferencesRead');
833 netscape.security.PrivilegeManager.enablePrivilege('UniversalPreferencesWrite');
834 var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
836 pref.setBoolPref('print.always_print_silent', true);
838 console.log("Unable to initialize auto-printing");
845 SelfCheckManager.prototype.printReceipt = function(callback) {
848 var circCtx = []; // circ context data. in this case, renewal_failure info
850 // collect the circs and failure info
854 circIds.push(blob.circ);
855 circCtx.push({renewal_failure:blob.renewal_failure});
863 'format.selfcheck.checkout',
870 fieldmapper.standardRequest(
871 ['open-ils.circ', 'open-ils.circ.fire_circ_trigger_events'],
875 oncomplete : function(r) {
876 var resp = openils.Util.readResponse(r);
877 var output = resp.template_output();
879 self.printData(output.data(), self.checkouts.length, callback);
881 var error = resp.error_output();
883 throw new Error("Error creating receipt: " + error.data());
885 throw new Error("No receipt data returned from server");
893 SelfCheckManager.prototype.printData = function(data, numItems, callback) {
895 var win = window.open('', '', 'resizable,width=700,height=500,scrollbars=1');
896 win.document.body.innerHTML = data;
900 * There is no way to know when the browser is done printing.
901 * Make a best guess at when to close the print window by basing
902 * the setTimeout wait on the number of items to be printed plus
905 var sleepTime = 1000;
907 sleepTime += (numItems / 2) * 1000;
911 win.close(); // close the print window
913 callback(); // fire optional post-print callback
922 * Logout the patron and return to the login page
924 SelfCheckManager.prototype.logoutPatron = function(print) {
925 if(print && this.checkouts.length) {
928 location.href = location.href;
932 location.href = location.href;
938 * Fire up the manager on page load
940 openils.Util.addOnLoad(
942 new SelfCheckManager().init();