From 898ab81a7b70515725eb0d97a9595248532dfbc1 Mon Sep 17 00:00:00 2001 From: Maxime Pelletier Date: Tue, 30 Apr 2013 17:27:32 -0400 Subject: [PATCH] Add new features and tests to the budget duplication : * Allow to enter a new name for the duplicate * Add option to transfer the not received orders in the budget copy * Transfer the duplication code in Budgets.pm to allow easier testing. In doing this I removed the code block " # deal with any children" because I just don't understand it and it had a typo in the table name ('aqcudgets'), so I figured it was useless code * Update Budgets.t with the right field names, test the data more and tests the duplication code. Sadly I didn't write tests for the order transfers because it just depends on too much stuff :/ http://bugs.koha-community.org/show_bug.cgi?id=7498 Signed-off-by: Cedric Vita Signed-off-by: Jonathan Druart --- C4/Budgets.pm | 61 ++++++++++++++++++++ admin/aqbudgetperiods.pl | 55 ++---------------- .../prog/en/modules/admin/aqbudgetperiods.tt | 10 ++++ t/db_dependent/Budgets.t | 38 +++++++++--- 4 files changed, 106 insertions(+), 58 deletions(-) diff --git a/C4/Budgets.pm b/C4/Budgets.pm index 2c78284..5221802 100644 --- a/C4/Budgets.pm +++ b/C4/Budgets.pm @@ -867,6 +867,67 @@ 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 diff --git a/admin/aqbudgetperiods.pl b/admin/aqbudgetperiods.pl index 9371d01..c2aa661 100755 --- a/admin/aqbudgetperiods.pl +++ b/admin/aqbudgetperiods.pl @@ -173,66 +173,23 @@ 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, ); } # handle the actual duplication 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'} = $startdate; - $data->{'budget_period_enddate'} = $enddate; - 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; - - # deal with any children - if( defined $parent_children{$old_id} ){ - # tell my children my new id - for my $child ( @{$parent_children{$old_id}} ){ - C4::SQLHelper::UpdateInTable('aqcudgets', [ 'budget_id' => $child, 'budget_parent_id' => $new_id ]); - } - 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'; 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 8a4a4ea..86a3600 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tt @@ -199,6 +199,16 @@
[% INCLUDE 'date-format.inc' %]
+
  • + + +
  • + +
  • + + +
  • + diff --git a/t/db_dependent/Budgets.t b/t/db_dependent/Budgets.t index 717f8fe..0f4302b 100755 --- a/t/db_dependent/Budgets.t +++ b/t/db_dependent/Budgets.t @@ -1,6 +1,6 @@ use strict; use warnings; -use Test::More tests=>18; +use Test::More tests=>29; BEGIN {use_ok('C4::Budgets') } use C4::Dates; @@ -16,13 +16,13 @@ my $active_period; my $mod_status; my $del_status; ok($bpid=AddBudgetPeriod( - { budget_period_startdate => '2008-01-01' - , budget_period_enddate => '2008-12-31' - , budget_description => "MAPERI"}), - "AddBudgetPeriod with iso dates OK"); + { budget_period_startdate => '2008-01-01' + , budget_period_enddate => '2008-12-31' + , budget_period_description => "MAPERI"}), + "AddBudgetPeriod with iso dates OK"); ok($budgetperiod=GetBudgetPeriod($bpid), - "GetBudgetPeriod($bpid) returned ".Dump($budgetperiod)); + "GetBudgetPeriod($bpid) returned ".Dump($budgetperiod)); ok(!GetBudgetPeriod(0) ,"GetBudgetPeriod(0) returned undef : noactive BudgetPeriod"); $$budgetperiod{budget_period_active}=1; ok($mod_status=ModBudgetPeriod($budgetperiod),"ModBudgetPeriod OK"); @@ -39,20 +39,20 @@ if (C4::Context->preference('dateformat') eq "metric"){ ok($bpid=AddBudgetPeriod( { budget_period_startdate =>'01-01-2008' , budget_period_enddate =>'31-12-2008' - , budget_description =>"MAPERI"}), + , 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_description =>"MAPERI"}), + , budget_period_description =>"MAPERI"}), "AddBudgetPeriod returned $bpid"); } else{ ok($bpid=AddBudgetPeriod( {budget_period_startdate=>'2008-01-01' ,budget_period_enddate =>'2008-12-31' - ,budget_description =>"MAPERI" + ,budget_period_description =>"MAPERI" }), "AddBudgetPeriod returned $bpid"); @@ -102,5 +102,25 @@ ok(GetBudgets({budget_period_id=>GetBudgetPeriod($bpid)->{budget_period_id}},[{" my $budget_name = GetBudgetName( $budget_id ); is($budget_name, $budget->{budget_name}, "Test the GetBudgetName routine"); +my $new_bpid; +ok($new_bpid = C4::Budgets::DuplicateBudget($bpid, '2012-01-01', '2012-08-01', 'MAPERI COPY'), + "Duplicate budget returned $new_bpid"); + +my $budgetcpyp; +ok($budgetcpyp = GetBudgetPeriod($new_bpid) ,"GetBudgetPeriod OK"); + +#Verify data of the copy +is ($budgetcpyp->{'budget_period_startdate'}, '2012-01-01'); +is ($budgetcpyp->{'budget_period_enddate'}, '2012-08-01'); +is ($budgetcpyp->{'budget_period_description'}, 'MAPERI COPY'); + +my $budgetscpy; +ok($budgetscpy = GetBudgetHierarchy( $new_bpid )); +is (scalar(@$budgetscpy), 2, '2 budgets for that budget period : the original and his child'); +is (@$budgetscpy[0]->{'budget_code'}, 'ABCD'); +is (@$budgetscpy[0]->{'budget_amount'}, '123.132000'); +is (@$budgetscpy[0]->{'budget_notes'}, 'This is a note'); +is (@$budgetscpy[0]->{'budget_name'}, 'Serials'); + ok($del_status=DelBudget($budget_id), "DelBudget returned $del_status"); -- 1.7.10.4