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