Bug 14362: PEGI15 Circulation/AgeRestrictionMarkers test fails
[koha.git] / t / Prices.t
1 use Modern::Perl;
2 use Test::More tests => 17;
3 use Test::MockModule;
4
5 use t::lib::Mocks;
6
7 BEGIN {
8     use_ok('C4::Acquisition');
9     use_ok('C4::Bookseller');
10     use_ok('C4::Context');
11     use_ok('Koha::Number::Price');
12 };
13
14 t::lib::Mocks::mock_preference( 'gist', '0.02|0.05|0.196' );
15
16 use Test::DBIx::Class {
17     schema_class => 'Koha::Schema',
18     connect_info => ['dbi:SQLite:dbname=:memory:','',''],
19     connect_opts => { name_sep => '.', quote_char => '`', },
20     fixture_class => '::Populate',
21 }, 'Currency' ;
22
23 my $db = Test::MockModule->new('Koha::Database');
24 $db->mock( _new_schema => sub { return Schema(); } );
25
26 fixtures_ok [
27     Currency => [
28         [ qw/ currency symbol rate active / ],
29         [[ 'my_cur', '€', 1, 1, ]],
30     ],
31 ], 'add currency fixtures';
32
33 my $bookseller_module = Test::MockModule->new('Koha::Acquisition::Bookseller');
34
35 my ( $basketno_0_0,  $basketno_1_1,  $basketno_1_0,  $basketno_0_1 );
36 my ( $invoiceid_0_0, $invoiceid_1_1, $invoiceid_1_0, $invoiceid_0_1 );
37 my $today;
38
39 for my $currency_format ( qw( US FR ) ) {
40     t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
41     subtest 'Configuration 1: 0 0' => sub {
42         plan tests => 12;
43         $bookseller_module->mock(
44             'fetch',
45             sub {
46                 return { listincgst => 0, invoiceincgst => 0 };
47             }
48         );
49
50         my $biblionumber_0_0 = 42;
51
52         my $order_0_0 = {
53             biblionumber     => $biblionumber_0_0,
54             quantity         => 2,
55             listprice        => 82.000000,
56             unitprice        => 73.80000,
57             quantityreceived => 2,
58             basketno         => $basketno_0_0,
59             invoiceid        => $invoiceid_0_0,
60             rrp              => 82.00,
61             ecost            => 73.80,
62             gstrate          => 0.0500,
63             discount         => 10.0000,
64             datereceived     => $today
65         };
66         $order_0_0 = C4::Acquisition::populate_order_with_prices(
67             {
68                 order        => $order_0_0,
69                 booksellerid => 'just_something',
70                 ordering     => 1,
71             }
72         );
73
74         # Note that this configuration is correct \o/
75         compare(
76             {
77                 got      => $order_0_0->{rrpgsti},
78                 expected => 86.10,
79                 conf     => '0 0',
80                 field    => 'rrpgsti'
81             }
82         );
83         compare(
84             {
85                 got      => $order_0_0->{rrpgste},
86                 expected => 82.00,
87                 conf     => '0 0',
88                 field    => 'rrpgste'
89             }
90         );
91         compare(
92             {
93                 got      => $order_0_0->{ecostgsti},
94                 expected => 77.49,
95                 conf     => '0 0',
96                 field    => 'ecostgsti'
97             }
98         );
99         compare(
100             {
101                 got      => $order_0_0->{ecostgste},
102                 expected => 73.80,
103                 conf     => '0 0',
104                 field    => 'ecostgste'
105             }
106         );
107         compare(
108             {
109                 got      => $order_0_0->{gstvalue},
110                 expected => 7.38,
111                 conf     => '0 0',
112                 field    => 'gstvalue'
113             }
114         );
115         compare(
116             {
117                 got      => $order_0_0->{totalgsti},
118                 expected => 154.98,
119                 conf     => '0 0',
120                 field    => 'totalgsti'
121             }
122         );
123         compare(
124             {
125                 got      => $order_0_0->{totalgste},
126                 expected => 147.60,
127                 conf     => '0 0',
128                 field    => 'totalgste'
129             }
130         );
131
132         $order_0_0 = C4::Acquisition::populate_order_with_prices(
133             {
134                 order        => $order_0_0,
135                 booksellerid => 'just_something',
136                 receiving    => 1,
137             }
138         );
139
140         # Note that this configuration is correct \o/
141         compare(
142             {
143                 got      => $order_0_0->{unitpricegsti},
144                 expected => 77.49,
145                 conf     => '0 0',
146                 field    => 'unitpricegsti'
147             }
148         );
149         compare(
150             {
151                 got      => $order_0_0->{unitpricegste},
152                 expected => 73.80,
153                 conf     => '0 0',
154                 field    => 'unitpricegste'
155             }
156         );
157         compare(
158             {
159                 got      => $order_0_0->{gstvalue},
160                 expected => 7.38,
161                 conf     => '0 0',
162                 field    => 'gstvalue'
163             }
164         );
165         compare(
166             {
167                 got      => $order_0_0->{totalgsti},
168                 expected => 154.98,
169                 conf     => '0 0',
170                 field    => 'totalgsti'
171             }
172         );
173         compare(
174             {
175                 got      => $order_0_0->{totalgste},
176                 expected => 147.60,
177                 conf     => '0 0',
178                 field    => 'totalgste'
179             }
180         );
181     };
182
183     subtest 'Configuration 1: 1 1' => sub {
184         plan tests => 12;
185         $bookseller_module->mock(
186             'fetch',
187             sub {
188                 return { listincgst => 1, invoiceincgst => 1 };
189             }
190         );
191
192         my $biblionumber_1_1 = 43;
193         my $order_1_1        = {
194             biblionumber     => $biblionumber_1_1,
195             quantity         => 2,
196             listprice        => 82.000000,
197             unitprice        => 73.800000,
198             quantityreceived => 2,
199             basketno         => $basketno_1_1,
200             invoiceid        => $invoiceid_1_1,
201             rrp              => 82.00,
202             ecost            => 73.80,
203             gstrate          => 0.0500,
204             discount         => 10.0000,
205             datereceived     => $today
206         };
207
208         $order_1_1 = C4::Acquisition::populate_order_with_prices(
209             {
210                 order        => $order_1_1,
211                 booksellerid => 'just_something',
212                 ordering     => 1,
213             }
214         );
215
216         # Note that this configuration is *not* correct
217         # gstvalue should be 7.03 instead of 7.02
218         compare(
219             {
220                 got      => $order_1_1->{rrpgsti},
221                 expected => 82.00,
222                 conf     => '1 1',
223                 field    => 'rrpgsti'
224             }
225         );
226         compare(
227             {
228                 got      => $order_1_1->{rrpgste},
229                 expected => 78.10,
230                 conf     => '1 1',
231                 field    => 'rrpgste'
232             }
233         );
234         compare(
235             {
236                 got      => $order_1_1->{ecostgsti},
237                 expected => 73.80,
238                 conf     => '1 1',
239                 field    => 'ecostgsti'
240             }
241         );
242         compare(
243             {
244                 got      => $order_1_1->{ecostgste},
245                 expected => 70.29,
246                 conf     => '1 1',
247                 field    => 'ecostgste'
248             }
249         );
250         compare(
251             {
252                 got      => $order_1_1->{gstvalue},
253                 expected => 7.02,
254                 conf     => '1 1',
255                 field    => 'gstvalue'
256             }
257         );
258         compare(
259             {
260                 got      => $order_1_1->{totalgsti},
261                 expected => 147.60,
262                 conf     => '1 1',
263                 field    => 'totalgsti'
264             }
265         );
266         compare(
267             {
268                 got      => $order_1_1->{totalgste},
269                 expected => 140.58,
270                 conf     => '1 1',
271                 field    => 'totalgste'
272             }
273         );
274
275         $order_1_1 = C4::Acquisition::populate_order_with_prices(
276             {
277                 order        => $order_1_1,
278                 booksellerid => 'just_something',
279                 receiving    => 1,
280             }
281         );
282         # Note that this configuration is *not* correct!
283         # gstvalue should be 7.03
284         compare(
285             {
286                 got      => $order_1_1->{unitpricegsti},
287                 expected => 73.80,
288                 conf     => '1 1',
289                 field    => 'unitpricegsti'
290             }
291         );
292         compare(
293             {
294                 got      => $order_1_1->{unitpricegste},
295                 expected => 70.29,
296                 conf     => '1 1',
297                 field    => 'unitpricegste'
298             }
299         );
300         compare(
301             {
302                 got      => $order_1_1->{gstvalue},
303                 expected => 7.02,
304                 conf     => '1 1',
305                 field    => 'gstvalue'
306             }
307         );
308         compare(
309             {
310                 got      => $order_1_1->{totalgsti},
311                 expected => 147.60,
312                 conf     => '1 1',
313                 field    => 'totalgsti'
314             }
315         );
316         compare(
317             {
318                 got      => $order_1_1->{totalgste},
319                 expected => 140.58,
320                 conf     => '1 1',
321                 field    => 'totalgste'
322             }
323         );
324     };
325
326     subtest 'Configuration 1: 1 0' => sub {
327         plan tests => 12;
328         $bookseller_module->mock(
329             'fetch',
330             sub {
331                 return { listincgst => 1, invoiceincgst => 0 };
332             }
333         );
334
335         my $biblionumber_1_0 = 44;
336         my $order_1_0        = {
337             biblionumber     => $biblionumber_1_0,
338             quantity         => 2,
339             listprice        => 82.000000,
340             unitprice        => 73.804500,
341             quantityreceived => 2,
342             basketno         => $basketno_1_1,
343             invoiceid        => $invoiceid_1_1,
344             rrp              => 82.01,
345             ecost            => 73.80,
346             gstrate          => 0.0500,
347             discount         => 10.0000,
348             datereceived     => $today
349         };
350
351         $order_1_0 = C4::Acquisition::populate_order_with_prices(
352             {
353                 order        => $order_1_0,
354                 booksellerid => 'just_something',
355                 ordering     => 1,
356             }
357         );
358
359         # Note that this configuration is *not* correct!
360         # rrp gsti should be 82 (what we inserted!)
361         # => Actually we need to fix the inserted value (here we have 82.01 in DB)
362         # gstvalue should be 7.03 instead of 7.02
363
364         compare(
365             {
366                 got      => $order_1_0->{rrpgsti},
367                 expected => 82.01,
368                 conf     => '1 0',
369                 field    => 'rrpgsti'
370             }
371         );
372         compare(
373             {
374                 got      => $order_1_0->{rrpgste},
375                 expected => 78.10,
376                 conf     => '1 0',
377                 field    => 'rrpgste'
378             }
379         );
380         compare(
381             {
382                 got      => $order_1_0->{ecostgsti},
383                 expected => 73.80,
384                 conf     => '1 0',
385                 field    => 'ecostgsti'
386             }
387         );
388         compare(
389             {
390                 got      => $order_1_0->{ecostgste},
391                 expected => 70.29,
392                 conf     => '1 0',
393                 field    => 'ecostgste'
394             }
395         );
396         compare(
397             {
398                 got      => $order_1_0->{gstvalue},
399                 expected => 7.02,
400                 conf     => '1 0',
401                 field    => 'gstvalue'
402             }
403         );
404         compare(
405             {
406                 got      => $order_1_0->{totalgsti},
407                 expected => 147.60,
408                 conf     => '1 0',
409                 field    => 'totalgsti'
410             }
411         );
412         compare(
413             {
414                 got      => $order_1_0->{totalgste},
415                 expected => 140.58,
416                 conf     => '1 0',
417                 field    => 'totalgste'
418             }
419         );
420
421         $order_1_0 = C4::Acquisition::populate_order_with_prices(
422             {
423                 order        => $order_1_0,
424                 booksellerid => 'just_something',
425                 receiving    => 1,
426             }
427         );
428         # Note that this configuration is *not* correct!
429         # gstvalue should be 7.03
430         compare(
431             {
432                 got      => $order_1_0->{unitpricegsti},
433                 expected => 73.80,
434                 conf     => '1 0',
435                 field    => 'unitpricegsti'
436             }
437         );
438         compare(
439             {
440                 got      => $order_1_0->{unitpricegste},
441                 expected => 70.29,
442                 conf     => '1 0',
443                 field    => 'unitpricegste'
444             }
445         );
446         compare(
447             {
448                 got      => $order_1_0->{gstvalue},
449                 expected => 7.02,
450                 conf     => '1 0',
451                 field    => 'gstvalue'
452             }
453         );
454         compare(
455             {
456                 got      => $order_1_0->{totalgsti},
457                 expected => 147.60,
458                 conf     => '1 0',
459                 field    => 'totalgsti'
460             }
461         );
462         compare(
463             {
464                 got      => $order_1_0->{totalgste},
465                 expected => 140.58,
466                 conf     => '1 0',
467                 field    => 'totalgste'
468             }
469         );
470     };
471
472     subtest 'Configuration 1: 0 1' => sub {
473         plan tests => 12;
474         $bookseller_module->mock(
475             'fetch',
476             sub {
477                 return { listincgst => 0, invoiceincgst => 1 };
478             }
479         );
480
481         my $biblionumber_0_1 = 45;
482         my $order_0_1        = {
483             biblionumber     => $biblionumber_0_1,
484             quantity         => 2,
485             listprice        => 82.000000,
486             unitprice        => 73.800000,
487             quantityreceived => 2,
488             basketno         => $basketno_1_1,
489             invoiceid        => $invoiceid_1_1,
490             rrp              => 82.00,
491             ecost            => 73.80,
492             gstrate          => 0.0500,
493             discount         => 10.0000,
494             datereceived     => $today
495         };
496
497         $order_0_1 = C4::Acquisition::populate_order_with_prices(
498             {
499                 order        => $order_0_1,
500                 booksellerid => 'just_something',
501                 ordering     => 1,
502             }
503         );
504
505         # Note that this configuration is correct \o/
506         compare(
507             {
508                 got      => $order_0_1->{rrpgsti},
509                 expected => 86.10,
510                 conf     => '1 0',
511                 field    => 'rrpgsti'
512             }
513         );
514         compare(
515             {
516                 got      => $order_0_1->{rrpgste},
517                 expected => 82.00,
518                 conf     => '1 0',
519                 field    => 'rrpgste'
520             }
521         );
522         compare(
523             {
524                 got      => $order_0_1->{ecostgsti},
525                 expected => 77.49,
526                 conf     => '1 0',
527                 field    => 'ecostgsti'
528             }
529         );
530         compare(
531             {
532                 got      => $order_0_1->{ecostgste},
533                 expected => 73.80,
534                 conf     => '1 0',
535                 field    => 'ecostgste'
536             }
537         );
538         compare(
539             {
540                 got      => $order_0_1->{gstvalue},
541                 expected => 7.38,
542                 conf     => '1 0',
543                 field    => 'gstvalue'
544             }
545         );
546         compare(
547             {
548                 got      => $order_0_1->{totalgsti},
549                 expected => 154.98,
550                 conf     => '1 0',
551                 field    => 'totalgsti'
552             }
553         );
554         compare(
555             {
556                 got      => $order_0_1->{totalgste},
557                 expected => 147.60,
558                 conf     => '1 0',
559                 field    => 'totalgste'
560             }
561         );
562
563         $order_0_1 = C4::Acquisition::populate_order_with_prices(
564             {
565                 order        => $order_0_1,
566                 booksellerid => 'just_something',
567                 receiving    => 1,
568             }
569         );
570         # Note that this configuration is correct
571         compare(
572             {
573                 got      => $order_0_1->{unitpricegsti},
574                 expected => 77.49,
575                 conf     => '0 1',
576                 field    => 'unitpricegsti'
577             }
578         );
579         compare(
580             {
581                 got      => $order_0_1->{unitpricegste},
582                 expected => 73.80,
583                 conf     => '0 1',
584                 field    => 'unitpricegste'
585             }
586         );
587         compare(
588             {
589                 got      => $order_0_1->{gstvalue},
590                 expected => 7.38,
591                 conf     => '0 1',
592                 field    => 'gstvalue'
593             }
594         );
595         compare(
596             {
597                 got      => $order_0_1->{totalgsti},
598                 expected => 154.98,
599                 conf     => '0 1',
600                 field    => 'totalgsti'
601             }
602         );
603         compare(
604             {
605                 got      => $order_0_1->{totalgste},
606                 expected => 147.60,
607                 conf     => '0 1',
608                 field    => 'totalgste'
609             }
610         );
611     };
612 }
613
614 sub compare {
615     my ($params) = @_;
616     is(
617         Koha::Number::Price->new( $params->{got} )->format,
618         Koha::Number::Price->new( $params->{expected} )->format,
619 "configuration $params->{conf}: $params->{field} should be correctly calculated"
620     );
621 }
622
623 # format_for_editing
624 for my $currency_format ( qw( US FR ) ) {
625     t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
626     is( Koha::Number::Price->new( 1234567 )->format_for_editing, '1234567.00', 'format_for_editing should return unformated integer part with 2 decimals' );
627     is( Koha::Number::Price->new( 1234567.89 )->format_for_editing, '1234567.89', 'format_for_editing should return unformated integer part with 2 decimals' );
628 }