]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/circ/copy_status.js
refactor spawn_copy_editor and perm check
[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_copyid_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                                                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
292                                                         JSAN.use('util.file');
293                                                         var f = new util.file('');
294                             var content = f.import_file( { 'title' : 'Import Barcode File', 'not_json' : true } );
295                             if (!content) return;
296                                                         var barcodes = content.split(/\s+/);
297                                         if (barcodes.length > 0) {
298                                             JSAN.use('util.exec'); var exec = new util.exec();
299                                             var funcs = [];
300                                             for (var i = 0; i < barcodes.length; i++) {
301                                                 funcs.push(
302                                                     function(b){
303                                                         return function() {
304                                                             obj.copy_status(b);
305                                                         }
306                                                     }(barcodes[i])
307                                                 );
308                                             }
309                                                                 funcs.push( function() { alert('File uploaded.'); } );
310                                             exec.chain( funcs );
311                                         } else {
312                                                                 alert('No barcodes found in file.');
313                                                         }
314
315                                                 }
316                                         ],
317                                         'cmd_copy_status_print' : [
318                                                 ['command'],
319                                                 function() {
320                                                         try {
321                                 var p = { 
322                                     'template' : 'item_status'
323                                 };
324                                                                 obj.list.print(p);
325                                                         } catch(E) {
326                                                                 obj.error.standard_unexpected_error_alert('print',E); 
327                                                         }
328                                                 }
329                                         ],
330                                         'cmd_copy_status_export' : [
331                                                 ['command'],
332                                                 function() {
333                                                         try {
334                                                                 obj.list.dump_csv_to_clipboard();
335                                                         } catch(E) {
336                                                                 obj.error.standard_unexpected_error_alert('export',E); 
337                                                         }
338                                                 }
339                                         ],
340                                         'cmd_copy_status_print_export' : [
341                                                 ['command'],
342                                                 function() {
343                                                         try {
344                                                                 obj.list.on_all_fleshed =
345                                                                         function() {
346                                                                                 try {
347                                                                                         dump( obj.list.dump_csv() + '\n' );
348                                                                                         //copy_to_clipboard(obj.list.dump_csv());
349                                                                                         JSAN.use('util.print'); var print = new util.print();
350                                                                                         print.simple(obj.list.dump_csv(),{'content_type':'text/plain'});
351                                                                                         setTimeout(function(){ obj.list.on_all_fleshed = null; },0);
352                                                                                 } catch(E) {
353                                                                                         obj.error.standard_unexpected_error_alert('export',E); 
354                                                                                 }
355                                                                         }
356                                                                 obj.list.full_retrieve();
357                                                         } catch(E) {
358                                                                 obj.error.standard_unexpected_error_alert('export',E); 
359                                                         }
360                                                 }
361                                         ],
362
363                                         'cmd_add_items' : [
364                                                 ['command'],
365                                                 function() {
366                                                         try {
367
368                                                                 JSAN.use('util.functional');
369                                                                 var list = util.functional.map_list( obj.selection_list, function(o) { return o.acn_id; } );
370                                                                 if (list.length == 0) return;
371
372                                                                 var copy_shortcut = {}; var map_acn = {};
373
374                                                                 for (var i = 0; i < list.length; i++) {
375                                                                         var volume_id = list[i];
376                                                                         if (volume_id == -1) continue; /* ignore magic pre-cat volume */
377                                                                         if (! map_acn[volume_id]) {
378                                                                                 map_acn[ volume_id ] = obj.network.simple_request('FM_ACN_RETRIEVE',[ volume_id ]);
379                                                                         }
380                                                                         var record_id = map_acn[ volume_id ].record();
381                                                                         var ou_id = map_acn[ volume_id ].owning_lib();
382                                                                         var label = map_acn[ volume_id ].label();
383                                                                         if (!copy_shortcut[record_id]) copy_shortcut[record_id] = {};
384                                                                         if (!copy_shortcut[record_id][ou_id]) copy_shortcut[record_id][ou_id] = {};
385                                                                         copy_shortcut[record_id][ou_id][ label ] = volume_id;
386
387                                                                 }
388
389                                                                 for (var r in copy_shortcut) {
390
391                                                                         /* quick fix */  /* what was this fixing? */
392                                                                         list = []; for (var i in copy_shortcut[r]) { list.push( i ); }
393
394                                                                         var edit = 0;
395                                                                         try {
396                                                                                 edit = obj.network.request(
397                                                                                         api.PERM_MULTI_ORG_CHECK.app,
398                                                                                         api.PERM_MULTI_ORG_CHECK.method,
399                                                                                         [ 
400                                                                                                 ses(), 
401                                                                                                 obj.data.list.au[0].id(), 
402                                                                                                 list,
403                                                                                                 [ 'CREATE_COPY' ]
404                                                                                         ]
405                                                                                 ).length == 0 ? 1 : 0;
406                                                                         } catch(E) {
407                                                                                 obj.error.sdump('D_ERROR','batch permission check: ' + E);
408                                                                         }
409         
410                                                                         if (edit==0) return; // no read-only view for this interface
411         
412                                                                         var title = 'Add Item for record #' + r;
413         
414                                                                         JSAN.use('util.window'); var win = new util.window();
415                                                                         var w = win.open(
416                                                                                 window.xulG.url_prefix(urls.XUL_VOLUME_COPY_CREATOR),
417                                                                                         //+'?doc_id=' + window.escape(r)
418                                                                                         //+'&ou_ids=' + window.escape( js2JSON(list) )
419                                                                                         //+'&copy_shortcut=' + window.escape( js2JSON(copy_shortcut[r]) ),
420                                                                                 title,
421                                                                                 'chrome,resizable',
422                                                                                 { 'doc_id' : r, 'ou_ids' : list, 'copy_shortcut' : copy_shortcut[r] }
423                                                                         );
424                                                                 }
425
426                                                         } catch(E) {
427                                                                 obj.error.standard_unexpected_error_alert('copy status -> add copies',E);
428                                                         }
429                                                 }
430
431                                         ],
432                                         'cmd_delete_items' : [
433                                                 ['command'],
434                                                 function() {
435                                                         try {
436
437                                 JSAN.use('util.functional');
438
439                                                                 var list = util.functional.map_list( obj.selection_list, function(o) { return o.copy_id; } );
440
441                                 var copies = util.functional.map_list(
442                                     list,
443                                     function (acp_id) {
444                                         return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
445                                     }
446                                 );
447
448                                 for (var i = 0; i < copies.length; i++) {
449                                     copies[i].ischanged(1);
450                                     copies[i].isdeleted(1);
451                                 }
452
453                                                                 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;
454
455                                 var robj = obj.network.simple_request('FM_ACP_FLESHED_BATCH_UPDATE',[ ses(), copies, true]);
456                                                                 var robj = obj.network.simple_request(
457                                                                         'FM_ACP_FLESHED_BATCH_UPDATE', 
458                                                                         [ ses(), copies, true ], 
459                                                                         null,
460                                                                         {
461                                                                                 'title' : 'Override Delete Failure?',
462                                                                                 'overridable_events' : [
463                                                                                         1208 /* TITLE_LAST_COPY */,
464                                                                                         1227 /* COPY_DELETE_WARNING */,
465                                                                                 ]
466                                                                         }
467                                                                 );
468         
469                                 if (typeof robj.ilsevent != 'undefined') {
470                                                                         switch(robj.ilsevent) {
471                                                                                 case 1208 /* TITLE_LAST_COPY */:
472                                                                                 case 1227 /* COPY_DELETE_WARNING */:
473                                                                                 break;
474                                                                                 default:
475                                                                                         obj.error.standard_unexpected_error_alert('Batch Item Deletion',robj);
476                                                                                 break;
477                                                                         }
478                                                                 } else { alert('Items Deleted'); }
479
480                                                         } catch(E) {
481                                                                 obj.error.standard_unexpected_error_alert('copy status -> delete items',E);
482                                                         }
483                                                 }
484                                         ],
485                                         'cmd_transfer_items' : [
486                                                 ['command'],
487                                                 function() {
488                                                                 try {
489                                                                         obj.data.stash_retrieve();
490                                                                         if (!obj.data.marked_volume) {
491                                                                                 alert('Please mark a volume as the destination and then try this again.');
492                                                                                 return;
493                                                                         }
494                                                                         
495                                                                         JSAN.use('util.functional');
496
497                                                                         var list = util.functional.map_list( obj.selection_list, function(o) { return o.copy_id; } );
498
499                                                                         var volume = obj.network.simple_request('FM_ACN_RETRIEVE',[ obj.data.marked_volume ]);
500
501                                                                         JSAN.use('cat.util'); cat.util.transfer_copies( { 
502                                                                                 'copy_ids' : list, 
503                                                                                 'docid' : volume.record(),
504                                                                                 'volume_label' : volume.label(),
505                                                                                 'owning_lib' : volume.owning_lib(),
506                                                                         } );
507
508                                                                 } catch(E) {
509                                                                         obj.error.standard_unexpected_error_alert('All copies not likely transferred.',E);
510                                                                 }
511                                                         }
512
513                                         ],
514                                         'cmd_add_volumes' : [
515                                                 ['command'],
516                                                 function() {
517                                                         try {
518                                                                 JSAN.use('util.functional');
519                                                                 var list = util.functional.map_list( obj.selection_list, function(o) { return o.acn_id; } );
520                                                                 if (list.length == 0) return;
521
522                                                                 var aou_hash = {}; var map_acn = {};
523
524                                                                 for (var i = 0; i < list.length; i++) {
525                                                                         var volume_id = list[i];
526                                                                         if (volume_id == -1) continue; /* ignore magic pre-cat volume */
527                                                                         if (! map_acn[volume_id]) {
528                                                                                 map_acn[ volume_id ] = obj.network.simple_request('FM_ACN_RETRIEVE',[ volume_id ]);
529                                                                         }
530                                                                         var record_id = map_acn[ volume_id ].record();
531                                                                         var ou_id = map_acn[ volume_id ].owning_lib();
532                                                                         var label = map_acn[ volume_id ].label();
533                                                                         if (!aou_hash[record_id]) aou_hash[record_id] = {};
534                                                                         aou_hash[record_id][ou_id] = 1;
535
536                                                                 }
537
538                                                                 for (var r in aou_hash) {
539
540                                                                         list = []; for (var org in aou_hash[r]) list.push(org);
541
542                                                                         var edit = 0;
543                                                                         try {
544                                                                                 edit = obj.network.request(
545                                                                                         api.PERM_MULTI_ORG_CHECK.app,
546                                                                                         api.PERM_MULTI_ORG_CHECK.method,
547                                                                                         [ 
548                                                                                                 ses(), 
549                                                                                                 obj.data.list.au[0].id(), 
550                                                                                                 list,
551                                                                                                 [ 'CREATE_VOLUME', 'CREATE_COPY' ]
552                                                                                         ]
553                                                                                 ).length == 0 ? 1 : 0;
554                                                                         } catch(E) {
555                                                                                 obj.error.sdump('D_ERROR','batch permission check: ' + E);
556                                                                         }
557
558                                                                         if (edit==0) {
559                                                                                 alert("You don't have permission to add volumes to that library.");
560                                                                                 return; // no read-only view for this interface
561                                                                         }
562
563                                                                         var title = 'Add Volume/Item for Record # ' + r;
564
565                                                                         JSAN.use('util.window'); var win = new util.window();
566                                                                         var w = win.open(
567                                                                                 window.xulG.url_prefix(urls.XUL_VOLUME_COPY_CREATOR),
568                                                                                         //+'?doc_id=' + window.escape(r)
569                                                                                         //+'&ou_ids=' + window.escape( js2JSON(list) ),
570                                                                                 title,
571                                                                                 'chrome,resizable',
572                                                                                 { 'doc_id' : r, 'ou_ids' : list }
573                                                                         );
574
575                                                                 }
576
577                                                         } catch(E) {
578                                                                 obj.error.standard_unexpected_error_alert('copy status -> add volumes',E);
579                                                         }
580                                                 }
581
582                                         ],
583                                         'cmd_edit_volumes' : [
584                                                 ['command'],
585                                                 function() {
586                                                         try {
587                                                                 JSAN.use('util.functional');
588                                                                 var list = util.functional.map_list( obj.selection_list, function(o) { return o.acn_id; } );
589                                                                 if (list.length == 0) return;
590
591                                                                 var volume_hash = {}; var map_acn = {};
592
593                                                                 for (var i = 0; i < list.length; i++) {
594                                                                         var volume_id = list[i];
595                                                                         if (volume_id == -1) continue; /* ignore magic pre-cat volume */
596                                                                         if (! map_acn[volume_id]) {
597                                                                                 map_acn[ volume_id ] = obj.network.simple_request('FM_ACN_RETRIEVE',[ volume_id ]);
598                                                                                 map_acn[ volume_id ].copies( [] );
599                                                                         }
600                                                                         var record_id = map_acn[ volume_id ].record();
601                                                                         if (!volume_hash[record_id]) volume_hash[record_id] = {};
602                                                                         volume_hash[record_id][volume_id] = 1;
603                                                                 }
604
605                                                                 for (var rec in volume_hash) {
606
607                                                                         list = []; for (var v in volume_hash[rec]) list.push( map_acn[v] );
608
609                                                                         var edit = 0;
610                                                                         try {
611                                                                                 edit = obj.network.request(
612                                                                                         api.PERM_MULTI_ORG_CHECK.app,
613                                                                                         api.PERM_MULTI_ORG_CHECK.method,
614                                                                                         [ 
615                                                                                                 ses(), 
616                                                                                                 obj.data.list.au[0].id(), 
617                                                                                                 util.functional.map_list(
618                                                                                                         list,
619                                                                                                         function (o) {
620                                                                                                                 return o.owning_lib();
621                                                                                                         }
622                                                                                                 ),
623                                                                                                 [ 'UPDATE_VOLUME' ]
624                                                                                         ]
625                                                                                 ).length == 0 ? 1 : 0;
626                                                                         } catch(E) {
627                                                                                 obj.error.sdump('D_ERROR','batch permission check: ' + E);
628                                                                         }
629
630                                                                         if (edit==0) {
631                                                                                 alert("You don't have permission to edit this volume.");
632                                                                                 return; // no read-only view for this interface
633                                                                         }
634
635                                                                         var title = (list.length == 1 ? 'Volume' : 'Volumes') + ' for record # ' + rec;
636
637                                                                         JSAN.use('util.window'); var win = new util.window();
638                                                                         //obj.data.volumes_temp = js2JSON( list );
639                                                                         //obj.data.stash('volumes_temp');
640                                                                         var my_xulG = win.open(
641                                                                                 window.xulG.url_prefix(urls.XUL_VOLUME_EDITOR),
642                                                                                 title,
643                                                                                 'chrome,modal,resizable',
644                                                                                 { 'volumes' : list }
645                                                                         );
646
647                                                                         /* FIXME -- need to unique the temp space, and not rely on modalness of window */
648                                                                         //obj.data.stash_retrieve();
649                                                                         //var volumes = JSON2js( obj.data.volumes_temp );
650                                                                         var volumes = my_xulG.volumes;
651                                                                         if (!volumes) return;
652                                                                 
653                                                                         volumes = util.functional.filter_list(
654                                                                                 volumes,
655                                                                                 function (o) {
656                                                                                         return o.ischanged() == '1';
657                                                                                 }
658                                                                         );
659
660                                                                         volumes = util.functional.map_list(
661                                                                                 volumes,
662                                                                                 function (o) {
663                                                                                         o.record( rec ); // staff client 2 did not do this.  Does it matter?
664                                                                                         return o;
665                                                                                 }
666                                                                         );
667
668                                                                         if (volumes.length == 0) return;
669
670                                                                         try {
671                                                                                 var r = obj.network.request(
672                                                                                         api.FM_ACN_TREE_UPDATE.app,
673                                                                                         api.FM_ACN_TREE_UPDATE.method,
674                                                                                         [ ses(), volumes, false ]
675                                                                                 );
676                                         if (typeof r.ilsevent != 'undefined') {
677                                             switch(r.ilsevent) {
678                                                 case 1705 /* VOLUME_LABEL_EXISTS */ :
679                                                     alert("Edit failed:  You tried to change a volume's callnumber to one that is already in use for the given library.  You should transfer the items to the desired callnumber instead.");
680                                                     break;
681                                                 default: throw(r);
682                                             }
683                                         } else {
684                                                                                 alert('Volumes modified.');
685                                         }
686                                                                         } catch(E) {
687                                                                                 obj.error.standard_unexpected_error_alert('volume update error: ',E);
688                                                                         }
689
690                                                                 }
691                                                         } catch(E) {
692                                                                 obj.error.standard_unexpected_error_alert('Copy Status -> Volume Edit',E);
693                                                         }
694                                                 }
695
696                                         ],
697                                         'cmd_delete_volumes' : [
698                                                 ['command'],
699                                                 function() {
700                                                         try {
701                                                                 JSAN.use('util.functional');
702                                                                 var list = util.functional.map_list( obj.selection_list, function(o) { return o.acn_id; } );
703                                                                 if (list.length == 0) return;
704
705                                                                 var map_acn = {};
706
707                                                                 for (var i = 0; i < list.length; i++) {
708                                                                         var volume_id = list[i];
709                                                                         if (volume_id == -1) continue; /* ignore magic pre-cat volume */
710                                                                         if (! map_acn[volume_id]) {
711                                                                                 map_acn[ volume_id ] = obj.network.simple_request('FM_ACN_RETRIEVE',[ volume_id ]);
712                                                                         }
713                                                                 }
714
715                                                                 list = []; for (var v in map_acn) list.push( map_acn[v] );
716
717                                                                 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');
718
719                                                                 if (r == 0) {
720                                                                         for (var i = 0; i < list.length; i++) {
721                                                                                 list[i].isdeleted('1');
722                                                                         }
723                                                                         var robj = obj.network.simple_request(
724                                                                                 'FM_ACN_TREE_UPDATE', 
725                                                                                 [ ses(), list, true ],
726                                                                                 null,
727                                                                                 {
728                                                                                         'title' : 'Override Delete Failure?',
729                                                                                         'overridable_events' : [
730                                                                                         ]
731                                                                                 }
732                                                                         );
733                                                                         if (robj == null) throw(robj);
734                                                                         if (typeof robj.ilsevent != 'undefined') {
735                                                                                 if (robj.ilsevent == 1206 /* VOLUME_NOT_EMPTY */) {
736                                                                                         alert('You must delete all the copies on the volume before you may delete the volume itself.');
737                                                                                         return;
738                                                                                 }
739                                                                                 if (robj.ilsevent != 0) throw(robj);
740                                                                         }
741                                                                         alert('Volumes deleted.');
742                                                                 }
743                                                         } catch(E) {
744                                                                 obj.error.standard_unexpected_error_alert('copy status -> delete volumes',E);
745                                                         }
746
747                                                 }
748
749                                         ],
750                                         'cmd_mark_volume' : [
751                                                 ['command'],
752                                                 function() {
753                                                         try {
754                                                                 JSAN.use('util.functional');
755                                                                 var list = util.functional.map_list( obj.selection_list, function(o) { return o.acn_id; } );
756
757                                                                 if (list.length == 1) {
758                                                                         obj.data.marked_volume = list[0];
759                                                                         obj.data.stash('marked_volume');
760                                                                         alert('Volume marked as Item Transfer Destination');
761                                                                 } else {
762                                                                         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');
763                                                                 }
764                                                         } catch(E) {
765                                                                 obj.error.standard_unexpected_error_alert('copy status -> mark volume',E);
766                                                         }
767                                                 }
768                                         ],
769                                         'cmd_mark_library' : [
770                                                 ['command'],
771                                                 function() {
772                                                         try {
773                                                                 JSAN.use('util.functional');
774                                                                 var list = util.functional.map_list( obj.selection_list, function(o) { return o.acn_id; } );
775
776                                                                 if (list.length == 1) {
777                                                                         var v = obj.network.simple_request('FM_ACN_RETRIEVE',[list[0]]);
778                                                                         var owning_lib = v.owning_lib(); if (typeof owning_lib == 'object') owning_lib = owning_lib.id();
779
780                                                                         obj.data.marked_library = { 'lib' : owning_lib, 'docid' : v.record() };
781                                                                         obj.data.stash('marked_library');
782                                                                         alert('Library + Record marked as Volume Transfer Destination');
783                                                                 } else {
784                                                                         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');
785                                                                 }
786                                                         } catch(E) {
787                                                                 obj.error.standard_unexpected_error_alert('copy status -> mark library',E);
788                                                         }
789                                                 }
790                                         ],
791                                         'cmd_transfer_volume' : [
792                                                 ['command'],
793                                                 function() {
794                                                         try {
795                                                                         obj.data.stash_retrieve();
796                                                                         if (!obj.data.marked_library) {
797                                                                                 alert('Please mark a library as the destination from within holdings maintenance and then try this again.');
798                                                                                 return;
799                                                                         }
800                                                                         
801                                                                         JSAN.use('util.functional');
802
803                                                                         var list = util.functional.map_list( obj.selection_list, function(o) { return o.acn_id; } );
804                                                                         if (list.length == 0) return;
805
806                                                                         var map_acn = {};
807
808                                                                         for (var i = 0; i < list.length; i++) {
809                                                                                 var volume_id = list[i];
810                                                                                 if (volume_id == -1) continue; /* ignore magic pre-cat volume */
811                                                                                 if (! map_acn[volume_id]) {
812                                                                                         map_acn[ volume_id ] = obj.network.simple_request('FM_ACN_RETRIEVE',[ volume_id ]);
813                                                                                 }
814                                                                         }
815
816                                                                         list = []; for (v in map_acn) list.push(map_acn[v]);
817
818                                                                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
819                                                                         var xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: auto">';
820                                                                         xml += '<description>Transfer volumes ';
821
822                                                                         xml += util.functional.map_list(
823                                                                                 list,
824                                                                                 function (o) {
825                                                                                         return o.label();
826                                                                                 }
827                                                                         ).join(", ");
828
829                                                                         xml += ' to library ' + obj.data.hash.aou[ obj.data.marked_library.lib ].shortname();
830                                                                         xml += ' on the following record?</description>';
831                                                                         xml += '<hbox><button label="Transfer" name="fancy_submit"/>';
832                                                                         xml += '<button label="Cancel" accesskey="C" name="fancy_cancel"/></hbox>';
833                                                                         xml += '<iframe style="overflow: scroll" flex="1" src="' + urls.XUL_BIB_BRIEF + '?docid=' + obj.data.marked_library.docid + '"/>';
834                                                                         xml += '</vbox>';
835                                                                         JSAN.use('OpenILS.data');
836                                                                         //var data = new OpenILS.data(); data.init({'via':'stash'});
837                                                                         //data.temp_transfer = xml; data.stash('temp_transfer');
838                                                                         JSAN.use('util.window'); var win = new util.window();
839                                                                         var fancy_prompt_data = win.open(
840                                                                                 urls.XUL_FANCY_PROMPT,
841                                                                                 //+ '?xml_in_stash=temp_transfer'
842                                                                                 //+ '&title=' + window.escape('Volume Transfer'),
843                                                                                 'fancy_prompt', 'chrome,resizable,modal,width=500,height=300',
844                                                                                 { 'xml' : xml, 'title' : 'Volume Transfer' }
845                                                                         );
846                                                                 
847                                                                         if (fancy_prompt_data.fancy_status == 'incomplete') { alert('Transfer Aborted'); return; }
848
849                                                                         var robj = obj.network.simple_request(
850                                                                                 'FM_ACN_TRANSFER', 
851                                                                                 [ ses(), { 'docid' : obj.data.marked_library.docid, 'lib' : obj.data.marked_library.lib, 'volumes' : util.functional.map_list( list, function(o) { return o.id(); }) } ],
852                                                                                 null,
853                                                                                 {
854                                                                                         'title' : 'Override Volume Transfer Failure?',
855                                                                                         'overridable_events' : [
856                                                                                                 1208 /* TITLE_LAST_COPY */,
857                                                                                                 1219 /* COPY_REMOTE_CIRC_LIB */,
858                                                                                         ],
859                                                                                 }
860                                                                         );
861
862                                                                         if (typeof robj.ilsevent != 'undefined') {
863                                                                                 if (robj.ilsevent == 1221 /* ORG_CANNOT_HAVE_VOLS */) {
864                                                                                         alert('That destination cannot have volumes.');
865                                                                                 } else {
866                                                                                         throw(robj);
867                                                                                 }
868                                                                         } else {
869                                                                                 alert('Volumes transferred.');
870                                                                         }
871
872                                                         } catch(E) {
873                                                                 obj.error.standard_unexpected_error_alert('All volumes not likely transferred.',E);
874                                                         }
875                                                 }
876
877                                         ],
878                                 }
879                         }
880                 );
881                 this.controller.render();
882                 this.controller.view.copy_status_barcode_entry_textbox.focus();
883
884         },
885
886         'test_barcode' : function(bc) {
887                 var obj = this;
888                 var good = util.barcode.check(bc);
889                 var x = document.getElementById('strict_barcode');
890                 if (x && x.checked != true) return true;
891                 if (good) {
892                         return true;
893                 } else {
894                         if ( 1 == obj.error.yns_alert(
895                                                 'Bad checkdigit; possible mis-scan.  Use this barcode ("' + bc + '") anyway?',
896                                                 'Bad Barcode',
897                                                 'Cancel',
898                                                 'Accept Barcode',
899                                                 null,
900                                                 'Check here to confirm this action',
901                                                 '/xul/server/skin/media/images/bad_barcode.png'
902                         ) ) {
903                                 return true;
904                         } else {
905                                 return false;
906                         }
907                 }
908         },
909
910         'copy_status' : function(barcode,refresh) {
911                 var obj = this;
912                 try {
913                         try { document.getElementById('last_scanned').setAttribute('value',''); } catch(E) {}
914                         if (!barcode) barcode = obj.controller.view.copy_status_barcode_entry_textbox.value;
915                         if (!barcode) return;
916                         if (barcode) {
917                                 if ( obj.test_barcode(barcode) ) { /* good */ } else { /* bad */ return; }
918                         }
919                         JSAN.use('circ.util');
920                         function handle_req(req) {
921                                 try {
922                                         var details = req.getResultObject();
923                                         if (details == null) {
924                                                 throw('Something weird happened.  null result');
925                                         } else if (details.ilsevent) {
926                                                 switch(details.ilsevent) {
927                                                         case -1: 
928                                                                 obj.error.standard_network_error_alert(); 
929                                                                 obj.controller.view.copy_status_barcode_entry_textbox.select();
930                                                                 obj.controller.view.copy_status_barcode_entry_textbox.focus();
931                                                                 return;
932                                                         break;
933                                                         case 1502 /* ASSET_COPY_NOT_FOUND */ :
934                                                                 try { document.getElementById('last_scanned').setAttribute('value',barcode + ' was either mis-scanned or is not cataloged.'); } catch(E) {}
935                                                                 obj.error.yns_alert(barcode + ' was either mis-scanned or is not cataloged.','Not Cataloged','OK',null,null,'Check here to confirm this message');
936                                                                 obj.controller.view.copy_status_barcode_entry_textbox.select();
937                                                                 obj.controller.view.copy_status_barcode_entry_textbox.focus();
938                                                                 return;
939                                                         break;
940                                                         default: 
941                                                                 throw(details); 
942                                                         break;
943                                                 }
944                                         }
945                                         var msg = details.copy.barcode() + ' -- ';
946                                         if (details.copy.call_number() == -1) msg += 'Item is a Pre-Cat.  ';
947                                         if (details.hold) msg += 'Item is captured for a Hold.  ';
948                                         if (details.transit) msg += 'Item is in Transit.  ';
949                                         if (details.circ && ! details.circ.checkin_time()) msg += 'Item is circulating.  ';
950                                         try { document.getElementById('last_scanned').setAttribute('value',msg); } catch(E) {}
951                                         if (document.getElementById('trim_list')) {
952                                                 var x = document.getElementById('trim_list');
953                                                 if (x.checked) { obj.list.trim_list = 20; } else { obj.list.trim_list = null; }
954                                         }
955                                         var params = {
956                                                 'retrieve_id' : js2JSON( 
957                                                         { 
958                                                                 'renewable' : details.circ ? 't' : 'f', 
959                                                                 'copy_id' : details.copy.id(), 
960                                                                 'acn_id' : details.volume ? details.volume.id() : -1, 
961                                                                 'barcode' : barcode, 
962                                                                 'doc_id' : details.mvr ? details.mvr.doc_id() : null  
963                                                         } 
964                                                 ),
965                                                 'row' : {
966                                                         'my' : {
967                                                                 'mvr' : details.mvr,
968                                                                 'acp' : details.copy,
969                                                                 'acn' : details.volume,
970                                                                 'atc' : details.transit,
971                                                                 'circ' : details.circ,
972                                                                 'ahr' : details.hold,
973                                                         }
974                                                 },
975                                                 'to_top' : true,
976                                         };
977                                         if (!refresh) {
978                                                 var nparams = obj.list.append(params);
979                                                 if (!document.getElementById('trim_list').checked) {
980                                                         if (typeof obj.list_copyid_map[details.copy.id()] == 'undefined') obj.list_copyid_map[details.copy.id()] =[];
981                                                         obj.list_copyid_map[details.copy.id()].push(nparams);
982                                                 }
983                                         } else {
984                                                 if (!document.getElementById('trim_list').checked) {
985                             if (typeof obj.list_copyid_map[details.copy.id()] != 'undefined') {
986                                 for (var i = 0; i < obj.list_copyid_map[details.copy.id()].length; i++) {
987                                     if (typeof obj.list_copyid_map[details.copy.id()][i] == 'undefined') {
988                                         obj.list.append(params);
989                                     } else {
990                                         params.my_node = obj.list_copyid_map[details.copy.id()][i].my_node;
991                                         obj.list.refresh_row(params);
992                                     }
993                                 }
994                             } else {
995                                                             obj.list.append(params);
996                             }
997                                                 } else {
998                                                         obj.list.append(params);
999                                                 }
1000                                         }
1001                                 } catch(E) {
1002                                         obj.error.standard_unexpected_error_alert('barcode = ' + barcode,E);
1003                                 }
1004                         }
1005                         var result = obj.network.simple_request('FM_ACP_DETAILS_VIA_BARCODE', [ ses(), barcode ]);
1006                         handle_req({'getResultObject':function(){return result;}}); // used to be async
1007                         obj.controller.view.copy_status_barcode_entry_textbox.value = '';
1008                         obj.controller.view.copy_status_barcode_entry_textbox.focus();
1009                         
1010                 } catch(E) {
1011                         obj.error.standard_unexpected_error_alert('barcode = ' + barcode,E);
1012                         obj.controller.view.copy_status_barcode_entry_textbox.select();
1013                         obj.controller.view.copy_status_barcode_entry_textbox.focus();
1014                 }
1015
1016         },
1017         
1018         'spawn_copy_editor' : function() {
1019
1020                 var obj = this;
1021
1022                 JSAN.use('util.functional');
1023
1024                 var list = obj.selection_list;
1025
1026                 list = util.functional.map_list(
1027                         list,
1028                         function (o) {
1029                                 return o.copy_id;
1030                         }
1031                 );
1032
1033                 JSAN.use('cat.util'); cat.util.spawn_copy_editor( { 'copy_ids' : list, 'edit' : 1 } );
1034
1035         },
1036
1037 }
1038
1039 dump('exiting circ.copy_status.js\n');