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