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