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