mitigate race condition
[Evergreen.git] / Open-ILS / xul / staff_client / server / patron / items.js
1 dump('entering patron.items.js\n');
2
3 if (typeof patron == 'undefined') patron = {};
4 patron.items = 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 patron.items.prototype = {
12
13         'init' : function( params ) {
14
15                 var obj = this;
16
17                 obj.patron_id = params['patron_id'];
18
19                 obj.init_lists();
20
21                 JSAN.use('util.controller'); obj.controller = new util.controller();
22                 obj.controller.init(
23                         {
24                                 'control_map' : {
25                                         'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
26                                         'save_columns2' : [ [ 'command' ], function() { obj.list2.save_columns(); } ],
27                                         'cmd_broken' : [ ['command'], function() { alert('Not Yet Implemented'); } ],
28                                         'sel_clip' : [ ['command'], function() { obj.list.clipboard(); } ],
29                                         'sel_clip2' : [ ['command'], function() { obj.list2.clipboard(); } ],
30                                         'sel_patron' : [ ['command'], function() { JSAN.use('circ.util'); circ.util.show_last_few_circs(obj.retrieve_ids); } ],
31                                         'sel_mark_items_damaged' : [
32                                                 ['command'],
33                                                 function() {
34                                                         JSAN.use('cat.util'); JSAN.use('util.functional');
35                                                         cat.util.mark_item_damaged( util.functional.map_list( obj.retrieve_ids, function(o) { return o.copy_id; } ) );
36                                                 }
37                                         ],
38                                         'sel_mark_items_missing' : [
39                                                 ['command'],
40                                                 function() {
41                                                         JSAN.use('cat.util'); JSAN.use('util.functional');
42                                                         cat.util.mark_item_missing( util.functional.map_list( obj.retrieve_ids, function(o) { return o.copy_id; } ) );
43                                                 }
44                                         ],
45                                         'sel_mark_items_damaged2' : [
46                                                 ['command'],
47                                                 function() {
48                                                         JSAN.use('cat.util'); JSAN.use('util.functional');
49                                                         cat.util.mark_item_damaged( util.functional.map_list( obj.retrieve_ids2, function(o) { return o.copy_id; } ) );
50                                                 }
51                                         ],
52                                         'sel_mark_items_missing2' : [
53                                                 ['command'],
54                                                 function() {
55                                                         JSAN.use('cat.util'); JSAN.use('util.functional');
56                                                         cat.util.mark_item_missing( util.functional.map_list( obj.retrieve_ids2, function(o) { return o.copy_id; } ) );
57                                                 }
58                                         ],
59                                         'sel_copy_details' : [ ['command'],
60                                                 function() {
61                                                         JSAN.use('circ.util');
62                                                         for (var i = 0; i < obj.retrieve_ids.length; i++) { circ.util.show_copy_details( obj.retrieve_ids[i].copy_id ); }
63                                                 }
64                                         ],
65                                         'sel_patron2' : [ ['command'], function() { JSAN.use('circ.util'); circ.util.show_last_few_circs(obj.retrieve_ids2); } ],
66                                         'sel_copy_details2' : [ ['command'],
67                                                 function() {
68                                                         JSAN.use('circ.util');
69                                                         for (var i = 0; i < obj.retrieve_ids2.length; i++) { circ.util.show_copy_details( obj.retrieve_ids2[i].copy_id ); }
70                                                 }
71                                         ],
72                                         'cmd_items_print' : [ ['command'], function() { obj.items_print(1); } ],
73                                         'cmd_items_print2' : [ ['command'], function() { obj.items_print(2); } ],
74                                         'cmd_items_renew' : [ ['command'], function() { obj.items_renew(1); alert('Action complete.'); obj.retrieve(); } ],
75                                         'cmd_items_renew_all' : [ ['command'], function() { obj.items_renew_all(); } ],
76                                         'cmd_items_renew2' : [ ['command'], function() { obj.items_renew(2); alert('Action complete.'); obj.retrieve(); } ],
77                                         'cmd_items_edit' : [ ['command'], function() { obj.items_edit(1); alert('Action complete.'); obj.retrieve(); } ],
78                                         'cmd_items_edit2' : [ ['command'], function() { obj.items_edit(2); alert('Action complete.'); obj.retrieve(); } ],
79                                         'cmd_items_mark_lost' : [ ['command'], function() { obj.items_mark_lost(1); alert('Action complete.'); obj.retrieve(); } ],
80                                         'cmd_items_mark_lost2' : [ ['command'], function() { obj.items_mark_lost(2); alert('Action complete.'); obj.retrieve(); } ],
81                                         'cmd_items_claimed_returned' : [ ['command'], function() { obj.items_claimed_returned(1); alert('Action complete.'); obj.retrieve(); } ],
82                                         'cmd_items_claimed_returned2' : [ ['command'], function() { obj.items_claimed_returned(2); alert('Action complete.'); obj.retrieve(); } ],
83                                         'cmd_items_checkin' : [ ['command'], function() { obj.items_checkin(1); alert('Action complete.'); obj.retrieve(); } ],
84                                         'cmd_items_checkin2' : [ ['command'], function() { obj.items_checkin(2); alert('Action complete.'); obj.retrieve(); } ],
85                                         'cmd_show_catalog' : [ ['command'], function() { obj.show_catalog(1); } ],
86                                         'cmd_show_catalog2' : [ ['command'], function() { obj.show_catalog(2); } ],
87                                         'cmd_add_billing' : [ ['command'], function() { obj.add_billing(1); alert('Action complete.'); obj.retrieve(); } ],
88                                         'cmd_add_billing2' : [ ['command'], function() { obj.add_billing(2); alert('Action complete.'); obj.retrieve(); } ],
89                                         'cmd_show_noncats' : [ ['command'], function() { obj.show_noncats(); } ],
90                                 }
91                         }
92                 );
93
94                 obj.retrieve();
95
96                 obj.controller.view.sel_mark_items_damaged.setAttribute('disabled','true');
97                 obj.controller.view.sel_mark_items_missing.setAttribute('disabled','true');
98                 obj.controller.view.sel_mark_items_damaged2.setAttribute('disabled','true');
99                 obj.controller.view.sel_mark_items_missing2.setAttribute('disabled','true');
100                 obj.controller.view.sel_clip.setAttribute('disabled','true');
101                 obj.controller.view.sel_clip2.setAttribute('disabled','true');
102                 obj.controller.view.sel_copy_details.setAttribute('disabled','true');
103                 obj.controller.view.sel_patron.setAttribute('disabled','true');
104                 obj.controller.view.sel_copy_details2.setAttribute('disabled','true');
105                 obj.controller.view.sel_patron2.setAttribute('disabled','true');
106                 obj.controller.view.cmd_items_claimed_returned.setAttribute('disabled','true');
107                 obj.controller.view.cmd_items_renew.setAttribute('disabled','true');
108                 obj.controller.view.cmd_items_checkin.setAttribute('disabled','true');
109                 obj.controller.view.cmd_items_edit.setAttribute('disabled','true');
110                 obj.controller.view.cmd_items_mark_lost.setAttribute('disabled','true');
111                 obj.controller.view.cmd_show_catalog.setAttribute('disabled','true');
112                 obj.controller.view.cmd_items_claimed_returned2.setAttribute('disabled','true');
113                 obj.controller.view.cmd_items_renew2.setAttribute('disabled','true');
114                 obj.controller.view.cmd_items_checkin2.setAttribute('disabled','true');
115                 obj.controller.view.cmd_items_edit2.setAttribute('disabled','true');
116                 obj.controller.view.cmd_items_mark_lost2.setAttribute('disabled','true');
117                 obj.controller.view.cmd_show_catalog2.setAttribute('disabled','true');
118         },
119
120         'show_noncats' : function() {
121                 var obj = this; var checkout = {};
122                 try {
123                         var robj = obj.network.simple_request('FM_ANCC_RETRIEVE_VIA_USER',[ ses(), obj.patron_id ]);
124                         if (typeof robj.ilsevent != 'undefined') throw(robj);
125
126                         for (var ii = 0; ii < robj.length; ii++) {
127                                 try {
128                                         var nc_circ = obj.network.simple_request('FM_ANCC_RETRIEVE_VIA_ID',[ ses(), robj[ii] ]);
129                                         if (typeof nc_circ.ilsevent != 'undefined') throw(nc_circ);
130                                         var fake_circ = new aoc();
131                                         fake_circ.circ_lib( nc_circ.circ_lib() );
132                                         fake_circ.circ_staff( nc_circ.staff() );
133                                         fake_circ.usr( nc_circ.patron() );
134                                         fake_circ.xact_start( nc_circ.circ_time() );
135                                         fake_circ.renewal_remaining(0);
136                                         fake_circ.stop_fines('Non-Cataloged');
137                                                 
138                                         JSAN.use('util.date');
139                                         var c = nc_circ.circ_time();
140                                         var d = c == "now" ? new Date() : util.date.db_date2Date( c );
141                                         var t = obj.data.hash.cnct[ nc_circ.item_type() ];
142                                         if (!t) {
143                                                 var robj2 = obj.network.simple_request('FM_CNCT_RETRIEVE',[ nc_circ.circ_lib() ]);
144                                                 if (typeof robj2.ilsevent != 'undefined') throw(robj);
145                                                 obj.data.stash_retrieve();
146                                                 for (var j = 0; j < robj2.length; j++) {
147                                                         if (! obj.data.hash.cnct[ robj2[j].id() ] ) {
148                                                                 obj.data.hash.cnct[ robj2[j].id() ] = robj2[j];
149                                                                 obj.data.list.cnct.push( robj2[j] );
150                                                         }
151                                                 }
152                                                 obj.data.stash('hash','list');
153                                                 t = obj.data.hash.cnct[ nc_circ.item_type() ];
154                                         }
155                                         var cd = t.circ_duration() || "14 days";
156                                         var i = util.date.interval_to_seconds( cd ) * 1000;
157                                         d.setTime( Date.parse(d) + i );
158                                         fake_circ.due_date( util.date.formatted_date(d,'%F') );
159         
160                                         var fake_record = new mvr();
161                                         fake_record.title( obj.data.hash.cnct[ nc_circ.item_type() ].name());
162         
163                                         var fake_copy = new acp();
164                                         fake_copy.barcode( '' );
165                                         fake_copy.circ_lib( nc_circ.circ_lib() );
166
167                                         obj.list.append( { 'row' : { 'my' : { 'circ' : fake_circ, 'mvr' : fake_record, 'acp' : fake_copy } }, } );
168
169                                 } catch(F) {
170                                         obj.error.standard_unexpected_error_alert('Error showing NonCat #' + robj[ii].id(),F);
171                                 }
172                         }
173
174                 } catch(E) {
175                         obj.error.standard_unexpected_error_alert('Error showing NonCat circulations',E);
176                 }
177         },
178
179         'items_print' : function(which) {
180                 var obj = this;
181                 try {
182                         var list = (which==2 ? obj.list2 : obj.list);
183                         dump(js2JSON( list.dump_with_keys() ) + '\n');
184                         function flesh_callback() {
185                                 try {
186                                         JSAN.use('patron.util');
187                                         var params = { 
188                                                 'patron' : patron.util.retrieve_au_via_id(ses(),obj.patron_id), 
189                                                 'lib' : obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ],
190                                                 'staff' : obj.data.list.au[0],
191                                                 'header' : obj.data.print_list_templates.checkout.header,
192                                                 'line_item' : obj.data.print_list_templates.checkout.line_item,
193                                                 'footer' : obj.data.print_list_templates.checkout.footer,
194                                                 'type' : obj.data.print_list_templates.checkout.type,
195                                                 'list' : list.dump_with_keys(),
196                                         };
197                                         JSAN.use('util.print'); var print = new util.print();
198                                         print.tree_list( params );
199                                         setTimeout(function(){list.on_all_fleshed = null;},0);
200                                 } catch(E) {
201                                         obj.error.standard_unexpected_error_alert('printing 2',E);
202                                 }
203                         }
204                         list.on_all_fleshed = flesh_callback;
205                         list.full_retrieve();
206                 } catch(E) {
207                         obj.error.standard_unexpected_error_alert('printing 1',E);
208                 }
209         },
210
211         'items_renew_all' : function() {
212                 try {
213                         var obj = this; var list = obj.list;
214                         if (list.on_all_fleshed != null) {
215                                 var r = window.confirm('This is list is busy retrieving/rendering rows for a prior action.  Abort the prior action and proceed?');
216                                 if (!r) return;
217                         }
218                         var r = window.confirm('Renew all the items in this list?');
219                         if (!r) return;
220                         function flesh_callback() {
221                                 try {
222                                         obj.list.select_all();
223                                         obj.items_renew(1,true);        
224                                         setTimeout(function(){list.on_all_fleshed = null; alert('Action complete.'); obj.retrieve(); },0);
225                                 } catch(E) {
226                                         obj.error.standard_unexpected_error_alert('2 All items were not likely renewed',E);
227                                 }
228                         }
229                         list.on_all_fleshed = flesh_callback;
230                         list.full_retrieve();
231                 } catch(E) {
232                         this.error.standard_unexpected_error_alert('All items were not likely renewed',E);
233                 }
234         },
235
236         'items_renew' : function(which,skip_prompt) {
237                 var obj = this;
238                 try{
239                         JSAN.use('circ.util');
240                         var retrieve_ids = ( which == 2 ? obj.retrieve_ids2 : obj.retrieve_ids );
241                         if (!retrieve_ids || retrieve_ids.length == 0) return;
242                         JSAN.use('util.functional');
243                         if (!skip_prompt) {
244                                 var msg = 'Are you sure you would like to renew item' + ( retrieve_ids.length > 1 ? 's ' : ' ') + util.functional.map_list( retrieve_ids, function(o){return o.barcode;}).join(', ') + '?';
245                                 var r = window.confirm(msg);
246                                 if (!r) { return; }
247                         }
248                         for (var i = 0; i < retrieve_ids.length; i++) {
249                                 try {
250                                         var barcode = retrieve_ids[i].barcode;
251                                         //alert('Renew barcode = ' + barcode);
252                                         var renew = circ.util.renew_via_barcode( barcode, obj.patron_id );
253
254                                 } catch(E) {
255                                         obj.error.standard_unexpected_error_alert('Renew probably did not happen for barcode ' + barcode,E);
256                                 }
257                         }
258                 } catch(E) {
259                         obj.error.standard_unexpected_error_alert('Renew probably did not happen.',E);
260                 }
261         },
262
263         'items_edit' : function(which) {
264                         var obj = this;
265                         try {
266                                 var retrieve_ids = ( which == 2 ? obj.retrieve_ids2 : obj.retrieve_ids );
267                                 if (!retrieve_ids || retrieve_ids.length == 0) return;
268                                 function check_date(value) {
269                                         JSAN.use('util.date');
270                                         try {
271                                                 if (! util.date.check('YYYY-MM-DD',value) ) { 
272                                                         throw('Invalid Date'); 
273                                                 }
274                                                 if (util.date.check_past('YYYY-MM-DD',value) ) { 
275                                                         throw('Due date needs to be after today.'); 
276                                                 }
277                                                 /*
278                                                 if ( util.date.formatted_date(new Date(),'%F') == value) { 
279                                                         throw('Due date needs to be after today.'); 
280                                                 }
281                                                 */
282                                                 return true;
283                                         } catch(E) {
284                                                 alert(E);
285                                                 return false;
286                                         }
287                                 }
288
289                                 JSAN.use('util.functional');
290                                 var title = 'Edit Due Date' + (retrieve_ids.length > 1 ? 's' : '');
291                                 var value = 'YYYY-MM-DD';
292                                 var text = 'Enter a new due date for these items: ' + 
293                                         util.functional.map_list(retrieve_ids,function(o){return o.barcode;}).join(', ');
294                                 var due_date; var invalid = true;
295                                 while(invalid) {
296                                         due_date = window.prompt(text,value,title);
297                                         if (due_date) {
298                                                 invalid = ! check_date(due_date);
299                                         } else {
300                                                 invalid = false;
301                                         }
302                                 }
303                                 if (due_date) {
304                                         var circs = util.functional.map_list(retrieve_ids,function(o){return o.circ_id;});
305                                         for (var i = 0; i < circs.length; i++) {
306                                                 var robj = obj.network.simple_request('FM_CIRC_EDIT_DUE_DATE',[ses(),circs[i],due_date]);
307                                                 if (typeof robj.ilsevent != 'undefined') { if (robj.ilsevent != 0) throw(robj); }
308                                         }
309                                 }
310                         } catch(E) {
311                                 obj.error.standard_unexpected_error_alert('The due dates were not likely modified.',E);
312                         }
313         },
314
315         'items_mark_lost' : function(which) {
316                 var obj = this;
317                 try {
318                         var retrieve_ids = ( which == 2 ? obj.retrieve_ids2 : obj.retrieve_ids );
319                         if (!retrieve_ids || retrieve_ids.length == 0) return;
320                         for (var i = 0; i < retrieve_ids.length; i++) {
321                                 var barcode = retrieve_ids[i].barcode;
322                                 dump('Mark barcode lost = ' + barcode);
323                                 var robj = obj.network.simple_request( 'MARK_ITEM_LOST', [ ses(), { barcode: barcode } ]);
324                                 if (typeof robj.ilsevent != 'undefined') { if (robj.ilsevent != 0) throw(robj); }
325                         }
326                 } catch(E) {
327                         obj.error.standard_unexpected_error_alert('The items were not likely marked lost.',E);
328                 }
329         },
330
331         'items_claimed_returned' : function(which) {
332                 var obj = this;
333                 try {
334                         var retrieve_ids = ( which == 2 ? obj.retrieve_ids2 : obj.retrieve_ids );
335                         if (!retrieve_ids || retrieve_ids.length == 0) return;
336                         function check_date(value) {
337                                 JSAN.use('util.date');
338                                 try {
339                                         if (! util.date.check('YYYY-MM-DD',value) ) { 
340                                                 throw('Invalid Date'); 
341                                         }
342                                         if ( util.date.formatted_date(new Date(),'%F') == value) { 
343                                                 return true;
344                                         }
345                                         if (! util.date.check_past('YYYY-MM-DD',value) ) { 
346                                                 throw('Claims Returned Date cannot be in the future.'); 
347                                         }
348                                         return true;
349                                 } catch(E) {
350                                         alert(E);
351                                         return false;
352                                 }
353                         }
354
355                         JSAN.use('util.functional');
356                         var title = 'Claimed Returned';
357                         var value = 'YYYY-MM-DD';
358                         var text = 'Enter a claimed returned date for these items: ' + 
359                                 util.functional.map_list(retrieve_ids,function(o){return o.barcode;}).join(', ');
360                         var backdate; var invalid = true;
361                         while(invalid) {
362                                 backdate = window.prompt(text,value,title);
363                                 if (backdate) {
364                                         invalid = ! check_date(backdate);
365                                 } else {
366                                         invalid = false;
367                                 }
368                         }
369                         //alert('backdate = ' + backdate);
370                         if (backdate) {
371                                 var barcodes = util.functional.map_list(retrieve_ids,function(o){return o.barcode;});
372                                 for (var i = 0; i < barcodes.length; i++) {
373                                         var robj = obj.network.simple_request(
374                                                 'MARK_ITEM_CLAIM_RETURNED', 
375                                                 [ ses(), { barcode: barcodes[i], backdate: backdate } ]
376                                         );
377                                         if (typeof robj.ilsevent != 'undefined') { if (robj.ilsevent != 0) throw(robj); }
378                                 }
379                         }
380                 } catch(E) {
381                         obj.error.standard_unexpected_error_alert('The items were not likely marked Claimed Returned.',E);
382                 }
383         },
384
385         'items_checkin' : function(which) {
386                 var obj = this;
387                 try {
388                         var retrieve_ids = ( which == 2 ? obj.retrieve_ids2 : obj.retrieve_ids );
389                         if (!retrieve_ids || retrieve_ids.length == 0) return;
390                         JSAN.use('util.functional');
391                         var msg = 'Are you sure you would like to check in item' + ( retrieve_ids.length > 1 ? 's ' : ' ') + util.functional.map_list( retrieve_ids, function(o){return o.barcode;}).join(', ') + '?';
392                         var r = window.confirm(msg);
393                         if (!r) { return; }
394                         JSAN.use('circ.util');
395                         for (var i = 0; i < retrieve_ids.length; i++) {
396                                 var barcode = retrieve_ids[i].barcode;
397                                 dump('Check in barcode = ' + barcode);
398                                 var robj = circ.util.checkin_via_barcode(
399                                         ses(), barcode
400                                 );
401                                 /* circ.util.checkin_via_barcode handles errors currently */
402                         }
403                 } catch(E) {
404                         obj.error.standard_unexpected_error_alert('Checkin probably did not happen.',E);
405                 }
406         },
407
408         'show_catalog' : function(which) {
409                 var obj = this;
410                 try {
411                         var retrieve_ids = ( which == 2 ? obj.retrieve_ids2 : obj.retrieve_ids );
412                         if (!retrieve_ids || retrieve_ids.length == 0) return;
413                         for (var i = 0; i < retrieve_ids.length; i++) {
414                                 var doc_id = retrieve_ids[i].doc_id;
415                                 if (!doc_id) {
416                                         alert(retrieve_ids[i].barcode + ' is not cataloged');
417                                         continue;
418                                 }
419                                 var opac_url = xulG.url_prefix( urls.opac_rdetail ) + '?r=' + doc_id;
420                                 var content_params = { 
421                                         'session' : ses(),
422                                         'authtime' : ses('authtime'),
423                                         'opac_url' : opac_url,
424                                 };
425                                 xulG.new_tab(
426                                         xulG.url_prefix(urls.XUL_OPAC_WRAPPER), 
427                                         {'tab_name':'Retrieving title...'}, 
428                                         content_params
429                                 );
430                         }
431                 } catch(E) {
432                         obj.error.standard_unexpected_error_alert('',E);
433                 }
434         },
435
436         'add_billing' : function(which) {
437                 var obj = this;
438                 try {
439                         var retrieve_ids = ( which == 2 ? obj.retrieve_ids2 : obj.retrieve_ids );
440                         if (!retrieve_ids || retrieve_ids.length == 0) return;
441                         JSAN.use('util.window');
442                         var win = new util.window();
443                         for (var i = 0; i < retrieve_ids.length; i++) {
444                                 var circ_id = retrieve_ids[i].circ_id;
445                                 var w = win.open(
446                                         urls.XUL_PATRON_BILL_WIZARD
447                                                 + '?patron_id=' + window.escape(obj.patron_id)
448                                                 + '&xact_id=' + window.escape( circ_id ),
449                                         'billwizard',
450                                         'chrome,resizable,modal'
451                                 );
452                         }
453                 } catch(E) {
454                         obj.error.standard_unexpected_error_alert('',E);
455                 }
456         },
457
458         'init_lists' : function() {
459                 var obj = this;
460
461                 JSAN.use('circ.util');
462                 var columns = circ.util.columns( 
463                         { 
464                                 'barcode' : { 'hidden' : false },
465                                 'circ_lib' : { 'hidden' : false },
466                                 'title' : { 'hidden' : false, 'flex' : '3' },
467                                 'due_date' : { 'hidden' : false },
468                                 'renewal_remaining' : { 'hidden' : false },
469                                 'stop_fines' : { 'hidden' : false },
470                         } 
471                 );
472                 var columns2 = circ.util.columns( 
473                         { 
474                                 'barcode' : { 'hidden' : false },
475                                 'circ_lib' : { 'hidden' : false },
476                                 'title' : { 'hidden' : false, 'flex' : '3' },
477                                 'checkin_time' : { 'hidden' : false },
478                                 'stop_fines' : { 'hidden' : false },
479                         } 
480                 );
481
482                 function retrieve_row(params) {
483                         var row = params.row;
484
485                         if (!row.my.circ_id) {
486                                 if (typeof params.on_retrieve == 'function') { params.on_retrieve(row); }
487                                 return row;
488                         }
489
490                         var funcs = [];
491
492                         funcs.push(
493                                 function() {
494                                         row.my.circ = obj.network.simple_request('FM_CIRC_RETRIEVE_VIA_ID',[ ses(), row.my.circ_id ]);
495                                 }
496                         );
497                         
498                         if (!row.my.mvr) funcs.push(
499                                 function() {
500
501                                         row.my.mvr = obj.network.request(
502                                                 api.MODS_SLIM_RECORD_RETRIEVE_VIA_COPY.app,
503                                                 api.MODS_SLIM_RECORD_RETRIEVE_VIA_COPY.method,
504                                                 [ row.my.circ.target_copy() ]
505                                         );
506
507                                 }
508                         );
509                         if (!row.my.acp) {
510                                 funcs.push(     
511                                         function() {
512
513                                                 row.my.acp = obj.network.request(
514                                                         api.FM_ACP_RETRIEVE.app,
515                                                         api.FM_ACP_RETRIEVE.method,
516                                                         [ row.my.circ.target_copy() ]
517                                                 );
518
519                                                 params.row_node.setAttribute( 'retrieve_id', js2JSON({'copy_id':row.my.circ.target_copy(),'circ_id':row.my.circ.id(),'barcode':row.my.acp.barcode(),'doc_id': (typeof row.my.mvr.ilsevent == 'undefined' ? row.my.mvr.doc_id() : null )}) );
520
521                                         }
522                                 );
523                         } else {
524                                 params.row_node.setAttribute( 'retrieve_id', js2JSON({'copy_id':row.my.circ.target_copy(),'circ_id':row.my.circ.id(),'barcode':row.my.acp.barcode(),'doc_id': ( typeof row.my.mvr.ilsevent == 'undefined' ? row.my.mvr.doc_id() : null) }) );
525                         }
526
527                         funcs.push(
528                                 function() {
529
530                                         if (typeof params.on_retrieve == 'function') {
531                                                 params.on_retrieve(row);
532                                         }
533
534                                 }
535                         );
536
537                         JSAN.use('util.exec'); var exec = new util.exec();
538                         exec.on_error = function(E) {
539                                 //var err = 'items chain: ' + js2JSON(E);
540                                 //obj.error.sdump('D_ERROR',err);
541                                 return true; /* keep going */
542                         }
543                         exec.chain( funcs );
544
545                         return row;
546
547                 }
548
549                 JSAN.use('util.list'); obj.list = new util.list('items_list');
550                 obj.list.init(
551                         {
552                                 'columns' : columns,
553                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
554                                 'retrieve_row' : retrieve_row,
555                                 'on_select' : function(ev) {
556                                         JSAN.use('util.functional');
557                                         var sel = obj.list.retrieve_selection();
558                                         obj.controller.view.sel_clip.setAttribute('disabled',sel.length < 1);
559                                         var list = util.functional.map_list(
560                                                 sel,
561                                                 function(o) { return JSON2js( o.getAttribute('retrieve_id') ); }
562                                         );
563                                         if (typeof obj.on_select == 'function') {
564                                                 obj.on_select(list);
565                                         }
566                                         if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
567                                                 obj.error.sdump('D_PATRON','patron.items: Calling external .on_select()\n');
568                                                 window.xulG.on_select(list);
569                                         } else {
570                                                 obj.error.sdump('D_PATRON','patron.items: No external .on_select()\n');
571                                         }
572                                 },
573                         }
574                 );
575                 
576                 obj.list2 = new util.list('items_list2');
577                 obj.list2.init(
578                         {
579                                 'columns' : columns2,
580                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
581                                 'retrieve_row' : retrieve_row,
582                                 'on_select' : function(ev) {
583                                         JSAN.use('util.functional');
584                                         var sel = obj.list2.retrieve_selection();
585                                         obj.controller.view.sel_clip2.setAttribute('disabled',sel.length < 1);
586                                         var list = util.functional.map_list(
587                                                 sel,
588                                                 function(o) { return JSON2js( o.getAttribute('retrieve_id') ); }
589                                         );
590                                         if (typeof obj.on_select2 == 'function') {
591                                                 obj.on_select2(list);
592                                         }
593                                 },
594                         }
595                 );
596         },
597
598         'retrieve' : function(dont_show_me_the_list_change) {
599                 var obj = this;
600                 if (window.xulG && window.xulG.checkouts) {
601                         obj.checkouts = window.xulG.checkouts;
602                 } else {
603                         obj.checkouts = [];
604                         obj.checkouts2 = [];
605                         var robj = obj.network.simple_request(
606                                 'FM_CIRC_RETRIEVE_VIA_USER',
607                                 [ ses(), obj.patron_id ]
608                         );
609                         if (typeof robj.ilsevent!='undefined') {
610                                 obj.error.standard_unexpected_error_alert('Error retrieving circulations.',E);
611                         } else {
612                                 obj.checkouts = obj.checkouts.concat( robj.overdue );
613                                 obj.checkouts = obj.checkouts.concat( robj.out );
614                                 obj.checkouts2 = obj.checkouts2.concat( robj.lost );
615                                 obj.checkouts2 = obj.checkouts2.concat( robj.claims_returned );
616                                 obj.checkouts2 = obj.checkouts2.concat( robj.long_overdue );
617                         }
618                         var robj = obj.network.simple_request(
619                                 'FM_CIRC_IN_WITH_FINES_VIA_USER',
620                                 [ ses(), obj.patron_id ]
621                         );
622                         if (typeof robj.ilsevent!='undefined') {
623                                 obj.error.standard_unexpected_error_alert('Error retrieving circulations.',E);
624                         } else {
625                                 obj.checkouts2 = obj.checkouts2.concat( robj.lost );
626                                 obj.checkouts2 = obj.checkouts2.concat( robj.claims_returned );
627                                 obj.checkouts2 = obj.checkouts2.concat( robj.long_overdue );
628                         }
629                 }
630
631                 function gen_list_append(circ_id,which_list) {
632                         return function() {
633                                 try {
634                                         switch(which_list) {
635                                                 case 1:
636                                                         obj.list2.append( { 'row' : { 'my' : { 'circ_id' : circ_id } }, } );
637                                                 break;
638                                                 default:
639                                                         obj.list.append( { 'row' : { 'my' : { 'circ_id' : circ_id } }, } );
640                                                 break;
641                                         }
642                                 } catch(E) {
643                                         alert(E);
644                                 }
645                         };
646                 }
647
648                 obj.list.clear(); obj.list2.clear();
649
650                 JSAN.use('util.exec'); var exec = new util.exec();
651                 var rows = [];
652                 for (var i in obj.checkouts) {
653                         rows.push( gen_list_append(obj.checkouts[i],0) );
654                 }
655                 for (var i in obj.checkouts2) {
656                         rows.push( gen_list_append(obj.checkouts2[i],1) );
657                 }
658                 exec.chain( rows );
659                 if (!dont_show_me_the_list_change) {
660                         if (window.xulG && typeof window.xulG.on_list_change == 'function') {
661                                 try { window.xulG.on_list_change(obj.checkouts); } catch(E) { this.error.sdump('D_ERROR',E); }
662                         }
663                 }
664         },
665
666         'on_select' : function(list) {
667
668                 dump('patron.items.on_select list = ' + js2JSON(list) + '\n');
669
670                 var obj = this;
671
672                 obj.controller.view.cmd_items_claimed_returned.setAttribute('disabled','false');
673                 obj.controller.view.cmd_items_renew.setAttribute('disabled','false');
674                 obj.controller.view.cmd_items_checkin.setAttribute('disabled','false');
675                 obj.controller.view.cmd_items_edit.setAttribute('disabled','false');
676                 obj.controller.view.cmd_items_mark_lost.setAttribute('disabled','false');
677                 obj.controller.view.cmd_show_catalog.setAttribute('disabled','false');
678                 obj.controller.view.sel_copy_details.setAttribute('disabled','false');
679                 obj.controller.view.sel_patron.setAttribute('disabled','false');
680                 obj.controller.view.sel_mark_items_damaged.setAttribute('disabled','false');
681                 obj.controller.view.sel_mark_items_missing.setAttribute('disabled','false');
682
683                 obj.retrieve_ids = list;
684         },
685
686         'on_select2' : function(list) {
687         
688                 dump('patron.items.on_select2 list = ' + js2JSON(list) + '\n');
689
690                 var obj = this;
691
692                 obj.controller.view.cmd_items_claimed_returned2.setAttribute('disabled','false');
693                 obj.controller.view.cmd_items_renew2.setAttribute('disabled','false');
694                 obj.controller.view.cmd_items_checkin2.setAttribute('disabled','false');
695                 obj.controller.view.cmd_items_edit2.setAttribute('disabled','false');
696                 obj.controller.view.cmd_items_mark_lost2.setAttribute('disabled','false');
697                 obj.controller.view.cmd_show_catalog2.setAttribute('disabled','false');
698                 obj.controller.view.sel_copy_details2.setAttribute('disabled','false');
699                 obj.controller.view.sel_patron2.setAttribute('disabled','false');
700                 obj.controller.view.sel_mark_items_damaged2.setAttribute('disabled','false');
701                 obj.controller.view.sel_mark_items_missing2.setAttribute('disabled','false');
702
703                 this.retrieve_ids2 = list;
704         },
705
706 }
707
708 dump('exiting patron.items.js\n');