e388aeb0b8dd0488f8b25552f4ca1d62663bd120
[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 use Spreadsheet::ParseExcel::Simple;
7
8 use DateTime::Format::Strptime qw(strptime);
9
10 our $VERSION = '0.01';
11
12 sub new {
13     my $class = shift;
14     my $self = {};
15     bless $self, $class;
16     $self->_init;
17     if ($self->load(@_)) {
18         return undef;
19     }
20     return $self;
21 }
22
23 sub _init {
24     my $self = shift;
25     $self->{balance} = 0;
26     $self->{statement} = [];
27 }
28
29 sub _parse {
30     my $self = shift;
31     my $xls = shift;
32     my @sheets = $xls->sheets;
33     my $sheet = $sheets[0];
34     if ($sheet->has_data) {
35         my @header = $sheet->next_row;
36     }
37     while ($sheet->has_data) {
38         my @line = $sheet->next_row;
39         my $obj = {
40             'date' => strptime("%d/%m/%Y", $line[0]),
41             'name' => $line[2],
42             'extra' => $line[3],
43             'value' => $line[4],
44             'balance' => $line[5],
45         };
46         push @{$self->{statement}}, $obj;
47         $self->{balance} = $line[5];
48     }
49 }
50
51 sub load {
52     my $self = shift;
53     my $filename = shift;
54     if (defined($filename)) {
55         my $xls = Spreadsheet::ParseExcel::Simple->read($filename);
56         if (!defined($xls)) {
57             return 1;
58         }
59         $self->_parse($xls);
60     }
61     return 0;
62 }
63
64 sub balance {
65     my $self = shift;
66     return $self->{balance};
67 }
68
69 sub statement {
70     my $self = shift;
71     return $self->{statement};
72 }
73
74 1;
75
76 __END__
77
78 =head1 NAME
79
80 Finance::Bank::BR::Santander::Spreadsheet - Parse statement exported from Brazilian branch of Santander Internet Banking
81
82 =head1 SYNOPSIS
83
84     use Finance::Bank::BR::Santander::Spreadsheet;
85
86     my $spreadsheet = Finance::Bank::BR::Santander::Spreadsheet->new($filename);
87     my $balance = $spreadsheet->balance;
88     my $data = $spreadsheet->statement;
89     foreach my $transaction (@{$data}) {
90         say "$transaction->{date}, $transaction->{name}, $transaction->{extra}, $transaction->{value}, $transaction->{balance}";
91     }
92
93 =head1 Description
94
95 This module is an object-oriented interface that parses statements exported as XLS from the Internet Banking for the Brazilian branch of Santander.
96
97 =head1 Spreadsheet
98
99 =head2 new($filename)
100
101 The C<new()> method creates a new Spreadsheet object containing the data parsed from C<$filename>.
102
103 If an error occurs while loading the file, C<new()> returns C<undef>.
104
105 =head2 balance()
106
107 The C<balance()> method returns the last balance found in the sheet.
108
109 =head2 statement()
110
111 The C<statement()> method returns a reference to an array of transactions, described as below.
112
113 =head1 Transaction
114
115 The transaction is a hash containing the following keys:
116
117 =head2 name
118
119 A string with a name describing the transaction.
120
121 =head2 value
122
123 A floating number containing the credit (positive) or debit (negative) of the transaction.
124
125 =head2 date
126
127 A DateTime object representing the date when the transaction occurred.
128
129 =head2 balance
130
131 A floating number containing the balance resulting from the transaction.
132
133 =head2 extra
134
135 Data provided by the bank identifying the transaction. In this case, a number specific to the type of transaction. It can be used to help uniquely identify the transaction.
136
137 =head1 COPYRIGHT AND LICENSE
138
139   Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
140
141   This program is free software; you can redistribute it and/or modify it under
142   the terms of the GNU General Public License; either version 2 of the License,
143   or (at your option) any later version.
144
145   This program is distributed in the hope that it will be useful, but WITHOUT
146   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
147   FOR A PARTICULAR PURPOSE.
148
149 =cut