Code cleaning :
[koha.git] / C4 / Members.pm
index f350c64..761a4c6 100644 (file)
@@ -54,7 +54,6 @@ This module contains routines for adding, modifying and deleting members/patrons
 #'
 
 @ISA    = qw(Exporter);
-@EXPORT = qw();
 
 @EXPORT = qw(
  &BornameSearch &getmember &borrdata &borrdata2 &fixup_cardnumber &findguarantees &findguarantor &GuarantornameSearch &NewBorrowerNumber   &modmember &newmember &changepassword &borrissues &allissues
@@ -64,7 +63,7 @@ This module contains routines for adding, modifying and deleting members/patrons
   &borrowercategories &getborrowercategory
   &fixEthnicity
   &ethnicitycategories get_institutions add_member_orgs
-  &get_age
+  &get_age &GetBorrowersFromSurname &GetBranchCodeFromBorrowers
 );
 
 
@@ -305,8 +304,9 @@ sub modmember {
        $data{'dateofbirth'}=format_date_in_iso($data{'dateofbirth'});
        $data{'dateexpiry'}=format_date_in_iso($data{'dateexpiry'});
        $data{'dateenrolled'}=format_date_in_iso($data{'dateenrolled'});
-       warn "la date:".$data{dateenrolled};
+#      warn "num user".$data{'borrowernumber'};
        my $query;
+       my $sth;
        $data{'userid'}='' if ($data{'password'}eq '');
        # test to know if u must update or not the borrower password
        if ($data{'password'} eq '****'){
@@ -318,27 +318,10 @@ sub modmember {
                B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
                categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
                contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship =  ?,ethnicity = ?,
-               ethnotes = ?,sex = ?,password = ?,flags = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ? 
-               WHERE borrowernumber=$data{'borrowernumber'}";
-       }
-       else{
-               
-               ($data{'password'}=md5_base64($data{'password'})) if ($data{'password'} ne '');
-               $query="UPDATE borrowers SET 
-               cardnumber  = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
-               streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
-               email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
-               B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
-               categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
-               contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship =  ?,ethnicity = ?,
-               ethnotes = ?,sex = ?,password = ?,flags = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ? 
+               ethnotes = ?,sex = ?,flags = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ? 
                WHERE borrowernumber=$data{'borrowernumber'}";
-       
-       }
-       my $sth=$dbh->prepare($query);
-
-       if ($data{'password'} eq '****'){
-               $sth->execute(
+       $sth=$dbh->prepare($query);
+       $sth->execute(
                $data{'cardnumber'},$data{'surname'},
                $data{'firstname'},$data{'title'},
                $data{'othernames'},$data{'initials'},
@@ -359,14 +342,25 @@ sub modmember {
                $data{'contacttitle'},$data{'guarantorid'},
                $data{'borrowernotes'},$data{'relationship'},
                $data{'ethnicity'},$data{'ethnotes'},
-               $data{'sex'},$data{'password'},
+               $data{'sex'},
                $data{'flags'},$data{'userid'},
                $data{'opacnote'},$data{'contactnote'},
-               $data{'sort1'},$data{'sort2'}
-               );
-       
-       }else{
-               $sth->execute(
+               $data{'sort1'},$data{'sort2'});
+       }
+       else{
+               
+               ($data{'password'}=md5_base64($data{'password'})) if ($data{'password'} ne '');
+               $query="UPDATE borrowers SET 
+               cardnumber  = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
+               streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
+               email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
+               B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
+               categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
+               contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship =  ?,ethnicity = ?,
+               ethnotes = ?,sex = ?,password = ?,flags = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ? 
+               WHERE borrowernumber=$data{'borrowernumber'}";
+       $sth=$dbh->prepare($query);
+       $sth->execute(
                $data{'cardnumber'},$data{'surname'},
                $data{'firstname'},$data{'title'},
                $data{'othernames'},$data{'initials'},
@@ -393,8 +387,6 @@ sub modmember {
                $data{'sort1'},$data{'sort2'}
                );
        }
-
-       $sth->execute;
        $sth->finish;
        # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
        # so when we update information for an adult we should check for guarantees and update the relevant part
@@ -405,9 +397,8 @@ sub modmember {
        # is adult check guarantees;
                updateguarantees(%data);
        
-       }       
+       }
 
-       
 
 }
 
@@ -419,7 +410,7 @@ sub newmember {
     $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 =
+       my $query =
         "insert into borrowers set cardnumber="
       . $dbh->quote( $data{'cardnumber'} )
       . ",surname="
@@ -472,16 +463,16 @@ sub newmember {
       . $dbh->quote( $data{'dateexpiry'} )
       . ",contactnote="
       . $dbh->quote( $data{'contactnote'} )
-      . ",b_address="
-      . $dbh->quote( $data{'b_address'} )
-      . ",b_zipcode="
-      . $dbh->quote( $data{'b_zipcode'} )
-      . ",b_city="
-      . $dbh->quote( $data{'b_city'} )
-      . ",b_phone="
-      . $dbh->quote( $data{'b_phone'} )
-      . ",b_email="
-      . $dbh->quote( $data{'b_email'}, )
+      . ",B_address="
+      . $dbh->quote( $data{'B_address'} )
+      . ",B_zipcode="
+      . $dbh->quote( $data{'B_zipcode'} )
+      . ",B_city="
+      . $dbh->quote( $data{'B_city'} )
+      . ",B_phone="
+      . $dbh->quote( $data{'B_phone'} )
+      . ",B_email="
+      . $dbh->quote( $data{'B_email'}, )
       . ",password="
       . $dbh->quote( $data{'password'} )
       . ",userid="
@@ -504,10 +495,10 @@ sub newmember {
       . $dbh->quote( $data{'flags'} )
       . ",relationship="
       . $dbh->quote( $data{'relationship'} )
-      . ",b_streetnumber="
-      . $dbh->quote( $data{'b_streetnumber'}) 
-      . ",b_streettype="
-      . $dbh->quote( $data{'b_streettype'})
+      . ",B_streetnumber="
+      . $dbh->quote( $data{'B_streetnumber'}) 
+      . ",B_streettype="
+      . $dbh->quote( $data{'B_streettype'})
       . ",gonenoaddress="
       . $dbh->quote( $data{'gonenoaddress'})   
       . ",lost="
@@ -522,8 +513,8 @@ sub newmember {
     my $sth = $dbh->prepare($query);
     $sth->execute;
     $sth->finish;
-    $data{'borrowerid'} = $dbh->{'mysql_insertid'};
-    return $data{'borrowerid'};
+    $data{'borrowernumber'} = $dbh->{'mysql_insertid'};
+    return $data{'borrowernumber'};
 }
 
 sub changepassword {
@@ -876,6 +867,7 @@ sub allissues {
 
     #FIXME: sanity-check order and limit
     my $dbh   = C4::Context->dbh;
+    my $count=0;
     my $query = "Select * from issues,biblio,items,biblioitems
   where borrowernumber=? and
   items.biblioitemnumber=biblioitems.biblioitemnumber and
@@ -893,8 +885,35 @@ sub allissues {
     while ( my $data = $sth->fetchrow_hashref ) {
         $result[$i] = $data;
         $i++;
+       $count++;
+    }
+
+    # get all issued items for bornum from oldissues table
+    # large chunk of older issues data put into table oldissues
+    # to speed up db calls for issuing items
+    if(C4::Context->preference("ReadingHistory")){
+          my $query2="SELECT * FROM oldissues,biblio,items,biblioitems
+                      WHERE borrowernumber=? 
+                      AND items.biblioitemnumber=biblioitems.biblioitemnumber
+                      AND items.itemnumber=oldissues.itemnumber
+                      AND items.biblionumber=biblio.biblionumber
+                      ORDER BY $order";
+          if ($limit !=0){
+                $limit=$limit-$count;
+                $query2.=" limit $limit";
+          }
+
+          my $sth2=$dbh->prepare($query2);
+          $sth2->execute($bornum);
+
+          while (my $data2=$sth2->fetchrow_hashref){
+                $result[$i]=$data2;
+                $i++;
+          }
+          $sth2->finish;
     }
     $sth->finish;
+
     return ( $i, \@result );
 }
 
@@ -1033,7 +1052,7 @@ sub getguarantordata {
     my $dbh          = C4::Context->dbh;
     my $sth          =
       $dbh->prepare(
-"Select title,firstname,surname,streetnumber,address,streettype,address2,zipcode,city,phone,phonepro,mobile,email,emailpro  from borrowers where borrowernumber =? "
+"Select title,firstname,surname,streetnumber,address,streettype,address2,zipcode,city,phone,phonepro,mobile,email,emailpro,fax  from borrowers where borrowernumber =? "
       );
     $sth->execute($borrowerid);
     my $guarantor_data = $sth->fetchrow_hashref;
@@ -1095,14 +1114,14 @@ return the number of record
 =cut
 
 sub checkuserpassword {
-    my ( $borrowerid, $userid, $password ) = @_;
+    my ( $borrowernumber, $userid, $password ) = @_;
     $password = md5_base64($password);
     my $dbh = C4::Context->dbh;
     my $sth =
       $dbh->prepare(
 "Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? "
       );
-    $sth->execute( $borrowerid, $userid, $password );
+    $sth->execute( $borrowernumber, $userid, $password );
     my $number_rows = $sth->fetchrow;
     return $number_rows;
 
@@ -1141,10 +1160,10 @@ sub borrowercategories {
 
 =head2 getborrowercategory
 
-  $description = &getborrowercategory($categorycode);
+  $description,$dateofbirthrequired,$upperagelimit,$category_type = &getborrowercategory($categorycode);
 
 Given the borrower's category code, the function returns the corresponding
-description for a comprehensive information display.
+description , dateofbirthrequired , upperagelimit and category type for a comprehensive information display.
 
 =cut
 
@@ -1153,13 +1172,15 @@ sub getborrowercategory {
     my $dbh       = C4::Context->dbh;
     my $sth       =
       $dbh->prepare(
-        "SELECT description FROM categories WHERE categorycode = ?");
+        "SELECT description,dateofbirthrequired,upperagelimit,category_type FROM categories WHERE categorycode = ?");
     $sth->execute($catcode);
-    my $description = $sth->fetchrow();
+    my ($description,$dateofbirthrequired,$upperagelimit,$category_type) = $sth->fetchrow();
     $sth->finish();
-    return $description;
+    return ($description,$dateofbirthrequired,$upperagelimit,$category_type);
 }    # sub getborrowercategory
 
+
+
 =head2 ethnicitycategories
 
   ($codes_arrayref, $labels_hashref) = &ethnicitycategories();
@@ -1284,4 +1305,66 @@ sub add_member_orgs {
     $sth->finish();
 
 }    # sub add_member_orgs
+
+=head2 GetBorrowersFromSurname
+
+=over 4
+
+\@resutlts = GetBorrowersFromSurname($surname)
+this function get the list of borrower names like $surname.
+return :
+the table of results in @results
+
+=back
+
+=cut
+sub GetBorrowersFromSurname  {
+    my ($searchstring)=@_;
+    my $dbh = C4::Context->dbh;
+    $searchstring=~ s/\'/\\\'/g;
+    my @data=split(' ',$searchstring);
+    my $count=@data;
+    my $query = qq|
+        SELECT   surname,firstname
+        FROM     borrowers
+        WHERE    (surname like ?)
+        ORDER BY surname
+    |;
+    my $sth=$dbh->prepare($query);
+    $sth->execute("$data[0]%");
+    my @results;
+    my $count = 0;
+    while (my $data=$sth->fetchrow_hashref){
+         push(@results,$data);
+         $count++;
+    }
+     $sth->finish;
+     return ($count,\@results);
+}
+
+=head2 GetBranchCodeFromBorrowers
+
+=over 4
+
+$sth = GetBranchCodeFromBorrowers();
+
+this function just prepare the SQL request.
+After this function, don't forget to execute it by using $sth->execute($borrowernumber)
+return :
+$sth = $dbh->prepare($query).
+
+=back
+
+=cut
+sub GetBranchCodeFromBorrowers {
+    my $dbh = C4::Context->dbh;
+    my $query = qq|
+        SELECT flags, branchcode
+        FROM   borrowers
+        WHERE  borrowernumber = ?
+    |;
+    return $dbh->prepare($query);
+}
+END { }       # module clean-up code here (global destructor)
+
 1;