]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/circ/offline_renew.js
Improve Firefox/XULRunner Support
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / circ / offline_renew.js
1 var offlineStrings;
2 var local_lock = false;
3
4 function my_init() {
5     try {
6         offlineStrings = $('offlineStrings');
7         if (typeof JSAN == 'undefined') { throw( "The JSAN library object is missing."); }
8         JSAN.errorLevel = "die"; // none, warn, or die
9         JSAN.addRepository('..');
10         JSAN.use('util.error'); g.error = new util.error();
11         g.error.sdump('D_TRACE','my_init() for offline_renew.xul');
12
13         if (typeof window.xulG == 'object' && typeof window.xulG.set_tab_name == 'function') {
14             try { window.xulG.set_tab_name('Standalone'); } catch(E) { alert(E); }
15         }
16
17         JSAN.use('OpenILS.data'); g.data = new OpenILS.data(); g.data.init({'via':'stash'});
18
19         JSAN.use('util.list'); g.list = new util.list('checkout_list');
20         JSAN.use('circ.util');
21         g.list.init( {
22             'columns' : circ.util.offline_renew_columns(),
23             'map_row_to_column' : circ.util.std_map_row_to_column(),
24         } );
25
26         JSAN.use('util.date');
27         var today = new Date();
28         var todayPlus = new Date(); todayPlus.setTime( today.getTime() + 24*60*60*1000*14 );
29         todayPlus = util.date.formatted_date(todayPlus,"%F");
30
31         function handle_lock(ev) {
32             if (!(ev.altKey || ev.ctrlKey || ev.metakey)) {
33                 if (!local_lock) {
34                     local_lock = true;
35                     xulG.lock();
36                 }
37             }
38         }
39
40         $('duedate').setAttribute('value',todayPlus);
41         $('duedate').addEventListener('change',check_date,false);
42
43         $('p_barcode').addEventListener('keypress',handle_lock,false);
44         $('p_barcode').addEventListener('change',test_patron,false);
45
46         $('p_barcode').addEventListener('keypress',handle_keypress,false);
47         $('p_barcode').focus();    
48
49         $('i_barcode').addEventListener('keypress',handle_lock,false);
50         $('i_barcode').addEventListener('keypress',handle_keypress,false);
51         $('enter').addEventListener('command',handle_enter,false);
52
53         $('duedate_menu').addEventListener('command',handle_duedate_menu,false);
54
55         $('submit').addEventListener('command',next_patron,false);
56         $('cancel').addEventListener('command',function(){next_patron('cancel');},false);
57
58         JSAN.use('util.file');
59         var file = new util.file('offline_delta'); 
60         if (file._file.exists()) { g.delta = file.get_object()[0]; file.close(); } else { g.delta = 0; }
61
62     } catch(E) {
63         var err_msg = "!! This software has encountered an error.  Please tell your friendly " +
64             "system administrator or software developer the following:\ncirc/offline_renew.xul\n" + E + '\n';
65         try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); }
66         alert(err_msg);
67     }
68 }
69
70 function $(id) { return document.getElementById(id); }
71
72 function test_patron(ev) {
73     try {
74         var barcode = ev.target.value;
75         JSAN.use('util.barcode');
76         if ( ($('strict_p_barcode').checked) && (! util.barcode.check(barcode)) ) {
77             var r = g.error.yns_alert('This barcode has a bad checkdigit.','Barcode Warning','Ok','Clear',null,'Check here to confirm this message');
78             if (r == 1) {
79                 setTimeout(
80                     function() {
81                         ev.target.value = '';
82                         ev.target.focus();
83                     },0
84                 );
85             }
86
87         }
88
89         if (g.data.bad_patrons[barcode]) {
90             var msg = 'Warning: As of ' + g.data.bad_patrons_date.substr(0,15) + ', this barcode (' + barcode + ') was flagged ';
91             switch(g.data.bad_patrons[barcode]) {
92                 case 'L' : msg += 'Lost'; break;
93                 case 'E' : msg += 'Expired'; break;
94                 case 'B' : msg += 'Barred'; break;
95                 case 'D' : msg += 'Blocked'; break;
96                 default : msg += ' with an unknown code: ' + g.data.bad_patrons[barcode]; break;
97             }
98             var r = g.error.yns_alert(msg,'Barcode Warning','Ok','Clear',null,'Check here to confirm this message');
99             if (r == 1) {
100                 setTimeout(
101                     function() {
102                         ev.target.value = '';
103                         ev.target.focus();
104                     },0
105                 );
106             }
107         }
108     } catch(E) {
109         alert(E);
110     }
111 }
112
113 function handle_keypress(ev) {
114     if ( (! ev.keyCode) || (ev.keyCode != 13) ) return;
115     switch(ev.target) {
116         case $('p_barcode') : setTimeout( function() { $('i_barcode').focus(); },0 ); break;
117         case $('i_barcode') : handle_enter(); break;
118         default: break;
119     }
120 }
121
122 function handle_enter(ev) {
123     JSAN.use('util.barcode');
124     if ( ($('strict_i_barcode').checked) && (! util.barcode.check($('i_barcode').value)) ) {
125         var r = g.error.yns_alert('This barcode has a bad checkdigit.','Barcode Warning','Ok','Clear',null,'Check here to confirm this message');
126         if (r == 1) {
127             setTimeout(
128                 function() {
129                     ev.target.value = '';
130                     ev.target.focus();
131                 },0
132             );
133         } else {
134             append_to_list('barcode');
135         }
136     } else {
137         append_to_list('barcode');
138     }
139 }
140
141 function handle_duedate_menu(ev) {
142     if (ev.target.value=='0') return; 
143     JSAN.use('util.date'); 
144     var today = new Date(); 
145     var todayPlus = new Date(); 
146     todayPlus.setTime( today.getTime() + 24*60*60*1000*ev.target.value ); 
147     todayPlus = util.date.formatted_date(todayPlus,'%F'); 
148     $('duedate').setAttribute('value',todayPlus); 
149     $('duedate').value = todayPlus;
150 }
151
152 function check_date(ev) {
153     JSAN.use('util.date');
154     try {
155         if (! util.date.check('YYYY-MM-DD',ev.target.value) ) { throw('Invalid Date'); }
156         if (util.date.check_past('YYYY-MM-DD',ev.target.value) ) { throw('Due date needs to be after today.'); }
157         if ( util.date.formatted_date(new Date(),'%F') == ev.target.value) { throw('Due date needs to be after today.'); }
158     } catch(E) {
159         alert(E);
160         var today = new Date();
161         var todayPlus = new Date(); todayPlus.setTime( today.getTime() + 24*60*60*1000*14 );
162         todayPlus = util.date.formatted_date(todayPlus,"%F");
163         ev.target.value = todayPlus;
164     }
165 }
166
167 function append_to_list(checkout_type,count) {
168
169     try {
170
171         var my = {};
172
173         my.type = 'renew';
174         my.timestamp = parseInt( new Date().getTime() / 1000) + g.delta;
175         my.checkout_time = util.date.formatted_date(new Date(),"%F %H:%M:%s");
176
177         var p_barcode = $('p_barcode').value;
178         if (! p_barcode) {
179             /* Not strictly necessary for a renewal
180             alert('Please enter a patron barcode first.');
181             return;
182             */
183         } else {
184
185             // Need to validate patron barcode against bad patron list
186             my.patron_barcode = p_barcode;
187         }
188
189         var due_date = $('duedate').value; // Need to validate this
190         my.due_date = due_date;
191
192         var i_barcode = $('i_barcode').value;
193         switch(checkout_type) {
194             case 'barcode' : 
195                 if (! i_barcode) return; 
196                 
197                 var rows = g.list.dump_with_keys();
198                 for (var i = 0; i < rows.length; i++) {
199                     if (rows[i].barcode == i_barcode) {
200                         g.error.yns_alert('This barcode has already been scanned.','Duplicate Scan','Ok',null,null,'Check here to confirm this message');
201                         return;
202                     }
203                 }
204
205                 my.barcode = i_barcode; 
206             break;
207             default: alert("Please report that this happened."); break;
208         }
209     
210         g.list.append( { 'row' : { 'my' : my }, 'to_top' : true } );
211
212         var x = $('i_barcode'); x.value = ''; x.focus();
213
214         if (!local_lock) {
215             local_lock = true;
216             xulG.lock();
217         }
218
219     } catch(E) {
220
221         dump(E+'\n'); alert(E);
222
223     }
224 }
225
226 function next_patron(cancel) {
227     try {
228
229         if (cancel!='cancel') {
230                 JSAN.use('util.file'); var file = new util.file('pending_xacts');
231                 var rows = g.list.dump_with_keys();
232                 for (var i = 0; i < rows.length; i++) {
233                     var row = rows[i]; row.delta = g.delta;
234                     if (row.patron_barcode == '') {
235                         delete(row.patron_barcode);
236                     }
237                     file.append_object(row);
238                 }
239                 file.close();
240
241                 if (local_lock) {
242                     local_lock = false;
243                     xulG.unlock();
244                 }
245                
246                 if ($('print_receipt').checked) {
247                     try {
248                         var params = {
249                             'patron_barcode' : $('p_barcode').value,
250                             'template' : 'offline_renew',
251                             'printer_context' : 'offline',
252                             'callback' : function() {
253                                 g.list.clear();
254                                 var x = $('i_barcode'); x.value = '';
255                                 x = $('p_barcode'); x.value = ''; x.focus();
256                             }
257                         };
258                         g.list.print( params );
259                     } catch(E) {
260                         g.error.sdump('D_ERROR','print: ' + E);
261                         alert('print: ' + E);
262                     }
263                 } else {
264                     g.list.clear();
265                     var x = $('i_barcode'); x.value = '';
266                     x = $('p_barcode'); x.value = ''; x.focus();
267                 }
268         }
269     } catch(E) {
270         dump(E+'\n'); alert(E);
271     }
272 }