+#!/usr/bin/perl
use Modern::Perl;
-use Test::More tests => 108;
+use Test::More tests => 144;
BEGIN {
use_ok('C4::Budgets')
}
use C4::Context;
use C4::Biblio;
-use C4::Bookseller;
use C4::Acquisition;
-use C4::Dates;
-use Koha::Acquisition::Order;
+use Koha::Acquisition::Booksellers;
+use Koha::Acquisition::Orders;
+use Koha::Patrons;
+
+use t::lib::TestBuilder;
use YAML;
-my $dbh = C4::Context->dbh;
-$dbh->{AutoCommit} = 0;
-$dbh->{RaiseError} = 1;
+my $schema = Koha::Database->new->schema;
+$schema->storage->txn_begin;
+my $builder = t::lib::TestBuilder->new;
+my $dbh = C4::Context->dbh;
$dbh->do(q|DELETE FROM aqbudgetperiods|);
$dbh->do(q|DELETE FROM aqbudgets|);
+my $library = $builder->build({
+ source => 'Branch',
+});
+
# Mock userenv
local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
my $userenv;
*C4::Context::userenv = \&Mock_userenv;
-$userenv = { flags => 1, id => 'my_userid', branch => 'CPL' };
+$userenv = { flags => 1, id => 'my_userid', branch => $library->{branchcode} };
#
# Budget Periods :
# Budget :
#
+# The budget hierarchy will be:
+# budget_1
+# budget_11
+# budget_111
+# budget_12
+# budget_2
+# budget_21
+
is( AddBudget(), undef, 'AddBuget without argument returns undef' );
my $budgets = GetBudgets();
is( @$budgets, 0, 'GetBudgets returns the correct number of budgets' );
-$bpid = AddBudgetPeriod($my_budgetperiod);
+$bpid = AddBudgetPeriod($my_budgetperiod); #this is an active budget
+
my $my_budget = {
budget_code => 'ABCD',
budget_amount => '123.132000',
my $budget_name = GetBudgetName( $budget_id );
is($budget_name, $my_budget->{budget_name}, "Test the GetBudgetName routine");
+my $my_inactive_budgetperiod = { #let's add an inactive
+ budget_period_startdate => '2010-01-01',
+ budget_period_enddate => '2010-12-31',
+ budget_period_description => 'MODIF_MAPERI',
+ budget_period_active => 0,
+};
+my $bpid_i = AddBudgetPeriod($my_inactive_budgetperiod); #this is an inactive budget
+
+my $my_budget_inactive = {
+ budget_code => 'EFG',
+ budget_amount => '123.132000',
+ budget_name => 'Periodiques',
+ budget_notes => 'This is a note',
+ budget_period_id => $bpid_i,
+};
+my $budget_id_inactive = AddBudget($my_budget_inactive);
+
my $budget_code = $my_budget->{budget_code};
my $budget_by_code = GetBudgetByCode( $budget_code );
-is($budget_by_code->{budget_id}, $budget_id, "GetBudgetByCode, check id");
+is($budget_by_code->{budget_id}, $budget_id, "GetBudgetByCode, check id"); #this should match the active budget, not the inactive
is($budget_by_code->{budget_notes}, $my_budget->{budget_notes}, "GetBudgetByCode, check notes");
my $second_budget_id = AddBudget({
is( DelBudget($budget_id), 1, 'DelBudget returns true' );
$budgets = GetBudgets();
-is( @$budgets, 1, 'GetBudgets returns the correct number of budget periods' );
+is( @$budgets, 2, 'GetBudgets returns the correct number of budget periods' );
# GetBudgetHierarchySpent and GetBudgetHierarchyOrdered
budget_amount => $budget_2_total,
}
);
-my $budget_id11 = AddBudget(
+my $budget_id12 = AddBudget(
{
- budget_code => 'budget_11',
- budget_name => 'budget_11',
+ budget_code => 'budget_12',
+ budget_name => 'budget_12',
budget_period_id => $budget_period_id,
budget_parent_id => $budget_id1,
- budget_amount => $budget_11_total,
+ budget_amount => $budget_12_total,
}
);
-my $budget_id12 = AddBudget(
+my $budget_id11 = AddBudget(
{
- budget_code => 'budget_12',
- budget_name => 'budget_12',
+ budget_code => 'budget_11',
+ budget_name => 'budget_11',
budget_period_id => $budget_period_id,
budget_parent_id => $budget_id1,
- budget_amount => $budget_12_total,
+ budget_amount => $budget_11_total,
}
);
my $budget_id111 = AddBudget(
}
);
-my $booksellerid = C4::Bookseller::AddBookseller(
+my $bookseller = Koha::Acquisition::Bookseller->new(
{
name => "my vendor",
address1 => "bookseller's address",
active => 1,
deliverytime => 5,
}
-);
+)->store;
+my $booksellerid = $bookseller->id;
my $basketno = C4::Acquisition::NewBasket( $booksellerid, 1 );
my ( $biblionumber, $biblioitemnumber ) =
my %budgets;
my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
+my $invoice = GetInvoice( $invoiceid );
my $item_price = 10;
my $item_quantity = 2;
my $number_of_orders_to_move = 0;
order_internalnote => "internal note",
order_vendornote => "vendor note",
quantity => 2,
- cost => $item_price,
- rrp => $item_price,
+ cost_tax_included => $item_price,
+ rrp_tax_included => $item_price,
listprice => $item_price,
- ecost => $item_price,
- rrp => $item_price,
+ ecost_tax_include => $item_price,
discount => 0,
uncertainprice => 0,
- gstrate => 0,
}
- )->insert;
- my $ordernumber = $order->{ordernumber};
+ )->store;
+ my $ordernumber = $order->ordernumber;
push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
$number_of_orders_to_move++;
}
order_vendornote => "vendor note",
quantity => $item_quantity,
cost => $item_price,
- rrp => $item_price,
+ rrp_tax_included => $item_price,
listprice => $item_price,
- ecost => $item_price,
- rrp => $item_price,
+ ecost_tax_included => $item_price,
discount => 0,
uncertainprice => 0,
- gstrate => 0,
}
- )->insert;
- my $ordernumber = $order->{ordernumber};
+ )->store;
+ my $ordernumber = $order->ordernumber;
ModReceiveOrder({
biblionumber => $biblionumber,
- ordernumber => $ordernumber,
+ order => $order->unblessed,
budget_id => $infos->{budget_id},
quantityreceived => $item_quantity,
- cost => $item_price,
- ecost => $item_price,
- invoiceid => $invoiceid,
- rrp => $item_price,
+ invoice => $invoice,
received_items => [],
} );
}
is( GetBudgetHierarchySpent( $budget_id11 ), 100, "total spent for budget11 is 100" );
is( GetBudgetHierarchySpent( $budget_id111 ), 20, "total spent for budget111 is 20" );
+# GetBudgetSpent and GetBudgetOrdered
+my $budget_period_amount = 100;
+my $budget_amount = 50;
+
+$budget = AddBudgetPeriod(
+ {
+ budget_period_startdate => '2017-08-22',
+ budget_period_enddate => '2018-08-22',
+ budget_period_description => 'Test budget',
+ budget_period_active => 1,
+ budget_period_total => $budget_period_amount,
+ }
+);
+
+my $fund = AddBudget(
+ {
+ budget_code => 'Test fund',
+ budget_name => 'Test fund',
+ budget_period_id => $budget,
+ budget_parent_id => undef,
+ budget_amount => $budget_amount,
+ }
+);
+
+my $vendor = Koha::Acquisition::Bookseller->new(
+ {
+ name => "test vendor",
+ address1 => "test address",
+ phone => "0123456",
+ active => 1,
+ deliverytime => 5,
+ }
+)->store;
+
+my $vendorid = $vendor->id;
+
+my $basketnumber = C4::Acquisition::NewBasket( $vendorid, 1 );
+my ( $biblio, $biblioitem ) = C4::Biblio::AddBiblio( MARC::Record->new, '' );
+
+my @orders = (
+ {
+ budget_id => $fund,
+ pending_quantity => 1,
+ spent_quantity => 0,
+ },
+);
+
+my $invoiceident = AddInvoice( invoicenumber => 'invoice_test_clone', booksellerid => $vendorid, shipmentdate => '2017-08-22', shipmentcost => 6, shipmentcost_budgetid => $fund );
+my $test_invoice = GetInvoice( $invoiceident );
+my $individual_item_price = 10;
+
+my $order = Koha::Acquisition::Order->new(
+ {
+ basketno => $basketnumber,
+ biblionumber => $biblio,
+ budget_id => $fund,
+ order_internalnote => "internalnote",
+ order_vendornote => "vendor note",
+ quantity => 2,
+ cost_tax_included => $individual_item_price,
+ rrp_tax_included => $individual_item_price,
+ listprice => $individual_item_price,
+ ecost_tax_included => $individual_item_price,
+ discount => 0,
+ uncertainprice => 0,
+ }
+)->store;
+
+ModReceiveOrder({
+ bibionumber => $biblio,
+ order => $order->unblessed,
+ budget_id => $fund,
+ quantityreceived => 2,
+ invoice => $test_invoice,
+ received_items => [],
+} );
+
+is ( GetBudgetSpent( $fund ), 6, "total shipping cost is 6");
+is ( GetBudgetOrdered( $fund ), '20.000000', "total ordered price is 20");
+
+
# CloneBudgetPeriod
my $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
{
}
);
+$budget_hierarchy = GetBudgetHierarchy($budget_period_id);
+is( $budget_hierarchy->[0]->{children}->[0]->{budget_name}, 'budget_11', 'GetBudgetHierarchy should return budgets ordered by name, first child is budget_11' );
+is( $budget_hierarchy->[0]->{children}->[1]->{budget_name}, 'budget_12', 'GetBudgetHierarchy should return budgets ordered by name, second child is budget_12' );
+is($budget_hierarchy->[0]->{budget_name},'budget_1','GetBudgetHierarchy should return budgets ordered by name, first budget is budget_1');
+is($budget_hierarchy->[0]->{budget_level},'0','budget_level of budget (budget_1) should be 0');
+is($budget_hierarchy->[0]->{children}->[0]->{budget_level},'1','budget_level of first fund(budget_11) should be 1');
+is($budget_hierarchy->[0]->{children}->[1]->{budget_level},'1','budget_level of second fund(budget_12) should be 1');
+is($budget_hierarchy->[0]->{children}->[0]->{children}->[0]->{budget_level},'2','budget_level of child fund budget_11 should be 2');
$budget_hierarchy = GetBudgetHierarchy($budget_period_id);
$budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
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( $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 $patron_category = $builder->build({ source => 'Category' });
+my $branchcode = $library->{branchcode};
+my $john_doe = Koha::Patron->new({
+ cardnumber => '123456',
+ firstname => 'John',
+ surname => 'Doe',
+ categorycode => $patron_category->{categorycode},
+ branchcode => $branchcode,
+ dateofbirth => '',
+ dateexpiry => '9999-12-31',
+ userid => 'john.doe'
+})->store->borrowernumber;
+
+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 = Koha::Patron->new({
+ cardnumber => '789012',
+ firstname => 'Jane',
+ surname => 'Doe',
+ categorycode => $patron_category->{categorycode},
+ branchcode => $branchcode,
+ dateofbirth => '',
+ dateexpiry => '9999-12-31',
+ userid => 'jane.doe'
+})->store->borrowernumber;
+
+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;