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