Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / Budgets.t
index f127a77..617673d 100755 (executable)
@@ -1,13 +1,13 @@
 #!/usr/bin/perl
 use Modern::Perl;
-use Test::More tests => 146;
+use Test::More tests => 144;
 
 BEGIN {
-    use_ok('C4::Budgets')
+    use_ok('C4::Budgets', qw( AddBudgetPeriod AddBudget GetBudgetPeriods GetBudgetPeriod GetBudget ModBudgetPeriod ModBudget DelBudgetPeriod DelBudget GetBudgets GetBudgetName GetBudgetByCode GetBudgetHierarchy GetBudgetHierarchySpent GetBudgetSpent GetBudgetOrdered CloneBudgetPeriod GetBudgetsByActivity MoveOrders GetBudgetByOrderNumber SetOwnerToFundHierarchy GetBudgetAuthCats GetBudgetsPlanCell ));
 }
 use C4::Context;
-use C4::Biblio;
-use C4::Acquisition;
+use C4::Biblio qw( AddBiblio );
+use C4::Acquisition qw( NewBasket AddInvoice GetInvoice ModReceiveOrder populate_order_with_prices );
 
 use Koha::Acquisition::Booksellers;
 use Koha::Acquisition::Orders;
@@ -20,8 +20,6 @@ use t::lib::TestBuilder;
 use t::lib::Mocks;
 use Koha::DateUtils;
 
-use YAML;
-
 use t::lib::Mocks;
 t::lib::Mocks::mock_preference('OrderPriceRounding','');
 
@@ -58,7 +56,6 @@ $bpid = AddBudgetPeriod({
     budget_period_enddate => '2008-12-31',
 });
 is( $bpid, undef, 'AddBugetPeriod without start date returns undef' );
-is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
 my $budgetperiods = GetBudgetPeriods();
 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
 
@@ -76,8 +73,6 @@ is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_s
 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'AddBudgetPeriod stores the end date correctly' );
 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'AddBudgetPeriod stores the description correctly' );
 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'AddBudgetPeriod stores active correctly' );
-is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
-
 
 $my_budgetperiod = {
     budget_period_startdate   => '2009-01-01',
@@ -96,8 +91,6 @@ is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_s
 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'ModBudgetPeriod updates the end date correctly' );
 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'ModBudgetPeriod updates the description correctly' );
 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'ModBudgetPeriod upates active correctly' );
-isnt( GetBudgetPeriod(0), undef, 'GetBugetPeriods functions correctly' );
-
 
 $budgetperiods = GetBudgetPeriods();
 is( @$budgetperiods, 1, 'GetBudgetPeriods returns the correct number of budget periods' );
@@ -111,7 +104,6 @@ is( DelBudgetPeriod($bpid), 1, 'DelBudgetPeriod returns true' );
 $budgetperiods = GetBudgetPeriods();
 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
 
-
 #
 # Budget  :
 #
