+# CloneBudgetPeriod with param reset_all_budgets
+$budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
+ {
+ budget_period_id => $budget_period_id,
+ budget_period_startdate => '2014-01-01',
+ budget_period_enddate => '2014-12-31',
+ reset_all_budgets => 1,
+ }
+);
+
+$budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
+$number_of_budgets_not_reset = 0;
+for my $budget (@$budget_hierarchy_cloned) {
+ $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
+}
+is( $number_of_budgets_not_reset, 0,
+ 'CloneBudgetPeriod has reset all budgets (funds)' );
+
+#GetBudgetsByActivity
+my $result=C4::Budgets::GetBudgetsByActivity(1);
+isnt( $result, undef ,'GetBudgetsByActivity return correct value with parameter 1');
+$result=C4::Budgets::GetBudgetsByActivity(0);
+ isnt( $result, undef ,'GetBudgetsByActivity return correct value with parameter 0');
+$result=C4::Budgets::GetBudgetsByActivity();
+ is( $result, 0 , 'GetBudgetsByActivity return 0 with none parameter or other 0 or 1' );
+DelBudget($budget_id);
+DelBudgetPeriod($bpid);
+
+# CloneBudgetPeriod with param amount_change_*
+$budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
+ {
+ budget_period_id => $budget_period_id,
+ budget_period_startdate => '2014-01-01',
+ budget_period_enddate => '2014-12-31',
+ amount_change_percentage => 16,
+ amount_change_round_increment => 5,
+ }
+);
+
+$budget_period_cloned = C4::Budgets::GetBudgetPeriod($budget_period_id_cloned);
+cmp_ok($budget_period_cloned->{budget_period_total}, '==', 11600, "CloneBudgetPeriod changed correctly budget amount");
+$budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
+cmp_ok($budget_hierarchy_cloned->[0]->{budget_amount}, '==', 1160, "CloneBudgetPeriod changed correctly funds amounts");
+cmp_ok($budget_hierarchy_cloned->[1]->{budget_amount}, '==', 115, "CloneBudgetPeriod changed correctly funds amounts");
+cmp_ok($budget_hierarchy_cloned->[2]->{budget_amount}, '==', 55, "CloneBudgetPeriod changed correctly funds amounts");
+cmp_ok($budget_hierarchy_cloned->[3]->{budget_amount}, '==', 115, "CloneBudgetPeriod changed correctly funds amounts");
+cmp_ok($budget_hierarchy_cloned->[4]->{budget_amount}, '==', 2320, "CloneBudgetPeriod changed correctly funds amounts");
+cmp_ok($budget_hierarchy_cloned->[5]->{budget_amount}, '==', 0, "CloneBudgetPeriod changed correctly funds amounts");
+
+$budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
+ {
+ budget_period_id => $budget_period_id,
+ budget_period_startdate => '2014-01-01',
+ budget_period_enddate => '2014-12-31',
+ amount_change_percentage => 16,
+ amount_change_round_increment => 5,
+ reset_all_budgets => 1,
+ }
+);
+$budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
+cmp_ok($budget_hierarchy_cloned->[0]->{budget_amount}, '==', 0, "CloneBudgetPeriod reset all fund amounts");
+
+# MoveOrders
+my $number_orders_moved = C4::Budgets::MoveOrders();
+is( $number_orders_moved, undef, 'MoveOrders return undef if no arg passed' );
+$number_orders_moved =
+ C4::Budgets::MoveOrders( { from_budget_period_id => $budget_period_id } );
+is( $number_orders_moved, undef,
+ 'MoveOrders return undef if only 1 arg passed' );
+$number_orders_moved =
+ C4::Budgets::MoveOrders( { to_budget_period_id => $budget_period_id } );
+is( $number_orders_moved, undef,
+ 'MoveOrders return undef if only 1 arg passed' );
+$number_orders_moved = C4::Budgets::MoveOrders(
+ {
+ from_budget_period_id => $budget_period_id,
+ to_budget_period_id => $budget_period_id
+ }
+);
+is( $number_orders_moved, undef,
+ 'MoveOrders return undef if 2 budget period id are the same' );
+
+$budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
+ {
+ budget_period_id => $budget_period_id,
+ budget_period_startdate => '2014-01-01',
+ budget_period_enddate => '2014-12-31',
+ }
+);
+
+my $report = C4::Budgets::MoveOrders(
+ {
+ from_budget_period_id => $budget_period_id,
+ to_budget_period_id => $budget_period_id_cloned,
+ move_remaining_unspent => 1,
+ }
+);
+is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
+
+my $number_of_orders_moved = 0;
+$number_of_orders_moved += scalar( @{ $_->{orders_moved} } ) for @$report;
+is( $number_of_orders_moved, $number_of_orders_to_move, "MoveOrders has moved $number_of_orders_to_move orders" );
+
+my @new_budget_ids = map { $_->{budget_id} }
+ @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
+my @old_budget_ids = map { $_->{budget_id} }
+ @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
+for my $budget_id ( keys %budgets ) {
+ for my $ordernumber ( @{ $budgets{$budget_id} } ) {
+ my $budget = GetBudgetByOrderNumber($ordernumber);
+ my $is_in_new_budgets = grep /^$budget->{budget_id}$/, @new_budget_ids;
+ my $is_in_old_budgets = grep /^$budget->{budget_id}$/, @old_budget_ids;
+ is( $is_in_new_budgets, 1, "MoveOrders changed the budget_id for order $ordernumber" );
+ is( $is_in_old_budgets, 0, "MoveOrders changed the budget_id for order $ordernumber" );
+ }
+}
+
+
+# MoveOrders with param move_remaining_unspent
+my @new_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
+my @old_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
+
+for my $new_budget ( @new_budgets ) {
+ my ( $old_budget ) = map { $_->{budget_code} eq $new_budget->{budget_code} ? $_ : () } @old_budgets;
+ my $new_budget_amount_should_be = $old_budget->{budget_amount} * 2 - $old_budget->{total_spent};
+ is( $new_budget->{budget_amount} + 0, $new_budget_amount_should_be, "MoveOrders updated the budget amount with the previous unspent budget (for budget $new_budget->{budget_code})" );
+}
+
+# Test SetOwnerToFundHierarchy
+
+my $categorycode = 'S';
+my $branchcode = $library->{branchcode};
+my $john_doe = C4::Members::AddMember(
+ cardnumber => '123456',
+ firstname => 'John',
+ surname => 'Doe',
+ categorycode => $categorycode,
+ branchcode => $branchcode,
+ dateofbirth => '',
+ dateexpiry => '9999-12-31',
+ userid => 'john.doe'
+);
+
+C4::Budgets::SetOwnerToFundHierarchy( $budget_id1, $john_doe );
+is( C4::Budgets::GetBudget($budget_id1)->{budget_owner_id},
+ $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 1 ($budget_id1)" );
+is( C4::Budgets::GetBudget($budget_id11)->{budget_owner_id},
+ $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 11 ($budget_id11)" );
+is( C4::Budgets::GetBudget($budget_id111)->{budget_owner_id},
+ $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 111 ($budget_id111)" );
+is( C4::Budgets::GetBudget($budget_id12)->{budget_owner_id},
+ $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 12 ($budget_id12 )" );
+is( C4::Budgets::GetBudget($budget_id2)->{budget_owner_id},
+ undef, "SetOwnerToFundHierarchy should not have set an owner for budget 2 ($budget_id2)" );
+is( C4::Budgets::GetBudget($budget_id21)->{budget_owner_id},
+ undef, "SetOwnerToFundHierarchy should not have set an owner for budget 21 ($budget_id21)" );
+
+my $jane_doe = C4::Members::AddMember(
+ cardnumber => '789012',
+ firstname => 'Jane',
+ surname => 'Doe',
+ categorycode => $categorycode,
+ branchcode => $branchcode,
+ dateofbirth => '',
+ dateexpiry => '9999-12-31',
+ userid => 'jane.doe'
+);
+
+C4::Budgets::SetOwnerToFundHierarchy( $budget_id11, $jane_doe );
+is( C4::Budgets::GetBudget($budget_id1)->{budget_owner_id},
+ $john_doe, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 1 ($budget_id1)" );
+is( C4::Budgets::GetBudget($budget_id11)->{budget_owner_id},
+ $jane_doe, "SetOwnerToFundHierarchy should have set John Doe $jane_doe for budget 11 ($budget_id11)" );
+is( C4::Budgets::GetBudget($budget_id111)->{budget_owner_id},
+ $jane_doe, "SetOwnerToFundHierarchy should have set John Doe $jane_doe for budget 111 ($budget_id111)" );
+is( C4::Budgets::GetBudget($budget_id12)->{budget_owner_id},
+ $john_doe, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 12 ($budget_id12 )" );
+is( C4::Budgets::GetBudget($budget_id2)->{budget_owner_id},
+ undef, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 2 ($budget_id2)" );
+is( C4::Budgets::GetBudget($budget_id21)->{budget_owner_id},
+ undef, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 21 ($budget_id21)" );
+
+# Test GetBudgetAuthCats
+
+my $budgetPeriodId = AddBudgetPeriod({
+ budget_period_startdate => '2008-01-01',
+ budget_period_enddate => '2008-12-31',
+ budget_period_description => 'just another budget',
+ budget_period_active => 0,
+});
+
+$budgets = GetBudgets();
+my $i = 0;
+for my $budget ( @$budgets )
+{
+ $budget->{sort1_authcat} = "sort1_authcat_$i";
+ $budget->{sort2_authcat} = "sort2_authcat_$i";
+ $budget->{budget_period_id} = $budgetPeriodId;
+ ModBudget( $budget );
+ $i++;
+}
+
+my $authCat = GetBudgetAuthCats($budgetPeriodId);
+
+is( scalar @{$authCat}, $i * 2, "GetBudgetAuthCats returns only non-empty sorting categories (no empty authCat in db)" );
+
+$i = 0;
+for my $budget ( @$budgets )
+{
+ $budget->{sort1_authcat} = "sort_authcat_$i";
+ $budget->{sort2_authcat} = "sort_authcat_$i";
+ $budget->{budget_period_id} = $budgetPeriodId;
+ ModBudget( $budget );
+ $i++;
+}
+
+$authCat = GetBudgetAuthCats($budgetPeriodId);
+is( scalar @$authCat, scalar @$budgets, "GetBudgetAuthCats returns distinct authCat" );
+
+$i = 0;
+for my $budget ( @$budgets )
+{
+ $budget->{sort1_authcat} = "sort1_authcat_$i";
+ $budget->{sort2_authcat} = "";
+ $budget->{budget_period_id} = $budgetPeriodId;
+ ModBudget( $budget );
+ $i++;
+}
+
+$authCat = GetBudgetAuthCats($budgetPeriodId);
+
+is( scalar @{$authCat}, $i, "GetBudgetAuthCats returns only non-empty sorting categories (empty sort2_authcat on all records)" );
+
+$i = 0;
+for my $budget ( @$budgets )
+{
+ $budget->{sort1_authcat} = "";
+ $budget->{sort2_authcat} = "";
+ $budget->{budget_period_id} = $budgetPeriodId;
+ ModBudget( $budget );
+ $i++;
+}
+
+$authCat = GetBudgetAuthCats($budgetPeriodId);
+
+is( scalar @{$authCat}, 0, "GetBudgetAuthCats returns only non-empty sorting categories (all empty)" );
+
+# /Test GetBudgetAuthCats
+
+sub _get_dependencies {
+ my ($budget_hierarchy) = @_;
+ my $graph;
+ for my $budget (@$budget_hierarchy) {
+ if ( $budget->{child} ) {
+ my @sorted = sort @{ $budget->{child} };
+ for my $child_id (@sorted) {
+ push @{ $graph->{ $budget->{budget_name} }{children} },
+ _get_budgetname_by_id( $budget_hierarchy, $child_id );
+ }
+ }
+ push @{ $graph->{ $budget->{budget_name} }{parents} },
+ $budget->{parent_id};
+ }
+ return $graph;
+}
+
+sub _get_budgetname_by_id {
+ my ( $budgets, $budget_id ) = @_;
+ my ($budget_name) =
+ map { ( $_->{budget_id} eq $budget_id ) ? $_->{budget_name} : () }
+ @$budgets;
+ return $budget_name;
+}
+
+# C4::Context->userenv
+sub Mock_userenv {
+ return $userenv;
+}