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