Finance/Bank/Postbank_de/Account.pm

filter: Statementstmt 94.7% 143 / 151 Branchbran 84.7% 61 / 72 Conditioncond 86.4% 32 / 37 MC/DCmcdc 71.4% 15 / 21 Subroutinesub 100% 15 / 15 Podpod 57.1% 4 / 7 totaltotal 89.1% 270 / 303 CC 60 SCAR 31.7
CC
60
Cov
91%
parse_statement
41.6
transactions
14.0
__ANON__
6.0
CRAP 23.8
↑ Summary Finance/Bank/Postbank_de.pm »
1 package Finance::Bank::Postbank_de::Account;
2 14 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use Moo 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 14 our $VERSION = '0.57';
10  
11 14 has [
12 'number',
13 'balance',
14 'balance_unavailable',
15 'balance_prev',
16 'transactions_future',
17 'iban',
18 'blz',
19 'account_type',
20 'name',
21 ] => ( is => 'rw' );
22  
23 18 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
24 18 my ($orig, $class,%args) = @_;
25  
26 18 ▶ if( exists $args{ number } and exists $args{ kontonummer }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"number"} and exists $args{"kontonummer"} and $args{"number"} ne $args{"kontonummer"}) 2 16 18
Condition100%6/6
exists $args{"number"} and exists $args{"kontonummer"} and $args{"number"} ne $args{"kontonummer"}
!ll&&!rl&&r
1512
exists $args{"number"} and exists $args{"kontonummer"}
!ll&&!rl&&r
693
MC/DC100%3/3
exists $args{"number"} and exists $args{"kontonummer"} and $args{"number"} ne $args{"kontonummer"}
exists $args{"number"} exists $args{"kontonummer"} $args{"number"} ne $args{"kontonummer"}
Truth table100%4/4
A and B and C
A= exists $args{"number"}B= exists $args{"kontonummer"}C= $args{"number"} ne $args{"kontonummer"}
ABCresult
0XX0
10X0
1100
1111
27 and $args{ number } ne $args{ kontonummer } ) {
28 2 croak "'kontonummer' is '$args{kontonummer}' and 'number' is '$args{ number }'";
29 };
30 16 ▶ my $num = delete $args{number} || delete $args{kontonummer};
Condition100%3/3
delete $args{'number'} || delete $args{'kontonummer'}
l!l&&r!l&&!r
916
MC/DC100%2/2
delete $args{'number'} || delete $args{'kontonummer'}
delete $args{'number'} delete $args{'kontonummer'}
Truth table100%3/3
A || B
A= delete $args{'number'}B= delete $args{'kontonummer'}
ABresult
1X1
011
000
31 16 ▶ $args{ number } = $num
Branch: 1 branch
BranchTrueFalseTotal
if defined $num 10 6 16
32 if defined $num;
33  
34 16 $orig->( $class, %args );
35 14 };
36  
37 14 ▶{ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 14 *kontonummer = *number;
39 }
40  
41 14 our %safety_check = (
42 name => 1,
43 kontonummer => 1,
44 );
45  
46 14 our %tags = (
47 #Girokonto => [qw(Name BLZ Kontonummer IBAN)],
48 "gebuchte Ums\x{00E4}tze" => [qw(Name BLZ Kontonummer IBAN)],
49 Tagesgeldkonto => [qw(Name BLZ Kontonummer IBAN)],
50 Sparcard => [qw(Name BLZ Kontonummer IBAN)],
51 Sparkonto => [qw(Name BLZ Kontonummer IBAN)],
52 Kreditkarte => [qw(Name BLZ Kontonummer IBAN)],
53 '' => [qw(Name BLZ Kontonummer IBAN)],
54 );
55  
56 14 our %totals = (
57 "gebuchte Ums\x{00E4}tze" => [
58 [qr'^Aktueller Kontostand' => 'balance'],
59 [qr'^Summe vorgemerkter Ums.tze' => 'transactions_future'],
60 [qr'^Davon noch nicht verf.gbar' => 'balance_unavailable'],
61 ],
62 Sparcard => [[qr'Aktueller Kontostand' => 'balance'],],
63 Sparkonto => [[qr'Aktueller Kontostand' => 'balance'],],
64 Tagesgeldkonto => [[qr'Aktueller Kontostand' => 'balance'],],
65 "" => [
66 [qr'^Aktueller Kontostand' => 'balance'],
67 [qr'^Summe der Ums.tze in den n.chsten 14 Tagen' => 'transactions_future'],
68 [qr'^Davon noch nicht verf.gbar' => 'balance_unavailable'],
69 ],
70 );
71  
72 14 our %columns = (
73 qr'Buchungsdatum' => 'tradedate',
74 qr'Wertstellung' => 'valuedate',
75 qr'Umsatzart' => 'type',
76 qr'Buchungsdetails' => 'comment',
77 qr'Auftraggeber' => 'sender',
78 qr'Empf.nger' => 'receiver',
79 qr"Betrag \((?:\x{20AC}|\x{80})\)" => 'amount',
80 qr"Saldo \((?:\x{20AC}|\x{80})\)" => 'running_total',
81 );
82  
83 103 ▶sub parse_date {
Subroutine parse_date: called CC=2 SCAR=6.9
Pod undocumented
84 103 my ($self,$date) = @_;
85 103 ▶ $date =~ /^(\d{2})\.(\d{2})\.(\d{4})$/
Branch: 1 branch
BranchTrueFalseTotal
$date =~ /^(\d{2})\.(\d{2})\.(\d{4})$/ or die "Unknown date format '${date}'. A date must be in the format 'DD.MM.YYYY'\n" 100 3 103
86 or die "Unknown date format '$date'. A date must be in the format 'DD.MM.YYYY'\n";
87 100 $3.$2.$1;
88 };
89  
90 118 ▶sub parse_amount {
Subroutine parse_amount: called CC=3 SCAR=11.0
Pod undocumented
91 118 my ($self,$amount) = @_;
92 # '¿ 5.314,05'
93 118 ▶ die "String '$amount' does not look like a number"
Branch: 1 branch
BranchTrueFalseTotal
unless $amount =~ /^(-?)(?:\s*\x{20AC}\s*|\s*\x{80}\s*|\s*\x{A4}\s*)?([0-9]{1,3}(?:\.\d{3})*,\d{2})(?:\s*\x{20AC}|\s*\x{80})?$/ 3 115 118
94 unless $amount =~ /^(-?)(?:\s*\x{20AC}\s*|\s*\x{80}\s*|\s*\x{A4}\s*)?([0-9]{1,3}(?:\.\d{3})*,\d{2})(?:\s*\x{20AC}|\s*\x{80})?$/;
95 115 ▶ $amount = ($1||'') . $2;
Condition100%2/2
$1 || ''
l!l
4372
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
96 115 $amount =~ tr/.//d;
97 115 $amount =~ s/,/./;
98 115 $amount;
99 };
100  
101 3 ▶sub slurp_file {
Subroutine slurp_file: called CC=2 SCAR=7.0
Pod undocumented
102 3 my ($self,$filename) = @_;
103 3 local $/ = undef;
104 3 ▶ open my $fh, "< $filename"
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "< $filename" or croak("Couldn't read from file '${filename}' : $!") 3 0 3
105 or croak "Couldn't read from file '$filename' : $!";
106 3 binmode $fh, ':encoding(UTF-8)';
107 3 <$fh>;
108 };
109  
110 16 ▶sub parse_statement {
Subroutine parse_statement: called CC=36 SCAR=37.3
Pod documented
111 16 my ($self,%args) = @_;
112  
113 # If $self is just a string, we want to make a new class out of us
114 16 ▶ $self = $self->new
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 4 12 16
115 unless ref $self;
116 16 my $filename = $args{file};
117 16 my $raw_statement = $args{content};
118 16 ▶ if ($filename) {
Branch: 2 branches
BranchTrueFalseTotal
if ($filename) { } 4 12 16
elsif (not defined $raw_statement) { } 4 8 12
119 4 $raw_statement = $self->slurp_file($filename);
120 } elsif (! defined $raw_statement) {
121 croak "Need an account number if I have to retrieve the statement online"
122 4 ▶ unless $args{number};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"number"} 1 3 4
123 croak "Need a password if I have to retrieve the statement online"
124 3 ▶ unless exists $args{password};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"password"} 1 2 3
125 2 ▶ my $login = $args{login} || $args{number};
Condition66%2/3
$args{'login'} || $args{'number'}
l!l&&r!l&&!r
110
MC/DC0%0/2
$args{'login'} || $args{'number'}
$args{'login'} $args{'number'}
Truth table66%2/3
A || B
A= $args{'login'}B= $args{'number'}
ABresult
1X1
011
000
126  
127 2 require Finance::Bank::Postbank_de;
128 2 return Finance::Bank::Postbank_de->new( login => $login, password => $args{password}, past_days => $args{past_days} )->get_account_statement;
129 };
130  
131 11 ▶ croak "Don't know what to do with empty content"
Branch: 1 branch
BranchTrueFalseTotal
unless $raw_statement 1 10 11
132 unless $raw_statement;
133  
134 10 my @lines = split /\r?\n/, $raw_statement;
135 10 ▶ croak "No valid account statement: '$lines[0]'"
Branch: 1 branch
BranchTrueFalseTotal
unless $lines[0] =~ /^Umsatzauskunft;$/ 1 9 10
136 unless $lines[0] =~ /^Umsatzauskunft;$/;
137 9 shift @lines;
138  
139 9 my $account_type = '';
140 #my $account_type = $1;
141 #if( ! exists $tags{ $account_type }) {
142 # $account_type =~ s!([^\x00-\x7f])!sprintf '%08x', ord($1)!ge;
143 # croak "Unknown account type '$account_type' (" . (join ",",keys %tags) . ")"
144 # unless exists $tags{$account_type};
145 #};
146 #$self->account_type($account_type);
147  
148 # Name: PETRA PFIFFIG
149 #for my $tag (@{ $tags{ $self->account_type }||[] }) {
150 9 my $sep = ";";
151 9 ▶ if( $lines[0] =~ /([\t;])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[0] =~ /([\t;])/) 8 1 9
152 8 $sep = $1;
153 };
154 9 ▶ for my $tag (@{ $tags{ $account_type }||[] }) {
Condition50%1/2
$Finance::Bank::Postbank_de::Account::tags{$account_type} || []
l!l
90
MC/DC0%0/1
$Finance::Bank::Postbank_de::Account::tags{$account_type} || []
$Finance::Bank::Postbank_de::Account::tags{$account_type}
Truth table50%1/2
A
A= $Finance::Bank::Postbank_de::Account::tags{$account_type}
Aresult
11
00
155 29 ▶ $lines[0] =~ /^\Q$tag\E$sep(.*?)$sep?$/
Branch: 1 branch
BranchTrueFalseTotal
$lines[0] =~ /^\Q$tag\E$sep(.*?)$sep?$/ or croak("Field '${tag}' not found in account statement ($lines[0])") 25 4 29
156 or croak "Field '$tag' not found in account statement ($lines[0])";
157 25 my $method = lc($tag);
158 25 my $value = $1;
159  
160 # special check for special fields:
161 croak "Wrong/mixed account $method: Got '$value', expected '" . $self->$method . "'"
162 25 ▶ if (exists $safety_check{$method} and defined $self->$method and $self->$method ne $value);
Branch: 1 branch
BranchTrueFalseTotal
if exists $Finance::Bank::Postbank_de::Account::safety_check{$method} and defined $self->$method and $self->$method ne $value 1 24 25
Condition100%6/6
exists $Finance::Bank::Postbank_de::Account::safety_check{$method} and defined $self->$method and $self->$method ne $value
!ll&&!rl&&r
1771
exists $Finance::Bank::Postbank_de::Account::safety_check{$method} and defined $self->$method
!ll&&!rl&&r
1168
MC/DC100%3/3
exists $Finance::Bank::Postbank_de::Account::safety_check{$method} and defined $self->$method and $self->$method ne $value
exists $Finance::Bank::Postbank_de::Account::safety_check{$method} defined $self->$method $self->$method ne $value
Truth table100%4/4
A and B and C
A= exists $Finance::Bank::Postbank_de::Account::safety_check{$method}B= defined $self->$methodC= $self->$method ne $value
ABCresult
0XX0
10X0
1100
1111
163  
164 24 $self->$method($value);
165 24 shift @lines;
166 };
167  
168  
169 4 ▶ while ($lines[0] !~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lines[0] =~ /^\s*$/) 8 4 12
170 8 my $line = shift @lines;
171 8 my ($method,$balance);
172 8 ▶ for my $total (@{ $totals{ $account_type }||[] }) {
Condition50%1/2
$Finance::Bank::Postbank_de::Account::totals{$account_type} || []
l!l
80
MC/DC0%0/1
$Finance::Bank::Postbank_de::Account::totals{$account_type} || []
$Finance::Bank::Postbank_de::Account::totals{$account_type}
Truth table50%1/2
A
A= $Finance::Bank::Postbank_de::Account::totals{$account_type}
Aresult
11
00
173 24 my ($re,$possible_method) = @$total;
174 24 ▶ if ($line =~ /$re$sep\s*(?:(?:(\S+)\s*(?:\x{20AC}|\x{80}))|(null))$sep$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$re$sep\s*(?:(?:(\S+)\s*(?:\x{20AC}|\x{80}))|(null))$sep$/) 8 16 24
175 8 $method = $possible_method;
176 8 ▶ $balance = $1 || $2;
Condition33%1/3
$1 || $2
l!l&&r!l&&!r
800
MC/DC0%0/2
$1 || $2
$1 $2
Truth table33%1/3
A || B
A= $1B= $2
ABresult
1X1
011
000
177 8 ▶ if ($balance =~ /^(-?[0-9.,]+)\s*$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($balance =~ /^(-?[0-9.,]+)\s*$/) { } 8 0 8
elsif ('null' eq $balance) { } 0 0 0
178 8 $self->$method( ['????????',$self->parse_amount($balance)]);
179 } elsif ('null' eq $balance) {
180 0 $self->$method( ['????????',$self->parse_amount("0,00")]);
181 } else {
182 0 die "Invalid number '$balance' found for $method in '$line'";
183 };
184 };
185 };
186 8 ▶ if (! $method) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method) 0 8 8
187 0 $account_type =~ s!([^\x00-\x7f])!sprintf '%08x', ord($1)!ge;
188 0 $line =~ s!([^\x00-\x7f])!sprintf '%08x', ord($1)!ge;
189 0 croak "No summary found in account '$account_type' statement ($line)";
190 };
191 };
192  
193 4 ▶ $lines[0] =~ m!^\s*$!
Branch: 1 branch
BranchTrueFalseTotal
$lines[0] =~ /^\s*$/ or croak("Expected an empty line after the account balances, got '$lines[0]'") 4 0 4
194 or croak "Expected an empty line after the account balances, got '$lines[0]'";
195 4 shift @lines;
196  
197 # Now skip over the transactions in the future
198 4 ▶ while( $lines[0] !~ /^gebuchte Ums.tze/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lines[0] =~ /^gebuchte Ums.tze/) 28 4 32
199 28 shift @lines;
200 };
201 4 shift @lines;
202  
203 # Now parse the lines for each cashflow :
204 4 ▶ $lines[0] =~ /^Buchungsdatum${sep}Wertstellung${sep}Umsatzart/
Branch: 1 branch
BranchTrueFalseTotal
$lines[0] =~ /^Buchungsdatum${sep}Wertstellung${sep}Umsatzart/ or croak("Couldn't find start of transactions ($lines[0])") 4 0 4
205 or croak "Couldn't find start of transactions ($lines[0])";
206  
207 4 my (@fields);
208 COLUMN:
209 4 for my $col (split /$sep/, $lines[0]) {
210 32 for my $target (keys %columns) {
211 144 ▶ if ($col =~ m!^["']?$target["']?$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col =~ /^["']?${target}["']?$/) 32 112 144
212 32 push @fields, $columns{$target};
213 32 next COLUMN;
214 };
215 };
216 0 die "Unknown column '$col' in '$lines[0]'";
217 };
218 4 shift @lines;
219  
220 4 my (%convert) = (
221 tradedate => \&parse_date,
222 valuedate => \&parse_date,
223 amount => \&parse_amount,
224 running_total => \&parse_amount,
225 );
226  
227 4 my @transactions;
228 my $line;
229 4 for $line (@lines) {
230 48 ▶ next if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 48 48
231 48 my (@row) = split /$sep/, $line;
232 48 ▶ scalar @row == scalar @fields
Branch: 1 branch
BranchTrueFalseTotal
scalar @row == scalar @fields or die "Malformed cashflow ($line): Expected " . scalar(@fields) . ' entries, got ' . scalar(@row) 48 0 48
233 or die "Malformed cashflow ($line): Expected ".scalar(@fields)." entries, got ".scalar(@row);
234  
235 48 for (@row) {
236 384 ▶ $_ = $1
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*["']\s*(.*?)\s*["']\s*$/ 0 384 384
237 if /^\s*["']\s*(.*?)\s*["']\s*$/;
238 };
239  
240 48 my (%rec);
241 48 @rec{@fields} = @row;
242 48 for (keys %convert) {
243 192 $rec{$_} = $convert{$_}->($self,$rec{$_});
244 };
245  
246 48 push @transactions, \%rec;
247 };
248  
249 # Filter the transactions
250 4 $self->{transactions} = \@transactions;
251  
252 4 $self
253 };
254  
255 84 ▶sub transactions {
Subroutine transactions: called CC=14 SCAR=26.4
Pod documented
256 84 my ($self,%args) = @_;
257  
258 84 my ($start_date,$end_date);
259 84 ▶ if (exists $args{on}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'on'}) { } 33 51 84
260  
261 croak "Options 'since'+'upto' and 'on' are incompatible"
262 33 ▶ if (exists $args{since} and exists $args{upto});
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"since"} and exists $args{"upto"} 1 32 33
Condition100%3/3
exists $args{"since"} and exists $args{"upto"}
!ll&&!rl&&r
3111
MC/DC100%2/2
exists $args{"since"} and exists $args{"upto"}
exists $args{"since"} exists $args{"upto"}
Truth table100%3/3
A and B
A= exists $args{"since"}B= exists $args{"upto"}
ABresult
0X0
100
111
263 croak "Options 'since' and 'on' are incompatible"
264 32 ▶ if (exists $args{since});
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"since"} 1 31 32
265 croak "Options 'upto' and 'on' are incompatible"
266 31 ▶ if (exists $args{upto});
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"upto"} 1 30 31
267 $args{on} = strftime('%Y%m%d',localtime())
268 30 ▶ if ($args{on} eq 'today');
Branch: 1 branch
BranchTrueFalseTotal
if $args{"on"} eq "today" 1 29 30
269 30 ▶ $args{on} =~ /^\d{8}$/ or croak "Argument {on => '$args{on}'} dosen't look like a date to me.";
Branch: 1 branch
BranchTrueFalseTotal
$args{"on"} =~ /^\d{8}$/ or croak(qq[Argument {on => '$args{"on"}'} dosen't look like a date to me.]) 30 0 30
270  
271 30 $start_date = $args{on} -1;
272 30 $end_date = $args{on};
273 } else {
274 51 ▶ $start_date = $args{since} || "00000000";
Condition100%2/2
$args{'since'} || '00000000'
l!l
3714
MC/DC100%1/1
$args{'since'} || '00000000'
$args{'since'}
Truth table100%2/2
A
A= $args{'since'}
Aresult
11
00
275 51 ▶ $end_date = $args{upto} || "99999999";
Condition100%2/2
$args{'upto'} || '99999999'
l!l
942
MC/DC100%1/1
$args{'upto'} || '99999999'
$args{'upto'}
Truth table100%2/2
A
A= $args{'upto'}
Aresult
11
00
276 51 ▶ $start_date =~ /^\d{8}$/ or croak "Argument {since => '$start_date'} dosen't look like a date to me.";
Branch: 1 branch
BranchTrueFalseTotal
$start_date =~ /^\d{8}$/ or croak("Argument {since => '${start_date}'} dosen't look like a date to me.") 44 7 51
277 44 ▶ $end_date =~ /^\d{8}$/ or croak "Argument {upto => '$end_date'} dosen't look like a date to me.";
Branch: 1 branch
BranchTrueFalseTotal
$end_date =~ /^\d{8}$/ or croak("Argument {upto => '${end_date}'} dosen't look like a date to me.") 37 7 44
278 37 ▶ $start_date < $end_date or croak "The 'since' argument must be less than the 'upto' argument";
Branch: 1 branch
BranchTrueFalseTotal
$start_date < $end_date or croak("The 'since' argument must be less than the 'upto' argument") 35 2 37
279 };
280  
281 # Filter the transactions
282 65 ▶ grep { $_->{tradedate} > $start_date and $_->{tradedate} <= $end_date } @{$self->{transactions}};
Condition100%3/3
$_->{'tradedate'} > $start_date && $_->{'tradedate'} <= $end_date
!ll&&!rl&&r
380158242
MC/DC100%2/2
$_->{'tradedate'} > $start_date && $_->{'tradedate'} <= $end_date
$_->{'tradedate'} > $start_date $_->{'tradedate'} <= $end_date
Truth table100%3/3
A && B
A= $_->{'tradedate'} > $start_dateB= $_->{'tradedate'} <= $end_date
ABresult
0X0
100
111
283 };
284  
285 1 ▶sub value_dates {
Subroutine value_dates: called CC=2 SCAR=6.9
Pod documented
286 1 my ($self) = @_;
287 1 my %dates;
288 1 $dates{$_->{valuedate}} = 1 for $self->transactions();
289 1 sort keys %dates;
290 };
291  
292 1 ▶sub trade_dates {
Subroutine trade_dates: called CC=2 SCAR=6.9
Pod documented
293 1 my ($self) = @_;
294 1 my %dates;
295 1 $dates{$_->{tradedate}} = 1 for $self->transactions();
296 1 sort keys %dates;
297 };
298  
299 14 1;
300 __END__
↑ Summary Finance/Bank/Postbank_de.pm »