lib/Finance/Bank/DE/NetBank.pm

filter: Statementstmt 20.5% 32 / 156 Branchbran 0.0% 0 / 46 Conditioncond 4.7% 2 / 42 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 53.3% 8 / 15 Podpod 100% 8 / 8 totaltotal 17.2% 50 / 290 CC 40 SCAR 51.8
CC
40
Cov
14%
transfer
420.0
_parse_csv
90.0
statement
42.0
CRAP 177.1
↑ Summary blib/lib/Finance/Bank/DE/NetBank.pm »
1 package Finance::Bank::DE::NetBank;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use vars qw($VERSION $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 Finance::Bank::DE::NetBank->mk_accessors(
7 qw(BASE_URL BLZ CUSTOMER_ID PASSWORD AGENT_TYPE AGENT ACCOUNT Debug));
8  
9 5 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Text::CSV_XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 $| = 1;
14  
15 5 $VERSION = "1.05_01";
16  
17 sub Version {
18 1 ▶ return $VERSION;
Subroutine Version: called CC=1 SCAR=0.0
Pod documented
19 }
20  
21 sub new {
22 4 ▶ my $proto = shift;
Subroutine new: called CC=4 SCAR=14.6
Pod documented
23 4 my %values = (
24 AGENT_TYPE => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) ",
25 BASE_URL =>
26 'https://www.netbank-money.de/netbank-barrierefrei-banking/view/',
27 BLZ => '20090500', # NetBank BLZ
28 CUSTOMER_ID => '',
29 PASSWORD => '',
30 ACCOUNT => '',
31 @_
32 );
33  
34 4 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
35 4 ▶ my $parent = ref($proto) && $proto;
Condition33%1/3
ref $proto && $proto
!ll&&!rl&&r
400
MC/DC0%0/2
ref $proto && $proto
ref $proto $proto
Truth table33%1/3
A && B
A= ref $protoB= $proto
ABresult
0X0
100
111
36  
37 4 my $self = {};
38 4 bless( $self, $class );
39  
40 4 foreach my $key ( keys %values ) {
41 24 $self->$key("$values{$key}");
42 }
43  
44 4 return $self;
45 }
46  
47 sub connect {
48 0 ▶ my $self = shift;
Subroutine connect: not called CC=1 SCAR=6.9
Pod documented
49 0 print STDERR "Method connect() is deprecated. Use only login() instead!\n";
50 0 return $self->login(@_);
51 }
52  
53 sub login {
54 0 ▶ my $self = shift;
Subroutine login: not called CC=3 SCAR=24.8
Pod documented
55 0 my %values = (
56 CUSTOMER_ID => $self->CUSTOMER_ID(),
57 PASSWORD => $self->PASSWORD(),
58 @_
59 );
60
61 0 my $url = $self->BASE_URL() . "index.jsp?blz=" . $self->BLZ();
62 0 my $agent = WWW::Mechanize->new( agent => $self->AGENT_TYPE(), );
63 0 $agent->get($url);
64 0 $self->AGENT($agent);
65  
66 0 $agent->field( "kundennummer", $values{'CUSTOMER_ID'} );
67 0 $agent->field( "pin", $values{'PASSWORD'} );
68 0 $agent->click();
69  
70 0 ▶ print STDERR Dumper( $agent->content ) if $self->Debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->Debug 0 0 0
71
72 0 ▶ if ($agent->content =~ /fieldtableerrorred/ig) {
Branch: 1 branch
BranchTrueFalseTotal
if ($agent->content =~ /fieldtableerrorred/gi) 0 0 0
73 0 return undef;
74 }
75  
76 0 return 1;
77
78 }
79  
80 sub saldo {
81 0 ▶ my $self = shift;
Subroutine saldo: not called CC=3 SCAR=24.8
Pod documented
82 0 my $data = $self->statement(@_);
83
84 0 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) { } 0 0 0
85 0 ▶ print STDERR Dumper($data) if $self->Debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->Debug 0 0 0
86 0 return $data->{'STATEMENT'}{'SALDO'};
87 } else {
88 0 return undef;
89 }
90 }
91  
92 sub statement {
93 0 ▶ my $self = shift;
Subroutine statement: not called CC=6 SCAR=37.4
Pod documented
94 0 my %values = (
95 TIMEFRAME => "30"
96 , # 1 or 30 days || "alle" = ALL || "variabel" = between START_DATE and END_DATE only
97 START_DATE => 0, # dd.mm.yyyy
98 END_DATE => 0, # dd.mm.yyyy
99 ACCOUNT => $self->ACCOUNT(),
100 @_
101 );
102  
103 # get mainpage
104 0 my $login_status = $self->login();
105 0 ▶ return undef unless $login_status;
Branch: 1 branch
BranchTrueFalseTotal
unless $login_status 0 0 0
106
107 0 my $agent = $self->AGENT();
108  
109 # If you've problems with your environmet settings activate this and "use encodings"
110 # binmode(STDOUT, ":encoding(iso-8859-15)");
111  
112 0 $agent->field( "kontonummer", $values{'ACCOUNT'} );
113 0 $agent->field( "zeitraum", $values{'TIMEFRAME'} );
114  
115 0 ▶ if ( $values{'TIMEFRAME'} eq "variabel"
Branch: 1 branch
BranchTrueFalseTotal
if ($values{"TIMEFRAME"} eq "variabel" and $values{"START_DATE"} and $values{"END_DATE"}) 0 0 0
Condition0%0/6
$values{"TIMEFRAME"} eq "variabel" and $values{"START_DATE"} and $values{"END_DATE"}
!ll&&!rl&&r
000
$values{"TIMEFRAME"} eq "variabel" and $values{"START_DATE"}
!ll&&!rl&&r
000
MC/DC0%0/3
$values{"TIMEFRAME"} eq "variabel" and $values{"START_DATE"} and $values{"END_DATE"}
$values{"TIMEFRAME"} eq "variabel" $values{"START_DATE"} $values{"END_DATE"}
Truth table0%0/4
A and B and C
A= $values{"TIMEFRAME"} eq "variabel"B= $values{"START_DATE"}C= $values{"END_DATE"}
ABCresult
0XX0
10X0
1100
1111
116 && $values{'START_DATE'}
117 && $values{'END_DATE'} )
118 {
119 0 $agent->field( "startdatum", $values{'START_DATE'} );
120 0 $agent->field( "enddatum", $values{'END_DATE'} );
121 }
122  
123 0 $agent->click();
124 0 $agent->get( $self->BASE_URL() . "/umsatzdownload.do" );
125  
126 0 my $content = $agent->content();
127 0 ▶ print STDERR Dumper($content) if $self->Debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->Debug 0 0 0
128  
129 0 my $csv_content = $self->_parse_csv($content);
130 0 return $csv_content;
131 }
132  
133 sub transfer {
134 0 ▶ my $self = shift;
Subroutine transfer: not called CC=20 SCAR=60.4
Pod documented
135 0 my %values = (
136 SENDER_ACCOUNT => $self->ACCOUNT(),
137 RECEIVER_NAME => "",
138 RECEIVER_ACCOUNT => "",
139 RECEIVER_BLZ => "",
140 RECEIVER_SAVE => "false",
141 COMMENT_1 => "",
142 COMMENT_2 => "",
143 AMOUNT => "0.00",
144 TAN => "",
145 @_
146 );
147
148 # get mainpage
149 0 my $login_status = $self->login();
150 0 ▶ return undef unless $login_status;
Branch: 1 branch
BranchTrueFalseTotal
unless $login_status 0 0 0
151  
152 0 my $agent = $self->AGENT();
153 0 my $url = $self->BASE_URL();
154  
155 0 $agent->get($url . "ueberweisung_per_heute_neu.do");
156  
157 ( $values{'AMOUNT_EURO'}, $values{'AMOUNT_CENT'} ) =
158 0 split( /\.|,/, $values{'AMOUNT'} );
159
160 0 $values{'AMOUNT_CENT'} = sprintf("%02d", $values{'AMOUNT_CENT'});
161  
162 0 $agent->field("auftraggeberKontonummer", $values{'SENDER_ACCOUNT'});
163 0 $agent->field("empfaengerName", $values{'RECEIVER_NAME'});
164 0 $agent->field("empfaengerBankleitzahl", $values{'RECEIVER_BLZ'});
165 0 $agent->field("empfaengerKontonummer", $values{'RECEIVER_ACCOUNT'});
166 0 $agent->field("betragEuro", $values{'AMOUNT_EURO'});
167 0 $agent->field("betragCent", $values{'AMOUNT_CENT'});
168 0 $agent->field("verwendungszweck1", $values{'COMMENT_1'});
169 0 $agent->field("verwendungszweck2", $values{'COMMENT_2'});
170 0 $agent->click("btnNeuSpeichern");
171  
172 # get TAN fieldname, index
173 0 my $tan_field;
174 my $tan_index;
175 0 my $tan;
176
177 0 $agent->content =~ /<label\s*for=["']*(.*?)["']\s*>(?:Tr|TAN)(?:[a-z].*?)\s?(\d+)/gmix;
178
179 0 $tan_field = $1;
180 0 $tan_index = $2;
181
182 0 ▶ print STDERR "tan_field: $tan_field, tan_index: $tan_index\n" if $self->Debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->Debug 0 0 0
183
184 0 ▶ if ($tan_field && $tan_index && ref($values{'TAN'}) eq "HASH" && $values{'TAN'}->{$tan_index}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tan_field and $tan_index and ref $values{'TAN'} eq 'HASH' and $values{'TAN'}{$tan_index}) { } 0 0 0
elsif ($tan_field and $tan_index and ref $values{'TAN'} eq 'ARRAY') { } 0 0 0
elsif ($tan_field and $tan_index and ref $values{'TAN'} eq 'CODE') { } 0 0 0
Condition0%0/9
$tan_field and $tan_index and ref $values{"TAN"} eq "HASH" and $values{"TAN"}{$tan_index}
!ll&&!rl&&r
000
$tan_field and $tan_index and ref $values{"TAN"} eq "HASH"
!ll&&!rl&&r
000
$tan_field and $tan_index
!ll&&!rl&&r
000
MC/DC0%0/4
$tan_field and $tan_index and ref $values{"TAN"} eq "HASH" and $values{"TAN"}{$tan_index}
$tan_field $tan_index ref $values{"TAN"} eq "HASH" $values{"TAN"}{$tan_index}
Truth table0%0/5
A and B and C and D
A= $tan_fieldB= $tan_indexC= ref $values{"TAN"} eq "HASH"D= $values{"TAN"}{$tan_index}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
185 0 ▶ print STDERR "METHOD: TAN HASH" if $self->Debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->Debug 0 0 0
186 0 $tan = $values{'TAN'}->{$tan_index};
187 ▶ } elsif ($tan_field && $tan_index && ref($values{'TAN'}) eq "ARRAY") {
Condition0%0/6
$tan_field and $tan_index and ref $values{"TAN"} eq "ARRAY"
!ll&&!rl&&r
000
$tan_field and $tan_index
!ll&&!rl&&r
000
MC/DC0%0/3
$tan_field and $tan_index and ref $values{"TAN"} eq "ARRAY"
$tan_field $tan_index ref $values{"TAN"} eq "ARRAY"
Truth table0%0/4
A and B and C
A= $tan_fieldB= $tan_indexC= ref $values{"TAN"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
188 0 ▶ print STDERR "METHOD: Object/Method" if $self->Debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->Debug 0 0 0
189 0 my $obj = $values{'TAN'}[0];
190 0 my $method = $values{'TAN'}[1];
191 0 eval ( $tan = $obj->$method() );
192 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
193 0 print "ERROR: Could not execute TAN method: $method";
194 0 return undef;
195 }
196 ▶ } elsif ($tan_field && $tan_index && ref($values{'TAN'}) eq "CODE") {
Condition0%0/6
$tan_field and $tan_index and ref $values{"TAN"} eq "CODE"
!ll&&!rl&&r
000
$tan_field and $tan_index
!ll&&!rl&&r
000
MC/DC0%0/3
$tan_field and $tan_index and ref $values{"TAN"} eq "CODE"
$tan_field $tan_index ref $values{"TAN"} eq "CODE"
Truth table0%0/4
A and B and C
A= $tan_fieldB= $tan_indexC= ref $values{"TAN"} eq "CODE"
ABCresult
0XX0
10X0
1100
1111
197 0 ▶ print STDERR "METHOD: CALLBACK" if $self->Debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->Debug 0 0 0
198 0 $tan = &{$values{'TAN'}}($tan_index);
199 }
200  
201 0 ▶ print STDERR " TAN: [$tan]\n" if $self->Debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->Debug 0 0 0
202
203 0 ▶ if ($tan) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tan) { } 0 0 0
204 0 $agent->field($tan_field, $tan_index);
205 0 $agent->click("btnBestaetigen");
206  
207 # lazy error checking
208 0 ▶ if ( $agent->content() =~ m|<span class="error">(.*)30017(.*)</span>| ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($agent->content =~ m[<span class="error">(.*)30017(.*)</span>]) { } 0 0 0
209 0 $agent->content() =~ m|<span class="error">(.*?)</span>|;
210 0 my $error = $1;
211 0 print "ERROR: $error";
212 0 return;
213 } else {
214 0 my $content = $agent->content();
215 0 return $agent->content();
216 }
217 } else {
218 0 print "ERROR: Could not identify requested TAN Index #";
219 0 return;
220 }
221 }
222  
223 sub logout {
224 0 ▶ my $self = shift;
Subroutine logout: not called CC=1 SCAR=6.9
Pod documented
225 0 my $agent = $self->AGENT();
226 0 my $url = $self->BASE_URL();
227 0 $agent->get( $url . "logout.do" );
228 }
229  
230 sub _parse_csv {
231 0 ▶ my $self = shift;
Subroutine _parse_csv: not called CC=9 SCAR=45.0
232 0 my $csv_content = shift;
233 0 $csv_content =~ s/\r//gmi;
234 0 $csv_content =~ s/\f//gmi;
235 0 my @lines = split( "\n", $csv_content );
236 0 my %data;
237  
238 0 my $csv = Text::CSV_XS->new(
239 {
240 sep_char => "\t",
241 binary => 1, ### german umlauts...
242 }
243 );
244  
245 0 my $line_count = 0;
246  
247 0 foreach my $line (@lines) {
248 0 my $status = $csv->parse($line);
249 0 my @columns = $csv->fields();
250 0 $line_count++;
251  
252 ### Account Details ########################
253 0 ▶ if ( $line_count > 3 && $line_count < 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_count > 3 and $line_count < 6) 0 0 0
Condition0%0/3
$line_count > 3 and $line_count < 6
!ll&&!rl&&r
000
MC/DC0%0/2
$line_count > 3 and $line_count < 6
$line_count > 3 $line_count < 6
Truth table0%0/3
A and B
A= $line_count > 3B= $line_count < 6
ABresult
0X0
100
111
254 0 $columns[0] =~ s/://;
255 0 $data{"ACCOUNT"}{ uc( $columns[0] ) } = $columns[1];
256 }
257  
258 ### Statement Details ######################
259 0 ▶ if ( $line_count == 9 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_count == 9) 0 0 0
260 0 $data{"STATEMENT"}{"START_DATE"} = $columns[0];
261 0 $data{"STATEMENT"}{"END_DATE"} = $columns[1];
262 0 $data{"STATEMENT"}{"ACCOUNT_ID"} = $columns[2];
263 0 $data{"STATEMENT"}{"SALDO"} = $columns[3];
264 0 $data{"STATEMENT"}{"WAEHRUNG"} = $columns[4];
265 }
266  
267 ### Transactions ###########################
268 0 ▶ if ( $line_count > 12 && $line_count <= $#lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_count > 12 and $line_count <= $#lines) 0 0 0
Condition0%0/3
$line_count > 12 and $line_count <= $#lines
!ll&&!rl&&r
000
MC/DC0%0/2
$line_count > 12 and $line_count <= $#lines
$line_count > 12 $line_count <= $#lines
Truth table0%0/3
A and B
A= $line_count > 12B= $line_count <= $#lines
ABresult
0X0
100
111
269 0 my $row = $line_count - 13;
270 0 $data{"TRANSACTION"}[$row]{"BUCHUNGSTAG"} = $columns[0];
271 0 $data{"TRANSACTION"}[$row]{"WERTSTELLUNGSTAG"} = $columns[1];
272 0 $data{"TRANSACTION"}[$row]{"VERWENDUNGSZWECK"} = $columns[2];
273  
274 0 $columns[3] =~ s/\.//;
275 0 $columns[3] =~ s/,/\./;
276  
277 0 $data{"TRANSACTION"}[$row]{"UMSATZ"} = $columns[3];
278 0 $data{"TRANSACTION"}[$row]{"WAEHRUNG"} = $columns[4];
279 0 ▶ $data{"TRANSACTION"}[$row]{"NOT_YET_FINISHED"} = $columns[5]
Branch: 1 branch
BranchTrueFalseTotal
if defined $columns[5] and $columns[5] =~ /^[^\s]$/gi 0 0 0
Condition0%0/3
defined $columns[5] and $columns[5] =~ /^[^\s]$/gi
!ll&&!rl&&r
000
MC/DC0%0/2
defined $columns[5] and $columns[5] =~ /^[^\s]$/gi
defined $columns[5] $columns[5] =~ /^[^\s]$/gi
Truth table0%0/3
A and B
A= defined $columns[5]B= $columns[5] =~ /^[^\s]$/gi
ABresult
0X0
100
111
280 if ( defined( $columns[5] ) && $columns[5] =~ m/^[^\s]$/ig );
281 }
282 }
283  
284 0 return \%data;
285 }
286  
287 5 1;
288 __END__
↑ Summary blib/lib/Finance/Bank/DE/NetBank.pm »