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

(-)a/t/Acquisition.t (+68 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Copyright (C) 2018  Mark Tompsett
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
use Test::More tests => 3;
22
use t::lib::Mocks;
23
24
use_ok( 'C4::Acquisition' );
25
26
subtest 'Tests for _get_rounding_sql' => sub {
27
28
    plan tests => 2;
29
30
    my $value = '3.141592';
31
32
    t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{} );
33
    my $no_rounding_result = C4::Acquisition::_get_rounding_sql($value);
34
    t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{nearest_cent} );
35
    my $rounding_result = C4::Acquisition::_get_rounding_sql($value);
36
37
    ok( $no_rounding_result eq $value, "Value ($value) not to be rounded" );
38
    ok( $rounding_result =~ /CAST/,    "Value ($value) will be rounded" );
39
40
};
41
42
subtest 'Test for get_rounded_price' => sub {
43
44
    plan tests => 6;
45
46
    my $exact_price      = 3.14;
47
    my $up_price         = 3.145592;
48
    my $down_price       = 3.141592;
49
    my $round_up_price   = sprintf( '%0.2f', $up_price );
50
    my $round_down_price = sprintf( '%0.2f', $down_price );
51
52
    t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{} );
53
    my $not_rounded_result1 = C4::Acquisition::get_rounded_price($exact_price);
54
    my $not_rounded_result2 = C4::Acquisition::get_rounded_price($up_price);
55
    my $not_rounded_result3 = C4::Acquisition::get_rounded_price($down_price);
56
    t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{nearest_cent} );
57
    my $rounded_result1 = C4::Acquisition::get_rounded_price($exact_price);
58
    my $rounded_result2 = C4::Acquisition::get_rounded_price($up_price);
59
    my $rounded_result3 = C4::Acquisition::get_rounded_price($down_price);
60
61
    is( $not_rounded_result1, $exact_price,      "Price ($exact_price) was correctly not rounded ($not_rounded_result1)" );
62
    is( $not_rounded_result2, $up_price,         "Price ($up_price) was correctly not rounded ($not_rounded_result2)" );
63
    is( $not_rounded_result3, $down_price,       "Price ($down_price) was correctly not rounded ($not_rounded_result3)" );
64
    is( $rounded_result1,     $exact_price,      "Price ($exact_price) was correctly rounded ($rounded_result1)" );
65
    is( $rounded_result2,     $round_up_price,   "Price ($up_price) was correctly rounded ($rounded_result2)" );
66
    is( $rounded_result3,     $round_down_price, "Price ($down_price) was correctly rounded ($rounded_result3)" );
