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