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

Return to bug 12969