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