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