Bug 13321: Rename variables
[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             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 => 'just_something',
78                 ordering     => 1,
79             }
80         );
81
82         # Note that this configuration is correct \o/
83         compare(
84             {
85                 got      => $order_0_0->{rrp_tax_included},
86                 expected => 86.10,
87                 conf     => '0 0',
88                 field    => 'rrp_tax_included'
89             }
90         );
91         compare(
92             {
93                 got      => $order_0_0->{rrp_tax_excluded},
94                 expected => 82.00,
95                 conf     => '0 0',
96                 field    => 'rrp_tax_excluded'
97             }
98         );
99         compare(
100             {
101                 got      => $order_0_0->{ecost_tax_included},
102                 expected => 77.49,
103                 conf     => '0 0',
104                 field    => 'ecost_tax_included'
105             }
106         );
107         compare(
108             {
109                 got      => $order_0_0->{ecost_tax_excluded},
110                 expected => 73.80,
111                 conf     => '0 0',
112                 field    => 'ecost_tax_excluded'
113             }
114         );
115         compare(
116             {
117                 got      => $order_0_0->{tax_value},
118                 expected => 7.38,
119                 conf     => '0 0',
120                 field    => 'tax_value'
121             }
122         );
123         compare(
124             {
125                 got      => $order_0_0->{total_tax_included},
126                 expected => 154.98,
127                 conf     => '0 0',
128                 field    => 'total_tax_included'
129             }
130         );
131         compare(
132             {
133                 got      => $order_0_0->{total_tax_excluded},
134                 expected => 147.60,
135                 conf     => '0 0',
136                 field    => 'total_tax_excluded'
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->{unitprice_tax_included},
152                 expected => 77.49,
153                 conf     => '0 0',
154                 field    => 'unitprice_tax_included'
155             }
156         );
157         compare(
158             {
159                 got      => $order_0_0->{unitprice_tax_excluded},
160                 expected => 73.80,
161                 conf     => '0 0',
162                 field    => 'unitprice_tax_excluded'
163             }
164         );
165         compare(
166             {
167                 got      => $order_0_0->{tax_value},
168                 expected => 7.38,
169                 conf     => '0 0',
170                 field    => 'tax_value'
171             }
172         );
173         compare(
174             {
175                 got      => $order_0_0->{total_tax_included},
176                 expected => 154.98,
177                 conf     => '0 0',
178                 field    => 'total_tax_included'
179             }
180         );
181         compare(
182             {
183                 got      => $order_0_0->{total_tax_excluded},
184                 expected => 147.60,
185                 conf     => '0 0',
186                 field    => 'total_tax_excluded'
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             tax_rate         => 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         # tax_value should be 7.03 instead of 7.02
226         compare(
227             {
228                 got      => $order_1_1->{rrp_tax_included},
229                 expected => 82.00,
230                 conf     => '1 1',
231                 field    => 'rrp_tax_included'
232             }
233         );
234         compare(
235             {
236                 got      => $order_1_1->{rrp_tax_excluded},
237                 expected => 78.10,
238                 conf     => '1 1',
239                 field    => 'rrp_tax_excluded'
240             }
241         );
242         compare(
243             {
244                 got      => $order_1_1->{ecost_tax_included},
245                 expected => 73.80,
246                 conf     => '1 1',
247                 field    => 'ecost_tax_included'
248             }
249         );
250         compare(
251             {
252                 got      => $order_1_1->{ecost_tax_excluded},
253                 expected => 70.29,
254                 conf     => '1 1',
255                 field    => 'ecost_tax_excluded'
256             }
257         );
258         compare(
259             {
260                 got      => $order_1_1->{tax_value},
261                 expected => 7.02,
262                 conf     => '1 1',
263                 field    => 'tax_value'
264             }
265         );
266         compare(
267             {
268                 got      => $order_1_1->{total_tax_included},
269                 expected => 147.60,
270                 conf     => '1 1',
271                 field    => 'total_tax_included'
272             }
273         );
274         compare(
275             {
276                 got      => $order_1_1->{total_tax_excluded},
277                 expected => 140.58,
278                 conf     => '1 1',
279                 field    => 'total_tax_excluded'
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         # tax_value should be 7.03
292         compare(
293             {
294                 got      => $order_1_1->{unitprice_tax_included},
295                 expected => 73.80,
296                 conf     => '1 1',
297                 field    => 'unitprice_tax_included'
298             }
299         );
300         compare(
301             {
302                 got      => $order_1_1->{unitprice_tax_excluded},
303                 expected => 70.29,
304                 conf     => '1 1',
305                 field    => 'unitprice_tax_excluded'
306             }
307         );
308         compare(
309             {
310                 got      => $order_1_1->{tax_value},
311                 expected => 7.02,
312                 conf     => '1 1',
313                 field    => 'tax_value'
314             }
315         );
316         compare(
317             {
318                 got      => $order_1_1->{total_tax_included},
319                 expected => 147.60,
320                 conf     => '1 1',
321                 field    => 'total_tax_included'
322             }
323         );
324         compare(
325             {
326                 got      => $order_1_1->{total_tax_excluded},
327                 expected => 140.58,
328                 conf     => '1 1',
329                 field    => 'total_tax_excluded'
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             tax_rate         => 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_tax_included should be 82 (what we inserted!)
369         # tax_value should be 7.03 instead of 7.02
370
371         compare(
372             {
373                 got      => $order_1_0->{rrp_tax_included},
374                 expected => 82.01,
375                 conf     => '1 0',
376                 field    => 'rrp_tax_included'
377             }
378         );
379         compare(
380             {
381                 got      => $order_1_0->{rrp_tax_excluded},
382                 expected => 78.10,
383                 conf     => '1 0',
384                 field    => 'rrp_tax_excluded'
385             }
386         );
387         compare(
388             {
389                 got      => $order_1_0->{ecost_tax_included},
390                 expected => 73.80,
391                 conf     => '1 0',
392                 field    => 'ecost_tax_included'
393             }
394         );
395         compare(
396             {
397                 got      => $order_1_0->{ecost_tax_excluded},
398                 expected => 70.29,
399                 conf     => '1 0',
400                 field    => 'ecost_tax_excluded'
401             }
402         );
403         compare(
404             {
405                 got      => $order_1_0->{tax_value},
406                 expected => 7.02,
407                 conf     => '1 0',
408                 field    => 'tax_value'
409             }
410         );
411         compare(
412             {
413                 got      => $order_1_0->{total_tax_included},
414                 expected => 147.60,
415                 conf     => '1 0',
416                 field    => 'total_tax_included'
417             }
418         );
419         compare(
420             {
421                 got      => $order_1_0->{total_tax_excluded},
422                 expected => 140.58,
423                 conf     => '1 0',
424                 field    => 'total_tax_excluded'
425             }
426         );
427
428         $order_1_0 = C4::Acquisition::populate_order_with_prices(
429             {
430                 order        => $order_1_0,
431                 booksellerid => 'just_something',
432                 receiving    => 1,
433             }
434         );
435         # Note that this configuration is *not* correct!
436         # gstvalue should be 7.03
437         compare(
438             {
439                 got      => $order_1_0->{unitprice_tax_included},
440                 expected => 73.80,
441                 conf     => '1 0',
442                 field    => 'unitprice_tax_included'
443             }
444         );
445         compare(
446             {
447                 got      => $order_1_0->{unitprice_tax_excluded},
448                 expected => 70.29,
449                 conf     => '1 0',
450                 field    => 'unitprice_tax_excluded'
451             }
452         );
453         compare(
454             {
455                 got      => $order_1_0->{tax_value},
456                 expected => 7.02,
457                 conf     => '1 0',
458                 field    => 'tax_value'
459             }
460         );
461         compare(
462             {
463                 got      => $order_1_0->{total_tax_included},
464                 expected => 147.60,
465                 conf     => '1 0',
466                 field    => 'total_tax_included'
467             }
468         );
469         compare(
470             {
471                 got      => $order_1_0->{total_tax_excluded},
472                 expected => 140.58,
473                 conf     => '1 0',
474                 field    => 'total_tax_excluded'
475             }
476         );
477     };
478
479     subtest 'Configuration 1: 0 1' => sub {
480         plan tests => 12;
481         $bookseller_module->mock(
482             'fetch',
483             sub {
484                 return { listincgst => 0, invoiceincgst => 1 };
485             }
486         );
487
488         my $biblionumber_0_1 = 45;
489         my $order_0_1        = {
490             biblionumber     => $biblionumber_0_1,
491             quantity         => 2,
492             listprice        => 82.000000,
493             unitprice        => 73.800000,
494             quantityreceived => 2,
495             basketno         => $basketno_1_1,
496             invoiceid        => $invoiceid_1_1,
497             rrp              => 82.00,
498             ecost            => 73.80,
499             tax_rate         => 0.0500,
500             discount         => 10.0000,
501             datereceived     => $today
502         };
503
504         $order_0_1 = C4::Acquisition::populate_order_with_prices(
505             {
506                 order        => $order_0_1,
507                 booksellerid => 'just_something',
508                 ordering     => 1,
509             }
510         );
511
512         # Note that this configuration is correct \o/
513         compare(
514             {
515                 got      => $order_0_1->{rrp_tax_included},
516                 expected => 86.10,
517                 conf     => '1 0',
518                 field    => 'rrp_tax_included'
519             }
520         );
521         compare(
522             {
523                 got      => $order_0_1->{rrp_tax_excluded},
524                 expected => 82.00,
525                 conf     => '1 0',
526                 field    => 'rrp_tax_excluded'
527             }
528         );
529         compare(
530             {
531                 got      => $order_0_1->{ecost_tax_included},
532                 expected => 77.49,
533                 conf     => '1 0',
534                 field    => 'ecost_tax_included'
535             }
536         );
537         compare(
538             {
539                 got      => $order_0_1->{ecost_tax_excluded},
540                 expected => 73.80,
541                 conf     => '1 0',
542                 field    => 'ecost_tax_excluded'
543             }
544         );
545         compare(
546             {
547                 got      => $order_0_1->{tax_value},
548                 expected => 7.38,
549                 conf     => '1 0',
550                 field    => 'tax_value'
551             }
552         );
553         compare(
554             {
555                 got      => $order_0_1->{total_tax_included},
556                 expected => 154.98,
557                 conf     => '1 0',
558                 field    => 'total_tax_included'
559             }
560         );
561         compare(
562             {
563                 got      => $order_0_1->{total_tax_excluded},
564                 expected => 147.60,
565                 conf     => '1 0',
566                 field    => 'total_tax_excluded'
567             }
568         );
569
570         $order_0_1 = C4::Acquisition::populate_order_with_prices(
571             {
572                 order        => $order_0_1,
573                 booksellerid => 'just_something',
574                 receiving    => 1,
575             }
576         );
577         # Note that this configuration is correct
578         compare(
579             {
580                 got      => $order_0_1->{unitprice_tax_included},
581                 expected => 77.49,
582                 conf     => '0 1',
583                 field    => 'unitprice_tax_included'
584             }
585         );
586         compare(
587             {
588                 got      => $order_0_1->{unitprice_tax_excluded},
589                 expected => 73.80,
590                 conf     => '0 1',
591                 field    => 'unitprice_tax_excluded'
592             }
593         );
594         compare(
595             {
596                 got      => $order_0_1->{tax_value},
597                 expected => 7.38,
598                 conf     => '0 1',
599                 field    => 'tax_value'
600             }
601         );
602         compare(
603             {
604                 got      => $order_0_1->{total_tax_included},
605                 expected => 154.98,
606                 conf     => '0 1',
607                 field    => 'total_tax_included'
608             }
609         );
610         compare(
611             {
612                 got      => $order_0_1->{total_tax_excluded},
613                 expected => 147.60,
614                 conf     => '0 1',
615                 field    => 'total_tax_excluded'
616             }
617         );
618     };
619 }
620
621 sub compare {
622     my ($params) = @_;
623     is(
624         Koha::Number::Price->new( $params->{got} )->format,
625         Koha::Number::Price->new( $params->{expected} )->format,
626 "configuration $params->{conf}: $params->{field} should be correctly calculated"
627     );
628 }
629
630 # format_for_editing
631 for my $currency_format ( qw( US FR ) ) {
632     t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
633     is( Koha::Number::Price->new( 1234567 )->format_for_editing, '1234567.00', 'format_for_editing should return unformated integer part with 2 decimals' );
634     is( Koha::Number::Price->new( 1234567.89 )->format_for_editing, '1234567.89', 'format_for_editing should return unformated integer part with 2 decimals' );
635 }