Bug 23523: unitprice tax column values are not populated if entered upon ordering
[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::Acquisition;
7 use C4::Context;
8 use Koha::Database;
9 use t::lib::TestBuilder;
10 use t::lib::Mocks;
11
12 # Start transaction
13 my $schema = Koha::Database->new()->schema();
14 $schema->storage->txn_begin();
15
16 my $dbh = C4::Context->dbh;
17 $dbh->{RaiseError} = 1;
18
19 my $builder = t::lib::TestBuilder->new;
20
21 my $bookseller_inc_tax = Koha::Acquisition::Bookseller->new(
22     {
23         name          => "Tax included",
24         address1      => "bookseller's address",
25         phone         => "0123456",
26         active        => 1,
27         listincgst    => 1,
28         invoiceincgst => 1,
29     }
30 )->store;
31
32 my $bookseller_exc_tax = Koha::Acquisition::Bookseller->new(
33     {
34         name          => "Tax excluded",
35         address1      => "bookseller's address",
36         phone         => "0123456",
37         active        => 1,
38         listincgst    => 0,
39         invoiceincgst => 0,
40     }
41 )->store;
42
43 my $order_exc_tax = {
44     tax_rate  => .1965,
45     discount  => .42,
46     rrp       => 16.99,
47     unitprice => 0.00,
48     quantity  => 8,
49 };
50
51 #Vendor prices exclude tax, no rounding, ordering
52 t::lib::Mocks::mock_preference('OrderPriceRounding', '');
53 my $order_with_prices = C4::Acquisition::populate_order_with_prices({
54     ordering     => 1,
55     booksellerid => $bookseller_exc_tax->id,
56     order        => $order_exc_tax,
57 });
58
59 is( $order_with_prices->{rrp_tax_excluded}+0      ,16.99      ,"Ordering tax excluded, no round: rrp tax excluded is rrp");
60 is( $order_with_prices->{rrp_tax_included}+0      ,20.328535  ,"Ordering tax excluded, no round: rrp tax included is rr tax excluded * (1 + tax rate on ordering)");
61 is( $order_with_prices->{ecost_tax_excluded}+0    ,9.8542     ,"Ordering tax excluded, no round: ecost tax excluded is rrp * ( 1 - discount )");
62 is( $order_with_prices->{ecost_tax_included}+0    ,11.7905503 ,"Ordering tax excluded, no round: ecost tax included is ecost tax excluded * (1 + tax rate on ordering)");
63 is( $order_with_prices->{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");
64
65 $order_exc_tax->{unitprice} = 9.85;
66
67 $order_with_prices = C4::Acquisition::populate_order_with_prices({
68     ordering     => 1,
69     booksellerid => $bookseller_exc_tax->id,
70     order        => $order_exc_tax,
71 });
72
73 is( $order_with_prices->{unitprice_tax_excluded}+0      ,9.85      ,"Ordering tax excluded, no round: rrp tax excluded is rrp");
74 is( $order_with_prices->{unitprice_tax_included}+0      ,11.785525  ,"Ordering tax excluded, no round: rrp tax included is rr tax excluded * (1 + tax rate on ordering)");
75 is( $order_with_prices->{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");
76
77 #Vendor prices exclude tax, no rounding, receiving
78 $order_with_prices = C4::Acquisition::populate_order_with_prices({
79     receiving     => 1,
80     booksellerid => $bookseller_exc_tax->id,
81     order        => $order_exc_tax,
82 });
83
84 is( $order_with_prices->{unitprice}+0              ,9.8542     ,"Receiving tax excluded, no round, rounded ecost tax excluded = rounded unitprice : unitprice is ecost tax excluded");
85 is( $order_with_prices->{unitprice_tax_excluded}+0 ,9.8542     ,"Receiving tax excluded, no round, rounded ecost tax excluded = rounded unitprice : unitprice tax excluded is ecost tax excluded");
86 is( $order_with_prices->{unitprice_tax_included}+0 ,11.7905503 ,"Receiving tax excluded, no round: unitprice tax included is unitprice tax excluded * (1 + tax rate on ordering)");
87 is( $order_with_prices->{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");
88
89
90 $order_exc_tax->{unitprice} = 9.85;
91 #populate order with prices updates the passed in order hashref
92 #we need to reset after additional tests and changes
93
94 #Vendor prices exclude tax, rounding to nearest cent, ordering
95 t::lib::Mocks::mock_preference('OrderPriceRounding', 'nearest_cent');
96 $order_with_prices = C4::Acquisition::populate_order_with_prices({
97     ordering     => 1,
98     booksellerid => $bookseller_exc_tax->id,
99     order        => $order_exc_tax,
100 });
101
102 is( $order_with_prices->{unitprice_tax_excluded}+0      ,9.85      ,"Ordering tax excluded, round: unitprice tax excluded is unitprice");
103 is( $order_with_prices->{unitprice_tax_included}+0      ,11.785525  ,"Ordering tax excluded, round: unitprice tax included is unitprice tax excluded * (1 + tax rate on ordering)");
104 is( $order_with_prices->{rrp_tax_excluded}+0      ,16.99      ,"Ordering tax excluded, round: rrp tax excluded is rrp");
105 is( $order_with_prices->{rrp_tax_included}+0      ,20.328535  ,"Ordering tax excluded, round: rrp tax included is rr tax excluded * (1 + tax rate on ordering)");
106 is( $order_with_prices->{ecost_tax_excluded}+0    ,9.8542     ,"Ordering tax excluded, round: ecost tax excluded is rrp * ( 1 - discount )");
107 is( $order_with_prices->{ecost_tax_included}+0    ,11.7905503 ,"Ordering tax excluded, round: ecost tax included is ecost tax excluded * (1 + tax rate on ordering)");
108 is( $order_with_prices->{tax_value_on_ordering}+0 ,15.4842    ,"Ordering tax excluded, round: tax value on ordering is quantity * ecost_tax_excluded * tax rate on ordering");
109
110 #Vendor prices exclude tax, no rounding, receiving
111 $order_with_prices = C4::Acquisition::populate_order_with_prices({
112     receiving     => 1,
113     booksellerid => $bookseller_exc_tax->id,
114     order        => $order_exc_tax,
115 });
116
117 is( $order_with_prices->{unitprice_tax_excluded}+0 ,9.8542     ,"Receiving tax excluded, round, rounded ecost tax excluded = rounded unitprice : unitprice tax excluded is ecost tax excluded");
118 is( $order_with_prices->{unitprice_tax_included}+0 ,11.7905503 ,"Receiving tax excluded, round: unitprice tax included is unitprice tax excluded * (1 + tax rate on ordering)");
119 is( $order_with_prices->{tax_value_on_receiving}+0  ,15.4842   ,"Receiving tax excluded, round: tax value on receiving is quantity * unitprice_tax_excluded * tax rate on receiving");
120
121
122
123 my $order_inc_tax = {
124     tax_rate  => .1965,
125     discount  => .42,
126     rrp       => 20.33,
127     unitprice => 0.00,
128     quantity  => 8,
129 };
130
131 #Vendor prices include tax, no rounding, ordering
132 t::lib::Mocks::mock_preference('OrderPriceRounding', '');
133 $order_with_prices = C4::Acquisition::populate_order_with_prices({
134     ordering     => 1,
135     booksellerid => $bookseller_inc_tax->id,
136     order        => $order_inc_tax,
137 });
138
139 is( $order_with_prices->{rrp_tax_included}+0      ,20.33            ,"Ordering tax included, no round: rrp tax included is rrp");
140 is( $order_with_prices->{rrp_tax_excluded}+0      ,16.9912244045132 ,"Ordering tax included, no round: rrp tax excluded is rrp tax included / (1 + tax rate on ordering)");
141 is( $order_with_prices->{ecost_tax_included}+0    ,11.7914          ,"Ordering tax included, no round: ecost tax included is rrp tax included * (1 - discount)");
142 is( $order_with_prices->{ecost_tax_excluded}+0    ,9.85491015461764 ,"Ordering tax included, no round: ecost tax excluded is rrp tax excluded * ( 1 - discount )");
143 is( $order_with_prices->{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");
144
145 $order_inc_tax->{unitprice} = 11.79;
146 $order_with_prices = C4::Acquisition::populate_order_with_prices({
147     ordering     => 1,
148     booksellerid => $bookseller_inc_tax->id,
149     order        => $order_inc_tax,
150 });
151
152 is( $order_with_prices->{unitprice_tax_included}+0 ,11.79             ,"Ordering tax included, no round: unitprice tax included is unitprice");
153 is( $order_with_prices->{unitprice_tax_excluded}+0 ,9.85374007521939  ,"Ordering tax included, no round: unitprice tax excluded is unitprice tax included / (1 + tax_rate_on_ordering ");
154 is( $order_with_prices->{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");
155
156 #Vendor prices include tax, no rounding, receiving
157 $order_with_prices = C4::Acquisition::populate_order_with_prices({
158     receiving     => 1,
159     booksellerid => $bookseller_inc_tax->id,
160     order        => $order_inc_tax,
161 });
162
163 is( $order_with_prices->{unitprice}+0              ,11.7914          ,"Receiving tax included, no round, rounded ecost tax excluded = rounded unitprice : unitprice is ecost tax excluded");
164 is( $order_with_prices->{unitprice_tax_included}+0 ,11.7914          ,"Receiving tax included, no round: unitprice tax included is unitprice");
165 is( $order_with_prices->{unitprice_tax_excluded}+0 ,9.85491015461764 ,"Receiving tax included, no round: unitprice tax excluded is unitprice tax included / (1 + tax rate on receiving)");
166 is( $order_with_prices->{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");
167
168 #Vendor prices include tax, rounding to nearest cent, ordering
169 t::lib::Mocks::mock_preference('OrderPriceRounding', 'nearest_cent');
170 $order_inc_tax->{unitprice} = 11.79;
171 $order_with_prices = C4::Acquisition::populate_order_with_prices({
172     ordering     => 1,
173     booksellerid => $bookseller_inc_tax->id,
174     order        => $order_inc_tax,
175 });
176
177 is( $order_with_prices->{unitprice_tax_included}+0      ,11.79      ,"Ordering tax included, round: unitprice tax included is unitprice");
178 is( $order_with_prices->{unitprice_tax_excluded}+0,9.85374007521939 ,"Ordering tax included, round: unitprice tax excluded is unitprice tax included / (1 + tax_rate_on_ordering ");
179 is( $order_with_prices->{rrp_tax_included}+0      ,20.33            ,"Ordering tax included, round: rrp tax included is rrp");
180 is( $order_with_prices->{rrp_tax_excluded}+0      ,16.9912244045132 ,"Ordering tax included, round: rrp tax excluded is rounded rrp tax included * (1 + tax rate on ordering)");
181 is( $order_with_prices->{ecost_tax_included}+0    ,11.7914          ,"Ordering tax included, round: ecost tax included is rounded rrp * ( 1 - discount )");
182 is( $order_with_prices->{ecost_tax_excluded}+0    ,9.85491015461764 ,"Ordering tax included, round: ecost tax excluded is rounded ecost tax excluded * (1 - discount)");
183 is( $order_with_prices->{tax_value_on_ordering}+0 ,15.52            ,"Ordering tax included, round: tax value on ordering is (ecost_tax_included - ecost_tax_excluded) * quantity");
184
185 #Vendor prices include tax, no rounding, receiving
186 $order_with_prices = C4::Acquisition::populate_order_with_prices({
187     receiving     => 1,
188     booksellerid => $bookseller_inc_tax->id,
189     order        => $order_inc_tax,
190 });
191
192 is( $order_with_prices->{unitprice_tax_included}+0 ,11.7914          ,"Receiving tax included, round: rounded ecost tax included = rounded unitprice : unitprice tax excluded is ecost tax included");
193 is( $order_with_prices->{unitprice_tax_excluded}+0 ,9.85491015461764 ,"Receiving tax included, round: unitprice tax excluded is unitprice tax included / (1 + tax rate on ordering)");
194 is( $order_with_prices->{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");
195
196
197 $schema->storage->txn_rollback();