]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/copy_buckets.js
print tweaks
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / copy_buckets.js
1 dump('entering cat.copy_buckets.js\n');
2
3 if (typeof cat == 'undefined') cat = {};
4 cat.copy_buckets = 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.date');
9         JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init({'via':'stash'});
10 }
11
12 cat.copy_buckets.prototype = {
13         'selection_list1' : [],
14         'selection_list2' : [],
15         'bucket_id_name_map' : {},
16
17         'render_pending_copies' : function() {
18                 var obj = this;
19                 obj.list1.clear();
20                 for (var i = 0; i < obj.copy_ids.length; i++) {
21                         var item = obj.flesh_item_for_list( obj.copy_ids[i] );
22                         if (item) obj.list1.append( item );
23                 }
24         },
25
26         'init' : function( params ) {
27
28                 var obj = this;
29
30                 obj.copy_ids = params['copy_ids'] || [];
31
32                 JSAN.use('circ.util');
33                 var columns = circ.util.columns( 
34                         { 
35                                 'barcode' : { 'hidden' : false },
36                                 'title' : { 'hidden' : false },
37                                 'location' : { 'hidden' : false },
38                                 'call_number' : { 'hidden' : false },
39                                 'status' : { 'hidden' : false },
40                         } 
41                 );
42
43                 JSAN.use('util.list'); 
44
45                 obj.list1 = new util.list('pending_copies_list');
46                 obj.list1.init(
47                         {
48                                 'columns' : columns,
49                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
50                                 'on_select' : function(ev) {
51                                         try {
52                                                 JSAN.use('util.functional');
53                                                 var sel = obj.list1.retrieve_selection();
54                                                 obj.selection_list1 = util.functional.map_list(
55                                                         sel,
56                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
57                                                 );
58                                                 obj.error.sdump('D_TRACE','circ/copy_buckets: selection list 1 = ' + js2JSON(obj.selection_list1) );
59                                                 if (obj.selection_list1.length == 0) {
60                                                         obj.controller.view.copy_buckets_sel_add.disabled = true;
61                                                 } else {
62                                                         obj.controller.view.copy_buckets_sel_add.disabled = false;
63                                                 }
64                                         } catch(E) {
65                                                 alert('FIXME: ' + E);
66                                         }
67                                 },
68
69                         }
70                 );
71
72                 obj.render_pending_copies();
73         
74                 obj.list2 = new util.list('copies_in_bucket_list');
75                 obj.list2.init(
76                         {
77                                 'columns' : columns,
78                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
79                                 'on_select' : function(ev) {
80                                         try {
81                                                 JSAN.use('util.functional');
82                                                 var sel = obj.list2.retrieve_selection();
83                                                 document.getElementById('clip_button').disabled = sel.length < 1;
84                                                 obj.selection_list2 = util.functional.map_list(
85                                                         sel,
86                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
87                                                 );
88                                                 obj.error.sdump('D_TRACE','circ/copy_buckets: selection list 2 = ' + js2JSON(obj.selection_list2) );
89                                                 if (obj.selection_list2.length == 0) {
90                                                         obj.controller.view.copy_buckets_delete_item.disabled = true;
91                                                         obj.controller.view.copy_buckets_delete_item.setAttribute('disabled','true');
92                                                         obj.controller.view.copy_buckets_export.disabled = true;
93                                                         obj.controller.view.copy_buckets_export.setAttribute('disabled','true');
94                                                 } else {
95                                                         obj.controller.view.copy_buckets_delete_item.disabled = false;
96                                                         obj.controller.view.copy_buckets_delete_item.setAttribute('disabled','false');
97                                                         obj.controller.view.copy_buckets_export.disabled = false;
98                                                         obj.controller.view.copy_buckets_export.setAttribute('disabled','false');
99                                                 }
100                                         } catch(E) {
101                                                 alert('FIXME: ' + E);
102                                         }
103                                 },
104                         }
105                 );
106                 
107                 JSAN.use('util.controller'); obj.controller = new util.controller();
108                 obj.controller.init(
109                         {
110                                 'control_map' : {
111                                         'sel_clip' : [
112                                                 ['command'],
113                                                 function() { obj.list2.clipboard(); }
114                                         ],
115                                         'copy_buckets_menulist_placeholder' : [
116                                                 ['render'],
117                                                 function(e) {
118                                                         return function() {
119                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
120                                                                 var items = [ ['Choose a bucket...',''] ].concat(
121                                                                         util.functional.map_list(
122                                                                                 obj.network.simple_request(
123                                                                                         'BUCKET_RETRIEVE_VIA_USER',
124                                                                                         [ ses(), obj.data.list.au[0].id() ]
125                                                                                 ).copy,
126                                                                                 function(o) {
127                                                                                         obj.bucket_id_name_map[ o.id() ] = o.name();
128                                                                                         return [ o.name(), o.id() ];
129                                                                                 }
130                                                                         )
131                                                                 );
132                                                                 g.error.sdump('D_TRACE','items = ' + js2JSON(items));
133                                                                 util.widgets.remove_children( e );
134                                                                 var ml = util.widgets.make_menulist(
135                                                                         items
136                                                                 );
137                                                                 e.appendChild( ml );
138                                                                 ml.setAttribute('id','bucket_menulist');
139                                                                 ml.setAttribute('accesskey','');
140
141                                                                 function change_bucket(ev) {
142                                                                         var bucket_id = ev.target.value;
143                                                                         if (!bucket_id) return;
144                                                                         var bucket = obj.network.simple_request(
145                                                                                 'BUCKET_FLESH',
146                                                                                 [ ses(), 'copy', bucket_id ]
147                                                                         );
148                                                                         var items = bucket.items() || [];
149                                                                         obj.list2.clear();
150                                                                         for (var i = 0; i < items.length; i++) {
151                                                                                 var item = obj.flesh_item_for_list( 
152                                                                                         items[i].target_copy(),
153                                                                                         items[i].id()
154                                                                                 );
155                                                                                 if (item) obj.list2.append( item );
156                                                                         }
157                                                                 }
158
159                                                                 ml.addEventListener( 'change_bucket', change_bucket , false);
160                                                                 ml.addEventListener( 'command', function() {
161                                                                         JSAN.use('util.widgets'); util.widgets.dispatch('change_bucket',ml);
162                                                                 }, false);
163                                                                 obj.controller.view.bucket_menulist = ml;
164                                                                 JSAN.use('util.widgets'); util.widgets.dispatch('change_bucket',ml);
165                                                         };
166                                                 },
167                                         ],
168
169                                         'copy_buckets_add' : [
170                                                 ['command'],
171                                                 function() {
172                                                         var bucket_id = obj.controller.view.bucket_menulist.value;
173                                                         if (!bucket_id) return;
174                                                         for (var i = 0; i < obj.copy_ids.length; i++) {
175                                                                 var bucket_item = new ccbi();
176                                                                 bucket_item.isnew('1');
177                                                                 bucket_item.bucket(bucket_id);
178                                                                 bucket_item.target_copy( obj.copy_ids[i] );
179                                                                 try {
180                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_CREATE',
181                                                                                 [ ses(), 'copy', bucket_item ]);
182
183                                                                         if (typeof robj == 'object') throw robj;
184
185                                                                         var item = obj.flesh_item_for_list( obj.copy_ids[i], robj );
186                                                                         if (!item) continue;
187
188                                                                         obj.list2.append( item );
189                                                                 } catch(E) {
190                                                                         obj.error.standard_unexpected_error_alert('Addition likely failed.',E);
191                                                                 }
192                                                         }
193                                                 }
194                                         ],
195                                         'copy_buckets_sel_add' : [
196                                                 ['command'],
197                                                 function() {                                                        
198                                                         var bucket_id = obj.controller.view.bucket_menulist.value;
199                                                         if (!bucket_id) return;
200                                                         for (var i = 0; i < obj.selection_list1.length; i++) {
201                                                                 var acp_id = obj.selection_list1[i][0];
202                                                                 //var barcode = obj.selection_list1[i][1];
203                                                                 var bucket_item = new ccbi();
204                                                                 bucket_item.isnew('1');
205                                                                 bucket_item.bucket(bucket_id);
206                                                                 bucket_item.target_copy( acp_id );
207                                                                 try {
208                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_CREATE',
209                                                                                 [ ses(), 'copy', bucket_item ]);
210
211                                                                         if (typeof robj == 'object') throw robj;
212
213                                                                         var item = obj.flesh_item_for_list( acp_id, robj );
214                                                                         if (!item) continue;
215
216                                                                         obj.list2.append( item );
217                                                                 } catch(E) {
218                                                                         obj.error.standard_unexpected_error_alert('Deletion likely failed.',E);
219                                                                 }
220                                                         }
221
222                                                 }
223                                         ],
224                                         'copy_buckets_export' : [
225                                                 ['command'],
226                                                 function() {                                                        
227                                                         for (var i = 0; i < obj.selection_list2.length; i++) {
228                                                                 var acp_id = obj.selection_list2[i][0];
229                                                                 //var barcode = obj.selection_list1[i][1];
230                                                                 //var bucket_item_id = obj.selection_list1[i][2];
231                                                                 var item = obj.flesh_item_for_list( acp_id );
232                                                                 if (item) {
233                                                                         obj.list1.append( item );
234                                                                         obj.copy_ids.push( acp_id );
235                                                                 }
236                                                         }
237                                                 }
238                                         ],
239
240                                         'copy_buckets_delete_item' : [
241                                                 ['command'],
242                                                 function() {
243                                                         for (var i = 0; i < obj.selection_list2.length; i++) {
244                                                                 try {
245                                                                         //var acp_id = obj.selection_list2[i][0];
246                                                                         //var barcode = obj.selection_list2[i][1];
247                                                                         var bucket_item_id = obj.selection_list2[i][2];
248                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_DELETE',
249                                                                                 [ ses(), 'copy', bucket_item_id ]);
250                                                                         if (typeof robj == 'object') throw robj;
251                                                                 } catch(E) {
252                                                                         obj.error.standard_unexpected_error_alert('Deletion likely failed.',E);
253                                                                 }
254                                                         }
255                                                         setTimeout(
256                                                                 function() {
257                                                                         JSAN.use('util.widgets'); 
258                                                                         util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
259                                                                 }, 0
260                                                         );
261                                                 }
262                                         ],
263                                         'copy_buckets_delete_bucket' : [
264                                                 ['command'],
265                                                 function() {
266                                                         try {
267                                                                 var bucket = obj.controller.view.bucket_menulist.value;
268                                                                 var name = obj.bucket_id_name_map[ bucket ];
269                                                                 var conf = window.confirm('Delete the bucket named ' + name + '?');
270                                                                 if (!conf) return;
271                                                                 obj.list2.clear();
272                                                                 var robj = obj.network.simple_request('BUCKET_DELETE',[ses(),'copy',bucket]);
273                                                                 if (typeof robj == 'object') throw robj;
274                                                                 obj.controller.render('copy_buckets_menulist_placeholder');
275                                                         } catch(E) {
276                                                                 obj.error.standard_unexpected_error_alert('Bucket deletion likely failed.',E);
277                                                         }
278                                                 }
279                                         ],
280                                         'copy_buckets_new_bucket' : [
281                                                 ['command'],
282                                                 function() {
283                                                         try {
284                                                                 var name = prompt('What would you like to name the bucket?','','Bucket Creation');
285
286                                                                 if (name) {
287                                                                         var bucket = new ccb();
288                                                                         bucket.btype('staff_client');
289                                                                         bucket.owner( obj.data.list.au[0].id() );
290                                                                         bucket.name( name );
291
292                                                                         var robj = obj.network.simple_request('BUCKET_CREATE',[ses(),'copy',bucket]);
293
294                                                                         if (typeof robj == 'object') throw robj;
295
296                                                                         obj.controller.render('copy_buckets_menulist_placeholder');
297                                                                         obj.controller.view.bucket_menulist.value = robj;
298                                                                         setTimeout(
299                                                                                 function() {
300                                                                                         JSAN.use('util.widgets'); 
301                                                                                         util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
302                                                                                 }, 0
303                                                                         );
304                                                                 }
305                                                         } catch(E) {
306                                                                 obj.error.standard_unexpected_error_alert('Bucket creation failed.',E);
307                                                         }
308                                                 }
309                                         ],
310                                         'copy_buckets_batch_copy_edit' : [
311                                                 ['command'],
312                                                 function() {
313                                                         try {
314
315                                                                 obj.list2.select_all();
316                                                         
317                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
318
319                                                                 var list = util.functional.map_list(
320                                                                         obj.list2.dump_retrieve_ids(),
321                                                                         function (o) {
322                                                                                 return JSON2js(o)[0]; // acp_id
323                                                                         }
324                                                                 );
325
326                                                                 var copies = util.functional.map_list(
327                                                                         list,
328                                                                         function (acp_id) {
329                                                                                 return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
330                                                                         }
331                                                                 );
332
333                                                                 var edit = 0;
334                                                                 try {
335                                                                         edit = obj.network.request(
336                                                                                 api.PERM_MULTI_ORG_CHECK.app,
337                                                                                 api.PERM_MULTI_ORG_CHECK.method,
338                                                                                 [ 
339                                                                                         ses(), 
340                                                                                         obj.data.list.au[0].id(), 
341                                                                                         util.functional.map_list(
342                                                                                                 copies,
343                                                                                                 function (o) {
344                                                                                                         return obj.network.simple_request('FM_ACN_RETRIEVE',[o.call_number()]).owning_lib();
345                                                                                                 }
346                                                                                         ),
347                                                                                         [ 'UPDATE_COPY', 'UPDATE_BATCH_COPY' ]
348                                                                                 ]
349                                                                         ).length == 0 ? 1 : 0;
350                                                                 } catch(E) {
351                                                                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
352                                                                 }
353
354                                                                 JSAN.use('cat.util'); cat.util.spawn_copy_editor(list,edit);
355
356                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
357                                                                 setTimeout(
358                                                                         function() {
359                                                                                 JSAN.use('util.widgets'); 
360                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
361                                                                         }, 0
362                                                                 );
363                                                         } catch(E) {
364                                                                 alert( js2JSON(E) );
365                                                         }
366                                                 }
367                                         ],
368                                         'copy_buckets_batch_copy_delete' : [
369                                                 ['command'],
370                                                 function() {
371                                                         try {
372                                                         
373                                                                 obj.list2.select_all();
374
375                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
376
377                                                                 var list = util.functional.map_list(
378                                                                         obj.list2.dump_retrieve_ids(),
379                                                                         function (o) {
380                                                                                 return JSON2js(o)[0]; // acp_id
381                                                                         }
382                                                                 );
383
384                                                                 var copies = util.functional.map_list(
385                                                                         list,
386                                                                         function (acp_id) {
387                                                                                 return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
388                                                                         }
389                                                                 );
390
391                                                                 for (var i = 0; i < copies.length; i++) {
392                                                                         copies[i].ischanged(1);
393                                                                         copies[i].isdeleted(1);
394                                                                 }
395
396                                                                 var robj = obj.network.simple_request('FM_ACP_FLESHED_BATCH_UPDATE',[ ses(), copies, true]);
397                                                                 if (typeof robj.ilsevent != 'undefined') obj.error.standard_unexpected_error_alert('Batch Item Deletion',robj);
398
399                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
400                                                                 setTimeout(
401                                                                         function() {
402                                                                                 JSAN.use('util.widgets'); 
403                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
404                                                                         }, 0
405                                                                 );
406                                                         } catch(E) {
407                                                                 alert( js2JSON(E) );
408                                                         }
409                                                 }
410                                         ],
411
412                                         'copy_buckets_transfer_to_volume' : [
413                                                 ['command'],
414                                                 function() {
415                                                         try {
416                                                                 obj.list2.select_all();
417
418                                                                 obj.data.stash_retrieve();
419                                                                 if (!obj.data.marked_volume) {
420                                                                         alert('Please mark a volume as the destination from within the copy browser and then try this again.');
421                                                                         return;
422                                                                 }
423
424                                                                 var copy_ids = util.functional.map_list(
425                                                                         obj.list2.dump_retrieve_ids(),
426                                                                         function (o) {
427                                                                                 return JSON2js(o)[0]; // acp_id
428                                                                         }
429                                                                 )
430
431                                                                 var volume = obj.network.simple_request('FM_ACN_RETRIEVE',[ obj.data.marked_volume ]);
432
433                                                                 var msg = 'Transfer the items in bucket "';
434                                                                 msg += obj.controller.view.bucket_menulist.getAttribute('label') + '" ';
435                                                                 msg += 'from their original volumes to ';
436                                                                 msg += obj.data.hash.aou[ volume.owning_lib() ].shortname() + "'s volume labelled ";
437                                                                 msg += '"' + volume.label() + '" on the following record?';
438
439                                                                 JSAN.use('cat.util'); cat.util.transfer_copies( { 
440                                                                         'copy_ids' : copy_ids, 
441                                                                         'message' : msg, 
442                                                                         'docid' : volume.record(),
443                                                                         'volume_label' : volume.label(),
444                                                                         'owning_lib' : volume.owning_lib(),
445                                                                 } );
446
447                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
448                                                                 setTimeout(
449                                                                         function() {
450                                                                                 JSAN.use('util.widgets'); 
451                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
452                                                                         }, 0
453                                                                 );
454                                                                 
455                                                         } catch(E) {
456                                                                 obj.error.standard_unexpected_error_alert('Items not likely transferred.',E);
457                                                         }
458                                                 }
459                                         ],
460                                         'cmd_broken' : [
461                                                 ['command'],
462                                                 function() { alert('Not Yet Implemented'); }
463                                         ],
464                                         'cmd_copy_buckets_print' : [
465                                                 ['command'],
466                                                 function() {
467                                                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
468                                                         obj.list2.on_all_fleshed = function() {
469                                                                 try {
470                                                                         dump( js2JSON( obj.list2.dump_with_keys() ) + '\n' );
471                                                                         data.stash_retrieve();
472                                                                         var lib = data.hash.aou[ data.list.au[0].ws_ou() ];
473                                                                         lib.children(null);
474                                                                         var p = { 
475                                                                                 'lib' : lib,
476                                                                                 'staff' : data.list.au[0],
477                                                                                 'header' : data.print_list_templates.item_status.header,
478                                                                                 'line_item' : data.print_list_templates.item_status.line_item,
479                                                                                 'footer' : data.print_list_templates.item_status.footer,
480                                                                                 'type' : data.print_list_templates.item_status.type,
481                                                                                 'list' : obj.list2.dump_with_keys(),
482                                                                         };
483                                                                         JSAN.use('util.print'); var print = new util.print();
484                                                                         print.tree_list( p );
485                                                                         setTimeout(function(){obj.list2.on_all_fleshed = null;},0);
486                                                                 } catch(E) {
487                                                                         alert(E); 
488                                                                 }
489                                                         }
490                                                         obj.list2.full_retrieve();
491                                                 }
492                                         ],
493                                         'cmd_copy_buckets_reprint' : [
494                                                 ['command'],
495                                                 function() {
496                                                 }
497                                         ],
498                                         'cmd_copy_buckets_done' : [
499                                                 ['command'],
500                                                 function() {
501                                                         window.close();
502                                                 }
503                                         ],
504                                         'cmd_export_to_copy_status' : [
505                                                 ['command'],
506                                                 function() {
507                                                         try {
508                                                                 obj.list2.select_all();
509                                                                 JSAN.use('util.functional');
510                                                                 var barcodes = util.functional.map_list(
511                                                                         obj.list2.dump_retrieve_ids(),
512                                                                         function(o) { return JSON2js(o)[1]; }
513                                                                 );
514                                                                 var url = urls.XUL_COPY_STATUS + '?barcodes=' + window.escape( js2JSON(barcodes) );
515                                                                 xulG.new_tab( url, {}, {});
516                                                         } catch(E) {
517                                                                 obj.error.standard_unexpected_error_alert('Copy Status from Copy Buckets',E);
518                                                         }
519                                                 }
520                                         ],
521                                 }
522                         }
523                 );
524                 this.controller.render();
525
526                 if (typeof xulG == 'undefined') {
527                         obj.controller.view.cmd_export_to_copy_status.disabled = true;
528                         obj.controller.view.cmd_export_to_copy_status.setAttribute('disabled',true);
529                 } else {
530                         obj.controller.view.cmd_copy_buckets_done.disabled = true;
531                         obj.controller.view.cmd_copy_buckets_done.setAttribute('disabled',true);
532                 }
533         
534         },
535
536         'flesh_item_for_list' : function(acp_id,bucket_item_id) {
537                 var obj = this;
538                 try {
539                         var copy = obj.network.simple_request( 'FM_ACP_RETRIEVE', [ acp_id ]);
540                         if (copy == null || typeof copy.ilsevent != 'undefined') {
541                                 throw(copy);
542                         } else {
543                                 var item = {
544                                         'retrieve_id' : js2JSON( [ copy.id(), copy.barcode(), bucket_item_id ] ),
545                                         'row' : {
546                                                 'my' : {
547                                                         'mvr' : obj.network.simple_request('MODS_SLIM_RECORD_RETRIEVE_VIA_COPY', [ copy.id() ]),
548                                                         'acp' : copy,
549                                                 }
550                                         }
551                                 };
552                                 return item;
553                         }
554                 } catch(E) {
555                         obj.error.standard_unexpected_error_alert('List building failed.',E);
556                         return null;
557                 }
558
559         },
560         
561 }
562
563 dump('exiting cat.copy_buckets.js\n');