blib/lib/Basket/Calc.pm

filter: Statementstmt 70.6% 89 / 126 Branchbran 57.6% 30 / 52 Conditioncond 47.0% 8 / 17 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 84.6% 11 / 13 Podpod 100% 3 / 3 totaltotal 64.4% 143 / 222 CC 37 SCAR 31.0
CC
37
Cov
65%
add_discount
42.9
add_item
27.1
calculate
10.9
CRAP 22.2
↑ Summary
1 package Basket::Calc;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use experimental 'smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # ABSTRACT: Basket/Cart calculation library with support for currency conversion, discounts and tax
8  
9 1 our $VERSION = '0.5'; # VERSION
10  
11 1 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Finance::Currency::Convert::Yahoo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 has 'debug' => (
17 is => 'rw',
18 isa => 'Bool',
19 trigger => \&_set_debug,
20 lazy => 1,
21 1 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 1 );
23  
24 1 has 'items' => (
25 is => 'rw',
26 isa => 'ArrayRef',
27 clearer => 'empty_items',
28 );
29  
30 1 has 'discount' => (
31 is => 'rw',
32 isa => 'HashRef',
33 clearer => 'no_discount',
34 );
35  
36  
37 1 has 'currency' => (
38 is => 'rw',
39 isa => 'Str',
40 required => 1,
41 );
42  
43  
44 has 'tax' => (
45 is => 'rw',
46 isa => 'Num',
47 lazy => 1,
48 0 ▶ default => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 1 );
50  
51  
52 8 ▶sub add_item {
Subroutine add_item: called CC=15 SCAR=33.0
Pod documented
53 8 my ($self, $item) = @_;
54  
55 # make sure the input is sane
56 8 ▶ unless (ref $item eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $item eq "HASH") 0 8 8
57 0 carp "parameter has to be a HASHREF";
58 0 return;
59 }
60  
61 8 foreach my $key ('price') {
62 8 ▶ unless (exists $item->{$key} and $item->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $item->{$key} and $item->{$key}) 0 8 8
Condition33%1/3
exists $item->{$key} and $item->{$key}
!ll&&!rl&&r
008
MC/DC0%0/2
exists $item->{$key} and $item->{$key}
exists $item->{$key} $item->{$key}
Truth table33%1/3
A and B
A= exists $item->{$key}B= $item->{$key}
ABresult
0X0
100
111
63 0 carp "$key missing";
64 0 return;
65 }
66 }
67  
68 8 ▶ unless (looks_like_number($item->{price})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($item->{"price"})) 0 8 8
69 0 carp "'price' is not a number";
70 0 return;
71 }
72  
73 # calculate amount from quantity and price
74 8 ▶ if (exists $item->{quantity}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{'quantity'}) { } 4 4 8
75 4 ▶ if (!looks_like_number($item->{quantity}) || ($item->{quantity} < 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not &looks_like_number($item->{"quantity"}) or $item->{"quantity"} < 0) 0 4 4
Condition33%1/3
not &looks_like_number($item->{"quantity"}) or $item->{"quantity"} < 0
l!l&&r!l&&!r
004
MC/DC0%0/2
not &looks_like_number($item->{"quantity"}) or $item->{"quantity"} < 0
not &looks_like_number($item->{"quantity"}) $item->{"quantity"} < 0
Truth table33%1/3
not(A) or B
A= not &looks_like_number($item->{"quantity"})B= $item->{"quantity"} < 0
ABresult
1X1
011
000
76 0 carp "'quantity' is not a number or smaller than 0";
77 0 return;
78 }
79  
80 4 $item->{amount} = $item->{price} * $item->{quantity};
81 }
82 else {
83 4 $item->{amount} = $item->{price};
84 4 $item->{quantity} = 1;
85 }
86  
87 $item->{currency} = $self->currency
88 8 ▶ unless (exists $item->{currency} and $item->{currency});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $item->{"currency"} and $item->{"currency"} 6 2 8
Condition66%2/3
exists $item->{"currency"} and $item->{"currency"}
!ll&&!rl&&r
602
MC/DC50%1/2
exists $item->{"currency"} and $item->{"currency"}
exists $item->{"currency"} $item->{"currency"}
Truth table66%2/3
A and B
A= exists $item->{"currency"}B= $item->{"currency"}
ABresult
0X0
100
111
89  
90 # convert currency if needed
91 8 ▶ if ($item->{currency} ne $self->currency) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"currency"} ne $self->currency) 2 6 8
92 my $amount =
93 Finance::Currency::Convert::Yahoo::convert($item->{amount},
94 2 $item->{currency}, $self->currency);
95  
96 2 ▶ unless ($amount) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($amount) 2 0 2
97 carp "could not get "
98 . $item->{amount} . " "
99 . $item->{currency}
100 2 . " converted to "
101 . $self->currency;
102 2 return;
103 }
104  
105 0 $item->{orig_amount} = $item->{amount};
106 0 $item->{orig_currency} = $item->{currency};
107  
108 0 $item->{amount} = $amount;
109 0 $item->{currency} = $self->currency;
110 }
111  
112 6 ▶ print __PACKAGE__ . ' added item: ' . join(' ', %$item) . $/
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 6 6
113 if $self->debug;
114  
115 6 ▶ $self->items([ @{ $self->items || [] }, $item ]);
Condition100%2/2
$self->items || []
l!l
33
MC/DC100%1/1
$self->items || []
$self->items
Truth table100%2/2
A
A= $self->items
Aresult
11
00
116  
117 6 return $item;
118 }
119  
120  
121 2 ▶sub add_discount {
Subroutine add_discount: called CC=13 SCAR=37.6
Pod documented
122 2 my ($self, $discount) = @_;
123  
124 # make sure the input is sane
125 2 ▶ unless (ref $discount eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $discount eq "HASH") 0 2 2
126 0 carp "parameter has to be a HASHREF";
127 0 return;
128 }
129  
130 2 foreach my $key ('type', 'value') {
131 4 ▶ unless (exists $discount->{$key} and $discount->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $discount->{$key} and $discount->{$key}) 0 4 4
Condition33%1/3
exists $discount->{$key} and $discount->{$key}
!ll&&!rl&&r
004
MC/DC0%0/2
exists $discount->{$key} and $discount->{$key}
exists $discount->{$key} $discount->{$key}
Truth table33%1/3
A and B
A= exists $discount->{$key}B= $discount->{$key}
ABresult
0X0
100
111
132 0 carp "'$key' missing";
133 0 return;
134 }
135 }
136  
137 2 ▶ unless ($discount->{type} =~ m/^(percent|amount)$/x) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($discount->{"type"} =~ /^(percent|amount)$/x) 0 2 2
138 0 carp "'type' has to be either percent, or amount";
139 0 return;
140 }
141  
142 2 ▶ unless (looks_like_number($discount->{value})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($discount->{"value"})) 0 2 2
143 0 carp "'value' is not a number";
144 0 return;
145 }
146  
147 2 given ($discount->{type}) {
148 2 when ('percent') {
149 1 ▶ if ($discount->{value} <= 0 or $discount->{value} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($discount->{"value"} <= 0 or $discount->{"value"} > 1) 0 1 1
Condition33%1/3
$discount->{"value"} <= 0 or $discount->{"value"} > 1
l!l&&r!l&&!r
001
MC/DC0%0/2
$discount->{"value"} <= 0 or $discount->{"value"} > 1
$discount->{"value"} <= 0 $discount->{"value"} > 1
Truth table33%1/3
A or B
A= $discount->{"value"} <= 0B= $discount->{"value"} > 1
ABresult
1X1
011
000
150 0 carp "'percent' has to be a decimal between 0 and 1";
151 0 return;
152 }
153 }
154 1 when ('amount') {
155 $discount->{currency} = $self->currency
156 1 ▶ unless exists $discount->{currency};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $discount->{"currency"} 1 0 1
157  
158 # convert currency if needed
159 1 ▶ if ($discount->{currency} ne $self->currency) {
Branch: 1 branch
BranchTrueFalseTotal
if ($discount->{"currency"} ne $self->currency) 0 1 1
160 my $amount = Finance::Currency::Convert::Yahoo::convert(
161 0 $discount->{value}, $discount->{currency}, $self->currency);
162  
163 0 ▶ unless ($amount) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($amount) 0 0 0
164 carp "could not get "
165 . $discount->{value} . " "
166 . $discount->{currency}
167 0 . " converted to "
168 . $self->currency;
169 0 return;
170 }
171  
172 0 $discount->{orig_value} = $discount->{value};
173 0 $discount->{orig_currency} = $discount->{currency};
174  
175 0 $discount->{value} = $amount;
176 0 $discount->{currency} = $self->currency;
177 }
178 }
179 }
180  
181 2 ▶ print __PACKAGE__ . ' added discount: ' . join(' ', %$discount) . $/
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 2 2
182 if $self->debug;
183  
184 2 $self->discount($discount);
185  
186 2 return $self->discount;
187 }
188  
189  
190 3 ▶sub calculate {
Subroutine calculate: called CC=10 SCAR=23.9
Pod documented
191 3 my ($self) = @_;
192  
193 3 ▶ unless ($self->items) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->items) 0 3 3
194 0 carp "no items added";
195 0 return;
196 }
197  
198 3 my $total = {
199 value => 0,
200 net => 0,
201 tax_amount => 0,
202 discount => 0,
203 };
204  
205 3 ▶ print __PACKAGE__ . " -- calculating totals --\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 3 3
206  
207 # calculate net
208 3 foreach my $item (@{ $self->items }) {
209 6 ▶ print __PACKAGE__ . ' item: ' . join(' ', %$item) . $/ if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 6 6
210  
211 6 $total->{net} += $item->{amount};
212 }
213  
214 3 my $original_net = $total->{net};
215  
216 # apply discounts
217 3 ▶ if ($self->discount) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->discount) 2 1 3
218 0 ▶ print __PACKAGE__ . ' discount: ' . join(' ', %{ $self->discount }) . $/
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 2 2
219 if $self->debug;
220  
221 2 given ($self->discount->{type}) {
222 2 when ('percent') {
223 1 $total->{net} *= (1 - $self->discount->{value});
224 }
225 1 when ('amount') {
226 1 $total->{net} = $total->{net} - $self->discount->{value};
227 1 ▶ $total->{net} = 0 if $total->{net} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $total->{"net"} < 0 0 1 1
228 }
229 }
230 }
231  
232 # calculate tax
233 3 $total->{tax_amount} = $total->{net} * $self->tax;
234 3 $total->{value} = $total->{net} + $total->{tax_amount};
235 3 $total->{discount} = $original_net - $total->{net};
236  
237 # proper rounding and formatting
238 3 $total->{$_} = _round($total->{$_}) for keys %$total;
239  
240 # remind what the currency is that was requested
241 3 $total->{currency} = $self->currency;
242  
243 3 ▶ print __PACKAGE__ . ' total: ' . join(' ', %$total) . $/ if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 3 3
244  
245 3 $self->empty_items;
246 3 $self->no_discount;
247  
248 3 return $total;
249 }
250  
251 0 ▶sub _set_debug {
Subroutine _set_debug: not called CC=1 SCAR=6.9
252 0 my ($self, $value, $some) = @_;
253  
254 0 $Finance::Currency::Convert::Yahoo::CHAT = $value;
255  
256 0 return;
257 }
258  
259 12 ▶sub _round {
Subroutine _round: called CC=2 SCAR=6.9
260 12 my ($float) = @_;
261  
262 # some stupid perl versions on some platforms can't round correctly and i
263 # don't want to use more modules
264 12 ▶ $float += 0.001 if ($float =~ m/\.[0-9]{2}5/);
Branch: 1 branch
BranchTrueFalseTotal
if $float =~ /\.[0-9]{2}5/ 4 8 12
265  
266 12 return sprintf('%.2f', sprintf('%.10f', $float)) + 0;
267 }
268  
269  
270 1 1; # End of Basket::Calc
271  
272 __END__
↑ Summary