0e69a2dc7a8b27834025c8de6d9c5020b9186c1b
[working/Evergreen.git] / Open-ILS / web / js / ui / default / booking / capture.js
1 dojo.require("openils.User");
2 dojo.require("openils.widget.OrgUnitFilteringSelect");
3 dojo.requireLocalization("openils.booking", "capture");
4
5 const CAPTURE_FAILURE = 0;
6 const CAPTURE_SUCCESS = 1;
7 const CAPTURE_UNKNOWN = 2;
8
9 var localeStrings = dojo.i18n.getLocalization("openils.booking", "capture");
10
11 function CaptureDisplay(control_holder, data_holder) {
12     this.control_holder = control_holder;
13     this.data_holder = data_holder;
14 }
15 CaptureDisplay.prototype.no_payload = function() {
16     this.data_holder.appendChild(
17         document.createTextNode(localeStrings.NO_PAYLOAD)
18     );
19 };
20 CaptureDisplay.prototype.dump = function(payload) {
21     var div = document.createElement("div");
22     div.appendChild(document.createTextNode(localeStrings.HERES_WHAT_WE_KNOW));
23     this.data_holder.appendChild(div);
24
25     var ul = document.createElement("ul");
26     for (var k in payload) {
27         var li = document.createElement("li");
28         li.appendChild(document.createTextNode(k + ": " + payload[k]));
29         ul.appendChild(li);
30     }
31     this.data_holder.appendChild(ul);
32 };
33 CaptureDisplay.prototype._generate_barcode_line = function(payload) {
34     var div = document.createElement("div");
35     div.appendChild(document.createTextNode(
36         localeStrings.BARCODE + ": " + payload.resource.barcode()
37     ));
38     return div;
39 };
40 CaptureDisplay.prototype._generate_title_line = function(payload) {
41     var div = document.createElement("div");
42     div.appendChild(document.createTextNode(
43         localeStrings.TITLE + ": " +
44         (payload.mvr ? payload.mvr.title() : payload.type.name())
45     ));
46     return div;
47 };
48 CaptureDisplay.prototype._generate_author_line = function(payload) {
49     var div = document.createElement("div");
50     if (payload.mvr) {
51         div.appendChild(document.createTextNode(
52             localeStrings.AUTHOR + ": " + payload.mvr.author()
53         ));
54     }
55     return div;
56 };
57 CaptureDisplay.prototype._generate_transit_notice = function(payload) {
58     var div = document.createElement("div");
59     if (payload.transit) {
60         div.setAttribute("class", "transit_notice");
61         div.appendChild(document.createTextNode(localeStrings.TRANSIT));
62     }
63     return div;
64 };
65 CaptureDisplay.prototype._generate_route_line = function(payload) {
66     var div = document.createElement("div");
67     var strong = document.createElement("strong");
68     strong.appendChild(document.createTextNode(
69         (payload.transit ?
70             fieldmapper.aou.findOrgUnit(payload.transit.dest()).shortname() :
71             localeStrings.RESERVATION_SHELF) + ":"
72     ));
73     div.appendChild(document.createTextNode(
74         localeStrings.NEEDS_ROUTED_TO + " "
75     ));
76     div.appendChild(strong);
77     return div;
78 };
79 CaptureDisplay.prototype._generate_patron_info = function(payload) {
80     var p = document.createElement("p");
81     p.innerHTML = "<strong>" + localeStrings.RESERVED + "</strong> " +
82         formal_name(payload.reservation.usr()) + "<br />" +
83         localeStrings.BARCODE + ": " +
84         payload.reservation.usr().card().barcode();
85     return p;
86 };
87 CaptureDisplay.prototype._generate_resv_info = function(payload) {
88     var p = document.createElement("p");
89     p.innerHTML = localeStrings.REQUEST + ": " +
90         humanize_timestamp_string(payload.reservation.request_time()) +
91         "<br />" + 
92         localeStrings.DURATION + ": " +
93         humanize_timestamp_string(payload.reservation.start_time()) +
94         " - " + 
95         humanize_timestamp_string(payload.reservation.end_time());
96     return p;
97 };
98 CaptureDisplay.prototype._generate_meta_info = function(result) {
99     var p = document.createElement("p");
100     p.innerHTML = localeStrings.SLIP_DATE + ": " + result.servertime +
101         "<br />" + localeStrings.PRINTED_BY + " " +
102         formal_name(openils.User.user) + " " + localeStrings.AT + " " +
103         fieldmapper.aou.findOrgUnit(openils.User.user.ws_ou()).shortname()
104     return p;
105 };
106 CaptureDisplay.prototype.display_with_transit_info = function(result) {
107     var div = document.createElement("div");
108     var span = document.createElement("span");
109     span.appendChild(document.createTextNode(localeStrings.CAPTURE_INFO));
110     span.setAttribute("class", "capture_info");
111     this.control_holder.appendChild(span);
112
113     var button = document.createElement("button");
114     button.setAttribute("class", "print_slip");
115     button.setAttribute("type", "button");
116     button.setAttribute("accesskey", localeStrings.PRINT_ACCESSKEY);
117     button.innerHTML = localeStrings.PRINT;
118     button.onclick = function() {
119         try { dojo.byId("printing_iframe").contentWindow.print(); }
120         catch (E) { alert(E); } /* XXX */
121         return false;
122     };
123     this.control_holder.appendChild(button);
124
125     div.appendChild(this._generate_transit_notice(result.payload));
126
127     var p = document.createElement("p");
128     p.appendChild(this._generate_route_line(result.payload));
129     p.appendChild(this._generate_barcode_line(result.payload));
130     p.appendChild(this._generate_title_line(result.payload));
131     p.appendChild(this._generate_author_line(result.payload));
132     div.appendChild(p);
133
134     div.appendChild(this._generate_patron_info(result.payload));
135     div.appendChild(this._generate_resv_info(result.payload));
136     div.appendChild(this._generate_meta_info(result));
137
138     this._create_iframe(div);
139 };
140 CaptureDisplay.prototype._create_iframe = function(contents) {
141     var iframe = document.createElement("iframe");
142     iframe.setAttribute("name", "printing_iframe");
143     iframe.setAttribute("id", "printing_iframe");
144     iframe.setAttribute("src", "");
145     iframe.setAttribute("width", "100%");
146     iframe.setAttribute("height", "400"); /* hardcode 400px? really? */
147
148     this.data_holder.appendChild(iframe);
149
150     var w = dojo.byId("printing_iframe").contentWindow;
151     w.document.open();
152     w.document.write(
153         "<html><head><link rel='stylesheet' type='text/css' href='" +
154         dojo.byId("booking_stylesheet_link").href +
155         "' /><body></body></html>"
156     );
157     w.document.close();
158     w.document.body.appendChild(contents);
159     /* FIXME if (determine_autoprint_setting_somehow()) w.print(); */
160 };
161 CaptureDisplay.prototype.clear = function() {
162     this.control_holder.innerHTML = "";
163     this.data_holder.innerHTML = "";
164 };
165 CaptureDisplay.prototype.load = function(result) {
166     try {
167         this.control_holder.appendChild(document.createElement("hr"));
168         if (!result.payload) {
169             this.no_payload();
170         } else if (!result.payload.fail_cause && result.payload.captured) {
171             this.display_with_transit_info(result);
172         } else {
173             this.dump(result.payload);
174         }
175     } catch (E) {
176         alert(E); /* XXX */
177     }
178 };
179
180 var capture_display;
181 var last_result;
182
183 function clear_for_next() {
184     if (last_result == CAPTURE_SUCCESS) {
185         last_result = undefined;
186         document.getElementById("result_display").innerHTML = "";
187         document.getElementById("resource_barcode").value = "";
188     }
189 }
190
191 function capture() {
192     var barcode = document.getElementById("resource_barcode").value;
193     var result = fieldmapper.standardRequest(
194         [
195             "open-ils.booking",
196             "open-ils.booking.resources.capture_for_reservation"
197         ],
198         [openils.User.authtoken, barcode]
199     );
200
201     if (result && result.ilsevent !== undefined) {
202         if (result.payload && result.payload.captured > 0) {
203             capture_display.load(result);
204             return CAPTURE_SUCCESS;
205         } else {
206             capture_display.load(result);
207             alert(my_ils_error(localeStrings.CAPTURED_NOTHING, result));
208             return CAPTURE_FAILURE;
209         }
210     } else {
211         return CAPTURE_UNKNOWN;
212     }
213 }
214
215 function attempt_capture() {
216     var rd = document.getElementById("result_display");
217     capture_display.clear();
218     switch(last_result = capture()) {
219         case CAPTURE_FAILURE:
220             rd.setAttribute("class", "capture_failure");
221             rd.innerHTML = localeStrings.FAILURE;
222             break;
223         case CAPTURE_SUCCESS:
224             rd.setAttribute("class", "capture_success");
225             rd.innerHTML = localeStrings.SUCCESS;
226             break;
227         default:
228             alert(localeStrings.UNKNOWN_PROBLEM);
229             break;
230     }
231 }
232
233 function my_init() {
234     init_auto_l10n(dojo.byId("auto_l10n_start_here"));
235     capture_display = new CaptureDisplay(
236         dojo.byId("capture_info_top"),
237         dojo.byId("capture_info_bottom")
238     );
239 }