shortname vs id in volume wizard
[Evergreen.git] / Evergreen / staff_client / chrome / content / evergreen / cat / volume.js
1 var data = {}; var data_backup; var cn_list = [];
2 var new_id = -1;
3
4 function my_init() {
5         mw.sdump('D_CAT','entering my_init for volume.js\n');
6         mw.sdump('D_CAT','TESTING: volume.js: ' + mw.G['main_test_variable'] + '\n');
7         mw.sdump('D_CAT','record_id = ' + record_id + '\n');
8         mw.sdump('D_CAT','tree_items: ' + tree_items + '\n');
9         if (params.shortcut == 'volume_add') {
10                 build_page_one();
11         } else if (params.shortcut == 'copy_add') {
12                 build_page_two();
13         } else {
14                 mw.sdump('D_CAT','broken\n');
15         }
16         listen_for_enter('volume_add');
17         mw.sdump('D_CAT','exiting my_init for volume.js\n');
18 }
19
20 function listen_for_enter(w) {
21         if (typeof(w) != 'object') {
22                 w = document.getElementById(w);
23         }
24         w.addEventListener('keypress',
25                 function (ev) {
26                         mw.sdump('D_CAT','wizard: ev.target.tagName = ' + ev.target.tagName + '\n');
27                         mw.sdump('D_CAT','\tev.keyCode = ' + ev.keyCode + '\n');
28                         mw.sdump('D_CAT','\tev.charCode = ' + ev.charCode + '\n');
29                         if ((ev.target.tagName == 'textbox') && (ev.keyCode == 13)) {
30                                 ev.preventDefault();
31                                 ev.stopPropagation(); // XBL bindings? bleh
32                                 fake_tab_for_textboxes(w,ev.target);
33                                 return true;
34                         }
35                 },
36                 true
37         );
38 }
39
40 function page1_add_volume_row(ou) {
41         var row = document.createElement('row');
42                 row.setAttribute('id','p1_' + ou.id());
43         document.getElementById('page1_rows').appendChild(row);
44         var label = document.createElement('label');
45                 label.setAttribute('value',ou.name());
46         row.appendChild(label);
47         var textbox = document.createElement('textbox');
48                 textbox.setAttribute('size','1');
49                 textbox.setAttribute('value','0');
50         row.appendChild(textbox);
51 }
52
53 function page2_add_volume_row(ou,ti) {
54         mw.sdump('D_CAT','page2_add_volume_row...\n');
55         var desired_volumes = 
56                 document.getElementById('p1_' + ou.id()).lastChild.value;
57         mw.sdump('D_CAT','ou = ' + ou.name() + '  desired = ' + desired_volumes + '\n');
58         if (desired_volumes > 0) { } else { return; }
59         var rows = document.getElementById('page2_rows');
60         var row = document.createElement('row');
61         rows.appendChild(row);
62         var label = document.createElement('label');
63                 label.setAttribute('value',ou.name());
64         row.appendChild(label);
65         for (var i = 0; i < desired_volumes; i++) {
66                 var cn_row = document.createElement('row');
67                         //cn_row.setAttribute('id','p2_'+ou.id());
68                         cn_row.setAttribute('ou_name',ou.name());
69                         cn_row.setAttribute('ou_id',ou.id());
70                 rows.appendChild(cn_row);
71                 cn_row.appendChild( document.createElement('label') );
72                 var cn_text1 = document.createElement('textbox');
73                         cn_text1.setAttribute('size','20');
74                         cn_text1.setAttribute('value','A Call Number');
75                 cn_row.appendChild( cn_text1 );
76                 var cn_text2 = document.createElement('textbox');
77                         cn_text2.setAttribute('size','4');
78                         cn_text2.setAttribute('value','0');
79                 cn_row.appendChild( cn_text2 );
80         }
81 }
82
83 function copy_add_page2_add_volume_row(ou,ti) {
84         mw.sdump('D_CAT','copy_add_page2_add_volume_row...\n');
85         var rows = document.getElementById('page2_rows');
86         var row = document.getElementById('page2_row_cn_' + ou.id() );
87         if (!row) {
88                 row = document.createElement('row');
89                 row.setAttribute('id','page2_row_cn' + ou.id() );
90                 rows.appendChild(row);
91         }
92         var label = document.createElement('label');
93                 label.setAttribute('value',ou.name());
94         row.appendChild(label);
95                 var cn_row = document.createElement('row');
96                         //cn_row.setAttribute('id','p2_'+ou.id());
97                         cn_row.setAttribute('ou_name',ou.name());
98                         cn_row.setAttribute('ou_id',ou.id());
99                         cn_row.setAttribute('volume_id',ti.getAttribute('volume_id'));
100                 rows.appendChild(cn_row);
101                 cn_row.appendChild( document.createElement('label') );
102                 var cn_text1 = document.createElement('textbox');
103                         cn_text1.setAttribute('size','20');
104                         cn_text1.setAttribute('volume_id',ti.getAttribute('volume_id'));
105                         cn_text1.setAttribute('value',ti.getAttribute('callnumber'));
106                 cn_row.appendChild( cn_text1 );
107                 cn_text1.disabled = true;
108                 var cn_text2 = document.createElement('textbox');
109                         cn_text2.setAttribute('size','4');
110                         cn_text2.setAttribute('value','0');
111                 cn_row.appendChild( cn_text2 );
112 }
113
114 function page3_add_volume_row(id,data) {
115         if (data.length>0) { } else { return; }
116         var rows = document.getElementById('page3_rows');
117         var org_row = document.createElement('row');
118         rows.appendChild(org_row);
119                 var org_label = document.createElement('label');
120                         org_label.setAttribute('value',data[0].name);
121                 org_row.appendChild(org_label);
122         for (var i in data) {
123                 var callnumber = data[i].callnumber;
124                 var desired_copies = data[i].copies;
125                 var cn_row = document.createElement('row');
126                 rows.appendChild(cn_row);
127                 var cn_box = document.createElement('hbox');
128                 cn_row.appendChild(cn_box);
129                 cn_box.appendChild( document.createElement('spacer') );
130                 var cn_label = document.createElement('label');
131                         cn_label.setAttribute('value',callnumber);
132                 cn_box.appendChild(cn_label);
133                 for (var c = 0; c < desired_copies; c++) {
134                         var bc_row = document.createElement('row');
135                         rows.appendChild(bc_row);
136                         bc_row.appendChild( document.createElement('label') );
137                         var bc_text = document.createElement('textbox');
138                                 bc_text.setAttribute('size','15');
139                                 bc_text.setAttribute('ou_name',data[i].name);
140                                 bc_text.setAttribute('ou_id',id);
141                                 bc_text.setAttribute('volume_id',data[i].volume_id);
142                                 bc_text.setAttribute('callnumber',callnumber);
143                         bc_row.appendChild(bc_text);
144                 }
145         }
146 }
147
148 function page_four_add_volume_row(name,callnumber,barcode) {
149         mw.sdump('D_CAT','xul: name = ' + name + ' cn = ' + callnumber + ' bc = ' + barcode + '\n');
150         var listbox = document.getElementById('ephemeral_listbox');
151         var listitem = document.createElement('listitem');
152         listbox.appendChild(listitem);
153         var listcell1 = document.createElement('listcell');
154                 listcell1.setAttribute('label',name);
155         listitem.appendChild(listcell1);
156         var listcell2 = document.createElement('listcell');
157                 listcell2.setAttribute('label',callnumber);
158         listitem.appendChild(listcell2);
159         var listcell3 = document.createElement('listcell');
160                 listcell3.setAttribute('label',barcode);
161         listitem.appendChild(listcell3);
162
163 }
164
165 function build_page_one() {
166         mw.sdump('D_CAT','build_page_one\n');
167         for (var i in tree_items) {
168                 var ti = tree_items[i];
169                 switch( ti.getAttribute('object_type') ) {
170                         case 'org_unit' :
171                                 if (params.shortcut == 'volume_add') {
172                                         var id = ti.getAttribute('id').split('_')[2];
173                                         var ou = mw.G.org_tree_hash[id];
174                                         var check_ou = check_volume_ou_perm( id );
175                                         if ( check_ou ) {
176                                                 page1_add_volume_row( check_ou );
177                                         }
178                                 }
179                         break;
180                 }
181         }
182 }
183
184 function build_page_two() {
185         mw.sdump('D_CAT','build_page_two\n');
186         for (var i in tree_items) {
187                 var ti = tree_items[i];
188                 mw.sdump('D_CAT','Considering item with object_type = ' + ti.getAttribute('object_type') + '\n');
189                 switch( ti.getAttribute('object_type') ) {
190                         case 'org_unit' :
191                                 var id = ti.getAttribute('id').split('_')[2];
192                                 var ou = mw.G.org_tree_hash[id];
193                                 var check_ou = check_volume_ou_perm( id );
194                                 if ( check_ou ) {
195                                         page2_add_volume_row( check_ou, ti );
196                                 }
197                         break;
198                         case 'volume' :
199                                 if (params.shortcut == 'copy_add') {
200                                         var check_ou = find_ou( mw.G.user_ou , ti.getAttribute('ou_id') );
201                                         if (check_ou) {
202                                                 copy_add_page2_add_volume_row( check_ou, ti );  
203                                         }
204                                 }
205                         break;
206                 }
207         }
208 }
209
210 function build_page_three() {
211         mw.sdump('D_CAT','build_page_three\n');
212         var rows = document.getElementById('page2_rows');
213         for (var i = 0; i < rows.childNodes.length; i++) {
214                 var row = rows.childNodes[i];
215                 mw.sdump('D_CAT',row + '\n');
216                 var ou_id = row.getAttribute('ou_id');
217                 var ou_name = row.getAttribute('ou_name');
218                 var volume_id = row.getAttribute('volume_id');
219                 if (ou_id) {
220                         var call_number = row.childNodes[1].value;
221                         if (!call_number) { continue; }
222                         var desired_copies = row.childNodes[2].value;
223                         if (!data[ou_id]) { data[ou_id] = []; }
224                         data[ou_id].push( 
225                                 { 
226                                         'callnumber' : call_number, 
227                                         'copies' : desired_copies,
228                                         'name' : ou_name,
229                                         'volume_id' : volume_id
230                                 } 
231                         );
232                 }
233         }
234         for (var i in data) {
235                 mw.sdump('D_CAT','i: ' + i + ' data[i]: ' + js2JSON(data[i]) + '\n');
236                 page3_add_volume_row( i, data[i]);
237         }
238 }
239
240 function build_page_four() {
241         mw.sdump('D_CAT','build page four\n');
242         document.getElementById('volume_add').canAdvance = false;
243         var new_data = [];
244         var rows = document.getElementById('page3_rows');
245         var nl = rows.getElementsByTagName('textbox');
246         for (var i in nl) {
247                 if (typeof(nl[i])=='object') {
248                         var t = nl[i];
249                         var ou_id = t.getAttribute('ou_id');
250                         var ou_name = t.getAttribute('ou_name');
251                         var callnumber = t.getAttribute('callnumber');
252                         var volume_id = t.getAttribute('volume_id');
253                         var barcode = t.value;
254                         //page_four_add_volume_row(ou_name,callnumber,barcode);
255                         mw.sdump('D_CAT','t.tagName = ' + t.tagName + ' ou_id = ' + ou_id + ' cn = ' + callnumber + ' volume_id = ' + volume_id + ' bc = ' + barcode + '\n');
256                         if (! new_data[ou_id] ) { new_data[ou_id] = {}; }
257                         if (! new_data[ou_id][callnumber] ) {
258                                 new_data[ou_id][callnumber] = [];
259                         }
260                         if (! new_data[ou_id][callnumber]['barcode'] ) {
261                                 new_data[ou_id][callnumber]['barcode'] = [];
262                         }
263                         new_data[ou_id][callnumber].barcode.push(barcode);
264                         if (params.shortcut == 'copy_add') {
265                                 new_data[ou_id][callnumber].volume_id = volume_id;
266                         }
267                 }
268         }
269         cn_list = [];
270         for (var ou_id in new_data) {
271                 for (var cnum in new_data[ou_id]) {
272                         //var ou_shortname = find_ou(mw.G['org_tree'],ou_id).shortname();
273                         //var ou_shortname = mw.G.org_tree_hash[ou_id].shortname();
274                         var cn = new acn();
275                         cn.label(cnum);
276                         cn.owning_lib(ou_id);
277                         cn.record(record_id);
278                         if (params.shortcut == 'volume_add') {
279                                 cn.isnew(1);
280                                 cn.id(new_id--);
281                         } else if (params.shortcut == 'copy_add') {
282                                 cn.id( new_data[ou_id][cnum].volume_id );
283                         }
284                         cn.copies([]);
285         
286                         for (var c in new_data[ou_id][cnum].barcode) {
287                                 var cp = new acp();
288                                 cp.id(new_id--);
289                                 cp.isnew(1);
290                                 cp.barcode(new_data[ou_id][cnum].barcode[c]);
291                                 cp.circ_lib(    mw.G.org_tree_hash[ ou_id ]);
292                                 cn.copies().push(cp);
293                                 cp.stat_cat_entries( [] );
294                         }
295
296                         cn_list.push(cn);
297                 }
298         }
299         mw.sdump('D_CAT','Final data object: ' + js2JSON(cn_list) + '\n');
300         mw.sdump('D_CAT','Final data object: ' + cn_list + '\n');
301         spawn_local_legacy_copy_editor();
302 }
303
304 function send_to_bill() {
305         try {
306                 var result = user_request(
307                                 'open-ils.cat',
308                                 'open-ils.cat.asset.volume_tree.fleshed.batch.update',
309                                 [ mw.G['auth_ses'][0], cn_list]
310                 );
311                 mw.sdump('D_CAT','volume_tree.fleshed.batch.update result: ' + js2JSON(result) + '\n');
312         } catch(E) {
313                 handle_error(E);
314         }
315         refresh_spawning_browse_list();
316 }
317
318 function refresh_spawning_browse_list() {
319         try {
320                 params.refresh_func();
321         } catch(E) {
322                 mw.sdump('D_CAT','refresh_spawning_browse_list error: ' + js2JSON(E) + '\n');
323         }
324 }
325
326 function spawn_local_legacy_copy_editor(tab) {
327         mw.sdump('D_CAT','trying to spawn_copy_editor()\n');
328         var params = { 'select_all' : true };
329         var chrome = 'chrome://evergreen/content/cat/copy.xul';
330         var frame = document.getElementById('page4_iframe');
331         frame.setAttribute('src',chrome);
332         frame.setAttribute('flex','1');
333         frame.contentWindow.cn_list = cn_list;
334         frame.contentWindow.mw = mw;
335         frame.contentWindow.real_parentWindow = this;
336         frame.contentWindow.parentWindow = window.app_shell;
337         frame.contentWindow.params = params;
338 }
339
340 function backup_data() {
341         data_backup = data;
342 }
343
344 function restore_data() {
345         data = data_backup;
346 }
347
348 function check_volume_ou_perm(id) {
349         var top_ou = mw.G.org_tree_hash[ mw.G.user_ou.id() ];
350         var check_ou = find_ou(top_ou, id);
351         return check_ou;
352 }
353
354 // ***************************************************** Batch Volume Edit
355
356 function volume_edit_init() {
357         mw.sdump('D_CAT','TESTING: volume.js: ' + mw.G['main_test_variable'] + '\n');
358         build_batch_edit_page1();
359         listen_for_enter('volume_edit');
360 }
361
362 function build_batch_edit_page1() {
363         var rows = document.getElementById('page1_rows');
364
365         var org = {};
366
367         for (var i = 0; i < tree_items.length; i++) {
368                 var volume = tree_items[i];
369                 if (!  org[ volume.getAttribute('ou_id') ] ) {
370                         org[ volume.getAttribute('ou_id') ] = new Array();
371                 }
372                 org[ volume.getAttribute('ou_id') ].push( volume );
373         }
374
375         for (var i in org) {
376                 var row = document.createElement('row');
377                 rows.appendChild(row);
378                 var lib = document.createElement('label');
379                 row.appendChild(lib);
380                         lib.setAttribute( 'value', mw.G.org_tree_hash[i].shortname() );
381
382                 for (var j = 0; j < org[i].length; j++) {
383                         var volume = org[i][j];
384
385                         var vrow = document.createElement('row');
386                         rows.appendChild(vrow);
387                         vrow.appendChild( document.createElement('label') );
388
389                         var t = document.createElement('textbox');
390                         vrow.appendChild( t );
391                         t.value = volume.getAttribute('callnumber');
392                         t.setAttribute( 'original', volume.getAttribute('callnumber') );
393                         t.setAttribute( 'volume_id', volume.getAttribute('volume_id') );
394                 }
395         }
396 }
397
398 function submit_edited_volumes() {
399         var nl = document.getElementsByTagName('textbox');
400         for (var i = 0; i < nl.length; i++) {
401                 var t = nl[i];
402                 if (t.getAttribute('original') == t.value) continue;
403                 var cn = new acn();
404                 cn.id( t.getAttribute('volume_id') );
405                 cn.label( t.value );
406                 cn.ischanged('1');
407                 cn_list.push( cn );
408         }
409         send_to_bill();
410 }