Merge remote branch 'koha-fbc/k_bug_5203' into master
[koha.git] / admin / aqbudgets.pl
index 01bfdbc..95f5f26 100755 (executable)
 # 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 CGI;
 use List::Util qw/min/;
 use Number::Format qw(format_price);
@@ -68,7 +69,7 @@ if  (  not defined $template->{param_map}->{'CAN_user_acquisition_budget_add_del
 my $num=FormatNumber;
 
 my $script_name               = "/cgi-bin/koha/admin/aqbudgets.pl";
-my $budget_hash=$input->Vars;
+my $budget_hash               = $input->Vars;
 my $budget_id                 = $$budget_hash{budget_id};
 my $budget_permission         = $input->param('budget_permission');
 my $budget_period_dropbox     = $input->param('budget_period_dropbox');
@@ -221,13 +222,13 @@ if ($op eq 'add_form') {
         } else {
             AddBudget( $budget_hash );
         }
-    }            
+    }
     my $branches = GetBranches();
     my $budget_period_dropbox = GetBudgetPeriodsDropbox($$period{budget_period_id} );
     $template->param(
         budget_period_dropbox     => $budget_period_dropbox,
         budget_id                 => $budget_id,
-               %$period,
+        %$period,
     );
 
     my $moo = GetBudgetHierarchy($$period{budget_period_id}, C4::Context->userenv->{branchcode}, $show_mine?$borrower_id:'');
@@ -236,13 +237,14 @@ if ($op eq 'add_form') {
     my $toggle = 0;
     my @loop;
     my $period_total = 0;
-    my ( $period_alloc_total, $base_alloc_total, $base_spent_total, $base_remaining_total );
+    my ( $period_alloc_total, $base_spent_total );
 
-       use YAML;
-       $debug && warn Dump(@budgets);
        #This Looks WEIRD to me : should budgets be filtered in such a way ppl who donot own it would not see the amount spent on the budget by others ?
 
     foreach my $budget (@budgets) {
+        #Level and sublevels total spent
+        $budget->{'total_levels_spent'} = GetChildBudgetsSpent($budget->{"budget_id"});
+
         # PERMISSIONS
         unless($staffflags->{'superlibrarian'} % 2   == 1 ) {
             #IF NO PERMS, THEN DISABLE EDIT/DELETE
@@ -282,26 +284,40 @@ if ($op eq 'add_form') {
 ## TOTALS
         # adds to total  - only if budget is a 'top-level' budget
         $period_alloc_total += $budget->{'budget_amount_total'} if $budget->{'depth'} == 0;
-        $base_alloc_total += $budget->{'budget_amount'};
         $base_spent_total += $budget->{'budget_spent'};
-        $budget->{'budget_remaining'} = $budget->{'budget_amount'} - $budget->{'budget_spent'};
-        $base_remaining_total += $budget->{'budget_remaining'};
+        $budget->{'budget_remaining'} = $budget->{'budget_amount'} - $budget->{'total_levels_spent'};
 
 # if amount == 0 dont display...
         delete  $budget->{'budget_unalloc_sublevel'} if  $budget->{'budget_unalloc_sublevel'} == 0 ;
 
         $budget->{'remaining_pos'} = 1 if $budget->{'budget_remaining'} > 0;
         $budget->{'remaining_neg'} = 1 if $budget->{'budget_remaining'} < 0;
-               for (grep {/budget_spent|budget_amount|budget_remaining|budget_unalloc/} keys %$budget){
-        $$budget{$_}               = $num->format_price( $$budget{$_} ) if defined($$budget{$_})
+               for (grep {/total_levels_spent|budget_spent|budget_amount|budget_remaining|budget_unalloc/} keys %$budget){
+            $budget->{$_}               = $num->format_price( $budget->{$_} ) if defined($budget->{$_})
                }
 
+        # Value of budget_spent equals 0 instead of undefined value
+        $budget->{"budget_spent"} = $num->format_price(0) unless defined($budget->{"budget_spent"});
+
         my $borrower = &GetMember( borrowernumber=>$budget->{budget_owner_id} );
         $budget->{"budget_owner_name"}     = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
         $budget->{"budget_borrowernumber"} = $borrower->{'borrowernumber'};
 
+        #Make a list of parents of the bugdet
+        my @budget_hierarchy;
+        push  @budget_hierarchy, { element_name => $budget->{"budget_name"}, element_id => $budget->{"budget_id"} };
+        my $parent_id = $budget->{"budget_parent_id"};
+        while ($parent_id) {
+            my $parent = GetBudget($parent_id);
+            push @budget_hierarchy, { element_name => $parent->{"budget_name"}, element_id => $parent->{"budget_id"} };
+            $parent_id = $parent->{"budget_parent_id"};
+        }
+        push  @budget_hierarchy, { element_name => $period->{"budget_period_description"} }; 
+        @budget_hierarchy = reverse(@budget_hierarchy);
+
         push( @loop, {  %{$budget},
                         branchname  => $branches->{ $budget->{branchcode} }->{branchname},
+                        budget_hierarchy => \@budget_hierarchy,
                     }
         );
     }
@@ -312,10 +328,7 @@ if ($op eq 'add_form') {
         budget                 => \@loop,
         budget_period_total    => $budget_period_total,
         period_alloc_total     => $num->format_price($period_alloc_total),
-        base_alloc_total       => $num->format_price($base_alloc_total),
         base_spent_total       => $num->format_price($base_spent_total),
-        base_remaining_total   => $num->format_price($base_remaining_total),
-        period_remaining_total => $num->format_price( $period_alloc_total - $base_alloc_total ),
         branchloop             => \@branchloop2,
     );