From: Jan Büren Date: Thu, 14 Sep 2017 07:53:46 +0000 (+0200) Subject: _replace_special_chars in Helper ausgelagert. X-Git-Tag: release-3.5.4~818 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=fec48603264c8adad06fbc212358633778322b56;p=kivitendo-erp.git _replace_special_chars in Helper ausgelagert. test_sepa_export in bank_transactions.t um einen Testfall hierfür erweitert. --- diff --git a/SL/DB/Helper/ReplaceSpecialChars.pm b/SL/DB/Helper/ReplaceSpecialChars.pm new file mode 100644 index 000000000..cbc677f3a --- /dev/null +++ b/SL/DB/Helper/ReplaceSpecialChars.pm @@ -0,0 +1,82 @@ +package SL::DB::Helper::ReplaceSpecialChars; + +use strict; +use utf8; + +use parent qw(Exporter); +our @EXPORT = qw(replace_special_chars); + +use Carp; +use Text::Unidecode qw(unidecode); + + + + +sub replace_special_chars { + my $text = shift; + + return unless $text; + + my %special_chars = ( + 'ä' => 'ae', + 'ö' => 'oe', + 'ü' => 'ue', + 'Ä' => 'Ae', + 'Ö' => 'Oe', + 'Ü' => 'Ue', + 'ß' => 'ss', + '&' => '+', + '`' => '\'', + ); + + map { $text =~ s/$_/$special_chars{$_}/g; } keys %special_chars; + + # for all other non ascii chars 'OLÉ S.L.' and 'Årdberg AB'! + $text = unidecode($text); + + return $text; +} + +1; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::DB::Helper::ReplaceSpecialChars - Helper functions for replacing non-ascii characaters + +=head1 SYNOPSIS + + use SL::DB::Helper::ReplaceSpecialChars qw(replace_special_chars); + my $ansi_string = replace_special_chars("Überhaupt, with Olé \x{5317}\x{4EB0}"); # hint perldoc may already convert + print $ansi_string; + # Ueberhaupt, with Ole Bei Jing + +=head1 FUNCTIONS + +=over 4 + +=item C + +Given a text string this method replaces the most common german umlaute, +transforms '&' to '+' and escapes a single quote ('). +If there are still some non-ascii chars, we use unidecode to guess +a sensible ascii presentation, C + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +M.Bunkus +J.Büren (Unidecode added) + +=cut + + diff --git a/SL/SEPA/XML.pm b/SL/SEPA/XML.pm index e4bf360d6..54df4eda6 100644 --- a/SL/SEPA/XML.pm +++ b/SL/SEPA/XML.pm @@ -12,6 +12,7 @@ use XML::Writer; use SL::Iconv; use SL::SEPA::XML::Transaction; +use SL::DB::Helper::ReplaceSpecialChars qw(replace_special_chars); sub new { my $class = shift; @@ -40,7 +41,7 @@ sub _init { croak "Missing parameter: $missing_parameter" if ($missing_parameter); croak "Missing parameter: creditor_id" if !$self->{creditor_id} && $self->{collection}; - map { $self->{$_} = $self->_replace_special_chars($self->{iconv}->convert($self->{$_})) } qw(company message_id creditor_id); + map { $self->{$_} = replace_special_chars($self->{iconv}->convert($self->{$_})) } qw(company message_id creditor_id); } sub add_transaction { @@ -54,31 +55,6 @@ sub add_transaction { return 1; } -sub _replace_special_chars { - my $self = shift; - my $text = shift; - - my %special_chars = ( - 'ä' => 'ae', - 'ö' => 'oe', - 'ü' => 'ue', - 'Ä' => 'Ae', - 'Ö' => 'Oe', - 'Ü' => 'Ue', - 'ß' => 'ss', - '&' => '+', - '`' => '\'', - ); - - map { $text =~ s/$_/$special_chars{$_}/g; } keys %special_chars; - - # for all other non ascii chars 'OLÉ S.L.' and 'Årdberg AB'! - use Text::Unidecode qw(unidecode); - $text = unidecode($text); - - return $text; -} - sub _format_amount { my $self = shift; my $amount = shift; diff --git a/SL/SEPA/XML/Transaction.pm b/SL/SEPA/XML/Transaction.pm index 83a749e37..22f661bb6 100644 --- a/SL/SEPA/XML/Transaction.pm +++ b/SL/SEPA/XML/Transaction.pm @@ -2,6 +2,8 @@ package SL::SEPA::XML::Transaction; use strict; +use SL::DB::Helper::ReplaceSpecialChars qw(replace_special_chars); + use Carp; use Encode; use List::Util qw(first); @@ -35,7 +37,7 @@ sub _init { map { $self->{$_} = $self->{sepa}->{iconv}->convert($params{$_}) } keys %params; map { $self->{$_} =~ s/\s+//g } qw(src_iban src_bic dst_iban dst_bic); - map { $self->{$_} = $self->{sepa}->_replace_special_chars($self->{$_}) } qw(company reference end_to_end_id); + map { $self->{$_} = replace_special_chars($self->{$_}) } qw(company reference end_to_end_id); } sub get { diff --git a/t/bank/bank_transactions.t b/t/bank/bank_transactions.t index a85a72f3f..0220c951e 100644 --- a/t/bank/bank_transactions.t +++ b/t/bank/bank_transactions.t @@ -1,4 +1,4 @@ -use Test::More tests => 137; +use Test::More tests => 138; use strict; @@ -119,7 +119,7 @@ sub reset_state { )->save; $customer = new_customer( - name => 'Test Customer', + name => 'Test Customer OLÉ S.L. Årdbärg AB', iban => 'DE12500105170648489890', bic => 'TESTBIC', account_number => '648489890', @@ -710,6 +710,15 @@ sub test_sepa_export { vc_depositor => $customer->depositor, amount => $ar_transaction->amount, ); + require SL::SEPA::XML; + my $sepa_xml = SL::SEPA::XML->new('company' => $customer->name, + 'creditor_id' => "id", + 'src_charset' => 'UTF-8', + 'message_id' => "test", + 'grouped' => 1, + 'collection' => 1, + ); + is($sepa_xml->{company} , 'Test Customer OLE S.L. Ardbaerg AB'); $ar_transaction->load; $bt->load;