]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/circ/checkin.js
export csv from list to clipboard
[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_export' : [
246                                                 ['command'],
247                                                 function() {
248                                                         obj.list.on_all_fleshed = function() {
249                                                                 try {
250                                                                         dump(obj.list.dump_csv() + '\n');
251                                                                         copy_to_clipboard(obj.list.dump_csv());
252                                                                         setTimeout(function(){obj.list.on_all_fleshed = null;},0);
253                                                                 } catch(E) {
254                                                                         alert(E); 
255                                                                 }
256                                                         }
257                                                         obj.list.full_retrieve();
258                                                 }
259                                         ],
260
261                                         'cmd_checkin_reprint' : [
262                                                 ['command'],
263                                                 function() {
264                                                         JSAN.use('util.print'); var print = new util.print();
265                                                         print.reprint_last();
266                                                 }
267                                         ],
268                                         'cmd_checkin_done' : [
269                                                 ['command'],
270                                                 function() {
271                                                 }
272                                         ],
273                                 }
274                         }
275                 );
276                 this.controller.render();
277                 this.controller.view.checkin_barcode_entry_textbox.focus();
278
279         },
280
281         'test_barcode' : function(bc) {
282                 var obj = this;
283                 var good = util.barcode.check(bc);
284                 if (good) {
285                         return true;
286                 } else {
287                         if ( 1 == obj.error.yns_alert(
288                                                 'Bad checkdigit; possible mis-scan.  Use this barcode ("' + bc + '") anyway?',
289                                                 'Bad Barcode',
290                                                 'Cancel',
291                                                 'Accept Barcode',
292                                                 null,
293                                                 'Check here to confirm this action',
294                                                 '/xul/server/skin/media/images/bad_barcode.png'
295                         ) ) {
296                                 return true;
297                         } else {
298                                 return false;
299                         }
300                 }
301         },
302
303         'checkin' : function() {
304                 var obj = this;
305                 try {
306                         var barcode = obj.controller.view.checkin_barcode_entry_textbox.value;
307                         if (!barcode) return;
308                         if (barcode) {
309                                 if ( obj.test_barcode(barcode) ) { /* good */ } else { /* bad */ return; }
310                         }
311                         var backdate = obj.controller.view.checkin_effective_date_textbox.value;
312                         var auto_print = document.getElementById('checkin_auto');
313                         if (auto_print) auto_print = auto_print.checked;
314                         JSAN.use('circ.util');
315                         circ.util.checkin_via_barcode(
316                                 ses(), { 'barcode' : barcode }, backdate, auto_print, {
317                                         'disable_textbox' : function() { 
318                                                 obj.controller.view.checkin_barcode_entry_textbox.disabled = true; 
319                                                 obj.controller.view.cmd_checkin_submit_barcode.setAttribute('disabled', 'true'); 
320                                         },
321                                         'enable_textbox' : function() { 
322                                                 obj.controller.view.checkin_barcode_entry_textbox.disabled = false; 
323                                                 obj.controller.view.cmd_checkin_submit_barcode.setAttribute('disabled', 'false'); 
324                                         },
325                                         'checkin_result' : function(checkin) {
326                                                 obj.controller.view.checkin_barcode_entry_textbox.disabled = false;
327                                                 obj.controller.view.cmd_checkin_submit_barcode.setAttribute('disabled', 'false'); 
328                                                 obj.checkin2(checkin,backdate);
329                                         }
330                                 }
331                         );
332                 } catch(E) {
333                         obj.error.standard_unexpected_error_alert('Something went wrong in circ.util.checkin: ',E);
334                         if (typeof obj.on_failure == 'function') {
335                                 obj.on_failure(E);
336                         }
337                         if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
338                                 obj.error.sdump('D_CIRC','circ.util.checkin: Calling external .on_failure()\n');
339                                 window.xulG.on_failure(E);
340                         } else {
341                                 obj.error.sdump('D_CIRC','circ.util.checkin: No external .on_failure()\n');
342                         }
343                 }
344
345         },
346
347         'checkin2' : function(checkin,backdate) {
348                 var obj = this;
349                 try {
350                         if (!checkin) return obj.on_failure(); /* circ.util.checkin handles errors and returns null currently */
351                         if (checkin.ilsevent == 7010 /* COPY_ALERT_MESSAGE */
352                                 || checkin.ilsevent == 1203 /* COPY_BAD_STATUS */
353                                 || checkin.ilsevent == -1 /* offline */
354                                 || checkin.ilsevent == 1502 /* ASSET_COPY_NOT_FOUND */
355                                 || checkin.ilsevent == 1203 /* COPY_BAD_STATUS */
356                                 || checkin.ilsevent == 7009 /* CIRC_CLAIMS_RETURNED */ 
357                                 || checkin.ilsevent == 7011 /* COPY_STATUS_LOST */ 
358                                 || checkin.ilsevent == 7012 /* COPY_STATUS_MISSING */) return obj.on_failure();
359                         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 ) } );
360                         if (checkin.circ && checkin.circ.checkin_time() == 'now') checkin.circ.checkin_time(backdate);
361                         if (document.getElementById('trim_list')) {
362                                 var x = document.getElementById('trim_list');
363                                 if (x.checked) { obj.list.trim_list = 20; } else { obj.list.trim_list = null; }
364                         }
365                         obj.list.append(
366                                 {
367                                         'retrieve_id' : retrieve_id,
368                                         'row' : {
369                                                 'my' : {
370                                                         'circ' : checkin.circ,
371                                                         'mvr' : checkin.record,
372                                                         'acp' : checkin.copy,
373                                                         'status' : checkin.status,
374                                                         'route_to' : checkin.route_to,
375                                                         'message' : checkin.message,
376                                                 }
377                                         },
378                                         'to_top' : true,
379                                 //I could override map_row_to_column here
380                                 }
381                         );
382
383                         JSAN.use('util.sound'); var sound = new util.sound(); sound.circ_good();
384
385                         if (typeof obj.on_checkin == 'function') {
386                                 obj.on_checkin(checkin);
387                         }
388                         if (typeof window.xulG == 'object' && typeof window.xulG.on_checkin == 'function') {
389                                 obj.error.sdump('D_CIRC','circ.checkin: Calling external .on_checkin()\n');
390                                 window.xulG.on_checkin(checkin);
391                         } else {
392                                 obj.error.sdump('D_CIRC','circ.checkin: No external .on_checkin()\n');
393                         }
394
395                         return true;
396
397                 } catch(E) {
398                         obj.error.standard_unexpected_error_alert('Something went wrong in circ.util.checkin2: ',E);
399                         if (typeof obj.on_failure == 'function') {
400                                 obj.on_failure(E);
401                         }
402                         if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
403                                 obj.error.sdump('D_CIRC','circ.util.checkin2: Calling external .on_failure()\n');
404                                 window.xulG.on_failure(E);
405                         } else {
406                                 obj.error.sdump('D_CIRC','circ.util.checkin2: No external .on_failure()\n');
407                         }
408                 }
409
410         },
411
412         'on_checkin' : function() {
413                 this.controller.view.checkin_barcode_entry_textbox.disabled = false;
414                 this.controller.view.checkin_barcode_entry_textbox.select();
415                 this.controller.view.checkin_barcode_entry_textbox.value = '';
416                 this.controller.view.checkin_barcode_entry_textbox.focus();
417         },
418
419         'on_failure' : function() {
420                 this.controller.view.checkin_barcode_entry_textbox.disabled = false;
421                 this.controller.view.checkin_barcode_entry_textbox.select();
422                 this.controller.view.checkin_barcode_entry_textbox.focus();
423         },
424         
425         'spawn_copy_editor' : function() {
426
427                 /* FIXME -  a lot of redundant calls here */
428
429                 var obj = this;
430
431                 JSAN.use('util.widgets'); JSAN.use('util.functional');
432
433                 var list = obj.selection_list;
434
435                 list = util.functional.map_list(
436                         list,
437                         function (o) {
438                                 return o.copy_id;
439                         }
440                 );
441
442                 var copies = util.functional.map_list(
443                         list,
444                         function (acp_id) {
445                                 return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
446                         }
447                 );
448
449                 var edit = 0;
450                 try {
451                         edit = obj.network.request(
452                                 api.PERM_MULTI_ORG_CHECK.app,
453                                 api.PERM_MULTI_ORG_CHECK.method,
454                                 [ 
455                                         ses(), 
456                                         obj.data.list.au[0].id(), 
457                                         util.functional.map_list(
458                                                 copies,
459                                                 function (o) {
460                                                         return o.call_number() == -1 ? o.circ_lib() : obj.network.simple_request('FM_ACN_RETRIEVE',[o.call_number()]).owning_lib();
461                                                 }
462                                         ),
463                                         copies.length == 1 ? [ 'UPDATE_COPY' ] : [ 'UPDATE_COPY', 'UPDATE_BATCH_COPY' ]
464                                 ]
465                         ).length == 0 ? 1 : 0;
466                 } catch(E) {
467                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
468                 }
469
470                 JSAN.use('cat.util'); cat.util.spawn_copy_editor(list,edit);
471
472         },
473
474 }
475
476 dump('exiting circ.checkin.js\n');