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 2888-2893 sub GetBiblioCountByBasketno { Link Here
2888
    return $sth->fetchrow;
2889
    return $sth->fetchrow;
2889
}
2890
}
2890
2891
2892
# This is *not* the good way to calcul prices
2893
# But it's how it works at the moment into Koha
2894
# This will be fixed later.
2895
# Note this subroutine should be moved to Koha::Acquisition::Order
2896
# Will do when a DBIC decision will be taken.
2897
sub populate_order_with_prices {
2898
    my ($params) = @_;
2899
2900
    my $order        = $params->{order};
2901
    my $booksellerid = $params->{booksellerid};
2902
    return unless $booksellerid;
2903
2904
    my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
2905
2906
    my $receiving = $params->{receiving};
2907
    my $ordering  = $params->{ordering};
2908
    my $discount  = $order->{discount};
2909
    $discount /= 100 if $discount > 1;
2910
2911
    $order->{rrp}   = Koha::Number::Price->new( $order->{rrp} )->round;
2912
    $order->{ecost} = Koha::Number::Price->new( $order->{ecost} )->round;
2913
    if ($ordering) {
2914
        if ( $bookseller->{listincgst} ) {
2915
            $order->{rrpgsti} = $order->{rrp};
2916
            $order->{rrpgste} = Koha::Number::Price->new(
2917
                $order->{rrpgsti} / ( 1 + $order->{gstrate} ) )->round;
2918
            $order->{ecostgsti} = $order->{ecost};
2919
            $order->{ecostgste} = Koha::Number::Price->new(
2920
                $order->{ecost} / ( 1 + $order->{gstrate} ) )->round;
2921
            $order->{gstvalue} = Koha::Number::Price->new(
2922
                ( $order->{ecostgsti} - $order->{ecostgste} ) *
2923
                  $order->{quantity} )->round;
2924
            $order->{totalgste} = $order->{ecostgste} * $order->{quantity};
2925
            $order->{totalgsti} = $order->{ecostgsti} * $order->{quantity};
2926
        }
2927
        else {
2928
            $order->{rrpgste} = $order->{rrp};
2929
            $order->{rrpgsti} = Koha::Number::Price->new(
2930
                $order->{rrp} * ( 1 + $order->{gstrate} ) )->round;
2931
            $order->{ecostgste} = $order->{ecost};
2932
            $order->{ecostgsti} = Koha::Number::Price->new(
2933
                $order->{ecost} * ( 1 + $order->{gstrate} ) )->round;
2934
            $order->{gstvalue} = Koha::Number::Price->new(
2935
                ( $order->{ecostgsti} - $order->{ecostgste} ) *
2936
                  $order->{quantity} )->round;
2937
            $order->{totalgste} = $order->{ecostgste} * $order->{quantity};
2938
            $order->{totalgsti} = $order->{ecostgsti} * $order->{quantity};
2939
        }
2940
    }
2941
2942
    # Not used yet
2943
    #if ($receiving) {
2944
    #    if ( $bookseller->{invoiceincgst} ) {
2945
    #        $order->{unitpricegsti} = $order->{unitprice};
2946
    #        $order->{unitpricegste} =
2947
    #          $order->{unitpricegsti} / ( 1 + $order->{gstrate} );
2948
    #    }
2949
    #    else {
2950
    #        $order->{unitpricegste} = $order->{unitprice};
2951
    #        $order->{unitpricegsti} =
2952
    #          $order->{unitpricegste} * ( 1 + $order->{gstrate} );
2953
    #    }
2954
    #    $order->{gstvalue} =
2955
    #      $order->{quantityreceived} *
2956
    #      $order->{unitpricegste} *
2957
    #      $order->{gstrate};
2958
    #}
2959
2960
    return $order;
2961
}
2962
2891
1;
2963
1;
2892
__END__
2964
__END__
2893
2965
(-)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