Skip to content

Commit

Permalink
Allow sorting by tax_code in Taxes report (woocommerce#1812)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljullu authored Mar 15, 2019
1 parent 7df44d3 commit 0224ef6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
10 changes: 5 additions & 5 deletions client/analytics/report/taxes/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ export default class TaxesReportTable extends Component {
return [
{
label: __( 'Tax Code', 'woocommerce-admin' ),
// @todo It should be the tax code, not the ID
key: 'tax_rate_id',
key: 'tax_code',
required: true,
isLeftAligned: true,
isSortable: true,
Expand Down Expand Up @@ -72,19 +71,20 @@ export default class TaxesReportTable extends Component {

getRowsContent( taxes ) {
return map( taxes, tax => {
const { order_tax, orders_count, tax_rate, tax_rate_id, total_tax, shipping_tax } = tax;
const { order_tax, orders_count, tax_rate, total_tax, shipping_tax } = tax;
const taxCode = getTaxCode( tax );

// @todo Must link to the tax detail report
const taxLink = (
<Link href="" type="wc-admin">
{ getTaxCode( tax ) }
{ taxCode }
</Link>
);

return [
{
display: taxLink,
value: tax_rate_id,
value: taxCode,
},
{
display: tax_rate.toFixed( 2 ) + '%',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ public function get_collection_params() {
'enum' => array(
'name',
'tax_rate_id',
'tax_code',
'rate',
'order_tax',
'total_tax',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ protected function get_cache_key( $params ) {
protected function normalize_order_by( $order_by ) {
global $wpdb;

if ( 'rate' === $order_by ) {
if ( 'tax_code' === $order_by ) {
return 'CONCAT_WS( "-", NULLIF(tax_rate_country, ""), NULLIF(tax_rate_state, ""), NULLIF(tax_rate_name, ""), NULLIF(tax_rate_priority, "") )';
} else if ( 'rate' === $order_by ) {
return "CAST({$wpdb->prefix}woocommerce_tax_rates.tax_rate as DECIMAL(7,4))";
}

Expand Down
55 changes: 55 additions & 0 deletions tests/api/reports-taxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,61 @@ public function test_get_reports_orderby_tax_rate() {
$this->assertEquals( 10, $reports[1]['tax_rate'] );
}

/**
* Test getting reports with param `orderby=tax_code`.
*
* @since 3.5.0
*/
public function test_get_reports_orderby_tax_code() {
global $wpdb;
wp_set_current_user( $this->user );
WC_Helper_Reports::reset_stats_dbs();

$wpdb->insert(
$wpdb->prefix . 'woocommerce_tax_rates',
array(
'tax_rate_id' => 1,
'tax_rate' => '7',
'tax_rate_country' => 'US',
'tax_rate_state' => 'GA',
'tax_rate_name' => 'TestTax',
'tax_rate_priority' => 1,
'tax_rate_order' => 1,
)
);

$wpdb->insert(
$wpdb->prefix . 'woocommerce_tax_rates',
array(
'tax_rate_id' => 2,
'tax_rate' => '10',
'tax_rate_country' => 'CA',
'tax_rate_state' => 'ON',
'tax_rate_name' => 'TestTax 2',
'tax_rate_priority' => 1,
'tax_rate_order' => 1,
)
);

$request = new WP_REST_Request( 'GET', $this->endpoint );
$request->set_query_params(
array(
'order' => 'asc',
'orderby' => 'tax_code',
'taxes' => '1,2',
)
);
$response = $this->server->dispatch( $request );
$reports = $response->get_data();

$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 2, count( $reports ) );

$this->assertEquals( 2, $reports[0]['tax_rate_id'] );

$this->assertEquals( 1, $reports[1]['tax_rate_id'] );
}

/**
* Test getting reports without valid permissions.
*
Expand Down

0 comments on commit 0224ef6

Please sign in to comment.