]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/common/li_table.js
Acq: Distribution formula dropdown in lineitem detail editor shows use count.
[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.ProgressBar');
8 dojo.require('openils.User');
9 dojo.require('openils.Util');
10 dojo.require('openils.acq.Lineitem');
11 dojo.require('openils.acq.PO');
12 dojo.require('openils.acq.Picklist');
13 dojo.require('openils.widget.AutoFieldWidget');
14 dojo.require('dojo.data.ItemFileReadStore');
15 dojo.require('openils.widget.ProgressDialog');
16 dojo.require('openils.PermaCrud');
17 dojo.require('openils.XUL');
18
19 dojo.requireLocalization('openils.acq', 'acq');
20 var localeStrings = dojo.i18n.getLocalization('openils.acq', 'acq');
21 const XUL_OPAC_WRAPPER = 'chrome://open_ils_staff_client/content/cat/opac.xul';
22 var li_exportable_attrs = ["issn", "isbn", "upc"];
23
24 function nodeByName(name, context) {
25     return dojo.query('[name='+name+']', context)[0];
26 }
27
28
29 var liDetailBatchFields = ['fund', 'owning_lib', 'location', 'collection_code', 'circ_modifier', 'cn_label'];
30 var liDetailFields = liDetailBatchFields.concat(['barcode', 'note']);
31
32 function AcqLiTable() {
33
34     var self = this;
35     this.liCache = {};
36     this.plCache = {};
37     this.poCache = {};
38     this.dfaCache = {};
39     this.dfeOffset = 0;
40     this.toggleState = false;
41     this.tbody = dojo.byId('acq-lit-tbody');
42     this.selectors = [];
43     this.noteAcks = {};
44     this.authtoken = openils.User.authtoken;
45     this.rowTemplate = this.tbody.removeChild(dojo.byId('acq-lit-row'));
46     this.copyTbody = dojo.byId('acq-lit-li-details-tbody');
47     this.copyRow = this.copyTbody.removeChild(dojo.byId('acq-lit-li-details-row'));
48     this.copyBatchRow = dojo.byId('acq-lit-li-details-batch-row');
49     this.copyBatchWidgets = {};
50     this.liNotesTbody = dojo.byId('acq-lit-notes-tbody');
51     this.liNotesRow = this.liNotesTbody.removeChild(dojo.byId('acq-lit-notes-row'));
52     this.realCopiesTbody = dojo.byId('acq-lit-real-copies-tbody');
53     this.realCopiesRow = this.realCopiesTbody.removeChild(dojo.byId('acq-lit-real-copies-row'));
54     this._copy_fields_for_acqdf = ['owning_lib', 'location'];
55
56     dojo.connect(acqLitLiActionsSelector, 'onChange', 
57         function() { 
58             self.applySelectedLiAction(this.attr('value')) 
59             acqLitLiActionsSelector.attr('value', '_');
60         });
61
62     acqLitCreatePoSubmit.onClick = function() {
63         acqLitPoCreateDialog.hide();
64         self._createPO(acqLitPoCreateDialog.getValues());
65     }
66
67     acqLitSavePlButton.onClick = function() {
68         acqLitSavePlDialog.hide();
69         self._savePl(acqLitSavePlDialog.getValues());
70     }
71
72     acqLitCancelLiStateButton.onClick = function() {
73         acqLitChangeLiStateDialog.hide();
74     }
75     acqLitSaveLiStateButton.onClick = function() {
76         acqLitChangeLiStateDialog.hide();
77         self._updateLiState(acqLitChangeLiStateDialog.getValues(), acqLitChangeLiStateDialog.attr('state'));
78     }
79
80
81     //dojo.byId('acq-lit-notes-new-button').onclick = function(){acqLitCreateLiNoteDialog.show();}
82
83     dojo.byId('acq-lit-select-toggle').onclick = function(){self.toggleSelect()};
84     dojo.byId('acq-lit-info-back-button').onclick = function(){self.show('list')};
85     dojo.byId('acq-lit-copies-back-button').onclick = function(){self.show('list')};
86     dojo.byId('acq-lit-notes-back-button').onclick = function(){self.show('list')};
87     dojo.byId('acq-lit-real-copies-back-button').onclick = function(){self.show('list')};
88
89     this.reset = function() {
90         while(self.tbody.childNodes[0])
91             self.tbody.removeChild(self.tbody.childNodes[0]);
92         self.selectors = [];
93         self.noteAcks = {};
94     };
95     
96     this.setNext = function(handler) {
97         var link = dojo.byId('acq-lit-next');
98         if(handler) {
99             dojo.style(link, 'visibility', 'visible');
100             link.onclick = handler;
101         } else {
102             dojo.style(link, 'visibility', 'hidden');
103         }
104     };
105
106     this.setPrev = function(handler) {
107         var link = dojo.byId('acq-lit-prev');
108         if(handler) {
109             dojo.style(link, 'visibility', 'visible'); 
110             link.onclick = handler; 
111         } else {
112             dojo.style(link, 'visibility', 'hidden');
113         }
114     };
115
116     this.show = function(div) {
117         openils.Util.hide('acq-lit-table-div');
118         openils.Util.hide('acq-lit-info-div');
119         openils.Util.hide('acq-lit-li-details');
120         openils.Util.hide('acq-lit-notes-div');
121         openils.Util.hide('acq-lit-real-copies-div');
122         switch(div) {
123             case 'list':
124                 openils.Util.show('acq-lit-table-div');
125                 break;
126             case 'info':
127                 openils.Util.show('acq-lit-info-div');
128                 break;
129             case 'copies':
130                 openils.Util.show('acq-lit-li-details');
131                 break;
132             case 'real-copies':
133                 openils.Util.show('acq-lit-real-copies-div');
134                 break;
135             case 'notes':
136                 openils.Util.show('acq-lit-notes-div');
137                 break;
138             default:
139                 if(div) 
140                     openils.Util.show(div);
141         }
142     }
143
144     this.hide = function() {
145         this.show(null);
146     }
147
148     this.toggleSelect = function() {
149         if(self.toggleState) 
150             dojo.forEach(self.selectors, function(i){i.checked = false});
151         else 
152             dojo.forEach(self.selectors, function(i){i.checked = true});
153         self.toggleState = !self.toggleState;
154     };
155
156
157     /** @param all If true, assume all are selected */
158     this.getSelected = function(all) {
159         var selected = [];
160         dojo.forEach(self.selectors, 
161             function(i) { 
162                 if(i.checked || all)
163                     selected.push(self.liCache[i.parentNode.parentNode.getAttribute('li')]);
164             }
165         );
166         return selected;
167     };
168
169     this.setRowAttr = function(td, liWrapper, field, type) {
170         var val = liWrapper.findAttr(field, type || 'lineitem_marc_attr_definition') || '';
171         td.appendChild(document.createTextNode(val));
172     };
173
174     /**
175      * Inserts a single lineitem into the growing table of lineitems
176      * @param {Object} li The lineitem object to insert
177      */
178     this.addLineitem = function(li, skip_final_placement) {
179         this.liCache[li.id()] = li;
180
181         // sort the lineitem notes on edit_time
182         if(!li.lineitem_notes()) li.lineitem_notes([]);
183
184         var liWrapper = new openils.acq.Lineitem({lineitem:li});
185         var row = self.rowTemplate.cloneNode(true);
186         row.setAttribute('li', li.id());
187         var tds = dojo.query('[attr]', row);
188         dojo.forEach(tds, function(td) {self.setRowAttr(td, liWrapper, td.getAttribute('attr'), td.getAttribute('attr_type'));});
189         dojo.query('[name=source_label]', row)[0].appendChild(document.createTextNode(li.source_label()));
190
191         var isbn = liWrapper.findAttr('isbn', 'lineitem_marc_attr_definition');
192         if(isbn) {
193             // XXX media prefix for added content
194             dojo.query('[name=jacket]', row)[0].setAttribute('src', '/opac/extras/ac/jacket/small/' + isbn);
195         }
196
197         dojo.query('[attr=title]', row)[0].onclick = function() {self.drawInfo(li.id())};
198         dojo.query('[name=copieslink]', row)[0].onclick = function() {self.drawCopies(li.id())};
199         dojo.query('[name=notes_count]', row)[0].innerHTML = li.lineitem_notes().length;
200         dojo.query('[name=noteslink]', row)[0].onclick = function() {self.drawLiNotes(li)};
201
202         // show which PO this lineitem is a member of
203         if(li.purchase_order() && !this.isPO) {
204             var po = 
205                 this.poCache[li.purchase_order()] =
206                 this.poCache[li.purchase_order()] ||
207                 fieldmapper.standardRequest(
208                     ['open-ils.acq', 'open-ils.acq.purchase_order.retrieve'],
209                     {params: [
210                         this.authtoken, li.purchase_order(), {
211                             "flesh_price_summary": true,
212                             "flesh_lineitem_count": true
213                         }
214                     ]});
215             if(po && !this.isMeta) {
216                 openils.Util.show(nodeByName('po', row), 'inline');
217                 var link = nodeByName('po_link', row);
218                 link.setAttribute('href', oilsBasePath + '/acq/po/view/' + li.purchase_order());
219                 link.innerHTML = 'PO: ' + po.name(); // TODO i18n
220             }
221         }
222
223         // show which picklist this lineitem is a member of
224         if(li.picklist() && (this.isPO || this.isMeta)) {
225             var pl = 
226                 this.plCache[li.picklist()] = 
227                 this.plCache[li.picklist()] || 
228                 fieldmapper.standardRequest(
229                     ['open-ils.acq', 'open-ils.acq.picklist.retrieve'],
230                     {params: [this.authtoken, li.picklist()]});
231             if(pl) {
232                 openils.Util.show(nodeByName('pl', row), 'inline');
233                 var link = nodeByName('pl_link', row);
234                 link.setAttribute('href', oilsBasePath + '/acq/picklist/view/' + li.picklist());
235                 link.innerHTML = 'PL: '+pl.name(); // TODO i18n
236             }
237         }
238
239         var countNode = nodeByName('count', row);
240         var count = li.item_count() || 0;
241         if (typeof(this._copy_count_cb) == "function") {
242             this._copy_count_cb(li.id(), count);
243         }
244         countNode.innerHTML = count;
245         countNode.id = 'acq-lit-copy-count-label-' + li.id();
246
247         // lineitem state
248         nodeByName('li_state', row).innerHTML = li.state(); // TODO i18n state labels
249         // lineitem price
250         var priceInput = dojo.query('[name=price]', row)[0];
251         var priceData = liWrapper.getPrice();
252         priceInput.value = (priceData) ? priceData.price : '';
253         priceInput.onchange = function() { self.updateLiPrice(priceInput, li) };
254
255         // show either "mark received" or "unreceive" as appropriate
256         this.updateLiReceivedness(li, row);
257
258         if (!skip_final_placement) {
259             self.tbody.appendChild(row);
260             self.selectors.push(dojo.query('[name=selectbox]', row)[0]);
261         } else {
262             return row;
263         }
264     };
265
266     this.updateLiReceivedness = function(li, row) {
267         if (typeof(row) == "undefined")
268             row = dojo.query('tr[li="' + li.id() + '"]', "acq-lit-tbody")[0];
269
270         var recv_link = nodeByName("receive_link", row);
271         var unrecv_link = nodeByName("unreceive_link", row);
272         var real_copies_link = nodeByName("real_copies_link", row);
273
274         /* handle row coloring for based on LI state */
275         openils.Util.removeCSSClass(row, /^oils-acq-li-state-/);
276         openils.Util.addCSSClass(row, "oils-acq-li-state-" + li.state());
277
278         /* handle links that appear/disappear based on whether LI is received */
279         if (this.isPO) {
280             var self = this;
281             switch(li.state()) {
282                 case "on-order":
283                     openils.Util.hide(real_copies_link);
284                     openils.Util.hide(unrecv_link);
285                     openils.Util.show(recv_link, "inline");
286                     recv_link.onclick = function() {
287                         if (self.checkLiAlerts(li.id()))
288                             self.issueReceive(li);
289                     };
290                     return;
291                 case "received":
292                     openils.Util.hide(recv_link);
293                     openils.Util.show(unrecv_link, "inline");
294                     unrecv_link.onclick = function() {
295                         if (confirm(localeStrings.UNRECEIVE_LI))
296                             self.issueReceive(li, /* rollback */ true);
297                     };
298                     // TODO we should allow editing before receipt, in which case the
299                     // test should be "if 1 or more real (acp) copies exist
300                     openils.Util.show(real_copies_link);
301                     real_copies_link.onclick = function() {
302                         self.showRealCopies(li);
303                     }
304                     return;
305             }
306         }
307
308         openils.Util.hide(recv_link);
309         openils.Util.hide(unrecv_link);
310         openils.Util.hide(real_copies_link);
311     };
312
313
314     /**
315      * Draws and shows the lineitem notes pane
316      */
317     this.drawLiNotes = function(li) {
318         var self = this;
319
320         li.lineitem_notes(
321             li.lineitem_notes().sort(
322                 function(a, b) { 
323                     if(a.edit_time() < b.edit_time()) return 1;
324                     return -1;
325                 }
326             )
327         );
328
329         while(this.liNotesTbody.childNodes[0])
330             this.liNotesTbody.removeChild(this.liNotesTbody.childNodes[0]);
331         this.show('notes');
332
333         acqLitCreateLiNoteSubmit.onClick = function() {
334             var value = acqLitCreateNoteText.attr('value');
335             if(!value) return;
336             var note = new fieldmapper.acqlin();
337             note.isnew(true);
338             note.value(value);
339             note.lineitem(li.id());
340             self.updateLiNotes(li, note);
341         }
342
343         dojo.byId('acq-lit-notes-save-button').onclick = function() {
344             self.updateLiNotes(li);
345         }
346
347         dojo.forEach(li.lineitem_notes(), function(note) { self.addLiNote(li, note) });
348     }
349
350     /**
351      * Draws a single lineitem note in the notes pane
352      */
353     this.addLiNote = function(li, note) {
354         if(note.isdeleted()) return;
355         var self = this;
356         var row = self.liNotesRow.cloneNode(true);
357         dojo.query('[name=value]', row)[0].innerHTML = note.value();
358
359         dojo.query('[name=delete]', row)[0].onclick = function() {
360             note.isdeleted(true);
361             self.liNotesTbody.removeChild(row);
362         };
363
364         if(note.edit_time()) {
365             dojo.query('[name=edit_time]', row)[0].innerHTML = 
366                 dojo.date.locale.format(
367                     dojo.date.stamp.fromISOString(note.edit_time()), 
368                     {formatLength:'short'});
369         }
370
371         self.liNotesTbody.appendChild(row);
372     }
373
374     /**
375      * Updates any new/changed/deleted notes on the server
376      */
377     this.updateLiNotes = function(li, newNote) {
378
379         var notes;
380         if(newNote) {
381             notes = [newNote];
382         } else {
383             notes = li.lineitem_notes().filter(
384                 function(note) {
385                     if(note.ischanged() || note.isnew() || note.isdeleted())
386                         return note;
387                 }
388             );
389         }
390
391         if(notes.length == 0) return;
392         progressDialog.show();
393
394         fieldmapper.standardRequest(
395             ['open-ils.acq', 'open-ils.acq.lineitem_note.cud.batch'],
396             {   async : true,
397                 params : [this.authtoken, notes],
398                 onresponse : function(r) {
399                     var resp = openils.Util.readResponse(r);
400
401                     if(resp.complete) {
402
403                         if(!newNote) {
404                             // remove the old changed notes
405                             var list = [];
406                             dojo.forEach(li.lineitem_notes(), 
407                                 function(note) {
408                                     if(!(note.ischanged() || note.isnew() || note.isdeleted()))
409                                         list.push(note);
410                                 }
411                             );
412                             li.lineitem_notes(list);
413                         }
414
415                         progressDialog.hide();
416                         self.drawLiNotes(li);
417                         return;
418                     }
419
420                     progressDialog.update(resp);
421                     var newnote = resp.note;
422
423                     if(!newnote.isdeleted()) {
424                         newnote.isnew(false);
425                         newnote.ischanged(false);
426                         li.lineitem_notes().push(newnote);
427                     }
428                 },
429             }
430         );
431     }
432
433     this.updateLiPrice = function(input, li) {
434
435         var price = input.value;
436         var liWrapper = new openils.acq.Lineitem({lineitem:li});
437         var oldPrice = liWrapper.getPrice() || null;
438
439         if(oldPrice) oldPrice = oldPrice.price;
440         if(price == oldPrice) return;
441
442         fieldmapper.standardRequest(
443             ['open-ils.acq', 'open-ils.acq.lineitem.price.set'],
444             {   async : true,
445                 params : [this.authtoken, li.id(), price],
446                 oncomplete : function(r) {
447                     openils.Util.readResponse(r);
448                 }
449             }
450         );
451     }
452
453     this.removeLineitem = function(liId) {
454         this.tbody.removeChild(dojo.query('[li='+liId+']', this.tbody)[0]);
455         delete this.liCache[liId];
456         //selected.push(self.liCache[i.parentNode.parentNode.getAttribute('li')]);
457     }
458
459     this.drawInfo = function(liId) {
460         this.show('info');
461         openils.acq.Lineitem.fetchAttrDefs(
462             function() { 
463                 self._fetchLineitem(liId, function(li){self._drawInfo(li);}); 
464             } 
465         );
466     };
467
468     this._fetchLineitem = function(liId, handler) {
469
470         var li = this.liCache[liId];
471         if(li && li.marc() && li.lineitem_details())
472             return handler(li);
473         
474         fieldmapper.standardRequest(
475             ['open-ils.acq', 'open-ils.acq.lineitem.retrieve'],
476             {   async: true,
477
478                 params: [self.authtoken, liId, {
479                     flesh_attrs: true,
480                     flesh_li_details: true,
481                     flesh_fund_debit: true }],
482
483                 oncomplete: function(r) {
484                     var li = openils.Util.readResponse(r);
485                     handler(li)
486                 }
487             }
488         );
489     };
490
491     this._drawInfo = function(li) {
492
493         acqLitEditOrderMarc.onClick = function() { self.editOrderMarc(li); }
494
495         if(li.eg_bib_id()) {
496             openils.Util.hide('acq-lit-marc-order-record-label');
497             openils.Util.hide(acqLitEditOrderMarc.domNode);
498             openils.Util.show('acq-lit-marc-real-record-label');
499         } else {
500             openils.Util.show('acq-lit-marc-order-record-label');
501             openils.Util.show(acqLitEditOrderMarc.domNode);
502             openils.Util.hide('acq-lit-marc-real-record-label');
503         }
504
505         this.drawMarcHTML(li);
506         this.infoTbody = dojo.byId('acq-lit-info-tbody');
507
508         if(!this.infoRow)
509             this.infoRow = this.infoTbody.removeChild(dojo.byId('acq-lit-info-row'));
510         while(this.infoTbody.childNodes[0])
511             this.infoTbody.removeChild(this.infoTbody.childNodes[0]);
512
513         for(var i = 0; i < li.attributes().length; i++) {
514             var attr = li.attributes()[i];
515             var row = this.infoRow.cloneNode(true);
516
517             var type = attr.attr_type().replace(/lineitem_(.*)_attr_definition/, '$1');
518             var name = openils.acq.Lineitem.attrDefs[type].filter(
519                 function(a) {
520                     return (a.code() == attr.attr_name());
521                 }
522             ).pop().description();
523
524             dojo.query('[name=label]', row)[0].appendChild(document.createTextNode(name));
525             dojo.query('[name=value]', row)[0].appendChild(document.createTextNode(attr.attr_value()));
526             this.infoTbody.appendChild(row);
527         }
528
529         if(li.eg_bib_id()) {
530             openils.Util.show('acq-lit-info-cat-link');
531             var link = dojo.byId('acq-lit-info-cat-link').getElementsByTagName('a')[0];
532
533             if(openils.XUL.isXUL()) {
534
535                 var makeRecTab = function() {
536                                     xulG.new_tab(
537                         XUL_OPAC_WRAPPER,
538                                             {tab_name: localeStrings.XUL_RECORD_DETAIL_PAGE, browser:false},
539                                             {
540                             no_xulG : false, 
541                             show_nav_buttons : true, 
542                             show_print_button : true, 
543                             opac_url : xulG.url_prefix(xulG.urls.opac_rdetail + '?r=' + li.eg_bib_id())
544                         }
545                     );
546                 }
547                 link.setAttribute('href', 'javascript:void(0);');
548                 link.onclick = makeRecTab;
549
550             } else {
551                 var href = link.getAttribute('href');
552                 if(href.match(/=$/))
553                     link.setAttribute('href',  href + li.eg_bib_id());
554             }
555         } else {
556             openils.Util.hide('acq-lit-info-cat-link');
557         }
558     };
559
560     this.drawMarcHTML = function(li) {
561         var params = [null, true, li.marc()];
562         if(li.eg_bib_id()) 
563             params = [li.eg_bib_id(), true];
564
565         fieldmapper.standardRequest(
566             ['open-ils.search', 'open-ils.search.biblio.record.html'],
567             {   async: true,
568                 params: params,
569                 oncomplete: function(r) {
570                     dojo.byId('acq-lit-marc-div').innerHTML = 
571                         openils.Util.readResponse(r);
572                 }
573             }
574         );
575     }
576
577     this.drawCopies = function(liId) {
578         this.show('copies');
579         var self = this;
580         this.copyCache = {};
581         this.copyWidgetCache = {};
582         this.oldCopyWidgetCache = {};
583         this.dfaCache = {};
584         this.dfeOffset = 0;
585
586         acqLitSaveCopies.onClick = function() { self.saveCopyChanges(liId) };
587         acqLitBatchUpdateCopies.onClick = function() { self.batchCopyUpdate() };
588         acqLitCopyCountInput.attr('value', '0');
589
590         while(this.copyTbody.childNodes[0])
591             this.copyTbody.removeChild(this.copyTbody.childNodes[0]);
592
593         this._drawBatchCopyWidgets();
594
595         this._fetchDistribFormulas(
596             function() {
597                 openils.acq.Lineitem.fetchAttrDefs(
598                     function() { 
599                         self._fetchLineitem(liId, function(li){self._drawCopies(li);}); 
600                     } 
601                 );
602             }
603         );
604     };
605
606     /**
607      * Insert a new row into the distribution formula selection form
608      */
609     this._addDistribFormulaRow = function() {
610         var self = this;
611
612         if (!self.distribForms) {
613             // no formulas, hide the form
614             openils.Util.hide('acq-lit-distrib-formula-tbody');
615             return;
616         }
617
618         if(!this.distribFormulaTemplate) 
619             this.distribFormulaTemplate = 
620                 dojo.byId('acq-lit-distrib-formula-tbody').removeChild(dojo.byId('acq-lit-distrib-form-row'));
621
622         var row = this.distribFormulaTemplate.cloneNode(true);
623         dojo.place(row, "acq-lit-distrib-formula-tbody", "only");
624
625         this.dfSelector = new dijit.form.FilteringSelect(
626             {"labelAttr": "dynLabel", "labelType": "html"},
627             nodeByName("selector", row)
628         );
629         this._updateFormulaStore();
630         this.dfSelector.fetchProperties =
631             {"sort": [{"attribute": "use_count", "descending": true}]};
632
633         var apply = new dijit.form.Button(
634             {"label": localeStrings.APPLY},
635             nodeByName('set_button', row)
636         ); 
637
638         var reset = new dijit.form.Button(
639             {"label": localeStrings.RESET_FORMULAE, "disabled": true},
640             nodeByName("reset_button", row)  
641         );
642
643         dojo.connect(apply, 'onClick', 
644             function() {
645                 var form_id = self.dfSelector.attr("value");
646                 if(!form_id) return;
647                 self._applyDistribFormula(form_id);
648                 reset.attr("disabled", false);
649             }
650         );
651
652         dojo.connect(reset, 'onClick', 
653             function() {
654                 self.restoreCopyFieldsBeforeDF();
655                 self.dfaCache = {};
656                 self.dfeOffset = 0;
657                 self._updateFormulaStore();
658                 reset.attr("disabled", "true");
659             }
660         );
661
662     };
663
664     /**
665      * Applies a distrib formula to the current set of copies
666      */
667     this._applyDistribFormula = function(formula) {
668         if(!formula) return;
669
670         formula = this.distribForms.filter(
671             function(form) { return form.id() == formula; }
672         )[0];
673
674         var copyRows = dojo.query('tr', self.copyTbody);
675
676         if (this.dfeOffset >= copyRows.length) {
677             alert(localeStrings.OUT_OF_COPIES);
678             return;
679         }
680
681         var entries_applied = 0;
682         for(
683             var rowIndex = this.dfeOffset;
684             rowIndex < copyRows.length;
685             rowIndex++
686         ) {
687             
688             var row = copyRows[rowIndex];
689             var copy_id = row.getAttribute('copy_id');
690             var copyWidgets = this.copyWidgetCache[copy_id];
691             var entryIndex = this.dfeOffset;
692             var entry = null;
693
694             // find the correct entry for the current row
695             dojo.forEach(formula.entries(), 
696                 function(e) {
697                     if(!entry) {
698                         entryIndex += e.item_count();
699                         if(entryIndex > rowIndex)
700                             entry = e;
701                     }
702                 }
703             );
704
705             if(entry) {
706                 
707                 //console.log("rowIndex = " + rowIndex + ", entry = " + entry.id() + ", entryIndex=" + 
708                 //  entryIndex + ", owning_lib = " + entry.owning_lib() + ", location = " + entry.location());
709     
710                 entries_applied++;
711                 this.saveCopyFieldsBeforeDF(copy_id);
712                 this._copy_fields_for_acqdf.forEach(
713                     function(field) {
714                         if(entry[field]()) {
715                             copyWidgets[field].attr('value', (entry[field]()));
716                         }
717                     }
718                 );
719             }
720         }
721
722         if (entries_applied) {
723             this.dfaCache[formula.id()] = ++(this.dfaCache[formula.id()]) || 1;
724             this._updateFormulaStore();
725             this.dfeOffset += entries_applied;
726         };
727     };
728
729     /**
730      * This function updates the DF store for the dropdown so that use_counts
731      * can reflect DF applications from this session before they're saved
732      * server-side.
733      */
734     this._updateFormulaStore = function() {
735         this.dfSelector.store = new dojo.data.ItemFileReadStore(
736             {
737                 "data": self._labelFormulasWithCounts(
738                     acqdf.toStoreData(self.distribForms)
739                 )
740             }
741         );
742     };
743
744     this.saveCopyFieldsBeforeDF = function(copy_id) {
745         var self = this;
746         if (!this.oldCopyWidgetCache[copy_id]) {
747             var copyWidgets = this.copyWidgetCache[copy_id];
748
749             this.oldCopyWidgetCache[copy_id] = {};
750             this._copy_fields_for_acqdf.forEach(
751                 function(f) {
752                     self.oldCopyWidgetCache[copy_id][f] =
753                         copyWidgets[f].attr("value");
754                 }
755             );
756         }
757     };
758
759     this.restoreCopyFieldsBeforeDF = function() {
760         var self = this;
761         for (var copy_id in this.oldCopyWidgetCache) {
762             this._copy_fields_for_acqdf.forEach(
763                 function(f) {
764                     self.copyWidgetCache[copy_id][f].attr(
765                         "value", self.oldCopyWidgetCache[copy_id][f]
766                     );
767                 }
768             );
769         }
770     };
771
772     this._labelFormulasWithCounts = function(store_data) {
773         for (var key in store_data.items) {
774             var obj = store_data.items[key];
775
776             if (this.dfaCache[obj.id])
777                 obj.use_count = Number(obj.use_count) + this.dfaCache[obj.id];
778
779             obj.dynLabel = "<span class='acq-lit-distrib-form-use-count'>[" +
780                 obj.use_count + "]</span>&nbsp; " + obj.name;
781         }
782         return store_data;
783     };
784
785     /**
786      * This method formerly would not refetch the DF formulas if they'd been
787      * loaded already, but now it always re-fetches, since use_count changes.
788      */
789     this._fetchDistribFormulas = function(onload) {
790         fieldmapper.standardRequest(
791             ["open-ils.acq",
792                 "open-ils.acq.distribution_formula.ranged.retrieve.atomic"],
793             {
794                 "async": true,
795                 "params": [openils.User.authtoken],
796                 "oncomplete": function(r) {
797                     self.distribForms = openils.Util.readResponse(r);
798                     if(!self.distribForms || self.distribForms.length == 0) {
799                         self.distribForms = [];
800                     }
801                     self._addDistribFormulaRow();
802                     onload();
803                 }
804             }
805         );
806     }
807
808     this._drawBatchCopyWidgets = function() {
809         var row = this.copyBatchRow;
810         dojo.forEach(liDetailBatchFields, 
811             function(field) {
812                 if(self.copyBatchRowDrawn) {
813                     self.copyBatchWidgets[field].attr('value', null);
814                 } else {
815                     var widget = new openils.widget.AutoFieldWidget({
816                         fmField : field,
817                         fmClass : 'acqlid',
818                         parentNode : dojo.query('[name='+field+']', row)[0],
819                         orgLimitPerms : ['CREATE_PICKLIST'],
820                         dijitArgs : {required:false},
821                         forceSync : true
822                     });
823                     widget.build(
824                         function(w, ww) {
825                             self.copyBatchWidgets[field] = w;
826                         }
827                     );
828                 }
829             }
830         );
831         this.copyBatchRowDrawn = true;
832     };
833
834     this.batchCopyUpdate = function() {
835         var self = this;
836         for(var k in this.copyWidgetCache) {
837             var cache = this.copyWidgetCache[k];
838             dojo.forEach(liDetailBatchFields, function(f) {
839                 var newval = self.copyBatchWidgets[f].attr('value');
840                 if(newval) cache[f].attr('value', newval);
841             });
842         }
843     };
844
845     this._drawCopies = function(li) {
846         var self = this;
847
848         // this button sets the total number of copies for a given lineitem
849         acqLitAddCopyCount.onClick = function() { 
850             var count = acqLitCopyCountInput.attr('value');
851
852             // add new rows
853             while(self.copyCount() < count)
854                 self.addCopy(li); 
855             
856             // delete rows if necessary
857             var diff = self.copyCount() - count;
858             if(diff > 0) {
859                 var rows = dojo.query('tr', self.copyTbody).reverse().slice(0, diff);
860                 if(confirm(dojo.string.substitute(localeStrings.DELETE_LI_COPIES_CONFIRM, [diff]))) {
861                     dojo.forEach(rows, function(row) {self.deleteCopy(row); });
862                 } else {
863                     acqLitCopyCountInput.attr('value', self.copyCount()+'');
864                 }
865             }
866         }
867
868
869         if(li.lineitem_details().length > 0) {
870             dojo.forEach(li.lineitem_details(),
871                 function(copy) {
872                     self.addCopy(li, copy);
873                 }
874             );
875         } else {
876             self.addCopy(li);
877         }
878     };
879
880     this.copyCount = function() {
881         var count = 0;
882         for(var id in this.copyCache) {
883             if(!this.copyCache[id].isdeleted())
884                 count++;
885         }
886         return count;
887     }
888
889     this.virtCopyId = -1;
890     this.addCopy = function(li, copy) {
891         var row = this.copyRow.cloneNode(true);
892         this.copyTbody.appendChild(row);
893         var self = this;
894
895         if(!copy) {
896             copy = new fieldmapper.acqlid();
897             copy.isnew(true);
898             copy.id(this.virtCopyId--);
899             copy.lineitem(li.id());
900         }
901
902         this.copyCache[copy.id()] = copy;
903         row.setAttribute('copy_id', copy.id());
904         self.copyWidgetCache[copy.id()] = {};
905
906         acqLitCopyCountInput.attr('value', self.copyCount()+'');
907
908         dojo.forEach(liDetailFields,
909             function(field) {
910                 var widget = new openils.widget.AutoFieldWidget({
911                     fmObject : copy,
912                     fmField : field,
913                     fmClass : 'acqlid',
914                     parentNode : dojo.query('[name='+field+']', row)[0],
915                     orgLimitPerms : ['CREATE_PICKLIST', 'CREATE_PURCHASE_ORDER'],
916                     readOnly : Boolean(copy.eg_copy_id())
917                 });
918                 widget.build(
919                     // make sure we capture the value from any async widgets
920                     function(w, ww) { 
921                         copy[field](ww.getFormattedValue()) 
922                         self.copyWidgetCache[copy.id()][field] = w;
923                     }
924                 );
925                 dojo.connect(widget.widget, 'onChange', 
926                     function(val) { 
927                         if(copy.isnew() || val != copy[field]()) {
928                             // prevent setting ischanged() automatically on widget load for existing copies
929                             copy[field](widget.getFormattedValue()) 
930                             copy.ischanged(true);
931                         }
932                     }
933                 );
934             }
935         );
936
937         this.updateLidReceivedness(copy, row);
938     };
939
940     this.updateLidReceivedness = function(copy, row) {
941         if (typeof(row) == "undefined") {
942             row = dojo.query(
943                 'tr[copy_id="' + copy.id() + '"]', this.copyTbody
944             )[0];
945         }
946
947         var self = this;
948         var recv_link = nodeByName("receive", row);
949         var unrecv_link = nodeByName("unreceive", row);
950         var del_link = nodeByName("delete", row);
951
952         if (this.isPO) {
953             openils.Util.hide(del_link.parentNode);
954
955             /* Avoid showing (un)receive links for virtual copies */
956             if (copy.id() > 0) {
957                 if(copy.recv_time()) {
958                     openils.Util.hide(recv_link);
959                     openils.Util.show(unrecv_link);
960                     unrecv_link.onclick = function() {
961                         if (confirm(localeStrings.UNRECEIVE_LID))
962                             self.issueReceive(copy, /* rollback */ true);
963                     };
964                 } else {
965                     openils.Util.hide(unrecv_link);
966                     openils.Util.show(recv_link);
967                     recv_link.onclick = function() {
968                         if (self.checkLiAlerts(copy.lineitem()))
969                             self.issueReceive(copy);
970                     };
971                 }
972             } else {
973                 openils.Util.hide(unrecv_link);
974                 openils.Util.hide(recv_link);
975             }
976         } else {
977             openils.Util.hide(unrecv_link);
978             openils.Util.hide(recv_link);
979
980             del_link.onclick = function() { self.deleteCopy(row) };
981             openils.Util.show(del_link.parentNode);
982         }
983     }
984
985     this._confirmAlert = function(li, lin) {
986         return confirm(
987             dojo.string.substitute(
988                 localeStrings.CONFIRM_LI_ALERT, [
989                     (new openils.acq.Lineitem({"lineitem": li})).findAttr(
990                         "title", "lineitem_marc_attr_definition"
991                     ),
992                     lin.alert_text().description(), lin.value()
993                 ]
994             )
995         );
996     };
997
998     this.checkLiAlerts = function(li_id) {
999         var li = this.liCache[li_id];
1000
1001         var alert_notes = li.lineitem_notes().filter(
1002             function(o) { return Boolean(o.alert_text()); }
1003         );
1004
1005         /* this is _intentionally_ not done in a call to forEach() ... */
1006         for (var i = 0; i < alert_notes.length; i++) {
1007             if (this.noteAcks[alert_notes[i].id()])
1008                 continue;
1009             else if (!this._confirmAlert(li, alert_notes[i]))
1010                 return false;
1011             else
1012                 this.noteAcks[alert_notes[i].id()] = true;
1013         }
1014
1015         return true;
1016     };
1017
1018     this.deleteCopy = function(row) {
1019         var copy = this.copyCache[row.getAttribute('copy_id')];
1020         copy.isdeleted(true);
1021         if(copy.isnew())
1022             delete this.copyCache[copy.id()];
1023         this.copyTbody.removeChild(row);
1024     }
1025
1026     this._dfaCacheAsList = function() {
1027         var L = [];
1028         for (var key in this.dfaCache) {
1029             for (var i = 0; i < this.dfaCache[key]; i++)
1030                 L.push(key);
1031         }
1032         return L;
1033     }
1034
1035     this.saveCopyChanges = function(liId) {
1036         var self = this;
1037         var copies = [];
1038
1039
1040         var total = 0;
1041         for(var id in this.copyCache) {
1042             var c = this.copyCache[id];
1043             if(!c.isdeleted()) total++;
1044             if(c.isnew() || c.ischanged() || c.isdeleted()) {
1045                 if(c.id() < 0) c.id(null);
1046                 copies.push(c);
1047             }
1048         }
1049
1050         if (typeof(this._copy_count_cb) == "function") {
1051             this._copy_count_cb(liId, total);
1052         }
1053
1054         dojo.byId('acq-lit-copy-count-label-' + liId).innerHTML = total;
1055
1056
1057         if (copies.length > 0) {
1058             openils.Util.show("acq-lit-update-copies-progress");
1059             fieldmapper.standardRequest(
1060                 ['open-ils.acq', 'open-ils.acq.lineitem_detail.cud.batch'],
1061                 {   async: true,
1062                     params: [openils.User.authtoken, copies],
1063                     onresponse: function(r) {
1064                         var res = openils.Util.readResponse(r);
1065                         litUpdateCopiesProgress.update(res);
1066                     },
1067                     oncomplete: function() {
1068                         self.drawCopies(liId);
1069                         openils.Util.hide("acq-lit-update-copies-progress");
1070                     }
1071                 }
1072             );
1073         }
1074
1075         var dfa_list = this._dfaCacheAsList();
1076         if (dfa_list.length > 0) {
1077             fieldmapper.standardRequest(
1078                 ["open-ils.acq",
1079                 "open-ils.acq.distribution_formula.record_application"],
1080                 {
1081                     "async": true,
1082                     "params": [openils.User.authtoken, dfa_list, liId],
1083                     "onresponse": function(r) {
1084                         var res = openils.Util.readResponse(r);
1085                         if (res && res.length < dfa_list.length)
1086                             alert(localeStrings.DFA_NOT_ALL);
1087                     }
1088                 }
1089             );
1090             this.dfaCache = {};
1091         }
1092     }
1093
1094     this.applySelectedLiAction = function(action) {
1095         var self = this;
1096         switch(action) {
1097
1098             case 'delete_selected':
1099                 this._deleteLiList(self.getSelected());
1100                 break;
1101
1102             case 'create_order':
1103
1104                 if(!this.createPoProviderSelector) {
1105                     var widget = new openils.widget.AutoFieldWidget({
1106                         fmField : 'provider',
1107                         fmClass : 'acqpo',
1108                         searchFilter: {"active": "t"},
1109                         parentNode : dojo.byId('acq-lit-po-provider'),
1110                     });
1111                     widget.build(
1112                         function(w) { self.createPoProviderSelector = w; }
1113                     );
1114                 }
1115
1116                 if(!this.createPoAgencySelector) {
1117                     var widget = new openils.widget.AutoFieldWidget({
1118                         fmField : 'ordering_agency',
1119                         fmClass : 'acqpo',
1120                         parentNode : dojo.byId('acq-lit-po-agency'),
1121                         orgLimitPerms : ['CREATE_PURCHASE_ORDER'],
1122                     });
1123                     widget.build(
1124                         function(w) { self.createPoAgencySelector = w; }
1125                     );
1126                 }
1127
1128          
1129                 acqLitPoCreateDialog.show();
1130                 break;
1131
1132             case 'save_picklist':
1133                 this._loadPLSelect();
1134                 acqLitSavePlDialog.show();
1135                 break;
1136
1137             case 'selector_ready':
1138             case 'order_ready':
1139                 acqLitChangeLiStateDialog.attr('state', action.replace('_', '-'));
1140                 acqLitChangeLiStateDialog.show();
1141                 break;
1142
1143             case 'print_po':
1144                 this.printPO();
1145                 break;
1146
1147             case 'receive_po':
1148                 this.receivePO();
1149                 break;
1150
1151             case 'rollback_receive_po':
1152                 this.rollbackPoReceive();
1153                 break;
1154
1155             case 'create_assets':
1156                 this.createAssets();
1157                 break;
1158
1159             case 'export_attr_list':
1160                 this.chooseExportAttr();
1161                 break;
1162
1163             case 'add_brief_record':
1164                 if(this.isPO)
1165                     location.href = oilsBasePath + '/acq/picklist/brief_record?po=' + this.isPO;
1166                 else
1167                     location.href = oilsBasePath + '/acq/picklist/brief_record?pl=' + this.isPL;
1168         }
1169     }
1170
1171     this.createAssets = function() {
1172         if(!this.isPO) return;
1173         if(!confirm(localeStrings.CREATE_PO_ASSETS_CONFIRM)) return;
1174         this.show('acq-lit-progress-numbers');
1175         var self = this;
1176         fieldmapper.standardRequest(
1177             ['open-ils.acq', 'open-ils.acq.purchase_order.assets.create'],
1178             {   async: true,
1179                 params: [this.authtoken, this.isPO],
1180                 onresponse: function(r) {
1181                     var resp = openils.Util.readResponse(r);
1182                     self._updateProgressNumbers(resp, true);
1183                 }
1184             }
1185         );
1186     }
1187
1188     this.chooseExportAttr = function() {
1189         if (!acqLitExportAttrSelector._li_setup) {
1190             var self = this;
1191             acqLitExportAttrSelector.store = new dojo.data.ItemFileReadStore(
1192                 {
1193                     "data": acqliad.toStoreData(
1194                         (new openils.PermaCrud()).search(
1195                             "acqliad", {"code": li_exportable_attrs}
1196                         )
1197                     )
1198                 }
1199             );
1200             acqLitExportAttrSelector.setValue();
1201             acqLitExportAttrButton.onClick = function(){self.exportAttrList();};
1202             acqLitExportAttrSelector._li_setup = true;
1203         }
1204         openils.Util.show("acq-lit-export-attr-holder", "inline");
1205     };
1206
1207     this.exportAttrList = function() {
1208         var attr_def = acqLitExportAttrSelector.item;
1209         var li_list = this.getSelected();
1210         var value_list = li_list.map(
1211             function(li) {
1212                 return (new openils.acq.Lineitem({"lineitem": li})).findAttr(
1213                     attr_def.code, "lineitem_marc_attr_definition"
1214                 );
1215             }
1216         ).filter(function(attr) { return Boolean(attr); });
1217
1218         if (value_list.length > 0) {
1219             if (value_list.length < li_list.length) {
1220                 if (!confirm(
1221                     dojo.string.substitute(
1222                         localeStrings.EXPORT_SHORT_LIST, [attr_def.description]
1223                     )
1224                 )) {
1225                     return;
1226                 }
1227             }
1228             try {
1229                 openils.XUL.contentToFileSaveDialog(
1230                     value_list.join("\n"),
1231                     localeStrings.EXPORT_SAVE_DIALOG_TITLE
1232                 );
1233             } catch (E) {
1234                 alert(E);
1235             }
1236         } else {
1237             alert(dojo.string.substitute(
1238                 localeStrings.EXPORT_EMPTY_LIST, [attr_def.description]
1239             ));
1240         }
1241
1242         openils.Util.hide("acq-lit-export-attr-holder");
1243     };
1244
1245     this.printPO = function() {
1246         if(!this.isPO) return;
1247         progressDialog.show(true);
1248         fieldmapper.standardRequest(
1249             ['open-ils.acq', 'open-ils.acq.purchase_order.format'],
1250             {   async: true,
1251                 params: [this.authtoken, this.isPO, 'html'],
1252                 oncomplete: function(r) {
1253                     progressDialog.hide();
1254                     var evt = openils.Util.readResponse(r);
1255                     if(evt && evt.template_output()) {
1256                         win = window.open('','', 'resizable,width=800,height=600,scrollbars=1');
1257                         win.document.body.innerHTML = evt.template_output().data();
1258                     }
1259                 }
1260             }
1261         );
1262     }
1263
1264
1265     this.receivePO = function() {
1266         if (!this.isPO) return;
1267
1268         for (var id in this.liCache) {
1269             /* assumption: liCache reflects exactly the
1270              * set of LIs that belong to our PO */
1271             if (this.liCache[id].state() != "received" &&
1272                 !this.checkLiAlerts(id)) return;
1273         }
1274
1275         this.show('acq-lit-progress-numbers');
1276         var self = this;
1277         fieldmapper.standardRequest(
1278             ['open-ils.acq', 'open-ils.acq.purchase_order.receive'],
1279             {   async: true,
1280                 params: [this.authtoken, this.isPO],
1281                 onresponse : function(r) {
1282                     var resp = openils.Util.readResponse(r);
1283                     self._updateProgressNumbers(resp, true);
1284                 },
1285             }
1286         );
1287     }
1288
1289     this.issueReceive = function(obj, rollback) {
1290         /* (For now) there shall be no marking LI or LIDs (un)received
1291          * except from the actual "view PO" interface. */
1292         if (!this.isPO) return;
1293
1294         var part =
1295             {"jub": "lineitem", "acqlid": "lineitem_detail"}[obj.classname];
1296         var method =
1297             "open-ils.acq." + part + ".receive" + (rollback ? ".rollback" : "");
1298
1299         progressDialog.show(true);
1300         fieldmapper.standardRequest(
1301             ["open-ils.acq", method], {
1302                 "async": true,
1303                 "params": [this.authtoken, obj.id()],
1304                 "onresponse": function(r) {
1305                     self.handleReceive(openils.Util.readResponse(r));
1306                 },
1307                 "oncomplete": function() { progressDialog.hide(); }
1308             }
1309         );
1310     };
1311
1312     /**
1313      * Handles the responses from receive and rollback ML calls.
1314      */
1315     this.handleReceive = function(resp) {
1316         if (resp) {
1317             if (resp.li) {
1318                 for (var li_id in resp.li) {
1319                     for (var key in resp.li[li_id])
1320                         self.liCache[li_id][key](resp.li[li_id][key]);
1321                     self.updateLiReceivedness(self.liCache[li_id]);
1322                 }
1323             }
1324             if (resp.po) {
1325                 if (typeof(self.poUpdateCallback) == "function")
1326                     self.poUpdateCallback(resp.po);
1327             }
1328             if (resp.lid) {
1329                 for (var lid_id in resp.lid) {
1330                     for (var key in resp.lid[lid_id])
1331                         self.copyCache[lid_id][key](resp.lid[lid_id][key]);
1332                     self.updateLidReceivedness(self.copyCache[lid_id]);
1333                 }
1334             }
1335         }
1336     };
1337
1338     this.rollbackPoReceive = function() {
1339         if(!this.isPO) return;
1340         if(!confirm(localeStrings.ROLLBACK_PO_RECEIVE_CONFIRM)) return;
1341         this.show('acq-lit-progress-numbers');
1342         var self = this;
1343         fieldmapper.standardRequest(
1344             ['open-ils.acq', 'open-ils.acq.purchase_order.receive.rollback'],
1345             {   async: true,
1346                 params: [this.authtoken, this.isPO],
1347                 onresponse : function(r) {
1348                     var resp = openils.Util.readResponse(r);
1349                     self._updateProgressNumbers(resp, true);
1350                 },
1351             }
1352         );
1353     }
1354
1355     this._updateProgressNumbers = function(resp, reloadOnComplete) {
1356         if(!resp) return;
1357         dojo.byId('acq-pl-lit-li-processed').innerHTML = resp.li;
1358         dojo.byId('acq-pl-lit-lid-processed').innerHTML = resp.lid;
1359         dojo.byId('acq-pl-lit-debits-processed').innerHTML = resp.debits_accrued;
1360         dojo.byId('acq-pl-lit-bibs-processed').innerHTML = resp.bibs;
1361         dojo.byId('acq-pl-lit-indexed-processed').innerHTML = resp.indexed;
1362         dojo.byId('acq-pl-lit-copies-processed').innerHTML = resp.copies;
1363         if(resp.complete && reloadOnComplete) 
1364             location.href = location.href;
1365     }
1366
1367
1368     this._createPO = function(fields) {
1369         this.show('acq-lit-progress-numbers');
1370         var po = new fieldmapper.acqpo();
1371         po.provider(this.createPoProviderSelector.attr('value'));
1372         po.ordering_agency(this.createPoAgencySelector.attr('value'));
1373
1374         var selected = this.getSelected( (fields.create_from == 'all') );
1375         if(selected.length == 0) return;
1376
1377         var max = selected.length * 3;
1378
1379         var self = this;
1380         fieldmapper.standardRequest(
1381             ['open-ils.acq', 'open-ils.acq.purchase_order.create'],
1382             {   async: true,
1383                 params: [
1384                     openils.User.authtoken, 
1385                     po, 
1386                     {
1387                         lineitems : selected.map(function(li) { return li.id() }),
1388                         create_assets : fields.create_assets[0],
1389                     }
1390                 ],
1391
1392                 onresponse : function(r) {
1393                     var resp = openils.Util.readResponse(r);
1394                     self._updateProgressNumbers(resp);
1395                     if(resp.complete) 
1396                         location.href = oilsBasePath + '/eg/acq/po/view/' + resp.purchase_order.id();
1397                 }
1398             }
1399         );
1400     }
1401
1402     this._deleteLiList = function(list, idx) {
1403         if(idx == null) idx = 0;
1404         if(idx >= list.length) return;
1405         var liId = list[idx].id();
1406         fieldmapper.standardRequest(
1407             ['open-ils.acq', 'open-ils.acq.lineitem.delete'],
1408             {   async: true,
1409                 params: [openils.User.authtoken, liId],
1410                 oncomplete: function(r) {
1411                     self.removeLineitem(liId);
1412                     self._deleteLiList(list, ++idx);
1413                 }
1414             }
1415         );
1416     }
1417
1418     this.editOrderMarc = function(li) {
1419
1420         /*  To run in Firefox directly, must set signed.applets.codebase_principal_support
1421             to true in about:config */
1422
1423         if(!openils.XUL.enableXPConnect()) return;
1424
1425         if(openils.XUL.isXUL()) {
1426             win = window.open('/xul/' + openils.XUL.buildId() + '/server/cat/marcedit.xul');
1427         } else {
1428             win = window.open('/xul/server/cat/marcedit.xul'); 
1429         }
1430         var self = this;
1431         win.xulG = {
1432             record : {marc : li.marc()},
1433             save : {
1434                 label: 'Save Record', // XXX I18N
1435                 func: function(xmlString) {
1436                     li.marc(xmlString);
1437                     fieldmapper.standardRequest(
1438                         ['open-ils.acq', 'open-ils.acq.lineitem.update'],
1439                         {   async: true,
1440                             params: [openils.User.authtoken, li],
1441                             oncomplete: function(r) {
1442                                 openils.Util.readResponse(r);
1443                                 win.close();
1444                                 self.drawInfo(li.id())
1445                             }
1446                         }
1447                     );
1448                 },
1449             }
1450         };
1451     }
1452
1453     this._savePl = function(values) {
1454         var self = this;
1455         var selected = this.getSelected( (values.which == 'all') );
1456         openils.Util.show('acq-lit-generic-progress');
1457
1458         if(values.new_name) {
1459             openils.acq.Picklist.create(
1460                 {name: values.new_name}, 
1461                 function(id) {
1462                     self._updateLiList(id, selected, 0, 
1463                         function(){
1464                             location.href = oilsBasePath + '/eg/acq/picklist/view/' + id;
1465                         });
1466                 }
1467             );
1468         } else if(values.existing_pl) {
1469             // update lineitems to use an existing picklist
1470             self._updateLiList(values.existing_pl, selected, 0, 
1471                 function(){
1472                     location.href = oilsBasePath + '/eg/acq/picklist/view/' + values.existing_pl;
1473                 });
1474         }
1475     }
1476
1477     this._updateLiState = function(values, state) {
1478         var self = this;
1479         var selected = this.getSelected( (values.which == 'all') );
1480         if(!selected.length) return;
1481         dojo.forEach(selected, function(li) {li.state(state);});
1482         self._updateLiList(null, selected, 0, 
1483             // TODO consider inline updates for efficiency
1484             function() { location.href = location.href }
1485         );
1486     }
1487
1488     this._updateLiList = function(pl, list, idx, oncomplete) {
1489         if(idx >= list.length) return oncomplete();
1490         var li = list[idx];
1491         if(pl != null) li.picklist(pl);
1492         litGenericProgress.update({maximum: list.length, progress: idx});
1493         new openils.acq.Lineitem({lineitem:li}).update(
1494             function(r) {
1495                 self._updateLiList(pl, list, ++idx, oncomplete);
1496             }
1497         );
1498     }
1499
1500     this._loadPLSelect = function() {
1501         if(this._plSelectLoaded) return;
1502         var plList = [];
1503         function handleResponse(r) {
1504             plList.push(r.recv().content());
1505         }
1506         var method = 'open-ils.acq.picklist.user.retrieve';
1507         fieldmapper.standardRequest(
1508             ['open-ils.acq', method],
1509             {   async: true,
1510                 params: [this.authtoken],
1511                 onresponse: handleResponse,
1512                 oncomplete: function() {
1513                     self._plSelectLoaded = true;
1514                     acqLitAddExistingSelect.store = 
1515                         new dojo.data.ItemFileReadStore({data:acqpl.toStoreData(plList)});
1516                     acqLitAddExistingSelect.setValue();
1517                 }
1518             }
1519         );
1520     }
1521
1522     // grab the li-details for this lineitem, grab the linked copies and volumes, add them to the table
1523     this.showRealCopies = function(li) {
1524         while(this.realCopiesTbody.childNodes[0])
1525             this.realCopiesTbody.removeChild(this.realCopiesTbody.childNodes[0]);
1526         this.show('real-copies');
1527
1528         var pcrud = new openils.PermaCrud({authtoken : this.authtoken});
1529         this.realCopyList = [];
1530         this.volCache = {};
1531         var tabIndex = 1000;
1532         var self = this;
1533
1534         acqLitSaveRealCopies.onClick = function() {
1535             self.saveRealCopies();
1536         }
1537
1538         this._fetchLineitem(li.id(), 
1539             function(fullLi) {
1540                 li = self.liCache[li.id()] = fullLi;
1541
1542                 pcrud.search(
1543                     'acp', {
1544                         id : li.lineitem_details().map(
1545                             function(item) { return item.eg_copy_id() }
1546                         )
1547                     }, {
1548                         async : true,
1549                         streaming : true,
1550                         onresponse : function(r) {
1551                             var copy = openils.Util.readResponse(r);
1552                             var volId = copy.call_number();
1553                             var volume = self.volCache[volId];
1554                             if(!volume) {
1555                                 volume = self.volCache[volId] = pcrud.retrieve('acn', volId);
1556                             }
1557                             self.addRealCopy(volume, copy, tabIndex++);
1558                         }
1559                     }
1560                 );
1561             }
1562         );
1563     }
1564
1565     this.addRealCopy = function(volume, copy, tabIndex) {
1566         var row = this.realCopiesRow.cloneNode(true);
1567         this.realCopyList.push(copy);
1568
1569         var selectNode;
1570         dojo.forEach(
1571             ['owning_lib', 'location', 'circ_modifier', 'label', 'barcode'],
1572
1573             function(field) {
1574                 var isvol = (field == 'owning_lib' || field == 'label');
1575                 var widget = new openils.widget.AutoFieldWidget({
1576                     fmField : field,
1577                     fmObject : isvol ? volume : copy,
1578                     parentNode : nodeByName(field, row),
1579                     readOnly : (field != 'barcode'),
1580                 });
1581
1582                 var widgetDrawn = null;
1583
1584                 if(field == 'barcode') {
1585
1586                     widgetDrawn = function(w, ww) {
1587                         var node = w.domNode;
1588                         node.setAttribute('tabindex', ''+tabIndex);
1589
1590                         // on enter, select the next barcode input
1591                         dojo.connect(w, 'onKeyDown',
1592                             function(e) {
1593                                 if(e.keyCode == dojo.keys.ENTER) {
1594                                     var ti = node.getAttribute('tabindex');
1595                                     var nextNode = dojo.query('[tabindex=' + String(Number(ti) + 1) + ']', self.realCopiesTbody)[0];
1596                                     if(nextNode) nextNode.select();
1597                                 }
1598                             }
1599                         );
1600
1601                         dojo.connect(w, 'onChange', 
1602                             function(val) { 
1603                                 if(!val || val == copy.barcode()) return;
1604                                 copy.ischanged(true);
1605                                 copy.barcode(val);
1606                             }
1607                         );
1608
1609
1610                         if(self.realCopiesTbody.getElementsByTagName('TR').length == 0)
1611                             selectNode = node;
1612                     }
1613                 }
1614
1615                 widget.build(widgetDrawn);
1616             }
1617         );
1618
1619         this.realCopiesTbody.appendChild(row);
1620         if(selectNode) selectNode.select();
1621     };
1622
1623     this.saveRealCopies = function() {
1624         var pcrud = new openils.PermaCrud({authtoken : this.authtoken});
1625         progressDialog.show(true);
1626         var list = this.realCopyList.filter(function(copy) { return copy.ischanged(); });
1627         pcrud.update(list, {oncomplete: function() { 
1628             progressDialog.hide();
1629             self.show('list');
1630         }});
1631     }
1632 }