Nitesi/Cart.pm

filter: Statementstmt 92.3% 206 / 223 Branchbran 78.9% 60 / 76 Conditioncond 65.6% 21 / 32 MC/DCmcdc 35.2% 6 / 17 Subroutinesub 96.3% 26 / 27 Podpod 100% 21 / 21 totaltotal 85.8% 340 / 396 CC 59 SCAR 20.5
CC
59
Cov
87%
add
20.6
cost
7.3
_calculate
7.0
CRAP 7.8
« Nitesi/Account/Manager.pm ↑ Summary Nitesi/Account/Provider/Test.pm »
1 # Nitesi::Cart - Nitesi cart class
2  
3 package Nitesi::Cart;
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use constant CART_DEFAULT => 'main';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Nitesi::Cart - Cart class for Nitesi Shop Machine
13  
14 =head1 DESCRIPTION
15  
16 Generic cart class for L<Nitesi>.
17  
18 =head2 CART ITEMS
19  
20 Each item in the cart has at least the following attributes:
21  
22 =over 4
23  
24 =item sku
25  
26 Unique item identifier.
27  
28 =item name
29  
30 Item name.
31  
32 =item quantity
33  
34 Item quantity.
35  
36 =item price
37  
38 Item price.
39  
40 =back
41  
42 =head1 CONSTRUCTOR
43  
44 =head2 new
45  
46 =cut
47  
48 6 ▶sub new {
Subroutine new: called CC=6 SCAR=18.1
Pod documented
49 6 my ($class, $self, %args);
50  
51 6 $class = shift;
52 6 %args = @_;
53  
54 6 my $time = time;
55  
56 6 $self = {error => '', items => [], modifiers => [],
57 costs => [], subtotal => 0, total => 0,
58 cache_subtotal => 1, cache_total => 1,
59 created => $time, last_modified => $time,
60 };
61  
62 6 ▶ if ($args{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'name'}) { } 0 6 6
63 0 $self->{name} = $args{name};
64 }
65 else {
66 6 $self->{name} = CART_DEFAULT;
67 }
68  
69 6 for my $ts (qw/created last_modified/) {
70 12 ▶ if (exists $args{$ts}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{$ts}) 1 11 12
71 1 $self->{$ts} = $args{$ts};
72 }
73 }
74  
75 6 ▶ if ($args{modifiers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"modifiers"}) 0 6 6
76 0 $self->{modifiers} = $args{modifiers};
77 }
78  
79 6 ▶ if ($args{run_hooks}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"run_hooks"}) 2 4 6
80 2 $self->{run_hooks} = $args{run_hooks};
81 }
82  
83 6 bless $self, $class;
84  
85 6 $self->init(%args);
86  
87 6 return $self;
88 }
89  
90 =head2 init
91  
92 Initializer which receives the constructor arguments, but does nothing.
93 May be overridden in a subclass.
94  
95 =cut
96  
97 6 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
98 6 return 1;
99 };
100  
101 =head1 METHODS
102  
103 =head2 items
104  
105 Returns items in the cart.
106  
107 =cut
108  
109 7 ▶sub items {
Subroutine items: called CC=1 SCAR=0.0
Pod documented
110 7 my ($self) = shift;
111  
112 7 return $self->{items};
113 }
114  
115 =head2 subtotal
116  
117 Returns subtotal of the cart.
118  
119 =cut
120  
121 7 ▶sub subtotal {
Subroutine subtotal: called CC=3 SCAR=11.0
Pod documented
122 7 my ($self) = shift;
123  
124 7 ▶ if ($self->{cache_subtotal}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache_subtotal"}) 3 4 7
125 3 return $self->{subtotal};
126 }
127  
128 4 $self->{subtotal} = 0;
129  
130 4 for my $item (@{$self->{items}}) {
131 6 $self->{subtotal} += $item->{price} * $item->{quantity};
132 }
133  
134 4 $self->{cache_subtotal} = 1;
135  
136 4 return $self->{subtotal};
137 }
138  
139 =head2 total
140  
141 Returns total of the cart.
142  
143 =cut
144  
145 8 ▶sub total {
Subroutine total: called CC=2 SCAR=6.9
Pod documented
146 8 my ($self) = shift;
147 8 my ($subtotal);
148  
149 8 ▶ if ($self->{cache_total}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache_total"}) 1 7 8
150 1 return $self->{total};
151 }
152  
153 7 $self->{total} = $subtotal = $self->subtotal();
154  
155 # calculate costs
156 7 $self->{total} += $self->_calculate($subtotal);
157  
158 7 $self->{cache_total} = 1;
159  
160 7 return $self->{total};
161 }
162
163 =head2 add $item
164  
165 Add item to the cart. Returns item in case of success.
166  
167 The item is a hash (reference) which is subject to the following
168 conditions:
169  
170 =over 4
171  
172 =item sku
173  
174 Item identifier is required.
175  
176 =item name
177  
178 Item name is required.
179  
180 =item quantity
181  
182 Item quantity is optional and has to be a natural number greater
183 than zero. Default for quantity is 1.
184  
185 =item price
186  
187 Item price is required and a positive number.
188  
189 Price is required, because you want to maintain the price that was valid at the time of adding to the cart. Should the price in the shop change in the meantime, it will maintain this price. If you would like to update the pages, you have to do it before loading the cart page on your shop.
190  
191  
192 B<Example:> Add 5 BMX2012 products to the cart
193  
194 $cart->add( sku => 'BMX2012', quantity => 5, price => 200);
195  
196 B<Example:> Add a BMX2012 product to the cart.
197  
198 $cart->add( sku => 'BMX2012', price => 200);
199  
200 =back
201  
202 =cut
203  
204 12 ▶sub add {
Subroutine add: called CC=18 SCAR=30.2
Pod documented
205 12 my $self = shift;
206 12 my (%item, $ret);
207  
208 12 ▶ if (ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 12 0 12
209 # copy item
210 12 %item = %{$_[0]};
211 }
212 else {
213 0 %item = @_;
214 }
215  
216 # run hooks before validating item
217 12 $self->_run_hook('before_cart_add_validate', $self, \%item);
218  
219 # validate item
220 12 ▶ unless (exists $item{sku} && defined $item{sku} && $item{sku} =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $item{"sku"} and defined $item{"sku"} and $item{"sku"} =~ /\S/) 1 11 12
Condition66%4/6
exists $item{"sku"} and defined $item{"sku"} and $item{"sku"} =~ /\S/
!ll&&!rl&&r
1011
exists $item{"sku"} and defined $item{"sku"}
!ll&&!rl&&r
1011
MC/DC33%1/3
exists $item{"sku"} and defined $item{"sku"} and $item{"sku"} =~ /\S/
exists $item{"sku"} defined $item{"sku"} $item{"sku"} =~ /\S/
Truth table50%2/4
A and B and C
A= exists $item{"sku"}B= defined $item{"sku"}C= $item{"sku"} =~ /\S/
ABCresult
0XX0
10X0
1100
1111
221 1 $self->{error} = 'Item added without SKU.';
222 1 return;
223 }
224  
225 11 ▶ unless (exists $item{name} && defined $item{name} && $item{name} =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $item{"name"} and defined $item{"name"} and $item{"name"} =~ /\S/) 1 10 11
Condition66%4/6
exists $item{"name"} and defined $item{"name"} and $item{"name"} =~ /\S/
!ll&&!rl&&r
1010
exists $item{"name"} and defined $item{"name"}
!ll&&!rl&&r
1010
MC/DC33%1/3
exists $item{"name"} and defined $item{"name"} and $item{"name"} =~ /\S/
exists $item{"name"} defined $item{"name"} $item{"name"} =~ /\S/
Truth table50%2/4
A and B and C
A= exists $item{"name"}B= defined $item{"name"}C= $item{"name"} =~ /\S/
ABCresult
0XX0
10X0
1100
1111
226 1 $self->{error} = "Item $item{sku} added without a name.";
227 1 return;
228 }
229  
230 10 ▶ if (exists $item{quantity} && defined $item{quantity}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item{'quantity'} and defined $item{'quantity'}) { } 3 7 10
Condition66%2/3
exists $item{"quantity"} and defined $item{"quantity"}
!ll&&!rl&&r
703
MC/DC50%1/2
exists $item{"quantity"} and defined $item{"quantity"}
exists $item{"quantity"} defined $item{"quantity"}
Truth table66%2/3
A and B
A= exists $item{"quantity"}B= defined $item{"quantity"}
ABresult
0X0
100
111
231 3 ▶ unless ($item{quantity} =~ /^(\d+)$/ && $item{quantity} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item{"quantity"} =~ /^(\d+)$/ and $item{"quantity"} > 0) 0 3 3
Condition33%1/3
$item{"quantity"} =~ /^(\d+)$/ and $item{"quantity"} > 0
!ll&&!rl&&r
003
MC/DC0%0/2
$item{"quantity"} =~ /^(\d+)$/ and $item{"quantity"} > 0
$item{"quantity"} =~ /^(\d+)$/ $item{"quantity"} > 0
Truth table33%1/3
A and B
A= $item{"quantity"} =~ /^(\d+)$/B= $item{"quantity"} > 0
ABresult
0X0
100
111
232 0 $self->{error} = "Item $item{sku} added with invalid quantity $item{quantity}.";
233 0 return;
234 }
235 }
236 else {
237 7 $item{quantity} = 1;
238 }
239  
240 10 ▶ unless (exists $item{price} && defined $item{price}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $item{"price"} and defined $item{"price"} and $item{"price"} =~ /^(\d+)(\.\d+)?$/ and $item{"price"} > 0) 1 9 10
Condition66%6/9
exists $item{"price"} and defined $item{"price"} and $item{"price"} =~ /^(\d+)(\.\d+)?$/ and $item{"price"} > 0
!ll&&!rl&&r
109
exists $item{"price"} and defined $item{"price"} and $item{"price"} =~ /^(\d+)(\.\d+)?$/
!ll&&!rl&&r
109
exists $item{"price"} and defined $item{"price"}
!ll&&!rl&&r
109
MC/DC25%1/4
exists $item{"price"} and defined $item{"price"} and $item{"price"} =~ /^(\d+)(\.\d+)?$/ and $item{"price"} > 0
exists $item{"price"} defined $item{"price"} $item{"price"} =~ /^(\d+)(\.\d+)?$/ $item{"price"} > 0
Truth table40%2/5
A and B and C and D
A= exists $item{"price"}B= defined $item{"price"}C= $item{"price"} =~ /^(\d+)(\.\d+)?$/D= $item{"price"} > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
241 && $item{price} =~ /^(\d+)(\.\d+)?$/ && $item{price} > 0) {
242 1 $self->{error} = "Item $item{sku} added with invalid price.";
243 1 return;
244 }
245
246 # run hooks before adding item to cart
247 9 $self->_run_hook('before_cart_add', $self, \%item);
248  
249 9 ▶ if (exists $item{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item{"error"}) 1 8 9
250 # one of the hooks denied the item
251 1 $self->{error} = $item{error};
252 1 return;
253 }
254  
255 # clear cache flags
256 8 $self->{cache_subtotal} = $self->{cache_total} = 0;
257  
258 8 ▶ unless ($ret = $self->_combine(\%item)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret = $self->_combine(\%item)) 7 1 8
259 7 push @{$self->{items}}, \%item;
260 7 $self->{last_modified} = time;
261 }
262  
263 # run hooks after adding item to cart
264 8 $self->_run_hook('after_cart_add', $self, \%item, $ret);
265  
266 8 return \%item;
267 }
268  
269 =head2 remove $sku
270  
271 Remove item from the cart. Takes SKU of item to identify the item.
272  
273 =cut
274  
275 3 ▶sub remove {
Subroutine remove: called CC=5 SCAR=16.1
Pod documented
276 3 my ($self, $arg) = @_;
277 3 my ($pos, $found, $item);
278  
279 3 $pos = 0;
280
281 # run hooks before locating item
282 3 $self->_run_hook('before_cart_remove_validate', $self, $arg);
283  
284 3 for $item (@{$self->{items}}) {
285 4 ▶ if ($item->{sku} eq $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"sku"} eq $arg) 3 1 4
286 3 $found = 1;
287 3 last;
288 }
289 1 $pos++;
290 }
291  
292 3 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 3 0 3
293 # run hooks before adding item to cart
294 3 $item = $self->{items}->[$pos];
295  
296 3 $self->_run_hook('before_cart_remove', $self, $item);
297  
298 3 ▶ if (exists $item->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{"error"}) 1 2 3
299 # one of the hooks denied removing the item
300 1 $self->{error} = $item->{error};
301 1 return;
302 }
303  
304 # clear cache flags
305 2 $self->{cache_subtotal} = $self->{cache_total} = 0;
306  
307 # removing item from our array
308 2 splice(@{$self->{items}}, $pos, 1);
309  
310 2 $self->{last_modified} = time;
311  
312 2 $self->_run_hook('after_cart_remove', $self, $item);
313 2 return 1;
314 }
315  
316 # item missing
317 0 $self->{error} = "Missing item $arg.";
318  
319 0 return;
320 }
321  
322 =head2 update
323  
324 Update quantity of items in the cart.
325  
326 Parameters are pairs of SKUs and quantities, e.g.
327  
328 $cart->update(9780977920174 => 5,
329 9780596004927 => 3);
330  
331 Triggers before_cart_update and after_cart_update hooks.
332  
333 A quantity of zero is equivalent to removing this item,
334 so in this case the remove hooks will be invoked instead
335 of the update hooks.
336  
337 =cut
338  
339 3 ▶sub update {
Subroutine update: called CC=6 SCAR=18.2
Pod documented
340 3 my ($self, @args) = @_;
341 3 my ($ref, $sku, $qty, $item, $new_item);
342  
343 3 ▶ while (@args > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args > 0) 4 3 7
344 4 $sku = shift @args;
345 4 $qty = shift @args;
346  
347 4 ▶ unless ($item = $self->find($sku)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item = $self->find($sku)) 0 4 4
348 0 die "Item for $sku not found in cart.\n";
349 }
350  
351 4 ▶ if ($qty == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qty == 0) 1 3 4
352 # remove item instead
353 1 $self->remove($sku);
354 1 next;
355 }
356  
357 # jump to next item if quantity stays the same
358 3 ▶ next if $qty == $item->{quantity};
Branch: 1 branch
BranchTrueFalseTotal
if $qty == $item->{"quantity"} 1 2 3
359  
360 # run hook before updating the cart
361 2 $new_item = {quantity => $qty};
362  
363 2 $self->_run_hook('before_cart_update', $self, $item, $new_item);
364  
365 2 ▶ if (exists $new_item->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $new_item->{"error"}) 0 2 2
366 # one of the hooks denied the item
367 0 $self->{error} = $new_item->{error};
368 0 return;
369 }
370  
371 2 $self->{last_modified} = time;
372  
373 2 $self->_run_hook('after_cart_update', $self, $item, $new_item);
374  
375 2 $item->{quantity} = $qty;
376 }
377 }
378  
379 =head2 clear
380  
381 Removes all items from the cart.
382  
383 =cut
384  
385 2 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
386 2 my ($self) = @_;
387  
388 # run hook before clearing the cart
389 2 $self->_run_hook('before_cart_clear', $self);
390
391 2 $self->{items} = [];
392  
393 # run hook after clearing the cart
394 2 $self->_run_hook('after_cart_clear', $self);
395  
396 # reset subtotal/total
397 2 $self->{subtotal} = 0;
398 2 $self->{total} = 0;
399 2 $self->{cache_subtotal} = 1;
400 2 $self->{cache_total} = 1;
401  
402 2 $self->{last_modified} = time;
403  
404 2 return;
405 }
406  
407 =head2 find
408  
409 Searches for an cart item with the given SKU.
410 Returns cart item in case of sucess.
411  
412 if ($item = $cart->find(9780977920174)) {
413 print "Quantity: $item->{quantity}.\n";
414 }
415  
416 =cut
417  
418 4 ▶sub find {
Subroutine find: called CC=3 SCAR=11.0
Pod documented
419 4 my ($self, $sku) = @_;
420  
421 4 for my $cartitem (@{$self->{items}}) {
422 5 ▶ if ($sku eq $cartitem->{sku}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sku eq $cartitem->{"sku"}) 4 1 5
423 4 return $cartitem;
424 }
425 }
426  
427 0 return;
428 }
429  
430 =head2 quantity
431  
432 Returns the sum of the quantity of all items in the shopping cart,
433 which is commonly used as number of items. If you have 5 apples and 6 pears it will return 11.
434  
435 print 'Items in your cart: ', $cart->quantity, "\n";
436  
437 =cut
438  
439 5 ▶sub quantity {
Subroutine quantity: called CC=2 SCAR=6.9
Pod documented
440 5 my $self = shift;
441 5 my $qty = 0;
442  
443 5 for my $item (@{$self->{items}}) {
444 7 $qty += $item->{quantity};
445 }
446  
447 5 return $qty;
448 }
449  
450 =head2 created
451  
452 Returns the time (epoch) when the cart was created.
453  
454 =cut
455  
456 1 ▶sub created {
Subroutine created: called CC=1 SCAR=0.0
Pod documented
457 1 my ($self) = @_;
458  
459 1 return $self->{created};
460 }
461  
462 =head2 last_modified
463  
464 Returns the time (epoch) when the cart was last modified.
465  
466 =cut
467  
468 5 ▶sub last_modified {
Subroutine last_modified: called CC=1 SCAR=0.0
Pod documented
469 5 my ($self) = @_;
470  
471 5 return $self->{last_modified};
472 }
473  
474 =head2 count
475  
476 Returns the number of different items in the shopping cart. If you have 5 apples and 6 pears it will return 2 (2 different items).
477  
478 =cut
479  
480 5 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
481 5 my $self = shift;
482  
483 5 return scalar(@{$self->{items}});
484 }
485  
486 =head2 apply_cost
487  
488 Apply cost to cart. apply_cost is a generic method typicaly used for taxes, discounts, coupons, gift certificates,...
489  
490 B<Example:> Absolute cost
491  
492 Uses absolute value for amount. Amount 5 is 5 units of currency used (ie. $5).
493  
494 $cart->apply_cost(amount => 5, name => 'shipping', label => 'Shipping');
495  
496 B<Example:> Relative cost
497  
498 Uses percentage instead of value for amount. Amount 0.19 in example is 19%.
499  
500 relative is a boolean value (0/1).
501  
502 $cart->apply_cost(amount => 0.19, name => 'tax', label => 'VAT', relative => 1);
503  
504 B<Example:> Inclusive cost
505  
506 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).
507  
508 $cart->apply_cost(amount => 0.19, name => 'tax', label => 'Sales Tax', relative => 1, inclusive => 1);
509  
510 =cut
511  
512 4 ▶sub apply_cost {
Subroutine apply_cost: called CC=2 SCAR=6.9
Pod documented
513 4 my ($self, %args) = @_;
514  
515 4 push @{$self->{costs}}, \%args;
516  
517 4 ▶ unless ($args{inclusive}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"inclusive"}) 3 1 4
518 # clear cache for total
519 3 $self->{cache_total} = 0;
520 }
521 }
522  
523 =head2 clear_cost
524  
525 It removes all the costs previously applied (using apply_cost). Used typically if you have free shipping or something similar, you can clear the costs.
526  
527 =cut
528  
529 4 ▶sub clear_cost {
Subroutine clear_cost: called CC=1 SCAR=0.0
Pod documented
530 4 my $self = shift;
531  
532 4 $self->{costs} = [];
533  
534 4 $self->{cache_total} = 0;
535 }
536  
537 =head2 cost
538  
539 Returns particular cost by position or by name.
540  
541 B<Example:> Return tax value by name
542
543 $cart->cost('tax');
544  
545 Returns value of the tax (absolute value in your currency, not percantage)
546  
547 B<Example:> Return tax value by position
548  
549 $cart->cost(0);
550  
551 Returns the cost that was first applied to subtotal. By increasing the number you can retrieve other costs applied.
552  
553 =cut
554  
555 6 ▶sub cost {
Subroutine cost: called CC=7 SCAR=19.9
Pod documented
556 6 my ($self, $loc) = @_;
557 6 my ($cost, $ret);
558  
559 6 ▶ if (defined $loc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $loc) 6 0 6
560 6 ▶ if ($loc =~ /^\d+/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($loc =~ /^\d+/) { } 3 3 6
elsif ($loc =~ /\S/) { } 3 0 3
561 # cost by position
562 3 $cost = $self->{costs}->[$loc];
563 }
564 elsif ($loc =~ /\S/) {
565 # cost by name
566 3 for my $c (@{$self->{costs}}) {
567 3 ▶ if ($c->{name} eq $loc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"name"} eq $loc) 3 0 3
568 3 $cost = $c;
569 }
570 }
571 }
572 }
573  
574 6 ▶ if (defined $cost) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cost) 6 0 6
575 6 $ret = $self->_calculate($self->{subtotal}, $cost, 1);
576 }
577  
578 6 return $ret;
579 }
580  
581 =head2 id
582  
583 Get or set id of the cart. This can be used for subclasses,
584 e.g. primary key value for carts in the database.
585  
586 =cut
587  
588 0 ▶sub id {
Subroutine id: not called CC=2 SCAR=17.9
Pod documented
589 0 my $self = shift;
590  
591 0 ▶ if (@_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 0 0 0
592 0 $self->{id} = $_[0];
593 }
594  
595 0 return $self->{id};
596 }
597  
598 =head2 name
599  
600 Get or set the name of the cart.
601  
602 =cut
603  
604 2 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
605 2 my $self = shift;
606  
607 2 ▶ if (@_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 1 1 2
608 1 my $old_name = $self->{name};
609  
610 1 $self->_run_hook('before_cart_rename', $self, $old_name, $_[0]);
611  
612 1 $self->{name} = $_[0];
613 1 $self->{last_modified} = time;
614  
615 1 $self->_run_hook('after_cart_rename', $self, $old_name, $_[0]);
616 }
617  
618 2 return $self->{name};
619 }
620  
621 =head2 error
622  
623 Returns last error.
624  
625 =cut
626  
627 10 ▶sub error {
Subroutine error: called CC=1 SCAR=0.0
Pod documented
628 10 my $self = shift;
629  
630 10 return $self->{error};
631 }
632  
633 =head2 seed $item_ref
634  
635 Seeds items within the cart from $item_ref.
636  
637 B<Example:>
638  
639 $cart->seed([
640 { sku => 'BMX2015', price => 20, quantity = 1 },
641 { sku => 'KTM2018', price => 400, quantity = 5 },
642 { sku => 'DBF2020', price => 200, quantity = 5 },
643 ]);
644  
645 =cut
646  
647 1 ▶sub seed {
Subroutine seed: called CC=2 SCAR=7.0
Pod documented
648 1 my ($self, $item_ref) = @_;
649  
650 1 ▶ @{$self->{items}} = @{$item_ref || []};
Condition50%1/2
$item_ref || []
l!l
10
MC/DC0%0/1
$item_ref || []
$item_ref
Truth table50%1/2
A
A= $item_ref
Aresult
11
00
651  
652 # clear cache flags
653 1 $self->{cache_subtotal} = $self->{cache_total} = 0;
654  
655 1 $self->{last_modified} = time;
656  
657 1 return $self->{items};
658 }
659  
660 8 ▶sub _combine {
Subroutine _combine: called CC=5 SCAR=16.5
661 8 my ($self, $item) = @_;
662  
663 8 ITEMS: for my $cartitem (@{$self->{items}}) {
664 4 ▶ if ($item->{sku} eq $cartitem->{sku}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"sku"} eq $cartitem->{"sku"}) 1 3 4
665 1 for my $mod (@{$self->{modifiers}}) {
666 0 ▶ next ITEMS unless($item->{$mod} eq $cartitem->{$mod});
Branch: 1 branch
BranchTrueFalseTotal
unless $item->{$mod} eq $cartitem->{$mod} 0 0 0
667 }
668
669 1 $cartitem->{'quantity'} += $item->{'quantity'};
670 1 $item->{'quantity'} = $cartitem->{'quantity'};
671  
672 1 return 1;
673 }
674 }
675  
676 7 return 0;
677 }
678  
679 13 ▶sub _calculate {
Subroutine _calculate: called CC=7 SCAR=19.5
680 13 my ($self, $subtotal, $costs, $display) = @_;
681 13 my ($cost_ref, $sum);
682  
683 13 ▶ if (ref $costs eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $costs eq 'HASH') { } 6 7 13
elsif (ref $costs eq 'ARRAY') { } 0 7 7
684 6 $cost_ref = [$costs];
685 }
686 elsif (ref $costs eq 'ARRAY') {
687 0 $cost_ref = $costs;
688 }
689 else {
690 7 $cost_ref = $self->{costs};
691 }
692  
693 13 $sum = 0;
694  
695 13 for my $calc (@$cost_ref) {
696 10 ▶ if ($calc->{inclusive} && ! $display) {
Branch: 1 branch
BranchTrueFalseTotal
if ($calc->{"inclusive"} and not $display) 1 9 10
Condition100%3/3
$calc->{"inclusive"} and not $display
!ll&&!rl&&r
721
MC/DC100%2/2
$calc->{"inclusive"} and not $display
$calc->{"inclusive"} not $display
Truth table100%3/3
A and not(B)
A= $calc->{"inclusive"}B= not $display
ABresult
0X0
100
111
697 1 next;
698 }
699  
700 9 ▶ if ($calc->{relative}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($calc->{'relative'}) { } 6 3 9
701 6 $sum += $subtotal * $calc->{amount};
702 }
703 else {
704 3 $sum += $calc->{amount};
705 }
706 }
707  
708 13 return $sum;
709 }
710  
711 47 ▶sub _run_hook {
Subroutine _run_hook: called CC=2 SCAR=6.9
712 47 my ($self, $name, @args) = @_;
713 47 my $ret;
714  
715 47 ▶ if ($self->{run_hooks}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"run_hooks"}) 21 26 47
716 21 $ret = $self->{run_hooks}->($name, @args);
717 }
718  
719 47 return $ret;
720 }
721  
722 =head1 AUTHOR
723  
724 Stefan Hornburg (Racke), <racke@linuxia.de>
725  
726 =head1 LICENSE AND COPYRIGHT
727  
728 Copyright 2011-2013 Stefan Hornburg (Racke) <racke@linuxia.de>.
729  
730 This program is free software; you can redistribute it and/or modify it
731 under the terms of either: the GNU General Public License as published
732 by the Free Software Foundation; or the Artistic License.
733  
734 See http://dev.perl.org/licenses/ for more information.
735  
736 =cut
737  
738 2 1;
« Nitesi/Account/Manager.pm ↑ Summary Nitesi/Account/Provider/Test.pm »