]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/financial/claim_eligible.js
Acq: large parts of the UI for making claims to a vendor against lineitems
[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     this.showEmpty = function() {
57         dojo.place(dojo.clone(this.emptyTemplate), this.tBody, "only");
58         openils.Util.hide("acq-eligible-claim-controls");
59     };
60
61     this.load = function() {
62         progressDialog.show(true);
63
64         var count = 0;
65         this.reset();
66         fieldmapper.standardRequest(
67             ["open-ils.acq", "open-ils.acq.claim.eligible.lineitem_detail"], {
68                 "params": [openils.User.authtoken, this.filter],
69                 "async": true,
70                 "onresponse": function(r) {
71                     if (r = openils.Util.readResponse(r)) {
72                         if (!count++)
73                             openils.Util.show("acq-eligible-claim-controls");
74                         self.addIfMissing(r.lineitem());
75                     } else {
76                         progressDialog.hide();
77                     }
78                 },
79                 "oncomplete": function() {
80                     if (count < 1) self.showEmpty();
81                     progressDialog.hide();
82                 }
83             }
84         );
85     };
86
87     this.reset = function() {
88         this.liCache = {};
89         this.numClaimableLids = {};
90         dojo.empty(this.tBody);
91     };
92
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 =
97                 function() {
98                     location.href = oilsBasePath + "/acq/po/view/" +
99                         self.liCache[liId].purchase_order().id() + "," +
100                         liId;
101                 };
102             openils.Util.show(
103                 nodeByName("lid_link_holder", "eligible-li-" + liId)
104             );
105         }
106     };
107
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;
113
114         var row = dojo.clone(this.rowTemplate);
115
116         var checkbox = nodeByName("selector", row);
117         var desc = nodeByName("description", row);
118
119         openils.acq.Lineitem.fetchAndRender(
120             liId, null, function(li, contents) {
121                 self.liCache[liId] = li;
122
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");
127             }
128         );
129     };
130
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));
138         }
139     };
140
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; });
145     };
146
147     this.resetVoucher = function() { this.voucherWin = null; };
148
149     this.addToVoucher = function(contents) {
150         if (!this.voucherWin) {
151             this.voucherWin = window.open(
152                 "", "", "resizable,width=800,height=600,scrollbars=1"
153             );
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>"
159             );
160         }
161         dojo.byId("main", this.voucherWin.document).innerHTML += (
162             contents + "<hr />"
163         );
164     };
165
166     this.claim = function() {
167         var lineitems = this.getSelected();
168         if (!lineitems.length) {
169             alert(localeStrings.NO_LI_TO_CLAIM);
170             return;
171         }
172
173         progressDialog.show(true);
174         self.resetVoucher();
175
176         fieldmapper.standardRequest(
177             ["open-ils.acq", "open-ils.acq.claim.lineitem"], {
178                 "params": [
179                     openils.User.authtoken, lineitems, null,
180                     this.claimType.attr("value"), this.claimNote.attr("value")
181                 ],
182                 "async": true,
183                 "onresponse": function(r) {
184                     if (r = openils.Util.readResponse(r))
185                         self.addToVoucher(r.template_output().data());
186                     else
187                         progressDialog.hide();
188                 },
189                 "oncomplete": function() {
190                     lineitems.forEach(
191                         function(liId) { self.removeIfPresent(liId); }
192                     );
193                     if (!nodeByName("selector", self.tBody)) // emptiness test
194                         self.showEmpty();
195                     progressDialog.hide();
196                 }
197             }
198         );
199     };
200 }
201
202 function init() {
203     var finished_filter = {};
204     if (filter && filter.indexOf(":") != -1) {
205         filter.split(",").forEach(
206             function(chunk) {
207                 var [key, value] = chunk.split(":");
208                 finished_filter[key] = value;
209             }
210         );
211     }
212     filter = finished_filter;
213
214     if (!filter.ordering_agency)
215         filter.ordering_agency = openils.User.user.ws_ou();
216
217     eligibleLiTable = new EligibleLiTable(filter);
218 }
219
220 openils.Util.addOnLoad(init);