4 use Test::More tests => 1;
8 use t::lib::TestBuilder;
11 use Koha::Number::Price;
13 my $schema = Koha::Database->new->schema;
14 $schema->storage->txn_begin;
15 my $builder = t::lib::TestBuilder->new;
17 subtest 'Tests from t' => sub {
20 t::lib::Mocks::mock_preference( 'TaxRates', '0.02|0.05|0.196' );
21 my $bookseller_module = Test::MockModule->new('Koha::Acquisition::Bookseller');
23 my ( @booksellers, @baskets );
24 my @names = ( '0 0', '0 1', '1 0', '1 1' );
25 foreach my $i ( 1..4 ) {
26 push @booksellers, $builder->build_object(
28 class => 'Koha::Acquisition::Booksellers',
30 { name => $names[ $i - 1 ], listincgst => $i < 3 ? 0 : 1, invoiceincgst => $i % 2 == 0 ? 1 : 0 }
33 push @baskets, $builder->build_object(
35 class => 'Koha::Acquisition::Baskets',
37 { basketname => $names[ $i - 1 ], booksellerid => $booksellers[$i-1]->id }
42 my ( $basketno_0_0, $basketno_0_1, $basketno_1_0, $basketno_1_1 ) = map { $_->id } @baskets;
43 my ( $invoiceid_0_0, $invoiceid_1_1 );
46 for my $currency_format ( qw( US FR ) ) {
47 t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
48 subtest 'Configuration 1: 0 0 (Vendor List prices do not include tax / Invoice prices do not include tax)' => sub {
51 my $biblionumber_0_0 = 42;
53 my $order_0_0 = Koha::Acquisition::Order->new({
54 biblionumber => $biblionumber_0_0,
58 quantityreceived => 2,
59 basketno => $basketno_0_0,
60 invoiceid => $invoiceid_0_0,
63 tax_rate_on_ordering => 0.0500,
64 tax_rate_on_receiving => 0.0500,
66 datereceived => $today
68 $order_0_0->populate_with_prices_for_ordering();
72 got => $order_0_0->rrp_tax_included,
75 field => 'rrp_tax_included'
80 got => $order_0_0->rrp_tax_excluded,
83 field => 'rrp_tax_excluded'
88 got => $order_0_0->ecost_tax_included,
91 field => 'ecost_tax_included'
96 got => $order_0_0->ecost_tax_excluded,
99 field => 'ecost_tax_excluded'
104 got => $order_0_0->tax_value_on_ordering,
111 $order_0_0->populate_with_prices_for_receiving();
115 got => $order_0_0->unitprice_tax_included,
118 field => 'unitprice_tax_included'
123 got => $order_0_0->unitprice_tax_excluded,
126 field => 'unitprice_tax_excluded'
131 got => $order_0_0->tax_value_on_receiving,
139 subtest 'Configuration 1: 1 1 (Vendor List prices do include tax / Invoice prices include tax)' => sub {
142 my $biblionumber_1_1 = 43;
143 my $order_1_1 = Koha::Acquisition::Order->new({
144 biblionumber => $biblionumber_1_1,
148 quantityreceived => 2,
149 basketno => $basketno_1_1,
150 invoiceid => $invoiceid_1_1,
153 tax_rate_on_ordering => 0.0500,
154 tax_rate_on_receiving => 0.0500,
156 datereceived => $today
159 $order_1_1->populate_with_prices_for_ordering();
163 got => $order_1_1->rrp_tax_included,
166 field => 'rrp_tax_included'
171 got => $order_1_1->rrp_tax_excluded,
174 field => 'rrp_tax_excluded'
179 got => $order_1_1->ecost_tax_included,
182 field => 'ecost_tax_included'
187 got => $order_1_1->ecost_tax_excluded,
190 field => 'ecost_tax_excluded'
195 got => $order_1_1->tax_value_on_ordering,
202 $order_1_1->populate_with_prices_for_receiving();
206 got => $order_1_1->unitprice_tax_included,
209 field => 'unitprice_tax_included'
214 got => $order_1_1->unitprice_tax_excluded,
217 field => 'unitprice_tax_excluded'
222 got => $order_1_1->tax_value_on_receiving,
229 # When unitprice is 0.00
230 # Koha::Acquisition::Order::populate_with_prices_for_ordering() falls
231 # back to using ecost_tax_included and ecost_tax_excluded
232 $order_1_1 = Koha::Acquisition::Order->new({
233 biblionumber => $biblionumber_1_1,
237 quantityreceived => 1,
238 basketno => $basketno_1_1,
239 invoiceid => $invoiceid_1_1,
242 tax_rate_on_ordering => 0.1500,
243 tax_rate_on_receiving => 0.1500,
245 datereceived => $today
248 $order_1_1->populate_with_prices_for_ordering();
252 got => $order_1_1->ecost_tax_included,
255 field => 'ecost_tax_included'
260 got => $order_1_1->ecost_tax_excluded,
263 field => 'ecost_tax_excluded'
268 got => $order_1_1->tax_value_on_ordering,
276 subtest 'Configuration 1: 1 0 (Vendor List prices include tax / Invoice prices do not include tax)' => sub {
279 my $biblionumber_1_0 = 44;
280 my $order_1_0 = Koha::Acquisition::Order->new({
281 biblionumber => $biblionumber_1_0,
285 quantityreceived => 2,
286 basketno => $basketno_1_0,
287 invoiceid => $invoiceid_1_1,
290 tax_rate_on_ordering => 0.0500,
291 tax_rate_on_receiving => 0.0500,
293 datereceived => $today
296 $order_1_0->populate_with_prices_for_ordering();
300 got => $order_1_0->rrp_tax_included,
303 field => 'rrp_tax_included'
308 got => $order_1_0->rrp_tax_excluded,
311 field => 'rrp_tax_excluded'
316 got => $order_1_0->ecost_tax_included,
319 field => 'ecost_tax_included'
324 got => $order_1_0->ecost_tax_excluded,
327 field => 'ecost_tax_excluded'
330 # If we order with unitprice = 0, tax is calculated from the ecost
331 # (note that in addorder.pl and addorderiso2709 the unitprice may/will be set to the ecost
334 got => $order_1_0->tax_value_on_ordering,
340 $order_1_0->unitprice(70.29);
341 $order_1_0->populate_with_prices_for_ordering();
343 # If a unitprice is provided at ordering, we calculate the tax from that
346 got => $order_1_0->tax_value_on_ordering,
353 $order_1_0->populate_with_prices_for_receiving();
357 got => $order_1_0->unitprice_tax_included,
360 field => 'unitprice_tax_included'
365 got => $order_1_0->unitprice_tax_excluded,
368 field => 'unitprice_tax_excluded'
373 got => $order_1_0->tax_value_on_receiving,
381 subtest 'Configuration 1: 0 1 (Vendor List prices do not include tax / Invoice prices include tax)' => sub {
384 my $biblionumber_0_1 = 45;
385 my $order_0_1 = Koha::Acquisition::Order->new({
386 biblionumber => $biblionumber_0_1,
390 quantityreceived => 2,
391 basketno => $basketno_0_1,
392 invoiceid => $invoiceid_1_1,
395 tax_rate_on_ordering => 0.0500,
396 tax_rate_on_receiving => 0.0500,
398 datereceived => $today
401 $order_0_1->populate_with_prices_for_ordering();
405 got => $order_0_1->rrp_tax_included,
408 field => 'rrp_tax_included'
413 got => $order_0_1->rrp_tax_excluded,
416 field => 'rrp_tax_excluded'
421 got => $order_0_1->ecost_tax_included,
424 field => 'ecost_tax_included'
429 got => $order_0_1->ecost_tax_excluded,
432 field => 'ecost_tax_excluded'
435 # If we order with unitprice = 0, tax is calculated from the ecost
436 # (note that in addorder.pl and addorderiso2709 the unitprice may/will be set to the ecost
439 got => $order_0_1->tax_value_on_ordering,
445 $order_0_1->unitprice(77.490000);
446 $order_0_1->populate_with_prices_for_ordering();
448 # If a unitprice is provided at ordering, we calculate the tax from that
451 got => $order_0_1->tax_value_on_ordering,
457 $order_0_1->populate_with_prices_for_receiving();
461 got => $order_0_1->unitprice_tax_included,
464 field => 'unitprice_tax_included'
469 got => $order_0_1->unitprice_tax_excluded,
472 field => 'unitprice_tax_excluded'
477 got => $order_0_1->tax_value_on_receiving,
489 Koha::Number::Price->new( $params->{got} )->format,
490 Koha::Number::Price->new( $params->{expected} )->format,
491 "configuration $params->{conf}: $params->{field} should be correctly calculated"
496 for my $currency_format ( qw( US FR ) ) {
497 t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
498 is( Koha::Number::Price->new( 1234567 )->format_for_editing, '1234567.00', 'format_for_editing should return unformated integer part with 2 decimals' );
499 is( Koha::Number::Price->new( 1234567.89 )->format_for_editing, '1234567.89', 'format_for_editing should return unformated integer part with 2 decimals' );
503 $schema->storage->txn_rollback;