1 package Koha::Number::Price;
3 # This file is part of Koha.
5 # Copyright 2014 BibLibre
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.
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.
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>.
22 use Number::Format qw( format_price );
24 use Koha::Acquisition::Currencies;
26 use base qw( Class::Accessor );
27 __PACKAGE__->mk_accessors(qw( value ));
30 my ( $class, $value ) = @_;
32 my $self->{value} = $value || 0;
39 my ( $self, $params ) = @_;
40 return unless defined $self->value;
42 my $format_params = $self->_format_params( $params );
44 # To avoid the system to crash, we will not format big number
45 # We divide per 100 because we want to keep the default DECIMAL_DIGITS (2)
46 # error - round() overflow. Try smaller precision or use Math::BigFloat
47 return $self->value if $self->value > Number::Format::MAX_INT/100;
49 return Number::Format->new(%$format_params)->format_price($self->value);
52 sub format_for_editing {
53 my ( $self, $params ) = @_;
54 return unless defined $self->value;
56 my $format_params = $self->_format_params( $params );
59 int_curr_symbol => '',
60 mon_thousands_sep => '',
61 mon_decimal_point => '.',
64 return Number::Format->new(%$format_params)->format_price($self->value);
68 my ( $self, $params ) = @_;
69 return unless defined $self->value;
71 my $format_params = $self->_format_params( $params );
73 return Number::Format->new(%$format_params)->unformat_number($self->value);
78 return unless defined $self->value;
80 my $format_params = $self->_format_params;
82 return Number::Format->new(%$format_params)->round($self->value);
86 my ( $self, $params ) = @_;
87 my $with_symbol = $params->{with_symbol} || 0;
88 my $p_cs_precedes = $params->{p_cs_precedes};
89 my $p_sep_by_space = $params->{p_sep_by_space};
90 my $currency = Koha::Acquisition::Currencies->get_active;
91 my $currency_format = C4::Context->preference("CurrencyFormat");
93 my $int_curr_symbol = q||;
97 int_curr_symbol => $int_curr_symbol,
98 mon_thousands_sep => ',',
100 mon_decimal_point => '.'
103 if ( $currency_format eq 'FR' ) {
104 # FIXME This test should be done for all currencies
105 $int_curr_symbol = $currency->symbol if $with_symbol;
108 decimal_point => ',',
109 int_curr_symbol => $int_curr_symbol,
110 mon_thousands_sep => ' ',
111 thousands_sep => ' ',
112 mon_decimal_point => ','
116 if ( $currency_format eq 'CH' ) {
117 $int_curr_symbol = $currency->symbol if $with_symbol;
120 decimal_point => '.',
121 int_curr_symbol => $int_curr_symbol,
122 mon_thousands_sep => '\'',
123 thousands_sep => '\'',
124 mon_decimal_point => '.'
129 $format_params{p_cs_precedes} = $p_cs_precedes if defined $p_cs_precedes;
130 $format_params{p_sep_by_space} = ( $int_curr_symbol and defined $p_sep_by_space ) ? $p_sep_by_space : 0;
132 return \%format_params;