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