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