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