Conversion of FlatRate driver to Moose.

This commit is contained in:
Colin Kuskie 2010-10-13 10:58:46 -07:00
parent ad13a9134e
commit 4b252a2d67
3 changed files with 47 additions and 147 deletions

View file

@ -1,9 +1,36 @@
package WebGUI::Shop::ShipDriver::FlatRate;
use strict;
use base qw/WebGUI::Shop::ShipDriver/;
use Moose;
use WebGUI::Definition::Shop;
extends qw/WebGUI::Shop::ShipDriver/;
use WebGUI::Exception;
use Tie::IxHash;
define pluginName => ['Flat Rate','ShipDriver_FlatRate'];
property flatFee => (
fieldType => 'float',
label => ['flatFee', 'ShipDriver_FlatRate'],
hoverHelp => ['flatFee help', 'ShipDriver_FlatRate'],
default => 0,
);
property percentageOfPrice => (
fieldType => 'float',
label => ['percentageOfPrice', 'ShipDriver_FlatRate'],
hoverHelp => ['percentageOfPrice help', 'ShipDriver_FlatRate'],
default => 0,
);
property pricePerWeight => (
fieldType => 'float',
label => ['percentageOfWeight', 'ShipDriver_FlatRate'],
hoverHelp => ['percentageOfWeight help', 'ShipDriver_FlatRate'],
default => 0,
);
property pricePerItem => (
fieldType => 'float',
label => ['pricePerItem', 'ShipDriver_FlatRate'],
hoverHelp => ['pricePerItem help', 'ShipDriver_FlatRate'],
default => 0,
);
=head1 NAME
@ -52,9 +79,9 @@ sub calculate {
my $sku = $item->getSku;
if ($sku->isShippingRequired) {
my $quantity = $item->get('quantity');
$cost += ($quantity * $sku->getPrice * $self->get("percentageOfPrice") / 100) # cost by price
+ ($quantity * $sku->getWeight * $self->get("pricePerWeight") / 100) # cost by weight
+ ($quantity * $self->get("pricePerItem")); # cost by item
$cost += ($quantity * $sku->getPrice * $self->percentageOfPrice / 100) # cost by price
+ ($quantity * $sku->getWeight * $self->pricePerWeight / 100) # cost by weight
+ ($quantity * $self->pricePerItem); # cost by item
$anyShippable = 1;
##Account for items which must be shipped separately, and with those that can be shipped
##together.
@ -70,61 +97,9 @@ sub calculate {
}
}
if ($anyShippable) {
$cost += $self->get('flatFee') * ($separatelyShipped + $looseBundle);
$cost += $self->flatFee * ($separatelyShipped + $looseBundle);
}
return $cost;
}
#-------------------------------------------------------------------
=head2 definition ( $session )
This subroutine returns an arrayref of hashrefs, used to validate data put into
the object by the user, and to automatically generate the edit form to show
the user.
=cut
sub definition {
my $class = shift;
my $session = shift;
WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable})
unless ref $session eq 'WebGUI::Session';
my $definition = shift || [];
my $i18n = WebGUI::International->new($session, 'ShipDriver_FlatRate');
tie my %fields, 'Tie::IxHash';
%fields = (
flatFee => {
fieldType => 'float',
label => $i18n->get('flatFee'),
hoverHelp => $i18n->get('flatFee help'),
defaultValue => 0,
},
percentageOfPrice => {
fieldType => 'float',
label => $i18n->get('percentageOfPrice'),
hoverHelp => $i18n->get('percentageOfPrice help'),
defaultValue => 0,
},
pricePerWeight => {
fieldType => 'float',
label => $i18n->get('percentageOfWeight'),
hoverHelp => $i18n->get('percentageOfWeight help'),
defaultValue => 0,
},
pricePerItem => {
fieldType => 'float',
label => $i18n->get('pricePerItem'),
hoverHelp => $i18n->get('pricePerItem help'),
defaultValue => 0,
},
);
my %properties = (
name => 'Flat Rate',
properties => \%fields,
);
push @{ $definition }, \%properties;
return $class->SUPER::definition($session, $definition);
}
1;

