]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/po/view_po.js
Acq: several interface improvements
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / po / view_po.js
1 dojo.require("dijit.form.Button");
2 dojo.require("dojo.string");
3 dojo.require('dijit.layout.ContentPane');
4 dojo.require('openils.PermaCrud');
5
6 var pcrud = new openils.PermaCrud();
7 var PO = null;
8 var liTable;
9 var poItemTable;
10 var poNoteTable;
11 var invoiceLinkDialogManager;
12
13 function AcqPoNoteTable() {
14     var self = this;
15
16     this.notesTbody = dojo.byId("acq-po-notes-tbody");
17     this.notesRow = this.notesTbody.removeChild(dojo.byId("acq-po-notes-row"));
18
19     dojo.byId("acq-po-notes-back-button").onclick = function() { self.hide(); };
20     dojo.byId("acq-po-view-notes").onclick = function() { self.show(); };
21
22     /* widgets' event properties are cased likeThis */
23     acqPoCreateNoteSubmit.onClick = function() {
24         if (!acqPoCreateNoteText.attr("value")) return;
25
26         /* prep new note */
27         var note = new acqpon();
28         note.vendor_public(
29             Boolean(acqPoCreateNoteVendorPublic.attr('checked'))
30         );
31         note.value(acqPoCreateNoteText.attr("value"));
32         note.purchase_order(PO.id());
33         note.isnew(true);
34
35         /* save it */
36         self.updatePoNotes(note);
37
38         /* reset fields for next use */
39         acqPoCreateNoteText.attr("value", "");
40         acqPoCreateNoteVendorPublic.attr("checked", false);
41     };
42
43     this.drawPoNote = function(note) {
44         if (note.isdeleted())
45             return;
46
47         var row = dojo.clone(this.notesRow);
48
49         nodeByName("value", row).innerHTML = note.value();
50
51         if (openils.Util.isTrue(note.vendor_public()))
52             nodeByName("vendor_public", row).innerHTML =
53                 localeStrings.VENDOR_PUBLIC;
54
55         nodeByName("delete", row).onclick = function() {
56             note.isdeleted(true);
57             self.notesTbody.removeChild(row);
58             self.updatePoNotes();
59         };
60
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"}
66                 );
67         }
68
69         self.notesTbody.appendChild(row);
70     };
71
72     this.drawPoNotes = function() {
73         /* sort */
74         PO.notes(
75             PO.notes().sort(
76                 function(a, b) {
77                     return (a.edit_time() < b.edit_time()) ? 1 : -1;
78                 }
79             )
80         );
81
82         /* remove old renderings of notes */
83         dojo.empty(this.notesTbody);
84
85         PO.notes().forEach(function(o) { self.drawPoNote(o); });
86     };
87
88     this.updatePoNotesCount = function() {
89         dojo.byId("acq-po-view-notes").innerHTML =
90             "(" + PO.notes().length + ")";
91     };
92
93     this.updatePoNotes = function(newNote) {
94         var notes = newNote ?
95             [newNote] :
96             PO.notes().filter(
97                 function(o) {
98                     if (o.ischanged() || o.isnew() || o.isdeleted())
99                         return o;
100                 }
101             );
102
103         if (notes.length < 1)
104             return;
105
106         progressDialog.show();
107
108         fieldmapper.standardRequest(
109             ["open-ils.acq", "open-ils.acq.po_note.cud.batch"], {
110                 "async": true,
111                 "params": [openils.User.authtoken, notes],
112                 "onresponse": function(r) {
113                     var resp = openils.Util.readResponse(r);
114                     if (resp) {
115                         progressDialog.update(resp);
116
117                         if (!resp.note.isdeleted()) {
118                             resp.note.isnew(false);
119                             resp.note.ischanged(false);
120                             PO.notes().push(resp.note);
121                         }
122                     }
123                 },
124                 "oncomplete": function() {
125                     if (!newNote) {
126                         /* remove the old changed notes */
127                         var list = [];
128                         PO.notes(
129                             PO.notes().filter(
130                                 function(o) {
131                                     return (!(
132                                         o.ischanged() || o.isnew() ||
133                                         o.isdeleted()
134                                     ));
135                                 }
136                             )
137                         );
138                     }
139
140                     progressDialog.hide();
141                     self.updatePoNotesCount();
142                     self.drawPoNotes();
143                 }
144             }
145         );
146     };
147
148     this.hide = function() {
149         openils.Util.hide("acq-po-notes-div");
150         liTable.show("list");
151         poItemTable.show();
152     };
153
154     this.show = function() {
155         liTable.hide();
156         poItemTable.hide();
157         self.drawPoNotes();
158         openils.Util.show("acq-po-notes-div");
159     };
160 }
161
162 function updatePoState(po_info) {
163     var data = po_info[PO.id()];
164     if (data) {
165         for (var key in data)
166             PO[key](data[key]);
167         renderPo();
168     }
169 }
170
171 function cancellationUpdater(r) {
172     var r = openils.Util.readResponse(r);
173     if (r) {
174         if (r.po) updatePoState(r.po);
175         if (r.li) {
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]);
180             }
181         }
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
187                     );
188                     liTable.updateLidState(liTable.copyCache[id]);
189                 }
190             }
191         }
192     }
193 }
194
195 function makeProviderLink(node, provider) {
196     return dojo.create(
197         "a", {
198             "href": oilsBasePath + "/conify/global/acq/provider/" + provider.id(),
199             "innerHTML": provider.name() + " (" + provider.code() + ")",
200         },
201         node,
202         "only"
203     );
204 }
205 function makePrepayWidget(node, prepay) {
206     if (prepay) {
207         openils.Util.addCSSClass(node, "oils-acq-po-prepay");
208         node.innerHTML = localeStrings.YES;
209     } else {
210         openils.Util.removeCSSClass(node, "oils-acq-po-prepay");
211         node.innerHTML = localeStrings.NO;
212     }
213 }
214
215 function makeCancelWidget(node, labelnode) {
216     openils.Util.hide("acq-po-choose-cancel-reason");
217
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 = "";
225         node.innerHTML = "";
226     } else {
227         dojo.destroy(this.oldTip);
228         labelnode.innerHTML = localeStrings.CANCEL;
229         node.innerHTML = "";
230         if (!acqPoCancelReasonSubmit._prepared) {
231             var widget = new openils.widget.AutoFieldWidget({
232                 "fmField": "cancel_reason",
233                 "fmClass": "acqpo",
234                 "parentNode": dojo.byId("acq-po-cancel-reason"),
235                 "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
236                 "forceSync": true
237             });
238             widget.build(
239                 function(w, ww) {
240                     acqPoCancelReasonSubmit.onClick = function() {
241                         if (w.attr("value")) {
242                             if (confirm(localeStrings.PO_CANCEL_CONFIRM)) {
243                                 fieldmapper.standardRequest(
244                                     ["open-ils.acq",
245                                         "open-ils.acq.purchase_order.cancel"],
246                                     {
247                                         "params": [
248                                             openils.User.authtoken,
249                                             PO.id(), 
250                                             w.attr("value")
251                                         ],
252                                         "async": true,
253                                         "oncomplete": cancellationUpdater
254                                     }
255                                 );
256                             }
257                         }
258                     };
259                     acqPoCancelReasonSubmit._prepared = true;
260                 }
261             );
262         }
263         openils.Util.show("acq-po-choose-cancel-reason", "inline");
264     }
265 }
266
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"], {
271             "params": [
272                 openils.User.authtoken,
273                 {"acqpo":[{"id": PO.id()}]},
274                 null,
275                 null,
276                 {"id_list": true}
277             ],
278             "async": true,
279             "oncomplete": function(r) {
280                 dojo.byId("acq-po-view-invoice-count").innerHTML =
281                     openils.Util.readResponse(r).length;
282             }
283         }
284     );
285
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()}]}) +
290             "&rt=invoice";
291     };
292
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();
297     };
298
299     openils.Util.show("acq-po-invoice-stuff", "table-cell");
300 }
301
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();
309     makeProviderLink(
310         dojo.byId("acq-po-view-provider"),
311         PO.provider()
312     );
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
317
318     if(PO.order_date()) {
319         openils.Util.show('acq-po-activated-on', 'inline');
320         liTable.enableActionsDropdownOptions("ao"); /* activated */
321
322         dojo.byId('acq-po-activated-on').innerHTML = 
323             dojo.string.substitute(
324                 localeStrings.PO_ACTIVATED_ON, [
325                     openils.Util.timeStamp(PO.order_date(), {formatLength:'short'})
326                 ]
327             );
328         /* These are handled another way now */
329 //        if (po_state == "on-order" || po_state == "cancelled") {
330 //            dojo.removeAttr('receive_lineitems', 'disabled');
331 //        } else if(po_state == "received") {
332 //            dojo.removeAttr('rollback_receive_lineitems', 'disabled');
333 //        }
334
335         /* cancel widgets only make sense for activate (ordered) POs */
336         makeCancelWidget(
337             dojo.byId("acq-po-view-cancel-reason"),
338             dojo.byId("acq-po-cancel-label")
339         );
340
341         /* likewise for invoice features */
342         openils.Util.show("acq-po-invoice-label", "table-cell");
343         prepareInvoiceFeatures();
344     } else {
345         /* These things only make sense for not-ordered-yet POs */
346
347         liTable.enableActionsDropdownOptions("po");
348
349         openils.Util.show("acq-po-zero-activate-label", "table-cell");
350         openils.Util.show("acq-po-zero-activate", "table-cell");
351
352         openils.Util.show("acq-po-item-table-controls");
353     }
354
355     makePrepayWidget(
356         dojo.byId("acq-po-view-prepay"),
357         openils.Util.isTrue(PO.prepayment_required())
358     );
359     // dojo.byId("acq-po-view-notes").innerHTML = PO.notes().length;
360     poNoteTable.updatePoNotesCount();
361
362     if (po_state == "pending") {
363         checkCouldActivatePo();
364         if (PO.lineitem_count() > 1)
365             openils.Util.show("acq-po-split");
366     } else {
367         dojo.byId("acq-po-activate-checking").innerHTML = localeStrings.NO;
368     }
369
370     // XXX we probably don't *always* need to do this...
371     poItemTable.reset();
372     PO.po_items().forEach(
373         function(po_item) { poItemTable.addItem(po_item); }
374     );
375     poItemTable.show();
376
377     dojo.attr(
378         "acq-po-view-history", "href",
379         oilsBasePath + "/acq/po/history/" + PO.id()
380     );
381     openils.Util.show("acq-po-view-history", "inline");
382
383     
384     /* if we got here from the search/invoice page with a focused LI,
385      * return to the previous page with the same LI focused */
386     var cgi = new openils.CGI();
387     var source = cgi.param('source');
388     var focus_li = cgi.param('focus_li');
389     if (focus_li && source) {
390         dojo.forEach(
391             ['search', 'invoice'], // perhaps a wee bit too loose
392             function(srcType) {
393                 if (source.match(new RegExp(srcType))) {
394                     openils.Util.show('acq-po-return-to-' + srcType);
395                     var newCgi = new openils.CGI({url : source});
396                     newCgi.param('focus_li', focus_li);
397                     dojo.byId('acq-po-return-to-' + srcType + '-button').onclick = function() {
398                         location.href = newCgi.url();
399                     }
400                 }
401             }
402         );
403     }
404 }
405
406
407 function init() {
408     /* set up li table */
409     liTable = new AcqLiTable();
410     liTable.reset();
411     liTable.isPO = poId;
412     liTable.poUpdateCallback = updatePoState;
413
414     /* set up po notes table */
415     poNoteTable = new AcqPoNoteTable();
416
417     /* retrieve data and populate */
418     fieldmapper.standardRequest(
419         ['open-ils.acq', 'open-ils.acq.purchase_order.retrieve'],
420         {   async: true,
421             params: [openils.User.authtoken, poId, {
422                 "flesh_provider": true,
423                 "flesh_price_summary": true,
424                 "flesh_lineitem_count": true,
425                 "flesh_notes": true,
426                 "flesh_po_items": true
427             }],
428             oncomplete: function(r) {
429                 PO = openils.Util.readResponse(r); /* save PO globally */
430
431                 /* po item table */
432                 poItemTable = new PoItemTable(PO, pcrud);
433
434                 liTable.testOrderIdentPerms( PO.ordering_agency(), init2);
435
436                 renderPo();
437             }
438         }
439     );
440
441 }
442
443 function init2() {
444
445     var totalEstimated = 0;
446     var zeroLi = true;
447     fieldmapper.standardRequest(
448         ['open-ils.acq', 'open-ils.acq.lineitem.search'],
449         {   async: true,
450             params: [
451                 openils.User.authtoken, 
452                 [{purchase_order:poId}, {"order_by": {"jub": "id ASC"}}], 
453                 {flesh_attrs:true, flesh_notes:true, flesh_cancel_reason:true, clear_marc:true}
454             ],
455             onresponse: function(r) {
456                 zeroLi = false;
457                 liTable.show('list');
458                 var li = openils.Util.readResponse(r);
459                 // TODO: Add po_item's to total estimated amount
460                 totalEstimated += (Number(li.item_count() || 0) * Number(li.estimated_unit_price() || 0));
461                 liTable.addLineitem(li);
462             },
463
464             oncomplete : function() {
465                 dojo.byId("acq-po-view-total-estimated").innerHTML = totalEstimated.toFixed(2);
466                 if (liFocus) liTable.drawCopies(liFocus);
467                 if(zeroLi) openils.Util.show('acq-po-no-lineitems');
468             }
469         }
470     );
471
472     pcrud.search(
473         'acqedim', 
474         {purchase_order : poId}, 
475         {
476             id_list : true,
477             oncomplete : function(r) {
478                 var resp = openils.Util.readResponse(r);
479                 // TODO: I18n
480                 if(resp) {
481                     dojo.byId('acq-po-view-edi-messages').innerHTML = '(' + resp.length + ')';
482                     dojo.byId('acq-po-view-edi-messages').setAttribute('href', oilsBasePath + '/acq/po/edi_messages/' + poId);
483                 } else {
484                     dojo.byId('acq-po-view-edi-messages').innerHTML = '0';
485                     dojo.byId('acq-po-view-edi-messages').setAttribute('href', '');
486                 }
487             }
488         }
489     );
490 }
491
492 function checkCouldActivatePo() {
493     var d = dojo.byId("acq-po-activate-checking");
494     var a = dojo.byId("acq-po-activate-link");  /* <span> not <a> now, but no diff */
495     d.innerHTML = localeStrings.PO_CHECKING;
496     var warnings = [];
497     var stops = [];
498     var other = [];
499
500     fieldmapper.standardRequest(
501         ["open-ils.acq", "open-ils.acq.purchase_order.activate.dry_run"], {
502             "params": [
503                 openils.User.authtoken,
504                 PO.id(),
505                 null,  // vandelay options
506                 {zero_copy_activate : dojo.byId('acq-po-activate-zero-copies').checked}
507             ],
508             "async": true,
509             "onresponse": function(r) {
510                 if ((r = openils.Util.readResponse(r, true /* eventOk */))) {
511                     if (typeof(r.textcode) != "undefined") {
512                         switch(r.textcode) {
513                             case "ACQ_FUND_EXCEEDS_STOP_PERCENT":
514                                 stops.push(r);
515                                 break;
516                             case "ACQ_FUND_EXCEEDS_WARN_PERCENT":
517                                 warnings.push(r);
518                                 break;
519                             default:
520                                 other.push(r);
521                         }
522                     }
523                 }
524             },
525             "oncomplete": function() {
526                 /* XXX in the future, this might be tweaked to display info
527                  * about more than one stop or warning event from the ML. */
528                 if (!(warnings.length || stops.length || other.length)) {
529                     d.innerHTML = localeStrings.PO_COULD_ACTIVATE;
530                     openils.Util.show(a, "inline");
531                     activatePoButton.attr("disabled", false);
532                 } else {
533                     if (other.length) {
534                         /* XXX make the textcode part a tooltip one day */
535                         d.innerHTML = localeStrings.NO + ": " +
536                             other[0].desc + " (" + other[0].textcode + ")";
537                         openils.Util.hide(a);
538                         
539                         if (other[0].textcode == 'ACQ_LINEITEM_NO_COPIES') {
540                             // when LIs w/ zero LIDs are present, list them
541                             fieldmapper.standardRequest(
542                                 [   'open-ils.acq', 
543                                     'open-ils.acq.purchase_order.no_copy_lineitems.id_list.authoritative.atomic' ],
544                                 {   async : true, 
545                                     params : [openils.User.authtoken, poId],
546                                     oncomplete : function(r) {
547                                         var ids = openils.Util.readResponse(r);
548                                         d.innerHTML += ' (' + ids + ')';
549                                     }
550                                 }
551                             );
552                         }
553                     } else if (stops.length) {
554                         d.innerHTML =
555                             dojo.string.substitute(
556                                 localeStrings.PO_STOP_BLOCKS_ACTIVATION, [
557                                     stops[0].payload.fund.code(),
558                                     stops[0].payload.fund.year()
559                                 ]
560                             );
561                         openils.Util.hide(a);
562                     } else {
563                         PO._warning_hack = true;
564                         d.innerHTML =
565                             dojo.string.substitute(
566                                 localeStrings.PO_WARNING_NO_BLOCK_ACTIVATION, [
567                                     warnings[0].payload.fund.code(),
568                                     warnings[0].payload.fund.year()
569                                 ]
570                             );
571                         openils.Util.show(a, "inline");
572                         activatePoButton.attr("disabled", false);
573                     }
574                 }
575             }
576         }
577     );
578 }
579
580 function activatePo() {
581     activatePoButton.attr("disabled", true);
582
583     if (openils.Util.isTrue(PO.prepayment_required())) {
584         if (!confirm(localeStrings.PREPAYMENT_REQUIRED_REMINDER)) {
585             activatePoButton.attr("disabled", false);
586             return false;
587         }
588     }
589
590     if (PO._warning_hack) {
591         if (!confirm(localeStrings.PO_FUND_WARNING_CONFIRM)) {
592             activatePoButton.attr("disabled", false);
593             return false;
594         }
595     }
596
597     liTable.showAssetCreator(activatePoStage2);
598 }
599
600 function activatePoStage2() {
601
602     var want_refresh = false;
603     progressDialog.show(true);
604     fieldmapper.standardRequest(
605         ["open-ils.acq", "open-ils.acq.purchase_order.activate"], {
606             "async": true,
607             "params": [
608                 openils.User.authtoken,
609                 PO.id(),
610                 null,  // vandelay options
611                 {zero_copy_activate : dojo.byId('acq-po-activate-zero-copies').checked}
612             ],
613             "onresponse": function(r) {
614                 progressDialog.hide();
615                 activatePoButton.attr("disabled", false);
616                 want_refresh = Boolean(openils.Util.readResponse(r));
617             },
618             "oncomplete": function() {
619                 if (want_refresh)
620                     location.href = location.href;
621             }
622         }
623     );
624 }
625
626 function splitPo() {
627     progressDialog.show(true);
628     try {
629         var list;
630         fieldmapper.standardRequest(
631             ['open-ils.acq', 'open-ils.acq.purchase_order.split_by_lineitems'],
632             {   async: true,
633                 params: [openils.User.authtoken, PO.id()],
634                 onresponse : function(r) {
635                     list = openils.Util.readResponse(r);
636                 },
637                 oncomplete : function() {
638                     progressDialog.hide();
639                     if (list) {
640                         location.href = oilsBasePath + '/acq/po/search/' +
641                             list.join(",");
642                     }
643                 }
644             }
645         );
646     } catch(E) {
647         progressDialog.hide();
648         alert(E);
649     }
650 }
651
652 function updatePoName() {
653     var value = prompt('Enter new purchase order name:', PO.name()); // TODO i18n
654     if(!value || value == PO.name()) return;
655     PO.name(value);
656     pcrud.update(PO, {
657         oncomplete : function(r, cudResults) {
658             var stat = cudResults[0];
659             if(stat)
660                 dojo.byId('acq-po-view-name').innerHTML = value;
661         }
662     });
663 }
664
665 openils.Util.addOnLoad(init);