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