188c1a725defb36376e89cf77ed552f8303087d3
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / volume_copy_creator.js
1 function my_init() {
2         try {
3
4                 /***********************************************************************************************************/
5                 /* Initial setup */
6
7                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
8                                 if (typeof JSAN == 'undefined') { throw( "The JSAN library object is missing."); }
9                 JSAN.errorLevel = "die"; // none, warn, or die
10                 JSAN.addRepository('/xul/server/');
11                 JSAN.use('util.error'); g.error = new util.error();
12                 g.error.sdump('D_TRACE','my_init() for cat/volume_copy_creator.xul');
13
14                 JSAN.use('OpenILS.data'); g.data = new OpenILS.data(); g.data.init({'via':'stash'});
15                 JSAN.use('util.widgets'); JSAN.use('util.functional');
16
17                 JSAN.use('util.network'); g.network = new util.network();
18
19                 g.cgi = new CGI();
20
21                 /***********************************************************************************************************/
22                 /* What record am I dealing with?  Am I adding just copies or copies and volumes? */
23
24                 g.doc_id = g.cgi.param('doc_id');
25                 document.getElementById('summary').setAttribute('src',urls.XUL_BIB_BRIEF + '?docid=' + window.escape(g.doc_id));
26
27                 g.copy_shortcut = g.cgi.param('copy_shortcut');
28                 g.error.sdump('D_ERROR','location.href = ' + location.href + '\n\ncopy_short cut = ' + g.copy_shortcut + '\n\nou_ids = ' + g.cgi.param('ou_ids'));
29                 if (g.copy_shortcut) g.copy_shortcut = JSON2js( g.copy_shortcut );
30
31                 var ou_ids = [];
32                 if (g.cgi.param('ou_ids')) 
33                         ou_ids = JSON2js( g.cgi.param('ou_ids') );
34                 if (!ou_ids) ou_ids = [];
35                 if (window.xulG && window.xulG.ou_ids) 
36                         ou_ids = ou_ids.concat( window.xulG.ou_ids );
37
38                 /***********************************************************************************************************/
39                 /* For the call number drop down */
40
41                 var cn_blob;
42                 try {
43                         cn_blob = g.network.simple_request('BLOB_MARC_CALLNUMBERS_RETRIEVE',[g.doc_id]);
44                 } catch(E) {
45                         cn_blob = [];
46                 }
47                 if ((!g.copy_shortcut)) {
48                         var hbox = document.getElementById('marc_cn');
49                         var ml = util.widgets.make_menulist(
50                                 util.functional.map_list(
51                                         cn_blob,
52                                         function(o) {
53                                                 for (var i in o) {
54                                                         return [ o[i], i ];
55                                                 }
56                                         }
57                                 ).sort(
58                                         function(a,b) {
59                                                 a = a[1]; b = b[1];
60                                                 if (a == '082') return -1; 
61                                                 if (b == '082') return 1; 
62                                                 if (a == '092')  return -1; 
63                                                 if (b == '092')  return 1; 
64                                                 if (a < b) return -1; 
65                                                 if (a > b) return 1; 
66                                                 return 0;
67                                         }
68                                 )
69                         ); hbox.appendChild(ml);
70                         ml.setAttribute('editable','true');
71                         ml.setAttribute('width', '200');
72                         var btn = document.createElement('button');
73                         btn.setAttribute('label','Apply');
74                         btn.setAttribute('accesskey','A');
75                         btn.setAttribute('image','/xul/server/skin/media/images/down_arrow.gif');
76                         hbox.appendChild(btn);
77                         btn.addEventListener(
78                                 'command',
79                                 function() {
80                                         var nl = document.getElementsByTagName('textbox');
81                                         for (var i = 0; i < nl.length; i++) {
82                                                 if (nl[i].getAttribute('rel_vert_pos')==2 
83                                                         && !nl[i].disabled) nl[i].value = ml.value;
84                                         }
85                                         if (g.last_focus) setTimeout( function() { g.last_focus.focus(); }, 0 );
86                                 }, 
87                                 false
88                         );
89                 }
90
91                 /***********************************************************************************************************/
92                 /* render the orgs and volumes/input */
93
94                 var rows = document.getElementById('rows');
95
96                 var node_id = 0;
97                 for (var i = 0; i < ou_ids.length; i++) {
98                         try {
99                                 var row = document.createElement('row'); rows.appendChild(row); row.setAttribute('ou_id',ou_ids[i]);
100                                 g.render_library_label(row,ou_ids[i]);
101                                 g.render_volume_count_entry(row,ou_ids[i]);
102                         } catch(E) {
103                                 g.error.sdump('D_ERROR',E);
104                         }
105                 }
106
107         } catch(E) {
108                 var err_msg = "!! This software has encountered an error.  Please tell your friendly " +
109                         "system administrator or software developer the following:\ncat/volume_copy_creator.xul\n" +E+ '\n';
110                 try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); dump(js2JSON(E)); }
111                 alert(err_msg);
112         }
113 }
114
115 g.render_library_label = function(row,ou_id) {
116         var label = document.createElement('label'); row.appendChild(label);
117         label.setAttribute('ou_id',ou_id);
118         label.setAttribute('value',g.data.hash.aou[ ou_id ].shortname());
119 }
120
121 g.render_volume_count_entry = function(row,ou_id) {
122         var hb = document.createElement('vbox'); row.appendChild(hb);
123         var tb = document.createElement('textbox'); hb.appendChild(tb);
124         tb.setAttribute('ou_id',ou_id); tb.setAttribute('size','3'); tb.setAttribute('cols','3');
125         tb.setAttribute('rel_vert_pos','1'); 
126         if ( (!g.copy_shortcut) && (!g.last_focus) ) { tb.focus(); g.last_focus = tb; }
127         var node;
128         function render_copy_count_entry(ev) {
129                 if (ev.target.disabled) return;
130                 if (! isNaN( Number( ev.target.value) ) ) {
131                         if (node) { row.removeChild(node); node = null; }
132                         //ev.target.disabled = true;
133                         node = g.render_callnumber_copy_count_entry(row,ou_id,ev.target.value);
134                 }
135         }
136         util.widgets.apply_vertical_tab_on_enter_handler( 
137                 tb, 
138                 function() { render_copy_count_entry({'target':tb}); setTimeout(function(){util.widgets.vertical_tab(tb);},0); }
139         );
140         tb.addEventListener( 'change', render_copy_count_entry, false);
141         tb.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
142         setTimeout(
143                 function() {
144                         try {
145                         if (g.copy_shortcut) {
146                                 JSAN.use('util.functional');
147                                 tb.value = util.functional.map_object_to_list(
148                                         g.copy_shortcut[ou_id],
149                                         function(o,i) {
150                                                 return g.copy_shortcut[ou_id][i];
151                                         }
152                                 ).length
153                                 render_copy_count_entry({'target':tb});
154                                 tb.disabled = true;
155                         }
156                         } catch(E) {
157                                 alert(E);
158                         }
159                 }, 0
160         );
161 }
162
163 g.render_callnumber_copy_count_entry = function(row,ou_id,count) {
164         var grid = util.widgets.make_grid( [ {}, {} ] ); row.appendChild(grid);
165         grid.setAttribute('flex','1');
166         grid.setAttribute('ou_id',ou_id);
167         var rows = grid.lastChild;
168         var r = document.createElement('row'); rows.appendChild( r );
169         var x = document.createElement('label'); r.appendChild(x);
170         x.setAttribute('value','Call Numbers'); x.setAttribute('style','font-weight: bold');
171         x = document.createElement('label'); r.appendChild(x);
172         x.setAttribute('value','# of Copies'); x.setAttribute('style','font-weight: bold');
173
174         function handle_change(tb1,tb2,hb3) {
175                 if (tb1.value == '') return;
176                 if (isNaN( Number( tb2.value ) )) return;
177
178                 //if (tb1.disabled || tb2.disabled) return;
179
180                 //tb1.disabled = true;
181                 //tb2.disabled = true;
182
183                 util.widgets.remove_children(hb3);
184
185                 g.render_barcode_entry(hb3,tb1.value,Number(tb2.value),ou_id);
186                 document.getElementById("Create").disabled = false;
187         }
188
189         function handle_change_tb1(ev) {
190                 var _tb1 = ev.target;   
191                 var _hb1 = _tb1.parentNode;
192                 var _hb2 = _hb1.nextSibling;
193                 var _tb2 = _hb2.firstChild;
194                 var _hb3 = _hb2.nextSibling;
195                 handle_change(_tb1,_tb2,_hb3);
196         }
197
198         function handle_change_tb2(ev) {
199                 var _tb2 = ev.target;   
200                 var _hb2 = _tb2.parentNode;
201                 var _hb1 = _hb2.previousSibling;
202                 var _tb1 = _hb1.firstChild;
203                 var _hb3 = _hb2.nextSibling;
204                 handle_change(_tb1,_tb2,_hb3);
205         }
206
207         for (var i = 0; i < count; i++) {
208                 var r = document.createElement('row'); rows.appendChild(r);
209                 var hb1 = document.createElement('vbox'); r.appendChild(hb1);
210                 var hb2 = document.createElement('vbox'); r.appendChild(hb2);
211                 var hb3 = document.createElement('vbox'); r.appendChild(hb3);
212                 var tb1 = document.createElement('textbox'); hb1.appendChild(tb1);
213                 tb1.setAttribute('rel_vert_pos','2');
214                 tb1.setAttribute('ou_id',ou_id);
215                 util.widgets.apply_vertical_tab_on_enter_handler( 
216                         tb1, 
217                         function() { handle_change_tb1({'target':tb1}); setTimeout(function(){util.widgets.vertical_tab(tb1);},0); }
218                 );
219                 var tb2 = document.createElement('textbox'); hb2.appendChild(tb2);
220                 tb2.setAttribute('size','3'); tb2.setAttribute('cols','3');
221                 tb2.setAttribute('rel_vert_pos','3');
222                 tb2.setAttribute('ou_id',ou_id);
223                 util.widgets.apply_vertical_tab_on_enter_handler( 
224                         tb2, 
225                         function() { handle_change_tb2({'target':tb2}); setTimeout(function(){util.widgets.vertical_tab(tb2);},0); }
226                 );
227
228                 tb1.addEventListener( 'change', handle_change_tb1, false);
229                 tb1.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
230                 tb2.addEventListener( 'change', handle_change_tb2, false);
231                 tb2.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
232                 if ( !g.last_focus ) { tb2.focus(); g.last_focus = tb2; }
233
234                 setTimeout(
235                         function(idx,tb){
236                                 return function() {
237                                         try {
238                                         JSAN.use('util.functional');
239                                         if (g.copy_shortcut) {
240                                                 var label = util.functional.map_object_to_list(
241                                                         g.copy_shortcut[ou_id],
242                                                         function(o,i) {
243                                                                 return i;
244                                                         }
245                                                 )[idx];
246                                                 tb.value = label; handle_change_tb1({'target':tb});
247                                                 tb.disabled = true;
248                                         }
249                                         } catch(E) {
250                                                 alert(E);
251                                         }
252                                 }
253                         }(i,tb1),0
254                 );
255         }
256
257         return grid;
258 }
259
260 g.render_barcode_entry = function(node,callnumber,count,ou_id) {
261         try {
262                 function ready_to_create(ev) {
263                         document.getElementById("Create").disabled = false;
264                 }
265
266                 JSAN.use('util.barcode'); 
267
268                 for (var i = 0; i < count; i++) {
269                         var tb = document.createElement('textbox'); node.appendChild(tb);
270                         tb.setAttribute('ou_id',ou_id);
271                         tb.setAttribute('callnumber',callnumber);
272                         tb.setAttribute('rel_vert_pos','4');
273                         util.widgets.apply_vertical_tab_on_enter_handler( 
274                                 tb, 
275                                 function() { ready_to_create({'target':tb}); setTimeout(function(){util.widgets.vertical_tab(tb);},0); }
276                         );
277                         //tb.addEventListener('change',ready_to_create,false);
278                         tb.addEventListener('change', function(ev) {
279                                 var barcode = ev.target.value;
280                                 if ($('check_barcodes').checked && ! util.barcode.check(barcode) ) {
281                                         g.error.yns_alert( '"' + barcode + '" is an invalid barcode.','Invalid Barcode','OK',null,null,'Check here to confirm this message.');
282                                         setTimeout( function() { ev.target.select(); ev.target.focus(); }, 0);
283                                 }
284                         }, false);
285                         tb.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
286                 }
287         } catch(E) {
288                 g.error.sdump('D_ERROR','g.render_barcode_entry: ' + E);
289         }
290 }
291
292 g.new_node_id = -1;
293
294 g.stash_and_close = function() {
295
296         try {
297
298                 var nl = document.getElementsByTagName('textbox');
299
300                 var volumes_hash = {};
301
302                 var barcodes = [];
303                 
304                 for (var i = 0; i < nl.length; i++) {
305                         if ( nl[i].getAttribute('rel_vert_pos') == 4 ) barcodes.push( nl[i] );
306                         if ( nl[i].getAttribute('rel_vert_pos') == 2 )  {
307                                 var ou_id = nl[i].getAttribute('ou_id');
308                                 var callnumber = nl[i].value;
309                                 if (typeof volumes_hash[ou_id] == 'undefined') { volumes_hash[ou_id] = {} }
310                                 if (typeof volumes_hash[ou_id][callnumber] == 'undefined') { volumes_hash[ou_id][callnumber] = [] }
311                         }
312                 };
313         
314                 for (var i = 0; i < barcodes.length; i++) {
315                         var ou_id = barcodes[i].getAttribute('ou_id');
316                         var callnumber = barcodes[i].getAttribute('callnumber');
317                         var barcode = barcodes[i].value;
318
319                         if (typeof volumes_hash[ou_id] == 'undefined') { volumes_hash[ou_id] = {} }
320                         if (typeof volumes_hash[ou_id][callnumber] == 'undefined') { volumes_hash[ou_id][callnumber] = [] }
321
322                         if (barcode != '') volumes_hash[ou_id][callnumber].push( barcode );
323                 }
324
325                 var volumes = [];
326                 var copies = [];
327                 var volume_labels = {};
328
329                 for (var ou_id in volumes_hash) {
330                         for (var cn in volumes_hash[ou_id]) {
331                                 var volume = new acn();
332                                 var acn_id;
333                                 if (!g.copy_shortcut) {
334                                         acn_id = g.new_node_id--;
335                                         volume.isnew('1');
336                                 } else {
337                                         acn_id = g.copy_shortcut[ou_id][cn];
338                                 }
339                                 volume.id( acn_id );
340                                 volume.record(g.doc_id);
341                                 volume.label(cn);
342                                 volume.owning_lib(ou_id);
343                                 volume.copies( [] );
344                                 volumes.push( volume );
345
346                                 volume_labels[ acn_id ] = { 'label' : cn, 'owning_lib' : ou_id };
347
348                                 for (var i = 0; i < volumes_hash[ou_id][cn].length; i++) {
349                                         var copy = new acp();
350                                         copy.id( g.new_node_id-- );
351                                         copy.isnew('1');
352                                         copy.barcode( volumes_hash[ou_id][cn][i] );
353                                         copy.call_number( acn_id );
354                                         copy.circ_lib(ou_id);
355                                         /* FIXME -- use constants */
356                                         copy.deposit(0);
357                                         copy.price(0);
358                                         copy.deposit_amount(0);
359                                         copy.fine_level(2);
360                                         copy.loan_duration(2);
361                                         copy.location(1);
362                                         copy.status(0);
363                                         copy.circulate(get_db_true());
364                                         copy.holdable(get_db_true());
365                                         copy.opac_visible(get_db_true());
366                                         copy.ref(get_db_false());
367                                         copies.push( copy );
368                                 }
369                         }
370                 }
371
372                 JSAN.use('util.window'); var win = new util.window();
373                 if (copies.length > 0) {
374                         g.data.temp_copies = js2JSON(copies); g.data.stash('temp_copies');
375                         g.data.temp_copy_ids = undefined; g.data.stash('temp_copy_ids');
376                         g.data.temp_callnumbers = js2JSON(volume_labels); g.data.stash('temp_callnumbers');
377                         var w = win.open(
378                                 urls.XUL_COPY_EDITOR
379                                         +'?edit=1&docid='+window.escape(g.doc_id),
380                                 title,
381                                 'chrome,modal,resizable'
382                         );
383                         /* FIXME -- need to unique the temp space, and not rely on modalness of window */
384                         g.data.stash_retrieve();
385                         copies = JSON2js( g.data.temp_copies );
386                         g.data.temp_copy_ids = undefined; g.data.stash('temp_copy_ids');
387                         g.data.temp_copies = undefined; g.data.stash('temp_copies');
388                         g.data.temp_callnumbers = undefined; g.data.stash('temp_callnumbers');
389                         if (!copies) {
390                                 alert('Items were not created.');
391                                 return;
392                         }
393                 }
394
395                 for (var i = 0; i < copies.length; i++) {
396                         var copy = copies[i];
397                         var volume = util.functional.find_id_object_in_list( volumes, copy.call_number() );
398                         var temp = volume.copies();
399                         temp.push( copy );
400                         volume.copies( temp );
401                 }
402
403                 try {
404                         var r = g.network.request(
405                                 api.FM_ACN_TREE_UPDATE.app,
406                                 api.FM_ACN_TREE_UPDATE.method,
407                                 [ ses(), volumes, true ]
408                         );
409                         if (typeof r.ilsevent != 'undefined') {
410                                 switch(r.ilsevent) {
411                                         case 1706 /* ITEM_BARCODE_EXISTS */ :
412                                                 alert('Some of these barcodes are or have been in use.  Please change them.');
413                                                 return;
414                                         break;
415                                         default: g.error.standard_unexpected_error_alert('volume tree update',r); break;
416                                 }
417                         } else {
418                                 if (copies.length > 0 && $('print_labels').checked) {
419                                         JSAN.use('util.functional');
420                                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
421                                         data.temp_barcodes_for_labels = util.functional.map_list( copies, function(o){return o.barcode();}) ; 
422                                         data.stash('temp_barcodes_for_labels');
423                                         var w = win.open(
424                                                 urls.XUL_SPINE_LABEL,
425                                                 'spine_labels',
426                                                 'chrome,resizable,width=750,height=550'
427                                         );
428                                 }
429                         }
430                 } catch(E) {
431                         g.error.standard_unexpected_error_alert('volume tree update 2',E);
432                 }
433
434                 if (typeof window.refresh == 'function') window.refresh();
435
436                 window.close();
437
438         } catch(E) {
439                 g.error.standard_unexpected_error_alert('volume tree update 3',E);
440         }
441 }
442
443