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';
23 function nodeByName(name, context) {
24 return dojo.query('[name='+name+']', context)[0];
28 var liDetailBatchFields = ['fund', 'owning_lib', 'location', 'collection_code', 'circ_modifier', 'cn_label'];
29 var liDetailFields = liDetailBatchFields.concat(['barcode', 'note']);
31 function AcqLiTable() {
39 this.toggleState = false;
40 this.tbody = dojo.byId('acq-lit-tbody');
42 this.authtoken = openils.User.authtoken;
43 this.rowTemplate = this.tbody.removeChild(dojo.byId('acq-lit-row'));
44 this.copyTbody = dojo.byId('acq-lit-li-details-tbody');
45 this.copyRow = this.copyTbody.removeChild(dojo.byId('acq-lit-li-details-row'));
46 this.copyBatchRow = dojo.byId('acq-lit-li-details-batch-row');
47 this.copyBatchWidgets = {};
48 this.liNotesTbody = dojo.byId('acq-lit-notes-tbody');
49 this.liNotesRow = this.liNotesTbody.removeChild(dojo.byId('acq-lit-notes-row'));
50 this.realCopiesTbody = dojo.byId('acq-lit-real-copies-tbody');
51 this.realCopiesRow = this.realCopiesTbody.removeChild(dojo.byId('acq-lit-real-copies-row'));
52 this._copy_fields_for_acqdf = ['owning_lib', 'location'];
54 dojo.connect(acqLitLiActionsSelector, 'onChange',
56 self.applySelectedLiAction(this.attr('value'))
57 acqLitLiActionsSelector.attr('value', '_');
60 acqLitCreatePoSubmit.onClick = function() {
61 acqLitPoCreateDialog.hide();
62 self._createPO(acqLitPoCreateDialog.getValues());
65 acqLitSavePlButton.onClick = function() {
66 acqLitSavePlDialog.hide();
67 self._savePl(acqLitSavePlDialog.getValues());
70 acqLitCancelLiStateButton.onClick = function() {
71 acqLitChangeLiStateDialog.hide();
73 acqLitSaveLiStateButton.onClick = function() {
74 acqLitChangeLiStateDialog.hide();
75 self._updateLiState(acqLitChangeLiStateDialog.getValues(), acqLitChangeLiStateDialog.attr('state'));
79 //dojo.byId('acq-lit-notes-new-button').onclick = function(){acqLitCreateLiNoteDialog.show();}
81 dojo.byId('acq-lit-select-toggle').onclick = function(){self.toggleSelect()};
82 dojo.byId('acq-lit-info-back-button').onclick = function(){self.show('list')};
83 dojo.byId('acq-lit-copies-back-button').onclick = function(){self.show('list')};
84 dojo.byId('acq-lit-notes-back-button').onclick = function(){self.show('list')};
85 dojo.byId('acq-lit-real-copies-back-button').onclick = function(){self.show('list')};
87 this.reset = function() {
88 while(self.tbody.childNodes[0])
89 self.tbody.removeChild(self.tbody.childNodes[0]);
93 this.setNext = function(handler) {
94 var link = dojo.byId('acq-lit-next');
96 dojo.style(link, 'visibility', 'visible');
97 link.onclick = handler;
99 dojo.style(link, 'visibility', 'hidden');
103 this.setPrev = function(handler) {
104 var link = dojo.byId('acq-lit-prev');
106 dojo.style(link, 'visibility', 'visible');
107 link.onclick = handler;
109 dojo.style(link, 'visibility', 'hidden');
113 this.show = function(div) {
114 openils.Util.hide('acq-lit-table-div');
115 openils.Util.hide('acq-lit-info-div');
116 openils.Util.hide('acq-lit-li-details');
117 openils.Util.hide('acq-lit-notes-div');
118 openils.Util.hide('acq-lit-real-copies-div');
121 openils.Util.show('acq-lit-table-div');
124 openils.Util.show('acq-lit-info-div');
127 openils.Util.show('acq-lit-li-details');
130 openils.Util.show('acq-lit-real-copies-div');
133 openils.Util.show('acq-lit-notes-div');
137 openils.Util.show(div);
141 this.hide = function() {
145 this.toggleSelect = function() {
147 dojo.forEach(self.selectors, function(i){i.checked = false});
149 dojo.forEach(self.selectors, function(i){i.checked = true});
150 self.toggleState = !self.toggleState;
154 /** @param all If true, assume all are selected */
155 this.getSelected = function(all) {
157 dojo.forEach(self.selectors,
160 selected.push(self.liCache[i.parentNode.parentNode.getAttribute('li')]);
166 this.setRowAttr = function(td, liWrapper, field, type) {
167 var val = liWrapper.findAttr(field, type || 'lineitem_marc_attr_definition') || '';
168 td.appendChild(document.createTextNode(val));
172 * Inserts a single lineitem into the growing table of lineitems
173 * @param {Object} li The lineitem object to insert
175 this.addLineitem = function(li, skip_final_placement) {
176 this.liCache[li.id()] = li;
178 // sort the lineitem notes on edit_time
179 if(!li.lineitem_notes()) li.lineitem_notes([]);
181 var liWrapper = new openils.acq.Lineitem({lineitem:li});
182 var row = self.rowTemplate.cloneNode(true);
183 row.setAttribute('li', li.id());
184 var tds = dojo.query('[attr]', row);
185 dojo.forEach(tds, function(td) {self.setRowAttr(td, liWrapper, td.getAttribute('attr'), td.getAttribute('attr_type'));});
186 dojo.query('[name=source_label]', row)[0].appendChild(document.createTextNode(li.source_label()));
188 var isbn = liWrapper.findAttr('isbn', 'lineitem_marc_attr_definition');
190 // XXX media prefix for added content
191 dojo.query('[name=jacket]', row)[0].setAttribute('src', '/opac/extras/ac/jacket/small/' + isbn);
194 dojo.query('[attr=title]', row)[0].onclick = function() {self.drawInfo(li.id())};
195 dojo.query('[name=copieslink]', row)[0].onclick = function() {self.drawCopies(li.id())};
196 dojo.query('[name=notes_count]', row)[0].innerHTML = li.lineitem_notes().length;
197 dojo.query('[name=noteslink]', row)[0].onclick = function() {self.drawLiNotes(li)};
199 // show which PO this lineitem is a member of
200 if(li.purchase_order() && !this.isPO) {
202 this.poCache[li.purchase_order()] =
203 this.poCache[li.purchase_order()] ||
204 fieldmapper.standardRequest(
205 ['open-ils.acq', 'open-ils.acq.purchase_order.retrieve'],
207 this.authtoken, li.purchase_order(), {
208 "flesh_price_summary": true,
209 "flesh_lineitem_count": true
212 if(po && !this.isMeta) {
213 openils.Util.show(nodeByName('po', row), 'inline');
214 var link = nodeByName('po_link', row);
215 link.setAttribute('href', oilsBasePath + '/acq/po/view/' + li.purchase_order());
216 link.innerHTML = 'PO: ' + po.name(); // TODO i18n
220 // show which picklist this lineitem is a member of
221 if(li.picklist() && (this.isPO || this.isMeta)) {
223 this.plCache[li.picklist()] =
224 this.plCache[li.picklist()] ||
225 fieldmapper.standardRequest(
226 ['open-ils.acq', 'open-ils.acq.picklist.retrieve'],
227 {params: [this.authtoken, li.picklist()]});
229 openils.Util.show(nodeByName('pl', row), 'inline');
230 var link = nodeByName('pl_link', row);
231 link.setAttribute('href', oilsBasePath + '/acq/picklist/view/' + li.picklist());
232 link.innerHTML = 'PL: '+pl.name(); // TODO i18n
236 var countNode = nodeByName('count', row);
237 var count = li.item_count() || 0;
238 if (typeof(this._copy_count_cb) == "function") {
239 this._copy_count_cb(li.id(), count);
241 countNode.innerHTML = count;
242 countNode.id = 'acq-lit-copy-count-label-' + li.id();
245 nodeByName('li_state', row).innerHTML = li.state(); // TODO i18n state labels
246 openils.Util.addCSSClass(row, 'oils-acq-li-state-' + li.state());
249 var priceInput = dojo.query('[name=price]', row)[0];
250 var priceData = liWrapper.getPrice();
251 priceInput.value = (priceData) ? priceData.price : '';
252 priceInput.onchange = function() { self.updateLiPrice(priceInput, li) };
254 var recv_link = dojo.query('[name=receive_link]', row)[0];
256 if(li.state() == 'on-order') {
257 recv_link.onclick = function() {
259 openils.Util.hide(recv_link)
262 openils.Util.hide(recv_link);
265 // TODO we should allow editing before receipt, in which case the
266 // test should be "if 1 or more real (acp) copies exist
267 if(li.state() == 'received') {
268 var real_copies_link = dojo.query('[name=real_copies_link]', row)[0];
269 openils.Util.show(real_copies_link);
270 real_copies_link.onclick = function() {
271 self.showRealCopies(li);
275 if (!skip_final_placement) {
276 self.tbody.appendChild(row);
277 self.selectors.push(dojo.query('[name=selectbox]', row)[0]);
284 * Draws and shows the lineitem notes pane
286 this.drawLiNotes = function(li) {
290 li.lineitem_notes().sort(
292 if(a.edit_time() < b.edit_time()) return 1;
298 while(this.liNotesTbody.childNodes[0])
299 this.liNotesTbody.removeChild(this.liNotesTbody.childNodes[0]);
302 acqLitCreateLiNoteSubmit.onClick = function() {
303 var value = acqLitCreateNoteText.attr('value');
305 var note = new fieldmapper.acqlin();
308 note.lineitem(li.id());
309 self.updateLiNotes(li, note);
312 dojo.byId('acq-lit-notes-save-button').onclick = function() {
313 self.updateLiNotes(li);
316 dojo.forEach(li.lineitem_notes(), function(note) { self.addLiNote(li, note) });
320 * Draws a single lineitem note in the notes pane
322 this.addLiNote = function(li, note) {
323 if(note.isdeleted()) return;
325 var row = self.liNotesRow.cloneNode(true);
326 dojo.query('[name=value]', row)[0].innerHTML = note.value();
328 dojo.query('[name=delete]', row)[0].onclick = function() {
329 note.isdeleted(true);
330 self.liNotesTbody.removeChild(row);
333 if(note.edit_time()) {
334 dojo.query('[name=edit_time]', row)[0].innerHTML =
335 dojo.date.locale.format(
336 dojo.date.stamp.fromISOString(note.edit_time()),
337 {formatLength:'short'});
340 self.liNotesTbody.appendChild(row);
344 * Updates any new/changed/deleted notes on the server
346 this.updateLiNotes = function(li, newNote) {
352 notes = li.lineitem_notes().filter(
354 if(note.ischanged() || note.isnew() || note.isdeleted())
360 if(notes.length == 0) return;
361 progressDialog.show();
363 fieldmapper.standardRequest(
364 ['open-ils.acq', 'open-ils.acq.lineitem_note.cud.batch'],
366 params : [this.authtoken, notes],
367 onresponse : function(r) {
368 var resp = openils.Util.readResponse(r);
373 // remove the old changed notes
375 dojo.forEach(li.lineitem_notes(),
377 if(!(note.ischanged() || note.isnew() || note.isdeleted()))
381 li.lineitem_notes(list);
384 progressDialog.hide();
385 self.drawLiNotes(li);
389 progressDialog.update(resp);
390 var newnote = resp.note;
392 if(!newnote.isdeleted()) {
393 newnote.isnew(false);
394 newnote.ischanged(false);
395 li.lineitem_notes().push(newnote);
402 this.updateLiPrice = function(input, li) {
404 var price = input.value;
405 var liWrapper = new openils.acq.Lineitem({lineitem:li});
406 var oldPrice = liWrapper.getPrice() || null;
408 if(oldPrice) oldPrice = oldPrice.price;
409 if(price == oldPrice) return;
411 fieldmapper.standardRequest(
412 ['open-ils.acq', 'open-ils.acq.lineitem.price.set'],
414 params : [this.authtoken, li.id(), price],
415 oncomplete : function(r) {
416 openils.Util.readResponse(r);
422 this.removeLineitem = function(liId) {
423 this.tbody.removeChild(dojo.query('[li='+liId+']', this.tbody)[0]);
424 delete this.liCache[liId];
425 //selected.push(self.liCache[i.parentNode.parentNode.getAttribute('li')]);
428 this.drawInfo = function(liId) {
430 openils.acq.Lineitem.fetchAttrDefs(
432 self._fetchLineitem(liId, function(li){self._drawInfo(li);});
437 this._fetchLineitem = function(liId, handler) {
439 var li = this.liCache[liId];
440 if(li && li.marc() && li.lineitem_details())
443 fieldmapper.standardRequest(
444 ['open-ils.acq', 'open-ils.acq.lineitem.retrieve'],
447 params: [self.authtoken, liId, {
449 flesh_li_details: true,
450 flesh_fund_debit: true }],
452 oncomplete: function(r) {
453 var li = openils.Util.readResponse(r);
460 this._drawInfo = function(li) {
462 acqLitEditOrderMarc.onClick = function() { self.editOrderMarc(li); }
465 openils.Util.hide('acq-lit-marc-order-record-label');
466 openils.Util.hide(acqLitEditOrderMarc.domNode);
467 openils.Util.show('acq-lit-marc-real-record-label');
469 openils.Util.show('acq-lit-marc-order-record-label');
470 openils.Util.show(acqLitEditOrderMarc.domNode);
471 openils.Util.hide('acq-lit-marc-real-record-label');
474 this.drawMarcHTML(li);
475 this.infoTbody = dojo.byId('acq-lit-info-tbody');
478 this.infoRow = this.infoTbody.removeChild(dojo.byId('acq-lit-info-row'));
479 while(this.infoTbody.childNodes[0])
480 this.infoTbody.removeChild(this.infoTbody.childNodes[0]);
482 for(var i = 0; i < li.attributes().length; i++) {
483 var attr = li.attributes()[i];
484 var row = this.infoRow.cloneNode(true);
486 var type = attr.attr_type().replace(/lineitem_(.*)_attr_definition/, '$1');
487 var name = openils.acq.Lineitem.attrDefs[type].filter(
489 return (a.code() == attr.attr_name());
491 ).pop().description();
493 dojo.query('[name=label]', row)[0].appendChild(document.createTextNode(name));
494 dojo.query('[name=value]', row)[0].appendChild(document.createTextNode(attr.attr_value()));
495 this.infoTbody.appendChild(row);
499 openils.Util.show('acq-lit-info-cat-link');
500 var link = dojo.byId('acq-lit-info-cat-link').getElementsByTagName('a')[0];
502 if(openils.XUL.isXUL()) {
504 var makeRecTab = function() {
507 {tab_name: localeStrings.XUL_RECORD_DETAIL_PAGE, browser:false},
510 show_nav_buttons : true,
511 show_print_button : true,
512 opac_url : xulG.url_prefix(xulG.urls.opac_rdetail + '?r=' + li.eg_bib_id())
516 link.setAttribute('href', 'javascript:void(0);');
517 link.onclick = makeRecTab;
520 var href = link.getAttribute('href');
522 link.setAttribute('href', href + li.eg_bib_id());
525 openils.Util.hide('acq-lit-info-cat-link');
529 this.drawMarcHTML = function(li) {
530 var params = [null, true, li.marc()];
532 params = [li.eg_bib_id(), true];
534 fieldmapper.standardRequest(
535 ['open-ils.search', 'open-ils.search.biblio.record.html'],
538 oncomplete: function(r) {
539 dojo.byId('acq-lit-marc-div').innerHTML =
540 openils.Util.readResponse(r);
546 this.drawCopies = function(liId) {
550 this.copyWidgetCache = {};
551 this.oldCopyWidgetCache = {};
555 acqLitSaveCopies.onClick = function() { self.saveCopyChanges(liId) };
556 acqLitBatchUpdateCopies.onClick = function() { self.batchCopyUpdate() };
557 acqLitCopyCountInput.attr('value', '0');
559 while(this.copyTbody.childNodes[0])
560 this.copyTbody.removeChild(this.copyTbody.childNodes[0]);
562 this._drawBatchCopyWidgets();
564 this._fetchDistribFormulas(
566 openils.acq.Lineitem.fetchAttrDefs(
568 self._fetchLineitem(liId, function(li){self._drawCopies(li);});
576 * Insert a new row into the distribution formula selection form
578 this._addDistribFormulaRow = function() {
581 if(!self.distribFormulaStore) {
582 // no formulas, hide the form
583 openils.Util.hide('acq-lit-distrib-formula-tbody');
587 if(!this.distribFormulaTemplate)
588 this.distribFormulaTemplate =
589 dojo.byId('acq-lit-distrib-formula-tbody').removeChild(dojo.byId('acq-lit-distrib-form-row'));
591 var row = dojo.byId('acq-lit-distrib-formula-tbody').appendChild(this.distribFormulaTemplate.cloneNode(true));
593 var selector = new dijit.form.FilteringSelect(
594 {store : self.distribFormulaStore},
595 nodeByName('selector', row)
598 var apply = new dijit.form.Button(
599 {"label": localeStrings.APPLY},
600 nodeByName('set_button', row)
603 var reset = new dijit.form.Button(
604 {"label": localeStrings.RESET_FORMULAE, "disabled": true},
605 nodeByName("reset_button", row)
608 dojo.connect(apply, 'onClick',
610 var form_id = selector.attr('value');
612 self._applyDistribFormula(form_id);
613 reset.attr("disabled", false);
617 dojo.connect(reset, 'onClick',
619 self.restoreCopyFieldsBeforeDF();
622 reset.attr("disabled", "true");
629 * Applies a distrib formula to the current set of copies
631 this._applyDistribFormula = function(formula) {
634 formula = this.distribForms.filter(
635 function(form) { return form.id() == formula; }
638 var copyRows = dojo.query('tr', self.copyTbody);
640 if (this.dfeOffset >= copyRows.length) {
641 alert(localeStrings.OUT_OF_COPIES);
645 var entries_applied = 0;
647 var rowIndex = this.dfeOffset;
648 rowIndex < copyRows.length;
652 var row = copyRows[rowIndex];
653 var copy_id = row.getAttribute('copy_id');
654 var copyWidgets = this.copyWidgetCache[copy_id];
655 var entryIndex = this.dfeOffset;
658 // find the correct entry for the current row
659 dojo.forEach(formula.entries(),
662 entryIndex += e.item_count();
663 if(entryIndex > rowIndex)
671 //console.log("rowIndex = " + rowIndex + ", entry = " + entry.id() + ", entryIndex=" +
672 // entryIndex + ", owning_lib = " + entry.owning_lib() + ", location = " + entry.location());
675 this.saveCopyFieldsBeforeDF(copy_id);
676 this._copy_fields_for_acqdf.forEach(
679 copyWidgets[field].attr('value', (entry[field]()));
686 if (entries_applied) {
687 this.dfaCache.push(formula.id());
688 this.dfeOffset += entries_applied;
692 this.saveCopyFieldsBeforeDF = function(copy_id) {
694 if (!this.oldCopyWidgetCache[copy_id]) {
695 var copyWidgets = this.copyWidgetCache[copy_id];
697 this.oldCopyWidgetCache[copy_id] = {};
698 this._copy_fields_for_acqdf.forEach(
700 self.oldCopyWidgetCache[copy_id][f] =
701 copyWidgets[f].attr("value");
707 this.restoreCopyFieldsBeforeDF = function() {
709 for (var copy_id in this.oldCopyWidgetCache) {
710 this._copy_fields_for_acqdf.forEach(
712 self.copyWidgetCache[copy_id][f].attr(
713 "value", self.oldCopyWidgetCache[copy_id][f]
720 this._fetchDistribFormulas = function(onload) {
721 if(this.distribForms) {
725 fieldmapper.standardRequest(
726 ['open-ils.acq', 'open-ils.acq.distribution_formula.ranged.retrieve.atomic'],
728 params: [openils.User.authtoken],
729 oncomplete: function(r) {
730 self.distribForms = openils.Util.readResponse(r);
731 if(!self.distribForms || self.distribForms.length == 0) {
732 self.distribForms = [];
734 self.distribFormulaStore =
735 new dojo.data.ItemFileReadStore(
736 {data:acqdf.toStoreData(self.distribForms)}
739 self._addDistribFormulaRow();
747 this._drawBatchCopyWidgets = function() {
748 var row = this.copyBatchRow;
749 dojo.forEach(liDetailBatchFields,
751 if(self.copyBatchRowDrawn) {
752 self.copyBatchWidgets[field].attr('value', null);
754 var widget = new openils.widget.AutoFieldWidget({
757 parentNode : dojo.query('[name='+field+']', row)[0],
758 orgLimitPerms : ['CREATE_PICKLIST'],
759 dijitArgs : {required:false},
764 self.copyBatchWidgets[field] = w;
770 this.copyBatchRowDrawn = true;
773 this.batchCopyUpdate = function() {
775 for(var k in this.copyWidgetCache) {
776 var cache = this.copyWidgetCache[k];
777 dojo.forEach(liDetailBatchFields, function(f) {
778 var newval = self.copyBatchWidgets[f].attr('value');
779 if(newval) cache[f].attr('value', newval);
784 this._drawCopies = function(li) {
787 // this button sets the total number of copies for a given lineitem
788 acqLitAddCopyCount.onClick = function() {
789 var count = acqLitCopyCountInput.attr('value');
792 while(self.copyCount() < count)
795 // delete rows if necessary
796 var diff = self.copyCount() - count;
798 var rows = dojo.query('tr', self.copyTbody).reverse().slice(0, diff);
799 if(confirm(dojo.string.substitute(localeStrings.DELETE_LI_COPIES_CONFIRM, [diff]))) {
800 dojo.forEach(rows, function(row) {self.deleteCopy(row); });
802 acqLitCopyCountInput.attr('value', self.copyCount()+'');
808 if(li.lineitem_details().length > 0) {
809 dojo.forEach(li.lineitem_details(),
811 self.addCopy(li, copy);
819 this.copyCount = function() {
821 for(var id in this.copyCache) {
822 if(!this.copyCache[id].isdeleted())
828 this.virtCopyId = -1;
829 this.addCopy = function(li, copy) {
830 var row = this.copyRow.cloneNode(true);
831 this.copyTbody.appendChild(row);
835 copy = new fieldmapper.acqlid();
837 copy.id(this.virtCopyId--);
838 copy.lineitem(li.id());
841 this.copyCache[copy.id()] = copy;
842 row.setAttribute('copy_id', copy.id());
843 self.copyWidgetCache[copy.id()] = {};
845 acqLitCopyCountInput.attr('value', self.copyCount()+'');
847 dojo.forEach(liDetailFields,
849 var widget = new openils.widget.AutoFieldWidget({
853 parentNode : dojo.query('[name='+field+']', row)[0],
854 orgLimitPerms : ['CREATE_PICKLIST', 'CREATE_PURCHASE_ORDER'],
855 readOnly : Boolean(copy.eg_copy_id())
858 // make sure we capture the value from any async widgets
860 copy[field](ww.getFormattedValue())
861 self.copyWidgetCache[copy.id()][field] = w;
864 dojo.connect(widget.widget, 'onChange',
866 if(copy.isnew() || val != copy[field]()) {
867 // prevent setting ischanged() automatically on widget load for existing copies
868 copy[field](widget.getFormattedValue())
869 copy.ischanged(true);
876 var recv_link = dojo.query('[name=receive]', row)[0];
877 if(copy.recv_time()) {
878 openils.Util.hide(recv_link);
880 recv_link.onclick = function() {
881 self.receiveLid(copy);
882 openils.Util.hide(recv_link);
887 openils.Util.hide(dojo.query('[name=delete]', row)[0].parentNode);
889 dojo.query('[name=delete]', row)[0].onclick =
890 function() { self.deleteCopy(row) };
894 this.deleteCopy = function(row) {
895 var copy = this.copyCache[row.getAttribute('copy_id')];
896 copy.isdeleted(true);
898 delete this.copyCache[copy.id()];
899 this.copyTbody.removeChild(row);
902 this.saveCopyChanges = function(liId) {
908 for(var id in this.copyCache) {
909 var c = this.copyCache[id];
910 if(!c.isdeleted()) total++;
911 if(c.isnew() || c.ischanged() || c.isdeleted()) {
912 if(c.id() < 0) c.id(null);
917 if (typeof(this._copy_count_cb) == "function") {
918 this._copy_count_cb(liId, total);
921 dojo.byId('acq-lit-copy-count-label-' + liId).innerHTML = total;
924 if (copies.length > 0) {
925 openils.Util.show("acq-lit-update-copies-progress");
926 fieldmapper.standardRequest(
927 ['open-ils.acq', 'open-ils.acq.lineitem_detail.cud.batch'],
929 params: [openils.User.authtoken, copies],
930 onresponse: function(r) {
931 var res = openils.Util.readResponse(r);
932 litUpdateCopiesProgress.update(res);
934 oncomplete: function() {
935 self.drawCopies(liId);
936 openils.Util.hide("acq-lit-update-copies-progress");
942 if (this.dfaCache.length > 0) {
943 var oldlength = this.dfaCache.length;
945 fieldmapper.standardRequest(
947 "open-ils.acq.distribution_formula.record_application"],
950 "params": [openils.User.authtoken, this.dfaCache, liId],
951 "onresponse": function(r) {
952 var res = openils.Util.readResponse(r);
953 if (res && res.length != oldlength)
954 alert(localeStrings.DFA_NOT_ALL);
962 this.applySelectedLiAction = function(action) {
966 case 'delete_selected':
967 this._deleteLiList(self.getSelected());
972 if(!this.createPoProviderSelector) {
973 var widget = new openils.widget.AutoFieldWidget({
974 fmField : 'provider',
976 parentNode : dojo.byId('acq-lit-po-provider'),
979 function(w) { self.createPoProviderSelector = w; }
983 if(!this.createPoAgencySelector) {
984 var widget = new openils.widget.AutoFieldWidget({
985 fmField : 'ordering_agency',
987 parentNode : dojo.byId('acq-lit-po-agency'),
988 orgLimitPerms : ['CREATE_PURCHASE_ORDER'],
991 function(w) { self.createPoAgencySelector = w; }
996 acqLitPoCreateDialog.show();
999 case 'save_picklist':
1000 this._loadPLSelect();
1001 acqLitSavePlDialog.show();
1004 case 'selector_ready':
1006 acqLitChangeLiStateDialog.attr('state', action.replace('_', '-'));
1007 acqLitChangeLiStateDialog.show();
1018 case 'rollback_receive_po':
1019 this.rollbackPoReceive();
1022 case 'create_assets':
1023 this.createAssets();
1026 case 'add_brief_record':
1028 location.href = oilsBasePath + '/acq/picklist/brief_record?po=' + this.isPO;
1030 location.href = oilsBasePath + '/acq/picklist/brief_record?pl=' + this.isPL;
1034 this.createAssets = function() {
1035 if(!this.isPO) return;
1036 if(!confirm(localeStrings.CREATE_PO_ASSETS_CONFIRM)) return;
1037 this.show('acq-lit-progress-numbers');
1039 fieldmapper.standardRequest(
1040 ['open-ils.acq', 'open-ils.acq.purchase_order.assets.create'],
1042 params: [this.authtoken, this.isPO],
1043 onresponse: function(r) {
1044 var resp = openils.Util.readResponse(r);
1045 self._updateProgressNumbers(resp, true);
1051 this.printPO = function() {
1052 if(!this.isPO) return;
1053 progressDialog.show(true);
1054 fieldmapper.standardRequest(
1055 ['open-ils.acq', 'open-ils.acq.purchase_order.format'],
1057 params: [this.authtoken, this.isPO, 'html'],
1058 oncomplete: function(r) {
1059 progressDialog.hide();
1060 var evt = openils.Util.readResponse(r);
1061 if(evt && evt.template_output()) {
1062 win = window.open('','', 'resizable,width=800,height=600,scrollbars=1');
1063 win.document.body.innerHTML = evt.template_output().data();
1071 this.receivePO = function() {
1072 if(!this.isPO) return;
1073 this.show('acq-lit-progress-numbers');
1075 fieldmapper.standardRequest(
1076 ['open-ils.acq', 'open-ils.acq.purchase_order.receive'],
1078 params: [this.authtoken, this.isPO],
1079 onresponse : function(r) {
1080 var resp = openils.Util.readResponse(r);
1081 self._updateProgressNumbers(resp, true);
1087 this.receiveLi = function(li) {
1088 if(!this.isPO) return;
1089 progressDialog.show(true);
1090 fieldmapper.standardRequest(
1091 ['open-ils.acq', 'open-ils.acq.lineitem.receive'],
1093 params: [this.authtoken, li.id()],
1094 onresponse : function(r) {
1095 var resp = openils.Util.readResponse(r);
1096 progressDialog.hide();
1102 this.receiveLid = function(li) {
1103 if(!this.isPO) return;
1104 progressDialog.show(true);
1105 fieldmapper.standardRequest(
1106 ['open-ils.acq', 'open-ils.acq.lineitem_detail.receive'],
1108 params: [this.authtoken, li.id()],
1109 onresponse : function(r) {
1110 var resp = openils.Util.readResponse(r);
1111 progressDialog.hide();
1117 this.rollbackPoReceive = function() {
1118 if(!this.isPO) return;
1119 if(!confirm(localeStrings.ROLLBACK_PO_RECEIVE_CONFIRM)) return;
1120 this.show('acq-lit-progress-numbers');
1122 fieldmapper.standardRequest(
1123 ['open-ils.acq', 'open-ils.acq.purchase_order.receive.rollback'],
1125 params: [this.authtoken, this.isPO],
1126 onresponse : function(r) {
1127 var resp = openils.Util.readResponse(r);
1128 self._updateProgressNumbers(resp, true);
1134 this._updateProgressNumbers = function(resp, reloadOnComplete) {
1136 dojo.byId('acq-pl-lit-li-processed').innerHTML = resp.li;
1137 dojo.byId('acq-pl-lit-lid-processed').innerHTML = resp.lid;
1138 dojo.byId('acq-pl-lit-debits-processed').innerHTML = resp.debits_accrued;
1139 dojo.byId('acq-pl-lit-bibs-processed').innerHTML = resp.bibs;
1140 dojo.byId('acq-pl-lit-indexed-processed').innerHTML = resp.indexed;
1141 dojo.byId('acq-pl-lit-copies-processed').innerHTML = resp.copies;
1142 if(resp.complete && reloadOnComplete)
1143 location.href = location.href;
1147 this._createPO = function(fields) {
1148 this.show('acq-lit-progress-numbers');
1149 var po = new fieldmapper.acqpo();
1150 po.provider(this.createPoProviderSelector.attr('value'));
1151 po.ordering_agency(this.createPoAgencySelector.attr('value'));
1153 var selected = this.getSelected( (fields.create_from == 'all') );
1154 if(selected.length == 0) return;
1156 var max = selected.length * 3;
1159 fieldmapper.standardRequest(
1160 ['open-ils.acq', 'open-ils.acq.purchase_order.create'],
1163 openils.User.authtoken,
1166 lineitems : selected.map(function(li) { return li.id() }),
1167 create_assets : fields.create_assets[0],
1171 onresponse : function(r) {
1172 var resp = openils.Util.readResponse(r);
1173 self._updateProgressNumbers(resp);
1175 location.href = oilsBasePath + '/eg/acq/po/view/' + resp.purchase_order.id();
1181 this._deleteLiList = function(list, idx) {
1182 if(idx == null) idx = 0;
1183 if(idx >= list.length) return;
1184 var liId = list[idx].id();
1185 fieldmapper.standardRequest(
1186 ['open-ils.acq', 'open-ils.acq.lineitem.delete'],
1188 params: [openils.User.authtoken, liId],
1189 oncomplete: function(r) {
1190 self.removeLineitem(liId);
1191 self._deleteLiList(list, ++idx);
1197 this.editOrderMarc = function(li) {
1199 /* To run in Firefox directly, must set signed.applets.codebase_principal_support
1200 to true in about:config */
1202 if(!openils.XUL.enableXPConnect()) return;
1204 if(openils.XUL.isXUL()) {
1205 win = window.open('/xul/' + openils.XUL.buildId() + '/server/cat/marcedit.xul');
1207 win = window.open('/xul/server/cat/marcedit.xul');
1211 record : {marc : li.marc()},
1213 label: 'Save Record', // XXX I18N
1214 func: function(xmlString) {
1216 fieldmapper.standardRequest(
1217 ['open-ils.acq', 'open-ils.acq.lineitem.update'],
1219 params: [openils.User.authtoken, li],
1220 oncomplete: function(r) {
1221 openils.Util.readResponse(r);
1223 self.drawInfo(li.id())
1232 this._savePl = function(values) {
1234 var selected = this.getSelected( (values.which == 'all') );
1235 openils.Util.show('acq-lit-generic-progress');
1237 if(values.new_name) {
1238 openils.acq.Picklist.create(
1239 {name: values.new_name},
1241 self._updateLiList(id, selected, 0,
1243 location.href = oilsBasePath + '/eg/acq/picklist/view/' + id;
1247 } else if(values.existing_pl) {
1248 // update lineitems to use an existing picklist
1249 self._updateLiList(values.existing_pl, selected, 0,
1251 location.href = oilsBasePath + '/eg/acq/picklist/view/' + values.existing_pl;
1256 this._updateLiState = function(values, state) {
1258 var selected = this.getSelected( (values.which == 'all') );
1259 if(!selected.length) return;
1260 dojo.forEach(selected, function(li) {li.state(state);});
1261 self._updateLiList(null, selected, 0,
1262 // TODO consider inline updates for efficiency
1263 function() { location.href = location.href }
1267 this._updateLiList = function(pl, list, idx, oncomplete) {
1268 if(idx >= list.length) return oncomplete();
1270 if(pl != null) li.picklist(pl);
1271 litGenericProgress.update({maximum: list.length, progress: idx});
1272 new openils.acq.Lineitem({lineitem:li}).update(
1274 self._updateLiList(pl, list, ++idx, oncomplete);
1279 this._loadPLSelect = function() {
1280 if(this._plSelectLoaded) return;
1282 function handleResponse(r) {
1283 plList.push(r.recv().content());
1285 var method = 'open-ils.acq.picklist.user.retrieve';
1286 fieldmapper.standardRequest(
1287 ['open-ils.acq', method],
1289 params: [this.authtoken],
1290 onresponse: handleResponse,
1291 oncomplete: function() {
1292 self._plSelectLoaded = true;
1293 acqLitAddExistingSelect.store =
1294 new dojo.data.ItemFileReadStore({data:acqpl.toStoreData(plList)});
1295 acqLitAddExistingSelect.setValue();
1301 // grab the li-details for this lineitem, grab the linked copies and volumes, add them to the table
1302 this.showRealCopies = function(li) {
1303 while(this.realCopiesTbody.childNodes[0])
1304 this.realCopiesTbody.removeChild(this.realCopiesTbody.childNodes[0]);
1305 this.show('real-copies');
1307 var pcrud = new openils.PermaCrud({authtoken : this.authtoken});
1308 this.realCopyList = [];
1310 var tabIndex = 1000;
1313 acqLitSaveRealCopies.onClick = function() {
1314 self.saveRealCopies();
1317 this._fetchLineitem(li.id(),
1319 li = self.liCache[li.id()] = fullLi;
1323 id : li.lineitem_details().map(
1324 function(item) { return item.eg_copy_id() }
1329 onresponse : function(r) {
1330 var copy = openils.Util.readResponse(r);
1331 var volId = copy.call_number();
1332 var volume = self.volCache[volId];
1334 volume = self.volCache[volId] = pcrud.retrieve('acn', volId);
1336 self.addRealCopy(volume, copy, tabIndex++);
1344 this.addRealCopy = function(volume, copy, tabIndex) {
1345 var row = this.realCopiesRow.cloneNode(true);
1346 this.realCopyList.push(copy);
1350 ['owning_lib', 'location', 'circ_modifier', 'label', 'barcode'],
1353 var isvol = (field == 'owning_lib' || field == 'label');
1354 var widget = new openils.widget.AutoFieldWidget({
1356 fmObject : isvol ? volume : copy,
1357 parentNode : nodeByName(field, row),
1358 readOnly : (field != 'barcode'),
1361 var widgetDrawn = null;
1363 if(field == 'barcode') {
1365 widgetDrawn = function(w, ww) {
1366 var node = w.domNode;
1367 node.setAttribute('tabindex', ''+tabIndex);
1369 // on enter, select the next barcode input
1370 dojo.connect(w, 'onKeyDown',
1372 if(e.keyCode == dojo.keys.ENTER) {
1373 var ti = node.getAttribute('tabindex');
1374 var nextNode = dojo.query('[tabindex=' + String(Number(ti) + 1) + ']', self.realCopiesTbody)[0];
1375 if(nextNode) nextNode.select();
1380 dojo.connect(w, 'onChange',
1382 if(!val || val == copy.barcode()) return;
1383 copy.ischanged(true);
1389 if(self.realCopiesTbody.getElementsByTagName('TR').length == 0)
1394 widget.build(widgetDrawn);
1398 this.realCopiesTbody.appendChild(row);
1399 if(selectNode) selectNode.select();
1402 this.saveRealCopies = function() {
1403 var pcrud = new openils.PermaCrud({authtoken : this.authtoken});
1404 progressDialog.show(true);
1405 var list = this.realCopyList.filter(function(copy) { return copy.ischanged(); });
1406 pcrud.update(list, {oncomplete: function() {
1407 progressDialog.hide();