From 9486ab789678de75a81358a468dab7e477051026 Mon Sep 17 00:00:00 2001 From: Joshua T Flowers Date: Tue, 18 Dec 2018 10:26:46 +0800 Subject: [PATCH] Update/taxes endpoint hookup/941 (https://github.com/woocommerce/woocommerce-admin/pull/1008) * Hook up taxes to endpoint * Hook up tax stats endpoint * Add default tax stat totals for empty results * Create subtotals from intervals in API * Change orders to orders_count to keep consistent naming --- .../client/analytics/report/taxes/table.js | 22 ++++++------- .../client/store/reports/items/resolvers.js | 2 +- .../client/store/reports/stats/resolvers.js | 4 +-- .../client/wc-api/reports/items/operations.js | 2 +- .../client/wc-api/reports/stats/operations.js | 4 +-- ...in-rest-reports-taxes-stats-controller.php | 32 ++++++++++++++++++- ...c-admin-reports-taxes-stats-data-store.php | 8 +++-- .../tests/api/reports-taxes-stats.php | 6 ++-- 8 files changed, 57 insertions(+), 23 deletions(-) diff --git a/plugins/woocommerce-admin/client/analytics/report/taxes/table.js b/plugins/woocommerce-admin/client/analytics/report/taxes/table.js index 01a09ffe07f..84c30cb4809 100644 --- a/plugins/woocommerce-admin/client/analytics/report/taxes/table.js +++ b/plugins/woocommerce-admin/client/analytics/report/taxes/table.js @@ -11,6 +11,7 @@ import { map } from 'lodash'; */ import { Link } from '@woocommerce/components'; import { formatCurrency, getCurrencyFormatDecimal } from '@woocommerce/currency'; +import { getTaxCode } from './utils'; /** * Internal dependencies @@ -71,25 +72,23 @@ export default class TaxesReportTable extends Component { getRowsContent( taxes ) { return map( taxes, tax => { - const { order_tax, orders_count, tax_rate_id, total_tax, shipping_tax } = tax; + const { order_tax, orders_count, tax_rate, tax_rate_id, total_tax, shipping_tax } = tax; // @TODO must link to the tax detail report const taxLink = ( - { tax_rate_id } + { getTaxCode( tax ) } ); return [ - // @TODO it should be the tax code, not the tax ID { display: taxLink, value: tax_rate_id, }, { - // @TODO add `rate` once it's returned by the API - display: '', - value: '', + display: tax_rate.toFixed( 2 ) + '%', + value: tax_rate, }, { display: formatCurrency( total_tax ), @@ -115,12 +114,10 @@ export default class TaxesReportTable extends Component { if ( ! totals ) { return []; } - // @TODO the number of total rows should come from the API - const totalRows = 0; return [ { - label: _n( 'tax code', 'tax codes', totalRows, 'wc-admin' ), - value: numberFormat( totalRows ), + label: _n( 'tax code', 'tax codes', totals.tax_codes, 'wc-admin' ), + value: numberFormat( totals.tax_codes ), }, { label: __( 'total tax', 'wc-admin' ), @@ -135,7 +132,7 @@ export default class TaxesReportTable extends Component { value: formatCurrency( totals.shipping_tax ), }, { - label: _n( 'order', 'orders', totals.orders_count, 'wc-admin' ), + label: _n( 'order', 'orders', totals.orders, 'wc-admin' ), value: numberFormat( totals.orders_count ), }, ]; @@ -153,6 +150,9 @@ export default class TaxesReportTable extends Component { getSummary={ this.getSummary } itemIdField="tax_rate_id" query={ query } + tableQuery={ { + orderby: query.orderby || 'tax_rate_id', + } } title={ __( 'Taxes', 'wc-admin' ) } columnPrefsKey="taxes_report_columns" /> diff --git a/plugins/woocommerce-admin/client/store/reports/items/resolvers.js b/plugins/woocommerce-admin/client/store/reports/items/resolvers.js index 3c34b6553b7..3c6887dc95f 100644 --- a/plugins/woocommerce-admin/client/store/reports/items/resolvers.js +++ b/plugins/woocommerce-admin/client/store/reports/items/resolvers.js @@ -20,7 +20,7 @@ export default { async getReportItems( ...args ) { const [ endpoint, query ] = args.slice( -2 ); - const swaggerEndpoints = [ 'categories', 'coupons', 'customers', 'taxes' ]; + const swaggerEndpoints = [ 'categories', 'coupons' ]; if ( swaggerEndpoints.indexOf( endpoint ) >= 0 ) { try { const response = await fetch( diff --git a/plugins/woocommerce-admin/client/store/reports/stats/resolvers.js b/plugins/woocommerce-admin/client/store/reports/stats/resolvers.js index 3ecb1479bb4..0cde00310d2 100644 --- a/plugins/woocommerce-admin/client/store/reports/stats/resolvers.js +++ b/plugins/woocommerce-admin/client/store/reports/stats/resolvers.js @@ -23,12 +23,12 @@ export default { // TODO: Change to just `getNotes( endpoint, query )` // after Gutenberg plugin uses @wordpress/data 3+ const [ endpoint, query ] = args.length === 2 ? args : args.slice( 1, 3 ); - const statEndpoints = [ 'orders', 'revenue', 'products' ]; + const statEndpoints = [ 'orders', 'revenue', 'products', 'taxes' ]; let apiPath = endpoint + stringifyQuery( query ); // TODO: Remove once swagger endpoints are phased out. - const swaggerEndpoints = [ 'categories', 'coupons', 'taxes' ]; + const swaggerEndpoints = [ 'categories', 'coupons' ]; if ( swaggerEndpoints.indexOf( endpoint ) >= 0 ) { apiPath = SWAGGERNAMESPACE + 'reports/' + endpoint + '/stats' + stringifyQuery( query ); try { diff --git a/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js b/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js index 14f4bf99720..9d9a90457c9 100644 --- a/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js +++ b/plugins/woocommerce-admin/client/wc-api/reports/items/operations.js @@ -17,7 +17,7 @@ import { NAMESPACE } from '../../constants'; import { SWAGGERNAMESPACE } from 'store/constants'; // TODO: Remove once swagger endpoints are phased out. -const swaggerEndpoints = [ 'categories', 'coupons', 'customers', 'taxes' ]; +const swaggerEndpoints = [ 'categories', 'coupons', 'customers' ]; const typeEndpointMap = { 'report-items-query-orders': 'orders', diff --git a/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js b/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js index ee3d67adb54..fdb265b8bb9 100644 --- a/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js +++ b/plugins/woocommerce-admin/client/wc-api/reports/stats/operations.js @@ -16,9 +16,9 @@ import { getResourceIdentifier, getResourcePrefix } from '../../utils'; import { NAMESPACE } from '../../constants'; import { SWAGGERNAMESPACE } from 'store/constants'; -const statEndpoints = [ 'orders', 'revenue', 'products' ]; +const statEndpoints = [ 'orders', 'revenue', 'products', 'taxes' ]; // TODO: Remove once swagger endpoints are phased out. -const swaggerEndpoints = [ 'categories', 'coupons', 'taxes' ]; +const swaggerEndpoints = [ 'categories', 'coupons' ]; const typeEndpointMap = { 'report-stats-query-orders': 'orders', diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-taxes-stats-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-taxes-stats-controller.php index 525e9b65ce9..59b466b5339 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-taxes-stats-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-taxes-stats-controller.php @@ -31,6 +31,36 @@ class WC_Admin_REST_Reports_Taxes_Stats_Controller extends WC_REST_Reports_Contr */ protected $rest_base = 'reports/taxes/stats'; + /** + * Constructor. + */ + public function __construct() { + add_filter( 'woocommerce_reports_taxes_stats_select_query', array( $this, 'set_default_report_data' ) ); + } + + /** + * Set the default results to 0 if API returns an empty array + * + * @param Mixed $results Report data. + * @return object + */ + public function set_default_report_data( $results ) { + if ( empty( $results ) ) { + $results = new stdClass(); + $results->total = 0; + $results->totals = new stdClass(); + $results->totals->tax_codes = 0; + $results->totals->total_tax = 0; + $results->totals->order_tax = 0; + $results->totals->shipping_tax = 0; + $results->totals->orders = 0; + $results->intervals = array(); + $results->pages = 1; + $results->page_no = 1; + } + return $results; + } + /** * Maps query arguments from the REST request. * @@ -150,7 +180,7 @@ class WC_Admin_REST_Reports_Taxes_Stats_Controller extends WC_REST_Reports_Contr 'context' => array( 'view', 'edit' ), 'readonly' => true, ), - 'order_count' => array( + 'orders_count' => array( 'description' => __( 'Amount of orders.', 'wc-admin' ), 'type' => 'integer', 'context' => array( 'view', 'edit' ), diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-taxes-stats-data-store.php b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-taxes-stats-data-store.php index 9fec0dc08ba..ca50e5919f1 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-taxes-stats-data-store.php +++ b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-taxes-stats-data-store.php @@ -30,7 +30,7 @@ class WC_Admin_Reports_Taxes_Stats_Data_Store extends WC_Admin_Reports_Data_Stor 'total_tax' => 'floatval', 'order_tax' => 'floatval', 'shipping_tax' => 'floatval', - 'order_count' => 'intval', + 'orders_count' => 'intval', ); /** @@ -43,7 +43,7 @@ class WC_Admin_Reports_Taxes_Stats_Data_Store extends WC_Admin_Reports_Data_Stor 'total_tax' => 'SUM(total_tax) AS total_tax', 'order_tax' => 'SUM(order_tax) as order_tax', 'shipping_tax' => 'SUM(shipping_tax) as shipping_tax', - 'order_count' => 'COUNT(DISTINCT order_id) as orders', + 'orders_count' => 'COUNT(DISTINCT order_id) as orders_count', ); /** @@ -198,6 +198,10 @@ class WC_Admin_Reports_Taxes_Stats_Data_Store extends WC_Admin_Reports_Data_Stor } $totals = (object) $this->cast_numbers( $totals[0] ); + + $this->update_interval_boundary_dates( $query_args['after'], $query_args['before'], $query_args['interval'], $intervals ); + $this->create_interval_subtotals( $intervals ); + $data = (object) array( 'totals' => $totals, 'intervals' => $intervals, diff --git a/plugins/woocommerce-admin/tests/api/reports-taxes-stats.php b/plugins/woocommerce-admin/tests/api/reports-taxes-stats.php index c4f39d0b582..5d373b7a2c7 100644 --- a/plugins/woocommerce-admin/tests/api/reports-taxes-stats.php +++ b/plugins/woocommerce-admin/tests/api/reports-taxes-stats.php @@ -114,7 +114,7 @@ class WC_Tests_API_Reports_Taxes_Stats extends WC_REST_Unit_Test_Case { $this->assertEquals( 16, $tax_report['total_tax'] ); $this->assertEquals( 13, $tax_report['order_tax'] ); $this->assertEquals( 3, $tax_report['shipping_tax'] ); - $this->assertEquals( 2, $tax_report['orders'] ); + $this->assertEquals( 2, $tax_report['orders_count'] ); } /** @@ -145,7 +145,7 @@ class WC_Tests_API_Reports_Taxes_Stats extends WC_REST_Unit_Test_Case { $this->assertEquals( 5, count( $totals ) ); $this->assertArrayHasKey( 'order_tax', $totals ); - $this->assertArrayHasKey( 'order_count', $totals ); + $this->assertArrayHasKey( 'orders_count', $totals ); $this->assertArrayHasKey( 'shipping_tax', $totals ); $this->assertArrayHasKey( 'tax_codes', $totals ); $this->assertArrayHasKey( 'total_tax', $totals ); @@ -162,7 +162,7 @@ class WC_Tests_API_Reports_Taxes_Stats extends WC_REST_Unit_Test_Case { $subtotals = $properties['intervals']['items']['properties']['subtotals']['properties']; $this->assertEquals( 5, count( $subtotals ) ); $this->assertArrayHasKey( 'order_tax', $totals ); - $this->assertArrayHasKey( 'order_count', $totals ); + $this->assertArrayHasKey( 'orders_count', $totals ); $this->assertArrayHasKey( 'shipping_tax', $totals ); $this->assertArrayHasKey( 'tax_codes', $totals ); $this->assertArrayHasKey( 'total_tax', $totals );