Bug 18557 - Mysqlim CURRENT_DATE in Koha::Clubs::get_enrollable
[koha.git] / t / Prices.t
blobf89c7f6dae718ece2b556b7d570bef434ac49c06
1 use Modern::Perl;
2 use Test::More;
3 use Test::MockModule;
5 use t::lib::Mocks;
7 use Module::Load::Conditional qw/check_install/;
9 BEGIN {
10     if ( check_install( module => 'Test::DBIx::Class' ) ) {
11         plan tests => 16;
12     } else {
13         plan skip_all => "Need Test::DBIx::Class"
14     }
17 use_ok('C4::Acquisition');
18 use_ok('C4::Context');
19 use_ok('Koha::Number::Price');
21 t::lib::Mocks::mock_preference( 'gist', '0.02|0.05|0.196' );
23 use Test::DBIx::Class {
24     schema_class => 'Koha::Schema',
25     connect_info => ['dbi:SQLite:dbname=:memory:','',''],
26     connect_opts => { name_sep => '.', quote_char => '`', },
27     fixture_class => '::Populate',
28 }, 'Currency' ;
30 my $db = Test::MockModule->new('Koha::Database');
31 $db->mock( _new_schema => sub { return Schema(); } );
33 fixtures_ok [
34     Currency => [
35         [ qw/ currency symbol rate active / ],
36         [[ 'my_cur', '€', 1, 1, ]],
37     ],
38     Aqbookseller => [
39         [ qw/ id name listincgst invoiceincgst / ],
40         [ 1, '0 0', 0, 0 ],
41         [ 2, '0 1', 0, 1 ],
42         [ 3, '1 0', 1, 0 ],
43         [ 4, '1 1', 1, 1 ],
44     ],
45 ], 'add currency fixtures';
47 my $bookseller_module = Test::MockModule->new('Koha::Acquisition::Bookseller');
49 my ( $basketno_0_0,  $basketno_1_1,  $basketno_1_0,  $basketno_0_1 );
50 my ( $invoiceid_0_0, $invoiceid_1_1, $invoiceid_1_0, $invoiceid_0_1 );
51 my $today;
53 for my $currency_format ( qw( US FR ) ) {
54     t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
55     subtest 'Configuration 1: 0 0' => sub {
56         plan tests => 8;
58         my $biblionumber_0_0 = 42;
60         my $order_0_0 = {
61             biblionumber     => $biblionumber_0_0,
62             quantity         => 2,
63             listprice        => 82.000000,
64             unitprice        => 73.80000,
65             quantityreceived => 2,
66             basketno         => $basketno_0_0,
67             invoiceid        => $invoiceid_0_0,
68             rrp              => 82.00,
69             ecost            => 73.80,
70             tax_rate         => 0.0500,
71             discount         => 10.0000,
72             datereceived     => $today
73         };
74         $order_0_0 = C4::Acquisition::populate_order_with_prices(
75             {
76                 order        => $order_0_0,
77                 booksellerid => 1,
78                 ordering     => 1,
79             }
80         );
82         compare(
83             {
84                 got      => $order_0_0->{rrp_tax_included},
85                 expected => 86.10,
86                 conf     => '0 0',
87                 field    => 'rrp_tax_included'
88             }
89         );
90         compare(
91             {
92                 got      => $order_0_0->{rrp_tax_excluded},
93                 expected => 82.00,
94                 conf     => '0 0',
95                 field    => 'rrp_tax_excluded'
96             }
97         );
98         compare(
99             {
100                 got      => $order_0_0->{ecost_tax_included},
101                 expected => 77.49,
102                 conf     => '0 0',
103                 field    => 'ecost_tax_included'
104             }
105         );
106         compare(
107             {
108                 got      => $order_0_0->{ecost_tax_excluded},
109                 expected => 73.80,
110                 conf     => '0 0',
111                 field    => 'ecost_tax_excluded'
112             }
113         );
114         compare(
115             {
116                 got      => $order_0_0->{tax_value_on_ordering},
117                 expected => 7.38,
118                 conf     => '0 0',
119                 field    => 'tax_value'
120             }
121         );
123         $order_0_0 = C4::Acquisition::populate_order_with_prices(
124             {
125                 order        => $order_0_0,
126                 booksellerid => 1,
127                 receiving    => 1,
128             }
129         );
131         compare(
132             {
133                 got      => $order_0_0->{unitprice_tax_included},
134                 expected => 77.49,
135                 conf     => '0 0',
136                 field    => 'unitprice_tax_included'
137             }
138         );
139         compare(
140             {
141                 got      => $order_0_0->{unitprice_tax_excluded},
142                 expected => 73.80,
143                 conf     => '0 0',
144                 field    => 'unitprice_tax_excluded'
145             }
146         );
147         compare(
148             {
149                 got      => $order_0_0->{tax_value_on_receiving},
150                 expected => 7.38,
151                 conf     => '0 0',
152                 field    => 'tax_value'
153             }
154         );
155     };
157     subtest 'Configuration 1: 1 1' => sub {
158         plan tests => 8;
160         my $biblionumber_1_1 = 43;
161         my $order_1_1        = {
162             biblionumber     => $biblionumber_1_1,
163             quantity         => 2,
164             listprice        => 82.000000,
165             unitprice        => 73.800000,
166             quantityreceived => 2,
167             basketno         => $basketno_1_1,
168             invoiceid        => $invoiceid_1_1,
169             rrp              => 82.00,
170             ecost            => 73.80,
171             tax_rate         => 0.0500,
172             discount         => 10.0000,
173             datereceived     => $today
174         };
176         $order_1_1 = C4::Acquisition::populate_order_with_prices(
177             {
178                 order        => $order_1_1,
179                 booksellerid => 4,
180                 ordering     => 1,
181             }
182         );
184         compare(
185             {
186                 got      => $order_1_1->{rrp_tax_included},
187                 expected => 82.00,
188                 conf     => '1 1',
189                 field    => 'rrp_tax_included'
190             }
191         );
192         compare(
193             {
194                 got      => $order_1_1->{rrp_tax_excluded},
195                 expected => 78.10,
196                 conf     => '1 1',
197                 field    => 'rrp_tax_excluded'
198             }
199         );
200         compare(
201             {
202                 got      => $order_1_1->{ecost_tax_included},
203                 expected => 73.80,
204                 conf     => '1 1',
205                 field    => 'ecost_tax_included'
206             }
207         );
208         compare(
209             {
210                 got      => $order_1_1->{ecost_tax_excluded},
211                 expected => 70.29,
212                 conf     => '1 1',
213                 field    => 'ecost_tax_excluded'
214             }
215         );
216         compare(
217             {
218                 got      => $order_1_1->{tax_value_on_ordering},
219                 expected => 7.03,
220                 conf     => '1 1',
221                 field    => 'tax_value'
222             }
223         );
225         $order_1_1 = C4::Acquisition::populate_order_with_prices(
226             {
227                 order        => $order_1_1,
228                 booksellerid => 4,
229                 receiving    => 1,
230             }
231         );
233         compare(
234             {
235                 got      => $order_1_1->{unitprice_tax_included},
236                 expected => 73.80,
237                 conf     => '1 1',
238                 field    => 'unitprice_tax_included'
239             }
240         );
241         compare(
242             {
243                 got      => $order_1_1->{unitprice_tax_excluded},
244                 expected => 70.29,
245                 conf     => '1 1',
246                 field    => 'unitprice_tax_excluded'
247             }
248         );
249         compare(
250             {
251                 got      => $order_1_1->{tax_value_on_receiving},
252                 expected => 7.03,
253                 conf     => '1 1',
254                 field    => 'tax_value'
255             }
256         );
257     };
259     subtest 'Configuration 1: 1 0' => sub {
260         plan tests => 8;
262         my $biblionumber_1_0 = 44;
263         my $order_1_0        = {
264             biblionumber     => $biblionumber_1_0,
265             quantity         => 2,
266             listprice        => 82.000000,
267             unitprice        => 70.290000,
268             quantityreceived => 2,
269             basketno         => $basketno_1_1,
270             invoiceid        => $invoiceid_1_1,
271             rrp              => 82.00,
272             ecost            => 73.80,
273             tax_rate         => 0.0500,
274             discount         => 10.0000,
275             datereceived     => $today
276         };
278         $order_1_0 = C4::Acquisition::populate_order_with_prices(
279             {
280                 order        => $order_1_0,
281                 booksellerid => 3,
282                 ordering     => 1,
283             }
284         );
286         compare(
287             {
288                 got      => $order_1_0->{rrp_tax_included},
289                 expected => 82,
290                 conf     => '1 0',
291                 field    => 'rrp_tax_included'
292             }
293         );
294         compare(
295             {
296                 got      => $order_1_0->{rrp_tax_excluded},
297                 expected => 78.10,
298                 conf     => '1 0',
299                 field    => 'rrp_tax_excluded'
300             }
301         );
302         compare(
303             {
304                 got      => $order_1_0->{ecost_tax_included},
305                 expected => 73.80,
306                 conf     => '1 0',
307                 field    => 'ecost_tax_included'
308             }
309         );
310         compare(
311             {
312                 got      => $order_1_0->{ecost_tax_excluded},
313                 expected => 70.29,
314                 conf     => '1 0',
315                 field    => 'ecost_tax_excluded'
316             }
317         );
318         compare(
319             {
320                 got      => $order_1_0->{tax_value_on_ordering},
321                 expected => 7.03,
322                 conf     => '1 0',
323                 field    => 'tax_value'
324             }
325         );
327         $order_1_0 = C4::Acquisition::populate_order_with_prices(
328             {
329                 order        => $order_1_0,
330                 booksellerid => 3,
331                 receiving    => 1,
332             }
333         );
335         compare(
336             {
337                 got      => $order_1_0->{unitprice_tax_included},
338                 expected => 73.80,
339                 conf     => '1 0',
340                 field    => 'unitprice_tax_included'
341             }
342         );
343         compare(
344             {
345                 got      => $order_1_0->{unitprice_tax_excluded},
346                 expected => 70.29,
347                 conf     => '1 0',
348                 field    => 'unitprice_tax_excluded'
349             }
350         );
351         compare(
352             {
353                 got      => $order_1_0->{tax_value_on_receiving},
354                 expected => 7.03,
355                 conf     => '1 0',
356                 field    => 'tax_value'
357             }
358         );
359     };
361     subtest 'Configuration 1: 0 1' => sub {
362         plan tests => 8;
364         my $biblionumber_0_1 = 45;
365         my $order_0_1        = {
366             biblionumber     => $biblionumber_0_1,
367             quantity         => 2,
368             listprice        => 82.000000,
369             unitprice        => 77.490000,
370             quantityreceived => 2,
371             basketno         => $basketno_1_1,
372             invoiceid        => $invoiceid_1_1,
373             rrp              => 82.00,
374             ecost            => 73.80,
375             tax_rate         => 0.0500,
376             discount         => 10.0000,
377             datereceived     => $today
378         };
380         $order_0_1 = C4::Acquisition::populate_order_with_prices(
381             {
382                 order        => $order_0_1,
383                 booksellerid => 2,
384                 ordering     => 1,
385             }
386         );
388         compare(
389             {
390                 got      => $order_0_1->{rrp_tax_included},
391                 expected => 86.10,
392                 conf     => '0 1',
393                 field    => 'rrp_tax_included'
394             }
395         );
396         compare(
397             {
398                 got      => $order_0_1->{rrp_tax_excluded},
399                 expected => 82.00,
400                 conf     => '0 1',
401                 field    => 'rrp_tax_excluded'
402             }
403         );
404         compare(
405             {
406                 got      => $order_0_1->{ecost_tax_included},
407                 expected => 77.49,
408                 conf     => '0 1',
409                 field    => 'ecost_tax_included'
410             }
411         );
412         compare(
413             {
414                 got      => $order_0_1->{ecost_tax_excluded},
415                 expected => 73.80,
416                 conf     => '0 1',
417                 field    => 'ecost_tax_excluded'
418             }
419         );
420         compare(
421             {
422                 got      => $order_0_1->{tax_value_on_ordering},
423                 expected => 7.38,
424                 conf     => '0 1',
425                 field    => 'tax_value'
426             }
427         );
429         $order_0_1 = C4::Acquisition::populate_order_with_prices(
430             {
431                 order        => $order_0_1,
432                 booksellerid => 2,
433                 receiving    => 1,
434             }
435         );
437         compare(
438             {
439                 got      => $order_0_1->{unitprice_tax_included},
440                 expected => 77.49,
441                 conf     => '0 1',
442                 field    => 'unitprice_tax_included'
443             }
444         );
445         compare(
446             {
447                 got      => $order_0_1->{unitprice_tax_excluded},
448                 expected => 73.80,
449                 conf     => '0 1',
450                 field    => 'unitprice_tax_excluded'
451             }
452         );
453         compare(
454             {
455                 got      => $order_0_1->{tax_value_on_receiving},
456                 expected => 7.38,
457                 conf     => '0 1',
458                 field    => 'tax_value'
459             }
460         );
461     };
464 sub compare {
465     my ($params) = @_;
466     is(
467         Koha::Number::Price->new( $params->{got} )->format,
468         Koha::Number::Price->new( $params->{expected} )->format,
469 "configuration $params->{conf}: $params->{field} should be correctly calculated"
470     );
473 # format_for_editing
474 for my $currency_format ( qw( US FR ) ) {
475     t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
476     is( Koha::Number::Price->new( 1234567 )->format_for_editing, '1234567.00', 'format_for_editing should return unformated integer part with 2 decimals' );
477     is( Koha::Number::Price->new( 1234567.89 )->format_for_editing, '1234567.89', 'format_for_editing should return unformated integer part with 2 decimals' );