2fbbad696390210a69474f4d319afe1e2c7101c7
[kivitendo-erp.git] / SL / MT940.pm
1 package SL::MT940;
2
3 use strict;
4 use warnings;
5
6 use Data::Dumper;
7 use DateTime;
8 use SL::DB::Default;
9 use Encode;
10 use File::Slurp qw(read_file);
11
12 sub _join_entries {
13   my ($parts, $from, $to, $separator) = @_;
14
15   $separator //= ' ';
16
17   return
18     join $separator,
19     grep { $_ }
20     map  { s{^\s+|\s+$}{}g; $_ }
21     grep { $_ }
22     map  { $parts->{$_} }
23     ($from..$to);
24 }
25
26 sub parse {
27   my ($class, $file_name) = @_;
28
29   my ($local_bank_code, $local_account_number, %transaction, @transactions, @lines);
30   my $line_number = 0;
31   my $default_currency = substr(SL::DB::Default->get_default_currency, -1, 1);
32
33   my $store_transaction = sub {
34     if (%transaction) {
35       push @transactions, { %transaction };
36       %transaction = ();
37     }
38   };
39
40   foreach my $line (read_file($file_name)) {
41     chomp $line;
42     $line = Encode::decode('UTF-8', $line);
43     $line =~ s{\r+}{};
44     $line_number++;
45
46     if (@lines && ($line =~ m{^\%})) {
47       $lines[-1]->[0] .= substr($line, 1);
48
49     } else {
50       push @lines, [ $line, $line_number ];
51     }
52   }
53
54   foreach my $line (@lines) {
55     # AT MT940 has the format  :25://AT20151/00797453990/EUR
56     # DE MT940 has the format  :25:BLZ/Konto
57     # https://www.bankaustria.at/files/MBS_MT940_V5107.pdf
58     if ($line->[0] =~ m{^:25:(?://AT)?(\d+)/(\d+)}) {
59
60       $local_bank_code      = $1;
61       $local_account_number = $2;
62
63     } 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) {
64       #                            1       2       3       4        5           6           7          8         9      10
65       # :61:2008060806CR952,N051NONREF
66
67       $store_transaction->();
68
69       my $valuta_year      = $1 * 1 + 2000;
70       my $valuta_month     = $2;
71       my $valuta_day       = $3;
72       my $trans_month      = $4 ? substr($4, 0, 2) : $valuta_month;
73       my $trans_day        = $4 ? substr($4, 2, 2) : $valuta_day;
74       my $debit_credit     = $5;
75       my $currency         = $6 || $default_currency;
76       my $amount1          = $7;
77       my $amount2          = $8 || 0;
78       my $transaction_code = $9;
79       my $reference        = $10;
80
81       my $valuta_date      = DateTime->new_local(year => $valuta_year, month => $valuta_month, day => $valuta_day);
82       my $trans_date       = DateTime->new_local(year => $valuta_year, month => $trans_month,  day => $trans_day);
83       my $diff             = $valuta_date->subtract_datetime($trans_date);
84       my $trans_year_diff  = $diff->months < 6           ?  0
85                            : $valuta_date  > $trans_date ?  1
86                            :                               -1;
87       $trans_date          = DateTime->new_local(year => $valuta_year + $trans_year_diff, month => $trans_month,  day => $trans_day);
88       my $sign             = ($debit_credit eq 'D') || ($debit_credit eq 'RC') ? -1 : 1;
89       $reference           =~ s{//.*}{};
90       $reference           = '' if $reference eq 'NONREF';
91
92       %transaction = (
93         line_number          => $line->[1],
94         currency             => $currency,
95         valutadate           => $valuta_date,
96         transdate            => $trans_date,
97         amount               => ($amount1 * 1 + ($amount2 / (10 ** length($amount2))))* $sign,
98         reference            => $reference,
99         transaction_code     => $transaction_code,
100         local_bank_code      => $local_bank_code,
101         local_account_number => $local_account_number,
102       );
103
104     } elsif (%transaction && ($line->[0] =~ m{^:86:})) {
105       if ($line->[0] =~ m{^:86:\d+\?(.+)}) {
106         # structured
107         my %parts = map { ((substr($_, 0, 2) // '0') * 1 => substr($_, 2)) } split m{\?}, $1;
108
109         $transaction{purpose}               = _join_entries(\%parts, 20, 29);
110         $transaction{remote_name}           = _join_entries(\%parts, 32, 33, '');
111         $transaction{remote_bank_code}      = $parts{30};
112         $transaction{remote_account_number} = $parts{31};
113
114       } else {
115         # unstructured
116         $transaction{purpose} = substr($line->[0], 5);
117       }
118
119       $store_transaction->();
120     }
121   }
122
123   $store_transaction->();
124
125   return @transactions;
126 }
127
128 1;