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