]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/common/li_table.js
Acq: Add access to lineitem worksheet from LI table interfaces.
[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                         parentNode : dojo.query('[name='+field+']', row)[0],
1132                         orgLimitPerms : ['CREATE_PICKLIST'],
1133                         dijitArgs : {required:false},
1134                         forceSync : true
1135                     });
1136                     widget.build(
1137                         function(w, ww) {
1138                             self.copyBatchWidgets[field] = w;
1139                         }
1140                     );
1141                 }
1142             }
1143         );
1144         this.copyBatchRowDrawn = true;
1145     };
1146
1147     this.batchCopyUpdate = function() {
1148         var self = this;
1149         for(var k in this.copyWidgetCache) {
1150             var cache = this.copyWidgetCache[k];
1151             dojo.forEach(liDetailBatchFields, function(f) {
1152                 var newval = self.copyBatchWidgets[f].attr('value');
1153                 if(newval) cache[f].attr('value', newval);
1154             });
1155         }
1156     };
1157
1158     this._drawCopies = function(li) {
1159         var self = this;
1160
1161         // this button sets the total number of copies for a given lineitem
1162         acqLitAddCopyCount.onClick = function() { 
1163             var count = acqLitCopyCountInput.attr('value');
1164
1165             // add new rows
1166             while(self.copyCount() < count)
1167                 self.addCopy(li); 
1168             
1169             // delete rows if necessary
1170             var diff = self.copyCount() - count;
1171             if(diff > 0) {
1172                 var rows = dojo.query('tr', self.copyTbody).reverse().slice(0, diff);
1173                 if(confirm(dojo.string.substitute(localeStrings.DELETE_LI_COPIES_CONFIRM, [diff]))) {
1174                     dojo.forEach(rows, function(row) {self.deleteCopy(row); });
1175                 } else {
1176                     acqLitCopyCountInput.attr('value', self.copyCount()+'');
1177                 }
1178             }
1179         }
1180
1181
1182         if(li.lineitem_details().length > 0) {
1183             dojo.forEach(li.lineitem_details(),
1184                 function(copy) {
1185                     self.addCopy(li, copy);
1186                 }
1187             );
1188         } else {
1189             self.addCopy(li);
1190         }
1191     };
1192
1193     this.copyCount = function() {
1194         var count = 0;
1195         for(var id in this.copyCache) {
1196             if(!this.copyCache[id].isdeleted())
1197                 count++;
1198         }
1199         return count;
1200     }
1201
1202     this.virtCopyId = -1;
1203     this.addCopy = function(li, copy) {
1204         var row = this.copyRow.cloneNode(true);
1205         this.copyTbody.appendChild(row);
1206         var self = this;
1207
1208         if(!copy) {
1209             copy = new fieldmapper.acqlid();
1210             copy.isnew(true);
1211             copy.id(this.virtCopyId--);
1212             copy.lineitem(li.id());
1213         }
1214
1215         this.copyCache[copy.id()] = copy;
1216         row.setAttribute('copy_id', copy.id());
1217         self.copyWidgetCache[copy.id()] = {};
1218
1219         acqLitCopyCountInput.attr('value', self.copyCount()+'');
1220
1221         dojo.forEach(liDetailFields,
1222             function(field) {
1223                 var widget = new openils.widget.AutoFieldWidget({
1224                     fmObject : copy,
1225                     fmField : field,
1226                     labelFormat : (field == 'fund') ? fundLabelFormat : null,
1227                     searchFormat : (field == 'fund') ? fundSearchFormat : null,
1228                     fmClass : 'acqlid',
1229                     parentNode : dojo.query('[name='+field+']', row)[0],
1230                     orgLimitPerms : ['CREATE_PICKLIST', 'CREATE_PURCHASE_ORDER'],
1231                     readOnly : Boolean(copy.eg_copy_id())
1232                 });
1233                 widget.build(
1234                     // make sure we capture the value from any async widgets
1235                     function(w, ww) { 
1236                         copy[field](ww.getFormattedValue()) 
1237                         self.copyWidgetCache[copy.id()][field] = w;
1238                     }
1239                 );
1240                 dojo.connect(widget.widget, 'onChange', 
1241                     function(val) { 
1242                         if(copy.isnew() || val != copy[field]()) {
1243                             // prevent setting ischanged() automatically on widget load for existing copies
1244                             copy[field](widget.getFormattedValue()) 
1245                             copy.ischanged(true);
1246                         }
1247                     }
1248                 );
1249             }
1250         );
1251
1252         this.updateLidState(copy, row);
1253     };
1254
1255     this.updateLidState = function(copy, row) {
1256         if (typeof(row) == "undefined") {
1257             row = dojo.query(
1258                 'tr[copy_id="' + copy.id() + '"]', this.copyTbody
1259             )[0];
1260         }
1261
1262         var self = this;
1263         var recv_link = nodeByName("receive", row);
1264         var unrecv_link = nodeByName("unreceive", row);
1265         var del_link = nodeByName("delete", row);
1266         var cxl_link = nodeByName("cancel", row);
1267         var cxl_reason_link = nodeByName("cancel_reason", row);
1268
1269         if (copy.cancel_reason()) {
1270             openils.Util.hide(del_link.parentNode);
1271             openils.Util.hide(recv_link);
1272             openils.Util.hide(unrecv_link);
1273             openils.Util.hide(cxl_link);
1274
1275             /* XXX the following may leak memory in a long lived table: dijits may not get destroyed... not positive. revisit. */
1276             var holds_reason = dojo.create(
1277                 "span", {
1278                     "style": "border-bottom: 1px dashed #000;",
1279                     "innerHTML": "Cancelled" /* XXX [sic] and i18n */
1280                 }, cxl_reason_link, "only"
1281             );
1282             new dijit.Tooltip(
1283                 {
1284                     "label": "<em>" + copy.cancel_reason().label() +
1285                         "</em><br />" + copy.cancel_reason().description(),
1286                     "connectId": [holds_reason]
1287                 }, dojo.create("span", null, cxl_reason_link, "last")
1288             );
1289             openils.Util.show(cxl_reason_link, "inline");
1290         } else if (this.isPO) {
1291             openils.Util.hide(del_link.parentNode);
1292             openils.Util.hide(cxl_reason_link);
1293
1294             /* Avoid showing (un)receive links, cancel links, for virt copies */
1295             if (copy.id() > 0) {
1296                 if(copy.recv_time()) {
1297                     openils.Util.hide(cxl_link);
1298                     openils.Util.hide(recv_link);
1299                     openils.Util.show(unrecv_link, "inline");
1300                     unrecv_link.onclick = function() {
1301                         if (confirm(localeStrings.UNRECEIVE_LID))
1302                             self.issueReceive(copy, /* rollback */ true);
1303                     };
1304                 } else {
1305                     openils.Util.hide(unrecv_link);
1306                     openils.Util.show(recv_link, "inline");
1307                     openils.Util.show(cxl_link, "inline");
1308                     recv_link.onclick = function() {
1309                         if (self.checkLiAlerts(copy.lineitem()))
1310                             self.issueReceive(copy);
1311                     };
1312                     cxl_link.onclick = function() {
1313                         self.cancelLid(copy.id());
1314                     };
1315                 }
1316             } else {
1317                 openils.Util.hide(cxl_link);
1318                 openils.Util.hide(unrecv_link);
1319                 openils.Util.hide(recv_link);
1320             }
1321         } else {
1322             openils.Util.hide(unrecv_link);
1323             openils.Util.hide(recv_link);
1324             openils.Util.hide(cxl_reason_link);
1325
1326             del_link.onclick = function() { self.deleteCopy(row) };
1327             openils.Util.show(del_link.parentNode);
1328         }
1329     }
1330
1331     this.cancelLid = function(lid_id) {
1332         lidCancelDialog._lid_id = lid_id;
1333         openils.Util.show(lidCancelDialog.domNode.parentNode);
1334         lidCancelDialog.show();
1335         if (!lidCancelDialog._prepared) {
1336             var widget = new openils.widget.AutoFieldWidget({
1337                 "fmField": "cancel_reason",
1338                 "fmClass": "acqlid",
1339                 "parentNode": dojo.byId("acq-lit-lid-cancel-reason"),
1340                 "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
1341                 "forceSync": true
1342             });
1343             widget.build(
1344                 function(w, ww) {
1345                     acqLidCancelButton.onClick = function() {
1346                         if (w.attr("value")) {
1347                             if (confirm(localeStrings.LID_CANCEL_CONFIRM)) {
1348                                 self._cancelLid(
1349                                     lidCancelDialog._lid_id,
1350                                     w.attr("value")
1351                                 );
1352                             }
1353                             lidCancelDialog.hide();
1354                         }
1355                     };
1356                     lidCancelDialog._prepared = true;
1357                 }
1358             );
1359         }
1360     };
1361
1362     this._cancelLid = function(lid_id, reason) {
1363         fieldmapper.standardRequest(
1364             ["open-ils.acq", "open-ils.acq.lineitem_detail.cancel"], {
1365                 "params": [openils.User.authtoken, lid_id, reason],
1366                 "async": true,
1367                 "onresponse": function(r) {
1368                     if (r = openils.Util.readResponse(r)) {
1369                         if (r.lid) {
1370                             for (var id in r.lid) {
1371                                 /* actually this should only iterate once */
1372                                 self.copyCache[id].cancel_reason(
1373                                     r.lid[id].cancel_reason
1374                                 );
1375                                 self.updateLidState(self.copyCache[id]);
1376                             }
1377                         }
1378                     }
1379                 }
1380             }
1381         );
1382     };
1383
1384     this._confirmAlert = function(li, lin) {
1385         return confirm(
1386             dojo.string.substitute(
1387                 localeStrings.CONFIRM_LI_ALERT, [
1388                     (new openils.acq.Lineitem({"lineitem": li})).findAttr(
1389                         "title", "lineitem_marc_attr_definition"
1390                     ),
1391                     lin.alert_text().code(),
1392                     lin.alert_text().description() || "",
1393                     lin.value()
1394                 ]
1395             )
1396         );
1397     };
1398
1399     this.checkLiAlerts = function(li_id) {
1400         var li = this.liCache[li_id];
1401
1402         var alert_notes = li.lineitem_notes().filter(
1403             function(o) { return Boolean(o.alert_text()); }
1404         );
1405
1406         /* this is _intentionally_ not done in a call to forEach() ... */
1407         for (var i = 0; i < alert_notes.length; i++) {
1408             if (this.noteAcks[alert_notes[i].id()])
1409                 continue;
1410             else if (!this._confirmAlert(li, alert_notes[i]))
1411                 return false;
1412             else
1413                 this.noteAcks[alert_notes[i].id()] = true;
1414         }
1415
1416         return true;
1417     };
1418
1419     this.deleteCopy = function(row) {
1420         var copy = this.copyCache[row.getAttribute('copy_id')];
1421         copy.isdeleted(true);
1422         if(copy.isnew())
1423             delete this.copyCache[copy.id()];
1424         this.copyTbody.removeChild(row);
1425     }
1426
1427     this._virtDfaCountsAsList = function() {
1428         var L = [];
1429         for (var key in this.virtDfaCounts) {
1430             for (var i = 0; i < this.virtDfaCounts[key]; i++)
1431                 L.push(key);
1432         }
1433         return L;
1434     }
1435
1436     this.saveCopyChanges = function(liId) {
1437         var self = this;
1438         var copies = [];
1439
1440
1441         var total = 0;
1442         for(var id in this.copyCache) {
1443             var c = this.copyCache[id];
1444             if(!c.isdeleted()) total++;
1445             if(c.isnew() || c.ischanged() || c.isdeleted()) {
1446                 if(c.id() < 0) c.id(null);
1447                 copies.push(c);
1448             }
1449         }
1450
1451         if (typeof(this._copy_count_cb) == "function") {
1452             this._copy_count_cb(liId, total);
1453         }
1454
1455         dojo.byId('acq-lit-copy-count-label-' + liId).innerHTML = total;
1456
1457
1458         if (copies.length > 0) {
1459             openils.Util.show("acq-lit-update-copies-progress");
1460             fieldmapper.standardRequest(
1461                 ['open-ils.acq', 'open-ils.acq.lineitem_detail.cud.batch'],
1462                 {   async: true,
1463                     params: [openils.User.authtoken, copies],
1464                     onresponse: function(r) {
1465                         var res = openils.Util.readResponse(r);
1466                         litUpdateCopiesProgress.update(res);
1467                     },
1468                     oncomplete: function() {
1469                         self.drawCopies(liId);
1470                         openils.Util.hide("acq-lit-update-copies-progress");
1471                     }
1472                 }
1473             );
1474         }
1475
1476         var dfa_list = this._virtDfaCountsAsList();
1477         if (dfa_list.length > 0) {
1478             fieldmapper.standardRequest(
1479                 ["open-ils.acq",
1480                 "open-ils.acq.distribution_formula.record_application"],
1481                 {
1482                     "async": true,
1483                     "params": [openils.User.authtoken, dfa_list, liId],
1484                     "onresponse": function(r) {
1485                         var res = openils.Util.readResponse(r);
1486                         if (res && res.length < dfa_list.length)
1487                             alert(localeStrings.DFA_NOT_ALL);
1488                     }
1489                 }
1490             );
1491             this.virtDfaCounts = {};
1492         }
1493     }
1494
1495     this._updateCreatePoPrepayCheckbox = function(prepay) {
1496         var prepay = openils.Util.isTrue(prepay);
1497         this._prepayRequiredByVendor = prepay;
1498         dijit.byId("acq-lit-po-prepay").attr("checked", prepay);
1499     };
1500
1501     this._confirmPoPrepaySituation = function() {
1502         var want_prepay = dijit.byId("acq-lit-po-prepay").attr("checked");
1503         if (want_prepay != this._prepayRequiredByVendor) {
1504             return confirm(
1505                 want_prepay ?
1506                     localeStrings.VENDOR_SAYS_PREPAY_NOT_NEEDED :
1507                     localeStrings.VENDOR_SAYS_PREPAY_NEEDED
1508             );
1509         } else {
1510             return true;
1511         }
1512     };
1513
1514     this.applySelectedLiAction = function(action) {
1515         var self = this;
1516         switch(action) {
1517
1518             case 'delete_selected':
1519                 this._deleteLiList(self.getSelected());
1520                 break;
1521
1522             case 'create_order':
1523
1524                 if(!this.createPoProviderSelector) {
1525                     var widget = new openils.widget.AutoFieldWidget({
1526                         fmField : 'provider',
1527                         fmClass : 'acqpo',
1528                         searchFilter: {"active": "t"},
1529                         parentNode : dojo.byId('acq-lit-po-provider'),
1530                         dijitArgs : {
1531                             "onChange": function() {
1532                                 if (this.item) {
1533                                     self._updateCreatePoPrepayCheckbox(
1534                                         this.item.prepayment_required
1535                                     );
1536                                 }
1537                             }
1538                         }
1539                     });
1540                     widget.build(
1541                         function(w) { self.createPoProviderSelector = w; }
1542                     );
1543                 }
1544
1545                 if(!this.createPoAgencySelector) {
1546                     var widget = new openils.widget.AutoFieldWidget({
1547                         fmField : 'ordering_agency',
1548                         fmClass : 'acqpo',
1549                         parentNode : dojo.byId('acq-lit-po-agency'),
1550                         orgLimitPerms : ['CREATE_PURCHASE_ORDER'],
1551                     });
1552                     widget.build(
1553                         function(w) { self.createPoAgencySelector = w; }
1554                     );
1555                 }
1556
1557          
1558                 acqLitPoCreateDialog.show();
1559                 break;
1560
1561             case 'save_picklist':
1562                 this._loadPLSelect();
1563                 acqLitSavePlDialog.show();
1564                 break;
1565
1566             case 'selector_ready':
1567             case 'order_ready':
1568                 acqLitChangeLiStateDialog.attr('state', action.replace('_', '-'));
1569                 acqLitChangeLiStateDialog.show();
1570                 break;
1571
1572             case 'print_po':
1573                 this.printPO();
1574                 break;
1575
1576             case 'receive_po':
1577                 this.receivePO();
1578                 break;
1579
1580             case 'rollback_receive_po':
1581                 this.rollbackPoReceive();
1582                 break;
1583
1584             case 'create_assets':
1585                 this.createAssets();
1586                 break;
1587
1588             case 'export_attr_list':
1589                 this.chooseExportAttr();
1590                 break;
1591
1592             case 'add_brief_record':
1593                 if(this.isPO)
1594                     location.href = oilsBasePath + '/acq/picklist/brief_record?po=' + this.isPO;
1595                 else
1596                     location.href = oilsBasePath + '/acq/picklist/brief_record?pl=' + this.isPL;
1597
1598                 break;
1599
1600             case "cancel_lineitems":
1601                 this.maybeCancelLineitems();
1602                 break;
1603         }
1604     }
1605
1606     this.createAssets = function() {
1607         if(!this.isPO) return;
1608         if(!confirm(localeStrings.CREATE_PO_ASSETS_CONFIRM)) return;
1609         this.show('acq-lit-progress-numbers');
1610         var self = this;
1611         fieldmapper.standardRequest(
1612             ['open-ils.acq', 'open-ils.acq.purchase_order.assets.create'],
1613             {   async: true,
1614                 params: [this.authtoken, this.isPO],
1615                 onresponse: function(r) {
1616                     var resp = openils.Util.readResponse(r);
1617                     self._updateProgressNumbers(resp, true);
1618                 }
1619             }
1620         );
1621     }
1622
1623     this.maybeCancelLineitems = function() {
1624         openils.Util.show("acq-lit-cancel-reason", "inline");
1625         if (!acqLitCancelLineitemsButton._prepared) {
1626             var widget = new openils.widget.AutoFieldWidget({
1627                 "fmField": "cancel_reason",
1628                 "fmClass": "jub",
1629                 "parentNode": dojo.byId("acq-lit-cancel-reason-selector"),
1630                 "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
1631                 "forceSync": true
1632             });
1633             widget.build(
1634                 function(w, ww) {
1635                     acqLitCancelLineitemsButton.onClick = function() {
1636                         if (w.attr("value")) {
1637                             if (confirm(localeStrings.LI_CANCEL_CONFIRM)) {
1638                                 self._cancelLineitems(w.attr("value"));
1639                             }
1640                             openils.Util.hide("acq-lit-cancel-reason");
1641                         }
1642                     };
1643                     acqLitCancelLineitemsButton._prepared = true;
1644                 }
1645             );
1646         }
1647     };
1648
1649     this._cancelLineitems = function(reason) {
1650         var id_list = this.getSelected().map(function(o) { return o.id(); });
1651         fieldmapper.standardRequest(
1652             ["open-ils.acq", "open-ils.acq.lineitem.cancel.batch"], {
1653                 "params": [openils.User.authtoken, id_list, reason],
1654                 "async": true,
1655                 "onresponse": function(r) {
1656                     if (r = openils.Util.readResponse(r)) {
1657                         if (r.li) {
1658                             for (var id in r.li) {
1659                                 self.liCache[id].state(r.li[id].state);
1660                                 self.liCache[id].cancel_reason(
1661                                     r.li[id].cancel_reason
1662                                 );
1663                                 self.updateLiState(self.liCache[id]);
1664                             }
1665                         }
1666                         if (r.lid && self.copyCache) {
1667                             for (var id in r.lid) {
1668                                 if (self.copyCache[id]) {
1669                                     self.copyCache[id].cancel_reason(
1670                                         r.lid[id].cancel_reason
1671                                     );
1672                                     self.updateLidState(self.copyCache[id]);
1673                                 }
1674                             }
1675                         }
1676                     }
1677                 }
1678             }
1679         );
1680     };
1681
1682     this.chooseExportAttr = function() {
1683         if (!acqLitExportAttrSelector._li_setup) {
1684             var self = this;
1685             acqLitExportAttrSelector.store = new dojo.data.ItemFileReadStore(
1686                 {
1687                     "data": acqliad.toStoreData(
1688                         this.pcrud.search(
1689                             "acqliad", {"code": li_exportable_attrs}
1690                         )
1691                     )
1692                 }
1693             );
1694             acqLitExportAttrSelector.setValue();
1695             acqLitExportAttrButton.onClick = function(){self.exportAttrList();};
1696             acqLitExportAttrSelector._li_setup = true;
1697         }
1698         openils.Util.show("acq-lit-export-attr-holder", "inline");
1699     };
1700
1701     this.exportAttrList = function() {
1702         var attr_def = acqLitExportAttrSelector.item;
1703         var li_list = this.getSelected();
1704         var value_list = li_list.map(
1705             function(li) {
1706                 return (new openils.acq.Lineitem({"lineitem": li})).findAttr(
1707                     attr_def.code, "lineitem_marc_attr_definition"
1708                 );
1709             }
1710         ).filter(function(attr) { return Boolean(attr); });
1711
1712         if (value_list.length > 0) {
1713             if (value_list.length < li_list.length) {
1714                 if (!confirm(
1715                     dojo.string.substitute(
1716                         localeStrings.EXPORT_SHORT_LIST, [attr_def.description]
1717                     )
1718                 )) {
1719                     return;
1720                 }
1721             }
1722             try {
1723                 openils.XUL.contentToFileSaveDialog(
1724                     value_list.join("\n"),
1725                     localeStrings.EXPORT_SAVE_DIALOG_TITLE
1726                 );
1727             } catch (E) {
1728                 alert(E);
1729             }
1730         } else {
1731             alert(dojo.string.substitute(
1732                 localeStrings.EXPORT_EMPTY_LIST, [attr_def.description]
1733             ));
1734         }
1735
1736         openils.Util.hide("acq-lit-export-attr-holder");
1737     };
1738
1739     this.printPO = function() {
1740         if(!this.isPO) return;
1741         progressDialog.show(true);
1742         fieldmapper.standardRequest(
1743             ['open-ils.acq', 'open-ils.acq.purchase_order.format'],
1744             {   async: true,
1745                 params: [this.authtoken, this.isPO, 'html'],
1746                 oncomplete: function(r) {
1747                     progressDialog.hide();
1748                     var evt = openils.Util.readResponse(r);
1749                     if(evt && evt.template_output()) {
1750                         win = window.open('','', 'resizable,width=800,height=600,scrollbars=1');
1751                         win.document.body.innerHTML = evt.template_output().data();
1752                     }
1753                 }
1754             }
1755         );
1756     }
1757
1758
1759     this.receivePO = function() {
1760         if (!this.isPO) return;
1761
1762         for (var id in this.liCache) {
1763             /* assumption: liCache reflects exactly the
1764              * set of LIs that belong to our PO */
1765             if (this.liCache[id].state() != "received" &&
1766                 !this.checkLiAlerts(id)) return;
1767         }
1768
1769         this.show('acq-lit-progress-numbers');
1770         var self = this;
1771         fieldmapper.standardRequest(
1772             ['open-ils.acq', 'open-ils.acq.purchase_order.receive'],
1773             {   async: true,
1774                 params: [this.authtoken, this.isPO],
1775                 onresponse : function(r) {
1776                     var resp = openils.Util.readResponse(r);
1777                     self._updateProgressNumbers(resp, true);
1778                 },
1779             }
1780         );
1781     }
1782
1783     this.issueReceive = function(obj, rollback) {
1784         /* (For now) there shall be no marking LI or LIDs (un)received
1785          * except from the actual "view PO" interface. */
1786         if (!this.isPO) return;
1787
1788         var part =
1789             {"jub": "lineitem", "acqlid": "lineitem_detail"}[obj.classname];
1790         var method =
1791             "open-ils.acq." + part + ".receive" + (rollback ? ".rollback" : "");
1792
1793         progressDialog.show(true);
1794         fieldmapper.standardRequest(
1795             ["open-ils.acq", method], {
1796                 "async": true,
1797                 "params": [this.authtoken, obj.id()],
1798                 "onresponse": function(r) {
1799                     self.handleReceive(openils.Util.readResponse(r));
1800                     progressDialog.hide();
1801                 }
1802             }
1803         );
1804     };
1805
1806     /**
1807      * Handles the responses from receive and rollback ML calls.
1808      */
1809     this.handleReceive = function(resp) {
1810         if (resp) {
1811             if (resp.li) {
1812                 for (var li_id in resp.li) {
1813                     for (var key in resp.li[li_id])
1814                         self.liCache[li_id][key](resp.li[li_id][key]);
1815                     self.updateLiState(self.liCache[li_id]);
1816                 }
1817             }
1818             if (resp.po) {
1819                 if (typeof(self.poUpdateCallback) == "function")
1820                     self.poUpdateCallback(resp.po);
1821             }
1822             if (resp.lid) {
1823                 for (var lid_id in resp.lid) {
1824                     for (var key in resp.lid[lid_id])
1825                         self.copyCache[lid_id][key](resp.lid[lid_id][key]);
1826                     self.updateLidState(self.copyCache[lid_id]);
1827                 }
1828             }
1829         }
1830     };
1831
1832     this.rollbackPoReceive = function() {
1833         if(!this.isPO) return;
1834         if(!confirm(localeStrings.ROLLBACK_PO_RECEIVE_CONFIRM)) return;
1835         this.show('acq-lit-progress-numbers');
1836         var self = this;
1837         fieldmapper.standardRequest(
1838             ['open-ils.acq', 'open-ils.acq.purchase_order.receive.rollback'],
1839             {   async: true,
1840                 params: [this.authtoken, this.isPO],
1841                 onresponse : function(r) {
1842                     var resp = openils.Util.readResponse(r);
1843                     self._updateProgressNumbers(resp, true);
1844                 },
1845             }
1846         );
1847     }
1848
1849     this._updateProgressNumbers = function(resp, reloadOnComplete) {
1850         if(!resp) return;
1851         dojo.byId('acq-pl-lit-li-processed').innerHTML = resp.li;
1852         dojo.byId('acq-pl-lit-lid-processed').innerHTML = resp.lid;
1853         dojo.byId('acq-pl-lit-debits-processed').innerHTML = resp.debits_accrued;
1854         dojo.byId('acq-pl-lit-bibs-processed').innerHTML = resp.bibs;
1855         dojo.byId('acq-pl-lit-indexed-processed').innerHTML = resp.indexed;
1856         dojo.byId('acq-pl-lit-copies-processed').innerHTML = resp.copies;
1857         if(resp.complete && reloadOnComplete) 
1858             location.href = location.href;
1859     }
1860
1861
1862     this._createPO = function(fields) {
1863         this.show('acq-lit-progress-numbers');
1864         var po = new fieldmapper.acqpo();
1865         po.provider(this.createPoProviderSelector.attr('value'));
1866         po.ordering_agency(this.createPoAgencySelector.attr('value'));
1867         po.prepayment_required(fields.prepayment_required[0] ? true : false);
1868
1869         var selected = this.getSelected( (fields.create_from == 'all') );
1870         if(selected.length == 0) return;
1871
1872         var max = selected.length * 3;
1873
1874         var self = this;
1875         fieldmapper.standardRequest(
1876             ['open-ils.acq', 'open-ils.acq.purchase_order.create'],
1877             {   async: true,
1878                 params: [
1879                     openils.User.authtoken, 
1880                     po, 
1881                     {
1882                         lineitems : selected.map(function(li) { return li.id() }),
1883                         create_assets : fields.create_assets[0],
1884                     }
1885                 ],
1886
1887                 onresponse : function(r) {
1888                     var resp = openils.Util.readResponse(r);
1889                     self._updateProgressNumbers(resp);
1890                     if(resp.complete) 
1891                         location.href = oilsBasePath + '/eg/acq/po/view/' + resp.purchase_order.id();
1892                 }
1893             }
1894         );
1895     }
1896
1897     this._deleteLiList = function(list, idx) {
1898         if(idx == null) idx = 0;
1899         if(idx >= list.length) return;
1900         var liId = list[idx].id();
1901         fieldmapper.standardRequest(
1902             ['open-ils.acq', 'open-ils.acq.lineitem.delete'],
1903             {   async: true,
1904                 params: [openils.User.authtoken, liId],
1905                 oncomplete: function(r) {
1906                     self.removeLineitem(liId);
1907                     self._deleteLiList(list, ++idx);
1908                 }
1909             }
1910         );
1911     }
1912
1913     this.editOrderMarc = function(li) {
1914
1915         /*  To run in Firefox directly, must set signed.applets.codebase_principal_support
1916             to true in about:config */
1917
1918         if(!openils.XUL.enableXPConnect()) return;
1919
1920         if(openils.XUL.isXUL()) {
1921             win = window.open('/xul/' + openils.XUL.buildId() + '/server/cat/marcedit.xul');
1922         } else {
1923             win = window.open('/xul/server/cat/marcedit.xul'); 
1924         }
1925         var self = this;
1926         win.xulG = {
1927             record : {marc : li.marc()},
1928             save : {
1929                 label: 'Save Record', // XXX I18N
1930                 func: function(xmlString) {
1931                     li.marc(xmlString);
1932                     fieldmapper.standardRequest(
1933                         ['open-ils.acq', 'open-ils.acq.lineitem.update'],
1934                         {   async: true,
1935                             params: [openils.User.authtoken, li],
1936                             oncomplete: function(r) {
1937                                 openils.Util.readResponse(r);
1938                                 win.close();
1939                                 self.drawInfo(li.id())
1940                             }
1941                         }
1942                     );
1943                 },
1944             }
1945         };
1946     }
1947
1948     this._savePl = function(values) {
1949         var self = this;
1950         var selected = this.getSelected( (values.which == 'all') );
1951         openils.Util.show('acq-lit-generic-progress');
1952
1953         if(values.new_name) {
1954             openils.acq.Picklist.create(
1955                 {name: values.new_name}, 
1956                 function(id) {
1957                     self._updateLiList(id, selected, 0, 
1958                         function(){
1959                             location.href = oilsBasePath + '/eg/acq/picklist/view/' + id;
1960                         });
1961                 }
1962             );
1963         } else if(values.existing_pl) {
1964             // update lineitems to use an existing picklist
1965             self._updateLiList(values.existing_pl, selected, 0, 
1966                 function(){
1967                     location.href = oilsBasePath + '/eg/acq/picklist/view/' + values.existing_pl;
1968                 });
1969         }
1970     }
1971
1972     this._updateLiState = function(values, state) {
1973         var self = this;
1974         var selected = this.getSelected( (values.which == 'all') );
1975         if(!selected.length) return;
1976         dojo.forEach(selected, function(li) {li.state(state);});
1977         self._updateLiList(null, selected, 0, 
1978             // TODO consider inline updates for efficiency
1979             function() { location.href = location.href }
1980         );
1981     }
1982
1983     this._updateLiList = function(pl, list, idx, oncomplete) {
1984         if(idx >= list.length) return oncomplete();
1985         var li = list[idx];
1986         if(pl != null) li.picklist(pl);
1987         litGenericProgress.update({maximum: list.length, progress: idx});
1988         new openils.acq.Lineitem({lineitem:li}).update(
1989             function(r) {
1990                 self._updateLiList(pl, list, ++idx, oncomplete);
1991             }
1992         );
1993     }
1994
1995     this._loadPLSelect = function() {
1996         if(this._plSelectLoaded) return;
1997         var plList = [];
1998         function handleResponse(r) {
1999             plList.push(r.recv().content());
2000         }
2001         var method = 'open-ils.acq.picklist.user.retrieve';
2002         fieldmapper.standardRequest(
2003             ['open-ils.acq', method],
2004             {   async: true,
2005                 params: [this.authtoken],
2006                 onresponse: handleResponse,
2007                 oncomplete: function() {
2008                     self._plSelectLoaded = true;
2009                     acqLitAddExistingSelect.store = 
2010                         new dojo.data.ItemFileReadStore({data:acqpl.toStoreData(plList)});
2011                     acqLitAddExistingSelect.setValue();
2012                 }
2013             }
2014         );
2015     }
2016
2017     this.showRealCopyEditUI = function(li) {
2018         copyList = [];
2019         var self = this;
2020         this.volCache = {};
2021
2022         this._fetchLineitem(li.id(), 
2023             function(fullLi) {
2024                 li = self.liCache[li.id()] = fullLi;
2025
2026                 self.pcrud.search(
2027                     'acp', {
2028                         id : li.lineitem_details().map(
2029                             function(item) { return item.eg_copy_id() }
2030                         )
2031                     }, {
2032                         async : true,
2033                         oncomplete : function(r) {
2034                             try {
2035                                 var r_list = openils.Util.readResponse( r );
2036                                 for (var i = 0; i < r_list.length; i++) {
2037                                     var copy = r_list[i];
2038                                     var volId = copy.call_number();
2039                                     var volume = self.volCache[volId];
2040                                     if(!volume) {
2041                                         volume = self.volCache[volId] = self.pcrud.retrieve('acn', volId);
2042                                     }
2043                                     copy.call_number(volume);
2044                                     copyList.push(copy);
2045                                 }
2046                                 if (xulG) {
2047                                     // If we need to, we can pass in an update_copy function to handle the update instead of volume_item_creator
2048                                     xulG.volume_item_creator( { 'existing_copies' : copyList } );
2049                                 }
2050                             } catch(E) {
2051                                 alert('error in oncomplete: ' + E);
2052                             }
2053                         }
2054                     }
2055                 );
2056             }
2057         );
2058     }
2059
2060     
2061     /*
2062     this.saveRealCopies = function() {
2063         progressDialog.show(true);
2064         var list = this.realCopyList.filter(function(copy) { return copy.ischanged(); });
2065         this.pcrud.update(list, {oncomplete: function() { 
2066             progressDialog.hide();
2067             self.show('list');
2068         }});
2069     }
2070
2071     // grab the li-details for this lineitem, grab the linked copies and volumes, add them to the table
2072     this.showRealCopies = function(li) {
2073         while(this.realCopiesTbody.childNodes[0])
2074             this.realCopiesTbody.removeChild(this.realCopiesTbody.childNodes[0]);
2075         this.show('real-copies');
2076
2077         this.realCopyList = [];
2078         this.volCache = {};
2079         var tabIndex = 1000;
2080         var self = this;
2081
2082         acqLitSaveRealCopies.onClick = function() {
2083             self.saveRealCopies();
2084         }
2085
2086         this._fetchLineitem(li.id(), 
2087             function(fullLi) {
2088                 li = self.liCache[li.id()] = fullLi;
2089
2090                 self.pcrud.search(
2091                     'acp', {
2092                         id : li.lineitem_details().map(
2093                             function(item) { return item.eg_copy_id() }
2094                         )
2095                     }, {
2096                         async : true,
2097                         streaming : true,
2098                         onresponse : function(r) {
2099                             var copy = openils.Util.readResponse(r);
2100                             var volId = copy.call_number();
2101                             var volume = self.volCache[volId];
2102                             if(!volume) {
2103                                 volume = self.volCache[volId] = self.pcrud.retrieve('acn', volId);
2104                             }
2105                             self.addRealCopy(volume, copy, tabIndex++);
2106                         }
2107                     }
2108                 );
2109             }
2110         );
2111     }
2112
2113     this.addRealCopy = function(volume, copy, tabIndex) {
2114         var row = this.realCopiesRow.cloneNode(true);
2115         this.realCopyList.push(copy);
2116
2117         var selectNode;
2118         dojo.forEach(
2119             ['owning_lib', 'location', 'circ_modifier', 'label', 'barcode'],
2120
2121             function(field) {
2122                 var isvol = (field == 'owning_lib' || field == 'label');
2123                 var widget = new openils.widget.AutoFieldWidget({
2124                     fmField : field,
2125                     fmObject : isvol ? volume : copy,
2126                     parentNode : nodeByName(field, row),
2127                     readOnly : (field != 'barcode'),
2128                 });
2129
2130                 var widgetDrawn = null;
2131
2132                 if(field == 'barcode') {
2133
2134                     widgetDrawn = function(w, ww) {
2135                         var node = w.domNode;
2136                         node.setAttribute('tabindex', ''+tabIndex);
2137
2138                         // on enter, select the next barcode input
2139                         dojo.connect(w, 'onKeyDown',
2140                             function(e) {
2141                                 if(e.keyCode == dojo.keys.ENTER) {
2142                                     var ti = node.getAttribute('tabindex');
2143                                     var nextNode = dojo.query('[tabindex=' + String(Number(ti) + 1) + ']', self.realCopiesTbody)[0];
2144                                     if(nextNode) nextNode.select();
2145                                 }
2146                             }
2147                         );
2148
2149                         dojo.connect(w, 'onChange', 
2150                             function(val) { 
2151                                 if(!val || val == copy.barcode()) return;
2152                                 copy.ischanged(true);
2153                                 copy.barcode(val);
2154                             }
2155                         );
2156
2157
2158                         if(self.realCopiesTbody.getElementsByTagName('TR').length == 0)
2159                             selectNode = node;
2160                     }
2161                 }
2162
2163                 widget.build(widgetDrawn);
2164             }
2165         );
2166
2167         this.realCopiesTbody.appendChild(row);
2168         if(selectNode) selectNode.select();
2169     };
2170     */
2171
2172 }