Bug 15504: (follow-up) Tidy 'lastseen' section of unit test
[koha.git] / t / db_dependent / Acquisition / populate_order_with_prices.t
1 #!/usr/bin/env perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 44;
6 use C4::Context;
7 use Koha::Database;
8 use Koha::Acquisition::Bookseller;
9 use Koha::Acquisition::Order;
10 use t::lib::TestBuilder;
11 use t::lib::Mocks;
12
13 # Start transaction
14 my $schema = Koha::Database->new()->schema();
15 $schema->storage->txn_begin();
16
17 my $builder = t::lib::TestBuilder->new;
18
19 my $bookseller_inc_tax = Koha::Acquisition::Bookseller->new(
20     {
21         name          => "Tax included",
22         address1      => "bookseller's address",
23         phone         => "0123456",
24         active        => 1,
25         listincgst    => 1,
26         invoiceincgst => 1,
27     }
28 )->store;
29
30 my $bookseller_exc_tax = Koha::Acquisition::Bookseller->new(
31     {
32         name          => "Tax excluded",
33         address1      => "bookseller's address",
34         phone         => "0123456",
35         active        => 1,
36         listincgst    => 0,
37         invoiceincgst => 0,
38     }
39 )->store;
40
41 my $basket_exc_tax = Koha::Acquisition::Basket->new(
42     {
43         basketname => 'Basket tax excluded',
44         booksellerid => $bookseller_exc_tax->id,
45     }
46 )->store;
47
48 my $order_exc_tax = Koha::Acquisition::Order->new({
49     tax_rate_on_ordering  => .1965,
50     tax_rate_on_receiving => .1965,
51     discount  => .42,
52     rrp       => 16.99,
53     unitprice => "0.00",
54     quantity  => 8,
55     basketno => $basket_exc_tax->basketno,
56 });
57
58 #Vendor prices exclude tax, no rounding, ordering
59 t::lib::Mocks::mock_preference('OrderPriceRounding', '');
60 $order_exc_tax->populate_with_prices_for_ordering();
61
62 is( $order_exc_tax->rrp_tax_excluded+0      ,16.99      ,"Ordering tax excluded, no round: rrp tax excluded is rrp");
63 is( $order_exc_tax->rrp_tax_included+0      ,20.328535  ,"Ordering tax excluded, no round: rrp tax included is rr tax excluded * (1 + tax rate on ordering)");
64 is( $order_exc_tax->ecost_tax_excluded+0    ,9.8542     ,"Ordering tax excluded, no round: ecost tax excluded is rrp * ( 1 - discount )");
65 is( $order_exc_tax->ecost_tax_included+0    ,11.7905503 ,"Ordering tax excluded, no round: ecost tax included is ecost tax excluded * (1 + tax rate on ordering)");
66 is( $order_exc_tax->tax_value_on_ordering+0 ,15.4908024 ,"Ordering tax excluded, no round: tax value on ordering is quantity * ecost_tax_excluded * tax rate on ordering if no unitprice");
67
68 $order_exc_tax->unitprice(9.85);
69
70 $order_exc_tax->populate_with_prices_for_ordering();
71
72 is( $order_exc_tax->unitprice_tax_excluded+0      ,9.85      ,"Ordering tax excluded, no round: rrp tax excluded is rrp");
73 is( $order_exc_tax->unitprice_tax_included+0      ,11.785525  ,"Ordering tax excluded, no round: rrp tax included is rr tax excluded * (1 + tax rate on ordering)");
74 is( $order_exc_tax->tax_value_on_ordering+0 ,15.4842 ,"Ordering tax excluded, no round: tax value on ordering is quantity * unitprice_tax_excluded * tax rate on ordering if unitprice");
75
76 #Vendor prices exclude tax, no rounding, receiving
77 $order_exc_tax->populate_with_prices_for_receiving();
78
79 is( $order_exc_tax->unitprice+0              ,9.8542     ,"Receiving tax excluded, no round, rounded ecost tax excluded = rounded unitprice : unitprice is ecost tax excluded");
80 is( $order_exc_tax->unitprice_tax_excluded+0 ,9.8542     ,"Receiving tax excluded, no round, rounded ecost tax excluded = rounded unitprice : unitprice tax excluded is ecost tax excluded");
81 is( $order_exc_tax->unitprice_tax_included+0 ,11.7905503 ,"Receiving tax excluded, no round: unitprice tax included is unitprice tax excluded * (1 + tax rate on ordering)");
82 is( $order_exc_tax->tax_value_on_receiving+0  ,15.4908024 ,"Receiving tax excluded, no round: tax value on receiving is quantity * unitprice_tax_excluded * tax rate on receiving");
83
84
85 $order_exc_tax->unitprice(9.85);
86 #populate order with prices updates the passed in order hashref
87 #we need to reset after additional tests and changes
88
89 #Vendor prices exclude tax, rounding to nearest cent, ordering
90 t::lib::Mocks::mock_preference('OrderPriceRounding', 'nearest_cent');
91 $order_exc_tax->populate_with_prices_for_ordering();
92
93 is( $order_exc_tax->unitprice_tax_excluded+0      ,9.85      ,"Ordering tax excluded, round: unitprice tax excluded is unitprice");
94 is( $order_exc_tax->unitprice_tax_included+0      ,11.785525  ,"Ordering tax excluded, round: unitprice tax included is unitprice tax excluded * (1 + tax rate on ordering)");
95 is( $order_exc_tax->rrp_tax_excluded+0      ,16.99      ,"Ordering tax excluded, round: rrp tax excluded is rrp");
96 is( $order_exc_tax->rrp_tax_included+0      ,20.328535  ,"Ordering tax excluded, round: rrp tax included is rr tax excluded * (1 + tax rate on ordering)");
97 is( $order_exc_tax->ecost_tax_excluded+0    ,9.8542     ,"Ordering tax excluded, round: ecost tax excluded is rrp * ( 1 - discount )");
98 is( $order_exc_tax->ecost_tax_included+0    ,11.7905503 ,"Ordering tax excluded, round: ecost tax included is ecost tax excluded * (1 + tax rate on ordering)");
99 is( $order_exc_tax->tax_value_on_ordering+0 ,15.4842    ,"Ordering tax excluded, round: tax value on ordering is quantity * ecost_tax_excluded * tax rate on ordering");
100
101 #Vendor prices exclude tax, no rounding, receiving
102 $order_exc_tax->populate_with_prices_for_receiving();
103
104 is( $order_exc_tax->unitprice_tax_excluded+0 ,9.8542     ,"Receiving tax excluded, round, rounded ecost tax excluded = rounded unitprice : unitprice tax excluded is ecost tax excluded");
105 is( $order_exc_tax->unitprice_tax_included+0 ,11.7905503 ,"Receiving tax excluded, round: unitprice tax included is unitprice tax excluded * (1 + tax rate on ordering)");
106 is( $order_exc_tax->tax_value_on_receiving+0  ,15.4842   ,"Receiving tax excluded, round: tax value on receiving is quantity * unitprice_tax_excluded * tax rate on receiving");
107
108
109 my $basket_inc_tax = Koha::Acquisition::Basket->new(
110     {
111         basketname => 'Basket tax included',
112         booksellerid => $bookseller_inc_tax->id,
113     }
114 )->store;
115
116 my $order_inc_tax = Koha::Acquisition::Order->new({
117     tax_rate_on_ordering  => .1965,
118     tax_rate_on_receiving => .1965,
119     discount  => .42,
120     rrp       => 20.33,
121     unitprice => 0.00,
122     quantity  => 8,
123     basketno => $basket_inc_tax->basketno,
124 });
125
126 #Vendor prices include tax, no rounding, ordering
127 t::lib::Mocks::mock_preference('OrderPriceRounding', '');
128 $order_inc_tax->populate_with_prices_for_ordering();
129
130 is( $order_inc_tax->rrp_tax_included+0      ,20.33            ,"Ordering tax included, no round: rrp tax included is rrp");
131 is( $order_inc_tax->rrp_tax_excluded+0      ,16.9912244045132 ,"Ordering tax included, no round: rrp tax excluded is rrp tax included / (1 + tax rate on ordering)");
132 is( $order_inc_tax->ecost_tax_included+0    ,11.7914          ,"Ordering tax included, no round: ecost tax included is rrp tax included * (1 - discount)");
133 is( $order_inc_tax->ecost_tax_excluded+0    ,9.85491015461764 ,"Ordering tax included, no round: ecost tax excluded is rrp tax excluded * ( 1 - discount )");
134 is( $order_inc_tax->tax_value_on_ordering+0 ,15.4919187630589 ,"Ordering tax included, no round: tax value on ordering is ( ecost tax included - ecost tax excluded ) * quantity if no unitprice");
135
136 $order_inc_tax->unitprice(11.79);
137 $order_inc_tax->populate_with_prices_for_ordering();
138
139 is( $order_inc_tax->unitprice_tax_included+0 ,11.79             ,"Ordering tax included, no round: unitprice tax included is unitprice");
140 is( $order_inc_tax->unitprice_tax_excluded+0 ,9.85374007521939  ,"Ordering tax included, no round: unitprice tax excluded is unitprice tax included / (1 + tax_rate_on_ordering ");
141 is( $order_inc_tax->tax_value_on_ordering+0  ,15.4900793982449  ,"Ordering tax included, no round: tax value on ordering is ( unitprice tax included - unitprice tax excluded ) * quantity if unitprice");
142
143 #Vendor prices include tax, no rounding, receiving
144 $order_inc_tax->populate_with_prices_for_receiving();
145
146 is( $order_inc_tax->unitprice+0              ,11.7914          ,"Receiving tax included, no round, rounded ecost tax excluded = rounded unitprice : unitprice is ecost tax excluded");
147 is( $order_inc_tax->unitprice_tax_included+0 ,11.7914          ,"Receiving tax included, no round: unitprice tax included is unitprice");
148 is( $order_inc_tax->unitprice_tax_excluded+0 ,9.85491015461764 ,"Receiving tax included, no round: unitprice tax excluded is unitprice tax included / (1 + tax rate on receiving)");
149 is( $order_inc_tax->tax_value_on_receiving+0 ,15.4919187630589 ,"Receiving tax included, no round: tax value on receiving is quantity * unitprice_tax_excluded * tax rate on receiving");
150
151 #Vendor prices include tax, rounding to nearest cent, ordering
152 t::lib::Mocks::mock_preference('OrderPriceRounding', 'nearest_cent');
153 $order_inc_tax->unitprice(11.79);
154 $order_inc_tax->populate_with_prices_for_ordering();
155
156 is( $order_inc_tax->unitprice_tax_included+0      ,11.79      ,"Ordering tax included, round: unitprice tax included is unitprice");
157 is( $order_inc_tax->unitprice_tax_excluded+0,9.85374007521939 ,"Ordering tax included, round: unitprice tax excluded is unitprice tax included / (1 + tax_rate_on_ordering ");
158 is( $order_inc_tax->rrp_tax_included+0      ,20.33            ,"Ordering tax included, round: rrp tax included is rrp");
159 is( $order_inc_tax->rrp_tax_excluded+0      ,16.9912244045132 ,"Ordering tax included, round: rrp tax excluded is rounded rrp tax included * (1 + tax rate on ordering)");
160 is( $order_inc_tax->ecost_tax_included+0    ,11.7914          ,"Ordering tax included, round: ecost tax included is rounded rrp * ( 1 - discount )");
161 is( $order_inc_tax->ecost_tax_excluded+0    ,9.85491015461764 ,"Ordering tax included, round: ecost tax excluded is rounded ecost tax excluded * (1 - discount)");
162 is( $order_inc_tax->tax_value_on_ordering+0 ,15.52            ,"Ordering tax included, round: tax value on ordering is (ecost_tax_included - ecost_tax_excluded) * quantity");
163
164 #Vendor prices include tax, no rounding, receiving
165 $order_inc_tax->populate_with_prices_for_receiving();
166
167 is( $order_inc_tax->unitprice_tax_included+0 ,11.7914          ,"Receiving tax included, round: rounded ecost tax included = rounded unitprice : unitprice tax excluded is ecost tax included");
168 is( $order_inc_tax->unitprice_tax_excluded+0 ,9.85491015461764 ,"Receiving tax included, round: unitprice tax excluded is unitprice tax included / (1 + tax rate on ordering)");
169 is( $order_inc_tax->tax_value_on_receiving+0  ,15.4842         ,"Receiving tax included, round: tax value on receiving is quantity * (rounded unitprice_tax_excluded) * tax rate on receiving");
170
171
172 $schema->storage->txn_rollback();