Bugzilla – Attachment 28455 Details for
Bug 12310
Decimal separators issues in patrons payments/fines
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Accept different monetary formats as input
0001-Bug-12310-Accept-different-monetary-formats-as-input.patch (text/plain), 4.44 KB, created by
paxed
on 2014-05-23 08:39:45 UTC
(
hide
)
Description:
Accept different monetary formats as input
Filename:
MIME Type:
Creator:
paxed
Created:
2014-05-23 08:39:45 UTC
Size:
4.44 KB
patch
obsolete
>From aa504312054b6856bc2a77d9a486b3403a21f97a Mon Sep 17 00:00:00 2001 >From: Pasi Kallinen <pasi.kallinen@pttk.fi> >Date: Fri, 23 May 2014 10:14:02 +0300 >Subject: [PATCH] Bug 12310: Accept different monetary formats as input > >Adds a new module Koha::MoneyUtils and tests for it. >Adds monetary amount parsing when entering a manual invoice >for a patron. > >To test: > >1) Test different manual invoice values, note which ones > do not work correctly. Eg. when entering amount of "12,34" > it is saved as "12.00" in the system. >2) Apply patch >3) Repeat part 1, all should work correctly. >--- > C4/Accounts.pm | 5 ++++- > Koha/MoneyUtils.pm | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ > t/MoneyUtils.t | 37 +++++++++++++++++++++++++++++++++++++ > 3 files changed, 93 insertions(+), 1 deletion(-) > create mode 100644 Koha/MoneyUtils.pm > create mode 100644 t/MoneyUtils.t > >diff --git a/C4/Accounts.pm b/C4/Accounts.pm >index 117f8d9..8f4f9ce 100644 >--- a/C4/Accounts.pm >+++ b/C4/Accounts.pm >@@ -25,6 +25,7 @@ use C4::Stats; > use C4::Members; > use C4::Circulation qw(ReturnLostItem); > use C4::Log qw(logaction); >+use Koha::MoneyUtils qw(normalize_money); > > use Data::Dumper qw(Dumper); > >@@ -416,7 +417,9 @@ sub manualinvoice { > my $notifyid = 0; > my $insert; > my $accountno = getnextacctno($borrowernumber); >- my $amountleft = $amount; >+ my $amountleft = normalize_money($amount); >+ >+ $amount = normalize_money($amount); > > if ( ( $type eq 'L' ) > or ( $type eq 'F' ) >diff --git a/Koha/MoneyUtils.pm b/Koha/MoneyUtils.pm >new file mode 100644 >index 0000000..f8714d5 >--- /dev/null >+++ b/Koha/MoneyUtils.pm >@@ -0,0 +1,52 @@ >+package Koha::MoneyUtils; >+ >+# Copyright (c) 2014 Pasi Kallinen >+# This file is part of Koha. >+# >+# 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 2 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, write to the Free Software Foundation, Inc., >+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. >+ >+use strict; >+use warnings; >+use 5.010; >+use C4::Context; >+ >+use base 'Exporter'; >+use version; our $VERSION = qv('1.0.0'); >+ >+our @EXPORT = ( >+ qw( normalize_money ) >+); >+ >+=head1 normalize_money >+ >+$str = normalize_money("12,35 â¬"); >+ >+Parses a currency string, ignoring anything but numbers, commas and >+full stops. The numbers after the last comma or full stop are considered >+cents. >+ >+Returns a "normalized" amount, using full stop as the cent separator. >+ >+=cut >+ >+sub normalize_money { >+ my $s = shift; >+ $s =~ s/[^\d\.,]//g; >+ my @tmp = split(/[\.,]/, $s); >+ my $cents = ((scalar(@tmp) > 1) ? pop(@tmp) : 0); >+ return join("", @tmp).".".$cents; >+} >+ >+ >+1; >diff --git a/t/MoneyUtils.t b/t/MoneyUtils.t >new file mode 100644 >index 0000000..645b025 >--- /dev/null >+++ b/t/MoneyUtils.t >@@ -0,0 +1,37 @@ >+use strict; >+use warnings; >+use 5.010; >+ >+use C4::Context; >+use Test::More tests => 18; >+use Test::MockModule; >+ >+BEGIN { use_ok('Koha::MoneyUtils'); } >+ >+my $money_string; >+ >+my @normalize_test = ( >+ { 'orig' => '', 'norm' => '.0' }, >+ { 'orig' => '0', 'norm' => '0.0' }, >+ { 'orig' => '1', 'norm' => '1.0' }, >+ { 'orig' => '23', 'norm' => '23.0' }, >+ { 'orig' => '1.23', 'norm' => '1.23' }, >+ { 'orig' => '1,23', 'norm' => '1.23' }, >+ { 'orig' => '1.23 â¬', 'norm' => '1.23' }, >+ { 'orig' => '1,23â¬', 'norm' => '1.23' }, >+ { 'orig' => '$1.23', 'norm' => '1.23' }, >+ { 'orig' => '$ 1,23', 'norm' => '1.23' }, >+ { 'orig' => '.23', 'norm' => '.23' }, >+ { 'orig' => ',23', 'norm' => '.23' }, >+ { 'orig' => '.2', 'norm' => '.2' }, >+ { 'orig' => ',2', 'norm' => '.2' }, >+ { 'orig' => '1.23456', 'norm' => '1.23456' }, >+ { 'orig' => '1,234.56', 'norm' => '1234.56' }, >+ { 'orig' => '1 234,56', 'norm' => '1234.56' } >+ ); >+ >+ >+foreach my $test (@normalize_test) { >+ my $errstr = 'normalize_money("'.$test->{'orig'}.'") is not '.$test->{'norm'}; >+ cmp_ok(normalize_money($test->{'orig'}), 'eq', $test->{'norm'}, $errstr); >+} >-- >1.8.3.2 >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 12310
:
28455
|
60530
|
60563
|
61357
|
61603
|
61604
|
62655
|
64671
|
64672
|
64673
|
64674
|
64675
|
64678
|
64679
|
64696
|
72104
|
72213
|
72983
|
73055
|
73215
|
73546
|
73657
|
73784
|
73785
|
73873
|
73874
|
73880
|
74524
|
74525
|
74526
|
74527
|
74528
|
74529
|
75123
|
75127
|
75128
|
75129
|
75130
|
75947
|
75948
|
75949
|
75950
|
75951
|
75952
|
75954
|
75955
|
75956
|
75957
|
75958
|
75959
|
76896
|
76933
|
77971
|
77972
|
77973
|
77995
|
78017
|
78631
|
78632
|
78633
|
78634
|
78635
|
78636
|
78637
|
78638