1 dojo.require("dijit.form.Button");
2 dojo.require("dojo.string");
3 dojo.require('dijit.layout.ContentPane');
4 dojo.require('openils.PermaCrud');
6 var pcrud = new openils.PermaCrud();
11 var invoiceLinkDialogManager;
13 function AcqPoNoteTable() {
16 this.notesTbody = dojo.byId("acq-po-notes-tbody");
17 this.notesRow = this.notesTbody.removeChild(dojo.byId("acq-po-notes-row"));
19 dojo.byId("acq-po-notes-back-button").onclick = function() { self.hide(); };
20 dojo.byId("acq-po-view-notes").onclick = function() { self.show(); };
22 /* widgets' event properties are cased likeThis */
23 acqPoCreateNoteSubmit.onClick = function() {
24 if (!acqPoCreateNoteText.attr("value")) return;
27 var note = new acqpon();
29 Boolean(acqPoCreateNoteVendorPublic.attr('checked'))
31 note.value(acqPoCreateNoteText.attr("value"));
32 note.purchase_order(PO.id());
36 self.updatePoNotes(note);
38 /* reset fields for next use */
39 acqPoCreateNoteText.attr("value", "");
40 acqPoCreateNoteVendorPublic.attr("checked", false);
43 this.drawPoNote = function(note) {
47 var row = dojo.clone(this.notesRow);
49 nodeByName("value", row).innerHTML = note.value();
51 if (openils.Util.isTrue(note.vendor_public()))
52 nodeByName("vendor_public", row).innerHTML =
53 localeStrings.VENDOR_PUBLIC;
55 nodeByName("delete", row).onclick = function() {
57 self.notesTbody.removeChild(row);
61 if (note.edit_time()) {
62 nodeByName("edit_time", row).innerHTML =
63 dojo.date.locale.format(
64 dojo.date.stamp.fromISOString(note.edit_time()),
65 {"formatLength": "short"}
69 self.notesTbody.appendChild(row);
72 this.drawPoNotes = function() {
77 return (a.edit_time() < b.edit_time()) ? 1 : -1;
82 /* remove old renderings of notes */
83 dojo.empty(this.notesTbody);
85 PO.notes().forEach(function(o) { self.drawPoNote(o); });
88 this.updatePoNotesCount = function() {
89 dojo.byId("acq-po-view-notes").innerHTML =
90 "(" + PO.notes().length + ")";
93 this.updatePoNotes = function(newNote) {
98 if (o.ischanged() || o.isnew() || o.isdeleted())
103 if (notes.length < 1)
106 progressDialog.show();
108 fieldmapper.standardRequest(
109 ["open-ils.acq", "open-ils.acq.po_note.cud.batch"], {
111 "params": [openils.User.authtoken, notes],
112 "onresponse": function(r) {
113 var resp = openils.Util.readResponse(r);
115 progressDialog.update(resp);
117 if (!resp.note.isdeleted()) {
118 resp.note.isnew(false);
119 resp.note.ischanged(false);
120 PO.notes().push(resp.note);
124 "oncomplete": function() {
126 /* remove the old changed notes */
132 o.ischanged() || o.isnew() ||
140 progressDialog.hide();
141 self.updatePoNotesCount();
148 this.hide = function() {
149 openils.Util.hide("acq-po-notes-div");
150 liTable.show("list");
154 this.show = function() {
158 openils.Util.show("acq-po-notes-div");
162 function updatePoState(po_info) {
163 var data = po_info[PO.id()];
165 for (var key in data)
171 function cancellationUpdater(r) {
172 var r = openils.Util.readResponse(r);
174 if (r.po) updatePoState(r.po);
176 for (var id in r.li) {
177 liTable.liCache[id].state(r.li[id].state);
178 liTable.liCache[id].cancel_reason(r.li[id].cancel_reason);
179 liTable.updateLiState(liTable.liCache[id]);
182 if (r.lid && liTable.copyCache) {
183 for (var id in r.lid) {
184 if (liTable.copyCache[id]) {
185 liTable.copyCache[id].cancel_reason(
186 r.lid[id].cancel_reason
188 liTable.updateLidState(liTable.copyCache[id]);
195 function makeProviderLink(node, provider) {
198 "href": oilsBasePath + "/conify/global/acq/provider/" + provider.id(),
199 "innerHTML": provider.name() + " (" + provider.code() + ")",
205 function makePrepayWidget(node, prepay) {
207 openils.Util.addCSSClass(node, "oils-acq-po-prepay");
208 node.innerHTML = localeStrings.YES;
210 openils.Util.removeCSSClass(node, "oils-acq-po-prepay");
211 node.innerHTML = localeStrings.NO;
215 function makeCancelWidget(node, labelnode) {
216 openils.Util.hide("acq-po-choose-cancel-reason");
218 if (PO.cancel_reason()) {
219 labelnode.innerHTML = localeStrings.CANCEL_REASON;
220 node.innerHTML = PO.cancel_reason().description() + " (" +
221 PO.cancel_reason().label() + ")";
222 } else if (["on-order", "pending"].indexOf(PO.state()) == -1) {
223 dojo.destroy(this.oldTip);
224 labelnode.innerHTML = "";
227 dojo.destroy(this.oldTip);
228 labelnode.innerHTML = localeStrings.CANCEL;
230 if (!acqPoCancelReasonSubmit._prepared) {
231 var widget = new openils.widget.AutoFieldWidget({
232 "fmField": "cancel_reason",
234 "parentNode": dojo.byId("acq-po-cancel-reason"),
235 "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
240 acqPoCancelReasonSubmit.onClick = function() {
241 if (w.attr("value")) {
242 if (confirm(localeStrings.PO_CANCEL_CONFIRM)) {
243 fieldmapper.standardRequest(
245 "open-ils.acq.purchase_order.cancel"],
248 openils.User.authtoken,
253 "oncomplete": cancellationUpdater
259 acqPoCancelReasonSubmit._prepared = true;
263 openils.Util.show("acq-po-choose-cancel-reason", "inline");
267 function prepareInvoiceFeatures() {
268 /* show the count of related invoices on the "view invoices" button */
269 fieldmapper.standardRequest(
270 ["open-ils.acq", "open-ils.acq.invoice.unified_search.atomic"], {
272 openils.User.authtoken,
273 {"acqpo":[{"id": PO.id()}]},
279 "oncomplete": function(r) {
280 dojo.byId("acq-po-view-invoice-count").innerHTML =
281 openils.Util.readResponse(r).length;
286 /* view invoices button */
287 dijit.byId("acq-po-view-invoice-link").onClick = function() {
288 location.href = oilsBasePath + "/acq/search/unified?so=" +
289 base64Encode({"acqpo":[{"id": PO.id()}]}) +
293 /* create invoice button */
294 dijit.byId("acq-po-create-invoice-link").onClick = function() {
295 location.href = oilsBasePath +
296 "/acq/invoice/view?create=1&attach_po=" + PO.id();
299 openils.Util.show("acq-po-invoice-stuff", "table-cell");
302 /* renderPo() is the best place to add tests that depend on PO-state
303 * (or simple ordered-or-not? checks) to enable/disable UI elements
304 * across the whole interface. */
305 function renderPo() {
306 var po_state = PO.state();
307 dojo.byId("acq-po-view-id").innerHTML = PO.id();
308 dojo.byId("acq-po-view-name").innerHTML = PO.name();
310 dojo.byId("acq-po-view-provider"),
313 dojo.byId("acq-po-view-total-li").innerHTML = PO.lineitem_count();
314 dojo.byId("acq-po-view-total-enc").innerHTML = PO.amount_encumbered().toFixed(2);
315 dojo.byId("acq-po-view-total-spent").innerHTML = PO.amount_spent().toFixed(2);
316 dojo.byId("acq-po-view-state").innerHTML = po_state; // TODO i18n
318 if(PO.order_date()) {
319 openils.Util.show('acq-po-activated-on', 'inline');
320 liTable.enableActionsDropdownOptions("ao"); /* activated */
321 liTable.initBatchUpdater(["item_count", "distribution_formula"]);
323 dojo.byId('acq-po-activated-on').innerHTML =
324 dojo.string.substitute(
325 localeStrings.PO_ACTIVATED_ON, [
326 openils.Util.timeStamp(PO.order_date(), {formatLength:'short'})
329 /* These are handled another way now */
330 // if (po_state == "on-order" || po_state == "cancelled") {
331 // dojo.removeAttr('receive_lineitems', 'disabled');
332 // } else if(po_state == "received") {
333 // dojo.removeAttr('rollback_receive_lineitems', 'disabled');
336 /* cancel widgets only make sense for activate (ordered) POs */
338 dojo.byId("acq-po-view-cancel-reason"),
339 dojo.byId("acq-po-cancel-label")
342 /* likewise for invoice features */
343 openils.Util.show("acq-po-invoice-label", "table-cell");
344 prepareInvoiceFeatures();
346 /* These things only make sense for not-ordered-yet POs */
348 liTable.initBatchUpdater();
349 liTable.enableActionsDropdownOptions("po");
351 openils.Util.show("acq-po-zero-activate-label", "table-cell");
352 openils.Util.show("acq-po-zero-activate", "table-cell");
354 openils.Util.show("acq-po-item-table-controls");
358 dojo.byId("acq-po-view-prepay"),
359 openils.Util.isTrue(PO.prepayment_required())
361 // dojo.byId("acq-po-view-notes").innerHTML = PO.notes().length;
362 poNoteTable.updatePoNotesCount();
364 if (po_state == "pending") {
365 checkCouldActivatePo();
366 if (PO.lineitem_count() > 1)
367 openils.Util.show("acq-po-split");
369 if (PO.order_date()) {
370 dojo.byId("acq-po-activate-checking").innerHTML = localeStrings.PO_ALREADY_ACTIVATED;
372 dojo.byId("acq-po-activate-checking").innerHTML = localeStrings.NO;
376 // XXX we probably don't *always* need to do this...
378 PO.po_items().forEach(
379 function(po_item) { poItemTable.addItem(po_item); }
384 "acq-po-view-history", "href",
385 oilsBasePath + "/acq/po/history/" + PO.id()
387 openils.Util.show("acq-po-view-history", "inline");
390 /* if we got here from the search/invoice page with a focused LI,
391 * return to the previous page with the same LI focused */
392 var cgi = new openils.CGI();
393 var source = cgi.param('source');
394 var focus_li = cgi.param('focus_li');
395 if (focus_li && source) {
397 ['search', 'invoice'], // perhaps a wee bit too loose
399 if (source.match(new RegExp(srcType))) {
400 openils.Util.show('acq-po-return-to-' + srcType);
401 var newCgi = new openils.CGI({url : source});
402 newCgi.param('focus_li', focus_li);
403 dojo.byId('acq-po-return-to-' + srcType + '-button').onclick = function() {
404 location.href = newCgi.url();
414 /* set up li table */
415 liTable = new AcqLiTable();
418 liTable.poUpdateCallback = updatePoState;
420 /* set up po notes table */
421 poNoteTable = new AcqPoNoteTable();
423 /* retrieve data and populate */
424 fieldmapper.standardRequest(
425 ['open-ils.acq', 'open-ils.acq.purchase_order.retrieve'],
427 params: [openils.User.authtoken, poId, {
428 "flesh_provider": true,
429 "flesh_price_summary": true,
430 "flesh_lineitem_count": true,
432 "flesh_po_items": true
434 oncomplete: function(r) {
435 PO = openils.Util.readResponse(r); /* save PO globally */
438 poItemTable = new PoItemTable(PO, pcrud);
440 liTable.testOrderIdentPerms( PO.ordering_agency(), init2);
451 var totalEstimated = 0;
453 fieldmapper.standardRequest(
454 ['open-ils.acq', 'open-ils.acq.lineitem.search'],
457 openils.User.authtoken,
458 [{purchase_order:poId}, {"order_by": {"jub": "id ASC"}}],
459 {flesh_attrs:true, flesh_notes:true, flesh_cancel_reason:true, clear_marc:true}
461 onresponse: function(r) {
463 liTable.show('list');
464 var li = openils.Util.readResponse(r);
465 // TODO: Add po_item's to total estimated amount
466 totalEstimated += (Number(li.item_count() || 0) * Number(li.estimated_unit_price() || 0));
467 liTable.addLineitem(li);
470 oncomplete : function() {
471 dojo.byId("acq-po-view-total-estimated").innerHTML = totalEstimated.toFixed(2);
472 if (liFocus) liTable.drawCopies(liFocus);
473 if(zeroLi) openils.Util.show('acq-po-no-lineitems');
480 {purchase_order : poId},
483 oncomplete : function(r) {
484 var resp = openils.Util.readResponse(r);
487 dojo.byId('acq-po-view-edi-messages').innerHTML = '(' + resp.length + ')';
488 dojo.byId('acq-po-view-edi-messages').setAttribute('href', oilsBasePath + '/acq/po/edi_messages/' + poId);
490 dojo.byId('acq-po-view-edi-messages').innerHTML = '0';
491 dojo.byId('acq-po-view-edi-messages').setAttribute('href', '');
498 function checkCouldActivatePo() {
499 var d = dojo.byId("acq-po-activate-checking");
500 var a = dojo.byId("acq-po-activate-links"); /* <span> not <a> now, but no diff */
501 d.innerHTML = localeStrings.PO_CHECKING;
506 fieldmapper.standardRequest(
507 ["open-ils.acq", "open-ils.acq.purchase_order.activate.dry_run"], {
509 openils.User.authtoken,
511 null, // vandelay options
512 {zero_copy_activate : dojo.byId('acq-po-activate-zero-copies').checked}
515 "onresponse": function(r) {
516 if ((r = openils.Util.readResponse(r, true /* eventOk */))) {
517 if (typeof(r.textcode) != "undefined") {
519 case "ACQ_FUND_EXCEEDS_STOP_PERCENT":
522 case "ACQ_FUND_EXCEEDS_WARN_PERCENT":
531 "oncomplete": function() {
532 /* XXX in the future, this might be tweaked to display info
533 * about more than one stop or warning event from the ML. */
534 if (!(warnings.length || stops.length || other.length)) {
535 d.innerHTML = localeStrings.PO_COULD_ACTIVATE;
536 openils.Util.show(a, "inline");
537 activatePoButton.attr("disabled", false);
540 /* XXX make the textcode part a tooltip one day */
541 d.innerHTML = localeStrings.NO + ": " +
542 other[0].desc + " (" + other[0].textcode + ")";
543 openils.Util.hide(a);
545 if (other[0].textcode == 'ACQ_LINEITEM_NO_COPIES') {
546 // when LIs w/ zero LIDs are present, list them
547 fieldmapper.standardRequest(
549 'open-ils.acq.purchase_order.no_copy_lineitems.id_list.authoritative.atomic' ],
551 params : [openils.User.authtoken, poId],
552 oncomplete : function(r) {
553 var ids = openils.Util.readResponse(r);
554 d.innerHTML += ' (' + ids + ')';
559 } else if (stops.length) {
561 dojo.string.substitute(
562 localeStrings.PO_STOP_BLOCKS_ACTIVATION, [
563 stops[0].payload.fund.code(),
564 stops[0].payload.fund.year()
567 openils.Util.hide(a);
569 PO._warning_hack = true;
571 dojo.string.substitute(
572 localeStrings.PO_WARNING_NO_BLOCK_ACTIVATION, [
573 warnings[0].payload.fund.code(),
574 warnings[0].payload.fund.year()
577 openils.Util.show(a, "inline");
578 activatePoButton.attr("disabled", false);
586 function activatePo(noAssets) {
587 activatePoButton.attr("disabled", true);
588 activatePoNoAssetsButton.attr("disabled", true);
590 if (openils.Util.isTrue(PO.prepayment_required())) {
591 if (!confirm(localeStrings.PREPAYMENT_REQUIRED_REMINDER)) {
592 activatePoButton.attr("disabled", false);
597 if (PO._warning_hack) {
598 if (!confirm(localeStrings.PO_FUND_WARNING_CONFIRM)) {
599 activatePoButton.attr("disabled", false);
605 // no need for AssetCreator when assets are not desired
606 activatePoStage2(true);
608 liTable.showAssetCreator(activatePoStage2);
612 function activatePoStage2(noAssets) {
614 var want_refresh = false;
615 progressDialog.show(true);
616 fieldmapper.standardRequest(
617 ["open-ils.acq", "open-ils.acq.purchase_order.activate"], {
620 openils.User.authtoken,
622 null, // vandelay options
624 no_assets : noAssets, // no bibs/volumes/copies required
625 zero_copy_activate : dojo.byId('acq-po-activate-zero-copies').checked
628 "onresponse": function(r) {
629 progressDialog.hide();
630 activatePoButton.attr("disabled", false);
631 want_refresh = Boolean(openils.Util.readResponse(r));
633 "oncomplete": function() {
635 location.href = location.href;
642 progressDialog.show(true);
645 fieldmapper.standardRequest(
646 ['open-ils.acq', 'open-ils.acq.purchase_order.split_by_lineitems'],
648 params: [openils.User.authtoken, PO.id()],
649 onresponse : function(r) {
650 list = openils.Util.readResponse(r);
652 oncomplete : function() {
653 progressDialog.hide();
655 location.href = oilsBasePath + '/acq/po/search/' +
662 progressDialog.hide();
667 function updatePoName() {
668 var value = prompt('Enter new purchase order name:', PO.name()); // TODO i18n
669 if(!value || value == PO.name()) return;
672 oncomplete : function(r, cudResults) {
673 var stat = cudResults[0];
675 dojo.byId('acq-po-view-name').innerHTML = value;
680 openils.Util.addOnLoad(init);