]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/booking/pull_list.js
Stab at matching up reservation.js with the pluralized, JSON-compliant Dojo NLS file
[working/Evergreen.git] / Open-ILS / web / js / ui / default / booking / pull_list.js
1 dojo.require("openils.User");
2 dojo.require("openils.PermaCrud");
3 dojo.require("fieldmapper.OrgUtils");
4 dojo.require("openils.widget.OrgUnitFilteringSelect");
5 dojo.requireLocalization("openils.booking", "pull_list");
6
7 var localeStrings = dojo.i18n.getLocalization("openils.booking", "pull_list");
8 var pcrud = new openils.PermaCrud();
9
10 var owning_lib_selected;
11 var acp_cache = {};
12
13 function init_owning_lib_selector() {
14     var User = new openils.User();
15     User.buildPermOrgSelector(
16         "RETRIEVE_RESERVATION_PULL_LIST", owning_lib_selector, null,
17         function() {
18             owning_lib_selected = owning_lib_selector.getValue();
19             dojo.connect(owning_lib_selector, "onChange",
20                 function() { owning_lib_selected = this.getValue(); }
21             )
22         }
23     );
24 }
25
26 function retrieve_pull_list(ivl_in_days) {
27     var secs = Number(ivl_in_days) * 86400;
28
29     if (isNaN(secs) || secs < 1)
30         throw new Error("Invalid interval");
31
32     return fieldmapper.standardRequest(
33         ["open-ils.booking", "open-ils.booking.reservations.get_pull_list"],
34         [xulG.auth.session.key, null, secs, owning_lib_selected]
35     );
36 }
37
38 function dom_table_rowid(resource_id) {
39     return "pull_list_resource_" + resource_id;
40 }
41
42 function generate_result_row(one) {
43     function cell(id, content) {
44         var td = document.createElement("td");
45         if (id != undefined) td.setAttribute("id", id);
46         td.appendChild(document.createTextNode(content));
47         return td;
48     }
49
50     function reservation_info_cell(one) {
51         var td = document.createElement("td");
52         for (var i in one.reservations) {
53             var one_resv = one.reservations[i];
54             var div = document.createElement("div");
55             var s = humanize_timestamp_string(one_resv.start_time()) + " - " +
56                 humanize_timestamp_string(one_resv.end_time()) + " " +
57                 formal_name(one_resv.usr());
58             /* FIXME: The above need patron barcode instead of name, but
59              * that requires a fix in the middle layer to flesh on the
60              * right stuff. */
61             div.appendChild(document.createTextNode(s));
62             td.appendChild(div);
63         }
64         return td;
65     }
66
67     var baseid = dom_table_rowid(one.current_resource.id());
68
69     var cells = [];
70     cells.push(cell(undefined, one.target_resource_type.name()));
71     cells.push(cell(undefined, one.current_resource.barcode()));
72     cells.push(cell(baseid + "_call_number", "-"));
73     cells.push(cell(baseid + "_copy_location", "-"));
74     cells.push(cell(baseid + "_copy_number", "-"));
75     cells.push(reservation_info_cell(one));
76
77     var row = document.createElement("tr");
78     row.setAttribute("id", baseid);
79
80     for (var i in cells) row.appendChild(cells[i]);
81     return row;
82 }
83
84 function render_pull_list_fundamentals(list) {
85     var rows = [];
86
87     for (var i in list)
88         rows.push(generate_result_row(list[i]));
89
90     document.getElementById("the_table_body").innerHTML = "";
91
92     for (var i in rows)
93         document.getElementById("the_table_body").appendChild(rows[i]);
94 }
95
96 function get_all_relevant_acp(list) {
97     var barcodes = [];
98     for (var i in list) {
99         if (list[i].target_resource_type.catalog_item()) {
100             /* There shouldn't be any duplicates. No need to worry bout that */
101             barcodes.push(list[i].current_resource.barcode());
102         }
103     }
104     if (barcodes.length > 0) {
105         var results = fieldmapper.standardRequest(
106             [
107                 "open-ils.booking",
108                 "open-ils.booking.asset.get_copy_fleshed_just_right"
109             ],
110             [xulG.auth.session.key, barcodes]
111         );
112
113         if (!results) {
114             alert(localeStrings.COPY_LOOKUP_NO_RESPONSE);
115             return null;
116         } else if (is_ils_event(results)) {
117             alert(my_ils_error(localeStrings.COPY_LOOKUP_ERROR, results));
118             return null;
119         } else {
120             return results;
121         }
122     }
123 }
124
125 function fill_in_pull_list_details(list, acp_cache) {
126     for (var i in list) {
127         var one = list[i];
128         if (one.target_resource_type.catalog_item() == "t") {
129             /* FIXME: This block could stand to be a lot more elegant. */
130             var call_number_el = document.getElementById(
131                 dom_table_rowid(one.current_resource.id()) + "_call_number"
132             );
133             var copy_location_el = document.getElementById(
134                 dom_table_rowid(one.current_resource.id()) + "_copy_location"
135             );
136             var copy_number_el = document.getElementById(
137                 dom_table_rowid(one.current_resource.id()) + "_copy_number"
138             );
139
140             var bc = one.current_resource.barcode();
141
142             if (acp_cache[bc]) {
143                 if (call_number_el && acp_cache[bc].call_number()) {
144                     var value = acp_cache[bc].call_number().label();
145                     if (value) call_number_el.innerHTML = value;
146                 }
147                 if (copy_location_el && acp_cache[bc].location()) {
148                     var value = acp_cache[bc].location().name();
149                     if (value) copy_location_el.innerHTML = value;
150                 }
151                 if (copy_number_el) {
152                     var value = acp_cache[bc].copy_number();
153                     if (value) copy_number_el.innerHTML = value;
154                 }
155             } else {
156                 alert(localeStrings.COPY_MISSING + bc);
157             }
158         }
159     }
160 }
161
162 function populate_pull_list(form) {
163     /* Step 1: get the pull list from the server. */
164     try {
165         var results = retrieve_pull_list(form.interval_in_days.value);
166     } catch (E) {
167         alert(localeStrings.PULL_LIST_ERROR + E);
168         return;
169     }
170     if (results == null) {
171         alert(localeStrings.PULL_LIST_NO_RESPONSE);
172         return;
173     } else if (is_ils_event(results)) {
174         alert(my_ils_error(localeStrings.PULL_LIST_ERROR, results));
175         return;
176     }
177
178     if (results.length) {
179         reveal_dom_element(document.getElementById("table_goes_here"));
180         hide_dom_element(document.getElementById("no_results"));
181
182         /* Step 2: render the table with the pull list */
183         render_pull_list_fundamentals(results);
184
185         /* Step 3: asynchronously fill in the copy details we're missing */
186         setTimeout(function() {
187             var acp_cache = {};
188             if ((acp_cache = get_all_relevant_acp(results)))
189                 fill_in_pull_list_details(results, acp_cache);
190         }, 0);
191     } else {
192         hide_dom_element(document.getElementById("table_goes_here"));
193         reveal_dom_element(document.getElementById("no_results"));
194     }
195
196 }
197
198 function my_init() {
199     hide_dom_element(document.getElementById("table_goes_here"));
200     hide_dom_element(document.getElementById("no_results"));
201     init_owning_lib_selector();
202     init_auto_l10n(document.getElementById("auto_l10n_start_here"));
203 }