Add Staff member type. Add permissions checks for bug# 1269
[koha_fer] / C4 / Members.pm
index 8ba07a4..4e1822f 100644 (file)
@@ -17,7 +17,6 @@ package C4::Members;
 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 # Suite 330, Boston, MA  02111-1307 USA
 
-# $Id$
 
 use strict;
 require Exporter;
@@ -31,7 +30,7 @@ use C4::Reserves;
 
 our ($VERSION,@ISA,@EXPORT,@EXPORT_OK);
 
-$VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
+$VERSION = 3.00;
 
 =head1 NAME
 
@@ -122,7 +121,7 @@ push @EXPORT, qw(
 
 =item SearchMember
 
-  ($count, $borrowers) = &SearchMember($searchstring, $type,$category_type);
+  ($count, $borrowers) = &SearchMember($searchstring, $type,$category_type,$filter,$showallbranches);
 
 Looks up patrons (borrowers) by name.
 
@@ -137,6 +136,10 @@ C<$searchstring> is a space-separated list of search terms. Each term
 must match the beginning a borrower's surname, first name, or other
 name.
 
+C<$filter> is assumed to be a list of elements to filter results on
+
+C<$showallbranches> is used in IndependantBranches Context to display all branches results.
+
 C<&SearchMember> returns a two-element list. C<$borrowers> is a
 reference-to-array; each element is a reference-to-hash, whose keys
 are the fields of the C<borrowers> table in the Koha database.
@@ -146,22 +149,43 @@ C<$count> is the number of elements in C<$borrowers>.
 
 #'
 #used by member enquiries from the intranet
-#called by member.pl
+#called by member.pl and circ/circulation.pl
 sub SearchMember {
-    my ($searchstring, $orderby, $type,$category_type ) = @_;
+    my ($searchstring, $orderby, $type,$category_type,$filter,$showallbranches ) = @_;
     my $dbh   = C4::Context->dbh;
     my $query = "";
     my $count;
     my @data;
     my @bind = ();
-
+       
+       # this is used by circulation everytime a new borrowers cardnumber is scanned
+       # so we can check an exact match first, if that works return, otherwise do the rest
+    $query = "SELECT * FROM borrowers
+                    LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
+                             WHERE cardnumber = ?";
+    my $sth = $dbh->prepare($query);
+    $sth->execute($searchstring);
+    my $data = $sth->fetchall_arrayref({});
+    if (@$data){
+               return ( scalar(@$data), $data );
+       }
+    $sth->finish;
+               
     if ( $type eq "simple" )    # simple search for one letter only
     {
-        $query =
-          "SELECT * FROM borrowers
-                  LEFT JOIN categories ON borrowers.categorycode=categories.categorycode ".
-                  ($category_type?" AND category_type = ".$dbh->quote($category_type):"").
-                  " WHERE surname LIKE ? OR cardnumber like ? ORDER BY $orderby";
+        $query =  "SELECT *
+             FROM borrowers
+             LEFT JOIN categories ON borrowers.categorycode=categories.categorycode ".
+                    ($category_type?" AND category_type = ".$dbh->quote($category_type):""); 
+
+        $query .=
+         " WHERE (surname LIKE ? OR cardnumber like ?) ";
+        if (C4::Context->preference("IndependantBranches") && !$showallbranches){
+          if (C4::Context->userenv && C4::Context->userenv->{flags}!=1 && C4::Context->userenv->{'branch'}){
+            $query.=" AND borrowers.branchcode =".$dbh->quote(C4::Context->userenv->{'branch'}) unless (C4::Context->userenv->{'branch'} eq "insecure");
+          }      
+        }     
+        $query.=" ORDER BY $orderby";
         @bind = ("$searchstring%","$searchstring");
     }
     else    # advanced search looking in surname, firstname and othernames
@@ -170,9 +194,15 @@ sub SearchMember {
         $count = @data;
         $query = "SELECT * FROM borrowers
                     LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
-               WHERE ((surname LIKE ? OR surname LIKE ?
-               OR firstname  LIKE ? OR firstname LIKE ?
-               OR othernames LIKE ? OR othernames LIKE ?)
+                             WHERE ";
+        if (C4::Context->preference("IndependantBranches") && !$showallbranches){
+          if (C4::Context->userenv && C4::Context->userenv->{flags}!=1 && C4::Context->userenv->{'branch'}){
+            $query.=" borrowers.branchcode =".$dbh->quote(C4::Context->userenv->{'branch'})." AND " unless (C4::Context->userenv->{'branch'} eq "insecure");
+          }      
+        }     
+        $query.="((surname LIKE ? OR surname LIKE ?
+                             OR firstname  LIKE ? OR firstname LIKE ?
+                             OR othernames LIKE ? OR othernames LIKE ?)
                ".
                   ($category_type?" AND category_type = ".$dbh->quote($category_type):"");
         @bind = (
@@ -198,7 +228,7 @@ sub SearchMember {
 
     my $sth = $dbh->prepare($query);
 
-    #  warn "Q $orderby : $query";
+#     warn "Q $orderby : $query";
     $sth->execute(@bind);
     my @results;
     my $data = $sth->fetchall_arrayref({});
@@ -322,6 +352,7 @@ sub GetMemberDetails {
     my $borrower = $sth->fetchrow_hashref;
     my ($amount) = GetMemberAccountRecords( $borrowernumber);
     $borrower->{'amountoutstanding'} = $amount;
+       # FIXME - patronflags calls GetMemberAccountRecords... just have patronflags return $amount
     my $flags = patronflags( $borrower);
     my $accessflagshash;
 
@@ -379,7 +410,7 @@ sub GetMemberDetails {
         {itemlist}    ref-to-array: list of available items
 
 =cut
-
+# FIXME rename this function.
 sub patronflags {
     my %flags;
     my ( $patroninformation) = @_;
@@ -573,8 +604,6 @@ sub ModMember {
     $data{'dateofbirth'}  = format_date_in_iso( $data{'dateofbirth'} ) if ($data{'dateofbirth'} );
     $data{'dateexpiry'}   = format_date_in_iso( $data{'dateexpiry'} ) if ($data{'dateexpiry'} );
     $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} ) if ($data{'dateenrolled'} );
-#     warn Data::Dumper::Dumper(%data);
-    #   warn "num user".$data{'borrowernumber'};
     my $qborrower=$dbh->prepare("SHOW columns from borrowers");
     $qborrower->execute;
     my %hashborrowerfields;  
@@ -583,14 +612,13 @@ sub ModMember {
     }  
     my $query;
     my $sth;
-    $data{'userid'} = '' if ( $data{'password'} eq '' );  
     my @parameters;  
     
     # test to know if u must update or not the borrower password
     if ( $data{'password'} eq '****' ) {
         delete $data{'password'};
         foreach (keys %data)
-        {push @parameters,"$_ = ".$dbh->quote($data{$_}) if ($_ ne "borrowernumber" and $hashborrowerfields{$_}) } ;
+        {push @parameters,"$_ = ".$dbh->quote($data{$_}) if ($_ ne "borrowernumber" and $_ ne "flags"  and $hashborrowerfields{$_}) } ;
         $query = "UPDATE borrowers SET ".join (",",@parameters)
     ." WHERE borrowernumber=$data{'borrowernumber'}";
 #         warn "$query";
@@ -601,7 +629,7 @@ sub ModMember {
         $data{'password'} = md5_base64( $data{'password'} )   if ( $data{'password'} ne '' );
         delete $data{'password'} if ($data{password} eq "");
         foreach (keys %data)
-        {push @parameters,"$_ = ".$dbh->quote($data{$_}) if ($_ ne "borrowernumber" and $hashborrowerfields{$_})} ;
+        {push @parameters,"$_ = ".$dbh->quote($data{$_}) if ($_ ne "borrowernumber" and $_ ne "flags" and $hashborrowerfields{$_})} ;
         
         $query = "UPDATE borrowers SET ".join (",",@parameters)." WHERE borrowernumber=$data{'borrowernumber'}";
 #         warn "$query";
@@ -614,7 +642,7 @@ sub ModMember {
 # so when we update information for an adult we should check for guarantees and update the relevant part
 # of their records, ie addresses and phone numbers
     my $borrowercategory= GetBorrowercategory( $data{'category_type'} );
-    if ( $borrowercategory->{'category_type'} eq 'A' ) {
+    if ( $borrowercategory->{'category_type'} eq ('A' || 'S') ) {
         # is adult check guarantees;
         UpdateGuarantees(%data);
 
@@ -641,9 +669,9 @@ sub AddMember {
     my $dbh = C4::Context->dbh;
     $data{'userid'} = '' unless $data{'password'};
     $data{'password'} = md5_base64( $data{'password'} ) if $data{'password'};
-    $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
-    $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} );
-    $data{'dateexpiry'}   = format_date_in_iso( $data{'dateexpiry'} );
+    $data{'dateofbirth'}  = format_date_in_iso( $data{'dateofbirth'} );
+    $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'});
+    $data{'dateexpiry'}   = format_date_in_iso( $data{'dateexpiry'}  );
     my $query =
         "insert into borrowers set cardnumber="
       . $dbh->quote( $data{'cardnumber'} )
@@ -706,7 +734,7 @@ sub AddMember {
       . ",B_phone="
       . $dbh->quote( $data{'B_phone'} )
       . ",B_email="
-      . $dbh->quote( $data{'B_email'}, )
+      . $dbh->quote( $data{'B_email'} )
       . ",password="
       . $dbh->quote( $data{'password'} )
       . ",userid="
@@ -1089,17 +1117,15 @@ sub GetMemberAccountRecords {
     my @acctlines;
     my $numlines = 0;
     my $strsth      = qq(
-SELECT * 
-FROM accountlines 
-WHERE borrowernumber=?);
+                        SELECT * 
+                        FROM accountlines 
+                        WHERE borrowernumber=?);
     my @bind = ($borrowernumber);
     if ($date && $date ne ''){
-    $strsth.="
-AND date < ? ";
-    push(@bind,$date);
+            $strsth.=" AND date < ? ";
+            push(@bind,$date);
     }
-    $strsth.="
-ORDER BY date desc,timestamp DESC";
+    $strsth.=" ORDER BY date desc,timestamp DESC";
     my $sth= $dbh->prepare( $strsth );
     $sth->execute( @bind );
     my $total = 0;
@@ -1133,16 +1159,15 @@ sub GetBorNotifyAcctRecord {
     my $dbh = C4::Context->dbh;
     my @acctlines;
     my $numlines = 0;
-    my $query    = qq| SELECT * 
-                       FROM accountlines 
-                       WHERE borrowernumber=? 
-                       AND notify_id=? 
-                       AND (accounttype='FU' OR accounttype='N' OR accounttype='M'OR accounttype='A'OR accounttype='F'OR accounttype='L' OR accounttype='IP' OR accounttype='CH' OR accounttype='RE' OR accounttype='RL')
-                       AND amountoutstanding != '0' 
-                       ORDER BY notify_id,accounttype
-               |;
-    my $sth = $dbh->prepare($query);
-
+    my $sth = $dbh->prepare(
+            "SELECT * 
+                FROM accountlines 
+                WHERE borrowernumber=? 
+                    AND notify_id=? 
+                    AND (accounttype='FU' OR accounttype='N' OR accounttype='M'OR accounttype='A'OR accounttype='F'OR accounttype='L' OR accounttype='IP' OR accounttype='CH' OR accounttype='RE' OR accounttype='RL')
+                    AND amountoutstanding != '0' 
+                ORDER BY notify_id,accounttype
+                ");
     $sth->execute( $borrowernumber, $notifyid );
     my $total = 0;
     while ( my $data = $sth->fetchrow_hashref ) {
@@ -1206,11 +1231,16 @@ sub checkuniquemember {
 }
 
 sub checkcardnumber {
-       my ($cardnumber) = @_;
+       my ($cardnumber,$borrowernumber) = @_;
        my $dbh = C4::Context->dbh;
        my $query = "SELECT * FROM borrowers WHERE cardnumber=?";
-       my $sth = $dbh->prepare($query);
-       $sth->execute($cardnumber);
+       $query .= " AND borrowernumber <> ?" if ($borrowernumber);
+  my $sth = $dbh->prepare($query);
+  if ($borrowernumber) {
+   $sth->execute($cardnumber,$borrowernumber);
+  } else { 
+        $sth->execute($cardnumber);
+  } 
        if (my $data= $sth->fetchrow_hashref()){
                return 1;
        }