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