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

(-)a/Koha/Number/Price.pm (+85 lines)
Line 0 Link Here
1
package Koha::Number::Price;
2
3
# This file is part of Koha.
4
#
5
# Copyright 2014 BibLibre
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Number::Format qw( format_price );
23
use C4::Context;
24
use C4::Budgets qw( GetCurrency );
25
26
use base qw( Class::Accessor );
27
__PACKAGE__->mk_accessors(qw( value ));
28
29
sub new {
30
    my ( $class, $value ) = @_;
31
32
    my $self->{value} = $value || 0;
33
34
    bless $self, $class;
35
    return $self;
36
}
37
38
sub format {
39
    my ( $self, $params ) = @_;
40
    return unless defined $self->value;
41
42
    my $format_params = $self->_format_params( $params );
43
44
    return Number::Format->new(%$format_params)->format_price($self->value);
45
}
46
47
sub unformat {
48
    my ( $self, $params ) = @_;
49
    return unless defined $self->value;
50
51
    my $format_params = $self->_format_params( $params );
52
53
    return Number::Format->new(%$format_params)->unformat_number($self->value);
54
}
55
56
sub _format_params {
57
    my ( $self, $params ) = @_;
58
    my $with_symbol = $params->{with_symbol} || 0;
59
    my $currency        = GetCurrency();
60
    my $currency_format = C4::Context->preference("CurrencyFormat");
61
62
    my $int_curr_symbol = q||;
63
    my %format_params = (
64
        int_curr_symbol   => $int_curr_symbol,
65
        mon_thousands_sep => ',',
66
        mon_decimal_point => '.'
67
    );
68
69
    if ( $currency_format eq 'FR' ) {
70
        # FIXME This test should be done for all currencies
71
        $int_curr_symbol = $currency->{symbol} if $with_symbol;
72
        %format_params = (
73
            decimal_fill      => '2',
74
            decimal_point     => ',',
75
            int_curr_symbol   => $int_curr_symbol,
76
            mon_thousands_sep => ' ',
77
            thousands_sep     => ' ',
78
            mon_decimal_point => ','
79
        );
80
    }
81
82
    return \%format_params;
83
}
84
85
1;
(-)a/Koha/Template/Plugin/Price.pm (+33 lines)
Line 0 Link Here
1
package Koha::Template::Plugin::Price;
2
3
# This file is part of Koha.
4
#
5
# Copyright 2014 BibLibre
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Template::Plugin::Filter;
23
use base qw( Template::Plugin::Filter );
24
25
use Koha::Number::Price;
26
27
sub filter {
28
    my ( $self, $value ) = @_;
29
    $value ||= 0;
30
    return Koha::Number::Price->new( $value )->format;
31
}
32
33
1;
(-)a/t/Number/Price.t (-1 / +73 lines)
Line 0 Link Here
0
- 
1
use Modern::Perl;
2
3
use Test::More tests => 19;
4
5
use Test::MockModule;
6
use t::lib::Mocks;
7
8
use C4::Budgets;
9
my $budget_module = Test::MockModule->new('C4::Budgets');
10
my $currency;
11
$budget_module->mock( 'GetCurrency', sub { return $currency; } );
12
use_ok('Koha::Number::Price');
13
14
t::lib::Mocks::mock_preference( 'CurrencyFormat', 'US' );
15
$currency = {
16
    currency => 'USD',
17
    symbol   => '$',
18
    rate     => 1,
19
    active   => 1,
20
};
21
22
is( Koha::Number::Price->new->format,    '0.00', 'US: format 0' );
23
is( Koha::Number::Price->new(3)->format, '3.00', 'US: format 3' );
24
is( Koha::Number::Price->new(1234567890)->format,
25
    '1,234,567,890.00', 'US: format 1234567890' );
26
27
# FIXME This should be display symbol, but it was the case before the creation of this module
28
is( Koha::Number::Price->new->format( { with_symbol => 1 } ),
29
    '0.00', 'US: format 0 with symbol' );
30
is( Koha::Number::Price->new(3)->format( { with_symbol => 1 } ),
31
    '3.00', 'US: format 3 with symbol' );
32
is(
33
    Koha::Number::Price->new(1234567890)
34
      ->format( { with_symbol => 1 }, 'US: format 1234567890 with symbol' ),
35
    '1,234,567,890.00'
36
);
37
38
is( Koha::Number::Price->new->unformat,    '0', 'US: unformat 0' );
39
is( Koha::Number::Price->new(3)->unformat, '3', 'US: unformat 3' );
40
is( Koha::Number::Price->new(1234567890)->unformat,
41
    '1234567890', 'US: unformat 1234567890' );
42
43
t::lib::Mocks::mock_preference( 'CurrencyFormat', 'FR' );
44
$currency = {
45
    currency => 'EUR',
46
    symbol   => '€',
47
    rate     => 1,
48
    active   => 1,
49
};
50
51
# Actually,the price formating for France is 3,00€
52
# How put the symbol at the end with Number::Format?
53
is( Koha::Number::Price->new->format,    '0,00', 'FR: format 0' );
54
is( Koha::Number::Price->new(3)->format, '3,00', 'FR: format 3' );
55
is(
56
    Koha::Number::Price->new(1234567890)->format,
57
    '1 234 567 890,00',
58
    'FR: format 1234567890'
59
);
60
is( Koha::Number::Price->new->format( { with_symbol => 1 } ),
61
    '€0,00', 'FR: format 0 with symbol' );
62
is( Koha::Number::Price->new(3)->format( { with_symbol => 1 } ),
63
    '€3,00', 'FR: format 3 with symbol' );
64
is(
65
    Koha::Number::Price->new(1234567890)
66
      ->format( { with_symbol => 1 }, 'FR: format 123567890 with symbol' ),
67
    '€1 234 567 890,00'
68
);
69
70
is( Koha::Number::Price->new->unformat,    '0', 'FR: unformat 0' );
71
is( Koha::Number::Price->new(3)->unformat, '3', 'FR: unformat 3' );
72
is( Koha::Number::Price->new(1234567890)->unformat,
73
    '1234567890', 'FR: unformat 1234567890' );

Return to bug 12844