Bug 6521 Follow up: previous patch breaks autoMemberNum functionality
[koha.git] / C4 / Members.pm
index 9fdb357..4970735 100644 (file)
@@ -45,6 +45,7 @@ BEGIN {
                &Search
                &SearchMember 
                &GetMemberDetails
+        &GetMemberRelatives
                &GetMember
 
                &GetGuarantees 
@@ -212,22 +213,28 @@ sub SearchMember {
             $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 ?)
+        $query.="((surname LIKE ? OR (surname LIKE ? AND surname REGEXP ?)
+                OR firstname  LIKE ? OR (firstname LIKE ? AND firstname REGEXP ?)
+                OR othernames LIKE ? OR (othernames LIKE ? AND othernames REGEXP ?))
         " .
         ($category_type?" AND category_type = ".$dbh->quote($category_type):"");
+        my $regex = '[[:punct:][:space:]]'.$data[0];
         @bind = (
-            "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
-            "$data[0]%", "% $data[0]%"
+            "$data[0]%", "%$data[0]%", $regex, 
+            "$data[0]%", "%$data[0]%", $regex, 
+            "$data[0]%", "%$data[0]%", $regex 
         );
         for ( my $i = 1 ; $i < $count ; $i++ ) {
-            $query = $query . " AND (" . " surname LIKE ? OR surname LIKE ?
-                OR firstname  LIKE ? OR firstname LIKE ?
-                OR othernames LIKE ? OR othernames LIKE ?)";
+            $query = $query . " AND (" . " surname LIKE ? OR (surname LIKE ? AND surname REGEXP ?)
+                OR firstname  LIKE ? OR (firstname LIKE ? AND firstname REGEXP ?)
+                OR othernames LIKE ? OR (othernames LIKE ? AND othernames REGEXP ?))";
+            $regex = '[[:punct:][:space:]]'.$data[$i];
             push( @bind,
-                "$data[$i]%",   "% $data[$i]%", "$data[$i]%",
-                "% $data[$i]%", "$data[$i]%",   "% $data[$i]%" );
+              "$data[$i]%", "%$data[$i]%", $regex,
+              "$data[$i]%", "%$data[$i]%", $regex,
+              "$data[$i]%", "%$data[$i]%", $regex
+            );
+
 
             # FIXME - .= <<EOT;
         }
@@ -582,6 +589,46 @@ sub GetMember {
     return;
 }
 
+=head2 GetMemberRelatives
+
+ @borrowernumbers = GetMemberRelatives($borrowernumber);
+
+ C<GetMemberRelatives> returns a borrowersnumber's list of guarantor/guarantees of the member given in parameter
+
+=cut 
+sub GetMemberRelatives {
+    my $borrowernumber = shift;
+    my $dbh = C4::Context->dbh;
+    my @glist;
+
+    # Getting guarantor
+    my $query = "SELECT guarantorid FROM borrowers WHERE borrowernumber=?";
+    my $sth = $dbh->prepare($query);
+    $sth->execute($borrowernumber);
+    my $data = $sth->fetchrow_arrayref();
+    push @glist, $data->[0] if $data->[0];
+    my $guarantor = $data->[0] if $data->[0];
+
+    # Getting guarantees
+    $query = "SELECT borrowernumber FROM borrowers WHERE guarantorid=?";
+    $sth = $dbh->prepare($query);
+    $sth->execute($borrowernumber);
+    while ($data = $sth->fetchrow_arrayref()) {
+       push @glist, $data->[0];
+    }
+
+    # Getting sibling guarantees
+    if ($guarantor) {
+        $query = "SELECT borrowernumber FROM borrowers WHERE guarantorid=?";
+        $sth = $dbh->prepare($query);
+        $sth->execute($guarantor);
+        while ($data = $sth->fetchrow_arrayref()) {
+           push @glist, $data->[0] if ($data->[0] != $borrowernumber);
+        }
+    }
+
+    return @glist;
+}
 
 =head2 IsMemberBlocked
 
