Bug 6492 - Deleted biblios cause rebuild_zebra to fail
[koha.git] / C4 / Budgets.pm
index 3c3c4c3..d2010d0 100644 (file)
@@ -13,11 +13,12 @@ package C4::Budgets;
 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 use strict;
+#use warnings; FIXME - Bug 2505
 use C4::Context;
 use C4::Dates qw(format_date format_date_in_iso);
 use C4::SQLHelper qw<:all>;
@@ -39,7 +40,9 @@ BEGIN {
         &ModBudget
         &DelBudget
         &GetBudgetSpent
+        &GetBudgetOrdered
         &GetPeriodsCount
+        &GetChildBudgetsSpent
 
            &GetBudgetPeriod
         &GetBudgetPeriods
@@ -74,6 +77,10 @@ BEGIN {
 # ----------------------------BUDGETS.PM-----------------------------";
 
 
+=head1 FUNCTIONS ABOUT BUDGETS
+
+=cut
+
 sub HideCols {
     my ( $authcat, @hide_cols ) = @_;
     my $dbh = C4::Context->dbh;
@@ -300,9 +307,26 @@ sub GetBudgetSpent {
        my ($budget_id) = @_;
        my $dbh = C4::Context->dbh;
        my $sth = $dbh->prepare(qq|
-        SELECT SUM(ecost *  quantity  ) AS sum FROM aqorders
+        SELECT SUM(ecost *  quantity) AS sum FROM aqorders
             WHERE budget_id = ? AND
-            datecancellationprinted IS NULL 
+            quantityreceived > 0 AND
+            datecancellationprinted IS NULL
+    |);
+
+       $sth->execute($budget_id);
+       my $sum =  $sth->fetchrow_array;
+       return $sum;
+}
+
+# -------------------------------------------------------------------
+sub GetBudgetOrdered {
+       my ($budget_id) = @_;
+       my $dbh = C4::Context->dbh;
+       my $sth = $dbh->prepare(qq|
+        SELECT SUM(ecost *  quantity) AS sum FROM aqorders
+            WHERE budget_id = ? AND
+            quantityreceived = 0 AND
+            datecancellationprinted IS NULL
     |);
 
        $sth->execute($budget_id);
@@ -318,6 +342,7 @@ sub GetBudgetPermDropbox {
        $labels{'1'} = 'Owner';
        $labels{'2'} = 'Library';
        my $radio = CGI::scrolling_list(
+               -id       => 'budget_permission',
                -name      => 'budget_permission',
                -values    => [ '0', '1', '2' ],
                -default   => $perm,
@@ -462,14 +487,16 @@ sub ModBudgetPeriod {
 
 # -------------------------------------------------------------------
 sub GetBudgetHierarchy {
-       my ($budget_period_id, $branchcode, $owner) = @_;
-       my @bind_params;
-       my $dbh   = C4::Context->dbh;
-       my $query = qq|
-                    SELECT aqbudgets.*
-                    FROM aqbudgets |;
-    # show only period X if requested
+    my ( $budget_period_id, $branchcode, $owner ) = @_;
+    my @bind_params;
+    my $dbh   = C4::Context->dbh;
+    my $query = qq|
+                    SELECT aqbudgets.*, aqbudgetperiods.budget_period_active
+                    FROM aqbudgets 
+                    JOIN aqbudgetperiods USING (budget_period_id)|;
+                        
        my @where_strings;
+    # show only period X if requested
     if ($budget_period_id) {
         push @where_strings," aqbudgets.budget_period_id = ?";
         push @bind_params, $budget_period_id;
@@ -477,8 +504,9 @@ sub GetBudgetHierarchy {
        # show only budgets owned by me, my branch or everyone
     if ($owner) {
         if ($branchcode) {
-            push @where_strings,qq{ (budget_owner_id = ? OR budget_branchcode = ? OR (budget_branchcode IS NULL or budget_branchcode="" AND (budget_owner_id IS NULL OR budget_owner_id="")))};
-            push @bind_params, ($owner, $branchcode);
+            push @where_strings,
+            qq{ (budget_owner_id = ? OR budget_branchcode = ? OR ((budget_branchcode IS NULL or budget_branchcode="") AND (budget_owner_id IS NULL OR budget_owner_id="")))};
+            push @bind_params, ( $owner, $branchcode );
         } else {
             push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
             push @bind_params, $owner;
@@ -611,24 +639,13 @@ sub DelBudget {
        return $rc;
 }
 
-=back
-
-=head2 FUNCTIONS ABOUT BUDGETS
-
-=over 2
-
-=cut
-
-=head3 GetBudget
 
-=over 4
+=head2 GetBudget
 
-&GetBudget($budget_id);
+  &GetBudget($budget_id);
 
 get a specific budget
 
-=back
-
 =cut
 
 # -------------------------------------------------------------------
@@ -646,15 +663,38 @@ sub GetBudget {
     return $result;
 }
 
-=head3 GetBudgets
-
-=over 4
+=head2 GetBudgets
 
-&GetBudgets($filter, $order_by);
+  &GetBudgets($filter, $order_by);
 
 gets all budgets
 
-=back
+=cut
+
+# -------------------------------------------------------------------
+sub GetChildBudgetsSpent {
+    my ( $budget_id ) = @_;
+    my $dbh = C4::Context->dbh;
+    my $query = "
+        SELECT *
+        FROM   aqbudgets
+        WHERE  budget_parent_id=?
+        ";
+    my $sth = $dbh->prepare($query);
+    $sth->execute( $budget_id );
+    my $result = $sth->fetchall_arrayref({});
+    my $total_spent = GetBudgetSpent($budget_id);
+    if ($result){
+        $total_spent += GetChildBudgetsSpent($_->{"budget_id"}) foreach @$result;    
+    }
+    return $total_spent;
+}
+
+=head2 GetChildBudgetsSpent
+
+  &GetChildBudgetsSpent($budget-id);
+
+gets the total spent of the level and sublevels of $budget_id
 
 =cut
 
@@ -666,9 +706,9 @@ sub GetBudgets {
 
 # -------------------------------------------------------------------
 
-=head3 GetCurrencies
+=head2 GetCurrencies
 
-@currencies = &GetCurrencies;
+  @currencies = &GetCurrencies;
 
 Returns the list of all known currencies.
 
@@ -704,7 +744,7 @@ sub GetCurrency {
     return $r;
 }
 
-=head3 ModCurrencies
+=head2 ModCurrencies
 
 &ModCurrencies($currency, $newrate);
 
@@ -725,15 +765,14 @@ sub ModCurrencies {
 
 # -------------------------------------------------------------------
 
-=head3 ConvertCurrency
+=head2 ConvertCurrency
 
-$foreignprice = &ConvertCurrency($currency, $localprice);
+  $foreignprice = &ConvertCurrency($currency, $localprice);
 
 Converts the price C<$localprice> to foreign currency C<$currency> by
 dividing by the exchange rate, and returns the result.
 
-If no exchange rate is found,e is one
-to one.
+If no exchange rate is found, e is one to one.
 
 =cut
 
@@ -754,9 +793,12 @@ sub ConvertCurrency {
     return ( $price / $cur );
 }
 
-=item
-       returns an array containing fieldname followed by PRI as value if PRIMARY Key
+=head2 _columns
+
+returns an array containing fieldname followed by PRI as value if PRIMARY Key
+
 =cut
+
 sub _columns(;$) {
        my $tablename=shift||"aqbudgets";
     return @{C4::Context->dbh->selectcol_arrayref("SHOW columns from $tablename",{Columns=>[1,4]})};
@@ -787,10 +829,8 @@ END { }    # module clean-up code here (global destructor)
 1;
 __END__
 
-=back
-
 =head1 AUTHOR
 
-Koha Developement team <info@koha.org>
+Koha Development Team <http://koha-community.org/>
 
 =cut