Interchange6/Role/Costs.pm

filter: Statementstmt 100% 62 / 62 Branchbran 100% 22 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 100% 99 / 99 CC 14 SCAR 13.5
CC
14
Cov
100%
cost
7.0
_build_total
5.0
apply_cost
4.0
CRAP 3.8
↑ Summary Interchange6/Cart.pm »
1 # Interchange6::Role::Costs - Interchange6 costs role for carts and products
2  
3 package Interchange6::Role::Costs;
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Interchange6::Cart::Cost;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Types::Standard qw/ArrayRef InstanceOf Num/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use MooX::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use MooseX::CoverableModifiers;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 ATTRIBUTES
16  
17 =head2 costs
18  
19 Holds an array reference of L<Interchange::Cart::Cost> items.
20  
21 When called without arguments returns an array reference of all costs associated with the object. Costs are ordered according to the order they were applied.
22  
23 =cut
24  
25 has costs => (
26 is => 'ro',
27 isa => ArrayRef [ InstanceOf ['Interchange::Cart::Cost'] ],
28 55 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 4 handles_via => 'Array',
30 handles => {
31 clear_costs => 'clear',
32 cost_get => 'get',
33 cost_set => 'set',
34 cost_count => 'count',
35 cost_push => 'push',
36 get_costs => 'elements',
37 },
38 init_arg => undef,
39 );
40  
41 after 'clear_costs', 'cost_set', 'cost_push' => sub {
42 16 ▶ shift->clear_total;
Subroutine __ANON__: called CC=1 SCAR=0.0
43 4 };
44  
45 =head2 total
46  
47 Returns the sum of the objects L</costs> added to its C<subtotal>.
48  
49 =cut
50  
51 4 has total => (
52 is => 'lazy',
53 isa => Num,
54 clearer => 1,
55 predicate => 1,
56 );
57  
58 sub _build_total {
59 74 ▶ my $self = shift;
Subroutine _build_total: called CC=5 SCAR=16.1
60  
61 74 my @costs = $self->get_costs;
62 74 my $subtotal = $self->subtotal;
63  
64 74 my $sum = 0;
65 74 foreach my $i ( 0 .. $#costs ) {
66  
67 27 ▶ if ( $costs[$i]->relative ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($costs[$i]->relative) { } 14 13 27
68 14 $costs[$i]->set_current_amount( $subtotal * $costs[$i]->amount );
69 }
70 else {
71 13 $costs[$i]->set_current_amount( $costs[$i]->amount );
72 }
73  
74 27 ▶ if ( $costs[$i]->compound ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($costs[$i]->compound) { } 11 16 27
elsif (not $costs[$i]->inclusive) { } 11 5 16
75 11 $subtotal += $costs[$i]->current_amount;
76 }
77 elsif ( !$costs[$i]->inclusive ) {
78 11 $sum += $costs[$i]->current_amount;
79 }
80 }
81  
82 74 return sprintf( "%.2f", $subtotal + $sum );
83 }
84  
85 =head1 METHODS
86  
87 =head2 clear_costs
88  
89 Removes all the costs previously applied (using apply_cost). Used typically if you have free shipping or something similar, you can clear the costs.
90  
91 This method also calls L</clear_total>.
92  
93 =head2 clear_total
94  
95 Clears L</total>.
96  
97 =head2 cost_get($index)
98  
99 Returns an element of the array of costs for the object by its index. You can also use negative index numbers, just as with Perl's core array handling.
100  
101 =head2 cost_count
102  
103 Returns the number of cost elements for the object.
104  
105 =head2 cost_push($cost)
106  
107 Like Perl's normal C<push> this adds the supplied L<Interchange::Cart::Cost>
108 to L</costs>.
109  
110 This method also calls L</clear_total>.
111  
112 =head2 get_costs
113  
114 Returns all of the cost elements for the object as an array (not an arrayref).
115  
116 =head2 cost_set($index, $cost)
117  
118 Sets the cost at C<$index> to <$cost>.
119  
120 This method also calls L</clear_total>.
121  
122 =head2 has_total
123  
124 predicate on L</total>.
125  
126 =head2 apply_cost
127  
128 Apply cost to object. L</apply_cost> is a generic method typicaly used for taxes, discounts, coupons, gift certificates, etc.
129  
130 The cost is added using L</cost_push>.
131  
132 B<Example:> Absolute cost
133  
134 Uses absolute value for amount. Amount 5 is 5 units of currency used (i.e. $5).
135  
136 $cart->apply_cost(amount => 5, name => 'shipping', label => 'Shipping');
137  
138 B<Example:> Relative cost
139  
140 Uses percentage instead of value for amount. Relative is a boolean value (0/1).
141  
142 Add 19% German VAT:
143  
144 $cart->apply_cost(
145 amount => 0.19, name => 'tax', label => 'VAT', relative => 1
146 );
147  
148 Add 10% discount (negative amount):
149  
150 $cart->apply_cost(
151 amount => -0.1, name => 'discount', label => 'Discount', relative => 1
152 );
153  
154  
155 B<Example:> Inclusive cost
156  
157 Same as relative cost, but it assumes that tax was included in the subtotal already, and only displays it (19% of subtotal value in example). Inclusive is a boolean value (0/1).
158  
159 $cart->apply_cost(amount => 0.19, name => 'tax', label => 'Sales Tax', relative => 1, inclusive => 1);
160  
161 =cut
162  
163 sub apply_cost {
164 14 ▶ my $self = shift;
Subroutine apply_cost: called CC=4 SCAR=13.9
Pod documented
165 14 my $cost = $_[0];
166  
167 14 ▶ die "argument to apply_cost undefined" unless defined($cost);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cost 1 13 14
168  
169 13 ▶ if ( blessed($cost) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($cost)) { } 2 11 13
170 2 ▶ die( "Supplied cost not an Interchange6::Cart::Cost : " . ref($cost) )
Branch: 1 branch
BranchTrueFalseTotal
unless $cost->isa("Interchange6::Cart::Cost") 1 1 2
171 unless $cost->isa('Interchange6::Cart::Cost');
172 }
173 else {
174 11 $cost = Interchange6::Cart::Cost->new(@_);
175 }
176  
177 12 $self->cost_push($cost);
178 }
179  
180 =head2 cost
181  
182 Returns particular cost by position or by name.
183  
184 B<Example:> Return tax value by name
185  
186 $cart->cost('tax');
187  
188 Returns value of the tax (absolute value in your currency, not percentage)
189  
190 B<Example:> Return tax value by position
191  
192 $cart->cost(0);
193  
194 Returns the cost that was first applied to subtotal. By increasing the number you can retrieve other costs applied.
195  
196 =cut
197  
198 sub cost {
199 24 ▶ my ( $self, $loc ) = @_;
Subroutine cost: called CC=7 SCAR=19.5
Pod documented
200 24 my ( $cost, $ret );
201  
202 24 ▶ if ( defined $loc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $loc) { } 23 1 24
203 23 ▶ if ( $loc =~ /^\d+$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($loc =~ /^\d+$/) { } 11 12 23
elsif ($loc =~ /\S/) { } 11 1 12
204  
205 # cost by position
206 11 $cost = $self->cost_get($loc);
207 }
208 elsif ( $loc =~ /\S/ ) {
209  
210 # cost by name
211 11 for my $c ( $self->get_costs ) {
212 18 ▶ if ( $c->name eq $loc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->name eq $loc) 10 8 18
213 10 $cost = $c;
214 }
215 }
216 }
217 }
218 else {
219 1 die "Either position or name required as argument to cost";
220 }
221  
222 23 ▶ if ( defined $cost ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cost) { } 20 3 23
223 # calculate total in order to reset all costs
224 20 $self->total;
225 }
226 else {
227 3 die "Bad argument to cost: " . $loc;
228 }
229  
230 20 return $cost->current_amount;
231 }
232  
233 4 1;
↑ Summary Interchange6/Cart.pm »