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 SET_PATRON_TIMEOUT = 'circ.selfcheck.patron_login_timeout';
41 var SET_BARCODE_REGEX = 'opac.barcode_regex';
42 var SET_ALERT_ON_CHECKOUT_EVENT = 'circ.selfcheck.alert_on_checkout_event';
45 function selfckInit() {
47 var staff = grabUser(cookieManager.read(STAFF_SES_PARAM) || cgi.param(STAFF_SES_PARAM));
51 orgUnit = findOrgUnitSN(cgi.param('l')) || globalOrgTree;
52 selfckFetchOrgDetails();
54 // fetch the relevent org-unit setting
55 var settings = fetchBatchOrgSetting(orgUnit.id(),
56 [SET_PATRON_TIMEOUT, SET_BARCODE_REGEX, SET_ALERT_ON_CHECKOUT_EVENT]);
57 if(settings[SET_PATRON_TIMEOUT])
58 patronTimeout = parseInt(settings[SET_PATRON_TIMEOUT].value) * 1000;
59 if(settings[SET_BARCODE_REGEX])
60 patronBarcodeRegex = new RegExp(settings[SET_BARCODE_REGEX].value);
61 if(settings[SET_ALERT_ON_CHECKOUT_EVENT])
62 alertOnCheckoutEvent = (settings[SET_ALERT_ON_CHECKOUT_EVENT].value) ? true : false;
65 // should not happen when behind the proxy
66 return alert('Staff must login');
69 unHideMe($('selfck-patron-login-container'));
70 $('selfck-patron-login-input').focus();
72 $('selfck-patron-login-input').onkeypress = function(evt) {
73 if(userPressedEnter(evt))
77 $('selfck-item-barcode-input').onkeypress = selfckItemBarcodeKeypress;
79 // for debugging, allow passing the user barcode via param
80 var urlbc = new CGI().param('patron');
82 selfckPatronLogin(urlbc);
86 printWrapper = $('selfck-print-items-list');
87 printTemplate = printWrapper.removeChild($n(printWrapper, 'selfck-print-items-template'));
88 itemsOutTemplate = $('selfck-items-out-tbody').removeChild($('selfck-items-out-row'));
90 selfckTryPatronCookie();
92 // selfckMkDummyCirc(); // testing only
96 function selfckFetchOrgDetails() {
97 var hreq = new Request('open-ils.actor:open-ils.actor.org_unit.hours_of_operation.retrieve', G.user.session, orgUnit.id());
98 hreq.callback(function(r) { orgUnitHours = r.getResultObject(); });
101 var areq = new Request('open-ils.actor:open-ils.actor.org_unit.address.retrieve', orgUnit.mailing_address());
102 areq.callback(function(r) { orgUnitAddress = r.getResultObject(); });
106 function selfckSetupPrinter() {
107 try { // Mozilla only
108 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
109 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
110 netscape.security.PrivilegeManager.enablePrivilege('UniversalPreferencesRead');
111 netscape.security.PrivilegeManager.enablePrivilege('UniversalPreferencesWrite');
112 var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
114 pref.setBoolPref('print.always_print_silent', true);
120 function selfckTryPatronCookie() {
121 var pb = cookieManager.read(PATRON_BARCODE_COOKIE);
123 cookieManager.write(PATRON_BARCODE_COOKIE, '');
124 $('selfck-patron-login-input').value = pb;
130 function selfckItemBarcodeKeypress(evt) {
131 if(userPressedEnter(evt)) {
132 clearTimeout(scanTimeoutId);
135 /* If scanTimeout milliseconds have passed and there is
136 still data in the input box, it's a likely indication
137 of a partial scan. Select the text so the next scan
138 will replace the value */
139 clearTimeout(scanTimeoutId);
140 scanTimeoutId = setTimeout(
142 if($('selfck-item-barcode-input').value) {
143 $('selfck-item-barcode-input').select();
152 * Start the logout timer
154 function selfckStartTimer() {
155 timerId = setTimeout(
157 selfckLogoutPatron();
165 * Reset the logout timer
167 function selfckResetTimer() {
168 clearTimeout(timerId);
173 * Clears fields and "logs out" the patron by reloading the page
175 function selfckLogoutPatron() {
176 $('selfck-item-barcode-input').value = ''; // prevent browser caching
177 $('selfck-patron-login-input').value = '';
178 hideMe($('selfck-patron-checkout-container'));
179 unHideMe($('selfck-print-queuing'));
183 function() { location.href = location.href; },
184 3500 // give the browser time to send the page to the printer
190 * Fetches the user by barcode and displays the item barcode input
193 function selfckPatronLogin(barcode) {
194 barcode = barcode || $('selfck-patron-login-input').value;
197 var bcReq = new Request(
198 'open-ils.actor:open-ils.actor.user.fleshed.retrieve_by_barcode',
199 G.user.session, barcode);
201 bcReq.request.alertEvent = false;
203 bcReq.callback(function(r) {
204 patron = r.getResultObject();
205 if(checkILSEvent(patron)) {
206 if(patron.textcode == 'ACTOR_USER_NOT_FOUND') {
207 unHideMe($('selfck-patron-not-found'));
208 $('selfck-patron-login-input').select();
212 if(patron.textcode == 'NO_SESSION')
213 return selfckLogoutStaff();
215 return alert(patron.textcode);
217 $('selfck-patron-login-input').value = ''; // reset the input
218 hideMe($('selfck-patron-login-container'));
219 unHideMe($('selfck-patron-checkout-container'));
220 $('selfck-patron-name-span').appendChild(text(patron.usrname()));
221 $('selfck-item-barcode-input').focus();
227 function selfckLogoutStaff() {
228 cookieManager.remove(STAFF_SES_PARAM);
229 location.reload(true);
233 * If a user barcode was scanned into the item barcode
234 * input, log out the current user and log in the new user
236 function selfckCheckPatronBarcode(itemBc) {
237 if(patronBarcodeRegex) {
238 if(itemBc.match(patronBarcodeRegex)) {
239 cookieManager.write(PATRON_BARCODE_COOKIE, itemBc, -1);
240 selfckLogoutPatron();
248 * Sends the checkout request
250 function selfckCheckout() {
251 if(pendingXact) return;
256 removeChildren($('selfck-event-time'));
257 removeChildren($('selfck-event-span'));
259 itemBarcode = $('selfck-item-barcode-input').value;
260 if(!itemBarcode) return;
262 if(selfckCheckPatronBarcode(itemBarcode))
265 if (itemBarcode in successfulItems) {
266 selfckShowMsgNode({textcode:'dupe-barcode'});
267 $('selfck-item-barcode-input').select();
272 var coReq = new Request(
273 'open-ils.circ:open-ils.circ.checkout.full',
274 G.user.session, {patron_id:patron.id(), copy_barcode:itemBarcode});
276 coReq.request.alertEvent = false;
277 coReq.callback(selfckHandleCoResult);
282 * Handles the resulting event. If the item is already checked out,
283 * attempts renewal. Any other events will display a message
285 function selfckHandleCoResult(r) {
289 evt = r.getResultObject();
292 selfckShowMsgNode({textcode:'UNKNOWN'});
293 appendClear($('selfck-errors'), text(E.toString()));
297 if(evt.textcode == 'SUCCESS') {
298 selfckDislplayCheckout(evt);
299 selfckShowMsgNode(evt);
300 successfulItems[itemBarcode] = 1;
302 } else if(evt.textcode == 'OPEN_CIRCULATION_EXISTS') {
305 } else if(evt.textcode == 'NO_SESSION') {
307 return selfckLogoutStaff();
311 selfckShowMsgNode(evt);
312 $('selfck-item-barcode-input').select();
317 * Display event text in the messages block
319 function selfckShowMsgNode(evt) {
320 var code = evt.textcode;
321 var msgspan = $('selfck-event-span');
323 // if we're not explicitly handling the event, just say "copy cannot circ"
324 if(!$('selfck-event-' + code))
325 code = 'COPY_CIRC_NOT_ALLOWED';
327 appendClear($('selfck-event-time'), text(new Date().toLocaleString()));
328 appendClear($('selfck-event-span'), text($('selfck-event-' + code).innerHTML));
330 if(code != 'SUCCESS' && alertOnCheckoutEvent)
331 alert($('selfck-event-' + code).innerHTML);
335 * Renders a row in the checkouts table for the current circulation
337 function selfckDislplayCheckout(evt) {
338 unHideMe($('selfck-items-out-table-wrapper'));
340 var template = itemsOutTemplate.cloneNode(true);
341 var copy = evt.payload.copy;
342 var record = evt.payload.record;
343 var circ = evt.payload.circ;
346 var pic = $n(template, 'selfck.jacket');
347 pic.setAttribute('src', '../ac/jacket/small/'+cleanISBN(record.isbn()));
349 $n(template, 'selfck.barcode').appendChild(text(copy.barcode()));
350 $n(template, 'selfck.title').appendChild(text(record.title()));
351 $n(template, 'selfck.author').appendChild(text(record.author()));
352 $n(template, 'selfck.due_date').appendChild(text(circ.due_date().replace(/T.*/,'')));
353 $n(template, 'selfck.remaining').appendChild(text(circ.renewal_remaining()));
355 hideMe($n(template, 'selfck.cotype_co'));
356 unHideMe($n(template, 'selfck.cotype_rn'));
359 var tbody = $('selfck-items-out-tbody');
360 tbody.insertBefore(template, tbody.getElementsByTagName('tr')[0]);
361 $('selfck-item-barcode-input').value = '';
364 // flesh out the printable version of the page as well
365 var ptemplate = printTemplate.cloneNode(true);
366 $n(ptemplate, 'title').appendChild(text(record.title()));
367 $n(ptemplate, 'barcode').appendChild(text(copy.barcode()));
368 $n(ptemplate, 'due_date').appendChild(text(circ.due_date().replace(/T.*/,'')));
369 printWrapper.insertBefore(ptemplate, printWrapper.getElementsByTagName('li')[0]);
375 * Checks to see if this item is checked out to the current patron. If so,
376 * this sends the renewal request.
378 function selfckRenew() {
380 // first, make sure the item is checked out to this patron
381 var detailReq = new Request(
382 'open-ils.circ:open-ils.circ.copy_details.retrieve.barcode',
383 G.user.session, itemBarcode);
387 var details = r.getResultObject();
388 if(details.circ.usr() == patron.id()) {
389 // OK, this is our item, renew it
391 var rnReq = new Request(
392 'open-ils.circ:open-ils.circ.renew',
393 G.user.session, {copy_barcode:itemBarcode});
394 rnReq.request.alertEvent = false;
395 rnReq.callback(selfckHandleCoResult);
399 selfckShowMsgNode({textcode:'already-out'});
408 * Sets the print date and prints the page
410 function selfckPrint() {
411 for(var x in successfulItems) { // make sure we've checked out at least one item
412 appendClear($('selfck-print-date'), text(new Date().toLocaleString()));
413 appendClear($('selfck-print-lib-name'), text(orgUnit.name()));
415 appendClear($('selfck-print-lib-addr-street'), text(orgUnitAddress.street1()+' '+orgUnitAddress.street2()));
416 appendClear($('selfck-print-lib-addr-city'), text(orgUnitAddress.city()));
417 appendClear($('selfck-print-lib-addr-state'), text(orgUnitAddress.state()));
418 appendClear($('selfck-print-lib-addr-post-code'), text(orgUnitAddress.post_code()));
420 appendClear($('selfck-print-lname'), text(patron.family_name()));
421 appendClear($('selfck-print-fname'), text(patron.first_given_name()));
422 appendClear($('selfck-print-lib-phone'), text(orgUnit.phone()));
424 for(var i in [0, 1, 2, 3, 4, 5, 6]) {
425 appendClear($('selfck-print-dow_'+i+'_open'), text(orgUnitHours['dow_'+i+'_open']()));
426 appendClear($('selfck-print-dow_'+i+'_close'), text(orgUnitHours['dow_'+i+'_close']()));
436 * Test method for generating dummy data in the checkout tables
438 function selfckMkDummyCirc() {
439 unHideMe($('selfck-items-out-table'));
441 var template = itemsOutTemplate.cloneNode(true);
442 $n(template, 'selfck.barcode').appendChild(text('123456789'));
443 $n(template, 'selfck.title').appendChild(text('Test Title'));
444 $n(template, 'selfck.author').appendChild(text('Test Author'));
445 $n(template, 'selfck.due_date').appendChild(text('2008-08-01'));
446 $n(template, 'selfck.remaining').appendChild(text('1'));
447 $('selfck-items-out-tbody').appendChild(template);
449 // flesh out the printable version of the page as well
450 var ptemplate = printTemplate.cloneNode(true);
451 $n(ptemplate, 'title').appendChild(text('Test Title'));
452 $n(ptemplate, 'barcode').appendChild(text('123456789'));
453 $n(ptemplate, 'due_date').appendChild(text('2008-08-01'));
454 printWrapper.appendChild(ptemplate);