X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FMT940.pm;h=2a97e5e797461d5e585057a0757ab95a69a19e2e;hb=c39e831b3bceef01a851a0825ce6b19d1ddc3eda;hp=d96a9f21b48115760ebe5ef0d2a0a785abbcb0cf;hpb=789de0c020f6aa3c289c1c74c3df98f076b3eef6;p=kivitendo-erp.git diff --git a/SL/MT940.pm b/SL/MT940.pm index d96a9f21b..2a97e5e79 100644 --- a/SL/MT940.pm +++ b/SL/MT940.pm @@ -5,6 +5,7 @@ use warnings; use Data::Dumper; use DateTime; +use SL::DB::Default; use Encode; use File::Slurp qw(read_file); @@ -23,10 +24,11 @@ sub _join_entries { } sub parse { - my ($class, $file_name) = @_; + my ($class, $file_name, %params) = @_; my ($local_bank_code, $local_account_number, %transaction, @transactions, @lines); my $line_number = 0; + my $default_currency = substr(SL::DB::Default->get_default_currency, -1, 1); my $store_transaction = sub { if (%transaction) { @@ -35,27 +37,41 @@ sub parse { } }; + my ($active_field); foreach my $line (read_file($file_name)) { chomp $line; - $line = Encode::decode('UTF-8', $line); + $line = Encode::decode($params{charset} // 'UTF-8', $line); $line =~ s{\r+}{}; $line_number++; + my $current_field; + if ($line =~ m{^:(\d+[a-z]*):}i) { + $current_field = $1; + $active_field = $1; + } + if (@lines && ($line =~ m{^\%})) { $lines[-1]->[0] .= substr($line, 1); + } elsif (@lines && ($active_field eq '86') && !$current_field) { + $lines[-1]->[0] .= $line; + } else { push @lines, [ $line, $line_number ]; } } foreach my $line (@lines) { - if ($line->[0] =~ m{^:25:(\d+)/(\d+)}) { + # AT MT940 has the format :25://AT20151/00797453990/EUR + # DE MT940 has the format :25:BLZ/Konto + # https://www.bankaustria.at/files/MBS_MT940_V5107.pdf + if ($line->[0] =~ m{^:25:(?://AT)?(\d+)/(\d+)}) { + $local_bank_code = $1; $local_account_number = $2; - } elsif ($line->[0] =~ m{^:61: (\d{2}) (\d{2}) (\d{2}) (\d{2}) (\d{2}) (C|D|RC|RD) (.) (\d+) (?:, (\d*))? N (.{3}) (.*)}x) { - # 1 2 3 4 5 6 7 8 9 10 11 + } elsif ($line->[0] =~ m{^:61: (\d{2}) (\d{2}) (\d{2}) (\d{4})? (C|D|RC|RD) ([a-zA-Z]?) (\d+) (?:, (\d*))? N (.{3}) (.*)}x) { + # 1 2 3 4 5 6 7 8 9 10 # :61:2008060806CR952,N051NONREF $store_transaction->(); @@ -63,14 +79,14 @@ sub parse { my $valuta_year = $1 * 1 + 2000; my $valuta_month = $2; my $valuta_day = $3; - my $trans_month = $4; - my $trans_day = $5; - my $debit_credit = $6; - my $currency = $7; - my $amount1 = $8; - my $amount2 = $9 || 0; - my $transaction_code = $10; - my $reference = $11; + my $trans_month = $4 ? substr($4, 0, 2) : $valuta_month; + my $trans_day = $4 ? substr($4, 2, 2) : $valuta_day; + my $debit_credit = $5; + my $currency = $6 || $default_currency; + my $amount1 = $7; + my $amount2 = $8 || 0; + my $transaction_code = $9; + my $reference = $10; my $valuta_date = DateTime->new_local(year => $valuta_year, month => $valuta_month, day => $valuta_day); my $trans_date = DateTime->new_local(year => $valuta_year, month => $trans_month, day => $trans_day);