10 use File::Slurp qw(read_file);
 
  13   my ($parts, $from, $to, $separator) = @_;
 
  20     map  { s{^\s+|\s+$}{}g; $_ }
 
  27   my ($class, $file_name) = @_;
 
  29   my ($local_bank_code, $local_account_number, %transaction, @transactions, @lines);
 
  31   my $default_currency = substr(SL::DB::Default->get_default_currency, -1, 1);
 
  33   my $store_transaction = sub {
 
  35       push @transactions, { %transaction };
 
  40   foreach my $line (read_file($file_name)) {
 
  42     $line = Encode::decode('UTF-8', $line);
 
  46     if (@lines && ($line =~ m{^\%})) {
 
  47       $lines[-1]->[0] .= substr($line, 1);
 
  50       push @lines, [ $line, $line_number ];
 
  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+)}) {
 
  60       $local_bank_code      = $1;
 
  61       $local_account_number = $2;
 
  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
 
  67       $store_transaction->();
 
  69       my $valuta_year      = $1 * 1 + 2000;
 
  70       my $valuta_month     = $2;
 
  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;
 
  77       my $amount2          = $8 || 0;
 
  78       my $transaction_code = $9;
 
  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
 
  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';
 
  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,
 
 104     } elsif (%transaction && ($line->[0] =~ m{^:86:})) {
 
 105       if ($line->[0] =~ m{^:86:\d+\?(.+)}) {
 
 107         my %parts = map { ((substr($_, 0, 2) // '0') * 1 => substr($_, 2)) } split m{\?}, $1;
 
 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};
 
 116         $transaction{purpose} = substr($line->[0], 5);
 
 119       $store_transaction->();
 
 123   $store_transaction->();
 
 125   return @transactions;