67
68
};
(-)a/t/db_dependent/Budgets.t (-1 / +91 lines)
Lines 13-24 use Koha::Acquisition::Booksellers; Link Here
13
use Koha::Acquisition::Orders;
13
use Koha::Acquisition::Orders;
14
use Koha::Acquisition::Funds;
14
use Koha::Acquisition::Funds;
15
use Koha::Patrons;
15
use Koha::Patrons;
16
use Koha::Number::Price;
16
17
17
use t::lib::TestBuilder;
18
use t::lib::TestBuilder;
18
use Koha::DateUtils;
19
use Koha::DateUtils;
19
20
20
use YAML;
21
use YAML;
21
22
23
use t::lib::Mocks;
24
t::lib::Mocks::mock_preference('OrderPriceRounding','');
25
22
my $schema  = Koha::Database->new->schema;
26
my $schema  = Koha::Database->new->schema;
23
$schema->storage->txn_begin;
27
$schema->storage->txn_begin;
24
my $builder = t::lib::TestBuilder->new;
28
my $builder = t::lib::TestBuilder->new;
Lines 472-477 ModReceiveOrder({ Link Here
472
   invoice           => $test_invoice,
476
   invoice           => $test_invoice,
473
   received_items    => [],
477
   received_items    => [],
474
} );
478
} );
479
t::lib::Mocks::mock_preference('OrderPriceRounding','');
475
480
476
is ( GetBudgetSpent( $fund ), 6, "total shipping cost is 6");
481
is ( GetBudgetSpent( $fund ), 6, "total shipping cost is 6");
477
is ( GetBudgetOrdered( $fund ), '20', "total ordered price is 20");
482
is ( GetBudgetOrdered( $fund ), '20', "total ordered price is 20");
Lines 924-929 subtest 'GetBudgetSpent and GetBudgetOrdered' => sub { Link Here
924
    $ordered = GetBudgetOrdered( $budget->{budget_id} );
929
    $ordered = GetBudgetOrdered( $budget->{budget_id} );
925
    is( $spent, 6, "After adding invoice adjustment on a different budget should be 6 spent/budget unaffected");
930
    is( $spent, 6, "After adding invoice adjustment on a different budget should be 6 spent/budget unaffected");
926
    is( $ordered, 3, "After adding invoice adjustment on a different budget, should still be 3 ordered/budget unaffected");
931
    is( $ordered, 3, "After adding invoice adjustment on a different budget, should still be 3 ordered/budget unaffected");
932
};
933
934
subtest 'OrderPriceRounding GetBudgetSpent GetBudgetOrdered tests' => sub {
935
936
    plan tests => 8;
937
938
#Let's build an order, we need a couple things though
939
940
    my $spent_biblio = $builder->build({ source => 'Biblio' });
941
    my $spent_basket = $builder->build({ source => 'Aqbasket', value => { is_standing => 0 } });
942
    my $spent_invoice = $builder->build({ source => 'Aqinvoice'});
943
    my $spent_currency = $builder->build({ source => 'Currency', value => { active => 1, archived => 0, symbol => 'F', rate => 2, isocode => undef, currency => 'FOO' }  });
944
    my $spent_vendor = $builder->build({ source => 'Aqbookseller',value => { listincgst => 0, listprice => $spent_currency->{currency}, invoiceprice => $spent_currency->{currency} } });
945
    my $spent_orderinfo = {
946
        basketno => $spent_basket->{basketno},
947
        booksellerid => $spent_vendor->{id},
948
        rrp => 16.99,
949
        discount => .42,
950
        ecost => 16.91,
951
        biblionumber => $spent_biblio->{biblionumber},
952
        currency => $spent_currency->{currency},
953
        tax_rate_on_ordering => 0,
954
        tax_value_on_ordering => 0,
955
        tax_rate_on_receiving => 0,
956
        tax_value_on_receiving => 0,
957
        quantity => 8,
958
        quantityreceived => 0,
959
        datecancellationprinted => undef,
960
        datereceived => undef,
961
    };
962
963
#Okay we have basically what the user would enter, now we do some maths
964
965
    $spent_orderinfo = C4::Acquisition::populate_order_with_prices({
966
            order        => $spent_orderinfo,
967
            booksellerid => $spent_orderinfo->{booksellerid},
968
            ordering     => 1,
969
    });
970
971
#And let's place the order
972
973
    my $spent_order = $builder->build({ source => 'Aqorder', value => $spent_orderinfo });
974
    t::lib::Mocks::mock_preference('OrderPriceRounding','');
975
    my $spent_ordered = GetBudgetOrdered( $spent_order->{budget_id} );
976
977
    is($spent_orderinfo->{ecost_tax_excluded}, 9.854200,'We store extra precision in price calculation');
978
    is( Koha::Number::Price->new($spent_orderinfo->{ecost_tax_excluded})->format(), 9.85,'But the price as formatted is two digits');
979
    is($spent_ordered,'78.8336',"We expect the ordered amount to be equal to the estimated price times quantity with full precision");
980
981
    t::lib::Mocks::mock_preference('OrderPriceRounding','nearest_cent');
982
    $spent_ordered = GetBudgetOrdered( $spent_order->{budget_id} );
983
    is($spent_ordered,'78.8',"We expect the ordered amount to be equal to the estimated price rounded times quantity");
984
985
#Okay, now we can receive the order, giving the price as the user would
986
987
    $spent_orderinfo->{unitprice} = 9.85; #we are paying what we expected
988
989
#Do our maths
990
991
    $spent_orderinfo = C4::Acquisition::populate_order_with_prices({
992
            order        => $spent_orderinfo,
993
            booksellerid => $spent_orderinfo->{booksellerid},
994
            receiving    => 1,
995
    });
996
    my $received_order = $builder->build({ source => 'Aqorder', value => $spent_orderinfo });
997
998
#And receive
999
1000
    ModReceiveOrder({
1001
            biblionumber => $spent_order->{biblionumber},
1002
            order => $received_order,
1003
            invoice => $spent_invoice,
1004
            quantityreceived => $spent_order->{quantity},
1005
            budget_id => $spent_order->{budget_id},
1006
            received_items => [],
1007
    });
1008
1009
    t::lib::Mocks::mock_preference('OrderPriceRounding','');
1010
    my $spent_spent = GetBudgetSpent( $spent_order->{budget_id} );
1011
    is($spent_orderinfo->{unitprice_tax_excluded}, 9.854200,'We store extra precision in price calculation');
1012
    is( Koha::Number::Price->new($spent_orderinfo->{unitprice_tax_excluded})->format(), 9.85,'But the price as formatted is two digits');
1013
    is($spent_spent,'78.8336',"We expect the spent amount to be equal to the estimated price times quantity with full precision");
1014
1015
    t::lib::Mocks::mock_preference('OrderPriceRounding','nearest_cent');
1016
    $spent_spent = GetBudgetSpent( $spent_order->{budget_id} );
1017
    is($spent_spent,'78.8',"We expect the spent amount to be equal to the estimated price rounded times quantity");
927
1018
928
};
1019
};
929
1020
930
- 

Return to bug 18736