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