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