diff --git a/lib/WebGUI/SQL.pm b/lib/WebGUI/SQL.pm index 6dba577dc..5ab46ff76 100644 --- a/lib/WebGUI/SQL.pm +++ b/lib/WebGUI/SQL.pm @@ -225,8 +225,7 @@ sub buildArrayRefOfHashRefs { my $sql = shift; my $params = shift; my $sth = $class->read($sql,$params); - my $data; - while ($data = $sth->hashRef) { + while (my $data = $sth->hashRef) { push(@array,$data); } $sth->finish; @@ -236,7 +235,47 @@ sub buildArrayRefOfHashRefs { #------------------------------------------------------------------- -=head2 buildHashRefOfHashRefs ( sql ) +=head2 buildDataTableStructure ( sql, params ) + +Builds a data structure that can be converted to JSON and sent +to a YUI Data Table. This is basically a hash of information about +the results, with one of the keys being an array ref of hashrefs. It also +calculates the total records that could have been matched without a limit +statement, as well as how many were actually matched. + +=head3 sql + +An SQL query. The query may select as many columns of data as you wish. The query +should contain a SQL_CALC_ROWS_FOUND entry so that the total number of available +rows can be sent to the Data Table. + +=head3 params + +An array reference containing values for any placeholder params used in the SQL query. + +=cut + +sub buildDataTableStructure { + my $self = shift; + my $sql = shift; + my $params = shift; + my %hash; + my @array; + ##Note, I need a valid statement handle for doing the rows method on. + my $sth = $self->read($sql,$params); + while (my $data = $sth->hashRef) { + push(@array,$data); + } + $hash{records} = \@array; + $hash{totalRecords} = $self->quickScalar('select found_rows()') + 0; ##Convert to numeric + $hash{recordsReturned} = $sth->rows(); + $sth->finish; + return \%hash; +} + +#------------------------------------------------------------------- + +=head2 buildHashRefOfHashRefs ( sql, params, key ) Builds a hash reference of hash references of data from a series of rows. Useful for returning many rows at once. diff --git a/lib/WebGUI/SQL/ResultSet.pm b/lib/WebGUI/SQL/ResultSet.pm index 5a91844c8..0e0bf7b28 100644 --- a/lib/WebGUI/SQL/ResultSet.pm +++ b/lib/WebGUI/SQL/ResultSet.pm @@ -68,8 +68,8 @@ Returns the next row of data as an array reference. Note that this is 12% faster =cut sub arrayRef { - my $self = shift; - return $self->sth->fetchrow_arrayref() or $self->db->session->errorHandler->fatal("Couldn't fetch array. ".$self->errorMessage); + my $self = shift; + return $self->sth->fetchrow_arrayref() or $self->db->session->errorHandler->fatal("Couldn't fetch array. ".$self->errorMessage); } @@ -82,8 +82,8 @@ A reference to the current WebGUI::SQL object. =cut sub db { - my $self = shift; - return $self->{_db}; + my $self = shift; + return $self->{_db}; } #------------------------------------------------------------------- diff --git a/lib/WebGUI/Shop/Transaction.pm b/lib/WebGUI/Shop/Transaction.pm index 5379ceccd..a8d53ee5f 100644 --- a/lib/WebGUI/Shop/Transaction.pm +++ b/lib/WebGUI/Shop/Transaction.pm @@ -9,6 +9,7 @@ use WebGUI::Exception::Shop; use WebGUI::Form; use WebGUI::International; use WebGUI::Paginator; +use WebGUI::Shop::Admin; use WebGUI::Shop::AddressBook; use WebGUI::Shop::TransactionItem; diff --git a/t/Shop/Tax.t b/t/Shop/Tax.t index 4dc0dc0fd..b0441a02f 100644 --- a/t/Shop/Tax.t +++ b/t/Shop/Tax.t @@ -607,7 +607,6 @@ is($taxer->calculate($cart), 5.5, 'calculate: simple tax calculation on 2 items $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( @@ -618,9 +617,16 @@ cmp_deeply( totalRecords => 1778, recordsReturned => 25, dir => 'desc', - records => array_each({taxId=>ignore, country => 'USA', state=>ignore, city=>ignore, code=>ignore, taxRate=>re('^\d+\.\d+$')}), + records => array_each({ + taxId=>ignore, + country => 'USA', + state=>ignore, + city=>ignore, + code=>ignore, + taxRate=>re('^\d+\.\d+$') + }), }, - 'Check major elements of JSON', + 'Check major elements of tax JSON', ); $taxableDonation->purge; diff --git a/t/Shop/Transaction.t b/t/Shop/Transaction.t index 933e1e984..32a6ef092 100644 --- a/t/Shop/Transaction.t +++ b/t/Shop/Transaction.t @@ -30,7 +30,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -plan tests => 64; # Increment this number for each test you create +plan tests => 66; # Increment this number for each test you create #---------------------------------------------------------------------------- # put your tests here @@ -183,13 +183,48 @@ my $icopy = $transaction->getItem($item->getId); isa_ok($icopy, "WebGUI::Shop::TransactionItem"); is($icopy->getId, $item->getId, "items are the same"); -# get itmes +# get items is(scalar @{$transaction->getItems}, 1, "can retrieve items"); # delete $item->delete; is(scalar @{$transaction->getItems}, 0, "can delete items"); +####################################################################### +# +# www_getTaxesAsJson +# +####################################################################### + +$session->user({userId=>3}); +my $json = WebGUI::Shop::Transaction->www_getTransactionsAsJson($session); +ok($json, 'www_getTransactionsAsJson returned something'); +my $jsonTransactions = JSON::from_json($json); +cmp_deeply( + $jsonTransactions, + { + sort => undef, + startIndex => 0, + totalRecords => 1, + recordsReturned => 1, + dir => 'desc', + records => array_each({ + orderNumber=>ignore, + transactionId=>ignore, + transactionCode=>ignore, + paymentDriverLabel=>ignore, + dateOfPurchase=>ignore, + username=>ignore, + amount=>ignore, + isSuccessful=>ignore, + statusCode=>ignore, + statusMessage=>ignore, + }), + }, + 'Check major elements of transaction JSON', +); + + $transaction->delete; is($session->db->quickScalar("select transactionId from transaction where transactionId=?",[$transaction->getId]), undef, "can delete transactions"); @@ -198,5 +233,5 @@ is($session->db->quickScalar("select transactionId from transaction where transa #---------------------------------------------------------------------------- # Cleanup END { - + $session->db->write('delete from transaction'); }