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