9 use File::Slurp qw(read_file);
12 my ($parts, $from, $to, $separator) = @_;
19 map { s{^\s+|\s+$}{}g; $_ }
26 my ($class, $file_name) = @_;
28 my ($local_bank_code, $local_account_number, %transaction, @transactions, @lines);
31 my $store_transaction = sub {
33 push @transactions, { %transaction };
38 foreach my $line (read_file($file_name)) {
40 $line = Encode::decode('UTF-8', $line);
44 if (@lines && ($line =~ m{^\%})) {
45 $lines[-1]->[0] .= substr($line, 1);
48 push @lines, [ $line, $line_number ];
52 foreach my $line (@lines) {
53 if ($line->[0] =~ m{^:25:(\d+)/(\d+)}) {
54 $local_bank_code = $1;
55 $local_account_number = $2;
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
61 $store_transaction->();
63 my $valuta_year = $1 * 1 + 2000;
64 my $valuta_month = $2;
68 my $debit_credit = $6;
71 my $amount2 = $9 || 0;
72 my $transaction_code = $10;
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
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';
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,
98 } elsif (%transaction && ($line->[0] =~ m{^:86:})) {
99 if ($line->[0] =~ m{^:86:\d+\?(.+)}) {
101 my %parts = map { ((substr($_, 0, 2) // '0') * 1 => substr($_, 2)) } split m{\?}, $1;
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};
110 $transaction{purpose} = substr($line->[0], 5);
113 $store_transaction->();
117 $store_transaction->();
119 return @transactions;