mitigate update/display race
[Evergreen.git] / Open-ILS / xul / staff_client / server / circ / copy_status.js
1 dump('entering circ.copy_status.js\n');
2
3 if (typeof circ == 'undefined') circ = {};
4 circ.copy_status = 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 circ.copy_status.prototype = {
13         'selection_list' : [],
14
15         'init' : function( params ) {
16
17                 var obj = this;
18
19                 JSAN.use('circ.util');
20                 var columns = circ.util.columns( 
21                         { 
22                                 'barcode' : { 'hidden' : false },
23                                 'title' : { 'hidden' : false },
24                                 'location' : { 'hidden' : false },
25                                 'call_number' : { 'hidden' : false },
26                                 'status' : { 'hidden' : false },
27                                 'alert_message' : { 'hidden' : false },
28                                 'due_date' : { 'hidden' : false },
29                         },
30                         {
31                                 'except_these' : [
32                                         'checkin_time', 'checkin_time_full', 'route_to', 'message', 'uses', 'xact_finish',
33                                 ],
34                         }
35                 );
36
37                 JSAN.use('util.list'); obj.list = new util.list('copy_status_list');
38                 obj.list.init(
39                         {
40                                 'columns' : columns,
41                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
42                                 'on_select' : function(ev) {
43                                         try {
44                                                 JSAN.use('util.functional');
45                                                 var sel = obj.list.retrieve_selection();
46                                                 obj.selection_list = util.functional.map_list(
47                                                         sel,
48                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
49                                                 );
50                                                 obj.error.sdump('D_TRACE','circ/copy_status: selection list = ' + js2JSON(obj.selection_list) );
51                                                 if (obj.selection_list.length == 0) {
52                                                         obj.controller.view.sel_checkin.setAttribute('disabled','true');
53                                                         obj.controller.view.cmd_replace_barcode.setAttribute('disabled','true');
54                                                         obj.controller.view.sel_edit.setAttribute('disabled','true');
55                                                         obj.controller.view.sel_opac.setAttribute('disabled','true');
56                                                         obj.controller.view.sel_bucket.setAttribute('disabled','true');
57                                                         obj.controller.view.sel_copy_details.setAttribute('disabled','true');
58                                                         obj.controller.view.sel_mark_items_damaged.setAttribute('disabled','true');
59                                                         obj.controller.view.sel_mark_items_missing.setAttribute('disabled','true');
60                                                         obj.controller.view.sel_patron.setAttribute('disabled','true');
61                                                         obj.controller.view.sel_spine.setAttribute('disabled','true');
62                                                         obj.controller.view.sel_transit_abort.setAttribute('disabled','true');
63                                                         obj.controller.view.sel_clip.setAttribute('disabled','true');
64                                                         obj.controller.view.sel_renew.setAttribute('disabled','true');
65                                                 } else {
66                                                         obj.controller.view.sel_checkin.setAttribute('disabled','false');
67                                                         obj.controller.view.cmd_replace_barcode.setAttribute('disabled','false');
68                                                         obj.controller.view.sel_edit.setAttribute('disabled','false');
69                                                         obj.controller.view.sel_opac.setAttribute('disabled','false');
70                                                         obj.controller.view.sel_patron.setAttribute('disabled','false');
71                                                         obj.controller.view.sel_bucket.setAttribute('disabled','false');
72                                                         obj.controller.view.sel_copy_details.setAttribute('disabled','false');
73                                                         obj.controller.view.sel_mark_items_damaged.setAttribute('disabled','false');
74                                                         obj.controller.view.sel_mark_items_missing.setAttribute('disabled','false');
75                                                         obj.controller.view.sel_spine.setAttribute('disabled','false');
76                                                         obj.controller.view.sel_transit_abort.setAttribute('disabled','false');
77                                                         obj.controller.view.sel_clip.setAttribute('disabled','false');
78                                                         obj.controller.view.sel_renew.setAttribute('disabled','false');
79                                                 }
80                                         } catch(E) {
81                                                 alert('FIXME: ' + E);
82                                         }
83                                 },
84                         }
85                 );
86                 
87                 JSAN.use('util.controller'); obj.controller = new util.controller();
88                 obj.controller.init(
89                         {
90                                 'control_map' : {
91                                         'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
92                                         'sel_clip' : [
93                                                 ['command'],
94                                                 function() { obj.list.clipboard(); }
95                                         ],
96                                         'sel_checkin' : [
97                                                 ['command'],
98                                                 function() {
99                                                         try {
100                                                                 JSAN.use('circ.util');
101                                                                 for (var i = 0; i < obj.selection_list.length; i++) {
102                                                                         var barcode = obj.selection_list[i].barcode;
103                                                                         var checkin = circ.util.checkin_via_barcode( ses(), barcode );
104                                                                         setTimeout( function(a) { return function() { obj.copy_status( a ); }; }(barcode), 0);
105                                                                 }
106                                                                 alert('Action complete.');
107                                                         } catch(E) {
108                                                                 obj.error.standard_unexpected_error_alert('Checkin did not likely happen.',E);
109                                                         }
110                                                 }
111                                         ],
112                                         'cmd_replace_barcode' : [
113                                                 ['command'],
114                                                 function() {
115                                                         try {
116                                                                 JSAN.use('cat.util');
117                                                                 for (var i = 0; i < obj.selection_list.length; i++) {
118                                                                         try { 
119                                                                                 var barcode = obj.selection_list[i].barcode;
120                                                                                 var new_bc = cat.util.replace_barcode( barcode );
121                                                                                 setTimeout( function(a) { return function() { obj.copy_status( a ); }; }(new_bc), 0);
122                                                                         } catch(E) {
123                                                                                 obj.error.standard_unexpected_error_alert('Barcode ' + barcode + ' was not likely replaced.',E);
124                                                                         }
125                                                                 }
126                                                         } catch(E) {
127                                                                 obj.error.standard_unexpected_error_alert('Barcode replacements did not likely happen.',E);
128                                                         }
129                                                 }
130                                         ],
131                                         'sel_edit' : [
132                                                 ['command'],
133                                                 function() {
134                                                         try {
135                                                                 obj.spawn_copy_editor();
136                                                                 for (var i = 0; i < obj.selection_list.length; i++) {
137                                                                                 var barcode = obj.selection_list[i].barcode;
138                                                                                 setTimeout( function(a) { return function() { obj.copy_status( a ); }; }(barcode), 0);
139                                                                 }
140                                                         } catch(E) {
141                                                                 obj.error.standard_unexpected_error_alert('with copy editor',E);
142                                                         }
143                                                 }
144                                         ],
145                                         'sel_spine' : [
146                                                 ['command'],
147                                                 function() {
148                                                         JSAN.use('cat.util');
149                                                         cat.util.spawn_spine_editor(obj.selection_list);
150                                                 }
151                                         ],
152                                         'sel_opac' : [
153                                                 ['command'],
154                                                 function() {
155                                                         JSAN.use('cat.util');
156                                                         cat.util.show_in_opac(obj.selection_list);
157                                                 }
158                                         ],
159                                         'sel_transit_abort' : [
160                                                 ['command'],
161                                                 function() {
162                                                         JSAN.use('circ.util');
163                                                         circ.util.abort_transits(obj.selection_list);
164                                                         for (var i = 0; i < obj.selection_list.length; i++) {
165                                                                 var barcode = obj.selection_list[i].barcode;
166                                                                 setTimeout( function(a) { return function() { obj.copy_status( a ); }; }(barcode), 0);
167                                                         }
168                                                         alert('Action complete.');
169                                                 }
170                                         ],
171                                         'sel_patron' : [
172                                                 ['command'],
173                                                 function() {
174                                                         JSAN.use('circ.util');
175                                                         circ.util.show_last_few_circs(obj.selection_list);
176                                                 }
177                                         ],
178                                         'sel_copy_details' : [
179                                                 ['command'],
180                                                 function() {
181                                                         JSAN.use('circ.util');
182                                                         for (var i = 0; i < obj.selection_list.length; i++) {
183                                                                 circ.util.show_copy_details( obj.selection_list[i].copy_id );
184                                                         }
185                                                 }
186                                         ],
187                                         'sel_renew' : [
188                                                 ['command'],
189                                                 function() {
190                                                         JSAN.use('circ.util');
191                                                         for (var i = 0; i < obj.selection_list.length; i++) {
192                                                                 var test = obj.selection_list[i].renewable;
193                                                                 var barcode = obj.selection_list[i].barcode;
194                                                                 if (test == 't') {
195                                                                         circ.util.renew_via_barcode( barcode );
196                                                                         setTimeout( function(a) { return function() { obj.copy_status( a ); }; }(barcode), 0);
197                                                                 } else {
198                                                                         alert('Item with barcode ' + barcode + ' is not circulating.');
199                                                                 }
200                                                         }
201                                                         alert('Action complete.');
202                                                 }
203                                         ],
204
205                                         'sel_mark_items_damaged' : [
206                                                 ['command'],
207                                                 function() {
208                                                         JSAN.use('cat.util'); JSAN.use('util.functional');
209                                                         cat.util.mark_item_damaged( util.functional.map_list( obj.selection_list, function(o) { return o.copy_id; } ) );
210                                                         for (var i = 0; i < obj.selection_list.length; i++) {
211                                                                 var barcode = obj.selection_list[i].barcode;
212                                                                 setTimeout( function(a) { return function() { obj.copy_status( a ); }; }(barcode), 0);
213                                                         }
214                                                 }
215                                         ],
216                                         'sel_mark_items_missing' : [
217                                                 ['command'],
218                                                 function() {
219                                                         JSAN.use('cat.util'); JSAN.use('util.functional');
220                                                         cat.util.mark_item_missing( util.functional.map_list( obj.selection_list, function(o) { return o.copy_id; } ) );
221                                                         for (var i = 0; i < obj.selection_list.length; i++) {
222                                                                 var barcode = obj.selection_list[i].barcode;
223                                                                 setTimeout( function(a) { return function() { obj.copy_status( a ); }; }(barcode), 0);
224                                                         }
225                                                 }
226                                         ],
227                                         'sel_bucket' : [
228                                                 ['command'],
229                                                 function() {
230                                                         JSAN.use('cat.util');
231                                                         cat.util.add_copies_to_bucket(obj.selection_list);
232                                                 }
233                                         ],
234                                         'copy_status_barcode_entry_textbox' : [
235                                                 ['keypress'],
236                                                 function(ev) {
237                                                         if (ev.keyCode && ev.keyCode == 13) {
238                                                                 obj.copy_status();
239                                                         }
240                                                 }
241                                         ],
242                                         'cmd_broken' : [
243                                                 ['command'],
244                                                 function() { alert('Not Yet Implemented'); }
245                                         ],
246                                         'cmd_copy_status_submit_barcode' : [
247                                                 ['command'],
248                                                 function() {
249                                                         obj.copy_status();
250                                                 }
251                                         ],
252                                         'cmd_copy_status_print' : [
253                                                 ['command'],
254                                                 function() {
255                                                         try {
256                                                                 obj.list.on_all_fleshed =
257                                                                         function() {
258                                                                                 try {
259                                                                                         dump( js2JSON( obj.list.dump_with_keys() ) + '\n' );
260                                                                                         obj.data.stash_retrieve();
261                                                                                         var lib = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
262                                                                                         lib.children(null);
263                                                                                         var p = { 
264                                                                                                 'lib' : lib,
265                                                                                                 'staff' : obj.data.list.au[0],
266                                                                                                 'header' : obj.data.print_list_templates.item_status.header,
267                                                                                                 'line_item' : obj.data.print_list_templates.item_status.line_item,
268                                                                                                 'footer' : obj.data.print_list_templates.item_status.footer,
269                                                                                                 'type' : obj.data.print_list_templates.item_status.type,
270                                                                                                 'list' : obj.list.dump_with_keys(),
271                                                                                         };
272                                                                                         JSAN.use('util.print'); var print = new util.print();
273                                                                                         print.tree_list( p );
274                                                                                         setTimeout(function(){ obj.list.on_all_fleshed = null; },0);
275                                                                                 } catch(E) {
276                                                                                         obj.error.standard_unexpected_error_alert('print',E); 
277                                                                                 }
278                                                                         }
279                                                                 obj.list.full_retrieve();
280                                                         } catch(E) {
281                                                                 obj.error.standard_unexpected_error_alert('print',E); 
282                                                         }
283                                                 }
284                                         ],
285                                         'cmd_copy_status_reprint' : [
286                                                 ['command'],
287                                                 function() {
288                                                 }
289                                         ],
290                                         'cmd_copy_status_done' : [
291                                                 ['command'],
292                                                 function() {
293                                                 }
294                                         ],
295                                 }
296                         }
297                 );
298                 this.controller.render();
299                 this.controller.view.copy_status_barcode_entry_textbox.focus();
300
301         },
302
303         'copy_status' : function(barcode) {
304                 var obj = this;
305                 try {
306                         if (!barcode) barcode = obj.controller.view.copy_status_barcode_entry_textbox.value;
307                         JSAN.use('circ.util');
308                         var copy = obj.network.simple_request( 'FM_ACP_RETRIEVE_VIA_BARCODE', [ barcode ]);
309                         if (copy == null) {
310                                 throw('Something weird happened.  null result');
311                         } else if (copy.ilsevent) {
312                                 switch(copy.ilsevent) {
313                                         case -1: 
314                                                 obj.error.standard_network_error_alert(); 
315                                                 obj.controller.view.copy_status_barcode_entry_textbox.select();
316                                                 obj.controller.view.copy_status_barcode_entry_textbox.focus();
317                                         break;
318                                         case 1502 /* ASSET_COPY_NOT_FOUND */ :
319                                                 obj.error.yns_alert(barcode + ' was either mis-scanned or is not cataloged.','Not Cataloged','OK',null,null,'Check here to confirm this message');
320                                                 obj.controller.view.copy_status_barcode_entry_textbox.select();
321                                                 obj.controller.view.copy_status_barcode_entry_textbox.focus();
322                                         break;
323                                         default: 
324                                                 throw(copy); 
325                                         break;
326                                 }
327                         } else {
328                                 var my_mvr = obj.network.simple_request('MODS_SLIM_RECORD_RETRIEVE_VIA_COPY', [ copy.id() ]);
329                                 obj.list.append(
330                                         {
331                                                 'retrieve_id' : js2JSON( { 'renewable' : copy.circulations() ? 't' : 'f', 'copy_id' : copy.id(), 'barcode' : barcode, 'doc_id' : (typeof my_mvr.ilsevent == 'undefined' ? my_mvr.doc_id() : null ) } ),
332                                                 'row' : {
333                                                         'my' : {
334                                                                 'mvr' : my_mvr,
335                                                                 'acp' : copy,
336                                                         }
337                                                 },
338                                                 'to_top' : true,
339                                         }
340                                 );
341                                 obj.controller.view.copy_status_barcode_entry_textbox.value = '';
342                                 obj.controller.view.copy_status_barcode_entry_textbox.focus();
343                         }
344                 } catch(E) {
345                         obj.error.standard_unexpected_error_alert('',E);
346                         obj.controller.view.copy_status_barcode_entry_textbox.select();
347                         obj.controller.view.copy_status_barcode_entry_textbox.focus();
348                 }
349
350         },
351         
352         'spawn_copy_editor' : function() {
353
354                 /* FIXME -  a lot of redundant calls here */
355
356                 var obj = this;
357
358                 JSAN.use('util.widgets'); JSAN.use('util.functional');
359
360                 var list = obj.selection_list;
361
362                 list = util.functional.map_list(
363                         list,
364                         function (o) {
365                                 return o.copy_id;
366                         }
367                 );
368
369                 var copies = util.functional.map_list(
370                         list,
371                         function (acp_id) {
372                                 return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
373                         }
374                 );
375
376                 var edit = 0;
377                 try {
378                         edit = obj.network.request(
379                                 api.PERM_MULTI_ORG_CHECK.app,
380                                 api.PERM_MULTI_ORG_CHECK.method,
381                                 [ 
382                                         ses(), 
383                                         obj.data.list.au[0].id(), 
384                                         util.functional.map_list(
385                                                 copies,
386                                                 function (o) {
387                                                         return o.call_number() == -1 ? o.circ_lib() : obj.network.simple_request('FM_ACN_RETRIEVE',[o.call_number()]).owning_lib();
388                                                 }
389                                         ),
390                                         copies.length == 1 ? [ 'UPDATE_COPY' ] : [ 'UPDATE_COPY', 'UPDATE_BATCH_COPY' ]
391                                 ]
392                         ).length == 0 ? 1 : 0;
393                 } catch(E) {
394                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
395                 }
396
397                 JSAN.use('cat.util'); cat.util.spawn_copy_editor(list,edit);
398
399         },
400
401 }
402
403 dump('exiting circ.copy_status.js\n');