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, @biblios );
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 }
40 push @biblios, $builder->build_sample_biblio;
43 my ( $basketno_0_0, $basketno_0_1, $basketno_1_0, $basketno_1_1 ) = map { $_->id } @baskets;
44 my ( $invoiceid_0_0, $invoiceid_1_1 );
47 for my $currency_format ( qw( US FR ) ) {
48 t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
49 subtest 'Configuration 1: 0 0 (Vendor List prices do not include tax / Invoice prices do not include tax)' => sub {
52 my $biblionumber_0_0 = $biblios[0]->id;
54 my $order_0_0 = Koha::Acquisition::Order->new({
55 biblionumber => $biblionumber_0_0,
59 quantityreceived => 2,
60 basketno => $basketno_0_0,
61 invoiceid => $invoiceid_0_0,
64 tax_rate_on_ordering => 0.0500,
65 tax_rate_on_receiving => 0.0500,
67 datereceived => $today
69 $order_0_0->populate_with_prices_for_ordering();
73 got => $order_0_0->rrp_tax_included,
76 field => 'rrp_tax_included'
81 got => $order_0_0->rrp_tax_excluded,
84 field => 'rrp_tax_excluded'
89 got => $order_0_0->ecost_tax_included,
92 field => 'ecost_tax_included'
97 got => $order_0_0->ecost_tax_excluded,
100 field => 'ecost_tax_excluded'
105 got => $order_0_0->tax_value_on_ordering,
112 $order_0_0->populate_with_prices_for_receiving();
116 got => $order_0_0->unitprice_tax_included,
119 field => 'unitprice_tax_included'
124 got => $order_0_0->unitprice_tax_excluded,
127 field => 'unitprice_tax_excluded'
132 got => $order_0_0->tax_value_on_receiving,
140 subtest 'Configuration 1: 1 1 (Vendor List prices do include tax / Invoice prices include tax)' => sub {
143 my $biblionumber_1_1 = $biblios[3]->id;
144 my $order_1_1 = Koha::Acquisition::Order->new({
145 biblionumber => $biblionumber_1_1,
149 quantityreceived => 2,
150 basketno => $basketno_1_1,
151 invoiceid => $invoiceid_1_1,
154 tax_rate_on_ordering => 0.0500,
155 tax_rate_on_receiving => 0.0500,
157 datereceived => $today
160 $order_1_1->populate_with_prices_for_ordering();
164 got => $order_1_1->rrp_tax_included,
167 field => 'rrp_tax_included'
172 got => $order_1_1->rrp_tax_excluded,
175 field => 'rrp_tax_excluded'
180 got => $order_1_1->ecost_tax_included,
183 field => 'ecost_tax_included'
188 got => $order_1_1->ecost_tax_excluded,
191 field => 'ecost_tax_excluded'
196 got => $order_1_1->tax_value_on_ordering,
203 $order_1_1->populate_with_prices_for_receiving();
207 got => $order_1_1->unitprice_tax_included,
210 field => 'unitprice_tax_included'
215 got => $order_1_1->unitprice_tax_excluded,
218 field => 'unitprice_tax_excluded'
223 got => $order_1_1->tax_value_on_receiving,
230 # When unitprice is 0.00
231 # Koha::Acquisition::Order::populate_with_prices_for_ordering() falls
232 # back to using ecost_tax_included and ecost_tax_excluded
233 $order_1_1 = Koha::Acquisition::Order->new({
234 biblionumber => $biblionumber_1_1,
238 quantityreceived => 1,
239 basketno => $basketno_1_1,
240 invoiceid => $invoiceid_1_1,
243 tax_rate_on_ordering => 0.1500,
244 tax_rate_on_receiving => 0.1500,
246 datereceived => $today
249 $order_1_1->populate_with_prices_for_ordering();
253 got => $order_1_1->ecost_tax_included,
256 field => 'ecost_tax_included'
261 got => $order_1_1->ecost_tax_excluded,
264 field => 'ecost_tax_excluded'
269 got => $order_1_1->tax_value_on_ordering,
277 subtest 'Configuration 1: 1 0 (Vendor List prices include tax / Invoice prices do not include tax)' => sub {
280 my $biblionumber_1_0 = $biblios[2]->id;
281 my $order_1_0 = Koha::Acquisition::Order->new({
282 biblionumber => $biblionumber_1_0,
286 quantityreceived => 2,
287 basketno => $basketno_1_0,
288 invoiceid => $invoiceid_1_1,
291 tax_rate_on_ordering => 0.0500,
292 tax_rate_on_receiving => 0.0500,
294 datereceived => $today
297 $order_1_0->populate_with_prices_for_ordering();
301 got => $order_1_0->rrp_tax_included,
304 field => 'rrp_tax_included'
309 got => $order_1_0->rrp_tax_excluded,
312 field => 'rrp_tax_excluded'
317 got => $order_1_0->ecost_tax_included,
320 field => 'ecost_tax_included'
325 got => $order_1_0->ecost_tax_excluded,
328 field => 'ecost_tax_excluded'
331 # If we order with unitprice = 0, tax is calculated from the ecost
332 # (note that in addorder.pl and addorderiso2709 the unitprice may/will be set to the ecost
335 got => $order_1_0->tax_value_on_ordering,
341 $order_1_0->unitprice(70.29);
342 $order_1_0->populate_with_prices_for_ordering();
344 # If a unitprice is provided at ordering, we calculate the tax from that
347 got => $order_1_0->tax_value_on_ordering,
354 $order_1_0->populate_with_prices_for_receiving();
358 got => $order_1_0->unitprice_tax_included,
361 field => 'unitprice_tax_included'
366 got => $order_1_0->unitprice_tax_excluded,
369 field => 'unitprice_tax_excluded'
374 got => $order_1_0->tax_value_on_receiving,
382 subtest 'Configuration 1: 0 1 (Vendor List prices do not include tax / Invoice prices include tax)' => sub {
385 my $biblionumber_0_1 = $biblios[1]->id;
386 my $order_0_1 = Koha::Acquisition::Order->new({
387 biblionumber => $biblionumber_0_1,
391 quantityreceived => 2,
392 basketno => $basketno_0_1,
393 invoiceid => $invoiceid_1_1,
396 tax_rate_on_ordering => 0.0500,
397 tax_rate_on_receiving => 0.0500,
399 datereceived => $today
402 $order_0_1->populate_with_prices_for_ordering();
406 got => $order_0_1->rrp_tax_included,
409 field => 'rrp_tax_included'
414 got => $order_0_1->rrp_tax_excluded,
417 field => 'rrp_tax_excluded'
422 got => $order_0_1->ecost_tax_included,
425 field => 'ecost_tax_included'
430 got => $order_0_1->ecost_tax_excluded,
433 field => 'ecost_tax_excluded'
436 # If we order with unitprice = 0, tax is calculated from the ecost
437 # (note that in addorder.pl and addorderiso2709 the unitprice may/will be set to the ecost
440 got => $order_0_1->tax_value_on_ordering,
446 $order_0_1->unitprice(77.490000);
447 $order_0_1->populate_with_prices_for_ordering();
449 # If a unitprice is provided at ordering, we calculate the tax from that
452 got => $order_0_1->tax_value_on_ordering,
458 $order_0_1->populate_with_prices_for_receiving();
462 got => $order_0_1->unitprice_tax_included,
465 field => 'unitprice_tax_included'
470 got => $order_0_1->unitprice_tax_excluded,
473 field => 'unitprice_tax_excluded'
478 got => $order_0_1->tax_value_on_receiving,
490 Koha::Number::Price->new( $params->{got} )->format,
491 Koha::Number::Price->new( $params->{expected} )->format,
492 "configuration $params->{conf}: $params->{field} should be correctly calculated"
497 for my $currency_format (qw( US FR )) {
498 t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
500 Koha::Number::Price->new(1234567)->format_for_editing, '1234567.00',
501 'format_for_editing should return unformated integer part with 2 decimals'
504 Koha::Number::Price->new(1234567.89)->format_for_editing, '1234567.89',
505 'format_for_editing should return unformated integer part with 2 decimals'
510 $schema->storage->txn_rollback;