Bug 4078: Add the ability to display and configure the symbol for prices
[koha.git] / Koha / Number / Price.pm
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 Koha::Acquisition::Currencies;
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     # 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;
48
49     return Number::Format->new(%$format_params)->format_price($self->value);
50 }
51
52 sub format_for_editing {
53     my ( $self, $params ) = @_;
54     return unless defined $self->value;
55
56     my $format_params = $self->_format_params( $params );
57     $format_params = {
58         %$format_params,
59         int_curr_symbol   => '',
60         mon_thousands_sep => '',
61         mon_decimal_point => '.',
62     };
63
64     return Number::Format->new(%$format_params)->format_price($self->value);
65 }
66
67 sub unformat {
68     my ( $self, $params ) = @_;
69     return unless defined $self->value;
70
71     my $format_params = $self->_format_params( $params );
72
73     return Number::Format->new(%$format_params)->unformat_number($self->value);
74 }
75
76 sub round {
77     my ( $self ) = @_;
78     return unless defined $self->value;
79
80     my $format_params = $self->_format_params;
81
82     return Number::Format->new(%$format_params)->round($self->value);
83 }
84
85 sub _format_params {
86     my ( $self, $params ) = @_;
87     my $with_symbol = $params->{with_symbol} || 0;
88     my $p_cs_precedes = $params->{p_cs_precedes};
89     my $currency        = Koha::Acquisition::Currencies->get_active;
90     my $currency_format = C4::Context->preference("CurrencyFormat");
91
92     my $int_curr_symbol = $with_symbol ? $currency->symbol : q||;
93     my %format_params = (
94         decimal_fill      => '2',
95         decimal_point     => '.',
96         int_curr_symbol   => $int_curr_symbol,
97         mon_thousands_sep => ',',
98         thousands_sep     => ',',
99         mon_decimal_point => '.'
100     );
101
102     if ( $currency_format eq 'FR' ) {
103         %format_params = (
104             decimal_fill      => '2',
105             decimal_point     => ',',
106             int_curr_symbol   => $int_curr_symbol,
107             mon_thousands_sep => ' ',
108             thousands_sep     => ' ',
109             mon_decimal_point => ','
110         );
111     }
112
113     if ( $currency_format eq 'CH' ) {
114         %format_params = (
115             decimal_fill      => '2',
116             decimal_point     => '.',
117             int_curr_symbol   => $int_curr_symbol,
118             mon_thousands_sep => '\'',
119             thousands_sep     => '\'',
120             mon_decimal_point => '.'
121         );
122     }
123
124
125     $format_params{p_cs_precedes}  = $p_cs_precedes  if defined $p_cs_precedes;
126     $format_params{p_sep_by_space} = $currency->p_sep_by_space ? 1 : 0;
127
128     return \%format_params;
129 }
130
131 1;