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