Notification/Transaction.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 145 CC 16 SCAR 50.6
CC
16
Cov
0%
from_txt
240.0
as_text
6.0
new
2.0
CRAP 157.6
1 package Finance::Bank::SK::SLSP::Notification::Transaction;
2  
3 use warnings;
4 use strict;
5 use utf8;
6  
7 our $VERSION = '0.02';
8  
9 use base 'Class::Accessor::Fast';
10  
11 __PACKAGE__->mk_accessors(qw{
12 original_text
13 }, ordered_attributes());
14  
15 sub new {
16 my ($class, %params) = @_;
17 my $self = $class->SUPER::new({ %params });
18 return $self;
19 }
20  
21 sub from_txt {
22 my ($class, $txt) = @_;
23  
24 my ($transactions, $our_account1, $our_account2, @rest) =
25 split(/_{10,}/, $txt);
26 die 'failed parsing 1' unless $transactions || $our_account1 || $our_account2;
27 my $our_account = $our_account1.$our_account2;
28 die 'failed parsing 2' unless @rest == 1 || $rest[0] eq '';
29  
30 my @transactions;
31 { # get transactions
32 my @trans_lines = split(/\r?\n/, $transactions);
33 if ($trans_lines[0] =~ m/^(\s+)\d/) {
34 my $prefix_whitespace = $1;
35 @trans_lines = map {
36 length($_) >= length($prefix_whitespace)
37 ? substr($_, length($prefix_whitespace))
38 : ''
39 } @trans_lines;
40 my $current_transaction;
41 foreach my $line (@trans_lines) {
42 if ($line =~ m/^(\d+)\s/) {
43 push(@transactions, $current_transaction)
44 if $current_transaction;
45 $current_transaction = { original_text => '' };
46 }
47 $current_transaction->{original_text} .= $line."\n";
48 }
49 push(@transactions, $current_transaction)
50 if $current_transaction;
51 }
52 }
53  
54 #parse transactions
55 foreach my $transaction (@transactions) {
56 my @lines = split(/\n/,$transaction->{original_text});
57  
58 my $info_line = shift(@lines);
59 die 'failed parsing "'.$info_line.'"'
60 unless $info_line =~ m/
61 ^\d+ \s
62 (.+?) \s+
63 (\d{6}) \s+
64 (\d{6}) \s+
65 (-?\d+\.\d{2}) $
66 /xms;
67 $transaction->{display_name} = $1;
68 $transaction->{date1} = $2;
69 $transaction->{date2} = $3;
70 $transaction->{amount} = $4;
71 $transaction->{cent_amount} = $transaction->{amount};
72 $transaction->{cent_amount} =~ s/[.]//;
73 $transaction->{cent_amount} += 0;
74 $transaction->{type} = ($transaction->{amount} > 0 ? 'credit' : 'payment');
75  
76 $transaction->{account_number} = '';
77 $transaction->{account_name} = '';
78 my $account_line = shift(@lines);
79 if ($account_line =~ m/
80 ^\s
81 (?:(\w{2} \d [^\s]{5,40}) \s)? # IBAN should be max 34 chars wide but it depends on country
82 ([^\s] .+)?
83 $
84 /xms) {
85 $transaction->{account_number} = $1;
86 $transaction->{account_name} = $2;
87 $transaction->{account_name} =~ s/\s+$//;
88 }
89  
90 my $symbols_line = shift(@lines);
91 die 'failed parsing "'.$symbols_line.'"'
92 unless $symbols_line =~ m/
93 ^\s
94 VS:(\d*) \s
95 KS:(\d*) \s
96 SS:(\d*)
97 $
98 /xms;
99 $transaction->{vs} = $1 if length($1 // '');
100 $transaction->{ks} = $2 if length($2 // '');
101 $transaction->{ss} = $3 if length($3 // '');
102  
103 @lines =
104 map { s/^\s+//;$_ }
105 map { s/\s+$//;$_ }
106 grep { $_ !~ m/^\s*$/ } @lines;
107 $transaction->{description} = join("", @lines);
108 }
109  
110 @transactions = map { $class->new(%{$_}) } @transactions;
111 return @transactions;
112  
113 }
114  
115 sub as_text {
116 my ($self) = @_;
117 my $text = '';
118 foreach my $attr ($self->ordered_attributes) {
119 $text .= $attr.': '.(defined($self->$attr) ? $self->$attr : '')."\n";
120 }
121 return $text;
122 }
123  
124 sub ordered_attributes {
125 return qw(
126 type
127 display_name
128 account_name
129 account_number
130 amount
131 cent_amount
132 date1
133 date2
134 vs
135 ks
136 ss
137 description
138 );
139 }
140  
141 1;
142  
143  
144 __END__