]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/common/li_table.js
Acq: when receiving items, require user to acknowledge any related alerts
[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.distribFormulaStore) {
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 = dojo.byId('acq-lit-distrib-formula-tbody').appendChild(this.distribFormulaTemplate.cloneNode(true));
623
624         var selector = new dijit.form.FilteringSelect(
625             {store : self.distribFormulaStore}, 
626             nodeByName('selector', row)
627         );
628
629         var apply = new dijit.form.Button(
630             {"label": localeStrings.APPLY},
631             nodeByName('set_button', row)
632         ); 
633
634         var reset = new dijit.form.Button(
635             {"label": localeStrings.RESET_FORMULAE, "disabled": true},
636             nodeByName("reset_button", row)  
637         );
638
639         dojo.connect(apply, 'onClick', 
640             function() {
641                 var form_id = selector.attr('value');
642                 if(!form_id) return;
643                 self._applyDistribFormula(form_id);
644                 reset.attr("disabled", false);
645             }
646         );
647
648         dojo.connect(reset, 'onClick', 
649             function() {
650                 self.restoreCopyFieldsBeforeDF();
651                 self.dfaCache = [];
652                 self.dfeOffset = 0;
653                 reset.attr("disabled", "true");
654             }
655         );
656
657     };
658
659     /**
660      * Applies a distrib formula to the current set of copies
661      */
662     this._applyDistribFormula = function(formula) {
663         if(!formula) return;
664
665         formula = this.distribForms.filter(
666             function(form) { return form.id() == formula; }
667         )[0];
668
669         var copyRows = dojo.query('tr', self.copyTbody);
670
671         if (this.dfeOffset >= copyRows.length) {
672             alert(localeStrings.OUT_OF_COPIES);
673             return;
674         }
675
676         var entries_applied = 0;
677         for(
678             var rowIndex = this.dfeOffset;
679             rowIndex < copyRows.length;
680             rowIndex++
681         ) {
682             
683             var row = copyRows[rowIndex];
684             var copy_id = row.getAttribute('copy_id');
685             var copyWidgets = this.copyWidgetCache[copy_id];
686             var entryIndex = this.dfeOffset;
687             var entry = null;
688
689             // find the correct entry for the current row
690             dojo.forEach(formula.entries(), 
691                 function(e) {
692                     if(!entry) {
693                         entryIndex += e.item_count();
694                         if(entryIndex > rowIndex)
695                             entry = e;
696                     }
697                 }
698             );
699
700             if(entry) {
701                 
702                 //console.log("rowIndex = " + rowIndex + ", entry = " + entry.id() + ", entryIndex=" + 
703                 //  entryIndex + ", owning_lib = " + entry.owning_lib() + ", location = " + entry.location());
704     
705                 entries_applied++;
706                 this.saveCopyFieldsBeforeDF(copy_id);
707                 this._copy_fields_for_acqdf.forEach(
708                     function(field) {
709                         if(entry[field]()) {
710                             copyWidgets[field].attr('value', (entry[field]()));
711                         }
712                     }
713                 );
714             }
715         }
716
717         if (entries_applied) {
718             this.dfaCache.push(formula.id());
719             this.dfeOffset += entries_applied;
720         };
721     };
722
723     this.saveCopyFieldsBeforeDF = function(copy_id) {
724         var self = this;
725         if (!this.oldCopyWidgetCache[copy_id]) {
726             var copyWidgets = this.copyWidgetCache[copy_id];
727
728             this.oldCopyWidgetCache[copy_id] = {};
729             this._copy_fields_for_acqdf.forEach(
730                 function(f) {
731                     self.oldCopyWidgetCache[copy_id][f] =
732                         copyWidgets[f].attr("value");
733                 }
734             );
735         }
736     };
737
738     this.restoreCopyFieldsBeforeDF = function() {
739         var self = this;
740         for (var copy_id in this.oldCopyWidgetCache) {
741             this._copy_fields_for_acqdf.forEach(
742                 function(f) {
743                     self.copyWidgetCache[copy_id][f].attr(
744                         "value", self.oldCopyWidgetCache[copy_id][f]
745                     );
746                 }
747             );
748         }
749     };
750
751     this._fetchDistribFormulas = function(onload) {
752         if(this.distribForms) {
753             onload();
754         } else {
755             var self = this;
756             fieldmapper.standardRequest(
757                 ['open-ils.acq', 'open-ils.acq.distribution_formula.ranged.retrieve.atomic'],
758                 {   async: true,
759                     params: [openils.User.authtoken],
760                     oncomplete: function(r) {
761                         self.distribForms = openils.Util.readResponse(r);
762                         if(!self.distribForms || self.distribForms.length == 0) {
763                             self.distribForms = [];
764                         } else {
765                             self.distribFormulaStore =
766                                 new dojo.data.ItemFileReadStore(
767                                     {data:acqdf.toStoreData(self.distribForms)}
768                                 );
769                         }
770                         self._addDistribFormulaRow();
771                         onload();
772                     }
773                 }
774             );
775         }
776     }
777
778     this._drawBatchCopyWidgets = function() {
779         var row = this.copyBatchRow;
780         dojo.forEach(liDetailBatchFields, 
781             function(field) {
782                 if(self.copyBatchRowDrawn) {
783                     self.copyBatchWidgets[field].attr('value', null);
784                 } else {
785                     var widget = new openils.widget.AutoFieldWidget({
786                         fmField : field,
787                         fmClass : 'acqlid',
788                         parentNode : dojo.query('[name='+field+']', row)[0],
789                         orgLimitPerms : ['CREATE_PICKLIST'],
790                         dijitArgs : {required:false},
791                         forceSync : true
792                     });
793                     widget.build(
794                         function(w, ww) {
795                             self.copyBatchWidgets[field] = w;
796                         }
797                     );
798                 }
799             }
800         );
801         this.copyBatchRowDrawn = true;
802     };
803
804     this.batchCopyUpdate = function() {
805         var self = this;
806         for(var k in this.copyWidgetCache) {
807             var cache = this.copyWidgetCache[k];
808             dojo.forEach(liDetailBatchFields, function(f) {
809                 var newval = self.copyBatchWidgets[f].attr('value');
810                 if(newval) cache[f].attr('value', newval);
811             });
812         }
813     };
814
815     this._drawCopies = function(li) {
816         var self = this;
817
818         // this button sets the total number of copies for a given lineitem
819         acqLitAddCopyCount.onClick = function() { 
820             var count = acqLitCopyCountInput.attr('value');
821
822             // add new rows
823             while(self.copyCount() < count)
824                 self.addCopy(li); 
825             
826             // delete rows if necessary
827             var diff = self.copyCount() - count;
828             if(diff > 0) {
829                 var rows = dojo.query('tr', self.copyTbody).reverse().slice(0, diff);
830                 if(confirm(dojo.string.substitute(localeStrings.DELETE_LI_COPIES_CONFIRM, [diff]))) {
831                     dojo.forEach(rows, function(row) {self.deleteCopy(row); });
832                 } else {
833                     acqLitCopyCountInput.attr('value', self.copyCount()+'');
834                 }
835             }
836         }
837
838
839         if(li.lineitem_details().length > 0) {
840             dojo.forEach(li.lineitem_details(),
841                 function(copy) {
842                     self.addCopy(li, copy);
843                 }
844             );
845         } else {
846             self.addCopy(li);
847         }
848     };
849
850     this.copyCount = function() {
851         var count = 0;
852         for(var id in this.copyCache) {
853             if(!this.copyCache[id].isdeleted())
854                 count++;
855         }
856         return count;
857     }
858
859     this.virtCopyId = -1;
860     this.addCopy = function(li, copy) {
861         var row = this.copyRow.cloneNode(true);
862         this.copyTbody.appendChild(row);
863         var self = this;
864
865         if(!copy) {
866             copy = new fieldmapper.acqlid();
867             copy.isnew(true);
868             copy.id(this.virtCopyId--);
869             copy.lineitem(li.id());
870         }
871
872         this.copyCache[copy.id()] = copy;
873         row.setAttribute('copy_id', copy.id());
874         self.copyWidgetCache[copy.id()] = {};
875
876         acqLitCopyCountInput.attr('value', self.copyCount()+'');
877
878         dojo.forEach(liDetailFields,
879             function(field) {
880                 var widget = new openils.widget.AutoFieldWidget({
881                     fmObject : copy,
882                     fmField : field,
883                     fmClass : 'acqlid',
884                     parentNode : dojo.query('[name='+field+']', row)[0],
885                     orgLimitPerms : ['CREATE_PICKLIST', 'CREATE_PURCHASE_ORDER'],
886                     readOnly : Boolean(copy.eg_copy_id())
887                 });
888                 widget.build(
889                     // make sure we capture the value from any async widgets
890                     function(w, ww) { 
891                         copy[field](ww.getFormattedValue()) 
892                         self.copyWidgetCache[copy.id()][field] = w;
893                     }
894                 );
895                 dojo.connect(widget.widget, 'onChange', 
896                     function(val) { 
897                         if(copy.isnew() || val != copy[field]()) {
898                             // prevent setting ischanged() automatically on widget load for existing copies
899                             copy[field](widget.getFormattedValue()) 
900                             copy.ischanged(true);
901                         }
902                     }
903                 );
904             }
905         );
906
907         this.updateLidReceivedness(copy, row);
908     };
909
910     this.updateLidReceivedness = function(copy, row) {
911         if (typeof(row) == "undefined") {
912             row = dojo.query(
913                 'tr[copy_id="' + copy.id() + '"]', this.copyTbody
914             )[0];
915         }
916
917         var self = this;
918         var recv_link = nodeByName("receive", row);
919         var unrecv_link = nodeByName("unreceive", row);
920         var del_link = nodeByName("delete", row);
921
922         if (this.isPO) {
923             openils.Util.hide(del_link.parentNode);
924
925             /* Avoid showing (un)receive links for virtual copies */
926             if (copy.id() > 0) {
927                 if(copy.recv_time()) {
928                     openils.Util.hide(recv_link);
929                     openils.Util.show(unrecv_link);
930                     unrecv_link.onclick = function() {
931                         if (confirm(localeStrings.UNRECEIVE_LID))
932                             self.issueReceive(copy, /* rollback */ true);
933                     };
934                 } else {
935                     openils.Util.hide(unrecv_link);
936                     openils.Util.show(recv_link);
937                     recv_link.onclick = function() {
938                         if (self.checkLiAlerts(copy.lineitem()))
939                             self.issueReceive(copy);
940                     };
941                 }
942             } else {
943                 openils.Util.hide(unrecv_link);
944                 openils.Util.hide(recv_link);
945             }
946         } else {
947             openils.Util.hide(unrecv_link);
948             openils.Util.hide(recv_link);
949
950             del_link.onclick = function() { self.deleteCopy(row) };
951             openils.Util.show(del_link.parentNode);
952         }
953     }
954
955     this._confirmAlert = function(li, lin) {
956         return confirm(
957             dojo.string.substitute(
958                 localeStrings.CONFIRM_LI_ALERT, [
959                     (new openils.acq.Lineitem({"lineitem": li})).findAttr(
960                         "title", "lineitem_marc_attr_definition"
961                     ),
962                     lin.alert_text().description(), lin.value()
963                 ]
964             )
965         );
966     };
967
968     this.checkLiAlerts = function(li_id) {
969         var li = this.liCache[li_id];
970
971         var alert_notes = li.lineitem_notes().filter(
972             function(o) { return Boolean(o.alert_text()); }
973         );
974
975         /* this is _intentionally_ not done in a call to forEach() ... */
976         for (var i = 0; i < alert_notes.length; i++) {
977             if (this.noteAcks[alert_notes[i].id()])
978                 continue;
979             else if (!this._confirmAlert(li, alert_notes[i]))
980                 return false;
981             else
982                 this.noteAcks[alert_notes[i].id()] = true;
983         }
984
985         return true;
986     };
987
988     this.deleteCopy = function(row) {
989         var copy = this.copyCache[row.getAttribute('copy_id')];
990         copy.isdeleted(true);
991         if(copy.isnew())
992             delete this.copyCache[copy.id()];
993         this.copyTbody.removeChild(row);
994     }
995
996     this.saveCopyChanges = function(liId) {
997         var self = this;
998         var copies = [];
999
1000
1001         var total = 0;
1002         for(var id in this.copyCache) {
1003             var c = this.copyCache[id];
1004             if(!c.isdeleted()) total++;
1005             if(c.isnew() || c.ischanged() || c.isdeleted()) {
1006                 if(c.id() < 0) c.id(null);
1007                 copies.push(c);
1008             }
1009         }
1010
1011         if (typeof(this._copy_count_cb) == "function") {
1012             this._copy_count_cb(liId, total);
1013         }
1014
1015         dojo.byId('acq-lit-copy-count-label-' + liId).innerHTML = total;
1016
1017
1018         if (copies.length > 0) {
1019             openils.Util.show("acq-lit-update-copies-progress");
1020             fieldmapper.standardRequest(
1021                 ['open-ils.acq', 'open-ils.acq.lineitem_detail.cud.batch'],
1022                 {   async: true,
1023                     params: [openils.User.authtoken, copies],
1024                     onresponse: function(r) {
1025                         var res = openils.Util.readResponse(r);
1026                         litUpdateCopiesProgress.update(res);
1027                     },
1028                     oncomplete: function() {
1029                         self.drawCopies(liId);
1030                         openils.Util.hide("acq-lit-update-copies-progress");
1031                     }
1032                 }
1033             );
1034         }
1035
1036         if (this.dfaCache.length > 0) {
1037             var oldlength = this.dfaCache.length;
1038
1039             fieldmapper.standardRequest(
1040                 ["open-ils.acq",
1041                 "open-ils.acq.distribution_formula.record_application"],
1042                 {
1043                     "async": true,
1044                     "params": [openils.User.authtoken, this.dfaCache, liId],
1045                     "onresponse": function(r) {
1046                         var res = openils.Util.readResponse(r);
1047                         if (res && res.length != oldlength)
1048                             alert(localeStrings.DFA_NOT_ALL);
1049                     }
1050                 }
1051             );
1052             this.dfaCache = [];
1053         }
1054     }
1055
1056     this.applySelectedLiAction = function(action) {
1057         var self = this;
1058         switch(action) {
1059
1060             case 'delete_selected':
1061                 this._deleteLiList(self.getSelected());
1062                 break;
1063
1064             case 'create_order':
1065
1066                 if(!this.createPoProviderSelector) {
1067                     var widget = new openils.widget.AutoFieldWidget({
1068                         fmField : 'provider',
1069                         fmClass : 'acqpo',
1070                         searchFilter: {"active": "t"},
1071                         parentNode : dojo.byId('acq-lit-po-provider'),
1072                     });
1073                     widget.build(
1074                         function(w) { self.createPoProviderSelector = w; }
1075                     );
1076                 }
1077
1078                 if(!this.createPoAgencySelector) {
1079                     var widget = new openils.widget.AutoFieldWidget({
1080                         fmField : 'ordering_agency',
1081                         fmClass : 'acqpo',
1082                         parentNode : dojo.byId('acq-lit-po-agency'),
1083                         orgLimitPerms : ['CREATE_PURCHASE_ORDER'],
1084                     });
1085                     widget.build(
1086                         function(w) { self.createPoAgencySelector = w; }
1087                     );
1088                 }
1089
1090          
1091                 acqLitPoCreateDialog.show();
1092                 break;
1093
1094             case 'save_picklist':
1095                 this._loadPLSelect();
1096                 acqLitSavePlDialog.show();
1097                 break;
1098
1099             case 'selector_ready':
1100             case 'order_ready':
1101                 acqLitChangeLiStateDialog.attr('state', action.replace('_', '-'));
1102                 acqLitChangeLiStateDialog.show();
1103                 break;
1104
1105             case 'print_po':
1106                 this.printPO();
1107                 break;
1108
1109             case 'receive_po':
1110                 this.receivePO();
1111                 break;
1112
1113             case 'rollback_receive_po':
1114                 this.rollbackPoReceive();
1115                 break;
1116
1117             case 'create_assets':
1118                 this.createAssets();
1119                 break;
1120
1121             case 'export_attr_list':
1122                 this.chooseExportAttr();
1123                 break;
1124
1125             case 'add_brief_record':
1126                 if(this.isPO)
1127                     location.href = oilsBasePath + '/acq/picklist/brief_record?po=' + this.isPO;
1128                 else
1129                     location.href = oilsBasePath + '/acq/picklist/brief_record?pl=' + this.isPL;
1130         }
1131     }
1132
1133     this.createAssets = function() {
1134         if(!this.isPO) return;
1135         if(!confirm(localeStrings.CREATE_PO_ASSETS_CONFIRM)) return;
1136         this.show('acq-lit-progress-numbers');
1137         var self = this;
1138         fieldmapper.standardRequest(
1139             ['open-ils.acq', 'open-ils.acq.purchase_order.assets.create'],
1140             {   async: true,
1141                 params: [this.authtoken, this.isPO],
1142                 onresponse: function(r) {
1143                     var resp = openils.Util.readResponse(r);
1144                     self._updateProgressNumbers(resp, true);
1145                 }
1146             }
1147         );
1148     }
1149
1150     this.chooseExportAttr = function() {
1151         if (!acqLitExportAttrSelector._li_setup) {
1152             var self = this;
1153             acqLitExportAttrSelector.store = new dojo.data.ItemFileReadStore(
1154                 {
1155                     "data": acqliad.toStoreData(
1156                         (new openils.PermaCrud()).search(
1157                             "acqliad", {"code": li_exportable_attrs}
1158                         )
1159                     )
1160                 }
1161             );
1162             acqLitExportAttrSelector.setValue();
1163             acqLitExportAttrButton.onClick = function(){self.exportAttrList();};
1164             acqLitExportAttrSelector._li_setup = true;
1165         }
1166         openils.Util.show("acq-lit-export-attr-holder", "inline");
1167     };
1168
1169     this.exportAttrList = function() {
1170         var attr_def = acqLitExportAttrSelector.item;
1171         var li_list = this.getSelected();
1172         var value_list = li_list.map(
1173             function(li) {
1174                 return (new openils.acq.Lineitem({"lineitem": li})).findAttr(
1175                     attr_def.code, "lineitem_marc_attr_definition"
1176                 );
1177             }
1178         ).filter(function(attr) { return Boolean(attr); });
1179
1180         if (value_list.length > 0) {
1181             if (value_list.length < li_list.length) {
1182                 if (!confirm(
1183                     dojo.string.substitute(
1184                         localeStrings.EXPORT_SHORT_LIST, [attr_def.description]
1185                     )
1186                 )) {
1187                     return;
1188                 }
1189             }
1190             try {
1191                 openils.XUL.contentToFileSaveDialog(
1192                     value_list.join("\n"),
1193                     localeStrings.EXPORT_SAVE_DIALOG_TITLE
1194                 );
1195             } catch (E) {
1196                 alert(E);
1197             }
1198         } else {
1199             alert(dojo.string.substitute(
1200                 localeStrings.EXPORT_EMPTY_LIST, [attr_def.description]
1201             ));
1202         }
1203
1204         openils.Util.hide("acq-lit-export-attr-holder");
1205     };
1206
1207     this.printPO = function() {
1208         if(!this.isPO) return;
1209         progressDialog.show(true);
1210         fieldmapper.standardRequest(
1211             ['open-ils.acq', 'open-ils.acq.purchase_order.format'],
1212             {   async: true,
1213                 params: [this.authtoken, this.isPO, 'html'],
1214                 oncomplete: function(r) {
1215                     progressDialog.hide();
1216                     var evt = openils.Util.readResponse(r);
1217                     if(evt && evt.template_output()) {
1218                         win = window.open('','', 'resizable,width=800,height=600,scrollbars=1');
1219                         win.document.body.innerHTML = evt.template_output().data();
1220                     }
1221                 }
1222             }
1223         );
1224     }
1225
1226
1227     this.receivePO = function() {
1228         if (!this.isPO) return;
1229
1230         for (var id in this.liCache) {
1231             /* assumption: liCache reflects exactly the
1232              * set of LIs that belong to our PO */
1233             if (this.liCache[id].state() != "received" &&
1234                 !this.checkLiAlerts(id)) return;
1235         }
1236
1237         this.show('acq-lit-progress-numbers');
1238         var self = this;
1239         fieldmapper.standardRequest(
1240             ['open-ils.acq', 'open-ils.acq.purchase_order.receive'],
1241             {   async: true,
1242                 params: [this.authtoken, this.isPO],
1243                 onresponse : function(r) {
1244                     var resp = openils.Util.readResponse(r);
1245                     self._updateProgressNumbers(resp, true);
1246                 },
1247             }
1248         );
1249     }
1250
1251     this.issueReceive = function(obj, rollback) {
1252         /* (For now) there shall be no marking LI or LIDs (un)received
1253          * except from the actual "view PO" interface. */
1254         if (!this.isPO) return;
1255
1256         var part =
1257             {"jub": "lineitem", "acqlid": "lineitem_detail"}[obj.classname];
1258         var method =
1259             "open-ils.acq." + part + ".receive" + (rollback ? ".rollback" : "");
1260
1261         progressDialog.show(true);
1262         fieldmapper.standardRequest(
1263             ["open-ils.acq", method], {
1264                 "async": true,
1265                 "params": [this.authtoken, obj.id()],
1266                 "onresponse": function(r) {
1267                     self.handleReceive(openils.Util.readResponse(r));
1268                 },
1269                 "oncomplete": function() { progressDialog.hide(); }
1270             }
1271         );
1272     };
1273
1274     /**
1275      * Handles the responses from receive and rollback ML calls.
1276      */
1277     this.handleReceive = function(resp) {
1278         if (resp) {
1279             if (resp.li) {
1280                 for (var li_id in resp.li) {
1281                     for (var key in resp.li[li_id])
1282                         self.liCache[li_id][key](resp.li[li_id][key]);
1283                     self.updateLiReceivedness(self.liCache[li_id]);
1284                 }
1285             }
1286             if (resp.po) {
1287                 if (typeof(self.poUpdateCallback) == "function")
1288                     self.poUpdateCallback(resp.po);
1289             }
1290             if (resp.lid) {
1291                 for (var lid_id in resp.lid) {
1292                     for (var key in resp.lid[lid_id])
1293                         self.copyCache[lid_id][key](resp.lid[lid_id][key]);
1294                     self.updateLidReceivedness(self.copyCache[lid_id]);
1295                 }
1296             }
1297         }
1298     };
1299
1300     this.rollbackPoReceive = function() {
1301         if(!this.isPO) return;
1302         if(!confirm(localeStrings.ROLLBACK_PO_RECEIVE_CONFIRM)) return;
1303         this.show('acq-lit-progress-numbers');
1304         var self = this;
1305         fieldmapper.standardRequest(
1306             ['open-ils.acq', 'open-ils.acq.purchase_order.receive.rollback'],
1307             {   async: true,
1308                 params: [this.authtoken, this.isPO],
1309                 onresponse : function(r) {
1310                     var resp = openils.Util.readResponse(r);
1311                     self._updateProgressNumbers(resp, true);
1312                 },
1313             }
1314         );
1315     }
1316
1317     this._updateProgressNumbers = function(resp, reloadOnComplete) {
1318         if(!resp) return;
1319         dojo.byId('acq-pl-lit-li-processed').innerHTML = resp.li;
1320         dojo.byId('acq-pl-lit-lid-processed').innerHTML = resp.lid;
1321         dojo.byId('acq-pl-lit-debits-processed').innerHTML = resp.debits_accrued;
1322         dojo.byId('acq-pl-lit-bibs-processed').innerHTML = resp.bibs;
1323         dojo.byId('acq-pl-lit-indexed-processed').innerHTML = resp.indexed;
1324         dojo.byId('acq-pl-lit-copies-processed').innerHTML = resp.copies;
1325         if(resp.complete && reloadOnComplete) 
1326             location.href = location.href;
1327     }
1328
1329
1330     this._createPO = function(fields) {
1331         this.show('acq-lit-progress-numbers');
1332         var po = new fieldmapper.acqpo();
1333         po.provider(this.createPoProviderSelector.attr('value'));
1334         po.ordering_agency(this.createPoAgencySelector.attr('value'));
1335
1336         var selected = this.getSelected( (fields.create_from == 'all') );
1337         if(selected.length == 0) return;
1338
1339         var max = selected.length * 3;
1340
1341         var self = this;
1342         fieldmapper.standardRequest(
1343             ['open-ils.acq', 'open-ils.acq.purchase_order.create'],
1344             {   async: true,
1345                 params: [
1346                     openils.User.authtoken, 
1347                     po, 
1348                     {
1349                         lineitems : selected.map(function(li) { return li.id() }),
1350                         create_assets : fields.create_assets[0],
1351                     }
1352                 ],
1353
1354                 onresponse : function(r) {
1355                     var resp = openils.Util.readResponse(r);
1356                     self._updateProgressNumbers(resp);
1357                     if(resp.complete) 
1358                         location.href = oilsBasePath + '/eg/acq/po/view/' + resp.purchase_order.id();
1359                 }
1360             }
1361         );
1362     }
1363
1364     this._deleteLiList = function(list, idx) {
1365         if(idx == null) idx = 0;
1366         if(idx >= list.length) return;
1367         var liId = list[idx].id();
1368         fieldmapper.standardRequest(
1369             ['open-ils.acq', 'open-ils.acq.lineitem.delete'],
1370             {   async: true,
1371                 params: [openils.User.authtoken, liId],
1372                 oncomplete: function(r) {
1373                     self.removeLineitem(liId);
1374                     self._deleteLiList(list, ++idx);
1375                 }
1376             }
1377         );
1378     }
1379
1380     this.editOrderMarc = function(li) {
1381
1382         /*  To run in Firefox directly, must set signed.applets.codebase_principal_support
1383             to true in about:config */
1384
1385         if(!openils.XUL.enableXPConnect()) return;
1386
1387         if(openils.XUL.isXUL()) {
1388             win = window.open('/xul/' + openils.XUL.buildId() + '/server/cat/marcedit.xul');
1389         } else {
1390             win = window.open('/xul/server/cat/marcedit.xul'); 
1391         }
1392         var self = this;
1393         win.xulG = {
1394             record : {marc : li.marc()},
1395             save : {
1396                 label: 'Save Record', // XXX I18N
1397                 func: function(xmlString) {
1398                     li.marc(xmlString);
1399                     fieldmapper.standardRequest(
1400                         ['open-ils.acq', 'open-ils.acq.lineitem.update'],
1401                         {   async: true,
1402                             params: [openils.User.authtoken, li],
1403                             oncomplete: function(r) {
1404                                 openils.Util.readResponse(r);
1405                                 win.close();
1406                                 self.drawInfo(li.id())
1407                             }
1408                         }
1409                     );
1410                 },
1411             }
1412         };
1413     }
1414
1415     this._savePl = function(values) {
1416         var self = this;
1417         var selected = this.getSelected( (values.which == 'all') );
1418         openils.Util.show('acq-lit-generic-progress');
1419
1420         if(values.new_name) {
1421             openils.acq.Picklist.create(
1422                 {name: values.new_name}, 
1423                 function(id) {
1424                     self._updateLiList(id, selected, 0, 
1425                         function(){
1426                             location.href = oilsBasePath + '/eg/acq/picklist/view/' + id;
1427                         });
1428                 }
1429             );
1430         } else if(values.existing_pl) {
1431             // update lineitems to use an existing picklist
1432             self._updateLiList(values.existing_pl, selected, 0, 
1433                 function(){
1434                     location.href = oilsBasePath + '/eg/acq/picklist/view/' + values.existing_pl;
1435                 });
1436         }
1437     }
1438
1439     this._updateLiState = function(values, state) {
1440         var self = this;
1441         var selected = this.getSelected( (values.which == 'all') );
1442         if(!selected.length) return;
1443         dojo.forEach(selected, function(li) {li.state(state);});
1444         self._updateLiList(null, selected, 0, 
1445             // TODO consider inline updates for efficiency
1446             function() { location.href = location.href }
1447         );
1448     }
1449
1450     this._updateLiList = function(pl, list, idx, oncomplete) {
1451         if(idx >= list.length) return oncomplete();
1452         var li = list[idx];
1453         if(pl != null) li.picklist(pl);
1454         litGenericProgress.update({maximum: list.length, progress: idx});
1455         new openils.acq.Lineitem({lineitem:li}).update(
1456             function(r) {
1457                 self._updateLiList(pl, list, ++idx, oncomplete);
1458             }
1459         );
1460     }
1461
1462     this._loadPLSelect = function() {
1463         if(this._plSelectLoaded) return;
1464         var plList = [];
1465         function handleResponse(r) {
1466             plList.push(r.recv().content());
1467         }
1468         var method = 'open-ils.acq.picklist.user.retrieve';
1469         fieldmapper.standardRequest(
1470             ['open-ils.acq', method],
1471             {   async: true,
1472                 params: [this.authtoken],
1473                 onresponse: handleResponse,
1474                 oncomplete: function() {
1475                     self._plSelectLoaded = true;
1476                     acqLitAddExistingSelect.store = 
1477                         new dojo.data.ItemFileReadStore({data:acqpl.toStoreData(plList)});
1478                     acqLitAddExistingSelect.setValue();
1479                 }
1480             }
1481         );
1482     }
1483
1484     // grab the li-details for this lineitem, grab the linked copies and volumes, add them to the table
1485     this.showRealCopies = function(li) {
1486         while(this.realCopiesTbody.childNodes[0])
1487             this.realCopiesTbody.removeChild(this.realCopiesTbody.childNodes[0]);
1488         this.show('real-copies');
1489
1490         var pcrud = new openils.PermaCrud({authtoken : this.authtoken});
1491         this.realCopyList = [];
1492         this.volCache = {};
1493         var tabIndex = 1000;
1494         var self = this;
1495
1496         acqLitSaveRealCopies.onClick = function() {
1497             self.saveRealCopies();
1498         }
1499
1500         this._fetchLineitem(li.id(), 
1501             function(fullLi) {
1502                 li = self.liCache[li.id()] = fullLi;
1503
1504                 pcrud.search(
1505                     'acp', {
1506                         id : li.lineitem_details().map(
1507                             function(item) { return item.eg_copy_id() }
1508                         )
1509                     }, {
1510                         async : true,
1511                         streaming : true,
1512                         onresponse : function(r) {
1513                             var copy = openils.Util.readResponse(r);
1514                             var volId = copy.call_number();
1515                             var volume = self.volCache[volId];
1516                             if(!volume) {
1517                                 volume = self.volCache[volId] = pcrud.retrieve('acn', volId);
1518                             }
1519                             self.addRealCopy(volume, copy, tabIndex++);
1520                         }
1521                     }
1522                 );
1523             }
1524         );
1525     }
1526
1527     this.addRealCopy = function(volume, copy, tabIndex) {
1528         var row = this.realCopiesRow.cloneNode(true);
1529         this.realCopyList.push(copy);
1530
1531         var selectNode;
1532         dojo.forEach(
1533             ['owning_lib', 'location', 'circ_modifier', 'label', 'barcode'],
1534
1535             function(field) {
1536                 var isvol = (field == 'owning_lib' || field == 'label');
1537                 var widget = new openils.widget.AutoFieldWidget({
1538                     fmField : field,
1539                     fmObject : isvol ? volume : copy,
1540                     parentNode : nodeByName(field, row),
1541                     readOnly : (field != 'barcode'),
1542                 });
1543
1544                 var widgetDrawn = null;
1545
1546                 if(field == 'barcode') {
1547
1548                     widgetDrawn = function(w, ww) {
1549                         var node = w.domNode;
1550                         node.setAttribute('tabindex', ''+tabIndex);
1551
1552                         // on enter, select the next barcode input
1553                         dojo.connect(w, 'onKeyDown',
1554                             function(e) {
1555                                 if(e.keyCode == dojo.keys.ENTER) {
1556                                     var ti = node.getAttribute('tabindex');
1557                                     var nextNode = dojo.query('[tabindex=' + String(Number(ti) + 1) + ']', self.realCopiesTbody)[0];
1558                                     if(nextNode) nextNode.select();
1559                                 }
1560                             }
1561                         );
1562
1563                         dojo.connect(w, 'onChange', 
1564                             function(val) { 
1565                                 if(!val || val == copy.barcode()) return;
1566                                 copy.ischanged(true);
1567                                 copy.barcode(val);
1568                             }
1569                         );
1570
1571
1572                         if(self.realCopiesTbody.getElementsByTagName('TR').length == 0)
1573                             selectNode = node;
1574                     }
1575                 }
1576
1577                 widget.build(widgetDrawn);
1578             }
1579         );
1580
1581         this.realCopiesTbody.appendChild(row);
1582         if(selectNode) selectNode.select();
1583     };
1584
1585     this.saveRealCopies = function() {
1586         var pcrud = new openils.PermaCrud({authtoken : this.authtoken});
1587         progressDialog.show(true);
1588         var list = this.realCopyList.filter(function(copy) { return copy.ischanged(); });
1589         pcrud.update(list, {oncomplete: function() { 
1590             progressDialog.hide();
1591             self.show('list');
1592         }});
1593     }
1594 }