| 1 | # ------------------------------------------------------------------------------------- | ||
| 2 | # MKDoc::ECommerce::Basket | ||
| 3 | # ------------------------------------------------------------------------------------- | ||
| 4 | # Author : Jean-Michel Hiver <jhiver@mkdoc.com>. | ||
| 5 | # Copyright : (c) MKDoc Holdings Ltd, 2003 | ||
| 6 | # | ||
| 7 | # This object represents a collection of Item objects. | ||
| 8 | # ------------------------------------------------------------------------------------- | ||
| 9 | package MKDoc::ECommerce::Basket; | ||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | |||
| 14 | ## | ||
| 15 | # $class->new(); | ||
| 16 | # -------------- | ||
| 17 | # Instanciates a new basket object. | ||
| 18 | ## | ||
| 19 | sub new | ||
| 20 | { | ||
| 21 | my $class = shift; | ||
| 22 | return bless { items => [] }, $class; | ||
| 23 | } | ||
| 24 | |||
| 25 | |||
| 26 | ## | ||
| 27 | # $self->clear(); | ||
| 28 | # --------------- | ||
| 29 | # Completely clears the shopping basket. | ||
| 30 | ## | ||
| 31 | sub clear | ||
| 32 | { | ||
| 33 | my $self = shift; | ||
| 34 | $self->{items} = []; | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | ## | ||
| 39 | # $self->add ($item); | ||
| 40 | # ------------------- | ||
| 41 | # Adds $item in quantity $qty. If not specified, | ||
| 42 | # $qty defaults to 1. | ||
| 43 | ## | ||
| 44 | sub add | ||
| 45 | { | ||
| 46 | my $self = shift; | ||
| 47 | my $item_to_add = shift; | ||
| 48 | my $item_existing = $self->item ($item_to_add); | ||
| 49 | if ($item_existing) | ||
| 50 | { | ||
| 51 | $item_existing->set_quantity ($item_existing->quantity() + $item_to_add->quantity()); | ||
| 52 | } | ||
| 53 | else | ||
| 54 | { | ||
| 55 | push @{$self->{items}}, $item_to_add; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | ## | ||
| 61 | # $self->delete ($item); | ||
| 62 | # ---------------------- | ||
| 63 | # Deletes item $item from basket. | ||
| 64 | ## | ||
| 65 | sub delete | ||
| 66 | { | ||
| 67 | my $self = shift; | ||
| 68 | my $ref = shift; | ||
| 69 | $ref = $ref->reference() if (ref $ref); | ||
| 70 | |||
| 71 | my @items = @{$self->{items}}; | ||
| 72 | @items = map { ($_->reference() eq $ref) ? () : $_ } @items; | ||
| 73 | $self->{items} = \@items; | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | ## | ||
| 78 | # $self->items(); | ||
| 79 | # --------------- | ||
| 80 | # Returns a list of items from the basket. | ||
| 81 | ## | ||
| 82 | sub items | ||
| 83 | { | ||
| 84 | my $self = shift; | ||
| 85 | my @items = @{$self->{items}}; | ||
| 86 | return wantarray ? @items : \@items; | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | ## | ||
| 91 | # $self->item ($ref_id); | ||
| 92 | # ---------------------- | ||
| 93 | # Returns a particular item from the basket. | ||
| 94 | ## | ||
| 95 | sub item | ||
| 96 | { | ||
| 97 | my $self = shift; | ||
| 98 | my $ref = shift; | ||
| 99 | $ref = $ref->reference() if (ref $ref); | ||
| 100 | |||
| 101 | my @items = map { $_ ? $_ : () } @{$self->{items}}; | ||
| 102 | @items = map { ($_->reference() eq $ref) ? $_ : () } @items; | ||
| 103 | |||
| 104 | return $items[0]; | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | ## | ||
| 109 | # $self->total(); | ||
| 110 | # --------------- | ||
| 111 | # Returns the total price for the basket. | ||
| 112 | ## | ||
| 113 | sub total | ||
| 114 | { | ||
| 115 | my $self = shift; | ||
| 116 | my $total = 0; | ||
| 117 | foreach my $item ($self->items()) { $total += $item->total() } | ||
| 118 | return $total; | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | ## | ||
| 123 | # $self->total_formatted(); | ||
| 124 | # ------------------------- | ||
| 125 | # Same as total(), but rounded at two decimals. | ||
| 126 | ## | ||
| 127 | sub total_formatted | ||
| 128 | { | ||
| 129 | my $self = shift; | ||
| 130 | return sprintf ("%.2f", $self->total()); | ||
| 131 | } | ||
| 132 | |||
| 133 | |||
| 134 | ## | ||
| 135 | # $self->count(); | ||
| 136 | # --------------- | ||
| 137 | # Counts the amount of items in the basket, including | ||
| 138 | # the quantities associated with these items. | ||
| 139 | ## | ||
| 140 | sub count | ||
| 141 | { | ||
| 142 | my $self = shift; | ||
| 143 | my $total = 0; | ||
| 144 | foreach my $item ($self->items()) { $total += $item->quantity() } | ||
| 145 | return $total; | ||
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | sub set_quantity | ||
| 150 | { | ||
| 151 | my $self = shift; | ||
| 152 | my $ref = shift; | ||
| 153 | my $qty = shift; | ||
| 154 | my $item = $self->item ($ref) || return; | ||
| 155 | $item->set_quantity ($qty); | ||
| 156 | } | ||
| 157 | |||
| 158 | 1; | ||
| 159 | |||
| 160 | |||
| 161 | __END__ |