DATEV::CSV Parameterübergabe sauber vereinheitlicht. Helper format_amount
[kivitendo-erp.git] / SL / DATEV / CSV.pm
index 9792d83..7738897 100644 (file)
@@ -7,6 +7,7 @@ use SL::DB::Datev;
 
 use Carp;
 use DateTime;
+use Encode qw(decode);
 
 
 my @kivitendo_to_datev = (
@@ -15,28 +16,28 @@ my @kivitendo_to_datev = (
                               csv_header_name => t8('Transaction Value'),
                               max_length      => 13,
                               type            => 'Value',
-                              valid_check     => sub { return (shift =~ m/^\d{1,10}(\,\d{1,2})?$/) },
+                              valid_check     => sub { my ($check) = @_; return ($check =~ m/^\d{1,10}(\,\d{1,2})?$/) },
                             },
                             {
                               kivi_datev_name => 'soll_haben_kennzeichen',
                               csv_header_name => t8('Debit/Credit Label'),
                               max_length      => 1,
                               type            => 'Text',
-                              valid_check     => sub { return (shift =~ m/^(S|H)$/) },
+                              valid_check     => sub { my ($check) = @_; return ($check =~ m/^(S|H)$/) },
                             },
                             {
                               kivi_datev_name => 'waehrung',
                               csv_header_name => t8('Transaction Value Currency Code'),
                               max_length      => 3,
                               type            => 'Text',
-                              valid_check     => sub { return (shift =~ m/^[A-Z]{3}$/) },
+                              valid_check     => sub { my ($check) = @_; return ($check =~ m/^[A-Z]{3}$/) },
                             },
                             {
                               kivi_datev_name => 'wechselkurs',
                               csv_header_name => t8('Exchange Rate'),
                               max_length      => 11,
                               type            => 'Number',
-                              valid_check     => sub { return (shift =~ m/^[0-9]*\.?[0-9]*$/) },
+                              valid_check     => sub { my ($check) = @_; return ($check =~ m/^[0-9]*\.?[0-9]*$/) },
                             },
                             {
                               kivi_datev_name => 'not yet implemented',
@@ -51,42 +52,42 @@ my @kivitendo_to_datev = (
                               csv_header_name => t8('Account'),
                               max_length      => 9, # May contain a maximum of 8 or 9 digits -> perldoc
                               type            => 'Account',
-                              valid_check     => sub { return (shift =~ m/^[0-9]{4,9}$/) },
+                              valid_check     => sub { my ($check) = @_; return ($check =~ m/^[0-9]{4,9}$/) },
                             },
                             {
                               kivi_datev_name => 'gegenkonto',
                               csv_header_name => t8('Contra Account'),
                               max_length      => 9, # May contain a maximum of 8 or 9 digits -> perldoc
                               type            => 'Account',
-                              valid_check     => sub { return (shift =~ m/^[0-9]{4,9}$/) },
+                              valid_check     => sub { my ($check) = @_; return ($check =~ m/^[0-9]{4,9}$/) },
                             },
                             {
                               kivi_datev_name => 'buchungsschluessel',
                               csv_header_name => t8('Posting Key'),
                               max_length      => 2,
                               type            => 'Text',
-                              valid_check     => sub { return (shift =~ m/^[0-9]{0,2}$/) },
+                              valid_check     => sub { my ($check) = @_; return ($check =~ m/^[0-9]{0,2}$/) },
                             },
                             {
                               kivi_datev_name => 'datum',
                               csv_header_name => t8('Invoice Date'),
                               max_length      => 4,
                               type            => 'Date',
-                              valid_check     => sub { return (shift =~ m/^[0-9]{4}$/) },
+                              valid_check     => sub { my ($check) = @_; return ($check =~ m/^[0-9]{4}$/) },
                             },
                             {
                               kivi_datev_name => 'belegfeld1',
                               csv_header_name => t8('Invoice Field 1'),
                               max_length      => 12,
                               type            => 'Text',
-                              valid_check     => sub { my $text = shift; check_encoding($text); },
+                              valid_check     => sub { my ($text) = @_; check_encoding($text); },
                             },
                             {
                               kivi_datev_name => 'not yet implemented',
                               csv_header_name => t8('Invoice Field 2'),
                              max_length      => 12,
                               type            => 'Text',
-                              valid_check     => sub { return (shift =~ m/[ -~]{1,12}/) },
+                              valid_check     => sub { my ($check) = @_; return ($check =~ m/[ -~]{1,12}/) },
                             },
                             {
                               kivi_datev_name => 'not yet implemented',
@@ -98,7 +99,7 @@ my @kivitendo_to_datev = (
                               csv_header_name => t8('Posting Text'),
                               max_length      => 60,
                               type            => 'Text',
-                              valid_check     => sub { my $text = shift; return 1 unless $text; check_encoding($text);  },
+                              valid_check     => sub { my ($text) = @_; return 1 unless $text; check_encoding($text);  },
                             },  # pos 14
                             {
                               kivi_datev_name => 'not yet implemented',
@@ -175,21 +176,21 @@ my @kivitendo_to_datev = (
                               csv_header_name => t8('Cost Center'),
                               max_length      => 8,
                               type            => 'Text',
-                              valid_check     => sub { my $text = shift; return 1 unless $text; check_encoding($text);  },
+                              valid_check     => sub { my ($text) = @_; return 1 unless $text; check_encoding($text);  },
                             }, # pos 37
                             {
                               kivi_datev_name => 'kost2',
                               csv_header_name => t8('Cost Center'),
                               max_length      => 8,
                               type            => 'Text',
-                              valid_check     => sub { my $text = shift; return 1 unless $text; check_encoding($text);  },
+                              valid_check     => sub { my ($text) = @_; return 1 unless $text; check_encoding($text);  },
                             }, # pos 38
                             {
                               kivi_datev_name => 'not yet implemented',
                               csv_header_name => t8('KOST Quantity'),
                               max_length      => 9,
                               type            => 'Number',
-                              valid_check     => sub { return (shift =~ m/^[0-9]{0,9}$/) },
+                              valid_check     => sub { my ($check) = @_; return ($check =~ m/^[0-9]{0,9}$/) },
                             }, # pos 39
                             {
                               kivi_datev_name => 'ustid',
@@ -197,7 +198,7 @@ my @kivitendo_to_datev = (
                               max_length      => 15,
                               type            => 'Text',
                               valid_check     => sub {
-                                                       my $ustid = shift;
+                                                       my ($ustid) = @_;
                                                        return 1 unless defined($ustid);
                                                        return ($ustid =~ m/^CH|^[A-Z]{2}\w{5,13}$/);
                                                      },
@@ -205,9 +206,7 @@ my @kivitendo_to_datev = (
   );
 
 sub check_encoding {
-  use Encode qw( decode );
-  # counter test: arabic doesnt work: ݐ
-  my $test = shift;
+  my ($test) = @_;
   return undef unless $test;
   if (eval {
     decode('Windows-1252', $test, Encode::FB_CROAK|Encode::LEAVE_SRC);
@@ -218,7 +217,7 @@ sub check_encoding {
 }
 
 sub kivitendo_to_datev {
-  my $self = shift;
+  my ($self) = @_;
 
   my $entries = scalar (@kivitendo_to_datev);
   push @kivitendo_to_datev, { kivi_datev_name => 'not yet implemented' } for 1 .. (116 - $entries);
@@ -243,7 +242,7 @@ sub generate_csv_header {
   my $length_of_accounts = length(SL::DB::Manager::Chart->get_first(where => [charttype => 'A'])->accno) // 4;
   my $default_curr       = SL::DB::Default->get_default_currency;
 
-  # datev metadata and the string lenght limits
+  # datev metadata and the string length limits
   my %meta_datev;
   my %meta_datev_to_valid_length = (
     beraternr   =>  7,
@@ -267,6 +266,11 @@ sub generate_csv_header {
 
   return @header;
 }
+
+sub _format_amount {
+  $::form->format_amount({ numberformat => '1000,00' }, @_);
+}
+
 1;
 
 __END__
@@ -347,6 +351,7 @@ Line 3 - n:  must contain 116 fields, a smaller subset is mandatory.
 =item check_encoding
 
 Helper function, returns true if a string is not empty and cp1252 encoded
+For example some arabic utf-8 like  ݐ  will return false
 
 =item generate_csv_header(from => 'YYYYDDMM', to => 'YYYYDDMM', locked => 0,
                           first_day_of_fiscal_year => 'YYYYDDMM')
@@ -368,4 +373,10 @@ Furthermore C<params{locked}> needs to be a boolean in number format (0|1).
 
 Returns the data structure C<@datev_data> as an array
 
+=item _format_amount
+
+Lightweight wrapper for form->format_amount.
+Expects a number in kivitendo database format and returns the same number
+in DATEV format.
+
 =back