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