New set of routines for HEAD.
[koha.git] / C4 / Members.pm
index a2ca438..4d5d31d 100644 (file)
@@ -54,17 +54,43 @@ 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
-  &checkuniquemember &getzipnamecity &getidcity &getguarantordata &getcategorytype
-  &calcexpirydate &checkuserpassword
-  &getboracctrecord
-  &borrowercategories &getborrowercategory
-  &fixEthnicity
-  &ethnicitycategories get_institutions add_member_orgs
-  &get_age
+&allissues
+&borrdata 
+&borrdata2 
+&BornameSearch 
+&borrissues
+&borrowercategories 
+
+&changepassword  
+&checkuniquemember 
+&calcexpirydate 
+&checkuserpassword
+&ethnicitycategories get_institutions add_member_orgs
+&fixEthnicity
+&fixup_cardnumber 
+&findguarantees 
+&findguarantor 
+
+
+
+&getmember 
+&getzipnamecity 
+&getidcity 
+&getguarantordata 
+&getcategorytype
+&getboracctrecord
+&getborrowercategory
+&get_age 
+&GetBorrowersFromSurname 
+&GetBranchCodeFromBorrowers
+&GetFlagsAndBranchFromBorrower
+&GuarantornameSearch 
+
+&NewBorrowerNumber   
+&modmember 
+&newmember 
 );
 
 
@@ -166,6 +192,37 @@ the C<borrowers> table in the Koha database.
 
 =cut
 
+=head3 GetFlagsAndBranchFromBorrower
+
+=over 4
+
+($flags, $homebranch) = GetFlagsAndBranchFromBorrower($loggedinuser);
+
+this function read on the database to get flags and homebranch for a user
+given on input arg.
+
+return : 
+it returns the $flags & the homebranch in scalar context.
+
+=back
+
+=cut
+
+sub GetFlagsAndBranchFromBorrower {
+    my $loggedinuser = @_;
+    my $dbh = C4::Context->dbh;
+    my $query = "
+       SELECT flags, branchcode
+       FROM   borrowers
+       WHERE  borrowernumber = ? 
+    ";
+    my $sth = $dbh->prepare($query);
+    $sth->execute($loggedinuser);
+
+    return $sth->fetchrow;
+}
+
+
 #'
 sub getmember {
     my ( $cardnumber, $bornum ) = @_;
@@ -305,7 +362,7 @@ 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 '');
@@ -398,9 +455,8 @@ sub modmember {
        # is adult check guarantees;
                updateguarantees(%data);
        
-       }       
+       }
 
-       
 
 }
 
@@ -412,7 +468,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="
@@ -465,16 +521,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="
@@ -497,10 +553,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="
@@ -515,8 +571,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 {
@@ -869,6 +925,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
@@ -886,8 +943,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 );
 }
 
@@ -1026,7 +1110,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;
@@ -1088,14 +1172,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;
 
@@ -1134,10 +1218,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
 
@@ -1146,13 +1230,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();
@@ -1277,4 +1363,43 @@ 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);
+}
+
+END { }       # module clean-up code here (global destructor)
+
 1;