7 use Module::Load::Conditional qw/check_install/;
10 if ( check_install( module => 'Test::DBIx::Class' ) ) {
13 plan skip_all => "Need Test::DBIx::Class"
17 use_ok('C4::Context');
18 use_ok('Koha::Number::Price');
20 t::lib::Mocks::mock_preference( 'TaxRates', '0.02|0.05|0.196' );
22 use Test::DBIx::Class;
24 my $db = Test::MockModule->new('Koha::Database');
25 $db->mock( _new_schema => sub { return Schema(); } );
26 Koha::Database::flush_schema_cache();
30 [ qw/ currency symbol rate active / ],
31 [ 'my_cur', '€', 1, 1, ],
34 [ qw/ id name listincgst invoiceincgst / ],
41 [ qw/ basketno basketname booksellerid / ],
47 ], 'add currency fixtures';
49 my $bookseller_module = Test::MockModule->new('Koha::Acquisition::Bookseller');
51 my ( $basketno_0_0, $basketno_0_1, $basketno_1_0, $basketno_1_1 ) = (1, 2, 3, 4);
52 my ( $invoiceid_0_0, $invoiceid_1_1 );
55 for my $currency_format ( qw( US FR ) ) {
56 t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
57 subtest 'Configuration 1: 0 0 (Vendor List prices do not include tax / Invoice prices do not include tax)' => sub {
60 my $biblionumber_0_0 = 42;
62 my $order_0_0 = Koha::Acquisition::Order->new({
63 biblionumber => $biblionumber_0_0,
67 quantityreceived => 2,
68 basketno => $basketno_0_0,
69 invoiceid => $invoiceid_0_0,
72 tax_rate_on_ordering => 0.0500,
73 tax_rate_on_receiving => 0.0500,
75 datereceived => $today
77 $order_0_0->populate_with_prices_for_ordering();
81 got => $order_0_0->rrp_tax_included,
84 field => 'rrp_tax_included'
89 got => $order_0_0->rrp_tax_excluded,
92 field => 'rrp_tax_excluded'
97 got => $order_0_0->ecost_tax_included,
100 field => 'ecost_tax_included'
105 got => $order_0_0->ecost_tax_excluded,
108 field => 'ecost_tax_excluded'
113 got => $order_0_0->tax_value_on_ordering,
120 $order_0_0->populate_with_prices_for_receiving();
124 got => $order_0_0->unitprice_tax_included,
127 field => 'unitprice_tax_included'
132 got => $order_0_0->unitprice_tax_excluded,
135 field => 'unitprice_tax_excluded'
140 got => $order_0_0->tax_value_on_receiving,
148 subtest 'Configuration 1: 1 1 (Vendor List prices do include tax / Invoice prices include tax)' => sub {
151 my $biblionumber_1_1 = 43;
152 my $order_1_1 = Koha::Acquisition::Order->new({
153 biblionumber => $biblionumber_1_1,
157 quantityreceived => 2,
158 basketno => $basketno_1_1,
159 invoiceid => $invoiceid_1_1,
162 tax_rate_on_ordering => 0.0500,
163 tax_rate_on_receiving => 0.0500,
165 datereceived => $today
168 $order_1_1->populate_with_prices_for_ordering();
172 got => $order_1_1->rrp_tax_included,
175 field => 'rrp_tax_included'
180 got => $order_1_1->rrp_tax_excluded,
183 field => 'rrp_tax_excluded'
188 got => $order_1_1->ecost_tax_included,
191 field => 'ecost_tax_included'
196 got => $order_1_1->ecost_tax_excluded,
199 field => 'ecost_tax_excluded'
204 got => $order_1_1->tax_value_on_ordering,
211 $order_1_1->populate_with_prices_for_receiving();
215 got => $order_1_1->unitprice_tax_included,
218 field => 'unitprice_tax_included'
223 got => $order_1_1->unitprice_tax_excluded,
226 field => 'unitprice_tax_excluded'
231 got => $order_1_1->tax_value_on_receiving,
238 # When unitprice is 0.00
239 # Koha::Acquisition::Order::populate_with_prices_for_ordering() falls
240 # back to using ecost_tax_included and ecost_tax_excluded
241 $order_1_1 = Koha::Acquisition::Order->new({
242 biblionumber => $biblionumber_1_1,
246 quantityreceived => 1,
247 basketno => $basketno_1_1,
248 invoiceid => $invoiceid_1_1,
251 tax_rate_on_ordering => 0.1500,
252 tax_rate_on_receiving => 0.1500,
254 datereceived => $today
257 $order_1_1->populate_with_prices_for_ordering();
261 got => $order_1_1->ecost_tax_included,
264 field => 'ecost_tax_included'
269 got => $order_1_1->ecost_tax_excluded,
272 field => 'ecost_tax_excluded'
277 got => $order_1_1->tax_value_on_ordering,
285 subtest 'Configuration 1: 1 0 (Vendor List prices include tax / Invoice prices do not include tax)' => sub {
288 my $biblionumber_1_0 = 44;
289 my $order_1_0 = Koha::Acquisition::Order->new({
290 biblionumber => $biblionumber_1_0,
294 quantityreceived => 2,
295 basketno => $basketno_1_0,
296 invoiceid => $invoiceid_1_1,
299 tax_rate_on_ordering => 0.0500,
300 tax_rate_on_receiving => 0.0500,
302 datereceived => $today
305 $order_1_0->populate_with_prices_for_ordering();
309 got => $order_1_0->rrp_tax_included,
312 field => 'rrp_tax_included'
317 got => $order_1_0->rrp_tax_excluded,
320 field => 'rrp_tax_excluded'
325 got => $order_1_0->ecost_tax_included,
328 field => 'ecost_tax_included'
333 got => $order_1_0->ecost_tax_excluded,
336 field => 'ecost_tax_excluded'
339 # If we order with unitprice = 0, tax is calculated from the ecost
340 # (note that in addorder.pl and addorderiso2709 the unitprice may/will be set to the ecost
343 got => $order_1_0->tax_value_on_ordering,
349 $order_1_0->unitprice(70.29);
350 $order_1_0->populate_with_prices_for_ordering();
352 # If a unitprice is provided at ordering, we calculate the tax from that
355 got => $order_1_0->tax_value_on_ordering,
362 $order_1_0->populate_with_prices_for_receiving();
366 got => $order_1_0->unitprice_tax_included,
369 field => 'unitprice_tax_included'
374 got => $order_1_0->unitprice_tax_excluded,
377 field => 'unitprice_tax_excluded'
382 got => $order_1_0->tax_value_on_receiving,
390 subtest 'Configuration 1: 0 1 (Vendor List prices do not include tax / Invoice prices include tax)' => sub {
393 my $biblionumber_0_1 = 45;
394 my $order_0_1 = Koha::Acquisition::Order->new({
395 biblionumber => $biblionumber_0_1,
399 quantityreceived => 2,
400 basketno => $basketno_0_1,
401 invoiceid => $invoiceid_1_1,
404 tax_rate_on_ordering => 0.0500,
405 tax_rate_on_receiving => 0.0500,
407 datereceived => $today
410 $order_0_1->populate_with_prices_for_ordering();
414 got => $order_0_1->rrp_tax_included,
417 field => 'rrp_tax_included'
422 got => $order_0_1->rrp_tax_excluded,
425 field => 'rrp_tax_excluded'
430 got => $order_0_1->ecost_tax_included,
433 field => 'ecost_tax_included'
438 got => $order_0_1->ecost_tax_excluded,
441 field => 'ecost_tax_excluded'
444 # If we order with unitprice = 0, tax is calculated from the ecost
445 # (note that in addorder.pl and addorderiso2709 the unitprice may/will be set to the ecost
448 got => $order_0_1->tax_value_on_ordering,
454 $order_0_1->unitprice(77.490000);
455 $order_0_1->populate_with_prices_for_ordering();
457 # If a unitprice is provided at ordering, we calculate the tax from that
460 got => $order_0_1->tax_value_on_ordering,
466 $order_0_1->populate_with_prices_for_receiving();
470 got => $order_0_1->unitprice_tax_included,
473 field => 'unitprice_tax_included'
478 got => $order_0_1->unitprice_tax_excluded,
481 field => 'unitprice_tax_excluded'
486 got => $order_0_1->tax_value_on_receiving,
498 Koha::Number::Price->new( $params->{got} )->format,
499 Koha::Number::Price->new( $params->{expected} )->format,
500 "configuration $params->{conf}: $params->{field} should be correctly calculated"
505 for my $currency_format ( qw( US FR ) ) {
506 t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
507 is( Koha::Number::Price->new( 1234567 )->format_for_editing, '1234567.00', 'format_for_editing should return unformated integer part with 2 decimals' );
508 is( Koha::Number::Price->new( 1234567.89 )->format_for_editing, '1234567.89', 'format_for_editing should return unformated integer part with 2 decimals' );