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