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

(-)a/C4/Accounts.pm (-1 / +4 lines)
Lines 25-30 use C4::Stats; Link Here
25
use C4::Members;
25
use C4::Members;
26
use C4::Circulation qw(ReturnLostItem);
26
use C4::Circulation qw(ReturnLostItem);
27
use C4::Log qw(logaction);
27
use C4::Log qw(logaction);
28
use Koha::MoneyUtils qw(normalize_money);
28
29
29
use Data::Dumper qw(Dumper);
30
use Data::Dumper qw(Dumper);
30
31
Lines 416-422 sub manualinvoice { Link Here
416
    my $notifyid = 0;
417
    my $notifyid = 0;
417
    my $insert;
418
    my $insert;
418
    my $accountno  = getnextacctno($borrowernumber);
419
    my $accountno  = getnextacctno($borrowernumber);
419
    my $amountleft = $amount;
420
    my $amountleft = normalize_money($amount);
421
422
    $amount = normalize_money($amount);
420
423
421
    if (   ( $type eq 'L' )
424
    if (   ( $type eq 'L' )
422
        or ( $type eq 'F' )
425
        or ( $type eq 'F' )
(-)a/Koha/MoneyUtils.pm (+52 lines)
Line 0 Link Here
1
package Koha::MoneyUtils;
2
3
# Copyright (c) 2014 Pasi Kallinen
4
# This file is part of Koha.
5
#
6
# Koha is free software; you can redistribute it and/or modify it under the
7
# terms of the GNU General Public License as published by the Free Software
8
# Foundation; either version 2 of the License, or (at your option) any later
9
# version.
10
#
11
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License along with
16
# Koha; if not, write to the Free Software Foundation, Inc.,
17
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19
use strict;
20
use warnings;
21
use 5.010;
22
use C4::Context;
23
24
use base 'Exporter';
25
use version; our $VERSION = qv('1.0.0');
26
27
our @EXPORT = (
28
    qw( normalize_money )
29
);
30
31
=head1 normalize_money
32
33
$str = normalize_money("12,35 €");
34
35
Parses a currency string, ignoring anything but numbers, commas and
36
full stops. The numbers after the last comma or full stop are considered
37
cents.
38
39
Returns a "normalized" amount, using full stop as the cent separator.
40
41
=cut
42
43
sub normalize_money {
44
    my $s = shift;
45
    $s =~ s/[^\d\.,]//g;
46
    my @tmp = split(/[\.,]/, $s);
47
    my $cents = ((scalar(@tmp) > 1) ? pop(@tmp) : 0);
48
    return join("", @tmp).".".$cents;
49
}
50
51
52
1;
(-)a/t/MoneyUtils.t (-1 / +37 lines)
Line 0 Link Here
0
- 
1
use strict;
2
use warnings;
3
use 5.010;
4
5
use C4::Context;
6
use Test::More tests => 18;
7
use Test::MockModule;
8
9
BEGIN { use_ok('Koha::MoneyUtils'); }
10
11
my $money_string;
12
13
my @normalize_test = (
14
    { 'orig' => '',        'norm' => '.0' },
15
    { 'orig' => '0',        'norm' => '0.0' },
16
    { 'orig' => '1',        'norm' => '1.0' },
17
    { 'orig' => '23',       'norm' => '23.0' },
18
    { 'orig' => '1.23',     'norm' => '1.23' },
19
    { 'orig' => '1,23',     'norm' => '1.23' },
20
    { 'orig' => '1.23 €',   'norm' => '1.23' },
21
    { 'orig' => '1,23€',    'norm' => '1.23' },
22
    { 'orig' => '$1.23',    'norm' => '1.23' },
23
    { 'orig' => '$ 1,23',   'norm' => '1.23' },
24
    { 'orig' => '.23',      'norm' => '.23' },
25
    { 'orig' => ',23',      'norm' => '.23' },
26
    { 'orig' => '.2',       'norm' => '.2' },
27
    { 'orig' => ',2',       'norm' => '.2' },
28
    { 'orig' => '1.23456',  'norm' => '1.23456' },
29
    { 'orig' => '1,234.56', 'norm' => '1234.56' },
30
    { 'orig' => '1 234,56', 'norm' => '1234.56' }
31
    );
32
33
34
foreach my $test (@normalize_test) {
35
    my $errstr = 'normalize_money("'.$test->{'orig'}.'") is not '.$test->{'norm'};
36
    cmp_ok(normalize_money($test->{'orig'}), 'eq', $test->{'norm'}, $errstr);
37
}

Return to bug 12310