diff --git a/C4/Budgets.pm b/C4/Budgets.pm index f2f376a34d..6d8bf21db0 100644 --- a/C4/Budgets.pm +++ b/C4/Budgets.pm @@ -529,8 +529,8 @@ sub GetBudgetHierarchy { # link child to parent my @first_parents; - foreach ( sort keys %links ) { - my $child = $links{$_}; + foreach my $budget ( sort { $a->{budget_code} cmp $b->{budget_code} } values %links ) { + my $child = $links{$budget->{budget_id}}; if ( $child->{'budget_parent_id'} ) { my $parent = $links{ $child->{'budget_parent_id'} }; if ($parent) { diff --git a/t/db_dependent/Budgets.t b/t/db_dependent/Budgets.t index 11838c840b..d74213cbdb 100755 --- a/t/db_dependent/Budgets.t +++ b/t/db_dependent/Budgets.t @@ -1,5 +1,5 @@ use Modern::Perl; -use Test::More tests => 120; +use Test::More tests => 122; BEGIN { use_ok('C4::Budgets') @@ -226,15 +226,6 @@ my $budget_id2 = AddBudget( budget_amount => $budget_2_total, } ); -my $budget_id11 = AddBudget( - { - budget_code => 'budget_11', - budget_name => 'budget_11', - budget_period_id => $budget_period_id, - budget_parent_id => $budget_id1, - budget_amount => $budget_11_total, - } -); my $budget_id12 = AddBudget( { budget_code => 'budget_12', @@ -244,6 +235,15 @@ my $budget_id12 = AddBudget( budget_amount => $budget_12_total, } ); +my $budget_id11 = AddBudget( + { + budget_code => 'budget_11', + budget_name => 'budget_11', + budget_period_id => $budget_period_id, + budget_parent_id => $budget_id1, + budget_amount => $budget_11_total, + } +); my $budget_id111 = AddBudget( { budget_code => 'budget_111', @@ -424,6 +424,10 @@ $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod( } ); +$budget_hierarchy = GetBudgetHierarchy($budget_period_id); +is( $budget_hierarchy->[0]->{children}->[0]->{budget_name}, 'budget_11', 'GetBudgetHierarchy should return budgets ordered by name, first child is budget_11' ); +is( $budget_hierarchy->[0]->{children}->[1]->{budget_name}, 'budget_12', 'GetBudgetHierarchy should return budgets ordered by name, second child is budget_12' ); + $budget_hierarchy = GetBudgetHierarchy($budget_period_id); $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);