View | Details | Raw Unified | Return to bug 11714
Collapse All | Expand All

(-)a/C4/Budgets.pm (-64 / +40 lines)
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 {
(-)a/acqui/acqui-home.pl (-2 lines)
Lines 95-102 my @budget_loop; Link Here
95
foreach my $budget ( @{$budget_arr} ) {
95
foreach my $budget ( @{$budget_arr} ) {
96
    next unless (CanUserUseBudget($loggedinuser, $budget, $userflags));
96
    next unless (CanUserUseBudget($loggedinuser, $budget, $userflags));
97
97
98
    $budget->{budget_code_indent} =~ s/\ /\&nbsp\;/g;
99
100
    $budget->{'budget_branchname'} =
98
    $budget->{'budget_branchname'} =
101
      GetBranchName( $budget->{'budget_branchcode'} );
99
      GetBranchName( $budget->{'budget_branchcode'} );
102
100
(-)a/admin/aqbudgets.pl (-2 / +1 lines)
Lines 141-148 if ($op eq 'add_form') { Link Here
141
    my @values;
141
    my @values;
142
    my $hier = GetBudgetHierarchy($$period{budget_period_id});
142
    my $hier = GetBudgetHierarchy($$period{budget_period_id});
143
    foreach my $r (@$hier) {
143
    foreach my $r (@$hier) {
144
        $r->{budget_code_indent} =~ s/ /\~/g;    #
144
        $labels{"$r->{budget_id}"} = $r->{budget_code};
145
        $labels{"$r->{budget_id}"} = $r->{budget_code_indent};
146
        push @values, $r->{budget_id};
145
        push @values, $r->{budget_id};
147
    }
146
    }
148
    push @values, '';
147
    push @values, '';
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgets.tt (-2 / +1 lines)
Lines 314-320 var MSG_PARENT_BENEATH_BUDGET = "- " + _("New budget-parent is beneath budget") Link Here
314
    [% END %]
314
    [% END %]
315
    <td>[% budget.budget_period_active %]</td>
315
    <td>[% budget.budget_period_active %]</td>
316
    <td>Budget [% budget.budget_period_description %] [id=[% budget.budget_period_id %]][% UNLESS budget.budget_period_active %] (inactive)[% END %]</td>
316
    <td>Budget [% budget.budget_period_description %] [id=[% budget.budget_period_id %]][% UNLESS budget.budget_period_active %] (inactive)[% END %]</td>
317
    <td>[% budget.budget_code_indent %]</td>
317
    <td>[% budget.budget_code %]</td>
318
    <td>[% budget.budget_name %]</td>
318
    <td>[% budget.budget_name %]</td>
319
    <td class="data">
319
    <td class="data">
320
      [% IF budget.budget_parent_id %]
320
      [% IF budget.budget_parent_id %]
321
- 

Return to bug 11714