Bug 3638 : Captured Holds may need to generate a transfer
[koha.git] / C4 / Budgets.pm
index 0882756..7c867e0 100644 (file)
@@ -50,10 +50,7 @@ BEGIN {
         &AddBudgetPeriod
            &DelBudgetPeriod
 
-           &GetBudgetPeriodsDropbox
-        &GetBudgetSortDropbox
         &GetAuthvalueDropbox
-        &GetBudgetPermDropbox
 
         &ModBudgetPlan
 
@@ -307,7 +304,7 @@ 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( COALESCE(unitprice, ecost) * quantity ) AS sum FROM aqorders
             WHERE budget_id = ? AND
             quantityreceived > 0 AND
             datecancellationprinted IS NULL
@@ -334,24 +331,6 @@ sub GetBudgetOrdered {
        return $sum;
 }
 
-# -------------------------------------------------------------------
-sub GetBudgetPermDropbox {
-       my ($perm) = @_;
-       my %labels;
-       $labels{'0'} = 'None';
-       $labels{'1'} = 'Owner';
-       $labels{'2'} = 'Library';
-       my $radio = CGI::scrolling_list(
-               -id       => 'budget_permission',
-               -name      => 'budget_permission',
-               -values    => [ '0', '1', '2' ],
-               -default   => $perm,
-               -labels    => \%labels,
-               -size    => 1,
-       );
-       return $radio;
-}
-
 # -------------------------------------------------------------------
 sub GetBudgetAuthCats  {
     my ($budget_period_id) = shift;
@@ -374,61 +353,27 @@ sub GetBudgetAuthCats  {
 
 # -------------------------------------------------------------------
 sub GetAuthvalueDropbox {
-       my ( $name, $authcat, $default ) = @_;
-       my @authorised_values;
-       my %authorised_lib;
-       my $value;
-       my $dbh = C4::Context->dbh;
-       my $sth = $dbh->prepare(
-               "SELECT authorised_value,lib
-            FROM authorised_values
-            WHERE category = ?
-            ORDER BY  lib"
-       );
-       $sth->execute( $authcat );
-
-       push @authorised_values, '';
-       while (my ($value, $lib) = $sth->fetchrow_array) {
-               push @authorised_values, $value;
-               $authorised_lib{$value} = $lib;
-       }
-
-    return 0 if keys(%authorised_lib) == 0;
-
-    my $budget_authvalue_dropbox = CGI::scrolling_list(
-        -values   => \@authorised_values,
-        -labels   => \%authorised_lib,
-        -default  => $default,
-        -override => 1,
-        -size     => 1,
-        -multiple => 0,
-        -name     => $name,
-        -id       => $name,
+    my ( $authcat, $default ) = @_;
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare(
+        'SELECT authorised_value,lib FROM authorised_values
+        WHERE category = ? ORDER BY lib'
     );
+    $sth->execute( $authcat );
+    my $option_list = [];
+    my @authorised_values = ( q{} );
+    while (my ($value, $lib) = $sth->fetchrow_array) {
+        push @{$option_list}, {
+            value => $value,
+            label => $lib,
+            default => ($default eq $value),
+        };
+    }
 
-    return $budget_authvalue_dropbox
-}
-
-# -------------------------------------------------------------------
-sub GetBudgetPeriodsDropbox {
-    my ($budget_period_id) = @_;
-       my %labels;
-       my @values;
-       my ($active, $periods) = GetBudgetPeriods();
-       foreach my $r (@$periods) {
-               $labels{"$r->{budget_period_id}"} = $r->{budget_period_description};
-               push @values, $r->{budget_period_id};
-       }
-
-       # if no buget_id is passed then its an add
-       my $budget_period_dropbox = CGI::scrolling_list(
-               -name    => 'budget_period_id',
-               -values  => \@values,
-               -default => $budget_period_id ? $budget_period_id :  $active,
-               -size    => 1,
-               -labels  => \%labels,
-       );
-       return $budget_period_dropbox;
+    if ( @{$option_list} ) {
+        return $option_list;
+    }
+    return;
 }
 
 # -------------------------------------------------------------------
@@ -487,17 +432,15 @@ sub ModBudgetPeriod {
 
 # -------------------------------------------------------------------
 sub GetBudgetHierarchy {
-       my ($budget_period_id, $branchcode, $owner) = @_;
-       my @bind_params;
-       my $dbh   = C4::Context->dbh;
-       my $query = qq|
-                    SELECT aqbudgets.*
+    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 
-                    LEFT JOIN aqbudgetperiods 
-                    ON aqbudgetperiods.budget_period_id=aqbudgets.budget_period_id |;
+                    JOIN aqbudgetperiods USING (budget_period_id)|;
+                        
        my @where_strings;
-    # Pick out the active ones
-    push @where_strings, 'aqbudgetperiods.budget_period_active=1';
     # show only period X if requested
     if ($budget_period_id) {
         push @where_strings," aqbudgets.budget_period_id = ?";
@@ -506,8 +449,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;
@@ -591,7 +535,10 @@ sub GetBudgetHierarchy {
 # add budget-percent and allocation, and flags for html-template
        foreach my $r (@sort) {
                my $subs_href = $r->{'child'};
-        my @subs_arr = @$subs_href if defined $subs_href;
+        my @subs_arr = ();
+        if ( defined $subs_href ) {
+            @subs_arr = @{$subs_href};
+        }
 
         my $moo = $r->{'budget_code_indent'};
         $moo =~ s/\ /\&nbsp\;/g;
@@ -664,11 +611,11 @@ sub GetBudget {
     return $result;
 }
 
-=head2 GetBudgets
+=head2 GetChildBudgetsSpent
 
-  &GetBudgets($filter, $order_by);
+  &GetChildBudgetsSpent($budget-id);
 
-gets all budgets
+gets the total spent of the level and sublevels of $budget_id
 
 =cut
 
@@ -691,11 +638,11 @@ sub GetChildBudgetsSpent {
     return $total_spent;
 }
 
-=head2 GetChildBudgetsSpent
+=head2 GetBudgets
 
-  &GetChildBudgetsSpent($budget-id);
+  &GetBudgets($filter, $order_by);
 
-gets the total spent of the level and sublevels of $budget_id
+gets all budgets
 
 =cut