From 15b3ad6e21827a81679623b4cb481067701664cf Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 18 Sep 2014 12:13:08 +0200 Subject: [PATCH] Bug 12958: Set a fund owner to a fund hierarchy This patch adds the ability to set an owner to a fund hierarchy On editing a fund, if it has children, a new checkbox appears "Set this owner to all children funds". If checked, all the fund hierarchy will herit to this owner. This will facilitate the fund owner modifications. Test plan: - Verify that the new checkbox only appears if the fund has at least a child. - Create a consistent fund hierarchy, something like: fund1 fund11 fund111 fund12 fund2 fund21 - Try to modify a fund owner without checking the checkbox. Verify the children have not been modified. - Try to modify a fund owner with checking the checkbox. Verify all fund hierarchy has been modified. Signed-off-by: Frederic Demians It works as announced. --- C4/Budgets.pm | 21 +++++++ admin/aqbudgets.pl | 9 +++ .../prog/en/modules/admin/aqbudgets.tt | 9 +++ t/db_dependent/Budgets.t | 65 +++++++++++++++++++++- 4 files changed, 103 insertions(+), 1 deletion(-) diff --git a/C4/Budgets.pm b/C4/Budgets.pm index 1a7f7e4..0cd7c93 100644 --- a/C4/Budgets.pm +++ b/C4/Budgets.pm @@ -187,6 +187,27 @@ sub BudgetHasChildren { return $sum->{'sum'}; } +sub GetBudgetChildren { + my ( $budget_id ) = @_; + my $dbh = C4::Context->dbh; + return $dbh->selectall_arrayref(q| + SELECT * FROM aqbudgets + WHERE budget_parent_id = ? + |, { Slice => {} }, $budget_id ); +} + +sub SetOwnerToFundHierarchy { + my ( $budget_id, $borrowernumber ) = @_; + + my $budget = GetBudget( $budget_id ); + $budget->{budget_owner_id} = $borrowernumber; + ModBudget( $budget ); + my $children = GetBudgetChildren( $budget_id ); + for my $child ( @$children ) { + SetOwnerToFundHierarchy( $child->{budget_id}, $borrowernumber ); + } +} + # ------------------------------------------------------------------- sub GetBudgetsPlanCell { my ( $cell, $period, $budget ) = @_; diff --git a/admin/aqbudgets.pl b/admin/aqbudgets.pl index d312ae8..2217633 100755 --- a/admin/aqbudgets.pl +++ b/admin/aqbudgets.pl @@ -211,6 +211,7 @@ if ($op eq 'add_form') { # if no buget_id is passed then its an add $template->param( + budget_has_children => BudgetHasChildren( $budget->{budget_id} ), budget_parent_id => $budget_parent->{'budget_id'}, budget_parent_name => $budget_parent->{'budget_name'}, branchloop_select => \@branchloop_select, @@ -241,12 +242,14 @@ if ($op eq 'add_form') { @budgetusersid = split(':', $budget_users_ids); } + my $budget_modified = 0; if (defined $budget_id) { if (CanUserModifyBudget($borrowernumber, $budget_hash->{budget_id}, $staffflags) ) { ModBudget( $budget_hash ); ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid); + $budget_modified = 1; } else { $template->param(error_not_authorised_to_modify => 1); @@ -254,6 +257,12 @@ if ($op eq 'add_form') { } else { $budget_hash->{budget_id} = AddBudget( $budget_hash ); ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid); + $budget_modified = 1; + } + + my $set_owner_to_children = $input->param('set_owner_to_children'); + if ( $set_owner_to_children and $budget_modified ) { + C4::Budgets::SetOwnerToFundHierarchy( $budget_hash->{budget_id}, $budget_hash->{budget_owner_id} ); } $op = 'list'; } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgets.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgets.tt index b0c4344..d6d23bb 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgets.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgets.tt @@ -473,6 +473,15 @@ var MSG_PARENT_BENEATH_BUDGET = "- " + _("New budget-parent is beneath budget") onclick="ownerRemove(); return false;" /> + [% IF budget_has_children %] +
  • + + + + +
  • + [% END %] +
  • Users:
      diff --git a/t/db_dependent/Budgets.t b/t/db_dependent/Budgets.t index c3e2ef4..d38d1a9 100755 --- a/t/db_dependent/Budgets.t +++ b/t/db_dependent/Budgets.t @@ -1,5 +1,5 @@ use Modern::Perl; -use Test::More tests => 108; +use Test::More tests => 120; BEGIN { use_ok('C4::Budgets') @@ -9,6 +9,7 @@ use C4::Biblio; use C4::Bookseller; use C4::Acquisition; use C4::Dates; +use C4::Members qw( AddMember ); use YAML; my $dbh = C4::Context->dbh; @@ -95,6 +96,14 @@ is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget p # Budget : # +# The budget hierarchy will be: +# budget_1 +# budget_11 +# budget_111 +# budget_12 +# budget_2 +# budget_21 + is( AddBudget(), undef, 'AddBuget without argument returns undef' ); my $budgets = GetBudgets(); is( @$budgets, 0, 'GetBudgets returns the correct number of budgets' ); @@ -512,6 +521,60 @@ for my $new_budget ( @new_budgets ) { is( $new_budget->{budget_amount} + 0, $new_budget_amount_should_be, "MoveOrders updated the budget amount with the previous unspent budget (for budget $new_budget->{budget_code})" ); } +# Test SetOwnerToFundHierarchy + +my $categorycode = 'S'; +my $branchcode = 'CPL'; +my $john_doe = C4::Members::AddMember( + cardnumber => '123456', + firstname => 'John', + surname => 'Doe', + categorycode => $categorycode, + branchcode => $branchcode, + dateofbirth => '', + dateexpiry => '9999-12-31', + userid => 'john.doe' +); + +C4::Budgets::SetOwnerToFundHierarchy( $budget_id1, $john_doe ); +is( C4::Budgets::GetBudget($budget_id1)->{budget_owner_id}, + $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 1 ($budget_id1)" ); +is( C4::Budgets::GetBudget($budget_id11)->{budget_owner_id}, + $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 11 ($budget_id11)" ); +is( C4::Budgets::GetBudget($budget_id111)->{budget_owner_id}, + $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 111 ($budget_id111)" ); +is( C4::Budgets::GetBudget($budget_id12)->{budget_owner_id}, + $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 12 ($budget_id12 )" ); +is( C4::Budgets::GetBudget($budget_id2)->{budget_owner_id}, + undef, "SetOwnerToFundHierarchy should not have set an owner for budget 2 ($budget_id2)" ); +is( C4::Budgets::GetBudget($budget_id21)->{budget_owner_id}, + undef, "SetOwnerToFundHierarchy should not have set an owner for budget 21 ($budget_id21)" ); + +my $jane_doe = C4::Members::AddMember( + cardnumber => '789012', + firstname => 'Jane', + surname => 'Doe', + categorycode => $categorycode, + branchcode => $branchcode, + dateofbirth => '', + dateexpiry => '9999-12-31', + userid => 'jane.doe' +); + +C4::Budgets::SetOwnerToFundHierarchy( $budget_id11, $jane_doe ); +is( C4::Budgets::GetBudget($budget_id1)->{budget_owner_id}, + $john_doe, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 1 ($budget_id1)" ); +is( C4::Budgets::GetBudget($budget_id11)->{budget_owner_id}, + $jane_doe, "SetOwnerToFundHierarchy should have set John Doe $jane_doe for budget 11 ($budget_id11)" ); +is( C4::Budgets::GetBudget($budget_id111)->{budget_owner_id}, + $jane_doe, "SetOwnerToFundHierarchy should have set John Doe $jane_doe for budget 111 ($budget_id111)" ); +is( C4::Budgets::GetBudget($budget_id12)->{budget_owner_id}, + $john_doe, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 12 ($budget_id12 )" ); +is( C4::Budgets::GetBudget($budget_id2)->{budget_owner_id}, + undef, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 2 ($budget_id2)" ); +is( C4::Budgets::GetBudget($budget_id21)->{budget_owner_id}, + undef, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 21 ($budget_id21)" ); + sub _get_dependencies { my ($budget_hierarchy) = @_; my $graph; -- 2.1.2