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