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),
paymentDriverId varchar(22) binary,
paymentDriverLabel varchar(35),
couponId varchar(22),
couponTitle varchar(35),
couponDiscount float,
taxes float,
dateOfPurchase datetime
)");
@ -240,8 +237,6 @@ sub addAddressBook {
addressBookId varchar(22) binary not null primary key,
sessionId varchar(22) binary,
userId varchar(22) binary,
lastPayId varchar(22) binary,
lastShipId varchar(22) binary,
index userId (sessionId),
index sessionId (sessionId)
)");

View file

@ -261,7 +261,7 @@ sub view {
$book->submit(value=>$i18n->get("populate from address book"));
# 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
my $info = WebGUI::HTMLForm->new($self->session, action=>$self->getUrl);

View file

@ -47,9 +47,13 @@ Does some bookkeeping to keep track of limited quantities of tickets that are av
sub addToCart {
my ($self, $badgeInfo) = @_;
$self->session->db->write("insert into EMSRegistrantTicket (badgeId, ticketAssetId) values (?,?)",
[$badgeInfo->{badgeId},$self->getId]);
$self->SUPER::addToCart($badgeInfo);
my $db = $self->session->db;
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);
}
}
#-------------------------------------------------------------------

View file

@ -80,7 +80,7 @@ sub www_address {
my $session = shift;
my $output = undef;
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)) {
$output = $cart->$method();
}

View file

@ -32,6 +32,7 @@ These subroutines are available from this package:
readonly session => my %session;
private properties => my %properties;
private addressCache => my %addressCache;
#-------------------------------------------------------------------
@ -53,37 +54,11 @@ sub addAddress {
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 )
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
@ -96,35 +71,8 @@ sub create {
unless (defined $session && $session->isa("WebGUI::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
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);
}
}
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);
}
my $id = $session->db->setRow("addressBook", "addressBookId", {addressBookId=>"new", userId=>$session->user->userId, sessionId=>$session->getId});
return $class->new($session, $id);
}
#-------------------------------------------------------------------
@ -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 )
Returns an address object.
@ -212,7 +147,11 @@ An address object's unique id.
sub getAddress {
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 )
Constructor. Instanciates a cart based upon a addressBookId.
@ -268,6 +220,56 @@ sub new {
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 )
@ -278,14 +280,6 @@ Sets properties in the addressBook
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
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 {
my ($self, $newProperties) = @_;
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};
}
##Having both a userId and sessionId will confuse create.

View file

