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