]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/common/claim_dialog.js
LP#1482400: when activating a PO, provide better progress updates
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / common / claim_dialog.js
1 function ClaimDialogManager(
2     dialog, finalDialog, eligibleLidByLi, claimCallback
3 ) {
4     var self = this;
5
6     this.anyLids = false;
7     this.anyEligible = false;
8     this.showingLidNodes = {};
9
10     this.dialog = dialog;
11     this.finalDialog = finalDialog;
12     this.eligibleLidByLi = eligibleLidByLi;
13     this.claimCallback = claimCallback;
14
15     this.showingList = dojo.byId("acq-lit-li-claim-dia-lid-list");
16     this.eligibleList = dojo.byId("acq-lit-li-claim-dia-lid-list-init");
17
18     this.showingLidTemplate = this.showingList.removeChild(
19         nodeByName("lid", this.showingList)
20     );
21     this.showingClaimTemplate =
22         nodeByName("claims", this.showingLidTemplate).removeChild(
23             nodeByName("claim", this.showingLidTemplate)
24         );
25     this.eligibleTemplate = this.eligibleList.removeChild(
26         nodeByName("lid_to_claim", this.eligibleList)
27     );
28
29     var acqclet_template_parent = dojo.byId("acqclet-tbody");
30     this.eventTypeTemplate = acqclet_template_parent.removeChild(
31         nodeByName("acqclet-template", acqclet_template_parent)
32     );
33
34     dojo.byId("acq-lit-li-claim-dia-claim").onclick = function() {
35         var lid_ids = self.getSelectedEligible();
36         if (lid_ids.length) {
37             dojo.byId("acq-eligible-claim-submit").onclick = function() {
38                 self.finalDialog.hide();
39                 self.claim(lid_ids);
40             };
41             self.dialog.hide();
42             self.finalDialog.show();
43         }
44         else {
45             alert(localeStrings.NO_LID_TO_CLAIM);
46         }
47     };
48
49     new openils.widget.AutoFieldWidget({
50         "fmClass": "acqclt",
51         "selfReference": true,
52         "dijitArgs": {"required": true},
53         "parentNode": dojo.byId("acq-eligible-claim-type")
54     }).build(function(w) { self.claimType = w; });
55
56     this.reset = function(li) {
57         this.anyLids = false;
58         this.anyEligible = false;
59         this.showingLidNodes = {};
60
61         openils.Util.hide("acq-lit-li-claim-dia-initiate");
62         openils.Util.hide("acq-lit-li-claim-dia-show");
63         openils.Util.hide("acqclet-display");
64
65         dojo.empty("acqclet-tbody");
66         dojo.empty(this.showingList);
67         dojo.empty(this.eligibleList);
68     };
69
70     this.show = function(li, preselected) {
71         this.reset();
72         this.prepare(li, preselected);
73         this.dialog.show();
74     };
75
76     this.hide = function() { this.dialog.hide(); };
77
78     this.prepare = function(li, preselected) {
79         this.workingLi = li;
80
81         dojo.byId("acq-lit-li-claim-dia-li-title").innerHTML =
82             li.attributes().filter(
83                 function(o) { return Boolean(o.attr_name() == "title"); }
84             )[0].attr_value();
85         dojo.byId("acq-lit-li-claim-dia-li-id").innerHTML = li.id();
86
87         li.lineitem_details().forEach(
88             function(lid) {
89                 lid.claims().forEach(
90                     function(claim) { self.addClaim(lid, claim); }
91                 );
92                 if (self.eligibleLidByLi[li.id()].indexOf(lid.id()) != -1) {
93                     self.addEligible(lid, preselected == lid.id());
94                 }
95             }
96         );
97
98         if (!li.claim_policy())
99             this.showClaimEventTypes();
100     };
101
102     this.showClaimEventTypes = function() {
103         if (!this._cached_event_types) {
104             this._cached_event_types = new openils.PermaCrud({
105                 "authtoken": openils.User.authtoken
106             }).retrieveAll(
107                 "acqclet", {"order_by": {"acqclet": "code"}}
108             );
109         }
110
111         if (this._cached_event_types && this._cached_event_types.length) {
112             openils.Util.show("acqclet-display");
113             dojo.empty("acqclet-tbody");
114             this._cached_event_types.forEach(
115                 function(clet) { self._render_event_type_row(clet); }
116             );
117         }
118     };
119
120     this.selectedEventTypes = function() {
121         var selected = dojo.query("[id^='acqclet-checkbox-']").filter(
122             function(node) {
123                 return dojo.attr(node, "checked");
124             }
125         ).map(
126             function(node) {
127                 return dojo.attr(node, "id").match(/-(\d+)$/)[1];
128             }
129         );
130
131         return selected.length ? selected : null;
132     };
133
134     this._render_event_type_row = function(clet) {
135         var row = dojo.clone(this.eventTypeTemplate);
136
137         var checkbox = nodeByName("acqclet-checkbox", row);
138         var label = nodeByName("acqclet-label", row);
139
140         var checkbox_id = "acqclet-checkbox-" + clet.id();
141         dojo.attr(checkbox, "id", checkbox_id);
142         dojo.attr(label, "for", checkbox_id);
143
144         label.innerHTML = dojo.string.substitute(label.innerHTML, {
145             "description": clet.description(),
146             "code": clet.code(),
147             "library_initiated": clet.library_initiated() ?
148                 localeStrings.LIBRARY_INITIATED : "",
149             "ou": aou.findOrgUnit(clet.org_unit()).shortname()
150         });
151
152         dojo.place(row, "acqclet-tbody");
153     };
154
155     this._reprReceived = function(lid) {
156         if (lid.cancel_reason())
157             return localeStrings.CANCELED + ": " + lid.cancel_reason().label();
158         else if (lid.recv_time())
159             return localeStrings.RECVD + " " + lid.recv_time();
160         else
161             return localeStrings.NOT_RECVD;
162     };
163
164     this.addClaim = function(lid, claim) {
165         if (!this.anyLids)
166             openils.Util.show("acq-lit-li-claim-dia-show");
167         this.anyLids = true;
168
169         var lidNode = this.showingLidNodes[lid.id()];
170         if (!lidNode) {
171             lidNode = dojo.clone(this.showingLidTemplate);
172             nodeByName("barcode", lidNode).innerHTML = lid.barcode();
173             nodeByName("recvd", lidNode).innerHTML = this._reprReceived(lid);
174
175             this.showingLidNodes[lid.id()] = lidNode;
176             dojo.place(lidNode, this.showingList, "last");
177         }
178
179         var claimNode = dojo.clone(this.showingClaimTemplate);
180         nodeByName("type", claimNode).innerHTML = claim.type().code();
181         nodeByName("voucher", claimNode).onclick = function() {
182             var win;
183             fieldmapper.standardRequest(
184                 ["open-ils.acq",
185                     "open-ils.acq.claim.voucher.by_lineitem_detail"], {
186                     "params": [openils.User.authtoken, lid.id()],
187                     "async": true,
188                     "onresponse": function(r) {
189                         if (r = openils.Util.readResponse(r)) {
190                             if (!win)
191                                 win = openClaimVoucherWindow();
192                         }
193                         dojo.byId("main", win.document).innerHTML +=
194                             (r.template_output().data() + "<hr />");
195                     },
196                     "oncomplete": function() {
197                         var print_button = dojo.byId("print", win.document);
198                         print_button.innerHTML = localeStrings.PRINT;
199                         print_button.disabled = false;
200                     }
201                 }
202             );
203         };
204
205         dojo.place(
206             claimNode, nodeByName("claims", lidNode), "last"
207         );
208     };
209
210     this.addEligible = function(lid, preselect) {
211         if (!this.anyEligible)
212             openils.Util.show("acq-lit-li-claim-dia-initiate");
213         this.anyEligible = true;
214
215         var eligibleNode = dojo.clone(this.eligibleTemplate);
216         var checkbox = nodeByName("claimable_lid", eligibleNode);
217
218         checkbox.value = lid.id();
219         dojo.attr(checkbox, "id", "claim-lid-" + lid.id());
220         dojo.attr(checkbox, "for", "claim-lid-" + lid.id());
221         if (preselect)
222             dojo.attr(checkbox, "checked", true);
223
224         nodeByName("barcode", eligibleNode).innerHTML = lid.barcode();
225         nodeByName("recvd", eligibleNode).innerHTML = this._reprReceived(lid);
226
227         dojo.place(eligibleNode, this.eligibleList, "last");
228     };
229
230     this.getSelectedEligible = function() {
231         return dojo.query("input[name='claimable_lid']", this.eligibleList).
232             filter(function(o) { return o.checked; }).
233             map(function(o) { return o.value; });
234     };
235
236     this.claim = function(lid_ids) {
237         progressDialog.show(true);
238
239         fieldmapper.standardRequest(
240             ["open-ils.acq", "open-ils.acq.claim.lineitem_detail.atomic"], {
241                 "params": [
242                     openils.User.authtoken, lid_ids, null,
243                     this.claimType.attr("value"),
244                     dijit.byId("acq-eligible-claim-note").attr("value"),
245                     null,
246                     this.selectedEventTypes()
247                 ],
248                 "async": true,
249                 "onresponse": function(r) {
250                     if (r = openils.Util.readResponse(r)) {
251                         if (r.length) {
252                             openils.Util.printHtmlString(
253                                 r.map(
254                                     function(o) {
255                                         return o.template_output().data();
256                                     }
257                                 ).join("<hr />")
258                             );
259                         }
260                     }
261                     progressDialog.hide();
262                     self.claimCallback(self.workingLi);
263                 }
264             }
265         );
266     };
267 }