Bug 14893: Separate temporary storage per instance in Upload.pm
[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
124         # Note that this configuration is correct \o/
125         compare(
126             {
127                 got      => $order_0_0->{unitpricegsti},
128                 expected => 77.49,
129                 conf     => '0 0',
130                 field    => 'unitpricegsti'
131             }
132         );
133         compare(
134             {
135                 got      => $order_0_0->{unitpricegste},
136                 expected => 73.80,
137                 conf     => '0 0',
138                 field    => 'unitpricegste'
139             }
140         );
141         compare(
142             {
143                 got      => $order_0_0->{gstvalue},
144                 expected => 7.38,
145                 conf     => '0 0',
146                 field    => 'gstvalue'
147             }
148         );
149         compare(
150             {
151                 got      => $order_0_0->{totalgsti},
152                 expected => 154.98,
153                 conf     => '0 0',
154                 field    => 'totalgsti'
155             }
156         );
157         compare(
158             {
159                 got      => $order_0_0->{totalgste},
160                 expected => 147.60,
161                 conf     => '0 0',
162                 field    => 'totalgste'
163             }
164         );
165     };
166
167     subtest 'Configuration 1: 1 1' => sub {
168         plan tests => 12;
169         $bookseller_module->mock(
170             'fetch',
171             sub {
172                 return { listincgst => 1, invoiceincgst => 1 };
173             }
174         );
175
176         my $biblionumber_1_1 = 43;
177         my $order_1_1        = {
178             biblionumber     => $biblionumber_1_1,
179             quantity         => 2,
180             listprice        => 82.000000,
181             unitprice        => 73.800000,
182             quantityreceived => 2,
183             basketno         => $basketno_1_1,
184             invoiceid        => $invoiceid_1_1,
185             rrp              => 82.00,
186             ecost            => 73.80,
187             gstrate          => 0.0500,
188             discount         => 10.0000,
189             datereceived     => $today
190         };
191
192         $order_1_1 = C4::Acquisition::populate_order_with_prices(
193             {
194                 order        => $order_1_1,
195                 booksellerid => 'just_something',
196                 ordering     => 1,
197             }
198         );
199
200         # Note that this configuration is *not* correct
201         # gstvalue should be 7.03 instead of 7.02
202         compare(
203             {
204                 got      => $order_1_1->{rrpgsti},
205                 expected => 82.00,
206                 conf     => '1 1',
207                 field    => 'rrpgsti'
208             }
209         );
210         compare(
211             {
212                 got      => $order_1_1->{rrpgste},
213                 expected => 78.10,
214                 conf     => '1 1',
215                 field    => 'rrpgste'
216             }
217         );
218         compare(
219             {
220                 got      => $order_1_1->{ecostgsti},
221                 expected => 73.80,
222                 conf     => '1 1',
223                 field    => 'ecostgsti'
224             }
225         );
226         compare(
227             {
228                 got      => $order_1_1->{ecostgste},
229                 expected => 70.29,
230                 conf     => '1 1',
231                 field    => 'ecostgste'
232             }
233         );
234         compare(
235             {
236                 got      => $order_1_1->{gstvalue},
237                 expected => 7.02,
238                 conf     => '1 1',
239                 field    => 'gstvalue'
240             }
241         );
242         compare(
243             {
244                 got      => $order_1_1->{totalgsti},
245                 expected => 147.60,
246                 conf     => '1 1',
247                 field    => 'totalgsti'
248             }
249         );
250         compare(
251             {
252                 got      => $order_1_1->{totalgste},
253                 expected => 140.58,
254                 conf     => '1 1',
255                 field    => 'totalgste'
256             }
257         );
258
259         $order_1_1 = C4::Acquisition::populate_order_with_prices(
260             {
261                 order        => $order_1_1,
262                 booksellerid => 'just_something',
263                 receiving    => 1,
264             }
265         );
266         # Note that this configuration is *not* correct!
267         # gstvalue should be 7.03
268         compare(
269             {
270                 got      => $order_1_1->{unitpricegsti},
271                 expected => 73.80,
272                 conf     => '1 1',
273                 field    => 'unitpricegsti'
274             }
275         );
276         compare(
277             {
278                 got      => $order_1_1->{unitpricegste},
279                 expected => 70.29,
280                 conf     => '1 1',
281                 field    => 'unitpricegste'
282             }
283         );
284         compare(
285             {
286                 got      => $order_1_1->{gstvalue},
287                 expected => 7.02,
288                 conf     => '1 1',
289                 field    => 'gstvalue'
290             }
291         );
292         compare(
293             {
294                 got      => $order_1_1->{totalgsti},
295                 expected => 147.60,
296                 conf     => '1 1',
297                 field    => 'totalgsti'
298             }
299         );
300         compare(
301             {
302                 got      => $order_1_1->{totalgste},
303                 expected => 140.58,
304                 conf     => '1 1',
305                 field    => 'totalgste'
306             }
307         );
308     };
309
310     subtest 'Configuration 1: 1 0' => sub {
311         plan tests => 12;
312         $bookseller_module->mock(
313             'fetch',
314             sub {
315                 return { listincgst => 1, invoiceincgst => 0 };
316             }
317         );
318
319         my $biblionumber_1_0 = 44;
320         my $order_1_0        = {
321             biblionumber     => $biblionumber_1_0,
322             quantity         => 2,
323             listprice        => 82.000000,
324             unitprice        => 73.804500,
325             quantityreceived => 2,
326             basketno         => $basketno_1_1,
327             invoiceid        => $invoiceid_1_1,
328             rrp              => 82.01,
329             ecost            => 73.80,
330             gstrate          => 0.0500,
331             discount         => 10.0000,
332             datereceived     => $today
333         };
334
335         $order_1_0 = C4::Acquisition::populate_order_with_prices(
336             {
337                 order        => $order_1_0,
338                 booksellerid => 'just_something',
339                 ordering     => 1,
340             }
341         );
342
343         # Note that this configuration is *not* correct!
344         # rrp gsti should be 82 (what we inserted!)
345         # => Actually we need to fix the inserted value (here we have 82.01 in DB)
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         # gstvalue should be 7.03
414         compare(
415             {
416                 got      => $order_1_0->{unitpricegsti},
417                 expected => 73.80,
418                 conf     => '1 0',
419                 field    => 'unitpricegsti'
420             }
421         );
422         compare(
423             {
424                 got      => $order_1_0->{unitpricegste},
425                 expected => 70.29,
426                 conf     => '1 0',
427                 field    => 'unitpricegste'
428             }
429         );
430         compare(
431             {
432                 got      => $order_1_0->{gstvalue},
433                 expected => 7.02,
434                 conf     => '1 0',
435                 field    => 'gstvalue'
436             }
437         );
438         compare(
439             {
440                 got      => $order_1_0->{totalgsti},
441                 expected => 147.60,
442                 conf     => '1 0',
443                 field    => 'totalgsti'
444             }
445         );
446         compare(
447             {
448                 got      => $order_1_0->{totalgste},
449                 expected => 140.58,
450                 conf     => '1 0',
451                 field    => 'totalgste'
452             }
453         );
454     };
455
456     subtest 'Configuration 1: 0 1' => sub {
457         plan tests => 12;
458         $bookseller_module->mock(
459             'fetch',
460             sub {
461                 return { listincgst => 0, invoiceincgst => 1 };
462             }
463         );
464
465         my $biblionumber_0_1 = 45;
466         my $order_0_1        = {
467             biblionumber     => $biblionumber_0_1,
468             quantity         => 2,
469             listprice        => 82.000000,
470             unitprice        => 73.800000,
471             quantityreceived => 2,
472             basketno         => $basketno_1_1,
473             invoiceid        => $invoiceid_1_1,
474             rrp              => 82.00,
475             ecost            => 73.80,
476             gstrate          => 0.0500,
477             discount         => 10.0000,
478             datereceived     => $today
479         };
480
481         $order_0_1 = C4::Acquisition::populate_order_with_prices(
482             {
483                 order        => $order_0_1,
484                 booksellerid => 'just_something',
485                 ordering     => 1,
486             }
487         );
488
489         # Note that this configuration is correct \o/
490         compare(
491             {
492                 got      => $order_0_1->{rrpgsti},
493                 expected => 86.10,
494                 conf     => '1 0',
495                 field    => 'rrpgsti'
496             }
497         );
498         compare(
499             {
500                 got      => $order_0_1->{rrpgste},
501                 expected => 82.00,
502                 conf     => '1 0',
503                 field    => 'rrpgste'
504             }
505         );
506         compare(
507             {
508                 got      => $order_0_1->{ecostgsti},
509                 expected => 77.49,
510                 conf     => '1 0',
511                 field    => 'ecostgsti'
512             }
513         );
514         compare(
515             {
516                 got      => $order_0_1->{ecostgste},
517                 expected => 73.80,
518                 conf     => '1 0',
519                 field    => 'ecostgste'
520             }
521         );
522         compare(
523             {
524                 got      => $order_0_1->{gstvalue},
525                 expected => 7.38,
526                 conf     => '1 0',
527                 field    => 'gstvalue'
528             }
529         );
530         compare(
531             {
532                 got      => $order_0_1->{totalgsti},
533                 expected => 154.98,
534                 conf     => '1 0',
535                 field    => 'totalgsti'
536             }
537         );
538         compare(
539             {
540                 got      => $order_0_1->{totalgste},
541                 expected => 147.60,
542                 conf     => '1 0',
543                 field    => 'totalgste'
544             }
545         );
546
547         $order_0_1 = C4::Acquisition::populate_order_with_prices(
548             {
549                 order        => $order_0_1,
550                 booksellerid => 'just_something',
551                 receiving    => 1,
552             }
553         );
554         # Note that this configuration is correct
555         compare(
556             {
557                 got      => $order_0_1->{unitpricegsti},
558                 expected => 77.49,
559                 conf     => '0 1',
560                 field    => 'unitpricegsti'
561             }
562         );
563         compare(
564             {
565                 got      => $order_0_1->{unitpricegste},
566                 expected => 73.80,
567                 conf     => '0 1',
568                 field    => 'unitpricegste'
569             }
570         );
571         compare(
572             {
573                 got      => $order_0_1->{gstvalue},
574                 expected => 7.38,
575                 conf     => '0 1',
576                 field    => 'gstvalue'
577             }
578         );
579         compare(
580             {
581                 got      => $order_0_1->{totalgsti},
582                 expected => 154.98,
583                 conf     => '0 1',
584                 field    => 'totalgsti'
585             }
586         );
587         compare(
588             {
589                 got      => $order_0_1->{totalgste},
590                 expected => 147.60,
591                 conf     => '0 1',
592                 field    => 'totalgste'
593             }
594         );
595     };
596 }
597
598 sub compare {
599     my ($params) = @_;
600     is(
601         Koha::Number::Price->new( $params->{got} )->format,
602         Koha::Number::Price->new( $params->{expected} )->format,
603 "configuration $params->{conf}: $params->{field} should be correctly calculated"
604     );
605 }
606
607 # format_for_editing
608 for my $currency_format ( qw( US FR ) ) {
609     t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
610     is( Koha::Number::Price->new( 1234567 )->format_for_editing, '1234567.00', 'format_for_editing should return unformated integer part with 2 decimals' );
611     is( Koha::Number::Price->new( 1234567.89 )->format_for_editing, '1234567.89', 'format_for_editing should return unformated integer part with 2 decimals' );
612 }