Bug 18137: List Mojolicious::Plugin::OpenAPI and JSON::Validator as dependencies
[koha.git] / t / Prices.t
blob46e2a3ace310ee935e153b874f7c609cf59b0766
1 use Modern::Perl;
2 use Test::More;
3 use Test::MockModule;
5 use t::lib::Mocks;
7 use Module::Load::Conditional qw/check_install/;
9 BEGIN {
10     if ( check_install( module => 'Test::DBIx::Class' ) ) {
11         plan tests => 16;
12     } else {
13         plan skip_all => "Need Test::DBIx::Class"
14     }
17 use_ok('C4::Acquisition');
18 use_ok('C4::Context');
19 use_ok('Koha::Number::Price');
21 t::lib::Mocks::mock_preference( 'gist', '0.02|0.05|0.196' );
23 use Test::DBIx::Class;
25 my $db = Test::MockModule->new('Koha::Database');
26 $db->mock( _new_schema => sub { return Schema(); } );
27 Koha::Database::flush_schema_cache();
29 fixtures_ok [
30     Currency => [
31         [ qw/ currency symbol rate active / ],
32         [[ 'my_cur', '€', 1, 1, ]],
33     ],
34     Aqbookseller => [
35         [ qw/ id name listincgst invoiceincgst / ],
36         [ 1, '0 0', 0, 0 ],
37         [ 2, '0 1', 0, 1 ],
38         [ 3, '1 0', 1, 0 ],
39         [ 4, '1 1', 1, 1 ],
40     ],
41 ], 'add currency fixtures';
43 my $bookseller_module = Test::MockModule->new('Koha::Acquisition::Bookseller');
45 my ( $basketno_0_0,  $basketno_1_1,  $basketno_1_0,  $basketno_0_1 );
46 my ( $invoiceid_0_0, $invoiceid_1_1, $invoiceid_1_0, $invoiceid_0_1 );
47 my $today;
49 for my $currency_format ( qw( US FR ) ) {
50     t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
51     subtest 'Configuration 1: 0 0' => sub {
52         plan tests => 8;
54         my $biblionumber_0_0 = 42;
56         my $order_0_0 = {
57             biblionumber     => $biblionumber_0_0,
58             quantity         => 2,
59             listprice        => 82.000000,
60             unitprice        => 73.80000,
61             quantityreceived => 2,
62             basketno         => $basketno_0_0,
63             invoiceid        => $invoiceid_0_0,
64             rrp              => 82.00,
65             ecost            => 73.80,
66             tax_rate         => 0.0500,
67             discount         => 10.0000,
68             datereceived     => $today
69         };
70         $order_0_0 = C4::Acquisition::populate_order_with_prices(
71             {
72                 order        => $order_0_0,
73                 booksellerid => 1,
74                 ordering     => 1,
75             }
76         );
78         compare(
79             {
80                 got      => $order_0_0->{rrp_tax_included},
81                 expected => 86.10,
82                 conf     => '0 0',
83                 field    => 'rrp_tax_included'
84             }
85         );
86         compare(
87             {
88                 got      => $order_0_0->{rrp_tax_excluded},
89                 expected => 82.00,
90                 conf     => '0 0',
91                 field    => 'rrp_tax_excluded'
92             }
93         );
94         compare(
95             {
96                 got      => $order_0_0->{ecost_tax_included},
97                 expected => 77.49,
98                 conf     => '0 0',
99                 field    => 'ecost_tax_included'
100             }
101         );
102         compare(
103             {
104                 got      => $order_0_0->{ecost_tax_excluded},
105                 expected => 73.80,
106                 conf     => '0 0',
107                 field    => 'ecost_tax_excluded'
108             }
109         );
110         compare(
111             {
112                 got      => $order_0_0->{tax_value_on_ordering},
113                 expected => 7.38,
114                 conf     => '0 0',
115                 field    => 'tax_value'
116             }
117         );
119         $order_0_0 = C4::Acquisition::populate_order_with_prices(
120             {
121                 order        => $order_0_0,
122                 booksellerid => 1,
123                 receiving    => 1,
124             }
125         );
127         compare(
128             {
129                 got      => $order_0_0->{unitprice_tax_included},
130                 expected => 77.49,
131                 conf     => '0 0',
132                 field    => 'unitprice_tax_included'
133             }
134         );
135         compare(
136             {
137                 got      => $order_0_0->{unitprice_tax_excluded},
138                 expected => 73.80,
139                 conf     => '0 0',
140                 field    => 'unitprice_tax_excluded'
141             }
142         );
143         compare(
144             {
145                 got      => $order_0_0->{tax_value_on_receiving},
146                 expected => 7.38,
147                 conf     => '0 0',
148                 field    => 'tax_value'
149             }
150         );
151     };
153     subtest 'Configuration 1: 1 1' => sub {
154         plan tests => 8;
156         my $biblionumber_1_1 = 43;
157         my $order_1_1        = {
158             biblionumber     => $biblionumber_1_1,
159             quantity         => 2,
160             listprice        => 82.000000,
161             unitprice        => 73.800000,
162             quantityreceived => 2,
163             basketno         => $basketno_1_1,
164             invoiceid        => $invoiceid_1_1,
165             rrp              => 82.00,
166             ecost            => 73.80,
167             tax_rate         => 0.0500,
168             discount         => 10.0000,
169             datereceived     => $today
170         };
172         $order_1_1 = C4::Acquisition::populate_order_with_prices(
173             {
174                 order        => $order_1_1,
175                 booksellerid => 4,
176                 ordering     => 1,
177             }
178         );
180         compare(
181             {
182                 got      => $order_1_1->{rrp_tax_included},
183                 expected => 82.00,
184                 conf     => '1 1',
185                 field    => 'rrp_tax_included'
186             }
187         );
188         compare(
189             {
190                 got      => $order_1_1->{rrp_tax_excluded},
191                 expected => 78.10,
192                 conf     => '1 1',
193                 field    => 'rrp_tax_excluded'
194             }
195         );
196         compare(
197             {
198                 got      => $order_1_1->{ecost_tax_included},
199                 expected => 73.80,
200                 conf     => '1 1',
201                 field    => 'ecost_tax_included'
202             }
203         );
204         compare(
205             {
206                 got      => $order_1_1->{ecost_tax_excluded},
207                 expected => 70.29,
208                 conf     => '1 1',
209                 field    => 'ecost_tax_excluded'
210             }
211         );
212         compare(
213             {
214                 got      => $order_1_1->{tax_value_on_ordering},
215                 expected => 7.03,
216                 conf     => '1 1',
217                 field    => 'tax_value'
218             }
219         );
221         $order_1_1 = C4::Acquisition::populate_order_with_prices(
222             {
223                 order        => $order_1_1,
224                 booksellerid => 4,
225                 receiving    => 1,
226             }
227         );
229         compare(
230             {
231                 got      => $order_1_1->{unitprice_tax_included},
232                 expected => 73.80,
233                 conf     => '1 1',
234                 field    => 'unitprice_tax_included'
235             }
236         );
237         compare(
238             {
239                 got      => $order_1_1->{unitprice_tax_excluded},
240                 expected => 70.29,
241                 conf     => '1 1',
242                 field    => 'unitprice_tax_excluded'
243             }
244         );
245         compare(
246             {
247                 got      => $order_1_1->{tax_value_on_receiving},
248                 expected => 7.03,
249                 conf     => '1 1',
250                 field    => 'tax_value'
251             }
252         );
253     };
255     subtest 'Configuration 1: 1 0' => sub {
256         plan tests => 8;
258         my $biblionumber_1_0 = 44;
259         my $order_1_0        = {
260             biblionumber     => $biblionumber_1_0,
261             quantity         => 2,
262             listprice        => 82.000000,
263             unitprice        => 70.290000,
264             quantityreceived => 2,
265             basketno         => $basketno_1_1,
266             invoiceid        => $invoiceid_1_1,
267             rrp              => 82.00,
268             ecost            => 73.80,
269             tax_rate         => 0.0500,
270             discount         => 10.0000,
271             datereceived     => $today
272         };
274         $order_1_0 = C4::Acquisition::populate_order_with_prices(
275             {
276                 order        => $order_1_0,
277                 booksellerid => 3,
278                 ordering     => 1,
279             }
280         );
282         compare(
283             {
284                 got      => $order_1_0->{rrp_tax_included},
285                 expected => 82,
286                 conf     => '1 0',
287                 field    => 'rrp_tax_included'
288             }
289         );
290         compare(
291             {
292                 got      => $order_1_0->{rrp_tax_excluded},
293                 expected => 78.10,
294                 conf     => '1 0',
295                 field    => 'rrp_tax_excluded'
296             }
297         );
298         compare(
299             {
300                 got      => $order_1_0->{ecost_tax_included},
301                 expected => 73.80,
302                 conf     => '1 0',
303                 field    => 'ecost_tax_included'
304             }
305         );
306         compare(
307             {
308                 got      => $order_1_0->{ecost_tax_excluded},
309                 expected => 70.29,
310                 conf     => '1 0',
311                 field    => 'ecost_tax_excluded'
312             }
313         );
314         compare(
315             {
316                 got      => $order_1_0->{tax_value_on_ordering},
317                 expected => 7.03,
318                 conf     => '1 0',
319                 field    => 'tax_value'
320             }
321         );
323         $order_1_0 = C4::Acquisition::populate_order_with_prices(
324             {
325                 order        => $order_1_0,
326                 booksellerid => 3,
327                 receiving    => 1,
328             }
329         );
331         compare(
332             {
333                 got      => $order_1_0->{unitprice_tax_included},
334                 expected => 73.80,
335                 conf     => '1 0',
336                 field    => 'unitprice_tax_included'
337             }
338         );
339         compare(
340             {
341                 got      => $order_1_0->{unitprice_tax_excluded},
342                 expected => 70.29,
343                 conf     => '1 0',
344                 field    => 'unitprice_tax_excluded'
345             }
346         );
347         compare(
348             {
349                 got      => $order_1_0->{tax_value_on_receiving},
350                 expected => 7.03,
351                 conf     => '1 0',
352                 field    => 'tax_value'
353             }
354         );
355     };
357     subtest 'Configuration 1: 0 1' => sub {
358         plan tests => 8;
360         my $biblionumber_0_1 = 45;
361         my $order_0_1        = {
362             biblionumber     => $biblionumber_0_1,
363             quantity         => 2,
364             listprice        => 82.000000,
365             unitprice        => 77.490000,
366             quantityreceived => 2,
367             basketno         => $basketno_1_1,
368             invoiceid        => $invoiceid_1_1,
369             rrp              => 82.00,
370             ecost            => 73.80,
371             tax_rate         => 0.0500,
372             discount         => 10.0000,
373             datereceived     => $today
374         };
376         $order_0_1 = C4::Acquisition::populate_order_with_prices(
377             {
378                 order        => $order_0_1,
379                 booksellerid => 2,
380                 ordering     => 1,
381             }
382         );
384         compare(
385             {
386                 got      => $order_0_1->{rrp_tax_included},
387                 expected => 86.10,
388                 conf     => '0 1',
389                 field    => 'rrp_tax_included'
390             }
391         );
392         compare(
393             {
394                 got      => $order_0_1->{rrp_tax_excluded},
395                 expected => 82.00,
396                 conf     => '0 1',
397                 field    => 'rrp_tax_excluded'
398             }
399         );
400         compare(
401             {
402                 got      => $order_0_1->{ecost_tax_included},
403                 expected => 77.49,
404                 conf     => '0 1',
405                 field    => 'ecost_tax_included'
406             }
407         );
408         compare(
409             {
410                 got      => $order_0_1->{ecost_tax_excluded},
411                 expected => 73.80,
412                 conf     => '0 1',
413                 field    => 'ecost_tax_excluded'
414             }
415         );
416         compare(
417             {
418                 got      => $order_0_1->{tax_value_on_ordering},
419                 expected => 7.38,
420                 conf     => '0 1',
421                 field    => 'tax_value'
422             }
423         );
425         $order_0_1 = C4::Acquisition::populate_order_with_prices(
426             {
427                 order        => $order_0_1,
428                 booksellerid => 2,
429                 receiving    => 1,
430             }
431         );
433         compare(
434             {
435                 got      => $order_0_1->{unitprice_tax_included},
436                 expected => 77.49,
437                 conf     => '0 1',
438                 field    => 'unitprice_tax_included'
439             }
440         );
441         compare(
442             {
443                 got      => $order_0_1->{unitprice_tax_excluded},
444                 expected => 73.80,
445                 conf     => '0 1',
446                 field    => 'unitprice_tax_excluded'
447             }
448         );
449         compare(
450             {
451                 got      => $order_0_1->{tax_value_on_receiving},
452                 expected => 7.38,
453                 conf     => '0 1',
454                 field    => 'tax_value'
455             }
456         );
457     };
460 sub compare {
461     my ($params) = @_;
462     is(
463         Koha::Number::Price->new( $params->{got} )->format,
464         Koha::Number::Price->new( $params->{expected} )->format,
465 "configuration $params->{conf}: $params->{field} should be correctly calculated"
466     );
469 # format_for_editing
470 for my $currency_format ( qw( US FR ) ) {
471     t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
472     is( Koha::Number::Price->new( 1234567 )->format_for_editing, '1234567.00', 'format_for_editing should return unformated integer part with 2 decimals' );
473     is( Koha::Number::Price->new( 1234567.89 )->format_for_editing, '1234567.89', 'format_for_editing should return unformated integer part with 2 decimals' );