Starting on C4::Query,
[koha.git] / C4 / Circulation / Borrower.pm
index 6675643..1921076 100755 (executable)
@@ -1,4 +1,6 @@
-package C4::Circulation::Borrower; #assumes C4/Circulation/Borrower
+package C4::Circulation::Borrower;
+
+# $Id$
 
 #package to deal with Issues
 #written 3/11/99 by chris@katipo.co.nz
@@ -21,67 +23,38 @@ package C4::Circulation::Borrower; #assumes C4/Circulation/Borrower
 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 # Suite 330, Boston, MA  02111-1307 USA
 
+# FIXME - This module is never used. Obsolete?
+
 use strict;
 require Exporter;
 use DBI;
-use C4::Database;
+use C4::Context;
 use C4::Accounts;
 use C4::InterfaceCDK;
 use C4::Interface::FlagsCDK;
 use C4::Circulation::Main;
+       # FIXME - C4::Circulation::Main and C4::Circulation::Borrower
+       # use each other, so functions get redefined.
 use C4::Circulation::Issues;
+       # FIXME - C4::Circulation::Issues and C4::Circulation::Borrower
+       # use each other, so functions get redefined.
 use C4::Circulation::Renewals;
 use C4::Scan;
 use C4::Search;
 use C4::Stats;
 use C4::Format;
-use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
-  
+use vars qw($VERSION @ISA @EXPORT);
+
 # set the version for version checking
 $VERSION = 0.01;
-    
+
 @ISA = qw(Exporter);
 @EXPORT = qw(&findborrower &Borenq &findoneborrower &NewBorrowerNumber
 &findguarantees);
