StockAccount/Import/OptionsXpress.pm

filter: Statementstmt 96.8% 61 / 63 Branchbran 76.6% 23 / 30 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 4 totaltotal 87.9% 95 / 108 CC 17 SCAR 19.7
CC
17
Cov
91%
nextAt
11.0
getTm
4.1
init
4.1
CRAP 7.1
« StockAccount.pm ↑ Summary StockAccount/Set.pm »
1 package Finance::StockAccount::Import::OptionsXpress;
2  
3 3 our $VERSION = '0.01';
4  
5 3 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent 'Finance::StockAccount::Import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 #### Expected Fields, tab separated:
9 # Symbol, Description, Action, Quantity, Price, Commission, Reg Fees, Date, TransactionID, Order Number, Transaction Type ID, Total Cost
10 # 0 1 2 3 4 5 6 7 8 9 10 11
11 3 my @pattern = qw(symbol 0 action 2 quantity 3 price 4 commission 5 regulatoryFees 6 date 7 totalCost 11);
12 3 my $numFields = 12;
13  
14 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
15 8 my ($class, $file, $tzoffset) = @_;
16 8 my $self = $class->SUPER::new($file, $tzoffset);
17 8 $self->{pattern} = \@pattern;
18 8 $self->{headers} = undef;
19 8 $self->init();
20 8 return $self;
21 }
22  
23 8 ▶sub init {
Subroutine init: called CC=4 SCAR=14.0
Pod undocumented
24 8 my $self = shift;
25 8 my $file = $self->{file};
26 8 ▶ open my $fh, '<', $file or die "Failed to open $file: $!.\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Failed to open ${file}: $!.\n" 8 0 8
27 8 $self->{fh} = $fh;
28  
29 8 my $hline = <$fh>;
30 8 chomp($hline);
31 8 my @headers = split(', ', $hline);
32 8 ▶ scalar(@headers) == $numFields or die "Unexpected number of headers. Header line:\n$hline\n";
Branch: 1 branch
BranchTrueFalseTotal
scalar @headers == $numFields or die "Unexpected number of headers. Header line:\n$hline\n" 8 0 8
33 8 $self->{headers} = \@headers;
34 8 my $blankLine = <$fh>;
35 8 ▶ $blankLine =~ /\w/ and warn "Expected blank line after header line. May have inadvertantly skipped first transaction...\n";
Branch: 1 branch
BranchTrueFalseTotal
$blankLine =~ /\w/ and warn "Expected blank line after header line. May have inadvertantly skipped first transaction...\n" 0 8 8
36 8 return 1;
37 }
38  
39 239 ▶sub getTm {
Subroutine getTm: called CC=4 SCAR=14.2
Pod undocumented
40 239 my ($self, $dateString) = @_;
41 239 ▶ if ($dateString =~ /^(\d{1,2})\/(\d{1,2})\/(\d{4})\s+(\d{1,2}):(\d{2}):(\d{2})\s+(\wM)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dateString =~ m[^(\d{1,2})/(\d{1,2})/(\d{4})\s+(\d{1,2}):(\d{2}):(\d{2})\s+(\wM)$]i) { } 239 0 239
42 239 my ($month, $day, $year, $hour, $minute, $second, $pm) = ($1, $2, $3, $4, $5, $6, $7);
43 239 ▶ if ($pm =~ /^PM$/i and $hour < 12) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pm =~ /^PM$/i and $hour < 12) 74 165 239
Condition100%3/3
$pm =~ /^PM$/i and $hour < 12
!ll&&!rl&&r
1323374
MC/DC100%2/2
$pm =~ /^PM$/i and $hour < 12
$pm =~ /^PM$/i $hour < 12
Truth table100%3/3
A and B
A= $pm =~ /^PM$/iB= $hour < 12
ABresult
0X0
100
111
44 74 $hour += 12;
45 }
46 return Time::Moment->new(
47 year => $year,
48 month => $month,
49 day => $day,
50 hour => $hour,
51 minute => $minute,
52 second => $second,
53 offset => $self->{tzoffset},
54 239 );
55 }
56 else {
57 0 warn "Did not recognize date time format:\n$dateString\n";
58 0 return undef;
59 }
60 }
61  
62 246 ▶sub nextAt {
Subroutine nextAt: called CC=11 SCAR=24.0
Pod undocumented
63 246 my $self = shift;
64 246 my $fh = $self->{fh};
65 246 my $pattern = $self->{pattern};
66 246 ▶ if (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = readline $fh) { } 239 7 246
67 239 chomp($line);
68 239 my @row = split(',', $line);
69  
70 239 my $hash = {};
71 239 my $action;
72 239 ▶ for (my $x=0; $x<scalar(@$pattern)-1; $x+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < scalar(@$pattern) - 1) 1912 239 2151
73 1912 my $index = $pattern->[$x+1];
74 1912 ▶ if (exists($row[$index])) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $row[$index]) 1912 0 1912
75 1912 my $key = $pattern->[$x];
76 1912 ▶ if ($key eq 'action') {
Branch: 3 branches
BranchTrueFalseTotal
if ($key eq 'action') { } 239 1673 1912
elsif ($key eq 'date') { } 239 1434 1673
elsif ($key eq 'totalCost') { } 239 1195 1434
77 239 $action = $row[$index];
78 }
79 elsif ($key eq 'date') {
80 239 $hash->{tm} = $self->getTm($row[$index]);
81 }
82 elsif ($key eq 'totalCost') {
83 239 next();
84 }
85 else {
86 1195 my $value = $row[$index];
87 1195 ▶ if ($key =~ /^(?:price|quantity|commission|regulatoryFees)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(?:price|quantity|commission|regulatoryFees)$/) 956 239 1195
88 956 $value += 0;
89 }
90 1195 $hash->{$key} = $row[$index];
91 }
92 }
93 }
94 239 my $at = Finance::StockAccount::AccountTransaction->new($hash);
95 239 ▶ if ($action eq 'Sell') {
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq 'Sell') { } 96 143 239
elsif ($action eq 'Buy') { } 143 0 143
96 96 $at->sell(1);
97 }
98 elsif ($action eq 'Buy') {
99 143 $at->buy(1);
100 }
101 239 return $at;
102 }
103 else {
104 7 ▶ close($fh) or die "Failed to close file descriptor: $!.\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Failed to close file descriptor: $!.\n" 7 0 7
105 7 return 0;
106 }
107 }
108  
109  
110  
111 3 1;
112  
« StockAccount.pm ↑ Summary StockAccount/Set.pm »