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::Acquisition');
18 use_ok('C4::Bookseller');
19 use_ok('C4::Context');
20 use_ok('Koha::Number::Price');
22 t::lib::Mocks::mock_preference( 'gist', '0.02|0.05|0.196' );
24 use Test::DBIx::Class {
25 schema_class => 'Koha::Schema',
26 connect_info => ['dbi:SQLite:dbname=:memory:','',''],
27 connect_opts => { name_sep => '.', quote_char => '`', },
28 fixture_class => '::Populate',
31 my $db = Test::MockModule->new('Koha::Database');
32 $db->mock( _new_schema => sub { return Schema(); } );
36 [ qw/ currency symbol rate active / ],
37 [[ 'my_cur', '€', 1, 1, ]],
39 ], 'add currency fixtures';
41 my $bookseller_module = Test::MockModule->new('Koha::Acquisition::Bookseller');
43 my ( $basketno_0_0, $basketno_1_1, $basketno_1_0, $basketno_0_1 );
44 my ( $invoiceid_0_0, $invoiceid_1_1, $invoiceid_1_0, $invoiceid_0_1 );
47 for my $currency_format ( qw( US FR ) ) {
48 t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
49 subtest 'Configuration 1: 0 0' => sub {
51 $bookseller_module->mock(
54 return { listincgst => 0, invoiceincgst => 0 };
58 my $biblionumber_0_0 = 42;
61 biblionumber => $biblionumber_0_0,
63 listprice => 82.000000,
64 unitprice => 73.80000,
65 quantityreceived => 2,
66 basketno => $basketno_0_0,
67 invoiceid => $invoiceid_0_0,
72 datereceived => $today
74 $order_0_0 = C4::Acquisition::populate_order_with_prices(
77 booksellerid => 'just_something',
82 # Note that this configuration is correct \o/
85 got => $order_0_0->{rrpgsti},
93 got => $order_0_0->{rrpgste},
101 got => $order_0_0->{ecostgsti},
109 got => $order_0_0->{ecostgste},
117 got => $order_0_0->{gstvalue},
125 got => $order_0_0->{totalgsti},
133 got => $order_0_0->{totalgste},
140 $order_0_0 = C4::Acquisition::populate_order_with_prices(
143 booksellerid => 'just_something',
148 # Note that this configuration is correct \o/
151 got => $order_0_0->{unitpricegsti},
154 field => 'unitpricegsti'
159 got => $order_0_0->{unitpricegste},
162 field => 'unitpricegste'
167 got => $order_0_0->{gstvalue},
175 got => $order_0_0->{totalgsti},
183 got => $order_0_0->{totalgste},
191 subtest 'Configuration 1: 1 1' => sub {
193 $bookseller_module->mock(
196 return { listincgst => 1, invoiceincgst => 1 };
200 my $biblionumber_1_1 = 43;
202 biblionumber => $biblionumber_1_1,
204 listprice => 82.000000,
205 unitprice => 73.800000,
206 quantityreceived => 2,
207 basketno => $basketno_1_1,
208 invoiceid => $invoiceid_1_1,
213 datereceived => $today
216 $order_1_1 = C4::Acquisition::populate_order_with_prices(
219 booksellerid => 'just_something',
224 # Note that this configuration is *not* correct
225 # gstvalue should be 7.03 instead of 7.02
228 got => $order_1_1->{rrpgsti},
236 got => $order_1_1->{rrpgste},
244 got => $order_1_1->{ecostgsti},
252 got => $order_1_1->{ecostgste},
260 got => $order_1_1->{gstvalue},
268 got => $order_1_1->{totalgsti},
276 got => $order_1_1->{totalgste},
283 $order_1_1 = C4::Acquisition::populate_order_with_prices(
286 booksellerid => 'just_something',
290 # Note that this configuration is *not* correct!
291 # gstvalue should be 7.03
294 got => $order_1_1->{unitpricegsti},
297 field => 'unitpricegsti'
302 got => $order_1_1->{unitpricegste},
305 field => 'unitpricegste'
310 got => $order_1_1->{gstvalue},
318 got => $order_1_1->{totalgsti},
326 got => $order_1_1->{totalgste},
334 subtest 'Configuration 1: 1 0' => sub {
336 $bookseller_module->mock(
339 return { listincgst => 1, invoiceincgst => 0 };
343 my $biblionumber_1_0 = 44;
345 biblionumber => $biblionumber_1_0,
347 listprice => 82.000000,
348 unitprice => 73.804500,
349 quantityreceived => 2,
350 basketno => $basketno_1_1,
351 invoiceid => $invoiceid_1_1,
356 datereceived => $today
359 $order_1_0 = C4::Acquisition::populate_order_with_prices(
362 booksellerid => 'just_something',
367 # Note that this configuration is *not* correct!
368 # rrp gsti should be 82 (what we inserted!)
369 # => Actually we need to fix the inserted value (here we have 82.01 in DB)
370 # gstvalue should be 7.03 instead of 7.02
374 got => $order_1_0->{rrpgsti},
382 got => $order_1_0->{rrpgste},
390 got => $order_1_0->{ecostgsti},
398 got => $order_1_0->{ecostgste},
406 got => $order_1_0->{gstvalue},
414 got => $order_1_0->{totalgsti},
422 got => $order_1_0->{totalgste},
429 $order_1_0 = C4::Acquisition::populate_order_with_prices(
432 booksellerid => 'just_something',
436 # Note that this configuration is *not* correct!
437 # gstvalue should be 7.03
440 got => $order_1_0->{unitpricegsti},
443 field => 'unitpricegsti'
448 got => $order_1_0->{unitpricegste},
451 field => 'unitpricegste'
456 got => $order_1_0->{gstvalue},
464 got => $order_1_0->{totalgsti},
472 got => $order_1_0->{totalgste},
480 subtest 'Configuration 1: 0 1' => sub {
482 $bookseller_module->mock(
485 return { listincgst => 0, invoiceincgst => 1 };
489 my $biblionumber_0_1 = 45;
491 biblionumber => $biblionumber_0_1,
493 listprice => 82.000000,
494 unitprice => 73.800000,
495 quantityreceived => 2,
496 basketno => $basketno_1_1,
497 invoiceid => $invoiceid_1_1,
502 datereceived => $today
505 $order_0_1 = C4::Acquisition::populate_order_with_prices(
508 booksellerid => 'just_something',
513 # Note that this configuration is correct \o/
516 got => $order_0_1->{rrpgsti},
524 got => $order_0_1->{rrpgste},
532 got => $order_0_1->{ecostgsti},
540 got => $order_0_1->{ecostgste},
548 got => $order_0_1->{gstvalue},
556 got => $order_0_1->{totalgsti},
564 got => $order_0_1->{totalgste},
571 $order_0_1 = C4::Acquisition::populate_order_with_prices(
574 booksellerid => 'just_something',
578 # Note that this configuration is correct
581 got => $order_0_1->{unitpricegsti},
584 field => 'unitpricegsti'
589 got => $order_0_1->{unitpricegste},
592 field => 'unitpricegste'
597 got => $order_0_1->{gstvalue},
605 got => $order_0_1->{totalgsti},
613 got => $order_0_1->{totalgste},
625 Koha::Number::Price->new( $params->{got} )->format,
626 Koha::Number::Price->new( $params->{expected} )->format,
627 "configuration $params->{conf}: $params->{field} should be correctly calculated"
632 for my $currency_format ( qw( US FR ) ) {
633 t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
634 is( Koha::Number::Price->new( 1234567 )->format_for_editing, '1234567.00', 'format_for_editing should return unformated integer part with 2 decimals' );
635 is( Koha::Number::Price->new( 1234567.89 )->format_for_editing, '1234567.89', 'format_for_editing should return unformated integer part with 2 decimals' );