Update the POD for Cart.pm. Return value for addItem is not items in cart, it's the

created item.
Finish up the Tax calculate method, with tests.
This commit is contained in:
Colin Kuskie 2008-03-05 03:37:53 +00:00
parent 2bc6cd49a7
commit 8c862439f9
3 changed files with 62 additions and 13 deletions

View file

@ -39,7 +39,7 @@ private error => my %error;
=head2 addItem ( sku )
Adds an item to the cart. Returns the number of items now in the cart.
Adds an item to the cart. Returns a reference to the newly added item.
=head3 sku

View file

@ -85,7 +85,10 @@ sub add {
=head2 calculate ( $cart )
Calculate the tax for the contents of the cart.
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.
=cut
@ -95,19 +98,31 @@ 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);
my $address = WebGUI::Shop::Address->new($book, $cart->get('shippingAddressId'));
my $address = $book->getAddress($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";
##Check for an item specific shipping address
my $itemAddress;
if (defined $item->get('shippingAddressId')) {
$itemAddress = $book->getAddress($item->get('shippingAddressId'));
}
else {
$itemAddress = $address;
}
my $taxables = $self->getTaxRates($itemAddress);
##Check for a SKU specific tax override rate
my $skuTaxRate = $sku->getTaxRate();
my $itemTax;
if (defined $skuTaxRate) {
$itemTax = $skuTaxRate;
}
else {
$itemTax = sum(@{$taxables});
}
$itemTax /= 100;
$tax += $unitPrice * $quantity * $itemTax;
}
return $tax;

View file

@ -19,6 +19,7 @@ use lib "$FindBin::Bin/../lib";
use Test::More;
use Test::Deep;
use Exception::Class;
use Data::Dumper;
use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI::Session;
@ -33,7 +34,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 68;
my $tests = 73;
plan tests => 1 + $tests;
#----------------------------------------------------------------------------
@ -228,7 +229,6 @@ my @header = WebGUI::Text::splitCSV($fileLines[0]);
my @expectedHeader = qw/field value taxRate/;
cmp_deeply(\@header, \@expectedHeader, 'exportTaxData: header line is correct');
my @row1 = WebGUI::Text::splitCSV($fileLines[1]);
use Data::Dumper;
my $wiData = $taxer->getItems->hashRef;
##Need to ignore the taxId from the database
cmp_bag([ @{ $wiData }{ @expectedHeader } ], \@row1, 'exportTaxData: first line of data is correct');
@ -437,7 +437,7 @@ $cart->update({ shippingAddressId => $taxingAddress->getId});
##Set up the tax information
$taxer->importTaxData(
WebGUI::Test->getTestCollateralPath('taxTables/goodTaxTable.csv')
WebGUI::Test->getTestCollateralPath('taxTables/largeTaxTable.csv')
),
my $taxableDonation = WebGUI::Asset->getRoot($session)->addChild({
@ -455,7 +455,41 @@ foreach my $item (@{ $cart->getItems }) {
my $tax = $taxer->calculate($cart);
is($tax, 5.5, 'calculate: simple tax calculation on 1 item in the cart');
$cart->update({ shippingAddressId => $taxFreeAddress->getId});
is($taxer->calculate($cart), 0, 'calculate: simple tax calculation on 1 item in the cart, tax free location');
foreach my $item (@{ $cart->getItems }) {
$item->setQuantity(2);
}
$cart->update({ shippingAddressId => $taxingAddress->getId});
is($taxer->calculate($cart), 11, 'calculate: simple tax calculation on 1 item in the cart, qty 2');
my $taxFreeDonation = WebGUI::Asset->getRoot($session)->addChild({
className => 'WebGUI::Asset::Sku::Donation',
title => 'Tax Free Donation',
defaultPrice => 100.00,
overrideTaxRate => 1,
taxRateOverride => 0,
});
$cart->addItem($taxFreeDonation);
foreach my $item (@{ $cart->getItems }) {
$item->setQuantity(1);
}
is($taxer->calculate($cart), 5.5, 'calculate: simple tax calculation on 2 items in the cart, 1 without taxes');
my $remoteItem = $cart->addItem($taxableDonation);
$remoteItem->update({shippingAddressId => $taxFreeAddress->getId});
foreach my $item (@{ $cart->getItems }) {
$item->setQuantity(1);
}
is($taxer->calculate($cart), 5.5, 'calculate: simple tax calculation on 2 items in the cart, 1 without taxes, 1 shipped to a location with no taxes');
$taxableDonation->purge;
$taxFreeDonation->purge;
$cart->delete;
$book->delete;
}