a number of bug fixes

This commit is contained in:
JT Smith 2008-04-04 22:42:54 +00:00
parent cdc48cf9f6
commit 8dd5a6bd2c
9 changed files with 134 additions and 145 deletions

View file

@ -199,9 +199,6 @@ sub convertTransactionLog {
paymentPhoneNumber varchar(35), paymentPhoneNumber varchar(35),
paymentDriverId varchar(22) binary, paymentDriverId varchar(22) binary,
paymentDriverLabel varchar(35), paymentDriverLabel varchar(35),
couponId varchar(22),
couponTitle varchar(35),
couponDiscount float,
taxes float, taxes float,
dateOfPurchase datetime dateOfPurchase datetime
)"); )");
@ -240,8 +237,6 @@ sub addAddressBook {
addressBookId varchar(22) binary not null primary key, addressBookId varchar(22) binary not null primary key,
sessionId varchar(22) binary, sessionId varchar(22) binary,
userId varchar(22) binary, userId varchar(22) binary,
lastPayId varchar(22) binary,
lastShipId varchar(22) binary,
index userId (sessionId), index userId (sessionId),
index sessionId (sessionId) index sessionId (sessionId)
)"); )");

View file

@ -261,7 +261,7 @@ sub view {
$book->submit(value=>$i18n->get("populate from address book")); $book->submit(value=>$i18n->get("populate from address book"));
# instanciate address # instanciate address
my $address = WebGUI::Shop::AddressBook->create($self->session)->getAddress($form->get("addressId")) if ($form->get("addressId")); my $address = WebGUI::Shop::AddressBook->newBySession($self->session)->getAddress($form->get("addressId")) if ($form->get("addressId"));
# build the form that the user needs to fill out with badge holder information # build the form that the user needs to fill out with badge holder information
my $info = WebGUI::HTMLForm->new($self->session, action=>$self->getUrl); my $info = WebGUI::HTMLForm->new($self->session, action=>$self->getUrl);

View file

@ -47,10 +47,14 @@ Does some bookkeeping to keep track of limited quantities of tickets that are av
sub addToCart { sub addToCart {
my ($self, $badgeInfo) = @_; my ($self, $badgeInfo) = @_;
$self->session->db->write("insert into EMSRegistrantTicket (badgeId, ticketAssetId) values (?,?)", my $db = $self->session->db;
[$badgeInfo->{badgeId},$self->getId]); my @params = ($badgeInfo->{badgeId},$self->getId);
# don't let them add a ticket they already have
unless ($db->quickScalar("select count(*) from EMSRegistrantTicket where badgeId=? and ticketAssetId=?",\@params)) {
$db->write("insert into EMSRegistrantTicket (badgeId, ticketAssetId) values (?,?)", \@params);
$self->SUPER::addToCart($badgeInfo); $self->SUPER::addToCart($badgeInfo);
} }
}
#------------------------------------------------------------------- #-------------------------------------------------------------------

View file

@ -80,7 +80,7 @@ sub www_address {
my $session = shift; my $session = shift;
my $output = undef; my $output = undef;
my $method = "www_". ( $session->form->get("method") || "view"); my $method = "www_". ( $session->form->get("method") || "view");
my $cart = WebGUI::Shop::AddressBook->create($session); my $cart = WebGUI::Shop::AddressBook->newBySession($session);
if ($cart->can($method)) { if ($cart->can($method)) {
$output = $cart->$method(); $output = $cart->$method();
} }

View file

@ -32,6 +32,7 @@ These subroutines are available from this package:
readonly session => my %session; readonly session => my %session;
private properties => my %properties; private properties => my %properties;
private addressCache => my %addressCache;
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -53,37 +54,11 @@ sub addAddress {
return $addressObj; return $addressObj;
} }
#-------------------------------------------------------------------
=head2 convertToUser ( userId )
Converts a session based address book to be owned by a user. If the user already has an address book then the address book will be merged with this one.
=head3 userId
The userId to own this address book.
=cut
sub convertToUser {
my ($self, $userId) = @_;
$self->update({userId=>$userId});
my $other = $self->session->db->read("select addressBookId from addressBook where addressBookId<>? and userId=?", [$self->getId, $userId]);
while (my ($id) = $other->array) {
my $book = __PACKAGE__->new($self->session, $id);
foreach my $address (@{$book->getAddresses}) {
$address->update({addressBookId=>$self->getId});
}
$book->delete;
}
}
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 create ( session ) =head2 create ( session )
Constructor. Creates a new address book for this user if they don't have one. If the user is not logged in creates an address book attached to the session if there isn't one for the session. In any case returns a reference to the address book. Constructor. Creates a new address book for this user or session if no user is logged in.
=head3 session =head3 session
@ -96,36 +71,9 @@ sub create {
unless (defined $session && $session->isa("WebGUI::Session")) { unless (defined $session && $session->isa("WebGUI::Session")) {
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session."); WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session.");
} }
# check to see if we're dealing with a registered user or just a visitor my $id = $session->db->setRow("addressBook", "addressBookId", {addressBookId=>"new", userId=>$session->user->userId, sessionId=>$session->getId});
if ($session->user->userId ne "1") {
# check to see if this user or his session already has an address book
my @ids = $session->db->buildArray("select addressBookId from addressBook where userId=? or sessionId=?",[$session->user->userId, $session->getId]);
if (scalar(@ids) > 0) {
# how are we looking
my $book = $class->new($session, $ids[0]);
if ($book->get("userId") eq "" || scalar(@ids) > 1) {
# it's attached to the session or we have too many
$book->convertToUser($session->user->userId);
}
# it's ours
return $book;
}
else {
# nope create one for the user
my $id = $session->db->setRow("addressBook", "addressBookId", {addressBookId=>"new", userId=>$session->user->userId});
return $class->new($session, $id); return $class->new($session, $id);
} }
}
else {
# check to see if this session already has an address book
my $addressBookId = $session->db->quickScalar("select addressBookId from addressBook where sessionId=?",[$session->getId]);
if ($addressBookId eq "") {
# nope, create one for the session
$addressBookId = $session->db->setRow("addressBook", "addressBookId", {addressBookId=>"new", sessionId=>$session->getId});
}
return $class->new($session, $addressBookId);
}
}
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -187,19 +135,6 @@ sub get {
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 getId ()
Returns the unique id for this cart.
=cut
sub getId {
my ($self) = @_;
return $self->get("addressBookId");
}
#-------------------------------------------------------------------
=head2 getAddress ( id ) =head2 getAddress ( id )
Returns an address object. Returns an address object.
@ -212,7 +147,11 @@ An address object's unique id.
sub getAddress { sub getAddress {
my ($self, $addressId) = @_; my ($self, $addressId) = @_;
return WebGUI::Shop::Address->new($self, $addressId); my $id = ref $self;
unless (exists $addressCache{$id}{$addressId}) {
$addressCache{$id}{$addressId} = WebGUI::Shop::Address->new($self, $addressId);
}
return $addressCache{$id}{$addressId};
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -235,6 +174,19 @@ sub getAddresses {
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 getId ()
Returns the unique id for this cart.
=cut
sub getId {
my ($self) = @_;
return $self->get("addressBookId");
}
#-------------------------------------------------------------------
=head2 new ( session, addressBookId ) =head2 new ( session, addressBookId )
Constructor. Instanciates a cart based upon a addressBookId. Constructor. Instanciates a cart based upon a addressBookId.
@ -268,6 +220,56 @@ sub new {
return $self; return $self;
} }
#-------------------------------------------------------------------
=head2 newBySession ( session )
Constructor. Creates a new address book for this user if they don't have one. If the user is not logged in creates an address book attached to the session if there isn't one for the session. In any case returns a reference to the address book.
=head3 session
A reference to the current session.
=cut
sub newBySession {
my ($class, $session) = @_;
unless (defined $session && $session->isa("WebGUI::Session")) {
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session.");
}
my $userId = $session->user->userId;
# check to see if this user or his session already has an address book
my @ids = $session->db->buildArray("select addressBookId from addressBook where (userId<>'1' and userId=?) or sessionId=?",[$session->user->userId, $session->getId]);
if (scalar(@ids) > 0) {
my $book = $class->new($session, $ids[0]);
# convert it to a specific user if we can
if ($userId ne '1') {
$book->update({userId => $userId, sessionId => ''});
}
# merge others if needed
if (scalar(@ids) > 1) {
# it's attached to the session or we have too many so lets merge them
shift @ids;
foreach my $id (@ids) {
my $oldbook = $class->new($session, $id);
foreach my $address (@{$oldbook->getAddresses}) {
$address->update({addressBookId=>$book->getId});
}
$oldbook->delete;
}
}
return $book;
}
else {
# nope create one for the user
return $class->create($session);
}
}
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 update ( properties ) =head2 update ( properties )
@ -278,14 +280,6 @@ Sets properties in the addressBook
A hash reference that contains one of the following: A hash reference that contains one of the following:
=head4 lastShipId
The last addressId used for shipping.
=head4 lastPayId
The last addressId used for payment.
=head4 userId =head4 userId
Assign the user that owns this address book. Assign the user that owns this address book.
@ -299,7 +293,7 @@ Assign the session that owns this adress book. Will automatically be set to "" i
sub update { sub update {
my ($self, $newProperties) = @_; my ($self, $newProperties) = @_;
my $id = id $self; my $id = id $self;
foreach my $field (qw(lastPayId lastShipId userId sessionId)) { foreach my $field (qw(userId sessionId)) {
$properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field}; $properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field};
} }
##Having both a userId and sessionId will confuse create. ##Having both a userId and sessionId will confuse create.

View file

@ -10,7 +10,6 @@ use WebGUI::Form;
use WebGUI::International; use WebGUI::International;
use WebGUI::Shop::AddressBook; use WebGUI::Shop::AddressBook;
use WebGUI::Shop::CartItem; use WebGUI::Shop::CartItem;
# use WebGUI::Shop::Coupon;
use WebGUI::Shop::Ship; use WebGUI::Shop::Ship;
use WebGUI::Shop::Tax; use WebGUI::Shop::Tax;
@ -38,6 +37,7 @@ readonly session => my %session;
private properties => my %properties; private properties => my %properties;
private error => my %error; private error => my %error;
private itemCache => my %itemCache; private itemCache => my %itemCache;
private addressBookCache => my %addressBookCache;
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -186,7 +186,11 @@ Returns a reference to the address book for the user who's cart this is.
sub getAddressBook { sub getAddressBook {
my $self = shift; my $self = shift;
return WebGUI::Shop::AddressBook->create($self->session); my $id = ref $self;
unless (exists $addressBookCache{$id}) {
$addressBookCache{$id} = WebGUI::Shop::AddressBook->newBySession($self->session);
}
return $addressBookCache{$id};
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -439,10 +443,6 @@ Sets properties in the cart.
A hash reference that contains one of the following: A hash reference that contains one of the following:
=head4 couponId
The unique id for a coupon used in this cart.
=head4 shippingAddressId =head4 shippingAddressId
The unique id for a shipping address attached to this cart. The unique id for a shipping address attached to this cart.
@ -459,7 +459,7 @@ sub update {
WebGUI::Error::InvalidParam->throw(error=>"Need a properties hash ref."); WebGUI::Error::InvalidParam->throw(error=>"Need a properties hash ref.");
} }
my $id = id $self; my $id = id $self;
foreach my $field (qw(couponId shippingAddressId shipperId)) { foreach my $field (qw(shippingAddressId shipperId)) {
$properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field}; $properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field};
} }
$self->session->db->setRow("cart","cartId",$properties{$id}); $self->session->db->setRow("cart","cartId",$properties{$id});
@ -492,8 +492,9 @@ Remove an item from the cart and then display the cart again.
sub www_removeItem { sub www_removeItem {
my $self = shift; my $self = shift;
my $item = $self->getItem($self->session->form->get("itemId"))->remove; my $item = $self->getItem($self->session->form->get("itemId"));
delete $itemCache{ref $self}{$item->getId}; delete $itemCache{ref $self}{$item->getId};
$item->remove;
return $self->www_view; return $self->www_view;
} }
@ -544,7 +545,6 @@ sub www_update {
my $cartProperties; my $cartProperties;
$cartProperties->{ shipperId } = $form->process( 'shipperId' ) if $form->process( 'shipperId' ); $cartProperties->{ shipperId } = $form->process( 'shipperId' ) if $form->process( 'shipperId' );
$cartProperties->{ couponId } = $form->process( 'couponId' ) if $form->process( 'couponId' );
$self->update( $cartProperties ); $self->update( $cartProperties );
return $self->www_view; return $self->www_view;
@ -564,6 +564,8 @@ sub www_view {
my $url = $session->url; my $url = $session->url;
my $i18n = WebGUI::International->new($session, "Shop"); my $i18n = WebGUI::International->new($session, "Shop");
my @items = (); my @items = ();
# set up html header
$session->style->setRawHeadTags(q| $session->style->setRawHeadTags(q|
<script type="text/javascript"> <script type="text/javascript">
function setCallbackForAddressChooser (form, itemId) { function setCallbackForAddressChooser (form, itemId) {
@ -575,6 +577,8 @@ sub www_view {
} }
</script> </script>
|); |);
# generate template variables for the items in the cart
foreach my $item (@{$self->getItems}) { foreach my $item (@{$self->getItems}) {
my $sku = $item->getSku; my $sku = $item->getSku;
$sku->applyOptions($item->get("options")); $sku->applyOptions($item->get("options"));
@ -616,18 +620,22 @@ sub www_view {
extras=>q|onclick="setCallbackForAddressChooser(this.form);"|}), extras=>q|onclick="setCallbackForAddressChooser(this.form);"|}),
shipToButton => WebGUI::Form::submit($session, {value=>$i18n->get("ship to button"), shipToButton => WebGUI::Form::submit($session, {value=>$i18n->get("ship to button"),
extras=>q|onclick="setCallbackForAddressChooser(this.form);"|}), extras=>q|onclick="setCallbackForAddressChooser(this.form);"|}),
couponField => WebGUI::Form::text($session, {name=>"couponCode", value=>"", size=>20}),
subtotalPrice => $self->formatCurrency($self->calculateSubtotal()), subtotalPrice => $self->formatCurrency($self->calculateSubtotal()),
couponDiscount => $self->formatCurrency(0),
); );
# get the shipping address
my $address = eval { $self->getShippingAddress }; my $address = eval { $self->getShippingAddress };
if (WebGUI::Error->caught("WebGUI::Error::ObjectNotFound")) { if (WebGUI::Error->caught("WebGUI::Error::ObjectNotFound")) {
# choose another address cuz we've got a problem # choose another address cuz we've got a problem
$self->update({shippingAddressId=>""}); $self->update({shippingAddressId=>""});
} }
# if there is no shipping address we can't check out
if (WebGUI::Error->caught) { if (WebGUI::Error->caught) {
$var{shippingPrice} = $var{tax} = $self->formatCurrency(0); $var{shippingPrice} = $var{tax} = $self->formatCurrency(0);
} }
# if there is a shipping address calculate tax and shipping options
else { else {
$var{hasShippingAddress} = 1; $var{hasShippingAddress} = 1;
$var{shippingAddress} = $address->getHtmlFormatted; $var{shippingAddress} = $address->getHtmlFormatted;
@ -644,12 +652,10 @@ sub www_view {
$var{shippingPrice} = ($self->get("shipperId") ne "") ? $options->{$self->get("shipperId")}{price} : $options->{$defaultOption}{price}; $var{shippingPrice} = ($self->get("shipperId") ne "") ? $options->{$self->get("shipperId")}{price} : $options->{$defaultOption}{price};
$var{shippingPrice} = $self->formatCurrency($var{shippingPrice}); $var{shippingPrice} = $self->formatCurrency($var{shippingPrice});
} }
if ($self->get("couponId")) { $var{totalPrice} = $self->formatCurrency($var{subtotalPrice} + $var{shippingPrice} + $var{tax});
$var{couponDiscount} = $self->formatCurrency(0);
} # render the cart
$var{totalPrice} = $self->formatCurrency($var{subtotalPrice} + $var{couponDiscount} + $var{shippingPrice} + $var{tax});
my $template = WebGUI::Asset::Template->new($session, $session->setting->get("shopCartTemplateId")); my $template = WebGUI::Asset::Template->new($session, $session->setting->get("shopCartTemplateId"));
$template->prepare;
return $session->style->userStyle($template->process(\%var)); return $session->style->userStyle($template->process(\%var));
} }

View file

@ -159,7 +159,6 @@ Returns the WebGUI::Shop::Address object that is attached to this item for shipp
sub getShippingAddress { sub getShippingAddress {
my $self = shift; my $self = shift;
my $addressId = $self->get("shippingAddressId") || $self->cart->get("shippingAddressId"); my $addressId = $self->get("shippingAddressId") || $self->cart->get("shippingAddressId");
return $self->cart->getAddressBook->getAddress($addressId); return $self->cart->getAddressBook->getAddress($addressId);
} }

View file

@ -305,11 +305,11 @@ A hash reference that contains one of the following:
=head4 cart =head4 cart
A reference to a cart object. Will pull shipping method, shipping address, tax, items, total, and coupon from A reference to a cart object. Will pull shipping method, shipping address, tax, items, and total from
it. Alternatively you can set manually any of the following properties that are set by cart automatically: it. Alternatively you can set manually any of the following properties that are set by cart automatically:
amount shippingAddressId shippingAddressName shippingAddress1 shippingAddress2 shippingAddress3 shippingCity amount shippingAddressId shippingAddressName shippingAddress1 shippingAddress2 shippingAddress3 shippingCity
shippingState shippingCountry shippingCode shippingPhoneNumber shippingDriverId shippingDriverLabel shippingPrice shippingState shippingCountry shippingCode shippingPhoneNumber shippingDriverId shippingDriverLabel shippingPrice
couponId couponTitle couponDiscount taxes taxes
You can also use the addItem() method to manually add items to the transaction rather than passing a cart full of items. You can also use the addItem() method to manually add items to the transaction rather than passing a cart full of items.
@ -364,10 +364,7 @@ sub update {
$newProperties->{shippingDriverId} = $shipper->getId; $newProperties->{shippingDriverId} = $shipper->getId;
$newProperties->{shippingDriverLabel} = $shipper->get('label'); $newProperties->{shippingDriverLabel} = $shipper->get('label');
$newProperties->{shippingPrice} = $shipper->calculate($cart); $newProperties->{shippingPrice} = $shipper->calculate($cart);
$newProperties->{couponId} = $cart->get('couponId'); $newProperties->{amount} = $cart->calculateSubtotal + $newProperties->{shippingPrice} + $newProperties->{taxes};
$newProperties->{couponTitle} = '';
$newProperties->{couponDiscount} = '';
$newProperties->{amount} = $cart->calculateSubtotal + $newProperties->{couponDiscount} + $newProperties->{shippingPrice} + $newProperties->{taxes};
foreach my $item (@{$cart->getItems}) { foreach my $item (@{$cart->getItems}) {
$self->addItem({item=>$item}); $self->addItem({item=>$item});
} }
@ -395,7 +392,7 @@ sub update {
shippingCountry shippingCode shippingPhoneNumber shippingDriverId shippingDriverLabel shippingCountry shippingCode shippingPhoneNumber shippingDriverId shippingDriverLabel
shippingPrice paymentAddressId paymentAddressName shippingPrice paymentAddressId paymentAddressName
paymentAddress1 paymentAddress2 paymentAddress3 paymentCity paymentState paymentCountry paymentCode paymentAddress1 paymentAddress2 paymentAddress3 paymentCity paymentState paymentCountry paymentCode
paymentPhoneNumber paymentDriverId paymentDriverLabel couponId couponTitle couponDiscount taxes )); paymentPhoneNumber paymentDriverId paymentDriverLabel taxes ));
foreach my $field (@fields) { foreach my $field (@fields) {
$properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field}; $properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field};
} }

