Bug 15344: Remove some other calls of GetMemberDetails from pl scripts
[koha.git] / admin / aqbudgets.pl
index accdb78..3e7e14f 100755 (executable)
@@ -6,29 +6,27 @@
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY 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.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 
-use CGI;
+use CGI qw ( -utf8 );
 use List::Util qw/min/;
-use Number::Format qw(format_price);
 
 use Koha::Database;
 use C4::Auth qw/get_user_subpermissions/;
 use C4::Branch; # GetBranches
-use C4::Dates qw/format_date format_date_in_iso/;
 use C4::Auth;
 use C4::Acquisition;
 use C4::Budgets;
@@ -59,15 +57,14 @@ $template->param( symbol => $cur->{symbol},
 my $op = $input->param('op') || 'list';
 
 # see if the user want to see all budgets or only owned ones by default
-my $show_mine = $input->param('show_mine') // 1;
+my $show_mine = $input->param('show_mine') // 0;
 
-# IF USER DOESNT HAVE PERM FOR AN 'ADD', THEN REDIRECT TO THE DEFAULT VIEW...
+# IF USER DOESN'T HAVE PERM FOR AN 'ADD', THEN REDIRECT TO THE DEFAULT VIEW...
 if (not defined $template->{VARS}->{'CAN_user_acquisition_budget_add_del'}
     and $op eq 'add_form')
 {
     $op = 'list';
 }
-my $num=FormatNumber;
 
 # get only the columns of aqbudgets in budget_hash
 my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns;
@@ -91,8 +88,8 @@ if ( $budget_period_id ) {
 # ------- get periods stuff ------------------
 
 # USED FOR PERMISSION COMPARISON LATER
-my $borrower_id         = $template->{VARS}->{'USER_INFO'}[0]->{'borrowernumber'};
-my $user                = GetMemberDetails($borrower_id);
+my $borrower_id         = $template->{VARS}->{'USER_INFO'}->{'borrowernumber'};
+my $user                = C4::Members::GetMember( borrowernumber => $borrower_id );
 my $user_branchcode     = $user->{'branchcode'};
 
 $template->param(
@@ -102,16 +99,7 @@ $template->param(
 
 my $budget;
 
-my $branches = GetBranches($show_mine);
-my @branchloop2;
-foreach my $thisbranch (keys %$branches) {
-    my %row = (
-        value      => $thisbranch,
-        branchname => $branches->{$thisbranch}->{'branchname'},
-    );
-    $row{selected} = 1 if $thisbranch eq $filter_budgetbranch;
-    push @branchloop2, \%row;
-}
+my $branchloop = C4::Branch::GetBranchesLoop($filter_budgetbranch);
 
 $template->param(auth_cats_loop => GetBudgetAuthCats( $budget_period_id ))
     if $budget_period_id;
@@ -132,7 +120,6 @@ if ($op eq 'add_form') {
         $dropbox_disabled = BudgetHasChildren($budget_id);
         my $borrower = &GetMember( borrowernumber=>$budget->{budget_owner_id} );
         $budget->{budget_owner_name} = ( $borrower ? $borrower->{'firstname'} . ' ' . $borrower->{'surname'} : '' );
-        $$budget{$_}= sprintf("%.2f", $budget->{$_}) for grep{ /amount|encumb|expend/ } keys %$budget;
     }
 
     # build budget hierarchy
@@ -140,8 +127,7 @@ if ($op eq 'add_form') {
     my @values;
     my $hier = GetBudgetHierarchy($$period{budget_period_id});
     foreach my $r (@$hier) {
-        $r->{budget_code_indent} =~ s/&nbsp;/\~/g;    #
-        $labels{"$r->{budget_id}"} = $r->{budget_code_indent};
+        $labels{"$r->{budget_id}"} = $r->{budget_code};
         push @values, $r->{budget_id};
     }
     push @values, '';
@@ -211,6 +197,7 @@ if ($op eq 'add_form') {
 
     # if no buget_id is passed then its an add
     $template->param(
+        budget_has_children => BudgetHasChildren( $budget->{budget_id} ),
         budget_parent_id                 => $budget_parent->{'budget_id'},
         budget_parent_name               => $budget_parent->{'budget_name'},
         branchloop_select         => \@branchloop_select,
@@ -228,11 +215,15 @@ if ($op eq 'add_form') {
         budget_id     => $budget->{'budget_id'},
         budget_code   => $budget->{'budget_code'},
         budget_name   => $budget->{'budget_name'},
-        budget_amount => $num->format_price(  $budget->{'budget_amount'} ),
+        budget_amount => $budget->{'budget_amount'},
     );
                                                     # END $OP eq DELETE_CONFIRM
 # called by delete_confirm, used to effectively confirm deletion of data in DB
 } elsif ( $op eq 'delete_confirmed' ) {
+    if ( BudgetHasChildren( $budget_id ) ) {
+        # We should never be here, the interface does not provide this action.
+        die("Delete a fund with children is not possible");
+    }
     my $rc = DelBudget($budget_id);
     $op = 'list';
 } elsif( $op eq 'add_validate' ) {
@@ -241,19 +232,27 @@ if ($op eq 'add_form') {
         @budgetusersid = split(':', $budget_users_ids);
     }
 
+    my $budget_modified = 0;
     if (defined $budget_id) {
         if (CanUserModifyBudget($borrowernumber, $budget_hash->{budget_id},
             $staffflags)
         ) {
             ModBudget( $budget_hash );
             ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
+            $budget_modified = 1;
         }
         else {
             $template->param(error_not_authorised_to_modify => 1);
         }
     } else {
-        AddBudget( $budget_hash );
+        $budget_hash->{budget_id} = AddBudget( $budget_hash );
         ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
+        $budget_modified = 1;
+    }
+
+    my $set_owner_to_children = $input->param('set_owner_to_children');
+    if ( $set_owner_to_children and $budget_modified ) {
+        C4::Budgets::SetOwnerToFundHierarchy( $budget_hash->{budget_id}, $budget_hash->{budget_owner_id} );
     }
     $op = 'list';
 }
@@ -275,13 +274,14 @@ if ( $op eq 'list' ) {
 
        #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 ?
 
+    my @budgets_to_display;
     foreach my $budget (@budgets) {
         # PERMISSIONS
         unless(CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
             $budget->{'budget_lock'} = 1;
         }
 
-        # if a budget search doesnt match, next
+        # if a budget search doesn't match, next
         if ($filter_budgetname) {
             next
               unless $budget->{budget_code} =~ m/$filter_budgetname/i
@@ -295,30 +295,21 @@ if ( $op eq 'list' ) {
         $budget->{'budget_remaining'} = $budget->{'budget_amount'} - $budget->{'budget_spent'} - $budget->{budget_ordered};
         $budget->{'total_remaining'} = $budget->{'budget_amount'} - $budget->{'total_spent'} - $budget->{total_ordered};
         # adds to total  - only if budget is a 'top-level' budget
-        if ($budget->{depth} == 0) {
+        unless ( defined $budget->{budget_parent_id} ) {
             $period_alloc_total += $budget->{'budget_amount'};
             $spent_total += $budget->{total_spent};
             $ordered_total += $budget->{total_ordered};
             $available_total += $budget->{total_remaining};
         }
 
-# if amount == 0 dont display...
+# if amount == 0 don't display...
         delete $budget->{'budget_unalloc_sublevel'}
             if (!defined $budget->{'budget_unalloc_sublevel'}
             or $budget->{'budget_unalloc_sublevel'} == 0);
 
-        for (grep {/total_spent|budget_spent|total_ordered|budget_ordered|budget_amount/} keys %$budget){
-            $budget->{$_}               = $num->format_price( $budget->{$_} ) if defined($budget->{$_})
-               }
-        for (qw/budget_remaining total_remaining/) {
-            if (defined $budget->{$_}) {
-                $budget->{$_.'_display'} = $num->format_price($budget->{$_});
-            }
-        }
-
         # Value of budget_spent equals 0 instead of undefined value
-        $budget->{"budget_spent"} = $num->format_price(0) unless defined($budget->{"budget_spent"});
-        $budget->{budget_ordered} = $num->format_price(0) unless defined($budget->{"budget_ordered"});
+        $budget->{budget_spent} = 0 unless defined($budget->{budget_spent});
+        $budget->{budget_ordered} = 0 unless defined($budget->{budget_ordered});
 
         #Make a list of parents of the bugdet
         my @budget_hierarchy;
@@ -333,26 +324,26 @@ if ( $op eq 'list' ) {
         @budget_hierarchy = reverse(@budget_hierarchy);
 
         $budget->{budget_hierarchy} = \@budget_hierarchy;
+
+        $budget->{budget_has_children} = BudgetHasChildren( $budget->{budget_id} );
+        push @budgets_to_display, $budget;
     }
 
     my $budget_period_total = $period->{budget_period_total};
 
-    foreach ($budget_period_total, $period_alloc_total, $spent_total, $ordered_total, $available_total) {
-        $_ = $num->format_price($_);
-    }
-
     my $periods = GetBudgetPeriods();
 
     $template->param(
         op                     => 'list',
-        budgets                => \@budgets,
+        budgets                => \@budgets_to_display,
         periods                => $periods,
         budget_period_total    => $budget_period_total,
         period_alloc_total     => $period_alloc_total,
         spent_total            => $spent_total,
         ordered_total          => $ordered_total,
         available_total        => $available_total,
-        branchloop             => \@branchloop2,
+        branchloop             => $branchloop,
+        filter_budgetname      => $filter_budgetname,
     );
 
 } #---- END list