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