Added the methods for the pluggable tax system that add templ vars to the cart and cart items.

This commit is contained in:
Martin Kamerbeek 2009-05-08 13:34:42 +00:00
parent 9d90b92629
commit 7fb87de71d
5 changed files with 154 additions and 14 deletions

View file

@ -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.

View file

@ -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 = (

View file

@ -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.

View file

@ -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.

View file

@ -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' );
}
#----------------------------------------------------------------------------