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