diff --git a/C4/Budgets.pm b/C4/Budgets.pm
index 1a7f7e4689..0cd7c93357 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 287cf139c7..60463870f7 100755
--- a/admin/aqbudgets.pl
+++ b/admin/aqbudgets.pl
@@ -210,6 +210,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,
@@ -240,12 +241,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);
@@ -253,6 +256,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 bec6272ffd..0f1d4b27f0 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgets.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgets.tt
@@ -474,6 +474,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 3ac733cc1c..11838c840b 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 Koha::Acquisition::Order;
@@ -97,6 +98,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' );
@@ -516,6 +525,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;