]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/financial/claim_eligible.js
Acq: more work on the claiming UI. It begins to be workable, but still rough.
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / financial / claim_eligible.js
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");
6
7 var eligibleLiTable;
8
9 function nodeByName(n, c) { return dojo.query("[name='" + n + "']", c)[0]; }
10
11 function EligibleLiTable(filter) {
12     var self = this;
13
14     this.filter = filter;
15     this.liCache = {};
16     this.numClaimableLids = {};
17
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); }
25         );
26
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; }
31         );
32     };
33
34     new openils.widget.AutoFieldWidget({
35         "fmClass": "acqclt",
36         "selfReference": true,
37         "dijitArgs": {"required": true},
38         "parentNode": dojo.byId("acq-eligible-claim-type")
39     }).build(function(w) { self.claimType = w; });
40
41     new openils.User().buildPermOrgSelector(
42         "VIEW_PURCHASE_ORDER", orderingAgency, null,
43         function() {
44             orderingAgency.attr("value", self.filter.ordering_agency);
45             dojo.connect(
46                 orderingAgency, "onChange",
47                 function() {
48                     self.filter.ordering_agency = this.attr("value");
49                     self.load();
50                 }
51             );
52             self.load();
53         }
54     );
55
56     dojo.byId("acq-eligible-claim-submit").onclick = function() {
57         finalClaimDialog.hide();
58         self.claim(self.getSelected());
59     };
60
61     dojo.query("button[name='claim_submit']").forEach(
62         function(button) {
63             button.onclick = function() {
64                 if (self.getSelected().length)
65                     finalClaimDialog.show();
66                 else
67                     alert(localeStrings.NO_LI_TO_CLAIM);
68             };
69         }
70     );
71
72     this.showEmpty = function() {
73         dojo.place(dojo.clone(this.emptyTemplate), this.tBody, "only");
74         openils.Util.hide("acq-eligible-claim-controls");
75     };
76
77     this.load = function() {
78         progressDialog.show(true);
79
80         var count = 0;
81         this.reset();
82         fieldmapper.standardRequest(
83             ["open-ils.acq", "open-ils.acq.claim.eligible.lineitem_detail"], {
84                 "params": [openils.User.authtoken, this.filter],
85                 "async": true,
86                 "onresponse": function(r) {
87                     if (r = openils.Util.readResponse(r)) {
88                         if (!count++)
89                             openils.Util.show("acq-eligible-claim-controls");
90                         self.addIfMissing(r.lineitem());
91                     } else {
92                         progressDialog.hide();
93                     }
94                 },
95                 "oncomplete": function() {
96                     if (count < 1) self.showEmpty();
97                     progressDialog.hide();
98                 }
99             }
100         );
101     };
102
103     this.reset = function() {
104         this.liCache = {};
105         this.numClaimableLids = {};
106         dojo.empty(this.tBody);
107     };
108
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 =
113                 function() {
114                     location.href = oilsBasePath + "/acq/po/view/" +
115                         self.liCache[liId].purchase_order().id() + "," +
116                         liId;
117                 };
118             openils.Util.show(
119                 nodeByName("lid_link_holder", "eligible-li-" + liId)
120             );
121         }
122     };
123
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;
129
130         var row = dojo.clone(this.rowTemplate);
131
132         var checkbox = nodeByName("selector", row);
133         var desc = nodeByName("description", row);
134
135         openils.acq.Lineitem.fetchAndRender(
136             liId, null, function(li, contents) {
137                 self.liCache[liId] = li;
138
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");
143             }
144         );
145     };
146
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));
154         }
155     };
156
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; });
161     };
162
163     this.resetVoucher = function() { this.voucherWin = null; };
164
165     this.addToVoucher = function(contents) {
166         if (!this.voucherWin)
167             this.voucherWin = openClaimVoucherWindow();
168         dojo.byId("main", this.voucherWin.document).innerHTML +=
169             (contents + "<hr />");
170     };
171
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;
176     };
177
178     this.claim = function(lineitems) {
179         progressDialog.show(true);
180         self.resetVoucher();
181
182         fieldmapper.standardRequest(
183             ["open-ils.acq", "open-ils.acq.claim.lineitem"], {
184                 "params": [
185                     openils.User.authtoken, lineitems, null,
186                     this.claimType.attr("value"), this.claimNote.attr("value")
187                 ],
188                 "async": true,
189                 "onresponse": function(r) {
190                     if (r = openils.Util.readResponse(r))
191                         self.addToVoucher(r.template_output().data());
192                     else
193                         progressDialog.hide();
194                 },
195                 "oncomplete": function() {
196                     lineitems.forEach(
197                         function(liId) { self.removeIfPresent(liId); }
198                     );
199                     if (!nodeByName("selector", self.tBody)) // emptiness test
200                         self.showEmpty();
201
202                     self.finishVoucher();
203                     progressDialog.hide();
204                 }
205             }
206         );
207     };
208 }
209
210 function init() {
211     var finished_filter = {};
212     if (filter && filter.indexOf(":") != -1) {
213         filter.split(",").forEach(
214             function(chunk) {
215                 var [key, value] = chunk.split(":");
216                 finished_filter[key] = value;
217             }
218         );
219     }
220     filter = finished_filter;
221
222     if (!filter.ordering_agency)
223         filter.ordering_agency = openils.User.user.ws_ou();
224
225     eligibleLiTable = new EligibleLiTable(filter);
226 }
227
228 openils.Util.addOnLoad(init);