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');
44 this.authtoken = openils.User.authtoken;
45 this.rowTemplate = this.tbody.removeChild(dojo.byId('acq-lit-row'));
46 this.copyTbody = dojo.byId('acq-lit-li-details-tbody');
47 this.copyRow = this.copyTbody.removeChild(dojo.byId('acq-lit-li-details-row'));
48 this.copyBatchRow = dojo.byId('acq-lit-li-details-batch-row');
49 this.copyBatchWidgets = {};
50 this.liNotesTbody = dojo.byId('acq-lit-notes-tbody');
51 this.liNotesRow = this.liNotesTbody.removeChild(dojo.byId('acq-lit-notes-row'));
52 this.realCopiesTbody = dojo.byId('acq-lit-real-copies-tbody');
53 this.realCopiesRow = this.realCopiesTbody.removeChild(dojo.byId('acq-lit-real-copies-row'));
54 this._copy_fields_for_acqdf = ['owning_lib', 'location'];
56 dojo.connect(acqLitLiActionsSelector, 'onChange',
58 self.applySelectedLiAction(this.attr('value'))
59 acqLitLiActionsSelector.attr('value', '_');
62 acqLitCreatePoSubmit.onClick = function() {
63 acqLitPoCreateDialog.hide();
64 self._createPO(acqLitPoCreateDialog.getValues());
67 acqLitSavePlButton.onClick = function() {
68 acqLitSavePlDialog.hide();
69 self._savePl(acqLitSavePlDialog.getValues());
72 acqLitCancelLiStateButton.onClick = function() {
73 acqLitChangeLiStateDialog.hide();
75 acqLitSaveLiStateButton.onClick = function() {
76 acqLitChangeLiStateDialog.hide();
77 self._updateLiState(acqLitChangeLiStateDialog.getValues(), acqLitChangeLiStateDialog.attr('state'));
81 //dojo.byId('acq-lit-notes-new-button').onclick = function(){acqLitCreateLiNoteDialog.show();}
83 dojo.byId('acq-lit-select-toggle').onclick = function(){self.toggleSelect()};
84 dojo.byId('acq-lit-info-back-button').onclick = function(){self.show('list')};
85 dojo.byId('acq-lit-copies-back-button').onclick = function(){self.show('list')};
86 dojo.byId('acq-lit-notes-back-button').onclick = function(){self.show('list')};
87 dojo.byId('acq-lit-real-copies-back-button').onclick = function(){self.show('list')};
89 this.reset = function() {
90 while(self.tbody.childNodes[0])
91 self.tbody.removeChild(self.tbody.childNodes[0]);
96 this.setNext = function(handler) {
97 var link = dojo.byId('acq-lit-next');
99 dojo.style(link, 'visibility', 'visible');
100 link.onclick = handler;
102 dojo.style(link, 'visibility', 'hidden');
106 this.setPrev = function(handler) {
107 var link = dojo.byId('acq-lit-prev');
109 dojo.style(link, 'visibility', 'visible');
110 link.onclick = handler;
112 dojo.style(link, 'visibility', 'hidden');
116 this.show = function(div) {
117 openils.Util.hide('acq-lit-table-div');
118 openils.Util.hide('acq-lit-info-div');
119 openils.Util.hide('acq-lit-li-details');
120 openils.Util.hide('acq-lit-notes-div');
121 openils.Util.hide('acq-lit-real-copies-div');
124 openils.Util.show('acq-lit-table-div');
127 openils.Util.show('acq-lit-info-div');
130 openils.Util.show('acq-lit-li-details');
133 openils.Util.show('acq-lit-real-copies-div');
136 openils.Util.show('acq-lit-notes-div');
140 openils.Util.show(div);
144 this.hide = function() {
148 this.toggleSelect = function() {
150 dojo.forEach(self.selectors, function(i){i.checked = false});
152 dojo.forEach(self.selectors, function(i){i.checked = true});
153 self.toggleState = !self.toggleState;
157 /** @param all If true, assume all are selected */
158 this.getSelected = function(all) {
160 dojo.forEach(self.selectors,
163 selected.push(self.liCache[i.parentNode.parentNode.getAttribute('li')]);
169 this.setRowAttr = function(td, liWrapper, field, type) {
170 var val = liWrapper.findAttr(field, type || 'lineitem_marc_attr_definition') || '';
171 td.appendChild(document.createTextNode(val));
175 * Inserts a single lineitem into the growing table of lineitems
176 * @param {Object} li The lineitem object to insert
178 this.addLineitem = function(li, skip_final_placement) {
179 this.liCache[li.id()] = li;
181 // sort the lineitem notes on edit_time
182 if(!li.lineitem_notes()) li.lineitem_notes([]);
184 var liWrapper = new openils.acq.Lineitem({lineitem:li});
185 var row = self.rowTemplate.cloneNode(true);
186 row.setAttribute('li', li.id());
187 var tds = dojo.query('[attr]', row);
188 dojo.forEach(tds, function(td) {self.setRowAttr(td, liWrapper, td.getAttribute('attr'), td.getAttribute('attr_type'));});
189 dojo.query('[name=source_label]', row)[0].appendChild(document.createTextNode(li.source_label()));
191 var isbn = liWrapper.findAttr('isbn', 'lineitem_marc_attr_definition');
193 // XXX media prefix for added content
194 dojo.query('[name=jacket]', row)[0].setAttribute('src', '/opac/extras/ac/jacket/small/' + isbn);
197 dojo.query('[attr=title]', row)[0].onclick = function() {self.drawInfo(li.id())};
198 dojo.query('[name=copieslink]', row)[0].onclick = function() {self.drawCopies(li.id())};
199 dojo.query('[name=notes_count]', row)[0].innerHTML = li.lineitem_notes().length;
200 dojo.query('[name=noteslink]', row)[0].onclick = function() {self.drawLiNotes(li)};
202 // show which PO this lineitem is a member of
203 if(li.purchase_order() && !this.isPO) {
205 this.poCache[li.purchase_order()] =
206 this.poCache[li.purchase_order()] ||
207 fieldmapper.standardRequest(
208 ['open-ils.acq', 'open-ils.acq.purchase_order.retrieve'],
210 this.authtoken, li.purchase_order(), {
211 "flesh_price_summary": true,
212 "flesh_lineitem_count": true
215 if(po && !this.isMeta) {
216 openils.Util.show(nodeByName('po', row), 'inline');
217 var link = nodeByName('po_link', row);
218 link.setAttribute('href', oilsBasePath + '/acq/po/view/' + li.purchase_order());
219 link.innerHTML = 'PO: ' + po.name(); // TODO i18n
223 // show which picklist this lineitem is a member of
224 if(li.picklist() && (this.isPO || this.isMeta)) {
226 this.plCache[li.picklist()] =
227 this.plCache[li.picklist()] ||
228 fieldmapper.standardRequest(
229 ['open-ils.acq', 'open-ils.acq.picklist.retrieve'],
230 {params: [this.authtoken, li.picklist()]});
232 openils.Util.show(nodeByName('pl', row), 'inline');
233 var link = nodeByName('pl_link', row);
234 link.setAttribute('href', oilsBasePath + '/acq/picklist/view/' + li.picklist());
235 link.innerHTML = 'PL: '+pl.name(); // TODO i18n
239 var countNode = nodeByName('count', row);
240 var count = li.item_count() || 0;
241 if (typeof(this._copy_count_cb) == "function") {
242 this._copy_count_cb(li.id(), count);
244 countNode.innerHTML = count;
245 countNode.id = 'acq-lit-copy-count-label-' + li.id();
248 nodeByName('li_state', row).innerHTML = li.state(); // TODO i18n state labels
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 // show either "mark received" or "unreceive" as appropriate
256 this.updateLiReceivedness(li, row);
258 if (!skip_final_placement) {
259 self.tbody.appendChild(row);
260 self.selectors.push(dojo.query('[name=selectbox]', row)[0]);
266 this.updateLiReceivedness = function(li, row) {
267 if (typeof(row) == "undefined")
268 row = dojo.query('tr[li="' + li.id() + '"]', "acq-lit-tbody")[0];
270 var recv_link = nodeByName("receive_link", row);
271 var unrecv_link = nodeByName("unreceive_link", row);
272 var real_copies_link = nodeByName("real_copies_link", row);
274 /* handle row coloring for based on LI state */
275 openils.Util.removeCSSClass(row, /^oils-acq-li-state-/);
276 openils.Util.addCSSClass(row, "oils-acq-li-state-" + li.state());
278 /* handle links that appear/disappear based on whether LI is received */
283 openils.Util.hide(real_copies_link);
284 openils.Util.hide(unrecv_link);
285 openils.Util.show(recv_link, "inline");
286 recv_link.onclick = function() {
287 if (self.checkLiAlerts(li.id()))
288 self.issueReceive(li);
292 openils.Util.hide(recv_link);
293 openils.Util.show(unrecv_link, "inline");
294 unrecv_link.onclick = function() {
295 if (confirm(localeStrings.UNRECEIVE_LI))
296 self.issueReceive(li, /* rollback */ true);
298 // TODO we should allow editing before receipt, in which case the
299 // test should be "if 1 or more real (acp) copies exist
300 openils.Util.show(real_copies_link);
301 real_copies_link.onclick = function() {
302 self.showRealCopies(li);
308 openils.Util.hide(recv_link);
309 openils.Util.hide(unrecv_link);
310 openils.Util.hide(real_copies_link);
315 * Draws and shows the lineitem notes pane
317 this.drawLiNotes = function(li) {
321 li.lineitem_notes().sort(
323 if(a.edit_time() < b.edit_time()) return 1;
329 while(this.liNotesTbody.childNodes[0])
330 this.liNotesTbody.removeChild(this.liNotesTbody.childNodes[0]);
333 acqLitCreateLiNoteSubmit.onClick = function() {
334 var value = acqLitCreateNoteText.attr('value');
336 var note = new fieldmapper.acqlin();
339 note.lineitem(li.id());
340 self.updateLiNotes(li, note);
343 dojo.byId('acq-lit-notes-save-button').onclick = function() {
344 self.updateLiNotes(li);
347 dojo.forEach(li.lineitem_notes(), function(note) { self.addLiNote(li, note) });
351 * Draws a single lineitem note in the notes pane
353 this.addLiNote = function(li, note) {
354 if(note.isdeleted()) return;
356 var row = self.liNotesRow.cloneNode(true);
357 dojo.query('[name=value]', row)[0].innerHTML = note.value();
359 dojo.query('[name=delete]', row)[0].onclick = function() {
360 note.isdeleted(true);
361 self.liNotesTbody.removeChild(row);
364 if(note.edit_time()) {
365 dojo.query('[name=edit_time]', row)[0].innerHTML =
366 dojo.date.locale.format(
367 dojo.date.stamp.fromISOString(note.edit_time()),
368 {formatLength:'short'});
371 self.liNotesTbody.appendChild(row);
375 * Updates any new/changed/deleted notes on the server
377 this.updateLiNotes = function(li, newNote) {
383 notes = li.lineitem_notes().filter(
385 if(note.ischanged() || note.isnew() || note.isdeleted())
391 if(notes.length == 0) return;
392 progressDialog.show();
394 fieldmapper.standardRequest(
395 ['open-ils.acq', 'open-ils.acq.lineitem_note.cud.batch'],
397 params : [this.authtoken, notes],
398 onresponse : function(r) {
399 var resp = openils.Util.readResponse(r);
404 // remove the old changed notes
406 dojo.forEach(li.lineitem_notes(),
408 if(!(note.ischanged() || note.isnew() || note.isdeleted()))
412 li.lineitem_notes(list);
415 progressDialog.hide();
416 self.drawLiNotes(li);
420 progressDialog.update(resp);
421 var newnote = resp.note;
423 if(!newnote.isdeleted()) {
424 newnote.isnew(false);
425 newnote.ischanged(false);
426 li.lineitem_notes().push(newnote);
433 this.updateLiPrice = function(input, li) {
435 var price = input.value;
436 var liWrapper = new openils.acq.Lineitem({lineitem:li});
437 var oldPrice = liWrapper.getPrice() || null;
439 if(oldPrice) oldPrice = oldPrice.price;
440 if(price == oldPrice) return;
442 fieldmapper.standardRequest(
443 ['open-ils.acq', 'open-ils.acq.lineitem.price.set'],
445 params : [this.authtoken, li.id(), price],
446 oncomplete : function(r) {
447 openils.Util.readResponse(r);
453 this.removeLineitem = function(liId) {
454 this.tbody.removeChild(dojo.query('[li='+liId+']', this.tbody)[0]);
455 delete this.liCache[liId];
456 //selected.push(self.liCache[i.parentNode.parentNode.getAttribute('li')]);
459 this.drawInfo = function(liId) {
461 openils.acq.Lineitem.fetchAttrDefs(
463 self._fetchLineitem(liId, function(li){self._drawInfo(li);});
468 this._fetchLineitem = function(liId, handler) {
470 var li = this.liCache[liId];
471 if(li && li.marc() && li.lineitem_details())
474 fieldmapper.standardRequest(
475 ['open-ils.acq', 'open-ils.acq.lineitem.retrieve'],
478 params: [self.authtoken, liId, {
480 flesh_li_details: true,
481 flesh_fund_debit: true }],
483 oncomplete: function(r) {
484 var li = openils.Util.readResponse(r);
491 this._drawInfo = function(li) {
493 acqLitEditOrderMarc.onClick = function() { self.editOrderMarc(li); }
496 openils.Util.hide('acq-lit-marc-order-record-label');
497 openils.Util.hide(acqLitEditOrderMarc.domNode);
498 openils.Util.show('acq-lit-marc-real-record-label');
500 openils.Util.show('acq-lit-marc-order-record-label');
501 openils.Util.show(acqLitEditOrderMarc.domNode);
502 openils.Util.hide('acq-lit-marc-real-record-label');
505 this.drawMarcHTML(li);
506 this.infoTbody = dojo.byId('acq-lit-info-tbody');
509 this.infoRow = this.infoTbody.removeChild(dojo.byId('acq-lit-info-row'));
510 while(this.infoTbody.childNodes[0])
511 this.infoTbody.removeChild(this.infoTbody.childNodes[0]);
513 for(var i = 0; i < li.attributes().length; i++) {
514 var attr = li.attributes()[i];
515 var row = this.infoRow.cloneNode(true);
517 var type = attr.attr_type().replace(/lineitem_(.*)_attr_definition/, '$1');
518 var name = openils.acq.Lineitem.attrDefs[type].filter(
520 return (a.code() == attr.attr_name());
522 ).pop().description();
524 dojo.query('[name=label]', row)[0].appendChild(document.createTextNode(name));
525 dojo.query('[name=value]', row)[0].appendChild(document.createTextNode(attr.attr_value()));
526 this.infoTbody.appendChild(row);
530 openils.Util.show('acq-lit-info-cat-link');
531 var link = dojo.byId('acq-lit-info-cat-link').getElementsByTagName('a')[0];
533 if(openils.XUL.isXUL()) {
535 var makeRecTab = function() {
538 {tab_name: localeStrings.XUL_RECORD_DETAIL_PAGE, browser:false},
541 show_nav_buttons : true,
542 show_print_button : true,
543 opac_url : xulG.url_prefix(xulG.urls.opac_rdetail + '?r=' + li.eg_bib_id())
547 link.setAttribute('href', 'javascript:void(0);');
548 link.onclick = makeRecTab;
551 var href = link.getAttribute('href');
553 link.setAttribute('href', href + li.eg_bib_id());
556 openils.Util.hide('acq-lit-info-cat-link');
560 this.drawMarcHTML = function(li) {
561 var params = [null, true, li.marc()];
563 params = [li.eg_bib_id(), true];
565 fieldmapper.standardRequest(
566 ['open-ils.search', 'open-ils.search.biblio.record.html'],
569 oncomplete: function(r) {
570 dojo.byId('acq-lit-marc-div').innerHTML =
571 openils.Util.readResponse(r);
577 this.drawCopies = function(liId) {
581 this.copyWidgetCache = {};
582 this.oldCopyWidgetCache = {};
586 acqLitSaveCopies.onClick = function() { self.saveCopyChanges(liId) };
587 acqLitBatchUpdateCopies.onClick = function() { self.batchCopyUpdate() };
588 acqLitCopyCountInput.attr('value', '0');
590 while(this.copyTbody.childNodes[0])
591 this.copyTbody.removeChild(this.copyTbody.childNodes[0]);
593 this._drawBatchCopyWidgets();
595 this._fetchDistribFormulas(
597 openils.acq.Lineitem.fetchAttrDefs(
599 self._fetchLineitem(liId, function(li){self._drawCopies(li);});
607 * Insert a new row into the distribution formula selection form
609 this._addDistribFormulaRow = function() {
612 if(!self.distribFormulaStore) {
613 // no formulas, hide the form
614 openils.Util.hide('acq-lit-distrib-formula-tbody');
618 if(!this.distribFormulaTemplate)
619 this.distribFormulaTemplate =
620 dojo.byId('acq-lit-distrib-formula-tbody').removeChild(dojo.byId('acq-lit-distrib-form-row'));
622 var row = dojo.byId('acq-lit-distrib-formula-tbody').appendChild(this.distribFormulaTemplate.cloneNode(true));
624 var selector = new dijit.form.FilteringSelect(
625 {store : self.distribFormulaStore},
626 nodeByName('selector', row)
629 var apply = new dijit.form.Button(
630 {"label": localeStrings.APPLY},
631 nodeByName('set_button', row)
634 var reset = new dijit.form.Button(
635 {"label": localeStrings.RESET_FORMULAE, "disabled": true},
636 nodeByName("reset_button", row)
639 dojo.connect(apply, 'onClick',
641 var form_id = selector.attr('value');
643 self._applyDistribFormula(form_id);
644 reset.attr("disabled", false);
648 dojo.connect(reset, 'onClick',
650 self.restoreCopyFieldsBeforeDF();
653 reset.attr("disabled", "true");
660 * Applies a distrib formula to the current set of copies
662 this._applyDistribFormula = function(formula) {
665 formula = this.distribForms.filter(
666 function(form) { return form.id() == formula; }
669 var copyRows = dojo.query('tr', self.copyTbody);
671 if (this.dfeOffset >= copyRows.length) {
672 alert(localeStrings.OUT_OF_COPIES);
676 var entries_applied = 0;
678 var rowIndex = this.dfeOffset;
679 rowIndex < copyRows.length;
683 var row = copyRows[rowIndex];
684 var copy_id = row.getAttribute('copy_id');
685 var copyWidgets = this.copyWidgetCache[copy_id];
686 var entryIndex = this.dfeOffset;
689 // find the correct entry for the current row
690 dojo.forEach(formula.entries(),
693 entryIndex += e.item_count();
694 if(entryIndex > rowIndex)
702 //console.log("rowIndex = " + rowIndex + ", entry = " + entry.id() + ", entryIndex=" +
703 // entryIndex + ", owning_lib = " + entry.owning_lib() + ", location = " + entry.location());
706 this.saveCopyFieldsBeforeDF(copy_id);
707 this._copy_fields_for_acqdf.forEach(
710 copyWidgets[field].attr('value', (entry[field]()));
717 if (entries_applied) {
718 this.dfaCache.push(formula.id());
719 this.dfeOffset += entries_applied;
723 this.saveCopyFieldsBeforeDF = function(copy_id) {
725 if (!this.oldCopyWidgetCache[copy_id]) {
726 var copyWidgets = this.copyWidgetCache[copy_id];
728 this.oldCopyWidgetCache[copy_id] = {};
729 this._copy_fields_for_acqdf.forEach(
731 self.oldCopyWidgetCache[copy_id][f] =
732 copyWidgets[f].attr("value");
738 this.restoreCopyFieldsBeforeDF = function() {
740 for (var copy_id in this.oldCopyWidgetCache) {
741 this._copy_fields_for_acqdf.forEach(
743 self.copyWidgetCache[copy_id][f].attr(
744 "value", self.oldCopyWidgetCache[copy_id][f]
751 this._fetchDistribFormulas = function(onload) {
752 if(this.distribForms) {
756 fieldmapper.standardRequest(
757 ['open-ils.acq', 'open-ils.acq.distribution_formula.ranged.retrieve.atomic'],
759 params: [openils.User.authtoken],
760 oncomplete: function(r) {
761 self.distribForms = openils.Util.readResponse(r);
762 if(!self.distribForms || self.distribForms.length == 0) {
763 self.distribForms = [];
765 self.distribFormulaStore =
766 new dojo.data.ItemFileReadStore(
767 {data:acqdf.toStoreData(self.distribForms)}
770 self._addDistribFormulaRow();
778 this._drawBatchCopyWidgets = function() {
779 var row = this.copyBatchRow;
780 dojo.forEach(liDetailBatchFields,
782 if(self.copyBatchRowDrawn) {
783 self.copyBatchWidgets[field].attr('value', null);
785 var widget = new openils.widget.AutoFieldWidget({
788 parentNode : dojo.query('[name='+field+']', row)[0],
789 orgLimitPerms : ['CREATE_PICKLIST'],
790 dijitArgs : {required:false},
795 self.copyBatchWidgets[field] = w;
801 this.copyBatchRowDrawn = true;
804 this.batchCopyUpdate = function() {
806 for(var k in this.copyWidgetCache) {
807 var cache = this.copyWidgetCache[k];
808 dojo.forEach(liDetailBatchFields, function(f) {
809 var newval = self.copyBatchWidgets[f].attr('value');
810 if(newval) cache[f].attr('value', newval);
815 this._drawCopies = function(li) {
818 // this button sets the total number of copies for a given lineitem
819 acqLitAddCopyCount.onClick = function() {
820 var count = acqLitCopyCountInput.attr('value');
823 while(self.copyCount() < count)
826 // delete rows if necessary
827 var diff = self.copyCount() - count;
829 var rows = dojo.query('tr', self.copyTbody).reverse().slice(0, diff);
830 if(confirm(dojo.string.substitute(localeStrings.DELETE_LI_COPIES_CONFIRM, [diff]))) {
831 dojo.forEach(rows, function(row) {self.deleteCopy(row); });
833 acqLitCopyCountInput.attr('value', self.copyCount()+'');
839 if(li.lineitem_details().length > 0) {
840 dojo.forEach(li.lineitem_details(),
842 self.addCopy(li, copy);
850 this.copyCount = function() {
852 for(var id in this.copyCache) {
853 if(!this.copyCache[id].isdeleted())
859 this.virtCopyId = -1;
860 this.addCopy = function(li, copy) {
861 var row = this.copyRow.cloneNode(true);
862 this.copyTbody.appendChild(row);
866 copy = new fieldmapper.acqlid();
868 copy.id(this.virtCopyId--);
869 copy.lineitem(li.id());
872 this.copyCache[copy.id()] = copy;
873 row.setAttribute('copy_id', copy.id());
874 self.copyWidgetCache[copy.id()] = {};
876 acqLitCopyCountInput.attr('value', self.copyCount()+'');
878 dojo.forEach(liDetailFields,
880 var widget = new openils.widget.AutoFieldWidget({
884 parentNode : dojo.query('[name='+field+']', row)[0],
885 orgLimitPerms : ['CREATE_PICKLIST', 'CREATE_PURCHASE_ORDER'],
886 readOnly : Boolean(copy.eg_copy_id())
889 // make sure we capture the value from any async widgets
891 copy[field](ww.getFormattedValue())
892 self.copyWidgetCache[copy.id()][field] = w;
895 dojo.connect(widget.widget, 'onChange',
897 if(copy.isnew() || val != copy[field]()) {
898 // prevent setting ischanged() automatically on widget load for existing copies
899 copy[field](widget.getFormattedValue())
900 copy.ischanged(true);
907 this.updateLidReceivedness(copy, row);
910 this.updateLidReceivedness = function(copy, row) {
911 if (typeof(row) == "undefined") {
913 'tr[copy_id="' + copy.id() + '"]', this.copyTbody
918 var recv_link = nodeByName("receive", row);
919 var unrecv_link = nodeByName("unreceive", row);
920 var del_link = nodeByName("delete", row);
923 openils.Util.hide(del_link.parentNode);
925 /* Avoid showing (un)receive links for virtual copies */
927 if(copy.recv_time()) {
928 openils.Util.hide(recv_link);
929 openils.Util.show(unrecv_link);
930 unrecv_link.onclick = function() {
931 if (confirm(localeStrings.UNRECEIVE_LID))
932 self.issueReceive(copy, /* rollback */ true);
935 openils.Util.hide(unrecv_link);
936 openils.Util.show(recv_link);
937 recv_link.onclick = function() {
938 if (self.checkLiAlerts(copy.lineitem()))
939 self.issueReceive(copy);
943 openils.Util.hide(unrecv_link);
944 openils.Util.hide(recv_link);
947 openils.Util.hide(unrecv_link);
948 openils.Util.hide(recv_link);
950 del_link.onclick = function() { self.deleteCopy(row) };
951 openils.Util.show(del_link.parentNode);
955 this._confirmAlert = function(li, lin) {
957 dojo.string.substitute(
958 localeStrings.CONFIRM_LI_ALERT, [
959 (new openils.acq.Lineitem({"lineitem": li})).findAttr(
960 "title", "lineitem_marc_attr_definition"
962 lin.alert_text().description(), lin.value()
968 this.checkLiAlerts = function(li_id) {
969 var li = this.liCache[li_id];
971 var alert_notes = li.lineitem_notes().filter(
972 function(o) { return Boolean(o.alert_text()); }
975 /* this is _intentionally_ not done in a call to forEach() ... */
976 for (var i = 0; i < alert_notes.length; i++) {
977 if (this.noteAcks[alert_notes[i].id()])
979 else if (!this._confirmAlert(li, alert_notes[i]))
982 this.noteAcks[alert_notes[i].id()] = true;
988 this.deleteCopy = function(row) {
989 var copy = this.copyCache[row.getAttribute('copy_id')];
990 copy.isdeleted(true);
992 delete this.copyCache[copy.id()];
993 this.copyTbody.removeChild(row);
996 this.saveCopyChanges = function(liId) {
1002 for(var id in this.copyCache) {
1003 var c = this.copyCache[id];
1004 if(!c.isdeleted()) total++;
1005 if(c.isnew() || c.ischanged() || c.isdeleted()) {
1006 if(c.id() < 0) c.id(null);
1011 if (typeof(this._copy_count_cb) == "function") {
1012 this._copy_count_cb(liId, total);
1015 dojo.byId('acq-lit-copy-count-label-' + liId).innerHTML = total;
1018 if (copies.length > 0) {
1019 openils.Util.show("acq-lit-update-copies-progress");
1020 fieldmapper.standardRequest(
1021 ['open-ils.acq', 'open-ils.acq.lineitem_detail.cud.batch'],
1023 params: [openils.User.authtoken, copies],
1024 onresponse: function(r) {
1025 var res = openils.Util.readResponse(r);
1026 litUpdateCopiesProgress.update(res);
1028 oncomplete: function() {
1029 self.drawCopies(liId);
1030 openils.Util.hide("acq-lit-update-copies-progress");
1036 if (this.dfaCache.length > 0) {
1037 var oldlength = this.dfaCache.length;
1039 fieldmapper.standardRequest(
1041 "open-ils.acq.distribution_formula.record_application"],
1044 "params": [openils.User.authtoken, this.dfaCache, liId],
1045 "onresponse": function(r) {
1046 var res = openils.Util.readResponse(r);
1047 if (res && res.length != oldlength)
1048 alert(localeStrings.DFA_NOT_ALL);
1056 this.applySelectedLiAction = function(action) {
1060 case 'delete_selected':
1061 this._deleteLiList(self.getSelected());
1064 case 'create_order':
1066 if(!this.createPoProviderSelector) {
1067 var widget = new openils.widget.AutoFieldWidget({
1068 fmField : 'provider',
1070 searchFilter: {"active": "t"},
1071 parentNode : dojo.byId('acq-lit-po-provider'),
1074 function(w) { self.createPoProviderSelector = w; }
1078 if(!this.createPoAgencySelector) {
1079 var widget = new openils.widget.AutoFieldWidget({
1080 fmField : 'ordering_agency',
1082 parentNode : dojo.byId('acq-lit-po-agency'),
1083 orgLimitPerms : ['CREATE_PURCHASE_ORDER'],
1086 function(w) { self.createPoAgencySelector = w; }
1091 acqLitPoCreateDialog.show();
1094 case 'save_picklist':
1095 this._loadPLSelect();
1096 acqLitSavePlDialog.show();
1099 case 'selector_ready':
1101 acqLitChangeLiStateDialog.attr('state', action.replace('_', '-'));
1102 acqLitChangeLiStateDialog.show();
1113 case 'rollback_receive_po':
1114 this.rollbackPoReceive();
1117 case 'create_assets':
1118 this.createAssets();
1121 case 'export_attr_list':
1122 this.chooseExportAttr();
1125 case 'add_brief_record':
1127 location.href = oilsBasePath + '/acq/picklist/brief_record?po=' + this.isPO;
1129 location.href = oilsBasePath + '/acq/picklist/brief_record?pl=' + this.isPL;
1133 this.createAssets = function() {
1134 if(!this.isPO) return;
1135 if(!confirm(localeStrings.CREATE_PO_ASSETS_CONFIRM)) return;
1136 this.show('acq-lit-progress-numbers');
1138 fieldmapper.standardRequest(
1139 ['open-ils.acq', 'open-ils.acq.purchase_order.assets.create'],
1141 params: [this.authtoken, this.isPO],
1142 onresponse: function(r) {
1143 var resp = openils.Util.readResponse(r);
1144 self._updateProgressNumbers(resp, true);
1150 this.chooseExportAttr = function() {
1151 if (!acqLitExportAttrSelector._li_setup) {
1153 acqLitExportAttrSelector.store = new dojo.data.ItemFileReadStore(
1155 "data": acqliad.toStoreData(
1156 (new openils.PermaCrud()).search(
1157 "acqliad", {"code": li_exportable_attrs}
1162 acqLitExportAttrSelector.setValue();
1163 acqLitExportAttrButton.onClick = function(){self.exportAttrList();};
1164 acqLitExportAttrSelector._li_setup = true;
1166 openils.Util.show("acq-lit-export-attr-holder", "inline");
1169 this.exportAttrList = function() {
1170 var attr_def = acqLitExportAttrSelector.item;
1171 var li_list = this.getSelected();
1172 var value_list = li_list.map(
1174 return (new openils.acq.Lineitem({"lineitem": li})).findAttr(
1175 attr_def.code, "lineitem_marc_attr_definition"
1178 ).filter(function(attr) { return Boolean(attr); });
1180 if (value_list.length > 0) {
1181 if (value_list.length < li_list.length) {
1183 dojo.string.substitute(
1184 localeStrings.EXPORT_SHORT_LIST, [attr_def.description]
1191 openils.XUL.contentToFileSaveDialog(
1192 value_list.join("\n"),
1193 localeStrings.EXPORT_SAVE_DIALOG_TITLE
1199 alert(dojo.string.substitute(
1200 localeStrings.EXPORT_EMPTY_LIST, [attr_def.description]
1204 openils.Util.hide("acq-lit-export-attr-holder");
1207 this.printPO = function() {
1208 if(!this.isPO) return;
1209 progressDialog.show(true);
1210 fieldmapper.standardRequest(
1211 ['open-ils.acq', 'open-ils.acq.purchase_order.format'],
1213 params: [this.authtoken, this.isPO, 'html'],
1214 oncomplete: function(r) {
1215 progressDialog.hide();
1216 var evt = openils.Util.readResponse(r);
1217 if(evt && evt.template_output()) {
1218 win = window.open('','', 'resizable,width=800,height=600,scrollbars=1');
1219 win.document.body.innerHTML = evt.template_output().data();
1227 this.receivePO = function() {
1228 if (!this.isPO) return;
1230 for (var id in this.liCache) {
1231 /* assumption: liCache reflects exactly the
1232 * set of LIs that belong to our PO */
1233 if (this.liCache[id].state() != "received" &&
1234 !this.checkLiAlerts(id)) return;
1237 this.show('acq-lit-progress-numbers');
1239 fieldmapper.standardRequest(
1240 ['open-ils.acq', 'open-ils.acq.purchase_order.receive'],
1242 params: [this.authtoken, this.isPO],
1243 onresponse : function(r) {
1244 var resp = openils.Util.readResponse(r);
1245 self._updateProgressNumbers(resp, true);
1251 this.issueReceive = function(obj, rollback) {
1252 /* (For now) there shall be no marking LI or LIDs (un)received
1253 * except from the actual "view PO" interface. */
1254 if (!this.isPO) return;
1257 {"jub": "lineitem", "acqlid": "lineitem_detail"}[obj.classname];
1259 "open-ils.acq." + part + ".receive" + (rollback ? ".rollback" : "");
1261 progressDialog.show(true);
1262 fieldmapper.standardRequest(
1263 ["open-ils.acq", method], {
1265 "params": [this.authtoken, obj.id()],
1266 "onresponse": function(r) {
1267 self.handleReceive(openils.Util.readResponse(r));
1269 "oncomplete": function() { progressDialog.hide(); }
1275 * Handles the responses from receive and rollback ML calls.
1277 this.handleReceive = function(resp) {
1280 for (var li_id in resp.li) {
1281 for (var key in resp.li[li_id])
1282 self.liCache[li_id][key](resp.li[li_id][key]);
1283 self.updateLiReceivedness(self.liCache[li_id]);
1287 if (typeof(self.poUpdateCallback) == "function")
1288 self.poUpdateCallback(resp.po);
1291 for (var lid_id in resp.lid) {
1292 for (var key in resp.lid[lid_id])
1293 self.copyCache[lid_id][key](resp.lid[lid_id][key]);
1294 self.updateLidReceivedness(self.copyCache[lid_id]);
1300 this.rollbackPoReceive = function() {
1301 if(!this.isPO) return;
1302 if(!confirm(localeStrings.ROLLBACK_PO_RECEIVE_CONFIRM)) return;
1303 this.show('acq-lit-progress-numbers');
1305 fieldmapper.standardRequest(
1306 ['open-ils.acq', 'open-ils.acq.purchase_order.receive.rollback'],
1308 params: [this.authtoken, this.isPO],
1309 onresponse : function(r) {
1310 var resp = openils.Util.readResponse(r);
1311 self._updateProgressNumbers(resp, true);
1317 this._updateProgressNumbers = function(resp, reloadOnComplete) {
1319 dojo.byId('acq-pl-lit-li-processed').innerHTML = resp.li;
1320 dojo.byId('acq-pl-lit-lid-processed').innerHTML = resp.lid;
1321 dojo.byId('acq-pl-lit-debits-processed').innerHTML = resp.debits_accrued;
1322 dojo.byId('acq-pl-lit-bibs-processed').innerHTML = resp.bibs;
1323 dojo.byId('acq-pl-lit-indexed-processed').innerHTML = resp.indexed;
1324 dojo.byId('acq-pl-lit-copies-processed').innerHTML = resp.copies;
1325 if(resp.complete && reloadOnComplete)
1326 location.href = location.href;
1330 this._createPO = function(fields) {
1331 this.show('acq-lit-progress-numbers');
1332 var po = new fieldmapper.acqpo();
1333 po.provider(this.createPoProviderSelector.attr('value'));
1334 po.ordering_agency(this.createPoAgencySelector.attr('value'));
1336 var selected = this.getSelected( (fields.create_from == 'all') );
1337 if(selected.length == 0) return;
1339 var max = selected.length * 3;
1342 fieldmapper.standardRequest(
1343 ['open-ils.acq', 'open-ils.acq.purchase_order.create'],
1346 openils.User.authtoken,
1349 lineitems : selected.map(function(li) { return li.id() }),
1350 create_assets : fields.create_assets[0],
1354 onresponse : function(r) {
1355 var resp = openils.Util.readResponse(r);
1356 self._updateProgressNumbers(resp);
1358 location.href = oilsBasePath + '/eg/acq/po/view/' + resp.purchase_order.id();
1364 this._deleteLiList = function(list, idx) {
1365 if(idx == null) idx = 0;
1366 if(idx >= list.length) return;
1367 var liId = list[idx].id();
1368 fieldmapper.standardRequest(
1369 ['open-ils.acq', 'open-ils.acq.lineitem.delete'],
1371 params: [openils.User.authtoken, liId],
1372 oncomplete: function(r) {
1373 self.removeLineitem(liId);
1374 self._deleteLiList(list, ++idx);
1380 this.editOrderMarc = function(li) {
1382 /* To run in Firefox directly, must set signed.applets.codebase_principal_support
1383 to true in about:config */
1385 if(!openils.XUL.enableXPConnect()) return;
1387 if(openils.XUL.isXUL()) {
1388 win = window.open('/xul/' + openils.XUL.buildId() + '/server/cat/marcedit.xul');
1390 win = window.open('/xul/server/cat/marcedit.xul');
1394 record : {marc : li.marc()},
1396 label: 'Save Record', // XXX I18N
1397 func: function(xmlString) {
1399 fieldmapper.standardRequest(
1400 ['open-ils.acq', 'open-ils.acq.lineitem.update'],
1402 params: [openils.User.authtoken, li],
1403 oncomplete: function(r) {
1404 openils.Util.readResponse(r);
1406 self.drawInfo(li.id())
1415 this._savePl = function(values) {
1417 var selected = this.getSelected( (values.which == 'all') );
1418 openils.Util.show('acq-lit-generic-progress');
1420 if(values.new_name) {
1421 openils.acq.Picklist.create(
1422 {name: values.new_name},
1424 self._updateLiList(id, selected, 0,
1426 location.href = oilsBasePath + '/eg/acq/picklist/view/' + id;
1430 } else if(values.existing_pl) {
1431 // update lineitems to use an existing picklist
1432 self._updateLiList(values.existing_pl, selected, 0,
1434 location.href = oilsBasePath + '/eg/acq/picklist/view/' + values.existing_pl;
1439 this._updateLiState = function(values, state) {
1441 var selected = this.getSelected( (values.which == 'all') );
1442 if(!selected.length) return;
1443 dojo.forEach(selected, function(li) {li.state(state);});
1444 self._updateLiList(null, selected, 0,
1445 // TODO consider inline updates for efficiency
1446 function() { location.href = location.href }
1450 this._updateLiList = function(pl, list, idx, oncomplete) {
1451 if(idx >= list.length) return oncomplete();
1453 if(pl != null) li.picklist(pl);
1454 litGenericProgress.update({maximum: list.length, progress: idx});
1455 new openils.acq.Lineitem({lineitem:li}).update(
1457 self._updateLiList(pl, list, ++idx, oncomplete);
1462 this._loadPLSelect = function() {
1463 if(this._plSelectLoaded) return;
1465 function handleResponse(r) {
1466 plList.push(r.recv().content());
1468 var method = 'open-ils.acq.picklist.user.retrieve';
1469 fieldmapper.standardRequest(
1470 ['open-ils.acq', method],
1472 params: [this.authtoken],
1473 onresponse: handleResponse,
1474 oncomplete: function() {
1475 self._plSelectLoaded = true;
1476 acqLitAddExistingSelect.store =
1477 new dojo.data.ItemFileReadStore({data:acqpl.toStoreData(plList)});
1478 acqLitAddExistingSelect.setValue();
1484 // grab the li-details for this lineitem, grab the linked copies and volumes, add them to the table
1485 this.showRealCopies = function(li) {
1486 while(this.realCopiesTbody.childNodes[0])
1487 this.realCopiesTbody.removeChild(this.realCopiesTbody.childNodes[0]);
1488 this.show('real-copies');
1490 var pcrud = new openils.PermaCrud({authtoken : this.authtoken});
1491 this.realCopyList = [];
1493 var tabIndex = 1000;
1496 acqLitSaveRealCopies.onClick = function() {
1497 self.saveRealCopies();
1500 this._fetchLineitem(li.id(),
1502 li = self.liCache[li.id()] = fullLi;
1506 id : li.lineitem_details().map(
1507 function(item) { return item.eg_copy_id() }
1512 onresponse : function(r) {
1513 var copy = openils.Util.readResponse(r);
1514 var volId = copy.call_number();
1515 var volume = self.volCache[volId];
1517 volume = self.volCache[volId] = pcrud.retrieve('acn', volId);
1519 self.addRealCopy(volume, copy, tabIndex++);
1527 this.addRealCopy = function(volume, copy, tabIndex) {
1528 var row = this.realCopiesRow.cloneNode(true);
1529 this.realCopyList.push(copy);
1533 ['owning_lib', 'location', 'circ_modifier', 'label', 'barcode'],
1536 var isvol = (field == 'owning_lib' || field == 'label');
1537 var widget = new openils.widget.AutoFieldWidget({
1539 fmObject : isvol ? volume : copy,
1540 parentNode : nodeByName(field, row),
1541 readOnly : (field != 'barcode'),
1544 var widgetDrawn = null;
1546 if(field == 'barcode') {
1548 widgetDrawn = function(w, ww) {
1549 var node = w.domNode;
1550 node.setAttribute('tabindex', ''+tabIndex);
1552 // on enter, select the next barcode input
1553 dojo.connect(w, 'onKeyDown',
1555 if(e.keyCode == dojo.keys.ENTER) {
1556 var ti = node.getAttribute('tabindex');
1557 var nextNode = dojo.query('[tabindex=' + String(Number(ti) + 1) + ']', self.realCopiesTbody)[0];
1558 if(nextNode) nextNode.select();
1563 dojo.connect(w, 'onChange',
1565 if(!val || val == copy.barcode()) return;
1566 copy.ischanged(true);
1572 if(self.realCopiesTbody.getElementsByTagName('TR').length == 0)
1577 widget.build(widgetDrawn);
1581 this.realCopiesTbody.appendChild(row);
1582 if(selectNode) selectNode.select();
1585 this.saveRealCopies = function() {
1586 var pcrud = new openils.PermaCrud({authtoken : this.authtoken});
1587 progressDialog.show(true);
1588 var list = this.realCopyList.filter(function(copy) { return copy.ischanged(); });
1589 pcrud.update(list, {oncomplete: function() {
1590 progressDialog.hide();