From 9c81343bf6d050d34a4d6f15766025434efafb01 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Tue, 13 Oct 2015 12:37:23 +0200 Subject: [PATCH] Bug 15004: Allow to change amounts of duplicated budgets When duplicating a budget we now have the possibility to change amounts of budget and funds by a given percentage. Additionally, we can configure how to round the amounts. Test plan: 1. Create a budget and several funds with different amounts 2. Duplicate it using the 2 new options 3. Check that the amounts are correct Signed-off-by: Frederic Demians It works exactly as advertised. Was happy to see that decimal point can be used: for example, increase a budget by 2.6%, and round the amound to 0.5. Signed-off-by: Jonathan Druart Signed-off-by: Brendan A Gallagher --- C4/Budgets.pm | 28 +++++++++++++++++++ admin/aqbudgetperiods.pl | 4 +++ .../prog/en/modules/admin/aqbudgetperiods.tt | 12 ++++++++ t/db_dependent/Budgets.t | 23 ++++++++++++++- 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/C4/Budgets.pm b/C4/Budgets.pm index 79012ba329..1c30e1bf21 100644 --- a/C4/Budgets.pm +++ b/C4/Budgets.pm @@ -985,6 +985,15 @@ sub ConvertCurrency { return ( $price / $cur ); } +sub _round { + my ($value, $increment) = @_; + + if ($increment && $increment != 0) { + $value = int($value / $increment) * $increment; + } + + return $value; +} =head2 CloneBudgetPeriod @@ -1011,6 +1020,8 @@ sub CloneBudgetPeriod { my $budget_period_startdate = $params->{budget_period_startdate}; my $budget_period_enddate = $params->{budget_period_enddate}; my $budget_period_description = $params->{budget_period_description}; + my $amount_change_percentage = $params->{amount_change_percentage}; + my $amount_change_round_increment = $params->{amount_change_round_increment}; my $mark_original_budget_as_inactive = $params->{mark_original_budget_as_inactive} || 0; my $reset_all_budgets = $params->{reset_all_budgets} || 0; @@ -1022,6 +1033,14 @@ sub CloneBudgetPeriod { $budget_period->{budget_period_description} = $budget_period_description; # The new budget (budget_period) should be active by default $budget_period->{budget_period_active} = 1; + + if ($amount_change_percentage) { + my $total = $budget_period->{budget_period_total}; + $total += $total * $amount_change_percentage / 100; + $total = _round($total, $amount_change_round_increment); + $budget_period->{budget_period_total} = $total; + } + my $original_budget_period_id = $budget_period->{budget_period_id}; delete $budget_period->{budget_period_id}; my $new_budget_period_id = AddBudgetPeriod( $budget_period ); @@ -1049,6 +1068,15 @@ sub CloneBudgetPeriod { $budget->{budget_amount} = 0; ModBudget( $budget ); } + } elsif ($amount_change_percentage) { + my $budgets = GetBudgets({ budget_period_id => $new_budget_period_id }); + for my $budget ( @$budgets ) { + my $amount = $budget->{budget_amount}; + $amount += $amount * $amount_change_percentage / 100; + $amount = _round($amount, $amount_change_round_increment); + $budget->{budget_amount} = $amount; + ModBudget( $budget ); + } } return $new_budget_period_id; diff --git a/admin/aqbudgetperiods.pl b/admin/aqbudgetperiods.pl index f837b818a1..93cef8a8eb 100755 --- a/admin/aqbudgetperiods.pl +++ b/admin/aqbudgetperiods.pl @@ -163,6 +163,8 @@ elsif ( $op eq 'duplicate_budget' ){ my $budget_period_startdate = dt_from_string $input->param('budget_period_startdate'); my $budget_period_enddate = dt_from_string $input->param('budget_period_enddate'); my $budget_period_description = $input->param('budget_period_description'); + my $amount_change_percentage = $input->param('amount_change_percentage'); + my $amount_change_round_increment = $input->param('amount_change_round_increment'); my $mark_original_budget_as_inactive = $input->param('mark_original_budget_as_inactive'); my $reset_all_budgets = $input->param('reset_all_budgets'); @@ -172,6 +174,8 @@ elsif ( $op eq 'duplicate_budget' ){ budget_period_startdate => $budget_period_startdate, budget_period_enddate => $budget_period_enddate, budget_period_description => $budget_period_description, + amount_change_percentage => $amount_change_percentage, + amount_change_round_increment => $amount_change_round_increment, mark_original_budget_as_inactive => $mark_original_budget_as_inactive, reset_all_budgets => $reset_all_budgets, } 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 0d73b859d9..f9ab2091c5 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tt @@ -284,6 +284,18 @@ +
  • + + % +
    (can be positive or negative)
    +
  • + +
  • + + +
    (amounts will be rounded down)
    +
  • +
  • diff --git a/t/db_dependent/Budgets.t b/t/db_dependent/Budgets.t index 9c34a8f0f0..91a255f66a 100755 --- a/t/db_dependent/Budgets.t +++ b/t/db_dependent/Budgets.t @@ -1,5 +1,5 @@ use Modern::Perl; -use Test::More tests => 122; +use Test::More tests => 129; BEGIN { use_ok('C4::Budgets') @@ -469,6 +469,27 @@ is( $number_of_budgets_not_reset, 0, 'CloneBudgetPeriod has reset all budgets (funds)' ); +# CloneBudgetPeriod with param amount_change_* +$budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod( + { + budget_period_id => $budget_period_id, + budget_period_startdate => '2014-01-01', + budget_period_enddate => '2014-12-31', + amount_change_percentage => 16, + amount_change_round_increment => 5, + } +); + +$budget_period_cloned = C4::Budgets::GetBudgetPeriod($budget_period_id_cloned); +cmp_ok($budget_period_cloned->{budget_period_total}, '==', 11600, "CloneBudgetPeriod changed correctly budget amount"); +$budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned); +cmp_ok($budget_hierarchy_cloned->[0]->{budget_amount}, '==', 1160, "CloneBudgetPeriod changed correctly funds amounts"); +cmp_ok($budget_hierarchy_cloned->[1]->{budget_amount}, '==', 115, "CloneBudgetPeriod changed correctly funds amounts"); +cmp_ok($budget_hierarchy_cloned->[2]->{budget_amount}, '==', 55, "CloneBudgetPeriod changed correctly funds amounts"); +cmp_ok($budget_hierarchy_cloned->[3]->{budget_amount}, '==', 115, "CloneBudgetPeriod changed correctly funds amounts"); +cmp_ok($budget_hierarchy_cloned->[4]->{budget_amount}, '==', 2320, "CloneBudgetPeriod changed correctly funds amounts"); +cmp_ok($budget_hierarchy_cloned->[5]->{budget_amount}, '==', 0, "CloneBudgetPeriod changed correctly funds amounts"); + # MoveOrders my $number_orders_moved = C4::Budgets::MoveOrders(); is( $number_orders_moved, undef, 'MoveOrders return undef if no arg passed' ); -- 2.39.5