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