]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/circ/offline_renew.js
don't do js2JSON on a negative number
[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('keypress',handle_keypress,false);
32                 $('p_barcode').focus(); 
33
34                 $('i_barcode').addEventListener('keypress',handle_keypress,false);
35                 $('enter').addEventListener('command',handle_enter,false);
36
37                 $('duedate_menu').addEventListener('command',handle_duedate_menu,false);
38
39                 $('submit').addEventListener('command',next_patron,false);
40
41                 JSAN.use('util.file');
42                 var file = new util.file('offline_delta'); 
43                 if (file._file.exists()) { g.delta = file.get_object()[0]; file.close(); } else { g.delta = 0; }
44
45         } catch(E) {
46                 var err_msg = "!! This software has encountered an error.  Please tell your friendly " +
47                         "system administrator or software developer the following:\ncirc/offline_renew.xul\n" + E + '\n';
48                 try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); }
49                 alert(err_msg);
50         }
51 }
52
53 function $(id) { return document.getElementById(id); }
54
55 function handle_keypress(ev) {
56         if ( (! ev.keyCode) || (ev.keyCode != 13) ) return;
57         switch(ev.target) {
58                 case $('p_barcode') : $('i_barcode').focus(); break;
59                 case $('i_barcode') : append_to_list('barcode'); break;
60                 default: break;
61         }
62 }
63
64 function handle_enter(ev) {
65         append_to_list('barcode');
66 }
67
68 function handle_duedate_menu(ev) {
69         if (ev.target.value=='0') return; 
70         JSAN.use('util.date'); 
71         var today = new Date(); 
72         var todayPlus = new Date(); 
73         todayPlus.setTime( today.getTime() + 24*60*60*1000*ev.target.value ); 
74         todayPlus = util.date.formatted_date(todayPlus,'%F'); 
75         $('duedate').setAttribute('value',todayPlus); 
76         $('duedate').value = todayPlus;
77 }
78
79 function check_date(ev) {
80         JSAN.use('util.date');
81         try {
82                 if (! util.date.check('YYYY-MM-DD',ev.target.value) ) { throw('Invalid Date'); }
83                 if (util.date.check_past('YYYY-MM-DD',ev.target.value) ) { throw('Due date needs to be after today.'); }
84                 if ( util.date.formatted_date(new Date(),'%F') == ev.target.value) { throw('Due date needs to be after today.'); }
85         } catch(E) {
86                 alert(E);
87                 var today = new Date();
88                 var todayPlus = new Date(); todayPlus.setTime( today.getTime() + 24*60*60*1000*14 );
89                 todayPlus = util.date.formatted_date(todayPlus,"%F");
90                 ev.target.value = todayPlus;
91         }
92 }
93
94 function append_to_list(checkout_type,count) {
95
96         try {
97
98                 var my = {};
99
100                 my.type = 'renew';
101                 my.timestamp = parseInt( new Date().getTime() / 1000) + g.delta;
102                 my.checkout_time = util.date.formatted_date(new Date(),"%F %H:%M:%s");
103
104                 var p_barcode = $('p_barcode').value;
105                 if (! p_barcode) {
106                         /* Not strictly necessary for a renewal
107                         alert('Please enter a patron barcode first.');
108                         return;
109                         */
110                 } else {
111
112                         // Need to validate patron barcode against bad patron list
113                         my.patron_barcode = p_barcode;
114                 }
115
116                 var due_date = $('duedate').value; // Need to validate this
117                 my.due_date = due_date;
118
119                 var i_barcode = $('i_barcode').value;
120                 switch(checkout_type) {
121                         case 'barcode' : 
122                                 if (! i_barcode) return; 
123                                 
124                                 var rows = g.list.dump_with_keys();
125                                 for (var i = 0; i < rows.length; i++) {
126                                         if (rows[i].barcode == i_barcode) {
127                                                 g.error.yns_alert('This barcode has already been scanned.','Duplicate Scan','Ok',null,null,'Check here to confirm this message');
128                                                 return;
129                                         }
130                                 }
131
132                                 my.barcode = i_barcode; 
133                         break;
134                         default: alert("Please report that this happened."); break;
135                 }
136         
137                 g.list.append( { 'row' : { 'my' : my } } );
138
139                 var x = $('i_barcode'); x.value = ''; x.focus();
140
141         } catch(E) {
142
143                 dump(E+'\n'); alert(E);
144
145         }
146 }
147
148 function next_patron() {
149         try {
150                 JSAN.use('util.file'); var file = new util.file('pending_xacts');
151                 var rows = g.list.dump_with_keys();
152                 for (var i = 0; i < rows.length; i++) {
153                         var row = rows[i]; row.delta = g.delta;
154                         if (row.patron_barcode == '') {
155                                 delete(row.patron_barcode);
156                         }
157                         file.append_object(row);
158                 }
159                 file.close();
160                 
161                 if ($('print_receipt').checked) {
162                         try {
163                                 var params = {
164                                         'patron_barcode' : $('p_barcode').value,
165                                         'header' : g.data.print_list_templates.offline_renew.header,
166                                         'line_item' : g.data.print_list_templates.offline_renew.line_item,
167                                         'footer' : g.data.print_list_templates.offline_renew.footer,
168                                         'type' : g.data.print_list_templates.offline_renew.type,
169                                         'list' : g.list.dump(),
170                                 };
171                                 JSAN.use('util.print'); var print = new util.print();
172                                 print.tree_list( params );
173                         } catch(E) {
174                                 g.error.sdump('D_ERROR','print: ' + E);
175                                 alert('print: ' + E);
176                         }
177                 }
178
179                 g.list.clear();
180                 
181                 var x;
182                 x = $('i_barcode'); x.value = '';
183                 x = $('p_barcode'); x.value = ''; x.focus();
184
185         } catch(E) {
186                 dump(E+'\n'); alert(E);
187         }
188 }