testing commit, please ignore
[kcls-web.git] / opac / extras / selfcheck / selfcheck.js.082010
1 /* -----------------------------------------------------------------
2 * Copyright (C) 2008  Equinox Software, Inc.
3 * Bill Erickson <erickson@esilibrary.com>
4
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.
9
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.
14
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  
18 * 02110-1301, USA
19 ----------------------------------------------------------------- */
20
21 var STAFF_SES_PARAM = 'ses';
22 var PATRON_BARCODE_COOKIE = 'pbcc';
23 var patron = null
24 var itemBarcode = null;
25 var itemsOutTemplate = null;
26 var isRenewal = false;
27 var pendingXact = false;
28 var patronTimeout = 600000; /* 10 minutes */
29 var timerId = null;
30 var printWrapper;
31 var printTemplate;
32 var successfulItems = {};
33 var scanTimeout = 800;
34 var scanTimeoutId;
35 var patronBarcodeRegex;
36 var orgUnit;
37 var orgUnitAddress;
38 var orgUnitHours;
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';
45
46
47 function selfckInit() {
48     var cgi = new CGI();
49     var staff = grabUser(cookieManager.read(STAFF_SES_PARAM) || cgi.param(STAFF_SES_PARAM));
50
51     selfckSetupPrinter();
52
53     orgUnit = findOrgUnitSN(cgi.param('l')) || globalOrgTree;
54     selfckFetchOrgDetails();
55
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;
67
68     if(!staff) {
69         // should not happen when behind the proxy
70         return alert('Staff must login');
71     }
72
73     unHideMe($('selfck-patron-login-container'));
74     $('selfck-patron-login-input').focus();
75
76     $('selfck-patron-login-input').onkeypress = function(evt) {
77         if(userPressedEnter(evt)) 
78             selfckPatronLogin();
79     };
80
81     $('selfck-item-barcode-input').onkeypress = selfckItemBarcodeKeypress;
82
83     // for debugging, allow passing the user barcode via param
84     var urlbc = new CGI().param('patron');
85     if(urlbc)
86         selfckPatronLogin(urlbc);
87
88     selfckStartTimer();
89
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'));
93
94     selfckTryPatronCookie();
95
96 //    selfckMkDummyCirc(); // testing only
97     
98 }
99
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(); });
103     hreq.send();
104
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(); });
107     areq.send();
108 }
109
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);
117         if (pref)
118             pref.setBoolPref('print.always_print_silent', true);
119     } catch(E) {
120         
121     }
122 }
123
124 function selfckTryPatronCookie() {
125     var pb = cookieManager.read(PATRON_BARCODE_COOKIE);
126     if(pb) {
127         cookieManager.write(PATRON_BARCODE_COOKIE, '');
128         $('selfck-patron-login-input').value = pb;
129         selfckPatronLogin();
130     }
131 }
132
133
134 function selfckItemBarcodeKeypress(evt) {
135     if(userPressedEnter(evt)) {
136         clearTimeout(scanTimeoutId);
137         selfckCheckout();
138     } else {
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(
145             function() {
146                 if($('selfck-item-barcode-input').value) {
147                     $('selfck-item-barcode-input').select();
148                 }
149             },
150             scanTimeout
151         );
152     }
153 }
154
155 /*
156  * Start the logout timer
157  */
158 function selfckStartTimer() {
159     timerId = setTimeout(
160         function() {
161             selfckLogoutPatron();
162         },
163         patronTimeout
164     );
165
166 }
167
168 /*
169  * Reset the logout timer
170  */
171 function selfckResetTimer() {
172     clearTimeout(timerId);
173     selfckStartTimer();
174 }
175
176 /*
177  * Clears fields and "logs out" the patron by reloading the page
178  */
179 function selfckLogoutPatron() {
180     $('selfck-item-barcode-input').value = ''; // prevent browser caching
181     $('selfck-patron-login-input').value = '';
182     if(patron) {
183         var numItems = selfckPrint();
184         var sleepTime = 1000;
185         if(numItems > 0) sleepTime += (numItems / 2) * 1000;
186         setTimeout(
187             function() { location.href = location.href; },
188             sleepTime // give the browser time to send the page to the printer
189         );
190     }
191 }
192
193 /*
194  * Fetches the user by barcode and displays the item barcode input
195  */
196
197 function selfckPatronLogin(barcode) {
198     barcode = barcode || $('selfck-patron-login-input').value;
199     if(!barcode) return;
200
201     var bcReq = new Request(
202         'open-ils.actor:open-ils.actor.user.fleshed.retrieve_by_barcode',
203         G.user.session, barcode);
204
205         bcReq.request.alertEvent = false;
206
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();
213                 return;
214             }
215
216             if(patron.textcode == 'NO_SESSION') 
217                 return selfckLogoutStaff();
218
219             return alert(patron.textcode);
220         }
221
222         if(!isTrue(patron.active())) {
223             unHideMe($('selfck-patron-not-found'));
224             $('selfck-patron-login-input').select();
225             return;
226         }
227
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();
233     });
234
235     bcReq.send();
236 }
237
238 function selfckLogoutStaff() {
239     cookieManager.remove(STAFF_SES_PARAM);
240     location.reload(true);
241 }
242
243 /**
244   * If a user barcode was scanned into the item barcode
245   * input, log out the current user and log in the new user
246   */
247 function selfckCheckPatronBarcode(itemBc) {
248     if(patronBarcodeRegex) {
249         if(itemBc.match(patronBarcodeRegex)) {
250             cookieManager.write(PATRON_BARCODE_COOKIE, itemBc, -1);
251             selfckLogoutPatron();
252             return true;
253         }
254     }
255     return false;
256 }
257
258 /**
259   * Sends the checkout request
260   */
261 function selfckCheckout(override) {
262     if(pendingXact) return;
263     selfckResetTimer();
264     pendingXact = true;
265     isRenewal = false;
266
267     removeChildren($('selfck-event-time'));
268     removeChildren($('selfck-event-span'));
269
270     itemBarcode = $('selfck-item-barcode-input').value;
271     if(!itemBarcode) return;
272
273     if(selfckCheckPatronBarcode(itemBarcode))
274         return;
275
276     if (itemBarcode in successfulItems) {
277         selfckShowMsgNode({textcode:'dupe-barcode'});
278         $('selfck-item-barcode-input').select();
279         pendingXact = false;
280         return;
281     }
282
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});
286
287         coReq.request.alertEvent = false;
288     coReq.callback(selfckHandleCoResult);
289     coReq.send();
290 }
291
292 /**
293   * Handles the resulting event.  If the item is already checked out,
294   * attempts renewal.  Any other events will display a message
295   */
296 function selfckHandleCoResult(r) {
297     var evt;
298
299     try {
300         evt = r.getResultObject();
301     } catch(E) {
302         pendingXact = false;
303         selfckShowMsgNode({textcode:'UNKNOWN'});
304         appendClear($('selfck-errors'), text(E.toString()));
305         return;
306     }
307
308     if(evt.textcode == 'SUCCESS') {
309         selfckDislplayCheckout(evt);
310         selfckShowMsgNode(evt);
311         successfulItems[itemBarcode] = 1;
312
313     } else if(evt.textcode == 'OPEN_CIRCULATION_EXISTS') {
314         selfckRenew();
315
316     } else if(evt.textcode == 'NO_SESSION') {
317         
318         return selfckLogoutStaff();
319
320     } else {
321         pendingXact = false;
322
323         if(!evt.length) evt = [evt];
324         if(overrideCircEvents.length) {
325
326             // see if the events we received are all in the list of 
327             // events to override
328             var override = true;
329             for(var i = 0; i < evt.length; i++) {
330                 var match = overrideCircEvents.filter(
331                     function(e) { return (e == evt[i].textcode); })[0];
332                 if(!match) {
333                     override = false;
334                     break;
335                 }
336             }
337
338             if(override)
339                 return selfckCheckout(true);
340         }
341
342         selfckShowMsgNode(evt);
343         $('selfck-item-barcode-input').select();
344     }
345 }
346
347 /**
348   * Display event text in the messages block
349   */
350 function selfckShowMsgNode(evt) {
351     var code = evt.textcode;
352     var msgspan = $('selfck-event-span');
353
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';
357
358     appendClear($('selfck-event-time'), text(new Date().toLocaleString()));
359     appendClear($('selfck-event-span'), text($('selfck-event-' + code).innerHTML));
360
361     if(code != 'SUCCESS' && alertOnCheckoutEvent)
362         alert($('selfck-event-' + code).innerHTML);
363 }
364
365 /**
366   * Renders a row in the checkouts table for the current circulation
367   */
368 function selfckDislplayCheckout(evt) {
369     unHideMe($('selfck-items-out-table-wrapper'));
370
371     var template = itemsOutTemplate.cloneNode(true);
372     var copy = evt.payload.copy;
373     var record = evt.payload.record;
374     var circ = evt.payload.circ;
375
376     if(record.isbn()) {
377             var pic = $n(template, 'selfck.jacket');
378             pic.setAttribute('src', '../ac/jacket/small/'+cleanISBN(record.isbn()));
379     }
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()));
385     if(isRenewal) {
386         hideMe($n(template, 'selfck.cotype_co'));
387         unHideMe($n(template, 'selfck.cotype_rn'));
388     }
389
390     var tbody = $('selfck-items-out-tbody');
391     tbody.insertBefore(template, tbody.getElementsByTagName('tr')[0]);
392     $('selfck-item-barcode-input').value = '';
393
394
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]);
401
402     pendingXact = false;
403 }
404
405 /**
406   * Checks to see if this item is checked out to the current patron.  If so, 
407   * this sends the renewal request.
408   */
409 function selfckRenew() {
410
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);
415
416     detailReq.callback(
417         function(r) {
418             var details = r.getResultObject();
419             if(details.circ.usr() == patron.id()) {
420                 // OK, this is our item, renew it
421                 isRenewal = true;
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);
427                 rnReq.send();
428             } else {
429                 pendingXact = false;
430                 selfckShowMsgNode({textcode:'already-out'});
431             }
432         }
433     );
434
435     detailReq.send();
436 }
437
438 /**
439   * Sets the print date and prints the page
440   */
441 function selfckPrint() {
442
443     var numItems = 0;
444     for(var x in successfulItems)  
445         numItems++;
446
447     if(numItems > 0) {
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()));
452         if(orgUnitAddress) {
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()));
457         }
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()));
461         if(orgUnitHours) {
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']()));
465             }
466         }
467         window.print();
468     }
469
470     return numItems;
471 }
472
473
474 /**
475   * Test method for generating dummy data in the checkout tables
476   */
477 function selfckMkDummyCirc() {
478     unHideMe($('selfck-items-out-table'));
479
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);
487
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);
494 }