blib/lib/Finance/Bank/BR/Caixa/CSV.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 77 CC 4 SCAR 13.5
CC
4
Cov
0%
new
6.0
_parse
6.0
load
6.0
CRAP 3.8
1 package Finance::Bank::BR::Caixa::CSV;
2  
3 use strict;
4 use warnings;
5  
6 use DateTime::Format::Strptime qw(strptime);
7 use Text::CSV::Encoded;
8  
9 our $VERSION = '0.01';
10  
11 sub new {
12 my $class = shift;
13 my $self = {};
14 bless $self, $class;
15 $self->_init;
16 if ($self->load(@_)) {
17 return undef;
18 }
19 return $self;
20 }
21  
22 sub _init {
23 my $self = shift;
24 $self->{balance} = 0;
25 $self->{statement} = [];
26 }
27  
28 sub _parse {
29 my $self = shift;
30 my $file = shift;
31 my $csv = Text::CSV::Encoded->new({
32 encoding => "utf-8",
33 encoding_in => "utf-8",
34 sep_char => ";"
35 });
36 my $lines = $csv->getline_all($file, 1);
37 my $balance = $self->{balance};
38 for my $line (@{$lines}) {
39 my $val = $line->[4];
40 $val = -$val if ($line->[5] eq "D");
41 $balance += $val;
42 my $obj = {
43 'date' => strptime("%Y%m%d", $line->[1]),
44 'name' => $line->[3],
45 'extra' => $line->[2],
46 'value' => $val,
47 'balance' => $balance,
48 };
49 push @{$self->{statement}}, $obj;
50 }
51 $self->{balance} = $balance;
52 }
53  
54 sub load {
55 my $self = shift;
56 my $filename = shift;
57 if (defined($filename)) {
58 open my $file, "<", $filename or return 1;
59 $self->_parse($file);
60 close $file;
61 }
62 return 0;
63 }
64  
65 sub balance {
66 my $self = shift;
67 return $self->{balance};
68 }
69  
70 sub statement {
71 my $self = shift;
72 return @{$self->{statement}};
73 }
74  
75 1;
76  
77 __END__