]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/circ/selfcheck/selfcheck.js
cleanup, fixes. very basic checkout works
[Evergreen.git] / Open-ILS / web / js / ui / default / circ / selfcheck / selfcheck.js
1 dojo.require('openils.CGI');
2 dojo.require('openils.Util');
3 dojo.require('openils.User');
4 dojo.require('openils.Event');
5
6 const SET_BARCODE_REGEX = 'opac.barcode_regex';
7 const SET_PATRON_TIMEOUT = 'circ.selfcheck.patron_login_timeout';
8 const SET_ALERT_ON_CHECKOUT_EVENT = 'circ.selfcheck.alert_on_checkout_event';
9 const SET_AUTO_OVERRIDE_EVENTS = 'circ.selfcheck.auto_override_checkout_events';
10 const SET_PATRON_PASSWORD_REQUIRED = 'circ.selfcheck.patron_password_required';
11
12 function SelfCheckManager() {
13
14     this.cgi = new openils.CGI();
15     this.staff = null; 
16     this.workstation = null;
17     this.authtoken = null;
18
19     this.patron = null; 
20     this.patronBarcodeRegex = null;
21
22     // current item barcode
23     this.itemBarcode = null; 
24
25     // are we currently performing a renewal?
26     this.isRenewal = false; 
27
28     // is a transaction pending?
29     this.pendingXact = false; 
30
31     // dict of org unit settings for "here"
32     this.orgSettings = {};
33 }
34
35 /**
36  * Fetch the org-unit settings, initialize the display, etc.
37  */
38 SelfCheckManager.prototype.init = function() {
39
40     this.staff = openils.User.user;
41     this.workstation = openils.User.workstation;
42     this.authtoken = openils.User.authtoken;
43     this.loadOrgSettings();
44
45     if(this.cgi.param('patron')) {
46         // Patron barcode via cgi param.  Mainly used for debugging.
47         this.loginPatron(this.cgi.param('patron'));
48     } else {
49         this.drawLoginPage();
50     }
51 }
52
53 /**
54  * Loads the org unit settings
55  */
56 SelfCheckManager.prototype.loadOrgSettings = function() {
57
58     var settings = fieldmapper.aou.fetchOrgSettingBatch(
59         this.staff.ws_ou(), [
60             SET_BARCODE_REGEX,
61             SET_PATRON_TIMEOUT,
62             SET_ALERT_ON_CHECKOUT_EVENT,
63             SET_AUTO_OVERRIDE_EVENTS,
64         ]
65     );
66
67     for(k in settings) {
68         if(settings[k])
69             this.orgSettings[k] = settings[k].value;
70     }
71
72     if(settings[SET_BARCODE_REGEX]) 
73         this.patronBarcodeRegex = new RegExp(settings[SET_BARCODE_REGEX].value);
74 }
75
76 SelfCheckManager.prototype.drawLoginPage = function() {
77     var self = this;
78
79     var bcHandler = function(barcode) {
80         // handle patron barcode entry
81
82         if(self.orgSettings[SET_PATRON_PASSWORD_REQUIRED]) {
83             
84             // password is required.  wire up the scan box to read it
85             self.updateScanBox({
86                 msg : 'Please enter your password', // TODO i18n 
87                 handler : function(pw) { self.loginPatron(barcode, ps); }
88             });
89
90             dojo.connect(selfckScanBox, 'onKeyDown', pwHandler);
91
92         } else {
93             // password is not required, go ahead and login
94             self.loginPatron(barcode);
95         }
96     };
97
98     this.updateScanBox({
99         msg : 'Please log in with your library barcode.', // TODO
100         handler : bcHandler
101     });
102 }
103
104 /**
105  * Login the patron.  
106  */
107 SelfCheckManager.prototype.loginPatron = function(barcode, passwd) {
108     console.log('loginPatron: ' + barcode);
109
110     if(this.orgSettings[SET_PATRON_PASSWORD_REQUIRED]) {
111
112         // patron password is required.  Verify it.
113
114         var res = fieldmapper.standardRequest(
115             ['open-ils.actor', 'open-ils.actor.verify_user_password'],
116             {params : [this.authtoken, barcode, null, hex_md5(passwd)]}
117         );
118
119         if(res == 0) {
120             return alert('login failed'); // TODO
121         }
122     } 
123
124     // retrieve the fleshed user by barcode
125     this.patron = fieldmapper.standardRequest(
126         ['open-ils.actor', 'open-ils.actor.user.fleshed.retrieve_by_barcode'],
127         {params : [this.authtoken, barcode]}
128     );
129
130     var evt = openils.Event.parse(this.patron);
131     if(evt) {
132
133         // User login failed, why?
134         
135         switch(evt.textcode) {
136
137             case 'ACTOR_USER_NOT_FOUND':
138                 return alert('user not found'); // TODO
139
140             case 'NO_SESSION':
141                 return alert('staff login timed out'); // TODO
142
143             default:
144                 return alert('unexpected patron login error occured: ' + evt.textcode); // TODO
145         }
146     }
147
148     // patron login succeeded
149     dojo.byId('oils-selfck-user-banner').innerHTML = 'Welcome, ' + this.patron.usrname(); // TODO i18n
150     this.drawCircPage();
151 }
152
153
154 /**
155  * Manages the main input box
156  * @param msg The context message to display with the box
157  * @param clearOnly Don't update the context message, just clear the value and re-focus
158  * @param handler Optional "on-enter" handler.  
159  */
160 SelfCheckManager.prototype.updateScanBox = function(args) {
161
162     selfckScanBox.attr('value', '');
163
164     if(args.value)
165         selfckScanBox.attr('value', args.value);
166
167     if(args.msg) 
168         dojo.byId('oils-selfck-scan-text').innerHTML = args.msg;
169
170     if(selfckScanBox._lastHandler && (args.handler || args.clearHandler)) {
171         console.log('disconnecting ' + selfckScanBox._lastHandler);
172         dojo.disconnect(selfckScanBox._lastHandler);
173     }
174
175     if(args.handler) {
176         console.log('updating scan box with ['+args.msg+'] and handler ' + args.handler);
177
178         selfckScanBox._lastHandler = dojo.connect(
179             selfckScanBox, 
180             'onKeyDown', 
181             function(e) {
182                 if(e.keyCode != dojo.keys.ENTER) 
183                     return;
184                 args.handler(selfckScanBox.attr('value'));
185             }
186         );
187     }
188
189     selfckScanBox.focus();
190 }
191
192 /**
193  *  Sets up the checkout/renewal interface
194  */
195 SelfCheckManager.prototype.drawCircPage = function() {
196
197     var self = this;
198     this.updateScanBox({
199         msg : 'Please enter an item barcode', // TODO i18n
200         handler : function(barcode) { self.checkout(barcode); }
201     });
202
203     openils.Util.show('oils-selfck-circ-page');
204
205     this.circTbody = dojo.byId('oils-selfck-circ-tbody');
206     if(!this.circTemplate)
207         this.circTemplate = this.circTbody.removeChild(dojo.byId('oils-selfck-circ-row'));
208 }
209
210
211
212 /**
213  * Check out a single item.  If the item is already checked 
214  * out to the patron, redirect to renew()
215  */
216 SelfCheckManager.prototype.checkout = function(barcode, override) {
217
218     if(!barcode) {
219         this.updateScanbox(null, true);
220         return;
221     }
222
223     // TODO see if it's a patron barcode
224     // TODO see if this item has already been checked out in this session
225
226     var method = 'open-ils.circ.checkout.full';
227     if(override) method += '.override';
228
229     var result = fieldmapper.standardRequest(
230         ['open-ils.circ', 'open-ils.circ.checkout.full'],
231         {params: [
232             this.authtoken, {
233                 patron_id : this.patron.id(),
234                 copy_barcode : barcode
235             }
236         ]}
237     );
238
239
240     if(dojo.isArray(result)) {
241         // list of results.  See if we can override all of them.
242
243     } else {
244         var evt = openils.Event.parse(result);
245
246         switch(evt.textcode) {
247             // standard result events
248             
249             case 'SUCCESS':
250                 this.displayCheckout(evt);
251                 break;
252
253             case 'OPEN_CIRCULATION_EXISTS':
254                 // TODO renewal
255                 break;
256
257             case 'NO_SESSION':
258                 // TODO logout staff
259                 break;
260         }
261     }
262
263     console.log("Circ resulted in " + js2JSON(result));
264 }
265
266 /**
267  * Renew an item
268  */
269 SelfCheckManager.prototype.renew = function() {
270 }
271
272 /**
273  * Display the result of a checkout or renewal in the items out table
274  */
275 SelfCheckManager.prototype.displayCheckout = function(evt) {
276
277     var copy = evt.payload.copy;
278     var record = evt.payload.record;
279     var circ = evt.payload.circ;
280     var row = this.circTemplate.cloneNode(true);
281
282     /*
283     if(record.isbn()) {
284             var pic = $n(template, 'jacket');
285             pic.setAttribute('src', '/opac/ac/jacket/small/' + cleanISBN(record.isbn()));
286     }
287     */
288
289     this.byName(row, 'barcode').innerHTML = copy.barcode();
290     this.byName(row, 'title').innerHTML = record.title();
291     this.byName(row, 'author').innerHTML = record.author();
292     this.circTbody.appendChild(row);
293 }
294
295
296 SelfCheckManager.prototype.byName = function(node, name) {
297     return dojo.query('[name=' + name+']', node)[0];
298 }
299
300 /**
301  * Print a receipt
302  */
303 SelfCheckManager.prototype.printReceipt = function() {
304 }
305
306 /**
307  * Build the patron holds table
308  */
309 SelfCheckManager.prototype.displayHolds = function() {
310 }
311
312
313 /**
314  * Logout the patron and return to the login page
315  */
316 SelfCheckManager.prototype.logoutPatron = function() {
317 }
318
319
320 /**
321  * Fire up the manager on page load
322  */
323 openils.Util.addOnLoad(
324     function() {
325         new SelfCheckManager().init();
326     }
327 );