Bug 13499: Tidy of Auth.pm
[koha.git] / Koha / Number / Price.pm
blob6380bbca220fc5a3ab89d09f0fa3f1cdef88df98
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 qw( format_price );
23 use C4::Context;
24 use C4::Budgets qw( GetCurrency );
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 );
44 return Number::Format->new(%$format_params)->format_price($self->value);
47 sub unformat {
48 my ( $self, $params ) = @_;
49 return unless defined $self->value;
51 my $format_params = $self->_format_params( $params );
53 return Number::Format->new(%$format_params)->unformat_number($self->value);
56 sub round {
57 my ( $self ) = @_;
58 return unless defined $self->value;
60 my $format_params = $self->_format_params;
62 return Number::Format->new(%$format_params)->round($self->value);
65 sub _format_params {
66 my ( $self, $params ) = @_;
67 my $with_symbol = $params->{with_symbol} || 0;
68 my $p_cs_precedes = $params->{p_cs_precedes};
69 my $p_sep_by_space = $params->{p_sep_by_space};
70 my $currency = GetCurrency();
71 my $currency_format = C4::Context->preference("CurrencyFormat");
73 my $int_curr_symbol = q||;
74 my %format_params = (
75 int_curr_symbol => $int_curr_symbol,
76 mon_thousands_sep => ',',
77 mon_decimal_point => '.'
80 if ( $currency_format eq 'FR' ) {
81 # FIXME This test should be done for all currencies
82 $int_curr_symbol = $currency->{symbol} if $with_symbol;
83 %format_params = (
84 decimal_fill => '2',
85 decimal_point => ',',
86 int_curr_symbol => $int_curr_symbol,
87 mon_thousands_sep => ' ',
88 thousands_sep => ' ',
89 mon_decimal_point => ','
93 $format_params{p_cs_precedes} = $p_cs_precedes if defined $p_cs_precedes;
94 $format_params{p_sep_by_space} = $p_sep_by_space if defined $p_sep_by_space;
96 return \%format_params;