lib/Finance/Bank/SentinelBenefits/Csv401kConverter/QifWriter.pm

filter: Statementstmt 100% 42 / 42 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 98.2% 56 / 57 CC 2 SCAR 1.7
CC
2
Cov
98%
__ANON__
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« lib/Finance/Bank/SentinelBenefits/Csv401kConverter/SideReverser.pm ↑ Summary lib/Finance/Bank/SentinelBenefits/Csv401kConverter/Line.pm »
1 package Finance::Bank::SentinelBenefits::Csv401kConverter::QifWriter;
2 6 $Finance::Bank::SentinelBenefits::Csv401kConverter::QifWriter::VERSION = '1.3';
3 6 ▶use Modern::Perl '2015';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 6 ▶use Finance::QIF;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use Finance::Bank::SentinelBenefits::Csv401kConverter::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 16 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
47 16 my $orig = shift;
48 16 my $class = shift;
49  
50 16 my $hashref = $class->$orig(@_);
51  
52 16 ▶ if(exists $hashref->{output_file}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hashref->{"output_file"}) 16 0 16
53 16 my $qif_output = Finance::QIF->new( file => $hashref->{output_file} );
54  
55 16 $hashref->{qif_output} = $qif_output;
56 }
57  
58 16 return $hashref;
59 6 };
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 16 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
71 16 my $self = shift;
72  
73 16 $self->_get_qif_output()->header ( 'Account' );
74  
75 16 my %init = (
76 name => $self->account(),
77 type => 'Type:Invst',
78 header=> 'Account',
79 );
80  
81 16 $self->_get_qif_output()->write(\%init);
82  
83 16 $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 6 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 6 has 'output_file' => (
107 is => 'ro',
108 isa => 'Str',
109 required => 1,
110 );
111  
112 6 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 ▶sub output_line($self, $line){
Subroutine output_line: called CC=1 SCAR=0.0
Pod documented
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 my $date_string = sprintf("%u/%u/%4d", $line->date()->mon(), $line->date()->day(), $line->date()->year());
141  
142 20 my $price = sprintf("%.6f", $line->total() / $line->quantity());#recalc price to avoid bad quantities
143 20 $price =~ s/0+$//;
144  
145  
146 20 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 $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 ▶sub close($self){
Subroutine close: called CC=1 SCAR=0.0
Pod documented
168 12 $self->_get_qif_output()->close();
169 }
170  
171 6 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
172  
173 6 __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
« lib/Finance/Bank/SentinelBenefits/Csv401kConverter/SideReverser.pm ↑ Summary lib/Finance/Bank/SentinelBenefits/Csv401kConverter/Line.pm »