Bug 18433: Remember item search results selected rows in session storage
[koha.git] / t / Prices.t
blob740bfe5e11ca91b1102e35ba51799d2fe2f639bf
1 use Modern::Perl;
2 use Test::More;
3 use Test::MockModule;
5 use t::lib::Mocks;
7 use Module::Load::Conditional qw/check_install/;
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     }
17 use_ok('C4::Acquisition');
18 use_ok('C4::Context');
19 use_ok('Koha::Number::Price');
21 t::lib::Mocks::mock_preference( 'gist', '0.02|0.05|0.196' );
23 use Test::DBIx::Class;
25 my $db = Test::MockModule->new('Koha::Database');
26 $db->mock( _new_schema => sub { return Schema(); } );
27 Koha::Database::flush_schema_cache();
29 fixtures_ok [
30     Currency => [
31         [ qw/ currency symbol rate active / ],
32         [ 'my_cur', '€', 1, 1, ],
33     ],
34     Aqbookseller => [
35         [ qw/ id name listincgst invoiceincgst / ],
36         [ 1, '0 0', 0, 0 ],
37         [ 2, '0 1', 0, 1 ],
38         [ 3, '1 0', 1, 0 ],
39         [ 4, '1 1', 1, 1 ],
40     ],
41 ], 'add currency fixtures';
43 my $bookseller_module = Test::MockModule->new('Koha::Acquisition::Bookseller');
45 my ( $basketno_0_0,  $basketno_1_1,  $basketno_1_0,  $basketno_0_1 );
46 my ( $invoiceid_0_0, $invoiceid_1_1, $invoiceid_1_0, $invoiceid_0_1 );
47 my $today;
49 for my $currency_format ( qw( US FR ) ) {
50     t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
51     subtest 'Configuration 1: 0 0 (Vendor List prices do not include tax / Invoice prices do not include tax)' => sub {
52         plan tests => 8;
54         my $biblionumber_0_0 = 42;
56         my $order_0_0 = {
57             biblionumber     => $biblionumber_0_0,
58             quantity         => 2,
59             listprice        => 82,
60             unitprice        => 73.80,
61             quantityreceived => 2,
62             basketno         => $basketno_0_0,
63             invoiceid        => $invoiceid_0_0,
64             rrp              => 82.00,
65             ecost            => 73.80,
66             tax_rate         => 0.0500,
67             discount         => 10,
68             datereceived     => $today
69         };
70         $order_0_0 = C4::Acquisition::populate_order_with_prices(
71             {
72                 order        => $order_0_0,
73                 booksellerid => 1,
74                 ordering     => 1,
75             }
76         );
78         compare(
79             {
80                 got      => $order_0_0->{rrp_tax_included},
81                 expected => 86.10,
82                 conf     => '0 0',
83                 field    => 'rrp_tax_included'
84             }
85         );
86         compare(
87             {
88                 got      => $order_0_0->{rrp_tax_excluded},
89                 expected => 82.00,
90                 conf     => '0 0',
91                 field    => 'rrp_tax_excluded'
92             }
93         );
94         compare(
95             {
96                 got      => $order_0_0->{ecost_tax_included},
97                 expected => 77.49,
98                 conf     => '0 0',
99                 field    => 'ecost_tax_included'
100             }
101         );
102         compare(
103             {
104                 got      => $order_0_0->{ecost_tax_excluded},
105                 expected => 73.80,
106                 conf     => '0 0',
107                 field    => 'ecost_tax_excluded'
108             }
109         );
110         compare(
111             {
112                 got      => $order_0_0->{tax_value_on_ordering},
113                 expected => 7.38,
114                 conf     => '0 0',
115                 field    => 'tax_value'
116             }
117         );
119         $order_0_0 = C4::Acquisition::populate_order_with_prices(
120             {
121                 order        => $order_0_0,
122                 booksellerid => 1,
123                 receiving    => 1,
124             }
125         );
127         compare(
128             {
129                 got      => $order_0_0->{unitprice_tax_included},
130                 expected => 77.49,
131                 conf     => '0 0',
132                 field    => 'unitprice_tax_included'
133             }
134         );
135         compare(
136             {
137                 got      => $order_0_0->{unitprice_tax_excluded},
138                 expected => 73.80,
139                 conf     => '0 0',
140                 field    => 'unitprice_tax_excluded'
141             }
142         );
143         compare(
144             {
145                 got      => $order_0_0->{tax_value_on_receiving},
146                 expected => 7.38,
147                 conf     => '0 0',
148                 field    => 'tax_value'
149             }
150         );
151     };
153     subtest 'Configuration 1: 1 1 (Vendor List prices do include tax / Invoice prices include tax)' => sub {
154         plan tests => 8;
156         my $biblionumber_1_1 = 43;
157         my $order_1_1        = {
158             biblionumber     => $biblionumber_1_1,
159             quantity         => 2,
160             listprice        => 82,
161             unitprice        => 73.80,
162             quantityreceived => 2,
163             basketno         => $basketno_1_1,
164             invoiceid        => $invoiceid_1_1,
165             rrp              => 82.00,
166             ecost            => 73.80,
167             tax_rate         => 0.0500,
168             discount         => 10,
169             datereceived     => $today
170         };
172         $order_1_1 = C4::Acquisition::populate_order_with_prices(
173             {
174                 order        => $order_1_1,
175                 booksellerid => 4,
176                 ordering     => 1,
177             }
178         );
180         compare(
181             {
182                 got      => $order_1_1->{rrp_tax_included},
183                 expected => 82.00,
184                 conf     => '1 1',
185                 field    => 'rrp_tax_included'
186             }
187         );
188         compare(
189             {
190                 got      => $order_1_1->{rrp_tax_excluded},
191                 expected => 78.10,
192                 conf     => '1 1',
193                 field    => 'rrp_tax_excluded'
194             }
195         );
196         compare(
197             {
198                 got      => $order_1_1->{ecost_tax_included},
199                 expected => 73.80,
200                 conf     => '1 1',
201                 field    => 'ecost_tax_included'
202             }
203         );
204         compare(
205             {
206                 got      => $order_1_1->{ecost_tax_excluded},
207                 expected => 70.29,
208                 conf     => '1 1',
209                 field    => 'ecost_tax_excluded'
210             }
211         );
212         compare(
213             {
214                 got      => $order_1_1->{tax_value_on_ordering},
215                 expected => 7.03,
216                 conf     => '1 1',
217                 field    => 'tax_value'
218             }
219         );
221         $order_1_1 = C4::Acquisition::populate_order_with_prices(
222             {
223                 order        => $order_1_1,
224                 booksellerid => 4,
225                 receiving    => 1,
226             }
227         );
229         compare(
230             {
231                 got      => $order_1_1->{unitprice_tax_included},
232                 expected => 73.80,
233                 conf     => '1 1',
234                 field    => 'unitprice_tax_included'
235             }
236         );
237         compare(
238             {
239                 got      => $order_1_1->{unitprice_tax_excluded},
240                 expected => 70.29,
241                 conf     => '1 1',
242                 field    => 'unitprice_tax_excluded'
243             }
244         );
245         compare(
246             {
247                 got      => $order_1_1->{tax_value_on_receiving},
248                 expected => 7.03,
249                 conf     => '1 1',
250                 field    => 'tax_value'
251             }
252         );
253     };
255     subtest 'Configuration 1: 1 0 (Vendor List prices include tax / Invoice prices do not include tax)' => sub {
256         plan tests => 9;
258         my $biblionumber_1_0 = 44;
259         my $order_1_0        = {
260             biblionumber     => $biblionumber_1_0,
261             quantity         => 2,
262             listprice        => 82,
263             unitprice        => 0,
264             quantityreceived => 2,
265             basketno         => $basketno_1_1,
266             invoiceid        => $invoiceid_1_1,
267             rrp              => 82.00,
268             ecost            => 73.80,
269             tax_rate         => 0.0500,
270             discount         => 10,
271             datereceived     => $today
272         };
274         $order_1_0 = C4::Acquisition::populate_order_with_prices(
275             {
276                 order        => $order_1_0,
277                 booksellerid => 3,
278                 ordering     => 1,
279             }
280         );
282         compare(
283             {
284                 got      => $order_1_0->{rrp_tax_included},
285                 expected => 82,
286                 conf     => '1 0',
287                 field    => 'rrp_tax_included'
288             }
289         );
290         compare(
291             {
292                 got      => $order_1_0->{rrp_tax_excluded},
293                 expected => 78.10,
294                 conf     => '1 0',
295                 field    => 'rrp_tax_excluded'
296             }
297         );
298         compare(
299             {
300                 got      => $order_1_0->{ecost_tax_included},
301                 expected => 73.80,
302                 conf     => '1 0',
303                 field    => 'ecost_tax_included'
304             }
305         );
306         compare(
307             {
308                 got      => $order_1_0->{ecost_tax_excluded},
309                 expected => 70.29,
310                 conf     => '1 0',
311                 field    => 'ecost_tax_excluded'
312             }
313         );
314         # If we order with unitprice = 0, tax is calculated from the ecost
315         # (note that in addorder.pl and addorderiso2709 the unitprice may/will be set to the ecost
316         compare(
317             {
318                 got      => $order_1_0->{tax_value_on_ordering},
319                 expected => 7.03,
320                 conf     => '1 0',
321                 field    => 'tax_value'
322             }
323         );
324         $order_1_0->{unitprice} = 70.29;
325         $order_1_0 = C4::Acquisition::populate_order_with_prices(
326             {
327                 order        => $order_1_0,
328                 booksellerid => 3,
329                 ordering    => 1,
330             }
331         );
332         # If a unitprice is provided at ordering, we calculate the tax from that
333         compare(
334             {
335                 got      => $order_1_0->{tax_value_on_ordering},
336                 expected => 6.69,
337                 conf     => '1 0',
338                 field    => 'tax_value'
339             }
340         );
342         $order_1_0 = C4::Acquisition::populate_order_with_prices(
343             {
344                 order        => $order_1_0,
345                 booksellerid => 3,
346                 receiving    => 1,
347             }
348         );
350         compare(
351             {
352                 got      => $order_1_0->{unitprice_tax_included},
353                 expected => 73.80,
354                 conf     => '1 0',
355                 field    => 'unitprice_tax_included'
356             }
357         );
358         compare(
359             {
360                 got      => $order_1_0->{unitprice_tax_excluded},
361                 expected => 70.29,
362                 conf     => '1 0',
363                 field    => 'unitprice_tax_excluded'
364             }
365         );
366         compare(
367             {
368                 got      => $order_1_0->{tax_value_on_receiving},
369                 expected => 7.03,
370                 conf     => '1 0',
371                 field    => 'tax_value'
372             }
373         );
374     };
376     subtest 'Configuration 1: 0 1 (Vendor List prices do not include tax / Invoice prices include tax)' => sub {
377         plan tests => 9;
379         my $biblionumber_0_1 = 45;
380         my $order_0_1        = {
381             biblionumber     => $biblionumber_0_1,
382             quantity         => 2,
383             listprice        => 82,
384             unitprice        => 0,
385             quantityreceived => 2,
386             basketno         => $basketno_1_1,
387             invoiceid        => $invoiceid_1_1,
388             rrp              => 82.00,
389             ecost            => 73.80,
390             tax_rate         => 0.0500,
391             discount         => 10,
392             datereceived     => $today
393         };
395         $order_0_1 = C4::Acquisition::populate_order_with_prices(
396             {
397                 order        => $order_0_1,
398                 booksellerid => 2,
399                 ordering     => 1,
400             }
401         );
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 = C4::Acquisition::populate_order_with_prices(
447             {
448                 order        => $order_0_1,
449                 booksellerid => 2,
450                 ordering     => 1,
451             }
452         );
453         # If a unitprice is provided at ordering, we calculate the tax from that
454         compare(
455             {
456                 got      => $order_0_1->{tax_value_on_ordering},
457                 expected => 7.75,
458                 conf     => '0 1',
459                 field    => 'tax_value'
460             }
461         );
462         $order_0_1 = C4::Acquisition::populate_order_with_prices(
463             {
464                 order        => $order_0_1,
465                 booksellerid => 2,
466                 receiving    => 1,
467             }
468         );
470         compare(
471             {
472                 got      => $order_0_1->{unitprice_tax_included},
473                 expected => 77.49,
474                 conf     => '0 1',
475                 field    => 'unitprice_tax_included'
476             }
477         );
478         compare(
479             {
480                 got      => $order_0_1->{unitprice_tax_excluded},
481                 expected => 73.80,
482                 conf     => '0 1',
483                 field    => 'unitprice_tax_excluded'
484             }
485         );
486         compare(
487             {
488                 got      => $order_0_1->{tax_value_on_receiving},
489                 expected => 7.38,
490                 conf     => '0 1',
491                 field    => 'tax_value'
492             }
493         );
494     };
497 sub compare {
498     my ($params) = @_;
499     is(
500         Koha::Number::Price->new( $params->{got} )->format,
501         Koha::Number::Price->new( $params->{expected} )->format,
502 "configuration $params->{conf}: $params->{field} should be correctly calculated"
503     );
506 # format_for_editing
507 for my $currency_format ( qw( US FR ) ) {
508     t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
509     is( Koha::Number::Price->new( 1234567 )->format_for_editing, '1234567.00', 'format_for_editing should return unformated integer part with 2 decimals' );
510     is( Koha::Number::Price->new( 1234567.89 )->format_for_editing, '1234567.89', 'format_for_editing should return unformated integer part with 2 decimals' );