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