plugged in autorenewal, more event handling
[Evergreen.git] / Open-ILS / web / js / ui / default / circ / selfcheck / selfcheck.js
index 6e4a6b4..25c02e7 100644 (file)
@@ -16,6 +16,8 @@ const SET_ALERT_ON_CHECKOUT_EVENT = 'circ.selfcheck.alert_on_checkout_event';
 const SET_AUTO_OVERRIDE_EVENTS = 'circ.selfcheck.auto_override_checkout_events';
 const SET_PATRON_PASSWORD_REQUIRED = 'circ.selfcheck.patron_password_required';
 
+//openils.Util.playAudioUrl('/xul/server/skin/media/audio/bonus.wav');
+
 function SelfCheckManager() {
 
     this.cgi = new openils.CGI();
@@ -32,13 +34,9 @@ function SelfCheckManager() {
     // are we currently performing a renewal?
     this.isRenewal = false; 
 
-    // is a transaction pending?
-    this.pendingXact = false; 
-
     // dict of org unit settings for "here"
     this.orgSettings = {};
 
-
     // Construct a mock checkout for debugging purposes
     if(this.mockCheckouts = this.cgi.param('mock-circ')) {
 
@@ -133,11 +131,9 @@ SelfCheckManager.prototype.drawLoginPage = function() {
             // password is required.  wire up the scan box to read it
             self.updateScanBox({
                 msg : 'Please enter your password', // TODO i18n 
-                handler : function(pw) { self.loginPatron(barcode, ps); }
+                handler : function(pw) { self.loginPatron(barcode, pw); }
             });
 
-            dojo.connect(selfckScanBox, 'onKeyDown', pwHandler);
-
         } else {
             // password is not required, go ahead and login
             self.loginPatron(barcode);
@@ -165,7 +161,8 @@ SelfCheckManager.prototype.loginPatron = function(barcode, passwd) {
         );
 
         if(res == 0) {
-            return alert('login failed'); // TODO
+            // user-not-found results in login failure
+            this.handleXactResult('login', barcode, {textcode : 'ACTOR_USER_NOT_FOUND'});
         }
     } 
 
@@ -177,25 +174,14 @@ SelfCheckManager.prototype.loginPatron = function(barcode, passwd) {
 
     var evt = openils.Event.parse(this.patron);
     if(evt) {
+        this.handleXactResult('login', barcode, evt);
 
-        // User login failed, why?
-        
-        switch(evt.textcode) {
-
-            case 'ACTOR_USER_NOT_FOUND':
-                return alert('user not found'); // TODO
-
-            case 'NO_SESSION':
-                return alert('staff login timed out'); // TODO
+    } else {
 
-            default:
-                return alert('unexpected patron login error occured: ' + evt.textcode); // TODO
-        }
+        dojo.byId('oils-selfck-status-div').innerHTML = '';
+        dojo.byId('oils-selfck-user-banner').innerHTML = 'Welcome, ' + this.patron.usrname(); // TODO i18n
+        this.drawCircPage();
     }
-
-    // patron login succeeded
-    dojo.byId('oils-selfck-user-banner').innerHTML = 'Welcome, ' + this.patron.usrname(); // TODO i18n
-    this.drawCircPage();
 }
 
 
@@ -206,6 +192,7 @@ SelfCheckManager.prototype.loginPatron = function(barcode, passwd) {
  * @param handler Optional "on-enter" handler.  
  */
 SelfCheckManager.prototype.updateScanBox = function(args) {
+    args = args || {};
 
     if(args.select) {
         selfckScanBox.domNode.select();
@@ -284,7 +271,7 @@ SelfCheckManager.prototype.drawCircPage = function() {
     // render mock checkouts for debugging?
     if(this.mockCheckouts) {
         for(var i in [1,2,3]) 
-            this.displayCheckout(this.mockCheckout);
+            this.displayCheckout(this.mockCheckout, 'checkout');
     }
 }
 
@@ -467,7 +454,7 @@ SelfCheckManager.prototype.checkout = function(barcode, override) {
     if(this.mockCheckouts) {
         // if we're in mock-checkout mode, just insert another
         // fake circ into the table and get out of here.
-        this.displayCheckout(this.mockCheckout);
+        this.displayCheckout(this.mockCheckout, 'checkout');
         return;
     }
 
@@ -477,6 +464,8 @@ SelfCheckManager.prototype.checkout = function(barcode, override) {
     var method = 'open-ils.circ.checkout.full';
     if(override) method += '.override';
 
+    console.log("Checkout out item " + barcode + " with method " + method);
+
     var result = fieldmapper.standardRequest(
         ['open-ils.circ', 'open-ils.circ.checkout.full'],
         {params: [
@@ -487,47 +476,156 @@ SelfCheckManager.prototype.checkout = function(barcode, override) {
         ]}
     );
 
+    var stat = this.handleXactResult('checkout', barcode, result);
+
+    if(stat.override) {
+        this.checkout(barcode, true);
+    } else if(stat.renew) {
+        // TODO check org setting for auto-renewal interval
+        this.renew(barcode);
+    }
+}
+
 
-    if(dojo.isArray(result)) {
-        // list of results.  See if we can override all of them.
+SelfCheckManager.prototype.handleXactResult = function(action, item, result) {
+
+    var displayText = '';
+    var popup = false;
+
+    // TODO handle lost/missing/etc checkin+checkout override steps
+        
+    if(result.textcode == 'NO_SESSION') {
+
+        return this.logoutStaff();
+
+    } else if(result.textcode == 'SUCCESS') {
+
+        if(action == 'checkout') {
+
+            displayText = dojo.string.substitute(
+                localeStrings.CHECKOUT_SUCCESS, [item]);
+                this.displayCheckout(result, 'checkout');
+
+        } else if(action == 'renew') {
+
+            displayText = dojo.string.substitute(
+                localeStrings.RENEW_SUCCESS, [item]);
+                this.displayCheckout(result, 'renew');
+        }
+
+        this.updateScanBox();
+
+    } else if(result.textcode == 'OPEN_CIRCULATION_EXISTS' && action == 'checkout') {
+
+        return { renew : true };
 
     } else {
-        var evt = openils.Event.parse(result);
 
-        switch(evt.textcode) {
-            // standard result events
+        var overrideEvents = this.orgSettings[SET_AUTO_OVERRIDE_EVENTS];
+    
+        if(overrideEvents && overrideEvents.length) {
             
-            case 'SUCCESS':
-                this.displayCheckout(evt);
+            // see if the events we received are all in the list of
+            // events to override
+    
+            if(!result.length) result = [result];
+    
+            var override = true;
+            for(var i = 0; i < result.length; i++) {
+                var match = overrideEvents.filter(
+                    function(e) { return (e == result[i].textcode); })[0];
+                if(!match) {
+                    override = false;
+                    break;
+                }
+            }
+
+            if(override) 
+                return { override : true };
+        }
+    
+        this.updateScanBox({select : true});
+        popup = true;
+
+        if(result.length) 
+            result = result[0];
+
+        switch(result.textcode) {
+
+            case 'ACTOR_USER_NOT_FOUND' : 
+                displayText = dojo.string.substitute(
+                    localeStrings.LOGIN_FAILED, [item]);
                 break;
 
-            case 'OPEN_CIRCULATION_EXISTS':
-                // TODO renewal
+            case 'MAX_RENEWALS_REACHED' :
+                displayText = dojo.string.substitute(
+                    localeStrings.MAX_RENEWALS, [item]);
                 break;
 
-            case 'NO_SESSION':
-                // TODO logout staff?
+            case 'ITEM_NOT_CATALOGED' :
+                displayText = dojo.string.substitute(
+                    localeStrings.ITEM_NOT_CATALOGED, [item]);
+                break;
+
+            case 'already-out' : 
+                displayText = dojo.string.substitute(
+                    localeStrings.ALREADY_OUT, [item]);
                 break;
 
             default:
-                dojo.byId('oils-selfck-status-div').innerHTML = evt.textcode;
-                this.updateScanBox({select:true});
+                console.error('Unhandled event ' + result.textcode);
+
+                if(action == 'checkout' || action == 'renew') {
+                    displayText = dojo.string.substitute(
+                        localeStrings.GENERIC_CIRC_FAILURE, [item]);
+                } else {
+                    displayText = dojo.string.substitute(
+                        localeStrings.UNKNOWN_ERROR, [result.textcode]);
+                }
         }
     }
 
-    console.log("Circ resulted in " + js2JSON(result));
+    console.log("Updating status with " + displayText);
+
+    dojo.byId('oils-selfck-status-div').innerHTML = displayText;
+
+    if(popup && this.orgSettings[SET_ALERT_ON_CHECKOUT_EVENT]) 
+        alert(displayText);
+
+    return {};
 }
 
+
 /**
  * Renew an item
  */
-SelfCheckManager.prototype.renew = function() {
+SelfCheckManager.prototype.renew = function(barcode, override) {
+
+    var method = 'open-ils.circ.renew';
+    if(override) method += '.override';
+
+    console.log("Renewing item " + barcode + " with method " + method);
+
+    var result = fieldmapper.standardRequest(
+        ['open-ils.circ', method],
+        {params: [
+            this.authtoken, {
+                patron_id : this.patron.id(),
+                copy_barcode : barcode
+            }
+        ]}
+    );
+
+    var stat = this.handleXactResult('renew', barcode, result);
+
+    if(stat.override)
+        this.renew(barcode, true);
 }
 
 /**
  * Display the result of a checkout or renewal in the items out table
  */
-SelfCheckManager.prototype.displayCheckout = function(evt) {
+SelfCheckManager.prototype.displayCheckout = function(evt, type) {
 
     var copy = evt.payload.copy;
     var record = evt.payload.record;
@@ -542,6 +640,7 @@ SelfCheckManager.prototype.displayCheckout = function(evt) {
     this.byName(row, 'title').innerHTML = record.title();
     this.byName(row, 'author').innerHTML = record.author();
     this.byName(row, 'remaining').innerHTML = circ.renewal_remaining();
+    openils.Util.show(this.byName(row, type));
 
     var date = dojo.date.stamp.fromISOString(circ.due_date());
     this.byName(row, 'due_date').innerHTML = 
@@ -576,6 +675,12 @@ SelfCheckManager.prototype.printReceipt = function() {
  * Logout the patron and return to the login page
  */
 SelfCheckManager.prototype.logoutPatron = function() {
+
+    this.patron = null;
+    this.holdsSummary = null;
+    this.circSummary = null;
+
+    this.drawLoginPage();
 }