StockAccount/Transaction.pm

filter: Statementstmt 76.6% 161 / 210 Branchbran 64.1% 59 / 92 Conditioncond 48.0% 12 / 25 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 92.1% 35 / 38 Podpod 0.0% 0 / 30 totaltotal 66.1% 271 / 410 CC 59 SCAR 16.9
CC
59
Cov
71%
get
12.0
string
9.3
actionString
8.8
CRAP 5.4
« StockAccount/Stock.pm ↑ Summary StockAccount/Realization.pm »
1 package Finance::StockAccount::Transaction;
2  
3 10 our $VERSION = '0.01';
4  
5 10 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use Finance::StockAccount::Stock;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶use constant BUY => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use constant SELL => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use constant SHORT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use constant COVER => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 10 my $lineFormatPattern = "%-35s %-6s %-6s %8s %7.2f %10.2f %5.2f %10.2f\n";
18 10 my $headerPattern = "%-35s %-6s %-6s %8s %7s %10s %5s %10s\n";
19 10 my @headerNames = qw(Date Symbol Action Quantity Price Commission Fees CashEffect);
20  
21 278 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
22 278 my ($class, $init) = @_;
23 278 my $self = {
24 tm => undef,
25 action => undef,
26 stock => undef,
27 quantity => undef,
28 price => undef,
29 commission => 0,
30 regulatoryFees => 0,
31 otherFees => 0,
32 };
33 278 bless($self, $class);
34 278 ▶ $init and $self->set($init);
Branch: 1 branch
BranchTrueFalseTotal
$init and $self->set($init) 277 1 278
35 278 return $self;
36 }
37  
38 2 ▶sub order {
Subroutine order: called CC=1 SCAR=0.0
Pod undocumented
39 2 return qw(date action stock quantity price commission regulatoryFees otherFees);
40 }
41  
42 13484 ▶sub tm { # Time::Moment object getter/setter
Subroutine tm: called CC=4 SCAR=15.2
Pod undocumented
43 13484 my ($self, $tm) = @_;
44 13484 ▶ if ($tm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tm) { } 241 13243 13484
45 241 ▶ if (ref($tm) and ref($tm) eq 'Time::Moment') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tm and ref $tm eq 'Time::Moment') { } 241 0 241
Condition33%1/3
ref $tm and ref $tm eq "Time::Moment"
!ll&&!rl&&r
00241
MC/DC0%0/2
ref $tm and ref $tm eq "Time::Moment"
ref $tm ref $tm eq "Time::Moment"
Truth table33%1/3
A and B
A= ref $tmB= ref $tm eq "Time::Moment"
ABresult
0X0
100
111
46 241 $self->{tm} = $tm;
47 241 return 1;
48 }
49 else {
50 0 warn "$tm not a valid Time::Moment object.\n";
51 0 return 0;
52 }
53 }
54 else {
55 13243 return $self->{tm};
56 }
57 }
58  
59  
60 29 ▶sub dateString {
Subroutine dateString: called CC=4 SCAR=15.0
Pod undocumented
61 29 my ($self, $dateString) = @_;
62 29 ▶ if ($dateString) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dateString) { } 27 2 29
63 27 my $tm = Time::Moment->from_string($dateString);
64 27 ▶ if ($tm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tm) { } 27 0 27
65 27 $self->{tm} = $tm;
66 27 return 1;
67 }
68 else {
69 0 warn "Unable to create Time::Moment object from date string $dateString.\n";
70 0 return 0;
71 }
72 }
73 else {
74 2 my $tm = $self->{tm};
75 2 ▶ if ($tm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tm) { } 2 0 2
76 2 return $tm->to_string();
77 }
78 else {
79 0 warn "Time::Moment property not set.\n";
80 0 return undef;
81 }
82 }
83 }
84  
85 36 ▶sub action {
Subroutine action: called CC=6 SCAR=20.5
Pod undocumented
86 36 my ($self, $action) = @_;
87 36 ▶ if ($action) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action) { } 36 0 36
88 36 ▶ if ($action eq 'buy') {
Branch: 4 branches
BranchTrueFalseTotal
if ($action eq 'buy') { } 21 15 36
elsif ($action eq 'sell') { } 14 1 15
elsif ($action eq 'short') { } 1 0 1
elsif ($action eq 'cover') { } 0 0 0
89 21 return $self->buy(1);
90 }
91 elsif ($action eq 'sell') {
92 14 return $self->sell(1);
93 }
94 elsif ($action eq 'short') {
95 1 return $self->short(1);
96 }
97 elsif ($action eq 'cover') {
98 0 return $self->cover(1);
99 }
100 else {
101 0 die "Action must a string, one of 'buy', 'sell', 'short', 'cover'.\n";
102 }
103 }
104 else {
105 0 return $self->{action};
106 }
107 }
108  
109 2678 ▶sub stock {
Subroutine stock: called CC=5 SCAR=21.5
Pod undocumented
110 2678 my ($self, $stock) = @_;
111 2678 ▶ if ($stock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stock) { } 0 2678 2678
112 0 ▶ if (ref($stock) and 'Finance::StockAccount::Stock' eq ref($stock)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $stock and 'Finance::StockAccount::Stock' eq ref $stock) { } 0 0 0
Condition0%0/3
ref $stock and "Finance::StockAccount::Stock" eq ref $stock
!ll&&!rl&&r
000
MC/DC0%0/2
ref $stock and "Finance::StockAccount::Stock" eq ref $stock
ref $stock "Finance::StockAccount::Stock" eq ref $stock
Truth table0%0/3
A and B
A= ref $stockB= "Finance::StockAccount::Stock" eq ref $stock
ABresult
0X0
100
111
113 0 $self->{stock} = $stock;
114 0 return 1;
115 }
116 else {
117 0 warn "$stock is not a recognized Finance::StockAccount::Stock object.\n";
118 0 return 0;
119 }
120 }
121 else {
122 2678 $stock = $self->{stock};
123 2678 ▶ if (!$stock) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stock) 275 2403 2678
124 275 $stock = Finance::StockAccount::Stock->new();
125 275 $self->{stock} = $stock;
126 }
127 2678 return $stock;
128 }
129 }
130  
131 0 ▶sub sameStock {
Subroutine sameStock: not called CC=2 SCAR=17.9
Pod undocumented
132 0 my ($self, $testStock) = @_;
133 0 my $stock = $self->{stock};
134 0 ▶ if ($stock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stock) { } 0 0 0
135 0 return $stock->same($testStock);
136 }
137 else {
138 0 warn "Can't test for sameStock, object stock property not yet defined.\n";
139 0 return 0;
140 }
141 }
142  
143 1101 ▶sub symbol {
Subroutine symbol: called CC=1 SCAR=0.0
Pod undocumented
144 1101 my ($self, $symbol) = @_;
145 1101 my $stock = $self->stock();
146 1101 return $stock->symbol($symbol);
147 }
148  
149 2 ▶sub exchange {
Subroutine exchange: called CC=1 SCAR=0.0
Pod undocumented
150 2 my ($self, $exchange) = @_;
151 2 my $stock = $self->stock();
152 2 return $stock->exchange($exchange);
153 }
154  
155 3065 ▶sub quantity {
Subroutine quantity: called CC=2 SCAR=6.9
Pod undocumented
156 3065 my ($self, $quantity) = @_;
157 3065 ▶ if ($quantity) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quantity) { } 1 3064 3065
158 1 $self->{quantity} = $quantity;
159 1 return 1;
160 }
161 else {
162 3064 return $self->{quantity};
163 }
164 }
165  
166 1 ▶sub price {
Subroutine price: called CC=2 SCAR=8.4
Pod undocumented
167 1 my ($self, $price) = @_;
168 1 ▶ if ($price) {
Branch: 1 branch
BranchTrueFalseTotal
if ($price) { } 0 1 1
169 0 $self->{price} = $price;
170 0 return 1;
171 }
172 else {
173 1 return $self->{price};
174 }
175 }
176  
177 3010 ▶sub commission {
Subroutine commission: called CC=2 SCAR=8.4
Pod undocumented
178 3010 my ($self, $commission) = @_;
179 3010 ▶ if ($commission) {
Branch: 1 branch
BranchTrueFalseTotal
if ($commission) { } 0 3010 3010
180 0 $self->{commission} = $commission;
181 0 return 1;
182 }
183 else {
184 3010 return $self->{commission};
185 }
186 }
187  
188 3009 ▶sub regulatoryFees {
Subroutine regulatoryFees: called CC=2 SCAR=8.4
Pod undocumented
189 3009 my ($self, $regulatoryFees) = @_;
190 3009 ▶ if ($regulatoryFees) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regulatoryFees) { } 0 3009 3009
191 0 $self->{regulatoryFees} = $regulatoryFees;
192 0 return 1;
193 }
194 else {
195 3009 return $self->{regulatoryFees};
196 }
197 }
198  
199 3009 ▶sub otherFees {
Subroutine otherFees: called CC=2 SCAR=8.4
Pod undocumented
200 3009 my ($self, $otherFees) = @_;
201 3009 ▶ if ($otherFees) {
Branch: 1 branch
BranchTrueFalseTotal
if ($otherFees) { } 0 3009 3009
202 0 $self->{otherFees} = $otherFees;
203 0 return 1;
204 }
205 else {
206 3009 return $self->{otherFees};
207 }
208 }
209  
210 3543 ▶sub priceByQuantity {
Subroutine priceByQuantity: called CC=1 SCAR=0.0
Pod undocumented
211 3543 my $self = shift;
212 3543 return $self->{price} * $self->{quantity};
213 }
214  
215 3543 ▶sub feesAndCommissions {
Subroutine feesAndCommissions: called CC=1 SCAR=0.0
Pod undocumented
216 3543 my $self = shift;
217 3543 return $self->{commission} + $self->{regulatoryFees} + $self->{otherFees};
218 }
219  
220 3543 ▶sub cashEffect {
Subroutine cashEffect: called CC=6 SCAR=19.9
Pod undocumented
221 3543 my $self = shift;
222 3543 my $cashEffect;
223 3543 ▶ if ($self->buy() or $self->short()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->buy or $self->short) { } 1604 1939 3543
elsif ($self->sell or $self->cover) { } 1939 0 1939
Condition66%2/3
$self->buy or $self->short
l!l&&r!l&&!r
160401939
MC/DC50%1/2
$self->buy or $self->short
$self->buy $self->short
Truth table66%2/3
A or B
A= $self->buyB= $self->short
ABresult
1X1
011
000
224 1604 $cashEffect = 0 - ($self->priceByQuantity() + $self->feesAndCommissions());
225 }
226 ▶ elsif ($self->sell() or $self->cover()) {
Condition33%1/3
$self->sell or $self->cover
l!l&&r!l&&!r
193900
MC/DC0%0/2
$self->sell or $self->cover
$self->sell $self->cover
Truth table33%1/3
A or B
A= $self->sellB= $self->cover
ABresult
1X1
011
000
227 1939 $cashEffect = $self->priceByQuantity() - $self->feesAndCommissions();
228 }
229 3543 ▶ if ($cashEffect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cashEffect) { } 3543 0 3543
230 3543 return $cashEffect;
231 }
232 else {
233 0 warn "Cannot calculate cash effect.\n";
234 0 return 0;
235 }
236 }
237  
238 280 ▶sub set {
Subroutine set: called CC=8 SCAR=21.3
Pod undocumented
239 280 my ($self, $init) = @_;
240 280 my $status = 1;
241 280 foreach my $key (keys %{$init}) {
242 1649 ▶ if (exists($self->{$key})) {
Branch: 4 branches
BranchTrueFalseTotal
if (exists $self->{$key}) { } 1348 301 1649
elsif ($key eq 'symbol') { } 274 27 301
elsif ($key eq 'exchange') { } 0 27 27
elsif ($key eq 'dateString') { } 27 0 27
243 1348 ▶ if ($key eq 'action') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'action') { } 36 1312 1348
elsif ($key eq 'tm') { } 241 1071 1312
244 36 $self->action($init->{$key});
245 }
246 elsif ($key eq 'tm') {
247 241 $self->tm($init->{$key});
248 }
249 else {
250 1071 $self->{$key} = $init->{$key};
251 }
252 }
253 elsif ($key eq 'symbol') {
254 274 $self->symbol($init->{$key});
255 }
256 elsif ($key eq 'exchange') {
257 0 $self->exchange($init->{$key});
258 }
259 elsif ($key eq 'dateString') {
260 27 $self->dateString($init->{$key});
261 }
262 else {
263 0 $status = 0;
264 0 warn "Tried to set $key in StockAccount::Transaction object, but that's not a known key.\n";
265 }
266 }
267 280 return $status;
268 }
269  
270 0 ▶sub get {
Subroutine get: not called CC=3 SCAR=24.8
Pod undocumented
271 0 my ($self, $key) = @_;
272 0 ▶ if ($key and exists($self->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key and exists $self->{$key}) { } 0 0 0
Condition0%0/3
$key and exists $self->{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
$key and exists $self->{$key}
$key exists $self->{$key}
Truth table0%0/3
A and B
A= $keyB= exists $self->{$key}
ABresult
0X0
100
111
273 0 return $self->{$key};
274 }
275 else {
276 0 warn "Tried to get key from StockAccount::Transaction object, but that's not a known key.\n";
277 0 return 0;
278 }
279 }
280  
281 24843 ▶sub validateAction {
Subroutine validateAction: called CC=2 SCAR=7.0
Pod undocumented
282 24843 my $self = shift;
283 24843 ▶ if (!defined($self->{action})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'action'}) { } 1 24842 24843
284 1 die "Action has not yet been set.";
285 0 return 0;
286 }
287 else {
288 24842 return 1;
289 }
290 }
291  
292 15847 ▶sub buy {
Subroutine buy: called CC=2 SCAR=6.9
Pod undocumented
293 15847 my ($self, $assertion) = @_;
294 15847 ▶ if ($assertion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($assertion) { } 167 15680 15847
295 167 $self->{action} = BUY;
296 167 return 1;
297 }
298 else {
299 15680 $self->validateAction();
300 15680 return $self->{action} == BUY;
301 }
302 }
303  
304 5727 ▶sub sell {
Subroutine sell: called CC=2 SCAR=6.9
Pod undocumented
305 5727 my ($self, $assertion) = @_;
306 5727 ▶ if ($assertion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($assertion) { } 111 5616 5727
307 111 $self->{action} = SELL;
308 111 return 1;
309 }
310 else {
311 5616 $self->validateAction();
312 5615 return $self->{action} == SELL;
313 }
314 }
315  
316 3548 ▶sub short {
Subroutine short: called CC=2 SCAR=6.9
Pod undocumented
317 3548 my ($self, $assertion) = @_;
318 3548 ▶ if ($assertion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($assertion) { } 1 3547 3548
319 1 $self->{action} = SHORT;
320 1 return 1;
321 }
322 else {
323 3547 $self->validateAction();
324 3547 return $self->{action} == SHORT;
325 }
326 }
327  
328 0 ▶sub cover {
Subroutine cover: not called CC=2 SCAR=17.9
Pod undocumented
329 0 my ($self, $assertion) = @_;
330 0 ▶ if ($assertion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($assertion) { } 0 0 0
331 0 $self->{action} = COVER;
332 0 return 1;
333 }
334 else {
335 0 $self->validateAction();
336 0 return $self->{action} == COVER;
337 }
338 }
339  
340 1154 ▶sub actionString {
Subroutine actionString: called CC=5 SCAR=21.7
Pod undocumented
341 1154 my $self = shift;
342 1154 ▶ if ($self->buy()) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->buy) { } 93 1061 1154
elsif ($self->sell) { } 1061 0 1061
elsif ($self->short) { } 0 0 0
elsif ($self->cover) { } 0 0 0
343 93 return 'buy';
344 }
345 elsif ($self->sell()) {
346 1061 return 'sell';
347 }
348 elsif ($self->short()) {
349 0 return 'short';
350 }
351 elsif ($self->cover()) {
352 0 return 'cover';
353 }
354 else {
355 0 return '';
356 }
357 }
358  
359 2 ▶sub string {
Subroutine string: called CC=9 SCAR=22.3
Pod undocumented
360 2 my $self = shift;
361 2 my $pattern = "%14s %-35s\n";
362 2 my $string;
363 2 foreach my $key ($self->order()) {
364 16 ▶ if (defined($self->{$key})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{$key}) { } 14 2 16
elsif ($key eq 'date') { } 2 0 2
365 14 ▶ if ($key eq 'stock') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'stock') { } 2 12 14
366 2 my $symbol = $self->symbol();
367 2 my $exchange = $self->exchange();
368 2 ▶ if (defined($symbol)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $symbol) 2 0 2
369 2 $string .= sprintf($pattern, 'symbol', $self->symbol());
370 }
371 2 ▶ if (defined($exchange)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exchange) 0 2 2
372 0 $string .= sprintf($pattern, 'exchange', $self->exchange());
373 }
374 }
375 else {
376 12 my $value;
377 12 ▶ if ($key eq 'action') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'action') { } 2 10 12
378 2 $value = $self->actionString();
379 }
380 else {
381 10 $value = $self->{$key};
382 }
383 12 $string .= sprintf($pattern, $key, $value);
384 }
385 }
386 elsif ($key eq 'date') {
387 2 ▶ if ($self->{tm}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tm"}) 2 0 2
388 2 $string .= sprintf($pattern, $key, $self->dateString());
389 }
390 }
391 }
392 2 return $string;
393 }
394  
395 3 ▶sub lineFormatHeader {
Subroutine lineFormatHeader: called CC=1 SCAR=0.0
Pod undocumented
396 3 return sprintf($headerPattern, @headerNames);
397 }
398  
399 148 ▶sub lineFormatPattern {
Subroutine lineFormatPattern: called CC=1 SCAR=0.0
Pod undocumented
400 148 return $lineFormatPattern;
401 }
402  
403 148 ▶sub lineFormatValues {
Subroutine lineFormatValues: called CC=6 SCAR=18.2
Pod undocumented
404 148 my $self = shift;
405 return [
406 $self->{tm} || '', $self->symbol(), $self->actionString(), $self->{quantity}, $self->{price} || 0,
407 148 ▶ $self->{commission} || 0, ($self->{regulatoryFees} + $self->{otherFees}) || 0, $self->cashEffect() || 0
Condition80%8/10
$self->{'tm'} || ''
l!l
1453
$self->{'price'} || 0
l!l
1480
$self->{'commission'} || 0
l!l
12325
$self->{'regulatoryFees'} + $self->{'otherFees'} || 0
l!l
4999
$self->cashEffect || 0
l!l
1480
MC/DC100%1/1
$self->{'tm'} || ''
$self->{'tm'}
MC/DC0%0/1
$self->{'price'} || 0
$self->{'price'}
MC/DC100%1/1
$self->{'commission'} || 0
$self->{'commission'}
MC/DC100%1/1
$self->{'regulatoryFees'} + $self->{'otherFees'} || 0
$self->{'regulatoryFees'} + $self->{'otherFees'}
MC/DC0%0/1
$self->cashEffect || 0
$self->cashEffect
Truth table100%2/2
A
A= $self->{'tm'}
Aresult
11
00
Truth table50%1/2
A
A= $self->{'price'}
Aresult
11
00
Truth table100%2/2
A
A= $self->{'commission'}
Aresult
11
00
Truth table100%2/2
A
A= $self->{'regulatoryFees'} + $self->{'otherFees'}
Aresult
11
00
Truth table50%1/2
A
A= $self->cashEffect
Aresult
11
00
408 ];
409 }
410  
411 1 ▶sub lineFormatString {
Subroutine lineFormatString: called CC=1 SCAR=0.0
Pod undocumented
412 1 my $self = shift;
413 1 return sprintf($lineFormatPattern, @{$self->lineFormatValues()});
414 }
415  
416  
417 10 1;
418  
419 __END__
« StockAccount/Stock.pm ↑ Summary StockAccount/Realization.pm »