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