]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/circ/checkin.js
better event handling
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / circ / checkin.js
1 dump('entering circ.checkin.js\n');
2
3 if (typeof circ == 'undefined') circ = {};
4 circ.checkin = 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('util.date');
9         this.OpenILS = {}; JSAN.use('OpenILS.data'); this.OpenILS.data = new OpenILS.data(); this.OpenILS.data.init({'via':'stash'});
10         this.data = this.OpenILS.data;
11 }
12
13 circ.checkin.prototype = {
14
15         'selection_list' : [],
16
17         'init' : function( params ) {
18
19                 var obj = this;
20
21                 JSAN.use('circ.util');
22                 var columns = circ.util.columns( 
23                         { 
24                                 'barcode' : { 'hidden' : false },
25                                 'title' : { 'hidden' : false },
26                                 'location' : { 'hidden' : false },
27                                 'call_number' : { 'hidden' : false },
28                                 'status' : { 'hidden' : false },
29                                 'route_to' : { 'hidden' : false },
30                                 'alert_message' : { 'hidden' : false },
31                         } 
32                 );
33
34                 JSAN.use('util.list'); obj.list = new util.list('checkin_list');
35                 obj.list.init(
36                         {
37                                 'columns' : columns,
38                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
39                                 'on_select' : function(ev) {
40                                         try {
41                                                 JSAN.use('util.functional');
42                                                 var sel = obj.list.retrieve_selection();
43                                                 obj.selection_list = util.functional.map_list(
44                                                         sel,
45                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
46                                                 );
47                                                 obj.error.sdump('D_TRACE', 'circ/copy_status: selection list = ' + js2JSON(obj.selection_list) );
48                                                 if (obj.selection_list.length == 0) {
49                                                         obj.controller.view.sel_edit.setAttribute('disabled','true');
50                                                         obj.controller.view.sel_opac.setAttribute('disabled','true');
51                                                         obj.controller.view.sel_patron.setAttribute('disabled','true');
52                                                         obj.controller.view.sel_bucket.setAttribute('disabled','true');
53                                                         obj.controller.view.sel_spine.setAttribute('disabled','true');
54                                                         obj.controller.view.sel_transit_abort.setAttribute('disabled','true');
55                                                 } else {
56                                                         obj.controller.view.sel_edit.setAttribute('disabled','false');
57                                                         obj.controller.view.sel_opac.setAttribute('disabled','false');
58                                                         obj.controller.view.sel_patron.setAttribute('disabled','false');
59                                                         obj.controller.view.sel_bucket.setAttribute('disabled','false');
60                                                         obj.controller.view.sel_spine.setAttribute('disabled','false');
61                                                         obj.controller.view.sel_transit_abort.setAttribute('disabled','false');
62                                                 }
63                                         } catch(E) {
64                                                 alert('FIXME: ' + E);
65                                         }
66                                 },
67
68                         }
69                 );
70                 
71                 JSAN.use('util.controller'); obj.controller = new util.controller();
72                 obj.controller.init(
73                         {
74                                 'control_map' : {
75                                         'sel_edit' : [
76                                                 ['command'],
77                                                 function() {
78                                                         try {
79                                                                 obj.spawn_copy_editor();
80                                                         } catch(E) {
81                                                                 alert(E);
82                                                         }
83                                                 }
84                                         ],
85                                         'sel_spine' : [
86                                                 ['command'],
87                                                 function() {
88                                                         JSAN.use('cat.util');
89                                                         cat.util.spawn_spine_editor(obj.selection_list);
90                                                 }
91                                         ],
92                                         'sel_opac' : [
93                                                 ['command'],
94                                                 function() {
95                                                         JSAN.use('cat.util');
96                                                         cat.util.show_in_opac(obj.selection_list);
97                                                 }
98                                         ],
99                                         'sel_transit_abort' : [
100                                                 ['command'],
101                                                 function() {
102                                                         JSAN.use('circ.util');
103                                                         circ.util.abort_transits(obj.selection_list);
104                                                 }
105                                         ],
106                                         'sel_patron' : [
107                                                 ['command'],
108                                                 function() {
109                                                         JSAN.use('circ.util');
110                                                         circ.util.show_last_few_circs(obj.selection_list);
111                                                 }
112                                         ],
113                                         'sel_bucket' : [
114                                                 ['command'],
115                                                 function() {
116                                                         JSAN.use('cat.util');
117                                                         cat.util.add_copies_to_bucket(obj.selection_list);
118                                                 }
119                                         ],
120                                         'checkin_barcode_entry_textbox' : [
121                                                 ['keypress'],
122                                                 function(ev) {
123                                                         if (ev.keyCode && ev.keyCode == 13) {
124                                                                 obj.checkin();
125                                                         }
126                                                 }
127                                         ],
128                                         'checkin_effective_date_label' : [
129                                                 ['render'],
130                                                 function(e) {
131                                                         return function() {
132                                                                 obj.controller.view.checkin_effective_date_textbox.value =
133                                                                         util.date.formatted_date(new Date(),'%F');
134                                                         };
135                                                 }
136                                         ],
137                                         'checkin_effective_date_textbox' : [
138                                                 ['change'],
139                                                 function(ev) {
140                                                         if (ev.target.nodeName == 'textbox') {
141                                                                 try {
142                                                                         var flag = false;
143                                                                         var darray = ev.target.value.split('-');
144                                                                         var year = darray[0]; var month = darray[1]; var day = darray[2]; 
145                                                                         if ( (!year) || (year.length != 4) || (!parseInt(year)) ) flag = true;
146                                                                         if ( (!month) || (month.length !=2) || (!parseInt(month)) ) flag = true;
147                                                                         if ( (!day) || (day.length !=2) || (!parseInt(day)) ) flag = true;
148                                                                         if (flag) {
149                                                                                 throw('invalid date format');
150                                                                         }
151                                                                         var d = new Date( year, month - 1, day );
152                                                                         if (d.toString() == 'Invalid Date') throw('Invalid Date');
153                                                                         if ( d > new Date() ) throw('Future Date');
154                                                                         ev.target.value = util.date.formatted_date(d,'%F');
155                                                                         var x = document.getElementById('background');
156                                                                         if (x) {
157                                                                                 if ( ev.target.value == util.date.formatted_date(new Date(),'%F') ) {
158                                                                                         x.setAttribute('style','background-color: green');
159                                                                                 } else {
160                                                                                         x.setAttribute('style','background-color: red');
161                                                                                 }
162                                                                         }
163
164                                                                 } catch(E) {
165                                                                         dump('checkin:effective_date: ' + E + '\n');
166                                                                         alert('Problem setting backdate: ' + E);
167                                                                         ev.target.value = util.date.formatted_date(new Date(),'%F');
168                                                                 }
169                                                         }
170                                                 }
171                                         ],
172                                         'cmd_broken' : [
173                                                 ['command'],
174                                                 function() { alert('Not Yet Implemented'); }
175                                         ],
176                                         'cmd_checkin_submit_barcode' : [
177                                                 ['command'],
178                                                 function() {
179                                                         obj.checkin();
180                                                 }
181                                         ],
182                                         'cmd_checkin_print' : [
183                                                 ['command'],
184                                                 function() {
185                                                         obj.list.on_all_fleshed = function() {
186                                                                 try {
187                                                                         dump( js2JSON( obj.list.dump() ) + '\n' );
188                                                                         obj.OpenILS.data.stash_retrieve();
189                                                                         var lib = obj.OpenILS.data.hash.aou[ obj.OpenILS.data.list.au[0].ws_ou() ];
190                                                                         lib.children(null);
191                                                                         var p = { 
192                                                                                 'lib' : lib,
193                                                                                 'staff' : obj.OpenILS.data.list.au[0],
194                                                                                 'header' : obj.OpenILS.data.print_list_templates.checkin.header,
195                                                                                 'line_item' : obj.OpenILS.data.print_list_templates.checkin.line_item,
196                                                                                 'footer' : obj.OpenILS.data.print_list_templates.checkin.footer,
197                                                                                 'type' : obj.OpenILS.data.print_list_templates.checkin.type,
198                                                                                 'list' : obj.list.dump(),
199                                                                         };
200                                                                         JSAN.use('util.print'); var print = new util.print();
201                                                                         print.tree_list( p );
202                                                                         setTimeout(function(){obj.list.on_all_fleshed = null;},0);
203                                                                 } catch(E) {
204                                                                         alert(E); 
205                                                                 }
206                                                         }
207                                                         obj.list.full_retrieve();
208                                                 }
209                                         ],
210                                         'cmd_checkin_reprint' : [
211                                                 ['command'],
212                                                 function() {
213                                                         JSAN.use('util.print'); var print = new util.print();
214                                                         print.reprint_last();
215                                                 }
216                                         ],
217                                         'cmd_checkin_done' : [
218                                                 ['command'],
219                                                 function() {
220                                                 }
221                                         ],
222                                 }
223                         }
224                 );
225                 this.controller.render();
226                 this.controller.view.checkin_barcode_entry_textbox.focus();
227
228         },
229
230         'checkin' : function() {
231                 var obj = this;
232                 try {
233                         var barcode = obj.controller.view.checkin_barcode_entry_textbox.value;
234                         if (!barcode) return;
235                         var backdate = obj.controller.view.checkin_effective_date_textbox.value;
236                         var auto_print = document.getElementById('checkin_auto');
237                         if (auto_print) auto_print = auto_print.checked;
238                         JSAN.use('circ.util');
239                         var checkin = circ.util.checkin_via_barcode(
240                                 ses(), barcode, backdate, auto_print
241                         );
242                         if (!checkin) return obj.on_failure(); /* circ.util.checkin handles errors and returns null currently */
243                         if (checkin.ilsevent == 7010 /* COPY_ALERT_MESSAGE */
244                                 || checkin.ilsevent == 1203 /* COPY_BAD_STATUS */
245                                 || checkin.ilsevent == -1 /* offline */
246                                 || checkin.ilsevent == 1502 /* ASSET_COPY_NOT_FOUND */
247                                 || checkin.ilsevent == 1203 /* COPY_BAD_STATUS */
248                                 || checkin.ilsevent == 7009 /* CIRC_CLAIMS_RETURNED */ 
249                                 || checkin.ilsevent == 7011 /* COPY_STATUS_LOST */ 
250                                 || checkin.ilsevent == 7012 /* COPY_STATUS_MISSING */) return obj.on_failure();
251                         var retrieve_id = js2JSON( { 'copy_id' : checkin.copy.id(), 'barcode' : checkin.copy.barcode(), 'doc_id' : (typeof checkin.record != 'undefined' ? ( typeof checkin.record.ilsevent == 'undefined' ? checkin.record.doc_id() : null ) : null ) } );
252                         obj.list.append(
253                                 {
254                                         'retrieve_id' : retrieve_id,
255                                         'row' : {
256                                                 'my' : {
257                                                         'circ' : checkin.circ,
258                                                         'mvr' : checkin.record,
259                                                         'acp' : checkin.copy,
260                                                         'status' : checkin.status,
261                                                         'route_to' : checkin.route_to,
262                                                         'message' : checkin.message,
263                                                 }
264                                         }
265                                 //I could override map_row_to_column here
266                                 }
267                         );
268
269                         JSAN.use('util.sound'); var sound = new util.sound(); sound.circ_good();
270
271                         if (typeof obj.on_checkin == 'function') {
272                                 obj.on_checkin(checkin);
273                         }
274                         if (typeof window.xulG == 'object' && typeof window.xulG.on_checkin == 'function') {
275                                 obj.error.sdump('D_CIRC','circ.checkin: Calling external .on_checkin()\n');
276                                 window.xulG.on_checkin(checkin);
277                         } else {
278                                 obj.error.sdump('D_CIRC','circ.checkin: No external .on_checkin()\n');
279                         }
280
281                 } catch(E) {
282                         obj.error.standard_unexpected_error_alert('Something went wrong in circ.checkin.checkin: ',E);
283                         if (typeof obj.on_failure == 'function') {
284                                 obj.on_failure(E);
285                         }
286                         if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
287                                 obj.error.sdump('D_CIRC','circ.checkin: Calling external .on_failure()\n');
288                                 window.xulG.on_failure(E);
289                         } else {
290                                 obj.error.sdump('D_CIRC','circ.checkin: No external .on_failure()\n');
291                         }
292                 }
293
294         },
295
296         'on_checkin' : function() {
297                 this.controller.view.checkin_barcode_entry_textbox.value = '';
298                 this.controller.view.checkin_barcode_entry_textbox.focus();
299         },
300
301         'on_failure' : function() {
302                 this.controller.view.checkin_barcode_entry_textbox.select();
303                 this.controller.view.checkin_barcode_entry_textbox.focus();
304         },
305         
306         'spawn_copy_editor' : function() {
307
308                 /* FIXME -  a lot of redundant calls here */
309
310                 var obj = this;
311
312                 JSAN.use('util.widgets'); JSAN.use('util.functional');
313
314                 var list = obj.selection_list;
315
316                 list = util.functional.map_list(
317                         list,
318                         function (o) {
319                                 return o.copy_id;
320                         }
321                 );
322
323                 var copies = util.functional.map_list(
324                         list,
325                         function (acp_id) {
326                                 return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
327                         }
328                 );
329
330                 var edit = 0;
331                 try {
332                         edit = obj.network.request(
333                                 api.PERM_MULTI_ORG_CHECK.app,
334                                 api.PERM_MULTI_ORG_CHECK.method,
335                                 [ 
336                                         ses(), 
337                                         obj.data.list.au[0].id(), 
338                                         util.functional.map_list(
339                                                 copies,
340                                                 function (o) {
341                                                         return obj.network.simple_request('FM_ACN_RETRIEVE',[o.call_number()]).owning_lib();
342                                                 }
343                                         ),
344                                         [ 'UPDATE_COPY', 'UPDATE_BATCH_COPY' ]
345                                 ]
346                         ).length == 0 ? 1 : 0;
347                 } catch(E) {
348                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
349                 }
350
351                 JSAN.use('cat.util'); cat.util.spawn_copy_editor(list,edit);
352
353         },
354
355 }
356
357 dump('exiting circ.checkin.js\n');