]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/circ/offline_checkout.js
seconds instead of milliseconds
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / circ / offline_checkout.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_checkout.xul');
9
10                 JSAN.use('util.widgets'); JSAN.use('util.file');
11
12                 if (typeof window.xulG == 'object' && typeof window.xulG.set_tab_name == 'function') {
13                         try { window.xulG.set_tab_name('Standalone'); } catch(E) { alert(E); }
14                 }
15
16                 JSAN.use('util.list'); g.list = new util.list('checkout_list');
17                 g.list.init( {
18                         'columns' : [
19                                 { 
20                                         'id' : 'timestamp', 
21                                         'label' : 'Timestamp', 
22                                         'flex' : 1, 'primary' : false, 'hidden' : true, 
23                                         'render' : 'my.timestamp' 
24                                 },
25                                 { 
26                                         'id' : 'checkout_time', 
27                                         'label' : 'Check Out Time', 
28                                         'flex' : 1, 'primary' : false, 'hidden' : true, 
29                                         'render' : 'my.checkout_time' 
30                                 },
31                                 { 
32                                         'id' : 'type', 
33                                         'label' : 'Transaction Type', 
34                                         'flex' : 1, 'primary' : false, 'hidden' : true, 
35                                         'render' : 'my.type' 
36                                 },
37                                 {
38                                         'id' : 'noncat',
39                                         'label' : 'Non-Cataloged?',
40                                         'flex' : 1, 'primary' : false, 'hidden' : true, 
41                                         'render' : 'my.noncat'
42                                 },
43                                 {
44                                         'id' : 'noncat_type',
45                                         'label' : 'Non-Cat Type ID',
46                                         'flex' : 1, 'primary' : false, 'hidden' : true,
47                                         'render' : 'my.noncat_type'
48                                 },
49                                 {
50                                         'id' : 'noncat_count',
51                                         'label' : 'Count',
52                                         'flex' : 1, 'primary' : false, 'hidden' : false,
53                                         'render' : 'my.noncat_count'
54                                 },
55                                 { 
56                                         'id' : 'patron_barcode', 
57                                         'label' : 'Patron Barcode', 
58                                         'flex' : 1, 'primary' : false, 'hidden' : true, 
59                                         'render' : 'my.patron_barcode' 
60                                 },
61                                 { 
62                                         'id' : 'barcode', 
63                                         'label' : 'Item Barcode', 
64                                         'flex' : 2, 'primary' : true, 'hidden' : false, 
65                                         'render' : 'my.barcode' 
66                                 },
67                                 { 
68                                         'id' : 'due_date', 
69                                         'label' : 'Due Date', 
70                                         'flex' : 1, 'primary' : false, 'hidden' : false, 
71                                         'render' : 'my.due_date' 
72                                 },
73                         ],
74                         'map_row_to_column' : function(row,col) {
75                                 // row contains { 'my' : { 'barcode' : xxx, 'duedate' : xxx } }
76                                 // col contains one of the objects listed above in columns
77
78                                 var my = row.my;
79                                 var value;
80                                 try {
81                                         value = eval( col.render );
82                                         if (typeof value == 'undefined') value = '';
83
84                                 } catch(E) {
85                                         JSAN.use('util.error'); var error = new util.error();
86                                         error.sdump('D_WARN','map_row_to_column: ' + E);
87                                         value = '???';
88                                 }
89                                 return value;
90                         }
91                 } );
92
93                 JSAN.use('util.date');
94                 var today = new Date();
95                 var todayPlus = new Date(); todayPlus.setTime( today.getTime() + 24*60*60*1000*14 );
96                 todayPlus = util.date.formatted_date(todayPlus,"%F");
97
98                 $('duedate').setAttribute('value',todayPlus);
99                 $('duedate').addEventListener('change',check_date,false);
100
101                 $('p_barcode').addEventListener('keypress',handle_keypress,false);
102                 $('p_barcode').focus(); 
103
104                 $('i_barcode').addEventListener('keypress',handle_keypress,false);
105                 $('enter').addEventListener('command',handle_enter,false);
106
107                 $('duedate_menu').addEventListener('command',handle_duedate_menu,false);
108
109                 $('submit').addEventListener('command',function(ev){
110                         save_xacts(); next_patron(); /* kludge */ ev.target.focus(); next_patron();
111                 },false);
112                 $('cancel').addEventListener('command',function(ev){
113                         next_patron(); /* kludge */ ev.target.focus(); next_patron();
114                 },false);
115
116                 var file; var list_data; var ml;
117
118                 file = new util.file('offline_cnct_list'); 
119                 if (file._file.exists()) {
120                         list_data = file.get_object(); file.close();
121                         ml = util.widgets.make_menulist( 
122                                 [ ['or choose a non-barcoded option...', ''] ].concat(list_data[0]), 
123                                 list_data[1] 
124                         );
125                         ml.setAttribute('id','noncat_type_menu'); $('x_noncat_type').appendChild(ml);
126                         ml.addEventListener(
127                                 'command',
128                                 function(ev) { 
129                                         var count = window.prompt('Enter the number of items:',1,ml.getAttribute('label'));
130                                         append_to_list('noncat',count); 
131                                         ml.value = '';
132                                 },
133                                 false
134                         );
135                 } else {
136                         alert('WARNING: The non-barcode types have not been downloaded from the server.  You should log in to retrieve these.');
137                 }
138
139                 var file = new util.file('offline_delta'); 
140                 if (file._file.exists()) { g.delta = file.get_object(); file.close(); } else { g.delta = 0; }
141
142         } catch(E) {
143                 var err_msg = "!! This software has encountered an error.  Please tell your friendly " +
144                         "system administrator or software developer the following:\ncirc/offline_checkout.xul\n" + E + '\n';
145                 try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); }
146                 alert(err_msg);
147         }
148 }
149
150 function $(id) { return document.getElementById(id); }
151
152 function check_date(ev) {
153         JSAN.use('util.date');
154         try {
155                 if (! util.date.check('YYYY-MM-DD',ev.target.value) ) { throw('Invalid Date'); }
156                 if (util.date.check_past('YYYY-MM-DD',ev.target.value) ) { throw('Due date needs to be after today.'); }
157                 if ( util.date.formatted_date(new Date(),'%F') == ev.target.value) { throw('Due date needs to be after today.'); }
158         } catch(E) {
159                 alert(E);
160                 var today = new Date();
161                 var todayPlus = new Date(); todayPlus.setTime( today.getTime() + 24*60*60*1000*14 );
162                 todayPlus = util.date.formatted_date(todayPlus,"%F");
163                 ev.target.value = todayPlus;
164         }
165 }
166
167 function handle_keypress(ev) {
168         if ( (! ev.keyCode) || (ev.keyCode != 13) ) return;
169         switch(ev.target) {
170                 case $('p_barcode') : $('p_barcode').disabled = true; $('i_barcode').focus(); break;
171                 case $('i_barcode') : append_to_list('barcode'); break;
172                 default: break;
173         }
174 }
175
176 function handle_enter(ev) {
177         append_to_list('barcode');
178 }
179
180 function handle_duedate_menu(ev) {
181         if (ev.target.value=='0') return; 
182         JSAN.use('util.date'); 
183         var today = new Date(); 
184         var todayPlus = new Date(); 
185         todayPlus.setTime( today.getTime() + 24*60*60*1000*ev.target.value ); 
186         todayPlus = util.date.formatted_date(todayPlus,'%F'); 
187         $('duedate').setAttribute('value',todayPlus); 
188         $('duedate').value = todayPlus;
189 }
190
191 function append_to_list(checkout_type,count) {
192
193         try {
194
195                 var my = {};
196
197                 my.type = 'checkout';
198                 my.timestamp = parseInt( new Date().getTime() / 1000);
199                 my.checkout_time = util.date.formatted_date(new Date(),"%F %H:%M:%s");
200
201                 var p_barcode = $('p_barcode').value;
202                 if (! p_barcode) {
203                         alert('Please enter a patron barcode first.');
204                         return;
205                 } else {
206
207                         // Need to validate patron barcode against bad patron list
208                         my.patron_barcode = p_barcode;
209                 }
210
211                 var due_date = $('duedate').value; // Need to validate this
212                 my.due_date = due_date;
213
214                 var i_barcode = $('i_barcode').value;
215                 switch(checkout_type) {
216                         case 'barcode' : 
217                                 if (! i_barcode) return; 
218                                 
219                                 var rows = g.list.dump_with_keys();
220                                 for (var i = 0; i < rows.length; i++) {
221                                         if (rows[i].barcode == i_barcode) {
222                                                 alert('This barcode has already been scanned.');
223                                                 return;
224                                         }
225                                 }
226
227                                 my.barcode = i_barcode; 
228                         break;
229                         case 'noncat' :
230                                 count = parseInt(count); if (! (count>0) ) {
231                                         alert("Please try again and enter a valid count.");
232                                         return;
233                                 }
234                                 my.barcode = $('noncat_type_menu').getAttribute('label');
235                                 my.noncat = 1;
236                                 my.noncat_type = JSON2js($('noncat_type_menu').value)[0];
237                                 my.noncat_count = count;
238                         break;
239                         default: alert("Please report that this happened."); break;
240                 }
241         
242                 g.list.append( { 'row' : { 'my' : my } } );
243
244                 var x = $('i_barcode'); x.value = ''; x.focus();
245
246         } catch(E) {
247
248                 dump(E+'\n'); alert(E);
249
250         }
251 }
252
253
254 function save_xacts() {
255         JSAN.use('util.file'); var file = new util.file('pending_xacts');
256         var rows = g.list.dump_with_keys();
257         for (var i = 0; i < rows.length; i++) {
258                 var row = rows[i]; row.delta = g.delta;
259                 if (row.noncat == 1) {
260                         delete(row.barcode);
261                 } else {
262                         delete(row.noncat);
263                         delete(row.noncat_type);
264                         delete(row.noncat_count);
265                 }
266                 file.append_object(row);
267         }
268         file.close();
269 }
270
271 function next_patron() {
272         try {
273                 g.list.clear();
274                 
275                 var x;
276                 x = $('i_barcode'); x.value = '';
277                 x = $('p_barcode'); x.value = ''; 
278                 x.setAttribute('disabled','false'); x.disabled = false; 
279                 x.focus();
280
281         } catch(E) {
282                 dump(E+'\n'); alert(E);
283         }
284 }