]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/common/li_table.js
Acq: Fixed fund dropdowns for new copies and copies without funds.
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / common / li_table.js
1 dojo.require('dojo.date.locale');
2 dojo.require('dojo.date.stamp');
3 dojo.require('dijit.form.Button');
4 dojo.require('dijit.form.TextBox');
5 dojo.require('dijit.form.FilteringSelect');
6 dojo.require('dijit.form.Textarea');
7 dojo.require('dijit.Tooltip');
8 dojo.require('dijit.ProgressBar');
9 dojo.require('openils.User');
10 dojo.require('openils.Util');
11 dojo.require('openils.acq.Lineitem');
12 dojo.require('openils.acq.PO');
13 dojo.require('openils.acq.Picklist');
14 dojo.require('openils.widget.AutoFieldWidget');
15 dojo.require('dojo.data.ItemFileReadStore');
16 dojo.require('openils.widget.ProgressDialog');
17 dojo.require('openils.PermaCrud');
18 dojo.require('openils.XUL');
19
20 dojo.requireLocalization('openils.acq', 'acq');
21 var localeStrings = dojo.i18n.getLocalization('openils.acq', 'acq');
22 const XUL_OPAC_WRAPPER = 'chrome://open_ils_staff_client/content/cat/opac.xul';
23 var li_exportable_attrs = ["issn", "isbn", "upc"];
24
25 var fundLabelFormat = ['${0} (${1})', 'code', 'year'];
26 var fundSearchFormat = ['${0} (${1})', 'code', 'year'];
27
28 function nodeByName(name, context) {
29     return dojo.query('[name='+name+']', context)[0];
30 }
31
32
33 var liDetailBatchFields = ['fund', 'owning_lib', 'location', 'collection_code', 'circ_modifier', 'cn_label'];
34 var liDetailFields = liDetailBatchFields.concat(['barcode', 'note']);
35
36 function AcqLiTable() {
37
38     var self = this;
39     this.liCache = {};
40     this.plCache = {};
41     this.poCache = {};
42     this.relCache = {};
43     this.realDfaCache = {};
44     this.virtDfaCounts = {};
45     this.virtDfaId = -1;
46     this.dfeOffset = 0;
47     this.toggleState = false;
48     this.tbody = dojo.byId('acq-lit-tbody');
49     this.selectors = [];
50     this.noteAcks = {};
51     this.authtoken = openils.User.authtoken;
52     this.pcrud = new openils.PermaCrud();
53     this.rowTemplate = this.tbody.removeChild(dojo.byId('acq-lit-row'));
54     this.copyTbody = dojo.byId('acq-lit-li-details-tbody');
55     this.copyRow = this.copyTbody.removeChild(dojo.byId('acq-lit-li-details-row'));
56     this.copyBatchRow = dojo.byId('acq-lit-li-details-batch-row');
57     this.copyBatchWidgets = {};
58     this.liNotesTbody = dojo.byId('acq-lit-notes-tbody');
59     this.liNotesRow = this.liNotesTbody.removeChild(dojo.byId('acq-lit-notes-row'));
60     this.realCopiesTbody = dojo.byId('acq-lit-real-copies-tbody');
61     this.realCopiesRow = this.realCopiesTbody.removeChild(dojo.byId('acq-lit-real-copies-row'));
62     this._copy_fields_for_acqdf = ['owning_lib', 'location'];
63
64     dojo.connect(acqLitLiActionsSelector, 'onChange', 
65         function() { 
66             self.applySelectedLiAction(this.attr('value')) 
67             acqLitLiActionsSelector.attr('value', '_');
68         });
69
70     acqLitCreatePoSubmit.onClick = function() {
71         if (self._confirmPoPrepaySituation()) {
72             acqLitPoCreateDialog.hide();
73             self._createPO(acqLitPoCreateDialog.getValues());
74         } else {
75             return false;
76         }
77     }
78
79     acqLitSavePlButton.onClick = function() {
80         acqLitSavePlDialog.hide();
81         self._savePl(acqLitSavePlDialog.getValues());
82     }
83
84     acqLitCancelLiStateButton.onClick = function() {
85         acqLitChangeLiStateDialog.hide();
86     }
87     acqLitSaveLiStateButton.onClick = function() {
88         acqLitChangeLiStateDialog.hide();
89         self._updateLiState(acqLitChangeLiStateDialog.getValues(), acqLitChangeLiStateDialog.attr('state'));
90     }
91
92
93     dojo.byId('acq-lit-select-toggle').onclick = function(){self.toggleSelect()};
94     dojo.byId('acq-lit-info-back-button').onclick = function(){self.show('list')};
95     dojo.byId('acq-lit-copies-back-button').onclick = function(){self.show('list')};
96     dojo.byId('acq-lit-notes-back-button').onclick = function(){self.show('list')};
97     dojo.byId('acq-lit-real-copies-back-button').onclick = function(){self.show('list')};
98
99     this.reset = function() {
100         while(self.tbody.childNodes[0])
101             self.tbody.removeChild(self.tbody.childNodes[0]);
102         self.selectors = [];
103         self.noteAcks = {};
104         self.relCache = {};
105     };
106     
107     this.setNext = function(handler) {
108         var link = dojo.byId('acq-lit-next');
109         if(handler) {
110             dojo.style(link, 'visibility', 'visible');
111             link.onclick = handler;
112         } else {
113             dojo.style(link, 'visibility', 'hidden');
114         }
115     };
116
117     this.setPrev = function(handler) {
118         var link = dojo.byId('acq-lit-prev');
119         if(handler) {
120             dojo.style(link, 'visibility', 'visible'); 
121             link.onclick = handler; 
122         } else {
123             dojo.style(link, 'visibility', 'hidden');
124         }
125     };
126
127     this.show = function(div) {
128         openils.Util.hide('acq-lit-table-div');
129         openils.Util.hide('acq-lit-info-div');
130         openils.Util.hide('acq-lit-li-details');
131         openils.Util.hide('acq-lit-notes-div');
132         openils.Util.hide('acq-lit-real-copies-div');
133         switch(div) {
134             case 'list':
135                 openils.Util.show('acq-lit-table-div');
136                 break;
137             case 'info':
138                 openils.Util.show('acq-lit-info-div');
139                 break;
140             case 'copies':
141                 openils.Util.show('acq-lit-li-details');
142                 break;
143             case 'real-copies':
144                 openils.Util.show('acq-lit-real-copies-div');
145                 break;
146             case 'notes':
147                 openils.Util.show('acq-lit-notes-div');
148                 break;
149             default:
150                 if(div) 
151                     openils.Util.show(div);
152         }
153     }
154
155     this.hide = function() {
156         this.show(null);
157     }
158
159     this.toggleSelect = function() {
160         if(self.toggleState) 
161             dojo.forEach(self.selectors, function(i){i.checked = false});
162         else 
163             dojo.forEach(self.selectors, function(i){i.checked = true});
164         self.toggleState = !self.toggleState;
165     };
166
167
168     /** @param all If true, assume all are selected */
169     this.getSelected = function(all) {
170         var selected = [];
171         dojo.forEach(self.selectors, 
172             function(i) { 
173                 if(i.checked || all)
174                     selected.push(self.liCache[i.parentNode.parentNode.getAttribute('li')]);
175             }
176         );
177         return selected;
178     };
179
180     this.setRowAttr = function(td, liWrapper, field, type) {
181         var val = liWrapper.findAttr(field, type || 'lineitem_marc_attr_definition') || '';
182         td.appendChild(document.createTextNode(val));
183     };
184
185     /**
186      * Inserts a single lineitem into the growing table of lineitems
187      * @param {Object} li The lineitem object to insert
188      */
189     this.addLineitem = function(li, skip_final_placement) {
190         this.liCache[li.id()] = li;
191
192         // sort the lineitem notes on edit_time
193         if(!li.lineitem_notes()) li.lineitem_notes([]);
194
195         var liWrapper = new openils.acq.Lineitem({lineitem:li});
196         var row = self.rowTemplate.cloneNode(true);
197         row.setAttribute('li', li.id());
198         var tds = dojo.query('[attr]', row);
199         dojo.forEach(tds, function(td) {self.setRowAttr(td, liWrapper, td.getAttribute('attr'), td.getAttribute('attr_type'));});
200         dojo.query('[name=source_label]', row)[0].appendChild(document.createTextNode(li.source_label()));
201
202         var isbn = liWrapper.findAttr('isbn', 'lineitem_marc_attr_definition');
203         if(isbn) {
204             // XXX media prefix for added content
205             dojo.query('[name=jacket]', row)[0].setAttribute('src', '/opac/extras/ac/jacket/small/' + isbn);
206         }
207
208         nodeByName("worksheet_link", row).href =
209             "/eg/acq/lineitem/worksheet/" + li.id();
210
211         dojo.query('[attr=title]', row)[0].onclick = function() {self.drawInfo(li.id())};
212         dojo.query('[name=copieslink]', row)[0].onclick = function() {self.drawCopies(li.id())};
213         dojo.query('[name=noteslink]', row)[0].onclick = function() {self.drawLiNotes(li)};
214
215         this.updateLiNotesCount(li, row);
216
217         // show which PO this lineitem is a member of
218         if(li.purchase_order() && !this.isPO) {
219             var po = 
220                 this.poCache[li.purchase_order()] =
221                 this.poCache[li.purchase_order()] ||
222                 fieldmapper.standardRequest(
223                     ['open-ils.acq', 'open-ils.acq.purchase_order.retrieve'],
224                     {params: [
225                         this.authtoken, li.purchase_order(), {
226                             "flesh_price_summary": true,
227                             "flesh_lineitem_count": true
228                         }
229                     ]});
230             if(po && !this.isMeta) {
231                 openils.Util.show(nodeByName('po', row), 'inline');
232                 var link = nodeByName('po_link', row);
233                 link.setAttribute('href', oilsBasePath + '/acq/po/view/' + li.purchase_order());
234                 link.innerHTML = 'PO: ' + po.name(); // TODO i18n
235             }
236         }
237
238         // show which picklist this lineitem is a member of
239         if(li.picklist() && (this.isPO || this.isMeta)) {
240             var pl = 
241                 this.plCache[li.picklist()] = 
242                 this.plCache[li.picklist()] || 
243                 fieldmapper.standardRequest(
244                     ['open-ils.acq', 'open-ils.acq.picklist.retrieve'],
245                     {params: [this.authtoken, li.picklist()]});
246             if(pl) {
247                 openils.Util.show(nodeByName('pl', row), 'inline');
248                 var link = nodeByName('pl_link', row);
249                 link.setAttribute('href', oilsBasePath + '/acq/picklist/view/' + li.picklist());
250                 link.innerHTML = 'PL: '+pl.name(); // TODO i18n
251             }
252         }
253
254         var countNode = nodeByName('count', row);
255         var count = li.item_count() || 0;
256         if (typeof(this._copy_count_cb) == "function") {
257             this._copy_count_cb(li.id(), count);
258         }
259         countNode.innerHTML = count;
260         countNode.id = 'acq-lit-copy-count-label-' + li.id();
261
262         // lineitem price
263         var priceInput = dojo.query('[name=price]', row)[0];
264         priceInput.value = li.estimated_unit_price() || '';
265         priceInput.onchange = function() { self.updateLiPrice(priceInput, li) };
266
267         // show either "mark received" or "unreceive" as appropriate
268         this.updateLiState(li, row);
269
270         if (!skip_final_placement) {
271             self.tbody.appendChild(row);
272             self.selectors.push(dojo.query('[name=selectbox]', row)[0]);
273         } else {
274             return row;
275         }
276     };
277
278     this.updateLiNotesCount = function(li, row) {
279         if (typeof(row) == "undefined")
280             row = dojo.query('tr[li="' + li.id() + '"]', "acq-lit-tbody")[0];
281
282         var has_notes = (li.lineitem_notes().filter(
283                 function(o) { return Boolean (o.alert_text()); }
284             ).length > 0);
285
286         /* U+2691 is the code point for a filled-in flag character */
287         nodeByName("notes_alert_flag", row).innerHTML =
288              has_notes ? "⚑" : "";
289         nodeByName("noteslink", row).style.fontStyle =
290             has_notes ? "italic" : "normal";
291         nodeByName("notes_count", row).innerHTML = li.lineitem_notes().length;
292     };
293
294     /* XXX NOT related to _updateLiState(). rethink */
295     this.updateLiState = function(li, row) {
296         if (typeof(row) == "undefined")
297             row = dojo.query('tr[li="' + li.id() + '"]', "acq-lit-tbody")[0];
298
299         var recv_link = nodeByName("receive_link", row);
300         var unrecv_link = nodeByName("unreceive_link", row);
301         var real_copies_link = nodeByName("real_copies_link", row);
302         var holdings_maintenance_link = nodeByName("holdings_maintenance_link", row);
303         var state_cell = nodeByName("li_state", row);
304
305         if (li.state() == "cancelled") {
306             if (typeof li.cancel_reason() == "object") {
307                 var holds_state = dojo.create(
308                     "span", {
309                         "style": "border-bottom: 1px dashed #000;",
310                         "innerHTML": li.state()
311                     }, state_cell, "only"
312                 );
313                 new dijit.Tooltip(
314                     {
315                         "label": "<em>" + li.cancel_reason().label() +
316                             "</em><br />" + li.cancel_reason().description(),
317                         "connectId": [holds_state]
318                     }, dojo.create("span", null, state_cell, "last")
319                 );
320             } else {
321                 state_cell.innerHTML = li.state(); // TODO i18n state labels
322             }
323         } else {
324             state_cell.innerHTML = li.state(); // TODO i18n state labels
325         }
326
327
328         /* handle row coloring for based on LI state */
329         openils.Util.removeCSSClass(row, /^oils-acq-li-state-/);
330         openils.Util.addCSSClass(row, "oils-acq-li-state-" + li.state());
331
332         /* handle links that appear/disappear based on whether LI is received */
333         if (this.isPO) {
334             var self = this;
335             switch(li.state()) {
336                 case "on-order":
337                     openils.Util.hide(real_copies_link);
338                     openils.Util.hide(unrecv_link);
339                     openils.Util.show(recv_link, "inline");
340                     recv_link.onclick = function() {
341                         if (self.checkLiAlerts(li.id()))
342                             self.issueReceive(li);
343                     };
344                     return;
345                 case "received":
346                     openils.Util.hide(recv_link);
347                     openils.Util.show(unrecv_link, "inline");
348                     unrecv_link.onclick = function() {
349                         if (confirm(localeStrings.UNRECEIVE_LI))
350                             self.issueReceive(li, /* rollback */ true);
351                     };
352                     // TODO we should allow editing before receipt, in which case the
353                     // test should be "if 1 or more real (acp) copies exist
354                     openils.Util.show(real_copies_link, "inline");
355                     real_copies_link.onclick = function() {
356                         self.showRealCopyEditUI(li);
357                     }
358                     openils.Util.show(holdings_maintenance_link, "inline");
359                     holdings_maintenance_link.onclick = self.generateMakeRecTab( li.eg_bib_id(), 'copy_browser' );
360                     return;
361             }
362         }
363
364         openils.Util.hide(recv_link);
365         openils.Util.hide(unrecv_link);
366         openils.Util.hide(real_copies_link);
367     };
368
369
370     this._setAlertStore = function() {
371         acqLitAlertAlertText.store = new dojo.data.ItemFileReadStore(
372             {
373                 "data": acqliat.toStoreData(
374                     this.pcrud.search(
375                         "acqliat", {"id": {"!=": null}}
376                     )
377                 )
378             }
379         );
380         acqLitAlertAlertText.setValue(); /* make the store "live" */
381         acqLitAlertAlertText._store_ready = true;
382     };
383
384     /**
385      * Draws and shows the lineitem notes pane
386      */
387     this.drawLiNotes = function(li) {
388         var self = this;
389
390         if (!acqLitAlertAlertText._store_ready)
391             this._setAlertStore();
392
393         li.lineitem_notes(
394             li.lineitem_notes().sort(
395                 function(a, b) { 
396                     if(a.edit_time() < b.edit_time()) return 1;
397                     return -1;
398                 }
399             )
400         );
401
402         while(this.liNotesTbody.childNodes[0])
403             this.liNotesTbody.removeChild(this.liNotesTbody.childNodes[0]);
404         this.show('notes');
405
406         acqLitCreateNoteSubmit.onClick = function() {
407             var value = acqLitCreateNoteText.attr('value');
408             if(!value) return;
409             var note = new fieldmapper.acqlin();
410             note.isnew(true);
411             note.vendor_public(
412                 Boolean(acqLitCreateNoteVendorPublic.attr('checked'))
413             );
414             note.value(value);
415             note.lineitem(li.id());
416
417             self.updateLiNotes(li, note);
418             acqLitCreateNoteVendorPublic.attr("checked", false);
419             acqLitCreateNoteText.attr("value", "");
420         }
421
422         acqLitCreateAlertSubmit.onClick = function() {
423             if (!acqLitAlertAlertText.item) {
424                 alert(localeStrings.ALERT_UNSELECTED);
425                 return;
426             }
427
428             var alert_text = new fieldmapper.acqliat().fromStoreItem(
429                 acqLitAlertAlertText.item
430             );
431             var value = acqLitAlertNoteValue.attr("value") || "";
432
433             var note = new fieldmapper.acqlin();
434             note.isnew(true);
435             note.lineitem(li.id());
436             note.value(value);
437             note.alert_text(alert_text);
438
439             self.updateLiNotes(li, note);
440         }
441
442         dojo.forEach(li.lineitem_notes(), function(note) { self.addLiNote(li, note) });
443     }
444
445     /**
446      * Draws a single lineitem note in the notes pane
447      */
448     this.addLiNote = function(li, note) {
449         if(note.isdeleted()) return;
450         var self = this;
451         var row = self.liNotesRow.cloneNode(true);
452         nodeByName("value", row).innerHTML = note.value();
453         var alert_node = nodeByName("alert_code", row);
454         if (note.alert_text()) {
455             alert_node.innerHTML = note.alert_text().code();
456             if (note.alert_text().description()) {
457                 new dijit.Tooltip(
458                     {
459                         "connectId": [alert_node],
460                         "label": note.alert_text().description()
461                     }, dojo.create("span", null, alert_node, "after")
462                 );
463             }
464         }
465
466         if (openils.Util.isTrue(note.vendor_public()))
467             nodeByName("vendor_public", row).innerHTML =
468                 localeStrings.VENDOR_PUBLIC;
469
470         nodeByName("delete", row).onclick = function() {
471             note.isdeleted(true);
472             self.liNotesTbody.removeChild(row);
473             self.updateLiNotes(li);
474         };
475
476         if(note.edit_time()) {
477             nodeByName("edit_time", row).innerHTML =
478                 dojo.date.locale.format(
479                     dojo.date.stamp.fromISOString(note.edit_time()), 
480                     {formatLength:'short'});
481         }
482
483         self.liNotesTbody.appendChild(row);
484     }
485
486     /**
487      * Updates any new/changed/deleted notes on the server
488      */
489     this.updateLiNotes = function(li, newNote) {
490
491         var notes;
492         if(newNote) {
493             notes = [newNote];
494         } else {
495             notes = li.lineitem_notes().filter(
496                 function(note) {
497                     if(note.ischanged() || note.isnew() || note.isdeleted())
498                         return note;
499                 }
500             );
501         }
502
503         if(notes.length == 0) return;
504         progressDialog.show();
505
506         fieldmapper.standardRequest(
507             ['open-ils.acq', 'open-ils.acq.lineitem_note.cud.batch'],
508             {   async : true,
509                 params : [this.authtoken, notes],
510                 onresponse : function(r) {
511                     var resp = openils.Util.readResponse(r);
512
513                     if(resp.complete) {
514
515                         if(!newNote) {
516                             // remove the old changed notes
517                             var list = [];
518                             dojo.forEach(li.lineitem_notes(), 
519                                 function(note) {
520                                     if(!(note.ischanged() || note.isnew() || note.isdeleted()))
521                                         list.push(note);
522                                 }
523                             );
524                             li.lineitem_notes(list);
525                         }
526
527                         progressDialog.hide();
528                         self.updateLiNotesCount(li);
529                         self.drawLiNotes(li);
530                         return;
531                     }
532
533                     progressDialog.update(resp);
534                     var newnote = resp.note;
535
536                     if(!newnote.isdeleted()) {
537                         newnote.isnew(false);
538                         newnote.ischanged(false);
539                         li.lineitem_notes().push(newnote);
540                     }
541                 },
542             }
543         );
544     }
545
546     this.updateLiPrice = function(input, li) {
547
548         var price = input.value;
549         if(Number(price) == Number(li.estimated_unit_price())) return;
550
551         fieldmapper.standardRequest(
552             ['open-ils.acq', 'open-ils.acq.lineitem.price.set'],
553             {   async : true,
554                 params : [this.authtoken, li.id(), price],
555                 oncomplete : function(r) {
556                     openils.Util.readResponse(r);
557                 }
558             }
559         );
560     }
561
562     this.removeLineitem = function(liId) {
563         this.tbody.removeChild(dojo.query('[li='+liId+']', this.tbody)[0]);
564         delete this.liCache[liId];
565         //selected.push(self.liCache[i.parentNode.parentNode.getAttribute('li')]);
566     }
567
568     this.drawInfo = function(liId) {
569         if (!this._isRelatedViewer) {
570             var d = dojo.byId("acq-lit-info-related");
571             if (!this.relCache[liId]) {
572                 fieldmapper.standardRequest(
573                     [
574                         "open-ils.acq",
575                         "open-ils.acq.lineitems_for_bib.by_lineitem_id.count"
576                     ], {
577                         "async": true,
578                         "params": [openils.User.authtoken, liId],
579                         "onresponse": function(r) {
580                             self.relCache[liId] = openils.Util.readResponse(r);
581                             nodeByName("related_number", d).innerHTML =
582                                 self.relCache[liId];
583                             openils.Util[
584                                 self.relCache[liId] >1 ? "show" : "hide"
585                             ](d);
586                         }
587                     }
588                 );
589             } else {
590                 nodeByName("related_number", d).innerHTML = this.relCache[liId];
591                 openils.Util[this.relCache[liId] > 1 ? "show" : "hide"](d);
592             }
593         }
594
595         this.show('info');
596         openils.acq.Lineitem.fetchAttrDefs(
597             function() { 
598                 self._fetchLineitem(liId, function(li){self._drawInfo(li);}); 
599             } 
600         );
601     };
602
603     this._fetchLineitem = function(liId, handler) {
604
605         var li = this.liCache[liId];
606         if(li && li.marc() && li.lineitem_details())
607             return handler(li);
608         
609         fieldmapper.standardRequest(
610             ['open-ils.acq', 'open-ils.acq.lineitem.retrieve'],
611             {   async: true,
612
613                 params: [self.authtoken, liId, {
614                     flesh_attrs: true,
615                     flesh_cancel_reason: true,
616                     flesh_li_details: true,
617                     flesh_fund_debit: true }],
618
619                 oncomplete: function(r) {
620                     var li = openils.Util.readResponse(r);
621                     handler(li)
622                 }
623             }
624         );
625     };
626
627     this._drawInfo = function(li) {
628
629         acqLitEditOrderMarc.onClick = function() { self.editOrderMarc(li); }
630
631         if(li.eg_bib_id()) {
632             openils.Util.hide('acq-lit-marc-order-record-label');
633             openils.Util.hide(acqLitEditOrderMarc.domNode);
634             openils.Util.show('acq-lit-marc-real-record-label');
635         } else {
636             openils.Util.show('acq-lit-marc-order-record-label');
637             openils.Util.show(acqLitEditOrderMarc.domNode);
638             openils.Util.hide('acq-lit-marc-real-record-label');
639         }
640
641         this.drawMarcHTML(li);
642         this.infoTbody = dojo.byId('acq-lit-info-tbody');
643
644         if(!this.infoRow)
645             this.infoRow = this.infoTbody.removeChild(dojo.byId('acq-lit-info-row'));
646         while(this.infoTbody.childNodes[0])
647             this.infoTbody.removeChild(this.infoTbody.childNodes[0]);
648
649         if (!this._isRelatedViewer) {
650             for(var i = 0; i < li.attributes().length; i++) {
651                 var attr = li.attributes()[i];
652                 var row = this.infoRow.cloneNode(true);
653
654                 var type = attr.attr_type().replace(/lineitem_(.*)_attr_definition/, '$1');
655                 var name = openils.acq.Lineitem.attrDefs[type].filter(
656                     function(a) {
657                         return (a.code() == attr.attr_name());
658                     }
659                 ).pop().description();
660
661                 dojo.query('[name=label]', row)[0].appendChild(document.createTextNode(name));
662                 dojo.query('[name=value]', row)[0].appendChild(document.createTextNode(attr.attr_value()));
663                 this.infoTbody.appendChild(row);
664             }
665
666             nodeByName("rel_link", dojo.byId("acq-lit-info-related")).href =
667                 "/eg/acq/lineitem/related/" + li.id();
668         }
669
670         if(li.eg_bib_id()) {
671
672             openils.Util.show('acq-lit-info-cat-link');
673             var link = dojo.byId('acq-lit-info-cat-link').getElementsByTagName('a')[0];
674
675             if(openils.XUL.isXUL()) {
676
677                 link.setAttribute('href', 'javascript:void(0);');
678                 link.onclick = this.generateMakeRecTab( li.eg_bib_id() );
679
680             } else {
681                 var href = link.getAttribute('href');
682                 if(href.match(/=$/))
683                     link.setAttribute('href',  href + li.eg_bib_id());
684             }
685         } else {
686             openils.Util.hide('acq-lit-info-cat-link');
687         }
688     };
689
690     this.generateMakeRecTab = function(bib_id,default_view) {
691         return function() {
692             xulG.new_tab(
693                 XUL_OPAC_WRAPPER,
694                 {tab_name: localeStrings.XUL_RECORD_DETAIL_PAGE, browser:false},
695                 {
696                     no_xulG : false, 
697                     show_nav_buttons : true, 
698                     show_print_button : true, 
699                     opac_url : xulG.url_prefix(xulG.urls.opac_rdetail + '?r=' + bib_id),
700                     default_view : default_view
701                 }
702             );
703         }
704     };
705
706     this.drawMarcHTML = function(li) {
707         var params = [null, true, li.marc()];
708         if(li.eg_bib_id()) 
709             params = [li.eg_bib_id(), true];
710
711         fieldmapper.standardRequest(
712             ['open-ils.search', 'open-ils.search.biblio.record.html'],
713             {   async: true,
714                 params: params,
715                 oncomplete: function(r) {
716                     dojo.byId('acq-lit-marc-div').innerHTML = 
717                         openils.Util.readResponse(r);
718                 }
719             }
720         );
721     }
722
723     this.drawCopies = function(liId) {
724         this.show('copies');
725         var self = this;
726         this.copyCache = {};
727         this.copyWidgetCache = {};
728         this.oldCopyWidgetCache = {};
729         this.virtDfaCounts = {};
730         this.realDfaCache = {};
731         this.dfeOffset = 0;
732
733         acqLitSaveCopies.onClick = function() { self.saveCopyChanges(liId) };
734         acqLitBatchUpdateCopies.onClick = function() { self.batchCopyUpdate() };
735         acqLitCopyCountInput.attr('value', '0');
736
737         while(this.copyTbody.childNodes[0])
738             this.copyTbody.removeChild(this.copyTbody.childNodes[0]);
739
740         this._drawBatchCopyWidgets();
741
742         this._drawDistribApplied(liId);
743
744         this._fetchDistribFormulas(
745             function() {
746                 openils.acq.Lineitem.fetchAttrDefs(
747                     function() { 
748                         self._fetchLineitem(liId, function(li){self._drawCopies(li);}); 
749                     } 
750                 );
751             }
752         );
753     };
754
755     this._saveDistribAppliedTemplates = function() {
756         if (!this._appliedDistribTemplate) {
757             this._appliedDistribTemplate =
758                 dojo.byId("acq-lit-distrib-applied-tbody").
759                     removeChild(dojo.byId("acq-lit-distrib-applied-row"));
760             dojo.attr(this._appliedDistribTemplate, "id");
761         }
762     };
763
764     this._drawDistribApplied = function(liId) {
765         /* Build this table while hidden to prevent rendering artifacts */
766         openils.Util.hide("acq-lit-distrib-applied-tbody");
767
768         this._saveDistribAppliedTemplates();
769
770         /* Remove any rows in the table from previous populations */
771         dojo.query("tr[formula]", "acq-lit-distrib-applied-tbody").
772             forEach(dojo.destroy);
773
774         /* Unregister all dijits previously created (for some reason this isn't
775          * covered by the above destroy calls). */
776         dijit.registry.forEach(
777             function(w) { if (/^dfa-/.test(w.id)) w.destroyRecursive(); }
778         );
779
780         /* Populate the table with our liId */
781         var total = 0;
782         fieldmapper.standardRequest(
783             ["open-ils.acq",
784             "open-ils.acq.distribution_formula_application.ranged.retrieve"],
785             {
786                 "async": true,
787                 "params": [self.authtoken, liId],
788                 "onresponse": function(r) {
789                     var dfa = openils.Util.readResponse(r);
790                     if (dfa) {
791                         total++;
792                         self.realDfaCache[dfa.id()] = dfa;
793                         self._drawDistribAppliedUnit(dfa);
794                     }
795                 },
796                 "oncomplete": function() {
797                     /* Reveal built table */
798                     if (total) {
799                         openils.Util.show(
800                             "acq-lit-distrib-applied-tbody", "table-row-group"
801                         );
802                     }
803                 }
804             }
805         );
806     };
807
808     this._drawDistribAppliedUnit = function(dfa) {
809         var new_row = false;
810         var row = dojo.query(
811             'tr[formula="' + dfa.formula().id() + '"]',
812             "acq-lit-distrib-applied-tbody"
813         )[0];
814
815         if (!row) {
816             new_row = true;
817             row = dojo.clone(this._appliedDistribTemplate);
818             dojo.attr(row, "formula", dfa.formula().id());
819             dojo.query("th", row)[0].innerHTML = dfa.formula().name();
820         }
821
822         var td = dojo.query("td", row)[0];
823
824         dojo.create("span", {"id": "dfa-button-" + dfa.id()}, td, "last");
825         dojo.create("span", {"id": "dfa-tip-" + dfa.id()}, td, "last");
826
827         if (new_row)
828             dojo.place(row, "acq-lit-distrib-applied-tbody", "last");
829
830         new dijit.form.Button(
831             {
832                 "onClick": function() {
833                     if (confirm(localeStrings.EXPLAIN_DFA_MGMT))
834                         self.deleteDfa(dfa);
835                 },
836                 "label": "X",
837                 /* XXX I /cannot/ make the following work in as a CSS class
838                  * for some reason. So frustrating... */
839                 "style": function(id) {
840                      return (id > 0 ?
841                         "font-weight: bold; color: #c00;" :
842                         "color: #666;");
843                      }(dfa.id()) + "margin: 0 6px;display: inline;"
844             }, "dfa-button-" + dfa.id()
845         );
846         new dijit.Tooltip(
847             {
848                 "connectId": ["dfa-button-" + dfa.id()],
849                 "label": dojo.string.substitute(
850                     localeStrings.DFA_TIP, dfa.id() > 0 ? [
851                         openils.User.formalName(dfa.creator()),
852                         dojo.date.locale.format(
853                             dojo.date.stamp.fromISOString(dfa.create_time()),
854                             {"formatLength":"short"}
855                         )
856                     ] : [localeStrings.ITS_YOU, localeStrings.JUST_NOW]
857                 )
858             }, "dfa-tip-" + dfa.id()
859         );
860     }
861
862     this.deleteDfa = function(dfa) {
863         if (dfa.id() > 0) { /* real */
864             this.pcrud.eliminate(
865                 dfa, {
866                     "async": true,
867                     "oncomplete": function() {
868                         self._removeDistribApplied(dfa.id());
869                         delete self.realDfaCache[dfa.id()];
870                     }
871                 }
872             );
873         } else { /* virtual */
874             if (--(this.virtDfaCounts[dfa.formula().id()]) < 0)
875             this.virtDfaCounts[dfa.formula().id()] = 0;
876             /* hasn't been saved yet, so no need to do anything server side */
877             this._removeDistribApplied(dfa.id());
878         }
879
880     };
881
882     this._removeDistribApplied = function(dfaId) {
883         var re = new RegExp("^dfa-\\w+-" + String(dfaId));
884         dijit.registry.forEach(
885             function(w) { if (re.test(w.id)) w.destroyRecursive(); }
886         );
887         this._removeDistribAppliedEmptyRows();
888     };
889
890     this._removeAllDistribAppliedVirtual = function() {
891         /* Unregister dijits */
892         dijit.registry.forEach(
893             function(w) { if (/^dfa-\w+--/.test(w.id)) w.destroyRecursive(); }
894         );
895         this._removeDistribAppliedEmptyRows();
896     };
897
898     this._removeDistribAppliedEmptyRows = function() {
899         /* Remove any rows with no DFA at all */
900         dojo.query("tr[formula] td", "acq-lit-distrib-applied-tbody").forEach(
901             function(o) {
902                 if (o.childNodes.length < 1) dojo.destroy(o.parentNode);
903             }
904         );
905     };
906
907     /**
908      * Insert a new row into the distribution formula selection form
909      */
910     this._addDistribFormulaRow = function() {
911         var self = this;
912
913         if (!self.distribForms) {
914             // no formulas, hide the form
915             openils.Util.hide('acq-lit-distrib-formula-tbody');
916             return;
917         }
918
919         if(!this.distribFormulaTemplate) 
920             this.distribFormulaTemplate = 
921                 dojo.byId('acq-lit-distrib-formula-tbody').removeChild(dojo.byId('acq-lit-distrib-form-row'));
922
923         var row = this.distribFormulaTemplate.cloneNode(true);
924         dojo.place(row, "acq-lit-distrib-formula-tbody", "only");
925
926         this.dfSelector = new dijit.form.FilteringSelect(
927             {"labelAttr": "dynLabel", "labelType": "html"},
928             nodeByName("selector", row)
929         );
930         this._updateFormulaStore();
931         this.dfSelector.fetchProperties =
932             {"sort": [{"attribute": "use_count", "descending": true}]};
933
934         var apply = new dijit.form.Button(
935             {"label": localeStrings.APPLY},
936             nodeByName('set_button', row)
937         ); 
938
939         var reset = new dijit.form.Button(
940             {"label": localeStrings.RESET_FORMULAE, "disabled": true},
941             nodeByName("reset_button", row)  
942         );
943
944         dojo.connect(apply, 'onClick', 
945             function() {
946                 var form_id = self.dfSelector.attr("value");
947                 if(!form_id) return;
948                 self._applyDistribFormula(form_id);
949                 reset.attr("disabled", false);
950             }
951         );
952
953         dojo.connect(reset, 'onClick', 
954             function() {
955                 self.restoreCopyFieldsBeforeDF();
956                 self.virtDfaCounts = {};
957                 self.virtDfaId = -1;
958                 self.dfeOffset = 0;
959                 self._updateFormulaStore();
960                 self._removeAllDistribAppliedVirtual();
961                 reset.attr("disabled", "true");
962             }
963         );
964
965     };
966
967     /**
968      * Applies a distrib formula to the current set of copies
969      */
970     this._applyDistribFormula = function(formula) {
971         if(!formula) return;
972
973         formula = this.distribForms.filter(
974             function(form) { return form.id() == formula; }
975         )[0];
976
977         var copyRows = dojo.query('tr', self.copyTbody);
978
979         if (this.dfeOffset >= copyRows.length) {
980             alert(localeStrings.OUT_OF_COPIES);
981             return;
982         }
983
984         var entries_applied = 0;
985         for(
986             var rowIndex = this.dfeOffset;
987             rowIndex < copyRows.length;
988             rowIndex++
989         ) {
990             
991             var row = copyRows[rowIndex];
992             var copy_id = row.getAttribute('copy_id');
993             var copyWidgets = this.copyWidgetCache[copy_id];
994             var entryIndex = this.dfeOffset;
995             var entry = null;
996
997             // find the correct entry for the current row
998             dojo.forEach(formula.entries(), 
999                 function(e) {
1000                     if(!entry) {
1001                         entryIndex += e.item_count();
1002                         if(entryIndex > rowIndex)
1003                             entry = e;
1004                     }
1005                 }
1006             );
1007
1008             if(entry) {
1009                 
1010                 //console.log("rowIndex = " + rowIndex + ", entry = " + entry.id() + ", entryIndex=" + 
1011                 //  entryIndex + ", owning_lib = " + entry.owning_lib() + ", location = " + entry.location());
1012     
1013                 entries_applied++;
1014                 this.saveCopyFieldsBeforeDF(copy_id);
1015                 this._copy_fields_for_acqdf.forEach(
1016                     function(field) {
1017                         if(entry[field]()) {
1018                             copyWidgets[field].attr('value', (entry[field]()));
1019                         }
1020                     }
1021                 );
1022             }
1023         }
1024
1025         if (entries_applied) {
1026             this.virtDfaCounts[formula.id()] =
1027                 ++(this.virtDfaCounts[formula.id()]) || 1;
1028             this._updateFormulaStore();
1029             this._drawDistribAppliedUnit(
1030                 function(df) {
1031                     var dfa = new acqdfa();
1032                     dfa.formula(df); dfa.id(self.virtDfaId--); return dfa;
1033                 }(formula)
1034             );
1035             this.dfeOffset += entries_applied;
1036         };
1037     };
1038
1039     /**
1040      * This function updates the DF store for the dropdown so that use_counts
1041      * can reflect DF applications from this session before they're saved
1042      * server-side.
1043      */
1044     this._updateFormulaStore = function() {
1045         this.dfSelector.store = new dojo.data.ItemFileReadStore(
1046             {
1047                 "data": self._labelFormulasWithCounts(
1048                     acqdf.toStoreData(self.distribForms)
1049                 )
1050             }
1051         );
1052     };
1053
1054     this.saveCopyFieldsBeforeDF = function(copy_id) {
1055         var self = this;
1056         if (!this.oldCopyWidgetCache[copy_id]) {
1057             var copyWidgets = this.copyWidgetCache[copy_id];
1058
1059             this.oldCopyWidgetCache[copy_id] = {};
1060             this._copy_fields_for_acqdf.forEach(
1061                 function(f) {
1062                     self.oldCopyWidgetCache[copy_id][f] =
1063                         copyWidgets[f].attr("value");
1064                 }
1065             );
1066         }
1067     };
1068
1069     this.restoreCopyFieldsBeforeDF = function() {
1070         var self = this;
1071         for (var copy_id in this.oldCopyWidgetCache) {
1072             this._copy_fields_for_acqdf.forEach(
1073                 function(f) {
1074                     self.copyWidgetCache[copy_id][f].attr(
1075                         "value", self.oldCopyWidgetCache[copy_id][f]
1076                     );
1077                 }
1078             );
1079         }
1080     };
1081
1082     this._labelFormulasWithCounts = function(store_data) {
1083         for (var key in store_data.items) {
1084             var obj = store_data.items[key];
1085             obj.use_count = Number(obj.use_count); /* needed for sorting */
1086
1087             if (this.virtDfaCounts[obj.id])
1088                 obj.use_count = obj.use_count + Number(this.virtDfaCounts[obj.id]);
1089
1090             obj.dynLabel = "<span class='acq-lit-distrib-form-use-count'>[" +
1091                 obj.use_count + "]</span>&nbsp; " + obj.name;
1092         }
1093         return store_data;
1094     };
1095
1096     /**
1097      * This method formerly would not refetch the DF formulas if they'd been
1098      * loaded already, but now it always re-fetches, since use_count changes.
1099      */
1100     this._fetchDistribFormulas = function(onload) {
1101         fieldmapper.standardRequest(
1102             ["open-ils.acq",
1103                 "open-ils.acq.distribution_formula.ranged.retrieve.atomic"],
1104             {
1105                 "async": true,
1106                 "params": [openils.User.authtoken],
1107                 "oncomplete": function(r) {
1108                     self.distribForms = openils.Util.readResponse(r);
1109                     if(!self.distribForms || self.distribForms.length == 0) {
1110                         self.distribForms = [];
1111                     }
1112                     self._addDistribFormulaRow();
1113                     onload();
1114                 }
1115             }
1116         );
1117     }
1118
1119     this._drawBatchCopyWidgets = function() {
1120         var row = this.copyBatchRow;
1121         dojo.forEach(liDetailBatchFields, 
1122             function(field) {
1123                 if(self.copyBatchRowDrawn) {
1124                     self.copyBatchWidgets[field].attr('value', null);
1125                 } else {
1126                     var widget = new openils.widget.AutoFieldWidget({
1127                         fmField : field,
1128                         fmClass : 'acqlid',
1129                         labelFormat : (field == 'fund') ? fundLabelFormat : null,
1130                         searchFormat : (field == 'fund') ? fundSearchFormat : null,
1131                         searchFilter : (field == 'fund') ? {"active": "t"} : null,
1132                         parentNode : dojo.query('[name='+field+']', row)[0],
1133                         orgLimitPerms : ['CREATE_PICKLIST'],
1134                         dijitArgs : {required:false},
1135                         forceSync : true
1136                     });
1137                     widget.build(
1138                         function(w, ww) {
1139                             self.copyBatchWidgets[field] = w;
1140                         }
1141                     );
1142                 }
1143             }
1144         );
1145         this.copyBatchRowDrawn = true;
1146     };
1147
1148     this.batchCopyUpdate = function() {
1149         var self = this;
1150         for(var k in this.copyWidgetCache) {
1151             var cache = this.copyWidgetCache[k];
1152             dojo.forEach(liDetailBatchFields, function(f) {
1153                 var newval = self.copyBatchWidgets[f].attr('value');
1154                 if(newval) cache[f].attr('value', newval);
1155             });
1156         }
1157     };
1158
1159     this._drawCopies = function(li) {
1160         var self = this;
1161
1162         // this button sets the total number of copies for a given lineitem
1163         acqLitAddCopyCount.onClick = function() { 
1164             var count = acqLitCopyCountInput.attr('value');
1165
1166             // add new rows
1167             while(self.copyCount() < count)
1168                 self.addCopy(li); 
1169             
1170             // delete rows if necessary
1171             var diff = self.copyCount() - count;
1172             if(diff > 0) {
1173                 var rows = dojo.query('tr', self.copyTbody).reverse().slice(0, diff);
1174                 if(confirm(dojo.string.substitute(localeStrings.DELETE_LI_COPIES_CONFIRM, [diff]))) {
1175                     dojo.forEach(rows, function(row) {self.deleteCopy(row); });
1176                 } else {
1177                     acqLitCopyCountInput.attr('value', self.copyCount()+'');
1178                 }
1179             }
1180         }
1181
1182
1183         if(li.lineitem_details().length > 0) {
1184             dojo.forEach(li.lineitem_details(),
1185                 function(copy) {
1186                     self.addCopy(li, copy);
1187                 }
1188             );
1189         } else {
1190             self.addCopy(li);
1191         }
1192     };
1193
1194     this.copyCount = function() {
1195         var count = 0;
1196         for(var id in this.copyCache) {
1197             if(!this.copyCache[id].isdeleted())
1198                 count++;
1199         }
1200         return count;
1201     }
1202
1203     this.virtCopyId = -1;
1204     this.addCopy = function(li, copy) {
1205         var row = this.copyRow.cloneNode(true);
1206         this.copyTbody.appendChild(row);
1207         var self = this;
1208
1209         if(!copy) {
1210             copy = new fieldmapper.acqlid();
1211             copy.isnew(true);
1212             copy.id(this.virtCopyId--);
1213             copy.lineitem(li.id());
1214         }
1215
1216         this.copyCache[copy.id()] = copy;
1217         row.setAttribute('copy_id', copy.id());
1218         self.copyWidgetCache[copy.id()] = {};
1219
1220         acqLitCopyCountInput.attr('value', self.copyCount()+'');
1221
1222         dojo.forEach(liDetailFields,
1223             function(field) {
1224                 var searchFilter;
1225                 if (field == "fund") {
1226                     searchFilter = (copy.fund() ?
1227                         {"-or": {"active": "t", "id": copy.fund()}} :
1228                         {"active" : "t"});
1229                 } else {
1230                     searchFilter = null;
1231                 }
1232                 var widget = new openils.widget.AutoFieldWidget({
1233                     fmObject : copy,
1234                     fmField : field,
1235                     labelFormat : (field == 'fund') ? fundLabelFormat : null,
1236                     searchFormat : (field == 'fund') ? fundSearchFormat : null,
1237                     searchFilter : searchFilter,
1238                     noCache: true,
1239                     fmClass : 'acqlid',
1240                     parentNode : dojo.query('[name='+field+']', row)[0],
1241                     orgLimitPerms : ['CREATE_PICKLIST', 'CREATE_PURCHASE_ORDER'],
1242                     readOnly : Boolean(copy.eg_copy_id())
1243                 });
1244                 widget.build(
1245                     // make sure we capture the value from any async widgets
1246                     function(w, ww) { 
1247                         copy[field](ww.getFormattedValue()) 
1248                         self.copyWidgetCache[copy.id()][field] = w;
1249                     }
1250                 );
1251                 dojo.connect(widget.widget, 'onChange', 
1252                     function(val) { 
1253                         if(copy.isnew() || val != copy[field]()) {
1254                             // prevent setting ischanged() automatically on widget load for existing copies
1255                             copy[field](widget.getFormattedValue()) 
1256                             copy.ischanged(true);
1257                         }
1258                     }
1259                 );
1260             }
1261         );
1262
1263         this.updateLidState(copy, row);
1264     };
1265
1266     this.updateLidState = function(copy, row) {
1267         if (typeof(row) == "undefined") {
1268             row = dojo.query(
1269                 'tr[copy_id="' + copy.id() + '"]', this.copyTbody
1270             )[0];
1271         }
1272
1273         var self = this;
1274         var recv_link = nodeByName("receive", row);
1275         var unrecv_link = nodeByName("unreceive", row);
1276         var del_link = nodeByName("delete", row);
1277         var cxl_link = nodeByName("cancel", row);
1278         var cxl_reason_link = nodeByName("cancel_reason", row);
1279
1280         if (copy.cancel_reason()) {
1281             openils.Util.hide(del_link.parentNode);
1282             openils.Util.hide(recv_link);
1283             openils.Util.hide(unrecv_link);
1284             openils.Util.hide(cxl_link);
1285
1286             /* XXX the following may leak memory in a long lived table: dijits may not get destroyed... not positive. revisit. */
1287             var holds_reason = dojo.create(
1288                 "span", {
1289                     "style": "border-bottom: 1px dashed #000;",
1290                     "innerHTML": "Cancelled" /* XXX [sic] and i18n */
1291                 }, cxl_reason_link, "only"
1292             );
1293             new dijit.Tooltip(
1294                 {
1295                     "label": "<em>" + copy.cancel_reason().label() +
1296                         "</em><br />" + copy.cancel_reason().description(),
1297                     "connectId": [holds_reason]
1298                 }, dojo.create("span", null, cxl_reason_link, "last")
1299             );
1300             openils.Util.show(cxl_reason_link, "inline");
1301         } else if (this.isPO) {
1302             openils.Util.hide(del_link.parentNode);
1303             openils.Util.hide(cxl_reason_link);
1304
1305             /* Avoid showing (un)receive links, cancel links, for virt copies */
1306             if (copy.id() > 0) {
1307                 if(copy.recv_time()) {
1308                     openils.Util.hide(cxl_link);
1309                     openils.Util.hide(recv_link);
1310                     openils.Util.show(unrecv_link, "inline");
1311                     unrecv_link.onclick = function() {
1312                         if (confirm(localeStrings.UNRECEIVE_LID))
1313                             self.issueReceive(copy, /* rollback */ true);
1314                     };
1315                 } else {
1316                     openils.Util.hide(unrecv_link);
1317                     openils.Util.show(recv_link, "inline");
1318                     openils.Util.show(cxl_link, "inline");
1319                     recv_link.onclick = function() {
1320                         if (self.checkLiAlerts(copy.lineitem()))
1321                             self.issueReceive(copy);
1322                     };
1323                     cxl_link.onclick = function() {
1324                         self.cancelLid(copy.id());
1325                     };
1326                 }
1327             } else {
1328                 openils.Util.hide(cxl_link);
1329                 openils.Util.hide(unrecv_link);
1330                 openils.Util.hide(recv_link);
1331             }
1332         } else {
1333             openils.Util.hide(unrecv_link);
1334             openils.Util.hide(recv_link);
1335             openils.Util.hide(cxl_reason_link);
1336
1337             del_link.onclick = function() { self.deleteCopy(row) };
1338             openils.Util.show(del_link.parentNode);
1339         }
1340     }
1341
1342     this.cancelLid = function(lid_id) {
1343         lidCancelDialog._lid_id = lid_id;
1344         openils.Util.show(lidCancelDialog.domNode.parentNode);
1345         lidCancelDialog.show();
1346         if (!lidCancelDialog._prepared) {
1347             var widget = new openils.widget.AutoFieldWidget({
1348                 "fmField": "cancel_reason",
1349                 "fmClass": "acqlid",
1350                 "parentNode": dojo.byId("acq-lit-lid-cancel-reason"),
1351                 "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
1352                 "forceSync": true
1353             });
1354             widget.build(
1355                 function(w, ww) {
1356                     acqLidCancelButton.onClick = function() {
1357                         if (w.attr("value")) {
1358                             if (confirm(localeStrings.LID_CANCEL_CONFIRM)) {
1359                                 self._cancelLid(
1360                                     lidCancelDialog._lid_id,
1361                                     w.attr("value")
1362                                 );
1363                             }
1364                             lidCancelDialog.hide();
1365                         }
1366                     };
1367                     lidCancelDialog._prepared = true;
1368                 }
1369             );
1370         }
1371     };
1372
1373     this._cancelLid = function(lid_id, reason) {
1374         fieldmapper.standardRequest(
1375             ["open-ils.acq", "open-ils.acq.lineitem_detail.cancel"], {
1376                 "params": [openils.User.authtoken, lid_id, reason],
1377                 "async": true,
1378                 "onresponse": function(r) {
1379                     if (r = openils.Util.readResponse(r)) {
1380                         if (r.lid) {
1381                             for (var id in r.lid) {
1382                                 /* actually this should only iterate once */
1383                                 self.copyCache[id].cancel_reason(
1384                                     r.lid[id].cancel_reason
1385                                 );
1386                                 self.updateLidState(self.copyCache[id]);
1387                             }
1388                         }
1389                     }
1390                 }
1391             }
1392         );
1393     };
1394
1395     this._confirmAlert = function(li, lin) {
1396         return confirm(
1397             dojo.string.substitute(
1398                 localeStrings.CONFIRM_LI_ALERT, [
1399                     (new openils.acq.Lineitem({"lineitem": li})).findAttr(
1400                         "title", "lineitem_marc_attr_definition"
1401                     ),
1402                     lin.alert_text().code(),
1403                     lin.alert_text().description() || "",
1404                     lin.value()
1405                 ]
1406             )
1407         );
1408     };
1409
1410     this.checkLiAlerts = function(li_id) {
1411         var li = this.liCache[li_id];
1412
1413         var alert_notes = li.lineitem_notes().filter(
1414             function(o) { return Boolean(o.alert_text()); }
1415         );
1416
1417         /* this is _intentionally_ not done in a call to forEach() ... */
1418         for (var i = 0; i < alert_notes.length; i++) {
1419             if (this.noteAcks[alert_notes[i].id()])
1420                 continue;
1421             else if (!this._confirmAlert(li, alert_notes[i]))
1422                 return false;
1423             else
1424                 this.noteAcks[alert_notes[i].id()] = true;
1425         }
1426
1427         return true;
1428     };
1429
1430     this.deleteCopy = function(row) {
1431         var copy = this.copyCache[row.getAttribute('copy_id')];
1432         copy.isdeleted(true);
1433         if(copy.isnew())
1434             delete this.copyCache[copy.id()];
1435         this.copyTbody.removeChild(row);
1436     }
1437
1438     this._virtDfaCountsAsList = function() {
1439         var L = [];
1440         for (var key in this.virtDfaCounts) {
1441             for (var i = 0; i < this.virtDfaCounts[key]; i++)
1442                 L.push(key);
1443         }
1444         return L;
1445     }
1446
1447     this.saveCopyChanges = function(liId) {
1448         var self = this;
1449         var copies = [];
1450
1451
1452         var total = 0;
1453         for(var id in this.copyCache) {
1454             var c = this.copyCache[id];
1455             if(!c.isdeleted()) total++;
1456             if(c.isnew() || c.ischanged() || c.isdeleted()) {
1457                 if(c.id() < 0) c.id(null);
1458                 copies.push(c);
1459             }
1460         }
1461
1462         if (typeof(this._copy_count_cb) == "function") {
1463             this._copy_count_cb(liId, total);
1464         }
1465
1466         dojo.byId('acq-lit-copy-count-label-' + liId).innerHTML = total;
1467
1468
1469         if (copies.length > 0) {
1470             openils.Util.show("acq-lit-update-copies-progress");
1471             fieldmapper.standardRequest(
1472                 ['open-ils.acq', 'open-ils.acq.lineitem_detail.cud.batch'],
1473                 {   async: true,
1474                     params: [openils.User.authtoken, copies],
1475                     onresponse: function(r) {
1476                         var res = openils.Util.readResponse(r);
1477                         litUpdateCopiesProgress.update(res);
1478                     },
1479                     oncomplete: function() {
1480                         self.drawCopies(liId);
1481                         openils.Util.hide("acq-lit-update-copies-progress");
1482                     }
1483                 }
1484             );
1485         }
1486
1487         var dfa_list = this._virtDfaCountsAsList();
1488         if (dfa_list.length > 0) {
1489             fieldmapper.standardRequest(
1490                 ["open-ils.acq",
1491                 "open-ils.acq.distribution_formula.record_application"],
1492                 {
1493                     "async": true,
1494                     "params": [openils.User.authtoken, dfa_list, liId],
1495                     "onresponse": function(r) {
1496                         var res = openils.Util.readResponse(r);
1497                         if (res && res.length < dfa_list.length)
1498                             alert(localeStrings.DFA_NOT_ALL);
1499                     }
1500                 }
1501             );
1502             this.virtDfaCounts = {};
1503         }
1504     }
1505
1506     this._updateCreatePoPrepayCheckbox = function(prepay) {
1507         var prepay = openils.Util.isTrue(prepay);
1508         this._prepayRequiredByVendor = prepay;
1509         dijit.byId("acq-lit-po-prepay").attr("checked", prepay);
1510     };
1511
1512     this._confirmPoPrepaySituation = function() {
1513         var want_prepay = dijit.byId("acq-lit-po-prepay").attr("checked");
1514         if (want_prepay != this._prepayRequiredByVendor) {
1515             return confirm(
1516                 want_prepay ?
1517                     localeStrings.VENDOR_SAYS_PREPAY_NOT_NEEDED :
1518                     localeStrings.VENDOR_SAYS_PREPAY_NEEDED
1519             );
1520         } else {
1521             return true;
1522         }
1523     };
1524
1525     this.applySelectedLiAction = function(action) {
1526         var self = this;
1527         switch(action) {
1528
1529             case 'delete_selected':
1530                 this._deleteLiList(self.getSelected());
1531                 break;
1532
1533             case 'create_order':
1534
1535                 if(!this.createPoProviderSelector) {
1536                     var widget = new openils.widget.AutoFieldWidget({
1537                         fmField : 'provider',
1538                         fmClass : 'acqpo',
1539                         searchFilter: {"active": "t"},
1540                         parentNode : dojo.byId('acq-lit-po-provider'),
1541                         dijitArgs : {
1542                             "onChange": function() {
1543                                 if (this.item) {
1544                                     self._updateCreatePoPrepayCheckbox(
1545                                         this.item.prepayment_required
1546                                     );
1547                                 }
1548                             }
1549                         }
1550                     });
1551                     widget.build(
1552                         function(w) { self.createPoProviderSelector = w; }
1553                     );
1554                 }
1555
1556                 if(!this.createPoAgencySelector) {
1557                     var widget = new openils.widget.AutoFieldWidget({
1558                         fmField : 'ordering_agency',
1559                         fmClass : 'acqpo',
1560                         parentNode : dojo.byId('acq-lit-po-agency'),
1561                         orgLimitPerms : ['CREATE_PURCHASE_ORDER'],
1562                     });
1563                     widget.build(
1564                         function(w) { self.createPoAgencySelector = w; }
1565                     );
1566                 }
1567
1568          
1569                 acqLitPoCreateDialog.show();
1570                 break;
1571
1572             case 'save_picklist':
1573                 this._loadPLSelect();
1574                 acqLitSavePlDialog.show();
1575                 break;
1576
1577             case 'selector_ready':
1578             case 'order_ready':
1579                 acqLitChangeLiStateDialog.attr('state', action.replace('_', '-'));
1580                 acqLitChangeLiStateDialog.show();
1581                 break;
1582
1583             case 'print_po':
1584                 this.printPO();
1585                 break;
1586
1587             case 'receive_po':
1588                 this.receivePO();
1589                 break;
1590
1591             case 'rollback_receive_po':
1592                 this.rollbackPoReceive();
1593                 break;
1594
1595             case 'create_assets':
1596                 this.createAssets();
1597                 break;
1598
1599             case 'export_attr_list':
1600                 this.chooseExportAttr();
1601                 break;
1602
1603             case 'add_brief_record':
1604                 if(this.isPO)
1605                     location.href = oilsBasePath + '/acq/picklist/brief_record?po=' + this.isPO;
1606                 else
1607                     location.href = oilsBasePath + '/acq/picklist/brief_record?pl=' + this.isPL;
1608
1609                 break;
1610
1611             case "cancel_lineitems":
1612                 this.maybeCancelLineitems();
1613                 break;
1614         }
1615     }
1616
1617     this.createAssets = function() {
1618         if(!this.isPO) return;
1619         if(!confirm(localeStrings.CREATE_PO_ASSETS_CONFIRM)) return;
1620         this.show('acq-lit-progress-numbers');
1621         var self = this;
1622         fieldmapper.standardRequest(
1623             ['open-ils.acq', 'open-ils.acq.purchase_order.assets.create'],
1624             {   async: true,
1625                 params: [this.authtoken, this.isPO],
1626                 onresponse: function(r) {
1627                     var resp = openils.Util.readResponse(r);
1628                     self._updateProgressNumbers(resp, true);
1629                 }
1630             }
1631         );
1632     }
1633
1634     this.maybeCancelLineitems = function() {
1635         openils.Util.show("acq-lit-cancel-reason", "inline");
1636         if (!acqLitCancelLineitemsButton._prepared) {
1637             var widget = new openils.widget.AutoFieldWidget({
1638                 "fmField": "cancel_reason",
1639                 "fmClass": "jub",
1640                 "parentNode": dojo.byId("acq-lit-cancel-reason-selector"),
1641                 "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
1642                 "forceSync": true
1643             });
1644             widget.build(
1645                 function(w, ww) {
1646                     acqLitCancelLineitemsButton.onClick = function() {
1647                         if (w.attr("value")) {
1648                             if (confirm(localeStrings.LI_CANCEL_CONFIRM)) {
1649                                 self._cancelLineitems(w.attr("value"));
1650                             }
1651                             openils.Util.hide("acq-lit-cancel-reason");
1652                         }
1653                     };
1654                     acqLitCancelLineitemsButton._prepared = true;
1655                 }
1656             );
1657         }
1658     };
1659
1660     this._cancelLineitems = function(reason) {
1661         var id_list = this.getSelected().map(function(o) { return o.id(); });
1662         fieldmapper.standardRequest(
1663             ["open-ils.acq", "open-ils.acq.lineitem.cancel.batch"], {
1664                 "params": [openils.User.authtoken, id_list, reason],
1665                 "async": true,
1666                 "onresponse": function(r) {
1667                     if (r = openils.Util.readResponse(r)) {
1668                         if (r.li) {
1669                             for (var id in r.li) {
1670                                 self.liCache[id].state(r.li[id].state);
1671                                 self.liCache[id].cancel_reason(
1672                                     r.li[id].cancel_reason
1673                                 );
1674                                 self.updateLiState(self.liCache[id]);
1675                             }
1676                         }
1677                         if (r.lid && self.copyCache) {
1678                             for (var id in r.lid) {
1679                                 if (self.copyCache[id]) {
1680                                     self.copyCache[id].cancel_reason(
1681                                         r.lid[id].cancel_reason
1682                                     );
1683                                     self.updateLidState(self.copyCache[id]);
1684                                 }
1685                             }
1686                         }
1687                     }
1688                 }
1689             }
1690         );
1691     };
1692
1693     this.chooseExportAttr = function() {
1694         if (!acqLitExportAttrSelector._li_setup) {
1695             var self = this;
1696             acqLitExportAttrSelector.store = new dojo.data.ItemFileReadStore(
1697                 {
1698                     "data": acqliad.toStoreData(
1699                         this.pcrud.search(
1700                             "acqliad", {"code": li_exportable_attrs}
1701                         )
1702                     )
1703                 }
1704             );
1705             acqLitExportAttrSelector.setValue();
1706             acqLitExportAttrButton.onClick = function(){self.exportAttrList();};
1707             acqLitExportAttrSelector._li_setup = true;
1708         }
1709         openils.Util.show("acq-lit-export-attr-holder", "inline");
1710     };
1711
1712     this.exportAttrList = function() {
1713         var attr_def = acqLitExportAttrSelector.item;
1714         var li_list = this.getSelected();
1715         var value_list = li_list.map(
1716             function(li) {
1717                 return (new openils.acq.Lineitem({"lineitem": li})).findAttr(
1718                     attr_def.code, "lineitem_marc_attr_definition"
1719                 );
1720             }
1721         ).filter(function(attr) { return Boolean(attr); });
1722
1723         if (value_list.length > 0) {
1724             if (value_list.length < li_list.length) {
1725                 if (!confirm(
1726                     dojo.string.substitute(
1727                         localeStrings.EXPORT_SHORT_LIST, [attr_def.description]
1728                     )
1729                 )) {
1730                     return;
1731                 }
1732             }
1733             try {
1734                 openils.XUL.contentToFileSaveDialog(
1735                     value_list.join("\n"),
1736                     localeStrings.EXPORT_SAVE_DIALOG_TITLE
1737                 );
1738             } catch (E) {
1739                 alert(E);
1740             }
1741         } else {
1742             alert(dojo.string.substitute(
1743                 localeStrings.EXPORT_EMPTY_LIST, [attr_def.description]
1744             ));
1745         }
1746
1747         openils.Util.hide("acq-lit-export-attr-holder");
1748     };
1749
1750     this.printPO = function() {
1751         if(!this.isPO) return;
1752         progressDialog.show(true);
1753         fieldmapper.standardRequest(
1754             ['open-ils.acq', 'open-ils.acq.purchase_order.format'],
1755             {   async: true,
1756                 params: [this.authtoken, this.isPO, 'html'],
1757                 oncomplete: function(r) {
1758                     progressDialog.hide();
1759                     var evt = openils.Util.readResponse(r);
1760                     if(evt && evt.template_output()) {
1761                         win = window.open('','', 'resizable,width=800,height=600,scrollbars=1');
1762                         win.document.body.innerHTML = evt.template_output().data();
1763                     }
1764                 }
1765             }
1766         );
1767     }
1768
1769
1770     this.receivePO = function() {
1771         if (!this.isPO) return;
1772
1773         for (var id in this.liCache) {
1774             /* assumption: liCache reflects exactly the
1775              * set of LIs that belong to our PO */
1776             if (this.liCache[id].state() != "received" &&
1777                 !this.checkLiAlerts(id)) return;
1778         }
1779
1780         this.show('acq-lit-progress-numbers');
1781         var self = this;
1782         fieldmapper.standardRequest(
1783             ['open-ils.acq', 'open-ils.acq.purchase_order.receive'],
1784             {   async: true,
1785                 params: [this.authtoken, this.isPO],
1786                 onresponse : function(r) {
1787                     var resp = openils.Util.readResponse(r);
1788                     self._updateProgressNumbers(resp, true);
1789                 },
1790             }
1791         );
1792     }
1793
1794     this.issueReceive = function(obj, rollback) {
1795         /* (For now) there shall be no marking LI or LIDs (un)received
1796          * except from the actual "view PO" interface. */
1797         if (!this.isPO) return;
1798
1799         var part =
1800             {"jub": "lineitem", "acqlid": "lineitem_detail"}[obj.classname];
1801         var method =
1802             "open-ils.acq." + part + ".receive" + (rollback ? ".rollback" : "");
1803
1804         progressDialog.show(true);
1805         fieldmapper.standardRequest(
1806             ["open-ils.acq", method], {
1807                 "async": true,
1808                 "params": [this.authtoken, obj.id()],
1809                 "onresponse": function(r) {
1810                     self.handleReceive(openils.Util.readResponse(r));
1811                     progressDialog.hide();
1812                 }
1813             }
1814         );
1815     };
1816
1817     /**
1818      * Handles the responses from receive and rollback ML calls.
1819      */
1820     this.handleReceive = function(resp) {
1821         if (resp) {
1822             if (resp.li) {
1823                 for (var li_id in resp.li) {
1824                     for (var key in resp.li[li_id])
1825                         self.liCache[li_id][key](resp.li[li_id][key]);
1826                     self.updateLiState(self.liCache[li_id]);
1827                 }
1828             }
1829             if (resp.po) {
1830                 if (typeof(self.poUpdateCallback) == "function")
1831                     self.poUpdateCallback(resp.po);
1832             }
1833             if (resp.lid) {
1834                 for (var lid_id in resp.lid) {
1835                     for (var key in resp.lid[lid_id])
1836                         self.copyCache[lid_id][key](resp.lid[lid_id][key]);
1837                     self.updateLidState(self.copyCache[lid_id]);
1838                 }
1839             }
1840         }
1841     };
1842
1843     this.rollbackPoReceive = function() {
1844         if(!this.isPO) return;
1845         if(!confirm(localeStrings.ROLLBACK_PO_RECEIVE_CONFIRM)) return;
1846         this.show('acq-lit-progress-numbers');
1847         var self = this;
1848         fieldmapper.standardRequest(
1849             ['open-ils.acq', 'open-ils.acq.purchase_order.receive.rollback'],
1850             {   async: true,
1851                 params: [this.authtoken, this.isPO],
1852                 onresponse : function(r) {
1853                     var resp = openils.Util.readResponse(r);
1854                     self._updateProgressNumbers(resp, true);
1855                 },
1856             }
1857         );
1858     }
1859
1860     this._updateProgressNumbers = function(resp, reloadOnComplete) {
1861         if(!resp) return;
1862         dojo.byId('acq-pl-lit-li-processed').innerHTML = resp.li;
1863         dojo.byId('acq-pl-lit-lid-processed').innerHTML = resp.lid;
1864         dojo.byId('acq-pl-lit-debits-processed').innerHTML = resp.debits_accrued;
1865         dojo.byId('acq-pl-lit-bibs-processed').innerHTML = resp.bibs;
1866         dojo.byId('acq-pl-lit-indexed-processed').innerHTML = resp.indexed;
1867         dojo.byId('acq-pl-lit-copies-processed').innerHTML = resp.copies;
1868         if(resp.complete && reloadOnComplete) 
1869             location.href = location.href;
1870     }
1871
1872
1873     this._createPO = function(fields) {
1874         this.show('acq-lit-progress-numbers');
1875         var po = new fieldmapper.acqpo();
1876         po.provider(this.createPoProviderSelector.attr('value'));
1877         po.ordering_agency(this.createPoAgencySelector.attr('value'));
1878         po.prepayment_required(fields.prepayment_required[0] ? true : false);
1879
1880         var selected = this.getSelected( (fields.create_from == 'all') );
1881         if(selected.length == 0) return;
1882
1883         var max = selected.length * 3;
1884
1885         var self = this;
1886         fieldmapper.standardRequest(
1887             ['open-ils.acq', 'open-ils.acq.purchase_order.create'],
1888             {   async: true,
1889                 params: [
1890                     openils.User.authtoken, 
1891                     po, 
1892                     {
1893                         lineitems : selected.map(function(li) { return li.id() }),
1894                         create_assets : fields.create_assets[0],
1895                     }
1896                 ],
1897
1898                 onresponse : function(r) {
1899                     var resp = openils.Util.readResponse(r);
1900                     self._updateProgressNumbers(resp);
1901                     if(resp.complete) 
1902                         location.href = oilsBasePath + '/eg/acq/po/view/' + resp.purchase_order.id();
1903                 }
1904             }
1905         );
1906     }
1907
1908     this._deleteLiList = function(list, idx) {
1909         if(idx == null) idx = 0;
1910         if(idx >= list.length) return;
1911         var liId = list[idx].id();
1912         fieldmapper.standardRequest(
1913             ['open-ils.acq', 'open-ils.acq.lineitem.delete'],
1914             {   async: true,
1915                 params: [openils.User.authtoken, liId],
1916                 oncomplete: function(r) {
1917                     self.removeLineitem(liId);
1918                     self._deleteLiList(list, ++idx);
1919                 }
1920             }
1921         );
1922     }
1923
1924     this.editOrderMarc = function(li) {
1925
1926         /*  To run in Firefox directly, must set signed.applets.codebase_principal_support
1927             to true in about:config */
1928
1929         if(!openils.XUL.enableXPConnect()) return;
1930
1931         if(openils.XUL.isXUL()) {
1932             win = window.open('/xul/' + openils.XUL.buildId() + '/server/cat/marcedit.xul');
1933         } else {
1934             win = window.open('/xul/server/cat/marcedit.xul'); 
1935         }
1936         var self = this;
1937         win.xulG = {
1938             record : {marc : li.marc()},
1939             save : {
1940                 label: 'Save Record', // XXX I18N
1941                 func: function(xmlString) {
1942                     li.marc(xmlString);
1943                     fieldmapper.standardRequest(
1944                         ['open-ils.acq', 'open-ils.acq.lineitem.update'],
1945                         {   async: true,
1946                             params: [openils.User.authtoken, li],
1947                             oncomplete: function(r) {
1948                                 openils.Util.readResponse(r);
1949                                 win.close();
1950                                 self.drawInfo(li.id())
1951                             }
1952                         }
1953                     );
1954                 },
1955             }
1956         };
1957     }
1958
1959     this._savePl = function(values) {
1960         var self = this;
1961         var selected = this.getSelected( (values.which == 'all') );
1962         openils.Util.show('acq-lit-generic-progress');
1963
1964         if(values.new_name) {
1965             openils.acq.Picklist.create(
1966                 {name: values.new_name}, 
1967                 function(id) {
1968                     self._updateLiList(id, selected, 0, 
1969                         function(){
1970                             location.href = oilsBasePath + '/eg/acq/picklist/view/' + id;
1971                         });
1972                 }
1973             );
1974         } else if(values.existing_pl) {
1975             // update lineitems to use an existing picklist
1976             self._updateLiList(values.existing_pl, selected, 0, 
1977                 function(){
1978                     location.href = oilsBasePath + '/eg/acq/picklist/view/' + values.existing_pl;
1979                 });
1980         }
1981     }
1982
1983     this._updateLiState = function(values, state) {
1984         var self = this;
1985         var selected = this.getSelected( (values.which == 'all') );
1986         if(!selected.length) return;
1987         dojo.forEach(selected, function(li) {li.state(state);});
1988         self._updateLiList(null, selected, 0, 
1989             // TODO consider inline updates for efficiency
1990             function() { location.href = location.href }
1991         );
1992     }
1993
1994     this._updateLiList = function(pl, list, idx, oncomplete) {
1995         if(idx >= list.length) return oncomplete();
1996         var li = list[idx];
1997         if(pl != null) li.picklist(pl);
1998         litGenericProgress.update({maximum: list.length, progress: idx});
1999         new openils.acq.Lineitem({lineitem:li}).update(
2000             function(r) {
2001                 self._updateLiList(pl, list, ++idx, oncomplete);
2002             }
2003         );
2004     }
2005
2006     this._loadPLSelect = function() {
2007         if(this._plSelectLoaded) return;
2008         var plList = [];
2009         function handleResponse(r) {
2010             plList.push(r.recv().content());
2011         }
2012         var method = 'open-ils.acq.picklist.user.retrieve';
2013         fieldmapper.standardRequest(
2014             ['open-ils.acq', method],
2015             {   async: true,
2016                 params: [this.authtoken],
2017                 onresponse: handleResponse,
2018                 oncomplete: function() {
2019                     self._plSelectLoaded = true;
2020                     acqLitAddExistingSelect.store = 
2021                         new dojo.data.ItemFileReadStore({data:acqpl.toStoreData(plList)});
2022                     acqLitAddExistingSelect.setValue();
2023                 }
2024             }
2025         );
2026     }
2027
2028     this.showRealCopyEditUI = function(li) {
2029         copyList = [];
2030         var self = this;
2031         this.volCache = {};
2032
2033         this._fetchLineitem(li.id(), 
2034             function(fullLi) {
2035                 li = self.liCache[li.id()] = fullLi;
2036
2037                 self.pcrud.search(
2038                     'acp', {
2039                         id : li.lineitem_details().map(
2040                             function(item) { return item.eg_copy_id() }
2041                         )
2042                     }, {
2043                         async : true,
2044                         oncomplete : function(r) {
2045                             try {
2046                                 var r_list = openils.Util.readResponse( r );
2047                                 for (var i = 0; i < r_list.length; i++) {
2048                                     var copy = r_list[i];
2049                                     var volId = copy.call_number();
2050                                     var volume = self.volCache[volId];
2051                                     if(!volume) {
2052                                         volume = self.volCache[volId] = self.pcrud.retrieve('acn', volId);
2053                                     }
2054                                     copy.call_number(volume);
2055                                     copyList.push(copy);
2056                                 }
2057                                 if (xulG) {
2058                                     // If we need to, we can pass in an update_copy function to handle the update instead of volume_item_creator
2059                                     xulG.volume_item_creator( { 'existing_copies' : copyList } );
2060                                 }
2061                             } catch(E) {
2062                                 alert('error in oncomplete: ' + E);
2063                             }
2064                         }
2065                     }
2066                 );
2067             }
2068         );
2069     }
2070
2071     
2072     /*
2073     this.saveRealCopies = function() {
2074         progressDialog.show(true);
2075         var list = this.realCopyList.filter(function(copy) { return copy.ischanged(); });
2076         this.pcrud.update(list, {oncomplete: function() { 
2077             progressDialog.hide();
2078             self.show('list');
2079         }});
2080     }
2081
2082     // grab the li-details for this lineitem, grab the linked copies and volumes, add them to the table
2083     this.showRealCopies = function(li) {
2084         while(this.realCopiesTbody.childNodes[0])
2085             this.realCopiesTbody.removeChild(this.realCopiesTbody.childNodes[0]);
2086         this.show('real-copies');
2087
2088         this.realCopyList = [];
2089         this.volCache = {};
2090         var tabIndex = 1000;
2091         var self = this;
2092
2093         acqLitSaveRealCopies.onClick = function() {
2094             self.saveRealCopies();
2095         }
2096
2097         this._fetchLineitem(li.id(), 
2098             function(fullLi) {
2099                 li = self.liCache[li.id()] = fullLi;
2100
2101                 self.pcrud.search(
2102                     'acp', {
2103                         id : li.lineitem_details().map(
2104                             function(item) { return item.eg_copy_id() }
2105                         )
2106                     }, {
2107                         async : true,
2108                         streaming : true,
2109                         onresponse : function(r) {
2110                             var copy = openils.Util.readResponse(r);
2111                             var volId = copy.call_number();
2112                             var volume = self.volCache[volId];
2113                             if(!volume) {
2114                                 volume = self.volCache[volId] = self.pcrud.retrieve('acn', volId);
2115                             }
2116                             self.addRealCopy(volume, copy, tabIndex++);
2117                         }
2118                     }
2119                 );
2120             }
2121         );
2122     }
2123
2124     this.addRealCopy = function(volume, copy, tabIndex) {
2125         var row = this.realCopiesRow.cloneNode(true);
2126         this.realCopyList.push(copy);
2127
2128         var selectNode;
2129         dojo.forEach(
2130             ['owning_lib', 'location', 'circ_modifier', 'label', 'barcode'],
2131
2132             function(field) {
2133                 var isvol = (field == 'owning_lib' || field == 'label');
2134                 var widget = new openils.widget.AutoFieldWidget({
2135                     fmField : field,
2136                     fmObject : isvol ? volume : copy,
2137                     parentNode : nodeByName(field, row),
2138                     readOnly : (field != 'barcode'),
2139                 });
2140
2141                 var widgetDrawn = null;
2142
2143                 if(field == 'barcode') {
2144
2145                     widgetDrawn = function(w, ww) {
2146                         var node = w.domNode;
2147                         node.setAttribute('tabindex', ''+tabIndex);
2148
2149                         // on enter, select the next barcode input
2150                         dojo.connect(w, 'onKeyDown',
2151                             function(e) {
2152                                 if(e.keyCode == dojo.keys.ENTER) {
2153                                     var ti = node.getAttribute('tabindex');
2154                                     var nextNode = dojo.query('[tabindex=' + String(Number(ti) + 1) + ']', self.realCopiesTbody)[0];
2155                                     if(nextNode) nextNode.select();
2156                                 }
2157                             }
2158                         );
2159
2160                         dojo.connect(w, 'onChange', 
2161                             function(val) { 
2162                                 if(!val || val == copy.barcode()) return;
2163                                 copy.ischanged(true);
2164                                 copy.barcode(val);
2165                             }
2166                         );
2167
2168
2169                         if(self.realCopiesTbody.getElementsByTagName('TR').length == 0)
2170                             selectNode = node;
2171                     }
2172                 }
2173
2174                 widget.build(widgetDrawn);
2175             }
2176         );
2177
2178         this.realCopiesTbody.appendChild(row);
2179         if(selectNode) selectNode.select();
2180     };
2181     */
2182
2183 }