From e30f711014e1744233114e3decb1771d8f6d628c Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@biblibre.com>
Date: Thu, 28 Aug 2014 16:19:17 +0200
Subject: [PATCH] Bug 12844: New module to manage prices into Koha
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch introduces a new module to manage prices into Koha and
especially the acquisition module.

How to use is:
1/ You can use it in a perl script/module:
  my $price = Koha::Number::Price->new(3);
  $price->format; # Will display 3.00 (or 3,00 depending on the CurrencyFormat syspref).
  $price->format({with_symbol => 1}); # Will display €3.00 (or [$]3,00 depending on the CurrencyFormat syspref).

2/ But this module is usefull to display the price from a template file.
  [% my_price | Price %]
---
 Koha/Number/Price.pm          | 85 +++++++++++++++++++++++++++++++++++++++++++
 Koha/Template/Plugin/Price.pm | 33 +++++++++++++++++
 t/Number/Price.t              | 73 +++++++++++++++++++++++++++++++++++++
 3 files changed, 191 insertions(+)
 create mode 100644 Koha/Number/Price.pm
 create mode 100644 Koha/Template/Plugin/Price.pm
 create mode 100644 t/Number/Price.t

diff --git a/Koha/Number/Price.pm b/Koha/Number/Price.pm
new file mode 100644
index 0000000..2fb9c54
--- /dev/null
+++ b/Koha/Number/Price.pm
@@ -0,0 +1,85 @@
+package Koha::Number::Price;
+
+# This file is part of Koha.
+#
+# Copyright 2014 BibLibre
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Number::Format qw( format_price );
+use C4::Context;
+use C4::Budgets qw( GetCurrency );
+
+use base qw( Class::Accessor );
+__PACKAGE__->mk_accessors(qw( value ));
+
+sub new {
+    my ( $class, $value ) = @_;
+
+    my $self->{value} = $value || 0;
+
+    bless $self, $class;
+    return $self;
+}
+
+sub format {
+    my ( $self, $params ) = @_;
+    return unless defined $self->value;
+
+    my $format_params = $self->_format_params( $params );
+
+    return Number::Format->new(%$format_params)->format_price($self->value);
+}
+
+sub unformat {
+    my ( $self, $params ) = @_;
+    return unless defined $self->value;
+
+    my $format_params = $self->_format_params( $params );
+
+    return Number::Format->new(%$format_params)->unformat_number($self->value);
+}
+
+sub _format_params {
+    my ( $self, $params ) = @_;
+    my $with_symbol = $params->{with_symbol} || 0;
+    my $currency        = GetCurrency();
+    my $currency_format = C4::Context->preference("CurrencyFormat");
+
+    my $int_curr_symbol = q||;
+    my %format_params = (
+        int_curr_symbol   => $int_curr_symbol,
+        mon_thousands_sep => ',',
+        mon_decimal_point => '.'
+    );
+
+    if ( $currency_format eq 'FR' ) {
+        # FIXME This test should be done for all currencies
+        $int_curr_symbol = $currency->{symbol} if $with_symbol;
+        %format_params = (
+            decimal_fill      => '2',
+            decimal_point     => ',',
+            int_curr_symbol   => $int_curr_symbol,
+            mon_thousands_sep => ' ',
+            thousands_sep     => ' ',
+            mon_decimal_point => ','
+        );
+    }
+
+    return \%format_params;
+}
+
+1;
diff --git a/Koha/Template/Plugin/Price.pm b/Koha/Template/Plugin/Price.pm
new file mode 100644
index 0000000..f5d2a42
--- /dev/null
+++ b/Koha/Template/Plugin/Price.pm
@@ -0,0 +1,33 @@
+package Koha::Template::Plugin::Price;
+
+# This file is part of Koha.
+#
+# Copyright 2014 BibLibre
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Template::Plugin::Filter;
+use base qw( Template::Plugin::Filter );
+
+use Koha::Number::Price;
+
+sub filter {
+    my ( $self, $value ) = @_;
+    $value ||= 0;
+    return Koha::Number::Price->new( $value )->format;
+}
+
+1;
diff --git a/t/Number/Price.t b/t/Number/Price.t
new file mode 100644
index 0000000..edb6d53
--- /dev/null
+++ b/t/Number/Price.t
@@ -0,0 +1,73 @@
+use Modern::Perl;
+
+use Test::More tests => 19;
+
+use Test::MockModule;
+use t::lib::Mocks;
+
+use C4::Budgets;
+my $budget_module = Test::MockModule->new('C4::Budgets');
+my $currency;
+$budget_module->mock( 'GetCurrency', sub { return $currency; } );
+use_ok('Koha::Number::Price');
+
+t::lib::Mocks::mock_preference( 'CurrencyFormat', 'US' );
+$currency = {
+    currency => 'USD',
+    symbol   => '$',
+    rate     => 1,
+    active   => 1,
+};
+
+is( Koha::Number::Price->new->format,    '0.00', 'US: format 0' );
+is( Koha::Number::Price->new(3)->format, '3.00', 'US: format 3' );
+is( Koha::Number::Price->new(1234567890)->format,
+    '1,234,567,890.00', 'US: format 1234567890' );
+
+# FIXME This should be display symbol, but it was the case before the creation of this module
+is( Koha::Number::Price->new->format( { with_symbol => 1 } ),
+    '0.00', 'US: format 0 with symbol' );
+is( Koha::Number::Price->new(3)->format( { with_symbol => 1 } ),
+    '3.00', 'US: format 3 with symbol' );
+is(
+    Koha::Number::Price->new(1234567890)
+      ->format( { with_symbol => 1 }, 'US: format 1234567890 with symbol' ),
+    '1,234,567,890.00'
+);
+
+is( Koha::Number::Price->new->unformat,    '0', 'US: unformat 0' );
+is( Koha::Number::Price->new(3)->unformat, '3', 'US: unformat 3' );
+is( Koha::Number::Price->new(1234567890)->unformat,
+    '1234567890', 'US: unformat 1234567890' );
+
+t::lib::Mocks::mock_preference( 'CurrencyFormat', 'FR' );
+$currency = {
+    currency => 'EUR',
+    symbol   => '€',
+    rate     => 1,
+    active   => 1,
+};
+
+# Actually,the price formating for France is 3,00€
+# How put the symbol at the end with Number::Format?
+is( Koha::Number::Price->new->format,    '0,00', 'FR: format 0' );
+is( Koha::Number::Price->new(3)->format, '3,00', 'FR: format 3' );
+is(
+    Koha::Number::Price->new(1234567890)->format,
+    '1 234 567 890,00',
+    'FR: format 1234567890'
+);
+is( Koha::Number::Price->new->format( { with_symbol => 1 } ),
+    '€0,00', 'FR: format 0 with symbol' );
+is( Koha::Number::Price->new(3)->format( { with_symbol => 1 } ),
+    '€3,00', 'FR: format 3 with symbol' );
+is(
+    Koha::Number::Price->new(1234567890)
+      ->format( { with_symbol => 1 }, 'FR: format 123567890 with symbol' ),
+    '€1 234 567 890,00'
+);
+
+is( Koha::Number::Price->new->unformat,    '0', 'FR: unformat 0' );
+is( Koha::Number::Price->new(3)->unformat, '3', 'FR: unformat 3' );
+is( Koha::Number::Price->new(1234567890)->unformat,
+    '1234567890', 'FR: unformat 1234567890' );
-- 
2.0.0.rc2