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