LP1125471 - Use label instead of id in recording in-house use prompt
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / circ / in_house_use.js
1 dump('entering circ.in_house_use.js\n');
2 // vim:noet:sw=4:ts=4:
3
4 if (typeof circ == 'undefined') circ = {};
5 circ.in_house_use = function (params) {
6
7     JSAN.use('util.error'); this.error = new util.error();
8     JSAN.use('util.network'); this.network = new util.network();
9     JSAN.use('util.barcode');
10     JSAN.use('util.date');
11     JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init({'via':'stash'});
12     JSAN.use('util.sound'); this.sound = new util.sound();
13 }
14
15 circ.in_house_use.prototype = {
16
17     'init' : function( params ) {
18
19         var obj = this;
20
21         obj.entry_cap = Number( obj.data.hash.aous['ui.circ.in_house_use.entry_cap'] ) || 99;
22         obj.entry_warn = Number( obj.data.hash.aous['ui.circ.in_house_use.entry_warn'] ) || 20;
23
24         JSAN.use('circ.util');
25         var columns = circ.util.columns( 
26             { 
27                 'barcode' : { 'hidden' : false },
28                 'title' : { 'hidden' : false },
29                 'status' : { 'hidden' : false },
30                 'location' : { 'hidden' : false },
31                 'call_number' : { 'hidden' : false },
32                 'uses' : { 'hidden' : false }
33             } 
34         );
35
36         JSAN.use('util.list'); obj.list = new util.list('in_house_use_list');
37         obj.list.init(
38             {
39                 'columns' : columns,
40                 'on_select' : function() {
41                     var sel = obj.list.retrieve_selection();
42                     obj.controller.view.sel_clip.setAttribute('disabled', sel.length < 1);
43                 }
44             }
45         );
46         
47         JSAN.use('util.controller'); obj.controller = new util.controller();
48         obj.controller.init(
49             {
50                 'control_map' : {
51                     'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
52                     'sel_clip' : [
53                         ['command'],
54                         function() { 
55                             obj.list.clipboard(); 
56                             obj.controller.view.in_house_use_barcode_entry_textbox.focus();
57                         }
58                     ],
59                     'in_house_use_menu_placeholder' : [
60                         ['render'],
61                         function(e) {
62                             return function() {
63                                 JSAN.use('util.widgets'); JSAN.use('util.functional'); JSAN.use('util.fm_utils');
64                                 var items = [ [ document.getElementById('circStrings').getString('staff.circ.in_house_use.barcode') , 'barcode' ] ].concat(
65                                     util.functional.map_list(
66                                         util.functional.filter_list(
67                                             obj.data.list.my_cnct,
68                                             function(o) {
69                                                 return util.fm_utils.compare_aou_a_is_b_or_ancestor(o.owning_lib(), obj.data.list.au[0].ws_ou());
70                                             }
71                                         ).sort(
72
73                                             function(a,b) {
74                                                 try { 
75                                                     return util.fm_utils.sort_func_aou_by_depth_and_then_string(
76                                                         [ a.owning_lib(), a.name() ],
77                                                         [ b.owning_lib(), b.name() ]
78                                                     );
79                                                 } catch(E) {
80                                                     alert(document.getElementById('circStrings').getString('staff.circ.in_house_use.noncat_sort_error') + ' ' + E);
81                                                     return 0;
82                                                 }
83                                             }
84
85                                         ),
86                                         function(o) {
87                                             return [ obj.data.hash.aou[ o.owning_lib() ].shortname() + ' : ' + o.name(), o.id() ];
88                                         }
89                                     )
90                                 );
91                                 g.error.sdump('D_TRACE', document.getElementById('circStrings').getString('staff.circ.in_house_use.items_dump') + js2JSON(items));
92                                 util.widgets.remove_children( e );
93                                 var ml = util.widgets.make_menulist(
94                                     items
95                                 );
96                                 e.appendChild( ml );
97                                 ml.setAttribute('id','in_house_use_menulist');
98                                 ml.setAttribute('accesskey','');
99                                 ml.addEventListener(
100                                     'command',
101                                     function(ev) {
102                                         var tb = obj.controller.view.in_house_use_barcode_entry_textbox;
103                                         if (ev.target.value == 'barcode') {
104                                             tb.disabled = false;
105                                             tb.value = '';
106                                             tb.focus();
107                                         } else {
108                                             tb.disabled = true;
109                                             tb.value = document.getElementById('circStrings').getString('staff.circ.in_house_use.noncataloged');
110                                         }
111                                     }, false
112                                 );
113                                 obj.controller.view.in_house_use_menu = ml;
114                             };
115                         },
116                     ],
117                     'in_house_use_barcode_entry_textbox' : [
118                         ['keypress'],
119                         function(ev) {
120                             if (ev.keyCode && ev.keyCode == 13) {
121                                 obj.in_house_use();
122                             }
123                         }
124                     ],
125                     'in_house_use_multiplier_label' : [
126                         ['render'],
127                         function(e) {
128                             return function() {
129                                 obj.controller.view.in_house_use_multiplier_textbox.select();
130                                 obj.controller.view.in_house_use_multiplier_textbox.value = 1;
131                             };
132                         }
133                     ],
134                     'in_house_use_multiplier_textbox' : [
135                         ['change'],
136                         function(ev) {
137                             if (ev.target.nodeName == 'textbox') {
138                                 try {
139                                     var value = Number(ev.target.value);
140                                     if (value > 0) {
141                                         if (value > obj.entry_cap) { throw(value); }
142                                     } else {
143                                         throw(value);
144                                     }
145                                 } catch(E) {
146                                     dump('in_house_use:multiplier: ' + E + '\n');
147                                     obj.sound.circ_bad();
148                                     setTimeout(
149                                         function() {
150                                             obj.controller.view.in_house_use_multiplier_textbox.focus();
151                                             obj.controller.view.in_house_use_multiplier_textbox.select();
152                                         }, 0
153                                     );
154                                 }
155                             }
156                         }
157                     ],
158                     'cmd_broken' : [
159                         ['command'],
160                         function() { alert(document.getElementById('circStrings').getString('staff.circ.unimplemented')); }
161                     ],
162                     'cmd_in_house_use_submit_barcode' : [
163                         ['command'],
164                         function() {
165                             obj.in_house_use();
166                         }
167                     ],
168                     'cmd_in_house_use_print' : [
169                         ['command'],
170                         function() {
171                             var p = { 
172                                 'template' : 'in_house_use'
173                             };
174                             obj.list.print(p);
175                         }
176                     ],
177                     'cmd_csv_to_clipboard' : [ ['command'], function() { 
178                         obj.list.dump_csv_to_clipboard(); 
179                         obj.controller.view.in_house_use_barcode_entry_textbox.focus();
180                     } ],
181                     'cmd_csv_to_printer' : [ ['command'], function() { 
182                         obj.list.dump_csv_to_printer(); 
183                         obj.controller.view.in_house_use_barcode_entry_textbox.focus();
184                     } ],
185                     'cmd_csv_to_file' : [ ['command'], function() { 
186                         obj.list.dump_csv_to_file( { 'defaultFileName' : 'checked_in.txt' } ); 
187                         obj.controller.view.in_house_use_barcode_entry_textbox.focus();
188                     } ]
189
190                 }
191             }
192         );
193         this.controller.render();
194         this.controller.view.in_house_use_barcode_entry_textbox.focus();
195
196     },
197
198     'test_barcode' : function(bc) {
199         var obj = this;
200         var good = util.barcode.check(bc);
201         var x = document.getElementById('strict_barcode');
202         if (x && x.checked != true) return true;
203         if (good) {
204             return true;
205         } else {
206             if ( 1 == obj.error.yns_alert(
207                         document.getElementById('circStrings').getFormattedString('staff.circ.check_digit.bad', [bc]),
208                         document.getElementById('circStrings').getString('staff.circ.barcode.bad'),
209                         document.getElementById('circStrings').getString('staff.circ.cancel'),
210                         document.getElementById('circStrings').getString('staff.circ.barcode.accept'),
211                         null,
212                         document.getElementById('circStrings').getString('staff.circ.confirm'),
213                         '/xul/server/skin/media/images/bad_barcode.png'
214             ) ) {
215                 return true;
216             } else {
217                 return false;
218             }
219         }
220     },
221
222     'in_house_use' : function() {
223         var obj = this;
224         try {
225             var barcode;
226             if (obj.controller.view.in_house_use_menu.value == '' || obj.controller.view.in_house_use_menu.value == 'barcode') {
227                 barcode = obj.controller.view.in_house_use_barcode_entry_textbox.value;
228                 if (barcode) {
229                     if ( obj.test_barcode(barcode) ) { /* good */ } else { /* bad */ return; }
230                 }
231             } else {
232                 barcode = obj.data.hash.cnct[ obj.controller.view.in_house_use_menu.value ].name();
233             }
234             var multiplier = Number( obj.controller.view.in_house_use_multiplier_textbox.value );
235
236             if (barcode == '') {
237                 obj.controller.view.in_house_use_barcode_entry_textbox.focus();
238                 return; 
239             }
240
241             if (multiplier == 0 || multiplier > obj.entry_cap) {
242                 obj.controller.view.in_house_use_multiplier_textbox.focus();
243                 obj.controller.view.in_house_use_multiplier_textbox.select();
244                 obj.sound.circ_bad();
245                 return;
246             }
247
248             if (multiplier > obj.entry_warn) {
249                 var r = obj.error.yns_alert(
250                     document.getElementById('circStrings').getFormattedString('staff.circ.in_house_use.confirm_multiple', [barcode, multiplier]),
251                     document.getElementById('circStrings').getString('staff.circ.in_house_use.confirm_multiple.title'),
252                     document.getElementById('circStrings').getString('staff.circ.in_house_use.yes'),
253                     document.getElementById('circStrings').getString('staff.circ.in_house_use.no'),
254                     null,
255                     document.getElementById('circStrings').getString('staff.circ.confirm.msg')
256                 );
257                 if (r != 0) {
258                     obj.controller.view.in_house_use_multiplier_textbox.focus();
259                     obj.controller.view.in_house_use_multiplier_textbox.select();
260                     return;
261                 }
262             }
263
264             JSAN.use('circ.util');
265
266             if (obj.controller.view.in_house_use_menu.value == 'barcode') {
267
268                 var copy = obj.network.simple_request('FM_ACP_RETRIEVE_VIA_BARCODE',[ barcode ]); 
269                 if (copy.ilsevent) { 
270                     switch(Number(copy.ilsevent)) {
271                         case -1 : 
272                             obj.error.standard_network_error_alert(document.getElementById('circStrings').getString('staff.circ.in_house_use.failed.verbose'));
273                             break;
274                         case 1502 /* ASSET_COPY_NOT_FOUND */ : 
275                             obj.error.yns_alert(
276                                 copy.textcode,
277                                 document.getElementById('circStrings').getString('staff.circ.in_house_use.failed'),
278                                 document.getElementById('circStrings').getString('staff.circ.in_house_use.ok'),
279                                 null,
280                                 null,
281                                 document.getElementById('circStrings').getString('staff.circ.confirm.msg')
282                             );
283                             break;
284                         default:
285                             throw(copy);
286                     }
287                     return; 
288                 }
289     
290                 var mods = obj.network.simple_request('MODS_SLIM_RECORD_RETRIEVE_VIA_COPY.authoritative',[ copy.id() ]);
291                 var result = obj.network.simple_request('FM_AIHU_CREATE',
292                     [ ses(), { 'copyid' : copy.id(), 'location' : obj.data.list.au[0].ws_ou(), 'count' : multiplier } ]
293                 );
294
295             } else {
296                 var result = obj.network.simple_request('FM_ANCIHU_CREATE',
297                     [ ses(), { 'non_cat_type' : obj.controller.view.in_house_use_menu.value, 'location' : obj.data.list.au[0].ws_ou(), 'count' : multiplier } ]
298                 );
299                 mods = new mvr(); mods.title( obj.data.hash.cnct[ obj.controller.view.in_house_use_menu.value ].name()); 
300                 copy = new acp(); copy.barcode( '' );
301             }
302
303             if (document.getElementById('trim_list')) {
304                 var x = document.getElementById('trim_list');
305                 if (x.checked) { obj.list.trim_list = 20; } else { obj.list.trim_list = null; }
306             }
307             obj.list.append(
308                 {
309                     'row' : {
310                         'my' : {
311                             'mvr' : mods,
312                             'acp' : copy,
313                             'uses' : result.length
314                         }
315                     },
316                     'to_top' : true
317                 //I could override map_row_to_column here
318                 }
319             );
320             obj.sound.circ_good();
321
322             if (typeof obj.on_in_house_use == 'function') {
323                 obj.on_in_house_use(result);
324             }
325             if (typeof window.xulG == 'object' && typeof window.xulG.on_in_house_use == 'function') {
326                 obj.error.sdump('D_CIRC', + document.getElementById('circStrings').getString('staff.circ.in_house_use.external') + '\n');
327                 window.xulG.on_in_house_use(result);
328             } else {
329                 obj.error.sdump('D_CIRC', + document.getElementById('circStrings').getString('staff.circ.in_house_use.no_external') + '\n');
330             }
331
332         } catch(E) {
333             obj.error.standard_unexpected_error_alert(document.getElementById('circStrings').getString('staff.circ.in_house_use.failed'), E);
334             if (typeof obj.on_failure == 'function') {
335                 obj.on_failure(E);
336             }
337             if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
338                 obj.error.sdump('D_CIRC', + document.getElementById('circStrings').getString('staff.circ.in_house_use.on_failure.external') + '\n');
339                 window.xulG.on_failure(E);
340             } else {
341                 obj.error.sdump('D_CIRC', + document.getElementById('circStrings').getString('staff.circ.in_house_use.on_failure.external') + '\n');
342             }
343         }
344
345     },
346
347     'on_in_house_use' : function() {
348         this.controller.view.in_house_use_multiplier_textbox.select();
349         this.controller.view.in_house_use_multiplier_textbox.value = '1';
350         this.controller.view.in_house_use_barcode_entry_textbox.value = '';
351         this.controller.view.in_house_use_barcode_entry_textbox.focus();
352     },
353
354     'on_failure' : function() {
355         this.controller.view.in_house_use_barcode_entry_textbox.select();
356         this.controller.view.in_house_use_barcode_entry_textbox.focus();
357     }
358 }
359
360 dump('exiting circ.in_house_use.js\n');