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

(-)a/C4/Budgets.pm (-44 / +45 lines)
Lines 45-51 BEGIN { Link Here
45
        &GetBudgetOrdered
45
        &GetBudgetOrdered
46
        &GetBudgetName
46
        &GetBudgetName
47
        &GetPeriodsCount
47
        &GetPeriodsCount
48
        &GetChildBudgetsSpent
48
        GetBudgetHierarchySpent
49
        GetBudgetHierarchyOrdered
49
50
50
        &GetBudgetUsers
51
        &GetBudgetUsers
51
        &ModBudgetUsers
52
        &ModBudgetUsers
Lines 555-589 sub GetBudgetHierarchy { Link Here
555
		last if $children == 0;
556
		last if $children == 0;
556
	}
557
	}
557
558
558
# add budget-percent and allocation, and flags for html-template
559
	foreach my $r (@sort) {
560
		my $subs_href = $r->{'child'};
561
        my @subs_arr = ();
562
        if ( defined $subs_href ) {
563
            @subs_arr = @{$subs_href};
564
        }
565
566
        my $moo = $r->{'budget_code_indent'};
567
        $moo =~ s/\ /\&nbsp\;/g;
568
        $r->{'budget_code_indent'} =  $moo;
569
570
        $moo = $r->{'budget_name_indent'};
571
        $moo =~ s/\ /\&nbsp\;/g;
572
        $r->{'budget_name_indent'} = $moo;
573
574
        $r->{'budget_spent'}       = GetBudgetSpent( $r->{'budget_id'} );
575
        $r->{budget_ordered} = GetBudgetOrdered( $r->{budget_id} );
576
559
577
        $r->{budget_spent_sublevels} = 0;
560
    foreach my $budget (@sort) {
578
        $r->{budget_ordered_sublevels} = 0;
561
        $budget->{budget_spent}   = GetBudgetSpent( $budget->{budget_id} );
579
        # foreach sub-levels
562
        $budget->{budget_ordered} = GetBudgetOrdered( $budget->{budget_id} );
580
		foreach my $sub (@subs_arr) {
563
        $budget->{total_spent} = GetBudgetHierarchySpent( $budget->{budget_id} );
581
			my $sub_budget = GetBudget($sub);
564
        $budget->{total_ordered} = GetBudgetHierarchyOrdered( $budget->{budget_id} );
582
            $r->{budget_spent_sublevels} += GetBudgetSpent( $sub_budget->{'budget_id'} );
565
    }
583
            $r->{budget_ordered_sublevels} += GetBudgetOrdered($sub);
566
    return \@sort;
584
		}
585
	}
586
	return \@sort;
587
}
567
}
588
568
589
# -------------------------------------------------------------------
569
# -------------------------------------------------------------------
Lines 681-713 sub GetBudgetByCode { Link Here
681
    return $sth->fetchrow_hashref;
661
    return $sth->fetchrow_hashref;
682
}
662
}
683
663
684
=head2 GetChildBudgetsSpent
664
=head2 GetBudgetHierarchySpent
685
665
686
  &GetChildBudgetsSpent($budget-id);
666
  my $spent = GetBudgetHierarchySpent( $budget_id );
687
667
688
gets the total spent of the level and sublevels of $budget_id
668
Gets the total spent of the level and sublevels of $budget_id
689
669
690
=cut
670
=cut
691
671
692
# -------------------------------------------------------------------
672
sub GetBudgetHierarchySpent {
693
sub GetChildBudgetsSpent {
694
    my ( $budget_id ) = @_;
673
    my ( $budget_id ) = @_;
695
    my $dbh = C4::Context->dbh;
674
    my $dbh = C4::Context->dbh;
696
    my $query = "
675
    my $children_ids = $dbh->selectcol_arrayref(q|
697
        SELECT *
676
        SELECT budget_id
698
        FROM   aqbudgets
677
        FROM   aqbudgets
699
        WHERE  budget_parent_id=?
678
        WHERE  budget_parent_id = ?
700
        ";
679
    |, {}, $budget_id );
701
    my $sth = $dbh->prepare($query);
680
702
    $sth->execute( $budget_id );
681
    my $total_spent = GetBudgetSpent( $budget_id );
703
    my $result = $sth->fetchall_arrayref({});
682
    for my $child_id ( @$children_ids ) {
704
    my $total_spent = GetBudgetSpent($budget_id);
683
        $total_spent += GetBudgetHierarchySpent( $child_id );
705
    if ($result){
706
        $total_spent += GetChildBudgetsSpent($_->{"budget_id"}) foreach @$result;    
707
    }
684
    }
708
    return $total_spent;
685
    return $total_spent;
709
}
686
}
710
687
688
=head2 GetBudgetHierarchyOrdered
689
690
  my $ordered = GetBudgetHierarchyOrdered( $budget_id );
691
692
Gets the total ordered of the level and sublevels of $budget_id
693
694
=cut
695
696
sub GetBudgetHierarchyOrdered {
697
    my ( $budget_id ) = @_;
698
    my $dbh = C4::Context->dbh;
699
    my $children_ids = $dbh->selectcol_arrayref(q|
700
        SELECT budget_id
701
        FROM   aqbudgets
702
        WHERE  budget_parent_id = ?
703
    |, {}, $budget_id );
704
705
    my $total_ordered = GetBudgetOrdered( $budget_id );
706
    for my $child_id ( @$children_ids ) {
707
        $total_ordered += GetBudgetHierarchyOrdered( $child_id );
708
    }
709
    return $total_ordered;
710
}
711
711
=head2 GetBudgets
712
=head2 GetBudgets
712
713
713
  &GetBudgets($filter, $order_by);
714
  &GetBudgets($filter, $order_by);
(-)a/admin/aqbudgets.pl (-4 lines)
Lines 273-281 if ( $op eq 'list' ) { Link Here
273
	#This Looks WEIRD to me : should budgets be filtered in such a way ppl who donot own it would not see the amount spent on the budget by others ?
273
	#This Looks WEIRD to me : should budgets be filtered in such a way ppl who donot own it would not see the amount spent on the budget by others ?
274
274
275
    foreach my $budget (@budgets) {
275
    foreach my $budget (@budgets) {
276
        #Level and sublevels total spent and ordered
277
        $budget->{total_spent} = $budget->{budget_spent_sublevels} + $budget->{budget_spent};
278
        $budget->{total_ordered} = $budget->{budget_ordered_sublevels} + $budget->{budget_ordered};
279
        # PERMISSIONS
276
        # PERMISSIONS
280
        unless(CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
277
        unless(CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
281
            $budget->{'budget_lock'} = 1;
278
            $budget->{'budget_lock'} = 1;
282
- 

Return to bug 12168