|
Lines 528-625
sub GetBudgetHierarchy {
Link Here
|
| 528 |
$debug && warn $query,join(",",@bind_params); |
528 |
$debug && warn $query,join(",",@bind_params); |
| 529 |
my $sth = $dbh->prepare($query); |
529 |
my $sth = $dbh->prepare($query); |
| 530 |
$sth->execute(@bind_params); |
530 |
$sth->execute(@bind_params); |
| 531 |
my $results = $sth->fetchall_arrayref({}); |
|
|
| 532 |
my @res = @$results; |
| 533 |
my $i = 0; |
| 534 |
while (1) { |
| 535 |
my $depth_cnt = 0; |
| 536 |
foreach my $r (@res) { |
| 537 |
my @child; |
| 538 |
# look for children |
| 539 |
$r->{depth} = '0' if !defined $r->{budget_parent_id}; |
| 540 |
foreach my $r2 (@res) { |
| 541 |
if (defined $r2->{budget_parent_id} |
| 542 |
&& $r2->{budget_parent_id} == $r->{budget_id}) { |
| 543 |
push @child, $r2->{budget_id}; |
| 544 |
$r2->{depth} = ($r->{depth} + 1) if defined $r->{depth}; |
| 545 |
} |
| 546 |
} |
| 547 |
$r->{child} = \@child if scalar @child > 0; # add the child |
| 548 |
$depth_cnt++ if !defined $r->{'depth'}; |
| 549 |
} |
| 550 |
last if ($depth_cnt == 0 || $i == 100); |
| 551 |
$i++; |
| 552 |
} |
| 553 |
|
531 |
|
| 554 |
# look for top parents 1st |
532 |
my %links; |
| 555 |
my (@sort, $depth_count); |
533 |
# create hash with budget_id has key |
| 556 |
($i, $depth_count) = 0; |
534 |
while ( my $data = $sth->fetchrow_hashref ) { |
| 557 |
while (1) { |
535 |
$links{ $data->{'budget_id'} } = $data; |
| 558 |
my $children = 0; |
536 |
} |
| 559 |
foreach my $r (@res) { |
|
|
| 560 |
if ($r->{depth} == $depth_count) { |
| 561 |
$children++ if (ref $r->{child} eq 'ARRAY'); |
| 562 |
|
| 563 |
# find the parent id element_id and insert it after |
| 564 |
my $i2 = 0; |
| 565 |
my $parent; |
| 566 |
if ($depth_count > 0) { |
| 567 |
|
| 568 |
# add indent |
| 569 |
my $depth = $r->{depth} * 2; |
| 570 |
$r->{budget_code_indent} = $r->{budget_code}; |
| 571 |
$r->{budget_name_indent} = $r->{budget_name}; |
| 572 |
foreach my $r3 (@sort) { |
| 573 |
if ($r3->{budget_id} == $r->{budget_parent_id}) { |
| 574 |
$parent = $i2; |
| 575 |
last; |
| 576 |
} |
| 577 |
$i2++; |
| 578 |
} |
| 579 |
} else { |
| 580 |
$r->{budget_code_indent} = $r->{budget_code}; |
| 581 |
$r->{budget_name_indent} = $r->{budget_name}; |
| 582 |
} |
| 583 |
|
| 584 |
if (defined $parent) { |
| 585 |
splice @sort, ($parent + 1), 0, $r; |
| 586 |
} else { |
| 587 |
push @sort, $r; |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
$i++; |
| 592 |
} # --------------foreach |
| 593 |
$depth_count++; |
| 594 |
last if $children == 0; |
| 595 |
} |
| 596 |
|
537 |
|
| 597 |
# add budget-percent and allocation, and flags for html-template |
538 |
# link child to parent |
| 598 |
foreach my $r (@sort) { |
539 |
my @first_parents; |
| 599 |
my $subs_href = $r->{'child'}; |
540 |
foreach ( sort keys %links ) { |
| 600 |
my @subs_arr = (); |
541 |
my $child = $links{$_}; |
| 601 |
if ( defined $subs_href ) { |
542 |
if ( $child->{'budget_parent_id'} ) { |
| 602 |
@subs_arr = @{$subs_href}; |
543 |
my $parent = $links{ $child->{'budget_parent_id'} }; |
|
|
544 |
if ($parent) { |
| 545 |
unless ( $parent->{'children'} ) { |
| 546 |
# init child arrayref |
| 547 |
$parent->{'children'} = []; |
| 548 |
} |
| 549 |
# add as child |
| 550 |
push @{ $parent->{'children'} }, $child; |
| 551 |
} |
| 552 |
} else { |
| 553 |
push @first_parents, $child; |
| 603 |
} |
554 |
} |
|
|
555 |
} |
| 604 |
|
556 |
|
| 605 |
my $moo = $r->{'budget_code_indent'}; |
557 |
my @sort = (); |
|
|
558 |
foreach my $first_parent (@first_parents) { |
| 559 |
_add_budget_children(\@sort, $first_parent); |
| 560 |
} |
| 561 |
|
| 562 |
# add budget-percent and allocation, and flags for html-template |
| 563 |
foreach my $r (@sort) { |
| 564 |
my $subs = $r->{'children'} || []; |
| 565 |
|
| 566 |
my $moo = $r->{'budget_code'}; |
| 606 |
$moo =~ s/\ /\ \;/g; |
567 |
$moo =~ s/\ /\ \;/g; |
| 607 |
$r->{'budget_code_indent'} = $moo; |
568 |
$r->{'budget_code_indent'} = $moo; |
| 608 |
|
569 |
|
| 609 |
$moo = $r->{'budget_name_indent'}; |
570 |
$moo = $r->{'budget_name'}; |
| 610 |
$moo =~ s/\ /\ \;/g; |
571 |
$moo =~ s/\ /\ \;/g; |
| 611 |
$r->{'budget_name_indent'} = $moo; |
572 |
$r->{'budget_name_indent'} = $moo; |
| 612 |
|
573 |
|
| 613 |
$r->{'budget_spent'} = GetBudgetSpent( $r->{'budget_id'} ); |
574 |
$r->{'budget_spent'} = GetBudgetSpent( $r->{'budget_id'} ); |
| 614 |
|
575 |
|
| 615 |
$r->{'budget_amount_total'} = $r->{'budget_amount'}; |
576 |
$r->{'budget_amount_total'} = $r->{'budget_amount'}; |
| 616 |
|
577 |
|
| 617 |
# foreach sub-levels |
578 |
# foreach sub-levels |
| 618 |
my $unalloc_count ; |
579 |
my $unalloc_count ; |
| 619 |
|
580 |
|
| 620 |
foreach my $sub (@subs_arr) { |
581 |
foreach my $sub (@$subs) { |
| 621 |
my $sub_budget = GetBudget($sub); |
582 |
my $sub_budget = GetBudget($sub); |
| 622 |
|
|
|
| 623 |
$r->{budget_spent_sublevel} += GetBudgetSpent( $sub_budget->{'budget_id'} ); |
583 |
$r->{budget_spent_sublevel} += GetBudgetSpent( $sub_budget->{'budget_id'} ); |
| 624 |
$unalloc_count += $sub_budget->{'budget_amount'}; |
584 |
$unalloc_count += $sub_budget->{'budget_amount'}; |
| 625 |
} |
585 |
} |
|
Lines 627-632
sub GetBudgetHierarchy {
Link Here
|
| 627 |
return \@sort; |
587 |
return \@sort; |
| 628 |
} |
588 |
} |
| 629 |
|
589 |
|
|
|
590 |
# Recursive method to add a budget and its chidren to an array |
| 591 |
sub _add_budget_children { |
| 592 |
my $res = shift; |
| 593 |
my $budget = shift; |
| 594 |
push @$res, $budget; |
| 595 |
my $children = $budget->{'children'} || []; |
| 596 |
return unless @$children; # break recursivity |
| 597 |
foreach my $child (@$children) { |
| 598 |
_add_budget_children($res, $child); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 630 |
# ------------------------------------------------------------------- |
602 |
# ------------------------------------------------------------------- |
| 631 |
|
603 |
|
| 632 |
sub AddBudget { |
604 |
sub AddBudget { |
| 633 |
- |
|
|