]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/financial/claim_eligible.js
Merge branch 'master' of git.evergreen-ils.org:Evergreen-DocBook into doc_consolidati...
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / financial / claim_eligible.js
1 dojo.require("dijit.form.Button");
2 dojo.require("dijit.form.TextBox");
3 dojo.require("openils.acq.Lineitem");
4 dojo.require("openils.widget.OrgUnitFilteringSelect");
5 dojo.require("openils.widget.ProgressDialog");
6 dojo.require("openils.widget.AutoFieldWidget");
7
8 var eligibleLiTable;
9
10 function nodeByName(n, c) { return dojo.query("[name='" + n + "']", c)[0]; }
11
12 function EligibleLiTable(filter) {
13     var self = this;
14
15     this.filter = filter;
16     this.liCache = {};
17     this.numClaimableLids = {};
18
19     this.claimNote = dijit.byId("acq-eligible-claim-note");
20     this.table = dojo.byId("acq-eligible-li-table");
21     this.tBody = dojo.query("tbody", this.table)[0];
22     this.tHead = dojo.query("thead", this.table)[0];
23     [this.rowTemplate, this.emptyTemplate] =
24         dojo.query("tr", this.tBody).map(
25             function(o) { return self.tBody.removeChild(o); }
26         );
27
28     nodeByName("selector_all", this.tHead).onclick = function() {
29         var value = this.checked;
30         dojo.query("[name='selector']", self.tBody).forEach(
31             function(o) { o.checked = value; }
32         );
33     };
34
35     new openils.widget.AutoFieldWidget({
36         "fmClass": "acqclt",
37         "selfReference": true,
38         "dijitArgs": {"required": true},
39         "parentNode": dojo.byId("acq-eligible-claim-type")
40     }).build(function(w) { self.claimType = w; });
41
42     new openils.User().buildPermOrgSelector(
43         "VIEW_PURCHASE_ORDER", orderingAgency, null,
44         function() {
45             orderingAgency.attr("value", self.filter.ordering_agency);
46             dojo.connect(
47                 orderingAgency, "onChange",
48                 function() {
49                     self.filter.ordering_agency = this.attr("value");
50                     self.load();
51                 }
52             );
53             self.load();
54         }
55     );
56
57     dojo.byId("acq-eligible-claim-submit").onclick = function() {
58         finalClaimDialog.hide();
59         self.claim(self.getSelected());
60     };
61
62     dojo.query("button[name='claim_submit']").forEach(
63         function(button) {
64             button.onclick = function() {
65                 if (self.getSelected().length)
66                     finalClaimDialog.show();
67                 else
68                     alert(localeStrings.NO_LI_TO_CLAIM);
69             };
70         }
71     );
72
73     this.showEmpty = function() {
74         dojo.place(dojo.clone(this.emptyTemplate), this.tBody, "only");
75         openils.Util.hide("acq-eligible-claim-controls");
76     };
77
78     this.load = function() {
79         progressDialog.show(true);
80
81         var count = 0;
82         this.reset();
83         fieldmapper.standardRequest(
84             ["open-ils.acq", "open-ils.acq.claim.eligible.lineitem_detail.atomic"], {
85                 "params": [openils.User.authtoken, this.filter],
86                 "async": true,
87                 "oncomplete": function(r) {
88                     progressDialog.hide();
89                     var rset = openils.Util.readResponse(r);
90                     if (rset.length < 1) self.showEmpty();
91                     else {
92                         var byLi = {};
93                         rset.forEach(
94                             function(r) {
95                                 byLi[r.lineitem()] =
96                                     (byLi[r.lineitem()] || 0) + 1;
97                             }
98                         );
99                         for (var key in byLi)
100                             self.addIfMissing(key, byLi[key]);
101                     }
102                 }
103             }
104         );
105     };
106
107     this.reset = function() {
108         this.liCache = {};
109         this.numClaimableLids = {};
110         dojo.empty(this.tBody);
111     };
112
113     this._updateLidLink = function(liId) {
114         this.numClaimableLids[liId] = (this.numClaimableLids[liId] || 0) + 1;
115         if (this.numClaimableLids[liId] == 2) {
116             nodeByName("lid_link", "eligible-li-" + liId).onclick =
117                 function() {
118                     location.href = oilsBasePath + "/acq/po/view/" +
119                         self.liCache[liId].purchase_order().id() + "/" +
120                         liId;
121                 };
122             openils.Util.show(
123                 nodeByName("lid_link_holder", "eligible-li-" + liId)
124             );
125         }
126     };
127
128     /* Despite being called with an argument that's a lineitem ID, this method
129      * is actually called once per lineitem _detail_. */
130     this.addIfMissing = function(liId, number_of_appearances) {
131         var row = dojo.clone(this.rowTemplate);
132
133         var checkbox = nodeByName("selector", row);
134         var desc = nodeByName("description", row);
135
136         openils.acq.Lineitem.fetchAndRender(
137             liId, null, function(li, contents) {
138                 self.liCache[liId] = li;
139
140                 desc.innerHTML = contents;
141                 dojo.attr(row, "id", "eligible-li-" + liId);
142                 dojo.attr(checkbox, "value", liId);
143                 dojo.place(row, self.tBody, "last");
144
145                 for (var i = 0; i < number_of_appearances; i++)
146                     self._updateLidLink(liId);
147             }
148         );
149     };
150
151     /* Despite being called with an argument that's a lineitem ID, this method
152      * is actually called once per lineitem _detail_. */
153     this.removeIfPresent = function(liId) {
154         if (this.liCache[liId]) {
155             delete this.liCache[liId];
156             delete this.numClaimableLids[liId];
157             this.tBody.removeChild(dojo.byId("eligible-li-" + liId));
158         }
159     };
160
161     this.getSelected = function() {
162         return dojo.query("[name='selector']", this.tBody).
163             filter(function(o) { return o.checked; }).
164             map(function(o) { return o.value; });
165     };
166
167     this.resetVoucher = function() { this.voucherWin = null; };
168
169     this.addToVoucher = function(contents) {
170         if (!this.voucherWin)
171             this.voucherWin = openClaimVoucherWindow();
172         dojo.byId("main", this.voucherWin.document).innerHTML +=
173             (contents + "<hr />");
174     };
175
176     this.finishVoucher = function() {
177         var print_btn = dojo.byId("print", this.voucherWin.document);
178         print_btn.disabled = false;
179         print_btn.innerHTML = localeStrings.PRINT;
180     };
181
182     this.claim = function(lineitems) {
183         progressDialog.show(true);
184         self.resetVoucher();
185
186         fieldmapper.standardRequest(
187             ["open-ils.acq", "open-ils.acq.claim.lineitem"], {
188                 "params": [
189                     openils.User.authtoken, lineitems, null,
190                     this.claimType.attr("value"), this.claimNote.attr("value")
191                 ],
192                 "async": true,
193                 "onresponse": function(r) {
194                     if (r = openils.Util.readResponse(r))
195                         self.addToVoucher(r.template_output().data());
196                     else
197                         progressDialog.hide();
198                 },
199                 "oncomplete": function() {
200                     lineitems.forEach(
201                         function(liId) { self.removeIfPresent(liId); }
202                     );
203                     if (!nodeByName("selector", self.tBody)) // emptiness test
204                         self.showEmpty();
205
206                     self.finishVoucher();
207                     progressDialog.hide();
208                 }
209             }
210         );
211     };
212 }
213
214 function init() {
215     var finished_filter = {};
216     if (filter && filter.indexOf(":") != -1) {
217         filter.split(",").forEach(
218             function(chunk) {
219                 var kvlist = chunk.split(":");
220                 finished_filter[kvlist[0]] = kvlist[1];
221             }
222         );
223     }
224     filter = finished_filter;
225
226     if (!filter.ordering_agency)
227         filter.ordering_agency = openils.User.user.ws_ou();
228
229     eligibleLiTable = new EligibleLiTable(filter);
230 }
231
232 openils.Util.addOnLoad(init);