]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/circ/checkout.js
simple offline detection
[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                                         'cmd_broken' : [
90                                                 ['command'],
91                                                 function() { alert('Not Yet Implemented'); }
92                                         ],
93                                         'cmd_checkout_submit' : [
94                                                 ['command'],
95                                                 function() {
96                                                         var params = {}; var count = 1;
97                                                         if (obj.controller.view.checkout_menu.value == 'barcode' ||
98                                                                 obj.controller.view.checkout_menu.value == '') {
99                                                                 params.barcode = obj.controller.view.checkout_barcode_entry_textbox.value;
100                                                         } else {
101                                                                 params.noncat = 1;
102                                                                 params.noncat_type = obj.controller.view.checkout_menu.value;
103                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
104                                                                 var r = window.prompt('Enter the number of ' + obj.data.hash.cnct[ params.noncat_type].name() + ' circulating:','1','Non-cataloged Items');
105                                                                 if (r) {
106                                                                         count = parseInt(r);
107                                                                         if (count > 0) {
108                                                                                 if (count > 20) {
109                                                                                         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.');
110                                                                                         if (r != 0) return;
111                                                                                 }
112                                                                         } else {
113                                                                                 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.');
114                                                                                 return;
115                                                                         }
116                                                                 } else {
117                                                                         return;
118                                                                 }
119                                                         }
120                                                         for (var i = 0; i < count; i++) {
121                                                                 obj.checkout( params );
122                                                         }
123                                                 }
124                                         ],
125                                         'cmd_checkout_print' : [
126                                                 ['command'],
127                                                 function() {
128                                                         try {
129                                                                 var params = { 
130                                                                         'patron' : obj.patron, 
131                                                                         'lib' : obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ],
132                                                                         'staff' : obj.data.list.au[0],
133                                                                         'header' : obj.data.print_list_templates.checkout.header,
134                                                                         'line_item' : obj.data.print_list_templates.checkout.line_item,
135                                                                         'footer' : obj.data.print_list_templates.checkout.footer,
136                                                                         'type' : obj.data.print_list_templates.checkout.type,
137                                                                         'list' : obj.list.dump(),
138                                                                 };
139                                                                 JSAN.use('util.print'); var print = new util.print();
140                                                                 print.tree_list( params );
141                                                         } catch(E) {
142                                                                 this.error.sdump('D_ERROR','preview: ' + E);
143                                                                 alert('preview: ' + E);
144                                                         }
145
146                                                 }
147                                         ],
148                                         'cmd_checkout_reprint' : [
149                                                 ['command'],
150                                                 function() {
151                                                 }
152                                         ],
153                                         'cmd_checkout_done' : [
154                                                 ['command'],
155                                                 function() {
156                                                 }
157                                         ],
158                                 }
159                         }
160                 );
161                 this.controller.render();
162                 this.controller.view.checkout_barcode_entry_textbox.focus();
163
164         },
165
166         'checkout' : function(params) {
167                 var obj = this;
168
169                 if (! (params.barcode||params.noncat)) return;
170
171                 /**********************************************************************************************************************/
172                 /* This does the actual checkout/renewal, but is called after a permit test further below */
173                 function check_out(params) {
174
175                         var checkout = obj.network.request(
176                                 api.CHECKOUT.app,
177                                 api.CHECKOUT.method,
178                                 [ obj.session, params ]
179                         );
180
181                         if (checkout.ilsevent == 0) {
182
183                                 if (!checkout.payload) checkout.payload = {};
184
185                                 if (!checkout.payload.circ) {
186                                         checkout.payload.circ = new aoc();
187                                         /*********************************************************************************************/
188                                         /* Non Cat */
189                                         if (checkout.payload.noncat_circ) {
190                                                 checkout.payload.circ.circ_lib( checkout.payload.noncat_circ.circ_lib() );
191                                                 checkout.payload.circ.circ_staff( checkout.payload.noncat_circ.staff() );
192                                                 checkout.payload.circ.usr( checkout.payload.noncat_circ.patron() );
193                                                 
194                                                 JSAN.use('util.date');
195                                                 var c = checkout.payload.noncat_circ.circ_time();
196                                                 var d = c == "now" ? new Date() : util.date.db_date2Date( c );
197                                                 var t =obj.data.hash.cnct[ checkout.payload.noncat_circ.item_type() ];
198                                                 var cd = t.circ_duration() || "14 days";
199                                                 var i = util.date.interval_to_seconds( cd ) * 1000;
200                                                 d.setTime( Date.parse(d) + i );
201                                                 checkout.payload.circ.due_date( util.date.formatted_date(d,'%F') );
202
203                                         }
204                                 }
205
206                                 if (!checkout.payload.record) {
207                                         checkout.payload.record = new mvr();
208                                         /*********************************************************************************************/
209                                         /* Non Cat */
210                                         if (checkout.payload.noncat_circ) {
211                                                 checkout.payload.record.title(
212                                                         obj.data.hash.cnct[ checkout.payload.noncat_circ.item_type() ].name()
213                                                 );
214                                         }
215                                 }
216
217                                 if (!checkout.payload.copy) {
218                                         checkout.payload.copy = new acp();
219                                         checkout.payload.copy.barcode( '' );
220                                 }
221
222                                 /*********************************************************************************************/
223                                 /* Override mvr title/author with dummy title/author for Pre cat */
224                                 if (checkout.payload.copy.dummy_title())  checkout.payload.record.title( checkout.payload.copy.dummy_title() );
225                                 if (checkout.payload.copy.dummy_author())  checkout.payload.record.author( checkout.payload.copy.dummy_author() );
226
227                                 obj.list.append(
228                                         {
229                                                 'row' : {
230                                                         'my' : {
231                                                         'circ' : checkout.payload.circ,
232                                                         'mvr' : checkout.payload.record,
233                                                         'acp' : checkout.payload.copy
234                                                         }
235                                                 }
236                                         //I could override map_row_to_column here
237                                         }
238                                 );
239                                 if (typeof obj.on_checkout == 'function') {
240                                         obj.on_checkout(checkout.payload);
241                                 }
242                                 if (typeof window.xulG == 'object' && typeof window.xulG.on_list_change == 'function') {
243                                         window.xulG.on_list_change(checkout.payload);
244                                 } else {
245                                         obj.error.sdump('D_CIRC','circ.checkout: No external .on_checkout()\n');
246                                 }
247                         } else {
248                                 throw(checkout);
249                         }
250                 }
251
252                 /**********************************************************************************************************************/
253                 /* Permissibility test before checkout */
254                 try {
255
256                         params.patron = obj.patron_id;
257
258                         var permit = obj.network.request(
259                                 api.CHECKOUT_PERMIT.app,
260                                 api.CHECKOUT_PERMIT.method,
261                                 [ obj.session, params ],
262                                 null,
263                                 {
264                                         'title' : 'Override Checkout Failure?',
265                                         'overridable_events' : [ 7004, 7006 ],
266                                 }
267                         );
268
269                         /**********************************************************************************************************************/
270                         /* Normal case, proceed with checkout */
271                         if (permit.ilsevent == 0) {
272
273                                 JSAN.use('util.sound'); var sound = new util.sound(); sound.circ_good();
274                                 params.permit_key = permit.payload;
275                                 check_out( params );
276
277                         /**********************************************************************************************************************/
278                         /* Item not cataloged or barcode mis-scan.  Prompt for pre-cat option */
279                         } else if (permit.ilsevent == 1202) {
280
281                                 if ( 1 == obj.error.yns_alert(
282                                         'Mis-scan or non-cataloged item.  Checkout as a pre-cataloged item?',
283                                         'Alert',
284                                         'Cancel',
285                                         'Pre-Cat',
286                                         null,
287                                         null
288                                 ) ) {
289
290                                         obj.data.dummy_title = ''; obj.data.dummy_author = ''; obj.data.stash('dummy_title','dummy_author');
291                                         JSAN.use('util.window'); var win = new util.window();
292                                         win.open(urls.XUL_PRE_CAT, 'dummy_fields', 'chrome,resizable,modal');
293                                         obj.data.stash_retrieve();
294
295                                         params.permit_key = permit.payload;
296                                         params.dummy_title = obj.data.dummy_title;
297                                         params.dummy_author = obj.data.dummy_author;
298                                         params.precat = 1;
299
300                                         if (params.dummy_title != '') { check_out( params ); } else { throw('Checkout cancelled'); }
301                                 } 
302
303                         } else {
304                                 throw(permit);
305                         }
306
307                 } catch(E) {
308                         if (E.ilsevent && E.ilsevent == -1) {
309                                 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');
310                         } else {
311                                 obj.error.standard_unexpected_error_alert('Check Out Failed',E);
312                         }
313                         if (typeof obj.on_failure == 'function') {
314                                 obj.on_failure(E);
315                         }
316                         if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
317                                 obj.error.sdump('D_CIRC','circ.checkout: Calling external .on_failure()\n');
318                                 window.xulG.on_failure(E);
319                         } else {
320                                 obj.error.sdump('D_CIRC','circ.checkout: No external .on_failure()\n');
321                         }
322                 }
323
324         },
325
326         'on_checkout' : function() {
327                 this.controller.view.checkout_menu.selectedIndex = 0;
328                 this.controller.view.checkout_barcode_entry_textbox.disabled = false;
329                 this.controller.view.checkout_barcode_entry_textbox.value = '';
330                 this.controller.view.checkout_barcode_entry_textbox.focus();
331         },
332
333         'on_failure' : function() {
334                 this.controller.view.checkout_barcode_entry_textbox.select();
335                 this.controller.view.checkout_barcode_entry_textbox.focus();
336         }
337 }
338
339 dump('exiting circ.checkout.js\n');