testing commit, please ignore
[kcls-web.git] / 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     dojo.byId("acq-lit-li-claim-dia-claim").onclick = function() {
30         var lid_ids = self.getSelectedEligible();
31         if (lid_ids.length) {
32             dojo.byId("acq-eligible-claim-submit").onclick = function() {
33                 self.finalDialog.hide();
34                 self.claim(lid_ids);
35             };
36             self.dialog.hide();
37             self.finalDialog.show();
38         }
39         else {
40             alert(localeStrings.NO_LID_TO_CLAIM);
41         }
42     };
43
44     new openils.widget.AutoFieldWidget({
45         "fmClass": "acqclt",
46         "selfReference": true,
47         "dijitArgs": {"required": true},
48         "parentNode": dojo.byId("acq-eligible-claim-type")
49     }).build(function(w) { self.claimType = w; });
50
51     this.reset = function(li) {
52         this.anyLids = false;
53         this.anyEligible = false;
54         this.showingLidNodes = {};
55
56         openils.Util.hide("acq-lit-li-claim-dia-initiate");
57         openils.Util.hide("acq-lit-li-claim-dia-show");
58
59         dojo.empty(this.showingList);
60         dojo.empty(this.eligibleList);
61     };
62
63     this.show = function(li, preselected) {
64         this.reset();
65         this.prepare(li, preselected);
66         this.dialog.show();
67     };
68
69     this.hide = function() { this.dialog.hide(); };
70
71     this.prepare = function(li, preselected) {
72         this.workingLi = li;
73
74         dojo.byId("acq-lit-li-claim-dia-li-title").innerHTML =
75             li.attributes().filter(
76                 function(o) { return Boolean(o.attr_name() == "title"); }
77             )[0].attr_value();
78         dojo.byId("acq-lit-li-claim-dia-li-id").innerHTML = li.id();
79
80         li.lineitem_details().forEach(
81             function(lid) {
82                 lid.claims().forEach(
83                     function(claim) { self.addClaim(lid, claim); }
84                 );
85                 if (self.eligibleLidByLi[li.id()].indexOf(lid.id()) != -1) {
86                     self.addEligible(lid, preselected == lid.id());
87                 }
88             }
89         );
90     };
91
92     this._reprReceived = function(lid) {
93         if (lid.cancel_reason())
94             return localeStrings.CANCELED + ": " + lid.cancel_reason().label();
95         else if (lid.recv_time())
96             return localeStrings.RECVD + " " + lid.recv_time();
97         else
98             return localeStrings.NOT_RECVD;
99     };
100
101     this.addClaim = function(lid, claim) {
102         if (!this.anyLids)
103             openils.Util.show("acq-lit-li-claim-dia-show");
104         this.anyLids = true;
105
106         var lidNode = this.showingLidNodes[lid.id()];
107         if (!lidNode) {
108             lidNode = dojo.clone(this.showingLidTemplate);
109             nodeByName("barcode", lidNode).innerHTML = lid.barcode();
110             nodeByName("recvd", lidNode).innerHTML = this._reprReceived(lid);
111
112             this.showingLidNodes[lid.id()] = lidNode;
113             dojo.place(lidNode, this.showingList, "last");
114         }
115
116         var claimNode = dojo.clone(this.showingClaimTemplate);
117         nodeByName("type", claimNode).innerHTML = claim.type().code();
118         nodeByName("voucher", claimNode).onclick = function() {
119             var win;
120             fieldmapper.standardRequest(
121                 ["open-ils.acq",
122                     "open-ils.acq.claim.voucher.by_lineitem_detail"], {
123                     "params": [openils.User.authtoken, lid.id()],
124                     "async": true,
125                     "onresponse": function(r) {
126                         if (r = openils.Util.readResponse(r)) {
127                             if (!win)
128                                 win = openClaimVoucherWindow();
129                         }
130                         dojo.byId("main", win.document).innerHTML +=
131                             (r.template_output().data() + "<hr />");
132                     },
133                     "oncomplete": function() {
134                         var print_button = dojo.byId("print", win.document);
135                         print_button.innerHTML = localeStrings.PRINT;
136                         print_button.disabled = false;
137                     }
138                 }
139             );
140         };
141
142         dojo.place(
143             claimNode, nodeByName("claims", lidNode), "last"
144         );
145     };
146
147     this.addEligible = function(lid, preselect) {
148         if (!this.anyEligible)
149             openils.Util.show("acq-lit-li-claim-dia-initiate");
150         this.anyEligible = true;
151
152         var eligibleNode = dojo.clone(this.eligibleTemplate);
153         var checkbox = nodeByName("claimable_lid", eligibleNode);
154
155         checkbox.value = lid.id();
156         dojo.attr(checkbox, "id", "claim-lid-" + lid.id());
157         dojo.attr(checkbox, "for", "claim-lid-" + lid.id());
158         if (preselect)
159             dojo.attr(checkbox, "checked", true);
160
161         nodeByName("barcode", eligibleNode).innerHTML = lid.barcode();
162         nodeByName("recvd", eligibleNode).innerHTML = this._reprReceived(lid);
163
164         dojo.place(eligibleNode, this.eligibleList, "last");
165     };
166
167     this.getSelectedEligible = function() {
168         return dojo.query("input[name='claimable_lid']", this.eligibleList).
169             filter(function(o) { return o.checked; }).
170             map(function(o) { return o.value; });
171     };
172
173     this.claim = function(lid_ids) {
174         progressDialog.show(true);
175         var win = null;
176
177         fieldmapper.standardRequest(
178             ["open-ils.acq", "open-ils.acq.claim.lineitem_detail"], {
179                 "params": [
180                     openils.User.authtoken, lid_ids, null,
181                     this.claimType.attr("value"),
182                     dijit.byId("acq-eligible-claim-note").attr("value")
183                 ],
184                 "async": true,
185                 "onresponse": function(r) {
186                     if (r = openils.Util.readResponse(r)) {
187                         if (!win)
188                             win = openClaimVoucherWindow();
189                         dojo.byId("main", win.document).innerHTML +=
190                             (r.template_output().data() + "<hr />");
191                     }
192                     else {
193                         progressDialog.hide();
194                     }
195                 },
196                 "oncomplete": function() {
197                     progressDialog.hide();
198                     dojo.byId("print", win.document).innerHTML =
199                         localeStrings.PRINT;
200                     dojo.byId("print", win.document).disabled = false;
201                     self.claimCallback(self.workingLi);
202                 }
203             }
204         );
205     };
206 }