Bug 18351: Able to delete budget with funds
authorAleisha Amohia <aleishaamohia@hotmail.com>
Thu, 30 Mar 2017 04:15:54 +0000 (04:15 +0000)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 12 Sep 2017 14:28:53 +0000 (11:28 -0300)
To test:
1) Create a budget, add a fund
2) Delete budget. Notice this is successful and triggers no warning
message etc.
3) Go to Funds. Notice the funds appear as if they are not there
4) Go into mysql and view the aqbudgetperiods table - notice the funds
are still there and are now inaccessible.
5) Apply patch
6) Create a budget, add a fund
7) Attempt to delete budget. Notice you can't click Delete button.
Confirm number of funds in hover message is correct.
8) Delete fund
9) Confirm you can now delete budget.

Sponsored-by: Catalyst IT
Signed-off-by: Felix Hemme <felix.hemme@thulb.uni-jena.de>
Signed-off-by: Owen Leonard <oleonard@myacpl.org>
Bug 18351: [FOLLOW-UP] Some code fixes

See Comment 5. Ready to test.

Signed-off-by: Lee Jamison <ldjamison@marywood.edu>
Signed-off-by: Owen Leonard <oleonard@myacpl.org>
Bug 18351: [FOLLOW-UP] Code fix

See comment 10.
Ready for testing.

Signed-off-by: Owen Leonard <oleonard@myacpl.org>
Bug 18351: [FOLLOW-UP] Prevent deletion from forcing URL

This patch adds a check in the script for existing funds so that the
budget cannot be deleted when forcing the URL and has other small fixes.

Signed-off-by: Owen Leonard <oleonard@myacpl.org>
Bug 18351: [FOLLOW-UP] Prevent deletion if funds are added after clicking 'Delete' and before confirming delete

Followed test plan and patch works as described.

Signed-off-by: Dilan Johnpullé <dilan@calyx.net.au>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
admin/aqbudgetperiods.pl
koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tt

index ca2171f..fbe1938 100755 (executable)
@@ -127,21 +127,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 +273,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 +288,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 );
 }
 
index 76f0701..3323b17 100644 (file)
@@ -3,24 +3,30 @@
 
 [%- BLOCK action_menu %]
     <div class="dropdown">
-        <a class="btn btn-default btn-xs dropdown-toggle" id="budgetmenu_[% block_budget_id %]" role="button" data-toggle="dropdown" href="#">
+        <a class="btn btn-default btn-xs dropdown-toggle" id="budgetmenu_[% block_budget.budget_period_id %]" role="button" data-toggle="dropdown" href="#">
            Actions <b class="caret"></b>
         </a>
-        <ul class="dropdown-menu pull-right" role="menu" aria-labelledby="budgetmenu_[% block_budget_id %]">
+        <ul class="dropdown-menu pull-right" role="menu" aria-labelledby="budgetmenu_[% block_budget.budget_period_id %]">
               <li>
-                  <a href="[% script_name %]?op=add_form&amp;budget_period_id=[% block_budget_id |html %]"><i class="fa fa-pencil"></i> Edit</a>
-              </li>
-              <li>
-                  <a href="[% script_name %]?op=delete_confirm&amp;budget_period_id=[% block_budget_id %]"><i class="fa fa-trash"></i> Delete</a>
+                  <a href="[% script_name %]?op=add_form&amp;budget_period_id=[% block_budget.budget_period_id |html %]"><i class="fa fa-pencil"></i> Edit</a>
               </li>
+              [% IF block_budget.count %]
+                  <li class="disabled">
+                      <a data-toggle="tooltip" data-placement="left" title="[% block_budget.count %] fund(s) are attached to this budget. You must delete all attached funds before deleting this budget." href="#"><i class="fa fa-trash"></i> Delete</a>
+                  </li>
+              [% ELSE %]
+                  <li>
+                      <a href="[% script_name %]?op=delete_confirm&amp;budget_period_id=[% block_budget.budget_period_id %]"><i class="fa fa-trash"></i> Delete</a>
+                  </li>
+              [% END %]
               <li>
-                  <a href="[% script_name %]?op=duplicate_form&amp;budget_period_id=[% block_budget_id %]"><i class="fa fa-copy"></i> Duplicate</a>
+                  <a href="[% script_name %]?op=duplicate_form&amp;budget_period_id=[% block_budget.budget_period_id %]"><i class="fa fa-copy"></i> Duplicate</a>
               </li>
               <li>
-                  <a href="[% script_name %]?op=close_form&amp;budget_period_id=[% block_budget_id %]"><i class="fa fa-times-circle"></i> Close</a>
+                  <a href="[% script_name %]?op=close_form&amp;budget_period_id=[% block_budget.budget_period_id %]"><i class="fa fa-times-circle"></i> Close</a>
               </li>
               <li>
-                  <a href="/cgi-bin/koha/admin/aqbudgets.pl?op=add_form&amp;budget_period_id=[% block_budget_id %]"><i class="fa fa-plus"></i> Add fund</a>
+                  <a href="/cgi-bin/koha/admin/aqbudgets.pl?op=add_form&amp;budget_period_id=[% block_budget.budget_period_id %]"><i class="fa fa-plus"></i> Add fund</a>
               </li>
         </ul>
     </div>
     [% END %]
     [% IF ( delete_confirm ) %]
         <a href="/cgi-bin/koha/admin/aqbudgetperiods.pl">Budgets</a> &rsaquo;
-        [% IF ( total ) %]
+        [% IF ( total || funds_exist ) %]
             <span>Cannot delete budget '[% budget_period_description %]'</span>
         [% ELSE %]
             <span>Delete budget '[% budget_period_description %]'?</span>
         <h3>Cannot delete budget</h3>
         <p><strong>This record is used [% total %] times</strong>
         . Deletion is not possible.</p>
+    [% ELSIF ( funds_exist ) %]
+        <div class="dialog message">
+        <h3>Cannot delete budget</h3>
+        <p>This budget has funds attached. You must delete all attached funds before you can delete this budget.</p>
     [% ELSE %]
         <div class="dialog alert">
         <h3>Delete budget '[% budget_period_description %]'?</h3>
-    [% END %]
 
     <!-- ############################################################# -->
     <!-- "delete" and "cancel" buttons    -->
     <form action="[% script_name %]" method="post">
         <button type="submit" class="deny"><i class="fa fa-fw fa-remove"></i> No, do not delete</button>
     </form>
+    [% END %]
 
     </div>
 [% END %]
 [% 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">
                 </td>
                 <td class="data">[% period_active.budget_period_total | $Price %]</td>
                 <td>
-                    [% PROCESS action_menu block_budget_id=period_active.budget_period_id %]
+                    [% PROCESS action_menu block_budget=period_active %]
                 </td>
                 </tr>
               [% END %]
                   <td> [% IF ( period_loo.budget_period_locked ) %]<span style="color:green;">Locked</span>&nbsp;[% ELSE %][% END %] </td>
                   <td class="data">[% period_loo.budget_period_total | $Price %]</td>
                   <td>
-                        [% PROCESS action_menu block_budget_id=period_loo.budget_period_id %]
+                        [% PROCESS action_menu block_budget=period_loo %]
                   </td>
                   </tr>
               [% END %]