]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/copy_buckets.js
6ea2c187d19810a46b1e4209f959af0896a38f96
[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                                                                         alert('Bucket "' + name + '" created.');
298
299                                                                         obj.controller.render('copy_buckets_menulist_placeholder');
300                                                                         obj.controller.view.bucket_menulist.value = robj;
301                                                                         setTimeout(
302                                                                                 function() {
303                                                                                         JSAN.use('util.widgets'); 
304                                                                                         util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
305                                                                                 }, 0
306                                                                         );
307                                                                 }
308                                                         } catch(E) {
309                                                                 obj.error.standard_unexpected_error_alert('Bucket creation failed.',E);
310                                                         }
311                                                 }
312                                         ],
313                                         'copy_buckets_batch_copy_edit' : [
314                                                 ['command'],
315                                                 function() {
316                                                         try {
317
318                                                                 obj.list2.select_all();
319                                                         
320                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
321
322                                                                 var list = util.functional.map_list(
323                                                                         obj.list2.dump_retrieve_ids(),
324                                                                         function (o) {
325                                                                                 return JSON2js(o)[0]; // acp_id
326                                                                         }
327                                                                 );
328
329                                                                 var copies = util.functional.map_list(
330                                                                         list,
331                                                                         function (acp_id) {
332                                                                                 return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
333                                                                         }
334                                                                 );
335
336                                                                 var edit = 0;
337                                                                 try {
338                                                                         edit = obj.network.request(
339                                                                                 api.PERM_MULTI_ORG_CHECK.app,
340                                                                                 api.PERM_MULTI_ORG_CHECK.method,
341                                                                                 [ 
342                                                                                         ses(), 
343                                                                                         obj.data.list.au[0].id(), 
344                                                                                         util.functional.map_list(
345                                                                                                 copies,
346                                                                                                 function (o) {
347                                                                                                         return obj.network.simple_request('FM_ACN_RETRIEVE',[o.call_number()]).owning_lib();
348                                                                                                 }
349                                                                                         ),
350                                                                                         copies.length == 1 ? [ 'UPDATE_COPY' ] : [ 'UPDATE_COPY', 'UPDATE_BATCH_COPY' ]
351                                                                                 ]
352                                                                         ).length == 0 ? 1 : 0;
353                                                                 } catch(E) {
354                                                                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
355                                                                 }
356
357                                                                 JSAN.use('cat.util'); cat.util.spawn_copy_editor(list,edit);
358
359                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
360                                                                 setTimeout(
361                                                                         function() {
362                                                                                 JSAN.use('util.widgets'); 
363                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
364                                                                         }, 0
365                                                                 );
366                                                         } catch(E) {
367                                                                 alert( js2JSON(E) );
368                                                         }
369                                                 }
370                                         ],
371                                         'copy_buckets_batch_copy_delete' : [
372                                                 ['command'],
373                                                 function() {
374                                                         try {
375                                                         
376                                                                 obj.list2.select_all();
377
378                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
379
380                                                                 var list = util.functional.map_list(
381                                                                         obj.list2.dump_retrieve_ids(),
382                                                                         function (o) {
383                                                                                 return JSON2js(o)[0]; // acp_id
384                                                                         }
385                                                                 );
386
387                                                                 var copies = util.functional.map_list(
388                                                                         list,
389                                                                         function (acp_id) {
390                                                                                 return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
391                                                                         }
392                                                                 );
393
394                                                                 for (var i = 0; i < copies.length; i++) {
395                                                                         copies[i].ischanged(1);
396                                                                         copies[i].isdeleted(1);
397                                                                 }
398
399                                                                 var robj = obj.network.simple_request('FM_ACP_FLESHED_BATCH_UPDATE',[ ses(), copies, true]);
400                                                                 if (typeof robj.ilsevent != 'undefined') obj.error.standard_unexpected_error_alert('Batch Item Deletion',robj);
401
402                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
403                                                                 setTimeout(
404                                                                         function() {
405                                                                                 JSAN.use('util.widgets'); 
406                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
407                                                                         }, 0
408                                                                 );
409                                                         } catch(E) {
410                                                                 alert( js2JSON(E) );
411                                                         }
412                                                 }
413                                         ],
414
415                                         'copy_buckets_transfer_to_volume' : [
416                                                 ['command'],
417                                                 function() {
418                                                         try {
419                                                                 obj.list2.select_all();
420
421                                                                 obj.data.stash_retrieve();
422                                                                 if (!obj.data.marked_volume) {
423                                                                         alert('Please mark a volume as the destination from within the copy browser and then try this again.');
424                                                                         return;
425                                                                 }
426
427                                                                 var copy_ids = util.functional.map_list(
428                                                                         obj.list2.dump_retrieve_ids(),
429                                                                         function (o) {
430                                                                                 return JSON2js(o)[0]; // acp_id
431                                                                         }
432                                                                 )
433
434                                                                 var volume = obj.network.simple_request('FM_ACN_RETRIEVE',[ obj.data.marked_volume ]);
435
436                                                                 var msg = 'Transfer the items in bucket "';
437                                                                 msg += obj.controller.view.bucket_menulist.getAttribute('label') + '" ';
438                                                                 msg += 'from their original volumes to ';
439                                                                 msg += obj.data.hash.aou[ volume.owning_lib() ].shortname() + "'s volume labelled ";
440                                                                 msg += '"' + volume.label() + '" on the following record?';
441
442                                                                 JSAN.use('cat.util'); cat.util.transfer_copies( { 
443                                                                         'copy_ids' : copy_ids, 
444                                                                         'message' : msg, 
445                                                                         'docid' : volume.record(),
446                                                                         'volume_label' : volume.label(),
447                                                                         'owning_lib' : volume.owning_lib(),
448                                                                 } );
449
450                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
451                                                                 setTimeout(
452                                                                         function() {
453                                                                                 JSAN.use('util.widgets'); 
454                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
455                                                                         }, 0
456                                                                 );
457                                                                 
458                                                         } catch(E) {
459                                                                 obj.error.standard_unexpected_error_alert('Items not likely transferred.',E);
460                                                         }
461                                                 }
462                                         ],
463                                         'cmd_broken' : [
464                                                 ['command'],
465                                                 function() { alert('Not Yet Implemented'); }
466                                         ],
467                                         'cmd_copy_buckets_print' : [
468                                                 ['command'],
469                                                 function() {
470                                                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
471                                                         obj.list2.on_all_fleshed = function() {
472                                                                 try {
473                                                                         dump( js2JSON( obj.list2.dump_with_keys() ) + '\n' );
474                                                                         data.stash_retrieve();
475                                                                         var lib = data.hash.aou[ data.list.au[0].ws_ou() ];
476                                                                         lib.children(null);
477                                                                         var p = { 
478                                                                                 'lib' : lib,
479                                                                                 'staff' : data.list.au[0],
480                                                                                 'header' : data.print_list_templates.item_status.header,
481                                                                                 'line_item' : data.print_list_templates.item_status.line_item,
482                                                                                 'footer' : data.print_list_templates.item_status.footer,
483                                                                                 'type' : data.print_list_templates.item_status.type,
484                                                                                 'list' : obj.list2.dump_with_keys(),
485                                                                         };
486                                                                         JSAN.use('util.print'); var print = new util.print();
487                                                                         print.tree_list( p );
488                                                                         setTimeout(function(){obj.list2.on_all_fleshed = null;},0);
489                                                                 } catch(E) {
490                                                                         alert(E); 
491                                                                 }
492                                                         }
493                                                         obj.list2.full_retrieve();
494                                                 }
495                                         ],
496                                         'cmd_copy_buckets_reprint' : [
497                                                 ['command'],
498                                                 function() {
499                                                 }
500                                         ],
501                                         'cmd_copy_buckets_done' : [
502                                                 ['command'],
503                                                 function() {
504                                                         window.close();
505                                                 }
506                                         ],
507                                         'cmd_export_to_copy_status' : [
508                                                 ['command'],
509                                                 function() {
510                                                         try {
511                                                                 obj.list2.select_all();
512                                                                 JSAN.use('util.functional');
513                                                                 var barcodes = util.functional.map_list(
514                                                                         obj.list2.dump_retrieve_ids(),
515                                                                         function(o) { return JSON2js(o)[1]; }
516                                                                 );
517                                                                 var url = urls.XUL_COPY_STATUS + '?barcodes=' + window.escape( js2JSON(barcodes) );
518                                                                 xulG.new_tab( url, {}, {});
519                                                         } catch(E) {
520                                                                 obj.error.standard_unexpected_error_alert('Copy Status from Copy Buckets',E);
521                                                         }
522                                                 }
523                                         ],
524                                 }
525                         }
526                 );
527                 this.controller.render();
528
529                 if (typeof xulG == 'undefined') {
530                         obj.controller.view.cmd_export_to_copy_status.disabled = true;
531                         obj.controller.view.cmd_export_to_copy_status.setAttribute('disabled',true);
532                 } else {
533                         obj.controller.view.cmd_copy_buckets_done.disabled = true;
534                         obj.controller.view.cmd_copy_buckets_done.setAttribute('disabled',true);
535                 }
536         
537         },
538
539         'flesh_item_for_list' : function(acp_id,bucket_item_id) {
540                 var obj = this;
541                 try {
542                         var copy = obj.network.simple_request( 'FM_ACP_RETRIEVE', [ acp_id ]);
543                         if (copy == null || typeof copy.ilsevent != 'undefined') {
544                                 throw(copy);
545                         } else {
546                                 var item = {
547                                         'retrieve_id' : js2JSON( [ copy.id(), copy.barcode(), bucket_item_id ] ),
548                                         'row' : {
549                                                 'my' : {
550                                                         'mvr' : obj.network.simple_request('MODS_SLIM_RECORD_RETRIEVE_VIA_COPY', [ copy.id() ]),
551                                                         'acp' : copy,
552                                                 }
553                                         }
554                                 };
555                                 return item;
556                         }
557                 } catch(E) {
558                         obj.error.standard_unexpected_error_alert('List building failed.',E);
559                         return null;
560                 }
561
562         },
563         
564 }
565
566 dump('exiting cat.copy_buckets.js\n');