]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/circ/checkin.js
show the backdate if checkin_time == 'now'
[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.date');
9         this.OpenILS = {}; JSAN.use('OpenILS.data'); this.OpenILS.data = new OpenILS.data(); this.OpenILS.data.init({'via':'stash'});
10         this.data = this.OpenILS.data;
11 }
12
13 circ.checkin.prototype = {
14
15         'selection_list' : [],
16
17         'init' : function( params ) {
18
19                 var obj = this;
20
21                 JSAN.use('circ.util');
22                 var columns = circ.util.columns( 
23                         { 
24                                 'barcode' : { 'hidden' : false },
25                                 'title' : { 'hidden' : false },
26                                 'location' : { 'hidden' : false },
27                                 'call_number' : { 'hidden' : false },
28                                 'status' : { 'hidden' : false },
29                                 'route_to' : { 'hidden' : false },
30                                 'alert_message' : { 'hidden' : false },
31                         },
32                         {
33                                 'except_these' : [ 'uses', 'checkin_time_full' ],
34                         }
35                 );
36
37                 JSAN.use('util.list'); obj.list = new util.list('checkin_list');
38                 obj.list.init(
39                         {
40                                 'columns' : columns,
41                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
42                                 'on_select' : function(ev) {
43                                         try {
44                                                 JSAN.use('util.functional');
45                                                 var sel = obj.list.retrieve_selection();
46                                                 obj.selection_list = util.functional.map_list(
47                                                         sel,
48                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
49                                                 );
50                                                 obj.error.sdump('D_TRACE', 'circ/copy_status: selection list = ' + js2JSON(obj.selection_list) );
51                                                 if (obj.selection_list.length == 0) {
52                                                         obj.controller.view.sel_edit.setAttribute('disabled','true');
53                                                         obj.controller.view.sel_opac.setAttribute('disabled','true');
54                                                         obj.controller.view.sel_patron.setAttribute('disabled','true');
55                                                         obj.controller.view.sel_copy_details.setAttribute('disabled','true');
56                                                         obj.controller.view.sel_bucket.setAttribute('disabled','true');
57                                                         obj.controller.view.sel_spine.setAttribute('disabled','true');
58                                                         obj.controller.view.sel_transit_abort.setAttribute('disabled','true');
59                                                         obj.controller.view.sel_clip.setAttribute('disabled','true');
60                                                 } else {
61                                                         obj.controller.view.sel_edit.setAttribute('disabled','false');
62                                                         obj.controller.view.sel_opac.setAttribute('disabled','false');
63                                                         obj.controller.view.sel_patron.setAttribute('disabled','false');
64                                                         obj.controller.view.sel_copy_details.setAttribute('disabled','false');
65                                                         obj.controller.view.sel_bucket.setAttribute('disabled','false');
66                                                         obj.controller.view.sel_spine.setAttribute('disabled','false');
67                                                         obj.controller.view.sel_transit_abort.setAttribute('disabled','false');
68                                                         obj.controller.view.sel_clip.setAttribute('disabled','false');
69                                                 }
70                                         } catch(E) {
71                                                 alert('FIXME: ' + E);
72                                         }
73                                 },
74
75                         }
76                 );
77                 
78                 JSAN.use('util.controller'); obj.controller = new util.controller();
79                 obj.controller.init(
80                         {
81                                 'control_map' : {
82                                         'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
83                                         'sel_clip' : [
84                                                 ['command'],
85                                                 function() { obj.list.clipboard(); }
86                                         ],
87                                         'sel_edit' : [
88                                                 ['command'],
89                                                 function() {
90                                                         try {
91                                                                 obj.spawn_copy_editor();
92                                                         } catch(E) {
93                                                                 alert(E);
94                                                         }
95                                                 }
96                                         ],
97                                         'sel_spine' : [
98                                                 ['command'],
99                                                 function() {
100                                                         JSAN.use('cat.util');
101                                                         cat.util.spawn_spine_editor(obj.selection_list);
102                                                 }
103                                         ],
104                                         'sel_opac' : [
105                                                 ['command'],
106                                                 function() {
107                                                         JSAN.use('cat.util');
108                                                         cat.util.show_in_opac(obj.selection_list);
109                                                 }
110                                         ],
111                                         'sel_transit_abort' : [
112                                                 ['command'],
113                                                 function() {
114                                                         JSAN.use('circ.util');
115                                                         circ.util.abort_transits(obj.selection_list);
116                                                 }
117                                         ],
118                                         'sel_patron' : [
119                                                 ['command'],
120                                                 function() {
121                                                         JSAN.use('circ.util');
122                                                         circ.util.show_last_few_circs(obj.selection_list);
123                                                 }
124                                         ],
125                                         'sel_copy_details' : [
126                                                 ['command'],
127                                                 function() {
128                                                         JSAN.use('circ.util');
129                                                         for (var i = 0; i < obj.selection_list.length; i++) {
130                                                                 circ.util.show_copy_details( obj.selection_list[i].copy_id );
131                                                         }
132                                                 }
133                                         ],
134
135                                         'sel_bucket' : [
136                                                 ['command'],
137                                                 function() {
138                                                         JSAN.use('cat.util');
139                                                         cat.util.add_copies_to_bucket(obj.selection_list);
140                                                 }
141                                         ],
142                                         'checkin_barcode_entry_textbox' : [
143                                                 ['keypress'],
144                                                 function(ev) {
145                                                         if (ev.keyCode && ev.keyCode == 13) {
146                                                                 obj.checkin();
147                                                         }
148                                                 }
149                                         ],
150                                         'checkin_effective_date_label' : [
151                                                 ['render'],
152                                                 function(e) {
153                                                         return function() {
154                                                                 obj.controller.view.checkin_effective_date_textbox.value =
155                                                                         util.date.formatted_date(new Date(),'%F');
156                                                         };
157                                                 }
158                                         ],
159                                         'checkin_effective_date_textbox' : [
160                                                 ['change'],
161                                                 function(ev) {
162                                                         if (ev.target.nodeName == 'textbox') {
163                                                                 try {
164                                                                         var flag = false;
165                                                                         var darray = ev.target.value.split('-');
166                                                                         var year = darray[0]; var month = darray[1]; var day = darray[2]; 
167                                                                         if ( (!year) || (year.length != 4) || (!Number(year)) ) flag = true;
168                                                                         if ( (!month) || (month.length !=2) || (!Number(month)) ) flag = true;
169                                                                         if ( (!day) || (day.length !=2) || (!Number(day)) ) flag = true;
170                                                                         if (flag) {
171                                                                                 throw('invalid date format');
172                                                                         }
173                                                                         var d = new Date( year, month - 1, day );
174                                                                         if (d.toString() == 'Invalid Date') throw('Invalid Date');
175                                                                         if ( d > new Date() ) throw('Future Date');
176                                                                         ev.target.value = util.date.formatted_date(d,'%F');
177                                                                         var x = document.getElementById('background');
178                                                                         if (x) {
179                                                                                 if ( ev.target.value == util.date.formatted_date(new Date(),'%F') ) {
180                                                                                         x.setAttribute('style','background-color: green');
181                                                                                 } else {
182                                                                                         x.setAttribute('style','background-color: red');
183                                                                                 }
184                                                                         }
185
186                                                                 } catch(E) {
187                                                                         dump('checkin:effective_date: ' + E + '\n');
188                                                                         alert('Problem setting backdate: ' + E);
189                                                                         ev.target.value = util.date.formatted_date(new Date(),'%F');
190                                                                 }
191                                                         }
192                                                 }
193                                         ],
194                                         'cmd_broken' : [
195                                                 ['command'],
196                                                 function() { alert('Not Yet Implemented'); }
197                                         ],
198                                         'cmd_checkin_submit_barcode' : [
199                                                 ['command'],
200                                                 function() {
201                                                         obj.checkin();
202                                                 }
203                                         ],
204                                         'cmd_checkin_print' : [
205                                                 ['command'],
206                                                 function() {
207                                                         obj.list.on_all_fleshed = function() {
208                                                                 try {
209                                                                         dump( js2JSON( obj.list.dump_with_keys() ) + '\n' );
210                                                                         obj.OpenILS.data.stash_retrieve();
211                                                                         var lib = obj.OpenILS.data.hash.aou[ obj.OpenILS.data.list.au[0].ws_ou() ];
212                                                                         lib.children(null);
213                                                                         var p = { 
214                                                                                 'lib' : lib,
215                                                                                 'staff' : obj.OpenILS.data.list.au[0],
216                                                                                 'header' : obj.OpenILS.data.print_list_templates.checkin.header,
217                                                                                 'line_item' : obj.OpenILS.data.print_list_templates.checkin.line_item,
218                                                                                 'footer' : obj.OpenILS.data.print_list_templates.checkin.footer,
219                                                                                 'type' : obj.OpenILS.data.print_list_templates.checkin.type,
220                                                                                 'list' : obj.list.dump_with_keys(),
221                                                                         };
222                                                                         JSAN.use('util.print'); var print = new util.print();
223                                                                         print.tree_list( p );
224                                                                         setTimeout(function(){obj.list.on_all_fleshed = null;},0);
225                                                                 } catch(E) {
226                                                                         alert(E); 
227                                                                 }
228                                                         }
229                                                         obj.list.full_retrieve();
230                                                 }
231                                         ],
232                                         'cmd_checkin_reprint' : [
233                                                 ['command'],
234                                                 function() {
235                                                         JSAN.use('util.print'); var print = new util.print();
236                                                         print.reprint_last();
237                                                 }
238                                         ],
239                                         'cmd_checkin_done' : [
240                                                 ['command'],
241                                                 function() {
242                                                 }
243                                         ],
244                                 }
245                         }
246                 );
247                 this.controller.render();
248                 this.controller.view.checkin_barcode_entry_textbox.focus();
249
250         },
251
252         'checkin' : function() {
253                 var obj = this;
254                 try {
255                         var barcode = obj.controller.view.checkin_barcode_entry_textbox.value;
256                         if (!barcode) return;
257                         var backdate = obj.controller.view.checkin_effective_date_textbox.value;
258                         var auto_print = document.getElementById('checkin_auto');
259                         if (auto_print) auto_print = auto_print.checked;
260                         JSAN.use('circ.util');
261                         var checkin = circ.util.checkin_via_barcode(
262                                 ses(), barcode, backdate, auto_print
263                         );
264                         if (!checkin) return obj.on_failure(); /* circ.util.checkin handles errors and returns null currently */
265                         if (checkin.ilsevent == 7010 /* COPY_ALERT_MESSAGE */
266                                 || checkin.ilsevent == 1203 /* COPY_BAD_STATUS */
267                                 || checkin.ilsevent == -1 /* offline */
268                                 || checkin.ilsevent == 1502 /* ASSET_COPY_NOT_FOUND */
269                                 || checkin.ilsevent == 1203 /* COPY_BAD_STATUS */
270                                 || checkin.ilsevent == 7009 /* CIRC_CLAIMS_RETURNED */ 
271                                 || checkin.ilsevent == 7011 /* COPY_STATUS_LOST */ 
272                                 || checkin.ilsevent == 7012 /* COPY_STATUS_MISSING */) return obj.on_failure();
273                         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 ) } );
274                         if (checkin.circ.checkin_time() == 'now') checkin.circ.checkin_time(backdate);
275                         obj.list.append(
276                                 {
277                                         'retrieve_id' : retrieve_id,
278                                         'row' : {
279                                                 'my' : {
280                                                         'circ' : checkin.circ,
281                                                         'mvr' : checkin.record,
282                                                         'acp' : checkin.copy,
283                                                         'status' : checkin.status,
284                                                         'route_to' : checkin.route_to,
285                                                         'message' : checkin.message,
286                                                 }
287                                         },
288                                         'to_top' : true,
289                                 //I could override map_row_to_column here
290                                 }
291                         );
292
293                         JSAN.use('util.sound'); var sound = new util.sound(); sound.circ_good();
294
295                         if (typeof obj.on_checkin == 'function') {
296                                 obj.on_checkin(checkin);
297                         }
298                         if (typeof window.xulG == 'object' && typeof window.xulG.on_checkin == 'function') {
299                                 obj.error.sdump('D_CIRC','circ.checkin: Calling external .on_checkin()\n');
300                                 window.xulG.on_checkin(checkin);
301                         } else {
302                                 obj.error.sdump('D_CIRC','circ.checkin: No external .on_checkin()\n');
303                         }
304
305                 } catch(E) {
306                         obj.error.standard_unexpected_error_alert('Something went wrong in circ.checkin.checkin: ',E);
307                         if (typeof obj.on_failure == 'function') {
308                                 obj.on_failure(E);
309                         }
310                         if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
311                                 obj.error.sdump('D_CIRC','circ.checkin: Calling external .on_failure()\n');
312                                 window.xulG.on_failure(E);
313                         } else {
314                                 obj.error.sdump('D_CIRC','circ.checkin: No external .on_failure()\n');
315                         }
316                 }
317
318         },
319
320         'on_checkin' : function() {
321                 this.controller.view.checkin_barcode_entry_textbox.value = '';
322                 this.controller.view.checkin_barcode_entry_textbox.focus();
323         },
324
325         'on_failure' : function() {
326                 this.controller.view.checkin_barcode_entry_textbox.select();
327                 this.controller.view.checkin_barcode_entry_textbox.focus();
328         },
329         
330         'spawn_copy_editor' : function() {
331
332                 /* FIXME -  a lot of redundant calls here */
333
334                 var obj = this;
335
336                 JSAN.use('util.widgets'); JSAN.use('util.functional');
337
338                 var list = obj.selection_list;
339
340                 list = util.functional.map_list(
341                         list,
342                         function (o) {
343                                 return o.copy_id;
344                         }
345                 );
346
347                 var copies = util.functional.map_list(
348                         list,
349                         function (acp_id) {
350                                 return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
351                         }
352                 );
353
354                 var edit = 0;
355                 try {
356                         edit = obj.network.request(
357                                 api.PERM_MULTI_ORG_CHECK.app,
358                                 api.PERM_MULTI_ORG_CHECK.method,
359                                 [ 
360                                         ses(), 
361                                         obj.data.list.au[0].id(), 
362                                         util.functional.map_list(
363                                                 copies,
364                                                 function (o) {
365                                                         return obj.network.simple_request('FM_ACN_RETRIEVE',[o.call_number()]).owning_lib();
366                                                 }
367                                         ),
368                                         [ 'UPDATE_COPY', 'UPDATE_BATCH_COPY' ]
369                                 ]
370                         ).length == 0 ? 1 : 0;
371                 } catch(E) {
372                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
373                 }
374
375                 JSAN.use('cat.util'); cat.util.spawn_copy_editor(list,edit);
376
377         },
378
379 }
380
381 dump('exiting circ.checkin.js\n');