1 dojo.require('dojo.date.locale');
2 dojo.require('dojo.date.stamp');
3 dojo.require('openils.CGI');
4 dojo.require('openils.Util');
5 dojo.require('openils.User');
6 dojo.require('openils.Event');
8 dojo.requireLocalization('openils.circ', 'selfcheck');
9 var localeStrings = dojo.i18n.getLocalization('openils.circ', 'selfcheck');
12 const SET_BARCODE_REGEX = 'opac.barcode_regex';
13 const SET_PATRON_TIMEOUT = 'circ.selfcheck.patron_login_timeout';
14 const SET_ALERT_ON_CHECKOUT_EVENT = 'circ.selfcheck.alert_on_checkout_event';
15 const SET_AUTO_OVERRIDE_EVENTS = 'circ.selfcheck.auto_override_checkout_events';
16 const SET_PATRON_PASSWORD_REQUIRED = 'circ.selfcheck.patron_password_required';
18 function SelfCheckManager() {
20 this.cgi = new openils.CGI();
22 this.workstation = null;
23 this.authtoken = null;
26 this.patronBarcodeRegex = null;
28 // current item barcode
29 this.itemBarcode = null;
31 // are we currently performing a renewal?
32 this.isRenewal = false;
34 // is a transaction pending?
35 this.pendingXact = false;
37 // dict of org unit settings for "here"
38 this.orgSettings = {};
41 // Construct a mock checkout for debugging purposes
44 record : new fieldmapper.mvr(),
45 copy : new fieldmapper.acp(),
46 circ : new fieldmapper.circ()
50 this.mockCheckout.payload.record.title('Jazz improvisation for guitar');
51 this.mockCheckout.payload.record.author('Wise, Les');
52 this.mockCheckout.payload.record.isbn('0634033565');
53 this.mockCheckout.payload.copy.barcode('123456789');
54 this.mockCheckout.payload.circ.renewal_remaining(1);
55 this.mockCheckout.payload.circ.parent_circ(1);
56 this.mockCheckout.payload.circ.due_date('2012-12-21');
62 * Fetch the org-unit settings, initialize the display, etc.
64 SelfCheckManager.prototype.init = function() {
66 this.staff = openils.User.user;
67 this.workstation = openils.User.workstation;
68 this.authtoken = openils.User.authtoken;
69 this.loadOrgSettings();
71 // add onclick handlers for nav links
75 dojo.byId('oils-selfck-hold-details-link'),
77 function() { self.drawHoldsPage(); }
81 dojo.byId('oils-selfck-pay-fines-link'),
83 function() { self.drawPayFinesPage(); }
87 if(this.cgi.param('patron')) {
89 // Patron barcode via cgi param. Mainly used for debugging and
90 // only works if password is not required by policy
91 this.loginPatron(this.cgi.param('patron'));
99 * Loads the org unit settings
101 SelfCheckManager.prototype.loadOrgSettings = function() {
103 var settings = fieldmapper.aou.fetchOrgSettingBatch(
104 this.staff.ws_ou(), [
107 SET_ALERT_ON_CHECKOUT_EVENT,
108 SET_AUTO_OVERRIDE_EVENTS,
114 this.orgSettings[k] = settings[k].value;
117 if(settings[SET_BARCODE_REGEX])
118 this.patronBarcodeRegex = new RegExp(settings[SET_BARCODE_REGEX].value);
121 SelfCheckManager.prototype.drawLoginPage = function() {
124 var bcHandler = function(barcode) {
125 // handle patron barcode entry
127 if(self.orgSettings[SET_PATRON_PASSWORD_REQUIRED]) {
129 // password is required. wire up the scan box to read it
131 msg : 'Please enter your password', // TODO i18n
132 handler : function(pw) { self.loginPatron(barcode, ps); }
135 dojo.connect(selfckScanBox, 'onKeyDown', pwHandler);
138 // password is not required, go ahead and login
139 self.loginPatron(barcode);
144 msg : 'Please log in with your library barcode.', // TODO
152 SelfCheckManager.prototype.loginPatron = function(barcode, passwd) {
154 if(this.orgSettings[SET_PATRON_PASSWORD_REQUIRED]) {
156 // patron password is required. Verify it.
158 var res = fieldmapper.standardRequest(
159 ['open-ils.actor', 'open-ils.actor.verify_user_password'],
160 {params : [this.authtoken, barcode, null, hex_md5(passwd)]}
164 return alert('login failed'); // TODO
168 // retrieve the fleshed user by barcode
169 this.patron = fieldmapper.standardRequest(
170 ['open-ils.actor', 'open-ils.actor.user.fleshed.retrieve_by_barcode'],
171 {params : [this.authtoken, barcode]}
174 var evt = openils.Event.parse(this.patron);
177 // User login failed, why?
179 switch(evt.textcode) {
181 case 'ACTOR_USER_NOT_FOUND':
182 return alert('user not found'); // TODO
185 return alert('staff login timed out'); // TODO
188 return alert('unexpected patron login error occured: ' + evt.textcode); // TODO
192 // patron login succeeded
193 dojo.byId('oils-selfck-user-banner').innerHTML = 'Welcome, ' + this.patron.usrname(); // TODO i18n
199 * Manages the main input box
200 * @param msg The context message to display with the box
201 * @param clearOnly Don't update the context message, just clear the value and re-focus
202 * @param handler Optional "on-enter" handler.
204 SelfCheckManager.prototype.updateScanBox = function(args) {
206 selfckScanBox.attr('value', '');
209 selfckScanBox.attr('value', args.value);
212 dojo.byId('oils-selfck-scan-text').innerHTML = args.msg;
214 if(selfckScanBox._lastHandler && (args.handler || args.clearHandler)) {
215 dojo.disconnect(selfckScanBox._lastHandler);
219 selfckScanBox._lastHandler = dojo.connect(
223 if(e.keyCode != dojo.keys.ENTER)
225 args.handler(selfckScanBox.attr('value'));
230 selfckScanBox.focus();
234 * Sets up the checkout/renewal interface
236 SelfCheckManager.prototype.drawCircPage = function() {
240 msg : 'Please enter an item barcode', // TODO i18n
241 handler : function(barcode) { self.checkout(barcode); }
244 openils.Util.show('oils-selfck-circ-page');
246 this.circTbody = dojo.byId('oils-selfck-circ-tbody');
247 if(!this.circTemplate)
248 this.circTemplate = this.circTbody.removeChild(dojo.byId('oils-selfck-circ-row'));
250 // items out, holds, and fines summaries
253 fieldmapper.standardRequest(
254 ['open-ils.actor', 'open-ils.actor.user.fines.summary'],
256 params : [this.authtoken, this.patron.id()],
257 oncomplete : function(r) {
258 var summary = openils.Util.readResponse(r);
259 dojo.byId('oils-selfck-fines-total').innerHTML =
260 dojo.string.substitute(
261 localeStrings.TOTAL_FINES_ACCOUNT,
262 [summary.balance_owed()]
269 this.updateHoldsSummary();
272 this.updateCircSummary();
274 // render mock checkouts for debugging?
275 if(this.cgi.param('mock-circ')) {
276 for(var i in [1,2,3])
277 this.displayCheckout(this.mockCheckout);
281 SelfCheckManager.prototype.updateHoldsSummary = function(decrement) {
283 if(!this.holdsSummary) {
284 var summary = fieldmapper.standardRequest(
285 ['open-ils.circ', 'open-ils.circ.holds.user_summary'],
286 {params : [this.authtoken, this.patron.id()]}
289 this.holdsSummary = {};
290 this.holdsSummary.ready = Number(summary['4']);
291 this.holdsSummary.total = 0;
293 for(var i in summary)
294 this.holdsSummary.total += Number(summary[i]);
298 this.holdsSummary.ready -= 1;
300 dojo.byId('oils-selfck-holds-total').innerHTML =
301 dojo.string.substitute(
302 localeStrings.TOTAL_HOLDS,
303 [this.holdsSummary.total]
306 dojo.byId('oils-selfck-holds-ready').innerHTML =
307 dojo.string.substitute(
308 localeStrings.HOLDS_READY_FOR_PICKUP,
309 [this.holdsSummary.ready]
314 SelfCheckManager.prototype.updateCircSummary = function(increment) {
316 if(!this.circSummary) {
318 var summary = fieldmapper.standardRequest(
319 ['open-ils.actor', 'open-ils.actor.user.checked_out.count'],
320 {params : [this.authtoken, this.patron.id()]}
324 total : Number(summary.out) + Number(summary.overdue),
325 overdue : Number(summary.overdue),
331 // local checkout occurred. Add to the total and the session.
332 this.circSummary.total += 1;
333 this.circSummary.session += 1;
336 dojo.byId('oils-selfck-circ-account-total').innerHTML =
337 dojo.string.substitute(
338 localeStrings.TOTAL_ITEMS_ACCOUNT,
339 [this.circSummary.total]
342 dojo.byId('oils-selfck-circ-session-total').innerHTML =
343 dojo.string.substitute(
344 localeStrings.TOTAL_ITEMS_SESSION,
345 [this.circSummary.session]
350 SelfCheckManager.prototype.drawHoldsPage = function() {
352 // TODO add option to hid scanBox
353 // this.updateScanBox(...)
355 openils.Util.hide('oils-selfck-circ-page');
356 openils.Util.hide('oils-selfck-payment-page');
357 openils.Util.show('oils-selfck-holds-page');
364 * Check out a single item. If the item is already checked
365 * out to the patron, redirect to renew()
367 SelfCheckManager.prototype.checkout = function(barcode, override) {
370 this.updateScanbox(null, true);
374 // TODO see if it's a patron barcode
375 // TODO see if this item has already been checked out in this session
377 var method = 'open-ils.circ.checkout.full';
378 if(override) method += '.override';
380 var result = fieldmapper.standardRequest(
381 ['open-ils.circ', 'open-ils.circ.checkout.full'],
384 patron_id : this.patron.id(),
385 copy_barcode : barcode
391 if(dojo.isArray(result)) {
392 // list of results. See if we can override all of them.
395 var evt = openils.Event.parse(result);
397 switch(evt.textcode) {
398 // standard result events
401 this.displayCheckout(evt);
404 case 'OPEN_CIRCULATION_EXISTS':
414 console.log("Circ resulted in " + js2JSON(result));
420 SelfCheckManager.prototype.renew = function() {
424 * Display the result of a checkout or renewal in the items out table
426 SelfCheckManager.prototype.displayCheckout = function(evt) {
428 var copy = evt.payload.copy;
429 var record = evt.payload.record;
430 var circ = evt.payload.circ;
431 var row = this.circTemplate.cloneNode(true);
434 this.byName(row, 'jacket').setAttribute('src', '/opac/extras/ac/jacket/small/' + record.isbn());
437 this.byName(row, 'barcode').innerHTML = copy.barcode();
438 this.byName(row, 'title').innerHTML = record.title();
439 this.byName(row, 'author').innerHTML = record.author();
440 this.byName(row, 'remaining').innerHTML = circ.renewal_remaining();
442 var date = dojo.date.stamp.fromISOString(circ.due_date());
443 this.byName(row, 'due_date').innerHTML =
444 dojo.date.locale.format(date, {selector : 'date'});
446 this.circTbody.appendChild(row);
450 SelfCheckManager.prototype.byName = function(node, name) {
451 return dojo.query('[name=' + name+']', node)[0];
457 SelfCheckManager.prototype.printReceipt = function() {
461 * Build the patron holds table
463 SelfCheckManager.prototype.displayHolds = function() {
468 * Logout the patron and return to the login page
470 SelfCheckManager.prototype.logoutPatron = function() {
475 * Fire up the manager on page load
477 openils.Util.addOnLoad(
479 new SelfCheckManager().init();