]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/circ/checkout.js
event handling for checkout
[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                                                                                 if (ev.target.value == 'barcode') {
67                                                                                         tb.disabled = false;
68                                                                                         tb.value = '';
69                                                                                         tb.focus();
70                                                                                 } else {
71                                                                                         tb.disabled = true;
72                                                                                         tb.value = 'Non-Cataloged';
73                                                                                 }
74                                                                         }, false
75                                                                 );
76                                                                 obj.controller.view.checkout_menu = ml;
77                                                         };
78                                                 },
79                                         ],
80                                         'checkout_barcode_entry_textbox' : [
81                                                 ['keypress'],
82                                                 function(ev) {
83                                                         if (ev.keyCode && ev.keyCode == 13) {
84                                                                 obj.checkout( { barcode: ev.target.value } );
85                                                         }
86                                                 }
87                                         ],
88                                         'checkout_duedate_menu' : [
89                                                 ['change'],
90                                                 function(ev) { 
91                                                         try {
92                                                                 obj.check_date(ev.target);
93                                                                 ev.target.parentNode.setAttribute('style','');
94                                                         } catch(E) {
95                                                                 ev.target.parentNode.setAttribute('style','background-color: red');
96                                                                 alert(E + '\nUse this format: YYYY-MM-DD');
97                                                                 try {
98                                                                         ev.target.inputField.select();
99                                                                         ev.target.inputField.focus();
100                                                                 } catch(E) { /* this should work, let me try on other platforms */ 
101                                                                         obj.error.sdump('D_ERROR','menulist.inputField: ' + E);
102                                                                 }
103                                                         }
104                                                 }
105                                         ],
106                                         'cmd_broken' : [
107                                                 ['command'],
108                                                 function() { alert('Not Yet Implemented'); }
109                                         ],
110                                         'cmd_checkout_submit' : [
111                                                 ['command'],
112                                                 function() {
113                                                         var params = {}; var count = 1;
114
115                                                         if (obj.controller.view.checkout_menu.value == 'barcode' ||
116                                                                 obj.controller.view.checkout_menu.value == '') {
117                                                                 params.barcode = obj.controller.view.checkout_barcode_entry_textbox.value;
118                                                         } else {
119                                                                 params.noncat = 1;
120                                                                 params.noncat_type = obj.controller.view.checkout_menu.value;
121                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
122                                                                 var r = window.prompt('Enter the number of ' + obj.data.hash.cnct[ params.noncat_type].name() + ' circulating:','1','Non-cataloged Items');
123                                                                 if (r) {
124                                                                         count = parseInt(r);
125                                                                         if (count > 0) {
126                                                                                 if (count > 20) {
127                                                                                         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.');
128                                                                                         if (r != 0) return;
129                                                                                 }
130                                                                         } else {
131                                                                                 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.');
132                                                                                 return;
133                                                                         }
134                                                                 } else {
135                                                                         return;
136                                                                 }
137                                                         }
138                                                         for (var i = 0; i < count; i++) {
139                                                                 obj.checkout( params );
140                                                         }
141                                                 }
142                                         ],
143                                         'cmd_checkout_print' : [
144                                                 ['command'],
145                                                 function() {
146                                                         try {
147                                                                 var params = { 
148                                                                         'patron' : obj.patron, 
149                                                                         'lib' : obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ],
150                                                                         'staff' : obj.data.list.au[0],
151                                                                         'header' : obj.data.print_list_templates.checkout.header,
152                                                                         'line_item' : obj.data.print_list_templates.checkout.line_item,
153                                                                         'footer' : obj.data.print_list_templates.checkout.footer,
154                                                                         'type' : obj.data.print_list_templates.checkout.type,
155                                                                         'list' : obj.list.dump(),
156                                                                 };
157                                                                 JSAN.use('util.print'); var print = new util.print();
158                                                                 print.tree_list( params );
159                                                         } catch(E) {
160                                                                 this.error.sdump('D_ERROR','preview: ' + E);
161                                                                 alert('preview: ' + E);
162                                                         }
163
164                                                 }
165                                         ],
166                                         'cmd_checkout_reprint' : [
167                                                 ['command'],
168                                                 function() {
169                                                 }
170                                         ],
171                                         'cmd_checkout_done' : [
172                                                 ['command'],
173                                                 function() {
174                                                 }
175                                         ],
176                                 }
177                         }
178                 );
179                 this.controller.render();
180                 this.controller.view.checkout_barcode_entry_textbox.focus();
181
182         },
183
184         'check_date' : function(node) {
185                 JSAN.use('util.date');
186                 try {
187                         if (node.value == 'Normal') return true;
188                         var pattern = node.value.match(/Today \+ (\d+) days/);
189                         if (pattern) {
190                                 var today = new Date();
191                                 var todayPlus = new Date(); todayPlus.setTime( today.getTime() + 24*60*60*1000*pattern[1] );
192                                 node.value = util.date.formatted_date(todayPlus,"%F");
193                         }
194                         if (! util.date.check('YYYY-MM-DD',node.value) ) { throw('Invalid Date'); }
195                         if (util.date.check_past('YYYY-MM-DD',node.value) ) { throw('Due date needs to be after today.'); }
196                         if ( util.date.formatted_date(new Date(),'%F') == node.value) { throw('Due date needs to be after today.'); }
197                         return true;
198                 } catch(E) {
199                         throw(E);
200                 }
201         },
202
203         'checkout' : function(params) {
204                 var obj = this;
205
206                 try { obj.check_date(obj.controller.view.checkout_duedate_menu); } catch(E) { return; }
207                 if (obj.controller.view.checkout_duedate_menu.value != 'Normal') {
208                         params.due_date = obj.controller.view.checkout_duedate_menu.value;
209                 }
210
211                 if (! (params.barcode||params.noncat)) return;
212
213                 /**********************************************************************************************************************/
214                 /* This does the actual checkout/renewal, but is called after a permit test further below */
215                 function check_out(params) {
216
217                         var checkout = obj.network.request(
218                                 api.CHECKOUT.app,
219                                 api.CHECKOUT.method,
220                                 [ ses(), params ]
221                         );
222
223                         if (checkout.ilsevent == 0) {
224
225                                 if (!checkout.payload) checkout.payload = {};
226
227                                 if (!checkout.payload.circ) {
228                                         checkout.payload.circ = new aoc();
229                                         /*********************************************************************************************/
230                                         /* Non Cat */
231                                         if (checkout.payload.noncat_circ) {
232                                                 checkout.payload.circ.circ_lib( checkout.payload.noncat_circ.circ_lib() );
233                                                 checkout.payload.circ.circ_staff( checkout.payload.noncat_circ.staff() );
234                                                 checkout.payload.circ.usr( checkout.payload.noncat_circ.patron() );
235                                                 
236                                                 JSAN.use('util.date');
237                                                 var c = checkout.payload.noncat_circ.circ_time();
238                                                 var d = c == "now" ? new Date() : util.date.db_date2Date( c );
239                                                 var t =obj.data.hash.cnct[ checkout.payload.noncat_circ.item_type() ];
240                                                 var cd = t.circ_duration() || "14 days";
241                                                 var i = util.date.interval_to_seconds( cd ) * 1000;
242                                                 d.setTime( Date.parse(d) + i );
243                                                 checkout.payload.circ.due_date( util.date.formatted_date(d,'%F') );
244
245                                         }
246                                 }
247
248                                 if (!checkout.payload.record) {
249                                         checkout.payload.record = new mvr();
250                                         /*********************************************************************************************/
251                                         /* Non Cat */
252                                         if (checkout.payload.noncat_circ) {
253                                                 checkout.payload.record.title(
254                                                         obj.data.hash.cnct[ checkout.payload.noncat_circ.item_type() ].name()
255                                                 );
256                                         }
257                                 }
258
259                                 if (!checkout.payload.copy) {
260                                         checkout.payload.copy = new acp();
261                                         checkout.payload.copy.barcode( '' );
262                                 }
263
264                                 /*********************************************************************************************/
265                                 /* Override mvr title/author with dummy title/author for Pre cat */
266                                 if (checkout.payload.copy.dummy_title())  checkout.payload.record.title( checkout.payload.copy.dummy_title() );
267                                 if (checkout.payload.copy.dummy_author())  checkout.payload.record.author( checkout.payload.copy.dummy_author() );
268
269                                 obj.list.append(
270                                         {
271                                                 'row' : {
272                                                         'my' : {
273                                                         'circ' : checkout.payload.circ,
274                                                         'mvr' : checkout.payload.record,
275                                                         'acp' : checkout.payload.copy
276                                                         }
277                                                 }
278                                         //I could override map_row_to_column here
279                                         }
280                                 );
281                                 if (typeof obj.on_checkout == 'function') {
282                                         obj.on_checkout(checkout.payload);
283                                 }
284                                 if (typeof window.xulG == 'object' && typeof window.xulG.on_list_change == 'function') {
285                                         window.xulG.on_list_change(checkout.payload);
286                                 } else {
287                                         obj.error.sdump('D_CIRC','circ.checkout: No external .on_checkout()\n');
288                                 }
289                         } else {
290                                 throw(checkout);
291                         }
292                 }
293
294                 /**********************************************************************************************************************/
295                 /* Permissibility test before checkout */
296                 try {
297
298                         params.patron = obj.patron_id;
299
300                         var permit = obj.network.request(
301                                 api.CHECKOUT_PERMIT.app,
302                                 api.CHECKOUT_PERMIT.method,
303                                 [ ses(), params ],
304                                 null,
305                                 {
306                                         'title' : 'Override Checkout Failure?',
307                                         'overridable_events' : [ 
308                                                 7004 /* COPY_NOT_AVAILABLE */, 
309                                                 7006 /* COPY_IS_REFERENCE */, 
310                                                 7010 /* COPY_ALERT_MESSAGE */,
311                                         ],
312                                         'text' : {
313                                                 '7004' : function(r) {
314                                                         return obj.data.hash.ccs[ r.payload ].name();
315                                                 },
316                                                 '7010' : function(r) {
317                                                         return r.payload;
318                                                 },
319                                         }
320                                 }
321                         );
322
323                         if (!permit) throw(permit);
324
325                         function test_event(list,ev) {
326                                 if (typeof list.ilsevent != 'undefined' ) {
327                                         if (list.ilsevent == ev) {
328                                                 return list;
329                                         } else {
330                                                 return false;
331                                         }
332                                 } else {
333                                         for (var i = 0; i < list.length; i++) {
334                                                 if (typeof list[i].ilsevent != 'undefined') {
335                                                         if (list[i].ilsevent == ev) return list[i];
336                                                 }
337                                         }
338                                         return false;
339                                 }
340                         }
341
342                         /**********************************************************************************************************************/
343                         /* Normal case, proceed with checkout */
344                         if (permit.ilsevent == 0) {
345
346                                 JSAN.use('util.sound'); var sound = new util.sound(); sound.circ_good();
347                                 params.permit_key = permit.payload;
348                                 check_out( params );
349
350                         /**********************************************************************************************************************/
351                         /* Item not cataloged or barcode mis-scan.  Prompt for pre-cat option */
352                         } else {
353                         
354                                 var found_handled = false; var found_not_handled = false; var msg = ''; 
355
356                                 if (test_event(permit,1202 /* ITEM_NOT_CATALOGED */)) {
357
358                                         if ( 1 == obj.error.yns_alert(
359                                                 'Mis-scan or non-cataloged item.  Checkout as a pre-cataloged item?',
360                                                 'Alert',
361                                                 'Cancel',
362                                                 'Pre-Cat',
363                                                 null,
364                                                 null
365                                         ) ) {
366
367                                                 obj.data.dummy_title = ''; obj.data.dummy_author = ''; obj.data.stash('dummy_title','dummy_author');
368                                                 JSAN.use('util.window'); var win = new util.window();
369                                                 win.open(urls.XUL_PRE_CAT, 'dummy_fields', 'chrome,resizable,modal');
370                                                 obj.data.stash_retrieve();
371
372                                                 params.permit_key = permit.payload;
373                                                 params.dummy_title = obj.data.dummy_title;
374                                                 params.dummy_author = obj.data.dummy_author;
375                                                 params.precat = 1;
376
377                                                 if (params.dummy_title != '') { check_out( params ); } else { throw('Checkout cancelled'); }
378                                         } 
379                                 };
380
381                                 var test_permit;
382                                 if (typeof permit.ilsevent != 'undefined') { test_permit = [ permit ]; } else { test_permit = permit; }
383
384                                 for (var i = 0; i < test_permit.length; i++) {
385                                         dump('found [' + test_permit[i].ilsevent + ']\n');
386                                         switch(test_permit[i].ilsevent) {
387                                                 case 7004 /* COPY_NOT_AVAILABLE */ :
388                                                         msg += test_permit[i].desc + '\n' + 'Copy status = ' + obj.data.hash.ccs[ test_permit[i].payload ].name() + '\n';
389                                                         found_handled = true;
390                                                 break;
391                                                 case 7006 /* COPY_IS_REFERENCE */ :
392                                                         msg += test_permit[i].desc + '\n';
393                                                         found_handled = true;
394                                                 break;
395                                                 case 7010 /* COPY_ALERT_MESSAGE */ :
396                                                         msg += test_permit[i].desc + '\n' + 'Alert Message = ' + test_permit[i].payload + '\n';
397                                                         found_handled = true;
398                                                 break;
399                                                 case 1202 /* ITEM_NOT_CATALOGED */ :
400                                                         found_handled = true;
401                                                 break;
402                                                 case 5000 /* PERM_FAILURE */ :
403                                                         msg += test_permit[i].desc + '\n' + 'Permission Denied = ' + test_permit[i].ilsperm + '\n';
404                                                         found_handled = true;
405                                                 break;
406                                                 case 1702 /* OPEN_CIRCULATION_EXISTS */ :
407                                                         msg += test_permit[i].desc + '\n';
408                                                         found_handled = true;
409                                                         obj.error.yns_alert(msg,'Check Out Failed','OK',null,null,'Check here to confirm this message');
410                                                 break;
411                                                 case 7014 /* COPY_IN_TRANSIT */ :
412                                                         msg += test_permit[i].desc + '\n';
413                                                         found_handled = true;
414                                                         obj.error.yns_alert(msg,'Check Out Failed','OK',null,null,'Check here to confirm this message');
415                                                 break;
416                                                 case -1 /* NETWORK_FAILURE */ :
417                                                         msg += 'There was a network failure.\n';
418                                                         found_handled = true;
419                                                         obj.error.yns_alert(msg,'Check Out Failed','OK',null,null,'Check here to confirm this message');
420                                                 break;
421                                                 default:
422                                                         msg += 'FIXME: ' + js2JSON(test_permit[i]) + '\n';
423                                                         found_not_handled = true;
424                                                 break;
425                                         }
426                                 }
427                                 
428                                 if (found_not_handled) {
429                                         obj.error.standard_unexpected_error_alert(msg,permit);
430                                 }
431
432                         }
433
434                 } catch(E) {
435                         if (E.ilsevent && E.ilsevent == -1) {
436                                 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');
437                         } else {
438                                 obj.error.standard_unexpected_error_alert('Check Out Failed',E);
439                         }
440                         if (typeof obj.on_failure == 'function') {
441                                 obj.on_failure(E);
442                         }
443                         if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
444                                 obj.error.sdump('D_CIRC','circ.checkout: Calling external .on_failure()\n');
445                                 window.xulG.on_failure(E);
446                         } else {
447                                 obj.error.sdump('D_CIRC','circ.checkout: No external .on_failure()\n');
448                         }
449                 }
450
451         },
452
453         'on_checkout' : function() {
454                 this.controller.view.checkout_menu.selectedIndex = 0;
455                 this.controller.view.checkout_barcode_entry_textbox.disabled = false;
456                 this.controller.view.checkout_barcode_entry_textbox.value = '';
457                 this.controller.view.checkout_barcode_entry_textbox.focus();
458         },
459
460         'on_failure' : function() {
461                 this.controller.view.checkout_barcode_entry_textbox.select();
462                 this.controller.view.checkout_barcode_entry_textbox.focus();
463         }
464 }
465
466 dump('exiting circ.checkout.js\n');