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