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

(-)a/C4/Budgets.pm (-3 / +21 lines)
Lines 584-610 sub GetBudgetHierarchy { Link Here
584
        WHERE closedate IS NULL
584
        WHERE closedate IS NULL
585
        GROUP BY shipmentcost_budgetid
585
        GROUP BY shipmentcost_budgetid
586
        |, 'budget_id');
586
        |, 'budget_id');
587
    my $hr_budget_spent_adjustment = $dbh->selectall_hashref(q|
588
        SELECT budget_id,
589
               SUM(adjustment) as adjustments
590
        FROM aqinvoice_adjustments
591
        JOIN aqinvoices USING (invoiceid)
592
        WHERE closedate IS NOT NULL
593
        GROUP BY budget_id
594
        |, 'budget_id');
595
    my $hr_budget_ordered_adjustment = $dbh->selectall_hashref(q|
596
        SELECT budget_id,
597
               SUM(adjustment) as adjustments
598
        FROM aqinvoice_adjustments
599
        JOIN aqinvoices USING (invoiceid)
600
        WHERE closedate IS NULL AND encumber_open = 1
601
        GROUP BY budget_id
602
        |, 'budget_id');
587
603
588
604
589
    foreach my $budget (@sort) {
605
    foreach my $budget (@sort) {
590
        if ( not defined $budget->{budget_parent_id} ) {
606
        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 );
607
            _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
        }
608
        }
593
    }
609
    }
594
    return \@sort;
610
    return \@sort;
