Fix a bug in the json method for getting tax data.
Tests for the json method. A LOT of California tax data for testing.
This commit is contained in:
parent
a838b340c0
commit
94b03aa3f5
3 changed files with 1865 additions and 35 deletions
|
|
@ -6,6 +6,7 @@ use Class::InsideOut qw{ :std };
|
||||||
use WebGUI::Text;
|
use WebGUI::Text;
|
||||||
use WebGUI::Storage;
|
use WebGUI::Storage;
|
||||||
use WebGUI::Exception::Shop;
|
use WebGUI::Exception::Shop;
|
||||||
|
use WebGUI::Shop::Admin;
|
||||||
use WebGUI::Shop::Cart;
|
use WebGUI::Shop::Cart;
|
||||||
use WebGUI::Shop::CartItem;
|
use WebGUI::Shop::CartItem;
|
||||||
use List::Util qw{sum};
|
use List::Util qw{sum};
|
||||||
|
|
@ -139,25 +140,6 @@ sub calculate {
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
=head2 canEdit ( [ $user ] )
|
|
||||||
|
|
||||||
Determine whether or not a user can perform commerce functions
|
|
||||||
|
|
||||||
=head3 $user
|
|
||||||
|
|
||||||
An optional WebGUI::User object to check for permission to do commerce functions. If
|
|
||||||
this is not used, it uses the current session user object.
|
|
||||||
|
|
||||||
=cut
|
|
||||||
|
|
||||||
sub canEdit {
|
|
||||||
my $self = shift;
|
|
||||||
my $user = shift || $self->session->user;
|
|
||||||
return $user->isInGroup( $self->session->get('groupIdAdminCommerce'));
|
|
||||||
}
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
|
||||||
|
|
||||||
=head2 delete ( [$params] )
|
=head2 delete ( [$params] )
|
||||||
|
|
||||||
Deletes data from the tax table by taxId.
|
Deletes data from the tax table by taxId.
|
||||||
|
|
@ -357,6 +339,56 @@ Accessor for the session object. Returns the session object.
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
|
=head2 www_getTaxesAsJson ( )
|
||||||
|
|
||||||
|
Servers side pagination for tax data that is sent as JSON back to the browser to be
|
||||||
|
displayed in a YUI DataTable.
|
||||||
|
|
||||||
|
=cut
|
||||||
|
|
||||||
|
sub www_getTaxesAsJson {
|
||||||
|
my ($self) = @_;
|
||||||
|
my $session = $self->session;
|
||||||
|
my $admin = WebGUI::Shop::Admin->new($session);
|
||||||
|
return $session->privilege->insufficient
|
||||||
|
unless $admin->canManage;
|
||||||
|
my ($db, $form) = $session->quick(qw(db form));
|
||||||
|
my $startIndex = $form->get('startIndex') || 0;
|
||||||
|
my $numberOfResults = $form->get('results') || 25;
|
||||||
|
my @placeholders = ();
|
||||||
|
my $sql = 'select SQL_CALC_FOUND_ROWS * from tax';
|
||||||
|
my $keywords = $form->get("keywords");
|
||||||
|
if ($keywords ne "") {
|
||||||
|
$sql .= ' where';
|
||||||
|
foreach my $field (qw(country state city code)) {
|
||||||
|
$sql .= ' or' if (scalar @placeholders > 0);
|
||||||
|
$sql .= qq{ $field like ?};
|
||||||
|
push(@placeholders, '%'.$keywords.'%');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
push(@placeholders, $startIndex, $numberOfResults);
|
||||||
|
$sql .= ' order by country desc limit ?,?';
|
||||||
|
my $transactions = $db->read($sql, \@placeholders);
|
||||||
|
my $totalRecords = 0+$db->quickScalar('select found_rows()'); ##Must explicitly convert to numerics
|
||||||
|
my $tally = $transactions->rows();
|
||||||
|
my @records = ();
|
||||||
|
while (my $row = $transactions->hashRef) {
|
||||||
|
push(@records, $row);
|
||||||
|
}
|
||||||
|
my %results = (
|
||||||
|
totalRecords => $totalRecords + 0,
|
||||||
|
recordsReturned => $tally,
|
||||||
|
startIndex => $startIndex,
|
||||||
|
sort => undef,
|
||||||
|
dir => "desc",
|
||||||
|
records => \@records,
|
||||||
|
);
|
||||||
|
$session->http->setMimeType('text/json');
|
||||||
|
return JSON::to_json(\%results);
|
||||||
|
}
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
=head2 www_view ( )
|
=head2 www_view ( )
|
||||||
|
|
||||||
User interface to manage taxes. Provides a list of current taxes, and forms for adding
|
User interface to manage taxes. Provides a list of current taxes, and forms for adding
|
||||||
|
|
@ -366,22 +398,27 @@ new tax info, exporting and importing sets of taxes, and deleting individual tax
|
||||||
|
|
||||||
sub www_view {
|
sub www_view {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
return $self->session->privileges->insufficient
|
|
||||||
unless $self->canEdit;
|
|
||||||
my $session = $self->session;
|
my $session = $self->session;
|
||||||
|
my $admin = WebGUI::Shop::Admin->new($session);
|
||||||
|
return $session->privileges->insufficient
|
||||||
|
unless $admin->canManage;
|
||||||
##YUI specific datatable CSS
|
##YUI specific datatable CSS
|
||||||
$session->setLink($session->url->extras('yui/build/datatable/assets/skins/sam/datatable.css'), {type => 'text/CSS'});
|
my ($style, $url) = $session->quick(qw(style url));
|
||||||
|
$style->setLink($url->extras('/yui/build/fonts/fonts-min.css'), {rel=>'stylesheet', type=>'text/css'});
|
||||||
|
$style->setLink($url->extras('yui/build/datatable/assets/skins/sam/datatable.css'), {rel=>'stylesheet', type => 'text/CSS'});
|
||||||
##YUI basics
|
##YUI basics
|
||||||
$session->style->setScript($session->url->extras('yui/build/yahoo-dom-event/yahoo-dom-event.js'), {type => 'text/javascript'});
|
$style->setScript($url->extras('yui/build/yahoo-dom-event/yahoo-dom-event.js'), {type => 'text/javascript'});
|
||||||
$session->style->setScript($session->url->extras('yui/build/element/element-beta-min.js'), {type => 'text/javascript'});
|
$style->setScript($url->extras('yui/build/element/element-beta-min.js'), {type => 'text/javascript'});
|
||||||
$session->style->setScript($session->url->extras('yui/build/datasource/datasource-beta-min.js'), {type => 'text/javascript'});
|
$style->setScript($url->extras('yui/build/datasource/datasource-beta-min.js'), {type => 'text/javascript'});
|
||||||
##YUI Datatable
|
##YUI Datatable
|
||||||
$session->style->setScript($session->url->extras('yui/build/datatable/datatable-beta-min.js'), {type => 'text/javascript'});
|
$style->setScript($url->extras('yui/build/datatable/datatable-beta-min.js'), {type => 'text/javascript'});
|
||||||
##YUI JSON handler
|
##YUI JSON handler
|
||||||
$session->style->setScript($session->url->extras('yui/build/json/json-min.js'), {type => 'text/javascript'});
|
$style->setScript($url->extras('yui/build/json/json-min.js'), {type => 'text/javascript'});
|
||||||
##Build column headers. TODO: I18N
|
$style->setRawHeadTags('<style type="text/css"> #paging a { color: #0000de; } #search form { display: inline; } </style>');
|
||||||
my $i18n=WebGUI::International->new($session, 'Tax');
|
my $i18n=WebGUI::International->new($session, 'Tax');
|
||||||
$session->style->setRawHeadTags(sprintf <<'EOCHJS', $i18n->get('country'), $i18n->get('state'), $i18n->get('city'), $i18n->get('code'));
|
##Build column headers.
|
||||||
|
my $output = sprintf <<'EOCHJS', $i18n->get('country'), $i18n->get('state'), $i18n->get('city'), $i18n->get('code');
|
||||||
|
<script type="text/javascript">
|
||||||
var taxColumnDefs = [
|
var taxColumnDefs = [
|
||||||
{key:"country", label:"%s"},
|
{key:"country", label:"%s"},
|
||||||
{key:"state", label:"%s"},
|
{key:"state", label:"%s"},
|
||||||
|
|
@ -389,7 +426,6 @@ var taxColumnDefs = [
|
||||||
{key:"code", label:"%s"}
|
{key:"code", label:"%s"}
|
||||||
];
|
];
|
||||||
EOCHJS
|
EOCHJS
|
||||||
$session->style->setRawHeadTags();
|
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
32
t/Shop/Tax.t
32
t/Shop/Tax.t
|
|
@ -36,7 +36,7 @@ my $session = WebGUI::Test->session;
|
||||||
|
|
||||||
my $addExceptions = getAddExceptions($session);
|
my $addExceptions = getAddExceptions($session);
|
||||||
|
|
||||||
my $tests = 73 + 2*scalar(@{$addExceptions});
|
my $tests = 75 + 2*scalar(@{$addExceptions});
|
||||||
plan tests => 1 + $tests;
|
plan tests => 1 + $tests;
|
||||||
|
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
|
|
@ -492,8 +492,8 @@ my $alternateAddress = $book->addAddress({
|
||||||
label => 'using alternations',
|
label => 'using alternations',
|
||||||
city => 'Los Angeles',
|
city => 'Los Angeles',
|
||||||
state => 'CalifornIA',
|
state => 'CalifornIA',
|
||||||
code => '97123',
|
code => '92801',
|
||||||
country => 'U.S.A.',
|
country => 'USA',
|
||||||
});
|
});
|
||||||
|
|
||||||
eval { $taxer->getTaxRates(); };
|
eval { $taxer->getTaxRates(); };
|
||||||
|
|
@ -523,7 +523,7 @@ cmp_deeply(
|
||||||
|
|
||||||
cmp_deeply(
|
cmp_deeply(
|
||||||
$taxer->getTaxRates($alternateAddress),
|
$taxer->getTaxRates($alternateAddress),
|
||||||
[7.25], #Only 7.25 because it uses the alternate address for USA
|
[0.0, 8.25], #Hits USA and Los Angeles, California using the alternate spelling of the state
|
||||||
'getTaxRates: return correct data for a state when the address has alternations'
|
'getTaxRates: return correct data for a state when the address has alternations'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -599,6 +599,30 @@ foreach my $item (@{ $cart->getItems }) {
|
||||||
}
|
}
|
||||||
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');
|
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');
|
||||||
|
|
||||||
|
#######################################################################
|
||||||
|
#
|
||||||
|
# www_getTaxesAsJson
|
||||||
|
#
|
||||||
|
#######################################################################
|
||||||
|
|
||||||
|
$session->user({userId=>3});
|
||||||
|
my $json = $taxer->www_getTaxesAsJson();
|
||||||
|
diag $json;
|
||||||
|
ok($json, 'www_getTaxesAsJson returned something');
|
||||||
|
my $jsonTax = JSON::from_json($json);
|
||||||
|
cmp_deeply(
|
||||||
|
$jsonTax,
|
||||||
|
{
|
||||||
|
sort => undef,
|
||||||
|
startIndex => 0,
|
||||||
|
totalRecords => 1778,
|
||||||
|
recordsReturned => 25,
|
||||||
|
dir => 'desc',
|
||||||
|
records => array_each({taxId=>ignore, country => 'USA', state=>ignore, city=>ignore, code=>ignore, taxRate=>re('^\d+\.\d+$')}),
|
||||||
|
},
|
||||||
|
'Check major elements of JSON',
|
||||||
|
);
|
||||||
|
|
||||||
$taxableDonation->purge;
|
$taxableDonation->purge;
|
||||||
$taxFreeDonation->purge;
|
$taxFreeDonation->purge;
|
||||||
$cart->delete;
|
$cart->delete;
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue