separating additional authors with - instead of a ,
[koha.git] / C4 / Accounts2.pm
index bbec11d..06d485b 100755 (executable)
@@ -37,7 +37,7 @@ C4::Accounts - Functions for dealing with Koha accounts
 
 =head1 SYNOPSIS
 
-  use C4::Accounts;
+  use C4::Accounts2;
 
 =head1 DESCRIPTION
 
@@ -81,7 +81,6 @@ sub recordpayment{
   #here we update both the accountoffsets and the account lines
   my ($env,$bornumber,$data)=@_;
   my $dbh = C4::Context->dbh;
-  my $updquery = "";
   my $newamtos = 0;
   my $accdata = "";
   my $branch=$env->{'branchcode'};
@@ -89,40 +88,35 @@ sub recordpayment{
   # begin transaction
   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
   # get lines with outstanding amounts to offset
-  my $query = "select * from accountlines
-  where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
-  order by date";
-  my $sth = $dbh->prepare($query);
-  $sth->execute;
+  my $sth = $dbh->prepare("select * from accountlines
+  where (borrowernumber = ?) and (amountoutstanding<>0)
+  order by date");
+  $sth->execute($bornumber);
   # offset transactions
   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
      if ($accdata->{'amountoutstanding'} < $amountleft) {
         $newamtos = 0;
-       $amountleft = $amountleft - $accdata->{'amountoutstanding'};
+       $amountleft -= $accdata->{'amountoutstanding'};
      }  else {
         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
        $amountleft = 0;
      }
      my $thisacct = $accdata->{accountno};
-     $updquery = "update accountlines set amountoutstanding= '$newamtos'
-     where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
-     my $usth = $dbh->prepare($updquery);
-     $usth->execute;
+     my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
+     where (borrowernumber = ?) and (accountno=?)");
+     $usth->execute($newamtos,$bornumber,$thisacct);
      $usth->finish;
-     $updquery = "insert into accountoffsets
+     $usth = $dbh->prepare("insert into accountoffsets
      (borrowernumber, accountno, offsetaccount,  offsetamount)
-     values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
-     $usth = $dbh->prepare($updquery);
-     $usth->execute;
+     values (?,?,?,?)");
+     $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
      $usth->finish;
   }
   # create new line
-  $updquery = "insert into accountlines
+  my $usth = $dbh->prepare("insert into accountlines
   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
-  values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
-  'Pay',0-$amountleft)";
-  my $usth = $dbh->prepare($updquery);
-  $usth->execute;
+  values (?,?,now(),?,'Payment,thanks','Pay',?)");
+  $usth->execute($bornumber,$nextaccntno,0-$data,0-$amountleft);
   $usth->finish;
   UpdateStats($env,$branch,'payment',$data,'','','',$bornumber);
   $sth->finish;
@@ -155,34 +149,36 @@ sub makepayment{
   # begin transaction
   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
   my $newamtos=0;
-  my $sel="Select * from accountlines where  borrowernumber=$bornumber and
-  accountno=$accountno";
-  my $sth=$dbh->prepare($sel);
-  $sth->execute;
+  my $sth=$dbh->prepare("Select * from accountlines where  borrowernumber=? and accountno=?");
+  $sth->execute($bornumber,$accountno);
   my $data=$sth->fetchrow_hashref;
   $sth->finish;
-  # FIXME - This prepare/execute/finish sequence could be done with
-  # $dbh->do(), no?
-  my $updquery="Update accountlines set amountoutstanding=0 where
-  borrowernumber=$bornumber and accountno=$accountno";
-  $sth=$dbh->prepare($updquery);
-  $sth->execute;
-  $sth->finish;
+
+  $dbh->do(<<EOT);
+       UPDATE  accountlines
+       SET     amountoutstanding = 0
+       WHERE   borrowernumber = $bornumber
+         AND   accountno = $accountno
+EOT
+
 #  print $updquery;
-  $updquery = "insert into accountoffsets
-  (borrowernumber, accountno, offsetaccount,  offsetamount)
-  values ($bornumber,$accountno,$nextaccntno,$newamtos)";
-  my $usth = $dbh->prepare($updquery);
-  $usth->execute;
-  $usth->finish;
+  $dbh->do(<<EOT);
+       INSERT INTO     accountoffsets
+                       (borrowernumber, accountno, offsetaccount,
+                        offsetamount)
+       VALUES          ($bornumber, $accountno, $nextaccntno, $newamtos)
+EOT
+
   # create new line
   my $payment=0-$amount;
-  $updquery = "insert into accountlines
-  (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
-  values ($bornumber,$nextaccntno,now(),$payment,'Payment,thanks - $user', 'Pay',0)";
-  $usth = $dbh->prepare($updquery);
-  $usth->execute;
-  $usth->finish;
+  $dbh->do(<<EOT);
+       INSERT INTO     accountlines
+                       (borrowernumber, accountno, date, amount,
+                        description, accounttype, amountoutstanding)
+       VALUES          ($bornumber, $nextaccntno, now(), $payment,
+                       'Payment,thanks - $user', 'Pay', 0)
+EOT
+
   # FIXME - The second argument to &UpdateStats is supposed to be the
   # branch code.
   UpdateStats($env,$user,'payment',$amount,'','','',$bornumber);
@@ -210,11 +206,10 @@ C<$env> is ignored.
 sub getnextacctno {
   my ($env,$bornumber,$dbh)=@_;
   my $nextaccntno = 1;
-  my $query = "select * from accountlines
-  where (borrowernumber = '$bornumber')
-  order by accountno desc";
-  my $sth = $dbh->prepare($query);
-  $sth->execute;
+  my $sth = $dbh->prepare("select * from accountlines
+  where (borrowernumber = ?)
+  order by accountno desc");
+  $sth->execute($bornumber);
   if (my $accdata=$sth->fetchrow_hashref){
     $nextaccntno = $accdata->{'accountno'} + 1;
   }
@@ -232,22 +227,22 @@ sub getnextacctno {
 sub fixaccounts {
   my ($borrowernumber,$accountno,$amount)=@_;
   my $dbh = C4::Context->dbh;
-  my $query="Select * from accountlines where borrowernumber=$borrowernumber
-     and accountno=$accountno";
-  my $sth=$dbh->prepare($query);
-  $sth->execute;
+  my $sth=$dbh->prepare("Select * from accountlines where borrowernumber=?
+     and accountno=?");
+  $sth->execute($borrowernumber,$accountno);
   my $data=$sth->fetchrow_hashref;
        # FIXME - Error-checking
   my $diff=$amount-$data->{'amount'};
   my $outstanding=$data->{'amountoutstanding'}+$diff;
   $sth->finish;
-  # FIXME - Use $dbh->do();
-  $query="Update accountlines set amount='$amount',amountoutstanding='$outstanding' where
-          borrowernumber=$borrowernumber and accountno=$accountno";
-   $sth=$dbh->prepare($query);
-#   print $query;
-   $sth->execute;
-   $sth->finish;
+
+  $dbh->do(<<EOT);
+       UPDATE  accountlines
+       SET     amount = '$amount',
+               amountoutstanding = '$outstanding'
+       WHERE   borrowernumber = $borrowernumber
+         AND   accountno = $accountno
+EOT
  }
 
 # FIXME - Never used, but not exported, either.
@@ -255,18 +250,15 @@ sub returnlost{
   my ($borrnum,$itemnum)=@_;
   my $dbh = C4::Context->dbh;
   my $borrower=borrdata('',$borrnum); #from C4::Search;
-  my $upiss="Update issues set returndate=now() where
-  borrowernumber='$borrnum' and itemnumber='$itemnum' and returndate is null";
-  my $sth=$dbh->prepare($upiss);
-  $sth->execute;
+  my $sth=$dbh->prepare("Update issues set returndate=now() where
+  borrowernumber=? and itemnumber=? and returndate is null");
+  $sth->execute($borrnum,$itemnum);
   $sth->finish;
   my @datearr = localtime(time);
   my $date = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
   my $bor="$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
-  # FIXME - Use $dbh->do();
-  my $upitem="Update items set paidfor='Paid for by $bor $date' where itemnumber='$itemnum'";
-  $sth=$dbh->prepare($upitem);
-  $sth->execute;
+  $sth=$dbh->prepare("Update items set paidfor=? where itemnumber=?");
+  $sth->execute("Paid for by $bor $date",$itemnum);
   $sth->finish;
 }
 
@@ -309,25 +301,24 @@ sub manualinvoice{
     $amountleft=refund('',$bornum,$amount);
   }
   if ($itemnum ne ''){
-    my $sth=$dbh->prepare("Select * from items where barcode='$itemnum'");
-    $sth->execute;
-    my $data=$sth->fetchrow_hashref;
-    $sth->finish;
+#FIXME to use ? before uncommenting
+#     my $sth=$dbh->prepare("Select * from items where barcode='$itemnum'");
+#     $sth->execute;
+#     my $data=$sth->fetchrow_hashref;
+#     $sth->finish;
     $desc.=" ".$itemnum;
-    $desc=$dbh->quote($desc);
-    # FIXME - Use $dbh->do();
-    $insert="insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
-    values ($bornum,$accountno,now(),'$amount',$desc,'$type','$amountleft','$data->{'itemnumber'}')";
+    my $sth=$dbh->prepare("INSERT INTO accountlines
+                       (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber)
+       VALUES (?, ?, now(), ?,?, ?,?,?)");
+#     $sth->execute($bornum, $accountno, $amount, $desc, $type, $amountleft, $data->{'itemnumber'});
+     $sth->execute($bornum, $accountno, $amount, $desc, $type, $amountleft, $itemnum);
   } else {
-      $desc=$dbh->quote($desc);
-    # FIXME - Use $dbh->do();
-    $insert="insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
-    values ($bornum,$accountno,now(),'$amount',$desc,'$type','$amountleft')";
+    $desc=$dbh->quote($desc);
+    my $sth=$dbh->prepare("INSERT INTO accountlines
+                       (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding)
+                       VALUES (?, ?, now(), ?, ?, ?, ?)");
+    $sth->execute($bornum, $accountno, $amount, $desc, $type, $amountleft);
   }
-
-  my $sth=$dbh->prepare($insert);
-  $sth->execute;
-  $sth->finish;
 }
 
 # fixcredit
@@ -339,15 +330,14 @@ sub fixcredit{
   #here we update both the accountoffsets and the account lines
   my ($env,$bornumber,$data,$barcode,$type,$user)=@_;
   my $dbh = C4::Context->dbh;
-  my $updquery = "";
   my $newamtos = 0;
   my $accdata = "";
   my $amountleft = $data;
   if ($barcode ne ''){
     my $item=getiteminformation($env,'',$barcode);
     my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
-    my $query="Select * from accountlines where (borrowernumber='$bornumber'
-    and itemnumber='$item->{'itemnumber'}' and amountoutstanding > 0)";
+    my $query="Select * from accountlines where (borrowernumber=?
+    and itemnumber=? and amountoutstanding > 0)";
     if ($type eq 'CL'){
       $query.=" and (accounttype = 'L' or accounttype = 'Rep')";
     } elsif ($type eq 'CF'){
@@ -358,58 +348,53 @@ sub fixcredit{
     }
 #    print $query;
     my $sth=$dbh->prepare($query);
-    $sth->execute;
+    $sth->execute($bornumber,$item->{'itemnumber'});
     $accdata=$sth->fetchrow_hashref;
     $sth->finish;
     if ($accdata->{'amountoutstanding'} < $amountleft) {
         $newamtos = 0;
-       $amountleft = $amountleft - $accdata->{'amountoutstanding'};
+       $amountleft -= $accdata->{'amountoutstanding'};
      }  else {
         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
        $amountleft = 0;
      }
           my $thisacct = $accdata->{accountno};
-     my $updquery = "update accountlines set amountoutstanding= '$newamtos'
-     where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
-     my $usth = $dbh->prepare($updquery);
-     $usth->execute;
+     my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
+     where (borrowernumber = ?) and (accountno=?)");
+     $usth->execute($newamtos,$bornumber,$thisacct);
      $usth->finish;
-     $updquery = "insert into accountoffsets
+     $usth = $dbh->prepare("insert into accountoffsets
      (borrowernumber, accountno, offsetaccount,  offsetamount)
-     values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
-     $usth = $dbh->prepare($updquery);
-     $usth->execute;
+     values (?,?,?,?)");
+     $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
      $usth->finish;
   }
   # begin transaction
   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
   # get lines with outstanding amounts to offset
-  my $query = "select * from accountlines
-  where (borrowernumber = '$bornumber') and (amountoutstanding >0)
-  order by date";
-  my $sth = $dbh->prepare($query);
-  $sth->execute;
+  my $sth = $dbh->prepare("select * from accountlines
+  where (borrowernumber = ?) and (amountoutstanding >0)
+  order by date");
+  $sth->execute($bornumber);
 #  print $query;
   # offset transactions
   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
      if ($accdata->{'amountoutstanding'} < $amountleft) {
         $newamtos = 0;
-       $amountleft = $amountleft - $accdata->{'amountoutstanding'};
+       $amountleft -= $accdata->{'amountoutstanding'};
      }  else {
         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
        $amountleft = 0;
      }
      my $thisacct = $accdata->{accountno};
-     $updquery = "update accountlines set amountoutstanding= '$newamtos'
-     where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
-     my $usth = $dbh->prepare($updquery);
-     $usth->execute;
+     my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
+     where (borrowernumber = ?) and (accountno=?)");
+     $usth->execute($newamtos,$bornumber,$thisacct);
      $usth->finish;
-     $updquery = "insert into accountoffsets
+     $usth = $dbh->prepare("insert into accountoffsets
      (borrowernumber, accountno, offsetaccount,  offsetamount)
-     values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
-     $usth = $dbh->prepare($updquery);
-     $usth->execute;
+     values (?,?,?,?)");
+     $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
      $usth->finish;
   }
   $sth->finish;
@@ -426,7 +411,6 @@ sub refund{
   #here we update both the accountoffsets and the account lines
   my ($env,$bornumber,$data)=@_;
   my $dbh = C4::Context->dbh;
-  my $updquery = "";
   my $newamtos = 0;
   my $accdata = "";
 #  my $branch=$env->{'branchcode'};
@@ -435,34 +419,30 @@ sub refund{
   # begin transaction
   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
   # get lines with outstanding amounts to offset
-  my $query = "select * from accountlines
-  where (borrowernumber = '$bornumber') and (amountoutstanding<0)
-  order by date";
-  my $sth = $dbh->prepare($query);
-  $sth->execute;
-#  print $query;
+  my $sth = $dbh->prepare("select * from accountlines
+  where (borrowernumber = ?) and (amountoutstanding<0)
+  order by date");
+  $sth->execute($bornumber);
 #  print $amountleft;
   # offset transactions
   while (($accdata=$sth->fetchrow_hashref) and ($amountleft<0)){
      if ($accdata->{'amountoutstanding'} > $amountleft) {
         $newamtos = 0;
-       $amountleft = $amountleft - $accdata->{'amountoutstanding'};
+       $amountleft -= $accdata->{'amountoutstanding'};
      }  else {
         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
        $amountleft = 0;
      }
 #     print $amountleft;
      my $thisacct = $accdata->{accountno};
-     $updquery = "update accountlines set amountoutstanding= '$newamtos'
-     where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
-     my $usth = $dbh->prepare($updquery);
-     $usth->execute;
+     my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
+     where (borrowernumber = ?) and (accountno=?)");
+     $usth->execute($newamtos,$bornumber,$thisacct);
      $usth->finish;
-     $updquery = "insert into accountoffsets
+     $usth = $dbh->prepare("insert into accountoffsets
      (borrowernumber, accountno, offsetaccount,  offsetamount)
-     values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
-     $usth = $dbh->prepare($updquery);
-     $usth->execute;
+     values (?,?,?,?)");
+     $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
      $usth->finish;
   }
   $sth->finish;
@@ -473,10 +453,11 @@ END { }       # module clean-up code here (global destructor)
 
 1;
 __END__
+
 =back
 
 =head1 SEE ALSO
 
-L<DBI(3)|DBI>
+DBI(3)
 
 =cut