ptsb_util.pm untested

filter: Statementstmt 0.0% 0 / 296 Branchbran 0.0% 0 / 108 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 504 CC 55 SCAR 59.7
CC
55
Cov
0%
statement
930.0
parse_configfile
110.0
validate_options
56.0
CRAP 390.0
1 package ptsb_util;
2  
3 =head1 NAME
4  
5 ptsb_util - functions used by the Finance::Bank::IE::PermanentTSB CLI
6  
7 This is a package which contains a set of functions used by the CLI utility.
8  
9 =cut
10  
11 use Getopt::Long;
12 use Date::Calc qw(check_date);
13 use Switch;
14 use File::Copy;
15 use Data::Dumper;
16 use FindBin;
17 use lib "$FindBin::RealBin/lib";
18  
19 use Finance::Bank::IE::PermanentTSB;
20  
21 use strict;
22 use warnings;
23  
24 sub usage {
25 use Pod::Usage;
26 pod2usage(-verbose =>1);
27 }
28  
29 # parse config file to retrieve acc_no, password and pan
30 sub parse_configfile {
31  
32 my $cfgfile = shift;
33  
34 my $gpg = `which gpg`;
35 chop $gpg;
36  
37 if($gpg =~ /not found/ or not -x $gpg) {
38 print "You need to install and use GnuPG to secure your config file\n";
39 print "Please see the documentation on \n".
40 "http://search.cpan.org/~pallotron/Finance-Bank-IE-PermanentTSB/ptsb\n";
41 exit -1;
42 }
43  
44 # encryption dance
45 # use the 'file' command to check the cfgfile
46 my $res = `file $cfgfile`;
47 if($res !~ /GPG encrypted data/is) {
48 # not encrypted: encrypt it!
49 print("Config file not encrypted. I'm gonna encrypt it!\n");
50 print("Executing gpg.. \n");
51 print("You'll have to type the name of the key you want to use\n");
52 system('gpg -e '.$cfgfile);
53 # checking exit status
54 if($? != 0 ) {
55 # problem with gpg?
56 print "Exiting...\n";
57 exit -1;
58 }
59 # If file has been create overwrite the original one
60 if(-e $cfgfile.'.gpg') {
61 move($cfgfile.'.gpg', $cfgfile);
62 }
63 # now the config file is crypted!
64 }
65  
66 # decrypt file in memory
67 my @res = `gpg -d $cfgfile`;
68
69 # go thru the lines...
70 my($user, $pass, $pan);
71 foreach my $line (@res) {
72 $line =~ s/\n//g;
73 $_ = $line;
74 if(not /^\s{0,}#/ or not /^\s{0,}/) {
75 my ($key, $val) = split '=';
76 $val =~ s/^\s+//;
77 $val =~ s/\s+$//;
78 $user = $val if($key eq 'open24_number');
79 $pass = $val if($key eq 'password');
80 $pan = $val if($key eq 'pan');
81 }
82 }
83  
84 # die if some of the arguments are undef
85 foreach my $i ($user, $pass, $pan) {
86 if(not defined $i) {
87 die ("invalid configuration file!");
88 }
89 }
90  
91 return ($user, $pass, $pan);
92 }
93  
94 sub parse_options {
95  
96 my $cf = shift;
97  
98 # set some defaults
99  
100 # default config file is in ~/.ptsbrc
101 $cf->{cfgfile} = $ENV{HOME}."/.ptsbrc";
102 $cf->{no_balance} = 0;
103 $cf->{statement_type} = 'all';
104 $cf->{image} = '/tmp/file.png.'.$$.'.'.time;
105  
106 # =o -> Extended integer, Perl style. This can be either an optional
107 # leading plus or minus sign, followed by a sequence of digits, or
108 # an octal string (a zero, optionally followed by '0', '1', .. '7'),
109 # or a hexadecimal string (0x followed by '0' .. '9', 'a' .. 'f',
110 # case insensitive), or a binary string (0b followed by a series of
111 # '0' and '1').
112 #
113 # =s -> string
114 # =i -> integer
115  
116 my $error;
117 # be case sensitive!
118 Getopt::Long::Configure ("bundling");
119 # pase opts, put everything in the $cf hash_ref
120 Getopt::Long::GetOptions(
121 "file|f=s" => \$cf->{'cfgfile'},
122 "help|h" => \$cf->{'help'},
123 "debug|D" => \$cf->{'debug'},
124 "balance|b" => \$cf->{'balance'},
125 "statement|s" => \$cf->{'statement'},
126 "statement-type|T=s" => \$cf->{'statement_type'},
127 "from-date|f=s" => \$cf->{'fromdate'},
128 "to-date|t=s" => \$cf->{'todate'},
129 "account-type|a=s" => \$cf->{'acc_type'},
130 "account-num|n=s" => \$cf->{'acc_no'},
131 "no-balance|N" => \$cf->{'no_balance'},
132 "regexp|r=s" => \$cf->{'regexp'},
133 "version|v" => \$cf->{'version'},
134 "expr|e=s" => \$cf->{'expr'},
135 "i|image=s" => \$cf->{'image'},
136 "g|graph" => \$cf->{'graph'},
137 "o|output=s" => \$cf->{'csv'},
138 ) or $error = 1;
139  
140 usage if($error or $cf->{'help'});
141  
142 usage if(defined $cf->{graph} and $cf->{graph} eq '');
143  
144 }
145  
146 sub validate_options {
147  
148 my $cf = shift;
149  
150 # balance and statement cannot stay together
151 if($cf->{balance} and $cf->{statement}) {
152 print "You can not select -s and -b together!\n";
153 print "You can only select one operation at a time!\n";
154 exit -1;
155 }
156  
157 if(defined $cf->{acc_type}) {
158 # acc_type must be 'c' or 'v'
159 switch ($cf->{acc_type}) {
160 case "c" { $cf->{acc_type} = SWITCH_ACCOUNT; }
161 case "v" { $cf->{acc_type} = VISA_ACCOUNT; }
162 else {
163 print("Account type invalid\n");
164 exit -1;
165 }
166 }
167 }
168
169 if(defined $cf->{statement_type}) {
170 switch ($cf->{statement_type}) {
171 case /WITHDRAWAL/is { $cf->{statement_type} = WITHDRAWAL; }
172 case /DEPOSIT/is { $cf->{statement_type} = DEPOSIT; }
173 case /ALL/is { $cf->{statement_type} = ALL; }
174 else {
175 print("Statement type invalid\n");
176 exit -1;
177 }
178 }
179 }
180  
181 if(defined $cf->{acc_type}) {
182 if($cf->{acc_type} =~ /VISA/is) {
183 $cf->{no_balance} = 1;
184 $cf->{statement_type} = ALL;
185 }
186 }
187  
188 if(defined $cf->{version}) {
189 print Finance::Bank::IE::PermanentTSB->VERSION, "\n";
190 exit 0;
191 }
192  
193 # all the other check are made by Finance::Bank::IE::PermanentTSB
194 }
195  
196 sub print_statement_header {
197  
198 my $nobal = shift;
199  
200 if ($nobal) {
201 for(0...54) {
202 print("-");
203 }
204 } else {
205 for(0...68) {
206 print("-");
207 }
208 }
209 print "\n";
210 my $coldate = "Date";
211 my $coldesc = "Description";
212 my $colam = "Amount";
213 my $colbal = "Balance";
214 printf("| %10s | %25s | %11s |", $coldate, $coldesc, $colam);
215 printf(" %11s |", $colbal) if(not $nobal);
216 print "\n";
217 if ($nobal) {
218 for(0...54) {
219 print("-");
220 }
221 } else {
222 for(0...68) {
223 print("-");
224 }
225 }
226 print "\n";
227  
228 }
229  
230 sub print_statement_footer {
231  
232 my $nobal = shift;
233  
234 if ($nobal) {
235 for(0...54) {
236 print("-");
237 }
238 } else {
239 for(0...68) {
240 print("-");
241 }
242 }
243 print "\n";
244  
245 }
246  
247 sub print_balance_header {
248  
249 my $colname = "Account name";
250 my $coldigit = "Acc. #";
251 my $colbal = "Balance";
252 my $colavail = "Available";
253 for(0...58) {
254 print("-");
255 }
256 print "\n";
257 printf("| %18s | %6s | %11s | %11s |", $colname, $coldigit, $colbal,
258 $colavail);
259 print "\n";
260 for(0...58) {
261 print("-");
262 }
263 print "\n";
264  
265 }
266  
267 sub print_balance_footer {
268  
269 for(0...58) {
270 print("-");
271 }
272 print "\n";
273  
274 }
275  
276  
277 sub balance {
278
279 my $cf = shift;
280  
281 my %config = (
282 "open24numba" => $cf->{open24_num},
283 "password" => $cf->{pass},
284 "pan" => $cf->{pan},
285 "debug" => $cf->{debug},
286 );
287  
288 my $balance = Finance::Bank::IE::PermanentTSB->check_balance(
289 \%config,
290 $cf->{acc_type},
291 $cf->{acc_no},
292 $cf->{fromdate},
293 $cf->{todate});
294  
295  
296 if(defined $balance->[0]->{accno}) {
297 if($balance->[0]->{accno} eq '') {
298 print "Error while retrieving the content...\n";
299 return;
300 }
301 } else {
302 exit -1;
303 }
304  
305 print STDERR Dumper(\$balance) if($cf->{debug});
306  
307 print_balance_header;
308
309 foreach my $row (@$balance) {
310 printf("| %18s | %6s | %11s | %11s |\n",
311 $row->{accname},
312 $row->{accno},
313 $row->{accbal},
314 $row->{availbal});
315 }
316
317 print_balance_footer;
318  
319 }
320  
321 sub statement {
322  
323 my $cf = shift;
324 my $plot = shift;
325  
326 $plot ||= 0;
327  
328 my $counter_deposit = 0;
329 my $counter_withdrawal = 0;
330 my $initial_balance = 0;
331 my $final_balance = 0;
332  
333 my $gnuplot_tmpfile;
334  
335 my $error = 0;
336 my $csv = 0;
337  
338 my %config = (
339 "open24numba" => $cf->{open24_num},
340 "password" => $cf->{pass},
341 "pan" => $cf->{pan},
342 "debug" => $cf->{debug},
343 );
344  
345 my $statement = Finance::Bank::IE::PermanentTSB->account_statement(
346 \%config,
347 $cf->{acc_type},
348 $cf->{acc_no},
349 $cf->{fromdate},
350 $cf->{todate},
351 $cf->{statement_type},
352 );
353  
354 if(not defined $statement) {
355 return;
356 }
357  
358 print STDERR Dumper(\$statement) if($cf->{debug});
359  
360 if(defined $cf->{csv}) {
361 if(open(CSV, ">".$cf->{csv})) {
362 $csv = 1;
363 } else {
364 print STDERR "Error opening file ".$cf->{csv}.": $!\n";
365 $csv = 0;
366 }
367 }
368  
369 print_statement_header($cf->{no_balance});
370  
371 my $print = 1;
372 #foreach my $row (@$statement) {
373 for(my $i=0; $i<scalar($#$statement); $i++) {
374 my $row = $statement->[$i];
375 my $regex = $cf->{regexp};
376 my $expr = $cf->{expr};
377 if(defined $row->{description} and defined $cf->{regexp}) {
378 if($row->{description} =~ /$regex/is) {
379 $print = 1;
380 } else {
381 $print = 0;
382 }
383 }
384 # matches the value of --expr
385 if(defined $expr) {
386 if($expr =~ /([<,>,=])\s*(\d*)/) {
387 my $oper = $1;
388 my $val = scalar($2);
389 my $am = $row->{euro_amount};
390 $am =~ s/[\+-]//g;
391 if($cf->{debug}) {
392 print "\n";
393 print "am: '$am'\n";
394 print "1: '$oper'\n";
395 print "2: '$val'\n";
396 }
397 switch ($oper) {
398 case '<' {
399 if ($am < $val) {
400 $print = 1;
401 } else {
402 $print = 0;
403 }
404 }
405 case '>' {
406 if ($am > $val) {
407 $print = 1;
408 } else {
409 $print = 0;
410 }
411 }
412 case '=' {
413 if ($am == $val) {
414 $print = 1;
415 } else {
416 $print = 0;
417 }
418 }
419 case '<=' {
420 if ($am <= $val) {
421 $print = 1;
422 } else {
423 $print = 0;
424 }
425 }
426 case '>=' {
427 if ($am >= $val) {
428 $print = 1;
429 } else {
430 $print = 0;
431 }
432 }
433 else {
434 $print = 0;
435 }
436 }
437 }
438 }
439 if($print) {
440 printf("| %s | %25s | %11s ",
441 $row->{date},
442 $row->{description},
443 $row->{euro_amount},
444 );
445 if(not $cf->{no_balance}) {
446 printf "| %11s ", $row->{balance};
447 if ($statement->[($i+1)]->{date} ne
448 $statement->[$i]->{date}) {
449  
450 $gnuplot_tmpfile .=
451 $row->{date}."\t".$row->{balance}."\n";
452 }
453 }
454 print "|\n";
455 if($row->{euro_amount}<0) {
456 $counter_withdrawal += $row->{euro_amount};
457 } else {
458 $counter_deposit += $row->{euro_amount};
459 }
460 if($csv) {
461 printf CSV "%s,%s,%s", $row->{date}, $row->{description},
462 $row->{euro_amount};
463 print CSV ",", $row->{balance} if(not $cf->{no_balance});
464 print CSV "\n";
465 }
466 }
467  
468 }
469  
470 close CSV if ($csv);
471  
472 $gnuplot_tmpfile .=
473 $statement->[$#$statement]->{date}."\t".
474 $statement->[$#$statement]->{balance}."\n";
475  
476 print_statement_footer($cf->{no_balance});
477  
478 print "\n=== Totals ===\n\n";
479 if($cf->{statement_type} == ALL or $cf->{statement_type} == DEPOSIT) {
480 print "Total deposit: $counter_deposit\n";
481 }
482 if($cf->{statement_type} == ALL or $cf->{statement_type} ==
483 WITHDRAWAL) {
484 print "Total withdrawal: $counter_withdrawal\n";
485 }
486 if($cf->{statement_type} == ALL) {
487 printf("Profit: %f\n", $counter_deposit+$counter_withdrawal);
488 }
489  
490 $initial_balance = $statement->[0]->{balance};
491 $initial_balance =~ s/[\+|-]//;
492 $final_balance = $statement->[$#$statement]->{balance};
493 $final_balance =~ s/[\+|-]//;
494 if(not $cf->{no_balance}) {
495 print "\n";
496 print "Initial Balance: ", $initial_balance, "\n";
497 print "Final Balance: ", $final_balance, "\n";
498 printf "Delta Balance: %f\n", $final_balance-$initial_balance;
499 }
500  
501 if(defined $cf->{graph} and $cf->{graph}) {
502  
503 # writing $gnuplot_tmpfile to file
504 my $tmp_data = '/tmp/gnuplot_data'.$$.'.'.time;
505 open(TMP, ">$tmp_data");
506 print TMP $gnuplot_tmpfile;
507 close(TMP);
508  
509 # open lib/Finance/Bank/IE/templates/gnuplot_print.gp
510 # an parse it replacing [% var %]:
511 #
512 # - [% ACCOUNT %]
513 # - [% OUTPUT %]
514 # - [% FILENAME %]
515 # - [% TITLE %]
516  
517 # open the template and load it in @data
518 open(GNUPLOT, "<$FindBin::RealBin/lib/Finance/Bank/IE/templates/gnuplot_print.gp");
519 my @data=<GNUPLOT>;
520 close(GNUPLOT);
521  
522 # define substitutions
523 my $template_hash = {
524 ACCOUNT => $cf->{acc_type}."-".$cf->{acc_no},
525 OUTPUT => $cf->{image},
526 FILENAME => $tmp_data,
527 TITLE => $cf->{acc_no},
528 };
529  
530 print Dumper($template_hash) if($cf->{debug});
531  
532 # fill template
533 for(my $i = 0; $i<$#data; $i++) {
534 $data[$i] =~ s{\[%\s*([_a-zA-Z0-9]+)\s*%\]}{
535 _fill_template_element($template_hash, $1);
536 }gsex;
537 }
538  
539 # create the gnuplog command file
540 my $gnu_print = '/tmp/gnuplot_print'.$$.'.'.time;
541 open(TMP, ">$gnu_print");
542 foreach my $d (@data) {
543 print TMP $d;
544 }
545 close(TMP);
546  
547 # calling gnuplot
548 print "\n=== Plotting === \n";
549 print "Calling GnuPlot...\n";
550 my $res = `which gnuplot`;
551 chop $res;
552 if(!defined $res or not -e $res or not -x $res) {
553 print STDERR "problems with gnuplot! maybe not installed or permission problem?\n";
554 $error = 1;
555 } else {
556 system("gnuplot $gnu_print");
557 print "done.\n";
558 }
559  
560 print "Cleaning up...\n";
561 # unlink the tmp files
562 unlink ($tmp_data);
563 unlink ($gnu_print);
564 if($cf->{image} !~ /^\/tmp\/file\.png\.\d+\.\d+$/ and not $error){
565 print "PNG file has been created in ".$cf->{image}."\n";
566 } else {
567 unlink ($cf->{image});
568 }
569 }
570  
571 }
572  
573 sub _fill_template_element {
574 my ($hash, $name) = @_;
575 if (!defined $hash->{$name}) {
576 warn "undefined template tag: [\% $name \%]\n";
577 return '';
578 } else {
579 return $hash->{$name};
580 }
581 }
582  
583 1;
584