]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/circ/selfcheck/selfcheck.js
Separate payment form code from the rest of the self check interface
[working/Evergreen.git] / Open-ILS / web / js / ui / default / circ / selfcheck / selfcheck.js
1 dojo.require('dojo.date.locale');
2 dojo.require('dojo.date.stamp');
3 dojo.require('dijit.form.CheckBox');
4 dojo.require('dijit.form.NumberSpinner');
5 dojo.require('openils.CGI');
6 dojo.require('openils.Util');
7 dojo.require('openils.User');
8 dojo.require('openils.Event');
9 dojo.require('openils.widget.ProgressDialog');
10 dojo.require('openils.widget.OrgUnitFilteringSelect');
11
12 dojo.requireLocalization('openils.circ', 'selfcheck');
13 var localeStrings = dojo.i18n.getLocalization('openils.circ', 'selfcheck');
14
15
16 const SET_BARCODE_REGEX = 'opac.barcode_regex';
17 const SET_PATRON_TIMEOUT = 'circ.selfcheck.patron_login_timeout';
18 const SET_AUTO_OVERRIDE_EVENTS = 'circ.selfcheck.auto_override_checkout_events';
19 const SET_PATRON_PASSWORD_REQUIRED = 'circ.selfcheck.patron_password_required';
20 const SET_AUTO_RENEW_INTERVAL = 'circ.checkout_auto_renew_age';
21 const SET_WORKSTATION_REQUIRED = 'circ.selfcheck.workstation_required';
22 const SET_ALERT_POPUP = 'circ.selfcheck.alert.popup';
23 const SET_ALERT_SOUND = 'circ.selfcheck.alert.sound';
24 const SET_CC_PAYMENT_ALLOWED = 'credit.payments.allow';
25
26 function SelfCheckManager() {
27
28     this.cgi = new openils.CGI();
29     this.staff = null; 
30     this.workstation = null;
31     this.authtoken = null;
32
33     this.patron = null; 
34     this.patronBarcodeRegex = null;
35
36     this.checkouts = [];
37     this.itemsOut = [];
38
39     // During renewals, keep track of the ID of the previous circulation. 
40     // Previous circ is used for tracking failed renewals (for receipts).
41     this.prevCirc = null;
42
43     // current item barcode
44     this.itemBarcode = null; 
45
46     // are we currently performing a renewal?
47     this.isRenewal = false; 
48
49     // dict of org unit settings for "here"
50     this.orgSettings = {};
51
52     // Construct a mock checkout for debugging purposes
53     if(this.mockCheckouts = this.cgi.param('mock-circ')) {
54
55         this.mockCheckout = {
56             payload : {
57                 record : new fieldmapper.mvr(),
58                 copy : new fieldmapper.acp(),
59                 circ : new fieldmapper.circ()
60             }
61         };
62
63         this.mockCheckout.payload.record.title('Jazz improvisation for guitar');
64         this.mockCheckout.payload.record.author('Wise, Les');
65         this.mockCheckout.payload.record.isbn('0634033565');
66         this.mockCheckout.payload.copy.barcode('123456789');
67         this.mockCheckout.payload.circ.renewal_remaining(1);
68         this.mockCheckout.payload.circ.parent_circ(1);
69         this.mockCheckout.payload.circ.due_date('2012-12-21');
70     }
71
72     this.initPrinter();
73 }
74
75
76
77 /**
78  * Fetch the org-unit settings, initialize the display, etc.
79  */
80 SelfCheckManager.prototype.init = function() {
81
82     this.staff = openils.User.user;
83     this.workstation = openils.User.workstation;
84     this.authtoken = openils.User.authtoken;
85     this.loadOrgSettings();
86
87     this.circTbody = dojo.byId('oils-selfck-circ-tbody');
88     this.itemsOutTbody = dojo.byId('oils-selfck-circ-out-tbody');
89
90     // workstation is required but none provided
91     if(this.orgSettings[SET_WORKSTATION_REQUIRED] && !this.workstation) {
92         if(confirm(dojo.string.substitute(localeStrings.WORKSTATION_REQUIRED))) {
93             this.registerWorkstation();
94         }
95         return;
96     }
97     
98     var self = this;
99     // connect onclick handlers to the various navigation links
100     var linkHandlers = {
101         'oils-selfck-hold-details-link' : function() { self.drawHoldsPage(); },
102         'oils-selfck-view-fines-link' : function() { self.drawFinesPage(); },
103         'oils-selfck-pay-fines-link' : function() {
104             self.goToTab("payment");
105             self.drawPayFinesPage(
106                 self.patron, function() {
107                     self.updateFinesSummary();
108                     self.drawFinesPage();
109                 }
110             );
111         },
112         'oils-selfck-nav-home' : function() { self.drawCircPage(); },
113         'oils-selfck-nav-logout' : function() { self.logoutPatron(); },
114         'oils-selfck-nav-logout-print' : function() { self.logoutPatron(true); },
115         'oils-selfck-items-out-details-link' : function() { self.drawItemsOutPage(); },
116         'oils-selfck-print-list-link' : function() { self.printList(); }
117     }
118
119     for(var id in linkHandlers) 
120         dojo.connect(dojo.byId(id), 'onclick', linkHandlers[id]);
121
122
123     if(this.cgi.param('patron')) {
124         
125         // Patron barcode via cgi param.  Mainly used for debugging and
126         // only works if password is not required by policy
127         this.loginPatron(this.cgi.param('patron'));
128
129     } else {
130         this.drawLoginPage();
131     }
132
133     /**
134      * To test printing, pass a URL param of 'testprint'.  The value for the param
135      * should be a JSON string like so:  [{circ:<circ_id>}, ...]
136      */
137     var testPrint = this.cgi.param('testprint');
138     if(testPrint) {
139         this.checkouts = JSON2js(testPrint);
140         this.printSessionReceipt();
141         this.checkouts = [];
142     }
143 }
144
145
146 /**
147  * Registers a new workstion
148  */
149 SelfCheckManager.prototype.registerWorkstation = function() {
150     
151     oilsSelfckWsDialog.show();
152
153     new openils.User().buildPermOrgSelector(
154         'REGISTER_WORKSTATION', 
155         oilsSelfckWsLocSelector, 
156         this.staff.home_ou()
157     );
158
159
160     var self = this;
161     dojo.connect(oilsSelfckWsSubmit, 'onClick', 
162
163         function() {
164             oilsSelfckWsDialog.hide();
165             var name = oilsSelfckWsLocSelector.attr('displayedValue') + '-' + oilsSelfckWsName.attr('value');
166
167             var res = fieldmapper.standardRequest(
168                 ['open-ils.actor', 'open-ils.actor.workstation.register'],
169                 { params : [
170                         self.authtoken, name, oilsSelfckWsLocSelector.attr('value')
171                     ]
172                 }
173             );
174
175             if(evt = openils.Event.parse(res)) {
176                 if(evt.textcode == 'WORKSTATION_NAME_EXISTS') {
177                     if(confirm(localeStrings.WORKSTATION_EXISTS)) {
178                         location.href = location.href.replace(/\?.*/, '') + '?ws=' + name;
179                     } else {
180                         self.registerWorkstation();
181                     }
182                     return;
183                 } else {
184                     alert(evt);
185                 }
186             } else {
187                 location.href = location.href.replace(/\?.*/, '') + '?ws=' + name;
188             }
189         }
190     );
191 }
192
193 /**
194  * Loads the org unit settings
195  */
196 SelfCheckManager.prototype.loadOrgSettings = function() {
197
198     var settings = fieldmapper.aou.fetchOrgSettingBatch(
199         this.staff.ws_ou(), [
200             SET_BARCODE_REGEX,
201             SET_PATRON_TIMEOUT,
202             SET_ALERT_POPUP,
203             SET_ALERT_SOUND,
204             SET_AUTO_OVERRIDE_EVENTS,
205             SET_PATRON_PASSWORD_REQUIRED,
206             SET_AUTO_RENEW_INTERVAL,
207             SET_WORKSTATION_REQUIRED,
208             SET_CC_PAYMENT_ALLOWED
209         ]
210     );
211
212     for(k in settings) {
213         if(settings[k])
214             this.orgSettings[k] = settings[k].value;
215     }
216
217     if(settings[SET_BARCODE_REGEX]) 
218         this.patronBarcodeRegex = new RegExp(settings[SET_BARCODE_REGEX].value);
219 }
220
221 SelfCheckManager.prototype.drawLoginPage = function() {
222     var self = this;
223
224     var bcHandler = function(barcode) {
225         // handle patron barcode entry
226
227         if(self.orgSettings[SET_PATRON_PASSWORD_REQUIRED]) {
228             
229             // password is required.  wire up the scan box to read it
230             self.updateScanBox({
231                 msg : 'Please enter your password', // TODO i18n 
232                 handler : function(pw) { self.loginPatron(barcode, pw); },
233                 password : true
234             });
235
236         } else {
237             // password is not required, go ahead and login
238             self.loginPatron(barcode);
239         }
240     };
241
242     this.updateScanBox({
243         msg : 'Please log in with your library barcode.', // TODO
244         handler : bcHandler
245     });
246 }
247
248 /**
249  * Login the patron.  
250  */
251 SelfCheckManager.prototype.loginPatron = function(barcode, passwd) {
252
253     if(this.orgSettings[SET_PATRON_PASSWORD_REQUIRED]) {
254         
255         if(!passwd) {
256             // would only happen in dev/debug mode when using the patron= param
257             alert('password required by org setting.  remove patron= from URL'); 
258             return;
259         }
260
261         // patron password is required.  Verify it.
262
263         var res = fieldmapper.standardRequest(
264             ['open-ils.actor', 'open-ils.actor.verify_user_password'],
265             {params : [this.authtoken, barcode, null, hex_md5(passwd)]}
266         );
267
268         if(res == 0) {
269             // user-not-found results in login failure
270             this.handleAlert(
271                 dojo.string.substitute(localeStrings.LOGIN_FAILED, [barcode]),
272                 false, 'login-failure'
273             );
274             this.drawLoginPage();
275             return;
276         }
277     } 
278
279     // retrieve the fleshed user by barcode
280     this.patron = fieldmapper.standardRequest(
281         ['open-ils.actor', 'open-ils.actor.user.fleshed.retrieve_by_barcode'],
282         {params : [this.authtoken, barcode]}
283     );
284
285     var evt = openils.Event.parse(this.patron);
286     if(evt) {
287         this.handleAlert(
288             dojo.string.substitute(localeStrings.LOGIN_FAILED, [barcode]),
289             false, 'login-failure'
290         );
291         this.drawLoginPage();
292
293     } else {
294
295         this.handleAlert('', false, 'login-success');
296         dojo.byId('oils-selfck-user-banner').innerHTML = 'Welcome, ' + this.patron.first_given_name(); // TODO i18n
297         this.drawCircPage();
298     }
299 }
300
301
302 SelfCheckManager.prototype.handleAlert = function(message, shouldPopup, sound) {
303
304     console.log("Handling alert " + message);
305
306     dojo.byId('oils-selfck-status-div').innerHTML = message;
307
308     if(shouldPopup && this.orgSettings[SET_ALERT_POPUP]) 
309         alert(message);
310
311     if(sound && this.orgSettings[SET_ALERT_SOUND])
312         openils.Util.playAudioUrl(SelfCheckManager.audioConfig[sound]);
313 }
314
315
316 /**
317  * Manages the main input box
318  * @param msg The context message to display with the box
319  * @param clearOnly Don't update the context message, just clear the value and re-focus
320  * @param handler Optional "on-enter" handler.  
321  */
322 SelfCheckManager.prototype.updateScanBox = function(args) {
323     args = args || {};
324
325     if(args.select) {
326         selfckScanBox.domNode.select();
327     } else {
328         selfckScanBox.attr('value', '');
329     }
330
331     if(args.password) {
332         selfckScanBox.domNode.setAttribute('type', 'password');
333     } else {
334         selfckScanBox.domNode.setAttribute('type', '');
335     }
336
337     if(args.value)
338         selfckScanBox.attr('value', args.value);
339
340     if(args.msg) 
341         dojo.byId('oils-selfck-scan-text').innerHTML = args.msg;
342
343     if(selfckScanBox._lastHandler && (args.handler || args.clearHandler)) {
344         dojo.disconnect(selfckScanBox._lastHandler);
345     }
346
347     if(args.handler) {
348         selfckScanBox._lastHandler = dojo.connect(
349             selfckScanBox, 
350             'onKeyDown', 
351             function(e) {
352                 if(e.keyCode != dojo.keys.ENTER) 
353                     return;
354                 args.handler(selfckScanBox.attr('value'));
355             }
356         );
357     }
358
359     selfckScanBox.focus();
360 }
361
362 /**
363  *  Sets up the checkout/renewal interface
364  */
365 SelfCheckManager.prototype.drawCircPage = function() {
366
367     openils.Util.show('oils-selfck-circ-tbody', 'table-row-group');
368     this.goToTab('checkout');
369
370     while(this.itemsOutTbody.childNodes[0])
371         this.itemsOutTbody.removeChild(this.itemsOutTbody.childNodes[0]);
372
373     var self = this;
374     this.updateScanBox({
375         msg : 'Please enter an item barcode', // TODO i18n
376         handler : function(barcode) { self.checkout(barcode); }
377     });
378
379     if(!this.circTemplate)
380         this.circTemplate = this.circTbody.removeChild(dojo.byId('oils-selfck-circ-row'));
381
382     // fines summary
383     this.updateFinesSummary();
384
385     // holds summary
386     this.updateHoldsSummary();
387
388     // items out summary
389     this.updateCircSummary();
390
391     // render mock checkouts for debugging?
392     if(this.mockCheckouts) {
393         for(var i in [1,2,3]) 
394             this.displayCheckout(this.mockCheckout, 'checkout');
395     }
396 }
397
398
399 SelfCheckManager.prototype.updateFinesSummary = function() {
400     var self = this; 
401
402     // fines summary
403     fieldmapper.standardRequest(
404         ['open-ils.actor', 'open-ils.actor.user.fines.summary'],
405         {   async : true,
406             params : [this.authtoken, this.patron.id()],
407             oncomplete : function(r) {
408
409                 var summary = openils.Util.readResponse(r);
410
411                 dojo.byId('oils-selfck-fines-total').innerHTML = 
412                     dojo.string.substitute(
413                         localeStrings.TOTAL_FINES_ACCOUNT, 
414                         [summary.balance_owed()]
415                     );
416
417                 self.creditPayableBalance = summary.balance_owed();
418             }
419         }
420     );
421 }
422
423
424 SelfCheckManager.prototype.drawItemsOutPage = function() {
425     openils.Util.hide('oils-selfck-circ-tbody');
426
427     this.goToTab('items_out');
428
429     while(this.itemsOutTbody.childNodes[0])
430         this.itemsOutTbody.removeChild(this.itemsOutTbody.childNodes[0]);
431
432     progressDialog.show(true);
433     
434     var self = this;
435     fieldmapper.standardRequest(
436         ['open-ils.circ', 'open-ils.circ.actor.user.checked_out.atomic'],
437         {
438             async : true,
439             params : [this.authtoken, this.patron.id()],
440             oncomplete : function(r) {
441
442                 var resp = openils.Util.readResponse(r);
443
444                 var circs = resp.sort(
445                     function(a, b) {
446                         if(a.circ.due_date() > b.circ.due_date())
447                             return -1;
448                         return 1;
449                     }
450                 );
451
452                 progressDialog.hide();
453
454                 self.itemsOut = [];
455                 dojo.forEach(circs,
456                     function(circ) {
457                         self.itemsOut.push(circ.circ.id());
458                         self.displayCheckout(
459                             {payload : circ}, 
460                             (circ.circ.parent_circ()) ? 'renew' : 'checkout',
461                             true
462                         );
463                     }
464                 );
465             }
466         }
467     );
468 }
469
470
471 SelfCheckManager.prototype.goToTab = function(name) {
472     this.tabName = name;
473
474     openils.Util.hide('oils-selfck-fines-page');
475     openils.Util.hide('oils-selfck-payment-page');
476     openils.Util.hide('oils-selfck-holds-page');
477     openils.Util.hide('oils-selfck-circ-page');
478     openils.Util.hide('oils-selfck-pay-fines-link');
479     
480     switch(name) {
481         case 'checkout':
482             openils.Util.show('oils-selfck-circ-page');
483             break;
484         case 'items_out':
485             openils.Util.show('oils-selfck-circ-page');
486             break;
487         case 'holds':
488             openils.Util.show('oils-selfck-holds-page');
489             break;
490         case 'fines':
491             openils.Util.show('oils-selfck-fines-page');
492             break;
493         case 'payment':
494             openils.Util.show('oils-selfck-payment-page');
495             break;
496     }
497 }
498
499
500 SelfCheckManager.prototype.printList = function() {
501     switch(this.tabName) {
502         case 'checkout':
503             this.printSessionReceipt();
504             break;
505         case 'items_out':
506             this.printItemsOutReceipt();
507             break;
508         case 'holds':
509             this.printHoldsReceipt();
510             break;
511         case 'fines':
512             this.printFinesReceipt();
513             break;
514     }
515 }
516
517 SelfCheckManager.prototype.updateHoldsSummary = function() {
518
519     if(!this.holdsSummary) {
520         var summary = fieldmapper.standardRequest(
521             ['open-ils.circ', 'open-ils.circ.holds.user_summary'],
522             {params : [this.authtoken, this.patron.id()]}
523         );
524
525         this.holdsSummary = {};
526         this.holdsSummary.ready = Number(summary['4']);
527         this.holdsSummary.total = 0;
528
529         for(var i in summary) 
530             this.holdsSummary.total += Number(summary[i]);
531     }
532
533     dojo.byId('oils-selfck-holds-total').innerHTML = 
534         dojo.string.substitute(
535             localeStrings.TOTAL_HOLDS, 
536             [this.holdsSummary.total]
537         );
538
539     dojo.byId('oils-selfck-holds-ready').innerHTML = 
540         dojo.string.substitute(
541             localeStrings.HOLDS_READY_FOR_PICKUP, 
542             [this.holdsSummary.ready]
543         );
544 }
545
546
547 SelfCheckManager.prototype.updateCircSummary = function(increment) {
548
549     if(!this.circSummary) {
550
551         var summary = fieldmapper.standardRequest(
552             ['open-ils.actor', 'open-ils.actor.user.checked_out.count'],
553             {params : [this.authtoken, this.patron.id()]}
554         );
555
556         this.circSummary = {
557             total : Number(summary.out) + Number(summary.overdue),
558             overdue : Number(summary.overdue),
559             session : 0
560         };
561     }
562
563     if(increment) {
564         // local checkout occurred.  Add to the total and the session.
565         this.circSummary.total += 1;
566         this.circSummary.session += 1;
567     }
568
569     dojo.byId('oils-selfck-circ-account-total').innerHTML = 
570         dojo.string.substitute(
571             localeStrings.TOTAL_ITEMS_ACCOUNT, 
572             [this.circSummary.total]
573         );
574
575     dojo.byId('oils-selfck-circ-session-total').innerHTML = 
576         dojo.string.substitute(
577             localeStrings.TOTAL_ITEMS_SESSION, 
578             [this.circSummary.session]
579         );
580 }
581
582
583 SelfCheckManager.prototype.drawHoldsPage = function() {
584
585     // TODO add option to hid scanBox
586     // this.updateScanBox(...)
587
588     this.goToTab('holds');
589
590     this.holdTbody = dojo.byId('oils-selfck-hold-tbody');
591     if(!this.holdTemplate)
592         this.holdTemplate = this.holdTbody.removeChild(dojo.byId('oils-selfck-hold-row'));
593     while(this.holdTbody.childNodes[0])
594         this.holdTbody.removeChild(this.holdTbody.childNodes[0]);
595
596     progressDialog.show(true);
597
598     var self = this;
599     fieldmapper.standardRequest( // fetch the hold IDs
600
601         ['open-ils.circ', 'open-ils.circ.holds.id_list.retrieve'],
602         {   async : true,
603             params : [this.authtoken, this.patron.id()],
604
605             oncomplete : function(r) { 
606                 var ids = openils.Util.readResponse(r);
607                 if(!ids || ids.length == 0) {
608                     progressDialog.hide();
609                     return;
610                 }
611
612                 fieldmapper.standardRequest( // fetch the hold objects with fleshed details
613                     ['open-ils.circ', 'open-ils.circ.hold.details.batch.retrieve.atomic'],
614                     {   async : true,
615                         params : [self.authtoken, ids],
616
617                         oncomplete : function(rr) {
618                             self.drawHolds(openils.Util.readResponse(rr));
619                         }
620                     }
621                 );
622             }
623         }
624     );
625 }
626
627 /**
628  * Fetch and add a single hold to the list of holds
629  */
630 SelfCheckManager.prototype.drawHolds = function(holds) {
631
632     holds = holds.sort(
633         // sort available holds to the top of the list
634         // followed by queue position order
635         function(a, b) {
636             if(a.status == 4) return -1;
637             if(a.queue_position < b.queue_position) return -1;
638             return 1;
639         }
640     );
641
642     this.holds = holds;
643
644     progressDialog.hide();
645
646     for(var i in holds) {
647
648         var data = holds[i];
649         var row = this.holdTemplate.cloneNode(true);
650
651         if(data.mvr.isbn()) {
652             this.byName(row, 'jacket').setAttribute('src', '/opac/extras/ac/jacket/small/' + data.mvr.isbn());
653         }
654
655         this.byName(row, 'title').innerHTML = data.mvr.title();
656         this.byName(row, 'author').innerHTML = data.mvr.author();
657
658         if(data.status == 4) {
659
660             // hold is ready for pickup
661             this.byName(row, 'status').innerHTML = localeStrings.HOLD_STATUS_READY;
662
663         } else {
664
665             // hold is still pending
666             this.byName(row, 'status').innerHTML = 
667                 dojo.string.substitute(
668                     localeStrings.HOLD_STATUS_WAITING,
669                     [data.queue_position, data.potential_copies]
670                 );
671         }
672
673         this.holdTbody.appendChild(row);
674     }
675 }
676
677
678 SelfCheckManager.prototype.drawFinesPage = function() {
679
680     // TODO add option to hid scanBox
681     // this.updateScanBox(...)
682
683     this.goToTab('fines');
684     progressDialog.show(true);
685
686     if(this.creditPayableBalance > 0 && this.orgSettings[SET_CC_PAYMENT_ALLOWED]) {
687         openils.Util.show('oils-selfck-pay-fines-link', 'inline');
688     }
689
690     this.finesTbody = dojo.byId('oils-selfck-fines-tbody');
691     if(!this.finesTemplate)
692         this.finesTemplate = this.finesTbody.removeChild(dojo.byId('oils-selfck-fines-row'));
693     while(this.finesTbody.childNodes[0])
694         this.finesTbody.removeChild(this.finesTbody.childNodes[0]);
695
696     // when user clicks on a selector checkbox, update the total owed
697     var updateSelected = function() {
698         var total = 0;
699         dojo.forEach(
700             dojo.query('[name=selector]', this.finesTbody),
701             function(input) {
702                 if(input.checked)
703                     total += Number(input.getAttribute('balance_owed'));
704             }
705         );
706
707         total = total.toFixed(2);
708         dojo.byId('oils-selfck-selected-total').innerHTML = 
709             dojo.string.substitute(localeStrings.TOTAL_FINES_SELECTED, [total]);
710     }
711
712     // wire up the batch on/off selector
713     var sel = dojo.byId('oils-selfck-fines-selector');
714     sel.onchange = function() {
715         dojo.forEach(
716             dojo.query('[name=selector]', this.finesTbody),
717             function(input) {
718                 input.checked = sel.checked;
719             }
720         );
721     };
722
723     var self = this;
724     var handler = function(dataList) {
725
726         self.finesCount = dataList.length;
727         self.finesData = dataList;
728
729         for(var i in dataList) {
730
731             var data = dataList[i];
732             var row = self.finesTemplate.cloneNode(true);
733             var type = data.transaction.xact_type();
734
735             if(type == 'circulation') {
736                 self.byName(row, 'type').innerHTML = type;
737                 self.byName(row, 'details').innerHTML = data.record.title();
738
739             } else if(type == 'grocery') {
740                 self.byName(row, 'type').innerHTML = 'Miscellaneous'; // Go ahead and head off any confusion around "grocery".  TODO i18n
741                 self.byName(row, 'details').innerHTML = data.transaction.last_billing_type();
742             }
743
744             self.byName(row, 'total_owed').innerHTML = data.transaction.total_owed();
745             self.byName(row, 'total_paid').innerHTML = data.transaction.total_paid();
746             self.byName(row, 'balance').innerHTML = data.transaction.balance_owed();
747
748             // row selector
749             var selector = self.byName(row, 'selector')
750             selector.onchange = updateSelected;
751             selector.setAttribute('xact', data.transaction.id());
752             selector.setAttribute('balance_owed', data.transaction.balance_owed());
753             selector.checked = true;
754
755             self.finesTbody.appendChild(row);
756         }
757
758         updateSelected();
759     }
760
761
762     fieldmapper.standardRequest( 
763         ['open-ils.actor', 'open-ils.actor.user.transactions.have_balance.fleshed'],
764         {   async : true,
765             params : [this.authtoken, this.patron.id()],
766             oncomplete : function(r) { 
767                 progressDialog.hide();
768                 handler(openils.Util.readResponse(r));
769             }
770         }
771     );
772 }
773
774 SelfCheckManager.prototype.checkin = function(barcode, abortTransit) {
775
776     var resp = fieldmapper.standardRequest(
777         ['open-ils.circ', 'open-ils.circ.transit.abort'],
778         {params : [this.authtoken, {barcode : barcode}]}
779     );
780
781     // resp == 1 on success
782     if(openils.Event.parse(resp))
783         return false;
784
785     var resp = fieldmapper.standardRequest(
786         ['open-ils.circ', 'open-ils.circ.checkin.override'],
787         {params : [
788             this.authtoken, {
789                 patron_id : this.patron.id(),
790                 copy_barcode : barcode,
791                 noop : true
792             }
793         ]}
794     );
795
796     if(!resp.length) resp = [resp];
797     for(var i = 0; i < resp.length; i++) {
798         var tc = openils.Event.parse(resp[i]).textcode;
799         if(tc == 'SUCCESS' || tc == 'NO_CHANGE') {
800             continue;
801         } else {
802             return false;
803         }
804     }
805
806     return true;
807 }
808
809 /**
810  * Check out a single item.  If the item is already checked 
811  * out to the patron, redirect to renew()
812  */
813 SelfCheckManager.prototype.checkout = function(barcode, override) {
814
815     this.prevCirc = null;
816
817     if(!barcode) {
818         this.updateScanbox(null, true);
819         return;
820     }
821
822     if(this.mockCheckouts) {
823         // if we're in mock-checkout mode, just insert another
824         // fake circ into the table and get out of here.
825         this.displayCheckout(this.mockCheckout, 'checkout');
826         return;
827     }
828
829     // TODO see if it's a patron barcode
830     // TODO see if this item has already been checked out in this session
831
832     var method = 'open-ils.circ.checkout.full';
833     if(override) method += '.override';
834
835     console.log("Checkout out item " + barcode + " with method " + method);
836
837     var result = fieldmapper.standardRequest(
838         ['open-ils.circ', 'open-ils.circ.checkout.full'],
839         {params: [
840             this.authtoken, {
841                 patron_id : this.patron.id(),
842                 copy_barcode : barcode
843             }
844         ]}
845     );
846
847     var stat = this.handleXactResult('checkout', barcode, result);
848
849     if(stat.override) {
850         this.checkout(barcode, true);
851     } else if(stat.doOver) {
852         this.checkout(barcode);
853     } else if(stat.renew) {
854         this.renew(barcode);
855     }
856 }
857
858
859 SelfCheckManager.prototype.handleXactResult = function(action, item, result) {
860
861     var displayText = '';
862
863     // If true, the display message is important enough to pop up.  Whether or not
864     // an alert() actually occurs, depends on org unit settings
865     var popup = false;  
866     var sound = ''; // sound file reference
867     var payload = result.payload || {};
868     var overrideEvents = this.orgSettings[SET_AUTO_OVERRIDE_EVENTS];
869         
870     if(result.textcode == 'NO_SESSION') {
871
872         return this.logoutStaff();
873
874     } else if(result.textcode == 'SUCCESS') {
875
876         if(action == 'checkout') {
877
878             displayText = dojo.string.substitute(localeStrings.CHECKOUT_SUCCESS, [item]);
879             this.displayCheckout(result, 'checkout');
880
881             if(payload.holds_fulfilled && payload.holds_fulfilled.length) {
882                 // A hold was fulfilled, update the hold numbers in the circ summary
883                 console.log("fulfilled hold " + payload.holds_fulfilled + " during checkout");
884                 this.holdsSummary = null;
885                 this.updateHoldsSummary();
886             }
887
888             this.updateCircSummary(true);
889
890         } else if(action == 'renew') {
891
892             displayText = dojo.string.substitute(localeStrings.RENEW_SUCCESS, [item]);
893             this.displayCheckout(result, 'renew');
894         }
895
896         this.checkouts.push({circ : result.payload.circ.id()});
897         sound = 'checkout-success';
898         this.updateScanBox();
899
900     } else if(result.textcode == 'OPEN_CIRCULATION_EXISTS' && action == 'checkout') {
901
902         // Server says the item is already checked out.  If it's checked out to the
903         // current user, we may need to renew it.  
904
905         if(payload.old_circ) { 
906
907             /*
908             old_circ refers to the previous checkout IFF it's for the same user. 
909             If no auto-renew interval is not defined, assume we should renew it
910             If an auto-renew interval is defined and the payload comes back with
911             auto_renew set to true, do the renewal.  Otherwise, let the patron know
912             the item is already checked out to them.  */
913
914             if( !this.orgSettings[SET_AUTO_RENEW_INTERVAL] ||
915                 (this.orgSettings[SET_AUTO_RENEW_INTERVAL] && payload.auto_renew) ) {
916                 this.prevCirc = payload.old_circ.id();
917                 return { renew : true };
918             }
919
920             popup = true;
921             sound = 'checkout-failure';
922             displayText = dojo.string.substitute(localeStrings.ALREADY_OUT, [item]);
923
924         } else {
925
926             if( // copy is marked lost.  if configured to do so, check it in and try again.
927                 result.payload.copy && 
928                 result.payload.copy.status() == /* LOST */ 3 &&
929                 overrideEvents && overrideEvents.length &&
930                 overrideEvents.indexOf('COPY_STATUS_LOST') != -1) {
931
932                     if(this.checkin(item)) {
933                         return { doOver : true };
934                     }
935             }
936
937             
938             // item is checked out to some other user
939             popup = true;
940             sound = 'checkout-failure';
941             displayText = dojo.string.substitute(localeStrings.OPEN_CIRCULATION_EXISTS, [item]);
942         }
943
944         this.updateScanBox({select:true});
945
946     } else {
947
948     
949         if(overrideEvents && overrideEvents.length) {
950             
951             // see if the events we received are all in the list of
952             // events to override
953     
954             if(!result.length) result = [result];
955     
956             var override = true;
957             for(var i = 0; i < result.length; i++) {
958                 var match = overrideEvents.filter(
959                     function(e) { return (e == result[i].textcode); })[0];
960                 if(!match) {
961                     override = false;
962                     break;
963                 }
964
965                 if(result[i].textcode == 'COPY_IN_TRANSIT') {
966                     // to override a transit, we have to abort the transit and check it in first
967                     if(this.checkin(item, true)) {
968                         return { doOver : true };
969                     } else {
970                         override = false;
971                     }
972
973                 }
974             }
975
976             if(override) 
977                 return { override : true };
978         }
979     
980         this.updateScanBox({select : true});
981         popup = true;
982         sound = 'checkout-failure';
983
984         if(action == 'renew')
985             this.checkouts.push({circ : this.prevCirc, renewal_failure : true});
986
987         if(result.length) 
988             result = result[0];
989
990         switch(result.textcode) {
991
992             // TODO custom handler for blocking penalties
993
994             case 'MAX_RENEWALS_REACHED' :
995                 displayText = dojo.string.substitute(
996                     localeStrings.MAX_RENEWALS, [item]);
997                 break;
998
999             case 'ITEM_NOT_CATALOGED' :
1000                 displayText = dojo.string.substitute(
1001                     localeStrings.ITEM_NOT_CATALOGED, [item]);
1002                 break;
1003
1004             case 'OPEN_CIRCULATION_EXISTS' :
1005                 displayText = dojo.string.substitute(
1006                     localeStrings.OPEN_CIRCULATION_EXISTS, [item]);
1007
1008                 break;
1009
1010             default:
1011                 console.error('Unhandled event ' + result.textcode);
1012
1013                 if(action == 'checkout' || action == 'renew') {
1014                     displayText = dojo.string.substitute(
1015                         localeStrings.GENERIC_CIRC_FAILURE, [item]);
1016                 } else {
1017                     displayText = dojo.string.substitute(
1018                         localeStrings.UNKNOWN_ERROR, [result.textcode]);
1019                 }
1020         }
1021     }
1022
1023     this.handleAlert(displayText, popup, sound);
1024     return {};
1025 }
1026
1027
1028 /**
1029  * Renew an item
1030  */
1031 SelfCheckManager.prototype.renew = function(barcode, override) {
1032
1033     var method = 'open-ils.circ.renew';
1034     if(override) method += '.override';
1035
1036     console.log("Renewing item " + barcode + " with method " + method);
1037
1038     var result = fieldmapper.standardRequest(
1039         ['open-ils.circ', method],
1040         {params: [
1041             this.authtoken, {
1042                 patron_id : this.patron.id(),
1043                 copy_barcode : barcode
1044             }
1045         ]}
1046     );
1047
1048     console.log(js2JSON(result));
1049
1050     var stat = this.handleXactResult('renew', barcode, result);
1051
1052     if(stat.override)
1053         this.renew(barcode, true);
1054 }
1055
1056 /**
1057  * Display the result of a checkout or renewal in the items out table
1058  */
1059 SelfCheckManager.prototype.displayCheckout = function(evt, type, itemsOut) {
1060
1061     var copy = evt.payload.copy;
1062     var record = evt.payload.record;
1063     var circ = evt.payload.circ;
1064     var row = this.circTemplate.cloneNode(true);
1065
1066     if(record.isbn()) {
1067         this.byName(row, 'jacket').setAttribute('src', '/opac/extras/ac/jacket/small/' + record.isbn());
1068     }
1069
1070     this.byName(row, 'barcode').innerHTML = copy.barcode();
1071     this.byName(row, 'title').innerHTML = record.title();
1072     this.byName(row, 'author').innerHTML = record.author();
1073     this.byName(row, 'remaining').innerHTML = circ.renewal_remaining();
1074     openils.Util.show(this.byName(row, type));
1075
1076     var date = dojo.date.stamp.fromISOString(circ.due_date());
1077     this.byName(row, 'due_date').innerHTML = 
1078         dojo.date.locale.format(date, {selector : 'date'});
1079
1080     // put new circs at the top of the list
1081     var tbody = this.circTbody;
1082     if(itemsOut) tbody = this.itemsOutTbody;
1083     tbody.insertBefore(row, tbody.getElementsByTagName('tr')[0]);
1084 }
1085
1086
1087 SelfCheckManager.prototype.byName = function(node, name) {
1088     return dojo.query('[name=' + name+']', node)[0];
1089 }
1090
1091
1092 SelfCheckManager.prototype.initPrinter = function() {
1093     try { // Mozilla only
1094                 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
1095         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
1096         netscape.security.PrivilegeManager.enablePrivilege('UniversalPreferencesRead');
1097         netscape.security.PrivilegeManager.enablePrivilege('UniversalPreferencesWrite');
1098         var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
1099         if (pref)
1100             pref.setBoolPref('print.always_print_silent', true);
1101     } catch(E) {
1102         console.log("Unable to initialize auto-printing"); 
1103     }
1104 }
1105
1106 /**
1107  * Print a receipt for this session's checkouts
1108  */
1109 SelfCheckManager.prototype.printSessionReceipt = function(callback) {
1110
1111     var circIds = [];
1112     var circCtx = []; // circ context data.  in this case, renewal_failure info
1113
1114     // collect the circs and failure info
1115     dojo.forEach(
1116         this.checkouts, 
1117         function(blob) {
1118             circIds.push(blob.circ);
1119             circCtx.push({renewal_failure:blob.renewal_failure});
1120         }
1121     );
1122
1123     var params = [
1124         this.authtoken, 
1125         this.staff.ws_ou(),
1126         null,
1127         'format.selfcheck.checkout',
1128         'print-on-demand',
1129         circIds,
1130         circCtx
1131     ];
1132
1133     var self = this;
1134     fieldmapper.standardRequest(
1135         ['open-ils.circ', 'open-ils.circ.fire_circ_trigger_events'],
1136         {   
1137             async : true,
1138             params : params,
1139             oncomplete : function(r) {
1140                 var resp = openils.Util.readResponse(r);
1141                 var output = resp.template_output();
1142                 if(output) {
1143                     self.printData(output.data(), self.checkouts.length, callback); 
1144                 } else {
1145                     var error = resp.error_output();
1146                     if(error) {
1147                         throw new Error("Error creating receipt: " + error.data());
1148                     } else {
1149                         throw new Error("No receipt data returned from server");
1150                     }
1151                 }
1152             }
1153         }
1154     );
1155 }
1156
1157 SelfCheckManager.prototype.printData = function(data, numItems, callback) {
1158
1159     var win = window.open('', '', 'resizable,width=700,height=500,scrollbars=1'); 
1160     win.document.body.innerHTML = data;
1161     win.print();
1162
1163     /*
1164      * There is no way to know when the browser is done printing.
1165      * Make a best guess at when to close the print window by basing
1166      * the setTimeout wait on the number of items to be printed plus
1167      * a small buffer
1168      */
1169     var sleepTime = 1000;
1170     if(numItems > 0) 
1171         sleepTime += (numItems / 2) * 1000;
1172
1173     setTimeout(
1174         function() { 
1175             win.close(); // close the print window
1176             if(callback)
1177                 callback(); // fire optional post-print callback
1178         },
1179         sleepTime 
1180     );
1181 }
1182
1183
1184 /**
1185  * Print a receipt for this user's items out
1186  */
1187 SelfCheckManager.prototype.printItemsOutReceipt = function(callback) {
1188
1189     if(!this.itemsOut.length) return;
1190
1191     progressDialog.show(true);
1192
1193     var params = [
1194         this.authtoken, 
1195         this.staff.ws_ou(),
1196         null,
1197         'format.selfcheck.items_out',
1198         'print-on-demand',
1199         this.itemsOut
1200     ];
1201
1202     var self = this;
1203     fieldmapper.standardRequest(
1204         ['open-ils.circ', 'open-ils.circ.fire_circ_trigger_events'],
1205         {   
1206             async : true,
1207             params : params,
1208             oncomplete : function(r) {
1209                 progressDialog.hide();
1210                 var resp = openils.Util.readResponse(r);
1211                 var output = resp.template_output();
1212                 if(output) {
1213                     self.printData(output.data(), self.itemsOut.length, callback); 
1214                 } else {
1215                     var error = resp.error_output();
1216                     if(error) {
1217                         throw new Error("Error creating receipt: " + error.data());
1218                     } else {
1219                         throw new Error("No receipt data returned from server");
1220                     }
1221                 }
1222             }
1223         }
1224     );
1225 }
1226
1227 /**
1228  * Print a receipt for this user's items out
1229  */
1230 SelfCheckManager.prototype.printHoldsReceipt = function(callback) {
1231
1232     if(!this.holds.length) return;
1233
1234     progressDialog.show(true);
1235
1236     var holdIds = [];
1237     var holdData = [];
1238
1239     dojo.forEach(this.holds,
1240         function(data) {
1241             holdIds.push(data.hold.id());
1242             if(data.status == 4) {
1243                 holdData.push({ready : true});
1244             } else {
1245                 holdData.push({
1246                     queue_position : data.queue_position, 
1247                     potential_copies : data.potential_copies
1248                 });
1249             }
1250         }
1251     );
1252
1253     var params = [
1254         this.authtoken, 
1255         this.staff.ws_ou(),
1256         null,
1257         'format.selfcheck.holds',
1258         'print-on-demand',
1259         holdIds,
1260         holdData
1261     ];
1262
1263     var self = this;
1264     fieldmapper.standardRequest(
1265         ['open-ils.circ', 'open-ils.circ.fire_hold_trigger_events'],
1266         {   
1267             async : true,
1268             params : params,
1269             oncomplete : function(r) {
1270                 progressDialog.hide();
1271                 var resp = openils.Util.readResponse(r);
1272                 var output = resp.template_output();
1273                 if(output) {
1274                     self.printData(output.data(), self.holds.length, callback); 
1275                 } else {
1276                     var error = resp.error_output();
1277                     if(error) {
1278                         throw new Error("Error creating receipt: " + error.data());
1279                     } else {
1280                         throw new Error("No receipt data returned from server");
1281                     }
1282                 }
1283             }
1284         }
1285     );
1286 }
1287
1288
1289 /**
1290  * Print a receipt for this user's items out
1291  */
1292 SelfCheckManager.prototype.printFinesReceipt = function(callback) {
1293
1294     progressDialog.show(true);
1295
1296     var params = [
1297         this.authtoken, 
1298         this.staff.ws_ou(),
1299         null,
1300         'format.selfcheck.fines',
1301         'print-on-demand',
1302         [this.patron.id()]
1303     ];
1304
1305     var self = this;
1306     fieldmapper.standardRequest(
1307         ['open-ils.circ', 'open-ils.circ.fire_user_trigger_events'],
1308         {   
1309             async : true,
1310             params : params,
1311             oncomplete : function(r) {
1312                 progressDialog.hide();
1313                 var resp = openils.Util.readResponse(r);
1314                 var output = resp.template_output();
1315                 if(output) {
1316                     self.printData(output.data(), self.finesCount, callback); 
1317                 } else {
1318                     var error = resp.error_output();
1319                     if(error) {
1320                         throw new Error("Error creating receipt: " + error.data());
1321                     } else {
1322                         throw new Error("No receipt data returned from server");
1323                     }
1324                 }
1325             }
1326         }
1327     );
1328 }
1329
1330
1331
1332
1333 /**
1334  * Logout the patron and return to the login page
1335  */
1336 SelfCheckManager.prototype.logoutPatron = function(print) {
1337     if(print && this.checkouts.length) {
1338         this.printSessionReceipt(
1339             function() {
1340                 location.href = location.href;
1341             }
1342         );
1343     } else {
1344         location.href = location.href;
1345     }
1346 }
1347
1348
1349 /**
1350  * Fire up the manager on page load
1351  */
1352 openils.Util.addOnLoad(
1353     function() {
1354         new SelfCheckManager().init();
1355     }
1356 );