@ -10,7 +10,6 @@ use WebGUI::Form;
use WebGUI::International;
use WebGUI::Shop::AddressBook;
use WebGUI::Shop::CartItem;
# use WebGUI::Shop::Coupon;
use WebGUI::Shop::Ship;
use WebGUI::Shop::Tax;
@ -38,6 +37,7 @@ readonly session => my %session;
private properties => my %properties;
private error => my %error;
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 {
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:
=head4 couponId
The unique id for a coupon used in this cart.
=head4 shippingAddressId
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.");
}
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};
}
$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 {
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};
$item->remove;
return $self->www_view;
}
@ -512,7 +513,7 @@ sub www_setShippingAddress {
$self->getItem($form->get("itemId"))->update({shippingAddressId=>$form->get('addressId')});
}
else {
$self->update({shippingAddressId=>$form->get('addressId')});
$self->update({shippingAddressId=>$form->get('addressId')});
}
return $self->www_view;
}
@ -544,7 +545,6 @@ sub www_update {
my $cartProperties;
$cartProperties->{ shipperId } = $form->process( 'shipperId' ) if $form->process( 'shipperId' );
$cartProperties->{ couponId } = $form->process( 'couponId' ) if $form->process( 'couponId' );
$self->update( $cartProperties );
return $self->www_view;
@ -564,6 +564,8 @@ sub www_view {
my $url = $session->url;
my $i18n = WebGUI::International->new($session, "Shop");
my @items = ();
# set up html header
$session->style->setRawHeadTags(q|
<script type="text/javascript">
function setCallbackForAddressChooser (form, itemId) {
@ -575,6 +577,8 @@ sub www_view {
}
</script>
|);
# generate template variables for the items in the cart
foreach my $item (@{$self->getItems}) {
my $sku = $item->getSku;
$sku->applyOptions($item->get("options"));
@ -591,7 +595,7 @@ sub www_view {
shipToButton => WebGUI::Form::submit($session, {value=>$i18n->get("ship to button"),
extras=>q|onclick="setCallbackForAddressChooser(this.form,'|.$item->getId.q|');"|}),
);
my $address = eval { $item->getShippingAddress };
my $address = eval {$item->getShippingAddress};
unless (WebGUI::Error->caught) {
$properties{shippingAddress} = $address->getHtmlFormatted;
}
@ -616,40 +620,42 @@ sub www_view {
extras=>q|onclick="setCallbackForAddressChooser(this.form);"|}),
shipToButton => WebGUI::Form::submit($session, {value=>$i18n->get("ship to button"),
extras=>q|onclick="setCallbackForAddressChooser(this.form);"|}),
couponField => WebGUI::Form::text($session, {name=>"couponCode", value=>"", size=>20}),
subtotalPrice => $self->formatCurrency($self->calculateSubtotal()),
couponDiscount => $self->formatCurrency(0),
);
my $address = eval { $self->getShippingAddress };
if (WebGUI::Error->caught("WebGUI::Error::ObjectNotFound")) {
# choose another address cuz we've got a problem
$self->update({shippingAddressId=>""});
# get the shipping address
my $address = eval { $self->getShippingAddress };
if (WebGUI::Error->caught("WebGUI::Error::ObjectNotFound")) {
# choose another address cuz we've got a problem
$self->update({shippingAddressId=>""});
}
# if there is no shipping address we can't check out
if (WebGUI::Error->caught) {
$var{shippingPrice} = $var{tax} = $self->formatCurrency(0);
}
# if there is a shipping address calculate tax and shipping options
else {
$var{hasShippingAddress} = 1;
$var{shippingAddress} = $address->getHtmlFormatted;
$var{tax} = $self->getTaxes;
my $ship = WebGUI::Shop::Ship->new($self->session);
my $options = $ship->getOptions($self);
my %formOptions = ();
my $defaultOption = "";
foreach my $option (keys %{$options}) {
$defaultOption = $option;
$formOptions{$option} = $options->{$option}{label}." (".$self->formatCurrency($options->{$option}{price}).")";
}
if (WebGUI::Error->caught) {
$var{shippingPrice} = $var{tax} = $self->formatCurrency(0);
}
else {
$var{hasShippingAddress} = 1;
$var{shippingAddress} = $address->getHtmlFormatted;
$var{tax} = $self->getTaxes;
my $ship = WebGUI::Shop::Ship->new($self->session);
my $options = $ship->getOptions($self);
my %formOptions = ();
my $defaultOption = "";
foreach my $option (keys %{$options}) {
$defaultOption = $option;
$formOptions{$option} = $options->{$option}{label}." (".$self->formatCurrency($options->{$option}{price}).")";
}
$var{shippingOptions} = WebGUI::Form::selectBox($session, {name=>"shipperId", options=>\%formOptions, defaultValue=>$defaultOption, value=>$self->get("shipperId")});
$var{shippingPrice} = ($self->get("shipperId") ne "") ? $options->{$self->get("shipperId")}{price} : $options->{$defaultOption}{price};
$var{shippingPrice} = $self->formatCurrency($var{shippingPrice});
}
if ($self->get("couponId")) {
$var{couponDiscount} = $self->formatCurrency(0);
}
$var{totalPrice} = $self->formatCurrency($var{subtotalPrice} + $var{couponDiscount} + $var{shippingPrice} + $var{tax});
$var{shippingOptions} = WebGUI::Form::selectBox($session, {name=>"shipperId", options=>\%formOptions, defaultValue=>$defaultOption, value=>$self->get("shipperId")});
$var{shippingPrice} = ($self->get("shipperId") ne "") ? $options->{$self->get("shipperId")}{price} : $options->{$defaultOption}{price};
$var{shippingPrice} = $self->formatCurrency($var{shippingPrice});
}
$var{totalPrice} = $self->formatCurrency($var{subtotalPrice} + $var{shippingPrice} + $var{tax});
# render the cart
my $template = WebGUI::Asset::Template->new($session, $session->setting->get("shopCartTemplateId"));
$template->prepare;
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 {
my $self = shift;
my $addressId = $self->get("shippingAddressId") || $self->cart->get("shippingAddressId");
return $self->cart->getAddressBook->getAddress($addressId);
}

View file

@ -305,11 +305,11 @@ A hash reference that contains one of the following:
=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:
amount shippingAddressId shippingAddressName shippingAddress1 shippingAddress2 shippingAddress3 shippingCity
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.
@ -364,10 +364,7 @@ sub update {
$newProperties->{shippingDriverId} = $shipper->getId;
$newProperties->{shippingDriverLabel} = $shipper->get('label');
$newProperties->{shippingPrice} = $shipper->calculate($cart);
$newProperties->{couponId} = $cart->get('couponId');
$newProperties->{couponTitle} = '';
$newProperties->{couponDiscount} = '';
$newProperties->{amount} = $cart->calculateSubtotal + $newProperties->{couponDiscount} + $newProperties->{shippingPrice} + $newProperties->{taxes};
$newProperties->{amount} = $cart->calculateSubtotal + $newProperties->{shippingPrice} + $newProperties->{taxes};
foreach my $item (@{$cart->getItems}) {
$self->addItem({item=>$item});
}
@ -395,7 +392,7 @@ sub update {
shippingCountry shippingCode shippingPhoneNumber shippingDriverId shippingDriverLabel
shippingPrice paymentAddressId paymentAddressName
paymentAddress1 paymentAddress2 paymentAddress3 paymentCity paymentState paymentCountry paymentCode
paymentPhoneNumber paymentDriverId paymentDriverLabel couponId couponTitle couponDiscount taxes ));
paymentPhoneNumber paymentDriverId paymentDriverLabel taxes ));
foreach my $field (@fields) {
$properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field};
}

View file

@ -30,7 +30,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# 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
@ -62,9 +62,6 @@ my $transaction = WebGUI::Shop::Transaction->create($session,{
paymentPhoneNumber => '908765',
paymentDriverId => 'xxx4',
paymentDriverLabel => 'kkk',
couponId => 'xxx5',
couponTitle => 'title1',
couponDiscount => -5,
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("paymentDriverId"), 'xxx4', "set and get payment driver id");
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");