Initial dev repository
[kcls-web.git] / 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"], {
85                 "params": [openils.User.authtoken, this.filter],
86                 "async": true,
87                 "onresponse": function(r) {
88                     if (r = openils.Util.readResponse(r)) {
89                         if (!count++)
90                             openils.Util.show("acq-eligible-claim-controls");
91                         self.addIfMissing(r.lineitem());
92                     } else {
93                         progressDialog.hide();
94                     }
95                 },
96                 "oncomplete": function() {
97                     if (count < 1) self.showEmpty();
98                     progressDialog.hide();
99                 }
100             }
101         );
102     };
103
104     this.reset = function() {
105         this.liCache = {};
106         this.numClaimableLids = {};
107         dojo.empty(this.tBody);
108     };
109
110     this._updateLidLink = function(liId) {
111         this.numClaimableLids[liId] = (this.numClaimableLids[liId] || 0) + 1;
112         if (this.numClaimableLids[liId] == 2) {
113             nodeByName("lid_link", "eligible-li-" + liId).onclick =
114                 function() {
115                     location.href = oilsBasePath + "/acq/po/view/" +
116                         self.liCache[liId].purchase_order().id() + "," +
117                         liId;
118                 };
119             openils.Util.show(
120                 nodeByName("lid_link_holder", "eligible-li-" + liId)
121             );
122         }
123     };
124
125     /* Despite being called with an argument that's a lineitem ID, this method
126      * is actually called once per lineitem _detail_. */
127     this.addIfMissing = function(liId) {
128         this._updateLidLink(liId);
129         if (this.liCache[liId]) return;
130
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         );
146     };
147
148     /* Despite being called with an argument that's a lineitem ID, this method
149      * is actually called once per lineitem _detail_. */
150     this.removeIfPresent = function(liId) {
151         if (this.liCache[liId]) {
152             delete this.liCache[liId];
153             delete this.numClaimableLids[liId];
154             this.tBody.removeChild(dojo.byId("eligible-li-" + liId));
155         }
156     };
157
158     this.getSelected = function() {
159         return dojo.query("[name='selector']", this.tBody).
160             filter(function(o) { return o.checked; }).
161             map(function(o) { return o.value; });
162     };
163
164     this.resetVoucher = function() { this.voucherWin = null; };
165
166     this.addToVoucher = function(contents) {
167         if (!this.voucherWin)
168             this.voucherWin = openClaimVoucherWindow();
169         dojo.byId("main", this.voucherWin.document).innerHTML +=
170             (contents + "<hr />");
171     };
172
173     this.finishVoucher = function() {
174         var print_btn = dojo.byId("print", this.voucherWin.document);
175         print_btn.disabled = false;
176         print_btn.innerHTML = localeStrings.PRINT;
177     };
178
179     this.claim = function(lineitems) {
180         progressDialog.show(true);
181         self.resetVoucher();
182
183         fieldmapper.standardRequest(
184             ["open-ils.acq", "open-ils.acq.claim.lineitem"], {
185                 "params": [
186                     openils.User.authtoken, lineitems, null,
187                     this.claimType.attr("value"), this.claimNote.attr("value")
188                 ],
189                 "async": true,
190                 "onresponse": function(r) {
191                     if (r = openils.Util.readResponse(r))
192                         self.addToVoucher(r.template_output().data());
193                     else
194                         progressDialog.hide();
195                 },
196                 "oncomplete": function() {
197                     lineitems.forEach(
198                         function(liId) { self.removeIfPresent(liId); }
199                     );
200                     if (!nodeByName("selector", self.tBody)) // emptiness test
201                         self.showEmpty();
202
203                     self.finishVoucher();
204                     progressDialog.hide();
205                 }
206             }
207         );
208     };
209 }
210
211 function init() {
212     var finished_filter = {};
213     if (filter && filter.indexOf(":") != -1) {
214         filter.split(",").forEach(
215             function(chunk) {
216                 var kvlist = chunk.split(":");
217                 finished_filter[kvlist[0]] = kvlist[1];
218             }
219         );
220     }
221     filter = finished_filter;
222
223     if (!filter.ordering_agency)
224         filter.ordering_agency = openils.User.user.ws_ou();
225
226     eligibleLiTable = new EligibleLiTable(filter);
227 }
228
229 openils.Util.addOnLoad(init);