]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/circ/copy_status.js
refresh in place function for tree rows, adjustment to code that was expecting nodes...
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / circ / copy_status.js
1 dump('entering circ.copy_status.js\n');
2
3 if (typeof circ == 'undefined') circ = {};
4 circ.copy_status = 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('util.barcode');
9         JSAN.use('util.date');
10         JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init({'via':'stash'});
11         JSAN.use('util.sound'); this.sound = new util.sound();
12 }
13
14 circ.copy_status.prototype = {
15         'selection_list' : [],
16         'list_barcode_map' : {},
17
18         'init' : function( params ) {
19
20                 var obj = this;
21
22                 JSAN.use('circ.util');
23                 var columns = circ.util.columns( 
24                         { 
25                                 'barcode' : { 'hidden' : false },
26                                 'title' : { 'hidden' : false },
27                                 'location' : { 'hidden' : false },
28                                 'call_number' : { 'hidden' : false },
29                                 'status' : { 'hidden' : false },
30                                 'alert_message' : { 'hidden' : false },
31                                 'due_date' : { 'hidden' : false },
32                         },
33                         {
34                                 'except_these' : [
35                                         'checkin_time', 'checkin_time_full', 'route_to', 'message', 'uses', 'xact_finish',
36                                 ],
37                         }
38                 );
39
40                 JSAN.use('util.list'); obj.list = new util.list('copy_status_list');
41                 obj.list.init(
42                         {
43                                 'columns' : columns,
44                                 'map_row_to_columns' : circ.util.std_map_row_to_columns(),
45                                 'on_select' : function(ev) {
46                                         try {
47                                                 JSAN.use('util.functional');
48                                                 var sel = obj.list.retrieve_selection();
49                                                 obj.selection_list = util.functional.map_list(
50                                                         sel,
51                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
52                                                 );
53                                                 obj.error.sdump('D_TRACE','circ/copy_status: selection list = ' + js2JSON(obj.selection_list) );
54                                                 if (obj.selection_list.length == 0) {
55                                                         obj.controller.view.sel_checkin.setAttribute('disabled','true');
56                                                         obj.controller.view.cmd_replace_barcode.setAttribute('disabled','true');
57                                                         obj.controller.view.sel_edit.setAttribute('disabled','true');
58                                                         obj.controller.view.sel_opac.setAttribute('disabled','true');
59                                                         obj.controller.view.sel_bucket.setAttribute('disabled','true');
60                                                         obj.controller.view.sel_copy_details.setAttribute('disabled','true');
61                                                         obj.controller.view.sel_mark_items_damaged.setAttribute('disabled','true');
62                                                         obj.controller.view.sel_mark_items_missing.setAttribute('disabled','true');
63                                                         obj.controller.view.sel_patron.setAttribute('disabled','true');
64                                                         obj.controller.view.sel_spine.setAttribute('disabled','true');
65                                                         obj.controller.view.sel_transit_abort.setAttribute('disabled','true');
66                                                         obj.controller.view.sel_clip.setAttribute('disabled','true');
67                                                         obj.controller.view.sel_renew.setAttribute('disabled','true');
68                                                         obj.controller.view.cmd_add_items.setAttribute('disabled','true');
69                                                         obj.controller.view.cmd_delete_items.setAttribute('disabled','true');
70                                                         obj.controller.view.cmd_transfer_items.setAttribute('disabled','true');
71                                                         obj.controller.view.cmd_add_volumes.setAttribute('disabled','true');
72                                                         obj.controller.view.cmd_edit_volumes.setAttribute('disabled','true');
73                                                         obj.controller.view.cmd_delete_volumes.setAttribute('disabled','true');
74                                                         obj.controller.view.cmd_mark_volume.setAttribute('disabled','true');
75                                                         obj.controller.view.cmd_mark_library.setAttribute('disabled','true');
76                                                         obj.controller.view.cmd_transfer_volume.setAttribute('disabled','true');
77                                                 } else {
78                                                         obj.controller.view.sel_checkin.setAttribute('disabled','false');
79                                                         obj.controller.view.cmd_replace_barcode.setAttribute('disabled','false');
80                                                         obj.controller.view.sel_edit.setAttribute('disabled','false');
81                                                         obj.controller.view.sel_opac.setAttribute('disabled','false');
82                                                         obj.controller.view.sel_patron.setAttribute('disabled','false');
83                                                         obj.controller.view.sel_bucket.setAttribute('disabled','false');
84                                                         obj.controller.view.sel_copy_details.setAttribute('disabled','false');
85                                                         obj.controller.view.sel_mark_items_damaged.setAttribute('disabled','false');
86                                                         obj.controller.view.sel_mark_items_missing.setAttribute('disabled','false');
87                                                         obj.controller.view.sel_spine.setAttribute('disabled','false');
88                                                         obj.controller.view.sel_transit_abort.setAttribute('disabled','false');
89                                                         obj.controller.view.sel_clip.setAttribute('disabled','false');
90                                                         obj.controller.view.sel_renew.setAttribute('disabled','false');
91                                                         obj.controller.view.cmd_add_items.setAttribute('disabled','false');
92                                                         obj.controller.view.cmd_delete_items.setAttribute('disabled','false');
93                                                         obj.controller.view.cmd_transfer_items.setAttribute('disabled','false');
94                                                         obj.controller.view.cmd_add_volumes.setAttribute('disabled','false');
95                                                         obj.controller.view.cmd_edit_volumes.setAttribute('disabled','false');
96                                                         obj.controller.view.cmd_delete_volumes.setAttribute('disabled','false');
97                                                         obj.controller.view.cmd_mark_volume.setAttribute('disabled','false');
98                                                         obj.controller.view.cmd_mark_library.setAttribute('disabled','false');
99                                                         obj.controller.view.cmd_transfer_volume.setAttribute('disabled','false');
100                                                 }
101                                         } catch(E) {
102                                                 alert('FIXME: ' + E);
103                                         }
104                                 },
105                         }
106                 );
107                 
108                 JSAN.use('util.controller'); obj.controller = new util.controller();
109                 obj.controller.init(
110                         {
111                                 'control_map' : {
112                                         'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
113                                         'sel_clip' : [
114                                                 ['command'],
115                                                 function() { obj.list.clipboard(); }
116                                         ],
117                                         'sel_checkin' : [
118                                                 ['command'],
119                                                 function() {
120                                                         try {
121                                                                 var funcs = [];
122                                                                 JSAN.use('circ.util');
123                                                                 for (var i = 0; i < obj.selection_list.length; i++) {
124                                                                         var barcode = obj.selection_list[i].barcode;
125                                                                         var checkin = circ.util.checkin_via_barcode( ses(), { 'barcode' : barcode } );
126                                                                         funcs.push( function(a) { return function() { obj.copy_status( a, true ); }; }(barcode) );
127                                                                 }
128                                                                 for (var i = 0; i < funcs.length; i++) { funcs[i](); }
129                                                                 alert('Action complete.');
130                                                         } catch(E) {
131                                                                 obj.error.standard_unexpected_error_alert('Checkin did not likely happen.',E);
132                                                         }
133                                                 }
134                                         ],
135                                         'cmd_replace_barcode' : [
136                                                 ['command'],
137                                                 function() {
138                                                         try {
139                                                                 var funcs = [];
140                                                                 JSAN.use('cat.util');
141                                                                 for (var i = 0; i < obj.selection_list.length; i++) {
142                                                                         try { 
143                                                                                 var barcode = obj.selection_list[i].barcode;
144                                                                                 var new_bc = cat.util.replace_barcode( barcode );
145                                                                                 funcs.push( function(a) { return function() { obj.copy_status( a, true ); }; }(new_bc) );
146                                                                         } catch(E) {
147                                                                                 obj.error.standard_unexpected_error_alert('Barcode ' + barcode + ' was not likely replaced.',E);
148                                                                         }
149                                                                 }
150                                                                 for (var i = 0; i < funcs.length; i++) { funcs[i](); }
151                                                                 alert('Action complete.');
152                                                         } catch(E) {
153                                                                 obj.error.standard_unexpected_error_alert('Barcode replacements did not likely happen.',E);
154                                                         }
155                                                 }
156                                         ],
157                                         'sel_edit' : [
158                                                 ['command'],
159                                                 function() {
160                                                         try {
161                                                                 var funcs = [];
162                                                                 obj.spawn_copy_editor();
163                                                                 for (var i = 0; i < obj.selection_list.length; i++) {
164                                                                                 var barcode = obj.selection_list[i].barcode;
165                                                                                 funcs.push( function(a) { return function() { obj.copy_status( a, true ); }; }(barcode) );
166                                                                 }
167                                                                 for (var i = 0; i < funcs.length; i++) { funcs[i](); }
168                                                         } catch(E) {
169                                                                 obj.error.standard_unexpected_error_alert('with copy editor',E);
170                                                         }
171                                                 }
172                                         ],
173                                         'sel_spine' : [
174                                                 ['command'],
175                                                 function() {
176                                                         JSAN.use('cat.util');
177                                                         cat.util.spawn_spine_editor(obj.selection_list);
178                                                 }
179                                         ],
180                                         'sel_opac' : [
181                                                 ['command'],
182                                                 function() {
183                                                         JSAN.use('cat.util');
184                                                         cat.util.show_in_opac(obj.selection_list);
185                                                 }
186                                         ],
187                                         'sel_transit_abort' : [
188                                                 ['command'],
189                                                 function() {
190                                                         var funcs = [];
191                                                         JSAN.use('circ.util');
192                                                         circ.util.abort_transits(obj.selection_list);
193                                                         for (var i = 0; i < obj.selection_list.length; i++) {
194                                                                 var barcode = obj.selection_list[i].barcode;
195                                                                 funcs.push( function(a) { return function() { obj.copy_status( a, true ); }; }(barcode) );
196                                                         }
197                                                         for (var i = 0; i < funcs.length; i++) { funcs[i](); }
198                                                         alert('Action complete.');
199                                                 }
200                                         ],
201                                         'sel_patron' : [
202                                                 ['command'],
203                                                 function() {
204                                                         JSAN.use('circ.util');
205                                                         circ.util.show_last_few_circs(obj.selection_list);
206                                                 }
207                                         ],
208                                         'sel_copy_details' : [
209                                                 ['command'],
210                                                 function() {
211                                                         JSAN.use('circ.util');
212                                                         for (var i = 0; i < obj.selection_list.length; i++) {
213                                                                 circ.util.show_copy_details( obj.selection_list[i].copy_id );
214                                                         }
215                                                 }
216                                         ],
217                                         'sel_renew' : [
218                                                 ['command'],
219                                                 function() {
220                                                         var funcs = [];
221                                                         JSAN.use('circ.util');
222                                                         for (var i = 0; i < obj.selection_list.length; i++) {
223                                                                 var test = obj.selection_list[i].renewable;
224                                                                 var barcode = obj.selection_list[i].barcode;
225                                                                 if (test == 't') {
226                                                                         circ.util.renew_via_barcode( barcode );
227                                                                         funcs.push( function(a) { return function() { obj.copy_status( a, true ); }; }(barcode) );
228                                                                 } else {
229                                                                         alert('Item with barcode ' + barcode + ' is not circulating.');
230                                                                 }
231                                                         }
232                                                         for (var i = 0; i < funcs.length; i++) { funcs[i](); }
233                                                         alert('Action complete.');
234                                                 }
235                                         ],
236
237                                         'sel_mark_items_damaged' : [
238                                                 ['command'],
239                                                 function() {
240                                                         var funcs = [];
241                                                         JSAN.use('cat.util'); JSAN.use('util.functional');
242                                                         cat.util.mark_item_damaged( util.functional.map_list( obj.selection_list, function(o) { return o.copy_id; } ) );
243                                                         for (var i = 0; i < obj.selection_list.length; i++) {
244                                                                 var barcode = obj.selection_list[i].barcode;
245                                                                 funcs.push( function(a) { return function() { obj.copy_status( a, true ); }; }(barcode) );
246                                                         }
247                                                         for (var i = 0; i < funcs.length; i++) { funcs[i](); }
248                                                 }
249                                         ],
250                                         'sel_mark_items_missing' : [
251                                                 ['command'],
252                                                 function() {
253                                                         var funcs = [];
254                                                         JSAN.use('cat.util'); JSAN.use('util.functional');
255                                                         cat.util.mark_item_missing( util.functional.map_list( obj.selection_list, function(o) { return o.copy_id; } ) );
256                                                         for (var i = 0; i < obj.selection_list.length; i++) {
257                                                                 var barcode = obj.selection_list[i].barcode;
258                                                                 funcs.push( function(a) { return function() { obj.copy_status( a, true ); }; }(barcode) );
259                                                         }
260                                                         for (var i = 0; i < funcs.length; i++) { funcs[i](); }
261                                                 }
262                                         ],
263                                         'sel_bucket' : [
264                                                 ['command'],
265                                                 function() {
266                                                         JSAN.use('cat.util');
267                                                         cat.util.add_copies_to_bucket(obj.selection_list);
268                                                 }
269                                         ],
270                                         'copy_status_barcode_entry_textbox' : [
271                                                 ['keypress'],
272                                                 function(ev) {
273                                                         if (ev.keyCode && ev.keyCode == 13) {
274                                                                 obj.copy_status();
275                                                         }
276                                                 }
277                                         ],
278                                         'cmd_broken' : [
279                                                 ['command'],
280                                                 function() { alert('Not Yet Implemented'); }
281                                         ],
282                                         'cmd_copy_status_submit_barcode' : [
283                                                 ['command'],
284                                                 function() {
285                                                         obj.copy_status();
286                                                 }
287                                         ],
288                                         'cmd_copy_status_upload_file' : [
289                                                 ['command'],
290                                                 function() {
291                                                         function pick_file(mode) {
292                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
293                                                                 var nsIFilePicker = Components.interfaces.nsIFilePicker;
294                                                                 var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance( nsIFilePicker );
295                                                                 fp.init( 
296                                                                         window, 
297                                                                         mode == 'open' ? "Import Barcode File" : "Save Barcode File As", 
298                                                                         mode == 'open' ? nsIFilePicker.modeOpen : nsIFilePicker.modeSave
299                                                                 );
300                                                                 fp.appendFilters( nsIFilePicker.filterAll );
301                                                                 if ( fp.show( ) == nsIFilePicker.returnOK && fp.file ) {
302                                                                         return fp.file;
303                                                                 } else {
304                                                                         return null;
305                                                                 }
306                                                         }
307                                                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
308                                                         JSAN.use('util.file');
309                                                         var f = pick_file('open');
310                                                         var i_file = new util.file(''); i_file._file = f;
311                                                         var content = i_file.get_content();
312                                                         i_file.close();
313                                                         var barcodes = content.split(/\s+/);
314                                         if (barcodes.length > 0) {
315                                             JSAN.use('util.exec'); var exec = new util.exec();
316                                             var funcs = [];
317                                             for (var i = 0; i < barcodes.length; i++) {
318                                                 funcs.push(
319                                                     function(b){
320                                                         return function() {
321                                                             obj.copy_status(b);
322                                                         }
323                                                     }(barcodes[i])
324                                                 );
325                                             }
326                                                                 funcs.push( function() { alert('File uploaded.'); } );
327                                             exec.chain( funcs );
328                                         } else {
329                                                                 alert('No barcodes found in file.');
330                                                         }
331
332                                                 }
333                                         ],
334                                         'cmd_copy_status_print' : [
335                                                 ['command'],
336                                                 function() {
337                                                         try {
338                                                                 obj.list.on_all_fleshed =
339                                                                         function() {
340                                                                                 try {
341                                                                                         dump( js2JSON( obj.list.dump_with_keys() ) + '\n' );
342                                                                                         obj.data.stash_retrieve();
343                                                                                         var lib = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
344                                                                                         lib.children(null);
345                                                                                         var p = { 
346                                                                                                 'lib' : lib,
347                                                                                                 'staff' : obj.data.list.au[0],
348                                                                                                 'header' : obj.data.print_list_templates.item_status.header,
349                                                                                                 'line_item' : obj.data.print_list_templates.item_status.line_item,
350                                                                                                 'footer' : obj.data.print_list_templates.item_status.footer,
351                                                                                                 'type' : obj.data.print_list_templates.item_status.type,
352                                                                                                 'list' : obj.list.dump_with_keys(),
353                                                                                         };
354                                                                                         JSAN.use('util.print'); var print = new util.print();
355                                                                                         print.tree_list( p );
356                                                                                         setTimeout(function(){ obj.list.on_all_fleshed = null; },0);
357                                                                                 } catch(E) {
358                                                                                         obj.error.standard_unexpected_error_alert('print',E); 
359                                                                                 }
360                                                                         }
361                                                                 obj.list.full_retrieve();
362                                                         } catch(E) {
363                                                                 obj.error.standard_unexpected_error_alert('print',E); 
364                                                         }
365                                                 }
366                                         ],
367                                         'cmd_copy_status_export' : [
368                                                 ['command'],
369                                                 function() {
370                                                         try {
371                                                                 obj.list.on_all_fleshed =
372                                                                         function() {
373                                                                                 try {
374                                                                                         dump( obj.list.dump_csv() + '\n' );
375                                                                                         copy_to_clipboard(obj.list.dump_csv());
376                                                                                         setTimeout(function(){ obj.list.on_all_fleshed = null; },0);
377                                                                                 } catch(E) {
378                                                                                         obj.error.standard_unexpected_error_alert('export',E); 
379                                                                                 }
380                                                                         }
381                                                                 obj.list.full_retrieve();
382                                                         } catch(E) {
383                                                                 obj.error.standard_unexpected_error_alert('export',E); 
384                                                         }
385                                                 }
386                                         ],
387                                         'cmd_copy_status_print_export' : [
388                                                 ['command'],
389                                                 function() {
390                                                         try {
391                                                                 obj.list.on_all_fleshed =
392                                                                         function() {
393                                                                                 try {
394                                                                                         dump( obj.list.dump_csv() + '\n' );
395                                                                                         //copy_to_clipboard(obj.list.dump_csv());
396                                                                                         JSAN.use('util.print'); var print = new util.print();
397                                                                                         print.simple(obj.list.dump_csv(),{'content_type':'text/plain'});
398                                                                                         setTimeout(function(){ obj.list.on_all_fleshed = null; },0);
399                                                                                 } catch(E) {
400                                                                                         obj.error.standard_unexpected_error_alert('export',E); 
401                                                                                 }
402                                                                         }
403                                                                 obj.list.full_retrieve();
404                                                         } catch(E) {
405                                                                 obj.error.standard_unexpected_error_alert('export',E); 
406                                                         }
407                                                 }
408                                         ],
409
410                                         'cmd_add_items' : [
411                                                 ['command'],
412                                                 function() {
413                                                         try {
414
415                                                                 JSAN.use('util.functional');
416                                                                 var list = util.functional.map_list( obj.selection_list, function(o) { return o.acn_id; } );
417                                                                 if (list.length == 0) return;
418
419                                                                 var copy_shortcut = {}; var map_acn = {};
420
421                                                                 for (var i = 0; i < list.length; i++) {
422                                                                         var volume_id = list[i];
423                                                                         if (volume_id == -1) continue; /* ignore magic pre-cat volume */
424                                                                         if (! map_acn[volume_id]) {
425                                                                                 map_acn[ volume_id ] = obj.network.simple_request('FM_ACN_RETRIEVE',[ volume_id ]);
426                                                                         }
427                                                                         var record_id = map_acn[ volume_id ].record();
428                                                                         var ou_id = map_acn[ volume_id ].owning_lib();
429                                                                         var label = map_acn[ volume_id ].label();
430                                                                         if (!copy_shortcut[record_id]) copy_shortcut[record_id] = {};
431                                                                         if (!copy_shortcut[record_id][ou_id]) copy_shortcut[record_id][ou_id] = {};
432                                                                         copy_shortcut[record_id][ou_id][ label ] = volume_id;
433
434                                                                 }
435
436                                                                 for (var r in copy_shortcut) {
437
438                                                                         /* quick fix */  /* what was this fixing? */
439                                                                         list = []; for (var i in copy_shortcut[r]) { list.push( i ); }
440
441                                                                         var edit = 0;
442                                                                         try {
443                                                                                 edit = obj.network.request(
444                                                                                         api.PERM_MULTI_ORG_CHECK.app,
445                                                                                         api.PERM_MULTI_ORG_CHECK.method,
446                                                                                         [ 
447                                                                                                 ses(), 
448                                                                                                 obj.data.list.au[0].id(), 
449                                                                                                 list,
450                                                                                                 [ 'CREATE_COPY' ]
451                                                                                         ]
452                                                                                 ).length == 0 ? 1 : 0;
453                                                                         } catch(E) {
454                                                                                 obj.error.sdump('D_ERROR','batch permission check: ' + E);
455                                                                         }
456         
457                                                                         if (edit==0) return; // no read-only view for this interface
458         
459                                                                         var title = 'Add Item for record #' + r;
460         
461                                                                         JSAN.use('util.window'); var win = new util.window();
462                                                                         var w = win.open(
463                                                                                 window.xulG.url_prefix(urls.XUL_VOLUME_COPY_CREATOR),
464                                                                                         //+'?doc_id=' + window.escape(r)
465                                                                                         //+'&ou_ids=' + window.escape( js2JSON(list) )
466                                                                                         //+'&copy_shortcut=' + window.escape( js2JSON(copy_shortcut[r]) ),
467                                                                                 title,
468                                                                                 'chrome,resizable',
469                                                                                 { 'doc_id' : r, 'ou_ids' : list, 'copy_shortcut' : copy_shortcut[r] }
470                                                                         );
471                                                                 }
472
473                                                         } catch(E) {
474                                                                 obj.error.standard_unexpected_error_alert('copy status -> add copies',E);
475                                                         }
476                                                 }
477
478                                         ],
479                                         'cmd_delete_items' : [
480                                                 ['command'],
481                                                 function() {
482                                                         try {
483
484                                 JSAN.use('util.functional');
485
486                                                                 var list = util.functional.map_list( obj.selection_list, function(o) { return o.copy_id; } );
487
488                                 var copies = util.functional.map_list(
489                                     list,
490                                     function (acp_id) {
491                                         return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
492                                     }
493                                 );
494
495                                 for (var i = 0; i < copies.length; i++) {
496                                     copies[i].ischanged(1);
497                                     copies[i].isdeleted(1);
498                                 }
499
500                                                                 if (! window.confirm('Are you sure sure you want to delete these items? ' + util.functional.map_list( copies, function(o) { return o.barcode(); }).join(", ")) ) return;
501
502                                 var robj = obj.network.simple_request('FM_ACP_FLESHED_BATCH_UPDATE',[ ses(), copies, true]);
503                                                                 var robj = obj.network.simple_request(
504                                                                         'FM_ACP_FLESHED_BATCH_UPDATE', 
505                                                                         [ ses(), copies, true ], 
506                                                                         null,
507                                                                         {
508                                                                                 'title' : 'Override Delete Failure?',
509                                                                                 'overridable_events' : [
510                                                                                         1208 /* TITLE_LAST_COPY */,
511                                                                                         1227 /* COPY_DELETE_WARNING */,
512                                                                                 ]
513                                                                         }
514                                                                 );
515         
516                                 if (typeof robj.ilsevent != 'undefined') {
517                                                                         switch(robj.ilsevent) {
518                                                                                 case 1208 /* TITLE_LAST_COPY */:
519                                                                                 case 1227 /* COPY_DELETE_WARNING */:
520                                                                                 break;
521                                                                                 default:
522                                                                                         obj.error.standard_unexpected_error_alert('Batch Item Deletion',robj);
523                                                                                 break;
524                                                                         }
525                                                                 } else { alert('Items Deleted'); }
526
527                                                         } catch(E) {
528                                                                 obj.error.standard_unexpected_error_alert('copy status -> delete items',E);
529                                                         }
530                                                 }
531                                         ],
532                                         'cmd_transfer_items' : [
533                                                 ['command'],
534                                                 function() {
535                                                                 try {
536                                                                         obj.data.stash_retrieve();
537                                                                         if (!obj.data.marked_volume) {
538                                                                                 alert('Please mark a volume as the destination and then try this again.');
539                                                                                 return;
540                                                                         }
541                                                                         
542                                                                         JSAN.use('util.functional');
543
544                                                                         var list = util.functional.map_list( obj.selection_list, function(o) { return o.copy_id; } );
545
546                                                                         var volume = obj.network.simple_request('FM_ACN_RETRIEVE',[ obj.data.marked_volume ]);
547
548                                                                         JSAN.use('cat.util'); cat.util.transfer_copies( { 
549                                                                                 'copy_ids' : list, 
550                                                                                 'docid' : volume.record(),
551                                                                                 'volume_label' : volume.label(),
552                                                                                 'owning_lib' : volume.owning_lib(),
553                                                                         } );
554
555                                                                 } catch(E) {
556                                                                         obj.error.standard_unexpected_error_alert('All copies not likely transferred.',E);
557                                                                 }
558                                                         }
559
560                                         ],
561                                         'cmd_add_volumes' : [
562                                                 ['command'],
563                                                 function() {
564                                                         try {
565                                                                 JSAN.use('util.functional');
566                                                                 var list = util.functional.map_list( obj.selection_list, function(o) { return o.acn_id; } );
567                                                                 if (list.length == 0) return;
568
569                                                                 var aou_hash = {}; var map_acn = {};
570
571                                                                 for (var i = 0; i < list.length; i++) {
572                                                                         var volume_id = list[i];
573                                                                         if (volume_id == -1) continue; /* ignore magic pre-cat volume */
574                                                                         if (! map_acn[volume_id]) {
575                                                                                 map_acn[ volume_id ] = obj.network.simple_request('FM_ACN_RETRIEVE',[ volume_id ]);
576                                                                         }
577                                                                         var record_id = map_acn[ volume_id ].record();
578                                                                         var ou_id = map_acn[ volume_id ].owning_lib();
579                                                                         var label = map_acn[ volume_id ].label();
580                                                                         if (!aou_hash[record_id]) aou_hash[record_id] = {};
581                                                                         aou_hash[record_id][ou_id] = 1;
582
583                                                                 }
584
585                                                                 for (var r in aou_hash) {
586
587                                                                         list = []; for (var org in aou_hash[r]) list.push(org);
588
589                                                                         var edit = 0;
590                                                                         try {
591                                                                                 edit = obj.network.request(
592                                                                                         api.PERM_MULTI_ORG_CHECK.app,
593                                                                                         api.PERM_MULTI_ORG_CHECK.method,
594                                                                                         [ 
595                                                                                                 ses(), 
596                                                                                                 obj.data.list.au[0].id(), 
597                                                                                                 list,
598                                                                                                 [ 'CREATE_VOLUME', 'CREATE_COPY' ]
599                                                                                         ]
600                                                                                 ).length == 0 ? 1 : 0;
601                                                                         } catch(E) {
602                                                                                 obj.error.sdump('D_ERROR','batch permission check: ' + E);
603                                                                         }
604
605                                                                         if (edit==0) {
606                                                                                 alert("You don't have permission to add volumes to that library.");
607                                                                                 return; // no read-only view for this interface
608                                                                         }
609
610                                                                         var title = 'Add Volume/Item for Record # ' + r;
611
612                                                                         JSAN.use('util.window'); var win = new util.window();
613                                                                         var w = win.open(
614                                                                                 window.xulG.url_prefix(urls.XUL_VOLUME_COPY_CREATOR),
615                                                                                         //+'?doc_id=' + window.escape(r)
616                                                                                         //+'&ou_ids=' + window.escape( js2JSON(list) ),
617                                                                                 title,
618                                                                                 'chrome,resizable',
619                                                                                 { 'doc_id' : r, 'ou_ids' : list }
620                                                                         );
621
622                                                                 }
623
624                                                         } catch(E) {
625                                                                 obj.error.standard_unexpected_error_alert('copy status -> add volumes',E);
626                                                         }
627                                                 }
628
629                                         ],
630                                         'cmd_edit_volumes' : [
631                                                 ['command'],
632                                                 function() {
633                                                         try {
634                                                                 JSAN.use('util.functional');
635                                                                 var list = util.functional.map_list( obj.selection_list, function(o) { return o.acn_id; } );
636                                                                 if (list.length == 0) return;
637
638                                                                 var volume_hash = {}; var map_acn = {};
639
640                                                                 for (var i = 0; i < list.length; i++) {
641                                                                         var volume_id = list[i];
642                                                                         if (volume_id == -1) continue; /* ignore magic pre-cat volume */
643                                                                         if (! map_acn[volume_id]) {
644                                                                                 map_acn[ volume_id ] = obj.network.simple_request('FM_ACN_RETRIEVE',[ volume_id ]);
645                                                                                 map_acn[ volume_id ].copies( [] );
646                                                                         }
647                                                                         var record_id = map_acn[ volume_id ].record();
648                                                                         if (!volume_hash[record_id]) volume_hash[record_id] = {};
649                                                                         volume_hash[record_id][volume_id] = 1;
650                                                                 }
651
652                                                                 for (var rec in volume_hash) {
653
654                                                                         list = []; for (var v in volume_hash[rec]) list.push( map_acn[v] );
655
656                                                                         var edit = 0;
657                                                                         try {
658                                                                                 edit = obj.network.request(
659                                                                                         api.PERM_MULTI_ORG_CHECK.app,
660                                                                                         api.PERM_MULTI_ORG_CHECK.method,
661                                                                                         [ 
662                                                                                                 ses(), 
663                                                                                                 obj.data.list.au[0].id(), 
664                                                                                                 util.functional.map_list(
665                                                                                                         list,
666                                                                                                         function (o) {
667                                                                                                                 return o.owning_lib();
668                                                                                                         }
669                                                                                                 ),
670                                                                                                 [ 'UPDATE_VOLUME' ]
671                                                                                         ]
672                                                                                 ).length == 0 ? 1 : 0;
673                                                                         } catch(E) {
674                                                                                 obj.error.sdump('D_ERROR','batch permission check: ' + E);
675                                                                         }
676
677                                                                         if (edit==0) {
678                                                                                 alert("You don't have permission to edit this volume.");
679                                                                                 return; // no read-only view for this interface
680                                                                         }
681
682                                                                         var title = (list.length == 1 ? 'Volume' : 'Volumes') + ' for record # ' + rec;
683
684                                                                         JSAN.use('util.window'); var win = new util.window();
685                                                                         //obj.data.volumes_temp = js2JSON( list );
686                                                                         //obj.data.stash('volumes_temp');
687                                                                         var my_xulG = win.open(
688                                                                                 window.xulG.url_prefix(urls.XUL_VOLUME_EDITOR),
689                                                                                 title,
690                                                                                 'chrome,modal,resizable',
691                                                                                 { 'volumes' : list }
692                                                                         );
693
694                                                                         /* FIXME -- need to unique the temp space, and not rely on modalness of window */
695                                                                         //obj.data.stash_retrieve();
696                                                                         //var volumes = JSON2js( obj.data.volumes_temp );
697                                                                         var volumes = my_xulG.volumes;
698                                                                         if (!volumes) return;
699                                                                 
700                                                                         volumes = util.functional.filter_list(
701                                                                                 volumes,
702                                                                                 function (o) {
703                                                                                         return o.ischanged() == '1';
704                                                                                 }
705                                                                         );
706
707                                                                         volumes = util.functional.map_list(
708                                                                                 volumes,
709                                                                                 function (o) {
710                                                                                         o.record( rec ); // staff client 2 did not do this.  Does it matter?
711                                                                                         return o;
712                                                                                 }
713                                                                         );
714
715                                                                         if (volumes.length == 0) return;
716
717                                                                         try {
718                                                                                 var r = obj.network.request(
719                                                                                         api.FM_ACN_TREE_UPDATE.app,
720                                                                                         api.FM_ACN_TREE_UPDATE.method,
721                                                                                         [ ses(), volumes, false ]
722                                                                                 );
723                                                                                 if (typeof r.ilsevent != 'undefined') throw(r);
724                                                                                 alert('Volumes modified.');
725                                                                         } catch(E) {
726                                                                                 obj.error.standard_unexpected_error_alert('volume update error: ',E);
727                                                                         }
728
729                                                                 }
730                                                         } catch(E) {
731                                                                 obj.error.standard_unexpected_error_alert('Copy Status -> Volume Edit',E);
732                                                         }
733                                                 }
734
735                                         ],
736                                         'cmd_delete_volumes' : [
737                                                 ['command'],
738                                                 function() {
739                                                         try {
740                                                                 JSAN.use('util.functional');
741                                                                 var list = util.functional.map_list( obj.selection_list, function(o) { return o.acn_id; } );
742                                                                 if (list.length == 0) return;
743
744                                                                 var map_acn = {};
745
746                                                                 for (var i = 0; i < list.length; i++) {
747                                                                         var volume_id = list[i];
748                                                                         if (volume_id == -1) continue; /* ignore magic pre-cat volume */
749                                                                         if (! map_acn[volume_id]) {
750                                                                                 map_acn[ volume_id ] = obj.network.simple_request('FM_ACN_RETRIEVE',[ volume_id ]);
751                                                                         }
752                                                                 }
753
754                                                                 list = []; for (var v in map_acn) list.push( map_acn[v] );
755
756                                                                 var r = obj.error.yns_alert('Are you sure you would like to delete ' + (list.length != 1 ? 'these ' + list.length + ' volumes' : 'this one volume') + '?', 'Delete Volumes?', 'Delete', 'Cancel', null, 'Check here to confirm this action');
757
758                                                                 if (r == 0) {
759                                                                         for (var i = 0; i < list.length; i++) {
760                                                                                 list[i].isdeleted('1');
761                                                                         }
762                                                                         var robj = obj.network.simple_request(
763                                                                                 'FM_ACN_TREE_UPDATE', 
764                                                                                 [ ses(), list, true ],
765                                                                                 null,
766                                                                                 {
767                                                                                         'title' : 'Override Delete Failure?',
768                                                                                         'overridable_events' : [
769                                                                                         ]
770                                                                                 }
771                                                                         );
772                                                                         if (robj == null) throw(robj);
773                                                                         if (typeof robj.ilsevent != 'undefined') {
774                                                                                 if (robj.ilsevent == 1206 /* VOLUME_NOT_EMPTY */) {
775                                                                                         alert('You must delete all the copies on the volume before you may delete the volume itself.');
776                                                                                         return;
777                                                                                 }
778                                                                                 if (robj.ilsevent != 0) throw(robj);
779                                                                         }
780                                                                         alert('Volumes deleted.');
781                                                                 }
782                                                         } catch(E) {
783                                                                 obj.error.standard_unexpected_error_alert('copy status -> delete volumes',E);
784                                                         }
785
786                                                 }
787
788                                         ],
789                                         'cmd_mark_volume' : [
790                                                 ['command'],
791                                                 function() {
792                                                         try {
793                                                                 JSAN.use('util.functional');
794                                                                 var list = util.functional.map_list( obj.selection_list, function(o) { return o.acn_id; } );
795
796                                                                 if (list.length == 1) {
797                                                                         obj.data.marked_volume = list[0];
798                                                                         obj.data.stash('marked_volume');
799                                                                         alert('Volume marked as Item Transfer Destination');
800                                                                 } else {
801                                                                         obj.error.yns_alert('Choose just one Volume to mark as Item Transfer Destination','Limit Selection','OK',null,null,'Check here to confirm this dialog');
802                                                                 }
803                                                         } catch(E) {
804                                                                 obj.error.standard_unexpected_error_alert('copy status -> mark volume',E);
805                                                         }
806                                                 }
807                                         ],
808                                         'cmd_mark_library' : [
809                                                 ['command'],
810                                                 function() {
811                                                         try {
812                                                                 JSAN.use('util.functional');
813                                                                 var list = util.functional.map_list( obj.selection_list, function(o) { return o.acn_id; } );
814
815                                                                 if (list.length == 1) {
816                                                                         var v = obj.network.simple_request('FM_ACN_RETRIEVE',[list[0]]);
817                                                                         var owning_lib = v.owning_lib(); if (typeof owning_lib == 'object') owning_lib = owning_lib.id();
818
819                                                                         obj.data.marked_library = { 'lib' : owning_lib, 'docid' : v.record() };
820                                                                         obj.data.stash('marked_library');
821                                                                         alert('Library + Record marked as Volume Transfer Destination');
822                                                                 } else {
823                                                                         obj.error.yns_alert('Choose just one Library to mark as Volume Transfer Destination','Limit Selection','OK',null,null,'Check here to confirm this dialog');
824                                                                 }
825                                                         } catch(E) {
826                                                                 obj.error.standard_unexpected_error_alert('copy status -> mark library',E);
827                                                         }
828                                                 }
829                                         ],
830                                         'cmd_transfer_volume' : [
831                                                 ['command'],
832                                                 function() {
833                                                         try {
834                                                                         obj.data.stash_retrieve();
835                                                                         if (!obj.data.marked_library) {
836                                                                                 alert('Please mark a library as the destination from within holdings maintenance and then try this again.');
837                                                                                 return;
838                                                                         }
839                                                                         
840                                                                         JSAN.use('util.functional');
841
842                                                                         var list = util.functional.map_list( obj.selection_list, function(o) { return o.acn_id; } );
843                                                                         if (list.length == 0) return;
844
845                                                                         var map_acn = {};
846
847                                                                         for (var i = 0; i < list.length; i++) {
848                                                                                 var volume_id = list[i];
849                                                                                 if (volume_id == -1) continue; /* ignore magic pre-cat volume */
850                                                                                 if (! map_acn[volume_id]) {
851                                                                                         map_acn[ volume_id ] = obj.network.simple_request('FM_ACN_RETRIEVE',[ volume_id ]);
852                                                                                 }
853                                                                         }
854
855                                                                         list = []; for (v in map_acn) list.push(map_acn[v]);
856
857                                                                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
858                                                                         var xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: auto">';
859                                                                         xml += '<description>Transfer volumes ';
860
861                                                                         xml += util.functional.map_list(
862                                                                                 list,
863                                                                                 function (o) {
864                                                                                         return o.label();
865                                                                                 }
866                                                                         ).join(", ");
867
868                                                                         xml += ' to library ' + obj.data.hash.aou[ obj.data.marked_library.lib ].shortname();
869                                                                         xml += ' on the following record?</description>';
870                                                                         xml += '<hbox><button label="Transfer" name="fancy_submit"/>';
871                                                                         xml += '<button label="Cancel" accesskey="C" name="fancy_cancel"/></hbox>';
872                                                                         xml += '<iframe style="overflow: scroll" flex="1" src="' + urls.XUL_BIB_BRIEF + '?docid=' + obj.data.marked_library.docid + '"/>';
873                                                                         xml += '</vbox>';
874                                                                         JSAN.use('OpenILS.data');
875                                                                         //var data = new OpenILS.data(); data.init({'via':'stash'});
876                                                                         //data.temp_transfer = xml; data.stash('temp_transfer');
877                                                                         JSAN.use('util.window'); var win = new util.window();
878                                                                         var fancy_prompt_data = win.open(
879                                                                                 urls.XUL_FANCY_PROMPT,
880                                                                                 //+ '?xml_in_stash=temp_transfer'
881                                                                                 //+ '&title=' + window.escape('Volume Transfer'),
882                                                                                 'fancy_prompt', 'chrome,resizable,modal,width=500,height=300',
883                                                                                 { 'xml' : xml, 'title' : 'Volume Transfer' }
884                                                                         );
885                                                                 
886                                                                         if (fancy_prompt_data.fancy_status == 'incomplete') { alert('Transfer Aborted'); return; }
887
888                                                                         var robj = obj.network.simple_request(
889                                                                                 'FM_ACN_TRANSFER', 
890                                                                                 [ ses(), { 'docid' : obj.data.marked_library.docid, 'lib' : obj.data.marked_library.lib, 'volumes' : util.functional.map_list( list, function(o) { return o.id(); }) } ],
891                                                                                 null,
892                                                                                 {
893                                                                                         'title' : 'Override Volume Transfer Failure?',
894                                                                                         'overridable_events' : [
895                                                                                                 1208 /* TITLE_LAST_COPY */,
896                                                                                                 1219 /* COPY_REMOTE_CIRC_LIB */,
897                                                                                         ],
898                                                                                 }
899                                                                         );
900
901                                                                         if (typeof robj.ilsevent != 'undefined') {
902                                                                                 if (robj.ilsevent == 1221 /* ORG_CANNOT_HAVE_VOLS */) {
903                                                                                         alert('That destination cannot have volumes.');
904                                                                                 } else {
905                                                                                         throw(robj);
906                                                                                 }
907                                                                         } else {
908                                                                                 alert('Volumes transferred.');
909                                                                         }
910
911                                                         } catch(E) {
912                                                                 obj.error.standard_unexpected_error_alert('All volumes not likely transferred.',E);
913                                                         }
914                                                 }
915
916                                         ],
917                                 }
918                         }
919                 );
920                 this.controller.render();
921                 this.controller.view.copy_status_barcode_entry_textbox.focus();
922
923         },
924
925         'test_barcode' : function(bc) {
926                 var obj = this;
927                 var good = util.barcode.check(bc);
928                 var x = document.getElementById('strict_barcode');
929                 if (x && x.checked != true) return true;
930                 if (good) {
931                         return true;
932                 } else {
933                         if ( 1 == obj.error.yns_alert(
934                                                 'Bad checkdigit; possible mis-scan.  Use this barcode ("' + bc + '") anyway?',
935                                                 'Bad Barcode',
936                                                 'Cancel',
937                                                 'Accept Barcode',
938                                                 null,
939                                                 'Check here to confirm this action',
940                                                 '/xul/server/skin/media/images/bad_barcode.png'
941                         ) ) {
942                                 return true;
943                         } else {
944                                 return false;
945                         }
946                 }
947         },
948
949         'copy_status' : function(barcode,refresh) {
950                 var obj = this;
951                 try {
952                         try { document.getElementById('last_scanned').setAttribute('value',''); } catch(E) {}
953                         if (!barcode) barcode = obj.controller.view.copy_status_barcode_entry_textbox.value;
954                         if (!barcode) return;
955                         if (barcode) {
956                                 if ( obj.test_barcode(barcode) ) { /* good */ } else { /* bad */ return; }
957                         }
958                         JSAN.use('circ.util');
959                         function handle_req(req) {
960                                 try {
961                                         var details = req.getResultObject();
962                                         if (details == null) {
963                                                 throw('Something weird happened.  null result');
964                                         } else if (details.ilsevent) {
965                                                 switch(details.ilsevent) {
966                                                         case -1: 
967                                                                 obj.error.standard_network_error_alert(); 
968                                                                 obj.controller.view.copy_status_barcode_entry_textbox.select();
969                                                                 obj.controller.view.copy_status_barcode_entry_textbox.focus();
970                                                                 return;
971                                                         break;
972                                                         case 1502 /* ASSET_COPY_NOT_FOUND */ :
973                                                                 try { document.getElementById('last_scanned').setAttribute('value',barcode + ' was either mis-scanned or is not cataloged.'); } catch(E) {}
974                                                                 obj.error.yns_alert(barcode + ' was either mis-scanned or is not cataloged.','Not Cataloged','OK',null,null,'Check here to confirm this message');
975                                                                 obj.controller.view.copy_status_barcode_entry_textbox.select();
976                                                                 obj.controller.view.copy_status_barcode_entry_textbox.focus();
977                                                                 return;
978                                                         break;
979                                                         default: 
980                                                                 throw(details); 
981                                                         break;
982                                                 }
983                                         }
984                                         var msg = details.copy.barcode() + ' -- ';
985                                         if (details.copy.call_number() == -1) msg += 'Item is a Pre-Cat.  ';
986                                         if (details.hold) msg += 'Item is captured for a Hold.  ';
987                                         if (details.transit) msg += 'Item is in Transit.  ';
988                                         if (details.circ && ! details.circ.checkin_time()) msg += 'Item is circulating.  ';
989                                         try { document.getElementById('last_scanned').setAttribute('value',msg); } catch(E) {}
990                                         if (document.getElementById('trim_list')) {
991                                                 var x = document.getElementById('trim_list');
992                                                 if (x.checked) { obj.list.trim_list = 20; } else { obj.list.trim_list = null; }
993                                         }
994                                         var params = {
995                                                 'retrieve_id' : js2JSON( 
996                                                         { 
997                                                                 'renewable' : details.circ ? 't' : 'f', 
998                                                                 'copy_id' : details.copy.id(), 
999                                                                 'acn_id' : details.volume ? details.volume.id() : -1, 
1000                                                                 'barcode' : barcode, 
1001                                                                 'doc_id' : details.mvr ? details.mvr.doc_id() : null  
1002                                                         } 
1003                                                 ),
1004                                                 'row' : {
1005                                                         'my' : {
1006                                                                 'mvr' : details.mvr,
1007                                                                 'acp' : details.copy,
1008                                                                 'acn' : details.volume,
1009                                                                 'atc' : details.transit,
1010                                                                 'circ' : details.circ,
1011                                                                 'ahr' : details.hold,
1012                                                         }
1013                                                 },
1014                                                 'to_top' : true,
1015                                         };
1016                                         if (!refresh) {
1017                                                 var nparams = obj.list.append(params);
1018                                                 if (!document.getElementById('trim_list').checked) {
1019                                                         if (typeof obj.list_barcode_map[barcode] == 'undefined') obj.list_barcode_map[barcode] =[];
1020                                                         obj.list_barcode_map[barcode].push(nparams);
1021                                                 }
1022                                         } else {
1023                                                 if (!document.getElementById('trim_list').checked) {
1024                                                         for (var i = 0; i < obj.list_barcode_map[barcode].length; i++) {
1025                                                                 params.my_node = obj.list_barcode_map[barcode][i].my_node;
1026                                                                 obj.list.refresh_row(params);
1027                                                         }
1028                                                 } else {
1029                                                         obj.list.append(params);
1030                                                 }
1031                                         }
1032                                 } catch(E) {
1033                                         obj.error.standard_unexpected_error_alert('barcode = ' + barcode,E);
1034                                 }
1035                         }
1036                         var result = obj.network.simple_request('FM_ACP_DETAILS_VIA_BARCODE', [ ses(), barcode ]);
1037                         handle_req({'getResultObject':function(){return result;}}); // used to be async
1038                         obj.controller.view.copy_status_barcode_entry_textbox.value = '';
1039                         obj.controller.view.copy_status_barcode_entry_textbox.focus();
1040                         
1041                 } catch(E) {
1042                         obj.error.standard_unexpected_error_alert('barcode = ' + barcode,E);
1043                         obj.controller.view.copy_status_barcode_entry_textbox.select();
1044                         obj.controller.view.copy_status_barcode_entry_textbox.focus();
1045                 }
1046
1047         },
1048         
1049         'spawn_copy_editor' : function() {
1050
1051                 /* FIXME -  a lot of redundant calls here */
1052
1053                 var obj = this;
1054
1055                 JSAN.use('util.widgets'); JSAN.use('util.functional');
1056
1057                 var list = obj.selection_list;
1058
1059                 list = util.functional.map_list(
1060                         list,
1061                         function (o) {
1062                                 return o.copy_id;
1063                         }
1064                 );
1065
1066                 var copies = util.functional.map_list(
1067                         list,
1068                         function (acp_id) {
1069                                 return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
1070                         }
1071                 );
1072
1073                 var edit = 0;
1074                 try {
1075                         edit = obj.network.request(
1076                                 api.PERM_MULTI_ORG_CHECK.app,
1077                                 api.PERM_MULTI_ORG_CHECK.method,
1078                                 [ 
1079                                         ses(), 
1080                                         obj.data.list.au[0].id(), 
1081                                         util.functional.map_list(
1082                                                 copies,
1083                                                 function (o) {
1084                                                         return o.call_number() == -1 ? o.circ_lib() : obj.network.simple_request('FM_ACN_RETRIEVE',[o.call_number()]).owning_lib();
1085                                                 }
1086                                         ),
1087                                         copies.length == 1 ? [ 'UPDATE_COPY' ] : [ 'UPDATE_COPY', 'UPDATE_BATCH_COPY' ]
1088                                 ]
1089                         ).length == 0 ? 1 : 0;
1090                 } catch(E) {
1091                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
1092                 }
1093
1094                 JSAN.use('cat.util'); cat.util.spawn_copy_editor(list,edit);
1095
1096         },
1097
1098 }
1099
1100 dump('exiting circ.copy_status.js\n');