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

(-)a/C4/Budgets.pm (-3 / +25 lines)
Lines 488-493 sub ModBudgetPeriod { Link Here
488
488
489
# -------------------------------------------------------------------
489
# -------------------------------------------------------------------
490
sub GetBudgetHierarchy {
490
sub GetBudgetHierarchy {
491
    warn "Here";
492
    $debug=1;
491
    my ( $budget_period_id, $branchcode, $owner ) = @_;
493
    my ( $budget_period_id, $branchcode, $owner ) = @_;
492
    my @bind_params;
494
    my @bind_params;
493
    my $dbh   = C4::Context->dbh;
495
    my $dbh   = C4::Context->dbh;
Lines 532-538 sub GetBudgetHierarchy { Link Here
532
    }
534
    }
533
535
534
    # link child to parent
536
    # link child to parent
537
    warn Data::Dumper::Dumper( %links );
535
    my @first_parents;
538
    my @first_parents;
539
    warn Data::Dumper::Dumper(( sort { $a->{budget_code} cmp $b->{budget_code} } values %links ));
536
    foreach my $budget ( sort { $a->{budget_code} cmp $b->{budget_code} } values %links ) {
540
    foreach my $budget ( sort { $a->{budget_code} cmp $b->{budget_code} } values %links ) {
537
        my $child = $links{$budget->{budget_id}};
541
        my $child = $links{$budget->{budget_id}};
538
        if ( $child->{'budget_parent_id'} ) {
542
        if ( $child->{'budget_parent_id'} ) {
Lines 584-610 sub GetBudgetHierarchy { Link Here
584
        WHERE closedate IS NULL
588
        WHERE closedate IS NULL
585
        GROUP BY shipmentcost_budgetid
589
        GROUP BY shipmentcost_budgetid
586
        |, 'budget_id');
590
        |, 'budget_id');
591
    my $hr_budget_spent_adjustment = $dbh->selectall_hashref(q|
592
        SELECT budget_id,
593
               SUM(adjustment) as adjustments
594
        FROM aqinvoice_adjustments
595
        JOIN aqinvoices USING (invoiceid)
596
        WHERE closedate IS NOT NULL
597
        GROUP BY budget_id
598
        |, 'budget_id');
599
    my $hr_budget_ordered_adjustment = $dbh->selectall_hashref(q|
600
        SELECT budget_id,
601
               SUM(adjustment) as adjustments
602
        FROM aqinvoice_adjustments
603
        JOIN aqinvoices USING (invoiceid)
604
        WHERE closedate IS NULL AND encumber_open = 1
605
        GROUP BY budget_id
606
        |, 'budget_id');
587
607
588
608
589
    foreach my $budget (@sort) {
609
    foreach my $budget (@sort) {
590
        if ( not defined $budget->{budget_parent_id} ) {
610
        if ( not defined $budget->{budget_parent_id} ) {
591
            _recursiveAdd( $budget, undef, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_ordered_shipment );
611
            _recursiveAdd( $budget, undef, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_ordered_shipment, $hr_budget_spent_adjustment, $hr_budget_ordered_adjustment );
592
        }
612
        }
593
    }
613
    }
594
    return \@sort;
614
    return \@sort;
595
}
615
}
596
616
597
sub _recursiveAdd {
617
sub _recursiveAdd {
598
    my ($budget, $parent, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_ordered_shipment ) = @_;
618
    my ($budget, $parent, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_ordered_shipment, $hr_budget_spent_adjustment, $hr_budget_ordered_adjustment ) = @_;
599
619
600
    foreach my $child (@{$budget->{children}}){
620
    foreach my $child (@{$budget->{children}}){
601
        _recursiveAdd($child, $budget, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_ordered_shipment );
621
        _recursiveAdd($child, $budget, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_ordered_shipment, $hr_budget_spent_adjustment, $hr_budget_ordered_adjustment );
602
    }
622
    }
603
623
604
    $budget->{budget_spent} += $hr_budget_spent->{$budget->{budget_id}}->{budget_spent};
624
    $budget->{budget_spent} += $hr_budget_spent->{$budget->{budget_id}}->{budget_spent};
605
    $budget->{budget_spent} += $hr_budget_spent_shipment->{$budget->{budget_id}}->{shipmentcost};
625
    $budget->{budget_spent} += $hr_budget_spent_shipment->{$budget->{budget_id}}->{shipmentcost};
626
    $budget->{budget_spent} += $hr_budget_spent_adjustment->{$budget->{budget_id}}->{adjustments};
606
    $budget->{budget_ordered} += $hr_budget_ordered->{$budget->{budget_id}}->{budget_ordered};
627
    $budget->{budget_ordered} += $hr_budget_ordered->{$budget->{budget_id}}->{budget_ordered};
607
    $budget->{budget_ordered} += $hr_budget_ordered_shipment->{$budget->{budget_id}}->{shipmentcost};
628
    $budget->{budget_ordered} += $hr_budget_ordered_shipment->{$budget->{budget_id}}->{shipmentcost};
629
    $budget->{budget_ordered} += $hr_budget_ordered_adjustment->{$budget->{budget_id}}->{adjustments};
608
630
609
    $budget->{total_spent} += $budget->{budget_spent};
631
    $budget->{total_spent} += $budget->{budget_spent};
610
    $budget->{total_ordered} += $budget->{budget_ordered};
632
    $budget->{total_ordered} += $budget->{budget_ordered};
(-)a/t/db_dependent/Budgets.t (-3 / +24 lines)
Lines 821-830 is( scalar @{$authCat}, 0, "GetBudgetAuthCats returns only non-empty sorting cat Link Here
821
subtest 'GetBudgetSpent and GetBudgetOrdered' => sub {
821
subtest 'GetBudgetSpent and GetBudgetOrdered' => sub {
822
    plan tests => 10;
822
    plan tests => 10;
823
823
824
    my $budget_period = $builder->build({
825
        source => 'Aqbudgetperiod',
826
        value  => {
827
            budget_period_active => 1,
828
            budget_total => 10000,
829
        }
830
    });
831
    warn $budget_period->{budget_period_id};
832
    my $budget0 = $builder->build({
833
        source => 'Aqbudget',
834
        value  => {
835
            budget_amount => 1000,
836
            budget_encumb => undef,
837
            budget_expend => undef,
838
            budget_period_id => $budget_period->{budget_period_id},
839
        }
840
    });
824
    my $budget = $builder->build({
841
    my $budget = $builder->build({
825
        source => 'Aqbudget',
842
        source => 'Aqbudget',
826
        value  => {
843
        value  => {
827
            budget_amount => 1000,
844
            budget_amount => 1000,
845
            budget_encumb => undef,
846
            budget_expend => undef,
847
            budget_period_id => $budget_period->{budget_period_id},
828
        }
848
        }
829
    });
849
    });
830
    my $invoice = $builder->build({
850
    my $invoice = $builder->build({
Lines 834-844 subtest 'GetBudgetSpent and GetBudgetOrdered' => sub { Link Here
834
        }
854
        }
835
    });
855
    });
836
856
837
    my $spent = GetBudgetSpent( $budget->{budget_id} );
857
    my $spent     = GetBudgetSpent( $budget->{budget_id} );
838
    my $ordered = GetBudgetOrdered( $budget->{budget_id} );
858
    my $ordered   = GetBudgetOrdered( $budget->{budget_id} );
859
    my $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
839
860
840
    is( $spent, 0, "New budget, no orders/invoices, should be nothing spent");
861
    is( $spent, 0, "New budget, no orders/invoices, should be nothing spent");
841
    is( $ordered, 0, "New budget, no orders/invoices, should be nothing ordered");
862
    is( $ordered, 0, "New budget, no orders/invoices, should be nothing ordered");
863
    warn Data::Dumper::Dumper( $hierarchy );
842
864
843
    my $inv_adj_1 = $builder->build({
865
    my $inv_adj_1 = $builder->build({
844
        source => 'AqinvoiceAdjustment',
866
        source => 'AqinvoiceAdjustment',
845
- 

Return to bug 22296