From 26874d7b52ea2e8218b57ad460f7f3bd324ff49b Mon Sep 17 00:00:00 2001
From: Aleisha Amohia <aleishaamohia@hotmail.com>
Date: Fri, 18 Aug 2017 01:58:10 +0000
Subject: [PATCH] Bug 18351: [FOLLOW-UP] Prevent deletion if funds are added
 after clicking 'Delete' and before confirming delete

To test:
1) Create a budget with no fund
2) Click Delete but do not confirm
3) In another tab, add a fund to this budget
4) Go back to original tab and click 'Yes, delete'
5) Confirm budget is not deleted and an appropriate error message shows
---
 admin/aqbudgetperiods.pl                                  | 15 ++++++++++-----
 .../prog/en/modules/admin/aqbudgetperiods.tt              |  4 ++++
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/admin/aqbudgetperiods.pl b/admin/aqbudgetperiods.pl
index cdd2788..fbe1938 100755
--- a/admin/aqbudgetperiods.pl
+++ b/admin/aqbudgetperiods.pl
@@ -127,7 +127,6 @@ elsif ( $op eq 'add_validate' ) {
 #--------------------------------------------------
 elsif ( $op eq 'delete_confirm' ) {
 ## delete a budget period (preparation)
-    my $dbh = C4::Context->dbh;
     my $funds = GetBudgets({ budget_period_id => $budget_period_id });
     my $fund_count = scalar @$funds;
     if ( $fund_count > 0 ) {
@@ -143,10 +142,16 @@ elsif ( $op eq 'delete_confirm' ) {
 }
 
 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';
 }
 
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tt
index fa4e050..3323b17 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tt
@@ -542,6 +542,10 @@
 [% IF ( else ) %]
   <h2>Budgets administration</h2>
 
+    [% IF ( failed_delete_funds_exist ) %]
+        <div class="dialog message">Failed to delete budget because funds exist.</div>
+    [% END %]
+
   [% INCLUDE 'budgets-active-currency.inc' %]
 
   <div id="budgetsTabs" class="toptabs">
-- 
2.1.4