ffzg/recall_notices.pl: added --interval and --dedup
[koha.git] / admin / aqbudgetperiods.pl
index ca2171f..0a05a55 100755 (executable)
@@ -91,9 +91,11 @@ my ($template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
 
 # This is used in incbudgets-active-currency.inc
 my $active_currency = Koha::Acquisition::Currencies->get_active;
-$template->param( symbol => $active_currency->symbol,
-                  currency => $active_currency->currency
-               );
+if ( $active_currency ) {
+    $template->param( symbol => $active_currency->symbol,
+                      currency => $active_currency->currency
+                   );
+}
 
 # ADD OR MODIFY A BUDGET PERIOD - BUILD SCREEN
 if ( $op eq 'add_form' ) {
@@ -127,21 +129,31 @@ elsif ( $op eq 'add_validate' ) {
 #--------------------------------------------------
 elsif ( $op eq 'delete_confirm' ) {
 ## delete a budget period (preparation)
-    my $dbh = C4::Context->dbh;
-    ## $total = number of records linked to the record that must be deleted
+    my $funds = GetBudgets({ budget_period_id => $budget_period_id });
+    my $fund_count = scalar @$funds;
+    if ( $fund_count > 0 ) {
+        $template->param( funds_exist => 1 );
+    }
+
+    #$total = number of records linked to the record that must be deleted
     my $total = 0;
     my $data = GetBudgetPeriod( $budget_period_id);
-
     $template->param(
-               %$data
+        %$data
     );
 }
 
 elsif ( $op eq 'delete_confirmed' ) {
-## delete the budget period record
-
-    my $data = GetBudgetPeriod( $budget_period_id);
-    DelBudgetPeriod($budget_period_id);
+    ## confirm no funds have been added to budget
+    my $funds = GetBudgets({ budget_period_id => $budget_period_id });
+    my $fund_count = scalar @$funds;
+    if ( $fund_count > 0 ) {
+        $template->param( failed_delete_funds_exist => 1 );
+    } else {
+        ## delete the budget period record
+        my $data = GetBudgetPeriod( $budget_period_id);
+        DelBudgetPeriod($budget_period_id);
+    }
        $op='else';
 }
 
@@ -263,6 +275,8 @@ my @period_active_loop;
 foreach my $result ( @{$results} ) {
     my $budgetperiod = $result;
     $budgetperiod->{budget_active} = 1;
+    my $funds = GetBudgets({ budget_period_id => $budgetperiod->{budget_period_id} });
+    $budgetperiod->{count} = scalar @$funds;
     push( @period_active_loop, $budgetperiod );
 }
 
@@ -276,6 +290,8 @@ my @period_inactive_loop;
 foreach my $result ( @{$results} ) {
     my $budgetperiod = $result;
     $budgetperiod->{budget_active} = 1;
+    my $funds = GetBudgets({ budget_period_id => $budgetperiod->{budget_period_id} });
+    $budgetperiod->{count} = scalar @$funds;
     push( @period_inactive_loop, $budgetperiod );
 }