LP1816475: Booking module refresh
[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_notes_line = function(payload) {
80     var p = document.createElement("p");
81     if (payload.reservation.note()) {
82         p.innerHTML = "<strong>" + payload.reservation.note() + "</strong>";
83     }
84     return p;
85 };
86 CaptureDisplay.prototype._generate_patron_info = function(payload) {
87     var p = document.createElement("p");
88     p.innerHTML = "<strong>" + localeStrings.RESERVED + "</strong> " +
89         formal_name(payload.reservation.usr()) + "<br />" +
90         localeStrings.BARCODE + ": " +
91         payload.reservation.usr().card().barcode();
92     return p;
93 };
94 CaptureDisplay.prototype._generate_resv_info = function(payload) {
95     var p = document.createElement("p");
96     p.innerHTML = localeStrings.REQUEST + ": " +
97         humanize_timestamp_string(payload.reservation.request_time()) +
98         "<br />" + 
99         localeStrings.DURATION + ": " +
100         humanize_timestamp_string(payload.reservation.start_time()) +
101         " - " + 
102         humanize_timestamp_string(payload.reservation.end_time());
103     return p;
104 };
105 CaptureDisplay.prototype._generate_meta_info = function(result) {
106     var p = document.createElement("p");
107     p.innerHTML = localeStrings.SLIP_DATE + ": " + result.servertime +
108         "<br />" + localeStrings.PRINTED_BY + " " +
109         formal_name(openils.User.user) + " " + localeStrings.AT + " " +
110         fieldmapper.aou.findOrgUnit(openils.User.user.ws_ou()).shortname()
111     return p;
112 };
113 CaptureDisplay.prototype.display_with_transit_info = function(result) {
114     var div = document.createElement("div");
115     var span = document.createElement("span");
116     span.appendChild(document.createTextNode(localeStrings.CAPTURE_INFO));
117     span.setAttribute("class", "capture_info");
118     this.control_holder.appendChild(span);
119
120     var button = document.createElement("button");
121     button.setAttribute("class", "print_slip");
122     button.setAttribute("type", "button");
123     button.setAttribute("accesskey", localeStrings.PRINT_ACCESSKEY);
124     button.innerHTML = localeStrings.PRINT;
125     button.onclick = function() {
126         try { dojo.byId("printing_iframe").contentWindow.print(); }
127         catch (E) { alert(E); } /* XXX */
128         return false;
129     };
130     this.control_holder.appendChild(button);
131
132     div.appendChild(this._generate_transit_notice(result.payload));
133
134     var p = document.createElement("p");
135     p.appendChild(this._generate_route_line(result.payload));
136     p.appendChild(this._generate_barcode_line(result.payload));
137     p.appendChild(this._generate_title_line(result.payload));
138     p.appendChild(this._generate_author_line(result.payload));
139     div.appendChild(p);
140
141     div.appendChild(this._generate_notes_line(result.payload));
142
143     div.appendChild(this._generate_patron_info(result.payload));
144     div.appendChild(this._generate_resv_info(result.payload));
145     div.appendChild(this._generate_meta_info(result));
146
147     this._create_iframe(div);
148 };
149 CaptureDisplay.prototype._create_iframe = function(contents) {
150     var iframe = document.createElement("iframe");
151     iframe.setAttribute("name", "printing_iframe");
152     iframe.setAttribute("id", "printing_iframe");
153     iframe.setAttribute("src", "");
154     iframe.setAttribute("width", "100%");
155     iframe.setAttribute("height", "400"); /* hardcode 400px? really? */
156
157     this.data_holder.appendChild(iframe);
158
159     var w = dojo.byId("printing_iframe").contentWindow;
160     w.document.open();
161     w.document.write(
162         "<html><head><link rel='stylesheet' type='text/css' href='" +
163         dojo.byId("booking_stylesheet_link").href +
164         "' /><body></body></html>"
165     );
166     w.document.close();
167     w.document.body.appendChild(contents);
168     /* FIXME if (determine_autoprint_setting_somehow()) w.print(); */
169 };
170 CaptureDisplay.prototype.clear = function() {
171     this.control_holder.innerHTML = "";
172     this.data_holder.innerHTML = "";
173 };
174 CaptureDisplay.prototype.load = function(result) {
175     try {
176         this.control_holder.appendChild(document.createElement("hr"));
177         if (!result.payload) {
178             this.no_payload();
179         } else if (!result.payload.fail_cause && result.payload.captured) {
180             this.display_with_transit_info(result);
181         } else {
182             this.dump(result.payload);
183         }
184     } catch (E) {
185         alert(E); /* XXX */
186     }
187 };
188
189 var capture_display;
190 var last_result;
191
192 function clear_for_next() {
193     if (last_result == CAPTURE_SUCCESS) {
194         last_result = undefined;
195         document.getElementById("result_display").innerHTML = "";
196         document.getElementById("resource_barcode").value = "";
197     }
198 }
199
200 function capture() {
201     var barcode = document.getElementById("resource_barcode").value;
202     var result = fieldmapper.standardRequest(
203         [
204             "open-ils.booking",
205             "open-ils.booking.resources.capture_for_reservation"
206         ],
207         [openils.User.authtoken, barcode]
208     );
209
210     if (result && result.ilsevent !== undefined) {
211         if (result.payload && result.payload.captured > 0) {
212             capture_display.load(result);
213             return CAPTURE_SUCCESS;
214         } else {
215             capture_display.load(result);
216             alert(my_ils_error(localeStrings.CAPTURED_NOTHING, result));
217             return CAPTURE_FAILURE;
218         }
219     } else {
220         return CAPTURE_UNKNOWN;
221     }
222 }
223
224 function attempt_capture() {
225     var rd = document.getElementById("result_display");
226     capture_display.clear();
227     switch(last_result = capture()) {
228         case CAPTURE_FAILURE:
229             rd.setAttribute("class", "capture_failure");
230             rd.innerHTML = localeStrings.FAILURE;
231             break;
232         case CAPTURE_SUCCESS:
233             rd.setAttribute("class", "capture_success");
234             rd.innerHTML = localeStrings.SUCCESS;
235             break;
236         default:
237             alert(localeStrings.UNKNOWN_PROBLEM);
238             break;
239     }
240 }
241
242 function my_init() {
243     init_auto_l10n(dojo.byId("auto_l10n_start_here"));
244     capture_display = new CaptureDisplay(
245         dojo.byId("capture_info_top"),
246         dojo.byId("capture_info_bottom")
247     );
248 }