From 112834a9b7043d5952156d9c75fc92ea6cf3bf7c Mon Sep 17 00:00:00 2001 From: JT Smith Date: Wed, 5 Mar 2008 16:32:08 +0000 Subject: [PATCH] added some helper methods for addressing, and got the cart closer to working --- lib/WebGUI/Shop/Address.pm | 19 +++++++++ lib/WebGUI/Shop/Cart.pm | 85 ++++++++++++++++++++++++++++++++++--- lib/WebGUI/Shop/CartItem.pm | 13 ++++++ lib/WebGUI/Shop/Tax.pm | 3 +- 4 files changed, 112 insertions(+), 8 deletions(-) diff --git a/lib/WebGUI/Shop/Address.pm b/lib/WebGUI/Shop/Address.pm index 6ed8377bf..25ef86387 100644 --- a/lib/WebGUI/Shop/Address.pm +++ b/lib/WebGUI/Shop/Address.pm @@ -103,6 +103,25 @@ sub get { #------------------------------------------------------------------- +=head2 getHtmlFormatted () + +Returns an HTML formatted address for display. + +=cut + +sub getHtmlFormatted { + my $self = shift; + my $address = $self->get("name") . "
" . $self->get("address1") . "
"; + $address .= $self->get("address2") . "
" if ($self->get("address2") ne ""); + $address .= $self->get("address3") . "
" if ($self->get("address3") ne ""); + $address .= $self->get("city") . ","; + $address .= $self->get("state") . " " if ($self->get("state") ne ""); + $address .= $self->get("code") if ($self->get("code") ne ""); + $address .= '
' . $self->get("country"); +} + +#------------------------------------------------------------------- + =head2 getId () Returns the unique id of this item. diff --git a/lib/WebGUI/Shop/Cart.pm b/lib/WebGUI/Shop/Cart.pm index d887de0f5..ecaf5600e 100644 --- a/lib/WebGUI/Shop/Cart.pm +++ b/lib/WebGUI/Shop/Cart.pm @@ -6,6 +6,7 @@ use Class::InsideOut qw{ :std }; use WebGUI::Asset::Template; use WebGUI::Exception::Shop; use WebGUI::International; +use WebGUI::Shop::AddressBook; use WebGUI::Shop::CartItem; # use WebGUI::Shop::Coupon; use WebGUI::Shop::Ship; @@ -53,6 +54,26 @@ sub addItem { return $item; } +#------------------------------------------------------------------- + +=head2 calculateSubtotal () + +Returns the subtotal of the items in the cart. + +=cut + +sub calculateSubtotal { + my $self = shift; + my $subtotal = 0; + foreach my $item (@{$self->getItems}) { + my $sku = $item->getSku; + $subtotal += $sku->getPrice * $item->get("quantity"); + } + return $subtotal; +} + + + #------------------------------------------------------------------- =head2 create ( session ) @@ -110,6 +131,23 @@ sub empty { #------------------------------------------------------------------- +=head2 formatCurrency ( amount ) + +Formats a number as a float with two digits after the decimal like 0.00. + +=head3 amount + +The number to format. + +=cut + +sub formatCurrency { + my ($self, $amount) = @_; + return sprintf("%.2f", $amount); +} + +#------------------------------------------------------------------- + =head2 get ( [ property ] ) Returns a duplicated hash reference of this object’s data. @@ -131,6 +169,19 @@ sub get { #------------------------------------------------------------------- +=head2 getAddressBook () + +Returns a reference to the address book for the user who's cart this is. + +=cut + +sub getAddressBook { + my $self = shift; + return WebGUI::Shop::AddressBook->create($self->session); +} + +#------------------------------------------------------------------- + =head2 getId () Returns the unique id for this cart. @@ -162,6 +213,19 @@ sub getItems { #------------------------------------------------------------------- +=head2 getShippingAddress () + +Returns the WebGUI::Shop::Address object that is attached to this cart for shipping. + +=cut + +sub getShippingAddress { + my $self = shift; + return $self->getAddressBook->getAddress($self->get("shippingAddressId")); +} + +#------------------------------------------------------------------- + =head2 new ( session, cartId ) Constructor. Instanciates a cart based upon a cartId. @@ -297,16 +361,20 @@ sub www_view { quantityField => WebGUI::Form::integer($session, {name=>"quantity-".$item->getId, value=>$item->get("quantity")}), isUnique => ($sku->getMaxAllowedInCart == 1), isShippable => $sku->isShippingRequired, - extendedPrice => sprintf("%.2f", ($sku->getPrice * $item->get("quantity"))), - price => sprintf("%.2f", $sku->getPrice), + extendedPrice => $self->formatCurrency($sku->getPrice * $item->get("quantity")), + price => $self->formatCurrency($sku->getPrice), removeButton => WebGUI::Form::submit($session, {value=>$i18n->get("remove button"), extras=>q|onclick="this.form.method.value='removeItem';this.form.itemId.value='|.$item->getId.q|';this.form.submit;"|}), - shippingAddress => "todo", shipToButton => WebGUI::Form::submit($session, {value=>$i18n->get("ship to button"), extras=>q|onclick="this.form.shop.value='address';this.form.method.value='view';this.form.itemId.value='|.$item->getId.q|';this.form.submit;"|}), ); + my $address = eval { $item->getShippingAddress }; + unless (WebGUI::Error->caught) { + $properties{shippingAddress} = $address->getHtmlFormatted; + } push(@items, \%properties); } + my $tax = WebGUI::Shop::Tax->new($self->session); my %var = ( %{$self->get}, items => \@items, @@ -323,17 +391,20 @@ sub www_view { extras=>q|onclick="this.form.method.value='continueShopping';this.form.submit;"|}), chooseShippingButton => WebGUI::Form::submit($session, {value=>$i18n->get("choose shipping button"), extras=>q|onclick="this.form.shop.value='address';this.form.method.value='view';this.form.submit;"|}), - shipppingAddress => "todo", shippingOptions => "todo", shipToButton => WebGUI::Form::submit($session, {value=>$i18n->get("ship to button"), extras=>q|onclick="this.form.shop.value='address';this.form.method.value='view';this.form.submit;"|}), - hasShippingAddress => "todo", + hasShippingAddress => ($self->get("shippingAddressId") ne ""), couponField => WebGUI::Form::text($session, {name=>"couponCode", value=>"", size=>20}), couponDiscount => "todo", totalPrice => "todo", - tax => "todo", - subtotalPrice => "todo", + tax => $self->formatCurrency($tax->calculate($self)), + subtotalPrice => $self->formatCurrency($self->calculateSubtotal()), ); + my $address = eval { $self->getShippingAddress }; + unless (WebGUI::Error->caught) { + $var{shippingAddress} = $address->getHtmlFormatted; + } my $template = WebGUI::Asset::Template->new($session, $session->setting->get("shopCartTemplateId")); $template->prepare; return $session->style->userStyle($template->process(\%var)); diff --git a/lib/WebGUI/Shop/CartItem.pm b/lib/WebGUI/Shop/CartItem.pm index 43bc36cd7..a2babf543 100644 --- a/lib/WebGUI/Shop/CartItem.pm +++ b/lib/WebGUI/Shop/CartItem.pm @@ -112,6 +112,19 @@ sub getId { } +#------------------------------------------------------------------- + +=head2 getShippingAddress () + +Returns the WebGUI::Shop::Address object that is attached to this item for shipping. + +=cut + +sub getShippingAddress { + my $self = shift; + return $self->cart->getAddressBook->getAddress($self->get("shippingAddressId")); +} + #------------------------------------------------------------------- =head2 getSku ( ) diff --git a/lib/WebGUI/Shop/Tax.pm b/lib/WebGUI/Shop/Tax.pm index 54974cdae..c670c2d1b 100644 --- a/lib/WebGUI/Shop/Tax.pm +++ b/lib/WebGUI/Shop/Tax.pm @@ -88,7 +88,7 @@ sub add { Calculate the tax for the contents of the cart. The tax rate is calculated off of the shipping address stored in the cart. If an item in the cart has an alternate address, that is used instead. Finally, if the item in the cart has a Sku with a tax -rate override, that rate overrides all. +rate override, that rate overrides all. Returns 0 if no shipping address has been attached to the cart yet. =cut @@ -98,6 +98,7 @@ sub calculate { WebGUI::Error::InvalidParam->throw(error => 'Must pass in a WebGUI::Shop::Cart object') unless ref($cart) eq 'WebGUI::Shop::Cart'; my $book = WebGUI::Shop::AddressBook->create($self->session); + return 0 if $cart->get('shippingAddressId') eq ""; my $address = $book->getAddress($cart->get('shippingAddressId')); my $tax = 0; foreach my $item (@{ $cart->getItems }) {