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