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 function renderPo() {
303 dojo.byId("acq-po-view-id").innerHTML = PO.id();
304 dojo.byId("acq-po-view-name").innerHTML = PO.name();
306 dojo.byId("acq-po-view-provider"),
309 dojo.byId("acq-po-view-total-li").innerHTML = PO.lineitem_count();
310 dojo.byId("acq-po-view-total-enc").innerHTML = PO.amount_encumbered().toFixed(2);
311 dojo.byId("acq-po-view-total-spent").innerHTML = PO.amount_spent().toFixed(2);
312 dojo.byId("acq-po-view-state").innerHTML = PO.state(); // TODO i18n
314 if(PO.order_date()) {
315 openils.Util.show('acq-po-activated-on', 'inline');
316 dojo.byId('acq-po-activated-on').innerHTML =
317 dojo.string.substitute(
318 localeStrings.PO_ACTIVATED_ON, [
319 openils.Util.timeStamp(PO.order_date(), {formatLength:'short'})
326 dojo.byId("acq-po-view-prepay"),
327 openils.Util.isTrue(PO.prepayment_required())
330 dojo.byId("acq-po-view-cancel-reason"),
331 dojo.byId("acq-po-cancel-label")
333 // dojo.byId("acq-po-view-notes").innerHTML = PO.notes().length;
334 poNoteTable.updatePoNotesCount();
336 if (PO.state() == "pending") {
337 checkCouldActivatePo();
338 if (PO.lineitem_count() > 1)
339 openils.Util.show("acq-po-split");
341 dojo.byId("acq-po-activate-checking").innerHTML = localeStrings.NO;
344 // XXX we probably don't *always* need to do this...
346 PO.po_items().forEach(
347 function(po_item) { poItemTable.addItem(po_item); }
352 "acq-po-view-history", "href",
353 oilsBasePath + "/acq/po/history/" + PO.id()
355 openils.Util.show("acq-po-view-history", "inline");
358 /* if we got here from the search/invoice page with a focused LI,
359 * return to the previous page with the same LI focused */
360 var cgi = new openils.CGI();
361 if (cgi.param('focus_li')) {
363 ['search', 'invoice'], // perhaps a wee bit too loose
365 if (document.referrer.match(new RegExp(source))) {
366 openils.Util.show('acq-po-return-to-' + source);
367 var newCgi = new openils.CGI({url : document.referrer});
368 newCgi.param('focus_li', cgi.param('focus_li'));
369 dojo.byId('acq-po-return-to-' + source + '-button').onclick = function() {
370 location.href = newCgi.url();
377 prepareInvoiceFeatures();
382 /* set up li table */
383 liTable = new AcqLiTable();
386 liTable.poUpdateCallback = updatePoState;
388 /* set up po notes table */
389 poNoteTable = new AcqPoNoteTable();
391 /* retrieve data and populate */
392 fieldmapper.standardRequest(
393 ['open-ils.acq', 'open-ils.acq.purchase_order.retrieve'],
395 params: [openils.User.authtoken, poId, {
396 "flesh_provider": true,
397 "flesh_price_summary": true,
398 "flesh_lineitem_count": true,
400 "flesh_po_items": true
402 oncomplete: function(r) {
403 PO = openils.Util.readResponse(r); /* save PO globally */
406 poItemTable = new PoItemTable(PO, pcrud);
413 var totalEstimated = 0;
415 fieldmapper.standardRequest(
416 ['open-ils.acq', 'open-ils.acq.lineitem.search'],
419 openils.User.authtoken,
420 [{purchase_order:poId}, {"order_by": {"jub": "id ASC"}}],
421 {flesh_attrs:true, flesh_notes:true, flesh_cancel_reason:true, clear_marc:true}
423 onresponse: function(r) {
425 liTable.show('list');
426 var li = openils.Util.readResponse(r);
427 // TODO: Add po_item's to total estimated amount
428 totalEstimated += (Number(li.item_count() || 0) * Number(li.estimated_unit_price() || 0));
429 liTable.addLineitem(li);
432 oncomplete : function() {
433 dojo.byId("acq-po-view-total-estimated").innerHTML = totalEstimated.toFixed(2);
434 if (liFocus) liTable.drawCopies(liFocus);
435 if(zeroLi) openils.Util.show('acq-po-no-lineitems');
442 {purchase_order : poId},
445 oncomplete : function(r) {
446 var resp = openils.Util.readResponse(r);
449 dojo.byId('acq-po-view-edi-messages').innerHTML = '(' + resp.length + ')';
450 dojo.byId('acq-po-view-edi-messages').setAttribute('href', oilsBasePath + '/acq/po/edi_messages/' + poId);
452 dojo.byId('acq-po-view-edi-messages').innerHTML = '0';
453 dojo.byId('acq-po-view-edi-messages').setAttribute('href', '');
460 function checkCouldActivatePo() {
461 var d = dojo.byId("acq-po-activate-checking");
462 var a = dojo.byId("acq-po-activate-link");
463 d.innerHTML = localeStrings.PO_CHECKING;
468 fieldmapper.standardRequest(
469 ["open-ils.acq", "open-ils.acq.purchase_order.activate.dry_run"], {
471 openils.User.authtoken,
473 null, // vandelay options
474 {zero_copy_activate : dojo.byId('acq-po-activate-zero-copies').checked}
477 "onresponse": function(r) {
478 if ((r = openils.Util.readResponse(r, true /* eventOk */))) {
479 if (typeof(r.textcode) != "undefined") {
481 case "ACQ_FUND_EXCEEDS_STOP_PERCENT":
484 case "ACQ_FUND_EXCEEDS_WARN_PERCENT":
493 "oncomplete": function() {
494 /* XXX in the future, this might be tweaked to display info
495 * about more than one stop or warning event from the ML. */
496 if (!(warnings.length || stops.length || other.length)) {
497 d.innerHTML = localeStrings.PO_COULD_ACTIVATE;
498 openils.Util.show(a, "inline");
501 /* XXX make the textcode part a tooltip one day */
502 d.innerHTML = localeStrings.NO + ": " +
503 other[0].desc + " (" + other[0].textcode + ")";
504 openils.Util.hide(a);
506 if (other[0].textcode == 'ACQ_LINEITEM_NO_COPIES') {
507 // when LIs w/ zero LIDs are present, list them
508 fieldmapper.standardRequest(
510 'open-ils.acq.purchase_order.no_copy_lineitems.id_list.authoritative.atomic' ],
512 params : [openils.User.authtoken, poId],
513 oncomplete : function(r) {
514 var ids = openils.Util.readResponse(r);
515 d.innerHTML += ' (' + ids + ')';
520 } else if (stops.length) {
522 dojo.string.substitute(
523 localeStrings.PO_STOP_BLOCKS_ACTIVATION, [
524 stops[0].payload.fund.code(),
525 stops[0].payload.fund.year()
528 openils.Util.hide(a);
530 PO._warning_hack = true;
532 dojo.string.substitute(
533 localeStrings.PO_WARNING_NO_BLOCK_ACTIVATION, [
534 warnings[0].payload.fund.code(),
535 warnings[0].payload.fund.year()
538 openils.Util.show(a, "inline");
546 function activatePo() {
547 if (openils.Util.isTrue(PO.prepayment_required())) {
548 if (!confirm(localeStrings.PREPAYMENT_REQUIRED_REMINDER))
552 if (PO._warning_hack) {
553 if (!confirm(localeStrings.PO_FUND_WARNING_CONFIRM))
557 liTable.showAssetCreator(activatePoStage2);
560 function activatePoStage2() {
562 var want_refresh = false;
563 progressDialog.show(true);
564 fieldmapper.standardRequest(
565 ["open-ils.acq", "open-ils.acq.purchase_order.activate"], {
568 openils.User.authtoken,
570 null, // vandelay options
571 {zero_copy_activate : dojo.byId('acq-po-activate-zero-copies').checked}
573 "onresponse": function(r) {
574 want_refresh = Boolean(openils.Util.readResponse(r));
576 "oncomplete": function() {
577 progressDialog.hide();
579 location.href = location.href;
586 progressDialog.show(true);
589 fieldmapper.standardRequest(
590 ['open-ils.acq', 'open-ils.acq.purchase_order.split_by_lineitems'],
592 params: [openils.User.authtoken, PO.id()],
593 onresponse : function(r) {
594 list = openils.Util.readResponse(r);
596 oncomplete : function() {
597 progressDialog.hide();
599 location.href = oilsBasePath + '/acq/po/search/' +
606 progressDialog.hide();
611 function updatePoName() {
612 var value = prompt('Enter new purchase order name:', PO.name()); // TODO i18n
613 if(!value || value == PO.name()) return;
616 oncomplete : function(r, cudResults) {
617 var stat = cudResults[0];
619 dojo.byId('acq-po-view-name').innerHTML = value;
624 openils.Util.addOnLoad(init);