]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/volume_copy_creator.js
don't allow volumes to be added to orgs with can_have_vols == false
[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                 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         } catch(E) {
111                 var err_msg = "!! This software has encountered an error.  Please tell your friendly " +
112                         "system administrator or software developer the following:\ncat/volume_copy_creator.xul\n" +E+ '\n';
113                 try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); dump(js2JSON(E)); }
114                 alert(err_msg);
115         }
116 }
117
118 g.render_library_label = function(row,ou_id) {
119         var label = document.createElement('label'); row.appendChild(label);
120         label.setAttribute('ou_id',ou_id);
121         label.setAttribute('value',g.data.hash.aou[ ou_id ].shortname());
122 }
123
124 g.render_volume_count_entry = function(row,ou_id) {
125         var hb = document.createElement('vbox'); row.appendChild(hb);
126         var tb = document.createElement('textbox'); hb.appendChild(tb);
127         tb.setAttribute('ou_id',ou_id); tb.setAttribute('size','3'); tb.setAttribute('cols','3');
128         tb.setAttribute('rel_vert_pos','1'); 
129         if ( (!g.copy_shortcut) && (!g.last_focus) ) { tb.focus(); g.last_focus = tb; }
130         var node;
131         function render_copy_count_entry(ev) {
132                 if (ev.target.disabled) return;
133                 if (! isNaN( Number( ev.target.value) ) ) {
134                         if (node) { row.removeChild(node); node = null; }
135                         //ev.target.disabled = true;
136                         node = g.render_callnumber_copy_count_entry(row,ou_id,ev.target.value);
137                 }
138         }
139         util.widgets.apply_vertical_tab_on_enter_handler( 
140                 tb, 
141                 function() { render_copy_count_entry({'target':tb}); setTimeout(function(){util.widgets.vertical_tab(tb);},0); }
142         );
143         tb.addEventListener( 'change', render_copy_count_entry, false);
144         tb.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
145         setTimeout(
146                 function() {
147                         try {
148                         if (g.copy_shortcut) {
149                                 JSAN.use('util.functional');
150                                 tb.value = util.functional.map_object_to_list(
151                                         g.copy_shortcut[ou_id],
152                                         function(o,i) {
153                                                 return g.copy_shortcut[ou_id][i];
154                                         }
155                                 ).length
156                                 render_copy_count_entry({'target':tb});
157                                 tb.disabled = true;
158                         }
159                         } catch(E) {
160                                 alert(E);
161                         }
162                 }, 0
163         );
164 }
165
166 g.render_callnumber_copy_count_entry = function(row,ou_id,count) {
167         var grid = util.widgets.make_grid( [ {}, {} ] ); row.appendChild(grid);
168         grid.setAttribute('flex','1');
169         grid.setAttribute('ou_id',ou_id);
170         var rows = grid.lastChild;
171         var r = document.createElement('row'); rows.appendChild( r );
172         var x = document.createElement('label'); r.appendChild(x);
173         x.setAttribute('value','Call Numbers'); x.setAttribute('style','font-weight: bold');
174         x = document.createElement('label'); r.appendChild(x);
175         x.setAttribute('value','# of Copies'); x.setAttribute('style','font-weight: bold');
176
177         function handle_change(tb1,tb2,hb3) {
178                 if (tb1.value == '') return;
179                 if (isNaN( Number( tb2.value ) )) return;
180
181                 //if (tb1.disabled || tb2.disabled) return;
182
183                 //tb1.disabled = true;
184                 //tb2.disabled = true;
185
186                 util.widgets.remove_children(hb3);
187
188                 g.render_barcode_entry(hb3,tb1.value,Number(tb2.value),ou_id);
189                 document.getElementById("Create").disabled = false;
190         }
191
192         function handle_change_tb1(ev) {
193                 var _tb1 = ev.target;   
194                 var _hb1 = _tb1.parentNode;
195                 var _hb2 = _hb1.nextSibling;
196                 var _tb2 = _hb2.firstChild;
197                 var _hb3 = _hb2.nextSibling;
198                 handle_change(_tb1,_tb2,_hb3);
199         }
200
201         function handle_change_tb2(ev) {
202                 var _tb2 = ev.target;   
203                 var _hb2 = _tb2.parentNode;
204                 var _hb1 = _hb2.previousSibling;
205                 var _tb1 = _hb1.firstChild;
206                 var _hb3 = _hb2.nextSibling;
207                 handle_change(_tb1,_tb2,_hb3);
208         }
209
210         for (var i = 0; i < count; i++) {
211                 var r = document.createElement('row'); rows.appendChild(r);
212                 var hb1 = document.createElement('vbox'); r.appendChild(hb1);
213                 var hb2 = document.createElement('vbox'); r.appendChild(hb2);
214                 var hb3 = document.createElement('vbox'); r.appendChild(hb3);
215                 var tb1 = document.createElement('textbox'); hb1.appendChild(tb1);
216                 tb1.setAttribute('rel_vert_pos','2');
217                 tb1.setAttribute('ou_id',ou_id);
218                 util.widgets.apply_vertical_tab_on_enter_handler( 
219                         tb1, 
220                         function() { handle_change_tb1({'target':tb1}); setTimeout(function(){util.widgets.vertical_tab(tb1);},0); }
221                 );
222                 var tb2 = document.createElement('textbox'); hb2.appendChild(tb2);
223                 tb2.setAttribute('size','3'); tb2.setAttribute('cols','3');
224                 tb2.setAttribute('rel_vert_pos','3');
225                 tb2.setAttribute('ou_id',ou_id);
226                 util.widgets.apply_vertical_tab_on_enter_handler( 
227                         tb2, 
228                         function() { handle_change_tb2({'target':tb2}); setTimeout(function(){util.widgets.vertical_tab(tb2);},0); }
229                 );
230
231                 tb1.addEventListener( 'change', handle_change_tb1, false);
232                 tb1.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
233                 tb2.addEventListener( 'change', handle_change_tb2, false);
234                 tb2.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
235                 if ( !g.last_focus ) { tb2.focus(); g.last_focus = tb2; }
236
237                 setTimeout(
238                         function(idx,tb){
239                                 return function() {
240                                         try {
241                                         JSAN.use('util.functional');
242                                         if (g.copy_shortcut) {
243                                                 var label = util.functional.map_object_to_list(
244                                                         g.copy_shortcut[ou_id],
245                                                         function(o,i) {
246                                                                 return i;
247                                                         }
248                                                 )[idx];
249                                                 tb.value = label; handle_change_tb1({'target':tb});
250                                                 tb.disabled = true;
251                                         }
252                                         } catch(E) {
253                                                 alert(E);
254                                         }
255                                 }
256                         }(i,tb1),0
257                 );
258         }
259
260         return grid;
261 }
262
263 g.render_barcode_entry = function(node,callnumber,count,ou_id) {
264         try {
265                 function ready_to_create(ev) {
266                         document.getElementById("Create").disabled = false;
267                 }
268
269                 JSAN.use('util.barcode'); 
270
271                 for (var i = 0; i < count; i++) {
272                         var tb = document.createElement('textbox'); node.appendChild(tb);
273                         tb.setAttribute('ou_id',ou_id);
274                         tb.setAttribute('callnumber',callnumber);
275                         tb.setAttribute('rel_vert_pos','4');
276                         util.widgets.apply_vertical_tab_on_enter_handler( 
277                                 tb, 
278                                 function() { ready_to_create({'target':tb}); setTimeout(function(){util.widgets.vertical_tab(tb);},0); }
279                         );
280                         //tb.addEventListener('change',ready_to_create,false);
281                         tb.addEventListener('change', function(ev) {
282                                 var barcode = ev.target.value;
283                                 if ($('check_barcodes').checked && ! util.barcode.check(barcode) ) {
284                                         g.error.yns_alert( '"' + barcode + '" is an invalid barcode.','Invalid Barcode','OK',null,null,'Check here to confirm this message.');
285                                         setTimeout( function() { ev.target.select(); ev.target.focus(); }, 0);
286                                 }
287                         }, false);
288                         tb.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
289                 }
290         } catch(E) {
291                 g.error.sdump('D_ERROR','g.render_barcode_entry: ' + E);
292         }
293 }
294
295 g.new_node_id = -1;
296
297 g.stash_and_close = function() {
298
299         try {
300
301                 var nl = document.getElementsByTagName('textbox');
302
303                 var volumes_hash = {};
304
305                 var barcodes = [];
306                 
307                 for (var i = 0; i < nl.length; i++) {
308                         if ( nl[i].getAttribute('rel_vert_pos') == 4 ) barcodes.push( nl[i] );
309                         if ( nl[i].getAttribute('rel_vert_pos') == 2 )  {
310                                 var ou_id = nl[i].getAttribute('ou_id');
311                                 var callnumber = nl[i].value;
312                                 if (typeof volumes_hash[ou_id] == 'undefined') { volumes_hash[ou_id] = {} }
313                                 if (typeof volumes_hash[ou_id][callnumber] == 'undefined') { volumes_hash[ou_id][callnumber] = [] }
314                         }
315                 };
316         
317                 for (var i = 0; i < barcodes.length; i++) {
318                         var ou_id = barcodes[i].getAttribute('ou_id');
319                         var callnumber = barcodes[i].getAttribute('callnumber');
320                         var barcode = barcodes[i].value;
321
322                         if (typeof volumes_hash[ou_id] == 'undefined') { volumes_hash[ou_id] = {} }
323                         if (typeof volumes_hash[ou_id][callnumber] == 'undefined') { volumes_hash[ou_id][callnumber] = [] }
324
325                         if (barcode != '') volumes_hash[ou_id][callnumber].push( barcode );
326                 }
327
328                 var volumes = [];
329                 var copies = [];
330                 var volume_labels = {};
331
332                 for (var ou_id in volumes_hash) {
333                         for (var cn in volumes_hash[ou_id]) {
334                                 var volume = new acn();
335                                 var acn_id;
336                                 if (!g.copy_shortcut) {
337                                         acn_id = g.new_node_id--;
338                                         volume.isnew('1');
339                                 } else {
340                                         acn_id = g.copy_shortcut[ou_id][cn];
341                                 }
342                                 volume.id( acn_id );
343                                 volume.record(g.doc_id);
344                                 volume.label(cn);
345                                 volume.owning_lib(ou_id);
346                                 volume.copies( [] );
347                                 volumes.push( volume );
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                         var w = win.open(
381                                 urls.XUL_COPY_EDITOR
382                                         +'?edit=1&docid='+window.escape(g.doc_id),
383                                 title,
384                                 'chrome,modal,resizable'
385                         );
386                         /* FIXME -- need to unique the temp space, and not rely on modalness of window */
387                         g.data.stash_retrieve();
388                         copies = JSON2js( g.data.temp_copies );
389                         g.data.temp_copy_ids = undefined; g.data.stash('temp_copy_ids');
390                         g.data.temp_copies = undefined; g.data.stash('temp_copies');
391                         g.data.temp_callnumbers = undefined; g.data.stash('temp_callnumbers');
392                         if (!copies) {
393                                 alert('Items were not created.');
394                                 return;
395                         }
396                 }
397
398                 for (var i = 0; i < copies.length; i++) {
399                         var copy = copies[i];
400                         var volume = util.functional.find_id_object_in_list( volumes, copy.call_number() );
401                         var temp = volume.copies();
402                         temp.push( copy );
403                         volume.copies( temp );
404                 }
405
406                 try {
407                         var r = g.network.request(
408                                 api.FM_ACN_TREE_UPDATE.app,
409                                 api.FM_ACN_TREE_UPDATE.method,
410                                 [ ses(), volumes, true ]
411                         );
412                         if (typeof r.ilsevent != 'undefined') {
413                                 switch(r.ilsevent) {
414                                         case 1706 /* ITEM_BARCODE_EXISTS */ :
415                                                 alert('Some of these barcodes are or have been in use.  Please change them.');
416                                                 return;
417                                         break;
418                                         default: g.error.standard_unexpected_error_alert('volume tree update',r); break;
419                                 }
420                         } else {
421                                 alert('Copies added.');
422                                 if (copies.length > 0 && $('print_labels').checked) {
423                                         JSAN.use('util.functional');
424                                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
425                                         data.temp_barcodes_for_labels = util.functional.map_list( copies, function(o){return o.barcode();}) ; 
426                                         data.stash('temp_barcodes_for_labels');
427                                         var w = win.open(
428                                                 urls.XUL_SPINE_LABEL,
429                                                 'spine_labels',
430                                                 'chrome,resizable,width=750,height=550'
431                                         );
432                                 }
433                         }
434                 } catch(E) {
435                         g.error.standard_unexpected_error_alert('volume tree update 2',E);
436                 }
437
438                 if (typeof window.refresh == 'function') window.refresh();
439
440                 window.close();
441
442         } catch(E) {
443                 g.error.standard_unexpected_error_alert('volume tree update 3',E);
444         }
445 }
446
447