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");
10 function nodeByName(n, c) { return dojo.query("[name='" + n + "']", c)[0]; }
12 function EligibleLiTable(filter) {
17 this.numClaimableLids = {};
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); }
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; }
35 new openils.widget.AutoFieldWidget({
37 "selfReference": true,
38 "dijitArgs": {"required": true},
39 "parentNode": dojo.byId("acq-eligible-claim-type")
40 }).build(function(w) { self.claimType = w; });
42 new openils.User().buildPermOrgSelector(
43 "VIEW_PURCHASE_ORDER", orderingAgency, null,
45 orderingAgency.attr("value", self.filter.ordering_agency);
47 orderingAgency, "onChange",
49 self.filter.ordering_agency = this.attr("value");
57 dojo.byId("acq-eligible-claim-submit").onclick = function() {
58 finalClaimDialog.hide();
59 self.claim(self.getSelected());
62 dojo.query("button[name='claim_submit']").forEach(
64 button.onclick = function() {
65 if (self.getSelected().length)
66 finalClaimDialog.show();
68 alert(localeStrings.NO_LI_TO_CLAIM);
73 this.showEmpty = function() {
74 dojo.place(dojo.clone(this.emptyTemplate), this.tBody, "only");
75 openils.Util.hide("acq-eligible-claim-controls");
78 this.load = function() {
79 progressDialog.show(true);
83 fieldmapper.standardRequest(
84 ["open-ils.acq", "open-ils.acq.claim.eligible.lineitem_detail.atomic"], {
85 "params": [openils.User.authtoken, this.filter],
87 "oncomplete": function(r) {
88 progressDialog.hide();
89 var rset = openils.Util.readResponse(r);
90 if (rset.length < 1) self.showEmpty();
96 (byLi[r.lineitem()] || 0) + 1;
100 self.addIfMissing(key, byLi[key]);
107 this.reset = function() {
109 this.numClaimableLids = {};
110 dojo.empty(this.tBody);
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 =
118 location.href = oilsBasePath + "/acq/po/view/" +
119 self.liCache[liId].purchase_order().id() + "/" +
123 nodeByName("lid_link_holder", "eligible-li-" + liId)
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);
133 var checkbox = nodeByName("selector", row);
134 var desc = nodeByName("description", row);
136 openils.acq.Lineitem.fetchAndRender(
137 liId, null, function(li, contents) {
138 self.liCache[liId] = li;
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");
145 for (var i = 0; i < number_of_appearances; i++)
146 self._updateLidLink(liId);
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));
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; });
167 this.resetVoucher = function() { this.voucherWin = null; };
169 this.addToVoucher = function(contents) {
170 if (!this.voucherWin)
171 this.voucherWin = openClaimVoucherWindow();
172 dojo.byId("main", this.voucherWin.document).innerHTML +=
173 (contents + "<hr />");
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;
182 this.claim = function(lineitems) {
183 progressDialog.show(true);
186 fieldmapper.standardRequest(
187 ["open-ils.acq", "open-ils.acq.claim.lineitem"], {
189 openils.User.authtoken, lineitems, null,
190 this.claimType.attr("value"), this.claimNote.attr("value")
193 "onresponse": function(r) {
194 if (r = openils.Util.readResponse(r))
195 self.addToVoucher(r.template_output().data());
197 progressDialog.hide();
199 "oncomplete": function() {
201 function(liId) { self.removeIfPresent(liId); }
203 if (!nodeByName("selector", self.tBody)) // emptiness test
206 self.finishVoucher();
207 progressDialog.hide();
215 var finished_filter = {};
216 if (filter && filter.indexOf(":") != -1) {
217 filter.split(",").forEach(
219 var kvlist = chunk.split(":");
220 finished_filter[kvlist[0]] = kvlist[1];
224 filter = finished_filter;
226 if (!filter.ordering_agency)
227 filter.ordering_agency = openils.User.user.ws_ou();
229 eligibleLiTable = new EligibleLiTable(filter);
232 openils.Util.addOnLoad(init);