Give a version to 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 our $VERSION = '0.01';
23
24 sub new {
25     my $class = shift;
26     my $self = {};
27     bless $self, $class;
28     $self->_init;
29     $self->load(@_);
30     return $self;
31 }
32
33 sub _init {
34     my $self = shift;
35     $self->{balance} = 0;
36     $self->{statement} = [];
37 }
38
39 sub _parse {
40     my $self = shift;
41     my $xls = shift;
42     my @sheets = $xls->sheets;
43     my $sheet = $sheets[0];
44     if ($sheet->has_data) {
45         my @header = $sheet->next_row;
46     }
47     while ($sheet->has_data) {
48         my @line = $sheet->next_row;
49         my $obj = {
50             'date' => $line[0],
51             'name' => $line[2],
52             'extra' => $line[3],
53             'value' => $line[4],
54             'balance' => $line[5],
55         };
56         push @{$self->{statement}}, $obj;
57         $self->{balance} = $line[5];
58     }
59 }
60
61 sub load {
62     my $self = shift;
63     my $filename = shift;
64     if (defined($filename)) {
65         my $xls = Spreadsheet::ParseExcel::Simple->read($filename);
66         if (!defined($xls)) {
67             return;
68         }
69         $self->_parse($xls);
70     }
71 }
72
73 sub balance {
74     my $self = shift;
75     return $self->{balance};
76 }
77
78 sub statement {
79     my $self = shift;
80     return $self->{statement};
81 }
82
83 1;