From 8c862439f9c5d42d801f1adc95664772e25a1587 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Wed, 5 Mar 2008 03:37:53 +0000 Subject: [PATCH] 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. --- lib/WebGUI/Shop/Cart.pm | 2 +- lib/WebGUI/Shop/Tax.pm | 33 ++++++++++++++++++++++++--------- t/Shop/Tax.t | 40 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 62 insertions(+), 13 deletions(-) diff --git a/lib/WebGUI/Shop/Cart.pm b/lib/WebGUI/Shop/Cart.pm index 18923c4b0..d887de0f5 100644 --- a/lib/WebGUI/Shop/Cart.pm +++ b/lib/WebGUI/Shop/Cart.pm @@ -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 diff --git a/lib/WebGUI/Shop/Tax.pm b/lib/WebGUI/Shop/Tax.pm index 9917b97c9..54974cdae 100644 --- a/lib/WebGUI/Shop/Tax.pm +++ b/lib/WebGUI/Shop/Tax.pm @@ -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; diff --git a/t/Shop/Tax.t b/t/Shop/Tax.t index 4cd28b5b9..dda4a6c27 100644 --- a/t/Shop/Tax.t +++ b/t/Shop/Tax.t @@ -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; }