From 7b18be15c92abf9a66200fa169e6005133e2c96d Mon Sep 17 00:00:00 2001
From: erickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Date: Wed, 9 Dec 2009 19:22:58 +0000
Subject: [PATCH] added support for using patron usernames in addition to
 barcodes during offline checkout.  to use it, turn on the
 circ.offline.username_allowed org setting and have a value configured for the
 opac.barcode_regex org setting

git-svn-id: svn://svn.open-ils.org/ILS/trunk@15125 dcc99617-32d9-48b4-a31d-7c20da2025e4
---
 Open-ILS/src/offline/offline.pl | 48 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 44 insertions(+), 4 deletions(-)

diff --git a/Open-ILS/src/offline/offline.pl b/Open-ILS/src/offline/offline.pl
index 96b1bcf47e..0b4bb475cd 100755
--- a/Open-ILS/src/offline/offline.pl
+++ b/Open-ILS/src/offline/offline.pl
@@ -605,6 +605,7 @@ sub ol_handle_inhouse {
 # Pulls the relevant circ args from the command, fetches data where 
 # necessary
 # --------------------------------------------------------------------
+my %user_id_cache;
 sub ol_circ_args_from_command {
 	my $command = shift;
 
@@ -621,12 +622,43 @@ sub ol_circ_args_from_command {
 		", realtime=$realtime, workstation=$ws, checkout_time=$cotime, ".
 		"patron=$pbc, due_date=$due_date, noncat=$noncat");
 
+
 	my $args = { 
 		permit_override	=> 1, 
-		barcode				=> $barcode,		
-		checkout_time		=> $cotime, 
-		patron_barcode		=> $pbc,
-		due_date				=> $due_date };
+		barcode         => $barcode,		
+		checkout_time   => $cotime, 
+		patron_barcode  => $pbc,
+		due_date        => $due_date 
+    };
+
+    if(ol_get_org_setting('circ.offline.username_allowed')) {
+
+        my $format = ol_get_org_setting('opac.barcode_regex');
+        if($format) {
+
+            $format =~ s/^\/|\/$//g; # remove any couching //'s
+            if($pbc !~ qr/$format/) {
+
+                # the patron barcode does not match the configured barcode format
+                # assume they passed a username instead
+
+                my $user_id = $user_id_cache{$pbc} ||
+                    $U->simplereq(
+                        'open-ils.actor', 
+                        'open-ils.actor.username.exists', 
+                        $authtoken, $pbc);
+
+                
+                if($user_id) {
+                    # a valid username was provided, update the args and cache
+                    $user_id_cache{$pbc} = $user_id;
+                    $args->{patron_id} = $user_id;
+                    delete $args->{patron_barcode};
+                }
+            }
+        }
+    }
+
 
 	if( $command->{noncat} ) {
 		$args->{noncat} = 1;
@@ -637,6 +669,14 @@ sub ol_circ_args_from_command {
 	return $args;
 }
 
+sub ol_get_org_setting {
+    my $name = shift;
+    return $U->simplereq(
+        'open-ils.actor',
+        'open-ils.actor.ou_setting.ancestor_default',
+        $org, $name, $authtoken);
+}
+
 
 
 # --------------------------------------------------------------------
-- 
2.11.0