1 dojo.require("dijit.form.TextBox");
2 dojo.require("openils.acq.Lineitem");
3 dojo.require("openils.widget.OrgUnitFilteringSelect");
4 dojo.require("openils.widget.ProgressDialog");
5 dojo.require("openils.widget.AutoFieldWidget");
9 function nodeByName(n, c) { return dojo.query("[name='" + n + "']", c)[0]; }
11 function EligibleLiTable(filter) {
16 this.numClaimableLids = {};
18 this.claimNote = dijit.byId("acq-eligible-claim-note");
19 this.table = dojo.byId("acq-eligible-li-table");
20 this.tBody = dojo.query("tbody", this.table)[0];
21 this.tHead = dojo.query("thead", this.table)[0];
22 [this.rowTemplate, this.emptyTemplate] =
23 dojo.query("tr", this.tBody).map(
24 function(o) { return self.tBody.removeChild(o); }
27 nodeByName("selector_all", this.tHead).onclick = function() {
28 var value = this.checked;
29 dojo.query("[name='selector']", self.tBody).forEach(
30 function(o) { o.checked = value; }
34 new openils.widget.AutoFieldWidget({
36 "selfReference": true,
37 "dijitArgs": {"required": true},
38 "parentNode": dojo.byId("acq-eligible-claim-type")
39 }).build(function(w) { self.claimType = w; });
41 new openils.User().buildPermOrgSelector(
42 "VIEW_PURCHASE_ORDER", orderingAgency, null,
44 orderingAgency.attr("value", self.filter.ordering_agency);
46 orderingAgency, "onChange",
48 self.filter.ordering_agency = this.attr("value");
56 dojo.byId("acq-eligible-claim-submit").onclick = function() {
57 finalClaimDialog.hide();
58 self.claim(self.getSelected());
61 dojo.query("button[name='claim_submit']").forEach(
63 button.onclick = function() {
64 if (self.getSelected().length)
65 finalClaimDialog.show();
67 alert(localeStrings.NO_LI_TO_CLAIM);
72 this.showEmpty = function() {
73 dojo.place(dojo.clone(this.emptyTemplate), this.tBody, "only");
74 openils.Util.hide("acq-eligible-claim-controls");
77 this.load = function() {
78 progressDialog.show(true);
82 fieldmapper.standardRequest(
83 ["open-ils.acq", "open-ils.acq.claim.eligible.lineitem_detail"], {
84 "params": [openils.User.authtoken, this.filter],
86 "onresponse": function(r) {
87 if (r = openils.Util.readResponse(r)) {
89 openils.Util.show("acq-eligible-claim-controls");
90 self.addIfMissing(r.lineitem());
92 progressDialog.hide();
95 "oncomplete": function() {
96 if (count < 1) self.showEmpty();
97 progressDialog.hide();
103 this.reset = function() {
105 this.numClaimableLids = {};
106 dojo.empty(this.tBody);
109 this._updateLidLink = function(liId) {
110 this.numClaimableLids[liId] = (this.numClaimableLids[liId] || 0) + 1;
111 if (this.numClaimableLids[liId] == 2) {
112 nodeByName("lid_link", "eligible-li-" + liId).onclick =
114 location.href = oilsBasePath + "/acq/po/view/" +
115 self.liCache[liId].purchase_order().id() + "," +
119 nodeByName("lid_link_holder", "eligible-li-" + liId)
124 /* Despite being called with an argument that's a lineitem ID, this method
125 * is actually called once per lineitem _detail_. */
126 this.addIfMissing = function(liId) {
127 this._updateLidLink(liId);
128 if (this.liCache[liId]) return;
130 var row = dojo.clone(this.rowTemplate);
132 var checkbox = nodeByName("selector", row);
133 var desc = nodeByName("description", row);
135 openils.acq.Lineitem.fetchAndRender(
136 liId, null, function(li, contents) {
137 self.liCache[liId] = li;
139 desc.innerHTML = contents;
140 dojo.attr(row, "id", "eligible-li-" + liId);
141 dojo.attr(checkbox, "value", liId);
142 dojo.place(row, self.tBody, "last");
147 /* Despite being called with an argument that's a lineitem ID, this method
148 * is actually called once per lineitem _detail_. */
149 this.removeIfPresent = function(liId) {
150 if (this.liCache[liId]) {
151 delete this.liCache[liId];
152 delete this.numClaimableLids[liId];
153 this.tBody.removeChild(dojo.byId("eligible-li-" + liId));
157 this.getSelected = function() {
158 return dojo.query("[name='selector']", this.tBody).
159 filter(function(o) { return o.checked; }).
160 map(function(o) { return o.value; });
163 this.resetVoucher = function() { this.voucherWin = null; };
165 this.addToVoucher = function(contents) {
166 if (!this.voucherWin)
167 this.voucherWin = openClaimVoucherWindow();
168 dojo.byId("main", this.voucherWin.document).innerHTML +=
169 (contents + "<hr />");
172 this.finishVoucher = function() {
173 var print_btn = dojo.byId("print", this.voucherWin.document);
174 print_btn.disabled = false;
175 print_btn.innerHTML = localeStrings.PRINT;
178 this.claim = function(lineitems) {
179 progressDialog.show(true);
182 fieldmapper.standardRequest(
183 ["open-ils.acq", "open-ils.acq.claim.lineitem"], {
185 openils.User.authtoken, lineitems, null,
186 this.claimType.attr("value"), this.claimNote.attr("value")
189 "onresponse": function(r) {
190 if (r = openils.Util.readResponse(r))
191 self.addToVoucher(r.template_output().data());
193 progressDialog.hide();
195 "oncomplete": function() {
197 function(liId) { self.removeIfPresent(liId); }
199 if (!nodeByName("selector", self.tBody)) // emptiness test
202 self.finishVoucher();
203 progressDialog.hide();
211 var finished_filter = {};
212 if (filter && filter.indexOf(":") != -1) {
213 filter.split(",").forEach(
215 var [key, value] = chunk.split(":");
216 finished_filter[key] = value;
220 filter = finished_filter;
222 if (!filter.ordering_agency)
223 filter.ordering_agency = openils.User.user.ws_ou();
225 eligibleLiTable = new EligibleLiTable(filter);
228 openils.Util.addOnLoad(init);