]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/circ/checkin.js
changing the value of a datepicker within a change event handler can fire another...
[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                 JSAN.use('util.controller'); obj.controller = new util.controller();
84                 obj.controller.init(
85                         {
86                                 'control_map' : {
87                                         'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
88                                         'sel_clip' : [
89                                                 ['command'],
90                                                 function() { 
91                             obj.list.clipboard(); 
92                             obj.controller.view.checkin_barcode_entry_textbox.focus();
93                         }
94                                         ],
95                                         'sel_edit' : [
96                                                 ['command'],
97                                                 function() {
98                                                         try {
99                                                                 obj.spawn_copy_editor();
100                                                         } catch(E) {
101                                                                 alert(E);
102                                                         }
103                                                 }
104                                         ],
105                                         'sel_spine' : [
106                                                 ['command'],
107                                                 function() {
108                                                         JSAN.use('cat.util');
109                                                         cat.util.spawn_spine_editor(obj.selection_list);
110                                                 }
111                                         ],
112                                         'sel_opac' : [
113                                                 ['command'],
114                                                 function() {
115                                                         JSAN.use('cat.util');
116                                                         cat.util.show_in_opac(obj.selection_list);
117                                                 }
118                                         ],
119                                         'sel_transit_abort' : [
120                                                 ['command'],
121                                                 function() {
122                                                         JSAN.use('circ.util');
123                                                         circ.util.abort_transits(obj.selection_list);
124                                                 }
125                                         ],
126                                         'sel_patron' : [
127                                                 ['command'],
128                                                 function() {
129                                                         JSAN.use('circ.util');
130                                                         circ.util.show_last_few_circs(obj.selection_list);
131                                                 }
132                                         ],
133                                         'sel_last_patron' : [
134                                                 ['command'],
135                                                 function() {
136                                                         var patrons = {};
137                                                         for (var i = 0; i < obj.selection_list.length; i++) {
138                                                                 var circs = obj.network.simple_request('FM_CIRC_RETRIEVE_VIA_COPY',[ses(),obj.selection_list[i].copy_id,1]);
139                                                                 if (circs.length > 0) {
140                                                                         patrons[circs[0].usr()] = 1;
141                                                                 } else {
142                                                                         alert(document.getElementById('circStrings').getFormattedString('staff.circ.item_no_circs', [obj.selection_list[i].barcode]));
143                                                                 }
144                                                         }
145                                                         for (var i in patrons) {
146                                                                 xulG.new_patron_tab({},{'id' : i});
147                                                         }
148                                                 }
149                                         ],
150                                         'sel_copy_details' : [
151                                                 ['command'],
152                                                 function() {
153                                                         JSAN.use('circ.util');
154                                                         for (var i = 0; i < obj.selection_list.length; i++) {
155                                                                 circ.util.show_copy_details( obj.selection_list[i].copy_id );
156                                                         }
157                                                 }
158                                         ],
159                                         'sel_mark_items_damaged' : [
160                                                 ['command'],
161                                                 function() {
162                                                         var funcs = [];
163                                                         JSAN.use('cat.util'); JSAN.use('util.functional');
164                                                         cat.util.mark_item_damaged( util.functional.map_list( obj.selection_list, function(o) { return o.copy_id; } ) );
165                                                 }
166                                         ],
167                                         'sel_bucket' : [
168                                                 ['command'],
169                                                 function() {
170                                                         JSAN.use('cat.util');
171                                                         cat.util.add_copies_to_bucket(obj.selection_list);
172                                                 }
173                                         ],
174                                         'checkin_barcode_entry_textbox' : [
175                                                 ['keypress'],
176                                                 function(ev) {
177                                                         if (ev.keyCode && ev.keyCode == 13) {
178                                                                 obj.checkin();
179                                                         }
180                                                 }
181                                         ],
182                                         'checkin_effective_date_label' : [
183                                                 ['render'],
184                                                 function(e) {
185                                                         return function() {
186                                                                 obj.controller.view.checkin_effective_datepicker.value =
187                                                                         util.date.formatted_date(new Date(),'%F');
188                                                         };
189                                                 }
190                                         ],
191                                         'checkin_effective_datepicker' : [
192                                                 ['change'],
193                                                 function(ev) {
194                                                         if (ev.target.nodeName == 'datepicker') {
195                                                                 try {
196                                                                         if ( ev.target.dateValue > new Date() ) throw(document.getElementById('circStrings').getString('staff.circ.future_date'));
197                                                                         var x = document.getElementById('background');
198                                                                         if (x) {
199                                                                                 if ( ev.target.value == util.date.formatted_date(new Date(),'%F') ) {
200                                                                                         x.setAttribute('style','background-color: green');
201                                                                                         document.getElementById('background-text').setAttribute('value',document.getElementById('circStrings').getString('staff.circ.process_item'));
202                                                                                 } else {
203                                                                                         x.setAttribute('style','background-color: red');
204                                                                                         document.getElementById('background-text').setAttribute('value',document.getElementById('circStrings').getFormattedString('staff.circ.backdated_checkin', [ev.target.value]));
205                                                                                 }
206                                                                         }
207
208                                                                 } catch(E) {
209                                                                         var x = document.getElementById('background');
210                                                                         if (x) {
211                                         x.setAttribute('style','background-color: green');
212                                         document.getElementById('background-text').setAttribute('value',document.getElementById('circStrings').getString('staff.circ.process_item'));
213                                     }
214                                                                         dump('checkin:effective_date: ' + E + '\n');
215                                     ev.target.disabled = true;
216                                                                         //alert(document.getElementById('circStrings').getFormattedString('staff.circ.backdate.exception', [E]));
217                                     ev.target.value = util.date.formatted_date(new Date(),'%F');
218                                     ev.target.disabled = false;
219                                                 JSAN.use('util.sound'); var sound = new util.sound(); sound.bad();
220                                     
221                                                                 }
222                                                         }
223                                                 }
224                                         ],
225                                         'cmd_broken' : [
226                                                 ['command'],
227                                                 function() { alert(document.getElementById('circStrings').getString('staff.circ.unimplemented')); }
228                                         ],
229                                         'cmd_checkin_submit_barcode' : [
230                                                 ['command'],
231                                                 function() {
232                                                         obj.checkin();
233                                                 }
234                                         ],
235                                         'cmd_checkin_print' : [
236                                                 ['command'],
237                                                 function() {
238                                                         var p = { 
239                                                                 'template' : 'checkin'
240                                                         };
241                                                         obj.list.print(p);
242                                                 }
243                                         ],
244                                         'cmd_csv_to_clipboard' : [ ['command'], function() { 
245                         obj.list.dump_csv_to_clipboard(); 
246                         obj.controller.view.checkin_barcode_entry_textbox.focus();
247                     } ],
248                                         'cmd_csv_to_printer' : [ ['command'], function() { 
249                         obj.list.dump_csv_to_printer(); 
250                         obj.controller.view.checkin_barcode_entry_textbox.focus();
251                     } ],
252                                         'cmd_csv_to_file' : [ ['command'], function() { 
253                         obj.list.dump_csv_to_file( { 'defaultFileName' : 'checked_in.txt' } ); 
254                         obj.controller.view.checkin_barcode_entry_textbox.focus();
255                     } ]
256                                 }
257                         }
258                 );
259                 this.controller.render();
260                 this.controller.view.checkin_barcode_entry_textbox.focus();
261
262         },
263
264         'test_barcode' : function(bc) {
265                 var obj = this;
266                 var x = document.getElementById('strict_barcode');
267                 if (x && x.checked != true) return true;
268                 var good = util.barcode.check(bc);
269                 if (good) {
270                         return true;
271                 } else {
272                         if ( 1 == obj.error.yns_alert(
273                                                 document.getElementById('circStrings').getFormattedString('staff.circ.check_digit.bad', [bc]),
274                                                 document.getElementById('circStrings').getString('staff.circ.barcode.bad'),
275                                                 document.getElementById('circStrings').getString('staff.circ.cancel'),
276                                                 document.getElementById('circStrings').getString('staff.circ.barcode.accept'),
277                                                 null,
278                                                 document.getElementById('circStrings').getString('staff.circ.confirm'),
279                                                 '/xul/server/skin/media/images/bad_barcode.png'
280                         ) ) {
281                                 return true;
282                         } else {
283                                 return false;
284                         }
285                 }
286         },
287
288         'checkin' : function() {
289                 var obj = this;
290                 try {
291                         var barcode = obj.controller.view.checkin_barcode_entry_textbox.value;
292                         if (!barcode) return;
293                         if (barcode) {
294                                 if ( obj.test_barcode(barcode) ) { /* good */ } else { /* bad */ return; }
295                         }
296                         var backdate = obj.controller.view.checkin_effective_datepicker.value;
297                         var auto_print = document.getElementById('checkin_auto');
298                         if (auto_print) auto_print = auto_print.checked;
299                         JSAN.use('circ.util');
300                         circ.util.checkin_via_barcode(
301                                 ses(), 
302                 { 
303                     'barcode' : barcode,
304                                         'disable_textbox' : function() { 
305                                                 obj.controller.view.checkin_barcode_entry_textbox.disabled = true; 
306                                                 obj.controller.view.cmd_checkin_submit_barcode.setAttribute('disabled', 'true'); 
307                                         },
308                                         'enable_textbox' : function() { 
309                                                 obj.controller.view.checkin_barcode_entry_textbox.disabled = false; 
310                                                 obj.controller.view.cmd_checkin_submit_barcode.setAttribute('disabled', 'false'); 
311                                         },
312                                         'checkin_result' : function(checkin) {
313                                                 obj.controller.view.checkin_barcode_entry_textbox.disabled = false;
314                                                 obj.controller.view.cmd_checkin_submit_barcode.setAttribute('disabled', 'false'); 
315                                                 obj.checkin2(checkin,backdate);
316                                         }
317                 }, 
318                 backdate, 
319                 auto_print
320                         );
321                 } catch(E) {
322                         obj.error.standard_unexpected_error_alert(document.getElementById('circStrings').getFormattedString('staff.circ.checkin.exception', [E]));
323                         if (typeof obj.on_failure == 'function') {
324                                 obj.on_failure(E);
325                         }
326                         if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
327                                 obj.error.sdump('D_CIRC', document.getElementById('circStrings').getString('staff.circ.util.checkin.exception.external') + '\n');
328                                 window.xulG.on_failure(E);
329                         } else {
330                                 obj.error.sdump('D_CIRC', document.getElementById('circStrings').getString('staff.circ.util.checkin.exception.no_external') + '\n');
331                         }
332                 }
333
334         },
335
336         'checkin2' : function(checkin,backdate) {
337                 var obj = this;
338                 try {
339                         if (!checkin) return obj.on_failure(); /* circ.util.checkin handles errors and returns null currently */
340                         if (checkin.ilsevent == 7010 /* COPY_ALERT_MESSAGE */
341                                 || checkin.ilsevent == 1203 /* COPY_BAD_STATUS */
342                                 || checkin.ilsevent == -1 /* offline */
343                                 || checkin.ilsevent == 1502 /* ASSET_COPY_NOT_FOUND */
344                                 || checkin.ilsevent == 1203 /* COPY_BAD_STATUS */
345                                 || checkin.ilsevent == 7009 /* CIRC_CLAIMS_RETURNED */ 
346                                 || checkin.ilsevent == 7011 /* COPY_STATUS_LOST */ 
347                                 || checkin.ilsevent == 7012 /* COPY_STATUS_MISSING */) return obj.on_failure();
348                         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 ) } );
349                         if (checkin.circ && checkin.circ.checkin_time() == 'now') checkin.circ.checkin_time(backdate);
350                         if (document.getElementById('trim_list')) {
351                                 var x = document.getElementById('trim_list');
352                                 if (x.checked) { obj.list.trim_list = 20; } else { obj.list.trim_list = null; }
353                         }
354                         obj.list.append(
355                                 {
356                                         'retrieve_id' : retrieve_id,
357                                         'row' : {
358                                                 'my' : {
359                                                         'circ' : checkin.circ,
360                                                         'mvr' : checkin.record,
361                                                         'acp' : checkin.copy,
362                                                         'status' : checkin.status,
363                                                         'route_to' : checkin.route_to,
364                                                         'message' : checkin.message
365                                                 }
366                                         },
367                                         'to_top' : true
368                                 //I could override map_row_to_column here
369                                 }
370                         );
371                         obj.list.node.view.selection.select(0);
372
373                         JSAN.use('util.sound'); var sound = new util.sound(); sound.circ_good();
374
375                         if (typeof obj.on_checkin == 'function') {
376                                 obj.on_checkin(checkin);
377                         }
378                         if (typeof window.xulG == 'object' && typeof window.xulG.on_checkin == 'function') {
379                                 obj.error.sdump('D_CIRC', document.getElementById('circStrings').getString('staff.circ.checkin.exception.external') + '\n');
380                                 window.xulG.on_checkin(checkin);
381                         } else {
382                                 obj.error.sdump('D_CIRC', document.getElementById('circStrings').getString('staff.circ.checkin.exception.no_external') + '\n');
383                         }
384
385                         return true;
386
387                 } catch(E) {
388                         obj.error.standard_unexpected_error_alert(document.getElementById('circStrings').getFormattedString('staff.circ.checkin2.exception', [E]));
389                         if (typeof obj.on_failure == 'function') {
390                                 obj.on_failure(E);
391                         }
392                         if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
393                                 obj.error.sdump('D_CIRC', document.getElementById('circStrings').getString('staff.circ.checkin2.exception.external') + '\n');
394                                 window.xulG.on_failure(E);
395                         } else {
396                                 obj.error.sdump('D_CIRC', document.getElementById('circStrings').getString('staff.circ.checkin2.exception.no_external') + '\n');
397                         }
398                 }
399
400         },
401
402         'on_checkin' : function() {
403                 this.controller.view.checkin_barcode_entry_textbox.disabled = false;
404                 this.controller.view.checkin_barcode_entry_textbox.select();
405                 this.controller.view.checkin_barcode_entry_textbox.value = '';
406                 this.controller.view.checkin_barcode_entry_textbox.focus();
407         },
408
409         'on_failure' : function() {
410                 this.controller.view.checkin_barcode_entry_textbox.disabled = false;
411                 this.controller.view.checkin_barcode_entry_textbox.select();
412                 this.controller.view.checkin_barcode_entry_textbox.focus();
413         },
414         
415         'spawn_copy_editor' : function() {
416
417                 var obj = this;
418
419                 JSAN.use('util.functional');
420
421                 var list = obj.selection_list;
422
423                 list = util.functional.map_list(
424                         list,
425                         function (o) {
426                                 return o.copy_id;
427                         }
428                 );
429
430                 JSAN.use('cat.util'); cat.util.spawn_copy_editor( { 'copy_ids' : list, 'edit' : 1 } );
431
432         }
433
434 }
435
436 dump('exiting circ.checkin.js\n');