Remove an extra my from Shop::Cart.

Add the getTaxRates method to Tax, with tests.
Add some calculate code to Tax, with tests, which don't pass yet.
This commit is contained in:
Colin Kuskie 2008-03-05 00:22:40 +00:00
parent 52d2c63271
commit 2bc6cd49a7
4 changed files with 138 additions and 3 deletions

View file

@ -72,7 +72,7 @@ sub create {
}
my $cartId = $session->db->quickScalar("select cartId from cart where sessionId=?",[$session->getId]);
return $class->new($session, $cartId) if (defined $cartId);
my $cartId = $session->id->generate;
$cartId = $session->id->generate;
$session->db->write('insert into cart (cartId, sessionId) values (?,?)', [$cartId, $session->getId]);
return $class->new($session, $cartId);
}

View file

@ -6,6 +6,11 @@ use Class::InsideOut qw{ :std };
use WebGUI::Text;
use WebGUI::Storage;
use WebGUI::Exception::Shop;
use WebGUI::Shop::Cart;
use WebGUI::Shop::CartItem;
use WebGUI::Shop::AddressBook;
use WebGUI::Shop::Address;
use List::Util qw{sum};
=head1 NAME
@ -89,7 +94,23 @@ sub calculate {
my $cart = shift;
WebGUI::Error::InvalidParam->throw(error => 'Must pass in a WebGUI::Shop::Cart object')
unless ref($cart) eq 'WebGUI::Shop::Cart';
return;
my $book = WebGUI::Shop::AddressBook->create($self->session);
my $address = WebGUI::Shop::Address->new($book, $cart->get('shippingAddressId'));
my $tax = 0;
foreach my $item (@{ $cart->getItems }) {
my $sku = $item->getSku;
my $unitPrice = $sku->getPrice;
my $quantity = $item->get('quantity');
my $taxables = $self->getTaxRates($address);
use Data::Dumper;
warn Dumper $taxables;
my $itemTax = sum(@{$taxables}) / 100; ##Form a percentage
warn "unitPrice: $unitPrice\n";
warn "quantity : $quantity\n";
warn "itemTax : $itemTax\n";
$tax += $unitPrice * $quantity * $itemTax;
}
return $tax;
}
#-------------------------------------------------------------------
@ -160,6 +181,30 @@ sub getItems {
#-------------------------------------------------------------------
=head2 getTaxRates ( $address )
Given a WebGUI::Shop::Address object, return all rates associated with the address as an arrayRef.
=cut
sub getTaxRates {
my $self = shift;
my $address = shift;
WebGUI::Error::InvalidObject->throw(error => 'Need an address.', expected=>'WebGUI::Shop::Address', got=>(ref $address))
unless ref($address) eq 'WebGUI::Shop::Address';
my $result = $self->session->db->buildArrayRef(
q{
select taxRate from tax where
(field='state' and value=?)
OR (field='country' and value=?)
OR (field='code' and value=?)
},
[$address->get('state'), $address->get('country'), $address->get('code')]);
return $result;
}
#-------------------------------------------------------------------
=head2 importTaxData ( $filePath )
Import tax information from the specified file in CSV format. The