@@ -, +, @@ --- C4/Budgets.pm | 62 ++++++++++++++++++++ admin/aqbudgetperiods.pl | 8 +++ .../prog/en/modules/admin/aqbudgetperiods.tt | 10 ++++ t/db_dependent/Budgets.t | 52 +++++++++++++--- 4 files changed, 125 insertions(+), 7 deletions(-) --- a/C4/Budgets.pm +++ a/C4/Budgets.pm @@ -921,6 +921,68 @@ sub CanUserModifyBudget { # ------------------------------------------------------------------- +sub DuplicateBudget { + my ($budget_period_id, $startdate, $enddate, $budget_period_description, $budget_transfer_data) = @_; + die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' ); + + my $dbh = C4::Context->dbh; + my $data = GetBudgetPeriod( $budget_period_id); + + $data->{'budget_period_startdate'} = $startdate; + $data->{'budget_period_enddate'} = $enddate; + $data->{'budget_period_description'} = $budget_period_description; + delete $data->{'budget_period_id'}; + my $new_budget_period_id = C4::SQLHelper::InsertInTable('aqbudgetperiods', $data); + + my $tree = GetBudgetHierarchy( $budget_period_id ); + + # hash mapping old ids to new + my %old_new; + # hash mapping old parent ids to list of new children ids + # only store a child here if the parents old id isnt in the old_new map + # when the parent is found, this map will be used, and then the entry removed and their id placed in old_new + my %parent_children; + + for my $entry( @$tree ){ + die "serious errors, parent period $budget_period_id doesnt match child ", $entry->{'budget_period_id'}, "\n" if( $entry->{'budget_period_id'} != $budget_period_id ); + my $orphan = 0; # set to 1 if we need to make an entry in parent_children + my $old_id = delete $entry->{'budget_id'}; + my $parent_id = delete $entry->{'budget_parent_id'}; + $entry->{'budget_period_id'} = $new_budget_period_id; + + if( !defined $parent_id ){ + } elsif( defined $parent_id && $parent_id eq '' ){ + } elsif( defined $old_new{$parent_id} ){ + # set parent id now + $entry->{'budget_parent_id'} = $old_new{$parent_id}; + } else { + # make an entry in parent_children + $parent_children{$parent_id} = [] unless defined $parent_children{$parent_id}; + $orphan = 1; + } + + # write it to db + my $new_id = C4::SQLHelper::InsertInTable('aqbudgets', $entry); + $old_new{$old_id} = $new_id; + push @{$parent_children{$parent_id}}, $new_id if $orphan; + + if ($budget_transfer_data == 1) { + #Transfers the unreceived orders from the previous fund to the new + my $query = " + UPDATE aqorders SET budget_id = ? + WHERE budget_id=? AND + quantityreceived = 0 AND + datecancellationprinted IS NULL"; + my $sth = $dbh->prepare($query); + $sth->execute($new_id, $old_id ); + } + } + + return $new_budget_period_id; +} + +# ------------------------------------------------------------------- + =head2 GetCurrencies @currencies = &GetCurrencies; --- a/admin/aqbudgetperiods.pl +++ a/admin/aqbudgetperiods.pl @@ -176,9 +176,11 @@ elsif ( $op eq 'delete_confirmed' ) { # display the form for duplicating elsif ( $op eq 'duplicate_form'){ + my $budgetperiod = GetBudgetPeriod($budget_period_id, $input); $template->param( 'duplicate_form' => '1', 'budget_period_id' => $budget_period_id, + 'budgetperiod' => $budgetperiod, ); } @@ -186,6 +188,9 @@ elsif ( $op eq 'duplicate_form'){ elsif ( $op eq 'duplicate_budget' ){ die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' ); + my $startdate = $input->param('budget_period_startdate'); + my $enddate = $input->param('budget_period_enddate'); + my $data = GetBudgetPeriod( $budget_period_id); $data->{'budget_period_startdate'} = $budget_period_hashref->{budget_period_startdate}; $data->{'budget_period_enddate'} = $budget_period_hashref->{budget_period_enddate}; @@ -236,6 +241,9 @@ elsif ( $op eq 'duplicate_budget' ){ delete $parent_children{$old_id}; } } + my $budget_period_description = $input->param('budget_period_description'); + my $budget_transfer_data = $input->param('budget_transfer_data'); + C4::Budgets::DuplicateBudget($budget_period_id, $startdate, $enddate, $budget_period_description, $budget_transfer_data); # display the list of budgets $op = 'else'; --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tt @@ -185,6 +185,16 @@
[% INCLUDE 'date-format.inc' %]
+
  • + + +
  • + +
  • + + +
  • + --- a/t/db_dependent/Budgets.t +++ a/t/db_dependent/Budgets.t @@ -1,5 +1,5 @@ use Modern::Perl; -use Test::More tests => 63; +use Test::More tests => 75; BEGIN { use_ok('C4::Budgets') @@ -84,6 +84,26 @@ is( DelBudgetPeriod($bpid), 1, 'DelBudgetPeriod returns true' ); $budgetperiods = GetBudgetPeriods(); is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' ); +if (C4::Context->preference('dateformat') eq "metric"){ + ok($bpid=AddBudgetPeriod( + { budget_period_startdate =>'01-01-2008' + , budget_period_enddate =>'31-12-2008' + , budget_period_description =>"MAPERI"}), + "AddBudgetPeriod returned $bpid"); + } elsif (C4::Context->preference('dateformat') eq "us"){ + ok($bpid=AddBudgetPeriod( + { budget_period_startdate =>'01-01-2008' + , budget_period_enddate =>'12-31-2008' + , budget_period_description =>"MAPERI"}), + "AddBudgetPeriod returned $bpid"); + } + else{ + ok($bpid=AddBudgetPeriod( + {budget_period_startdate=>'2008-01-01' + ,budget_period_enddate =>'2008-12-31' + ,budget_period_description =>"MAPERI" + }), + "AddBudgetPeriod returned $bpid"); # # Budget : @@ -110,7 +130,6 @@ is( $budget->{budget_name}, $my_budget->{budget_name}, 'AddBudget stores the bud is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'AddBudget stores the budget notes correctly' ); is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'AddBudget stores the budget period id correctly' ); - $my_budget = { budget_code => 'EFG', budget_amount => '321.231000', @@ -131,7 +150,6 @@ is( $budget->{budget_name}, $my_budget->{budget_name}, 'ModBudget updates the bu is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'ModBudget updates the budget notes correctly' ); is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'ModBudget updates the budget period id correctly' ); - $budgets = GetBudgets(); is( @$budgets, 1, 'GetBudgets returns the correct number of budgets' ); is( $budgets->[0]->{budget_id}, $my_budget->{budget_id}, 'GetBudgets returns the budget id correctly' ); @@ -148,7 +166,6 @@ is( @$budgets, 1, 'GetBudgets With Order OK' ); $budgets = GetBudgets( {budget_period_id => GetBudgetPeriod($bpid)->{budget_period_id}}, {-asc => "budget_name"} ); is( @$budgets, 1, 'GetBudgets With Order Getting Active budgetPeriod OK'); - my $budget_name = GetBudgetName( $budget_id ); is($budget_name, $my_budget->{budget_name}, "Test the GetBudgetName routine"); @@ -169,10 +186,29 @@ isnt( $second_budget_id, undef, 'AddBudget does not returns undef' ); $budgets = GetBudgets( {budget_period_id => $bpid} ); ok( $budgets->[0]->{budget_name} lt $budgets->[1]->{budget_name}, 'default sort order for GetBudgets is by name' ); -is( DelBudget($budget_id), 1, 'DelBudget returns true' ); -$budgets = GetBudgets(); -is( @$budgets, 1, 'GetBudgets returns the correct number of budget periods' ); +my $new_bpid; +ok($new_bpid = C4::Budgets::DuplicateBudget($bpid, '2009-01-01', '2009-12-31', 'MAPERI COPY'), + "Duplicate budget returned $new_bpid"); + +my $budgetcpyp; +ok($budgetcpyp = GetBudgetPeriod($new_bpid) ,"GetBudgetPeriod OK"); +is ($budgetcpyp->{'budget_period_startdate'}, '2009-01-01'); +is ($budgetcpyp->{'budget_period_enddate'}, '2009-12-31'); +is ($budgetcpyp->{'budget_period_description'}, 'MAPERI COPY'); + +my $budgetscpy; +ok($budgetscpy = GetBudgetHierarchy( $bpid )); +is (scalar(@$budgetscpy), 2, '2 budgets for that budget period : the original and his child'); +is (@$budgetscpy[0]->{'budget_code'}, 'EFG'); +is (@$budgetscpy[0]->{'budget_amount'}, '321.231000'); +is (@$budgetscpy[0]->{'budget_notes'}, 'This is a modified note'); +is (@$budgetscpy[0]->{'budget_name'}, 'Modified name'); + +my $budgetCount = @{GetBudgets()}; +is( DelBudget($second_budget_id), 1, 'DelBudget returns true' ); +$budgets = GetBudgets(); +is( @$budgets, $budgetCount-1, 'GetBudgets returns the correct number of budget periods' ); # GetBudgetHierarchySpent and GetBudgetHierarchyOrdered my $budget_period_total = 10_000; @@ -352,3 +388,5 @@ for my $infos (@order_infos) { is( GetBudgetHierarchySpent( $budget_id1 ), 160, "total spent for budget1 is 160" ); is( GetBudgetHierarchySpent( $budget_id11 ), 100, "total spent for budget11 is 100" ); is( GetBudgetHierarchySpent( $budget_id111 ), 20, "total spent for budget111 is 20" ); + +} --