]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/circ/offline_checkout.js
offline noncat checkout
[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                 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' : 'noncat',
37                                         'label' : 'Non-Cataloged?',
38                                         'flex' : 1, 'primary' : false, 'hidden' : true, 
39                                         'render' : 'my.noncat'
40                                 },
41                                 {
42                                         'id' : 'noncat_type',
43                                         'label' : 'Non-Cat Type ID',
44                                         'flex' : 1, 'primary' : false, 'hidden' : true,
45                                         'render' : 'my.noncat_type'
46                                 },
47                                 {
48                                         'id' : 'noncat_count',
49                                         'label' : 'Count',
50                                         'flex' : 1, 'primary' : false, 'hidden' : false,
51                                         'render' : 'my.noncat_count'
52                                 },
53                                 { 
54                                         'id' : 'patron_barcode', 
55                                         'label' : 'Patron Barcode', 
56                                         'flex' : 1, 'primary' : false, 'hidden' : true, 
57                                         'render' : 'my.patron_barcode' 
58                                 },
59                                 { 
60                                         'id' : 'barcode', 
61                                         'label' : 'Item Barcode', 
62                                         'flex' : 2, 'primary' : true, 'hidden' : false, 
63                                         'render' : 'my.barcode' 
64                                 },
65                                 { 
66                                         'id' : 'due_date', 
67                                         'label' : 'Due Date', 
68                                         'flex' : 1, 'primary' : false, 'hidden' : false, 
69                                         'render' : 'my.due_date' 
70                                 },
71                         ],
72                         'map_row_to_column' : function(row,col) {
73                                 // row contains { 'my' : { 'barcode' : xxx, 'duedate' : xxx } }
74                                 // col contains one of the objects listed above in columns
75
76                                 var my = row.my;
77                                 var value;
78                                 try {
79                                         value = eval( col.render );
80                                         if (typeof value == 'undefined') value = '';
81
82                                 } catch(E) {
83                                         JSAN.use('util.error'); var error = new util.error();
84                                         error.sdump('D_WARN','map_row_to_column: ' + E);
85                                         value = '???';
86                                 }
87                                 return value;
88                         }
89                 } );
90
91                 JSAN.use('util.date');
92                 var today = new Date();
93                 var todayPlus = new Date(); todayPlus.setTime( today.getTime() + 24*60*60*1000*14 );
94                 todayPlus = util.date.formatted_date(todayPlus,"%F");
95
96                 $('duedate').setAttribute('value',todayPlus);
97
98                 $('p_barcode').addEventListener('keypress',handle_keypress,false);
99                 $('p_barcode').focus(); 
100
101                 $('i_barcode').addEventListener('keypress',handle_keypress,false);
102
103                 $('duedate_menu').addEventListener('command',handle_duedate_menu,false);
104
105                 $('submit').addEventListener('command',next_patron,false);
106
107                 JSAN.use('util.widgets'); JSAN.use('util.file');
108
109                 var file; var list_data; var ml;
110
111                 file = new util.file('offline_cnct_list'); list_data = file.get_object(); file.close();
112                 ml = util.widgets.make_menulist( 
113                         [ ['or choose a non-barcoded option...', ''] ].concat(list_data[0]), 
114                         list_data[1] 
115                 );
116                 ml.setAttribute('id','noncat_type_menu'); $('x_noncat_type').appendChild(ml);
117                 ml.addEventListener(
118                         'command',
119                         function(ev) { 
120                                 var count = window.prompt('Enter the number of items:',1,ml.getAttribute('label'));
121                                 append_to_list('noncat',count); 
122                                 ml.value = '';
123                         },
124                         false
125                 );
126
127         } catch(E) {
128                 var err_msg = "!! This software has encountered an error.  Please tell your friendly " +
129                         "system administrator or software developer the following:\ncirc/offline_checkout.xul\n" + E + '\n';
130                 try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); }
131                 alert(err_msg);
132         }
133 }
134
135 function $(id) { return document.getElementById(id); }
136
137 function handle_keypress(ev) {
138         if ( (! ev.keyCode) || (ev.keyCode != 13) ) return;
139         switch(ev.target) {
140                 case $('p_barcode') : $('i_barcode').focus(); break;
141                 case $('i_barcode') : append_to_list('barcode'); break;
142                 default: break;
143         }
144 }
145
146 function handle_duedate_menu(ev) {
147         if (ev.target.value=='0') return; 
148         JSAN.use('util.date'); 
149         var today = new Date(); 
150         var todayPlus = new Date(); 
151         todayPlus.setTime( today.getTime() + 24*60*60*1000*ev.target.value ); 
152         todayPlus = util.date.formatted_date(todayPlus,'%F'); 
153         $('duedate').setAttribute('value',todayPlus); 
154         $('duedate').value = todayPlus;
155 }
156
157 function handle_barcode_menu(ev) {
158 }
159
160 function append_to_list(checkout_type,count) {
161
162         try {
163
164                 var my = {};
165
166                 my.type = 'checkout';
167                 my.timestamp = new Date().getTime();
168                 my.checkout_time = util.date.formatted_date(new Date(),"%F %H:%M:%s");
169
170                 var p_barcode = $('p_barcode').value;
171                 if (! p_barcode) {
172                         alert('Please enter a patron barcode first.');
173                         return;
174                 } else {
175
176                         // Need to validate patron barcode against bad patron list
177                         my.patron_barcode = p_barcode;
178                 }
179
180                 var due_date = $('duedate').value; // Need to validate this
181                 my.due_date = due_date;
182
183                 var i_barcode = $('i_barcode').value;
184                 switch(checkout_type) {
185                         case 'barcode' : 
186                                 if (! i_barcode) return; 
187                                 
188                                 var rows = g.list.dump_with_keys();
189                                 for (var i = 0; i < rows.length; i++) {
190                                         if (rows[i].barcode == i_barcode) {
191                                                 alert('This barcode has already been scanned.');
192                                                 return;
193                                         }
194                                 }
195
196                                 my.barcode = i_barcode; 
197                         break;
198                         case 'noncat' :
199                                 count = parseInt(count); if (! (count>0) ) {
200                                         alert("Please try again and enter a valid count.");
201                                         return;
202                                 }
203                                 my.barcode = $('noncat_type_menu').getAttribute('label');
204                                 my.noncat = 1;
205                                 my.noncat_type = JSON2js($('noncat_type_menu').value)[0];
206                                 my.noncat_count = count;
207                         break;
208                         default: alert("Please report that this happened."); break;
209                 }
210         
211                 g.list.append( { 'row' : { 'my' : my } } );
212
213                 var x = $('i_barcode'); x.value = ''; x.focus();
214
215         } catch(E) {
216
217                 dump(E+'\n'); alert(E);
218
219         }
220 }
221
222 function next_patron() {
223         try {
224                 JSAN.use('util.file'); var file = new util.file('pending_xacts');
225                 var rows = g.list.dump_with_keys();
226                 for (var i = 0; i < rows.length; i++) {
227                         var row = rows[i];
228                         if (row.noncat == 1) {
229                                 delete(row.barcode);
230                         } else {
231                                 delete(row.noncat);
232                                 delete(row.noncat_type);
233                                 delete(row.noncat_count);
234                         }
235                         file.append_object(row);
236                 }
237                 file.close();
238                 g.list.clear();
239                 
240                 var x;
241                 x = $('i_barcode'); x.value = '';
242                 x = $('p_barcode'); x.value = ''; x.focus();
243
244         } catch(E) {
245                 dump(E+'\n'); alert(E);
246         }
247 }