Finance::Bank::BR::Santander::Spreadsheet module.
[cascardo/Finance-Bank-BR-Santander-Spreadsheet.git] / lib / Finance / Bank / BR / Santander / Spreadsheet.pm
1 package Finance::Bank::BR::Santander::Spreadsheet;
2
3 use strict;
4 use warnings;
5
6 =head1 COPYRIGHT AND LICENSE
7
8   Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
9
10   This program is free software; you can redistribute it and/or modify it under
11   the terms of the GNU General Public License; either version 2 of the License,
12   or (at your option) any later version.
13
14   This program is distributed in the hope that it will be useful, but WITHOUT
15   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16   FOR A PARTICULAR PURPOSE.
17
18 =cut
19
20 use Spreadsheet::ParseExcel::Simple;
21
22 sub new {
23     my $class = shift;
24     my $self = {};
25     bless $self, $class;
26     $self->_init;
27     $self->load(@_);
28     return $self;
29 }
30
31 sub _init {
32     my $self = shift;
33     $self->{balance} = 0;
34     $self->{statement} = [];
35 }
36
37 sub _parse {
38     my $self = shift;
39     my $xls = shift;
40     my @sheets = $xls->sheets;
41     my $sheet = $sheets[0];
42     if ($sheet->has_data) {
43         my @header = $sheet->next_row;
44     }
45     while ($sheet->has_data) {
46         my @line = $sheet->next_row;
47         my $obj = {
48             'date' => $line[0],
49             'name' => $line[2],
50             'extra' => $line[3],
51             'value' => $line[4],
52             'balance' => $line[5],
53         };
54         push @{$self->{statement}}, $obj;
55         $self->{balance} = $line[5];
56     }
57 }
58
59 sub load {
60     my $self = shift;
61     my $filename = shift;
62     if (defined($filename)) {
63         my $xls = Spreadsheet::ParseExcel::Simple->read($filename);
64         if (!defined($xls)) {
65             return;
66         }
67         $self->_parse($xls);
68     }
69 }
70
71 sub balance {
72     my $self = shift;
73     return $self->{balance};
74 }
75
76 sub statement {
77     my $self = shift;
78     return $self->{statement};
79 }
80
81 1;