Bug 34967: Remove Test::DBIx::Class
[koha.git] / t / Prices.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 1;
5 use Test::MockModule;
6
7 use t::lib::Mocks;
8 use t::lib::TestBuilder;
9
10 use Koha::Database;
11 use Koha::Number::Price;
12
13 my $schema  = Koha::Database->new->schema;
14 $schema->storage->txn_begin;
15 my $builder = t::lib::TestBuilder->new;
16
17 subtest 'Tests from t' => sub {
18     plan tests => 12;
19
20     t::lib::Mocks::mock_preference( 'TaxRates', '0.02|0.05|0.196' );
21     my $bookseller_module = Test::MockModule->new('Koha::Acquisition::Bookseller');
22
23     my ( @booksellers, @baskets );
24     my @names = ( '0 0', '0 1', '1 0', '1 1' );
25     foreach my $i ( 1..4 ) {
26         push @booksellers, $builder->build_object(
27             {
28                 class => 'Koha::Acquisition::Booksellers',
29                 value =>
30                     { name => $names[ $i - 1 ], listincgst => $i < 3 ? 0 : 1, invoiceincgst => $i % 2 == 0 ? 1 : 0 }
31             }
32         );
33         push @baskets, $builder->build_object(
34             {
35                 class => 'Koha::Acquisition::Baskets',
36                 value =>
37                     { basketname => $names[ $i - 1 ], booksellerid => $booksellers[$i-1]->id }
38             }
39         );
40     }
41
42     my ( $basketno_0_0, $basketno_0_1, $basketno_1_0, $basketno_1_1 ) = map { $_->id } @baskets;
43     my ( $invoiceid_0_0, $invoiceid_1_1 );
44     my $today;
45
46     for my $currency_format ( qw( US FR ) ) {
47         t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
48         subtest 'Configuration 1: 0 0 (Vendor List prices do not include tax / Invoice prices do not include tax)' => sub {
49             plan tests => 8;
50
51             my $biblionumber_0_0 = 42;
52
53             my $order_0_0 = Koha::Acquisition::Order->new({
54                 biblionumber     => $biblionumber_0_0,
55                 quantity         => 2,
56                 listprice        => 82,
57                 unitprice        => 73.80,
58                 quantityreceived => 2,
59                 basketno         => $basketno_0_0,
60                 invoiceid        => $invoiceid_0_0,
61                 rrp              => 82.00,
62                 ecost            => 73.80,
63                 tax_rate_on_ordering  => 0.0500,
64                 tax_rate_on_receiving => 0.0500,
65                 discount         => 10,
66                 datereceived     => $today
67             });
68             $order_0_0->populate_with_prices_for_ordering();
69
70             compare(
71                 {
72                     got      => $order_0_0->rrp_tax_included,
73                     expected => 86.10,
74                     conf     => '0 0',
75                     field    => 'rrp_tax_included'
76                 }
77             );
78             compare(
79                 {
80                     got      => $order_0_0->rrp_tax_excluded,
81                     expected => 82.00,
82                     conf     => '0 0',
83                     field    => 'rrp_tax_excluded'
84                 }
85             );
86             compare(
87                 {
88                     got      => $order_0_0->ecost_tax_included,
89                     expected => 77.49,
90                     conf     => '0 0',
91                     field    => 'ecost_tax_included'
92                 }
93             );
94             compare(
95                 {
96                     got      => $order_0_0->ecost_tax_excluded,
97                     expected => 73.80,
98                     conf     => '0 0',
99                     field    => 'ecost_tax_excluded'
100                 }
101             );
102             compare(
103                 {
104                     got      => $order_0_0->tax_value_on_ordering,
105                     expected => 7.38,
106                     conf     => '0 0',
107                     field    => 'tax_value'
108                 }
109             );
110
111             $order_0_0->populate_with_prices_for_receiving();
112
113             compare(
114                 {
115                     got      => $order_0_0->unitprice_tax_included,
116                     expected => 77.49,
117                     conf     => '0 0',
118                     field    => 'unitprice_tax_included'
119                 }
120             );
121             compare(
122                 {
123                     got      => $order_0_0->unitprice_tax_excluded,
124                     expected => 73.80,
125                     conf     => '0 0',
126                     field    => 'unitprice_tax_excluded'
127                 }
128             );
129             compare(
130                 {
131                     got      => $order_0_0->tax_value_on_receiving,
132                     expected => 7.38,
133                     conf     => '0 0',
134                     field    => 'tax_value'
135                 }
136             );
137         };
138
139         subtest 'Configuration 1: 1 1 (Vendor List prices do include tax / Invoice prices include tax)' => sub {
140             plan tests => 11;
141
142             my $biblionumber_1_1 = 43;
143             my $order_1_1        = Koha::Acquisition::Order->new({
144                 biblionumber     => $biblionumber_1_1,
145                 quantity         => 2,
146                 listprice        => 82,
147                 unitprice        => 73.80,
148                 quantityreceived => 2,
149                 basketno         => $basketno_1_1,
150                 invoiceid        => $invoiceid_1_1,
151                 rrp              => 82.00,
152                 ecost            => 73.80,
153                 tax_rate_on_ordering  => 0.0500,
154                 tax_rate_on_receiving => 0.0500,
155                 discount         => 10,
156                 datereceived     => $today
157             });
158
159             $order_1_1->populate_with_prices_for_ordering();
160
161             compare(
162                 {
163                     got      => $order_1_1->rrp_tax_included,
164                     expected => 82.00,
165                     conf     => '1 1',
166                     field    => 'rrp_tax_included'
167                 }
168             );
169             compare(
170                 {
171                     got      => $order_1_1->rrp_tax_excluded,
172                     expected => 78.10,
173                     conf     => '1 1',
174                     field    => 'rrp_tax_excluded'
175                 }
176             );
177             compare(
178                 {
179                     got      => $order_1_1->ecost_tax_included,
180                     expected => 73.80,
181                     conf     => '1 1',
182                     field    => 'ecost_tax_included'
183                 }
184             );
185             compare(
186                 {
187                     got      => $order_1_1->ecost_tax_excluded,
188                     expected => 70.29,
189                     conf     => '1 1',
190                     field    => 'ecost_tax_excluded'
191                 }
192             );
193             compare(
194                 {
195                     got      => $order_1_1->tax_value_on_ordering,
196                     expected => 7.03,
197                     conf     => '1 1',
198                     field    => 'tax_value'
199                 }
200             );
201
202             $order_1_1->populate_with_prices_for_receiving();
203
204             compare(
205                 {
206                     got      => $order_1_1->unitprice_tax_included,
207                     expected => 73.80,
208                     conf     => '1 1',
209                     field    => 'unitprice_tax_included'
210                 }
211             );
212             compare(
213                 {
214                     got      => $order_1_1->unitprice_tax_excluded,
215                     expected => 70.29,
216                     conf     => '1 1',
217                     field    => 'unitprice_tax_excluded'
218                 }
219             );
220             compare(
221                 {
222                     got      => $order_1_1->tax_value_on_receiving,
223                     expected => 7.03,
224                     conf     => '1 1',
225                     field    => 'tax_value'
226                 }
227             );
228
229             # When unitprice is 0.00
230             # Koha::Acquisition::Order::populate_with_prices_for_ordering() falls
231             # back to using ecost_tax_included and ecost_tax_excluded
232             $order_1_1        = Koha::Acquisition::Order->new({
233                 biblionumber     => $biblionumber_1_1,
234                 quantity         => 1,
235                 listprice        => 10,
236                 unitprice        => '0.00',
237                 quantityreceived => 1,
238                 basketno         => $basketno_1_1,
239                 invoiceid        => $invoiceid_1_1,
240                 rrp              => 10.00,
241                 ecost            => 10.00,
242                 tax_rate_on_ordering  => 0.1500,
243                 tax_rate_on_receiving => 0.1500,
244                 discount         => 0,
245                 datereceived     => $today
246             });
247
248             $order_1_1->populate_with_prices_for_ordering();
249
250             compare(
251                 {
252                     got      => $order_1_1->ecost_tax_included,
253                     expected => 10.00,
254                     conf     => '1 1',
255                     field    => 'ecost_tax_included'
256                 }
257             );
258             compare(
259                 {
260                     got      => $order_1_1->ecost_tax_excluded,
261                     expected => 8.70,
262                     conf     => '1 1',
263                     field    => 'ecost_tax_excluded'
264                 }
265             );
266             compare(
267                 {
268                     got      => $order_1_1->tax_value_on_ordering,
269                     expected => 1.30,
270                     conf     => '1 1',
271                     field    => 'tax_value'
272                 }
273             );
274         };
275
276         subtest 'Configuration 1: 1 0 (Vendor List prices include tax / Invoice prices do not include tax)' => sub {
277             plan tests => 9;
278
279             my $biblionumber_1_0 = 44;
280             my $order_1_0 = Koha::Acquisition::Order->new({
281                 biblionumber     => $biblionumber_1_0,
282                 quantity         => 2,
283                 listprice        => 82,
284                 unitprice        => 0,
285                 quantityreceived => 2,
286                 basketno         => $basketno_1_0,
287                 invoiceid        => $invoiceid_1_1,
288                 rrp              => 82.00,
289                 ecost            => 73.80,
290                 tax_rate_on_ordering  => 0.0500,
291                 tax_rate_on_receiving => 0.0500,
292                 discount         => 10,
293                 datereceived     => $today
294             });
295
296             $order_1_0->populate_with_prices_for_ordering();
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             # If we order with unitprice = 0, tax is calculated from the ecost
331             # (note that in addorder.pl and addorderiso2709 the unitprice may/will be set to the ecost
332             compare(
333                 {
334                     got      => $order_1_0->tax_value_on_ordering,
335                     expected => 7.03,
336                     conf     => '1 0',
337                     field    => 'tax_value'
338                 }
339             );
340             $order_1_0->unitprice(70.29);
341             $order_1_0->populate_with_prices_for_ordering();
342
343             # If a unitprice is provided at ordering, we calculate the tax from that
344             compare(
345                 {
346                     got      => $order_1_0->tax_value_on_ordering,
347                     expected => 6.69,
348                     conf     => '1 0',
349                     field    => 'tax_value'
350                 }
351             );
352
353             $order_1_0->populate_with_prices_for_receiving();
354
355             compare(
356                 {
357                     got      => $order_1_0->unitprice_tax_included,
358                     expected => 73.80,
359                     conf     => '1 0',
360                     field    => 'unitprice_tax_included'
361                 }
362             );
363             compare(
364                 {
365                     got      => $order_1_0->unitprice_tax_excluded,
366                     expected => 70.29,
367                     conf     => '1 0',
368                     field    => 'unitprice_tax_excluded'
369                 }
370             );
371             compare(
372                 {
373                     got      => $order_1_0->tax_value_on_receiving,
374                     expected => 7.03,
375                     conf     => '1 0',
376                     field    => 'tax_value'
377                 }
378             );
379         };
380
381         subtest 'Configuration 1: 0 1 (Vendor List prices do not include tax / Invoice prices include tax)' => sub {
382             plan tests => 9;
383
384             my $biblionumber_0_1 = 45;
385             my $order_0_1 = Koha::Acquisition::Order->new({
386                 biblionumber     => $biblionumber_0_1,
387                 quantity         => 2,
388                 listprice        => 82,
389                 unitprice        => 0,
390                 quantityreceived => 2,
391                 basketno         => $basketno_0_1,
392                 invoiceid        => $invoiceid_1_1,
393                 rrp              => 82.00,
394                 ecost            => 73.80,
395                 tax_rate_on_ordering  => 0.0500,
396                 tax_rate_on_receiving => 0.0500,
397                 discount         => 10,
398                 datereceived     => $today
399             });
400
401             $order_0_1->populate_with_prices_for_ordering();
402
403             compare(
404                 {
405                     got      => $order_0_1->rrp_tax_included,
406                     expected => 86.10,
407                     conf     => '0 1',
408                     field    => 'rrp_tax_included'
409                 }
410             );
411             compare(
412                 {
413                     got      => $order_0_1->rrp_tax_excluded,
414                     expected => 82.00,
415                     conf     => '0 1',
416                     field    => 'rrp_tax_excluded'
417                 }
418             );
419             compare(
420                 {
421                     got      => $order_0_1->ecost_tax_included,
422                     expected => 77.49,
423                     conf     => '0 1',
424                     field    => 'ecost_tax_included'
425                 }
426             );
427             compare(
428                 {
429                     got      => $order_0_1->ecost_tax_excluded,
430                     expected => 73.80,
431                     conf     => '0 1',
432                     field    => 'ecost_tax_excluded'
433                 }
434             );
435             # If we order with unitprice = 0, tax is calculated from the ecost
436             # (note that in addorder.pl and addorderiso2709 the unitprice may/will be set to the ecost
437             compare(
438                 {
439                     got      => $order_0_1->tax_value_on_ordering,
440                     expected => 7.38,
441                     conf     => '0 1',
442                     field    => 'tax_value'
443                 }
444             );
445             $order_0_1->unitprice(77.490000);
446             $order_0_1->populate_with_prices_for_ordering();
447
448             # If a unitprice is provided at ordering, we calculate the tax from that
449             compare(
450                 {
451                     got      => $order_0_1->tax_value_on_ordering,
452                     expected => 7.75,
453                     conf     => '0 1',
454                     field    => 'tax_value'
455                 }
456             );
457             $order_0_1->populate_with_prices_for_receiving();
458
459             compare(
460                 {
461                     got      => $order_0_1->unitprice_tax_included,
462                     expected => 77.49,
463                     conf     => '0 1',
464                     field    => 'unitprice_tax_included'
465                 }
466             );
467             compare(
468                 {
469                     got      => $order_0_1->unitprice_tax_excluded,
470                     expected => 73.80,
471                     conf     => '0 1',
472                     field    => 'unitprice_tax_excluded'
473                 }
474             );
475             compare(
476                 {
477                     got      => $order_0_1->tax_value_on_receiving,
478                     expected => 7.38,
479                     conf     => '0 1',
480                     field    => 'tax_value'
481                 }
482             );
483         };
484     }
485
486     sub compare {
487         my ($params) = @_;
488         is(
489             Koha::Number::Price->new( $params->{got} )->format,
490             Koha::Number::Price->new( $params->{expected} )->format,
491     "configuration $params->{conf}: $params->{field} should be correctly calculated"
492         );
493     }
494
495     # format_for_editing
496     for my $currency_format ( qw( US FR ) ) {
497         t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
498         is( Koha::Number::Price->new( 1234567 )->format_for_editing, '1234567.00', 'format_for_editing should return unformated integer part with 2 decimals' );
499         is( Koha::Number::Price->new( 1234567.89 )->format_for_editing, '1234567.89', 'format_for_editing should return unformated integer part with 2 decimals' );
500     }
501 };
502
503 $schema->storage->txn_rollback;