View | Details | Raw Unified | Return to bug 12969
Collapse All | Expand All

(-)a/C4/Acquisition.pm (+72 lines)
Lines 32-37 use C4::Templates qw(gettemplate); Link Here
32
use Koha::DateUtils qw( dt_from_string output_pref );
32
use Koha::DateUtils qw( dt_from_string output_pref );
33
use Koha::Acquisition::Order;
33
use Koha::Acquisition::Order;
34
use Koha::Acquisition::Bookseller;
34
use Koha::Acquisition::Bookseller;
35
use Koha::Number::Price;
35
36
36
use Time::localtime;
37
use Time::localtime;
37
use HTML::Entities;
38
use HTML::Entities;
Lines 2868-2873 sub GetBiblioCountByBasketno { Link Here
2868
    return $sth->fetchrow;
2869
    return $sth->fetchrow;
2869
}
2870
}
2870
2871
2872
# This is *not* the good way to calcul prices
2873
# But it's how it works at the moment into Koha
2874
# This will be fixed later.
2875
# Note this subroutine should be moved to Koha::Acquisition::Order
2876
# Will do when a DBIC decision will be taken.
2877
sub populate_order_with_prices {
2878
    my ($params) = @_;
2879
2880
    my $order        = $params->{order};
2881
    my $booksellerid = $params->{booksellerid};
2882
    return unless $booksellerid;
2883
2884
    my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
2885
2886
    my $receiving = $params->{receiving};
2887
    my $ordering  = $params->{ordering};
2888
    my $discount  = $order->{discount};
2889
    $discount /= 100 if $discount > 1;
2890
2891
    $order->{rrp}   = Koha::Number::Price->new( $order->{rrp} )->round;
2892
    $order->{ecost} = Koha::Number::Price->new( $order->{ecost} )->round;
2893
    if ($ordering) {
2894
        if ( $bookseller->{listincgst} ) {
2895
            $order->{rrpgsti} = $order->{rrp};
2896
            $order->{rrpgste} = Koha::Number::Price->new(
2897
                $order->{rrpgsti} / ( 1 + $order->{gstrate} ) )->round;
2898
            $order->{ecostgsti} = $order->{ecost};
2899
            $order->{ecostgste} = Koha::Number::Price->new(
2900
                $order->{ecost} / ( 1 + $order->{gstrate} ) )->round;
2901
            $order->{gstvalue} = Koha::Number::Price->new(
2902
                ( $order->{ecostgsti} - $order->{ecostgste} ) *
2903
                  $order->{quantity} )->round;
2904
            $order->{totalgste} = $order->{ecostgste} * $order->{quantity};
2905
            $order->{totalgsti} = $order->{ecostgsti} * $order->{quantity};
2906
        }
2907
        else {
2908
            $order->{rrpgste} = $order->{rrp};
2909
            $order->{rrpgsti} = Koha::Number::Price->new(
2910
                $order->{rrp} * ( 1 + $order->{gstrate} ) )->round;
2911
            $order->{ecostgste} = $order->{ecost};
2912
            $order->{ecostgsti} = Koha::Number::Price->new(
2913
                $order->{ecost} * ( 1 + $order->{gstrate} ) )->round;
2914
            $order->{gstvalue} = Koha::Number::Price->new(
2915
                ( $order->{ecostgsti} - $order->{ecostgste} ) *
2916
                  $order->{quantity} )->round;
2917
            $order->{totalgste} = $order->{ecostgste} * $order->{quantity};
2918
            $order->{totalgsti} = $order->{ecostgsti} * $order->{quantity};
2919
        }
2920
    }
2921
2922
    # Not used yet
2923
    #if ($receiving) {
2924
    #    if ( $bookseller->{invoiceincgst} ) {
2925
    #        $order->{unitpricegsti} = $order->{unitprice};
2926
    #        $order->{unitpricegste} =
2927
    #          $order->{unitpricegsti} / ( 1 + $order->{gstrate} );
2928
    #    }
2929
    #    else {
2930
    #        $order->{unitpricegste} = $order->{unitprice};
2931
    #        $order->{unitpricegsti} =
2932
    #          $order->{unitpricegste} * ( 1 + $order->{gstrate} );
2933
    #    }
2934
    #    $order->{gstvalue} =
2935
    #      $order->{quantityreceived} *
2936
    #      $order->{unitpricegste} *
2937
    #      $order->{gstrate};
2938
    #}
2939
2940
    return $order;
2941
}
2942
2871
1;
2943
1;
2872
__END__
2944
__END__
2873
2945
(-)a/Koha/Number/Price.pm (+9 lines)
Lines 53-58 sub unformat { Link Here
53
    return Number::Format->new(%$format_params)->unformat_number($self->value);
53
    return Number::Format->new(%$format_params)->unformat_number($self->value);
54
}
54
}
55
55
56
sub round {
57
    my ( $self ) = @_;
58
    return unless defined $self->value;
59
60
    my $format_params = $self->_format_params;
61
62
    return Number::Format->new(%$format_params)->round($self->value);
63
}
64
56
sub _format_params {
65
sub _format_params {
57
    my ( $self, $params ) = @_;
66
    my ( $self, $params ) = @_;
58
    my $with_symbol = $params->{with_symbol} || 0;
67
    my $with_symbol = $params->{with_symbol} || 0;
(-)a/t/Prices.t (-1 / +403 lines)
Line 0 Link Here
0
- 
1
use Modern::Perl;
2
use Test::More tests => 8;
3
use Test::MockModule;
4
5
use C4::Acquisition;
6
use C4::Bookseller;
7
use C4::Context;
8
9
use Koha::Number::Price;
10
11
use t::lib::Mocks;
12
13
t::lib::Mocks::mock_preference( 'gist', '0.02|0.05|0.196' );
14
15
my $bookseller_module = Test::MockModule->new('Koha::Acquisition::Bookseller');
16
17
my ( $basketno_0_0,  $basketno_1_1,  $basketno_1_0,  $basketno_0_1 );
18
my ( $invoiceid_0_0, $invoiceid_1_1, $invoiceid_1_0, $invoiceid_0_1 );
19
my $today;
20
21
for my $currency_format ( qw( US FR ) ) {
22
    t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format );
23
    subtest 'Configuration 1: 0 0' => sub {
24
        plan tests => 7;
25
        $bookseller_module->mock(
26
            'fetch',
27
            sub {
28
                return { listincgst => 0, invoiceincgst => 0 };
29
            }
30
        );
31
32
        my $biblionumber_0_0 = 42;
33
34
        my $order_0_0 = {
35
            biblionumber     => $biblionumber_0_0,
36
            quantity         => 2,
37
            listprice        => 82.000000,
38
            unitprice        => 73.80000,
39
            quantityreceived => 2,
40
            basketno         => $basketno_0_0,
41
            invoiceid        => $invoiceid_0_0,
42
            rrp              => 82.00,
43
            ecost            => 73.80,
44
            gstrate          => 0.0500,
45
            discount         => 10.0000,
46
            datereceived     => $today
47
        };
48
        $order_0_0 = C4::Acquisition::populate_order_with_prices(
49
            {
50
                order        => $order_0_0,
51
                booksellerid => 'just_something',
52
                ordering     => 1,
53
            }
54
        );
55
56
        # Note that this configuration is correct \o/
57
        compare(
58
            {
59
                got      => $order_0_0->{rrpgsti},
60
                expected => 86.10,
61
                conf     => '0 0',
62
                field    => 'rrpgsti'
63
            }
64
        );
65
        compare(
66
            {
67
                got      => $order_0_0->{rrpgste},
68
                expected => 82.00,
69
                conf     => '0 0',
70
                field    => 'rrpgsti'
71
            }
72
        );
73
        compare(
74
            {
75
                got      => $order_0_0->{ecostgsti},
76
                expected => 77.49,
77
                conf     => '0 0',
78
                field    => 'ecostgsti'
79
            }
80
        );
81
        compare(
82
            {
83
                got      => $order_0_0->{ecostgste},
84
                expected => 73.80,
85
                conf     => '0 0',
86
                field    => 'ecostgste'
87
            }
88
        );
89
        compare(
90
            {
91
                got      => $order_0_0->{gstvalue},
92
                expected => 7.38,
93
                conf     => '0 0',
94
                field    => 'gstvalue'
95
            }
96
        );
97
        compare(
98
            {
99
                got      => $order_0_0->{totalgsti},
100
                expected => 154.98,
101
                conf     => '0 0',
102
                field    => 'totalgsti'
103
            }
104
        );
105
        compare(
106
            {
107
                got      => $order_0_0->{totalgste},
108
                expected => 147.60,
109
                conf     => '0 0',
110
                field    => 'totalgste'
111
            }
112
        );
113
    };
114
115
    subtest 'Configuration 1: 1 1' => sub {
116
        plan tests => 7;
117
        $bookseller_module->mock(
118
            'fetch',
119
            sub {
120
                return { listincgst => 1, invoiceincgst => 1 };
121
            }
122
        );
123
124
        my $biblionumber_1_1 = 43;
125
        my $order_1_1        = {
126
            biblionumber     => $biblionumber_1_1,
127
            quantity         => 2,
128
            listprice        => 82.000000,
129
            unitprice        => 73.800000,
130
            quantityreceived => 2,
131
            basketno         => $basketno_1_1,
132
            invoiceid        => $invoiceid_1_1,
133
            rrp              => 82.00,
134
            ecost            => 73.80,
135
            gstrate          => 0.0500,
136
            discount         => 10.0000,
137
            datereceived     => $today
138
        };
139
140
        $order_1_1 = C4::Acquisition::populate_order_with_prices(
141
            {
142
                order        => $order_1_1,
143
                booksellerid => 'just_something',
144
                ordering     => 1,
145
            }
146
        );
147
148
        # Note that this configuration is *not* correct
149
        # gstvalue should be 7.03 instead of 7.02
150
        compare(
151
            {
152
                got      => $order_1_1->{rrpgsti},
153
                expected => 82.00,
154
                conf     => '1 1',
155
                field    => 'rrpgsti'
156
            }
157
        );
158
        compare(
159
            {
160
                got      => $order_1_1->{rrpgste},
161
                expected => 78.10,
162
                conf     => '1 1',
163
                field    => 'rrpgsti'
164
            }
165
        );
166
        compare(
167
            {
168
                got      => $order_1_1->{ecostgsti},
169
                expected => 73.80,
170
                conf     => '1 1',
171
                field    => 'ecostgsti'
172
            }
173
        );
174
        compare(
175
            {
176
                got      => $order_1_1->{ecostgste},
177
                expected => 70.29,
178
                conf     => '1 1',
179
                field    => 'ecostgste'
180
            }
181
        );
182
        compare(
183
            {
184
                got      => $order_1_1->{gstvalue},
185
                expected => 7.02,
186
                conf     => '1 1',
187
                field    => 'gstvalue'
188
            }
189
        );
190
        compare(
191
            {
192
                got      => $order_1_1->{totalgsti},
193
                expected => 147.60,
194
                conf     => '1 1',
195
                field    => 'totalgsti'
196
            }
197
        );
198
        compare(
199
            {
200
                got      => $order_1_1->{totalgste},
201
                expected => 140.58,
202
                conf     => '1 1',
203
                field    => 'totalgste'
204
            }
205
        );
206
    };
207
208
    subtest 'Configuration 1: 1 0' => sub {
209
        plan tests => 7;
210
        $bookseller_module->mock(
211
            'fetch',
212
            sub {
213
                return { listincgst => 1, invoiceincgst => 0 };
214
            }
215
        );
216
217
        my $biblionumber_1_0 = 44;
218
        my $order_1_0        = {
219
            biblionumber     => $biblionumber_1_0,
220
            quantity         => 2,
221
            listprice        => 82.000000,
222
            unitprice        => 73.804500,
223
            quantityreceived => 2,
224
            basketno         => $basketno_1_1,
225
            invoiceid        => $invoiceid_1_1,
226
            rrp              => 82.01,
227
            ecost            => 73.80,
228
            gstrate          => 0.0500,
229
            discount         => 10.0000,
230
            datereceived     => $today
231
        };
232
233
        $order_1_0 = C4::Acquisition::populate_order_with_prices(
234
            {
235
                order        => $order_1_0,
236
                booksellerid => 'just_something',
237
                ordering     => 1,
238
            }
239
        );
240
241
        # Note that this configuration is *not* correct!
242
        # rrp gsti should be 82 (what we inserted!)
243
        # gstvalue should be 7.03 instead of 7.02
244
245
        compare(
246
            {
247
                got      => $order_1_0->{rrpgsti},
248
                expected => 82.01,
249
                conf     => '1 0',
250
                field    => 'rrpgsti'
251
            }
252
        );
253
        compare(
254
            {
255
                got      => $order_1_0->{rrpgste},
256
                expected => 78.10,
257
                conf     => '1 0',
258
                field    => 'rrpgsti'
259
            }
260
        );
261
        compare(
262
            {
263
                got      => $order_1_0->{ecostgsti},
264
                expected => 73.80,
265
                conf     => '1 0',
266
                field    => 'ecostgsti'
267
            }
268
        );
269
        compare(
270
            {
271
                got      => $order_1_0->{ecostgste},
272
                expected => 70.29,
273
                conf     => '1 0',
274
                field    => 'ecostgste'
275
            }
276
        );
277
        compare(
278
            {
279
                got      => $order_1_0->{gstvalue},
280
                expected => 7.02,
281
                conf     => '1 0',
282
                field    => 'gstvalue'
283
            }
284
        );
285
        compare(
286
            {
287
                got      => $order_1_0->{totalgsti},
288
                expected => 147.60,
289
                conf     => '1 0',
290
                field    => 'totalgsti'
291
            }
292
        );
293
        compare(
294
            {
295
                got      => $order_1_0->{totalgste},
296
                expected => 140.58,
297
                conf     => '1 0',
298
                field    => 'totalgste'
299
            }
300
        );
301
    };
302
303
    subtest 'Configuration 1: 0 1' => sub {
304
        plan tests => 7;
305
        $bookseller_module->mock(
306
            'fetch',
307
            sub {
308
                return { listincgst => 0, invoiceincgst => 1 };
309
            }
310
        );
311
312
        my $biblionumber_0_1 = 45;
313
        my $order_0_1        = {
314
            biblionumber     => $biblionumber_0_1,
315
            quantity         => 2,
316
            listprice        => 82.000000,
317
            unitprice        => 73.800000,
318
            quantityreceived => 2,
319
            basketno         => $basketno_1_1,
320
            invoiceid        => $invoiceid_1_1,
321
            rrp              => 82.00,
322
            ecost            => 73.80,
323
            gstrate          => 0.0500,
324
            discount         => 10.0000,
325
            datereceived     => $today
326
        };
327
328
        $order_0_1 = C4::Acquisition::populate_order_with_prices(
329
            {
330
                order        => $order_0_1,
331
                booksellerid => 'just_something',
332
                ordering     => 1,
333
            }
334
        );
335
336
        # Note that this configuration is correct \o/
337
        compare(
338
            {
339
                got      => $order_0_1->{rrpgsti},
340
                expected => 86.10,
341
                conf     => '1 0',
342
                field    => 'rrpgsti'
343
            }
344
        );
345
        compare(
346
            {
347
                got      => $order_0_1->{rrpgste},
348
                expected => 82.00,
349
                conf     => '1 0',
350
                field    => 'rrpgsti'
351
            }
352
        );
353
        compare(
354
            {
355
                got      => $order_0_1->{ecostgsti},
356
                expected => 77.49,
357
                conf     => '1 0',
358
                field    => 'ecostgsti'
359
            }
360
        );
361
        compare(
362
            {
363
                got      => $order_0_1->{ecostgste},
364
                expected => 73.80,
365
                conf     => '1 0',
366
                field    => 'ecostgste'
367
            }
368
        );
369
        compare(
370
            {
371
                got      => $order_0_1->{gstvalue},
372
                expected => 7.38,
373
                conf     => '1 0',
374
                field    => 'gstvalue'
375
            }
376
        );
377
        compare(
378
            {
379
                got      => $order_0_1->{totalgsti},
380
                expected => 154.98,
381
                conf     => '1 0',
382
                field    => 'totalgsti'
383
            }
384
        );
385
        compare(
386
            {
387
                got      => $order_0_1->{totalgste},
388
                expected => 147.60,
389
                conf     => '1 0',
390
                field    => 'totalgste'
391
            }
392
        );
393
    };
394
}
395
396
sub compare {
397
    my ($params) = @_;
398
    is(
399
        Koha::Number::Price->new( $params->{got} )->format,
400
        Koha::Number::Price->new( $params->{expected} )->format,
401
"configuration $params->{conf}: $params->{field} should be correctly calculated"
402
    );
403
}

Return to bug 12969