Bug 26725: (QA follow-up) Add line break and 'the' for readability
[koha.git] / Koha / Number / Price.pm
blobe9a6aa748e3192629f5649becefb7646fbb99796
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>.
20 use Modern::Perl;
22 use Number::Format;
23 use C4::Context;
24 use Koha::Acquisition::Currencies;
26 use base qw( Class::Accessor );
27 __PACKAGE__->mk_accessors(qw( value ));
29 sub new {
30 my ( $class, $value ) = @_;
32 my $self->{value} = $value || 0;
34 bless $self, $class;
35 return $self;
38 sub format {
39 my ( $self, $params ) = @_;
40 return unless defined $self->value;
42 my $format_params = $self->_format_params( $params );
43 # To avoid the system to crash, we will not format big number
44 # We divide per 100 because we want to keep the default DECIMAL_DIGITS (2)
45 # error - round() overflow. Try smaller precision or use Math::BigFloat
46 return $self->value if abs($self->value) > Number::Format::MAX_INT/100;
48 return Number::Format->new(%$format_params)->format_price($self->value);
51 sub format_for_editing {
52 my ( $self, $params ) = @_;
53 return unless defined $self->value;
55 my $format_params = $self->_format_params( $params );
56 $format_params = {
57 %$format_params,
58 int_curr_symbol => '',
59 mon_thousands_sep => '',
60 mon_decimal_point => '.',
63 # To avoid the system to crash, we will not format big number
64 # We divide per 100 because we want to keep the default DECIMAL_DIGITS (2)
65 # error - round() overflow. Try smaller precision or use Math::BigFloat
66 return $self->value if $self->value > Number::Format::MAX_INT/100;
68 return Number::Format->new(%$format_params)->format_price($self->value);
71 sub unformat {
72 my ( $self, $params ) = @_;
73 return unless defined $self->value;
75 my $format_params = $self->_format_params( $params );
77 return Number::Format->new(%$format_params)->unformat_number($self->value);
80 sub round {
81 my ( $self ) = @_;
82 return unless defined $self->value;
84 my $format_params = $self->_format_params;
86 # To avoid the system to crash, we will not format big number
87 # We divide per 100 because we want to keep the default DECIMAL_DIGITS (2)
88 # error - round() overflow. Try smaller precision or use Math::BigFloat
89 return $self->value if $self->value > Number::Format::MAX_INT/100;
91 return Number::Format->new(%$format_params)->round($self->value);
94 sub _format_params {
95 my ( $self, $params ) = @_;
96 my $with_symbol = $params->{with_symbol} || 0;
97 my $p_cs_precedes = $params->{p_cs_precedes};
98 my $currency = Koha::Acquisition::Currencies->get_active;
99 my $currency_format = C4::Context->preference("CurrencyFormat");
101 my $int_curr_symbol = ( $with_symbol and $currency ) ? $currency->symbol : q||;
102 my %format_params = (
103 decimal_fill => '2',
104 decimal_point => '.',
105 int_curr_symbol => $int_curr_symbol,
106 mon_thousands_sep => ',',
107 thousands_sep => ',',
108 mon_decimal_point => '.'
111 if ( $currency_format eq 'FR' ) {
112 %format_params = (
113 decimal_fill => '2',
114 decimal_point => ',',
115 int_curr_symbol => $int_curr_symbol,
116 mon_thousands_sep => ' ',
117 thousands_sep => ' ',
118 mon_decimal_point => ','
122 if ( $currency_format eq 'CH' ) {
123 %format_params = (
124 decimal_fill => '2',
125 decimal_point => '.',
126 int_curr_symbol => $int_curr_symbol,
127 mon_thousands_sep => '\'',
128 thousands_sep => '\'',
129 mon_decimal_point => '.'
134 $format_params{p_cs_precedes} = $p_cs_precedes if defined $p_cs_precedes;
135 $format_params{p_sep_by_space} = ( $currency and $currency->p_sep_by_space ) ? 1 : 0;
137 return \%format_params;