]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/admin/transit_list.js
remote auto_select for transit list and offline xact manager..robustify xact manager...
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / admin / transit_list.js
1 dump('entering admin.transit_list.js\n');
2
3 if (typeof admin == 'undefined') admin = {};
4 admin.transit_list = 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 admin.transit_list.prototype = {
13
14         'init' : function( params ) {
15
16                 var obj = this;
17
18                 obj.list_init();
19                 obj.controller_init();
20                 obj.kick_off();
21
22         },
23
24         'kick_off' : function() {
25                 var obj = this;
26                 try {
27                         obj.network.simple_request('FM_ATC_RETRIEVE_VIA_AOU',[ ses(), obj.data.list.au[ 0 ].ws_ou() ], 
28                                 function(req) {
29                                         try {
30                                                 var robj = req.getResultObject();
31                                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
32
33                                                 JSAN.use('util.exec'); 
34                                                 var exec = new util.exec(2);
35                                                 var exec2 = new util.exec(2);
36
37                                                 function gen_list_append(id,which_list) {
38                                                         return function() {
39                                                                 switch(which_list) {
40                                                                         case 0: obj.list.append( { 'row' : { 'my' : { 'transit_id' : id } }, 'no_auto_select' : true } ); break;
41                                                                         case 1: obj.list2.append( { 'row' : { 'my' : { 'transit_id' : id } }, 'no_auto_select' : true } ); break;
42                                                                 }
43                                                         };
44                                                 }
45
46                                                 var rows = []; var rows2 = [];
47
48                                                 for (var i = 0; i < robj.from.length; i++) {
49                                                         //get_transit(robj.from[i], 0);
50                                                         rows.push( gen_list_append(robj.from[i],0) );
51                                                 }
52
53                                                 for (var i = 0; i < robj.to.length; i++) {
54                                                         //get_transit(robj.to[i], 1);
55                                                         rows2.push( gen_list_append(robj.to[i],1) );
56                                                 }
57                                 
58                                                 exec.chain( rows );
59                                                 exec2.chain( rows2 );
60
61                                         } catch(E) {
62                                                 try { obj.error.standard_unexpected_error_alert('retrieving transits',E); } catch(F) { alert(E); }
63                                         }
64                                 }
65                         );
66                 } catch(E) {
67                         try { obj.error.standard_unexpected_error_alert('pre-retrieving transits',E); } catch(F) { alert(E); }
68                 }
69         },
70
71         'list_init' : function() {
72
73                 var obj = this;
74
75                 obj.selection_list = [];
76                 obj.selection_list2 = [];
77
78                 JSAN.use('circ.util'); 
79                 var columns = circ.util.transit_columns(
80                         {
81                                 'transit_source' : { 'hidden' : false },
82                                 'transit_source_send_time' : { 'hidden' : false },
83                                 'transit_dest_lib' : { 'hidden' : false },
84                                 'transit_item_barcode' : { 'hidden' : false },
85                                 'transit_item_title' : { 'hidden' : false },
86                         },
87                         {
88                                 'just_these' : [
89                                         'transit_id',
90                                         'transit_source',
91                                         'transit_source_send_time',
92                                         'transit_dest_lib',
93                                         'transit_item_barcode',
94                                         'transit_item_title',
95                                         'transit_item_author',
96                                         'transit_item_callnumber',
97                                         'transit_target_copy',
98                                 ]
99                         }
100                 ).concat( 
101                         circ.util.hold_columns(
102                                 {
103                                         'request_time' : { 'hidden' : false },
104                                 },
105                                 {
106                                         'just_these' : [
107                                                 'request_timestamp',
108                                                 'request_time',
109                                                 'capture_timestamp',
110                                                 'capture_time',
111                                                 'hold_type',
112                                                 'expire_time',
113                                                 'patron_first_given_name',
114                                                 'patron_family_name',
115                                                 'patron_barcode',
116                                         ],
117                                 }
118                         ) 
119                 );
120
121                 JSAN.use('util.list'); 
122                 obj.list = new util.list('transit_from');
123                 obj.list.init( 
124                         { 
125                                 'columns' : columns, 
126                                 'map_row_to_columns' : circ.util.std_map_row_to_columns(), 
127                                 'retrieve_row' : function(params) {
128                                         var row = params.row;
129                                         try {
130                                                 obj.get_transit_and_hold_and_run_func(
131                                                         row.my.transit_id,
132                                                         function(transit,hold) { return obj.get_rest_of_row_given_transit_and_hold(params,transit,hold); }
133                                                 );
134                                         } catch(E) {
135                                                 try { obj.error.standard_unexpected_error_alert('retrieving row',E); } catch(F) { alert(E); }
136                                         }
137                                 },
138                                 'on_select' : function(ev) {
139                                         try {
140                                                 JSAN.use('util.functional');
141                                                 var sel = obj.list.retrieve_selection();
142                                                 obj.selection_list = util.functional.map_list(
143                                                         sel,
144                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
145                                                 );
146                                                 obj.error.sdump('D_TRACE','admin.transit_list: selection list = ' + js2JSON(obj.selection_list) );
147                                                 if (obj.selection_list.length == 0) {
148                                                         obj.controller.view.sel_edit.setAttribute('disabled','true');
149                                                         obj.controller.view.sel_opac.setAttribute('disabled','true');
150                                                         obj.controller.view.sel_bucket.setAttribute('disabled','true');
151                                                         obj.controller.view.sel_copy_details.setAttribute('disabled','true');
152                                                         obj.controller.view.sel_patron.setAttribute('disabled','true');
153                                                         obj.controller.view.sel_transit_abort.setAttribute('disabled','true');
154                                                         obj.controller.view.sel_clip.setAttribute('disabled','true');
155                                                 } else {
156                                                         obj.controller.view.sel_edit.setAttribute('disabled','false');
157                                                         obj.controller.view.sel_opac.setAttribute('disabled','false');
158                                                         obj.controller.view.sel_patron.setAttribute('disabled','false');
159                                                         obj.controller.view.sel_bucket.setAttribute('disabled','false');
160                                                         obj.controller.view.sel_copy_details.setAttribute('disabled','false');
161                                                         obj.controller.view.sel_transit_abort.setAttribute('disabled','false');
162                                                         obj.controller.view.sel_clip.setAttribute('disabled','false');
163                                                 }
164                                         } catch(E) {
165                                                 alert('FIXME: ' + E);
166                                         }
167                                 },
168                         }
169                 );
170                 obj.list2 = new util.list('transit_to');
171                 obj.list2.init( 
172                         { 
173                                 'columns' : columns, 
174                                 'map_row_to_columns' : circ.util.std_map_row_to_columns(), 
175                                 'retrieve_row' : function(params) {
176                                         var row = params.row;
177                                         try {
178                                                 obj.get_transit_and_hold_and_run_func(
179                                                         row.my.transit_id,
180                                                         function(transit,hold) { return obj.get_rest_of_row_given_transit_and_hold(params,transit,hold); }
181                                                 );
182                                         } catch(E) {
183                                                 try { obj.error.standard_unexpected_error_alert('retrieving row',E); } catch(F) { alert(E); }
184                                         }
185                                 },
186                                 'on_select' : function(ev) {
187                                         try {
188                                                 JSAN.use('util.functional');
189                                                 var sel = obj.list2.retrieve_selection();
190                                                 obj.selection_list2 = util.functional.map_list(
191                                                         sel,
192                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
193                                                 );
194                                                 obj.error.sdump('D_TRACE','admin.transit_list: selection list2 = ' + js2JSON(obj.selection_list2) );
195                                                 if (obj.selection_list2.length == 0) {
196                                                         obj.controller.view.sel_edit2.setAttribute('disabled','true');
197                                                         obj.controller.view.sel_opac2.setAttribute('disabled','true');
198                                                         obj.controller.view.sel_bucket2.setAttribute('disabled','true');
199                                                         obj.controller.view.sel_copy_details2.setAttribute('disabled','true');
200                                                         obj.controller.view.sel_patron2.setAttribute('disabled','true');
201                                                         obj.controller.view.sel_transit_abort2.setAttribute('disabled','true');
202                                                         obj.controller.view.sel_clip2.setAttribute('disabled','true');
203                                                 } else {
204                                                         obj.controller.view.sel_edit2.setAttribute('disabled','false');
205                                                         obj.controller.view.sel_opac2.setAttribute('disabled','false');
206                                                         obj.controller.view.sel_patron2.setAttribute('disabled','false');
207                                                         obj.controller.view.sel_bucket2.setAttribute('disabled','false');
208                                                         obj.controller.view.sel_copy_details2.setAttribute('disabled','false');
209                                                         obj.controller.view.sel_transit_abort2.setAttribute('disabled','false');
210                                                         obj.controller.view.sel_clip2.setAttribute('disabled','false');
211                                                 }
212                                         } catch(E) {
213                                                 alert('FIXME: ' + E);
214                                         }
215                                 },
216                         }
217                 );
218
219         },
220
221         'get_transit_and_hold_and_run_func' : function (transit_id,do_this) {
222                 var obj = this;
223                 obj.network.simple_request('FM_ATC_RETRIEVE', [ ses(), transit_id ],
224                         function(req2) {
225                                 try {
226                                         var r_atc = req2.getResultObject();
227                                         if (typeof r_atc.ilsevent != 'undefined') throw(r_atc);
228
229                                         if (instanceOf(r_atc,atc)) {
230                                                 do_this(r_atc,null);
231                                         } else if (instanceOf(r_atc,ahtc)) {
232                                                 obj.network.simple_request('FM_AHR_RETRIEVE', [ ses(), r_atc.hold() ],
233                                                         function(req3) {
234                                                                 try {
235                                                                         var r_ahr = req3.getResultObject();
236                                                                         if (typeof r_ahr.ilsevent != 'undefined') throw(r_ahr);
237                                                                         if (instanceOf(r_ahr[0],ahr)) {
238                                                                                 do_this(r_atc,r_ahr[0]);
239                                                                         } else {
240                                                                                 throw(r_ahr);
241                                                                         }
242                                                                 } catch(E) {
243                                                                         try { obj.error.standard_unexpected_error_alert('retrieving hold id = ' + r_atc.hold() + ' for transit id = ' + transit_id,E); } catch(F) { alert(E); }
244                                                                 }
245                                                         }
246                                                 );
247                                         } else {
248                                                 throw(r_atc);
249                                         }
250
251                                 } catch(E) {
252                                         try { obj.error.standard_unexpected_error_alert('retrieving transit id = ' + transit_id,E); } catch(F) { alert(E); }
253                                 }
254                         }
255                 );
256         },
257
258         'get_rest_of_row_given_transit_and_hold' : function(params,transit,hold) {
259                 var obj = this;
260                 var row = params.row;
261
262                 row.my.atc = transit;
263                 if (hold) row.my.ahr = hold;
264
265                 obj.network.simple_request(
266                         'FM_ACP_RETRIEVE',
267                         [ row.my.atc.target_copy() ],
268                         function(req) {
269                                 try { 
270                                         var r_acp = req.getResultObject();
271                                         if (typeof r_acp.ilsevent != 'undefined') throw(r_acp);
272                                         row.my.acp = r_acp;
273
274                                         obj.network.simple_request(
275                                                 'FM_ACN_RETRIEVE',
276                                                 [ r_acp.call_number() ],
277                                                 function(req2) {
278                                                         try {
279                                                                 var r_acn = req2.getResultObject();
280                                                                 if (typeof r_acn.ilsevent != 'undefined') throw(r_acn);
281                                                                 row.my.acn = r_acn;
282
283                                                                 if (row.my.acn.record() > 0) {
284                                                                         obj.network.simple_request(
285                                                                                 'MODS_SLIM_RECORD_RETRIEVE',
286                                                                                 [ r_acn.record() ],
287                                                                                 function(req3) {
288                                                                                         try {
289                                                                                                 var r_mvr = req3.getResultObject();
290                                                                                                 if (typeof r_mvr.ilsevent != 'undefined') throw(r_mvr);
291                                                                                                 row.my.mvr = r_mvr;
292
293                                                                                                 params.row_node.setAttribute(
294                                                                                                         'retrieve_id', js2JSON( { 
295                                                                                                                 'copy_id' : row.my.acp ? row.my.acp.id() : null, 
296                                                                                                                 'doc_id' : row.my.mvr ? row.my.mvr.doc_id() : null,  
297                                                                                                                 'barcode' : row.my.acp ? row.my.acp.barcode() : null, 
298                                                                                                                 'acp_id' : row.my.acp ? row.my.acp.id() : null, 
299                                                                                                                 'acn_id' : row.my.acn ? row.my.acn.id() : null,  
300                                                                                                                 'atc_id' : row.my.atc ? row.my.atc.id() : null,  
301                                                                                                                 'ahr_id' : row.my.ahr ? row.my.ahr.id() : null,  
302                                                                                                         } )
303                                                                                                 );
304                                                                                                 if (typeof params.on_retrieve == 'function') {
305                                                                                                         params.on_retrieve(row);
306                                                                                                 }
307                                                                                         } catch(E) {
308                                                                                                 try { obj.error.standard_unexpected_error_alert('retrieving mvr',E); } catch(F) { alert(E); }
309                                                                                         }
310                                                                                 }
311                                                                         );
312                                                                 } else {
313                                                                         params.row_node.setAttribute(
314                                                                                 'retrieve_id', js2JSON( { 
315                                                                                         'copy_id' : row.my.acp ? row.my.acp.id() : null, 
316                                                                                         'doc_id' : row.my.mvr ? row.my.mvr.doc_id() : null,  
317                                                                                         'barcode' : row.my.acp ? row.my.acp.barcode() : null, 
318                                                                                         'acp_id' : row.my.acp ? row.my.acp.id() : null, 
319                                                                                         'acn_id' : row.my.acn ? row.my.acn.id() : null,  
320                                                                                         'atc_id' : row.my.atc ? row.my.atc.id() : null,  
321                                                                                         'ahr_id' : row.my.ahr ? row.my.ahr.id() : null,  
322                                                                                 } )
323                                                                         );
324                                                                         if (typeof params.on_retrieve == 'function') {
325                                                                                 params.on_retrieve(row);
326                                                                         }
327                                                                 }
328                                         
329                                                         } catch(E) {
330                                                                 try { obj.error.standard_unexpected_error_alert('retrieving acn',E); } catch(F) { alert(E); }
331                                                         }
332                                                 }
333                                         );
334
335
336                                 } catch(E) {
337                                         try { obj.error.standard_unexpected_error_alert('retrieving acp',E); } catch(F) { alert(E); }
338                                 }
339                         }
340                 );
341         },
342
343         'controller_init' : function() {
344                 var obj = this;
345
346                 JSAN.use('util.controller'); obj.controller = new util.controller();
347                 obj.controller.init(
348                         {
349                                 'control_map' : {
350                                         'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
351                                         'save_columns2' : [ [ 'command' ], function() { obj.list2.save_columns(); } ],
352                                         'sel_clip' : [ ['command'], function() { obj.list.clipboard(); } ],
353                                         'sel_clip2' : [ ['command'], function() { obj.list2.clipboard(); } ],
354                                         'sel_edit' : [ ['command'], function() { try { obj.spawn_copy_editor(0); } catch(E) { alert(E); } } ],
355                                         'sel_edit2' : [ ['command'], function() { try { obj.spawn_copy_editor(1); } catch(E) { alert(E); } } ],
356                                         'sel_opac' : [ ['command'], function() { JSAN.use('cat.util'); cat.util.show_in_opac(obj.selection_list); } ],
357                                         'sel_opac2' : [ ['command'], function() { JSAN.use('cat.util'); cat.util.show_in_opac(obj.selection_list2); } ],
358                                         'sel_transit_abort' : [ ['command'], function() { JSAN.use('circ.util'); circ.util.abort_transits(obj.selection_list); } ],
359                                         'sel_transit_abort2' : [ ['command'], function() { JSAN.use('circ.util'); circ.util.abort_transits(obj.selection_list2); } ],
360                                         'sel_patron' : [ ['command'], function() { JSAN.use('circ.util'); circ.util.show_last_few_circs(obj.selection_list); } ],
361                                         'sel_patron2' : [ ['command'], function() { JSAN.use('circ.util'); circ.util.show_last_few_circs(obj.selection_list2); } ],
362                                         'sel_copy_details' : [ ['command'], function() { JSAN.use('circ.util'); for (var i = 0; i < obj.selection_list.length; i++) { circ.util.show_copy_details( obj.selection_list[i].copy_id ); } } ],
363                                         'sel_copy_details2' : [ ['command'], function() { JSAN.use('circ.util'); for (var i = 0; i < obj.selection_list2.length; i++) { circ.util.show_copy_details( obj.selection_list2[i].copy_id ); } } ],
364                                         'sel_bucket' : [ ['command'], function() { JSAN.use('cat.util'); cat.util.add_copies_to_bucket(obj.selection_list); } ],
365                                         'sel_bucket2' : [ ['command'], function() { JSAN.use('cat.util'); cat.util.add_copies_to_bucket(obj.selection_list2); } ],
366                                         'cmd_print_list' : [ ['command'], function() { obj.print_list(0); } ],
367                                         'cmd_print_list2' : [ ['command'], function() { obj.print_list(1); } ],
368                                 }
369                         }
370                 );
371                 this.controller.render();
372
373         },
374
375         'print_list' : function(which_list) {
376                 var obj = this;
377                 try {
378
379                         var list = which_list == 0 ? obj.list : obj.list2;
380
381                         if (list.on_all_fleshed != null) {
382                                 var r = window.confirm('This list is busy retrieving/rendering rows for a pending action.  Would you like to abort the pending action and proceed?');
383                                 if (!r) return;
384                         }
385                         list.on_all_fleshed =
386                                 function() {
387                                         try {
388                                                 dump( js2JSON( list.dump_with_keys() ) + '\n' );
389                                                 obj.data.stash_retrieve();
390                                                 var lib = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
391                                                 lib.children(null);
392                                                 var p = { 
393                                                         'lib' : lib,
394                                                         'staff' : obj.data.list.au[0],
395                                                         'header' : obj.data.print_list_templates.transit_list.header,
396                                                         'line_item' : obj.data.print_list_templates.transit_list.line_item,
397                                                         'footer' : obj.data.print_list_templates.transit_list.footer,
398                                                         'type' : obj.data.print_list_templates.transit_list.type,
399                                                         'list' : list.dump_with_keys(),
400                                                 };
401                                                 JSAN.use('util.print'); var print = new util.print();
402                                                 print.tree_list( p );
403                                                 setTimeout(function(){ list.on_all_fleshed = null; },0);
404                                         } catch(E) {
405                                                 obj.error.standard_unexpected_error_alert('print',E); 
406                                         }
407                                 }
408                         list.full_retrieve();
409                 } catch(E) {
410                         obj.error.standard_unexpected_error_alert('print',E); 
411                 }
412         },
413         
414         'spawn_copy_editor' : function(which_list) {
415
416                 /* FIXME -  a lot of redundant calls here */
417
418                 var obj = this;
419
420                 JSAN.use('util.widgets'); JSAN.use('util.functional');
421
422                 var list = which_list == 0 ? obj.selection_list : obj.selection_list2;
423
424                 list = util.functional.map_list(
425                         list,
426                         function (o) {
427                                 return o.copy_id;
428                         }
429                 );
430
431                 var copies = util.functional.map_list(
432                         list,
433                         function (acp_id) {
434                                 return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
435                         }
436                 );
437
438                 var edit = 0;
439                 try {
440                         edit = obj.network.request(
441                                 api.PERM_MULTI_ORG_CHECK.app,
442                                 api.PERM_MULTI_ORG_CHECK.method,
443                                 [ 
444                                         ses(), 
445                                         obj.data.list.au[0].id(), 
446                                         util.functional.map_list(
447                                                 copies,
448                                                 function (o) {
449                                                         return o.call_number() == -1 ? o.circ_lib() : obj.network.simple_request('FM_ACN_RETRIEVE',[o.call_number()]).owning_lib();
450                                                 }
451                                         ),
452                                         copies.length == 1 ? [ 'UPDATE_COPY' ] : [ 'UPDATE_COPY', 'UPDATE_BATCH_COPY' ]
453                                 ]
454                         ).length == 0 ? 1 : 0;
455                 } catch(E) {
456                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
457                 }
458
459                 JSAN.use('cat.util'); cat.util.spawn_copy_editor(list,edit);
460
461         },
462
463 }
464
465 dump('exiting admin.transit_list.js\n');