Lazy Circ (AKA partial barcode lookup)
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / circ / checkout.js
1 dump('entering circ.checkout.js\n');
2
3 if (typeof circ == 'undefined') { circ = {}; }
4 circ.checkout = function (params) {
5
6     JSAN.use('util.error'); this.error = new util.error();
7     JSAN.use('util.network'); this.network = new util.network();
8     JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init({'via':'stash'});
9     JSAN.use('util.money');
10     JSAN.use('util.barcode');
11 };
12
13 circ.checkout.prototype = {
14
15     'init' : function( params ) {
16
17         var obj = this;
18
19         obj.patron_id = params.patron_id;
20
21         JSAN.use('circ.util');
22         var columns = circ.util.columns( 
23             { 
24                 'barcode' : { 'hidden' : false },
25                 'title' : { 'hidden' : false },
26                 'due_date' : { 'hidden' : false }
27             } 
28         );
29
30         JSAN.use('util.list'); obj.list = new util.list('checkout_list');
31         obj.list.init(
32             {
33                 'columns' : columns,
34                 'map_row_to_columns' : circ.util.std_map_row_to_columns(),
35                 'on_select' : function() {
36                     var sel = obj.list.retrieve_selection();
37                     document.getElementById('clip_button').disabled = sel.length < 1;
38                 }
39             }
40         );
41         
42         JSAN.use('util.controller'); obj.controller = new util.controller();
43         obj.controller.init(
44             {
45                 'control_map' : {
46                     'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
47                     'sel_clip' : [
48                         ['command'],
49                         function() { obj.list.clipboard(); }
50                     ],
51                     'checkout_menu_placeholder' : [
52                         ['render'],
53                         function(e) {
54                             return function() {
55                                 JSAN.use('util.widgets'); JSAN.use('util.functional'); JSAN.use('util.fm_utils');
56                                 var items = [ [ document.getElementById('circStrings').getString('staff.circ.checkout.barcode') , 'barcode' ] ].concat(
57                                     util.functional.map_list(
58                                         util.functional.filter_list(
59                                             obj.data.list.my_cnct,
60                                             function(o) {
61                                                 return util.fm_utils.compare_aou_a_is_b_or_ancestor(o.owning_lib(), obj.data.list.au[0].ws_ou());
62                                             }
63                                         ).sort(
64
65                                             function(a,b) {
66                                                 try { 
67                                                     return util.fm_utils.sort_func_aou_by_depth_and_then_string(
68                                                         [ a.owning_lib(), a.name() ],
69                                                         [ b.owning_lib(), b.name() ]
70                                                     );
71                                                 } catch(E) {
72                                                     alert(document.getElementById('circStrings').getFormattedString('staff.circ.checkout.sorting.exception', [E]));
73                                                     return 0;
74                                                 }
75                                             }
76
77                                         ),
78                                         function(o) {
79                                             return [ obj.data.hash.aou[ o.owning_lib() ].shortname() + ' : ' + o.name(), o.id() ];
80                                         }
81                                     )
82                                 );
83                                 g.error.sdump('D_TRACE','items = ' + js2JSON(items));
84                                 util.widgets.remove_children( e );
85                                 var ml = util.widgets.make_menulist(items);
86                                 e.appendChild( ml );
87                                 ml.setAttribute('id','checkout_menulist');
88                                 ml.setAttribute('accesskey','');
89                                 ml.addEventListener(
90                                     'command',
91                                     function(ev) {
92                                         var tb = obj.controller.view.checkout_barcode_entry_textbox;
93                                         var db = document.getElementById('duedate_hbox');
94                                         if (ev.target.value == 'barcode') {
95                                             db.hidden = false;
96                                             tb.disabled = false;
97                                             tb.value = '';
98                                             tb.focus();
99                                         } else {
100                                             db.hidden = true;
101                                             tb.disabled = true;
102                                             tb.value = document.getElementById('circStrings').getString('staff.circ.non_cataloged');
103                                         }
104                                     }, false
105                                 );
106                                 obj.controller.view.checkout_menu = ml;
107                             };
108                         }
109                     ],
110                     'checkout_barcode_entry_textbox' : [
111                         ['keypress'],
112                         function(ev) {
113                             if (ev.keyCode && ev.keyCode == 13) {
114                                 obj.checkout( { barcode: ev.target.value } );
115                             }
116                         }
117                     ],
118                     'checkout_duedate_datepicker' : [
119                         ['change'],
120                         function(ev) { 
121                             try {
122                                 document.getElementById('checkout_duedate_checkbox').checked = true;
123                                 if (obj.check_date(ev.target)) {
124                                     ev.target.parentNode.setAttribute('style','');
125                                 } else {
126                                     ev.target.parentNode.setAttribute('style','background-color: red');
127                                 }
128                             } catch(E) {
129                                 alert('Error in checkout.js, checkout_duedate_datepicker @change: ' + E);
130                             }
131                         }
132                     ],
133                     'cmd_broken' : [
134                         ['command'],
135                         function() { alert(document.getElementById('circStrings').getString('staff.circ.checkout.unimplemented')); }
136                     ],
137                     'cmd_checkout_submit' : [
138                         ['command'],
139                         function() {
140                             var params = {}; var count = 1;
141
142                             if (obj.controller.view.checkout_menu.value == 'barcode' ||
143                                 obj.controller.view.checkout_menu.value === '') {
144                                 params.barcode = obj.controller.view.checkout_barcode_entry_textbox.value;
145                             } else {
146                                 params.noncat = 1;
147                                 params.noncat_type = obj.controller.view.checkout_menu.value;
148                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
149                                 var r = window.prompt(
150                                     document.getElementById('circStrings').getFormattedString('staff.circ.checkout.cmd_checkout_submit.msg', [obj.data.hash.cnct[params.noncat_type].name()]),
151                                     '1',
152                                     document.getElementById('circStrings').getString('staff.circ.checkout.cmd_checkout_submit.title')
153                                 );
154                                 if (r) {
155                                     count = Number(r);
156                                     if (count > 0) {
157                                         if (count > 99) {
158                                             obj.error.yns_alert(
159                                                 document.getElementById('circStrings').getFormattedString('staff.circ.checkout.cmd_checkout_submit.too_many.msg', [count, obj.data.hash.cnct[params.noncat_type].name()]),
160                                                 document.getElementById('circStrings').getString('staff.circ.checkout.cmd_checkout_submit.too_many.title'),
161                                                 document.getElementById('circStrings').getString('staff.circ.checkout.ok.btn'),
162                                                 null,
163                                                 null,
164                                                 document.getElementById('circStrings').getString('staff.circ.confirm')
165                                             );
166                                             return;
167                                         } else if (count > 20) {
168                                             r = obj.error.yns_alert(
169                                                 document.getElementById('circStrings').getFormattedString('staff.circ.checkout.cmd_checkout_submit.confirm.msg', [count, obj.data.hash.cnct[params.noncat_type].name()]),
170                                                 document.getElementById('circStrings').getString('staff.circ.checkout.cmd_checkout_submit.confirm.title'),
171                                                 document.getElementById('circStrings').getString('staff.circ.checkout.yes.btn'),
172                                                 document.getElementById('circStrings').getString('staff.circ.checkout.no.btn'),
173                                                 null,
174                                                 document.getElementById('circStrings').getString('staff.circ.confirm')
175                                             );
176                                             if (r !== 0) { return; }
177                                         }
178                                     } else {
179                                         r = obj.error.yns_alert(
180                                             document.getElementById('circStrings').getFormattedString('staff.circ.checkout.cmd_checkout_submit.non_numeric.msg', [r]),
181                                             document.getElementById('circStrings').getString('staff.circ.checkout.cmd_checkout_submit.non_numeric.title'),
182                                             document.getElementById('circStrings').getString('staff.circ.checkout.ok.btn'),
183                                             null,
184                                             null,
185                                             document.getElementById('circStrings').getString('staff.circ.confirm')
186                                         );
187                                         return;
188                                     }
189                                 } else {
190                                     return;
191                                 }
192                             }
193                             for (var i = 0; i < count; i++) {
194                                 obj.checkout( params );
195                             }
196                         }
197                     ],
198                     'cmd_checkout_print' : [
199                         ['command'],
200                         function() {
201                             try {
202                                 obj.print();
203                             } catch(E) {
204                                 obj.error.standard_unexpected_error_alert('cmd_checkout_print',E);
205                             }
206
207                         }
208                     ],
209                     'cmd_checkout_export' : [
210                         ['command'],
211                         function() {
212                             try {
213                                 obj.export_list();
214                             } catch(E) {
215                                 obj.error.standard_unexpected_error_alert('cmd_checkout_export',E); 
216                             }
217                         }
218                     ],
219
220
221                     'cmd_checkout_reprint' : [
222                         ['command'],
223                         function() {
224                             JSAN.use('util.print'); var print = new util.print();
225                             print.reprint_last();
226                         }
227                     ],
228                     'cmd_checkout_done' : [
229                         ['command'],
230                         function() {
231                             try {
232                                 var no_print_prompting = obj.data.hash.aous['circ.staff_client.do_not_auto_attempt_print'];
233                                 if (no_print_prompting) {
234                                     if (no_print_prompting.indexOf( "Checkout" ) > -1) {
235                                         obj.list.clear();
236                                         xulG.set_tab(urls.XUL_PATRON_BARCODE_ENTRY,{},{}); 
237                                         return;
238                                     }
239                                 }
240                                 if (document.getElementById('checkout_auto').checked) {
241                                     obj.print(true,function() { 
242                                         obj.list.clear();
243                                         xulG.set_tab(urls.XUL_PATRON_BARCODE_ENTRY,{},{}); 
244                                     });
245                                 } else {
246                                     obj.print(false,function() {
247                                         obj.list.clear();
248                                         xulG.set_tab(urls.XUL_PATRON_BARCODE_ENTRY,{},{});
249                                     });
250                                 }
251                             } catch(E) {
252                                 obj.error.standard_unexpected_error_alert('cmd_checkout_done',E);
253                             }
254                         }
255                     ]
256                 }
257             }
258         );
259         this.controller.render();
260         //this.controller.view.checkout_barcode_entry_textbox.focus();
261
262         this.check_disable();
263
264         var robj = obj.network.simple_request(
265             'FM_CIRC_COUNT_RETRIEVE_VIA_USER.authoritative',
266             [ ses(), obj.patron_id ]
267         );
268         obj.items_out_count = (robj.out + robj.overdue + robj.claims_returned + robj.long_overdue );
269
270     },
271
272     'check_disable' : function() {
273         var obj = this;
274         try {
275             if (typeof xulG.check_stop_checkouts == 'function') {
276                 var disable = xulG.check_stop_checkouts();
277                 if (disable) {
278                     document.getElementById('checkout_submit_barcode_button').disabled = true;
279                     document.getElementById('checkout_done').disabled = true;
280                     document.getElementById('checkout_duedate_checkbox').disabled = true;
281                     document.getElementById('checkout_duedate_timepicker').disabled = true;
282                     obj.controller.view.checkout_menu.disabled = true;
283                     obj.controller.view.checkout_barcode_entry_textbox.disabled = true;
284                     obj.controller.view.checkout_duedate_datepicker.disabled = true;
285                 } else {
286                     document.getElementById('checkout_submit_barcode_button').disabled = false;
287                     document.getElementById('checkout_done').disabled = false;
288                     document.getElementById('checkout_duedate_checkbox').disabled = false;
289                     document.getElementById('checkout_duedate_timepicker').disabled = false;
290                     obj.controller.view.checkout_menu.disabled = false;
291                     obj.controller.view.checkout_barcode_entry_textbox.disabled = false;
292                     obj.controller.view.checkout_duedate_datepicker.disabled = false;
293                 }
294             }
295         } catch(E) {
296             obj.error.standard_unexpected_error_alert(document.getElementById('circStrings').getString('staff.circ.checkout.disable.error'),E);
297         }
298     },
299
300     'print' : function(silent,f) {
301         var obj = this;
302         try {
303             obj.patron = obj.network.simple_request('FM_AU_FLESHED_RETRIEVE_VIA_ID',[ses(),obj.patron_id]);
304             var params = { 
305                 'patron' : obj.patron, 
306                 'lib' : obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ],
307                 'staff' : obj.data.list.au[0],
308                 'data' : {
309                     'balance_owed' : util.money.sanitize( obj.most_recent_balance_owed ),
310                 },
311                 'printer_context' : 'receipt',
312                 'template' : 'checkout',
313                 'callback' : function() {
314                     setTimeout(
315                         function(){
316                             if (typeof f == 'function') { 
317                                 setTimeout( 
318                                     function() {
319                                         f();
320                                     }, 1000
321                                 );
322                             } 
323                         }, 1000
324                     );
325                 }
326             };
327             if (silent) { params.no_prompt = true; }
328             obj.list.print(params);
329         } catch(E) {
330             obj.error.standard_unexpected_error_alert('print',E);
331         }
332     },
333     
334     'export_list' : function(silent,f) {
335         var obj = this;
336         try {
337             obj.list.dump_csv_to_clipboard();
338         } catch(E) {
339             obj.error.standard_unexpected_error_alert('export',E);
340         }
341     },
342
343     'check_date' : function(node) {
344         var obj = this;
345         JSAN.use('util.date');
346         try {
347             obj.controller.view.checkout_barcode_entry_textbox.setAttribute('disabled','false');
348             obj.controller.view.checkout_barcode_entry_textbox.disabled = false;
349             obj.controller.view.cmd_checkout_submit.setAttribute('disabled','false');
350             obj.controller.view.cmd_checkout_submit.disabled = false;
351             if (util.date.check_past('YYYY-MM-DD',node.value) ) {
352                 obj.controller.view.checkout_barcode_entry_textbox.setAttribute('disabled','true');
353                 obj.controller.view.cmd_checkout_submit.setAttribute('disabled','true');
354                 return false;
355             }
356             return true;
357         } catch(E) {
358             throw(E);
359         }
360     },
361
362     '_checkout_pending_hash' : {},
363
364     '_checkout' : function(params,permit) {
365         var obj = this;
366         try {
367         
368             /**********************************************************************************************************************/
369             /* This handles the return value of the checkout/renewal */
370             function _checkout_callback(req,x) {
371                 try {
372
373                     if (params.barcode) { 
374                         delete obj._checkout_pending_hash[ params.barcode ];
375                     }
376
377                     var checkout = req.getResultObject();
378
379                     if (checkout.ilsevent === '0') {
380         
381                         if (!checkout.payload) { checkout.payload = {}; }
382         
383                         if (!checkout.payload.circ) {
384                             checkout.payload.circ = new aoc();
385                             /*********************************************************************************************/
386                             /* Non Cat */
387                             if (checkout.payload.noncat_circ) {
388                                 checkout.payload.circ.circ_lib( checkout.payload.noncat_circ.circ_lib() );
389                                 checkout.payload.circ.circ_staff( checkout.payload.noncat_circ.staff() );
390                                 checkout.payload.circ.usr( checkout.payload.noncat_circ.patron() );
391                                 checkout.payload.circ.due_date( checkout.payload.noncat_circ.duedate() );
392                             }
393                         }
394     
395                         if (!checkout.payload.record) {
396                             checkout.payload.record = new mvr();
397                             /*********************************************************************************************/
398                             /* Non Cat */
399                             if (checkout.payload.noncat_circ) {
400                                 checkout.payload.record.title(
401                                     obj.data.hash.cnct[ checkout.payload.noncat_circ.item_type() ].name()
402                                 );
403                             }
404                         }
405             
406                         if (!checkout.payload.copy) {
407                             checkout.payload.copy = new acp();
408                             checkout.payload.copy.barcode( '' );
409                         }
410             
411                         /*********************************************************************************************/
412                         /* Override mvr title/author with dummy title/author for Pre cat */
413                         if (checkout.payload.copy.dummy_title()) {
414                             checkout.payload.record.title( checkout.payload.copy.dummy_title() );
415                         }
416                         if (checkout.payload.copy.dummy_author()) {
417                             checkout.payload.record.author( checkout.payload.copy.dummy_author() );
418                         }
419            
420                         if (checkout.payload.patron_money) {
421                             obj.most_recent_balance_owed = checkout.payload.patron_money.balance_owed();
422                         }
423  
424                         obj.list.append(
425                             {
426                                 'row' : {
427                                     'my' : {
428                                     'circ' : checkout.payload.circ,
429                                     'mvr' : checkout.payload.record,
430                                     'acp' : checkout.payload.copy,
431                                     'acn' : checkout.payload.volume
432                                     }
433                                 },
434                                 'to_top' : true
435                             //I could override map_row_to_column here
436                             }
437                         );
438                         try {
439                             obj.error.work_log(
440                                 document.getElementById('circStrings').getFormattedString(
441                                     (get_bool(checkout.payload.circ.opac_renewal())||get_bool(checkout.payload.circ.phone_renewal())||get_bool(checkout.payload.circ.desk_renewal())) ?
442                                         'staff.circ.work_log_renew.message' : 'staff.circ.work_log_checkout.message',
443                                     [
444                                         ses('staff_usrname'),
445                                         xulG.patron.family_name(),
446                                         xulG.patron.card().barcode(),
447                                         checkout.payload.copy.barcode()
448                                     ]
449                                 ), {
450                                     'au_id' : xulG.patron.id(),
451                                     'au_family_name' : xulG.patron.family_name(),
452                                     'au_barcode' : xulG.patron.card().barcode(),
453                                     'acp_barcode' : checkout.payload.copy.barcode()
454                                 }
455                             );
456                         } catch(E) {
457                             obj.error.sdump('D_ERROR','Error with work_logging in server/circ/checkout.js, _checkout:' + E);
458                         }
459                         document.getElementById('msg_area').removeChild(x);
460                         /*
461                         if (typeof obj.on_checkout == 'function') {
462                             obj.on_checkout(checkout.payload);
463                         }
464                         */
465                         if (typeof window.xulG == 'object' && typeof window.xulG.on_list_change == 'function') {
466                             window.xulG.on_list_change(checkout.payload);
467                         } else {
468                             obj.error.sdump('D_CIRC','circ.checkout: No external .on_checkout()\n');
469                         }
470         
471                     } else {
472                         // Should get here with failed renewals
473                         switch(Number(checkout.ilsevent)) {
474                             case null /* custom event */ : 
475                             case 5000 /* PERM_FAILURE */: 
476                             case 1212 /* PATRON_EXCEEDS_OVERDUE_COUNT */ : 
477                             case 1213 /* PATRON_BARRED */ : 
478                             case 1215 /* CIRC_EXCEEDS_COPY_RANGE */ : 
479                             case 1224 /* PATRON_ACCOUNT_EXPIRED */ : 
480                             case 1232 /* ITEM_DEPOSIT_REQUIRED */ : 
481                             case 1233 /* ITEM_RENTAL_FEE_REQUIRED */ : 
482                             case 1234 /* ITEM_DEPOSIT_PAID */ : 
483                             case 1500 /* ACTION_CIRCULATION_NOT_FOUND */ : 
484                             case 7002 /* PATRON_EXCEEDS_CHECKOUT_COUNT */ : 
485                             case 7003 /* COPY_CIRC_NOT_ALLOWED */ : 
486                             case 7004 /* COPY_NOT_AVAILABLE */ : 
487                             case 7006 /* COPY_IS_REFERENCE */ : 
488                             case 7007 /* COPY_NEEDED_FOR_HOLD */ : 
489                             case 7008 /* MAX_RENEWALS_REACHED */ : 
490                             case 7009 /* CIRC_CLAIMS_RETURNED */ : 
491                             case 7010 /* COPY_ALERT_MESSAGE */ : 
492                             case 7013 /* PATRON_EXCEEDS_FINES */ : 
493                                 x.setAttribute('style','color: red');
494                                 x.setAttribute('value', document.getElementById('circStrings').getFormattedString('staff.circ.checkout.barcode.failed', [params.barcode]));
495                                 if (typeof params.noncat == 'undefined') { obj.items_out_count--; }
496                             break;
497                             default: throw(checkout);
498                         }
499                     }
500         
501                 } catch(E) {
502                     x.setAttribute('style','color: red');
503                     x.setAttribute('value', document.getElementById('circStrings').getFormattedString('staff.circ.checkout.barcode.failed', [params.barcode]));
504                     if (typeof params.noncat == 'undefined') { obj.items_out_count--; }
505                     obj.error.standard_unexpected_error_alert(document.getElementById('circStrings').getFormattedString('staff.circ.checkout.barcode.failed.alert', ['#3']),E);
506                 }
507             }
508
509             /**********************************************************************************************************************/
510             /* This used to do the actual checkout/renewal */
511         
512             var x = document.createElement('label');
513             x.setAttribute('style','color: green');
514             if (params.barcode) {
515                 x.setAttribute('value',document.getElementById('circStrings').getFormattedString('staff.circ.checkout.barcode.pending', [params.barcode]));
516             } else {
517                 x.setAttribute('value',document.getElementById('circStrings').getString('staff.circ.checkout.non_cataloged.pending'));
518             }
519             document.getElementById('msg_area').appendChild(x);
520
521             /*
522             obj.network.request(
523                 api.CHECKOUT.app,
524                 api.CHECKOUT.method,
525                 [ ses(), params, obj.items_out_count ],
526                 function(req) {
527                     _checkout_callback(req,x);
528                 }
529             );
530             */
531             
532             if (typeof params.noncat == 'undefined') { obj.items_out_count++; }
533            
534            /* new */
535             _checkout_callback({ 'getResultObject' : function() { return permit; } },x);
536
537         } catch(E) {
538             x.setAttribute('style','color: red');
539             x.setAttribute('value', document.getElementById('circStrings').getFormattedString('staff.circ.checkout.barcode.failed', [params.barcode]));
540             if (typeof params.noncat == 'undefined') { obj.items_out_count--; }
541             obj.error.standard_unexpected_error_alert(document.getElementById('circStrings').getFormattedString('staff.circ.checkout.barcode.failed.alert', ['#2']),E);
542         }
543     },
544
545
546     'test_barcode' : function(bc) {
547         var obj = this;
548         var x = document.getElementById('strict_barcode');
549         if (x && x.checked !== true) { return true; }
550         var good = util.barcode.check(bc);
551         if (good) {
552             return true;
553         } else {
554             if ( 1 == obj.error.yns_alert(
555                         document.getElementById('circStrings').getFormattedString('staff.circ.check_digit.bad', [bc]),
556                         document.getElementById('circStrings').getString('staff.circ.barcode.bad'),
557                         document.getElementById('circStrings').getString('staff.circ.cancel'),
558                         document.getElementById('circStrings').getString('staff.circ.barcode.accept'),
559                         null,
560                         document.getElementById('circStrings').getString('staff.circ.confirm'),
561                         '/xul/server/skin/media/images/bad_barcode.png'
562                 )
563             ) {
564                 return true;
565             } else {
566                 return false;
567             }
568         }
569     },
570
571     'checkout' : function(params) {
572         var obj = this;
573
574         JSAN.use('util.date');
575         if (document.getElementById('checkout_duedate_checkbox').checked) {
576             if (! obj.check_date(obj.controller.view.checkout_duedate_datepicker)) return;
577             var tp = document.getElementById('checkout_duedate_timepicker');
578             var dp = obj.controller.view.checkout_duedate_datepicker;
579             var tp_date = tp.dateValue;
580             var dp_date = dp.dateValue;
581             dp_date.setHours( tp_date.getHours() );
582             dp_date.setMinutes( tp_date.getMinutes() );
583
584             params.due_date = util.date.formatted_date(dp_date,'%{iso8601}');
585         }
586
587         if (typeof obj.on_checkout == 'function') { obj.on_checkout(params); }
588
589         if (! (params.barcode||params.noncat)) { return; }
590
591         if (params.barcode) {
592             // Default is "just items"
593             var barcode_context = 'asset';
594             // Add actor (can be any string that includes 'actor') if looking up patrons at checkout
595             if(String( obj.data.hash.aous['circ.staff_client.actor_on_checkout'] ) == 'true')
596                 barcode_context += '-actor';
597             // Auto-complete the barcode
598             var in_barcode = xulG.get_barcode(window, barcode_context, params.barcode);
599             // user_false is "None of the above selected", don't error out/fall through as they already said no
600             if(in_barcode == "user_false") return;
601             // We have a barcode and there was no error?
602             if(in_barcode && typeof in_barcode.ilsevent == 'undefined') {
603                 // Check if it was an actor barcode (will never happen unless actor was added above)
604                 if(in_barcode.type == 'actor') {
605                     // Go to new patron (do not pass go, do not collect $200, do not prompt user)
606                     var horizontal_interface = String( obj.data.hash.aous['ui.circ.patron_summary.horizontal'] ) == 'true';
607                     var loc = xulG.url_prefix( horizontal_interface ? urls.XUL_PATRON_HORIZ_DISPLAY : urls.XUL_PATRON_DISPLAY );
608                     xulG.set_tab( loc, {}, { 'barcode' : in_barcode.barcode } );
609                     return;
610                 }
611                 params.barcode = in_barcode.barcode;
612             }
613
614             if ( obj.test_barcode(params.barcode) ) { /* good */ } else { /* bad */ return; }
615
616             if (typeof obj._checkout_pending_hash[ params.barcode ] != 'undefined') {
617
618                 obj.error.sdump('D_CIRC','Redundant barcode scan == ' + params.barcode);
619                 return; // redundant barcode scan
620
621             } else {
622
623                 obj._checkout_pending_hash[ params.barcode ] = true;    
624
625             }
626         }
627
628
629         /**********************************************************************************************************************/
630         /* This used to be the Permissibility test before checkout */
631         try {
632
633             params.patron = obj.patron_id;
634
635             var permit = obj.network.simple_request(
636                 //api.CHECKOUT_PERMIT.app,
637                 'CHECKOUT_FULL',
638                 [ ses(), params, obj.items_out_count ],
639                 null,
640                 {
641                     'title' : document.getElementById('circStrings').getString('staff.circ.checkout.override.confirm'),
642                     'overridable_events' : [ 
643                         null /* custom event */,
644                         1212 /* PATRON_EXCEEDS_OVERDUE_COUNT */,
645                         1213 /* PATRON_BARRED */,
646                         1215 /* CIRC_EXCEEDS_COPY_RANGE */,
647                         1232 /* ITEM_DEPOSIT_REQUIRED */,
648                         1233 /* ITEM_RENTAL_FEE_REQUIRED */,
649                         7002 /* PATRON_EXCEEDS_CHECKOUT_COUNT */,
650                         7003 /* COPY_CIRC_NOT_ALLOWED */,
651                         7004 /* COPY_NOT_AVAILABLE */, 
652                         7006 /* COPY_IS_REFERENCE */, 
653                         7010 /* COPY_ALERT_MESSAGE */,
654                         7016 /* ITEM_ON_HOLDS_SHELF */,
655                         7013 /* PATRON_EXCEEDS_FINES */
656                     ],
657                     'text' : {
658                         '1232' : function(r) {
659                             return document.getElementById('circStrings').getString('staff.circ.checkout.override.item_deposit_required.warning');
660                         },
661                         '1233' : function(r) {
662                             return document.getElementById('circStrings').getString('staff.circ.checkout.override.item_rental_fee_required.warning');
663                         },
664                         '7004' : function(r) {
665                             try {
666                                 status_name = obj.data.hash.ccs[ r.payload.status() ].name();
667                                 return status_name;
668                             } catch (E) {
669                                 return "Could not retrieve the name of the current status for the copy";  // XXX
670                             }
671                         },
672                         '7010' : function(r) {
673                             return r.payload;
674                         }
675                     }
676                 }
677             );
678
679             if (!permit) { throw(permit); }
680
681             function test_event(list,ev) {
682                 if (typeof list.ilsevent != 'undefined' ) {
683                     if (list.ilsevent == ev) {
684                         return list;
685                     } else {
686                         return false;
687                     }
688                 } else {
689                     for (var i = 0; i < list.length; i++) {
690                         if (typeof list[i].ilsevent != 'undefined') {
691                             if (list[i].ilsevent == ev) { return list[i]; }
692                         }
693                     }
694                     return false;
695                 }
696             }
697
698             /**********************************************************************************************************************/
699             /* Normal case, proceed with checkout */
700             if (permit.ilsevent === '0') {
701
702                 JSAN.use('util.sound'); var sound = new util.sound(); sound.circ_good();
703                 params.permit_key = permit.payload;
704                 obj._checkout( params, permit ); 
705
706             /**********************************************************************************************************************/
707             /* Item not cataloged or barcode mis-scan.  Prompt for pre-cat option */
708             } else {
709             
710                 if (params.barcode) { delete obj._checkout_pending_hash[ params.barcode ];    }
711
712                 var found_handled = false; var found_not_handled = false; var msg = '';    
713
714                 if (test_event(permit,1202 /* ITEM_NOT_CATALOGED */)) {
715
716                     if ( 1 == obj.error.yns_alert(
717                         document.getElementById('circStrings').getString('staff.circ.checkout.not_cataloged.confirm'),
718                         document.getElementById('circStrings').getString('staff.circ.alert'),
719                         document.getElementById('circStrings').getString('staff.circ.cancel'),
720                         document.getElementById('circStrings').getString('staff.circ.pre_cataloged'),
721                         null,
722                         document.getElementById('circStrings').getString('staff.circ.confirm'),
723                         '/xul/server/skin/media/images/book_question.png'
724                     ) ) {
725
726                         obj.data.dummy_title = ''; obj.data.dummy_author = ''; obj.data.stash('dummy_title','dummy_author');
727                         JSAN.use('util.window'); var win = new util.window();
728                         win.open(urls.XUL_PRE_CAT, 'dummy_fields', 'chrome,resizable,modal');
729                         obj.data.stash_retrieve();
730
731                         params.permit_key = permit.payload;
732                         params.dummy_title = obj.data.precat_dummy_title;
733                         params.dummy_author = obj.data.precat_dummy_author;
734                         params.dummy_isbn = obj.data.precat_dummy_isbn;
735                         params.circ_modifier = obj.data.precat_circ_modifier;
736                         params.precat = 1;
737
738                         if (obj.data.precat_submit == 'go') { 
739                             //obj._checkout( params ); No real request method here anymore
740                             obj.checkout( params );
741                         } else {
742                             alert(document.getElementById('circStrings').getString('staff.circ.checkout.cancelled'));
743                         }
744                     }
745
746                     return;
747                 }
748
749                 var test_permit;
750                 if (typeof permit.ilsevent != 'undefined') { test_permit = [ permit ]; } else { test_permit = permit; }
751
752                 var stop_checkout = false;
753                 for (var i = 0; i < test_permit.length; i++) {
754                     switch(Number(test_permit[i].ilsevent)) {
755                         case 1216 /* PATRON_CARD_INACTIVE */ :
756                         case 1217 /* PATRON_INACTIVE */ :
757                         case 1224 /* PATRON_ACCOUNT_EXPIRED */ :
758                             stop_checkout = true;
759                         break;
760                     }
761                 }
762
763                 for (var i = 0; i < test_permit.length; i++) {
764                     dump('found [' + test_permit[i].ilsevent + ']\n');
765                     switch(test_permit[i].ilsevent == null ? null : Number(test_permit[i].ilsevent)) {
766                         case null /* custom event */ :
767                             found_handled = true;
768                         break;
769                         case 1212 /* PATRON_EXCEEDS_OVERDUE_COUNT */ :
770                             found_handled = true;
771                         break;
772                         case 1213 /* PATRON_BARRED */ :
773                             found_handled = true;
774                         break;
775                         case 1215 /* CIRC_EXCEEDS_COPY_RANGE */ :
776                             found_handled = true;
777                         break;
778                         case 1216 /* PATRON_CARD_INACTIVE */ :
779                             found_handled = true;
780                             msg += document.getElementById('circStrings').getString('staff.circ.checkout.card.inactive') + '\n';
781                             obj.error.yns_alert(
782                                 msg,
783                                 document.getElementById('circStrings').getString('staff.circ.checkout.barcode.check_out_failed'),
784                                 document.getElementById('circStrings').getString('staff.circ.checkout.ok.btn'),
785                                 null,
786                                 null,
787                                 document.getElementById('circStrings').getString('staff.circ.confirm.msg')
788                             );
789                         break;
790                         case 1217 /* PATRON_INACTIVE */ :
791                             found_handled = true;
792                             msg += document.getElementById('circStrings').getString('staff.circ.checkout.account.inactive') + '\n';
793                             obj.error.yns_alert(
794                                 msg,
795                                 document.getElementById('circStrings').getString('staff.circ.checkout.barcode.check_out_failed'),
796                                 document.getElementById('circStrings').getString('staff.circ.checkout.ok.btn'),
797                                 null,
798                                 null,
799                                 document.getElementById('circStrings').getString('staff.circ.confirm.msg')
800                             );
801                         break;
802                         case 1224 /* PATRON_ACCOUNT_EXPIRED */ :
803                             found_handled = true;
804                             msg += document.getElementById('circStrings').getString('staff.circ.checkout.account.expired') + '\n';
805                             obj.error.yns_alert(
806                                 msg,
807                                 document.getElementById('circStrings').getString('staff.circ.checkout.barcode.check_out_failed'),
808                                 document.getElementById('circStrings').getString('staff.circ.checkout.ok.btn'),
809                                 null,
810                                 null,
811                                 document.getElementById('circStrings').getString('staff.circ.confirm.msg')
812                             );
813                         break;
814                         case 1232 /* ITEM_DEPOSIT_REQUIRED */ :
815                         case 1233 /* ITEM_RENTAL_FEE_REQUIRED */ :
816                         case 7013 /* PATRON_EXCEEDS_FINES */ :
817                             found_handled = true;
818                         break;
819                         case 7002 /* PATRON_EXCEEDS_CHECKOUT_COUNT */ :
820                             found_handled = true;
821                         break;
822                         case 7003 /* COPY_CIRC_NOT_ALLOWED */ :
823                             found_handled = true;
824                         break;
825                         case 7004 /* COPY_NOT_AVAILABLE */ :
826                             msg += test_permit[i].desc + '\n' + document.getElementById('circStrings').getFormattedString('staff.circ.checkout.copy_status', [obj.data.hash.ccs [test_permit[i].payload.status() ].name()]) + '\n';
827                             found_handled = true;
828                         break;
829                         case 7006 /* COPY_IS_REFERENCE */ :
830                             msg += test_permit[i].desc + '\n';
831                             found_handled = true;
832                         break;
833                         case 7009 /* CIRC_CLAIMS_RETURNED */ :
834                             msg += test_permit[i].desc + '\n';
835                             obj.error.yns_alert(
836                                 msg,
837                                 document.getElementById('circStrings').getString('staff.circ.checkout.barcode.check_out_failed'),
838                                 document.getElementById('circStrings').getString('staff.circ.checkout.ok.btn'),
839                                 null,
840                                 null,
841                                 document.getElementById('circStrings').getString('staff.circ.confirm.msg')
842                             );
843                             found_handled = true;
844                         break;
845                         case 7010 /* COPY_ALERT_MESSAGE */ :
846                             msg += test_permit[i].desc + '\n' + document.getElementById('circStrings').getFormattedString('staff.circ.checkout.alert_message', [test_permit[i].payload]) + '\n';
847                             found_handled = true;
848                         break;
849                         case 7016 /* ITEM_ON_HOLDS_SHELF */ :
850                             msg += test_permit[i].desc + '\n';
851                             found_handled = true;
852                         break;
853                         case 1202 /* ITEM_NOT_CATALOGED */ :
854                             found_handled = true;
855                         break;
856                         case 5000 /* PERM_FAILURE */ :
857                             msg += test_permit[i].desc + '\n' + document.getElementById('circStrings').getFormattedString('staff.circ.checkout.permission_denied', [test_permit[i].ilsperm]) + '\n';
858                             found_handled = true;
859                         break;
860                         case 1702 /* OPEN_CIRCULATION_EXISTS */ :
861                             msg += test_permit[i].desc + '\n';
862                             found_handled = true;
863                             var foreign_circ = true;
864                             var my_circ; 
865
866                             var payload = test_permit[i].payload;
867                             if (payload) {
868                                 if (typeof payload.old_circ == 'object') {
869                                     foreign_circ = false;
870                                     my_circ = payload.old_circ;
871                                 }
872                             }
873
874                             if (foreign_circ) {
875                                 var my_copy = obj.network.simple_request('FM_ACP_RETRIEVE_VIA_BARCODE',[params.barcode]);
876                                 if (typeof my_copy.ilsevent != 'undefined') { throw(my_copy); }
877                                 my_circ = obj.network.simple_request('FM_CIRC_RETRIEVE_VIA_COPY',[ses(),my_copy.id(),1]);
878                                 if (typeof my_circ.ilsevent != 'undefined') { throw(my_copy); }
879                                 my_circ = my_circ[0];
880                             }
881
882                             if (! stop_checkout ) {
883
884                                 var due_date = my_circ.due_date() ? util.date.formatted_date( my_circ.due_date(), '%F' ) : null;
885                                 JSAN.use('util.date'); var today = util.date.formatted_date(new Date(),'%F');
886                                 if (due_date) {
887                                     if (today > due_date) {
888                                         msg += (document.getElementById('circStrings').getFormattedString('staff.circ.checkout.item_due', [ util.date.formatted_date( my_circ.due_date(), '%{localized}' ) ]) + '\n');
889                                     }
890                                 }
891
892                                 if (foreign_circ) { // OFFER CANCEL, NORMAL CHECKIN, AND POSSIBLY FORGIVING-BACKDATED CHECKIN
893                                     var r = obj.error.yns_alert(
894                                         msg,
895                                         document.getElementById('circStrings').getString('staff.circ.checkout.barcode.check_out_failed'),
896                                         document.getElementById('circStrings').getString('staff.circ.cancel'),
897                                         document.getElementById('circStrings').getString('staff.circ.checkout.normal_checkin_then_checkout'),
898                                         due_date ? (today > due_date ? document.getElementById('circStrings').getString('staff.circ.checkout.forgiving_checkin_then_checkout') : null) : null,
899                                         document.getElementById('circStrings').getString('staff.circ.confirm.msg')
900                                     );
901                                     JSAN.use('circ.util');
902                                     switch(r) {
903                                         case 1:
904                                             circ.util.checkin_via_barcode( ses(), { 'barcode' : params.barcode, 'noop' : 1 } );
905                                             obj.checkout(params);
906                                         break;
907                                         case 2:
908                                             circ.util.checkin_via_barcode( ses(), { 'barcode' : params.barcode, 'noop' : 1 }, due_date );
909                                             obj.checkout(params);
910                                         break;
911                                     }
912                                 } else { // EITHER AUTO-RENEW OR OFFER CANCEL, NORMAL CHECKIN, AND RENEW
913                                     if (payload.auto_renew) {
914                                         circ.util.renew_via_barcode( { 'barcode': params.barcode, 'patron' : obj.patron_id }, function(r) {
915                                             try {
916                                                 params.renewal = true;
917                                                 obj._checkout( params, r[0] ); 
918                                             } catch(E) {
919                                                 alert('Error in checkout.js, RENEW 1: ' + E);
920                                             }
921                                         } );
922                                     } else {
923                                         var r = obj.error.yns_alert(
924                                             msg,
925                                             document.getElementById('circStrings').getString('staff.circ.checkout.barcode.check_out_failed'),
926                                             document.getElementById('circStrings').getString('staff.circ.cancel'),
927                                             document.getElementById('circStrings').getString('staff.circ.checkout.normal_checkin_then_checkout'),
928                                             document.getElementById('circStrings').getString('staff.circ.checkout.offer_renewal'),
929                                             document.getElementById('circStrings').getString('staff.circ.confirm.msg')
930                                         );
931                                         JSAN.use('circ.util');
932                                         switch(r) {
933                                             case 1:
934                                                 circ.util.checkin_via_barcode( ses(), { 'barcode' : params.barcode, 'noop' : 1 } );
935                                                 obj.checkout(params);
936                                             break;
937                                             case 2:
938                                                 circ.util.renew_via_barcode( { 'barcode' : params.barcode, 'patron' : obj.patron_id }, function(r) {
939                                                     try {
940                                                         params.renewal = true;
941                                                         obj._checkout( params, r[0] ); 
942                                                     } catch(E) {
943                                                         alert('Error in checkout.js, RENEW 2: ' + E);
944                                                     }
945                                                 } );
946                                             break;
947                                         }
948                                     }
949                                 }
950                             } else {
951                                 obj.error.yns_alert(
952                                     msg,
953                                     document.getElementById('circStrings').getString('staff.circ.checkout.barcode.check_out_failed'),
954                                     document.getElementById('circStrings').getString('staff.circ.checkout.ok.btn'),
955                                     null,
956                                     null,
957                                     document.getElementById('circStrings').getString('staff.circ.confirm.msg')
958                                 );
959                             }
960                         break;
961                         case 7014 /* COPY_IN_TRANSIT */ :
962                             msg += test_permit[i].desc + '\n';
963                             found_handled = true;
964                             if (! stop_checkout ) {
965                                 var r = obj.error.yns_alert(
966                                     msg,
967                                     document.getElementById('circStrings').getString('staff.circ.checkout.barcode.check_out_failed'),
968                                     document.getElementById('circStrings').getString('staff.circ.cancel'),
969                                     document.getElementById('circStrings').getString('staff.circ.checkout.abort_transit_then_checkout'),
970                                     null,
971                                     document.getElementById('circStrings').getString('staff.circ.confirm.msg')
972                                 );
973                                 if (r == 1) {
974                                     var robj = obj.network.simple_request('FM_ATC_VOID',[ ses(), { 'barcode' : params.barcode } ]);
975                                     if (typeof robj.ilsevent == 'undefined') {
976                                         obj.checkout(params);
977                                     } else {
978                                         switch(Number(robj.ilsevent)) {
979                                             case 1225 /* TRANSIT_ABORT_NOT_ALLOWED */ :
980                                                 alert(robj.desc);
981                                             break;
982                                             case 5000 /* PERM_FAILURE */ :
983                                             break;
984                                             default:
985                                                 throw(robj);
986                                             break;
987                                         }
988                                     }
989                                 }
990                             } else {
991                                 obj.error.yns_alert(
992                                     msg,
993                                     document.getElementById('circStrings').getString('staff.circ.checkout.barcode.check_out_failed'),
994                                     document.getElementById('circStrings').getString('staff.circ.checkout.ok.btn'),
995                                     null,
996                                     null,
997                                     document.getElementById('circStrings').getString('staff.circ.confirm.msg')
998                                 );
999                             }
1000                         break;
1001                         case -1 /* NETWORK_FAILURE */ :
1002                             msg += document.getElementById('circStrings').getString('staff.circ.checkout.network_failure') + '\n';
1003                             found_handled = true;
1004                             obj.error.yns_alert(
1005                                 msg,
1006                                 document.getElementById('circStrings').getString('staff.circ.checkout.barcode.check_out_failed'),
1007                                 document.getElementById('circStrings').getString('staff.circ.checkout.ok.btn'),
1008                                 null,
1009                                 null,
1010                                 document.getElementById('circStrings').getString('staff.circ.confirm.msg')
1011                             );
1012                         break;
1013                         default:
1014                             msg += 'FIXME: ' + js2JSON(test_permit[i]) + '\n';
1015                             found_not_handled = true;
1016                         break;
1017                     }
1018                 }
1019                 
1020                 if (found_not_handled) {
1021                     obj.error.standard_unexpected_error_alert(msg,permit);
1022                 }
1023
1024                 obj.controller.view.checkout_barcode_entry_textbox.select();
1025                 obj.controller.view.checkout_barcode_entry_textbox.focus();
1026             }
1027
1028         } catch(E) {
1029             if (params.barcode) { delete obj._checkout_pending_hash[ params.barcode ];    }
1030             if (typeof E.ilsevent != 'undefined' && E.ilsevent == -1) {
1031                 obj.error.standard_network_error_alert(document.getElementById('circStrings').getString('staff.circ.checkout.suggest_offline'));
1032             } else {
1033                 obj.error.standard_unexpected_error_alert(document.getElementById('circStrings').getString('staff.circ.checkout.barcode.check_out_failed'),E);
1034             }
1035             if (typeof obj.on_failure == 'function') {
1036                 obj.on_failure(E);
1037             }
1038             if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
1039                 obj.error.sdump('D_CIRC','circ.checkout: Calling external .on_failure()\n');
1040                 window.xulG.on_failure(E);
1041             } else {
1042                 obj.error.sdump('D_CIRC','circ.checkout: No external .on_failure()\n');
1043             }
1044         }
1045
1046     },
1047
1048     'on_checkout' : function() {
1049         this.controller.view.checkout_menu.selectedIndex = 0;
1050         this.controller.view.checkout_barcode_entry_textbox.disabled = false;
1051         this.controller.view.checkout_barcode_entry_textbox.value = '';
1052         this.controller.view.checkout_barcode_entry_textbox.focus();
1053         document.getElementById('duedate_hbox').hidden = false;
1054     },
1055
1056     'on_failure' : function() {
1057         this.controller.view.checkout_barcode_entry_textbox.select();
1058         this.controller.view.checkout_barcode_entry_textbox.focus();
1059     }
1060 };
1061
1062 dump('exiting circ.checkout.js\n');