@@ -753,7 +800,7 @@ Returns the borrowernumber
 sub AddMember {
     my (%data) = @_;
     my $dbh = C4::Context->dbh;
-    $data{'password'} = '!' if (not $data{'password'} and $data{'userid'});
+    $data{'userid'} = '' unless $data{'password'};
     $data{'password'} = md5_base64( $data{'password'} ) if $data{'password'};
        $data{'borrowernumber'}=InsertInTable("borrowers",\%data);      
     # mysql_insertid is probably bad.  not necessarily accurate and mysql-specific at best.
@@ -795,7 +842,7 @@ sub Generate_Userid {
   do {
     $firstname =~ s/[[:digit:][:space:][:blank:][:punct:][:cntrl:]]//g;
     $surname =~ s/[[:digit:][:space:][:blank:][:punct:][:cntrl:]]//g;
-    $newuid = lc("$firstname.$surname");
+    $newuid = lc(($firstname)? "$firstname.$surname" : $surname);
     $newuid .= $offset unless $offset == 0;
     $offset++;
 
@@ -963,7 +1010,7 @@ sub UpdateGuarantees {
 }
 =head2 GetPendingIssues
 
-  my $issues = &GetPendingIssues($borrowernumber);
+  my $issues = &GetPendingIssues(@borrowernumber);
 
 Looks up what the patron with the given borrowernumber has borrowed.
 
@@ -976,14 +1023,22 @@ The keys include C<biblioitems> fields except marc and marcxml.
 
 #'
 sub GetPendingIssues {
-    my ($borrowernumber) = @_;
+    my (@borrowernumbers) = @_;
+
+    # Borrowers part of the query
+    my $bquery = '';
+    for (my $i = 0; $i < @borrowernumbers; $i++) {
+        $bquery .= " borrowernumber = ?";
+        $bquery .= " OR" if ($i < (scalar(@borrowernumbers) - 1));
+    }
+
     # must avoid biblioitems.* to prevent large marc and marcxml fields from killing performance
     # FIXME: namespace collision: each table has "timestamp" fields.  Which one is "timestamp" ?
     # FIXME: circ/ciculation.pl tries to sort by timestamp!
     # FIXME: C4::Print::printslip tries to sort by timestamp!
     # FIXME: namespace collision: other collisions possible.
     # FIXME: most of this data isn't really being used by callers.
-    my $sth = C4::Context->dbh->prepare(
+    my $query =
    "SELECT issues.*,
             items.*,
            biblio.*,
@@ -1000,16 +1055,19 @@ sub GetPendingIssues {
            biblioitems.url,
            issues.timestamp AS timestamp,
            issues.renewals  AS renewals,
+           issues.borrowernumber AS borrowernumber,
             items.renewals  AS totalrenewals
     FROM   issues
     LEFT JOIN items       ON items.itemnumber       =      issues.itemnumber
     LEFT JOIN biblio      ON items.biblionumber     =      biblio.biblionumber
     LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
     WHERE
-      borrowernumber=?
+      $bquery
     ORDER BY issues.issuedate"
-    );
-    $sth->execute($borrowernumber);
+    ;
+
+    my $sth = C4::Context->dbh->prepare($query);
+    $sth->execute(@borrowernumbers);
     my $data = $sth->fetchall_arrayref({});
     my $today = C4::Dates->new->output('iso');
     foreach (@$data) {
@@ -1203,6 +1261,8 @@ sub checkuniquemember {
 
 sub checkcardnumber {
     my ($cardnumber,$borrowernumber) = @_;
+    # If cardnumber is null, we assume they're allowed.
+    return 0 if !defined($cardnumber);
     my $dbh = C4::Context->dbh;
     my $query = "SELECT * FROM borrowers WHERE cardnumber=?";
     $query .= " AND borrowernumber <> ?" if ($borrowernumber);