]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/circ/checkout.js
trying to focus on an editable menulist's inputField
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / circ / checkout.js
1 dump('entering circ.checkout.js\n');
2
3 if (typeof circ == 'undefined') circ = {};
4 circ.checkout = function (params) {
5
6         JSAN.use('util.error'); this.error = new util.error();
7         JSAN.use('util.network'); this.network = new util.network();
8         JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init({'via':'stash'});
9 }
10
11 circ.checkout.prototype = {
12
13         'init' : function( params ) {
14
15                 var obj = this;
16
17                 obj.session = params['session'];
18                 obj.patron_id = params['patron_id'];
19                 obj.patron = obj.network.simple_request('FM_AU_RETRIEVE_VIA_ID',[obj.session,obj.patron_id]);
20
21                 JSAN.use('circ.util');
22                 var columns = circ.util.columns( 
23                         { 
24                                 'barcode' : { 'hidden' : false },
25                                 'title' : { 'hidden' : false },
26                                 'due_date' : { 'hidden' : false },
27                         } 
28                 );
29
30                 JSAN.use('util.list'); obj.list = new util.list('checkout_list');
31                 obj.list.init(
32                         {
33                                 'columns' : columns,
34                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
35                         }
36                 );
37                 
38                 JSAN.use('util.controller'); obj.controller = new util.controller();
39                 obj.controller.init(
40                         {
41                                 'control_map' : {
42                                         'checkout_menu_placeholder' : [
43                                                 ['render'],
44                                                 function(e) {
45                                                         return function() {
46                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
47                                                                 var items = [ [ 'Barcode:' , 'barcode' ] ].concat(
48                                                                         util.functional.map_list(
49                                                                                 obj.data.list.cnct,
50                                                                                 function(o) {
51                                                                                         return [ o.name(), o.id() ];
52                                                                                 }
53                                                                         )
54                                                                 );
55                                                                 g.error.sdump('D_TRACE','items = ' + js2JSON(items));
56                                                                 util.widgets.remove_children( e );
57                                                                 var ml = util.widgets.make_menulist(
58                                                                         items
59                                                                 );
60                                                                 e.appendChild( ml );
61                                                                 ml.setAttribute('id','checkout_menulist');
62                                                                 ml.setAttribute('accesskey','');
63                                                                 ml.addEventListener(
64                                                                         'command',
65                                                                         function(ev) {
66                                                                                 var tb = obj.controller.view.checkout_barcode_entry_textbox;
67                                                                                 if (ev.target.value == 'barcode') {
68                                                                                         tb.disabled = false;
69                                                                                         tb.value = '';
70                                                                                         tb.focus();
71                                                                                 } else {
72                                                                                         tb.disabled = true;
73                                                                                         tb.value = 'Non-Cataloged';
74                                                                                 }
75                                                                         }, false
76                                                                 );
77                                                                 obj.controller.view.checkout_menu = ml;
78                                                         };
79                                                 },
80                                         ],
81                                         'checkout_barcode_entry_textbox' : [
82                                                 ['keypress'],
83                                                 function(ev) {
84                                                         if (ev.keyCode && ev.keyCode == 13) {
85                                                                 obj.checkout( { barcode: ev.target.value } );
86                                                         }
87                                                 }
88                                         ],
89                                         'checkout_duedate_menu' : [
90                                                 ['change'],
91                                                 function(ev) { 
92                                                         try {
93                                                                 obj.check_date(ev.target);
94                                                                 ev.target.parentNode.setAttribute('style','');
95                                                         } catch(E) {
96                                                                 ev.target.parentNode.setAttribute('style','background-color: red');
97                                                                 alert(E + '\nUse this format: YYYY-MM-DD');
98                                                                 try {
99                                                                         ev.target.inputField.select();
100                                                                         ev.target.inputField.focus();
101                                                                 } catch(E) { /* this should work, let me try on other platforms */ 
102                                                                         obj.error.sdump('D_ERROR','menulist.inputField: ' + E);
103                                                                 }
104                                                         }
105                                                 }
106                                         ],
107                                         'cmd_broken' : [
108                                                 ['command'],
109                                                 function() { alert('Not Yet Implemented'); }
110                                         ],
111                                         'cmd_checkout_submit' : [
112                                                 ['command'],
113                                                 function() {
114                                                         var params = {}; var count = 1;
115
116                                                         if (obj.controller.view.checkout_menu.value == 'barcode' ||
117                                                                 obj.controller.view.checkout_menu.value == '') {
118                                                                 params.barcode = obj.controller.view.checkout_barcode_entry_textbox.value;
119                                                         } else {
120                                                                 params.noncat = 1;
121                                                                 params.noncat_type = obj.controller.view.checkout_menu.value;
122                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
123                                                                 var r = window.prompt('Enter the number of ' + obj.data.hash.cnct[ params.noncat_type].name() + ' circulating:','1','Non-cataloged Items');
124                                                                 if (r) {
125                                                                         count = parseInt(r);
126                                                                         if (count > 0) {
127                                                                                 if (count > 20) {
128                                                                                         r = obj.error.yns_alert('Are you sure you want to circulate ' + count + ' ' + obj.data.hash.cnct[ params.noncat_type].name() + '?','Non-cataloged Circulation','Yes','No',null,'Check here to confirm this message.');
129                                                                                         if (r != 0) return;
130                                                                                 }
131                                                                         } else {
132                                                                                 r = obj.error.yns_alert('Error with non-cataloged checkout.  ' + r + ' is not a valid number.','Non-cataloged Circulation','Ok',null,null,'Check here to confirm this message.');
133                                                                                 return;
134                                                                         }
135                                                                 } else {
136                                                                         return;
137                                                                 }
138                                                         }
139                                                         for (var i = 0; i < count; i++) {
140                                                                 obj.checkout( params );
141                                                         }
142                                                 }
143                                         ],
144                                         'cmd_checkout_print' : [
145                                                 ['command'],
146                                                 function() {
147                                                         try {
148                                                                 var params = { 
149                                                                         'patron' : obj.patron, 
150                                                                         'lib' : obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ],
151                                                                         'staff' : obj.data.list.au[0],
152                                                                         'header' : obj.data.print_list_templates.checkout.header,
153                                                                         'line_item' : obj.data.print_list_templates.checkout.line_item,
154                                                                         'footer' : obj.data.print_list_templates.checkout.footer,
155                                                                         'type' : obj.data.print_list_templates.checkout.type,
156                                                                         'list' : obj.list.dump(),
157                                                                 };
158                                                                 JSAN.use('util.print'); var print = new util.print();
159                                                                 print.tree_list( params );
160                                                         } catch(E) {
161                                                                 this.error.sdump('D_ERROR','preview: ' + E);
162                                                                 alert('preview: ' + E);
163                                                         }
164
165                                                 }
166                                         ],
167                                         'cmd_checkout_reprint' : [
168                                                 ['command'],
169                                                 function() {
170                                                 }
171                                         ],
172                                         'cmd_checkout_done' : [
173                                                 ['command'],
174                                                 function() {
175                                                 }
176                                         ],
177                                 }
178                         }
179                 );
180                 this.controller.render();
181                 this.controller.view.checkout_barcode_entry_textbox.focus();
182
183         },
184
185         'check_date' : function(node) {
186                 JSAN.use('util.date');
187                 try {
188                         if (node.value == 'Normal') return true;
189                         var pattern = node.value.match(/Today \+ (\d+) days/);
190                         if (pattern) {
191                                 var today = new Date();
192                                 var todayPlus = new Date(); todayPlus.setTime( today.getTime() + 24*60*60*1000*pattern[1] );
193                                 node.value = util.date.formatted_date(todayPlus,"%F");
194                         }
195                         if (! util.date.check('YYYY-MM-DD',node.value) ) { throw('Invalid Date'); }
196                         if (util.date.check_past('YYYY-MM-DD',node.value) ) { throw('Due date needs to be after today.'); }
197                         if ( util.date.formatted_date(new Date(),'%F') == node.value) { throw('Due date needs to be after today.'); }
198                         return true;
199                 } catch(E) {
200                         throw(E);
201                 }
202         },
203
204         'checkout' : function(params) {
205                 var obj = this;
206
207                 try { obj.check_date(obj.controller.view.checkout_duedate_menu); } catch(E) { return; }
208                 if (obj.controller.view.checkout_duedate_menu.value != 'Normal') {
209                         params.due_date = obj.controller.view.checkout_duedate_menu.value;
210                 }
211
212                 if (! (params.barcode||params.noncat)) return;
213
214                 /**********************************************************************************************************************/
215                 /* This does the actual checkout/renewal, but is called after a permit test further below */
216                 function check_out(params) {
217
218                         var checkout = obj.network.request(
219                                 api.CHECKOUT.app,
220                                 api.CHECKOUT.method,
221                                 [ obj.session, params ]
222                         );
223
224                         if (checkout.ilsevent == 0) {
225
226                                 if (!checkout.payload) checkout.payload = {};
227
228                                 if (!checkout.payload.circ) {
229                                         checkout.payload.circ = new aoc();
230                                         /*********************************************************************************************/
231                                         /* Non Cat */
232                                         if (checkout.payload.noncat_circ) {
233                                                 checkout.payload.circ.circ_lib( checkout.payload.noncat_circ.circ_lib() );
234                                                 checkout.payload.circ.circ_staff( checkout.payload.noncat_circ.staff() );
235                                                 checkout.payload.circ.usr( checkout.payload.noncat_circ.patron() );
236                                                 
237                                                 JSAN.use('util.date');
238                                                 var c = checkout.payload.noncat_circ.circ_time();
239                                                 var d = c == "now" ? new Date() : util.date.db_date2Date( c );
240                                                 var t =obj.data.hash.cnct[ checkout.payload.noncat_circ.item_type() ];
241                                                 var cd = t.circ_duration() || "14 days";
242                                                 var i = util.date.interval_to_seconds( cd ) * 1000;
243                                                 d.setTime( Date.parse(d) + i );
244                                                 checkout.payload.circ.due_date( util.date.formatted_date(d,'%F') );
245
246                                         }
247                                 }
248
249                                 if (!checkout.payload.record) {
250                                         checkout.payload.record = new mvr();
251                                         /*********************************************************************************************/
252                                         /* Non Cat */
253                                         if (checkout.payload.noncat_circ) {
254                                                 checkout.payload.record.title(
255                                                         obj.data.hash.cnct[ checkout.payload.noncat_circ.item_type() ].name()
256                                                 );
257                                         }
258                                 }
259
260                                 if (!checkout.payload.copy) {
261                                         checkout.payload.copy = new acp();
262                                         checkout.payload.copy.barcode( '' );
263                                 }
264
265                                 /*********************************************************************************************/
266                                 /* Override mvr title/author with dummy title/author for Pre cat */
267                                 if (checkout.payload.copy.dummy_title())  checkout.payload.record.title( checkout.payload.copy.dummy_title() );
268                                 if (checkout.payload.copy.dummy_author())  checkout.payload.record.author( checkout.payload.copy.dummy_author() );
269
270                                 obj.list.append(
271                                         {
272                                                 'row' : {
273                                                         'my' : {
274                                                         'circ' : checkout.payload.circ,
275                                                         'mvr' : checkout.payload.record,
276                                                         'acp' : checkout.payload.copy
277                                                         }
278                                                 }
279                                         //I could override map_row_to_column here
280                                         }
281                                 );
282                                 if (typeof obj.on_checkout == 'function') {
283                                         obj.on_checkout(checkout.payload);
284                                 }
285                                 if (typeof window.xulG == 'object' && typeof window.xulG.on_list_change == 'function') {
286                                         window.xulG.on_list_change(checkout.payload);
287                                 } else {
288                                         obj.error.sdump('D_CIRC','circ.checkout: No external .on_checkout()\n');
289                                 }
290                         } else {
291                                 throw(checkout);
292                         }
293                 }
294
295                 /**********************************************************************************************************************/
296                 /* Permissibility test before checkout */
297                 try {
298
299                         params.patron = obj.patron_id;
300
301                         var permit = obj.network.request(
302                                 api.CHECKOUT_PERMIT.app,
303                                 api.CHECKOUT_PERMIT.method,
304                                 [ obj.session, params ],
305                                 null,
306                                 {
307                                         'title' : 'Override Checkout Failure?',
308                                         'overridable_events' : [ 7004, 7006 ],
309                                 }
310                         );
311
312                         /**********************************************************************************************************************/
313                         /* Normal case, proceed with checkout */
314                         if (permit.ilsevent == 0) {
315
316                                 JSAN.use('util.sound'); var sound = new util.sound(); sound.circ_good();
317                                 params.permit_key = permit.payload;
318                                 check_out( params );
319
320                         /**********************************************************************************************************************/
321                         /* Item not cataloged or barcode mis-scan.  Prompt for pre-cat option */
322                         } else if (permit.ilsevent == 1202) {
323
324                                 if ( 1 == obj.error.yns_alert(
325                                         'Mis-scan or non-cataloged item.  Checkout as a pre-cataloged item?',
326                                         'Alert',
327                                         'Cancel',
328                                         'Pre-Cat',
329                                         null,
330                                         null
331                                 ) ) {
332
333                                         obj.data.dummy_title = ''; obj.data.dummy_author = ''; obj.data.stash('dummy_title','dummy_author');
334                                         JSAN.use('util.window'); var win = new util.window();
335                                         win.open(urls.XUL_PRE_CAT, 'dummy_fields', 'chrome,resizable,modal');
336                                         obj.data.stash_retrieve();
337
338                                         params.permit_key = permit.payload;
339                                         params.dummy_title = obj.data.dummy_title;
340                                         params.dummy_author = obj.data.dummy_author;
341                                         params.precat = 1;
342
343                                         if (params.dummy_title != '') { check_out( params ); } else { throw('Checkout cancelled'); }
344                                 } 
345
346                         } else {
347                                 throw(permit);
348                         }
349
350                 } catch(E) {
351                         if (E.ilsevent && E.ilsevent == -1) {
352                                 obj.error.standard_network_error_alert('Check Out Failed.  If you wish to use the offline interface, in the top menubar select Circulation -> Offline Interface');
353                         } else {
354                                 obj.error.standard_unexpected_error_alert('Check Out Failed',E);
355                         }
356                         if (typeof obj.on_failure == 'function') {
357                                 obj.on_failure(E);
358                         }
359                         if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
360                                 obj.error.sdump('D_CIRC','circ.checkout: Calling external .on_failure()\n');
361                                 window.xulG.on_failure(E);
362                         } else {
363                                 obj.error.sdump('D_CIRC','circ.checkout: No external .on_failure()\n');
364                         }
365                 }
366
367         },
368
369         'on_checkout' : function() {
370                 this.controller.view.checkout_menu.selectedIndex = 0;
371                 this.controller.view.checkout_barcode_entry_textbox.disabled = false;
372                 this.controller.view.checkout_barcode_entry_textbox.value = '';
373                 this.controller.view.checkout_barcode_entry_textbox.focus();
374         },
375
376         'on_failure' : function() {
377                 this.controller.view.checkout_barcode_entry_textbox.select();
378                 this.controller.view.checkout_barcode_entry_textbox.focus();
379         }
380 }
381
382 dump('exiting circ.checkout.js\n');