File Coverage

lib/Finance/Bank/SentinelBenefits/Csv401kConverter/QifWriter.pm
Criterion Covered Total %
statement 36 36 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod 3 3 100.0
total 48 48 100.0


line stmt bran cond sub pod time code
1             package Finance::Bank::SentinelBenefits::Csv401kConverter::QifWriter;
2             $Finance::Bank::SentinelBenefits::Csv401kConverter::QifWriter::VERSION = '1.3';
3 6     6   1682 use Modern::Perl '2015';
  6         17  
  6         69  
4 6     6   1469 use feature 'signatures';
  6         14  
  6         185  
5              
6             =head1 NAME
7              
8             Finance::Bank::SentinelBenefits::Csv401kConverter::QifWriter -
9             Takes C<Finance::Bank::SentinelBenefits::Csv401kConverter::Line> objects
10             and writes them out to a QIF file
11              
12             =head1 VERSION
13              
14             version 1.3
15              
16             =head1 SYNOPSIS
17              
18             This class is responsible for taking a set of Lines and writing them out
19             to QIF format. We use the class C<Finance::QIF> to do the acutal writing to disk. If you pass it a secondary set of account information, it will also take any company matches, reverse their values, and write them to a secondary file. This is to allow you to keep track of your unvested balance.
20              
21             =cut
22              
23 6     6   1203 use Moose;
  6         917888  
  6         48  
24              
25 6     6   47469 use Finance::QIF;
  6         22524  
  6         237  
26 6     6   1088 use Finance::Bank::SentinelBenefits::Csv401kConverter::Line;
  6         34614  
  6         4226  
27              
28             =head1 Constructor
29              
30             =head2 new()
31              
32             my $l = Finance::Bank::SentinelBenefits::Csv401kConverter::QifWrite->new( {
33             main_output_filename => $main_output_filename,
34             flip_output_filename => $flip_output_filename, ( optional )
35             account => $account,
36             trade_date => $trade_date,
37             } );
38              
39             Constructs a new QifWriter for the given trade date and account. Each qif writer can only generate to one trade date and account, and one main file.
40              
41             =cut
42              
43             ####
44             # BUILDARGS initializes the QIF reader based on the passed in filename
45             ####
46             around BUILDARGS => sub {
47             my $orig = shift;
48             my $class = shift;
49              
50             my $hashref = $class->$orig(@_);
51              
52             if(exists $hashref->{output_file}){
53             my $qif_output = Finance::QIF->new( file => $hashref->{output_file} );
54              
55             $hashref->{qif_output} = $qif_output;
56             }
57              
58             return $hashref;
59             };
60              
61              
62             =head2 BUILD
63              
64             Called by Moose infrastructure
65              
66             =cut
67             ####
68             # BUILD sets up the QIF reader by writing out the header
69             ####
70             sub BUILD {
71 16     16 1 32 my $self = shift;
72              
73 16         635 $self->_get_qif_output()->header ( 'Account' );
74              
75 16         1455 my %init = (
76             name => $self->account(),
77             type => 'Type:Invst',
78             header=> 'Account',
79             );
80              
81 16         622 $self->_get_qif_output()->write(\%init);
82              
83 16         1742 $self->_get_qif_output()->header ( 'Type:Invst' );
84             };
85              
86             =head1 Accessors
87              
88             =head2 $l->account()
89              
90             The account of the transactions
91              
92             =cut
93              
94             has 'account'=> (
95             is => 'ro',
96             isa => 'Str',
97             required => 1,
98             );
99              
100             =head2 $l->output_file()
101              
102             The output file name that the QIF lines are written to.
103              
104             =cut
105              
106             has 'output_file' => (
107             is => 'ro',
108             isa => 'Str',
109             required => 1,
110             );
111              
112             has 'qif_output' => (
113             is => 'ro',
114             isa => 'Finance::QIF',
115             required => 1,
116             reader => '_get_qif_output',
117             );
118              
119             =head1 Methods
120              
121             =head2 $l->output_line($line)
122              
123             Writes one line out to the QIF file. Line is of type
124             C<Finance::Bank::SentinelBenefits::Csv401kConverter::Line>
125              
126             =cut
127              
128 20     20 1 41 sub output_line($self, $line){
  20         31  
  20         30  
  20         35  
129              
130             # my %security_transaction = ( header => 'Type:Invst',
131             # date => $date,
132             # action => 'Buy',
133             # security => 'IBM',
134             # price => 101.1,
135             # quantity => 20,
136             # memo => 'Purchased some stock',
137             # transaction => 101.1 * 20,
138             # );
139              
140 20         718 my $date_string = sprintf("%u/%u/%4d", $line->date()->mon(), $line->date()->day(), $line->date()->year());
141              
142 20         739 my $price = sprintf("%.6f", $line->total() / $line->quantity());#recalc price to avoid bad quantities
143 20         84 $price =~ s/0+$//;
144              
145              
146 20         605 my %transaction = (
147             header => 'Type:Invst',
148             date => $date_string,
149             action => $line->side(),
150             security => $line->symbol(),
151             #price => $line->price(),
152             price => $price,
153             quantity => $line->quantity(),
154             memo => $line->memo(),
155             transaction => $line->total(),
156             );
157 20         754 $self->_get_qif_output()->write(\%transaction);
158             }
159              
160             =head2 $l->close()
161              
162             Closes the writer, ensuring that data is flushed to disk. Calling output_line
163             after this is an error.
164              
165             =cut
166              
167 12     12 1 19 sub close($self){
  12         19  
  12         18  
168 12         515 $self->_get_qif_output()->close();
169             }
170              
171 6     6   59 no Moose;
  6         16  
  6         46  
172              
173             __PACKAGE__->meta->make_immutable;
174              
175             =head1 LICENSE AND COPYRIGHT
176             Copyright 2009-2023 David Solimano
177             This file is part of Finance::Bank::SentinelBenefits::Csv401kConverter
178              
179             Finance::Bank::SentinelBenefits::Csv401kConverter is free software: you can redistribute it and/or modify
180             it under the terms of the GNU General Public License as published by
181             the Free Software Foundation, either version 3 of the License, or
182             (at your option) any later version.
183              
184             Finance::Bank::SentinelBenefits::Csv401kConverter is distributed in the hope that it will be useful,
185             but WITHOUT ANY WARRANTY; without even the implied warranty of
186             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
187             GNU General Public License for more details.
188              
189             You should have received a copy of the GNU General Public License
190             along with Finance::Bank::SentinelBenefits::Csv401kConverter. If not, see <http://www.gnu.org/licenses/
191              
192             =cut