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