1 dojo.require("dojo.string");
2 dojo.require('dijit.layout.ContentPane');
3 dojo.require('openils.PermaCrud');
5 var pcrud = new openils.PermaCrud();
9 var invoiceLinkDialogManager;
11 function AcqPoNoteTable() {
14 this.notesTbody = dojo.byId("acq-po-notes-tbody");
15 this.notesRow = this.notesTbody.removeChild(dojo.byId("acq-po-notes-row"));
17 dojo.byId("acq-po-notes-back-button").onclick = function() { self.hide(); };
18 dojo.byId("acq-po-view-notes").onclick = function() { self.show(); };
20 /* widgets' event properties are cased likeThis */
21 acqPoCreateNoteSubmit.onClick = function() {
22 if (!acqPoCreateNoteText.attr("value")) return;
25 var note = new acqpon();
27 Boolean(acqPoCreateNoteVendorPublic.attr('checked'))
29 note.value(acqPoCreateNoteText.attr("value"));
30 note.purchase_order(PO.id());
34 self.updatePoNotes(note);
36 /* reset fields for next use */
37 acqPoCreateNoteText.attr("value", "");
38 acqPoCreateNoteVendorPublic.attr("checked", false);
41 this.drawPoNote = function(note) {
45 var row = dojo.clone(this.notesRow);
47 nodeByName("value", row).innerHTML = note.value();
49 if (openils.Util.isTrue(note.vendor_public()))
50 nodeByName("vendor_public", row).innerHTML =
51 localeStrings.VENDOR_PUBLIC;
53 nodeByName("delete", row).onclick = function() {
55 self.notesTbody.removeChild(row);
59 if (note.edit_time()) {
60 nodeByName("edit_time", row).innerHTML =
61 dojo.date.locale.format(
62 dojo.date.stamp.fromISOString(note.edit_time()),
63 {"formatLength": "short"}
67 self.notesTbody.appendChild(row);
70 this.drawPoNotes = function() {
75 return (a.edit_time() < b.edit_time()) ? 1 : -1;
80 /* remove old renderings of notes */
81 dojo.empty(this.notesTbody);
83 PO.notes().forEach(function(o) { self.drawPoNote(o); });
86 this.updatePoNotesCount = function() {
87 dojo.byId("acq-po-view-notes").innerHTML = PO.notes().length;
90 this.updatePoNotes = function(newNote) {
95 if (o.ischanged() || o.isnew() || o.isdeleted())
100 if (notes.length < 1)
103 progressDialog.show();
105 fieldmapper.standardRequest(
106 ["open-ils.acq", "open-ils.acq.po_note.cud.batch"], {
108 "params": [openils.User.authtoken, notes],
109 "onresponse": function(r) {
110 var resp = openils.Util.readResponse(r);
112 progressDialog.update(resp);
114 if (!resp.note.isdeleted()) {
115 resp.note.isnew(false);
116 resp.note.ischanged(false);
117 PO.notes().push(resp.note);
121 "oncomplete": function() {
123 /* remove the old changed notes */
129 o.ischanged() || o.isnew() ||
137 progressDialog.hide();
138 self.updatePoNotesCount();
145 this.hide = function() {
146 openils.Util.hide("acq-po-notes-div");
147 liTable.show("list");
150 this.show = function() {
153 openils.Util.show("acq-po-notes-div");
157 function updatePoState(po_info) {
158 var data = po_info[PO.id()];
160 for (var key in data)
166 function cancellationUpdater(r) {
167 var r = openils.Util.readResponse(r);
169 if (r.po) updatePoState(r.po);
171 for (var id in r.li) {
172 liTable.liCache[id].state(r.li[id].state);
173 liTable.liCache[id].cancel_reason(r.li[id].cancel_reason);
174 liTable.updateLiState(liTable.liCache[id]);
177 if (r.lid && liTable.copyCache) {
178 for (var id in r.lid) {
179 if (liTable.copyCache[id]) {
180 liTable.copyCache[id].cancel_reason(
181 r.lid[id].cancel_reason
183 liTable.updateLidState(liTable.copyCache[id]);
190 function makeProviderLink(node, provider) {
193 "href": oilsBasePath + "/conify/global/acq/provider/" + provider.id(),
194 "innerHTML": provider.name() + " (" + provider.code() + ")",
200 function makePrepayWidget(node, prepay) {
202 openils.Util.addCSSClass(node, "oils-acq-po-prepay");
203 node.innerHTML = localeStrings.YES;
205 openils.Util.removeCSSClass(node, "oils-acq-po-prepay");
206 node.innerHTML = localeStrings.NO;
210 function makeCancelWidget(node, labelnode) {
211 openils.Util.hide("acq-po-choose-cancel-reason");
213 if (PO.cancel_reason()) {
214 labelnode.innerHTML = localeStrings.CANCEL_REASON;
215 node.innerHTML = PO.cancel_reason().description() + " (" +
216 PO.cancel_reason().label() + ")";
217 } else if (["on-order", "pending"].indexOf(PO.state()) == -1) {
218 dojo.destroy(this.oldTip);
219 labelnode.innerHTML = "";
222 dojo.destroy(this.oldTip);
223 labelnode.innerHTML = localeStrings.CANCEL;
225 if (!acqPoCancelReasonSubmit._prepared) {
226 var widget = new openils.widget.AutoFieldWidget({
227 "fmField": "cancel_reason",
229 "parentNode": dojo.byId("acq-po-cancel-reason"),
230 "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
235 acqPoCancelReasonSubmit.onClick = function() {
236 if (w.attr("value")) {
237 if (confirm(localeStrings.PO_CANCEL_CONFIRM)) {
238 fieldmapper.standardRequest(
240 "open-ils.acq.purchase_order.cancel"],
243 openils.User.authtoken,
248 "oncomplete": cancellationUpdater
254 acqPoCancelReasonSubmit._prepared = true;
258 openils.Util.show("acq-po-choose-cancel-reason", "inline");
262 function prepareInvoiceFeatures() {
263 /* show the count of related invoices on the "view invoices" button */
264 fieldmapper.standardRequest(
265 ["open-ils.acq", "open-ils.acq.invoice.unified_search.atomic"], {
267 openils.User.authtoken,
268 {"acqpo":[{"id": PO.id()}]},
274 "oncomplete": function(r) {
275 dojo.byId("acq-po-view-invoice-count").innerHTML =
276 openils.Util.readResponse(r).length;
281 /* view invoices button */
282 dijit.byId("acq-po-view-invoice-link").onClick = function() {
283 location.href = oilsBasePath + "/acq/search/unified?so=" +
284 base64Encode({"jub":[{"purchase_order": PO.id()}]}) +
288 /* create invoice button */
289 dijit.byId("acq-po-create-invoice-link").onClick = function() {
290 location.href = oilsBasePath +
291 "/acq/invoice/view?create=1&attach_po=" + PO.id();
294 if (!invoiceLinkDialogManager)
295 invoiceLinkDialogManager = new InvoiceLinkDialogManager("po", PO);
297 openils.Util.show("acq-po-view-invoices", "table-row");
300 function renderPo() {
301 dojo.byId("acq-po-view-id").innerHTML = PO.id();
302 dojo.byId("acq-po-view-name").innerHTML = PO.name();
304 dojo.byId("acq-po-view-provider"),
307 dojo.byId("acq-po-view-total-li").innerHTML = PO.lineitem_count();
308 dojo.byId("acq-po-view-total-enc").innerHTML = PO.amount_encumbered().toFixed(2);
309 dojo.byId("acq-po-view-total-spent").innerHTML = PO.amount_spent().toFixed(2);
310 dojo.byId("acq-po-view-state").innerHTML = PO.state(); // TODO i18n
312 dojo.byId("acq-po-view-prepay"),
313 openils.Util.isTrue(PO.prepayment_required())
316 dojo.byId("acq-po-view-cancel-reason"),
317 dojo.byId("acq-po-cancel-label")
319 dojo.byId("acq-po-view-notes").innerHTML = PO.notes().length;
321 if(PO.state() == "pending") {
322 openils.Util.show("acq-po-activate", "table-row");
323 checkCouldActivatePo();
324 if (PO.lineitem_count() > 1)
325 openils.Util.show("acq-po-split");
328 prepareInvoiceFeatures();
333 /* set up li table */
334 liTable = new AcqLiTable();
337 liTable.poUpdateCallback = updatePoState;
339 /* set up po notes table */
340 poNoteTable = new AcqPoNoteTable();
342 /* retrieve data and populate */
343 fieldmapper.standardRequest(
344 ['open-ils.acq', 'open-ils.acq.purchase_order.retrieve'],
346 params: [openils.User.authtoken, poId, {
347 "flesh_provider": true,
348 "flesh_price_summary": true,
349 "flesh_lineitem_count": true,
352 oncomplete: function(r) {
353 PO = openils.Util.readResponse(r); /* save PO globally */
359 var totalEstimated = 0;
360 fieldmapper.standardRequest(
361 ['open-ils.acq', 'open-ils.acq.lineitem.search'],
363 params: [openils.User.authtoken, {purchase_order:poId}, {flesh_attrs:true, flesh_notes:true, flesh_cancel_reason:true}],
364 onresponse: function(r) {
365 liTable.show('list');
366 var li = openils.Util.readResponse(r);
367 totalEstimated += (Number(li.item_count() || 0) * Number(li.estimated_unit_price() || 0));
368 liTable.addLineitem(li);
371 oncomplete : function() {
372 dojo.byId("acq-po-view-total-estimated").innerHTML = totalEstimated.toFixed(2);
378 function checkCouldActivatePo() {
379 var d = dojo.byId("acq-po-activate-checking");
380 var a = dojo.byId("acq-po-activate-link");
381 d.innerHTML = localeStrings.PO_CHECKING;
385 fieldmapper.standardRequest(
386 ["open-ils.acq", "open-ils.acq.purchase_order.activate.dry_run"], {
387 "params": [openils.User.authtoken, PO.id()],
389 "onresponse": function(r) {
390 if ((r = openils.Util.readResponse(r, true /* eventOk */))) {
391 if (typeof(r.textcode) != "undefined") {
393 case "ACQ_FUND_EXCEEDS_STOP_PERCENT":
396 case "ACQ_FUND_EXCEEDS_WARN_PERCENT":
403 "oncomplete": function() {
404 /* XXX in the future, this might be tweaked to display info
405 * about more than one stop or warning event from the ML. */
406 if (!(warnings.length || stops.length)) {
407 d.innerHTML = localeStrings.PO_COULD_ACTIVATE;
408 openils.Util.show(a, "inline");
412 dojo.string.substitute(
413 localeStrings.PO_STOP_BLOCKS_ACTIVATION, [
414 stops[0].payload.fund.code(),
415 stops[0].payload.fund.year()
418 openils.Util.hide(a);
420 PO._warning_hack = true;
422 dojo.string.substitute(
423 localeStrings.PO_WARNING_NO_BLOCK_ACTIVATION, [
424 warnings[0].payload.fund.code(),
425 warnings[0].payload.fund.year()
428 openils.Util.show(a, "inline");
436 function activatePo() {
437 if (openils.Util.isTrue(PO.prepayment_required())) {
438 if (!confirm(localeStrings.PREPAYMENT_REQUIRED_REMINDER))
442 if (PO._warning_hack) {
443 if (!confirm(localeStrings.PO_FUND_WARNING_CONFIRM))
447 var want_refresh = false;
448 progressDialog.show(true);
449 fieldmapper.standardRequest(
450 ["open-ils.acq", "open-ils.acq.purchase_order.activate"], {
452 "params": [openils.User.authtoken, PO.id()],
453 "onresponse": function(r) {
454 want_refresh = Boolean(openils.Util.readResponse(r));
456 "oncomplete": function() {
457 progressDialog.hide();
459 location.href = location.href;
466 progressDialog.show(true);
469 fieldmapper.standardRequest(
470 ['open-ils.acq', 'open-ils.acq.purchase_order.split_by_lineitems'],
472 params: [openils.User.authtoken, PO.id()],
473 onresponse : function(r) {
474 list = openils.Util.readResponse(r);
476 oncomplete : function() {
477 progressDialog.hide();
479 location.href = oilsBasePath + '/acq/po/search/' +
486 progressDialog.hide();
491 function updatePoName() {
492 var value = prompt('Enter new purchase order name:', PO.name()); // TODO i18n
493 if(!value || value == PO.name()) return;
496 oncomplete : function(r, cudResults) {
497 var stat = cudResults[0];
499 dojo.byId('acq-po-view-name').innerHTML = value;
504 openils.Util.addOnLoad(init);