]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/circ/checkout.js
printing on an all_fleshed callback..need to do this for other print actions
[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 }
10
11 circ.checkout.prototype = {
12
13         'init' : function( params ) {
14
15                 var obj = this;
16
17                 obj.patron_id = params['patron_id'];
18                 obj.patron = obj.network.simple_request('FM_AU_RETRIEVE_VIA_ID',[ses(),obj.patron_id]);
19
20                 JSAN.use('circ.util');
21                 var columns = circ.util.columns( 
22                         { 
23                                 'barcode' : { 'hidden' : false },
24                                 'title' : { 'hidden' : false },
25                                 'due_date' : { 'hidden' : false },
26                         } 
27                 );
28
29                 JSAN.use('util.list'); obj.list = new util.list('checkout_list');
30                 obj.list.init(
31                         {
32                                 'columns' : columns,
33                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
34                         }
35                 );
36                 
37                 JSAN.use('util.controller'); obj.controller = new util.controller();
38                 obj.controller.init(
39                         {
40                                 'control_map' : {
41                                         'checkout_menu_placeholder' : [
42                                                 ['render'],
43                                                 function(e) {
44                                                         return function() {
45                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
46                                                                 var items = [ [ 'Barcode:' , 'barcode' ] ].concat(
47                                                                         util.functional.map_list(
48                                                                                 obj.data.list.cnct,
49                                                                                 function(o) {
50                                                                                         return [ o.name(), o.id() ];
51                                                                                 }
52                                                                         )
53                                                                 );
54                                                                 g.error.sdump('D_TRACE','items = ' + js2JSON(items));
55                                                                 util.widgets.remove_children( e );
56                                                                 var ml = util.widgets.make_menulist(
57                                                                         items
58                                                                 );
59                                                                 e.appendChild( ml );
60                                                                 ml.setAttribute('id','checkout_menulist');
61                                                                 ml.setAttribute('accesskey','');
62                                                                 ml.addEventListener(
63                                                                         'command',
64                                                                         function(ev) {
65                                                                                 var tb = obj.controller.view.checkout_barcode_entry_textbox;
66                                                                                 var db = document.getElementById('duedate_hbox');
67                                                                                 if (ev.target.value == 'barcode') {
68                                                                                         db.hidden = false;
69                                                                                         tb.disabled = false;
70                                                                                         tb.value = '';
71                                                                                         tb.focus();
72                                                                                 } else {
73                                                                                         db.hidden = true;
74                                                                                         tb.disabled = true;
75                                                                                         tb.value = 'Non-Cataloged';
76                                                                                 }
77                                                                         }, false
78                                                                 );
79                                                                 obj.controller.view.checkout_menu = ml;
80                                                         };
81                                                 },
82                                         ],
83                                         'checkout_barcode_entry_textbox' : [
84                                                 ['keypress'],
85                                                 function(ev) {
86                                                         if (ev.keyCode && ev.keyCode == 13) {
87                                                                 obj.checkout( { barcode: ev.target.value } );
88                                                         }
89                                                 }
90                                         ],
91                                         'checkout_duedate_menu' : [
92                                                 ['change'],
93                                                 function(ev) { 
94                                                         try {
95                                                                 obj.check_date(ev.target);
96                                                                 ev.target.parentNode.setAttribute('style','');
97                                                         } catch(E) {
98                                                                 ev.target.parentNode.setAttribute('style','background-color: red');
99                                                                 alert(E + '\nUse this format: YYYY-MM-DD');
100                                                                 try {
101                                                                         ev.target.inputField.select();
102                                                                         ev.target.inputField.focus();
103                                                                 } catch(E) { /* this should work, let me try on other platforms */ 
104                                                                         obj.error.sdump('D_ERROR','menulist.inputField: ' + E);
105                                                                 }
106                                                         }
107                                                 }
108                                         ],
109                                         'cmd_broken' : [
110                                                 ['command'],
111                                                 function() { alert('Not Yet Implemented'); }
112                                         ],
113                                         'cmd_checkout_submit' : [
114                                                 ['command'],
115                                                 function() {
116                                                         var params = {}; var count = 1;
117
118                                                         if (obj.controller.view.checkout_menu.value == 'barcode' ||
119                                                                 obj.controller.view.checkout_menu.value == '') {
120                                                                 params.barcode = obj.controller.view.checkout_barcode_entry_textbox.value;
121                                                         } else {
122                                                                 params.noncat = 1;
123                                                                 params.noncat_type = obj.controller.view.checkout_menu.value;
124                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
125                                                                 var r = window.prompt('Enter the number of ' + obj.data.hash.cnct[ params.noncat_type].name() + ' circulating:','1','Non-cataloged Items');
126                                                                 if (r) {
127                                                                         count = parseInt(r);
128                                                                         if (count > 0) {
129                                                                                 if (count > 20) {
130                                                                                         r = obj.error.yns_alert('Are you sure you want to circulate ' + count + ' ' + obj.data.hash.cnct[ params.noncat_type].name() + '?','Non-cataloged Circulation','Yes','No',null,'Check here to confirm this message.');
131                                                                                         if (r != 0) return;
132                                                                                 }
133                                                                         } else {
134                                                                                 r = obj.error.yns_alert('Error with non-cataloged checkout.  ' + r + ' is not a valid number.','Non-cataloged Circulation','Ok',null,null,'Check here to confirm this message.');
135                                                                                 return;
136                                                                         }
137                                                                 } else {
138                                                                         return;
139                                                                 }
140                                                         }
141                                                         for (var i = 0; i < count; i++) {
142                                                                 obj.checkout( params );
143                                                         }
144                                                 }
145                                         ],
146                                         'cmd_checkout_print' : [
147                                                 ['command'],
148                                                 function() {
149                                                         try {
150                                                                 obj.print();
151                                                         } catch(E) {
152                                                                 obj.error.standard_unexpected_error_alert('cmd_checkout_print',E);
153                                                         }
154
155                                                 }
156                                         ],
157                                         'cmd_checkout_reprint' : [
158                                                 ['command'],
159                                                 function() {
160                                                         JSAN.use('util.print'); var print = new util.print();
161                                                         print.reprint_last();
162                                                 }
163                                         ],
164                                         'cmd_checkout_done' : [
165                                                 ['command'],
166                                                 function() {
167                                                         try {
168                                                                 if (document.getElementById('checkout_auto').checked) obj.print(true);
169                                                                 obj.list.clear();
170                                                         } catch(E) {
171                                                                 obj.error.standard_unexpected_error_alert('cmd_checkout_done',E);
172                                                         }
173                                                 }
174                                         ],
175                                 }
176                         }
177                 );
178                 this.controller.render();
179                 this.controller.view.checkout_barcode_entry_textbox.focus();
180
181         },
182
183         'print' : function(silent) {
184                 var obj = this;
185                 try {
186                         obj.list.on_all_fleshed = function() {
187                                 try {
188                                         var params = { 
189                                                 'patron' : obj.patron, 
190                                                 'lib' : obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ],
191                                                 'staff' : obj.data.list.au[0],
192                                                 'header' : obj.data.print_list_templates.checkout.header,
193                                                 'line_item' : obj.data.print_list_templates.checkout.line_item,
194                                                 'footer' : obj.data.print_list_templates.checkout.footer,
195                                                 'type' : obj.data.print_list_templates.checkout.type,
196                                                 'list' : obj.list.dump(),
197                                         };
198                                         if (silent) params.no_prompt = true;
199                                         JSAN.use('util.print'); var print = new util.print();
200                                         print.tree_list( params );
201                                         setTimeout(function(){obj.list.on_all_fleshed = null;},0);
202                                 } catch(E) {
203                                         obj.error.standard_unexpected_error_alert('print',E);
204                                 }
205                         }
206                         obj.list.full_retrieve();
207                 } catch(E) {
208                         obj.error.standard_unexpected_error_alert('print',E);
209                 }
210         },
211
212         'check_date' : function(node) {
213                 JSAN.use('util.date');
214                 try {
215                         if (node.value == 'Normal') return true;
216                         var pattern = node.value.match(/Today \+ (\d+) days/);
217                         if (pattern) {
218                                 var today = new Date();
219                                 var todayPlus = new Date(); todayPlus.setTime( today.getTime() + 24*60*60*1000*pattern[1] );
220                                 node.value = util.date.formatted_date(todayPlus,"%F");
221                         }
222                         if (! util.date.check('YYYY-MM-DD',node.value) ) { throw('Invalid Date'); }
223                         if (util.date.check_past('YYYY-MM-DD',node.value) ) { throw('Due date needs to be after today.'); }
224                         if ( util.date.formatted_date(new Date(),'%F') == node.value) { throw('Due date needs to be after today.'); }
225                         return true;
226                 } catch(E) {
227                         throw(E);
228                 }
229         },
230
231         'checkout' : function(params) {
232                 var obj = this;
233
234                 try { obj.check_date(obj.controller.view.checkout_duedate_menu); } catch(E) { return; }
235                 if (obj.controller.view.checkout_duedate_menu.value != 'Normal') {
236                         params.due_date = obj.controller.view.checkout_duedate_menu.value;
237                 }
238
239                 if (! (params.barcode||params.noncat)) return;
240
241                 /**********************************************************************************************************************/
242                 /* This does the actual checkout/renewal, but is called after a permit test further below */
243                 function check_out(params) {
244
245                         var checkout = obj.network.request(
246                                 api.CHECKOUT.app,
247                                 api.CHECKOUT.method,
248                                 [ ses(), params ]
249                         );
250
251                         if (checkout.ilsevent == 0) {
252
253                                 if (!checkout.payload) checkout.payload = {};
254
255                                 if (!checkout.payload.circ) {
256                                         checkout.payload.circ = new aoc();
257                                         /*********************************************************************************************/
258                                         /* Non Cat */
259                                         if (checkout.payload.noncat_circ) {
260                                                 checkout.payload.circ.circ_lib( checkout.payload.noncat_circ.circ_lib() );
261                                                 checkout.payload.circ.circ_staff( checkout.payload.noncat_circ.staff() );
262                                                 checkout.payload.circ.usr( checkout.payload.noncat_circ.patron() );
263                                                 
264                                                 JSAN.use('util.date');
265                                                 var c = checkout.payload.noncat_circ.circ_time();
266                                                 var d = c == "now" ? new Date() : util.date.db_date2Date( c );
267                                                 var t =obj.data.hash.cnct[ checkout.payload.noncat_circ.item_type() ];
268                                                 var cd = t.circ_duration() || "14 days";
269                                                 var i = util.date.interval_to_seconds( cd ) * 1000;
270                                                 d.setTime( Date.parse(d) + i );
271                                                 checkout.payload.circ.due_date( util.date.formatted_date(d,'%F') );
272
273                                         }
274                                 }
275
276                                 if (!checkout.payload.record) {
277                                         checkout.payload.record = new mvr();
278                                         /*********************************************************************************************/
279                                         /* Non Cat */
280                                         if (checkout.payload.noncat_circ) {
281                                                 checkout.payload.record.title(
282                                                         obj.data.hash.cnct[ checkout.payload.noncat_circ.item_type() ].name()
283                                                 );
284                                         }
285                                 }
286
287                                 if (!checkout.payload.copy) {
288                                         checkout.payload.copy = new acp();
289                                         checkout.payload.copy.barcode( '' );
290                                 }
291
292                                 /*********************************************************************************************/
293                                 /* Override mvr title/author with dummy title/author for Pre cat */
294                                 if (checkout.payload.copy.dummy_title())  checkout.payload.record.title( checkout.payload.copy.dummy_title() );
295                                 if (checkout.payload.copy.dummy_author())  checkout.payload.record.author( checkout.payload.copy.dummy_author() );
296
297                                 obj.list.append(
298                                         {
299                                                 'row' : {
300                                                         'my' : {
301                                                         'circ' : checkout.payload.circ,
302                                                         'mvr' : checkout.payload.record,
303                                                         'acp' : checkout.payload.copy
304                                                         }
305                                                 }
306                                         //I could override map_row_to_column here
307                                         }
308                                 );
309                                 if (typeof obj.on_checkout == 'function') {
310                                         obj.on_checkout(checkout.payload);
311                                 }
312                                 if (typeof window.xulG == 'object' && typeof window.xulG.on_list_change == 'function') {
313                                         window.xulG.on_list_change(checkout.payload);
314                                 } else {
315                                         obj.error.sdump('D_CIRC','circ.checkout: No external .on_checkout()\n');
316                                 }
317                         } else {
318                                 throw(checkout);
319                         }
320                 }
321
322                 /**********************************************************************************************************************/
323                 /* Permissibility test before checkout */
324                 try {
325
326                         params.patron = obj.patron_id;
327
328                         var permit = obj.network.request(
329                                 api.CHECKOUT_PERMIT.app,
330                                 api.CHECKOUT_PERMIT.method,
331                                 [ ses(), params ],
332                                 null,
333                                 {
334                                         'title' : 'Override Checkout Failure?',
335                                         'overridable_events' : [ 
336                                                 1212 /* PATRON_EXCEEDS_OVERDUE_COUNT */,
337                                                 7002 /* PATRON_EXCEEDS_CHECKOUT_COUNT */,
338                                                 7003 /* COPY_CIRC_NOT_ALLOWED */,
339                                                 7004 /* COPY_NOT_AVAILABLE */, 
340                                                 7006 /* COPY_IS_REFERENCE */, 
341                                                 7010 /* COPY_ALERT_MESSAGE */,
342                                                 7013 /* PATRON_EXCEEDS_FINES */,
343                                         ],
344                                         'text' : {
345                                                 '7004' : function(r) {
346                                                         return obj.data.hash.ccs[ r.payload ].name();
347                                                 },
348                                                 '7010' : function(r) {
349                                                         return r.payload;
350                                                 },
351                                         }
352                                 }
353                         );
354
355                         if (!permit) throw(permit);
356
357                         function test_event(list,ev) {
358                                 if (typeof list.ilsevent != 'undefined' ) {
359                                         if (list.ilsevent == ev) {
360                                                 return list;
361                                         } else {
362                                                 return false;
363                                         }
364                                 } else {
365                                         for (var i = 0; i < list.length; i++) {
366                                                 if (typeof list[i].ilsevent != 'undefined') {
367                                                         if (list[i].ilsevent == ev) return list[i];
368                                                 }
369                                         }
370                                         return false;
371                                 }
372                         }
373
374                         /**********************************************************************************************************************/
375                         /* Normal case, proceed with checkout */
376                         if (permit.ilsevent == 0) {
377
378                                 JSAN.use('util.sound'); var sound = new util.sound(); sound.circ_good();
379                                 params.permit_key = permit.payload;
380                                 check_out( params );
381
382                         /**********************************************************************************************************************/
383                         /* Item not cataloged or barcode mis-scan.  Prompt for pre-cat option */
384                         } else {
385                         
386                                 var found_handled = false; var found_not_handled = false; var msg = ''; 
387
388                                 if (test_event(permit,1202 /* ITEM_NOT_CATALOGED */)) {
389
390                                         if ( 1 == obj.error.yns_alert(
391                                                 'Mis-scan or non-cataloged item.  Checkout as a pre-cataloged item?',
392                                                 'Alert',
393                                                 'Cancel',
394                                                 'Pre-Cat',
395                                                 null,
396                                                 'Check here to confirm this action'
397                                         ) ) {
398
399                                                 obj.data.dummy_title = ''; obj.data.dummy_author = ''; obj.data.stash('dummy_title','dummy_author');
400                                                 JSAN.use('util.window'); var win = new util.window();
401                                                 win.open(urls.XUL_PRE_CAT, 'dummy_fields', 'chrome,resizable,modal');
402                                                 obj.data.stash_retrieve();
403
404                                                 params.permit_key = permit.payload;
405                                                 params.dummy_title = obj.data.dummy_title;
406                                                 params.dummy_author = obj.data.dummy_author;
407                                                 params.precat = 1;
408
409                                                 if (params.dummy_title != '') { check_out( params ); } else { throw('Checkout cancelled'); }
410                                         } 
411                                 };
412
413                                 var test_permit;
414                                 if (typeof permit.ilsevent != 'undefined') { test_permit = [ permit ]; } else { test_permit = permit; }
415
416                                 for (var i = 0; i < test_permit.length; i++) {
417                                         dump('found [' + test_permit[i].ilsevent + ']\n');
418                                         switch(test_permit[i].ilsevent) {
419                                                 case 1212 /* PATRON_EXCEEDS_OVERDUE_COUNT */ :
420                                                         found_handled = true;
421                                                 break;
422                                                 case 7013 /* PATRON_EXCEEDS_FINES */ :
423                                                         found_handled = true;
424                                                 break;
425                                                 case 7002 /* PATRON_EXCEEDS_CHECKOUT_COUNT */ :
426                                                         found_handled = true;
427                                                 break;
428                                                 case 7003 /* COPY_CIRC_NOT_ALLOWED */ :
429                                                         found_handled = true;
430                                                 break;
431                                                 case 7004 /* COPY_NOT_AVAILABLE */ :
432                                                         msg += test_permit[i].desc + '\n' + 'Copy status = ' + obj.data.hash.ccs[ test_permit[i].payload ].name() + '\n';
433                                                         found_handled = true;
434                                                 break;
435                                                 case 7006 /* COPY_IS_REFERENCE */ :
436                                                         msg += test_permit[i].desc + '\n';
437                                                         found_handled = true;
438                                                 break;
439                                                 case 7010 /* COPY_ALERT_MESSAGE */ :
440                                                         msg += test_permit[i].desc + '\n' + 'Alert Message = ' + test_permit[i].payload + '\n';
441                                                         found_handled = true;
442                                                 break;
443                                                 case 1202 /* ITEM_NOT_CATALOGED */ :
444                                                         found_handled = true;
445                                                 break;
446                                                 case 5000 /* PERM_FAILURE */ :
447                                                         msg += test_permit[i].desc + '\n' + 'Permission Denied = ' + test_permit[i].ilsperm + '\n';
448                                                         found_handled = true;
449                                                 break;
450                                                 case 1702 /* OPEN_CIRCULATION_EXISTS */ :
451                                                         msg += test_permit[i].desc + '\n';
452                                                         found_handled = true;
453
454                                                         var my_copy = obj.network.simple_request('FM_ACP_RETRIEVE_VIA_BARCODE',[params.barcode]);
455                                                         if (typeof my_copy.ilsevent != 'undefined') throw(my_copy);
456                                                         var my_circ = obj.network.simple_request('FM_CIRC_RETRIEVE_VIA_COPY',[ses(),my_copy.id(),1]);
457                                                         if (typeof my_circ.ilsevent != 'undefined') throw(my_copy);
458                                                         my_circ = my_circ[0];
459                                                         var due_date = my_circ.due_date().substr(0,10);
460                                                         JSAN.use('util.date'); var today = util.date.formatted_date(new Date(),'%F');
461                                                         if (today > due_date) msg += '\nThis item was due on ' + due_date + '.\n';
462                                                         var r = obj.error.yns_alert(msg,'Check Out Failed','Cancel','Checkin then Checkout', today > due_date ? 'Forgiving Checkin then Checkout' : null,'Check here to confirm this message');
463                                                         JSAN.use('circ.util');
464                                                         switch(r) {
465                                                                 case 1:
466                                                                         circ.util.checkin_via_barcode( ses(), params.barcode );
467                                                                         obj.checkout(params);
468                                                                 break;
469                                                                 case 2:
470                                                                         circ.util.checkin_via_barcode( ses(), params.barcode, due_date );
471                                                                         obj.checkout(params);
472                                                                 break;
473                                                         }
474                                                 break;
475                                                 case 7014 /* COPY_IN_TRANSIT */ :
476                                                         msg += test_permit[i].desc + '\n';
477                                                         found_handled = true;
478                                                         var r = obj.error.yns_alert(msg,'Check Out Failed','Cancel','Abort Transit then Checkout',null,'Check here to confirm this message');
479                                                         switch(r) {
480                                                                 case 1:
481                                                                         var robj = obj.network.simple_request('FM_ATC_VOID',[ ses(), { 'barcode' : params.barcode } ]);
482                                                                         if (typeof robj.ilsevent == 'undefined') {
483                                                                                 obj.checkout(params);
484                                                                         } else {
485                                                                                 if (robj.ilsevent != 5000 /* PERM_FAILURE */) throw(robj);
486                                                                         }
487                                                                 break;
488                                                         }
489                                                 break;
490                                                 case -1 /* NETWORK_FAILURE */ :
491                                                         msg += 'There was a network failure.\n';
492                                                         found_handled = true;
493                                                         obj.error.yns_alert(msg,'Check Out Failed','OK',null,null,'Check here to confirm this message');
494                                                 break;
495                                                 default:
496                                                         msg += 'FIXME: ' + js2JSON(test_permit[i]) + '\n';
497                                                         found_not_handled = true;
498                                                 break;
499                                         }
500                                 }
501                                 
502                                 if (found_not_handled) {
503                                         obj.error.standard_unexpected_error_alert(msg,permit);
504                                 }
505
506                                 obj.controller.view.checkout_barcode_entry_textbox.select();
507                                 obj.controller.view.checkout_barcode_entry_textbox.focus();
508                         }
509
510                 } catch(E) {
511                         if (E.ilsevent && E.ilsevent == -1) {
512                                 obj.error.standard_network_error_alert('Check Out Failed.  If you wish to use the offline interface, in the top menubar select Circulation -> Offline Interface');
513                         } else {
514                                 obj.error.standard_unexpected_error_alert('Check Out Failed',E);
515                         }
516                         if (typeof obj.on_failure == 'function') {
517                                 obj.on_failure(E);
518                         }
519                         if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
520                                 obj.error.sdump('D_CIRC','circ.checkout: Calling external .on_failure()\n');
521                                 window.xulG.on_failure(E);
522                         } else {
523                                 obj.error.sdump('D_CIRC','circ.checkout: No external .on_failure()\n');
524                         }
525                 }
526
527         },
528
529         'on_checkout' : function() {
530                 this.controller.view.checkout_menu.selectedIndex = 0;
531                 this.controller.view.checkout_barcode_entry_textbox.disabled = false;
532                 this.controller.view.checkout_barcode_entry_textbox.value = '';
533                 this.controller.view.checkout_barcode_entry_textbox.focus();
534                 document.getElementById('duedate_hbox').hidden = false;
535         },
536
537         'on_failure' : function() {
538                 this.controller.view.checkout_barcode_entry_textbox.select();
539                 this.controller.view.checkout_barcode_entry_textbox.focus();
540         }
541 }
542
543 dump('exiting circ.checkout.js\n');