View file

@ -48,6 +48,11 @@ our $I18N = {
lastUpdated => 1203569582,
},
'Flat Rate' => {
message => q|Flat Rate|,
lastUpdated => 1203569582,
},
};
1;

View file

@ -29,94 +29,13 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 19;
plan tests => 1 + $tests;
plan tests => 17;
#----------------------------------------------------------------------------
# put your tests here
use_ok('WebGUI::Shop::ShipDriver::FlatRate');
#######################################################################
#
# definition
#
#######################################################################
my $definition;
my $e; ##Exception variable, used throughout the file
eval { $definition = WebGUI::Shop::ShipDriver::FlatRate->definition(); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'definition takes an exception to not giving it a session variable');
cmp_deeply(
$e,
methods(
error => 'Must provide a session variable',
),
'definition: requires a session variable',
);
$definition = WebGUI::Shop::ShipDriver::FlatRate->definition($session);
cmp_deeply(
$definition,
[ {
name => 'Flat Rate',
properties => {
flatFee => {
fieldType => 'float',
label => ignore(),
hoverHelp => ignore(),
defaultValue => 0,
},
percentageOfPrice => {
fieldType => 'float',
label => ignore(),
hoverHelp => ignore(),
defaultValue => 0,
},
pricePerWeight => {
fieldType => 'float',
label => ignore(),
hoverHelp => ignore(),
defaultValue => 0,
},
pricePerItem => {
fieldType => 'float',
label => ignore(),
hoverHelp => ignore(),
defaultValue => 0,
},
}
},
{
name => 'Shipper Driver',
properties => {
label => {
fieldType => 'text',
label => ignore(),
hoverHelp => ignore(),
defaultValue => undef,
},
enabled => {
fieldType => 'yesNo',
label => ignore(),
hoverHelp => ignore(),
defaultValue => 1,
},
groupToUse => {
fieldType => 'group',
label => ignore(),
hoverHelp => ignore(),
defaultValue => 7,
},
}
} ],
'Definition returns an array of hashrefs',
);
#######################################################################
#
# create
@ -132,7 +51,9 @@ my $options = {
pricePerItem => 0.1,
};
my $driver2 = WebGUI::Shop::ShipDriver::FlatRate->create($session, $options);
my $driver2 = WebGUI::Shop::ShipDriver::FlatRate->new($session, $options);
$driver2->write;
WebGUI::Test->addToCleanup($driver2);
isa_ok($driver2, 'WebGUI::Shop::ShipDriver::FlatRate');
@ -183,10 +104,6 @@ cmp_deeply(
name => undef,
type => 'submit',
},
{
name => 'driverId',
type => 'hidden',
},
{
name => 'shop',
type => 'hidden',
@ -199,6 +116,10 @@ cmp_deeply(
name => 'do',
type => 'hidden',
},
{
name => 'driverId',
type => 'hidden',
},
{
name => 'label',
type => 'text',
@ -306,7 +227,7 @@ $options = {
pricePerItem => 10,
};
my $driver = WebGUI::Shop::ShipDriver::FlatRate->create($session, $options);
my $driver = WebGUI::Shop::ShipDriver::FlatRate->new($session, $options);
WebGUI::Test->addToCleanup($driver);
my $cart = WebGUI::Shop::Cart->newBySession($session);
@ -372,7 +293,6 @@ my $boughtCar = $car->addToCart($car->getCollateral('variantsJSON', 'variantId',
my $firstKey = $key->addToCart($key->getCollateral('variantsJSON', 'variantId', $metalKey));
is($driver->calculate($cart), 2, 'shipsSeparately: returns two, one for ships separately, one for ships bundled');
diag $boughtCar->getSku->getMaxAllowedInCart;
$boughtCar->adjustQuantity();
is($driver->calculate($cart), 2, '... returns two, one for ships separately, one for ships bundled, even for two items');