Lines 499-570
sub GetBudgetHierarchy {
Link Here
|
499 |
$debug && warn $query,join(",",@bind_params); |
499 |
$debug && warn $query,join(",",@bind_params); |
500 |
my $sth = $dbh->prepare($query); |
500 |
my $sth = $dbh->prepare($query); |
501 |
$sth->execute(@bind_params); |
501 |
$sth->execute(@bind_params); |
502 |
my $results = $sth->fetchall_arrayref({}); |
|
|
503 |
my @res = @$results; |
504 |
my $i = 0; |
505 |
while (1) { |
506 |
my $depth_cnt = 0; |
507 |
foreach my $r (@res) { |
508 |
my @child; |
509 |
# look for children |
510 |
$r->{depth} = '0' if !defined $r->{budget_parent_id}; |
511 |
foreach my $r2 (@res) { |
512 |
if (defined $r2->{budget_parent_id} |
513 |
&& $r2->{budget_parent_id} == $r->{budget_id}) { |
514 |
push @child, $r2->{budget_id}; |
515 |
$r2->{depth} = ($r->{depth} + 1) if defined $r->{depth}; |
516 |
} |
517 |
} |
518 |
$r->{child} = \@child if scalar @child > 0; # add the child |
519 |
$depth_cnt++ if !defined $r->{'depth'}; |
520 |
} |
521 |
last if ($depth_cnt == 0 || $i == 100); |
522 |
$i++; |
523 |
} |
524 |
|
502 |
|
525 |
# look for top parents 1st |
503 |
my %links; |
526 |
my (@sort, $depth_count); |
504 |
# create hash with budget_id has key |
527 |
($i, $depth_count) = 0; |
505 |
while ( my $data = $sth->fetchrow_hashref ) { |
528 |
while (1) { |
506 |
$links{ $data->{'budget_id'} } = $data; |
529 |
my $children = 0; |
507 |
} |
530 |
foreach my $r (@res) { |
|
|
531 |
if ($r->{depth} == $depth_count) { |
532 |
$children++ if (ref $r->{child} eq 'ARRAY'); |
533 |
|
534 |
# find the parent id element_id and insert it after |
535 |
my $i2 = 0; |
536 |
my $parent; |
537 |
if ($depth_count > 0) { |
538 |
|
539 |
# add indent |
540 |
my $depth = $r->{depth} * 2; |
541 |
$r->{budget_code_indent} = $r->{budget_code}; |
542 |
$r->{budget_name_indent} = $r->{budget_name}; |
543 |
foreach my $r3 (@sort) { |
544 |
if ($r3->{budget_id} == $r->{budget_parent_id}) { |
545 |
$parent = $i2; |
546 |
last; |
547 |
} |
548 |
$i2++; |
549 |
} |
550 |
} else { |
551 |
$r->{budget_code_indent} = $r->{budget_code}; |
552 |
$r->{budget_name_indent} = $r->{budget_name}; |
553 |
} |
554 |
|
555 |
if (defined $parent) { |
556 |
splice @sort, ($parent + 1), 0, $r; |
557 |
} else { |
558 |
push @sort, $r; |
559 |
} |
560 |
} |
561 |
|
562 |
$i++; |
563 |
} # --------------foreach |
564 |
$depth_count++; |
565 |
last if $children == 0; |
566 |
} |
567 |
|
508 |
|
|
|
509 |
# link child to parent |
510 |
my @first_parents; |
511 |
foreach ( sort keys %links ) { |
512 |
my $child = $links{$_}; |
513 |
if ( $child->{'budget_parent_id'} ) { |
514 |
my $parent = $links{ $child->{'budget_parent_id'} }; |
515 |
if ($parent) { |
516 |
unless ( $parent->{'children'} ) { |
517 |
# init child arrayref |
518 |
$parent->{'children'} = []; |
519 |
} |
520 |
# add as child |
521 |
push @{ $parent->{'children'} }, $child; |
522 |
} |
523 |
} else { |
524 |
push @first_parents, $child; |
525 |
} |
526 |
} |
527 |
|
528 |
my @sort = (); |
529 |
foreach my $first_parent (@first_parents) { |
530 |
_add_budget_children(\@sort, $first_parent); |
531 |
} |
568 |
|
532 |
|
569 |
foreach my $budget (@sort) { |
533 |
foreach my $budget (@sort) { |
570 |
$budget->{budget_spent} = GetBudgetSpent( $budget->{budget_id} ); |
534 |
$budget->{budget_spent} = GetBudgetSpent( $budget->{budget_id} ); |
Lines 575-580
sub GetBudgetHierarchy {
Link Here
|
575 |
return \@sort; |
539 |
return \@sort; |
576 |
} |
540 |
} |
577 |
|
541 |
|
|
|
542 |
# Recursive method to add a budget and its chidren to an array |
543 |
sub _add_budget_children { |
544 |
my $res = shift; |
545 |
my $budget = shift; |
546 |
push @$res, $budget; |
547 |
my $children = $budget->{'children'} || []; |
548 |
return unless @$children; # break recursivity |
549 |
foreach my $child (@$children) { |
550 |
_add_budget_children($res, $child); |
551 |
} |
552 |
} |
553 |
|
578 |
# ------------------------------------------------------------------- |
554 |
# ------------------------------------------------------------------- |
579 |
|
555 |
|
580 |
sub AddBudget { |
556 |
sub AddBudget { |