Bug 13352: On editing, prices should not be formatted
[koha.git] / t / Prices.t
1 use Modern::Perl;
2 use Test::More tests => 16;
3 use Test::MockModule;
4
5 use t::lib::Mocks;
6
7 BEGIN {
8     my $context_module = t::lib::Mocks::mock_dbh;
9     use_ok('C4::Acquisition');
10     use_ok('C4::Bookseller');
11     use_ok('C4::Context');
12     use_ok('Koha::Number::Price');
13 };
14
15 t::lib::Mocks::mock_preference( 'gist', '0.02|0.05|0.196' );
16
17 my $bookseller_module = Test::MockModule->new('Koha::Acquisition::Bookseller');
18
19 my ( $basketno_0_0,  $basketno_1_1,  $basketno_1_0,  $basketno_0_1 );
20 my ( $invoiceid_0_0, $invoiceid_1_1, $invoiceid_1_0, $invoiceid_0_1 );
21 my $today;
22
23 for my $currency_format ( qw( US FR ) ) {
24     t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
25     subtest 'Configuration 1: 0 0' => sub {
26         plan tests => 7;
27         $bookseller_module->mock(
28             'fetch',
29             sub {
30                 return { listincgst => 0, invoiceincgst => 0 };
31             }
32         );
33
34         my $biblionumber_0_0 = 42;
35
36         my $order_0_0 = {
37             biblionumber     => $biblionumber_0_0,
38             quantity         => 2,
39             listprice        => 82.000000,
40             unitprice        => 73.80000,
41             quantityreceived => 2,
42             basketno         => $basketno_0_0,
43             invoiceid        => $invoiceid_0_0,
44             rrp              => 82.00,
45             ecost            => 73.80,
46             gstrate          => 0.0500,
47             discount         => 10.0000,
48             datereceived     => $today
49         };
50         $order_0_0 = C4::Acquisition::populate_order_with_prices(
51             {
52                 order        => $order_0_0,
53                 booksellerid => 'just_something',
54                 ordering     => 1,
55             }
56         );
57
58         # Note that this configuration is correct \o/
59         compare(
60             {
61                 got      => $order_0_0->{rrpgsti},
62                 expected => 86.10,
63                 conf     => '0 0',
64                 field    => 'rrpgsti'
65             }
66         );
67         compare(
68             {
69                 got      => $order_0_0->{rrpgste},
70                 expected => 82.00,
71                 conf     => '0 0',
72                 field    => 'rrpgste'
73             }
74         );
75         compare(
76             {
77                 got      => $order_0_0->{ecostgsti},
78                 expected => 77.49,
79                 conf     => '0 0',
80                 field    => 'ecostgsti'
81             }
82         );
83         compare(
84             {
85                 got      => $order_0_0->{ecostgste},
86                 expected => 73.80,
87                 conf     => '0 0',
88                 field    => 'ecostgste'
89             }
90         );
91         compare(
92             {
93                 got      => $order_0_0->{gstvalue},
94                 expected => 7.38,
95                 conf     => '0 0',
96                 field    => 'gstvalue'
97             }
98         );
99         compare(
100             {
101                 got      => $order_0_0->{totalgsti},
102                 expected => 154.98,
103                 conf     => '0 0',
104                 field    => 'totalgsti'
105             }
106         );
107         compare(
108             {
109                 got      => $order_0_0->{totalgste},
110                 expected => 147.60,
111                 conf     => '0 0',
112                 field    => 'totalgste'
113             }
114         );
115     };
116
117     subtest 'Configuration 1: 1 1' => sub {
118         plan tests => 7;
119         $bookseller_module->mock(
120             'fetch',
121             sub {
122                 return { listincgst => 1, invoiceincgst => 1 };
123             }
124         );
125
126         my $biblionumber_1_1 = 43;
127         my $order_1_1        = {
128             biblionumber     => $biblionumber_1_1,
129             quantity         => 2,
130             listprice        => 82.000000,
131             unitprice        => 73.800000,
132             quantityreceived => 2,
133             basketno         => $basketno_1_1,
134             invoiceid        => $invoiceid_1_1,
135             rrp              => 82.00,
136             ecost            => 73.80,
137             gstrate          => 0.0500,
138             discount         => 10.0000,
139             datereceived     => $today
140         };
141
142         $order_1_1 = C4::Acquisition::populate_order_with_prices(
143             {
144                 order        => $order_1_1,
145                 booksellerid => 'just_something',
146                 ordering     => 1,
147             }
148         );
149
150         # Note that this configuration is *not* correct
151         # gstvalue should be 7.03 instead of 7.02
152         compare(
153             {
154                 got      => $order_1_1->{rrpgsti},
155                 expected => 82.00,
156                 conf     => '1 1',
157                 field    => 'rrpgsti'
158             }
159         );
160         compare(
161             {
162                 got      => $order_1_1->{rrpgste},
163                 expected => 78.10,
164                 conf     => '1 1',
165                 field    => 'rrpgste'
166             }
167         );
168         compare(
169             {
170                 got      => $order_1_1->{ecostgsti},
171                 expected => 73.80,
172                 conf     => '1 1',
173                 field    => 'ecostgsti'
174             }
175         );
176         compare(
177             {
178                 got      => $order_1_1->{ecostgste},
179                 expected => 70.29,
180                 conf     => '1 1',
181                 field    => 'ecostgste'
182             }
183         );
184         compare(
185             {
186                 got      => $order_1_1->{gstvalue},
187                 expected => 7.02,
188                 conf     => '1 1',
189                 field    => 'gstvalue'
190             }
191         );
192         compare(
193             {
194                 got      => $order_1_1->{totalgsti},
195                 expected => 147.60,
196                 conf     => '1 1',
197                 field    => 'totalgsti'
198             }
199         );
200         compare(
201             {
202                 got      => $order_1_1->{totalgste},
203                 expected => 140.58,
204                 conf     => '1 1',
205                 field    => 'totalgste'
206             }
207         );
208     };
209
210     subtest 'Configuration 1: 1 0' => sub {
211         plan tests => 7;
212         $bookseller_module->mock(
213             'fetch',
214             sub {
215                 return { listincgst => 1, invoiceincgst => 0 };
216             }
217         );
218
219         my $biblionumber_1_0 = 44;
220         my $order_1_0        = {
221             biblionumber     => $biblionumber_1_0,
222             quantity         => 2,
223             listprice        => 82.000000,
224             unitprice        => 73.804500,
225             quantityreceived => 2,
226             basketno         => $basketno_1_1,
227             invoiceid        => $invoiceid_1_1,
228             rrp              => 82.01,
229             ecost            => 73.80,
230             gstrate          => 0.0500,
231             discount         => 10.0000,
232             datereceived     => $today
233         };
234
235         $order_1_0 = C4::Acquisition::populate_order_with_prices(
236             {
237                 order        => $order_1_0,
238                 booksellerid => 'just_something',
239                 ordering     => 1,
240             }
241         );
242
243         # Note that this configuration is *not* correct!
244         # rrp gsti should be 82 (what we inserted!)
245         # gstvalue should be 7.03 instead of 7.02
246
247         compare(
248             {
249                 got      => $order_1_0->{rrpgsti},
250                 expected => 82.01,
251                 conf     => '1 0',
252                 field    => 'rrpgsti'
253             }
254         );
255         compare(
256             {
257                 got      => $order_1_0->{rrpgste},
258                 expected => 78.10,
259                 conf     => '1 0',
260                 field    => 'rrpgste'
261             }
262         );
263         compare(
264             {
265                 got      => $order_1_0->{ecostgsti},
266                 expected => 73.80,
267                 conf     => '1 0',
268                 field    => 'ecostgsti'
269             }
270         );
271         compare(
272             {
273                 got      => $order_1_0->{ecostgste},
274                 expected => 70.29,
275                 conf     => '1 0',
276                 field    => 'ecostgste'
277             }
278         );
279         compare(
280             {
281                 got      => $order_1_0->{gstvalue},
282                 expected => 7.02,
283                 conf     => '1 0',
284                 field    => 'gstvalue'
285             }
286         );
287         compare(
288             {
289                 got      => $order_1_0->{totalgsti},
290                 expected => 147.60,
291                 conf     => '1 0',
292                 field    => 'totalgsti'
293             }
294         );
295         compare(
296             {
297                 got      => $order_1_0->{totalgste},
298                 expected => 140.58,
299                 conf     => '1 0',
300                 field    => 'totalgste'
301             }
302         );
303     };
304
305     subtest 'Configuration 1: 0 1' => sub {
306         plan tests => 7;
307         $bookseller_module->mock(
308             'fetch',
309             sub {
310                 return { listincgst => 0, invoiceincgst => 1 };
311             }
312         );
313
314         my $biblionumber_0_1 = 45;
315         my $order_0_1        = {
316             biblionumber     => $biblionumber_0_1,
317             quantity         => 2,
318             listprice        => 82.000000,
319             unitprice        => 73.800000,
320             quantityreceived => 2,
321             basketno         => $basketno_1_1,
322             invoiceid        => $invoiceid_1_1,
323             rrp              => 82.00,
324             ecost            => 73.80,
325             gstrate          => 0.0500,
326             discount         => 10.0000,
327             datereceived     => $today
328         };
329
330         $order_0_1 = C4::Acquisition::populate_order_with_prices(
331             {
332                 order        => $order_0_1,
333                 booksellerid => 'just_something',
334                 ordering     => 1,
335             }
336         );
337
338         # Note that this configuration is correct \o/
339         compare(
340             {
341                 got      => $order_0_1->{rrpgsti},
342                 expected => 86.10,
343                 conf     => '1 0',
344                 field    => 'rrpgsti'
345             }
346         );
347         compare(
348             {
349                 got      => $order_0_1->{rrpgste},
350                 expected => 82.00,
351                 conf     => '1 0',
352                 field    => 'rrpgste'
353             }
354         );
355         compare(
356             {
357                 got      => $order_0_1->{ecostgsti},
358                 expected => 77.49,
359                 conf     => '1 0',
360                 field    => 'ecostgsti'
361             }
362         );
363         compare(
364             {
365                 got      => $order_0_1->{ecostgste},
366                 expected => 73.80,
367                 conf     => '1 0',
368                 field    => 'ecostgste'
369             }
370         );
371         compare(
372             {
373                 got      => $order_0_1->{gstvalue},
374                 expected => 7.38,
375                 conf     => '1 0',
376                 field    => 'gstvalue'
377             }
378         );
379         compare(
380             {
381                 got      => $order_0_1->{totalgsti},
382                 expected => 154.98,
383                 conf     => '1 0',
384                 field    => 'totalgsti'
385             }
386         );
387         compare(
388             {
389                 got      => $order_0_1->{totalgste},
390                 expected => 147.60,
391                 conf     => '1 0',
392                 field    => 'totalgste'
393             }
394         );
395     };
396 }
397
398 sub compare {
399     my ($params) = @_;
400     is(
401         Koha::Number::Price->new( $params->{got} )->format,
402         Koha::Number::Price->new( $params->{expected} )->format,
403 "configuration $params->{conf}: $params->{field} should be correctly calculated"
404     );
405 }
406
407 # format_for_editing
408 for my $currency_format ( qw( US FR ) ) {
409     t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
410     is( Koha::Number::Price->new( 1234567 )->format_for_editing, '1234567.00', 'format_for_editing should return unformated integer part with 2 decimals' );
411     is( Koha::Number::Price->new( 1234567.89 )->format_for_editing, '1234567.89', 'format_for_editing should return unformated integer part with 2 decimals' );
412 }