Bug 15690: CardnumberLength should not be bigger than 16
[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 => 8;
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         compare(
83             {
84                 got      => $order_0_0->{rrp_tax_included},
85                 expected => 86.10,
86                 conf     => '0 0',
87                 field    => 'rrp_tax_included'
88             }
89         );
90         compare(
91             {
92                 got      => $order_0_0->{rrp_tax_excluded},
93                 expected => 82.00,
94                 conf     => '0 0',
95                 field    => 'rrp_tax_excluded'
96             }
97         );
98         compare(
99             {
100                 got      => $order_0_0->{ecost_tax_included},
101                 expected => 77.49,
102                 conf     => '0 0',
103                 field    => 'ecost_tax_included'
104             }
105         );
106         compare(
107             {
108                 got      => $order_0_0->{ecost_tax_excluded},
109                 expected => 73.80,
110                 conf     => '0 0',
111                 field    => 'ecost_tax_excluded'
112             }
113         );
114         compare(
115             {
116                 got      => $order_0_0->{tax_value_on_ordering},
117                 expected => 7.38,
118                 conf     => '0 0',
119                 field    => 'tax_value'
120             }
121         );
122
123         $order_0_0 = C4::Acquisition::populate_order_with_prices(
124             {
125                 order        => $order_0_0,
126                 booksellerid => 'just_something',
127                 receiving    => 1,
128             }
129         );
130
131         compare(
132             {
133                 got      => $order_0_0->{unitprice_tax_included},
134                 expected => 77.49,
135                 conf     => '0 0',
136                 field    => 'unitprice_tax_included'
137             }
138         );
139         compare(
140             {
141                 got      => $order_0_0->{unitprice_tax_excluded},
142                 expected => 73.80,
143                 conf     => '0 0',
144                 field    => 'unitprice_tax_excluded'
145             }
146         );
147         compare(
148             {
149                 got      => $order_0_0->{tax_value_on_receiving},
150                 expected => 7.38,
151                 conf     => '0 0',
152                 field    => 'tax_value'
153             }
154         );
155     };
156
157     subtest 'Configuration 1: 1 1' => sub {
158         plan tests => 8;
159         $bookseller_module->mock(
160             'fetch',
161             sub {
162                 return { listincgst => 1, invoiceincgst => 1 };
163             }
164         );
165
166         my $biblionumber_1_1 = 43;
167         my $order_1_1        = {
168             biblionumber     => $biblionumber_1_1,
169             quantity         => 2,
170             listprice        => 82.000000,
171             unitprice        => 73.800000,
172             quantityreceived => 2,
173             basketno         => $basketno_1_1,
174             invoiceid        => $invoiceid_1_1,
175             rrp              => 82.00,
176             ecost            => 73.80,
177             tax_rate         => 0.0500,
178             discount         => 10.0000,
179             datereceived     => $today
180         };
181
182         $order_1_1 = C4::Acquisition::populate_order_with_prices(
183             {
184                 order        => $order_1_1,
185                 booksellerid => 'just_something',
186                 ordering     => 1,
187             }
188         );
189
190         compare(
191             {
192                 got      => $order_1_1->{rrp_tax_included},
193                 expected => 82.00,
194                 conf     => '1 1',
195                 field    => 'rrp_tax_included'
196             }
197         );
198         compare(
199             {
200                 got      => $order_1_1->{rrp_tax_excluded},
201                 expected => 78.10,
202                 conf     => '1 1',
203                 field    => 'rrp_tax_excluded'
204             }
205         );
206         compare(
207             {
208                 got      => $order_1_1->{ecost_tax_included},
209                 expected => 73.80,
210                 conf     => '1 1',
211                 field    => 'ecost_tax_included'
212             }
213         );
214         compare(
215             {
216                 got      => $order_1_1->{ecost_tax_excluded},
217                 expected => 70.29,
218                 conf     => '1 1',
219                 field    => 'ecost_tax_excluded'
220             }
221         );
222         compare(
223             {
224                 got      => $order_1_1->{tax_value_on_ordering},
225                 expected => 7.03,
226                 conf     => '1 1',
227                 field    => 'tax_value'
228             }
229         );
230
231         $order_1_1 = C4::Acquisition::populate_order_with_prices(
232             {
233                 order        => $order_1_1,
234                 booksellerid => 'just_something',
235                 receiving    => 1,
236             }
237         );
238
239         compare(
240             {
241                 got      => $order_1_1->{unitprice_tax_included},
242                 expected => 73.80,
243                 conf     => '1 1',
244                 field    => 'unitprice_tax_included'
245             }
246         );
247         compare(
248             {
249                 got      => $order_1_1->{unitprice_tax_excluded},
250                 expected => 70.29,
251                 conf     => '1 1',
252                 field    => 'unitprice_tax_excluded'
253             }
254         );
255         compare(
256             {
257                 got      => $order_1_1->{tax_value_on_receiving},
258                 expected => 7.03,
259                 conf     => '1 1',
260                 field    => 'tax_value'
261             }
262         );
263     };
264
265     subtest 'Configuration 1: 1 0' => sub {
266         plan tests => 8;
267         $bookseller_module->mock(
268             'fetch',
269             sub {
270                 return { listincgst => 1, invoiceincgst => 0 };
271             }
272         );
273
274         my $biblionumber_1_0 = 44;
275         my $order_1_0        = {
276             biblionumber     => $biblionumber_1_0,
277             quantity         => 2,
278             listprice        => 82.000000,
279             unitprice        => 70.290000,
280             quantityreceived => 2,
281             basketno         => $basketno_1_1,
282             invoiceid        => $invoiceid_1_1,
283             rrp              => 82.00,
284             ecost            => 73.80,
285             tax_rate         => 0.0500,
286             discount         => 10.0000,
287             datereceived     => $today
288         };
289
290         $order_1_0 = C4::Acquisition::populate_order_with_prices(
291             {
292                 order        => $order_1_0,
293                 booksellerid => 'just_something',
294                 ordering     => 1,
295             }
296         );
297
298         compare(
299             {
300                 got      => $order_1_0->{rrp_tax_included},
301                 expected => 82,
302                 conf     => '1 0',
303                 field    => 'rrp_tax_included'
304             }
305         );
306         compare(
307             {
308                 got      => $order_1_0->{rrp_tax_excluded},
309                 expected => 78.10,
310                 conf     => '1 0',
311                 field    => 'rrp_tax_excluded'
312             }
313         );
314         compare(
315             {
316                 got      => $order_1_0->{ecost_tax_included},
317                 expected => 73.80,
318                 conf     => '1 0',
319                 field    => 'ecost_tax_included'
320             }
321         );
322         compare(
323             {
324                 got      => $order_1_0->{ecost_tax_excluded},
325                 expected => 70.29,
326                 conf     => '1 0',
327                 field    => 'ecost_tax_excluded'
328             }
329         );
330         compare(
331             {
332                 got      => $order_1_0->{tax_value_on_ordering},
333                 expected => 7.03,
334                 conf     => '1 0',
335                 field    => 'tax_value'
336             }
337         );
338
339         $order_1_0 = C4::Acquisition::populate_order_with_prices(
340             {
341                 order        => $order_1_0,
342                 booksellerid => 'just_something',
343                 receiving    => 1,
344             }
345         );
346
347         compare(
348             {
349                 got      => $order_1_0->{unitprice_tax_included},
350                 expected => 73.80,
351                 conf     => '1 0',
352                 field    => 'unitprice_tax_included'
353             }
354         );
355         compare(
356             {
357                 got      => $order_1_0->{unitprice_tax_excluded},
358                 expected => 70.29,
359                 conf     => '1 0',
360                 field    => 'unitprice_tax_excluded'
361             }
362         );
363         compare(
364             {
365                 got      => $order_1_0->{tax_value_on_receiving},
366                 expected => 7.03,
367                 conf     => '1 0',
368                 field    => 'tax_value'
369             }
370         );
371     };
372
373     subtest 'Configuration 1: 0 1' => sub {
374         plan tests => 8;
375         $bookseller_module->mock(
376             'fetch',
377             sub {
378                 return { listincgst => 0, invoiceincgst => 1 };
379             }
380         );
381
382         my $biblionumber_0_1 = 45;
383         my $order_0_1        = {
384             biblionumber     => $biblionumber_0_1,
385             quantity         => 2,
386             listprice        => 82.000000,
387             unitprice        => 77.490000,
388             quantityreceived => 2,
389             basketno         => $basketno_1_1,
390             invoiceid        => $invoiceid_1_1,
391             rrp              => 82.00,
392             ecost            => 73.80,
393             tax_rate         => 0.0500,
394             discount         => 10.0000,
395             datereceived     => $today
396         };
397
398         $order_0_1 = C4::Acquisition::populate_order_with_prices(
399             {
400                 order        => $order_0_1,
401                 booksellerid => 'just_something',
402                 ordering     => 1,
403             }
404         );
405
406         compare(
407             {
408                 got      => $order_0_1->{rrp_tax_included},
409                 expected => 86.10,
410                 conf     => '0 1',
411                 field    => 'rrp_tax_included'
412             }
413         );
414         compare(
415             {
416                 got      => $order_0_1->{rrp_tax_excluded},
417                 expected => 82.00,
418                 conf     => '0 1',
419                 field    => 'rrp_tax_excluded'
420             }
421         );
422         compare(
423             {
424                 got      => $order_0_1->{ecost_tax_included},
425                 expected => 77.49,
426                 conf     => '0 1',
427                 field    => 'ecost_tax_included'
428             }
429         );
430         compare(
431             {
432                 got      => $order_0_1->{ecost_tax_excluded},
433                 expected => 73.80,
434                 conf     => '0 1',
435                 field    => 'ecost_tax_excluded'
436             }
437         );
438         compare(
439             {
440                 got      => $order_0_1->{tax_value_on_ordering},
441                 expected => 7.38,
442                 conf     => '0 1',
443                 field    => 'tax_value'
444             }
445         );
446
447         $order_0_1 = C4::Acquisition::populate_order_with_prices(
448             {
449                 order        => $order_0_1,
450                 booksellerid => 'just_something',
451                 receiving    => 1,
452             }
453         );
454
455         compare(
456             {
457                 got      => $order_0_1->{unitprice_tax_included},
458                 expected => 77.49,
459                 conf     => '0 1',
460                 field    => 'unitprice_tax_included'
461             }
462         );
463         compare(
464             {
465                 got      => $order_0_1->{unitprice_tax_excluded},
466                 expected => 73.80,
467                 conf     => '0 1',
468                 field    => 'unitprice_tax_excluded'
469             }
470         );
471         compare(
472             {
473                 got      => $order_0_1->{tax_value_on_receiving},
474                 expected => 7.38,
475                 conf     => '0 1',
476                 field    => 'tax_value'
477             }
478         );
479     };
480 }
481
482 sub compare {
483     my ($params) = @_;
484     is(
485         Koha::Number::Price->new( $params->{got} )->format,
486         Koha::Number::Price->new( $params->{expected} )->format,
487 "configuration $params->{conf}: $params->{field} should be correctly calculated"
488     );
489 }
490
491 # format_for_editing
492 for my $currency_format ( qw( US FR ) ) {
493     t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
494     is( Koha::Number::Price->new( 1234567 )->format_for_editing, '1234567.00', 'format_for_editing should return unformated integer part with 2 decimals' );
495     is( Koha::Number::Price->new( 1234567.89 )->format_for_editing, '1234567.89', 'format_for_editing should return unformated integer part with 2 decimals' );
496 }