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 this.showEmpty = function() {
57 dojo.place(dojo.clone(this.emptyTemplate), this.tBody, "only");
58 openils.Util.hide("acq-eligible-claim-controls");
61 this.load = function() {
62 progressDialog.show(true);
66 fieldmapper.standardRequest(
67 ["open-ils.acq", "open-ils.acq.claim.eligible.lineitem_detail"], {
68 "params": [openils.User.authtoken, this.filter],
70 "onresponse": function(r) {
71 if (r = openils.Util.readResponse(r)) {
73 openils.Util.show("acq-eligible-claim-controls");
74 self.addIfMissing(r.lineitem());
76 progressDialog.hide();
79 "oncomplete": function() {
80 if (count < 1) self.showEmpty();
81 progressDialog.hide();
87 this.reset = function() {
89 this.numClaimableLids = {};
90 dojo.empty(this.tBody);
93 this._updateLidLink = function(liId) {
94 this.numClaimableLids[liId] = (this.numClaimableLids[liId] || 0) + 1;
95 if (this.numClaimableLids[liId] == 2) {
96 nodeByName("lid_link", "eligible-li-" + liId).onclick =
98 location.href = oilsBasePath + "/acq/po/view/" +
99 self.liCache[liId].purchase_order().id() + "," +
103 nodeByName("lid_link_holder", "eligible-li-" + liId)
108 /* Despite being called with an argument that's a lineitem ID, this method
109 * is actually called once per lineitem _detail_. */
110 this.addIfMissing = function(liId) {
111 this._updateLidLink(liId);
112 if (this.liCache[liId]) return;
114 var row = dojo.clone(this.rowTemplate);
116 var checkbox = nodeByName("selector", row);
117 var desc = nodeByName("description", row);
119 openils.acq.Lineitem.fetchAndRender(
120 liId, null, function(li, contents) {
121 self.liCache[liId] = li;
123 desc.innerHTML = contents;
124 dojo.attr(row, "id", "eligible-li-" + liId);
125 dojo.attr(checkbox, "value", liId);
126 dojo.place(row, self.tBody, "last");
131 /* Despite being called with an argument that's a lineitem ID, this method
132 * is actually called once per lineitem _detail_. */
133 this.removeIfPresent = function(liId) {
134 if (this.liCache[liId]) {
135 delete this.liCache[liId];
136 delete this.numClaimableLids[liId];
137 this.tBody.removeChild(dojo.byId("eligible-li-" + liId));
141 this.getSelected = function() {
142 return dojo.query("[name='selector']", this.tBody).
143 filter(function(o) { return o.checked; }).
144 map(function(o) { return o.value; });
147 this.resetVoucher = function() { this.voucherWin = null; };
149 this.addToVoucher = function(contents) {
150 if (!this.voucherWin) {
151 this.voucherWin = window.open(
152 "", "", "resizable,width=800,height=600,scrollbars=1"
154 this.voucherWin.document.title = localeStrings.CLAIM_VOUCHERS;
155 this.voucherWin.document.body.innerHTML = (
156 "<button onclick='window.print();'>" +
157 localeStrings.PRINT +
158 "</button><hr /><div id='main'></div>"
161 dojo.byId("main", this.voucherWin.document).innerHTML += (
166 this.claim = function() {
167 var lineitems = this.getSelected();
168 if (!lineitems.length) {
169 alert(localeStrings.NO_LI_TO_CLAIM);
173 progressDialog.show(true);
176 fieldmapper.standardRequest(
177 ["open-ils.acq", "open-ils.acq.claim.lineitem"], {
179 openils.User.authtoken, lineitems, null,
180 this.claimType.attr("value"), this.claimNote.attr("value")
183 "onresponse": function(r) {
184 if (r = openils.Util.readResponse(r))
185 self.addToVoucher(r.template_output().data());
187 progressDialog.hide();
189 "oncomplete": function() {
191 function(liId) { self.removeIfPresent(liId); }
193 if (!nodeByName("selector", self.tBody)) // emptiness test
195 progressDialog.hide();
203 var finished_filter = {};
204 if (filter && filter.indexOf(":") != -1) {
205 filter.split(",").forEach(
207 var [key, value] = chunk.split(":");
208 finished_filter[key] = value;
212 filter = finished_filter;
214 if (!filter.ordering_agency)
215 filter.ordering_agency = openils.User.user.ws_ou();
217 eligibleLiTable = new EligibleLiTable(filter);
220 openils.Util.addOnLoad(init);