]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/circ/offline_renew.js
delta on each transaction, but we're not going that route afterall. Still good for...
[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                 g.list.init( {
16                         'columns' : [
17                                 { 
18                                         'id' : 'timestamp', 
19                                         'label' : 'Timestamp', 
20                                         'flex' : 1, 'primary' : false, 'hidden' : true, 
21                                         'render' : 'my.timestamp' 
22                                 },
23                                 { 
24                                         'id' : 'checkout_time', 
25                                         'label' : 'Check Out Time', 
26                                         'flex' : 1, 'primary' : false, 'hidden' : true, 
27                                         'render' : 'my.checkout_time' 
28                                 },
29                                 { 
30                                         'id' : 'type', 
31                                         'label' : 'Transaction Type', 
32                                         'flex' : 1, 'primary' : false, 'hidden' : true, 
33                                         'render' : 'my.type' 
34                                 },
35                                 { 
36                                         'id' : 'patron_barcode', 
37                                         'label' : 'Patron Barcode', 
38                                         'flex' : 1, 'primary' : false, 'hidden' : true, 
39                                         'render' : 'my.patron_barcode' 
40                                 },
41                                 { 
42                                         'id' : 'barcode', 
43                                         'label' : 'Item Barcode', 
44                                         'flex' : 2, 'primary' : true, 'hidden' : false, 
45                                         'render' : 'my.barcode' 
46                                 },
47                                 { 
48                                         'id' : 'due_date', 
49                                         'label' : 'Due Date', 
50                                         'flex' : 1, 'primary' : false, 'hidden' : false, 
51                                         'render' : 'my.due_date' 
52                                 },
53                         ],
54                         'map_row_to_column' : function(row,col) {
55                                 // row contains { 'my' : { 'barcode' : xxx, 'duedate' : xxx } }
56                                 // col contains one of the objects listed above in columns
57
58                                 var my = row.my;
59                                 var value;
60                                 try {
61                                         value = eval( col.render );
62                                         if (typeof value == 'undefined') value = '';
63
64                                 } catch(E) {
65                                         JSAN.use('util.error'); var error = new util.error();
66                                         error.sdump('D_WARN','map_row_to_column: ' + E);
67                                         value = '???';
68                                 }
69                                 return value;
70                         }
71                 } );
72
73                 JSAN.use('util.date');
74                 var today = new Date();
75                 var todayPlus = new Date(); todayPlus.setTime( today.getTime() + 24*60*60*1000*14 );
76                 todayPlus = util.date.formatted_date(todayPlus,"%F");
77
78                 $('duedate').setAttribute('value',todayPlus);
79                 $('duedate').addEventListener('change',check_date,false);
80
81                 $('p_barcode').addEventListener('keypress',handle_keypress,false);
82                 $('p_barcode').focus(); 
83
84                 $('i_barcode').addEventListener('keypress',handle_keypress,false);
85                 $('enter').addEventListener('command',handle_enter,false);
86
87                 $('duedate_menu').addEventListener('command',handle_duedate_menu,false);
88
89                 $('submit').addEventListener('command',next_patron,false);
90
91                 var file = new util.file('offline_delta'); 
92                 if (file._file.exists()) { g.delta = file.get_object(); file.close(); } else { g.delta = 0; }
93
94         } catch(E) {
95                 var err_msg = "!! This software has encountered an error.  Please tell your friendly " +
96                         "system administrator or software developer the following:\ncirc/offline_renew.xul\n" + E + '\n';
97                 try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); }
98                 alert(err_msg);
99         }
100 }
101
102 function $(id) { return document.getElementById(id); }
103
104 function handle_keypress(ev) {
105         if ( (! ev.keyCode) || (ev.keyCode != 13) ) return;
106         switch(ev.target) {
107                 case $('p_barcode') : $('i_barcode').focus(); break;
108                 case $('i_barcode') : append_to_list('barcode'); break;
109                 default: break;
110         }
111 }
112
113 function handle_enter(ev) {
114         append_to_list('barcode');
115 }
116
117 function handle_duedate_menu(ev) {
118         if (ev.target.value=='0') return; 
119         JSAN.use('util.date'); 
120         var today = new Date(); 
121         var todayPlus = new Date(); 
122         todayPlus.setTime( today.getTime() + 24*60*60*1000*ev.target.value ); 
123         todayPlus = util.date.formatted_date(todayPlus,'%F'); 
124         $('duedate').setAttribute('value',todayPlus); 
125         $('duedate').value = todayPlus;
126 }
127
128 function check_date(ev) {
129         JSAN.use('util.date');
130         try {
131                 if (! util.date.check('YYYY-MM-DD',ev.target.value) ) { throw('Invalid Date'); }
132                 if (util.date.check_past('YYYY-MM-DD',ev.target.value) ) { throw('Due date needs to be after today.'); }
133                 if ( util.date.formatted_date(new Date(),'%F') == ev.target.value) { throw('Due date needs to be after today.'); }
134         } catch(E) {
135                 alert(E);
136                 var today = new Date();
137                 var todayPlus = new Date(); todayPlus.setTime( today.getTime() + 24*60*60*1000*14 );
138                 todayPlus = util.date.formatted_date(todayPlus,"%F");
139                 ev.target.value = todayPlus;
140         }
141 }
142
143 function append_to_list(checkout_type,count) {
144
145         try {
146
147                 var my = {};
148
149                 my.type = 'renew';
150                 my.timestamp = new Date().getTime();
151                 my.checkout_time = util.date.formatted_date(new Date(),"%F %H:%M:%s");
152
153                 var p_barcode = $('p_barcode').value;
154                 if (! p_barcode) {
155                         /* Not strictly necessary for a renewal
156                         alert('Please enter a patron barcode first.');
157                         return;
158                         */
159                 } else {
160
161                         // Need to validate patron barcode against bad patron list
162                         my.patron_barcode = p_barcode;
163                 }
164
165                 var due_date = $('duedate').value; // Need to validate this
166                 my.due_date = due_date;
167
168                 var i_barcode = $('i_barcode').value;
169                 switch(checkout_type) {
170                         case 'barcode' : 
171                                 if (! i_barcode) return; 
172                                 
173                                 var rows = g.list.dump_with_keys();
174                                 for (var i = 0; i < rows.length; i++) {
175                                         if (rows[i].barcode == i_barcode) {
176                                                 alert('This barcode has already been scanned.');
177                                                 return;
178                                         }
179                                 }
180
181                                 my.barcode = i_barcode; 
182                         break;
183                         default: alert("Please report that this happened."); break;
184                 }
185         
186                 g.list.append( { 'row' : { 'my' : my } } );
187
188                 var x = $('i_barcode'); x.value = ''; x.focus();
189
190         } catch(E) {
191
192                 dump(E+'\n'); alert(E);
193
194         }
195 }
196
197 function next_patron() {
198         try {
199                 JSAN.use('util.file'); var file = new util.file('pending_xacts');
200                 var rows = g.list.dump_with_keys();
201                 for (var i = 0; i < rows.length; i++) {
202                         var row = rows[i]; row.delta = g.delta;
203                         if (row.patron_barcode == '') {
204                                 delete(row.patron_barcode);
205                         }
206                         file.append_object(row);
207                 }
208                 file.close();
209                 g.list.clear();
210                 
211                 var x;
212                 x = $('i_barcode'); x.value = '';
213                 x = $('p_barcode'); x.value = ''; x.focus();
214
215         } catch(E) {
216                 dump(E+'\n'); alert(E);
217         }
218 }