afe6dcd7ffbdbd26c755773b415f1fd15b607c9b
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / patron / items.js
1 dump('entering patron.items.js\n');
2
3 function $(id) { return document.getElementById(id); }
4
5 if (typeof patron == 'undefined') patron = {};
6 patron.items = function (params) {
7
8     JSAN.use('util.error'); this.error = new util.error();
9     JSAN.use('util.network'); this.network = new util.network();
10     JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init({'via':'stash'});
11     JSAN.use('util.sound'); this.sound = new util.sound();
12 }
13
14 patron.items.prototype = {
15
16     'list_circ_map' : {},
17     'list_circ_map_by_copy' : {},
18
19     'init' : function( params ) {
20
21         var obj = this;
22
23         obj.patron_id = params['patron_id'];
24
25         obj.init_lists();
26
27         JSAN.use('util.controller'); obj.controller = new util.controller();
28         obj.controller.init(
29             {
30                 'control_map' : {
31                     'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
32                     'save_columns2' : [ [ 'command' ], function() { obj.list2.save_columns(); } ],
33                     'cmd_broken' : [ ['command'], function() { alert('Not Yet Implemented'); } ],
34                     'sel_clip' : [ ['command'], function() { obj.list.clipboard(); } ],
35                     'sel_clip2' : [ ['command'], function() { obj.list2.clipboard(); } ],
36                     'sel_edit' : [ ['command'], function() { obj.spawn_copy_editor(1); } ],
37                     'sel_edit2' : [ ['command'], function() { obj.spawn_copy_editor(2); } ],
38                     'sel_patron' : [ ['command'], function() { JSAN.use('circ.util'); circ.util.show_last_few_circs(obj.retrieve_ids); } ],
39                     'cmd_triggered_events' : [
40                         ['command'],
41                         function() {
42                             try {
43                                 var copy_ids = util.functional.map_list( obj.retrieve_ids, function(o) { return o.copy_id; } ); 
44                                 var barcodes = util.functional.map_list( obj.retrieve_ids, function(o) { return o.barcode; } ); 
45                                 for (var i = 0; i < copy_ids.length; i++) {
46                                     xulG.new_tab(
47                                         xulG.url_prefix(urls.XUL_REMOTE_BROWSER),
48                                         {
49                                             'tab_name' : document.getElementById('commonStrings').getFormattedString('tab.label.triggered_events_for_copy',[ barcodes[i] ])
50                                         },
51                                         {
52                                             'url': urls.EG_TRIGGER_EVENTS + "?copy_id=" + copy_ids[i],
53                                             'show_nav_buttons': false,
54                                             'show_print_button': false
55                                         }
56                                     );
57                                 }
58                             } catch(E) {
59                                 alert('Error in items.js, cmd_triggered_events: ' + E);
60                             }
61                         }
62                     ],
63                     'cmd_triggered_events2' : [
64                         ['command'],
65                         function() {
66                             try {
67                                 var copy_ids = util.functional.map_list( obj.retrieve_ids2, function(o) { return o.copy_id; } ); 
68                                 for (var i = 0; i < copy_ids.length; i++) {
69                                     xulG.new_tab(
70                                         xulG.url_prefix(urls.XUL_REMOTE_BROWSER),
71                                         {},
72                                         {
73                                             'url': urls.EG_TRIGGER_EVENTS + "?copy_id=" + copy_ids[i],
74                                             'show_nav_buttons': false,
75                                             'show_print_button': false
76                                         }
77                                     );
78                                 }
79                             } catch(E) {
80                                 alert('Error in copy_status.js, cmd_triggered_events: ' + E);
81                             }
82                         }
83                     ],
84
85                     'sel_bucket' : [
86                         ['command'],
87                         function() {
88                             JSAN.use('cat.util');
89                             cat.util.add_copies_to_bucket(util.functional.map_list( obj.retrieve_ids, function(o) { return o.copy_id; } ) );
90                         }
91                     ],
92                     'sel_bucket2' : [
93                         ['command'],
94                         function() {
95                             JSAN.use('cat.util');
96                             cat.util.add_copies_to_bucket(util.functional.map_list( obj.retrieve_ids2, function(o) { return o.copy_id; } ) );
97                         }
98                     ],
99                     'sel_mark_items_damaged' : [
100                         ['command'],
101                         function() {
102                             JSAN.use('cat.util'); JSAN.use('util.functional');
103                             cat.util.mark_item_damaged( util.functional.map_list( obj.retrieve_ids, function(o) { return o.copy_id; } ) );
104                         }
105                     ],
106                     'sel_mark_items_missing' : [
107                         ['command'],
108                         function() {
109                             JSAN.use('cat.util'); JSAN.use('util.functional');
110                             cat.util.mark_item_missing( util.functional.map_list( obj.retrieve_ids, function(o) { return o.copy_id; } ) );
111                         }
112                     ],
113                     'sel_mark_items_damaged2' : [
114                         ['command'],
115                         function() {
116                             JSAN.use('cat.util'); JSAN.use('util.functional');
117                             cat.util.mark_item_damaged( util.functional.map_list( obj.retrieve_ids2, function(o) { return o.copy_id; } ) );
118                         }
119                     ],
120                     'sel_mark_items_missing2' : [
121                         ['command'],
122                         function() {
123                             JSAN.use('cat.util'); JSAN.use('util.functional');
124                             cat.util.mark_item_missing( util.functional.map_list( obj.retrieve_ids2, function(o) { return o.copy_id; } ) );
125                         }
126                     ],
127                     'sel_copy_details' : [ ['command'],
128                         function() {
129                             JSAN.use('circ.util');
130                             circ.util.item_details_new(
131                                 util.functional.map_list(
132                                     obj.retrieve_ids,
133                                     function(o) { return o.barcode; }
134                                 )
135                             );
136                         }
137                     ],
138                     'sel_patron2' : [ ['command'], function() { JSAN.use('circ.util'); circ.util.show_last_few_circs(obj.retrieve_ids2); } ],
139                     'sel_copy_details2' : [ ['command'],
140                         function() {
141                             JSAN.use('circ.util');
142                             circ.util.item_details_new(
143                                 util.functional.map_list(
144                                     obj.retrieve_ids2,
145                                     function(o) { return o.barcode; }
146                                 )
147                             );
148                         }
149                     ],
150                     'cmd_items_print' : [ ['command'], function() { obj.items_print(1); } ],
151                     'cmd_items_print2' : [ ['command'], function() { obj.items_print(2); } ],
152                     'cmd_items_print_selected' : [ ['command'], function() { obj.items_print_selected(1); } ],
153                     'cmd_items_print_selected2' : [ ['command'], function() { obj.items_print_selected(2); } ],
154                     'cmd_items_export' : [ ['command'], function() { obj.items_export(1); } ],
155                     'cmd_items_export2' : [ ['command'], function() { obj.items_export(2); } ],
156                     'cmd_items_renew' : [ ['command'], function() { obj.items_renew({which_list:1}); } ],
157                     'cmd_items_renew_with_date' : [ ['command'], function() { obj.items_renew({which_list:1,skip_prompt:true,get_date:true}); } ],
158                     'cmd_items_renew_all' : [ ['command'], function() { obj.items_renew_all({which_list:1}); } ],
159                     'cmd_items_renew2' : [ ['command'], function() { obj.items_renew({which_list:2}); } ],
160                     'cmd_items_renew_with_date2' : [ ['command'], function() { obj.items_renew({which_list:2,skip_prompt:true,get_date:true}); } ],
161                     'cmd_items_edit' : [ ['command'], function() { obj.items_edit(1);  obj.refresh_outer_interfaces(); } ],
162                     'cmd_items_edit2' : [ ['command'], function() { obj.items_edit(2);  obj.refresh_outer_interfaces(); } ],
163                     'cmd_items_mark_lost' : [ ['command'], function() { obj.items_mark_lost(1);  obj.refresh_outer_interfaces(); } ],
164                     'cmd_items_mark_lost2' : [ ['command'], function() { obj.items_mark_lost(2);  obj.refresh_outer_interfaces(); } ],
165                     'cmd_items_claimed_returned' : [ ['command'], function() { obj.items_claimed_returned(1);  obj.refresh_outer_interfaces(); } ],
166                     'cmd_items_claimed_returned2' : [ ['command'], function() { obj.items_claimed_returned(2);  obj.refresh_outer_interfaces(); } ],
167                     'cmd_items_claimed_never_checked_out' : [
168                         ['command'],
169                         function() {
170                             obj.items_checkin(
171                                 1,{'claims_never_checked_out':true}
172                             );
173                         }
174                     ],
175                     'cmd_items_claimed_never_checked_out2' : [
176                         ['command'],
177                         function() {
178                             obj.items_checkin(
179                                 2,{'claims_never_checked_out':true}
180                             );
181                         }
182                     ],
183                     'cmd_items_checkin' : [ ['command'], function() { obj.items_checkin(1);  obj.refresh_outer_interfaces(); } ],
184                     'cmd_items_checkin2' : [ ['command'], function() { obj.items_checkin(2);  obj.refresh_outer_interfaces(); } ],
185                     'cmd_show_catalog' : [ ['command'], function() { obj.show_catalog(1); } ],
186                     'cmd_show_catalog2' : [ ['command'], function() { obj.show_catalog(2); } ],
187                     'cmd_add_billing' : [ ['command'], function() { obj.add_billing(1);  obj.refresh_outer_interfaces(); } ],
188                     'cmd_add_billing2' : [ ['command'], function() { obj.add_billing(2);  obj.refresh_outer_interfaces(); } ],
189                     'cmd_show_noncats' : [ ['command'], function() { obj.show_noncats(); } ]
190                 }
191             }
192         );
193
194         obj.retrieve();
195
196         obj.controller.view.sel_mark_items_damaged.setAttribute('disabled','true');
197         obj.controller.view.sel_mark_items_missing.setAttribute('disabled','true');
198         obj.controller.view.sel_mark_items_damaged2.setAttribute('disabled','true');
199         obj.controller.view.sel_mark_items_missing2.setAttribute('disabled','true');
200         obj.controller.view.sel_clip.setAttribute('disabled','true');
201         obj.controller.view.sel_clip2.setAttribute('disabled','true');
202         obj.controller.view.sel_edit.setAttribute('disabled','true');
203         obj.controller.view.sel_edit2.setAttribute('disabled','true');
204         obj.controller.view.sel_bucket.setAttribute('disabled','true');
205         obj.controller.view.sel_bucket2.setAttribute('disabled','true');
206         obj.controller.view.sel_copy_details.setAttribute('disabled','true');
207         obj.controller.view.sel_patron.setAttribute('disabled','true');
208         obj.controller.view.cmd_triggered_events.setAttribute('disabled','true');
209         obj.controller.view.sel_copy_details2.setAttribute('disabled','true');
210         obj.controller.view.sel_patron2.setAttribute('disabled','true');
211         obj.controller.view.cmd_triggered_events2.setAttribute('disabled','true');
212         obj.controller.view.cmd_items_claimed_returned.setAttribute('disabled','true');
213         obj.controller.view.cmd_items_claimed_never_checked_out.setAttribute('disabled','true');
214         obj.controller.view.cmd_items_renew.setAttribute('disabled','true');
215         obj.controller.view.cmd_items_renew_with_date.setAttribute('disabled','true');
216         obj.controller.view.cmd_items_checkin.setAttribute('disabled','true');
217         obj.controller.view.cmd_items_edit.setAttribute('disabled','true');
218         obj.controller.view.cmd_items_mark_lost.setAttribute('disabled','true');
219         obj.controller.view.cmd_show_catalog.setAttribute('disabled','true');
220         obj.controller.view.cmd_items_claimed_returned2.setAttribute('disabled','true');
221         obj.controller.view.cmd_items_claimed_never_checked_out2.setAttribute('disabled','true');
222         obj.controller.view.cmd_items_renew2.setAttribute('disabled','true');
223         obj.controller.view.cmd_items_renew_with_date2.setAttribute('disabled','true');
224         obj.controller.view.cmd_items_checkin2.setAttribute('disabled','true');
225         obj.controller.view.cmd_items_edit2.setAttribute('disabled','true');
226         obj.controller.view.cmd_items_mark_lost2.setAttribute('disabled','true');
227         obj.controller.view.cmd_show_catalog2.setAttribute('disabled','true');
228     },
229
230     'cleanup' : function() {
231         var obj = this;
232         obj.list.cleanup();
233         obj.list.clear();
234         obj.list2.cleanup();
235         obj.list2.clear();
236     },
237
238     'show_noncats' : function() {
239         var obj = this; var checkout = {};
240         try {
241             var robj = obj.network.simple_request('FM_ANCC_RETRIEVE_VIA_USER.authoritative',[ ses(), obj.patron_id ]);
242             if (typeof robj.ilsevent != 'undefined') throw(robj);
243
244             for (var ii = 0; ii < robj.length; ii++) {
245                 try {
246                     var nc_circ = obj.network.simple_request('FM_ANCC_RETRIEVE_VIA_ID',[ ses(), robj[ii] ]);
247                     if (typeof nc_circ.ilsevent != 'undefined') throw(nc_circ);
248                     var fake_circ = new aoc();
249                     fake_circ.circ_lib( nc_circ.circ_lib() );
250                     fake_circ.circ_staff( nc_circ.staff() );
251                     fake_circ.usr( nc_circ.patron() );
252                     fake_circ.xact_start( nc_circ.circ_time() );
253                     fake_circ.renewal_remaining(0);
254                     fake_circ.stop_fines('Non-Cataloged');
255                     fake_circ.due_date( nc_circ.duedate() );
256                         
257                     if (!obj.data.hash.cnct[ nc_circ.item_type() ]) {
258                         var robj2 = obj.network.simple_request('FM_CNCT_RETRIEVE',[ nc_circ.circ_lib() ]);
259                         if (typeof robj2.ilsevent != 'undefined') throw(robj);
260                         obj.data.stash_retrieve();
261                         for (var j = 0; j < robj2.length; j++) {
262                             if (! obj.data.hash.cnct[ robj2[j].id() ] ) {
263                                 obj.data.hash.cnct[ robj2[j].id() ] = robj2[j];
264                                 obj.data.list.cnct.push( robj2[j] );
265                             }
266                         }
267                         obj.data.stash('hash','list');
268                     }
269     
270                     var fake_record = new mvr();
271                     fake_record.title( obj.data.hash.cnct[ nc_circ.item_type() ].name());
272     
273                     var fake_copy = new acp();
274                     fake_copy.barcode( '' );
275                     fake_copy.circ_lib( nc_circ.circ_lib() );
276
277                     obj.list.append( { 'row' : { 'my' : { 'circ' : fake_circ, 'mvr' : fake_record, 'acp' : fake_copy } }, 'to_bottom' : true, 'no_auto_select' : true } );
278
279                 } catch(F) {
280                     obj.error.standard_unexpected_error_alert($("patronStrings").getFormattedString('staff.patron.items.show_noncats.err_showing_noncat', [robj[ii].id()]),F);
281                 }
282             }
283
284         } catch(E) {
285             obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.items.show_noncats.err_showing_circulations'),E);
286         }
287     },
288
289     'items_print' : function(which) {
290         var obj = this;
291         try {
292             var list = (which==2 ? obj.list2 : obj.list);
293             JSAN.use('patron.util');
294             var params = { 
295                 'patron' : patron.util.retrieve_fleshed_au_via_id(ses(),obj.patron_id), 
296                 'printer_context' : 'receipt', 
297                 'template' : 'items_out'
298             };
299             list.print( params );
300         } catch(E) {
301             obj.error.standard_unexpected_error_alert('printing 1',E);
302         }
303     },
304     'items_print_selected': function(which) {
305         var obj = this;
306         try {
307             JSAN.use('patron.util');
308             // JSAN.use('util.functional');
309             var list = ( which == 2 ? obj.list2 : obj.list);
310             // Selected Rows
311             var list_dump = list.dump_with_keys();
312             var pick_list = ( which == 2 ? obj.retrieve_ids2 : obj.retrieve_ids );
313             if (!pick_list || pick_list.length == 0) return;
314
315             // I don't know a better way to just get all the data from one row
316             for (var i=0; i<pick_list.length; i++) {
317                 var bc = pick_list[i].barcode;
318                 for (var j=0; j<list_dump.length; j++) {
319                     if (pick_list[i].barcode == list_dump[j].barcode) {
320                         pick_list[i] = list_dump[j];
321                     }
322                 }
323             }
324
325             var params = {
326                 'list': pick_list,
327                 'patron' : patron.util.retrieve_fleshed_au_via_id(ses(),obj.patron_id),
328                 'printer_context' : 'receipt',
329                 'template' : 'items_out'
330             };
331             list.print( params );
332         } catch(E) {
333             obj.error.standard_unexpected_error_alert('items_print_selected has failed',E);
334         }
335     },
336     'items_export' : function(which) {
337         var obj = this;
338         try {
339             var list = (which==2 ? obj.list2 : obj.list);
340             list.dump_csv_to_clipboard();
341         } catch(E) {
342             obj.error.standard_unexpected_error_alert('export 1',E);
343         }
344     },
345
346     'items_renew_all' : function(params) {
347         try {
348             if (!params) params = {};
349             var obj = this; var list = obj.list;
350             if (list.on_all_fleshed != null) {
351                 var r = window.confirm($("patronStrings").getString('staff.patron.items.items_renew_all.list_is_busy'));
352                 if (!r) return;
353             }
354             var r = window.confirm($("patronStrings").getString('staff.patron.items.items_renew_all.renew_items_in_list'));
355             if (!r) return;
356             obj.list.select_all();
357             function flesh_callback() {
358                 try {
359                     setTimeout(function(){
360                             list.on_all_fleshed = null;
361                             params.skip_prompt = true;
362                             if (!params.which_list) params.which_list = 1;
363                             obj.items_renew(params);    
364                         },0);
365                 } catch(E) {
366                     obj.error.standard_unexpected_error_alert($("patronStrings").getFormattedString('staff.patron.items.items_renew_all.items_not_renewed', ['2']),E);
367                 }
368             }
369             list.on_all_fleshed = flesh_callback;
370             list.full_retrieve();
371         } catch(E) {
372             this.error.standard_unexpected_error_alert($("patronStrings").getFormattedString('staff.patron.items.items_renew_all.items_not_renewed', ['1']),E);
373         }
374     },
375
376     'items_renew' : function(params) {
377         var obj = this;
378         try{
379             if (!params) { params = {}; }
380             JSAN.use('circ.util');
381             var retrieve_ids = ( params.which_list == 2 ? obj.retrieve_ids2 : obj.retrieve_ids );
382             if (!retrieve_ids || retrieve_ids.length == 0) return;
383             JSAN.use('util.functional');
384             if (!params.skip_prompt) {
385                 var msg = '';
386                 if(retrieve_ids.length > 1) {
387                     msg += $("patronStrings").getFormattedString('staff.patron.items.items_renew.renew_item_plural',[util.functional.map_list( retrieve_ids, function(o){return o.barcode;}).join(', ')]);
388                 } else {
389                     msg += $("patronStrings").getFormattedString('staff.patron.items.items_renew.renew_item_singular', [util.functional.map_list( retrieve_ids, function(o){return o.barcode;}).join(', ')]);
390                 }
391                 var r = window.confirm(msg);
392                 if (!r) { return; }
393             }
394
395             var count = 0;
396
397             if (params.get_date) {
398                 JSAN.use('util.functional');
399                 var title = $("patronStrings").getString('staff.patron.items.items_edit.renew_with_date.title');
400                 var msg = $("patronStrings").getFormattedString('staff.patron.items.items_edit.renew_with_date.prompt', [util.functional.map_list(retrieve_ids,function(o){return o.barcode;}).join(', ')]);
401                 var desc = $("patronStrings").getString('staff.patron.items.items_edit.renew_with_date.description');
402
403                 JSAN.use('util.window'); var win = new util.window();
404                 var my_xulG = win.open( 
405                     urls.XUL_TIMESTAMP_DIALOG, 'edit_renew_due_date', 'chrome,resizable,modal', 
406                     { 
407                         'title' : title, 
408                         'description' : desc, 
409                         'msg' : msg, 
410                         'allow_unset' : false,
411                         'disallow_future_dates' : false,
412                         'disallow_past_dates' : false,
413                         'disallow_today' : false,
414                         'time_readonly' : false
415                     }
416                 );
417
418                 if (my_xulG.complete) {
419                     params.due_date = my_xulG.timestamp;
420                 }
421             }
422
423             function gen_renew(bc,circ_id) {
424                 var x = document.getElementById('renew_msgs');
425                 if (x) {
426                     var l = document.createElement('label');
427                     l.setAttribute('value', $("patronStrings").getFormattedString('staff.patron.items.items_renew.renewing',[bc]));
428                     x.appendChild(l);
429                 }
430                 var p = { 'barcode' : bc, 'patron' : obj.patron_id };
431                 if (params.due_date) p.due_date = params.due_date;
432                 var r = circ.util.renew_via_barcode( p );
433                 try {
434                     if ( (typeof r[0].ilsevent != 'undefined' && r[0].ilsevent == 0) ) {
435                         l.setAttribute('value', $("patronStrings").getFormattedString('staff.patron.items.items_renew.renewed',[bc]));
436                         obj.list_circ_map[ circ_id ].row.my.circ = r[0].payload.circ;
437                         obj.list_circ_map[ circ_id ].row.my.acp = r[0].payload.copy;
438                         obj.list_circ_map[ circ_id ].row.my.mvr = r[0].payload.record;
439                         // A renewed circ is a new circ, and has a new circ_id.
440                         obj.list_circ_map[ r[0].payload.circ.id() ] = obj.list_circ_map[ circ_id ];
441                         obj.list_circ_map_by_copy[ r[0].payload.copy.id() ] = r[0].payload.circ.id();
442                     } else {
443                         var msg = $("patronStrings").getFormattedString('staff.patron.items.items_renew.not_renewed',[bc, r[0].textcode + r[0].desc]);
444                         l.setAttribute('value', msg);
445                         alert(msg);
446                     }
447                     count--;
448                     if (count == 0) {
449                         //if (window.confirm('Action completed. Refresh list?')) obj.retrieve();
450                         JSAN.use('util.widgets'); util.widgets.remove_children(x);
451                     }
452                     obj.refresh(circ_id);
453                 } catch(E) {
454                       obj.error.standard_unexpected_error_alert($("patronStrings").getFormattedString('staff.patron.items.items_renew.err_in_renew_via_barcode',[bc]), E);
455                 }
456             }
457
458             for (var i = 0; i < retrieve_ids.length; i++) {
459                 try {
460                     count++;
461                     var barcode = retrieve_ids[i].barcode;
462                     var circ_id = retrieve_ids[i].circ_id;
463                     gen_renew(barcode,circ_id);
464                 } catch(E) {
465                     obj.error.standard_unexpected_error_alert($("patronStrings").getFormattedString('staff.patron.items.items_renew.no_renew_for_barcode',[barcode]), E);
466                 }
467             }
468         } catch(E) {
469             obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.items.items_renew.no_renew'),E);
470         }
471     },
472
473     'items_edit' : function(which) {
474             var obj = this;
475             try {
476                 var retrieve_ids = ( which == 2 ? obj.retrieve_ids2 : obj.retrieve_ids );
477                 if (!retrieve_ids || retrieve_ids.length == 0) return;
478
479                 JSAN.use('util.functional');
480                 var title = '';
481                 if (retrieve_ids.length > 1) {
482                     title += $("patronStrings").getString('staff.patron.items.items_edit.edit_due_date.plural');
483                 } else {
484                     title += $("patronStrings").getString('staff.patron.items.items_edit.edit_due_date.singular');
485                 }
486                 var value = 'YYYY-MM-DD';
487                 var msg = $("patronStrings").getFormattedString('staff.patron.items.items_edit.new_due_date', [util.functional.map_list(retrieve_ids,function(o){return o.barcode;}).join(', ')]);
488                 var desc = $("patronStrings").getString('staff.patron.items.items_edit.edit_due_date.description');
489
490                 JSAN.use('util.window'); var win = new util.window();
491                 var my_xulG = win.open( 
492                     urls.XUL_TIMESTAMP_DIALOG, 'edit_due_date', 'chrome,resizable,modal', 
493                     { 
494                         'title' : title, 
495                         'description' : desc, 
496                         'msg' : msg, 
497                         'allow_unset' : false,
498                         'disallow_future_dates' : false,
499                         'disallow_past_dates' : false,
500                         'disallow_today' : false,
501                         'time_readonly' : false
502                     }
503                 );
504
505                 if (my_xulG.complete) {
506                     // XXX We need to append the time component from the original due date to the entered
507                     // date here, if (circ interval % 1 day == 0)
508                     // XXX I think the middle layer is doing this now
509                     var circs = util.functional.map_list(retrieve_ids,function(o){return o.circ_id;});
510                     for (var i = 0; i < circs.length; i++) {
511                         var robj = obj.network.simple_request('FM_CIRC_EDIT_DUE_DATE',[ses(),circs[i],my_xulG.timestamp]);
512                         if (typeof robj.ilsevent != 'undefined') { if (robj.ilsevent != 0) throw(robj); }
513                         obj.list_circ_map[ circs[i] ].row.my.circ = robj;
514                         obj.list_circ_map_by_copy[ robj.target_copy() ] = circs[i];
515                         obj.refresh(circs[i]);
516                     }
517                 }
518             } catch(E) {
519                 obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.items.items_edit.dates_not_modified'),E);
520             }
521     },
522
523     'items_mark_lost' : function(which) {
524         var obj = this;
525         try {
526             var retrieve_ids = ( which == 2 ? obj.retrieve_ids2 : obj.retrieve_ids );
527             if (!retrieve_ids || retrieve_ids.length == 0) return;
528             for (var i = 0; i < retrieve_ids.length; i++) {
529                 var barcode = retrieve_ids[i].barcode;
530                 dump($("patronStrings").getFormattedString('staff.patron.items.items_edit.mark_barcode_lost', [barcode]));
531                 var robj = obj.network.simple_request( 'MARK_ITEM_LOST', [ ses(), { barcode: barcode } ]);
532                 if (typeof robj.ilsevent != 'undefined') { 
533                     switch(Number(robj.ilsevent)) {
534                         case 7018 /* COPY_MARKED_LOST */ :
535                             alert( $("patronStrings").getFormattedString('staff.patron.items.items_edit.item_barcode', [barcode, robj.desc]) );
536                         break;
537                         default: throw(robj);
538                     }
539                 } else {
540                     obj.refresh(retrieve_ids[i].circ_id,true);
541                 }
542             }
543         } catch(E) {
544             obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.items.items_edit.items_not_marked_lost'),E);
545         }
546     },
547
548     'items_claimed_returned' : function(which) {
549         var obj = this;
550         try {
551             JSAN.use('util.date');
552             var retrieve_ids = ( which == 2 ? obj.retrieve_ids2 : obj.retrieve_ids );
553
554             JSAN.use('util.functional');
555             var title = $("patronStrings").getString('staff.patron.items.items_claimed_returned.claimed_returned');
556             var value = 'YYYY-MM-DD';
557             var msg = $("patronStrings").getFormattedString('staff.patron.items.items_claimed_returned.enter_returned_date',
558                 [util.functional.map_list(retrieve_ids,function(o){return o.barcode;}).join(', ')]);
559             var desc = $("patronStrings").getString('staff.patron.items.items_claimed_returned.claimed_returned.description');
560
561             JSAN.use('util.window'); var win = new util.window();
562             var my_xulG = win.open( 
563                 urls.XUL_TIMESTAMP_DIALOG, 'edit_claimed_returned', 'chrome,resizable,modal', 
564                 { 
565                     'title' : title, 
566                     'description' : desc, 
567                     'msg' : msg, 
568                     'allow_unset' : false,
569                     'disallow_future_dates' : true,
570                     'disallow_past_dates' : false,
571                     'disallow_today' : false,
572                     'time_readonly' : false
573                 }
574             );
575
576
577             if (my_xulG.complete) {
578                 var barcodes = util.functional.map_list(retrieve_ids,function(o){return o.barcode;});
579                 var do_not_move_these = {};
580                 for (var i = 0; i < barcodes.length; i++) {
581                     var robj = obj.network.simple_request(
582                         'MARK_ITEM_CLAIM_RETURNED', 
583                         [ ses(), { barcode: barcodes[i], backdate: my_xulG.timestamp } ],
584                         null,
585                         {
586                             'title' : $("patronStrings").getString('staff.patron.items.set_claim_returned_failure'),
587                             'overridable_events' : [
588                                 'PATRON_EXCEEDS_CLAIMS_RETURN_COUNT'                                
589                             ]
590                         }
591                     );
592                     if (typeof robj.ilsevent != 'undefined') { 
593                         if (robj.ilsevent != 0 && robj.textcode != 'PATRON_EXCEEDS_CLAIMS_RETURN_COUNT' ) {
594                             do_not_move_these[ barcodes[i] ] = true;
595                             obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.items.items_claimed_returned.not_marked_claimed_returned'),robj);
596                         }
597                         if (robj.textcode == 'PATRON_EXCEEDS_CLAIMS_RETURN_COUNT') {
598                             do_not_move_these[ barcodes[i] ] = true;
599                         }
600                     }
601                 }
602                 for (var i = 0; i < retrieve_ids.length; i++) {
603                     obj.refresh(retrieve_ids[i].circ_id, !do_not_move_these[ retrieve_ids[i].barcode ]);
604                 }
605             }
606         } catch(E) {
607             obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.items.items_claimed_returned.not_marked_claimed_returned'),E);
608         }
609     },
610
611     'items_checkin' : function(which,options) {
612         var obj = this;
613         try {
614             if (!options) { options = {}; }
615             var retrieve_ids = ( which == 2 ? obj.retrieve_ids2 : obj.retrieve_ids );
616             if (!retrieve_ids || retrieve_ids.length == 0) return;
617             JSAN.use('util.functional');
618             var msg = '';
619             if (options.claims_never_checked_out) {
620                 if (retrieve_ids.length > 1) {
621                     msg += $("patronStrings").getFormattedString(
622                         'staff.patron.items.items_checkin.confirm_item_claimed_never_checked_out.plural',
623                         [
624                             util.functional.map_list(
625                                 retrieve_ids,
626                                 function(o){return o.barcode;}
627                             ).join(', ')
628                         ]
629                     );
630                 } else {
631                     msg += $("patronStrings").getFormattedString(
632                         'staff.patron.items.items_checkin.confirm_item_claimed_never_checked_out.singular',
633                         [
634                             retrieve_ids[0].barcode,
635                         ]
636                     );
637                 }
638             } else {
639                 if (retrieve_ids.length > 1) {
640                     msg += $("patronStrings").getFormattedString(
641                         'staff.patron.items.items_checkin.confirm_item_check_in.plural',
642                         [
643                             util.functional.map_list(
644                                 retrieve_ids,
645                                 function(o){return o.barcode;}
646                             ).join(', ')
647                         ]
648                     );
649                 } else {
650                     msg += $("patronStrings").getFormattedString(
651                         'staff.patron.items.items_checkin.confirm_item_check_in.singular',
652                         [
653                             retrieve_ids[0].barcode,
654                         ]
655                     );
656                 }
657
658             }
659             var r = window.confirm(msg);
660             if (!r) { return; }
661             var auto_print = document.getElementById('checkin_auto_print_slips');
662             if (auto_print) auto_print = auto_print.getAttribute('checked') == 'true';
663             JSAN.use('circ.util');
664             for (var i = 0; i < retrieve_ids.length; i++) {
665                 var copy_id = retrieve_ids[i].copy_id;
666                 dump('Check in copy_id = ' + copy_id + ' barcode = ' + retrieve_ids[i].barcode + '\n');
667                 var opt = JSON2js( js2JSON( options || {} ) ); // clone options
668                 opt.copy_id = copy_id;
669                 var robj = circ.util.checkin_via_barcode(
670                     ses(),
671                     opt,
672                     false /* backdate */,
673                     auto_print
674                 );
675                 /* circ.util.checkin_via_barcode handles errors currently */
676             }
677             obj.retrieve();
678         } catch(E) {
679             obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.items.items_checkin.no_checkin'),E);
680         }
681     },
682
683     'show_catalog' : function(which) {
684         var obj = this;
685         try {
686             var retrieve_ids = ( which == 2 ? obj.retrieve_ids2 : obj.retrieve_ids );
687             if (!retrieve_ids || retrieve_ids.length == 0) return;
688             for (var i = 0; i < retrieve_ids.length; i++) {
689                 var doc_id = retrieve_ids[i].doc_id;
690                 if (!doc_id) {
691                     alert($("patronStrings").getFormattedString('staff.patron.items.show_catalog.barcode_not_cataloged', [retrieve_ids[i].barcode]));
692                     continue;
693                 }
694                 var opac_url = xulG.url_prefix('opac_rdetail') + doc_id;
695                 var content_params = { 
696                     'session' : ses(),
697                     'authtime' : ses('authtime'),
698                     'opac_url' : opac_url
699                 };
700                 xulG.new_tab(
701                     xulG.url_prefix('XUL_OPAC_WRAPPER'), 
702                     {'tab_name': $("patronStrings").getString('staff.patron.items.show_catalog.retrieving_title')}, 
703                     content_params
704                 );
705             }
706         } catch(E) {
707             obj.error.standard_unexpected_error_alert('',E);
708         }
709     },
710
711     'add_billing' : function(which) {
712         var obj = this;
713         try {
714             var retrieve_ids = ( which == 2 ? obj.retrieve_ids2 : obj.retrieve_ids );
715             if (!retrieve_ids || retrieve_ids.length == 0) return;
716             JSAN.use('util.window'); var win = new util.window();
717             for (var i = 0; i < retrieve_ids.length; i++) {
718                 var circ_id = retrieve_ids[i].circ_id;
719                 var my_xulG = win.open(
720                     urls.XUL_PATRON_BILL_WIZARD,
721                         //+ '?patron_id=' + window.escape(obj.patron_id)
722                         //+ '&xact_id=' + window.escape( circ_id ),
723                     'billwizard',
724                     'chrome,resizable,modal',
725                     { 'patron_id' : obj.patron_id, 'xact_id' : circ_id }
726                 );
727             }
728         } catch(E) {
729             obj.error.standard_unexpected_error_alert('',E);
730         }
731     },
732
733     'init_lists' : function() {
734         var obj = this;
735
736         JSAN.use('circ.util');
737         var columns = circ.util.columns( 
738             { 
739                 'barcode' : { 'hidden' : false },
740                 'checkout_lib' : { 'hidden' : false },
741                 'circ_lib' : { 'hidden' : false },
742                 'title' : { 'hidden' : false, 'flex' : '3' },
743                 'due_date' : { 'hidden' : false },
744                 'renewal_remaining' : { 'hidden' : false },
745                 'stop_fines' : { 'hidden' : false }
746             } 
747         );
748         var columns2 = circ.util.columns( 
749             { 
750                 'barcode' : { 'hidden' : false },
751                 'checkout_lib' : { 'hidden' : false },
752                 'circ_lib' : { 'hidden' : false },
753                 'title' : { 'hidden' : false, 'flex' : '3' },
754                 'checkin_time' : { 'hidden' : false },
755                 'stop_fines' : { 'hidden' : false }
756             } 
757         );
758
759         function retrieve_row(params) {
760             try { 
761                 var row = params.row;
762
763                 if (!row.my.circ_id) {
764                     if (typeof params.on_retrieve == 'function') { 
765                         params.on_retrieve(row); 
766                     }
767                     return row;
768                 }
769     
770                 if (!row.my.circ) {
771                     obj.network.simple_request(
772                         'FM_CIRC_DETAILS.authoritative',
773                         [ row.my.circ_id ],
774                         function(req) {
775                             try { 
776                                 var robj = req.getResultObject();
777                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
778                                 if (typeof robj.ilsevent == 'null') throw('null result');
779                                 row.my.circ = robj.circ;
780                                 row.my.acp = robj.copy;
781                                 row.my.mvr = robj.mvr;
782                                 row.my.acn = robj.volume;
783                                 row.my.record = robj.record;
784         
785                                 var copy_id = row.my.circ.target_copy();
786                                 if (typeof copy_id == 'object') {
787                                     if (copy_id != null) {
788                                         copy_id = copy_id.id();
789                                     } else {
790                                         if (typeof robj.copy == 'object' && robj.copy != null) copy_id = robj.copy.id();
791                                     }
792                                 } else {
793                                         if (typeof robj.copy == 'object' && robj.copy != null) copy_id = robj.copy.id();
794                                 }
795                                 
796                                 params.treeitem_node.setAttribute( 'retrieve_id', js2JSON({'copy_id':copy_id,'circ_id':row.my.circ.id(),'barcode':row.my.acp.barcode(),'doc_id': ( row.my.record ? row.my.record.id() : null ) }) );
797
798                                 obj.list_circ_map_by_copy[ copy_id ] = row.my.circ.id();
799             
800                                 if (typeof params.on_retrieve == 'function') {
801                                     params.on_retrieve(row);
802                                 }
803                             } catch(E) {
804                                 obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.items.retrieve_row.callback_error'), E);
805                             }
806                         }
807                     );
808                 } else {
809                     var copy_id = row.my.circ ? row.my.circ.target_copy() : null;
810                     if (typeof copy_id == 'object') {
811                         if (copy_id != null) {
812                             copy_id = copy_id.id();
813                         } else {
814                             if (typeof row.my.acp == 'object' && row.my.acp != null) copy_id = row.my.acp.id();
815                         }
816                     } else {
817                             if (typeof row.my.acp == 'object' && row.my.acp != null) copy_id = row.my.acp.id();
818                     }
819  
820                     params.treeitem_node.setAttribute( 'retrieve_id', js2JSON({'copy_id':row.my.acp.id(),'circ_id':row.my.circ.id(),'barcode':row.my.acp.barcode(),'doc_id': (row.my.record ? row.my.record.id() : null) }) );
821                     if (typeof params.on_retrieve == 'function') {
822                         params.on_retrieve(row);
823                     }
824                 }
825     
826                 return row;
827             } catch(E) {
828                 obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.items.retrieve_row.error_in_retrieve_row'),E);
829                 return params.row;
830             }
831         }
832
833         JSAN.use('util.list'); obj.list = new util.list('items_list');
834         obj.list.init(
835             {
836                 'columns' : columns,
837                 'retrieve_row' : retrieve_row,
838                 'on_select' : function(ev) {
839                     JSAN.use('util.functional');
840                     var sel = obj.list.retrieve_selection();
841                     obj.controller.view.sel_clip.setAttribute('disabled',sel.length < 1);
842                     obj.controller.view.sel_edit.setAttribute('disabled',sel.length < 1);
843                     var list = util.functional.map_list(
844                         sel,
845                         function(o) { return JSON2js( o.getAttribute('retrieve_id') ); }
846                     );
847                     if (typeof obj.on_select == 'function') {
848                         obj.on_select(list);
849                     }
850                     if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
851                         obj.error.sdump('D_PATRON','patron.items: Calling external .on_select()\n');
852                         window.xulG.on_select(list);
853                     } else {
854                         obj.error.sdump('D_PATRON','patron.items: No external .on_select()\n');
855                     }
856                 }
857             }
858         );
859         
860         obj.list2 = new util.list('items_list2');
861         obj.list2.init(
862             {
863                 'columns' : columns2,
864                 'retrieve_row' : retrieve_row,
865                 'on_select' : function(ev) {
866                     JSAN.use('util.functional');
867                     var sel = obj.list2.retrieve_selection();
868                     obj.controller.view.sel_clip2.setAttribute('disabled',sel.length < 1);
869                     obj.controller.view.sel_edit2.setAttribute('disabled',sel.length < 1);
870                     var list = util.functional.map_list(
871                         sel,
872                         function(o) { return JSON2js( o.getAttribute('retrieve_id') ); }
873                     );
874                     if (typeof obj.on_select2 == 'function') {
875                         obj.on_select2(list);
876                     }
877                 }
878             }
879         );
880     },
881
882     'refresh' : function(circ_id,move_to_bottom_list) {
883         var obj = this;
884         try {
885             var nparams = obj.list_circ_map[circ_id];
886             if (move_to_bottom_list) { 
887                 obj.list_circ_map[circ_id].treeitem_node.setAttribute('hidden','true');
888                 var nparams2 = obj.list2.append( { 'row' : { 'my' : { 'circ_id' : circ_id } },  'to_bottom' : true, 'which_list' : 1 } );
889                 obj.list_circ_map[circ_id] = nparams2; 
890             } else {
891                 var which_list = nparams.which_list;
892                 switch(which_list) {
893                     case 1:
894                     case '1':
895                         setTimeout(function(){try{obj.list2.refresh_row(nparams);}catch(E){
896                                                     obj.error.standard_unexpected_error_alert($("patronStrings").getFormattedString('staff.patron.items.refresh.error_refreshing_row2', [circ_id, nparams]),E);}},1000);
897                         break;
898                     default:
899                         setTimeout(function(){try{obj.list.refresh_row(nparams);}catch(E){
900                                                     obj.error.standard_unexpected_error_alert($("patronStrings").getFormattedString('staff.patron.items.refresh.error_refreshing_row2', [circ_id, nparams]),E);}},1000);
901                         break;
902                 }
903             }
904         } catch(E) {
905             obj.error.standard_unexpected_error_alert($("patronStrings").getFormattedString('staff.patron.items.refresh.error_refreshing_row', [circ_id, nparams]),E);
906         }
907     },
908
909     'retrieve' : function(dont_show_me_the_list_change) {
910         var obj = this;
911         if (window.xulG && window.xulG.checkouts) {
912             obj.checkouts = window.xulG.checkouts;
913         } else {
914             obj.checkouts = [];
915             obj.checkouts2 = [];
916             var robj = obj.network.simple_request(
917                 'FM_CIRC_RETRIEVE_VIA_USER.authoritative',
918                 [ ses(), obj.patron_id ]
919             );
920             if (typeof robj.ilsevent!='undefined') {
921                 obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.items.retrieve.err_retrieving_circulations'),E);
922             } else {
923                 obj.checkouts = obj.checkouts.concat( robj.overdue );
924                 obj.checkouts = obj.checkouts.concat( robj.out );
925                 obj.checkouts2 = obj.checkouts2.concat( robj.lost );
926                 obj.checkouts2 = obj.checkouts2.concat( robj.claims_returned );
927                 obj.checkouts2 = obj.checkouts2.concat( robj.long_overdue );
928             }
929             var robj = obj.network.simple_request(
930                 'FM_CIRC_IN_WITH_FINES_VIA_USER.authoritative',
931                 [ ses(), obj.patron_id ]
932             );
933             if (typeof robj.ilsevent!='undefined') {
934                 obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.items.retrieve.err_retrieving_circulations'),E);
935             } else {
936                 obj.checkouts2 = obj.checkouts2.concat( robj.lost );
937                 obj.checkouts2 = obj.checkouts2.concat( robj.claims_returned );
938                 obj.checkouts2 = obj.checkouts2.concat( robj.long_overdue );
939             }
940         }
941
942         function gen_list_append(circ_id,which_list) {
943             return function() {
944                 try {
945                     var nparams;
946                     switch(which_list) {
947                         case 1:
948                             nparams = obj.list2.append( { 'row' : { 'my' : { 'circ_id' : circ_id } },  'to_bottom' : true, 'which_list' : which_list } );
949                         break;
950                         default:
951                             nparams = obj.list.append( { 'row' : { 'my' : { 'circ_id' : circ_id } }, 'to_bottom' : true, 'which_list' : which_list } );
952                         break;
953                     }
954                     if (nparams) {
955                         obj.list_circ_map[circ_id] = nparams; // unlike item status interface, each circ should be in this list only once
956                     } else {
957                         throw('typeof nparams = ' + typeof nparams);
958                     }
959                 } catch(E) {
960                     obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.items.gen_list_append.error'),E);
961                 }
962             };
963         }
964
965         obj.list.clear(); obj.list2.clear();
966
967         JSAN.use('util.exec'); var exec = new util.exec();
968         var rows = [];
969         for (var i in obj.checkouts) {
970             rows.push( gen_list_append(obj.checkouts[i],0) );
971         }
972         for (var i in obj.checkouts2) {
973             rows.push( gen_list_append(obj.checkouts2[i],1) );
974         }
975         exec.chain( rows );
976         if (!dont_show_me_the_list_change) {
977             obj.refresh_outer_interfaces();
978         }
979     },
980
981     'refresh_outer_interfaces' : function() {
982         var obj = this;
983         if (window.xulG && typeof window.xulG.on_list_change == 'function') {
984             try { window.xulG.on_list_change(obj.checkouts); } catch(E) { this.error.sdump('D_ERROR',E); }
985         }
986     },
987
988     'on_select' : function(list) {
989
990         dump('patron.items.on_select list = ' + js2JSON(list) + '\n');
991
992         var obj = this;
993
994         obj.controller.view.cmd_items_claimed_returned.setAttribute('disabled','false');
995         obj.controller.view.cmd_items_claimed_never_checked_out.setAttribute('disabled','false');
996         obj.controller.view.cmd_items_renew.setAttribute('disabled','false');
997         obj.controller.view.cmd_items_renew_with_date.setAttribute('disabled','false');
998         obj.controller.view.cmd_items_checkin.setAttribute('disabled','false');
999         obj.controller.view.cmd_items_edit.setAttribute('disabled','false');
1000         obj.controller.view.cmd_items_mark_lost.setAttribute('disabled','false');
1001         obj.controller.view.cmd_show_catalog.setAttribute('disabled','false');
1002         obj.controller.view.sel_copy_details.setAttribute('disabled','false');
1003         obj.controller.view.sel_bucket.setAttribute('disabled','false');
1004         obj.controller.view.sel_patron.setAttribute('disabled','false');
1005         obj.controller.view.cmd_triggered_events.setAttribute('disabled','false');
1006         obj.controller.view.sel_mark_items_damaged.setAttribute('disabled','false');
1007         obj.controller.view.sel_mark_items_missing.setAttribute('disabled','false');
1008
1009         obj.retrieve_ids = list;
1010     },
1011
1012     'on_select2' : function(list) {
1013     
1014         dump('patron.items.on_select2 list = ' + js2JSON(list) + '\n');
1015
1016         var obj = this;
1017
1018         obj.controller.view.cmd_items_claimed_returned2.setAttribute('disabled','false');
1019         obj.controller.view.cmd_items_claimed_never_checked_out2.setAttribute('disabled','false');
1020         obj.controller.view.cmd_items_renew2.setAttribute('disabled','false');
1021         obj.controller.view.cmd_items_renew_with_date2.setAttribute('disabled','false');
1022         obj.controller.view.cmd_items_checkin2.setAttribute('disabled','false');
1023         obj.controller.view.cmd_items_edit2.setAttribute('disabled','false');
1024         obj.controller.view.cmd_items_mark_lost2.setAttribute('disabled','false');
1025         obj.controller.view.cmd_show_catalog2.setAttribute('disabled','false');
1026         obj.controller.view.sel_copy_details2.setAttribute('disabled','false');
1027         obj.controller.view.sel_bucket2.setAttribute('disabled','false');
1028         obj.controller.view.sel_patron2.setAttribute('disabled','false');
1029         obj.controller.view.cmd_triggered_events2.setAttribute('disabled','false');
1030         obj.controller.view.sel_mark_items_damaged2.setAttribute('disabled','false');
1031         obj.controller.view.sel_mark_items_missing2.setAttribute('disabled','false');
1032
1033         this.retrieve_ids2 = list;
1034     },
1035
1036     'spawn_copy_editor' : function(which) {
1037
1038         var obj = this;
1039
1040         JSAN.use('util.functional');
1041
1042         var copy_ids = util.functional.map_list( which == 1 ? obj.retrieve_ids : obj.retrieve_ids2, function(o) { return o.copy_id; } ); 
1043
1044         JSAN.use('cat.util'); cat.util.spawn_copy_editor( { 'copy_ids' : copy_ids, 'edit' : 1 } );
1045
1046     },
1047
1048
1049
1050 }
1051
1052 dump('exiting patron.items.js\n');