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