ef713ac6926472fa43af540179d33889b20aeb06
[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 }
11
12 circ.checkin.prototype = {
13
14         'init' : function( params ) {
15
16                 var obj = this;
17
18                 obj.session = params['session'];
19
20                 JSAN.use('circ.util');
21                 var columns = circ.util.columns( 
22                         { 
23                                 'barcode' : { 'hidden' : false },
24                                 'title' : { 'hidden' : false },
25                                 'location' : { 'hidden' : false },
26                                 'call_number' : { 'hidden' : false },
27                                 'status' : { 'hidden' : false },
28                                 'route_to' : { 'hidden' : false },
29                         } 
30                 );
31
32                 JSAN.use('util.list'); obj.list = new util.list('checkin_list');
33                 obj.list.init(
34                         {
35                                 'columns' : columns,
36                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
37                         }
38                 );
39                 
40                 JSAN.use('util.controller'); obj.controller = new util.controller();
41                 obj.controller.init(
42                         {
43                                 'control_map' : {
44                                         'checkin_barcode_entry_textbox' : [
45                                                 ['keypress'],
46                                                 function(ev) {
47                                                         if (ev.keyCode && ev.keyCode == 13) {
48                                                                 obj.checkin();
49                                                         }
50                                                 }
51                                         ],
52                                         'checkin_effective_date_label' : [
53                                                 ['render'],
54                                                 function(e) {
55                                                         return function() {
56                                                                 obj.controller.view.checkin_effective_date_textbox.value =
57                                                                         util.date.formatted_date(new Date(),'%F');
58                                                         };
59                                                 }
60                                         ],
61                                         'checkin_effective_date_textbox' : [
62                                                 ['change'],
63                                                 function(ev) {
64                                                         if (ev.target.nodeName == 'textbox') {
65                                                                 try {
66                                                                         var flag = false;
67                                                                         var darray = ev.target.value.split('-');
68                                                                         var year = darray[0]; var month = darray[1]; var day = darray[2]; 
69                                                                         if ( (!year) || (year.length != 4) || (!parseInt(year)) ) flag = true;
70                                                                         if ( (!month) || (month.length !=2) || (!parseInt(month)) ) flag = true;
71                                                                         if ( (!day) || (day.length !=2) || (!parseInt(day)) ) flag = true;
72                                                                         if (flag) {
73                                                                                 throw('invalid date format');
74                                                                         }
75                                                                         var d = new Date( year, month - 1, day );
76                                                                         if (d.toString() == 'Invalid Date') throw('Invalid Date');
77                                                                         if ( d > new Date() ) throw('Future Date');
78                                                                         ev.target.value = util.date.formatted_date(d,'%F');
79
80                                                                 } catch(E) {
81                                                                         dump('checkin:effective_date: ' + E + '\n');
82                                                                         ev.target.value = util.date.formatted_date(new Date(),'%F');
83                                                                 }
84                                                         }
85                                                 }
86                                         ],
87                                         'cmd_broken' : [
88                                                 ['command'],
89                                                 function() { alert('Not Yet Implemented'); }
90                                         ],
91                                         'cmd_checkin_submit_barcode' : [
92                                                 ['command'],
93                                                 function() {
94                                                         obj.checkin();
95                                                 }
96                                         ],
97                                         'cmd_checkin_print' : [
98                                                 ['command'],
99                                                 function() {
100                                                         dump( js2JSON( obj.list.dump() ) );
101                                                         alert( js2JSON( obj.list.dump() ) );
102                                                 }
103                                         ],
104                                         'cmd_checkin_reprint' : [
105                                                 ['command'],
106                                                 function() {
107                                                 }
108                                         ],
109                                         'cmd_checkin_done' : [
110                                                 ['command'],
111                                                 function() {
112                                                 }
113                                         ],
114                                 }
115                         }
116                 );
117                 this.controller.render();
118                 this.controller.view.checkin_barcode_entry_textbox.focus();
119
120         },
121
122         'checkin' : function() {
123                 var obj = this;
124                 try {
125                         var barcode = obj.controller.view.checkin_barcode_entry_textbox.value;
126                         var backdate = obj.controller.view.checkin_effective_date_textbox.value;
127                         JSAN.use('circ.util');
128                         var checkin = circ.util.checkin_via_barcode(
129                                 obj.session, barcode, backdate
130                         );
131                         obj.list.append(
132                                 {
133                                         'row' : {
134                                                 'my' : {
135                                                         'circ' : checkin.circ,
136                                                         'mvr' : checkin.record,
137                                                         'acp' : checkin.copy,
138                                                         'status' : checkin.status,
139                                                         'route_to' : checkin.route_to,
140                                                         'message' : checkin.message,
141                                                 }
142                                         }
143                                 //I could override map_row_to_column here
144                                 }
145                         );
146                         if (typeof obj.on_checkin == 'function') {
147                                 obj.on_checkin(checkin);
148                         }
149                         if (typeof window.xulG == 'object' && typeof window.xulG.on_checkin == 'function') {
150                                 obj.error.sdump('D_CIRC','circ.checkin: Calling external .on_checkin()\n');
151                                 window.xulG.on_checkin(checkin);
152                         } else {
153                                 obj.error.sdump('D_CIRC','circ.checkin: No external .on_checkin()\n');
154                         }
155
156                 } catch(E) {
157                         alert('FIXME: need special alert and error handling\n'
158                                 + js2JSON(E));
159                         if (typeof obj.on_failure == 'function') {
160                                 obj.on_failure(E);
161                         }
162                         if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
163                                 obj.error.sdump('D_CIRC','circ.checkin: Calling external .on_failure()\n');
164                                 window.xulG.on_failure(E);
165                         } else {
166                                 obj.error.sdump('D_CIRC','circ.checkin: No external .on_failure()\n');
167                         }
168                 }
169
170         },
171
172         'on_checkin' : function() {
173                 this.controller.view.checkin_barcode_entry_textbox.value = '';
174                 this.controller.view.checkin_barcode_entry_textbox.focus();
175         },
176
177         'on_failure' : function() {
178                 this.controller.view.checkin_barcode_entry_textbox.select();
179                 this.controller.view.checkin_barcode_entry_textbox.focus();
180         }
181 }
182
183 dump('exiting circ.checkin.js\n');