1 /* -----------------------------------------------------------------
2 * Copyright (C) 2008 Equinox Software, Inc.
3 * Bill Erickson <erickson@esilibrary.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 ----------------------------------------------------------------- */
21 var STAFF_SES_PARAM = 'ses';
22 var PATRON_BARCODE_COOKIE = 'pbcc';
24 var itemBarcode = null;
25 var itemsOutTemplate = null;
26 var isRenewal = false;
27 var pendingXact = false;
28 var patronTimeout = 600000; /* 10 minutes */
32 var successfulItems = {};
33 var scanTimeout = 800;
35 var patronBarcodeRegex;
39 var alertOnCheckoutEvent = false;
40 var overrideCircEvents = [];
41 var SET_PATRON_TIMEOUT = 'circ.selfcheck.patron_login_timeout';
42 var SET_BARCODE_REGEX = 'opac.barcode_regex';
43 var SET_ALERT_ON_CHECKOUT_EVENT = 'circ.selfcheck.alert_on_checkout_event';
44 var SET_AUTO_OVERRIDE_EVENTS = 'circ.selfcheck.auto_override_checkout_events';
47 function selfckInit() {
49 var staff = grabUser(cookieManager.read(STAFF_SES_PARAM) || cgi.param(STAFF_SES_PARAM));
53 orgUnit = findOrgUnitSN(cgi.param('l')) || globalOrgTree;
54 selfckFetchOrgDetails();
56 // fetch the relevent org-unit setting
57 var settings = fetchBatchOrgSetting(orgUnit.id(),
58 [SET_PATRON_TIMEOUT, SET_BARCODE_REGEX, SET_ALERT_ON_CHECKOUT_EVENT, SET_AUTO_OVERRIDE_EVENTS]);
59 if(settings[SET_PATRON_TIMEOUT])
60 patronTimeout = parseInt(settings[SET_PATRON_TIMEOUT].value) * 1000;
61 if(settings[SET_BARCODE_REGEX])
62 patronBarcodeRegex = new RegExp(settings[SET_BARCODE_REGEX].value);
63 if(settings[SET_ALERT_ON_CHECKOUT_EVENT])
64 alertOnCheckoutEvent = (settings[SET_ALERT_ON_CHECKOUT_EVENT].value) ? true : false;
65 if(settings[SET_AUTO_OVERRIDE_EVENTS])
66 overrideCircEvents = settings[SET_AUTO_OVERRIDE_EVENTS].value;
69 // should not happen when behind the proxy
70 return alert('Staff must login');
73 unHideMe($('selfck-patron-login-container'));
74 $('selfck-patron-login-input').focus();
76 $('selfck-patron-login-input').onkeypress = function(evt) {
77 if(userPressedEnter(evt))
81 $('selfck-item-barcode-input').onkeypress = selfckItemBarcodeKeypress;
83 // for debugging, allow passing the user barcode via param
84 var urlbc = new CGI().param('patron');
86 selfckPatronLogin(urlbc);
90 printWrapper = $('selfck-print-items-list');
91 printTemplate = printWrapper.removeChild($n(printWrapper, 'selfck-print-items-template'));
92 itemsOutTemplate = $('selfck-items-out-tbody').removeChild($('selfck-items-out-row'));
94 selfckTryPatronCookie();
96 // selfckMkDummyCirc(); // testing only
100 function selfckFetchOrgDetails() {
101 var hreq = new Request('open-ils.actor:open-ils.actor.org_unit.hours_of_operation.retrieve', G.user.session, orgUnit.id());
102 hreq.callback(function(r) { orgUnitHours = r.getResultObject(); });
105 var areq = new Request('open-ils.actor:open-ils.actor.org_unit.address.retrieve', orgUnit.mailing_address());
106 areq.callback(function(r) { orgUnitAddress = r.getResultObject(); });
110 function selfckSetupPrinter() {
111 try { // Mozilla only
112 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
113 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
114 netscape.security.PrivilegeManager.enablePrivilege('UniversalPreferencesRead');
115 netscape.security.PrivilegeManager.enablePrivilege('UniversalPreferencesWrite');
116 var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
118 pref.setBoolPref('print.always_print_silent', true);
124 function selfckTryPatronCookie() {
125 var pb = cookieManager.read(PATRON_BARCODE_COOKIE);
127 cookieManager.write(PATRON_BARCODE_COOKIE, '');
128 $('selfck-patron-login-input').value = pb;
134 function selfckItemBarcodeKeypress(evt) {
135 if(userPressedEnter(evt)) {
136 clearTimeout(scanTimeoutId);
139 /* If scanTimeout milliseconds have passed and there is
140 still data in the input box, it's a likely indication
141 of a partial scan. Select the text so the next scan
142 will replace the value */
143 clearTimeout(scanTimeoutId);
144 scanTimeoutId = setTimeout(
146 if($('selfck-item-barcode-input').value) {
147 $('selfck-item-barcode-input').select();
156 * Start the logout timer
158 function selfckStartTimer() {
159 timerId = setTimeout(
161 selfckLogoutPatron();
169 * Reset the logout timer
171 function selfckResetTimer() {
172 clearTimeout(timerId);
177 * Clears fields and "logs out" the patron by reloading the page
179 function selfckLogoutPatron() {
180 $('selfck-item-barcode-input').value = ''; // prevent browser caching
181 $('selfck-patron-login-input').value = '';
183 var numItems = selfckPrint();
184 var sleepTime = 1000;
185 if(numItems > 0) sleepTime += (numItems / 2) * 1000;
187 function() { location.href = location.href; },
188 sleepTime // give the browser time to send the page to the printer
194 * Fetches the user by barcode and displays the item barcode input
197 function selfckPatronLogin(barcode) {
198 barcode = barcode || $('selfck-patron-login-input').value;
201 var bcReq = new Request(
202 'open-ils.actor:open-ils.actor.user.fleshed.retrieve_by_barcode',
203 G.user.session, barcode);
205 bcReq.request.alertEvent = false;
207 bcReq.callback(function(r) {
208 patron = r.getResultObject();
209 if(checkILSEvent(patron)) {
210 if(patron.textcode == 'ACTOR_USER_NOT_FOUND') {
211 unHideMe($('selfck-patron-not-found'));
212 $('selfck-patron-login-input').select();
216 if(patron.textcode == 'NO_SESSION')
217 return selfckLogoutStaff();
219 return alert(patron.textcode);
222 if(!isTrue(patron.active())) {
223 unHideMe($('selfck-patron-not-found'));
224 $('selfck-patron-login-input').select();
228 $('selfck-patron-login-input').value = ''; // reset the input
229 hideMe($('selfck-patron-login-container'));
230 unHideMe($('selfck-patron-checkout-container'));
231 $('selfck-patron-name-span').appendChild(text(patron.usrname()));
232 $('selfck-item-barcode-input').focus();
238 function selfckLogoutStaff() {
239 cookieManager.remove(STAFF_SES_PARAM);
240 location.reload(true);
244 * If a user barcode was scanned into the item barcode
245 * input, log out the current user and log in the new user
247 function selfckCheckPatronBarcode(itemBc) {
248 if(patronBarcodeRegex) {
249 if(itemBc.match(patronBarcodeRegex)) {
250 cookieManager.write(PATRON_BARCODE_COOKIE, itemBc, -1);
251 selfckLogoutPatron();
259 * Sends the checkout request
261 function selfckCheckout(override) {
262 if(pendingXact) return;
267 removeChildren($('selfck-event-time'));
268 removeChildren($('selfck-event-span'));
270 itemBarcode = $('selfck-item-barcode-input').value;
271 if(!itemBarcode) return;
273 if(selfckCheckPatronBarcode(itemBarcode))
276 if (itemBarcode in successfulItems) {
277 selfckShowMsgNode({textcode:'dupe-barcode'});
278 $('selfck-item-barcode-input').select();
283 var coReq = new Request(
284 'open-ils.circ:open-ils.circ.checkout.full' + ((override) ? '.override' : ''),
285 G.user.session, {patron_id:patron.id(), copy_barcode:itemBarcode});
287 coReq.request.alertEvent = false;
288 coReq.callback(selfckHandleCoResult);
293 * Handles the resulting event. If the item is already checked out,
294 * attempts renewal. Any other events will display a message
296 function selfckHandleCoResult(r) {
300 evt = r.getResultObject();
303 selfckShowMsgNode({textcode:'UNKNOWN'});
304 appendClear($('selfck-errors'), text(E.toString()));
308 if(evt.textcode == 'SUCCESS') {
309 selfckDislplayCheckout(evt);
310 selfckShowMsgNode(evt);
311 successfulItems[itemBarcode] = 1;
313 } else if(evt.textcode == 'OPEN_CIRCULATION_EXISTS') {
316 } else if(evt.textcode == 'NO_SESSION') {
318 return selfckLogoutStaff();
323 if(!evt.length) evt = [evt];
324 if(overrideCircEvents.length) {
326 // see if the events we received are all in the list of
327 // events to override
329 for(var i = 0; i < evt.length; i++) {
330 var match = overrideCircEvents.filter(
331 function(e) { return (e == evt[i].textcode); })[0];
339 return selfckCheckout(true);
342 selfckShowMsgNode(evt);
343 $('selfck-item-barcode-input').select();
348 * Display event text in the messages block
350 function selfckShowMsgNode(evt) {
351 var code = evt.textcode;
352 var msgspan = $('selfck-event-span');
354 // if we're not explicitly handling the event, just say "copy cannot circ"
355 if(!$('selfck-event-' + code))
356 code = 'COPY_CIRC_NOT_ALLOWED';
358 appendClear($('selfck-event-time'), text(new Date().toLocaleString()));
359 appendClear($('selfck-event-span'), text($('selfck-event-' + code).innerHTML));
361 if(code != 'SUCCESS' && alertOnCheckoutEvent)
362 alert($('selfck-event-' + code).innerHTML);
366 * Renders a row in the checkouts table for the current circulation
368 function selfckDislplayCheckout(evt) {
369 unHideMe($('selfck-items-out-table-wrapper'));
371 var template = itemsOutTemplate.cloneNode(true);
372 var copy = evt.payload.copy;
373 var record = evt.payload.record;
374 var circ = evt.payload.circ;
377 var pic = $n(template, 'selfck.jacket');
378 pic.setAttribute('src', '../ac/jacket/small/'+cleanISBN(record.isbn()));
380 $n(template, 'selfck.barcode').appendChild(text(copy.barcode()));
381 $n(template, 'selfck.title').appendChild(text(record.title()));
382 $n(template, 'selfck.author').appendChild(text(record.author()));
383 $n(template, 'selfck.due_date').appendChild(text(circ.due_date().replace(/T.*/,'')));
384 $n(template, 'selfck.remaining').appendChild(text(circ.renewal_remaining()));
386 hideMe($n(template, 'selfck.cotype_co'));
387 unHideMe($n(template, 'selfck.cotype_rn'));
390 var tbody = $('selfck-items-out-tbody');
391 tbody.insertBefore(template, tbody.getElementsByTagName('tr')[0]);
392 $('selfck-item-barcode-input').value = '';
395 // flesh out the printable version of the page as well
396 var ptemplate = printTemplate.cloneNode(true);
397 $n(ptemplate, 'title').appendChild(text(record.title()));
398 $n(ptemplate, 'barcode').appendChild(text(copy.barcode()));
399 $n(ptemplate, 'due_date').appendChild(text(circ.due_date().replace(/T.*/,'')));
400 printWrapper.insertBefore(ptemplate, printWrapper.getElementsByTagName('li')[0]);
406 * Checks to see if this item is checked out to the current patron. If so,
407 * this sends the renewal request.
409 function selfckRenew() {
411 // first, make sure the item is checked out to this patron
412 var detailReq = new Request(
413 'open-ils.circ:open-ils.circ.copy_details.retrieve.barcode',
414 G.user.session, itemBarcode);
418 var details = r.getResultObject();
419 if(details.circ.usr() == patron.id()) {
420 // OK, this is our item, renew it
422 var rnReq = new Request(
423 'open-ils.circ:open-ils.circ.renew',
424 G.user.session, {copy_barcode:itemBarcode});
425 rnReq.request.alertEvent = false;
426 rnReq.callback(selfckHandleCoResult);
430 selfckShowMsgNode({textcode:'already-out'});
439 * Sets the print date and prints the page
441 function selfckPrint() {
444 for(var x in successfulItems)
448 hideMe($('selfck-patron-checkout-container'));
449 unHideMe($('selfck-print-queuing'));
450 appendClear($('selfck-print-date'), text(new Date().toLocaleString()));
451 appendClear($('selfck-print-lib-name'), text(orgUnit.name()));
453 appendClear($('selfck-print-lib-addr-street'), text(orgUnitAddress.street1()+' '+orgUnitAddress.street2()));
454 appendClear($('selfck-print-lib-addr-city'), text(orgUnitAddress.city()));
455 appendClear($('selfck-print-lib-addr-state'), text(orgUnitAddress.state()));
456 appendClear($('selfck-print-lib-addr-post-code'), text(orgUnitAddress.post_code()));
458 appendClear($('selfck-print-lname'), text(patron.family_name()));
459 appendClear($('selfck-print-fname'), text(patron.first_given_name()));
460 appendClear($('selfck-print-lib-phone'), text(orgUnit.phone()));
462 for(var i in [0, 1, 2, 3, 4, 5, 6]) {
463 appendClear($('selfck-print-dow_'+i+'_open'), text(orgUnitHours['dow_'+i+'_open']()));
464 appendClear($('selfck-print-dow_'+i+'_close'), text(orgUnitHours['dow_'+i+'_close']()));
475 * Test method for generating dummy data in the checkout tables
477 function selfckMkDummyCirc() {
478 unHideMe($('selfck-items-out-table'));
480 var template = itemsOutTemplate.cloneNode(true);
481 $n(template, 'selfck.barcode').appendChild(text('123456789'));
482 $n(template, 'selfck.title').appendChild(text('Test Title'));
483 $n(template, 'selfck.author').appendChild(text('Test Author'));
484 $n(template, 'selfck.due_date').appendChild(text('2008-08-01'));
485 $n(template, 'selfck.remaining').appendChild(text('1'));
486 $('selfck-items-out-tbody').appendChild(template);
488 // flesh out the printable version of the page as well
489 var ptemplate = printTemplate.cloneNode(true);
490 $n(ptemplate, 'title').appendChild(text('Test Title'));
491 $n(ptemplate, 'barcode').appendChild(text('123456789'));
492 $n(ptemplate, 'due_date').appendChild(text('2008-08-01'));
493 printWrapper.appendChild(ptemplate);