View file

@ -30,7 +30,7 @@ my $session = WebGUI::Test->session;
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Tests # Tests
plan tests => 70; # Increment this number for each test you create plan tests => 67; # Increment this number for each test you create
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# put your tests here # put your tests here
@ -62,9 +62,6 @@ my $transaction = WebGUI::Shop::Transaction->create($session,{
paymentPhoneNumber => '908765', paymentPhoneNumber => '908765',
paymentDriverId => 'xxx4', paymentDriverId => 'xxx4',
paymentDriverLabel => 'kkk', paymentDriverLabel => 'kkk',
couponId => 'xxx5',
couponTitle => 'title1',
couponDiscount => -5,
taxes => 7, taxes => 7,
}); });
@ -100,9 +97,6 @@ is($transaction->get("paymentCode"), '66666', "set and get payment code");
is($transaction->get("paymentPhoneNumber"), '908765', "set and get payment phone number"); is($transaction->get("paymentPhoneNumber"), '908765', "set and get payment phone number");
is($transaction->get("paymentDriverId"), 'xxx4', "set and get payment driver id"); is($transaction->get("paymentDriverId"), 'xxx4', "set and get payment driver id");
is($transaction->get("paymentDriverLabel"), 'kkk', "set and get payment driver label"); is($transaction->get("paymentDriverLabel"), 'kkk', "set and get payment driver label");
is($transaction->get("couponId"), 'xxx5', "set and get coupon id");
is($transaction->get("couponTitle"), 'title1', "set and get coupon title");
is($transaction->get("couponDiscount"), -5, "set and get coupon discount");
is($transaction->get("taxes"), 7, "set and get taxes"); is($transaction->get("taxes"), 7, "set and get taxes");