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