09c7811bde99e10ce1e908f6e81181044458547c
[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                                                         } catch(E) {
107                                                                 obj.error.standard_unexpected_error_alert('Checkin did not likely happen.',E);
108                                                         }
109                                                 }
110                                         ],
111                                         'cmd_replace_barcode' : [
112                                                 ['command'],
113                                                 function() {
114                                                         try {
115                                                                 JSAN.use('cat.util');
116                                                                 for (var i = 0; i < obj.selection_list.length; i++) {
117                                                                         try { 
118                                                                                 var barcode = obj.selection_list[i].barcode;
119                                                                                 var new_bc = cat.util.replace_barcode( barcode );
120                                                                                 setTimeout( function(a) { return function() { obj.copy_status( a ); }; }(new_bc), 0);
121                                                                         } catch(E) {
122                                                                                 obj.error.standard_unexpected_error_alert('Barcode ' + barcode + ' was not likely replaced.',E);
123                                                                         }
124                                                                 }
125                                                         } catch(E) {
126                                                                 obj.error.standard_unexpected_error_alert('Barcode replacements did not likely happen.',E);
127                                                         }
128                                                 }
129                                         ],
130                                         'sel_edit' : [
131                                                 ['command'],
132                                                 function() {
133                                                         try {
134                                                                 obj.spawn_copy_editor();
135                                                                 for (var i = 0; i < obj.selection_list.length; i++) {
136                                                                                 var barcode = obj.selection_list[i].barcode;
137                                                                                 setTimeout( function(a) { return function() { obj.copy_status( a ); }; }(barcode), 0);
138                                                                 }
139                                                         } catch(E) {
140                                                                 obj.error.standard_unexpected_error_alert('with copy editor',E);
141                                                         }
142                                                 }
143                                         ],
144                                         'sel_spine' : [
145                                                 ['command'],
146                                                 function() {
147                                                         JSAN.use('cat.util');
148                                                         cat.util.spawn_spine_editor(obj.selection_list);
149                                                 }
150                                         ],
151                                         'sel_opac' : [
152                                                 ['command'],
153                                                 function() {
154                                                         JSAN.use('cat.util');
155                                                         cat.util.show_in_opac(obj.selection_list);
156                                                 }
157                                         ],
158                                         'sel_transit_abort' : [
159                                                 ['command'],
160                                                 function() {
161                                                         JSAN.use('circ.util');
162                                                         circ.util.abort_transits(obj.selection_list);
163                                                         for (var i = 0; i < obj.selection_list.length; i++) {
164                                                                 var barcode = obj.selection_list[i].barcode;
165                                                                 setTimeout( function(a) { return function() { obj.copy_status( a ); }; }(barcode), 0);
166                                                         }
167                                                 }
168                                         ],
169                                         'sel_patron' : [
170                                                 ['command'],
171                                                 function() {
172                                                         JSAN.use('circ.util');
173                                                         circ.util.show_last_few_circs(obj.selection_list);
174                                                 }
175                                         ],
176                                         'sel_copy_details' : [
177                                                 ['command'],
178                                                 function() {
179                                                         JSAN.use('circ.util');
180                                                         for (var i = 0; i < obj.selection_list.length; i++) {
181                                                                 circ.util.show_copy_details( obj.selection_list[i].copy_id );
182                                                         }
183                                                 }
184                                         ],
185                                         'sel_renew' : [
186                                                 ['command'],
187                                                 function() {
188                                                         JSAN.use('circ.util');
189                                                         for (var i = 0; i < obj.selection_list.length; i++) {
190                                                                 var test = obj.selection_list[i].renewable;
191                                                                 var barcode = obj.selection_list[i].barcode;
192                                                                 if (test == 't') {
193                                                                         circ.util.renew_via_barcode( barcode );
194                                                                         setTimeout( function(a) { return function() { obj.copy_status( a ); }; }(barcode), 0);
195                                                                 } else {
196                                                                         alert('Item with barcode ' + barcode + ' is not circulating.');
197                                                                 }
198                                                         }
199                                                 }
200                                         ],
201
202                                         'sel_mark_items_damaged' : [
203                                                 ['command'],
204                                                 function() {
205                                                         JSAN.use('cat.util'); JSAN.use('util.functional');
206                                                         cat.util.mark_item_damaged( util.functional.map_list( obj.selection_list, function(o) { return o.copy_id; } ) );
207                                                         for (var i = 0; i < obj.selection_list.length; i++) {
208                                                                 var barcode = obj.selection_list[i].barcode;
209                                                                 setTimeout( function(a) { return function() { obj.copy_status( a ); }; }(barcode), 0);
210                                                         }
211                                                 }
212                                         ],
213                                         'sel_mark_items_missing' : [
214                                                 ['command'],
215                                                 function() {
216                                                         JSAN.use('cat.util'); JSAN.use('util.functional');
217                                                         cat.util.mark_item_missing( util.functional.map_list( obj.selection_list, function(o) { return o.copy_id; } ) );
218                                                         for (var i = 0; i < obj.selection_list.length; i++) {
219                                                                 var barcode = obj.selection_list[i].barcode;
220                                                                 setTimeout( function(a) { return function() { obj.copy_status( a ); }; }(barcode), 0);
221                                                         }
222                                                 }
223                                         ],
224                                         'sel_bucket' : [
225                                                 ['command'],
226                                                 function() {
227                                                         JSAN.use('cat.util');
228                                                         cat.util.add_copies_to_bucket(obj.selection_list);
229                                                 }
230                                         ],
231                                         'copy_status_barcode_entry_textbox' : [
232                                                 ['keypress'],
233                                                 function(ev) {
234                                                         if (ev.keyCode && ev.keyCode == 13) {
235                                                                 obj.copy_status();
236                                                         }
237                                                 }
238                                         ],
239                                         'cmd_broken' : [
240                                                 ['command'],
241                                                 function() { alert('Not Yet Implemented'); }
242                                         ],
243                                         'cmd_copy_status_submit_barcode' : [
244                                                 ['command'],
245                                                 function() {
246                                                         obj.copy_status();
247                                                 }
248                                         ],
249                                         'cmd_copy_status_print' : [
250                                                 ['command'],
251                                                 function() {
252                                                         try {
253                                                                 obj.list.on_all_fleshed =
254                                                                         function() {
255                                                                                 try {
256                                                                                         dump( js2JSON( obj.list.dump_with_keys() ) + '\n' );
257                                                                                         obj.data.stash_retrieve();
258                                                                                         var lib = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
259                                                                                         lib.children(null);
260                                                                                         var p = { 
261                                                                                                 'lib' : lib,
262                                                                                                 'staff' : obj.data.list.au[0],
263                                                                                                 'header' : obj.data.print_list_templates.item_status.header,
264                                                                                                 'line_item' : obj.data.print_list_templates.item_status.line_item,
265                                                                                                 'footer' : obj.data.print_list_templates.item_status.footer,
266                                                                                                 'type' : obj.data.print_list_templates.item_status.type,
267                                                                                                 'list' : obj.list.dump_with_keys(),
268                                                                                         };
269                                                                                         JSAN.use('util.print'); var print = new util.print();
270                                                                                         print.tree_list( p );
271                                                                                         setTimeout(function(){ obj.list.on_all_fleshed = null; },0);
272                                                                                 } catch(E) {
273                                                                                         obj.error.standard_unexpected_error_alert('print',E); 
274                                                                                 }
275                                                                         }
276                                                                 obj.list.full_retrieve();
277                                                         } catch(E) {
278                                                                 obj.error.standard_unexpected_error_alert('print',E); 
279                                                         }
280                                                 }
281                                         ],
282                                         'cmd_copy_status_reprint' : [
283                                                 ['command'],
284                                                 function() {
285                                                 }
286                                         ],
287                                         'cmd_copy_status_done' : [
288                                                 ['command'],
289                                                 function() {
290                                                 }
291                                         ],
292                                 }
293                         }
294                 );
295                 this.controller.render();
296                 this.controller.view.copy_status_barcode_entry_textbox.focus();
297
298         },
299
300         'copy_status' : function(barcode) {
301                 var obj = this;
302                 try {
303                         if (!barcode) barcode = obj.controller.view.copy_status_barcode_entry_textbox.value;
304                         JSAN.use('circ.util');
305                         var copy = obj.network.simple_request( 'FM_ACP_RETRIEVE_VIA_BARCODE', [ barcode ]);
306                         if (copy == null) {
307                                 throw('Something weird happened.  null result');
308                         } else if (copy.ilsevent) {
309                                 switch(copy.ilsevent) {
310                                         case -1: 
311                                                 obj.error.standard_network_error_alert(); 
312                                                 obj.controller.view.copy_status_barcode_entry_textbox.select();
313                                                 obj.controller.view.copy_status_barcode_entry_textbox.focus();
314                                         break;
315                                         case 1502 /* ASSET_COPY_NOT_FOUND */ :
316                                                 obj.error.yns_alert(barcode + ' was either mis-scanned or is not cataloged.','Not Cataloged','OK',null,null,'Check here to confirm this message');
317                                                 obj.controller.view.copy_status_barcode_entry_textbox.select();
318                                                 obj.controller.view.copy_status_barcode_entry_textbox.focus();
319                                         break;
320                                         default: 
321                                                 throw(copy); 
322                                         break;
323                                 }
324                         } else {
325                                 var my_mvr = obj.network.simple_request('MODS_SLIM_RECORD_RETRIEVE_VIA_COPY', [ copy.id() ]);
326                                 obj.list.append(
327                                         {
328                                                 '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 ) } ),
329                                                 'row' : {
330                                                         'my' : {
331                                                                 'mvr' : my_mvr,
332                                                                 'acp' : copy,
333                                                         }
334                                                 },
335                                                 'to_top' : true,
336                                         }
337                                 );
338                                 obj.controller.view.copy_status_barcode_entry_textbox.value = '';
339                                 obj.controller.view.copy_status_barcode_entry_textbox.focus();
340                         }
341                 } catch(E) {
342                         obj.error.standard_unexpected_error_alert('',E);
343                         obj.controller.view.copy_status_barcode_entry_textbox.select();
344                         obj.controller.view.copy_status_barcode_entry_textbox.focus();
345                 }
346
347         },
348         
349         'spawn_copy_editor' : function() {
350
351                 /* FIXME -  a lot of redundant calls here */
352
353                 var obj = this;
354
355                 JSAN.use('util.widgets'); JSAN.use('util.functional');
356
357                 var list = obj.selection_list;
358
359                 list = util.functional.map_list(
360                         list,
361                         function (o) {
362                                 return o.copy_id;
363                         }
364                 );
365
366                 var copies = util.functional.map_list(
367                         list,
368                         function (acp_id) {
369                                 return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
370                         }
371                 );
372
373                 var edit = 0;
374                 try {
375                         edit = obj.network.request(
376                                 api.PERM_MULTI_ORG_CHECK.app,
377                                 api.PERM_MULTI_ORG_CHECK.method,
378                                 [ 
379                                         ses(), 
380                                         obj.data.list.au[0].id(), 
381                                         util.functional.map_list(
382                                                 copies,
383                                                 function (o) {
384                                                         return o.call_number() == -1 ? o.circ_lib() : obj.network.simple_request('FM_ACN_RETRIEVE',[o.call_number()]).owning_lib();
385                                                 }
386                                         ),
387                                         copies.length == 1 ? [ 'UPDATE_COPY' ] : [ 'UPDATE_COPY', 'UPDATE_BATCH_COPY' ]
388                                 ]
389                         ).length == 0 ? 1 : 0;
390                 } catch(E) {
391                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
392                 }
393
394                 JSAN.use('cat.util'); cat.util.spawn_copy_editor(list,edit);
395
396         },
397
398 }
399
400 dump('exiting circ.copy_status.js\n');