595
}
611
}
596
612
597
sub _recursiveAdd {
613
sub _recursiveAdd {
598
    my ($budget, $parent, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_ordered_shipment ) = @_;
614
    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
615
600
    foreach my $child (@{$budget->{children}}){
616
    foreach my $child (@{$budget->{children}}){
601
        _recursiveAdd($child, $budget, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_ordered_shipment );
617
        _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
    }
618
    }
603
619
604
    $budget->{budget_spent} += $hr_budget_spent->{$budget->{budget_id}}->{budget_spent};
620
    $budget->{budget_spent} += $hr_budget_spent->{$budget->{budget_id}}->{budget_spent};
605
    $budget->{budget_spent} += $hr_budget_spent_shipment->{$budget->{budget_id}}->{shipmentcost};
621
    $budget->{budget_spent} += $hr_budget_spent_shipment->{$budget->{budget_id}}->{shipmentcost};
622
    $budget->{budget_spent} += $hr_budget_spent_adjustment->{$budget->{budget_id}}->{adjustments};
606
    $budget->{budget_ordered} += $hr_budget_ordered->{$budget->{budget_id}}->{budget_ordered};
623
    $budget->{budget_ordered} += $hr_budget_ordered->{$budget->{budget_id}}->{budget_ordered};
607
    $budget->{budget_ordered} += $hr_budget_ordered_shipment->{$budget->{budget_id}}->{shipmentcost};
624
    $budget->{budget_ordered} += $hr_budget_ordered_shipment->{$budget->{budget_id}}->{shipmentcost};
625
    $budget->{budget_ordered} += $hr_budget_ordered_adjustment->{$budget->{budget_id}}->{adjustments};
608
626
609
    $budget->{total_spent} += $budget->{budget_spent};
627
    $budget->{total_spent} += $budget->{budget_spent};
610
    $budget->{total_ordered} += $budget->{budget_ordered};
628
    $budget->{total_ordered} += $budget->{budget_ordered};
(-)a/t/db_dependent/Budgets.t (-9 / +38 lines)
Lines 819-830 is( scalar @{$authCat}, 0, "GetBudgetAuthCats returns only non-empty sorting cat Link Here
819
# /Test GetBudgetAuthCats
819
# /Test GetBudgetAuthCats
820
820
821
subtest 'GetBudgetSpent and GetBudgetOrdered' => sub {
821
subtest 'GetBudgetSpent and GetBudgetOrdered' => sub {
822
    plan tests => 10;
822
    plan tests => 20;
823
823
824
    my $budget_period = $builder->build({
825
        source => 'Aqbudgetperiod',
826
        value  => {
827
            budget_period_active => 1,
828
            budget_total => 10000,
829
        }
830
    });
824
    my $budget = $builder->build({
831
    my $budget = $builder->build({
825
        source => 'Aqbudget',
832
        source => 'Aqbudget',
826
        value  => {
833
        value  => {
827
            budget_amount => 1000,
834
            budget_amount => 1000,
835
            budget_encumb => undef,
836
            budget_expend => undef,
837
            budget_period_id => $budget_period->{budget_period_id},
838
            budget_parent_id => undef,
828
        }
839
        }
829
    });
840
    });
830
    my $invoice = $builder->build({
841
    my $invoice = $builder->build({
Lines 834-844 subtest 'GetBudgetSpent and GetBudgetOrdered' => sub { Link Here
834
        }
845
        }
835
    });
846
    });
836
847
837
    my $spent = GetBudgetSpent( $budget->{budget_id} );
848
    my $spent     = GetBudgetSpent( $budget->{budget_id} );
838
    my $ordered = GetBudgetOrdered( $budget->{budget_id} );
849
    my $ordered   = GetBudgetOrdered( $budget->{budget_id} );
850
    my $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
839
851
840
    is( $spent, 0, "New budget, no orders/invoices, should be nothing spent");
852
    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");
853
    is( $ordered, 0, "New budget, no orders/invoices, should be nothing ordered");
854
    is( @$hierarchy[0]->{total_spent},0,"New budgets, no orders/invoices, budget hierarchy shows 0 spent");
855
    is( @$hierarchy[0]->{total_ordered},0,"New budgets, no orders/invoices, budget hierarchy shows 0 ordered");
842
856
843
    my $inv_adj_1 = $builder->build({
857
    my $inv_adj_1 = $builder->build({
844
        source => 'AqinvoiceAdjustment',
858
        source => 'AqinvoiceAdjustment',
Lines 852-859 subtest 'GetBudgetSpent and GetBudgetOrdered' => sub { Link Here
852
866
853
    $spent = GetBudgetSpent( $budget->{budget_id} );
867
    $spent = GetBudgetSpent( $budget->{budget_id} );
854
    $ordered = GetBudgetOrdered( $budget->{budget_id} );
868
    $ordered = GetBudgetOrdered( $budget->{budget_id} );
869
    $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
855
    is( $spent, 0, "After adding invoice adjustment on open invoice, should be nothing spent");
870
    is( $spent, 0, "After adding invoice adjustment on open invoice, should be nothing spent");
856
    is( $ordered, 0, "After adding invoice adjustment on open invoice not encumbered, should be nothing ordered");
871
    is( $ordered, 0, "After adding invoice adjustment on open invoice not encumbered, should be nothing ordered");
872
    is( @$hierarchy[0]->{total_spent},0,"After adding invoice adjustment on open invoice, budget hierarchy shows 0 spent");
873
    is( @$hierarchy[0]->{total_ordered},0,"After adding invoice adjustment on open invoice, budget hierarchy shows 0 ordered");
857
874
858
    my $inv_adj_2 = $builder->build({
875
    my $inv_adj_2 = $builder->build({
859
        source => 'AqinvoiceAdjustment',
876
        source => 'AqinvoiceAdjustment',
Lines 867-874 subtest 'GetBudgetSpent and GetBudgetOrdered' => sub { Link Here
867
884
868
    $spent = GetBudgetSpent( $budget->{budget_id} );
885
    $spent = GetBudgetSpent( $budget->{budget_id} );
869
    $ordered = GetBudgetOrdered( $budget->{budget_id} );
886
    $ordered = GetBudgetOrdered( $budget->{budget_id} );
887
    $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
870
    is( $spent, 0, "After adding invoice adjustment on open invoice, should be nothing spent");
888
    is( $spent, 0, "After adding invoice adjustment on open invoice, should be nothing spent");
871
    is( $ordered, 3, "After adding invoice adjustment on open invoice encumbered, should be 3 ordered");
889
    is( $ordered, 3, "After adding invoice adjustment on open invoice encumbered, should be 3 ordered");
890
    is( @$hierarchy[0]->{total_spent},0,"After adding invoice adjustment on open invoice encumbered, budget hierarchy shows 0 spent");
891
    is( @$hierarchy[0]->{total_ordered},3,"After adding invoice adjustment on open invoice encumbered, budget hierarchy shows 3 ordered");
872
892
873
    my $invoice_2 = $builder->build({
893
    my $invoice_2 = $builder->build({
874
        source => 'Aqinvoice',
894
        source => 'Aqinvoice',
Lines 897-909 subtest 'GetBudgetSpent and GetBudgetOrdered' => sub { Link Here
897
917
898
    $spent = GetBudgetSpent( $budget->{budget_id} );
918
    $spent = GetBudgetSpent( $budget->{budget_id} );
899
    $ordered = GetBudgetOrdered( $budget->{budget_id} );
919
    $ordered = GetBudgetOrdered( $budget->{budget_id} );
920
    $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
900
    is( $spent, 6, "After adding invoice adjustment on closed invoice, should be 6 spent, encumber has no affect once closed");
921
    is( $spent, 6, "After adding invoice adjustment on closed invoice, should be 6 spent, encumber has no affect once closed");
901
    is( $ordered, 3, "After adding invoice adjustment on closed invoice, should still be 3 ordered");
922
    is( $ordered, 3, "After adding invoice adjustment on closed invoice, should still be 3 ordered");
923
    is( @$hierarchy[0]->{total_spent},6,"After adding invoice adjustment on closed invoice, budget hierarchy shows 6 spent");
924
    is( @$hierarchy[0]->{total_ordered},3,"After adding invoice adjustment on closed invoice, budget hierarchy still shows 3 ordered");
902
925
903
    my $budget_2 = $builder->build({
926
    my $budget0 = $builder->build({
904
        source => 'Aqbudget',
927
        source => 'Aqbudget',
905
        value  => {
928
        value  => {
906
            budget_amount => 1000,
929
            budget_amount => 1000,
930
            budget_encumb => undef,
931
            budget_expend => undef,
932
            budget_period_id => $budget_period->{budget_period_id},
933
            budget_parent_id => $budget->{budget_id},
907
        }
934
        }
908
    });
935
    });
909
    my $inv_adj_5 = $builder->build({
936
    my $inv_adj_5 = $builder->build({
Lines 912-918 subtest 'GetBudgetSpent and GetBudgetOrdered' => sub { Link Here
912
            invoiceid     => $invoice->{invoiceid},
939
            invoiceid     => $invoice->{invoiceid},
913
            adjustment    => 3,
940
            adjustment    => 3,
914
            encumber_open => 1,
941
            encumber_open => 1,
915
            budget_id     => $budget_2->{budget_id},
942
            budget_id     => $budget0->{budget_id},
916
        }
943
        }
917
    });
944
    });
918
    my $inv_adj_6 = $builder->build({
945
    my $inv_adj_6 = $builder->build({
Lines 921-934 subtest 'GetBudgetSpent and GetBudgetOrdered' => sub { Link Here
921
            invoiceid     => $invoice_2->{invoiceid},
948
            invoiceid     => $invoice_2->{invoiceid},
922
            adjustment    => 3,
949
            adjustment    => 3,
923
            encumber_open => 1,
950
            encumber_open => 1,
924
            budget_id     => $budget_2->{budget_id},
951
            budget_id     => $budget0->{budget_id},
925
        }
952
        }
926
    });
953
    });
927
954
928
    $spent = GetBudgetSpent( $budget->{budget_id} );
955
    $spent = GetBudgetSpent( $budget->{budget_id} );
929
    $ordered = GetBudgetOrdered( $budget->{budget_id} );
956
    $ordered = GetBudgetOrdered( $budget->{budget_id} );
930
    is( $spent, 6, "After adding invoice adjustment on a different budget should be 6 spent/budget unaffected");
957
    $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
931
    is( $ordered, 3, "After adding invoice adjustment on a different budget, should still be 3 ordered/budget unaffected");
958
    is( $spent, 6, "After adding invoice adjustment on a child budget should be 6 spent/budget unaffected");
959
    is( $ordered, 3, "After adding invoice adjustment on a child budget, should still be 3 ordered/budget unaffected");
960
    is( @$hierarchy[0]->{total_spent},9,"After adding invoice adjustment on child budget, budget hierarchy shows 9 spent");
961
    is( @$hierarchy[0]->{total_ordered},6,"After adding invoice adjustment on child budget, budget hierarchy shows 6 ordered");
932
962
933
};
963
};
934
964
935
- 

Return to bug 22296