]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/circ/checkin.js
proper timezone handling for checkin backdate
[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
8         var js = JSAN._loadJSFromUrl( urls.isodate_lib );
9         try { eval( js ); } catch(E) { this.error.standard_unexpected_error_alert('Problem loading ISO8601 date extension',E); }
10
11         JSAN.use('util.network'); this.network = new util.network();
12         JSAN.use('util.barcode');
13         JSAN.use('util.date');
14         this.OpenILS = {}; JSAN.use('OpenILS.data'); this.OpenILS.data = new OpenILS.data(); this.OpenILS.data.init({'via':'stash'});
15         this.data = this.OpenILS.data;
16 }
17
18 circ.checkin.prototype = {
19
20         'selection_list' : [],
21
22         'init' : function( params ) {
23
24                 var obj = this;
25
26                 JSAN.use('circ.util');
27                 var columns = circ.util.columns( 
28                         { 
29                                 'barcode' : { 'hidden' : false },
30                                 'title' : { 'hidden' : false },
31                                 'location' : { 'hidden' : false },
32                                 'call_number' : { 'hidden' : false },
33                                 'status' : { 'hidden' : false },
34                                 'route_to' : { 'hidden' : false },
35                                 'alert_message' : { 'hidden' : false },
36                                 'checkin_time' : { 'hidden' : false },
37                         },
38                         {
39                                 'except_these' : [ 'uses', 'checkin_time_full' ],
40                         }
41                 );
42
43                 JSAN.use('util.list'); obj.list = new util.list('checkin_list');
44                 obj.list.init(
45                         {
46                                 'columns' : columns,
47                                 'map_row_to_columns' : circ.util.std_map_row_to_columns(),
48                                 'on_select' : function(ev) {
49                                         try {
50                                                 JSAN.use('util.functional');
51                                                 var sel = obj.list.retrieve_selection();
52                                                 obj.selection_list = util.functional.map_list(
53                                                         sel,
54                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
55                                                 );
56                                                 obj.error.sdump('D_TRACE', 'circ/copy_status: selection list = ' + js2JSON(obj.selection_list) );
57                                                 if (obj.selection_list.length == 0) {
58                                                         obj.controller.view.sel_edit.setAttribute('disabled','true');
59                                                         obj.controller.view.sel_opac.setAttribute('disabled','true');
60                                                         obj.controller.view.sel_patron.setAttribute('disabled','true');
61                                                         obj.controller.view.sel_last_patron.setAttribute('disabled','true');
62                                                         obj.controller.view.sel_copy_details.setAttribute('disabled','true');
63                                                         obj.controller.view.sel_bucket.setAttribute('disabled','true');
64                                                         obj.controller.view.sel_spine.setAttribute('disabled','true');
65                                                         obj.controller.view.sel_transit_abort.setAttribute('disabled','true');
66                                                         obj.controller.view.sel_clip.setAttribute('disabled','true');
67                                                         obj.controller.view.sel_mark_items_damaged.setAttribute('disabled','true');
68                                                 } else {
69                                                         obj.controller.view.sel_edit.setAttribute('disabled','false');
70                                                         obj.controller.view.sel_opac.setAttribute('disabled','false');
71                                                         obj.controller.view.sel_patron.setAttribute('disabled','false');
72                                                         obj.controller.view.sel_last_patron.setAttribute('disabled','false');
73                                                         obj.controller.view.sel_copy_details.setAttribute('disabled','false');
74                                                         obj.controller.view.sel_bucket.setAttribute('disabled','false');
75                                                         obj.controller.view.sel_spine.setAttribute('disabled','false');
76                                                         obj.controller.view.sel_transit_abort.setAttribute('disabled','false');
77                                                         obj.controller.view.sel_clip.setAttribute('disabled','false');
78                                                         obj.controller.view.sel_mark_items_damaged.setAttribute('disabled','false');
79                                                 }
80                                         } catch(E) {
81                                                 alert('FIXME: ' + E);
82                                         }
83                                 },
84
85                         }
86                 );
87                 
88                 JSAN.use('util.controller'); obj.controller = new util.controller();
89                 obj.controller.init(
90                         {
91                                 'control_map' : {
92                                         'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
93                                         'sel_clip' : [
94                                                 ['command'],
95                                                 function() { obj.list.clipboard(); }
96                                         ],
97                                         'sel_edit' : [
98                                                 ['command'],
99                                                 function() {
100                                                         try {
101                                                                 obj.spawn_copy_editor();
102                                                         } catch(E) {
103                                                                 alert(E);
104                                                         }
105                                                 }
106                                         ],
107                                         'sel_spine' : [
108                                                 ['command'],
109                                                 function() {
110                                                         JSAN.use('cat.util');
111                                                         cat.util.spawn_spine_editor(obj.selection_list);
112                                                 }
113                                         ],
114                                         'sel_opac' : [
115                                                 ['command'],
116                                                 function() {
117                                                         JSAN.use('cat.util');
118                                                         cat.util.show_in_opac(obj.selection_list);
119                                                 }
120                                         ],
121                                         'sel_transit_abort' : [
122                                                 ['command'],
123                                                 function() {
124                                                         JSAN.use('circ.util');
125                                                         circ.util.abort_transits(obj.selection_list);
126                                                 }
127                                         ],
128                                         'sel_patron' : [
129                                                 ['command'],
130                                                 function() {
131                                                         JSAN.use('circ.util');
132                                                         circ.util.show_last_few_circs(obj.selection_list);
133                                                 }
134                                         ],
135                                         'sel_last_patron' : [
136                                                 ['command'],
137                                                 function() {
138                                                         var patrons = {};
139                                                         for (var i = 0; i < obj.selection_list.length; i++) {
140                                                                 var circs = obj.network.simple_request('FM_CIRC_RETRIEVE_VIA_COPY',[ses(),obj.selection_list[i].copy_id,1]);
141                                                                 if (circs.length > 0) {
142                                                                         patrons[circs[0].usr()] = 1;
143                                                                 } else {
144                                                                         alert('Item ' + obj.selection_list[i].barcode + ' has never circulated');
145                                                                 }
146                                                         }
147                                                         for (var i in patrons) {
148                                                                 xulG.new_tab(urls.XUL_PATRON_DISPLAY,{},{'id' : i});
149                                                         }
150                                                 }
151                                         ],
152                                         'sel_copy_details' : [
153                                                 ['command'],
154                                                 function() {
155                                                         JSAN.use('circ.util');
156                                                         for (var i = 0; i < obj.selection_list.length; i++) {
157                                                                 circ.util.show_copy_details( obj.selection_list[i].copy_id );
158                                                         }
159                                                 }
160                                         ],
161                                         'sel_mark_items_damaged' : [
162                                                 ['command'],
163                                                 function() {
164                                                         var funcs = [];
165                                                         JSAN.use('cat.util'); JSAN.use('util.functional');
166                                                         cat.util.mark_item_damaged( util.functional.map_list( obj.selection_list, function(o) { return o.copy_id; } ) );
167                                                 }
168                                         ],
169                                         'sel_bucket' : [
170                                                 ['command'],
171                                                 function() {
172                                                         JSAN.use('cat.util');
173                                                         cat.util.add_copies_to_bucket(obj.selection_list);
174                                                 }
175                                         ],
176                                         'checkin_barcode_entry_textbox' : [
177                                                 ['keypress'],
178                                                 function(ev) {
179                                                         if (ev.keyCode && ev.keyCode == 13) {
180                                                                 obj.checkin();
181                                                         }
182                                                 }
183                                         ],
184                                         'checkin_effective_date_label' : [
185                                                 ['render'],
186                                                 function(e) {
187                                                         return function() {
188                                                                 obj.controller.view.checkin_effective_date_textbox.value =
189                                                                         util.date.formatted_date(new Date(),'%F');
190                                                         };
191                                                 }
192                                         ],
193                                         'checkin_effective_date_textbox' : [
194                                                 ['change'],
195                                                 function(ev) {
196                                                         if (ev.target.nodeName == 'textbox') {
197                                                                 try {
198                                                                         var flag = false;
199                                                                         var darray = ev.target.value.split('-');
200                                                                         var year = darray[0]; var month = darray[1]; var day = darray[2]; 
201                                                                         if ( (!year) || (year.length != 4) || (!Number(year)) ) flag = true;
202                                                                         if ( (!month) || (month.length !=2) || (!Number(month)) ) flag = true;
203                                                                         if ( (!day) || (day.length !=2) || (!Number(day)) ) flag = true;
204                                                                         if (flag) {
205                                                                                 throw('invalid date format');
206                                                                         }
207                                                                         var d = new Date( year, month - 1, day );
208                                                                         if (d.toString() == 'Invalid Date') throw('Invalid Date');
209                                                                         if ( d > new Date() ) throw('Future Date');
210                                                                         ev.target.value = util.date.formatted_date(d,'%F');
211                                                                         var x = document.getElementById('background');
212                                                                         if (x) {
213                                                                                 if ( ev.target.value == util.date.formatted_date(new Date(),'%F') ) {
214                                                                                         x.setAttribute('style','background-color: green');
215                                                                                         document.getElementById('background-text').setAttribute('value','Check In / Process Item');
216                                                                                 } else {
217                                                                                         x.setAttribute('style','background-color: red');
218                                                                                         document.getElementById('background-text').setAttribute('value','Backdated ' + ev.target.value + ' Check In');
219                                                                                 }
220                                                                         }
221
222                                                                 } catch(E) {
223                                                                         dump('checkin:effective_date: ' + E + '\n');
224                                                                         alert('Problem setting backdate: ' + E);
225                                                                         ev.target.value = util.date.formatted_date(new Date(),'%F');
226                                                                 }
227                                                         }
228                                                 }
229                                         ],
230                                         'cmd_broken' : [
231                                                 ['command'],
232                                                 function() { alert('Not Yet Implemented'); }
233                                         ],
234                                         'cmd_checkin_submit_barcode' : [
235                                                 ['command'],
236                                                 function() {
237                                                         obj.checkin();
238                                                 }
239                                         ],
240                                         'cmd_checkin_print' : [
241                                                 ['command'],
242                                                 function() {
243                                                         obj.list.on_all_fleshed = function() {
244                                                                 try {
245                                                                         dump( js2JSON( obj.list.dump_with_keys() ) + '\n' );
246                                                                         obj.OpenILS.data.stash_retrieve();
247                                                                         var lib = obj.OpenILS.data.hash.aou[ obj.OpenILS.data.list.au[0].ws_ou() ];
248                                                                         lib.children(null);
249                                                                         var p = { 
250                                                                                 'lib' : lib,
251                                                                                 'staff' : obj.OpenILS.data.list.au[0],
252                                                                                 'header' : obj.OpenILS.data.print_list_templates.checkin.header,
253                                                                                 'line_item' : obj.OpenILS.data.print_list_templates.checkin.line_item,
254                                                                                 'footer' : obj.OpenILS.data.print_list_templates.checkin.footer,
255                                                                                 'type' : obj.OpenILS.data.print_list_templates.checkin.type,
256                                                                                 'list' : obj.list.dump_with_keys(),
257                                                                         };
258                                                                         JSAN.use('util.print'); var print = new util.print();
259                                                                         print.tree_list( p );
260                                                                         setTimeout(function(){obj.list.on_all_fleshed = null;},0);
261                                                                 } catch(E) {
262                                                                         alert(E); 
263                                                                 }
264                                                         }
265                                                         obj.list.full_retrieve();
266                                                 }
267                                         ],
268                                         'cmd_checkin_export' : [
269                                                 ['command'],
270                                                 function() {
271                                                         obj.list.on_all_fleshed = function() {
272                                                                 try {
273                                                                         dump(obj.list.dump_csv() + '\n');
274                                                                         copy_to_clipboard(obj.list.dump_csv());
275                                                                         setTimeout(function(){obj.list.on_all_fleshed = null;},0);
276                                                                 } catch(E) {
277                                                                         alert(E); 
278                                                                 }
279                                                         }
280                                                         obj.list.full_retrieve();
281                                                 }
282                                         ],
283
284                                         'cmd_checkin_reprint' : [
285                                                 ['command'],
286                                                 function() {
287                                                         JSAN.use('util.print'); var print = new util.print();
288                                                         print.reprint_last();
289                                                 }
290                                         ],
291                                         'cmd_checkin_done' : [
292                                                 ['command'],
293                                                 function() {
294                                                 }
295                                         ],
296                                 }
297                         }
298                 );
299                 this.controller.render();
300                 this.controller.view.checkin_barcode_entry_textbox.focus();
301
302         },
303
304         'test_barcode' : function(bc) {
305                 var obj = this;
306                 var x = document.getElementById('strict_barcode');
307                 if (x && x.checked != true) return true;
308                 var good = util.barcode.check(bc);
309                 if (good) {
310                         return true;
311                 } else {
312                         if ( 1 == obj.error.yns_alert(
313                                                 'Bad checkdigit; possible mis-scan.  Use this barcode ("' + bc + '") anyway?',
314                                                 'Bad Barcode',
315                                                 'Cancel',
316                                                 'Accept Barcode',
317                                                 null,
318                                                 'Check here to confirm this action',
319                                                 '/xul/server/skin/media/images/bad_barcode.png'
320                         ) ) {
321                                 return true;
322                         } else {
323                                 return false;
324                         }
325                 }
326         },
327
328         'checkin' : function() {
329                 var obj = this;
330                 try {
331                         var barcode = obj.controller.view.checkin_barcode_entry_textbox.value;
332                         if (!barcode) return;
333                         if (barcode) {
334                                 if ( obj.test_barcode(barcode) ) { /* good */ } else { /* bad */ return; }
335                         }
336                         var backdate = obj.controller.view.checkin_effective_date_textbox.value;
337                         var auto_print = document.getElementById('checkin_auto');
338                         if (auto_print) auto_print = auto_print.checked;
339                         JSAN.use('circ.util');
340                         circ.util.checkin_via_barcode(
341                                 ses(), { 'barcode' : barcode }, backdate, auto_print, {
342                                         'disable_textbox' : function() { 
343                                                 obj.controller.view.checkin_barcode_entry_textbox.disabled = true; 
344                                                 obj.controller.view.cmd_checkin_submit_barcode.setAttribute('disabled', 'true'); 
345                                         },
346                                         'enable_textbox' : function() { 
347                                                 obj.controller.view.checkin_barcode_entry_textbox.disabled = false; 
348                                                 obj.controller.view.cmd_checkin_submit_barcode.setAttribute('disabled', 'false'); 
349                                         },
350                                         'checkin_result' : function(checkin) {
351                                                 obj.controller.view.checkin_barcode_entry_textbox.disabled = false;
352                                                 obj.controller.view.cmd_checkin_submit_barcode.setAttribute('disabled', 'false'); 
353                                                 obj.checkin2(checkin,backdate);
354                                         }
355                                 }
356                         );
357                 } catch(E) {
358                         obj.error.standard_unexpected_error_alert('Something went wrong in circ.util.checkin: ',E);
359                         if (typeof obj.on_failure == 'function') {
360                                 obj.on_failure(E);
361                         }
362                         if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
363                                 obj.error.sdump('D_CIRC','circ.util.checkin: Calling external .on_failure()\n');
364                                 window.xulG.on_failure(E);
365                         } else {
366                                 obj.error.sdump('D_CIRC','circ.util.checkin: No external .on_failure()\n');
367                         }
368                 }
369
370         },
371
372         'checkin2' : function(checkin,backdate) {
373                 var obj = this;
374                 try {
375                         if (!checkin) return obj.on_failure(); /* circ.util.checkin handles errors and returns null currently */
376                         if (checkin.ilsevent == 7010 /* COPY_ALERT_MESSAGE */
377                                 || checkin.ilsevent == 1203 /* COPY_BAD_STATUS */
378                                 || checkin.ilsevent == -1 /* offline */
379                                 || checkin.ilsevent == 1502 /* ASSET_COPY_NOT_FOUND */
380                                 || checkin.ilsevent == 1203 /* COPY_BAD_STATUS */
381                                 || checkin.ilsevent == 7009 /* CIRC_CLAIMS_RETURNED */ 
382                                 || checkin.ilsevent == 7011 /* COPY_STATUS_LOST */ 
383                                 || checkin.ilsevent == 7012 /* COPY_STATUS_MISSING */) return obj.on_failure();
384                         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 ) } );
385                         if (checkin.circ && checkin.circ.checkin_time() == 'now') checkin.circ.checkin_time(backdate);
386                         if (document.getElementById('trim_list')) {
387                                 var x = document.getElementById('trim_list');
388                                 if (x.checked) { obj.list.trim_list = 20; } else { obj.list.trim_list = null; }
389                         }
390                         obj.list.append(
391                                 {
392                                         'retrieve_id' : retrieve_id,
393                                         'row' : {
394                                                 'my' : {
395                                                         'circ' : checkin.circ,
396                                                         'mvr' : checkin.record,
397                                                         'acp' : checkin.copy,
398                                                         'status' : checkin.status,
399                                                         'route_to' : checkin.route_to,
400                                                         'message' : checkin.message,
401                                                 }
402                                         },
403                                         'to_top' : true,
404                                 //I could override map_row_to_column here
405                                 }
406                         );
407                         obj.list.node.view.selection.select(0);
408
409                         JSAN.use('util.sound'); var sound = new util.sound(); sound.circ_good();
410
411                         if (typeof obj.on_checkin == 'function') {
412                                 obj.on_checkin(checkin);
413                         }
414                         if (typeof window.xulG == 'object' && typeof window.xulG.on_checkin == 'function') {
415                                 obj.error.sdump('D_CIRC','circ.checkin: Calling external .on_checkin()\n');
416                                 window.xulG.on_checkin(checkin);
417                         } else {
418                                 obj.error.sdump('D_CIRC','circ.checkin: No external .on_checkin()\n');
419                         }
420
421                         return true;
422
423                 } catch(E) {
424                         obj.error.standard_unexpected_error_alert('Something went wrong in circ.util.checkin2: ',E);
425                         if (typeof obj.on_failure == 'function') {
426                                 obj.on_failure(E);
427                         }
428                         if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
429                                 obj.error.sdump('D_CIRC','circ.util.checkin2: Calling external .on_failure()\n');
430                                 window.xulG.on_failure(E);
431                         } else {
432                                 obj.error.sdump('D_CIRC','circ.util.checkin2: No external .on_failure()\n');
433                         }
434                 }
435
436         },
437
438         'on_checkin' : function() {
439                 this.controller.view.checkin_barcode_entry_textbox.disabled = false;
440                 this.controller.view.checkin_barcode_entry_textbox.select();
441                 this.controller.view.checkin_barcode_entry_textbox.value = '';
442                 this.controller.view.checkin_barcode_entry_textbox.focus();
443         },
444
445         'on_failure' : function() {
446                 this.controller.view.checkin_barcode_entry_textbox.disabled = false;
447                 this.controller.view.checkin_barcode_entry_textbox.select();
448                 this.controller.view.checkin_barcode_entry_textbox.focus();
449         },
450         
451         'spawn_copy_editor' : function() {
452
453                 /* FIXME -  a lot of redundant calls here */
454
455                 var obj = this;
456
457                 JSAN.use('util.widgets'); JSAN.use('util.functional');
458
459                 var list = obj.selection_list;
460
461                 list = util.functional.map_list(
462                         list,
463                         function (o) {
464                                 return o.copy_id;
465                         }
466                 );
467
468                 var copies = util.functional.map_list(
469                         list,
470                         function (acp_id) {
471                                 return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
472                         }
473                 );
474
475                 var edit = 0;
476                 try {
477                         edit = obj.network.request(
478                                 api.PERM_MULTI_ORG_CHECK.app,
479                                 api.PERM_MULTI_ORG_CHECK.method,
480                                 [ 
481                                         ses(), 
482                                         obj.data.list.au[0].id(), 
483                                         util.functional.map_list(
484                                                 copies,
485                                                 function (o) {
486                                                         return o.call_number() == -1 ? o.circ_lib() : obj.network.simple_request('FM_ACN_RETRIEVE',[o.call_number()]).owning_lib();
487                                                 }
488                                         ),
489                                         copies.length == 1 ? [ 'UPDATE_COPY' ] : [ 'UPDATE_COPY', 'UPDATE_BATCH_COPY' ]
490                                 ]
491                         ).length == 0 ? 1 : 0;
492                 } catch(E) {
493                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
494                 }
495
496                 JSAN.use('cat.util'); cat.util.spawn_copy_editor(list,edit);
497
498         },
499
500 }
501
502 dump('exiting circ.checkin.js\n');