Bug 9218: fix intranet cart email for non english templates
[koha.git] / admin / aqbudgets.pl
index c74d1d2..42a39f7 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 Modern::Perl;
 
-use strict;
 use CGI;
 use List::Util qw/min/;
 use Number::Format qw(format_price);
@@ -50,29 +51,35 @@ my ($template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user(
     }
 );
 
-my $op = $input->param('op');
+my $cur = GetCurrency();
+$template->param( symbol => $cur->{symbol},
+                  currency => $cur->{currency}
+               );
+
+my $op = $input->param('op') // '';
 
 # see if the user want to see all budgets or only owned ones
 my $show_mine    = 1; #SHOW BY DEFAULT
-my $show         = $input->param('show'); # SET TO 1, BY A FORM SUMBIT
+my $show         = $input->param('show') // 0; # SET TO 1, BY A FORM SUMBIT
 $show_mine       = $input->param('show_mine') if $show == 1;
 
 # IF USER DOESNT HAVE PERM FOR AN 'ADD', THEN REDIRECT TO THE DEFAULT VIEW...
-if  (  not defined $template->{param_map}->{'CAN_user_acquisition_budget_add_del'}  &&  $op ==  'add_form'  )   {
+if (not defined $template->{VARS}->{'CAN_user_acquisition_budget_add_del'}
+    and $op eq 'add_form')
+{
     $op = '';
 }
-
 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');
+my $filter_budgetbranch       = $input->param('filter_budgetbranch') // '';
+my $filter_budgetname         = $input->param('filter_budgetname');
 #filtering non budget keys
 delete $$budget_hash{$_} foreach grep {/filter|^op$|show/} keys %$budget_hash;
-my $filter_budgetbranch       = $input->param('filter_budgetbranch');
-my $filter_budgetname         = $input->param('filter_budgetname');
+
 $template->param(
     notree => ($filter_budgetbranch or $show_mine)
 );
@@ -86,7 +93,7 @@ $template->param(
 # ------- get periods stuff ------------------
 
 # USED FOR PERMISSION COMPARISON LATER
-my $borrower_id         = $template->{param_map}->{'USER_INFO'}[0]->{'borrowernumber'};
+my $borrower_id         = $template->{VARS}->{'USER_INFO'}[0]->{'borrowernumber'};
 my $user                = GetMemberDetails($borrower_id);
 my $user_branchcode     = $user->{'branchcode'};
 
@@ -117,16 +124,20 @@ $template->param(auth_cats_loop => GetBudgetAuthCats($$period{budget_period_id})
 # Used to create form to add or  modify a record
 if ($op eq 'add_form') {
 #### ------------------- ADD_FORM -------------------------
-
     # if no buget_id is passed then its an add
     #  pass the period_id to build the dropbox - because we only want to show  budgets from this period
     my $dropbox_disabled;
     if (defined $budget_id ) {    ### MOD
-        $budget           = GetBudget($budget_id);
+        $budget = GetBudget($budget_id);
+        if (!CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
+            $template->param(error_not_authorised_to_modify => 1);
+            output_html_with_http_headers $input, $cookie, $template->output;
+            exit;
+        }
         $dropbox_disabled = BudgetHasChildren($budget_id);
         my $borrower = &GetMember( borrowernumber=>$budget->{budget_owner_id} );
         $budget->{budget_owner_name} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
-        $$budget{$_}= sprintf("%.2f", $budget->{$_}) for grep{/amount/} keys %$budget;
+        $$budget{$_}= sprintf("%.2f", $budget->{$_}) for grep{ /amount|encumb|expend/ } keys %$budget;
     }
 
     # build budget hierarchy
@@ -180,16 +191,34 @@ if ($op eq 'add_form') {
     $template->param(authorised_value_categories1 => \@auth_cats_loop1);
     $template->param(authorised_value_categories2 => \@auth_cats_loop2);
 
-    my $budget_perm_dropbox =
-    GetBudgetPermDropbox($budget->{'budget_permission'});
-    
+    if($budget->{'budget_permission'}){
+        my $budget_permission = "budget_perm_".$budget->{'budget_permission'};
+        $template->param($budget_permission => 1);
+    }
+
+    if ($budget) {
+        my @budgetusers = GetBudgetUsers($budget->{budget_id});
+        my @budgetusers_loop;
+        foreach my $borrowernumber (@budgetusers) {
+            my $member = C4::Members::GetMember(
+                borrowernumber => $borrowernumber);
+            push @budgetusers_loop, {
+                firstname => $member->{firstname},
+                surname => $member->{surname},
+                borrowernumber => $borrowernumber
+            };
+        }
+        $template->param(
+            budget_users => \@budgetusers_loop,
+            budget_users_ids => join ':', @budgetusers
+        );
+    }
+
     # if no buget_id is passed then its an add
     $template->param(
         add_validate                  => 1,
-        dateformat                => C4::Dates->new()->visual(),
         budget_parent_id                 => $budget_parent->{'budget_id'},
         budget_parent_name               => $budget_parent->{'budget_name'},
-        budget_perm_dropbox       => $budget_perm_dropbox,
         branchloop_select         => \@branchloop_select,
                %$period,
                %$budget,
@@ -213,64 +242,58 @@ if ($op eq 'add_form') {
     if ( $op eq 'delete_confirmed' ) {
         my $rc = DelBudget($budget_id);
     }elsif( $op eq 'add_validate' ) {
+        my @budgetusersid;
+        if (defined $$budget_hash{'budget_users_ids'}){
+            @budgetusersid = split(':', $budget_hash->{'budget_users_ids'});
+        }
+
         if ( defined $$budget_hash{budget_id} ) {
-            ModBudget( $budget_hash );
+            if (CanUserModifyBudget($borrowernumber, $budget_hash->{budget_id},
+                $staffflags)
+            ) {
+                ModBudget( $budget_hash );
+                ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
+            }
+            else {
+                $template->param(error_not_authorised_to_modify => 1);
+            }
         } else {
             AddBudget( $budget_hash );
+            ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
         }
-    }            
+    }
     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:'');
-    my @budgets = @$moo; #FIXME
+    my @budgets = @{
+        GetBudgetHierarchy($$period{budget_period_id},
+            C4::Context->userenv->{branchcode}, $show_mine ? $borrower_id : '')
+    };
 
     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
-            unless ( $template->{param_map}->{'CAN_user_acquisition_budget_modify'} ) {
-                $budget->{'budget_lock'} = 1;
-            }
-            # check budget permission
-            if ( $$period{budget_period_locked} == 1 ) {
-                $budget->{'budget_lock'} = 1;
-
-            } elsif ( $budget->{budget_permission} == 1 ) {
-
-                if ( $borrower_id != $budget->{'budget_owner_id'} ) {
-                    $budget->{'budget_lock'} = 1;
-                }
-                # check parent perms too
-                my $parents_perm = 0;
-                if ( $budget->{depth} > 0 ) {
-                    $parents_perm = CheckBudgetParentPerm( $budget, $borrower_id );
-                    delete $budget->{'budget_lock'} if $parents_perm == '1';
-                }
-            } elsif ( $budget->{budget_permission} == 2 ) {
-
-                $budget->{'budget_lock'} = 1 if $user_branchcode ne $budget->{budget_branchcode};
-            }
-        }    # ...SUPER_LIB END
+        unless(CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
+            $budget->{'budget_lock'} = 1;
+        }
 
         # if a budget search doesnt match, next
-        if ($filter_budgetname ) {
-            next unless  $budget->{budget_code}  =~ m/$filter_budgetname/  ||
-            $budget->{name}  =~ m/$filter_budgetname/ ;
+        if ($filter_budgetname) {
+            next
+              unless $budget->{budget_code} =~ m/$filter_budgetname/i
+                  || $budget->{budget_name} =~ m/$filter_budgetname/i;
         }
         if ($filter_budgetbranch ) {
             next unless  $budget->{budget_branchcode}  =~ m/$filter_budgetbranch/;
@@ -279,40 +302,66 @@ 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 ;
+        delete $budget->{'budget_unalloc_sublevel'}
+            if (!defined $budget->{'budget_unalloc_sublevel'}
+            or $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,
                     }
         );
     }
 
-    my $budget_period_total = $num->format_price($$period{budget_period_total}) if $$period{budget_period_total};
+    my $budget_period_total;
+    if ( $period->{budget_period_total} ) {
+        $budget_period_total =
+          $num->format_price( $period->{budget_period_total} );
+    }
+
+    if ($period_alloc_total) {
+        $period_alloc_total = $num->format_price($period_alloc_total);
+    }
+
+    if ($base_spent_total) {
+        $base_spent_total = $num->format_price($base_spent_total);
+    }
+
     $template->param(
         else                   => 1,
         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 ),
+        period_alloc_total     => $period_alloc_total,
+        base_spent_total       => $base_spent_total,
         branchloop             => \@branchloop2,
     );