From 0aa82f5c24f2a339a3ab113e538f1e3eed0365a3 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
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Followed test plan and patch works as described.

Signed-off-by: Dilan Johnpullé <dilan@calyx.net.au>
---
 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