2013-11-04 06:36:31 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce API Reports Class
|
|
|
|
*
|
|
|
|
* Handles requests to the /reports endpoint
|
|
|
|
*
|
|
|
|
* @author WooThemes
|
|
|
|
* @category API
|
|
|
|
* @package WooCommerce/API
|
|
|
|
* @since 2.1
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
|
|
|
|
|
|
|
|
2013-11-09 21:20:23 +00:00
|
|
|
class WC_API_Reports extends WC_API_Resource {
|
2013-11-04 06:36:31 +00:00
|
|
|
|
|
|
|
/** @var string $base the route base */
|
|
|
|
protected $base = '/reports';
|
|
|
|
|
2013-11-15 05:21:19 +00:00
|
|
|
/** @var WC_Admin_Report instance */
|
|
|
|
private $report;
|
|
|
|
|
2013-11-04 06:36:31 +00:00
|
|
|
/**
|
|
|
|
* Register the routes for this class
|
|
|
|
*
|
|
|
|
* GET /reports
|
|
|
|
* GET /reports/sales
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param array $routes
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
public function register_routes( $routes ) {
|
2013-11-04 06:36:31 +00:00
|
|
|
|
|
|
|
# GET /reports
|
|
|
|
$routes[ $this->base ] = array(
|
2013-11-11 00:29:36 +00:00
|
|
|
array( array( $this, 'get_reports' ), WC_API_Server::READABLE ),
|
2013-11-04 06:36:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
# GET /reports/sales
|
|
|
|
$routes[ $this->base . '/sales'] = array(
|
2013-11-11 00:29:36 +00:00
|
|
|
array( array( $this, 'get_sales_report' ), WC_API_Server::READABLE ),
|
2013-11-04 06:36:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $routes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a simple listing of available reports
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
public function get_reports() {
|
2013-11-04 06:36:31 +00:00
|
|
|
|
|
|
|
return array( 'reports' => array( 'sales' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the sales report
|
|
|
|
*
|
|
|
|
* @since 2.1
|
2013-11-15 05:21:19 +00:00
|
|
|
* @param string $fields fields to include in response
|
|
|
|
* @param array $filter date filtering
|
2013-11-04 06:36:31 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2013-11-15 05:21:19 +00:00
|
|
|
public function get_sales_report( $fields = null, $filter = array() ) {
|
|
|
|
|
|
|
|
// check user permissions
|
|
|
|
$check = $this->validate_request();
|
|
|
|
|
|
|
|
if ( is_wp_error( $check ) )
|
|
|
|
return $check;
|
|
|
|
|
|
|
|
// set date filtering
|
|
|
|
$this->setup_report( $filter );
|
|
|
|
|
|
|
|
// total sales, taxes, shipping, and order count
|
|
|
|
$totals = $this->report->get_order_report_data( array(
|
|
|
|
'data' => array(
|
|
|
|
'_order_total' => array(
|
|
|
|
'type' => 'meta',
|
|
|
|
'function' => 'SUM',
|
|
|
|
'name' => 'sales'
|
|
|
|
),
|
|
|
|
'_order_tax' => array(
|
|
|
|
'type' => 'meta',
|
|
|
|
'function' => 'SUM',
|
|
|
|
'name' => 'tax'
|
|
|
|
),
|
|
|
|
'_order_shipping_tax' => array(
|
|
|
|
'type' => 'meta',
|
|
|
|
'function' => 'SUM',
|
|
|
|
'name' => 'shipping_tax'
|
|
|
|
),
|
|
|
|
'_order_shipping' => array(
|
|
|
|
'type' => 'meta',
|
|
|
|
'function' => 'SUM',
|
|
|
|
'name' => 'shipping'
|
|
|
|
),
|
|
|
|
'ID' => array(
|
|
|
|
'type' => 'post_data',
|
|
|
|
'function' => 'COUNT',
|
|
|
|
'name' => 'order_count'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'filter_range' => true,
|
|
|
|
) );
|
|
|
|
|
|
|
|
// total items ordered
|
|
|
|
$total_items = absint( $this->report->get_order_report_data( array(
|
|
|
|
'data' => array(
|
|
|
|
'_qty' => array(
|
|
|
|
'type' => 'order_item_meta',
|
|
|
|
'order_item_type' => 'line_item',
|
|
|
|
'function' => 'SUM',
|
|
|
|
'name' => 'order_item_qty'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'query_type' => 'get_var',
|
|
|
|
'filter_range' => true,
|
|
|
|
) ) );
|
|
|
|
|
|
|
|
// total discount used
|
|
|
|
$total_discount = $this->report->get_order_report_data( array(
|
|
|
|
'data' => array(
|
|
|
|
'discount_amount' => array(
|
|
|
|
'type' => 'order_item_meta',
|
|
|
|
'order_item_type' => 'coupon',
|
|
|
|
'function' => 'SUM',
|
|
|
|
'name' => 'discount_amount'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'where' => array(
|
|
|
|
array(
|
|
|
|
'key' => 'order_item_type',
|
|
|
|
'value' => 'coupon',
|
|
|
|
'operator' => '='
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'query_type' => 'get_var',
|
|
|
|
'filter_range' => true,
|
|
|
|
) );
|
|
|
|
|
|
|
|
// get order totals grouped by period
|
|
|
|
$orders = $this->report->get_order_report_data( array(
|
|
|
|
'data' => array(
|
|
|
|
'_order_total' => array(
|
|
|
|
'type' => 'meta',
|
|
|
|
'function' => 'SUM',
|
|
|
|
'name' => 'total_sales'
|
|
|
|
),
|
|
|
|
'_order_shipping' => array(
|
|
|
|
'type' => 'meta',
|
|
|
|
'function' => 'SUM',
|
|
|
|
'name' => 'total_shipping'
|
|
|
|
),
|
2013-11-18 21:47:38 +00:00
|
|
|
'_order_tax' => array(
|
|
|
|
'type' => 'meta',
|
|
|
|
'function' => 'SUM',
|
|
|
|
'name' => 'total_tax'
|
|
|
|
),
|
|
|
|
'_order_shipping_tax' => array(
|
|
|
|
'type' => 'meta',
|
|
|
|
'function' => 'SUM',
|
|
|
|
'name' => 'total_shipping_tax'
|
|
|
|
),
|
2013-11-15 05:21:19 +00:00
|
|
|
'ID' => array(
|
|
|
|
'type' => 'post_data',
|
|
|
|
'function' => 'COUNT',
|
|
|
|
'name' => 'total_orders',
|
|
|
|
'distinct' => true,
|
|
|
|
),
|
|
|
|
'post_date' => array(
|
|
|
|
'type' => 'post_data',
|
|
|
|
'function' => '',
|
|
|
|
'name' => 'post_date'
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'group_by' => $this->report->group_by_query,
|
|
|
|
'order_by' => 'post_date ASC',
|
|
|
|
'query_type' => 'get_results',
|
|
|
|
'filter_range' => true,
|
|
|
|
) );
|
|
|
|
|
|
|
|
// get order item totals grouped by period
|
|
|
|
$order_items = $this->report->get_order_report_data( array(
|
|
|
|
'data' => array(
|
|
|
|
'_qty' => array(
|
|
|
|
'type' => 'order_item_meta',
|
|
|
|
'order_item_type' => 'line_item',
|
|
|
|
'function' => 'SUM',
|
|
|
|
'name' => 'order_item_count'
|
|
|
|
),
|
|
|
|
'post_date' => array(
|
|
|
|
'type' => 'post_data',
|
|
|
|
'function' => '',
|
|
|
|
'name' => 'post_date'
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'where' => array(
|
|
|
|
array(
|
|
|
|
'key' => 'order_item_type',
|
|
|
|
'value' => 'line_item',
|
|
|
|
'operator' => '='
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'group_by' => $this->report->group_by_query,
|
|
|
|
'order_by' => 'post_date ASC',
|
|
|
|
'query_type' => 'get_results',
|
|
|
|
'filter_range' => true,
|
|
|
|
) );
|
|
|
|
|
|
|
|
// get discount totals grouped by period
|
|
|
|
$discounts = $this->report->get_order_report_data( array(
|
|
|
|
'data' => array(
|
|
|
|
'discount_amount' => array(
|
|
|
|
'type' => 'order_item_meta',
|
|
|
|
'order_item_type' => 'coupon',
|
|
|
|
'function' => 'SUM',
|
|
|
|
'name' => 'discount_amount'
|
|
|
|
),
|
|
|
|
'post_date' => array(
|
|
|
|
'type' => 'post_data',
|
|
|
|
'function' => '',
|
|
|
|
'name' => 'post_date'
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'where' => array(
|
|
|
|
array(
|
|
|
|
'key' => 'order_item_type',
|
|
|
|
'value' => 'coupon',
|
|
|
|
'operator' => '='
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'group_by' => $this->report->group_by_query . ', order_item_name',
|
|
|
|
'order_by' => 'post_date ASC',
|
|
|
|
'query_type' => 'get_results',
|
|
|
|
'filter_range' => true,
|
|
|
|
) );
|
|
|
|
|
|
|
|
$period_totals = array();
|
|
|
|
|
|
|
|
// setup period totals by ensuring each period in the interval has data
|
|
|
|
for ( $i = 0; $i <= $this->report->chart_interval; $i ++ ) {
|
|
|
|
|
|
|
|
switch ( $this->report->chart_groupby ) {
|
|
|
|
case 'day' :
|
|
|
|
$time = date( 'Y-m-d', strtotime( "+{$i} DAY", $this->report->start_date ) );
|
|
|
|
break;
|
|
|
|
case 'month' :
|
|
|
|
$time = date( 'Y-m', strtotime( "+{$i} MONTH", $this->report->start_date ) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$period_totals[ $time ] = array(
|
2013-11-18 21:47:38 +00:00
|
|
|
'sales' => woocommerce_format_decimal( 0.00 ),
|
2013-11-15 05:21:19 +00:00
|
|
|
'orders' => 0,
|
|
|
|
'items' => 0,
|
2013-11-18 21:47:38 +00:00
|
|
|
'tax' => woocommerce_format_decimal( 0.00 ),
|
|
|
|
'shipping' => woocommerce_format_decimal( 0.00 ),
|
|
|
|
'discount' => woocommerce_format_decimal( 0.00 ),
|
2013-11-15 05:21:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add total sales, total order count, total tax and total shipping for each period
|
|
|
|
foreach ( $orders as $order ) {
|
|
|
|
|
2013-11-18 21:47:38 +00:00
|
|
|
$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $order->post_date ) ) : date( 'Y-m', strtotime( $order->post_date ) );
|
2013-11-15 05:21:19 +00:00
|
|
|
|
|
|
|
if ( ! isset( $period_totals[ $time ] ) )
|
|
|
|
continue;
|
|
|
|
|
2013-11-18 21:47:38 +00:00
|
|
|
$period_totals[ $time ]['sales'] = woocommerce_format_decimal( $order->total_sales );
|
|
|
|
$period_totals[ $time ]['orders'] = (int) $order->total_orders;
|
|
|
|
$period_totals[ $time ]['tax'] = woocommerce_format_decimal( $order->total_tax + $order->total_shipping_tax );
|
|
|
|
$period_totals[ $time ]['shipping'] = woocommerce_format_decimal( $order->total_shipping );
|
2013-11-15 05:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add total order items for each period
|
|
|
|
foreach ( $order_items as $order_item ) {
|
|
|
|
|
2013-11-18 21:47:38 +00:00
|
|
|
$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $order_item->post_date ) ) : date( 'Y-m', strtotime( $order_item->post_date ) );
|
2013-11-15 05:21:19 +00:00
|
|
|
|
|
|
|
if ( ! isset( $period_totals[ $time ] ) )
|
|
|
|
continue;
|
|
|
|
|
2013-11-18 21:47:38 +00:00
|
|
|
$period_totals[ $time ]['items'] = (int) $order_item->order_item_count;
|
2013-11-15 05:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add total discount for each period
|
|
|
|
foreach ( $discounts as $discount ) {
|
|
|
|
|
2013-11-18 21:47:38 +00:00
|
|
|
$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $discount->post_date ) ) : date( 'Y-m', strtotime( $discount->post_date ) );
|
2013-11-15 05:21:19 +00:00
|
|
|
|
|
|
|
if ( ! isset( $period_totals[ $time ] ) )
|
|
|
|
continue;
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-18 21:47:38 +00:00
|
|
|
$period_totals[ $time ]['discount'] = woocommerce_format_decimal( $discount->discount_amount );
|
2013-11-15 05:21:19 +00:00
|
|
|
}
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-15 05:21:19 +00:00
|
|
|
$sales_data = array(
|
2013-11-18 21:47:38 +00:00
|
|
|
'sales' => woocommerce_format_decimal( $totals->sales ),
|
|
|
|
'average' => woocommerce_format_decimal( $totals->sales / ( $this->report->chart_interval + 1 ) ),
|
|
|
|
'orders' => (int) $totals->order_count,
|
2013-11-15 05:21:19 +00:00
|
|
|
'items' => $total_items,
|
2013-11-18 21:47:38 +00:00
|
|
|
'tax' => woocommerce_format_decimal( $totals->tax + $totals->shipping_tax ),
|
|
|
|
'shipping' => woocommerce_format_decimal( $totals->shipping ),
|
|
|
|
'discount' => is_null( $total_discount ) ? woocommerce_format_decimal( 0.00 ) : woocommerce_format_decimal( $total_discount ),
|
2013-11-15 05:21:19 +00:00
|
|
|
'totals_grouped_by' => $this->report->chart_groupby,
|
2013-11-18 21:47:38 +00:00
|
|
|
'totals' => $period_totals,
|
2013-11-15 05:21:19 +00:00
|
|
|
);
|
|
|
|
|
2013-11-19 02:59:13 +00:00
|
|
|
return array( 'sales' => apply_filters( 'woocommerce_api_report_response', $sales_data, 'sales', $fields, $this->report, $this->server ) );
|
2013-11-15 05:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the report object and parse any date filtering
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param array $filter date filtering
|
|
|
|
*/
|
|
|
|
private function setup_report( $filter ) {
|
|
|
|
|
|
|
|
include_once( WC()->plugin_path() . '/includes/admin/reports/class-wc-admin-report.php' );
|
|
|
|
|
|
|
|
$this->report = new WC_Admin_Report();
|
|
|
|
|
|
|
|
if ( empty( $filter['period'] ) ) {
|
|
|
|
|
|
|
|
// custom date range
|
|
|
|
$filter['period'] = 'custom';
|
|
|
|
|
|
|
|
if ( ! empty( $filter['date_min'] ) || ! empty( $filter['date_max'] ) ) {
|
|
|
|
|
|
|
|
// overwrite _GET to make use of WC_Admin_Report::calculate_current_range() for custom date ranges
|
2013-11-18 21:47:38 +00:00
|
|
|
$_GET['start_date'] = $this->server->parse_datetime( $filter['date_min'] );
|
|
|
|
$_GET['end_date'] = isset( $filter['date_max'] ) ? $this->server->parse_datetime( $filter['date_max'] ) : null;
|
2013-11-15 05:21:19 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// default custom range to today
|
|
|
|
$_GET['start_date'] = $_GET['end_date'] = date( 'Y-m-d', current_time( 'timestamp' ) );
|
|
|
|
}
|
|
|
|
|
2013-11-18 21:47:38 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
// ensure period is valid
|
|
|
|
if ( ! in_array( $filter['period'], array( 'week', 'month', 'last_month', 'year' ) ) ) {
|
|
|
|
$filter['period'] = 'week';
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: change WC_Admin_Report class to use "week" instead, as it's more consistent with other periods
|
|
|
|
// allow "week" for period instead of "7day"
|
|
|
|
if ( 'week' === $filter['period'] ) {
|
|
|
|
$filter['period'] = '7day';
|
|
|
|
}
|
|
|
|
}
|
2013-11-15 05:21:19 +00:00
|
|
|
|
|
|
|
$this->report->calculate_current_range( $filter['period'] );
|
2013-11-04 06:36:31 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 05:21:19 +00:00
|
|
|
/**
|
|
|
|
* Verify that the current user has permission to view reports
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @see WC_API_Resource::validate_request()
|
|
|
|
* @param null $id unused
|
|
|
|
* @param null $type unused
|
|
|
|
* @param null $context unused
|
|
|
|
* @return bool true if the request is valid and should be processed, false otherwise
|
|
|
|
*/
|
|
|
|
protected function validate_request( $id = null, $type = null, $context = null ) {
|
|
|
|
|
|
|
|
if ( ! current_user_can( 'view_woocommerce_reports' ) ) {
|
|
|
|
|
|
|
|
return new WP_Error( 'woocommerce_api_user_cannot_read_report', __( 'You do not have permission to read this report', 'woocommerce' ), array( 'status' => 401 ) );
|
2013-11-19 02:59:13 +00:00
|
|
|
|
2013-11-15 05:21:19 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-11-04 06:36:31 +00:00
|
|
|
}
|