From 7fb87de71d481298cf5116b031b9b4d4e455c71e Mon Sep 17 00:00:00 2001 From: Martin Kamerbeek Date: Fri, 8 May 2009 13:34:42 +0000 Subject: [PATCH] Added the methods for the pluggable tax system that add templ vars to the cart and cart items. --- docs/changelog/7.x.x.txt | 3 ++ lib/WebGUI/Shop/Cart.pm | 4 ++ lib/WebGUI/Shop/TaxDriver.pm | 63 ++++++++++++++++++++++++++--- lib/WebGUI/Shop/TaxDriver/EU.pm | 27 +++++++++++++ t/Shop/TaxDriver/EU.t | 71 +++++++++++++++++++++++++++++---- 5 files changed, 154 insertions(+), 14 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 6ae6a0863..7aeb893bf 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -11,6 +11,9 @@ - fixed: i18n typo in Survey exit URL hover help. - fixed: Survey jump target and jump expression precedence order across Section, Question, Answer. - rfre #9998: Mark inbox messages read/unread + - The transaction items now store the tax that is applied to them, as well as + tax plugin specific data that needs to be stored together with + transactions. (Martin Kamerbeek / Oqapi ) 7.7.5 - Adding StoryManager. diff --git a/lib/WebGUI/Shop/Cart.pm b/lib/WebGUI/Shop/Cart.pm index f776114bf..1187ed80a 100644 --- a/lib/WebGUI/Shop/Cart.pm +++ b/lib/WebGUI/Shop/Cart.pm @@ -735,6 +735,7 @@ sub www_view { my $url = $session->url; my $i18n = WebGUI::International->new($session, "Shop"); my @items = (); + my $taxDriver = WebGUI::Shop::Tax->getDriver( $session ); if($url->forceSecureConnection()){ return "redirect"; @@ -785,6 +786,9 @@ sub www_view { unless (WebGUI::Error->caught) { $properties{shippingAddress} = $address->getHtmlFormatted; } + + $taxDriver->appendCartItemVars( \%properties, $item ); + push(@items, \%properties); } my %var = ( diff --git a/lib/WebGUI/Shop/TaxDriver.pm b/lib/WebGUI/Shop/TaxDriver.pm index 24893dd70..7dbdd80a0 100644 --- a/lib/WebGUI/Shop/TaxDriver.pm +++ b/lib/WebGUI/Shop/TaxDriver.pm @@ -63,24 +63,75 @@ readonly session => my %session; readonly messages => my %messages; private options => my %options; -#----------------------------------------------------------- -=head2 appendTaxDetailVars ($var) +=head2 appendCartItemVars ( var, cartItem ) -=head3 $var +Adds tax driver specific template variables for the given cart item to the supplied hashref. + +=head3 var + +The template variable hash ref to add the tax vars to. + +=head3 cartItem + +The instanstance of WebGUI::Shop::CartItem to add the vars for. =cut -sub appendTaxDetailVars { +sub appendCartItemVars { my $self = shift; my $var = shift; + my $item = shift; - return $var; + WebGUI::Error::InvalidParam->throw( 'Must supply a hash ref' ) + unless $var && ref $var eq 'HASH'; + WebGUI::Error::InvalidObject->throw( expected => 'WebGUI::Shop::CartItem', got => ref $item, error => 'Must pass a cart item' ) + unless $item && $item->isa( 'WebGUI::Shop::CartItem' ); + + my $sku = $item->getSku; + my $address = eval { $item->getShippingAddress }; + my $taxRate = $self->getTaxRate( $sku, $address ); + + my $quantity = $item->get( 'quantity' ); + my $price = $sku->getPrice; + my $tax = $price * $taxRate / 100; + + $var->{ taxRate } = $taxRate; + $var->{ taxAmount } = $item->cart->formatCurrency( $tax ); + $var->{ pricePlusTax } = $item->cart->formatCurrency( $price + $tax ); + $var->{ extendedPricePlusTax } = $item->cart->formatCurrency( $quantity * ( $price + $tax ) ); } #----------------------------------------------------------- -=head2 canManage +=head2 appendCartVars ( var, cart ) + +Extend this method to add tax driver specific template variables to those supplied to the cart template. + +=head3 var + +The hash ref to add the template variables to. + +=head3 cart + +The instance of WebGUI::Shop::Cart to add the template variables for. + +=cut + +sub appendCartVars { + my $self = shift; + my $var = shift; + my $cart = shift; + + WebGUI::Error::InvalidParam->throw( 'Must supply a hash ref' ) + unless $var && ref $var eq 'HASH'; + WebGUI::Error::InvalidObject->throw( expected => 'WebGUI::Shop::Cart', got => ref $cart, error => 'Must pass a cart' ) + unless $cart && $cart->isa( 'WebGUI::Shop::Cart' ); +} + +#----------------------------------------------------------- + +=head2 canManage ( ) Returns true if the current user can manage taxes. diff --git a/lib/WebGUI/Shop/TaxDriver/EU.pm b/lib/WebGUI/Shop/TaxDriver/EU.pm index 8b415dd8b..e75429eaa 100644 --- a/lib/WebGUI/Shop/TaxDriver/EU.pm +++ b/lib/WebGUI/Shop/TaxDriver/EU.pm @@ -178,6 +178,33 @@ sub addVATNumber { #------------------------------------------------------------------- +=head2 appendCartItemVars ( var, cartItem ) + +See WebGUI::Shop::TaxDriver->appendCartItemVars. + +Additionally adds VAT number to var. + +=cut + +sub appendCartItemVars { + my $self = shift; + my $var = shift; + my $item = shift; + + $self->SUPER::appendCartItemVars( $var, $item ); + + my $address = eval { $item->getShippingAddress }; + unless ( WebGUI::Error->caught ) { + my $countryCode = $self->getCountryCode( $address->get( 'country' ) ); + if ( $countryCode && $self->hasVATNumber( $countryCode ) ) { + $var->{ VATNumber } = $self->getVATNumbers( $countryCode )->[0]->{ vatNumber }; + } + } + +} + +#------------------------------------------------------------------- + =head2 className Returns the name of this class. diff --git a/t/Shop/TaxDriver/EU.t b/t/Shop/TaxDriver/EU.t index 936842e14..422f6bcf8 100644 --- a/t/Shop/TaxDriver/EU.t +++ b/t/Shop/TaxDriver/EU.t @@ -32,13 +32,13 @@ use WebGUI::Shop::AddressBook; my $session = WebGUI::Test->session; my $taxUser = WebGUI::User->new( $session, 'new' ); -$taxUser->username( 'MrEvasion' ); +$taxUser->username( 'Tex Evasion' ); #---------------------------------------------------------------------------- # Tests -my $tests = 48; +my $tests = 55; plan tests => 1 + $tests; #---------------------------------------------------------------------------- @@ -277,9 +277,6 @@ SKIP: { isa_ok($e, 'WebGUI::Error::InvalidParam', 'getTaxRate: error handling for not sending a sku'); is($e->error, 'Must pass in a WebGUI::Asset::Sku object', 'getTaxRate: error handling for not sending a sku'); - # Build a cart, add some Donation SKUs to it. Set one to be taxable. - my $cart = WebGUI::Shop::Cart->newBySession( $session ); - my $sku = WebGUI::Asset->getRoot($session)->addChild( { className => 'WebGUI::Asset::Sku::Donation', title => 'Taxable donation', @@ -312,6 +309,67 @@ SKIP: { ); is( $taxer->getTaxRate( $sku, $nlAddress ), 100, 'getTaxRate: shipping addresses in country of merchant w/ VAT number pay tax' ); + ####################################################################### + # + # appendCartItemVars + # + ####################################################################### + + eval { $taxer->appendCartItemVars }; + my $e = Exception::Class->caught(); + isa_ok( $e, 'WebGUI::Error::InvalidParam', 'appendCartItemVars requires a hash ref.' ); + is( $e, 'Must supply a hash ref', 'appendCartItemVars returns correct message for missing hash ref' ); + + eval { $taxer->appendCartItemVars( {}, 'NotAUserObject' ) }; + $e = Exception::Class->caught(); + isa_ok( $e, 'WebGUI::Error::InvalidObject', 'appendCartItemVars: Second argument must be a cart item object' ); + cmp_deeply( $e, methods( + error => 'Must pass a cart item', + expected => 'WebGUI::Shop::CartItem', + got => '', + ), 'appendCartItemVars returns correct error for missing CartItem' ); + + + my $cart = WebGUI::Shop::Cart->newBySession( $session ); + + my $item = $cart->addItem( $sku ); + $item->setQuantity( 2 ); + $item->update( { shippingAddressId => $nlAddress->getId } ); + + my $cartItemVars = { must => 'be kept' }; + $taxer->appendCartItemVars( $cartItemVars, $item ); + cmp_deeply( $cartItemVars, { + pricePlusTax => '200.00', + extendedPricePlusTax => '400.00', + taxRate => '100', + taxAmount => '100.00', + VATNumber => $testVAT_NL, + must => 'be kept', + }, 'appendCartItemVars returns correct data for address in shopy country.' ); + + $item->update( { shippingAddressId => $beAddress->getId } ); + $cartItemVars = { must => 'be kept' }; + $taxer->appendCartItemVars( $cartItemVars, $item ); + cmp_deeply( $cartItemVars, { + pricePlusTax => '100.00', + extendedPricePlusTax => '200.00', + taxRate => '0', + taxAmount => '0.00', + VATNumber => $testVAT_BE, + must => 'be kept', + }, 'appendCartItemVars returns correct data for address in otrher country in EU.' ); + + $item->update( { shippingAddressId => $usAddress->getId } ); + $cartItemVars = { must => 'be kept' }; + $taxer->appendCartItemVars( $cartItemVars, $item ); + cmp_deeply( $cartItemVars, { + pricePlusTax => '100.00', + extendedPricePlusTax => '200.00', + taxRate => '0', + taxAmount => '0.00', + must => 'be kept', + }, 'appendCartItemVars returns correct data for address outside EU.' ); + ####################################################################### # # getTransactionTaxData @@ -372,9 +430,6 @@ SKIP: { id => $id100, }, ], 'deleteGroup deletes correctly' ); - - - } #----------------------------------------------------------------------------