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 function selfckInit() {
41 var staff = grabUser(cookieManager.read(STAFF_SES_PARAM) || cgi.param(STAFF_SES_PARAM));
45 orgUnit = findOrgUnitSN(cgi.param('l')) || globalOrgTree;
47 var t = fetchOrgSettingDefault(orgUnit.id(), 'circ.selfcheck.patron_login_timeout');
48 patronTimeout = (t) ? parseInt(t) * 1000 : patronTimeout;
50 var reg = fetchOrgSettingDefault(orgUnit.id(), 'opac.barcode_regex');
51 if(reg) patronBarcodeRegex = new RegExp(reg);
54 // should not happen when behind the proxy
55 return alert('Staff must login');
58 unHideMe($('selfck-patron-login-container'));
59 $('selfck-patron-login-input').focus();
61 $('selfck-patron-login-input').onkeypress = function(evt) {
62 if(userPressedEnter(evt))
66 $('selfck-item-barcode-input').onkeypress = selfckItemBarcodeKeypress;
68 // for debugging, allow passing the user barcode via param
69 var urlbc = new CGI().param('patron');
71 selfckPatronLogin(urlbc);
75 printWrapper = $('selfck-print-items-list');
76 printTemplate = printWrapper.removeChild($n(printWrapper, 'selfck-print-items-template'));
77 itemsOutTemplate = $('selfck-items-out-tbody').removeChild($('selfck-items-out-row'));
79 selfckTryPatronCookie();
81 // selfckMkDummyCirc(); // testing only
85 function selfckSetupPrinter() {
87 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
88 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
89 netscape.security.PrivilegeManager.enablePrivilege('UniversalPreferencesRead');
90 netscape.security.PrivilegeManager.enablePrivilege('UniversalPreferencesWrite');
91 var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
93 pref.setBoolPref('print.always_print_silent', true);
99 function selfckTryPatronCookie() {
100 var pb = cookieManager.read(PATRON_BARCODE_COOKIE);
102 cookieManager.write(PATRON_BARCODE_COOKIE, '');
103 $('selfck-patron-login-input').value = pb;
109 function selfckItemBarcodeKeypress(evt) {
110 if(userPressedEnter(evt)) {
111 clearTimeout(scanTimeoutId);
114 /* If scanTimeout milliseconds have passed and there is
115 still data in the input box, it's a likely indication
116 of a partial scan. Select the text so the next scan
117 will replace the value */
118 clearTimeout(scanTimeoutId);
119 scanTimeoutId = setTimeout(
121 if($('selfck-item-barcode-input').value) {
122 $('selfck-item-barcode-input').select();
131 * Start the logout timer
133 function selfckStartTimer() {
134 timerId = setTimeout(
136 selfckLogoutPatron();
144 * Reset the logout timer
146 function selfckResetTimer() {
147 clearTimeout(timerId);
152 * Clears fields and "logs out" the patron by reloading the page
154 function selfckLogoutPatron() {
155 $('selfck-item-barcode-input').value = ''; // prevent browser caching
156 $('selfck-patron-login-input').value = '';
160 function() { location.href = location.href; },
167 * Fetches the user by barcode and displays the item barcode input
170 function selfckPatronLogin(barcode) {
171 barcode = barcode || $('selfck-patron-login-input').value;
174 var bcReq = new Request(
175 'open-ils.actor:open-ils.actor.user.fleshed.retrieve_by_barcode',
176 G.user.session, barcode);
178 bcReq.request.alertEvent = false;
180 bcReq.callback(function(r) {
181 patron = r.getResultObject();
182 if(checkILSEvent(patron)) {
183 if(patron.textcode == 'ACTOR_USER_NOT_FOUND') {
184 unHideMe($('selfck-patron-not-found'));
185 $('selfck-patron-login-input').select();
189 if(patron.textcode == 'NO_SESSION')
190 return selfckLogoutStaff();
192 return alert(patron.textcode);
194 $('selfck-patron-login-input').value = ''; // reset the input
195 hideMe($('selfck-patron-login-container'));
196 unHideMe($('selfck-patron-checkout-container'));
197 $('selfck-patron-name-span').appendChild(text(patron.usrname()));
198 $('selfck-item-barcode-input').focus();
204 function selfckLogoutStaff() {
205 cookieManager.remove(STAFF_SES_PARAM);
206 location.reload(true);
210 * If a user barcode was scanned into the item barcode
211 * input, log out the current user and log in the new user
213 function selfckCheckPatronBarcode(itemBc) {
214 if(patronBarcodeRegex) {
215 if(itemBc.match(patronBarcodeRegex)) {
216 cookieManager.write(PATRON_BARCODE_COOKIE, itemBc, -1);
217 selfckLogoutPatron();
225 * Sends the checkout request
227 function selfckCheckout() {
228 if(pendingXact) return;
233 removeChildren($('selfck-event-time'));
234 removeChildren($('selfck-event-span'));
236 itemBarcode = $('selfck-item-barcode-input').value;
237 if(!itemBarcode) return;
239 if(selfckCheckPatronBarcode(itemBarcode))
242 if (itemBarcode in successfulItems) {
243 selfckShowMsgNode({textcode:'dupe-barcode'});
244 $('selfck-item-barcode-input').select();
249 var coReq = new Request(
250 'open-ils.circ:open-ils.circ.checkout.full',
251 G.user.session, {patron_id:patron.id(), copy_barcode:itemBarcode});
253 coReq.request.alertEvent = false;
254 coReq.callback(selfckHandleCoResult);
259 * Handles the resulting event. If the item is already checked out,
260 * attempts renewal. Any other events will display a message
262 function selfckHandleCoResult(r) {
266 evt = r.getResultObject();
269 selfckShowMsgNode({textcode:'UNKNOWN'});
270 appendClear($('selfck-errors'), text(E.toString()));
274 if(evt.textcode == 'SUCCESS') {
275 selfckDislplayCheckout(evt);
276 selfckShowMsgNode(evt);
277 successfulItems[itemBarcode] = 1;
279 } else if(evt.textcode == 'OPEN_CIRCULATION_EXISTS') {
282 } else if(evt.textcode == 'NO_SESSION') {
284 return selfckLogoutStaff();
288 selfckShowMsgNode(evt);
289 $('selfck-item-barcode-input').select();
294 * Display event text in the messages block
296 function selfckShowMsgNode(evt) {
297 var code = evt.textcode;
298 var msgspan = $('selfck-event-span');
300 // if we're not explicitly handling the event, just say "copy cannot circ"
301 if(!$('selfck-event-' + code))
302 code = 'COPY_CIRC_NOT_ALLOWED';
304 appendClear($('selfck-event-time'), text(new Date().toLocaleString()));
305 appendClear($('selfck-event-span'), text($('selfck-event-' + code).innerHTML));
309 * Renders a row in the checkouts table for the current circulation
311 function selfckDislplayCheckout(evt) {
312 unHideMe($('selfck-items-out-table-wrapper'));
314 var template = itemsOutTemplate.cloneNode(true);
315 var copy = evt.payload.copy;
316 var record = evt.payload.record;
317 var circ = evt.payload.circ;
320 var pic = $n(template, 'selfck.jacket');
321 pic.setAttribute('src', '../ac/jacket/small/'+cleanISBN(record.isbn()));
323 $n(template, 'selfck.barcode').appendChild(text(copy.barcode()));
324 $n(template, 'selfck.title').appendChild(text(record.title()));
325 $n(template, 'selfck.author').appendChild(text(record.author()));
326 $n(template, 'selfck.due_date').appendChild(text(circ.due_date().replace(/T.*/,'')));
327 $n(template, 'selfck.remaining').appendChild(text(circ.renewal_remaining()));
329 hideMe($n(template, 'selfck.cotype_co'));
330 unHideMe($n(template, 'selfck.cotype_rn'));
333 $('selfck-items-out-tbody').appendChild(template);
334 $('selfck-item-barcode-input').value = '';
337 // flesh out the printable version of the page as well
338 var ptemplate = printTemplate.cloneNode(true);
339 $n(ptemplate, 'title').appendChild(text(record.title()));
340 $n(ptemplate, 'barcode').appendChild(text(copy.barcode()));
341 $n(ptemplate, 'due_date').appendChild(text(circ.due_date().replace(/T.*/,'')));
342 printWrapper.appendChild(ptemplate);
348 * Checks to see if this item is checked out to the current patron. If so,
349 * this sends the renewal request.
351 function selfckRenew() {
353 // first, make sure the item is checked out to this patron
354 var detailReq = new Request(
355 'open-ils.circ:open-ils.circ.copy_details.retrieve.barcode',
356 G.user.session, itemBarcode);
360 var details = r.getResultObject();
361 if(details.circ.usr() == patron.id()) {
362 // OK, this is our item, renew it
364 var rnReq = new Request(
365 'open-ils.circ:open-ils.circ.renew',
366 G.user.session, {copy_barcode:itemBarcode});
367 rnReq.request.alertEvent = false;
368 rnReq.callback(selfckHandleCoResult);
378 * Sets the print date and prints the page
380 function selfckPrint() {
381 for(var x in successfulItems) { // make sure we've checked out at least one item
382 appendClear($('selfck-print-lib-name'), text(orgUnit.name()));
383 appendClear($('selfck-print-date'), text(new Date().toLocaleString()));
391 * Test method for generating dummy data in the checkout tables
393 function selfckMkDummyCirc() {
394 unHideMe($('selfck-items-out-table'));
396 var template = itemsOutTemplate.cloneNode(true);
397 $n(template, 'selfck.barcode').appendChild(text('123456789'));
398 $n(template, 'selfck.title').appendChild(text('Test Title'));
399 $n(template, 'selfck.author').appendChild(text('Test Author'));
400 $n(template, 'selfck.due_date').appendChild(text('2008-08-01'));
401 $n(template, 'selfck.remaining').appendChild(text('1'));
402 $('selfck-items-out-tbody').appendChild(template);
404 // flesh out the printable version of the page as well
405 var ptemplate = printTemplate.cloneNode(true);
406 $n(ptemplate, 'title').appendChild(text('Test Title'));
407 $n(ptemplate, 'barcode').appendChild(text('123456789'));
408 $n(ptemplate, 'due_date').appendChild(text('2008-08-01'));
409 printWrapper.appendChild(ptemplate);