blib/lib/Finance/PaycheckRecords.pm

filter: Statementstmt 100% 103 / 103 Branchbran 80.9% 34 / 42 Conditioncond 64.2% 9 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 89.4% 161 / 180 CC 35 SCAR 25.7
CC
35
Cov
92%
parse_paystub
21.2
paystub_to_QIF
9.1
_add_splits
7.1
CRAP 13.0
↑ Summary
1 #---------------------------------------------------------------------
2 package Finance::PaycheckRecords;
3 #
4 # Copyright 2013 Christopher J. Madsen
5 #
6 # Author: Christopher J. Madsen <perl@cjmweb.net>
7 # Created: 2 Feb 2013
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the same terms as Perl itself.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
15 # GNU General Public License or the Artistic License for more details.
16 #
17 # ABSTRACT: Parse data from PaycheckRecords.com
18 #---------------------------------------------------------------------
19  
20 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 3 our $VERSION = '1.001';
25 # This file is part of Finance-PaycheckRecords 1.001 (October 24, 2015)
26  
27 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use HTML::TableExtract 2.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31  
32 3 ▶use Exporter 5.57 'import'; # exported import method
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 our @EXPORT = qw(parse_paystub paystub_to_QIF);
34  
35 # This indicates which HTML::TableExtract methods to call based on
36 # the keyword passed to parse_paystub.
37 3 our %parse_method = qw(
38 file parse_file
39 string parse
40 );
41  
42 3 our %eof_after_parse = (string => 1);
43  
44 # When converting a paystub to QIF, this controls which column
45 # contains the values that will be used in the transaction.
46 3 our $current = 'Current';
47  
48 #=====================================================================
49  
50  
51 sub parse_paystub
52 6 ▶{
Subroutine parse_paystub: called CC=21 SCAR=30.6
Pod documented
53 6 my ($input_type, $input) = @_;
54  
55 6 ▶ my $parse_method = $parse_method{$input_type}
Branch: 1 branch
BranchTrueFalseTotal
my $parse_method = $Finance::PaycheckRecords::parse_method{$input_type} or croak("Don't know how to parse '${input_type}'") 6 0 6
56 or croak("Don't know how to parse '$input_type'");
57  
58 6 my $te = HTML::TableExtract->new;
59 6 $te->$parse_method($input);
60 6 ▶ $te->eof if $eof_after_parse{$input_type};
Branch: 1 branch
BranchTrueFalseTotal
if $Finance::PaycheckRecords::eof_after_parse{$input_type} 2 4 6
61  
62 6 my %paystub;
63  
64 6 foreach my $ts ($te->tables) {
65 60 my @coords = $ts->coords;
66 60 my @rows = $ts->rows;
67  
68 3 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
69 60 ▶ if ($coords[0] == 2) {
Branch: 3 branches
BranchTrueFalseTotal
if ($coords[0] == 2) { } 12 48 60
elsif ($coords[0] == 4 and $coords[1] == 0) { } 6 42 48
elsif ($coords[0] == 3) { } 30 12 42
70 12 ▶ $paystub{pay_period} = $1
Branch: 1 branch
BranchTrueFalseTotal
if $rows[0][0] =~ /^\s*Pay stub for period:\s*(\S.+\S)\s*\z/s 6 6 12
71 if $rows[0][0] =~ /^\s*Pay stub for period:\s*(\S.+\S)\s*\z/s;
72 ▶ } elsif ($coords[0] == 4 and $coords[1] == 0) {
Condition66%2/3
$coords[0] == 4 and $coords[1] == 0
!ll&&!rl&&r
4206
MC/DC50%1/2
$coords[0] == 4 and $coords[1] == 0
$coords[0] == 4 $coords[1] == 0
Truth table66%2/3
A and B
A= $coords[0] == 4B= $coords[1] == 0
ABresult
0X0
100
111
73 6 $paystub{company} = $rows[0][0];
74 6 $paystub{payee} = $rows[2][0];
75 6 ▶ $paystub{check_number} = $1
Branch: 1 branch
BranchTrueFalseTotal
if $rows[0][2] =~ /\bCheck\s*#\s*(\d+)/ 3 3 6
76 if $rows[0][2] =~ /\bCheck\s*#\s*(\d+)/;
77 6 ▶ $paystub{date} = $1
Branch: 1 branch
BranchTrueFalseTotal
if $rows[0][2] =~ /\bDate:\s*(\S.+\S)/ 6 0 6
78 if $rows[0][2] =~ /\bDate:\s*(\S.+\S)/;
79 6 for (@paystub{qw(company payee)}) {
80 12 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 12 12
81 12 s/^[\s\xA0]+//;
82 12 s/[\s\xA0]+\z//;
83 12 s/\n[ \t]+/\n/g;
84 12 s/\n{2,}/\n/g;
85 }
86 } elsif ($coords[0] == 3) {
87 30 ▶ if ($rows[0][-1] =~ /^\s*YTD\s*\z/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rows[0][-1] =~ /^\s*YTD\s*\z/) { } 18 12 30
elsif ($rows[0][0] =~ /^\s*Net\s+This\s+Check:/) { } 6 6 12
88 18 my $headings = shift @rows;
89 18 my %table;
90 18 $paystub{split}{ shift @$headings } = \%table;
91 18 for my $row (@rows) {
92 48 for (@$row) {
93 168 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 168 168
94 168 s/^[\s\xA0]+//;
95 168 s/[\s\xA0]+\z//;
96 }
97 48 my $category = shift @$row;
98 48 @{ $table{$category} }{@$headings} = @$row;
99 }
100 } # end if YTD
101 elsif ($rows[0][0] =~ /^\s*Net\s+This\s+Check:/) {
102 6 for my $row (@rows) {
103 9 for (@$row) {
104 18 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 18 18
105 18 s/^[\s\xA0]+//;
106 18 s/[\s\xA0]+\z//;
107 }
108 9 $row->[0] =~ s/:\z//;
109 9 $row->[1] =~ s/[\$,]//g;
110  
111 9 $paystub{totals}{$row->[0]} = $row->[1];
112 }
113 } # end if Net This Check
114 }
115 } # end for each $ts in tables
116  
117 6 \%paystub;
118 } # end parse_paystub
119 #---------------------------------------------------------------------
120  
121  
122 sub paystub_to_QIF
123 4 ▶{
Subroutine paystub_to_QIF: called CC=9 SCAR=22.1
Pod documented
124 4 my ($paystub, $config) = @_;
125  
126 my $net_deposit = $paystub->{totals}{ $config->{net_deposit}
127 4 ▶ // 'Net This Check'};
Condition50%1/2
$config->{"net_deposit"} // "Net This Check"
l!l
04
MC/DC0%0/1
$config->{"net_deposit"} // "Net This Check"
$config->{"net_deposit"}
Truth table50%1/2
A
A= $config->{"net_deposit"}
Aresult
11
00
128 4 my @splits;
129  
130 4 _add_splits(\@splits, $paystub, $config->{income}, '');
131 4 _add_splits(\@splits, $paystub, $config->{expenses}, '-');
132  
133 4 my $sum = sprintf "%.2f", sum( map { $_->[0] } @splits);
134 4 ▶ croak("Sum of splits $sum != Net $net_deposit") unless $sum eq $net_deposit;
Branch: 1 branch
BranchTrueFalseTotal
unless $sum eq $net_deposit 0 4 4
135  
136 4 my $qif = "D$paystub->{date}\n";
137  
138 4 ▶ $qif .= "N$paystub->{check_number}\n" if length $paystub->{check_number};
Branch: 1 branch
BranchTrueFalseTotal
if length $paystub->{"check_number"} 2 2 4
139  
140 4 my $company = $paystub->{company};
141 4 $company =~ s/\n/\nA/g; # Subsequent lines are address
142 4 $qif .= "P$company\n";
143  
144 4 ▶ my $memo = $config->{memo} // "Paycheck for $paystub->{pay_period}";
Condition100%2/2
$config->{"memo"} // qq[Paycheck for $paystub->{"pay_period"}]
l!l
22
MC/DC100%1/1
$config->{"memo"} // qq[Paycheck for $paystub->{"pay_period"}]
$config->{"memo"}
Truth table100%2/2
A
A= $config->{"memo"}
Aresult
11
00
145 4 ▶ $qif .= "M$memo\n" if length $memo;
Branch: 1 branch
BranchTrueFalseTotal
if length $memo 4 0 4
146  
147 4 ▶ $qif .= sprintf "T%s\nL%s\n", $net_deposit, $config->{category} // 'Income';
Condition50%1/2
$config->{"category"} // "Income"
l!l
40
MC/DC0%0/1
$config->{"category"} // "Income"
$config->{"category"}
Truth table50%1/2
A
A= $config->{"category"}
Aresult
11
00
148  
149 4 for my $split (@splits) {
150 18 $qif .= "S$split->[1]\n";
151 18 ▶ $qif .= "E$split->[2]\n" if length $split->[2];
Branch: 1 branch
BranchTrueFalseTotal
if length $split->[2] 12 6 18
152 18 $qif .= "\$$split->[0]\n";
153 }
154  
155 4 $qif . "^\n";
156 } # end paystub_to_QIF
157  
158 #---------------------------------------------------------------------
159 sub _add_splits
160 8 ▶{
Subroutine _add_splits: called CC=7 SCAR=19.6
161 8 my ($all_splits, $paystub, $config, $sign) = @_;
162  
163 8 my @splits;
164  
165 8 ▶ while (my ($section, $fields) = each %$config) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$config) 8 8 16
166 8 ▶ while (my ($field, $values) = each %{ $paystub->{split}{$section} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$paystub{"split"}{$section};}) 20 8 28
167  
168 20 ▶ next unless ($values->{$current} // 0) != 0;
Branch: 1 branch
BranchTrueFalseTotal
unless ($values->{$Finance::PaycheckRecords::current} // 0) != 0 2 18 20
Condition50%1/2
$values->{$Finance::PaycheckRecords::current} // 0
l!l
200
MC/DC0%0/1
$values->{$Finance::PaycheckRecords::current} // 0
$values->{$Finance::PaycheckRecords::current}
Truth table50%1/2
A
A= $values->{$Finance::PaycheckRecords::current}
Aresult
11
00
169  
170 croak("Don't know what to do with $section: '$field'")
171 18 ▶ unless $fields->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $fields->{$field} 0 18 18
172  
173 18 push @splits, [ $sign . $values->{$current}, @{ $fields->{$field} } ];
174 }
175 }
176  
177 # Sort splits in ascending order by category name, and
178 # descending order by absolute value within a category:
179 8 ▶ push @$all_splits, sort { $a->[1] cmp $b->[1] or
Condition66%2/3
$Finance::PaycheckRecords::a->[1] cmp $Finance::PaycheckRecords::b->[1] || abs $Finance::PaycheckRecords::b->[0] <=> abs $Finance::PaycheckRecords::a->[0]
l!l&&r!l&&!r
750
MC/DC0%0/2
$Finance::PaycheckRecords::a->[1] cmp $Finance::PaycheckRecords::b->[1] || abs $Finance::PaycheckRecords::b->[0] <=> abs $Finance::PaycheckRecords::a->[0]
$Finance::PaycheckRecords::a->[1] cmp $Finance::PaycheckRecords::b->[1] abs $Finance::PaycheckRecords::b->[0] <=> abs $Finance::PaycheckRecords::a->[0]
Truth table66%2/3
A || B
A= $Finance::PaycheckRecords::a->[1] cmp $Finance::PaycheckRecords::b->[1]B= abs $Finance::PaycheckRecords::b->[0] <=> abs $Finance::PaycheckRecords::a->[0]
ABresult
1X1
011
000
180 abs($b->[0]) <=> abs($a->[0]) } @splits;
181 } # end _add_splits
182  
183 #=====================================================================
184 # Package Return Value:
185  
186 3 1;
187  
188 __END__
↑ Summary