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