1 dojo.require("dojo.string");
2 dojo.require('dijit.layout.ContentPane');
3 dojo.require('openils.PermaCrud');
5 var pcrud = new openils.PermaCrud();
10 var invoiceLinkDialogManager;
12 function AcqPoNoteTable() {
15 this.notesTbody = dojo.byId("acq-po-notes-tbody");
16 this.notesRow = this.notesTbody.removeChild(dojo.byId("acq-po-notes-row"));
18 dojo.byId("acq-po-notes-back-button").onclick = function() { self.hide(); };
19 dojo.byId("acq-po-view-notes").onclick = function() { self.show(); };
21 /* widgets' event properties are cased likeThis */
22 acqPoCreateNoteSubmit.onClick = function() {
23 if (!acqPoCreateNoteText.attr("value")) return;
26 var note = new acqpon();
28 Boolean(acqPoCreateNoteVendorPublic.attr('checked'))
30 note.value(acqPoCreateNoteText.attr("value"));
31 note.purchase_order(PO.id());
35 self.updatePoNotes(note);
37 /* reset fields for next use */
38 acqPoCreateNoteText.attr("value", "");
39 acqPoCreateNoteVendorPublic.attr("checked", false);
42 this.drawPoNote = function(note) {
46 var row = dojo.clone(this.notesRow);
48 nodeByName("value", row).innerHTML = note.value();
50 if (openils.Util.isTrue(note.vendor_public()))
51 nodeByName("vendor_public", row).innerHTML =
52 localeStrings.VENDOR_PUBLIC;
54 nodeByName("delete", row).onclick = function() {
56 self.notesTbody.removeChild(row);
60 if (note.edit_time()) {
61 nodeByName("edit_time", row).innerHTML =
62 dojo.date.locale.format(
63 dojo.date.stamp.fromISOString(note.edit_time()),
64 {"formatLength": "short"}
68 self.notesTbody.appendChild(row);
71 this.drawPoNotes = function() {
76 return (a.edit_time() < b.edit_time()) ? 1 : -1;
81 /* remove old renderings of notes */
82 dojo.empty(this.notesTbody);
84 PO.notes().forEach(function(o) { self.drawPoNote(o); });
87 this.updatePoNotesCount = function() {
88 dojo.byId("acq-po-view-notes").innerHTML = PO.notes().length;
91 this.updatePoNotes = function(newNote) {
96 if (o.ischanged() || o.isnew() || o.isdeleted())
101 if (notes.length < 1)
104 progressDialog.show();
106 fieldmapper.standardRequest(
107 ["open-ils.acq", "open-ils.acq.po_note.cud.batch"], {
109 "params": [openils.User.authtoken, notes],
110 "onresponse": function(r) {
111 var resp = openils.Util.readResponse(r);
113 progressDialog.update(resp);
115 if (!resp.note.isdeleted()) {
116 resp.note.isnew(false);
117 resp.note.ischanged(false);
118 PO.notes().push(resp.note);
122 "oncomplete": function() {
124 /* remove the old changed notes */
130 o.ischanged() || o.isnew() ||
138 progressDialog.hide();
139 self.updatePoNotesCount();
146 this.hide = function() {
147 openils.Util.hide("acq-po-notes-div");
148 liTable.show("list");
152 this.show = function() {
156 openils.Util.show("acq-po-notes-div");
160 function updatePoState(po_info) {
161 var data = po_info[PO.id()];
163 for (var key in data)
169 function cancellationUpdater(r) {
170 var r = openils.Util.readResponse(r);
172 if (r.po) updatePoState(r.po);
174 for (var id in r.li) {
175 liTable.liCache[id].state(r.li[id].state);
176 liTable.liCache[id].cancel_reason(r.li[id].cancel_reason);
177 liTable.updateLiState(liTable.liCache[id]);
180 if (r.lid && liTable.copyCache) {
181 for (var id in r.lid) {
182 if (liTable.copyCache[id]) {
183 liTable.copyCache[id].cancel_reason(
184 r.lid[id].cancel_reason
186 liTable.updateLidState(liTable.copyCache[id]);
193 function makeProviderLink(node, provider) {
196 "href": oilsBasePath + "/conify/global/acq/provider/" + provider.id(),
197 "innerHTML": provider.name() + " (" + provider.code() + ")",
203 function makePrepayWidget(node, prepay) {
205 openils.Util.addCSSClass(node, "oils-acq-po-prepay");
206 node.innerHTML = localeStrings.YES;
208 openils.Util.removeCSSClass(node, "oils-acq-po-prepay");
209 node.innerHTML = localeStrings.NO;
213 function makeCancelWidget(node, labelnode) {
214 openils.Util.hide("acq-po-choose-cancel-reason");
216 if (PO.cancel_reason()) {
217 labelnode.innerHTML = localeStrings.CANCEL_REASON;
218 node.innerHTML = PO.cancel_reason().description() + " (" +
219 PO.cancel_reason().label() + ")";
220 } else if (["on-order", "pending"].indexOf(PO.state()) == -1) {
221 dojo.destroy(this.oldTip);
222 labelnode.innerHTML = "";
225 dojo.destroy(this.oldTip);
226 labelnode.innerHTML = localeStrings.CANCEL;
228 if (!acqPoCancelReasonSubmit._prepared) {
229 var widget = new openils.widget.AutoFieldWidget({
230 "fmField": "cancel_reason",
232 "parentNode": dojo.byId("acq-po-cancel-reason"),
233 "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
238 acqPoCancelReasonSubmit.onClick = function() {
239 if (w.attr("value")) {
240 if (confirm(localeStrings.PO_CANCEL_CONFIRM)) {
241 fieldmapper.standardRequest(
243 "open-ils.acq.purchase_order.cancel"],
246 openils.User.authtoken,
251 "oncomplete": cancellationUpdater
257 acqPoCancelReasonSubmit._prepared = true;
261 openils.Util.show("acq-po-choose-cancel-reason", "inline");
265 function prepareInvoiceFeatures() {
266 /* show the count of related invoices on the "view invoices" button */
267 fieldmapper.standardRequest(
268 ["open-ils.acq", "open-ils.acq.invoice.unified_search.atomic"], {
270 openils.User.authtoken,
271 {"acqpo":[{"id": PO.id()}]},
277 "oncomplete": function(r) {
278 dojo.byId("acq-po-view-invoice-count").innerHTML =
279 openils.Util.readResponse(r).length;
284 /* view invoices button */
285 dijit.byId("acq-po-view-invoice-link").onClick = function() {
286 location.href = oilsBasePath + "/acq/search/unified?so=" +
287 base64Encode({"jub":[{"purchase_order": PO.id()}]}) +
291 /* create invoice button */
292 dijit.byId("acq-po-create-invoice-link").onClick = function() {
293 location.href = oilsBasePath +
294 "/acq/invoice/view?create=1&attach_po=" + PO.id();
297 if (!invoiceLinkDialogManager)
298 invoiceLinkDialogManager = new InvoiceLinkDialogManager("po", PO);
300 openils.Util.show("acq-po-view-invoices", "table-row");
303 function renderPo() {
304 dojo.byId("acq-po-view-id").innerHTML = PO.id();
305 dojo.byId("acq-po-view-name").innerHTML = PO.name();
307 dojo.byId("acq-po-view-provider"),
310 dojo.byId("acq-po-view-total-li").innerHTML = PO.lineitem_count();
311 dojo.byId("acq-po-view-total-enc").innerHTML = PO.amount_encumbered().toFixed(2);
312 dojo.byId("acq-po-view-total-spent").innerHTML = PO.amount_spent().toFixed(2);
313 dojo.byId("acq-po-view-state").innerHTML = PO.state(); // TODO i18n
315 dojo.byId("acq-po-view-prepay"),
316 openils.Util.isTrue(PO.prepayment_required())
319 dojo.byId("acq-po-view-cancel-reason"),
320 dojo.byId("acq-po-cancel-label")
322 dojo.byId("acq-po-view-notes").innerHTML = PO.notes().length;
324 if(PO.state() == "pending") {
325 openils.Util.show("acq-po-activate", "table-row");
326 checkCouldActivatePo();
327 if (PO.lineitem_count() > 1)
328 openils.Util.show("acq-po-split");
331 // XXX we probably don't *always* need to do this...
333 PO.po_items().forEach(
334 function(po_item) { poItemTable.addItem(po_item); }
338 prepareInvoiceFeatures();
343 /* set up li table */
344 liTable = new AcqLiTable();
347 liTable.poUpdateCallback = updatePoState;
349 /* set up po notes table */
350 poNoteTable = new AcqPoNoteTable();
352 /* retrieve data and populate */
353 fieldmapper.standardRequest(
354 ['open-ils.acq', 'open-ils.acq.purchase_order.retrieve'],
356 params: [openils.User.authtoken, poId, {
357 "flesh_provider": true,
358 "flesh_price_summary": true,
359 "flesh_lineitem_count": true,
361 "flesh_po_items": true
363 oncomplete: function(r) {
364 PO = openils.Util.readResponse(r); /* save PO globally */
367 poItemTable = new PoItemTable(PO, pcrud);
374 var totalEstimated = 0;
375 fieldmapper.standardRequest(
376 ['open-ils.acq', 'open-ils.acq.lineitem.search'],
378 params: [openils.User.authtoken, {purchase_order:poId}, {flesh_attrs:true, flesh_notes:true, flesh_cancel_reason:true}],
379 onresponse: function(r) {
380 liTable.show('list');
381 var li = openils.Util.readResponse(r);
382 // TODO: Add po_item's to total estimated amount
383 totalEstimated += (Number(li.item_count() || 0) * Number(li.estimated_unit_price() || 0));
384 liTable.addLineitem(li);
387 oncomplete : function() {
388 dojo.byId("acq-po-view-total-estimated").innerHTML = totalEstimated.toFixed(2);
389 if (liFocus) liTable.drawCopies(liFocus);
395 function checkCouldActivatePo() {
396 var d = dojo.byId("acq-po-activate-checking");
397 var a = dojo.byId("acq-po-activate-link");
398 d.innerHTML = localeStrings.PO_CHECKING;
402 fieldmapper.standardRequest(
403 ["open-ils.acq", "open-ils.acq.purchase_order.activate.dry_run"], {
404 "params": [openils.User.authtoken, PO.id()],
406 "onresponse": function(r) {
407 if ((r = openils.Util.readResponse(r, true /* eventOk */))) {
408 if (typeof(r.textcode) != "undefined") {
410 case "ACQ_FUND_EXCEEDS_STOP_PERCENT":
413 case "ACQ_FUND_EXCEEDS_WARN_PERCENT":
420 "oncomplete": function() {
421 /* XXX in the future, this might be tweaked to display info
422 * about more than one stop or warning event from the ML. */
423 if (!(warnings.length || stops.length)) {
424 d.innerHTML = localeStrings.PO_COULD_ACTIVATE;
425 openils.Util.show(a, "inline");
429 dojo.string.substitute(
430 localeStrings.PO_STOP_BLOCKS_ACTIVATION, [
431 stops[0].payload.fund.code(),
432 stops[0].payload.fund.year()
435 openils.Util.hide(a);
437 PO._warning_hack = true;
439 dojo.string.substitute(
440 localeStrings.PO_WARNING_NO_BLOCK_ACTIVATION, [
441 warnings[0].payload.fund.code(),
442 warnings[0].payload.fund.year()
445 openils.Util.show(a, "inline");
453 function activatePo() {
454 if (openils.Util.isTrue(PO.prepayment_required())) {
455 if (!confirm(localeStrings.PREPAYMENT_REQUIRED_REMINDER))
459 if (PO._warning_hack) {
460 if (!confirm(localeStrings.PO_FUND_WARNING_CONFIRM))
464 var want_refresh = false;
465 progressDialog.show(true);
466 fieldmapper.standardRequest(
467 ["open-ils.acq", "open-ils.acq.purchase_order.activate"], {
469 "params": [openils.User.authtoken, PO.id()],
470 "onresponse": function(r) {
471 want_refresh = Boolean(openils.Util.readResponse(r));
473 "oncomplete": function() {
474 progressDialog.hide();
476 location.href = location.href;
483 progressDialog.show(true);
486 fieldmapper.standardRequest(
487 ['open-ils.acq', 'open-ils.acq.purchase_order.split_by_lineitems'],
489 params: [openils.User.authtoken, PO.id()],
490 onresponse : function(r) {
491 list = openils.Util.readResponse(r);
493 oncomplete : function() {
494 progressDialog.hide();
496 location.href = oilsBasePath + '/acq/po/search/' +
503 progressDialog.hide();
508 function updatePoName() {
509 var value = prompt('Enter new purchase order name:', PO.name()); // TODO i18n
510 if(!value || value == PO.name()) return;
513 oncomplete : function(r, cudResults) {
514 var stat = cudResults[0];
516 dojo.byId('acq-po-view-name').innerHTML = value;
521 openils.Util.addOnLoad(init);