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');
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"];
24 function nodeByName(name, context) {
25 return dojo.query('[name='+name+']', context)[0];
29 var liDetailBatchFields = ['fund', 'owning_lib', 'location', 'collection_code', 'circ_modifier', 'cn_label'];
30 var liDetailFields = liDetailBatchFields.concat(['barcode', 'note']);
32 function AcqLiTable() {
40 this.toggleState = false;
41 this.tbody = dojo.byId('acq-lit-tbody');
43 this.authtoken = openils.User.authtoken;
44 this.rowTemplate = this.tbody.removeChild(dojo.byId('acq-lit-row'));
45 this.copyTbody = dojo.byId('acq-lit-li-details-tbody');
46 this.copyRow = this.copyTbody.removeChild(dojo.byId('acq-lit-li-details-row'));
47 this.copyBatchRow = dojo.byId('acq-lit-li-details-batch-row');
48 this.copyBatchWidgets = {};
49 this.liNotesTbody = dojo.byId('acq-lit-notes-tbody');
50 this.liNotesRow = this.liNotesTbody.removeChild(dojo.byId('acq-lit-notes-row'));
51 this.realCopiesTbody = dojo.byId('acq-lit-real-copies-tbody');
52 this.realCopiesRow = this.realCopiesTbody.removeChild(dojo.byId('acq-lit-real-copies-row'));
53 this._copy_fields_for_acqdf = ['owning_lib', 'location'];
55 dojo.connect(acqLitLiActionsSelector, 'onChange',
57 self.applySelectedLiAction(this.attr('value'))
58 acqLitLiActionsSelector.attr('value', '_');
61 acqLitCreatePoSubmit.onClick = function() {
62 acqLitPoCreateDialog.hide();
63 self._createPO(acqLitPoCreateDialog.getValues());
66 acqLitSavePlButton.onClick = function() {
67 acqLitSavePlDialog.hide();
68 self._savePl(acqLitSavePlDialog.getValues());
71 acqLitCancelLiStateButton.onClick = function() {
72 acqLitChangeLiStateDialog.hide();
74 acqLitSaveLiStateButton.onClick = function() {
75 acqLitChangeLiStateDialog.hide();
76 self._updateLiState(acqLitChangeLiStateDialog.getValues(), acqLitChangeLiStateDialog.attr('state'));
80 //dojo.byId('acq-lit-notes-new-button').onclick = function(){acqLitCreateLiNoteDialog.show();}
82 dojo.byId('acq-lit-select-toggle').onclick = function(){self.toggleSelect()};
83 dojo.byId('acq-lit-info-back-button').onclick = function(){self.show('list')};
84 dojo.byId('acq-lit-copies-back-button').onclick = function(){self.show('list')};
85 dojo.byId('acq-lit-notes-back-button').onclick = function(){self.show('list')};
86 dojo.byId('acq-lit-real-copies-back-button').onclick = function(){self.show('list')};
88 this.reset = function() {
89 while(self.tbody.childNodes[0])
90 self.tbody.removeChild(self.tbody.childNodes[0]);
94 this.setNext = function(handler) {
95 var link = dojo.byId('acq-lit-next');
97 dojo.style(link, 'visibility', 'visible');
98 link.onclick = handler;
100 dojo.style(link, 'visibility', 'hidden');
104 this.setPrev = function(handler) {
105 var link = dojo.byId('acq-lit-prev');
107 dojo.style(link, 'visibility', 'visible');
108 link.onclick = handler;
110 dojo.style(link, 'visibility', 'hidden');
114 this.show = function(div) {
115 openils.Util.hide('acq-lit-table-div');
116 openils.Util.hide('acq-lit-info-div');
117 openils.Util.hide('acq-lit-li-details');
118 openils.Util.hide('acq-lit-notes-div');
119 openils.Util.hide('acq-lit-real-copies-div');
122 openils.Util.show('acq-lit-table-div');
125 openils.Util.show('acq-lit-info-div');
128 openils.Util.show('acq-lit-li-details');
131 openils.Util.show('acq-lit-real-copies-div');
134 openils.Util.show('acq-lit-notes-div');
138 openils.Util.show(div);
142 this.hide = function() {
146 this.toggleSelect = function() {
148 dojo.forEach(self.selectors, function(i){i.checked = false});
150 dojo.forEach(self.selectors, function(i){i.checked = true});
151 self.toggleState = !self.toggleState;
155 /** @param all If true, assume all are selected */
156 this.getSelected = function(all) {
158 dojo.forEach(self.selectors,
161 selected.push(self.liCache[i.parentNode.parentNode.getAttribute('li')]);
167 this.setRowAttr = function(td, liWrapper, field, type) {
168 var val = liWrapper.findAttr(field, type || 'lineitem_marc_attr_definition') || '';
169 td.appendChild(document.createTextNode(val));
173 * Inserts a single lineitem into the growing table of lineitems
174 * @param {Object} li The lineitem object to insert
176 this.addLineitem = function(li, skip_final_placement) {
177 this.liCache[li.id()] = li;
179 // sort the lineitem notes on edit_time
180 if(!li.lineitem_notes()) li.lineitem_notes([]);
182 var liWrapper = new openils.acq.Lineitem({lineitem:li});
183 var row = self.rowTemplate.cloneNode(true);
184 row.setAttribute('li', li.id());
185 var tds = dojo.query('[attr]', row);
186 dojo.forEach(tds, function(td) {self.setRowAttr(td, liWrapper, td.getAttribute('attr'), td.getAttribute('attr_type'));});
187 dojo.query('[name=source_label]', row)[0].appendChild(document.createTextNode(li.source_label()));
189 var isbn = liWrapper.findAttr('isbn', 'lineitem_marc_attr_definition');
191 // XXX media prefix for added content
192 dojo.query('[name=jacket]', row)[0].setAttribute('src', '/opac/extras/ac/jacket/small/' + isbn);
195 dojo.query('[attr=title]', row)[0].onclick = function() {self.drawInfo(li.id())};
196 dojo.query('[name=copieslink]', row)[0].onclick = function() {self.drawCopies(li.id())};
197 dojo.query('[name=notes_count]', row)[0].innerHTML = li.lineitem_notes().length;
198 dojo.query('[name=noteslink]', row)[0].onclick = function() {self.drawLiNotes(li)};
200 // show which PO this lineitem is a member of
201 if(li.purchase_order() && !this.isPO) {
203 this.poCache[li.purchase_order()] =
204 this.poCache[li.purchase_order()] ||
205 fieldmapper.standardRequest(
206 ['open-ils.acq', 'open-ils.acq.purchase_order.retrieve'],
208 this.authtoken, li.purchase_order(), {
209 "flesh_price_summary": true,
210 "flesh_lineitem_count": true
213 if(po && !this.isMeta) {
214 openils.Util.show(nodeByName('po', row), 'inline');
215 var link = nodeByName('po_link', row);
216 link.setAttribute('href', oilsBasePath + '/acq/po/view/' + li.purchase_order());
217 link.innerHTML = 'PO: ' + po.name(); // TODO i18n
221 // show which picklist this lineitem is a member of
222 if(li.picklist() && (this.isPO || this.isMeta)) {
224 this.plCache[li.picklist()] =
225 this.plCache[li.picklist()] ||
226 fieldmapper.standardRequest(
227 ['open-ils.acq', 'open-ils.acq.picklist.retrieve'],
228 {params: [this.authtoken, li.picklist()]});
230 openils.Util.show(nodeByName('pl', row), 'inline');
231 var link = nodeByName('pl_link', row);
232 link.setAttribute('href', oilsBasePath + '/acq/picklist/view/' + li.picklist());
233 link.innerHTML = 'PL: '+pl.name(); // TODO i18n
237 var countNode = nodeByName('count', row);
238 var count = li.item_count() || 0;
239 if (typeof(this._copy_count_cb) == "function") {
240 this._copy_count_cb(li.id(), count);
242 countNode.innerHTML = count;
243 countNode.id = 'acq-lit-copy-count-label-' + li.id();
246 nodeByName('li_state', row).innerHTML = li.state(); // TODO i18n state labels
247 openils.Util.addCSSClass(row, 'oils-acq-li-state-' + li.state());
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) };
255 var recv_link = dojo.query('[name=receive_link]', row)[0];
257 if(li.state() == 'on-order') {
258 recv_link.onclick = function() {
260 openils.Util.hide(recv_link)
263 openils.Util.hide(recv_link);
266 // TODO we should allow editing before receipt, in which case the
267 // test should be "if 1 or more real (acp) copies exist
268 if(li.state() == 'received') {
269 var real_copies_link = dojo.query('[name=real_copies_link]', row)[0];
270 openils.Util.show(real_copies_link);
271 real_copies_link.onclick = function() {
272 self.showRealCopies(li);
276 if (!skip_final_placement) {
277 self.tbody.appendChild(row);
278 self.selectors.push(dojo.query('[name=selectbox]', row)[0]);
285 * Draws and shows the lineitem notes pane
287 this.drawLiNotes = function(li) {
291 li.lineitem_notes().sort(
293 if(a.edit_time() < b.edit_time()) return 1;
299 while(this.liNotesTbody.childNodes[0])
300 this.liNotesTbody.removeChild(this.liNotesTbody.childNodes[0]);
303 acqLitCreateLiNoteSubmit.onClick = function() {
304 var value = acqLitCreateNoteText.attr('value');
306 var note = new fieldmapper.acqlin();
309 note.lineitem(li.id());
310 self.updateLiNotes(li, note);
313 dojo.byId('acq-lit-notes-save-button').onclick = function() {
314 self.updateLiNotes(li);
317 dojo.forEach(li.lineitem_notes(), function(note) { self.addLiNote(li, note) });
321 * Draws a single lineitem note in the notes pane
323 this.addLiNote = function(li, note) {
324 if(note.isdeleted()) return;
326 var row = self.liNotesRow.cloneNode(true);
327 dojo.query('[name=value]', row)[0].innerHTML = note.value();
329 dojo.query('[name=delete]', row)[0].onclick = function() {
330 note.isdeleted(true);
331 self.liNotesTbody.removeChild(row);
334 if(note.edit_time()) {
335 dojo.query('[name=edit_time]', row)[0].innerHTML =
336 dojo.date.locale.format(
337 dojo.date.stamp.fromISOString(note.edit_time()),
338 {formatLength:'short'});
341 self.liNotesTbody.appendChild(row);
345 * Updates any new/changed/deleted notes on the server
347 this.updateLiNotes = function(li, newNote) {
353 notes = li.lineitem_notes().filter(
355 if(note.ischanged() || note.isnew() || note.isdeleted())
361 if(notes.length == 0) return;
362 progressDialog.show();
364 fieldmapper.standardRequest(
365 ['open-ils.acq', 'open-ils.acq.lineitem_note.cud.batch'],
367 params : [this.authtoken, notes],
368 onresponse : function(r) {
369 var resp = openils.Util.readResponse(r);
374 // remove the old changed notes
376 dojo.forEach(li.lineitem_notes(),
378 if(!(note.ischanged() || note.isnew() || note.isdeleted()))
382 li.lineitem_notes(list);
385 progressDialog.hide();
386 self.drawLiNotes(li);
390 progressDialog.update(resp);
391 var newnote = resp.note;
393 if(!newnote.isdeleted()) {
394 newnote.isnew(false);
395 newnote.ischanged(false);
396 li.lineitem_notes().push(newnote);
403 this.updateLiPrice = function(input, li) {
405 var price = input.value;
406 var liWrapper = new openils.acq.Lineitem({lineitem:li});
407 var oldPrice = liWrapper.getPrice() || null;
409 if(oldPrice) oldPrice = oldPrice.price;
410 if(price == oldPrice) return;
412 fieldmapper.standardRequest(
413 ['open-ils.acq', 'open-ils.acq.lineitem.price.set'],
415 params : [this.authtoken, li.id(), price],
416 oncomplete : function(r) {
417 openils.Util.readResponse(r);
423 this.removeLineitem = function(liId) {
424 this.tbody.removeChild(dojo.query('[li='+liId+']', this.tbody)[0]);
425 delete this.liCache[liId];
426 //selected.push(self.liCache[i.parentNode.parentNode.getAttribute('li')]);
429 this.drawInfo = function(liId) {
431 openils.acq.Lineitem.fetchAttrDefs(
433 self._fetchLineitem(liId, function(li){self._drawInfo(li);});
438 this._fetchLineitem = function(liId, handler) {
440 var li = this.liCache[liId];
441 if(li && li.marc() && li.lineitem_details())
444 fieldmapper.standardRequest(
445 ['open-ils.acq', 'open-ils.acq.lineitem.retrieve'],
448 params: [self.authtoken, liId, {
450 flesh_li_details: true,
451 flesh_fund_debit: true }],
453 oncomplete: function(r) {
454 var li = openils.Util.readResponse(r);
461 this._drawInfo = function(li) {
463 acqLitEditOrderMarc.onClick = function() { self.editOrderMarc(li); }
466 openils.Util.hide('acq-lit-marc-order-record-label');
467 openils.Util.hide(acqLitEditOrderMarc.domNode);
468 openils.Util.show('acq-lit-marc-real-record-label');
470 openils.Util.show('acq-lit-marc-order-record-label');
471 openils.Util.show(acqLitEditOrderMarc.domNode);
472 openils.Util.hide('acq-lit-marc-real-record-label');
475 this.drawMarcHTML(li);
476 this.infoTbody = dojo.byId('acq-lit-info-tbody');
479 this.infoRow = this.infoTbody.removeChild(dojo.byId('acq-lit-info-row'));
480 while(this.infoTbody.childNodes[0])
481 this.infoTbody.removeChild(this.infoTbody.childNodes[0]);
483 for(var i = 0; i < li.attributes().length; i++) {
484 var attr = li.attributes()[i];
485 var row = this.infoRow.cloneNode(true);
487 var type = attr.attr_type().replace(/lineitem_(.*)_attr_definition/, '$1');
488 var name = openils.acq.Lineitem.attrDefs[type].filter(
490 return (a.code() == attr.attr_name());
492 ).pop().description();
494 dojo.query('[name=label]', row)[0].appendChild(document.createTextNode(name));
495 dojo.query('[name=value]', row)[0].appendChild(document.createTextNode(attr.attr_value()));
496 this.infoTbody.appendChild(row);
500 openils.Util.show('acq-lit-info-cat-link');
501 var link = dojo.byId('acq-lit-info-cat-link').getElementsByTagName('a')[0];
503 if(openils.XUL.isXUL()) {
505 var makeRecTab = function() {
508 {tab_name: localeStrings.XUL_RECORD_DETAIL_PAGE, browser:false},
511 show_nav_buttons : true,
512 show_print_button : true,
513 opac_url : xulG.url_prefix(xulG.urls.opac_rdetail + '?r=' + li.eg_bib_id())
517 link.setAttribute('href', 'javascript:void(0);');
518 link.onclick = makeRecTab;
521 var href = link.getAttribute('href');
523 link.setAttribute('href', href + li.eg_bib_id());
526 openils.Util.hide('acq-lit-info-cat-link');
530 this.drawMarcHTML = function(li) {
531 var params = [null, true, li.marc()];
533 params = [li.eg_bib_id(), true];
535 fieldmapper.standardRequest(
536 ['open-ils.search', 'open-ils.search.biblio.record.html'],
539 oncomplete: function(r) {
540 dojo.byId('acq-lit-marc-div').innerHTML =
541 openils.Util.readResponse(r);
547 this.drawCopies = function(liId) {
551 this.copyWidgetCache = {};
552 this.oldCopyWidgetCache = {};
556 acqLitSaveCopies.onClick = function() { self.saveCopyChanges(liId) };
557 acqLitBatchUpdateCopies.onClick = function() { self.batchCopyUpdate() };
558 acqLitCopyCountInput.attr('value', '0');
560 while(this.copyTbody.childNodes[0])
561 this.copyTbody.removeChild(this.copyTbody.childNodes[0]);
563 this._drawBatchCopyWidgets();
565 this._fetchDistribFormulas(
567 openils.acq.Lineitem.fetchAttrDefs(
569 self._fetchLineitem(liId, function(li){self._drawCopies(li);});
577 * Insert a new row into the distribution formula selection form
579 this._addDistribFormulaRow = function() {
582 if(!self.distribFormulaStore) {
583 // no formulas, hide the form
584 openils.Util.hide('acq-lit-distrib-formula-tbody');
588 if(!this.distribFormulaTemplate)
589 this.distribFormulaTemplate =
590 dojo.byId('acq-lit-distrib-formula-tbody').removeChild(dojo.byId('acq-lit-distrib-form-row'));
592 var row = dojo.byId('acq-lit-distrib-formula-tbody').appendChild(this.distribFormulaTemplate.cloneNode(true));
594 var selector = new dijit.form.FilteringSelect(
595 {store : self.distribFormulaStore},
596 nodeByName('selector', row)
599 var apply = new dijit.form.Button(
600 {"label": localeStrings.APPLY},
601 nodeByName('set_button', row)
604 var reset = new dijit.form.Button(
605 {"label": localeStrings.RESET_FORMULAE, "disabled": true},
606 nodeByName("reset_button", row)
609 dojo.connect(apply, 'onClick',
611 var form_id = selector.attr('value');
613 self._applyDistribFormula(form_id);
614 reset.attr("disabled", false);
618 dojo.connect(reset, 'onClick',
620 self.restoreCopyFieldsBeforeDF();
623 reset.attr("disabled", "true");
630 * Applies a distrib formula to the current set of copies
632 this._applyDistribFormula = function(formula) {
635 formula = this.distribForms.filter(
636 function(form) { return form.id() == formula; }
639 var copyRows = dojo.query('tr', self.copyTbody);
641 if (this.dfeOffset >= copyRows.length) {
642 alert(localeStrings.OUT_OF_COPIES);
646 var entries_applied = 0;
648 var rowIndex = this.dfeOffset;
649 rowIndex < copyRows.length;
653 var row = copyRows[rowIndex];
654 var copy_id = row.getAttribute('copy_id');
655 var copyWidgets = this.copyWidgetCache[copy_id];
656 var entryIndex = this.dfeOffset;
659 // find the correct entry for the current row
660 dojo.forEach(formula.entries(),
663 entryIndex += e.item_count();
664 if(entryIndex > rowIndex)
672 //console.log("rowIndex = " + rowIndex + ", entry = " + entry.id() + ", entryIndex=" +
673 // entryIndex + ", owning_lib = " + entry.owning_lib() + ", location = " + entry.location());
676 this.saveCopyFieldsBeforeDF(copy_id);
677 this._copy_fields_for_acqdf.forEach(
680 copyWidgets[field].attr('value', (entry[field]()));
687 if (entries_applied) {
688 this.dfaCache.push(formula.id());
689 this.dfeOffset += entries_applied;
693 this.saveCopyFieldsBeforeDF = function(copy_id) {
695 if (!this.oldCopyWidgetCache[copy_id]) {
696 var copyWidgets = this.copyWidgetCache[copy_id];
698 this.oldCopyWidgetCache[copy_id] = {};
699 this._copy_fields_for_acqdf.forEach(
701 self.oldCopyWidgetCache[copy_id][f] =
702 copyWidgets[f].attr("value");
708 this.restoreCopyFieldsBeforeDF = function() {
710 for (var copy_id in this.oldCopyWidgetCache) {
711 this._copy_fields_for_acqdf.forEach(
713 self.copyWidgetCache[copy_id][f].attr(
714 "value", self.oldCopyWidgetCache[copy_id][f]
721 this._fetchDistribFormulas = function(onload) {
722 if(this.distribForms) {
726 fieldmapper.standardRequest(
727 ['open-ils.acq', 'open-ils.acq.distribution_formula.ranged.retrieve.atomic'],
729 params: [openils.User.authtoken],
730 oncomplete: function(r) {
731 self.distribForms = openils.Util.readResponse(r);
732 if(!self.distribForms || self.distribForms.length == 0) {
733 self.distribForms = [];
735 self.distribFormulaStore =
736 new dojo.data.ItemFileReadStore(
737 {data:acqdf.toStoreData(self.distribForms)}
740 self._addDistribFormulaRow();
748 this._drawBatchCopyWidgets = function() {
749 var row = this.copyBatchRow;
750 dojo.forEach(liDetailBatchFields,
752 if(self.copyBatchRowDrawn) {
753 self.copyBatchWidgets[field].attr('value', null);
755 var widget = new openils.widget.AutoFieldWidget({
758 parentNode : dojo.query('[name='+field+']', row)[0],
759 orgLimitPerms : ['CREATE_PICKLIST'],
760 dijitArgs : {required:false},
765 self.copyBatchWidgets[field] = w;
771 this.copyBatchRowDrawn = true;
774 this.batchCopyUpdate = function() {
776 for(var k in this.copyWidgetCache) {
777 var cache = this.copyWidgetCache[k];
778 dojo.forEach(liDetailBatchFields, function(f) {
779 var newval = self.copyBatchWidgets[f].attr('value');
780 if(newval) cache[f].attr('value', newval);
785 this._drawCopies = function(li) {
788 // this button sets the total number of copies for a given lineitem
789 acqLitAddCopyCount.onClick = function() {
790 var count = acqLitCopyCountInput.attr('value');
793 while(self.copyCount() < count)
796 // delete rows if necessary
797 var diff = self.copyCount() - count;
799 var rows = dojo.query('tr', self.copyTbody).reverse().slice(0, diff);
800 if(confirm(dojo.string.substitute(localeStrings.DELETE_LI_COPIES_CONFIRM, [diff]))) {
801 dojo.forEach(rows, function(row) {self.deleteCopy(row); });
803 acqLitCopyCountInput.attr('value', self.copyCount()+'');
809 if(li.lineitem_details().length > 0) {
810 dojo.forEach(li.lineitem_details(),
812 self.addCopy(li, copy);
820 this.copyCount = function() {
822 for(var id in this.copyCache) {
823 if(!this.copyCache[id].isdeleted())
829 this.virtCopyId = -1;
830 this.addCopy = function(li, copy) {
831 var row = this.copyRow.cloneNode(true);
832 this.copyTbody.appendChild(row);
836 copy = new fieldmapper.acqlid();
838 copy.id(this.virtCopyId--);
839 copy.lineitem(li.id());
842 this.copyCache[copy.id()] = copy;
843 row.setAttribute('copy_id', copy.id());
844 self.copyWidgetCache[copy.id()] = {};
846 acqLitCopyCountInput.attr('value', self.copyCount()+'');
848 dojo.forEach(liDetailFields,
850 var widget = new openils.widget.AutoFieldWidget({
854 parentNode : dojo.query('[name='+field+']', row)[0],
855 orgLimitPerms : ['CREATE_PICKLIST', 'CREATE_PURCHASE_ORDER'],
856 readOnly : Boolean(copy.eg_copy_id())
859 // make sure we capture the value from any async widgets
861 copy[field](ww.getFormattedValue())
862 self.copyWidgetCache[copy.id()][field] = w;
865 dojo.connect(widget.widget, 'onChange',
867 if(copy.isnew() || val != copy[field]()) {
868 // prevent setting ischanged() automatically on widget load for existing copies
869 copy[field](widget.getFormattedValue())
870 copy.ischanged(true);
877 var recv_link = dojo.query('[name=receive]', row)[0];
878 if(copy.recv_time()) {
879 openils.Util.hide(recv_link);
881 recv_link.onclick = function() {
882 self.receiveLid(copy);
883 openils.Util.hide(recv_link);
888 openils.Util.hide(dojo.query('[name=delete]', row)[0].parentNode);
890 dojo.query('[name=delete]', row)[0].onclick =
891 function() { self.deleteCopy(row) };
895 this.deleteCopy = function(row) {
896 var copy = this.copyCache[row.getAttribute('copy_id')];
897 copy.isdeleted(true);
899 delete this.copyCache[copy.id()];
900 this.copyTbody.removeChild(row);
903 this.saveCopyChanges = function(liId) {
909 for(var id in this.copyCache) {
910 var c = this.copyCache[id];
911 if(!c.isdeleted()) total++;
912 if(c.isnew() || c.ischanged() || c.isdeleted()) {
913 if(c.id() < 0) c.id(null);
918 if (typeof(this._copy_count_cb) == "function") {
919 this._copy_count_cb(liId, total);
922 dojo.byId('acq-lit-copy-count-label-' + liId).innerHTML = total;
925 if (copies.length > 0) {
926 openils.Util.show("acq-lit-update-copies-progress");
927 fieldmapper.standardRequest(
928 ['open-ils.acq', 'open-ils.acq.lineitem_detail.cud.batch'],
930 params: [openils.User.authtoken, copies],
931 onresponse: function(r) {
932 var res = openils.Util.readResponse(r);
933 litUpdateCopiesProgress.update(res);
935 oncomplete: function() {
936 self.drawCopies(liId);
937 openils.Util.hide("acq-lit-update-copies-progress");
943 if (this.dfaCache.length > 0) {
944 var oldlength = this.dfaCache.length;
946 fieldmapper.standardRequest(
948 "open-ils.acq.distribution_formula.record_application"],
951 "params": [openils.User.authtoken, this.dfaCache, liId],
952 "onresponse": function(r) {
953 var res = openils.Util.readResponse(r);
954 if (res && res.length != oldlength)
955 alert(localeStrings.DFA_NOT_ALL);
963 this.applySelectedLiAction = function(action) {
967 case 'delete_selected':
968 this._deleteLiList(self.getSelected());
973 if(!this.createPoProviderSelector) {
974 var widget = new openils.widget.AutoFieldWidget({
975 fmField : 'provider',
977 searchFilter: {"active": "t"},
978 parentNode : dojo.byId('acq-lit-po-provider'),
981 function(w) { self.createPoProviderSelector = w; }
985 if(!this.createPoAgencySelector) {
986 var widget = new openils.widget.AutoFieldWidget({
987 fmField : 'ordering_agency',
989 parentNode : dojo.byId('acq-lit-po-agency'),
990 orgLimitPerms : ['CREATE_PURCHASE_ORDER'],
993 function(w) { self.createPoAgencySelector = w; }
998 acqLitPoCreateDialog.show();
1001 case 'save_picklist':
1002 this._loadPLSelect();
1003 acqLitSavePlDialog.show();
1006 case 'selector_ready':
1008 acqLitChangeLiStateDialog.attr('state', action.replace('_', '-'));
1009 acqLitChangeLiStateDialog.show();
1020 case 'rollback_receive_po':
1021 this.rollbackPoReceive();
1024 case 'create_assets':
1025 this.createAssets();
1028 case 'export_attr_list':
1029 this.chooseExportAttr();
1032 case 'add_brief_record':
1034 location.href = oilsBasePath + '/acq/picklist/brief_record?po=' + this.isPO;
1036 location.href = oilsBasePath + '/acq/picklist/brief_record?pl=' + this.isPL;
1040 this.createAssets = function() {
1041 if(!this.isPO) return;
1042 if(!confirm(localeStrings.CREATE_PO_ASSETS_CONFIRM)) return;
1043 this.show('acq-lit-progress-numbers');
1045 fieldmapper.standardRequest(
1046 ['open-ils.acq', 'open-ils.acq.purchase_order.assets.create'],
1048 params: [this.authtoken, this.isPO],
1049 onresponse: function(r) {
1050 var resp = openils.Util.readResponse(r);
1051 self._updateProgressNumbers(resp, true);
1057 this.chooseExportAttr = function() {
1058 if (!acqLitExportAttrSelector._li_setup) {
1060 acqLitExportAttrSelector.store = new dojo.data.ItemFileReadStore(
1062 "data": acqliad.toStoreData(
1063 (new openils.PermaCrud()).search(
1064 "acqliad", {"code": li_exportable_attrs}
1069 acqLitExportAttrSelector.setValue();
1070 acqLitExportAttrButton.onClick = function(){self.exportAttrList();};
1071 acqLitExportAttrSelector._li_setup = true;
1073 openils.Util.show("acq-lit-export-attr-holder", "inline");
1076 this.exportAttrList = function() {
1077 var attr_def = acqLitExportAttrSelector.item;
1078 var li_list = this.getSelected();
1079 var value_list = li_list.map(
1081 return (new openils.acq.Lineitem({"lineitem": li})).findAttr(
1082 attr_def.code, "lineitem_marc_attr_definition"
1085 ).filter(function(attr) { return Boolean(attr); });
1087 if (value_list.length > 0) {
1088 if (value_list.length < li_list.length) {
1090 dojo.string.substitute(
1091 localeStrings.EXPORT_SHORT_LIST, [attr_def.description]
1098 openils.XUL.contentToFileSaveDialog(
1099 value_list.join("\n"),
1100 localeStrings.EXPORT_SAVE_DIALOG_TITLE
1106 alert(dojo.string.substitute(
1107 localeStrings.EXPORT_EMPTY_LIST, [attr_def.description]
1111 openils.Util.hide("acq-lit-export-attr-holder");
1114 this.printPO = function() {
1115 if(!this.isPO) return;
1116 progressDialog.show(true);
1117 fieldmapper.standardRequest(
1118 ['open-ils.acq', 'open-ils.acq.purchase_order.format'],
1120 params: [this.authtoken, this.isPO, 'html'],
1121 oncomplete: function(r) {
1122 progressDialog.hide();
1123 var evt = openils.Util.readResponse(r);
1124 if(evt && evt.template_output()) {
1125 win = window.open('','', 'resizable,width=800,height=600,scrollbars=1');
1126 win.document.body.innerHTML = evt.template_output().data();
1134 this.receivePO = function() {
1135 if(!this.isPO) return;
1136 this.show('acq-lit-progress-numbers');
1138 fieldmapper.standardRequest(
1139 ['open-ils.acq', 'open-ils.acq.purchase_order.receive'],
1141 params: [this.authtoken, this.isPO],
1142 onresponse : function(r) {
1143 var resp = openils.Util.readResponse(r);
1144 self._updateProgressNumbers(resp, true);
1150 this.receiveLi = function(li) {
1151 if(!this.isPO) return;
1152 progressDialog.show(true);
1153 fieldmapper.standardRequest(
1154 ['open-ils.acq', 'open-ils.acq.lineitem.receive'],
1156 params: [this.authtoken, li.id()],
1157 onresponse : function(r) {
1158 var resp = openils.Util.readResponse(r);
1159 progressDialog.hide();
1165 this.receiveLid = function(li) {
1166 if(!this.isPO) return;
1167 progressDialog.show(true);
1168 fieldmapper.standardRequest(
1169 ['open-ils.acq', 'open-ils.acq.lineitem_detail.receive'],
1171 params: [this.authtoken, li.id()],
1172 onresponse : function(r) {
1173 var resp = openils.Util.readResponse(r);
1174 progressDialog.hide();
1180 this.rollbackPoReceive = function() {
1181 if(!this.isPO) return;
1182 if(!confirm(localeStrings.ROLLBACK_PO_RECEIVE_CONFIRM)) return;
1183 this.show('acq-lit-progress-numbers');
1185 fieldmapper.standardRequest(
1186 ['open-ils.acq', 'open-ils.acq.purchase_order.receive.rollback'],
1188 params: [this.authtoken, this.isPO],
1189 onresponse : function(r) {
1190 var resp = openils.Util.readResponse(r);
1191 self._updateProgressNumbers(resp, true);
1197 this._updateProgressNumbers = function(resp, reloadOnComplete) {
1199 dojo.byId('acq-pl-lit-li-processed').innerHTML = resp.li;
1200 dojo.byId('acq-pl-lit-lid-processed').innerHTML = resp.lid;
1201 dojo.byId('acq-pl-lit-debits-processed').innerHTML = resp.debits_accrued;
1202 dojo.byId('acq-pl-lit-bibs-processed').innerHTML = resp.bibs;
1203 dojo.byId('acq-pl-lit-indexed-processed').innerHTML = resp.indexed;
1204 dojo.byId('acq-pl-lit-copies-processed').innerHTML = resp.copies;
1205 if(resp.complete && reloadOnComplete)
1206 location.href = location.href;
1210 this._createPO = function(fields) {
1211 this.show('acq-lit-progress-numbers');
1212 var po = new fieldmapper.acqpo();
1213 po.provider(this.createPoProviderSelector.attr('value'));
1214 po.ordering_agency(this.createPoAgencySelector.attr('value'));
1216 var selected = this.getSelected( (fields.create_from == 'all') );
1217 if(selected.length == 0) return;
1219 var max = selected.length * 3;
1222 fieldmapper.standardRequest(
1223 ['open-ils.acq', 'open-ils.acq.purchase_order.create'],
1226 openils.User.authtoken,
1229 lineitems : selected.map(function(li) { return li.id() }),
1230 create_assets : fields.create_assets[0],
1234 onresponse : function(r) {
1235 var resp = openils.Util.readResponse(r);
1236 self._updateProgressNumbers(resp);
1238 location.href = oilsBasePath + '/eg/acq/po/view/' + resp.purchase_order.id();
1244 this._deleteLiList = function(list, idx) {
1245 if(idx == null) idx = 0;
1246 if(idx >= list.length) return;
1247 var liId = list[idx].id();
1248 fieldmapper.standardRequest(
1249 ['open-ils.acq', 'open-ils.acq.lineitem.delete'],
1251 params: [openils.User.authtoken, liId],
1252 oncomplete: function(r) {
1253 self.removeLineitem(liId);
1254 self._deleteLiList(list, ++idx);
1260 this.editOrderMarc = function(li) {
1262 /* To run in Firefox directly, must set signed.applets.codebase_principal_support
1263 to true in about:config */
1265 if(!openils.XUL.enableXPConnect()) return;
1267 if(openils.XUL.isXUL()) {
1268 win = window.open('/xul/' + openils.XUL.buildId() + '/server/cat/marcedit.xul');
1270 win = window.open('/xul/server/cat/marcedit.xul');
1274 record : {marc : li.marc()},
1276 label: 'Save Record', // XXX I18N
1277 func: function(xmlString) {
1279 fieldmapper.standardRequest(
1280 ['open-ils.acq', 'open-ils.acq.lineitem.update'],
1282 params: [openils.User.authtoken, li],
1283 oncomplete: function(r) {
1284 openils.Util.readResponse(r);
1286 self.drawInfo(li.id())
1295 this._savePl = function(values) {
1297 var selected = this.getSelected( (values.which == 'all') );
1298 openils.Util.show('acq-lit-generic-progress');
1300 if(values.new_name) {
1301 openils.acq.Picklist.create(
1302 {name: values.new_name},
1304 self._updateLiList(id, selected, 0,
1306 location.href = oilsBasePath + '/eg/acq/picklist/view/' + id;
1310 } else if(values.existing_pl) {
1311 // update lineitems to use an existing picklist
1312 self._updateLiList(values.existing_pl, selected, 0,
1314 location.href = oilsBasePath + '/eg/acq/picklist/view/' + values.existing_pl;
1319 this._updateLiState = function(values, state) {
1321 var selected = this.getSelected( (values.which == 'all') );
1322 if(!selected.length) return;
1323 dojo.forEach(selected, function(li) {li.state(state);});
1324 self._updateLiList(null, selected, 0,
1325 // TODO consider inline updates for efficiency
1326 function() { location.href = location.href }
1330 this._updateLiList = function(pl, list, idx, oncomplete) {
1331 if(idx >= list.length) return oncomplete();
1333 if(pl != null) li.picklist(pl);
1334 litGenericProgress.update({maximum: list.length, progress: idx});
1335 new openils.acq.Lineitem({lineitem:li}).update(
1337 self._updateLiList(pl, list, ++idx, oncomplete);
1342 this._loadPLSelect = function() {
1343 if(this._plSelectLoaded) return;
1345 function handleResponse(r) {
1346 plList.push(r.recv().content());
1348 var method = 'open-ils.acq.picklist.user.retrieve';
1349 fieldmapper.standardRequest(
1350 ['open-ils.acq', method],
1352 params: [this.authtoken],
1353 onresponse: handleResponse,
1354 oncomplete: function() {
1355 self._plSelectLoaded = true;
1356 acqLitAddExistingSelect.store =
1357 new dojo.data.ItemFileReadStore({data:acqpl.toStoreData(plList)});
1358 acqLitAddExistingSelect.setValue();
1364 // grab the li-details for this lineitem, grab the linked copies and volumes, add them to the table
1365 this.showRealCopies = function(li) {
1366 while(this.realCopiesTbody.childNodes[0])
1367 this.realCopiesTbody.removeChild(this.realCopiesTbody.childNodes[0]);
1368 this.show('real-copies');
1370 var pcrud = new openils.PermaCrud({authtoken : this.authtoken});
1371 this.realCopyList = [];
1373 var tabIndex = 1000;
1376 acqLitSaveRealCopies.onClick = function() {
1377 self.saveRealCopies();
1380 this._fetchLineitem(li.id(),
1382 li = self.liCache[li.id()] = fullLi;
1386 id : li.lineitem_details().map(
1387 function(item) { return item.eg_copy_id() }
1392 onresponse : function(r) {
1393 var copy = openils.Util.readResponse(r);
1394 var volId = copy.call_number();
1395 var volume = self.volCache[volId];
1397 volume = self.volCache[volId] = pcrud.retrieve('acn', volId);
1399 self.addRealCopy(volume, copy, tabIndex++);
1407 this.addRealCopy = function(volume, copy, tabIndex) {
1408 var row = this.realCopiesRow.cloneNode(true);
1409 this.realCopyList.push(copy);
1413 ['owning_lib', 'location', 'circ_modifier', 'label', 'barcode'],
1416 var isvol = (field == 'owning_lib' || field == 'label');
1417 var widget = new openils.widget.AutoFieldWidget({
1419 fmObject : isvol ? volume : copy,
1420 parentNode : nodeByName(field, row),
1421 readOnly : (field != 'barcode'),
1424 var widgetDrawn = null;
1426 if(field == 'barcode') {
1428 widgetDrawn = function(w, ww) {
1429 var node = w.domNode;
1430 node.setAttribute('tabindex', ''+tabIndex);
1432 // on enter, select the next barcode input
1433 dojo.connect(w, 'onKeyDown',
1435 if(e.keyCode == dojo.keys.ENTER) {
1436 var ti = node.getAttribute('tabindex');
1437 var nextNode = dojo.query('[tabindex=' + String(Number(ti) + 1) + ']', self.realCopiesTbody)[0];
1438 if(nextNode) nextNode.select();
1443 dojo.connect(w, 'onChange',
1445 if(!val || val == copy.barcode()) return;
1446 copy.ischanged(true);
1452 if(self.realCopiesTbody.getElementsByTagName('TR').length == 0)
1457 widget.build(widgetDrawn);
1461 this.realCopiesTbody.appendChild(row);
1462 if(selectNode) selectNode.select();
1465 this.saveRealCopies = function() {
1466 var pcrud = new openils.PermaCrud({authtoken : this.authtoken});
1467 progressDialog.show(true);
1468 var list = this.realCopyList.filter(function(copy) { return copy.ischanged(); });
1469 pcrud.update(list, {oncomplete: function() {
1470 progressDialog.hide();