@@ -824,8 +816,8 @@ is( scalar @{$authCat}, 0, "GetBudgetAuthCats returns only non-empty sorting cat
 
 # /Test GetBudgetAuthCats
 
-subtest 'GetBudgetSpent and GetBudgetOrdered' => sub {
-    plan tests => 20;
+subtest 'GetBudgetSpent and GetBudgetOrdered and GetBudgetHierarchy shipping and adjustments' => sub {
+    plan tests => 26;
 
     my $budget_period = $builder->build({
         source => 'Aqbudgetperiod',
@@ -965,36 +957,84 @@ subtest 'GetBudgetSpent and GetBudgetOrdered' => sub {
     is( $ordered, 3, "After adding invoice adjustment on a child budget, should still be 3 ordered/budget unaffected");
     is( @$hierarchy[0]->{total_spent},9,"After adding invoice adjustment on child budget, budget hierarchy shows 9 spent");
     is( @$hierarchy[0]->{total_ordered},6,"After adding invoice adjustment on child budget, budget hierarchy shows 6 ordered");
+
+    my $invoice_3 = $builder->build({
+        source => 'Aqinvoice',
+        value  => {
+            closedate => '2017-07-01',
+            shipmentcost_budgetid => $budget0->{budget_id},
+            shipmentcost           => 1.25
+        }
+    });
+    my $invoice_4 = $builder->build({
+        source => 'Aqinvoice',
+        value  => {
+            closedate => undef,
+            shipmentcost_budgetid => $budget0->{budget_id},
+            shipmentcost           => 1.75
+        }
+    });
+
+    $spent = GetBudgetSpent( $budget->{budget_id} );
+    $ordered = GetBudgetOrdered( $budget->{budget_id} );
+    is( $spent, 6, "After adding invoice shipment cost on open and closed invoice on child, neither are counted as spent from parent");
+    is( $ordered, 3, "After adding invoice shipment cost on open and closed invoice, neither are counted as ordered from parent");
+    $spent = GetBudgetSpent( $budget0->{budget_id} );
+    $ordered = GetBudgetOrdered( $budget0->{budget_id} );
+    is( $spent, 6, "After adding invoice shipment cost on open and closed invoice on child, both are counted as spent from child");
+    is( $ordered, 3, "After adding invoice shipment cost on open and closed invoice, neither are counted as ordered from child");
+    $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
+    is( @$hierarchy[0]->{total_spent},12,"After adding shipmentcost on child, budget hierarchy shows 12 spent total");
+    is( @$hierarchy[0]->{total_ordered},6,"After adding shipmentcost on child, budget hierarchy still shows 6 ordered total");
+
+
 };
 
 subtest 'GetBudgetSpent GetBudgetOrdered GetBudgetsPlanCell tests' => sub {
 
-    plan tests => 12;
+    plan tests => 24;
 
 #Let's build an order, we need a couple things though
     t::lib::Mocks::mock_preference('OrderPriceRounding','nearest_cent');
 
-    my $spent_biblio = $builder->build({ source => 'Biblio' });
-    my $spent_basket = $builder->build({ source => 'Aqbasket', value => { is_standing => 0 } });
-    my $spent_invoice = $builder->build({ source => 'Aqinvoice'});
+    my $item_1         = $builder->build_sample_item;
+    my $spent_basket   = $builder->build({ source => 'Aqbasket', value => { is_standing => 0 } });
+    my $spent_invoice  = $builder->build({ source => 'Aqinvoice'});
     my $spent_currency = $builder->build({ source => 'Currency', value => { active => 1, archived => 0, symbol => 'F', rate => 2, isocode => undef, currency => 'FOO' }  });
-    my $spent_vendor = $builder->build({ source => 'Aqbookseller',value => { listincgst => 0, listprice => $spent_currency->{currency}, invoiceprice => $spent_currency->{currency} } });
+    my $spent_vendor   = $builder->build({ source => 'Aqbookseller',value => { listincgst => 0, listprice => $spent_currency->{currency}, invoiceprice => $spent_currency->{currency} } });
+    my $budget_authcat = $builder->build({ source => 'AuthorisedValueCategory', value => {} });
+    my $spent_sort1    = $builder->build({ source => 'AuthorisedValue', value => {
+            category => $budget_authcat->{category_name},
+            authorised_value => 'PICKLE',
+        }
+    });
+    my $spent_budget_period = $builder->build({ source => 'Aqbudgetperiod', value => {
+        }
+    });
+    my $spent_budget = $builder->build({ source => 'Aqbudget', value => {
+            sort1_authcat => $budget_authcat->{category_name},
+            budget_period_id => $spent_budget_period->{budget_period_id},
+            budget_parent_id => undef,
+        }
+    });
     my $spent_orderinfo = {
-        basketno => $spent_basket->{basketno},
-        booksellerid => $spent_vendor->{id},
-        rrp => 16.99,
-        discount => .42,
-        ecost => 16.91,
-        biblionumber => $spent_biblio->{biblionumber},
-        currency => $spent_currency->{currency},
-        tax_rate_on_ordering => 0,
-        tax_value_on_ordering => 0,
-        tax_rate_on_receiving => 0,
-        tax_value_on_receiving => 0,
-        quantity => 8,
-        quantityreceived => 0,
+        basketno                => $spent_basket->{basketno},
+        booksellerid            => $spent_vendor->{id},
+        rrp                     => 16.99,
+        discount                => .42,
+        ecost                   => 16.91,
+        biblionumber            => $item_1->biblionumber,
+        currency                => $spent_currency->{currency},
+        tax_rate_on_ordering    => 0,
+        tax_value_on_ordering   => 0,
+        tax_rate_on_receiving   => 0,
+        tax_value_on_receiving  => 0,
+        quantity                => 8,
+        quantityreceived        => 0,
         datecancellationprinted => undef,
-        datereceived => undef,
+        datereceived            => undef,
+        budget_id               => $spent_budget->{budget_id},
+        sort1                   => $spent_sort1->{authorised_value},
     };
 
 #Okay we have basically what the user would enter, now we do some maths
@@ -1019,34 +1059,66 @@ subtest 'GetBudgetSpent GetBudgetOrdered GetBudgetsPlanCell tests' => sub {
     $spent_ordered = GetBudgetOrdered( $spent_order->{budget_id} );
     is($spent_ordered,'78.8',"We expect the ordered amount to be equal to the estimated price rounded times quantity");
 
+    #Test GetBudgetHierarchy for rounding
+    t::lib::Mocks::mock_preference('OrderPriceRounding','');
+    my $gbh = GetBudgetHierarchy($spent_budget->{budget_period_id});
+    is ( @$gbh[0]->{budget_spent}+0, 0, "We expect this to be an exact order cost * quantity");
+    is ( @$gbh[0]->{budget_ordered}+0, 78.8336, "We expect this to be an exact order cost * quantity");
+    t::lib::Mocks::mock_preference('OrderPriceRounding','nearest_cent');
+    $gbh = GetBudgetHierarchy($spent_budget->{budget_period_id});
+    is ( @$gbh[0]->{budget_spent}+0, 0, "We expect this to be an rounded order cost * quantity");
+    is ( @$gbh[0]->{budget_ordered}+0, 78.8, "We expect this to be an exact order cost * quantity");
+
 #Let's test some budget planning
 #Regression tests for bug 18736
     #We need an item to test by BRANCHES
-    my $item_1 = $builder->build({ source => 'Item' });
-    my $order_item_1 = $builder->build({ source => 'AqordersItem', value => { ordernumber => $spent_order->{ordernumber}, itemnumber => $item_1->{itemnumber}  } });
+    my $order_item_1 = $builder->build({ source => 'AqordersItem', value => { ordernumber => $spent_order->{ordernumber}, itemnumber => $item_1->itemnumber  } });
     my $spent_fund = Koha::Acquisition::Funds->find( $spent_order->{budget_id} );
     my $cell = {
         authcat => 'MONTHS',
         cell_authvalue => $spent_order->{entrydate}, #normally this is just the year/month but full won't hurt us here
         budget_id => $spent_order->{budget_id},
         budget_period_id => $spent_fund->budget_period_id,
+        sort1_authcat => $spent_order->{sort1_authcat},
+        sort2_authcat => $spent_order->{sort1_authcat},
+    };
+    my $test_values = {
+        'MONTHS' => {
+            authvalue => $spent_order->{entrydate},
+            expected_rounded => 9.85,
+            expected_exact   => 9.8542,
+        },
+        'BRANCHES' => {
+            authvalue => $item_1->homebranch,
+            expected_rounded => 9.85,
+            expected_exact   => 9.8542,
+        },
+        'ITEMTYPES' => {
+            authvalue => $item_1->biblioitem->itemtype,
+            expected_rounded => 78.80,
+            expected_exact   => 78.8336,
+        },
+        'ELSE' => {
+            authvalue => $spent_sort1->{authorised_value},
+            expected_rounded => 78.8,
+            expected_exact   => 78.8336,
+        },
     };
-    t::lib::Mocks::mock_preference('OrderPriceRounding','');
-    my ( $actual ) = GetBudgetsPlanCell( $cell, undef, undef); #we are only testing the actual for now
-    is ( $actual, '9.854200', "We expect this to be an exact order cost"); #really we should expect cost*quantity but we don't
-    t::lib::Mocks::mock_preference('OrderPriceRounding','nearest_cent');
-    ( $actual ) = GetBudgetsPlanCell( $cell, undef, undef); #we are only testing the actual for now
-    is ( $actual, '9.8500', "We expect this to be a rounded order cost"); #really we should expect cost*quantity but we don't
-    $cell->{authcat} = 'BRANCHES';
-    $cell->{authvalue} = $item_1->{homebranch};
-    ( $actual ) = GetBudgetsPlanCell( $cell, undef, undef); #we are only testing the actual for now
-    t::lib::Mocks::mock_preference('OrderPriceRounding','');
-    ( $actual ) = GetBudgetsPlanCell( $cell, undef, undef); #we are only testing the actual for now
-    is ( $actual, '9.854200', "We expect this to be full cost for items from the branch"); #here we rely on items having been created for each order, again quantity should be considered
-    t::lib::Mocks::mock_preference('OrderPriceRounding','nearest_cent');
-    ( $actual ) = GetBudgetsPlanCell( $cell, undef, undef); #we are only testing the actual for now
-    is ( $actual, '9.8500', "We expect this to be rounded cost for items from the branch"); #here we rely on items having been created for each order, again quantity should be considered
 
+    for my $authcat ( keys %$test_values ) {
+        my $test_val         = $test_values->{$authcat};
+        my $authvalue        = $test_val->{authvalue};
+        my $expected_rounded = $test_val->{expected_rounded};
+        my $expected_exact   = $test_val->{expected_exact};
+        $cell->{authcat} = $authcat;
+        $cell->{authvalue} = $authvalue;
+        t::lib::Mocks::mock_preference('OrderPriceRounding','');
+        my ( $actual ) = GetBudgetsPlanCell( $cell, undef, $spent_budget); #we are only testing the actual for now
+        is ( $actual+0, $expected_exact, "We expect this to be an exact order cost ($authcat)"); #really we should expect cost*quantity but we don't
+        t::lib::Mocks::mock_preference('OrderPriceRounding','nearest_cent');
+        ( $actual ) = GetBudgetsPlanCell( $cell, undef, $spent_budget); #we are only testing the actual for now
+        is ( $actual+0, $expected_rounded, "We expect this to be a rounded order cost ($authcat)"); #really we should expect cost*quantity but we don't
+    }
 
 #Okay, now we can receive the order, giving the price as the user would
 
@@ -1061,7 +1133,7 @@ subtest 'GetBudgetSpent GetBudgetOrdered GetBudgetsPlanCell tests' => sub {
     });
     my $received_order = $builder->build({ source => 'Aqorder', value => $spent_orderinfo });
 
-#And receive
+#And receive a copy of the order so we have both spent and ordered values
 
     ModReceiveOrder({
             biblionumber => $spent_order->{biblionumber},
@@ -1082,6 +1154,16 @@ subtest 'GetBudgetSpent GetBudgetOrdered GetBudgetsPlanCell tests' => sub {
     $spent_spent = GetBudgetSpent( $spent_order->{budget_id} );
     is($spent_spent,'78.8',"We expect the spent amount to be equal to the estimated price rounded times quantity");
 
+    #Test GetBudgetHierarchy for rounding
+    t::lib::Mocks::mock_preference('OrderPriceRounding','');
+    $gbh = GetBudgetHierarchy($spent_budget->{budget_period_id});
+    is ( @$gbh[0]->{budget_spent}, 78.8336, "We expect this to be an exact order cost * quantity");
+    is ( @$gbh[0]->{budget_ordered}, 78.8336, "We expect this to be an exact order cost * quantity");
+    t::lib::Mocks::mock_preference('OrderPriceRounding','nearest_cent');
+    $gbh = GetBudgetHierarchy($spent_budget->{budget_period_id});
+    is ( @$gbh[0]->{budget_spent}+0, 78.8, "We expect this to be a rounded order cost * quantity");
+    is ( @$gbh[0]->{budget_ordered}, 78.8, "We expect this to be a rounded order cost * quantity");
+
 };
 
 sub _get_dependencies {