]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/extras/selfcheck/selfcheck.js
pull org location from URL param "l" .. default to org tree root
[Evergreen.git] / Open-ILS / web / opac / extras / selfcheck / selfcheck.js
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
38
39 function selfckInit() {
40     var cgi = new CGI();
41     var staff = grabUser(cookieManager.read(STAFF_SES_PARAM) || cgi.param(STAFF_SES_PARAM));
42
43     selfckSetupPrinter();
44
45     orgUnit = findOrgUnitSN(cgi.param('l')) || globalOrgTree;
46
47     var t = fetchOrgSettingDefault(orgUnit.id(), 'circ.selfcheck.patron_login_timeout');
48     patronTimeout = (t) ? parseInt(t) * 1000 : patronTimeout;
49
50     var reg = fetchOrgSettingDefault(orgUnit.id(), 'opac.barcode_regex');
51     if(reg) patronBarcodeRegex = new RegExp(reg);
52
53     if(!staff) {
54         // should not happen when behind the proxy
55         return alert('Staff must login');
56     }
57
58     unHideMe($('selfck-patron-login-container'));
59     $('selfck-patron-login-input').focus();
60
61     $('selfck-patron-login-input').onkeypress = function(evt) {
62         if(userPressedEnter(evt)) 
63             selfckPatronLogin();
64     };
65
66     $('selfck-item-barcode-input').onkeypress = selfckItemBarcodeKeypress;
67
68     // for debugging, allow passing the user barcode via param
69     var urlbc = new CGI().param('patron');
70     if(urlbc)
71         selfckPatronLogin(urlbc);
72
73     selfckStartTimer();
74
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'));
78
79     selfckTryPatronCookie();
80
81 //    selfckMkDummyCirc(); // testing only
82     
83 }
84
85 function selfckSetupPrinter() {
86     try { // Mozilla only
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);
92         if (pref)
93             pref.setBoolPref('print.always_print_silent', true);
94     } catch(E) {
95         
96     }
97 }
98
99 function selfckTryPatronCookie() {
100     var pb = cookieManager.read(PATRON_BARCODE_COOKIE);
101     if(pb) {
102         cookieManager.write(PATRON_BARCODE_COOKIE, '');
103         $('selfck-patron-login-input').value = pb;
104         selfckPatronLogin();
105     }
106 }
107
108
109 function selfckItemBarcodeKeypress(evt) {
110     if(userPressedEnter(evt)) {
111         clearTimeout(scanTimeoutId);
112         selfckCheckout();
113     } else {
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(
120             function() {
121                 if($('selfck-item-barcode-input').value) {
122                     $('selfck-item-barcode-input').select();
123                 }
124             },
125             scanTimeout
126         );
127     }
128 }
129
130 /*
131  * Start the logout timer
132  */
133 function selfckStartTimer() {
134     timerId = setTimeout(
135         function() {
136             selfckLogoutPatron();
137         },
138         patronTimeout
139     );
140
141 }
142
143 /*
144  * Reset the logout timer
145  */
146 function selfckResetTimer() {
147     clearTimeout(timerId);
148     selfckStartTimer();
149 }
150
151 /*
152  * Clears fields and "logs out" the patron by reloading the page
153  */
154 function selfckLogoutPatron() {
155     $('selfck-item-barcode-input').value = ''; // prevent browser caching
156     $('selfck-patron-login-input').value = '';
157     if(patron) {
158         selfckPrint();
159         setTimeout(
160             function() { location.href = location.href; },
161             800
162         );
163     }
164 }
165
166 /*
167  * Fetches the user by barcode and displays the item barcode input
168  */
169
170 function selfckPatronLogin(barcode) {
171     barcode = barcode || $('selfck-patron-login-input').value;
172     if(!barcode) return;
173
174     var bcReq = new Request(
175         'open-ils.actor:open-ils.actor.user.fleshed.retrieve_by_barcode',
176         G.user.session, barcode);
177
178         bcReq.request.alertEvent = false;
179
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();
186                 return;
187             }
188             return alert(patron.textcode);
189         }
190         $('selfck-patron-login-input').value = ''; // reset the input
191         hideMe($('selfck-patron-login-container'));
192         unHideMe($('selfck-patron-checkout-container'));
193         $('selfck-patron-name-span').appendChild(text(patron.usrname()));
194         $('selfck-item-barcode-input').focus();
195     });
196
197     bcReq.send();
198 }
199
200 /**
201   * If a user barcode was scanned into the item barcode
202   * input, log out the current user and log in the new user
203   */
204 function selfckCheckPatronBarcode(itemBc) {
205     if(patronBarcodeRegex) {
206         if(itemBc.match(patronBarcodeRegex)) {
207             cookieManager.write(PATRON_BARCODE_COOKIE, itemBc, -1);
208             selfckLogoutPatron();
209             return true;
210         }
211     }
212     return false;
213 }
214
215 /**
216   * Sends the checkout request
217   */
218 function selfckCheckout() {
219     if(pendingXact) return;
220     selfckResetTimer();
221     pendingXact = true;
222     isRenewal = false;
223
224     removeChildren($('selfck-event-time'));
225     removeChildren($('selfck-event-span'));
226
227     itemBarcode = $('selfck-item-barcode-input').value;
228     if(!itemBarcode) return;
229
230     if(selfckCheckPatronBarcode(itemBarcode))
231         return;
232
233     if (itemBarcode in successfulItems) {
234         selfckShowMsgNode({textcode:'dupe-barcode'});
235         $('selfck-item-barcode-input').select();
236         pendingXact = false;
237         return;
238     }
239
240     var coReq = new Request(
241         'open-ils.circ:open-ils.circ.checkout.full',
242         G.user.session, {patron_id:patron.id(), copy_barcode:itemBarcode});
243
244         coReq.request.alertEvent = false;
245     coReq.callback(selfckHandleCoResult);
246     coReq.send();
247 }
248
249 /**
250   * Handles the resulting event.  If the item is already checked out,
251   * attempts renewal.  Any other events will display a message
252   */
253 function selfckHandleCoResult(r) {
254     var evt;
255
256     try {
257         evt = r.getResultObject();
258     } catch(E) {
259         pendingXact = false;
260         selfckShowMsgNode({textcode:'UNKNOWN'});
261         appendClear($('selfck-errors'), text(E.toString()));
262         return;
263     }
264
265     if(evt.textcode == 'SUCCESS') {
266         selfckDislplayCheckout(evt);
267         selfckShowMsgNode(evt);
268         successfulItems[itemBarcode] = 1;
269
270     } else if(evt.textcode == 'OPEN_CIRCULATION_EXISTS') {
271         selfckRenew();
272
273     } else {
274         pendingXact = false;
275         selfckShowMsgNode(evt);
276         $('selfck-item-barcode-input').select();
277     }
278 }
279
280 /**
281   * Display event text in the messages block
282   */
283 function selfckShowMsgNode(evt) {
284     var code = evt.textcode;
285     var msgspan = $('selfck-event-span');
286
287     // if we're not explicitly handling the event, just say "copy cannot circ"
288     if(!$('selfck-event-' + code)) 
289         code = 'COPY_CIRC_NOT_ALLOWED';
290
291     appendClear($('selfck-event-time'), text(new Date().toLocaleString()));
292     appendClear($('selfck-event-span'), text($('selfck-event-' + code).innerHTML));
293 }
294
295 /**
296   * Renders a row in the checkouts table for the current circulation
297   */
298 function selfckDislplayCheckout(evt) {
299     unHideMe($('selfck-items-out-table-wrapper'));
300
301     var template = itemsOutTemplate.cloneNode(true);
302     var copy = evt.payload.copy;
303     var record = evt.payload.record;
304     var circ = evt.payload.circ;
305
306     if(record.isbn()) {
307             var pic = $n(template, 'selfck.jacket');
308             pic.setAttribute('src', '../ac/jacket/small/'+cleanISBN(record.isbn()));
309     }
310     $n(template, 'selfck.barcode').appendChild(text(copy.barcode()));
311     $n(template, 'selfck.title').appendChild(text(record.title()));
312     $n(template, 'selfck.author').appendChild(text(record.author()));
313     $n(template, 'selfck.due_date').appendChild(text(circ.due_date().replace(/T.*/,'')));
314     $n(template, 'selfck.remaining').appendChild(text(circ.renewal_remaining()));
315     if(isRenewal) {
316         hideMe($n(template, 'selfck.cotype_co'));
317         unHideMe($n(template, 'selfck.cotype_rn'));
318     }
319
320     $('selfck-items-out-tbody').appendChild(template);
321     $('selfck-item-barcode-input').value = '';
322
323
324     // flesh out the printable version of the page as well
325     var ptemplate = printTemplate.cloneNode(true);
326     $n(ptemplate, 'title').appendChild(text(record.title()));
327     $n(ptemplate, 'barcode').appendChild(text(copy.barcode()));
328     $n(ptemplate, 'due_date').appendChild(text(circ.due_date().replace(/T.*/,'')));
329     printWrapper.appendChild(ptemplate);
330
331     pendingXact = false;
332 }
333
334 /**
335   * Checks to see if this item is checked out to the current patron.  If so, 
336   * this sends the renewal request.
337   */
338 function selfckRenew() {
339
340     // first, make sure the item is checked out to this patron
341     var detailReq = new Request(
342         'open-ils.circ:open-ils.circ.copy_details.retrieve.barcode',
343         G.user.session, itemBarcode);
344
345     detailReq.callback(
346         function(r) {
347             var details = r.getResultObject();
348             if(details.circ.usr() == patron.id()) {
349                 // OK, this is our item, renew it
350                 isRenewal = true;
351                 var rnReq = new Request(
352                     'open-ils.circ:open-ils.circ.renew',
353                     G.user.session, {copy_barcode:itemBarcode});
354                 rnReq.request.alertEvent = false;
355                 rnReq.callback(selfckHandleCoResult);
356                 rnReq.send();
357             }
358         }
359     );
360
361     detailReq.send();
362 }
363
364 /**
365   * Sets the print date and prints the page
366   */
367 function selfckPrint() {
368     for(var x in successfulItems) { // make sure we've checked out at least one item
369         appendClear($('selfck-print-date'), text(new Date().toLocaleString()));
370         window.print();
371         return;
372     }
373 }
374
375
376 /**
377   * Test method for generating dummy data in the checkout tables
378   */
379 function selfckMkDummyCirc() {
380     unHideMe($('selfck-items-out-table'));
381
382     var template = itemsOutTemplate.cloneNode(true);
383     $n(template, 'selfck.barcode').appendChild(text('123456789'));
384     $n(template, 'selfck.title').appendChild(text('Test Title'));
385     $n(template, 'selfck.author').appendChild(text('Test Author'));
386     $n(template, 'selfck.due_date').appendChild(text('2008-08-01'));
387     $n(template, 'selfck.remaining').appendChild(text('1'));
388     $('selfck-items-out-tbody').appendChild(template);
389
390     // flesh out the printable version of the page as well
391     var ptemplate = printTemplate.cloneNode(true);
392     $n(ptemplate, 'title').appendChild(text('Test Title'));
393     $n(ptemplate, 'barcode').appendChild(text('123456789'));
394     $n(ptemplate, 'due_date').appendChild(text('2008-08-01'));
395     printWrapper.appendChild(ptemplate);
396 }