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