]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/circ/checkout.js
if auto-print is unchecked for checkout, prompt for printing on Done
[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) {
169                                                                         obj.print(true,function() { 
170                                                                                 obj.list.clear();
171                                                                                 xulG.set_tab(urls.XUL_PATRON_BARCODE_ENTRY,{},{}); 
172                                                                         });
173                                                                 } else {
174                                                                         obj.print(false,function() {
175                                                                                 obj.list.clear();
176                                                                                 xulG.set_tab(urls.XUL_PATRON_BARCODE_ENTRY,{},{});
177                                                                         });
178                                                                 }
179                                                         } catch(E) {
180                                                                 obj.error.standard_unexpected_error_alert('cmd_checkout_done',E);
181                                                         }
182                                                 }
183                                         ],
184                                 }
185                         }
186                 );
187                 this.controller.render();
188                 this.controller.view.checkout_barcode_entry_textbox.focus();
189
190         },
191
192         'print' : function(silent,f) {
193                 var obj = this;
194                 try {
195                         obj.list.on_all_fleshed = function() {
196                                 try {
197                                         var params = { 
198                                                 'patron' : obj.patron, 
199                                                 'lib' : obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ],
200                                                 'staff' : obj.data.list.au[0],
201                                                 'header' : obj.data.print_list_templates.checkout.header,
202                                                 'line_item' : obj.data.print_list_templates.checkout.line_item,
203                                                 'footer' : obj.data.print_list_templates.checkout.footer,
204                                                 'type' : obj.data.print_list_templates.checkout.type,
205                                                 'list' : obj.list.dump(),
206                                         };
207                                         if (silent) params.no_prompt = true;
208                                         JSAN.use('util.print'); var print = new util.print();
209                                         print.tree_list( params );
210                                         setTimeout(function(){obj.list.on_all_fleshed = null;if (typeof f == 'function') f();},0);
211                                 } catch(E) {
212                                         obj.error.standard_unexpected_error_alert('print',E);
213                                 }
214                         }
215                         obj.list.full_retrieve();
216                 } catch(E) {
217                         obj.error.standard_unexpected_error_alert('print',E);
218                 }
219         },
220
221         'check_date' : function(node) {
222                 JSAN.use('util.date');
223                 try {
224                         if (node.value == 'Normal') return true;
225                         var pattern = node.value.match(/Today \+ (\d+) days/);
226                         if (pattern) {
227                                 var today = new Date();
228                                 var todayPlus = new Date(); todayPlus.setTime( today.getTime() + 24*60*60*1000*pattern[1] );
229                                 node.value = util.date.formatted_date(todayPlus,"%F");
230                         }
231                         if (! util.date.check('YYYY-MM-DD',node.value) ) { throw('Invalid Date'); }
232                         if (util.date.check_past('YYYY-MM-DD',node.value) ) { throw('Due date needs to be after today.'); }
233                         if ( util.date.formatted_date(new Date(),'%F') == node.value) { throw('Due date needs to be after today.'); }
234                         return true;
235                 } catch(E) {
236                         throw(E);
237                 }
238         },
239
240         'checkout' : function(params) {
241                 var obj = this;
242
243                 try { obj.check_date(obj.controller.view.checkout_duedate_menu); } catch(E) { return; }
244                 if (obj.controller.view.checkout_duedate_menu.value != 'Normal') {
245                         params.due_date = obj.controller.view.checkout_duedate_menu.value;
246                 }
247
248                 if (! (params.barcode||params.noncat)) return;
249
250                 /**********************************************************************************************************************/
251                 /* This does the actual checkout/renewal, but is called after a permit test further below */
252                 function check_out(params) {
253
254                         var checkout = obj.network.request(
255                                 api.CHECKOUT.app,
256                                 api.CHECKOUT.method,
257                                 [ ses(), params ]
258                         );
259
260                         if (checkout.ilsevent == 0) {
261
262                                 if (!checkout.payload) checkout.payload = {};
263
264                                 if (!checkout.payload.circ) {
265                                         checkout.payload.circ = new aoc();
266                                         /*********************************************************************************************/
267                                         /* Non Cat */
268                                         if (checkout.payload.noncat_circ) {
269                                                 checkout.payload.circ.circ_lib( checkout.payload.noncat_circ.circ_lib() );
270                                                 checkout.payload.circ.circ_staff( checkout.payload.noncat_circ.staff() );
271                                                 checkout.payload.circ.usr( checkout.payload.noncat_circ.patron() );
272                                                 
273                                                 JSAN.use('util.date');
274                                                 var c = checkout.payload.noncat_circ.circ_time();
275                                                 var d = c == "now" ? new Date() : util.date.db_date2Date( c );
276                                                 var t =obj.data.hash.cnct[ checkout.payload.noncat_circ.item_type() ];
277                                                 var cd = t.circ_duration() || "14 days";
278                                                 var i = util.date.interval_to_seconds( cd ) * 1000;
279                                                 d.setTime( Date.parse(d) + i );
280                                                 checkout.payload.circ.due_date( util.date.formatted_date(d,'%F') );
281
282                                         }
283                                 }
284
285                                 if (!checkout.payload.record) {
286                                         checkout.payload.record = new mvr();
287                                         /*********************************************************************************************/
288                                         /* Non Cat */
289                                         if (checkout.payload.noncat_circ) {
290                                                 checkout.payload.record.title(
291                                                         obj.data.hash.cnct[ checkout.payload.noncat_circ.item_type() ].name()
292                                                 );
293                                         }
294                                 }
295
296                                 if (!checkout.payload.copy) {
297                                         checkout.payload.copy = new acp();
298                                         checkout.payload.copy.barcode( '' );
299                                 }
300
301                                 /*********************************************************************************************/
302                                 /* Override mvr title/author with dummy title/author for Pre cat */
303                                 if (checkout.payload.copy.dummy_title())  checkout.payload.record.title( checkout.payload.copy.dummy_title() );
304                                 if (checkout.payload.copy.dummy_author())  checkout.payload.record.author( checkout.payload.copy.dummy_author() );
305
306                                 obj.list.append(
307                                         {
308                                                 'row' : {
309                                                         'my' : {
310                                                         'circ' : checkout.payload.circ,
311                                                         'mvr' : checkout.payload.record,
312                                                         'acp' : checkout.payload.copy
313                                                         }
314                                                 }
315                                         //I could override map_row_to_column here
316                                         }
317                                 );
318                                 if (typeof obj.on_checkout == 'function') {
319                                         obj.on_checkout(checkout.payload);
320                                 }
321                                 if (typeof window.xulG == 'object' && typeof window.xulG.on_list_change == 'function') {
322                                         window.xulG.on_list_change(checkout.payload);
323                                 } else {
324                                         obj.error.sdump('D_CIRC','circ.checkout: No external .on_checkout()\n');
325                                 }
326                         } else {
327                                 throw(checkout);
328                         }
329                 }
330
331                 /**********************************************************************************************************************/
332                 /* Permissibility test before checkout */
333                 try {
334
335                         params.patron = obj.patron_id;
336
337                         var permit = obj.network.request(
338                                 api.CHECKOUT_PERMIT.app,
339                                 api.CHECKOUT_PERMIT.method,
340                                 [ ses(), params ],
341                                 null,
342                                 {
343                                         'title' : 'Override Checkout Failure?',
344                                         'overridable_events' : [ 
345                                                 1212 /* PATRON_EXCEEDS_OVERDUE_COUNT */,
346                                                 7002 /* PATRON_EXCEEDS_CHECKOUT_COUNT */,
347                                                 7003 /* COPY_CIRC_NOT_ALLOWED */,
348                                                 7004 /* COPY_NOT_AVAILABLE */, 
349                                                 7006 /* COPY_IS_REFERENCE */, 
350                                                 7010 /* COPY_ALERT_MESSAGE */,
351                                                 7013 /* PATRON_EXCEEDS_FINES */,
352                                         ],
353                                         'text' : {
354                                                 '7004' : function(r) {
355                                                         //return obj.data.hash.ccs[ r.payload ].name();
356                                                         return r.payload.status().name();
357                                                         //return r.payload.name();
358                                                         //return r.payload;
359                                                 },
360                                                 '7010' : function(r) {
361                                                         return r.payload;
362                                                 },
363                                         }
364                                 }
365                         );
366
367                         if (!permit) throw(permit);
368
369                         function test_event(list,ev) {
370                                 if (typeof list.ilsevent != 'undefined' ) {
371                                         if (list.ilsevent == ev) {
372                                                 return list;
373                                         } else {
374                                                 return false;
375                                         }
376                                 } else {
377                                         for (var i = 0; i < list.length; i++) {
378                                                 if (typeof list[i].ilsevent != 'undefined') {
379                                                         if (list[i].ilsevent == ev) return list[i];
380                                                 }
381                                         }
382                                         return false;
383                                 }
384                         }
385
386                         /**********************************************************************************************************************/
387                         /* Normal case, proceed with checkout */
388                         if (permit.ilsevent == 0) {
389
390                                 JSAN.use('util.sound'); var sound = new util.sound(); sound.circ_good();
391                                 params.permit_key = permit.payload;
392                                 check_out( params );
393
394                         /**********************************************************************************************************************/
395                         /* Item not cataloged or barcode mis-scan.  Prompt for pre-cat option */
396                         } else {
397                         
398                                 var found_handled = false; var found_not_handled = false; var msg = ''; 
399
400                                 if (test_event(permit,1202 /* ITEM_NOT_CATALOGED */)) {
401
402                                         if ( 1 == obj.error.yns_alert(
403                                                 'Mis-scan or non-cataloged item.  Checkout as a pre-cataloged item?',
404                                                 'Alert',
405                                                 'Cancel',
406                                                 'Pre-Cat',
407                                                 null,
408                                                 'Check here to confirm this action'
409                                         ) ) {
410
411                                                 obj.data.dummy_title = ''; obj.data.dummy_author = ''; obj.data.stash('dummy_title','dummy_author');
412                                                 JSAN.use('util.window'); var win = new util.window();
413                                                 win.open(urls.XUL_PRE_CAT, 'dummy_fields', 'chrome,resizable,modal');
414                                                 obj.data.stash_retrieve();
415
416                                                 params.permit_key = permit.payload;
417                                                 params.dummy_title = obj.data.dummy_title;
418                                                 params.dummy_author = obj.data.dummy_author;
419                                                 params.precat = 1;
420
421                                                 if (params.dummy_title != '') { check_out( params ); } else { throw('Checkout cancelled'); }
422                                         } 
423                                 };
424
425                                 var test_permit;
426                                 if (typeof permit.ilsevent != 'undefined') { test_permit = [ permit ]; } else { test_permit = permit; }
427
428                                 for (var i = 0; i < test_permit.length; i++) {
429                                         dump('found [' + test_permit[i].ilsevent + ']\n');
430                                         switch(test_permit[i].ilsevent) {
431                                                 case 1212 /* PATRON_EXCEEDS_OVERDUE_COUNT */ :
432                                                         found_handled = true;
433                                                 break;
434                                                 case 7013 /* PATRON_EXCEEDS_FINES */ :
435                                                         found_handled = true;
436                                                 break;
437                                                 case 7002 /* PATRON_EXCEEDS_CHECKOUT_COUNT */ :
438                                                         found_handled = true;
439                                                 break;
440                                                 case 7003 /* COPY_CIRC_NOT_ALLOWED */ :
441                                                         found_handled = true;
442                                                 break;
443                                                 case 7004 /* COPY_NOT_AVAILABLE */ :
444                                                         msg += test_permit[i].desc + '\n' + 'Copy status = ' + ( test_permit[i].payload.status().name() ) + '\n';
445                                                         found_handled = true;
446                                                 break;
447                                                 case 7006 /* COPY_IS_REFERENCE */ :
448                                                         msg += test_permit[i].desc + '\n';
449                                                         found_handled = true;
450                                                 break;
451                                                 case 7010 /* COPY_ALERT_MESSAGE */ :
452                                                         msg += test_permit[i].desc + '\n' + 'Alert Message = ' + test_permit[i].payload + '\n';
453                                                         found_handled = true;
454                                                 break;
455                                                 case 1202 /* ITEM_NOT_CATALOGED */ :
456                                                         found_handled = true;
457                                                 break;
458                                                 case 5000 /* PERM_FAILURE */ :
459                                                         msg += test_permit[i].desc + '\n' + 'Permission Denied = ' + test_permit[i].ilsperm + '\n';
460                                                         found_handled = true;
461                                                 break;
462                                                 case 1702 /* OPEN_CIRCULATION_EXISTS */ :
463                                                         msg += test_permit[i].desc + '\n';
464                                                         found_handled = true;
465
466                                                         var my_copy = obj.network.simple_request('FM_ACP_RETRIEVE_VIA_BARCODE',[params.barcode]);
467                                                         if (typeof my_copy.ilsevent != 'undefined') throw(my_copy);
468                                                         var my_circ = obj.network.simple_request('FM_CIRC_RETRIEVE_VIA_COPY',[ses(),my_copy.id(),1]);
469                                                         if (typeof my_circ.ilsevent != 'undefined') throw(my_copy);
470                                                         my_circ = my_circ[0];
471                                                         var due_date = my_circ.due_date().substr(0,10);
472                                                         JSAN.use('util.date'); var today = util.date.formatted_date(new Date(),'%F');
473                                                         if (today > due_date) msg += '\nThis item was due on ' + due_date + '.\n';
474                                                         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');
475                                                         JSAN.use('circ.util');
476                                                         switch(r) {
477                                                                 case 1:
478                                                                         circ.util.checkin_via_barcode( ses(), params.barcode );
479                                                                         obj.checkout(params);
480                                                                 break;
481                                                                 case 2:
482                                                                         circ.util.checkin_via_barcode( ses(), params.barcode, due_date );
483                                                                         obj.checkout(params);
484                                                                 break;
485                                                         }
486                                                 break;
487                                                 case 7014 /* COPY_IN_TRANSIT */ :
488                                                         msg += test_permit[i].desc + '\n';
489                                                         found_handled = true;
490                                                         var r = obj.error.yns_alert(msg,'Check Out Failed','Cancel','Abort Transit then Checkout',null,'Check here to confirm this message');
491                                                         switch(r) {
492                                                                 case 1:
493                                                                         var robj = obj.network.simple_request('FM_ATC_VOID',[ ses(), { 'barcode' : params.barcode } ]);
494                                                                         if (typeof robj.ilsevent == 'undefined') {
495                                                                                 obj.checkout(params);
496                                                                         } else {
497                                                                                 if (robj.ilsevent != 5000 /* PERM_FAILURE */) throw(robj);
498                                                                         }
499                                                                 break;
500                                                         }
501                                                 break;
502                                                 case -1 /* NETWORK_FAILURE */ :
503                                                         msg += 'There was a network failure.\n';
504                                                         found_handled = true;
505                                                         obj.error.yns_alert(msg,'Check Out Failed','OK',null,null,'Check here to confirm this message');
506                                                 break;
507                                                 default:
508                                                         msg += 'FIXME: ' + js2JSON(test_permit[i]) + '\n';
509                                                         found_not_handled = true;
510                                                 break;
511                                         }
512                                 }
513                                 
514                                 if (found_not_handled) {
515                                         obj.error.standard_unexpected_error_alert(msg,permit);
516                                 }
517
518                                 obj.controller.view.checkout_barcode_entry_textbox.select();
519                                 obj.controller.view.checkout_barcode_entry_textbox.focus();
520                         }
521
522                 } catch(E) {
523                         if (E.ilsevent && E.ilsevent == -1) {
524                                 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');
525                         } else {
526                                 obj.error.standard_unexpected_error_alert('Check Out Failed',E);
527                         }
528                         if (typeof obj.on_failure == 'function') {
529                                 obj.on_failure(E);
530                         }
531                         if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
532                                 obj.error.sdump('D_CIRC','circ.checkout: Calling external .on_failure()\n');
533                                 window.xulG.on_failure(E);
534                         } else {
535                                 obj.error.sdump('D_CIRC','circ.checkout: No external .on_failure()\n');
536                         }
537                 }
538
539         },
540
541         'on_checkout' : function() {
542                 this.controller.view.checkout_menu.selectedIndex = 0;
543                 this.controller.view.checkout_barcode_entry_textbox.disabled = false;
544                 this.controller.view.checkout_barcode_entry_textbox.value = '';
545                 this.controller.view.checkout_barcode_entry_textbox.focus();
546                 document.getElementById('duedate_hbox').hidden = false;
547         },
548
549         'on_failure' : function() {
550                 this.controller.view.checkout_barcode_entry_textbox.select();
551                 this.controller.view.checkout_barcode_entry_textbox.focus();
552         }
553 }
554
555 dump('exiting circ.checkout.js\n');