-%EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
-                 
-# your exported package globals go here,
-# as well as any optionally exported functions
-
-@EXPORT_OK   = qw($Var1 %Hashit);
-
-
-# non-exported package globals go here
-use vars qw(@more $stuff);
-       
-# initalize package globals, first exported ones
-
-my $Var1   = '';
-my %Hashit = ();
-                   
-# then the others (which are still accessible as $Some::Module::stuff)
-my $stuff  = '';
-my @more   = ();
-       
-# all file-scoped lexicals must be created before
-# the functions below that use them.
-               
-# file-private lexicals go here
-my $priv_var    = '';
-my %secret_hash = ();
-                           
-# here's a file-private function as a closure,
-# callable as &$priv_func;  it cannot be prototyped.
-my $priv_func = sub {
-  # stuff goes here.
-};
-                                                   
-# make all your functions, whether exported or not;
-
 
 sub findborrower  {
   my ($env,$dbh) = @_;
-  C4::InterfaceCDK::helptext('');
+  C4::InterfaceCDK::helptext('');      # FIXME - This looks useless
   C4::InterfaceCDK::clearscreen();
   my $bornum = "";
   my $sth = "";
@@ -93,38 +66,37 @@ sub findborrower  {
     #get borrowerbarcode from scanner
     my $title = C4::InterfaceCDK::titlepanel($env,$env->{'sysarea'},"Borrower Entry");
     if ($env->{'newborrower'} eq "") {
-      ($borcode,$reason,$book)=&C4::Circulation::Main::scanborrower($env); 
-    } else { 
+      ($borcode,$reason,$book)=&C4::Circulation::Main::scanborrower($env);
+    } else {
       $borcode = $env->{'newborrower'};
       $reason = "";
       $book = "";
       $env->{'newborrower'}= "";
-    }  
+    }
     #C4::Circulation::Main
     if ($reason eq "") {
       if ($borcode ne '') {
         ($bornum,$borrower) = findoneborrower($env,$dbh,$borcode);
         $env->{'IssuesAllowed'} = 1;
       } elsif ($book ne "") {
-        my $query = "select * from issues,items where (barcode = '$book') 
-          and (items.itemnumber = issues.itemnumber) 
-          and (issues.returndate is null)";
-        my $iss_sth=$dbh->prepare($query);
-        $iss_sth->execute;
+        my $iss_sth=$dbh->prepare("select * from issues,items where (barcode = ?)
+          and (items.itemnumber = issues.itemnumber)
+          and (issues.returndate is null)");
+        $iss_sth->execute($book);
         if (my $issdata  = $iss_sth->fetchrow_hashref) {
            $bornum=$issdata->{'borrowernumber'};
-          $sth = $dbh->prepare("Select * from borrowers 
+          $sth = $dbh->prepare("Select * from borrowers
             where borrowernumber =  '$bornum'");
           $sth->execute;
           $borrower=$sth->fetchrow_hashref;
-          $sth->finish;  
+          $sth->finish;
          } else {
            error_msg($env,"Item $book not found");
-         } 
+         }
         $iss_sth->finish;
       }
-    } 
-  } 
+    }
+  }
   my ($issuesallowed,$owing);
   if ($reason eq "") {
     $env->{'bornum'} = $bornum;
@@ -146,8 +118,8 @@ sub findoneborrower {
   my $borrower;
   my $ucborcode = uc $borcode;
   my $lcborcode = lc $borcode;
-  my $sth=$dbh->prepare("Select * from borrowers where cardnumber=\"$ucborcode\"");
-  $sth->execute;
+  my $sth=$dbh->prepare("Select * from borrowers where cardnumber=?");
+  $sth->execute($ucborcode);
   if ($borrower=$sth->fetchrow_hashref) {
     $bornum=$borrower->{'borrowernumber'};
     $sth->finish;
@@ -155,11 +127,9 @@ sub findoneborrower {
     $sth->finish;
     # my $borquery = "Select * from borrowers
     # where surname ~* '$borcode' order by surname";
-             
-    my $borquery = "Select * from borrowers 
-      where lower(surname) like \"$lcborcode%\" order by surname,firstname";
-    my $sthb =$dbh->prepare($borquery);
-    $sthb->execute;
+
+    my $sthb =$dbh->prepare("Select * from borrowers where lower(surname) like ? order by surname,firstname");
+    $sthb->execute("$lcborcode%");
     my $cntbor = 0;
     my @borrows;
     my @bornums;
@@ -171,17 +141,16 @@ sub findoneborrower {
       $cntbor++;
     }
     if ($cntbor == 1)  {
-      $bornum = $bornums[0];       
-      my $query = "select * from borrowers where borrowernumber = '$bornum'";     
+      $bornum = $bornums[0];
+      my $query = "select * from borrowers where borrowernumber = '$bornum'";
       $sth = $dbh->prepare($query);
       $sth->execute;
       $borrower =$sth->fetchrow_hashref;
-      $sth->finish;                                             
+      $sth->finish;
     } elsif ($cntbor > 0) {
       my ($cardnum) = C4::InterfaceCDK::selborrower($env,$dbh,\@borrows,\@bornums);
-      my $query = "select * from borrowers where cardnumber = '$cardnum'";   
-      $sth = $dbh->prepare($query);                          
-      $sth->execute;                          
+      $sth = $dbh->prepare("select * from borrowers where cardnumber = ?");
+      $sth->execute($cardnum);
       $borrower =$sth->fetchrow_hashref;
       $sth->finish;
       $bornum=$borrower->{'borrowernumber'};
@@ -189,22 +158,22 @@ sub findoneborrower {
       if ($bornum eq '') {
         error_msg($env,"Borrower not found");
       }
-    }  
+    }
   }
-  return ($bornum,$borrower); 
+  return ($bornum,$borrower);
 }
 sub checktraps {
   my ($env,$dbh,$bornum,$borrower) = @_;
   my $issuesallowed = "1";
   #my @traps_set;
   #check amountowing
-  my $traps_done; 
+  my $traps_done;
   my $odues;
   my $amount;
   while ($traps_done ne "DONE") {
     my @traps_set;
     $amount=C4::Accounts::checkaccount($env,$bornum,$dbh);    #from C4::Accounts
-    if ($amount > 0) { push (@traps_set,"CHARGES");}  
+    if ($amount > 0) { push (@traps_set,"CHARGES");}
     if ($borrower->{'gonenoaddress'} == 1){ push (@traps_set,"GNA");}
     #check if member has a card reported as lost
     if ($borrower->{'lost'} ==1){push (@traps_set,"LOST");}
@@ -213,17 +182,18 @@ sub checktraps {
     #check if borrower has overdue items
     #call overdue checker
     my $odues = &C4::Circulation::Main::checkoverdues($env,$bornum,$dbh);
-    if ($odues > 0) {push (@traps_set,"ODUES");}  
+    if ($odues > 0) {push (@traps_set,"ODUES");}
     #check if borrower has any items waiting
     my ($nowaiting,$itemswaiting) = &C4::Circulation::Main::checkwaiting($env,$dbh,$bornum);
-    if ($nowaiting > 0) { push (@traps_set,"WAITING"); } 
+    if ($nowaiting > 0) { push (@traps_set,"WAITING"); }
+    # FIXME - This should be $traps_set[0], right?
     if (@traps_set[0] ne "" ) {
-      ($issuesallowed,$traps_done,$amount,$odues) = 
+      ($issuesallowed,$traps_done,$amount,$odues) =
          process_traps($env,$dbh,$bornum,$borrower,
         $amount,$odues,\@traps_set,$itemswaiting);
     } else {
       $traps_done = "DONE";
-    }   
+    }
   }
   return ($issuesallowed, $odues,$amount);
 }
@@ -234,7 +204,7 @@ sub process_traps {
   my $x = 0;
   my %traps;
   while (@$traps_set[$x] ne "") {
-    $traps{@$traps_set[$x]} = 1; 
+    $traps{@$traps_set[$x]} = 1;
     $x++;
   }
   my $traps_done;
@@ -244,7 +214,7 @@ sub process_traps {
     $trapact = &trapscreen($env,$bornum,$borrower,$amount,$traps_set);
     if ($trapact eq "CHARGES") {
       C4::Accounts::reconcileaccount($env,$dbh,$bornum,$amount,$borrower,$odues);
-      ($odues,$issues,$amount)=borrdata2($env,$bornum);          
+      ($odues,$issues,$amount)=borrdata2($env,$bornum);
       if ($amount <= 0) {
         $traps{'CHARGES'} = 0;
         my @newtraps;
@@ -276,12 +246,10 @@ sub process_traps {
       }
     } elsif  ($trapact eq "NOTES") {
       my $notes = trapsnotes($env,$bornum,$borrower,$amount);
-      if ($notes ne $borrower->{'borrowernotes'}) { 
-        my $query = "update borrowers set borrowernotes = '$notes' 
-          where borrowernumber = $bornum";
-        my $sth = $dbh->prepare($query);
-       $sth->execute();
-       $sth->finish();
+      if ($notes ne $borrower->{'borrowernotes'}) {
+        my $sth = $dbh->prepare("update borrowers set borrowernotes = ? where borrowernumber = ?");
+               $sth->execute($notes,$bornum);
+               $sth->finish();
         $borrower->{'borrowernotes'} = $notes;
       }
       if ($notes eq "") {
@@ -293,8 +261,8 @@ sub process_traps {
            push @newtraps,$traps_set->[$x];
          }
          $x++;
-        }                 
-        $traps_set = \@newtraps;                                                     
+        }
+        $traps_set = \@newtraps;
       }
     }
     my $notr = @$traps_set;
@@ -318,7 +286,7 @@ sub process_traps {
 
 sub Borenq {
   my ($env)=@_;
-  my $dbh=C4Connect;
+  my $dbh = C4::Context->dbh;
   #get borrower guff
   my $bornum;
   my $issuesallowed;
@@ -337,10 +305,9 @@ sub Borenq {
         $reason = "";
        }
     }
-  $dbh->disconnect;
   }
   return $reason;
-}  
+}
 
 sub modifyuser {
   my ($env,$borrower) = @_;
@@ -350,14 +317,13 @@ sub modifyuser {
 
 sub reserveslist {
   my ($env,$borrower,$amount,$odues,$waiting) = @_;
-  my $dbh=C4Connect;
+  my $dbh = C4::Context->dbh;
   my @items;
   my $x=0;
-  my $query="Select * from reserves where
-  borrowernumber='$borrower->{'borrowernumber'}' and found='W' and
-  cancellationdate is null order by timestamp";
-  my $sth=$dbh->prepare($query);
-  $sth->execute;
+  my $sth=$dbh->prepare("Select * from reserves where
+  borrowernumber=? and found='W' and
+  cancellationdate is null order by timestamp");
+  $sth->execute($borrower->{'borrowernumber'});
   while (my $data=$sth->fetchrow_hashref){
     my $itemdata = itemnodata($env,$dbh,$data->{'itemnumber'});
     if ($itemdata){
@@ -366,27 +332,45 @@ sub reserveslist {
   }
   $sth->finish;
   reservesdisplay($env,$borrower,$amount,$odues,\@items);
-  $dbh->disconnect;
 }
-  
+
+=item NewBorrowerNumber
+
+  $num = &NewBorrowerNumber();
+
+Allocates a new, unused borrower number, and returns it.
+
+=cut
+#'
+# FIXME - This is identical to C4::Search::NewBorrowerNumber.
+# Pick one (preferably this one) and stick with it.
+
+# FIXME - Race condition: this function just says what the next unused
+# number is, but doesn't allocate it. Hence, two clients adding
+# patrons at the same time could get the same new borrower number and
+# clobber each other.
+# A better approach might be to change the database to make
+# borrowers.borrowernumber a unique key and auto_increment. Then, to
+# allocate a new borrower number, use "insert" to create a new record
+# (leaving the database daemon with the job of serializing requests),
+# and use the newly-created record.
+
 sub NewBorrowerNumber {
-  my $dbh=C4Connect;
+  my $dbh = C4::Context->dbh;
   my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
   $sth->execute;
   my $data=$sth->fetchrow_hashref;
   $sth->finish;
   $data->{'max(borrowernumber)'}++;
   return($data->{'max(borrowernumber)'});
-  $dbh->disconnect;
 }
 
 sub findguarantees{
   my ($bornum)=@_;
-  my $dbh=C4Connect;
-  my $query="select cardnumber,borrowernumber from borrowers where 
-  guarantor='$bornum'";
-  my $sth=$dbh->prepare($query);
-  $sth->execute;
+  my $dbh = C4::Context->dbh;
+  my $sth=$dbh->prepare("select cardnumber,borrowernumber from borrowers where
+  guarantor=?");
+  $sth->execute($bornum);
   my @dat;
   my $i=0;
   while (my $data=$sth->fetchrow_hashref){
@@ -394,7 +378,5 @@ sub findguarantees{
     $i++;
   }
   $sth->finish;
-  $dbh->disconnect;
   return($i,\@dat);
 }
-END { }       # module clean-up code here (global destructor)