]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/po/view_po.js
Acq: cancel POs, lineitems, or individual copies from the PO interface
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / po / view_po.js
1 dojo.require('dijit.layout.ContentPane');
2 dojo.require('openils.User');
3 dojo.require('openils.Util');
4 dojo.require('openils.PermaCrud');
5
6 var pcrud = new openils.PermaCrud();
7 var PO = null;
8 var liTable;
9 var poNoteTable;
10
11 function AcqPoNoteTable() {
12     var self = this;
13
14     this.notesTbody = dojo.byId("acq-po-notes-tbody");
15     this.notesRow = this.notesTbody.removeChild(dojo.byId("acq-po-notes-row"));
16
17     dojo.byId("acq-po-notes-back-button").onclick = function() { self.hide(); };
18     dojo.byId("acq-po-view-notes").onclick = function() { self.show(); };
19
20     /* widgets' event properties are cased likeThis */
21     acqPoCreateNoteSubmit.onClick = function() {
22         if (!acqPoCreateNoteText.attr("value")) return;
23
24         /* prep new note */
25         var note = new acqpon();
26         note.vendor_public(
27             Boolean(acqPoCreateNoteVendorPublic.attr('checked'))
28         );
29         note.value(acqPoCreateNoteText.attr("value"));
30         note.purchase_order(PO.id());
31         note.isnew(true);
32
33         /* save it */
34         self.updatePoNotes(note);
35
36         /* reset fields for next use */
37         acqPoCreateNoteText.attr("value", "");
38         acqPoCreateNoteVendorPublic.attr("checked", false);
39     };
40
41     this.drawPoNote = function(note) {
42         if (note.isdeleted())
43             return;
44
45         var row = dojo.clone(this.notesRow);
46
47         nodeByName("value", row).innerHTML = note.value();
48
49         if (note.vendor_public() == "t")
50             nodeByName("vendor_public", row).innerHTML =
51                 localeStrings.VENDOR_PUBLIC;
52
53         nodeByName("delete", row).onclick = function() {
54             note.isdeleted(true);
55             self.notesTbody.removeChild(row);
56             self.updatePoNotes();
57         };
58
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"}
64                 );
65         }
66
67         self.notesTbody.appendChild(row);
68     };
69
70     this.drawPoNotes = function() {
71         /* sort */
72         PO.notes(
73             PO.notes().sort(
74                 function(a, b) {
75                     return (a.edit_time() < b.edit_time()) ? 1 : -1;
76                 }
77             )
78         );
79
80         /* remove old renderings of notes */
81         dojo.empty(this.notesTbody);
82
83         PO.notes().forEach(function(o) { self.drawPoNote(o); });
84     };
85
86     this.updatePoNotesCount = function() {
87         dojo.byId("acq-po-view-notes").innerHTML = PO.notes().length;
88     };
89
90     this.updatePoNotes = function(newNote) {
91         var notes = newNote ?
92             [newNote] :
93             PO.notes().filter(
94                 function(o) {
95                     if (o.ischanged() || o.isnew() || o.isdeleted())
96                         return o;
97                 }
98             );
99
100         if (notes.length < 1)
101             return;
102
103         progressDialog.show();
104
105         fieldmapper.standardRequest(
106             ["open-ils.acq", "open-ils.acq.po_note.cud.batch"], {
107                 "async": true,
108                 "params": [openils.User.authtoken, notes],
109                 "onresponse": function(r) {
110                     var resp = openils.Util.readResponse(r);
111                     if (resp) {
112                         progressDialog.update(resp);
113
114                         if (!resp.note.isdeleted()) {
115                             resp.note.isnew(false);
116                             resp.note.ischanged(false);
117                             PO.notes().push(resp.note);
118                         }
119                     }
120                 },
121                 "oncomplete": function() {
122                     if (!newNote) {
123                         /* remove the old changed notes */
124                         var list = [];
125                         PO.notes(
126                             PO.notes().filter(
127                                 function(o) {
128                                     return (!(
129                                         o.ischanged() || o.isnew() ||
130                                         o.isdeleted()
131                                     ));
132                                 }
133                             )
134                         );
135                     }
136
137                     progressDialog.hide();
138                     self.updatePoNotesCount();
139                     self.drawPoNotes();
140                 }
141             }
142         );
143     };
144
145     this.hide = function() {
146         openils.Util.hide("acq-po-notes-div");
147         liTable.show("list");
148     };
149
150     this.show = function() {
151         liTable.hide();
152         self.drawPoNotes();
153         openils.Util.show("acq-po-notes-div");
154     };
155 }
156
157 function updatePoState(po_info) {
158     var data = po_info[PO.id()];
159     if (data) {
160         for (var key in data)
161             PO[key](data[key]);
162         renderPo();
163     }
164 }
165
166 function cancellationUpdater(r) {
167     var r = openils.Util.readResponse(r);
168     if (r) {
169         if (r.po) updatePoState(r.po);
170         if (r.li) {
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]);
175             }
176         }
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
182                     );
183                     liTable.updateLidState(liTable.copyCache[id]);
184                 }
185             }
186         }
187     }
188 }
189
190 function makeCancelWidget(node, labelnode) {
191     openils.Util.hide("acq-po-choose-cancel-reason");
192
193     if (PO.cancel_reason()) {
194         labelnode.innerHTML = localeStrings.CANCEL_REASON;
195         node.innerHTML = PO.cancel_reason().description() + " (" +
196             PO.cancel_reason().label() + ")";
197     } else if (["on-order", "pending"].indexOf(PO.state()) == -1) {
198         dojo.destroy(this.oldTip);
199         labelnode.innerHTML = "";
200         node.innerHTML = "";
201     } else {
202         dojo.destroy(this.oldTip);
203         labelnode.innerHTML = localeStrings.CANCEL;
204         node.innerHTML = "";
205         if (!acqPoCancelReasonSubmit._prepared) {
206             var widget = new openils.widget.AutoFieldWidget({
207                 "fmField": "cancel_reason",
208                 "fmClass": "acqpo",
209                 "parentNode": dojo.byId("acq-po-cancel-reason"),
210                 "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
211                 "forceSync": true
212             });
213             widget.build(
214                 function(w, ww) {
215                     acqPoCancelReasonSubmit.onClick = function() {
216                         if (w.attr("value")) {
217                             if (confirm(localeStrings.PO_CANCEL_CONFIRM)) {
218                                 fieldmapper.standardRequest(
219                                     ["open-ils.acq",
220                                         "open-ils.acq.purchase_order.cancel"],
221                                     {
222                                         "params": [
223                                             openils.User.authtoken,
224                                             PO.id(), 
225                                             w.attr("value")
226                                         ],
227                                         "async": true,
228                                         "oncomplete": cancellationUpdater
229                                     }
230                                 );
231                             }
232                         }
233                     };
234                     acqPoCancelReasonSubmit._prepared = true;
235                 }
236             );
237         }
238         openils.Util.show("acq-po-choose-cancel-reason", "inline");
239     }
240 }
241
242 function renderPo() {
243     dojo.byId("acq-po-view-id").innerHTML = PO.id();
244     dojo.byId("acq-po-view-name").innerHTML = PO.name();
245     dojo.byId("acq-po-view-total-li").innerHTML = PO.lineitem_count();
246     dojo.byId("acq-po-view-total-enc").innerHTML = PO.amount_encumbered();
247     dojo.byId("acq-po-view-total-spent").innerHTML = PO.amount_spent();
248     dojo.byId("acq-po-view-state").innerHTML = PO.state(); // TODO i18n
249     makeCancelWidget(
250         dojo.byId("acq-po-view-cancel-reason"),
251         dojo.byId("acq-po-cancel-label")
252     );
253     dojo.byId("acq-po-view-notes").innerHTML = PO.notes().length;
254
255     if(PO.state() == "pending") {
256         openils.Util.show("acq-po-activate");
257         if (PO.lineitem_count() > 1)
258             openils.Util.show("acq-po-split");
259     }
260 }
261
262
263 function init() {
264     /* set up li table */
265     liTable = new AcqLiTable();
266     liTable.reset();
267     liTable.isPO = poId;
268     liTable.poUpdateCallback = updatePoState;
269
270     /* set up po notes table */
271     poNoteTable = new AcqPoNoteTable();
272
273     /* retrieve data and populate */
274     fieldmapper.standardRequest(
275         ['open-ils.acq', 'open-ils.acq.purchase_order.retrieve'],
276         {   async: true,
277             params: [openils.User.authtoken, poId, {
278                 "flesh_price_summary": true,
279                 "flesh_lineitem_count": true,
280                 "flesh_notes": true
281             }],
282             oncomplete: function(r) {
283                 PO = openils.Util.readResponse(r); /* save PO globally */
284                 renderPo();
285             }
286         }
287     );
288
289     fieldmapper.standardRequest(
290         ['open-ils.acq', 'open-ils.acq.lineitem.search'],
291         {   async: true,
292 params: [openils.User.authtoken, {purchase_order:poId}, {flesh_attrs:true, flesh_notes:true, flesh_cancel_reason:true}],
293             onresponse: function(r) {
294                 liTable.show('list');
295                 liTable.addLineitem(openils.Util.readResponse(r));
296             }
297         }
298     );
299 }
300
301 function activatePo() {
302     progressDialog.show(true);
303     try {
304         fieldmapper.standardRequest(
305             ['open-ils.acq', 'open-ils.acq.purchase_order.activate'],
306             {   async: true,
307                 params: [openils.User.authtoken, PO.id()],
308                 oncomplete : function() {
309                     location.href = location.href;
310                 }
311             }
312         );
313     } catch(E) {
314         progressDialog.hide();
315     }
316 }
317
318 function splitPo() {
319     progressDialog.show(true);
320     try {
321         var list;
322         fieldmapper.standardRequest(
323             ['open-ils.acq', 'open-ils.acq.purchase_order.split_by_lineitems'],
324             {   async: true,
325                 params: [openils.User.authtoken, PO.id()],
326                 onresponse : function(r) {
327                     list = openils.Util.readResponse(r);
328                 },
329                 oncomplete : function() {
330                     progressDialog.hide();
331                     if (list) {
332                         location.href = oilsBasePath + '/eg/acq/po/search/' +
333                             list.join(",");
334                     }
335                 }
336             }
337         );
338     } catch(E) {
339         progressDialog.hide();
340         alert(E);
341     }
342 }
343
344 function updatePoName() {
345     var value = prompt('Enter new purchase order name:', PO.name()); // TODO i18n
346     if(!value || value == PO.name()) return;
347     PO.name(value);
348     pcrud.update(PO, {
349         oncomplete : function(r, cudResults) {
350             var stat = cudResults[0];
351             if(stat)
352                 dojo.byId('acq-po-view-name').innerHTML = value;
353         }
354     });
355 }
356
357 openils.Util.addOnLoad(init);