bug 2527: avoid targeting of items on hold shelf
[koha.git] / C4 / Bookfund.pm
old mode 100755 (executable)
new mode 100644 (file)
index a86841b..8e9c95f
@@ -17,15 +17,14 @@ package C4::Bookfund;
 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 # Suite 330, Boston, MA  02111-1307 USA
 
-# $Id$
 
 use strict;
-
+# use Smart::Comments;
 
 use vars qw($VERSION @ISA @EXPORT);
 
 # set the version for version checking
-$VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
+$VERSION = 3.00;
 
 =head1 NAME
 
@@ -139,9 +138,9 @@ sub GetBookFunds {
     if ( $branch ne '' ) {
         $strsth = "
         SELECT *
-        FROM   aqbookfund,aqbudget
-        WHERE  aqbookfund.bookfundid=aqbudget.bookfundid
-            AND startdate<now()
+        FROM   aqbookfund
+        LEFT JOIN aqbudget ON aqbookfund.bookfundid=aqbudget.bookfundid
+        WHERE  startdate<now()
             AND enddate>now()
             AND (aqbookfund.branchcode='' OR aqbookfund.branchcode= ? )
       GROUP BY aqbookfund.bookfundid ORDER BY bookfundname";
@@ -149,10 +148,9 @@ sub GetBookFunds {
     else {
         $strsth = "
             SELECT *
-            FROM   aqbookfund,
-                   aqbudget
-            WHERE aqbookfund.bookfundid=aqbudget.bookfundid
-                AND startdate<now()
+            FROM   aqbookfund
+            LEFT JOIN aqbudget ON aqbookfund.bookfundid=aqbudget.bookfundid
+            WHERE startdate<now()
                 AND enddate>now()
             GROUP BY aqbookfund.bookfundid ORDER BY bookfundname
         ";
@@ -229,7 +227,7 @@ sub GetBookFundBreakdown {
         FROM   aqorders
         LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
         LEFT JOIN aqbookfund ON (aqorderbreakdown.bookfundid=aqbookfund.bookfundid and aqorderbreakdown.branchcode=aqbookfund.branchcode)
-        LEFT JOIN aqbudget ON (aqbudget.bookfundid=aqbookfund.bookfundid and aqbudget.branchcode=aqbudget.branchcode)
+        LEFT JOIN aqbudget ON (aqbudget.bookfundid=aqbookfund.bookfundid and aqbudget.branchcode=aqbookfund.branchcode)
         WHERE  aqorderbreakdown.bookfundid=?
             AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
             AND ((budgetdate >= ? and budgetdate < ?) OR (startdate>=? and enddate<=?))
@@ -274,7 +272,7 @@ sub GetBookFundBreakdown {
 
     while ( my $data = $sth->fetchrow_hashref ) {
         my $left = $data->{'tleft'};
-        if ( (!$left && (!$data->{'datereceived'}||$data->{'datereceived'} eq '0000-00-00') ) || $left eq '' ) {
+        if ( !$left || $left eq '' ) {
             $left = $data->{'quantity'};
         }
         if ( $left && $left > 0 ) {
@@ -317,33 +315,39 @@ sub NewBookFund{
 
 =head3 ModBookFund
 
-&ModBookFund($bookfundname,$branchcode,$bookfundid);
-this function update the bookfundname and the branchcode on aqbookfund table
-on database.
+&ModBookFund($bookfundname,$bookfundid,$current_branch, $branchcode)
+
+This function updates the bookfundname and the branchcode in the aqbookfund table.
 
 =cut
 
+# FIXME: use placeholders,  ->prepare(), ->execute()
+
 sub ModBookFund {
-    my ($bookfundname,$bookfundid,$branchcode) = @_;
+    my ($bookfundname,$bookfundid,$current_branch, $branchcode) = @_;
+
     my $dbh = C4::Context->dbh;
-    my $query = "
-        UPDATE aqbookfund
-        SET    bookfundname = ?
-        WHERE  bookfundid = ?
-        AND branchcode= ?
-    ";
-    warn "name : $bookfundname";
-    my $sth=$dbh->prepare($query);
-    $sth->execute($bookfundname,$bookfundid,"$branchcode");
-# budgets depending on a bookfund must have the same branchcode
-# if the bookfund branchcode is set
-    if (defined $branchcode) {
-        $query = "
-            UPDATE aqbudget
-            SET branchcode = ?
-        ";
-        $sth=$dbh->prepare($query);
-        $sth->execute($branchcode);
+
+    my $retval = $dbh->do("
+     UPDATE aqbookfund
+        SET    bookfundname = '$bookfundname', 
+               branchcode = '$branchcode'
+        WHERE  bookfundid = '$bookfundid'
+        AND branchcode = '$current_branch'
+    ");
+
+    ### $retval
+
+    # budgets depending on a bookfund must have the same branchcode
+
+    # if the bookfund branchcode is set, and previous update is successfull, then update aqbudget.branchcode too.
+    if (defined $branchcode && $retval > 0) {
+        my $query = "UPDATE  aqbudget  
+            SET     branchcode = ?
+            WHERE   bookfundid = ? ";
+
+        my $sth=$dbh->prepare($query);
+        $sth->execute($branchcode, $bookfundid) ;
     }
 }
 
@@ -447,7 +451,7 @@ sub Countbookfund {
         AND   branchcode = ?
     ";
     my $sth = $dbh->prepare($query);
-    $sth->execute($bookfundid,$branchcode);
+    $sth->execute($bookfundid,"$branchcode");
     return $sth->fetchrow;
 }