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:
Colin Kuskie 2008-03-18 23:34:35 +00:00
parent a838b340c0
commit 94b03aa3f5
3 changed files with 1865 additions and 35 deletions

View file

@ -6,6 +6,7 @@ use Class::InsideOut qw{ :std };
use WebGUI::Text;
use WebGUI::Storage;
use WebGUI::Exception::Shop;
use WebGUI::Shop::Admin;
use WebGUI::Shop::Cart;
use WebGUI::Shop::CartItem;
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] )
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 ( )
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 {
my $self = shift;
return $self->session->privileges->insufficient
unless $self->canEdit;
my $session = $self->session;
my $admin = WebGUI::Shop::Admin->new($session);
return $session->privileges->insufficient
unless $admin->canManage;
##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
$session->style->setScript($session->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'});
$session->style->setScript($session->url->extras('yui/build/datasource/datasource-beta-min.js'), {type => 'text/javascript'});
$style->setScript($url->extras('yui/build/yahoo-dom-event/yahoo-dom-event.js'), {type => 'text/javascript'});
$style->setScript($url->extras('yui/build/element/element-beta-min.js'), {type => 'text/javascript'});
$style->setScript($url->extras('yui/build/datasource/datasource-beta-min.js'), {type => 'text/javascript'});
##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
$session->style->setScript($session->url->extras('yui/build/json/json-min.js'), {type => 'text/javascript'});
##Build column headers. TODO: I18N
$style->setScript($url->extras('yui/build/json/json-min.js'), {type => 'text/javascript'});
$style->setRawHeadTags('<style type="text/css"> #paging a { color: #0000de; } #search form { display: inline; } </style>');
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 = [
{key:"country", label:"%s"},
{key:"state", label:"%s"},
@ -389,7 +426,6 @@ var taxColumnDefs = [
{key:"code", label:"%s"}
];
EOCHJS
$session->style->setRawHeadTags();
return '';
}

View file

@ -36,7 +36,7 @@ my $session = WebGUI::Test->session;
my $addExceptions = getAddExceptions($session);
my $tests = 73 + 2*scalar(@{$addExceptions});
my $tests = 75 + 2*scalar(@{$addExceptions});
plan tests => 1 + $tests;
#----------------------------------------------------------------------------
@ -492,8 +492,8 @@ my $alternateAddress = $book->addAddress({
label => 'using alternations',
city => 'Los Angeles',
state => 'CalifornIA',
code => '97123',
country => 'U.S.A.',
code => '92801',
country => 'USA',
});
eval { $taxer->getTaxRates(); };
@ -523,7 +523,7 @@ cmp_deeply(
cmp_deeply(
$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'
);
@ -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');
#######################################################################
#
# 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;
$taxFreeDonation->purge;
$cart->delete;

File diff suppressed because it is too large Load diff