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
248 var priceInput = dojo.query('[name=price]', row)[0];
249 var priceData = liWrapper.getPrice();
250 priceInput.value = (priceData) ? priceData.price : '';
251 priceInput.onchange = function() { self.updateLiPrice(priceInput, li) };
253 // show either "mark received" or "unreceive" as appropriate
254 this.updateReceivedness(li, row);
256 if (!skip_final_placement) {
257 self.tbody.appendChild(row);
258 self.selectors.push(dojo.query('[name=selectbox]', row)[0]);
264 this.updateReceivedness = function(li, row) {
265 if (typeof(row) == "undefined")
266 row = dojo.query('tr[li="' + li.id() + '"]', "acq-lit-tbody")[0];
268 var recv_link = nodeByName("receive_link", row);
269 var unrecv_link = nodeByName("unreceive_link", row);
270 var real_copies_link = nodeByName("real_copies_link", row);
272 /* handle row coloring for based on LI state */
273 openils.Util.removeCSSClass(row, /^oils-acq-li-state-/);
274 openils.Util.addCSSClass(row, "oils-acq-li-state-" + li.state());
276 /* handle links that appear/disappear based on whether LI is received */
281 openils.Util.hide(real_copies_link);
282 openils.Util.hide(unrecv_link);
283 openils.Util.show(recv_link, "inline");
284 if (typeof(recv_link.onclick) != "function")
285 recv_link.onclick = function() { self.receiveLi(li); };
288 openils.Util.hide(recv_link);
289 openils.Util.show(unrecv_link, "inline");
290 if (typeof(unrecv_link.onclick) != "function") {
291 unrecv_link.onclick = function() {
292 if (confirm(localeStrings.UNRECEIVE_LI))
293 self.unReceiveLi(li);
296 // TODO we should allow editing before receipt, in which case the
297 // test should be "if 1 or more real (acp) copies exist
298 openils.Util.show(real_copies_link);
299 real_copies_link.onclick = function() {
300 self.showRealCopies(li);
306 openils.Util.hide(recv_link);
307 openils.Util.hide(unrecv_link);
308 openils.Util.hide(real_copies_link);
313 * Draws and shows the lineitem notes pane
315 this.drawLiNotes = function(li) {
319 li.lineitem_notes().sort(
321 if(a.edit_time() < b.edit_time()) return 1;
327 while(this.liNotesTbody.childNodes[0])
328 this.liNotesTbody.removeChild(this.liNotesTbody.childNodes[0]);
331 acqLitCreateLiNoteSubmit.onClick = function() {
332 var value = acqLitCreateNoteText.attr('value');
334 var note = new fieldmapper.acqlin();
337 note.lineitem(li.id());
338 self.updateLiNotes(li, note);
341 dojo.byId('acq-lit-notes-save-button').onclick = function() {
342 self.updateLiNotes(li);
345 dojo.forEach(li.lineitem_notes(), function(note) { self.addLiNote(li, note) });
349 * Draws a single lineitem note in the notes pane
351 this.addLiNote = function(li, note) {
352 if(note.isdeleted()) return;
354 var row = self.liNotesRow.cloneNode(true);
355 dojo.query('[name=value]', row)[0].innerHTML = note.value();
357 dojo.query('[name=delete]', row)[0].onclick = function() {
358 note.isdeleted(true);
359 self.liNotesTbody.removeChild(row);
362 if(note.edit_time()) {
363 dojo.query('[name=edit_time]', row)[0].innerHTML =
364 dojo.date.locale.format(
365 dojo.date.stamp.fromISOString(note.edit_time()),
366 {formatLength:'short'});
369 self.liNotesTbody.appendChild(row);
373 * Updates any new/changed/deleted notes on the server
375 this.updateLiNotes = function(li, newNote) {
381 notes = li.lineitem_notes().filter(
383 if(note.ischanged() || note.isnew() || note.isdeleted())
389 if(notes.length == 0) return;
390 progressDialog.show();
392 fieldmapper.standardRequest(
393 ['open-ils.acq', 'open-ils.acq.lineitem_note.cud.batch'],
395 params : [this.authtoken, notes],
396 onresponse : function(r) {
397 var resp = openils.Util.readResponse(r);
402 // remove the old changed notes
404 dojo.forEach(li.lineitem_notes(),
406 if(!(note.ischanged() || note.isnew() || note.isdeleted()))
410 li.lineitem_notes(list);
413 progressDialog.hide();
414 self.drawLiNotes(li);
418 progressDialog.update(resp);
419 var newnote = resp.note;
421 if(!newnote.isdeleted()) {
422 newnote.isnew(false);
423 newnote.ischanged(false);
424 li.lineitem_notes().push(newnote);
431 this.updateLiPrice = function(input, li) {
433 var price = input.value;
434 var liWrapper = new openils.acq.Lineitem({lineitem:li});
435 var oldPrice = liWrapper.getPrice() || null;
437 if(oldPrice) oldPrice = oldPrice.price;
438 if(price == oldPrice) return;
440 fieldmapper.standardRequest(
441 ['open-ils.acq', 'open-ils.acq.lineitem.price.set'],
443 params : [this.authtoken, li.id(), price],
444 oncomplete : function(r) {
445 openils.Util.readResponse(r);
451 this.removeLineitem = function(liId) {
452 this.tbody.removeChild(dojo.query('[li='+liId+']', this.tbody)[0]);
453 delete this.liCache[liId];
454 //selected.push(self.liCache[i.parentNode.parentNode.getAttribute('li')]);
457 this.drawInfo = function(liId) {
459 openils.acq.Lineitem.fetchAttrDefs(
461 self._fetchLineitem(liId, function(li){self._drawInfo(li);});
466 this._fetchLineitem = function(liId, handler) {
468 var li = this.liCache[liId];
469 if(li && li.marc() && li.lineitem_details())
472 fieldmapper.standardRequest(
473 ['open-ils.acq', 'open-ils.acq.lineitem.retrieve'],
476 params: [self.authtoken, liId, {
478 flesh_li_details: true,
479 flesh_fund_debit: true }],
481 oncomplete: function(r) {
482 var li = openils.Util.readResponse(r);
489 this._drawInfo = function(li) {
491 acqLitEditOrderMarc.onClick = function() { self.editOrderMarc(li); }
494 openils.Util.hide('acq-lit-marc-order-record-label');
495 openils.Util.hide(acqLitEditOrderMarc.domNode);
496 openils.Util.show('acq-lit-marc-real-record-label');
498 openils.Util.show('acq-lit-marc-order-record-label');
499 openils.Util.show(acqLitEditOrderMarc.domNode);
500 openils.Util.hide('acq-lit-marc-real-record-label');
503 this.drawMarcHTML(li);
504 this.infoTbody = dojo.byId('acq-lit-info-tbody');
507 this.infoRow = this.infoTbody.removeChild(dojo.byId('acq-lit-info-row'));
508 while(this.infoTbody.childNodes[0])
509 this.infoTbody.removeChild(this.infoTbody.childNodes[0]);
511 for(var i = 0; i < li.attributes().length; i++) {
512 var attr = li.attributes()[i];
513 var row = this.infoRow.cloneNode(true);
515 var type = attr.attr_type().replace(/lineitem_(.*)_attr_definition/, '$1');
516 var name = openils.acq.Lineitem.attrDefs[type].filter(
518 return (a.code() == attr.attr_name());
520 ).pop().description();
522 dojo.query('[name=label]', row)[0].appendChild(document.createTextNode(name));
523 dojo.query('[name=value]', row)[0].appendChild(document.createTextNode(attr.attr_value()));
524 this.infoTbody.appendChild(row);
528 openils.Util.show('acq-lit-info-cat-link');
529 var link = dojo.byId('acq-lit-info-cat-link').getElementsByTagName('a')[0];
531 if(openils.XUL.isXUL()) {
533 var makeRecTab = function() {
536 {tab_name: localeStrings.XUL_RECORD_DETAIL_PAGE, browser:false},
539 show_nav_buttons : true,
540 show_print_button : true,
541 opac_url : xulG.url_prefix(xulG.urls.opac_rdetail + '?r=' + li.eg_bib_id())
545 link.setAttribute('href', 'javascript:void(0);');
546 link.onclick = makeRecTab;
549 var href = link.getAttribute('href');
551 link.setAttribute('href', href + li.eg_bib_id());
554 openils.Util.hide('acq-lit-info-cat-link');
558 this.drawMarcHTML = function(li) {
559 var params = [null, true, li.marc()];
561 params = [li.eg_bib_id(), true];
563 fieldmapper.standardRequest(
564 ['open-ils.search', 'open-ils.search.biblio.record.html'],
567 oncomplete: function(r) {
568 dojo.byId('acq-lit-marc-div').innerHTML =
569 openils.Util.readResponse(r);
575 this.drawCopies = function(liId) {
579 this.copyWidgetCache = {};
580 this.oldCopyWidgetCache = {};
584 acqLitSaveCopies.onClick = function() { self.saveCopyChanges(liId) };
585 acqLitBatchUpdateCopies.onClick = function() { self.batchCopyUpdate() };
586 acqLitCopyCountInput.attr('value', '0');
588 while(this.copyTbody.childNodes[0])
589 this.copyTbody.removeChild(this.copyTbody.childNodes[0]);
591 this._drawBatchCopyWidgets();
593 this._fetchDistribFormulas(
595 openils.acq.Lineitem.fetchAttrDefs(
597 self._fetchLineitem(liId, function(li){self._drawCopies(li);});
605 * Insert a new row into the distribution formula selection form
607 this._addDistribFormulaRow = function() {
610 if(!self.distribFormulaStore) {
611 // no formulas, hide the form
612 openils.Util.hide('acq-lit-distrib-formula-tbody');
616 if(!this.distribFormulaTemplate)
617 this.distribFormulaTemplate =
618 dojo.byId('acq-lit-distrib-formula-tbody').removeChild(dojo.byId('acq-lit-distrib-form-row'));
620 var row = dojo.byId('acq-lit-distrib-formula-tbody').appendChild(this.distribFormulaTemplate.cloneNode(true));
622 var selector = new dijit.form.FilteringSelect(
623 {store : self.distribFormulaStore},
624 nodeByName('selector', row)
627 var apply = new dijit.form.Button(
628 {"label": localeStrings.APPLY},
629 nodeByName('set_button', row)
632 var reset = new dijit.form.Button(
633 {"label": localeStrings.RESET_FORMULAE, "disabled": true},
634 nodeByName("reset_button", row)
637 dojo.connect(apply, 'onClick',
639 var form_id = selector.attr('value');
641 self._applyDistribFormula(form_id);
642 reset.attr("disabled", false);
646 dojo.connect(reset, 'onClick',
648 self.restoreCopyFieldsBeforeDF();
651 reset.attr("disabled", "true");
658 * Applies a distrib formula to the current set of copies
660 this._applyDistribFormula = function(formula) {
663 formula = this.distribForms.filter(
664 function(form) { return form.id() == formula; }
667 var copyRows = dojo.query('tr', self.copyTbody);
669 if (this.dfeOffset >= copyRows.length) {
670 alert(localeStrings.OUT_OF_COPIES);
674 var entries_applied = 0;
676 var rowIndex = this.dfeOffset;
677 rowIndex < copyRows.length;
681 var row = copyRows[rowIndex];
682 var copy_id = row.getAttribute('copy_id');
683 var copyWidgets = this.copyWidgetCache[copy_id];
684 var entryIndex = this.dfeOffset;
687 // find the correct entry for the current row
688 dojo.forEach(formula.entries(),
691 entryIndex += e.item_count();
692 if(entryIndex > rowIndex)
700 //console.log("rowIndex = " + rowIndex + ", entry = " + entry.id() + ", entryIndex=" +
701 // entryIndex + ", owning_lib = " + entry.owning_lib() + ", location = " + entry.location());
704 this.saveCopyFieldsBeforeDF(copy_id);
705 this._copy_fields_for_acqdf.forEach(
708 copyWidgets[field].attr('value', (entry[field]()));
715 if (entries_applied) {
716 this.dfaCache.push(formula.id());
717 this.dfeOffset += entries_applied;
721 this.saveCopyFieldsBeforeDF = function(copy_id) {
723 if (!this.oldCopyWidgetCache[copy_id]) {
724 var copyWidgets = this.copyWidgetCache[copy_id];
726 this.oldCopyWidgetCache[copy_id] = {};
727 this._copy_fields_for_acqdf.forEach(
729 self.oldCopyWidgetCache[copy_id][f] =
730 copyWidgets[f].attr("value");
736 this.restoreCopyFieldsBeforeDF = function() {
738 for (var copy_id in this.oldCopyWidgetCache) {
739 this._copy_fields_for_acqdf.forEach(
741 self.copyWidgetCache[copy_id][f].attr(
742 "value", self.oldCopyWidgetCache[copy_id][f]
749 this._fetchDistribFormulas = function(onload) {
750 if(this.distribForms) {
754 fieldmapper.standardRequest(
755 ['open-ils.acq', 'open-ils.acq.distribution_formula.ranged.retrieve.atomic'],
757 params: [openils.User.authtoken],
758 oncomplete: function(r) {
759 self.distribForms = openils.Util.readResponse(r);
760 if(!self.distribForms || self.distribForms.length == 0) {
761 self.distribForms = [];
763 self.distribFormulaStore =
764 new dojo.data.ItemFileReadStore(
765 {data:acqdf.toStoreData(self.distribForms)}
768 self._addDistribFormulaRow();
776 this._drawBatchCopyWidgets = function() {
777 var row = this.copyBatchRow;
778 dojo.forEach(liDetailBatchFields,
780 if(self.copyBatchRowDrawn) {
781 self.copyBatchWidgets[field].attr('value', null);
783 var widget = new openils.widget.AutoFieldWidget({
786 parentNode : dojo.query('[name='+field+']', row)[0],
787 orgLimitPerms : ['CREATE_PICKLIST'],
788 dijitArgs : {required:false},
793 self.copyBatchWidgets[field] = w;
799 this.copyBatchRowDrawn = true;
802 this.batchCopyUpdate = function() {
804 for(var k in this.copyWidgetCache) {
805 var cache = this.copyWidgetCache[k];
806 dojo.forEach(liDetailBatchFields, function(f) {
807 var newval = self.copyBatchWidgets[f].attr('value');
808 if(newval) cache[f].attr('value', newval);
813 this._drawCopies = function(li) {
816 // this button sets the total number of copies for a given lineitem
817 acqLitAddCopyCount.onClick = function() {
818 var count = acqLitCopyCountInput.attr('value');
821 while(self.copyCount() < count)
824 // delete rows if necessary
825 var diff = self.copyCount() - count;
827 var rows = dojo.query('tr', self.copyTbody).reverse().slice(0, diff);
828 if(confirm(dojo.string.substitute(localeStrings.DELETE_LI_COPIES_CONFIRM, [diff]))) {
829 dojo.forEach(rows, function(row) {self.deleteCopy(row); });
831 acqLitCopyCountInput.attr('value', self.copyCount()+'');
837 if(li.lineitem_details().length > 0) {
838 dojo.forEach(li.lineitem_details(),
840 self.addCopy(li, copy);
848 this.copyCount = function() {
850 for(var id in this.copyCache) {
851 if(!this.copyCache[id].isdeleted())
857 this.virtCopyId = -1;
858 this.addCopy = function(li, copy) {
859 var row = this.copyRow.cloneNode(true);
860 this.copyTbody.appendChild(row);
864 copy = new fieldmapper.acqlid();
866 copy.id(this.virtCopyId--);
867 copy.lineitem(li.id());
870 this.copyCache[copy.id()] = copy;
871 row.setAttribute('copy_id', copy.id());
872 self.copyWidgetCache[copy.id()] = {};
874 acqLitCopyCountInput.attr('value', self.copyCount()+'');
876 dojo.forEach(liDetailFields,
878 var widget = new openils.widget.AutoFieldWidget({
882 parentNode : dojo.query('[name='+field+']', row)[0],
883 orgLimitPerms : ['CREATE_PICKLIST', 'CREATE_PURCHASE_ORDER'],
884 readOnly : Boolean(copy.eg_copy_id())
887 // make sure we capture the value from any async widgets
889 copy[field](ww.getFormattedValue())
890 self.copyWidgetCache[copy.id()][field] = w;
893 dojo.connect(widget.widget, 'onChange',
895 if(copy.isnew() || val != copy[field]()) {
896 // prevent setting ischanged() automatically on widget load for existing copies
897 copy[field](widget.getFormattedValue())
898 copy.ischanged(true);
905 var recv_link = dojo.query('[name=receive]', row)[0];
906 if(copy.recv_time()) {
907 openils.Util.hide(recv_link);
909 recv_link.onclick = function() {
910 self.receiveLid(copy);
911 openils.Util.hide(recv_link);
916 openils.Util.hide(dojo.query('[name=delete]', row)[0].parentNode);
918 dojo.query('[name=delete]', row)[0].onclick =
919 function() { self.deleteCopy(row) };
923 this.deleteCopy = function(row) {
924 var copy = this.copyCache[row.getAttribute('copy_id')];
925 copy.isdeleted(true);
927 delete this.copyCache[copy.id()];
928 this.copyTbody.removeChild(row);
931 this.saveCopyChanges = function(liId) {
937 for(var id in this.copyCache) {
938 var c = this.copyCache[id];
939 if(!c.isdeleted()) total++;
940 if(c.isnew() || c.ischanged() || c.isdeleted()) {
941 if(c.id() < 0) c.id(null);
946 if (typeof(this._copy_count_cb) == "function") {
947 this._copy_count_cb(liId, total);
950 dojo.byId('acq-lit-copy-count-label-' + liId).innerHTML = total;
953 if (copies.length > 0) {
954 openils.Util.show("acq-lit-update-copies-progress");
955 fieldmapper.standardRequest(
956 ['open-ils.acq', 'open-ils.acq.lineitem_detail.cud.batch'],
958 params: [openils.User.authtoken, copies],
959 onresponse: function(r) {
960 var res = openils.Util.readResponse(r);
961 litUpdateCopiesProgress.update(res);
963 oncomplete: function() {
964 self.drawCopies(liId);
965 openils.Util.hide("acq-lit-update-copies-progress");
971 if (this.dfaCache.length > 0) {
972 var oldlength = this.dfaCache.length;
974 fieldmapper.standardRequest(
976 "open-ils.acq.distribution_formula.record_application"],
979 "params": [openils.User.authtoken, this.dfaCache, liId],
980 "onresponse": function(r) {
981 var res = openils.Util.readResponse(r);
982 if (res && res.length != oldlength)
983 alert(localeStrings.DFA_NOT_ALL);
991 this.applySelectedLiAction = function(action) {
995 case 'delete_selected':
996 this._deleteLiList(self.getSelected());
1001 if(!this.createPoProviderSelector) {
1002 var widget = new openils.widget.AutoFieldWidget({
1003 fmField : 'provider',
1005 searchFilter: {"active": "t"},
1006 parentNode : dojo.byId('acq-lit-po-provider'),
1009 function(w) { self.createPoProviderSelector = w; }
1013 if(!this.createPoAgencySelector) {
1014 var widget = new openils.widget.AutoFieldWidget({
1015 fmField : 'ordering_agency',
1017 parentNode : dojo.byId('acq-lit-po-agency'),
1018 orgLimitPerms : ['CREATE_PURCHASE_ORDER'],
1021 function(w) { self.createPoAgencySelector = w; }
1026 acqLitPoCreateDialog.show();
1029 case 'save_picklist':
1030 this._loadPLSelect();
1031 acqLitSavePlDialog.show();
1034 case 'selector_ready':
1036 acqLitChangeLiStateDialog.attr('state', action.replace('_', '-'));
1037 acqLitChangeLiStateDialog.show();
1048 case 'rollback_receive_po':
1049 this.rollbackPoReceive();
1052 case 'create_assets':
1053 this.createAssets();
1056 case 'export_attr_list':
1057 this.chooseExportAttr();
1060 case 'add_brief_record':
1062 location.href = oilsBasePath + '/acq/picklist/brief_record?po=' + this.isPO;
1064 location.href = oilsBasePath + '/acq/picklist/brief_record?pl=' + this.isPL;
1068 this.createAssets = function() {
1069 if(!this.isPO) return;
1070 if(!confirm(localeStrings.CREATE_PO_ASSETS_CONFIRM)) return;
1071 this.show('acq-lit-progress-numbers');
1073 fieldmapper.standardRequest(
1074 ['open-ils.acq', 'open-ils.acq.purchase_order.assets.create'],
1076 params: [this.authtoken, this.isPO],
1077 onresponse: function(r) {
1078 var resp = openils.Util.readResponse(r);
1079 self._updateProgressNumbers(resp, true);
1085 this.chooseExportAttr = function() {
1086 if (!acqLitExportAttrSelector._li_setup) {
1088 acqLitExportAttrSelector.store = new dojo.data.ItemFileReadStore(
1090 "data": acqliad.toStoreData(
1091 (new openils.PermaCrud()).search(
1092 "acqliad", {"code": li_exportable_attrs}
1097 acqLitExportAttrSelector.setValue();
1098 acqLitExportAttrButton.onClick = function(){self.exportAttrList();};
1099 acqLitExportAttrSelector._li_setup = true;
1101 openils.Util.show("acq-lit-export-attr-holder", "inline");
1104 this.exportAttrList = function() {
1105 var attr_def = acqLitExportAttrSelector.item;
1106 var li_list = this.getSelected();
1107 var value_list = li_list.map(
1109 return (new openils.acq.Lineitem({"lineitem": li})).findAttr(
1110 attr_def.code, "lineitem_marc_attr_definition"
1113 ).filter(function(attr) { return Boolean(attr); });
1115 if (value_list.length > 0) {
1116 if (value_list.length < li_list.length) {
1118 dojo.string.substitute(
1119 localeStrings.EXPORT_SHORT_LIST, [attr_def.description]
1126 openils.XUL.contentToFileSaveDialog(
1127 value_list.join("\n"),
1128 localeStrings.EXPORT_SAVE_DIALOG_TITLE
1134 alert(dojo.string.substitute(
1135 localeStrings.EXPORT_EMPTY_LIST, [attr_def.description]
1139 openils.Util.hide("acq-lit-export-attr-holder");
1142 this.printPO = function() {
1143 if(!this.isPO) return;
1144 progressDialog.show(true);
1145 fieldmapper.standardRequest(
1146 ['open-ils.acq', 'open-ils.acq.purchase_order.format'],
1148 params: [this.authtoken, this.isPO, 'html'],
1149 oncomplete: function(r) {
1150 progressDialog.hide();
1151 var evt = openils.Util.readResponse(r);
1152 if(evt && evt.template_output()) {
1153 win = window.open('','', 'resizable,width=800,height=600,scrollbars=1');
1154 win.document.body.innerHTML = evt.template_output().data();
1162 this.receivePO = function() {
1163 if(!this.isPO) return;
1164 this.show('acq-lit-progress-numbers');
1166 fieldmapper.standardRequest(
1167 ['open-ils.acq', 'open-ils.acq.purchase_order.receive'],
1169 params: [this.authtoken, this.isPO],
1170 onresponse : function(r) {
1171 var resp = openils.Util.readResponse(r);
1172 self._updateProgressNumbers(resp, true);
1178 this.receiveLi = function(li) {
1179 /* (For now) there shall be no marking LIs received except from the
1180 * actual "view PO" interface. */
1181 if (!this.isPO) return;
1184 progressDialog.show(true);
1185 fieldmapper.standardRequest(
1186 ["open-ils.acq", "open-ils.acq.lineitem.receive"], {
1188 "params": [this.authtoken, li.id()],
1189 "onresponse": function(r) {
1190 self.handleReceiveOrRollback(openils.Util.readResponse(r));
1192 "oncomplete": function() {
1193 progressDialog.hide();
1199 this.handleReceiveOrRollback = function(resp) {
1202 for (var li_id in resp.li) {
1203 for (var key in resp.li[li_id])
1204 self.liCache[li_id][key](resp.li[li_id][key]);
1205 self.updateReceivedness(self.liCache[li_id]);
1209 if (typeof(self.poUpdateCallback) == "function")
1210 self.poUpdateCallback(resp.po);
1215 this.unReceiveLi = function(li) {
1216 /* (For now) there shall be no marking LIs un-received except from the
1217 * actual "view PO" interface. */
1218 if (!this.isPO) return;
1221 progressDialog.show(true);
1222 fieldmapper.standardRequest(
1223 ["open-ils.acq", "open-ils.acq.lineitem.receive.rollback"], {
1225 "params": [this.authtoken, li.id()],
1226 "onresponse": function(r) {
1227 self.handleReceiveOrRollback(openils.Util.readResponse(r));
1229 "oncomplete": function() {
1230 progressDialog.hide();
1236 this.receiveLid = function(li) {
1237 if(!this.isPO) return;
1238 progressDialog.show(true);
1239 fieldmapper.standardRequest(
1240 ['open-ils.acq', 'open-ils.acq.lineitem_detail.receive'],
1242 params: [this.authtoken, li.id()],
1243 onresponse : function(r) {
1244 var resp = openils.Util.readResponse(r);
1245 progressDialog.hide();
1251 this.rollbackPoReceive = function() {
1252 if(!this.isPO) return;
1253 if(!confirm(localeStrings.ROLLBACK_PO_RECEIVE_CONFIRM)) return;
1254 this.show('acq-lit-progress-numbers');
1256 fieldmapper.standardRequest(
1257 ['open-ils.acq', 'open-ils.acq.purchase_order.receive.rollback'],
1259 params: [this.authtoken, this.isPO],
1260 onresponse : function(r) {
1261 var resp = openils.Util.readResponse(r);
1262 self._updateProgressNumbers(resp, true);
1268 this._updateProgressNumbers = function(resp, reloadOnComplete) {
1270 dojo.byId('acq-pl-lit-li-processed').innerHTML = resp.li;
1271 dojo.byId('acq-pl-lit-lid-processed').innerHTML = resp.lid;
1272 dojo.byId('acq-pl-lit-debits-processed').innerHTML = resp.debits_accrued;
1273 dojo.byId('acq-pl-lit-bibs-processed').innerHTML = resp.bibs;
1274 dojo.byId('acq-pl-lit-indexed-processed').innerHTML = resp.indexed;
1275 dojo.byId('acq-pl-lit-copies-processed').innerHTML = resp.copies;
1276 if(resp.complete && reloadOnComplete)
1277 location.href = location.href;
1281 this._createPO = function(fields) {
1282 this.show('acq-lit-progress-numbers');
1283 var po = new fieldmapper.acqpo();
1284 po.provider(this.createPoProviderSelector.attr('value'));
1285 po.ordering_agency(this.createPoAgencySelector.attr('value'));
1287 var selected = this.getSelected( (fields.create_from == 'all') );
1288 if(selected.length == 0) return;
1290 var max = selected.length * 3;
1293 fieldmapper.standardRequest(
1294 ['open-ils.acq', 'open-ils.acq.purchase_order.create'],
1297 openils.User.authtoken,
1300 lineitems : selected.map(function(li) { return li.id() }),
1301 create_assets : fields.create_assets[0],
1305 onresponse : function(r) {
1306 var resp = openils.Util.readResponse(r);
1307 self._updateProgressNumbers(resp);
1309 location.href = oilsBasePath + '/eg/acq/po/view/' + resp.purchase_order.id();
1315 this._deleteLiList = function(list, idx) {
1316 if(idx == null) idx = 0;
1317 if(idx >= list.length) return;
1318 var liId = list[idx].id();
1319 fieldmapper.standardRequest(
1320 ['open-ils.acq', 'open-ils.acq.lineitem.delete'],
1322 params: [openils.User.authtoken, liId],
1323 oncomplete: function(r) {
1324 self.removeLineitem(liId);
1325 self._deleteLiList(list, ++idx);
1331 this.editOrderMarc = function(li) {
1333 /* To run in Firefox directly, must set signed.applets.codebase_principal_support
1334 to true in about:config */
1336 if(!openils.XUL.enableXPConnect()) return;
1338 if(openils.XUL.isXUL()) {
1339 win = window.open('/xul/' + openils.XUL.buildId() + '/server/cat/marcedit.xul');
1341 win = window.open('/xul/server/cat/marcedit.xul');
1345 record : {marc : li.marc()},
1347 label: 'Save Record', // XXX I18N
1348 func: function(xmlString) {
1350 fieldmapper.standardRequest(
1351 ['open-ils.acq', 'open-ils.acq.lineitem.update'],
1353 params: [openils.User.authtoken, li],
1354 oncomplete: function(r) {
1355 openils.Util.readResponse(r);
1357 self.drawInfo(li.id())
1366 this._savePl = function(values) {
1368 var selected = this.getSelected( (values.which == 'all') );
1369 openils.Util.show('acq-lit-generic-progress');
1371 if(values.new_name) {
1372 openils.acq.Picklist.create(
1373 {name: values.new_name},
1375 self._updateLiList(id, selected, 0,
1377 location.href = oilsBasePath + '/eg/acq/picklist/view/' + id;
1381 } else if(values.existing_pl) {
1382 // update lineitems to use an existing picklist
1383 self._updateLiList(values.existing_pl, selected, 0,
1385 location.href = oilsBasePath + '/eg/acq/picklist/view/' + values.existing_pl;
1390 this._updateLiState = function(values, state) {
1392 var selected = this.getSelected( (values.which == 'all') );
1393 if(!selected.length) return;
1394 dojo.forEach(selected, function(li) {li.state(state);});
1395 self._updateLiList(null, selected, 0,
1396 // TODO consider inline updates for efficiency
1397 function() { location.href = location.href }
1401 this._updateLiList = function(pl, list, idx, oncomplete) {
1402 if(idx >= list.length) return oncomplete();
1404 if(pl != null) li.picklist(pl);
1405 litGenericProgress.update({maximum: list.length, progress: idx});
1406 new openils.acq.Lineitem({lineitem:li}).update(
1408 self._updateLiList(pl, list, ++idx, oncomplete);
1413 this._loadPLSelect = function() {
1414 if(this._plSelectLoaded) return;
1416 function handleResponse(r) {
1417 plList.push(r.recv().content());
1419 var method = 'open-ils.acq.picklist.user.retrieve';
1420 fieldmapper.standardRequest(
1421 ['open-ils.acq', method],
1423 params: [this.authtoken],
1424 onresponse: handleResponse,
1425 oncomplete: function() {
1426 self._plSelectLoaded = true;
1427 acqLitAddExistingSelect.store =
1428 new dojo.data.ItemFileReadStore({data:acqpl.toStoreData(plList)});
1429 acqLitAddExistingSelect.setValue();
1435 // grab the li-details for this lineitem, grab the linked copies and volumes, add them to the table
1436 this.showRealCopies = function(li) {
1437 while(this.realCopiesTbody.childNodes[0])
1438 this.realCopiesTbody.removeChild(this.realCopiesTbody.childNodes[0]);
1439 this.show('real-copies');
1441 var pcrud = new openils.PermaCrud({authtoken : this.authtoken});
1442 this.realCopyList = [];
1444 var tabIndex = 1000;
1447 acqLitSaveRealCopies.onClick = function() {
1448 self.saveRealCopies();
1451 this._fetchLineitem(li.id(),
1453 li = self.liCache[li.id()] = fullLi;
1457 id : li.lineitem_details().map(
1458 function(item) { return item.eg_copy_id() }
1463 onresponse : function(r) {
1464 var copy = openils.Util.readResponse(r);
1465 var volId = copy.call_number();
1466 var volume = self.volCache[volId];
1468 volume = self.volCache[volId] = pcrud.retrieve('acn', volId);
1470 self.addRealCopy(volume, copy, tabIndex++);
1478 this.addRealCopy = function(volume, copy, tabIndex) {
1479 var row = this.realCopiesRow.cloneNode(true);
1480 this.realCopyList.push(copy);
1484 ['owning_lib', 'location', 'circ_modifier', 'label', 'barcode'],
1487 var isvol = (field == 'owning_lib' || field == 'label');
1488 var widget = new openils.widget.AutoFieldWidget({
1490 fmObject : isvol ? volume : copy,
1491 parentNode : nodeByName(field, row),
1492 readOnly : (field != 'barcode'),
1495 var widgetDrawn = null;
1497 if(field == 'barcode') {
1499 widgetDrawn = function(w, ww) {
1500 var node = w.domNode;
1501 node.setAttribute('tabindex', ''+tabIndex);
1503 // on enter, select the next barcode input
1504 dojo.connect(w, 'onKeyDown',
1506 if(e.keyCode == dojo.keys.ENTER) {
1507 var ti = node.getAttribute('tabindex');
1508 var nextNode = dojo.query('[tabindex=' + String(Number(ti) + 1) + ']', self.realCopiesTbody)[0];
1509 if(nextNode) nextNode.select();
1514 dojo.connect(w, 'onChange',
1516 if(!val || val == copy.barcode()) return;
1517 copy.ischanged(true);
1523 if(self.realCopiesTbody.getElementsByTagName('TR').length == 0)
1528 widget.build(widgetDrawn);
1532 this.realCopiesTbody.appendChild(row);
1533 if(selectNode) selectNode.select();
1536 this.saveRealCopies = function() {
1537 var pcrud = new openils.PermaCrud({authtoken : this.authtoken});
1538 progressDialog.show(true);
1539 var list = this.realCopyList.filter(function(copy) { return copy.ischanged(); });
1540 pcrud.update(list, {oncomplete: function() {
1541 progressDialog.hide();