Merge branch 'reports-api-fixes'
This commit is contained in:
commit
fcbd5c4292
|
@ -10,16 +10,26 @@
|
|||
class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
||||
|
||||
public $chart_colours = array();
|
||||
private $average_sales = 0;
|
||||
private $report_data = array();
|
||||
private $report_data;
|
||||
|
||||
/**
|
||||
* Get report data
|
||||
* @return array
|
||||
*/
|
||||
public function get_report_data() {
|
||||
if ( empty( $this->report_data ) ) {
|
||||
$this->query_report_data();
|
||||
}
|
||||
return $this->report_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all data needed for this report and store in the class
|
||||
*/
|
||||
private function query_report_data() {
|
||||
$this->report_data = array();
|
||||
$this->report_data = new stdClass;
|
||||
|
||||
$this->report_data['orders'] = (array) $this->get_order_report_data( array(
|
||||
$this->report_data->orders = (array) $this->get_order_report_data( array(
|
||||
'data' => array(
|
||||
'_order_total' => array(
|
||||
'type' => 'meta',
|
||||
|
@ -56,7 +66,7 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'parent_order_status' => array( 'completed', 'processing', 'on-hold' ),
|
||||
) );
|
||||
|
||||
$this->report_data['total_orders'] = (array) $this->get_order_report_data( array(
|
||||
$this->report_data->order_counts = (array) $this->get_order_report_data( array(
|
||||
'data' => array(
|
||||
'ID' => array(
|
||||
'type' => 'post_data',
|
||||
|
@ -78,7 +88,7 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'order_status' => array( 'completed', 'processing', 'on-hold' )
|
||||
) );
|
||||
|
||||
$this->report_data['coupons'] = (array) $this->get_order_report_data( array(
|
||||
$this->report_data->coupons = (array) $this->get_order_report_data( array(
|
||||
'data' => array(
|
||||
'order_item_name' => array(
|
||||
'type' => 'order_item',
|
||||
|
@ -112,7 +122,7 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'order_status' => array( 'completed', 'processing', 'on-hold' ),
|
||||
) );
|
||||
|
||||
$this->report_data['order_items'] = (array) $this->get_order_report_data( array(
|
||||
$this->report_data->order_items = (array) $this->get_order_report_data( array(
|
||||
'data' => array(
|
||||
'_qty' => array(
|
||||
'type' => 'order_item_meta',
|
||||
|
@ -141,7 +151,7 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'order_status' => array( 'completed', 'processing', 'on-hold' ),
|
||||
) );
|
||||
|
||||
$this->report_data['refunded_order_items'] = (array) $this->get_order_report_data( array(
|
||||
$this->report_data->refunded_order_items = (array) $this->get_order_report_data( array(
|
||||
'data' => array(
|
||||
'_qty' => array(
|
||||
'type' => 'order_item_meta',
|
||||
|
@ -170,7 +180,7 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'order_status' => array( 'refunded' ),
|
||||
) );
|
||||
|
||||
$this->report_data['partial_refunds'] = (array) $this->get_order_report_data( array(
|
||||
$this->report_data->partial_refunds = (array) $this->get_order_report_data( array(
|
||||
'data' => array(
|
||||
'_refund_amount' => array(
|
||||
'type' => 'meta',
|
||||
|
@ -197,13 +207,13 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'parent_order_status' => array( 'completed', 'processing', 'on-hold' ),
|
||||
) );
|
||||
|
||||
foreach( $this->report_data['partial_refunds'] as $key => $value ) {
|
||||
$this->report_data['partial_refunds'][ $key ]->order_item_count = $this->report_data['partial_refunds'][ $key ]->order_item_count * -1;
|
||||
foreach( $this->report_data->partial_refunds as $key => $value ) {
|
||||
$this->report_data->partial_refunds[ $key ]->order_item_count = $this->report_data->partial_refunds[ $key ]->order_item_count * -1;
|
||||
}
|
||||
|
||||
$this->report_data['order_items'] = array_merge( $this->report_data['order_items'], $this->report_data['partial_refunds'] );
|
||||
$this->report_data->order_items = array_merge( $this->report_data->order_items, $this->report_data->partial_refunds );
|
||||
|
||||
$this->report_data['total_order_refunds'] = (array) absint( $this->get_order_report_data( array(
|
||||
$this->report_data->total_order_refunds = array_sum( (array) absint( $this->get_order_report_data( array(
|
||||
'data' => array(
|
||||
'ID' => array(
|
||||
'type' => 'post_data',
|
||||
|
@ -215,9 +225,9 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'filter_range' => true,
|
||||
'order_types' => wc_get_order_types( 'order-count' ),
|
||||
'order_status' => array( 'refunded' ),
|
||||
) ) );
|
||||
) ) ) );
|
||||
|
||||
$this->report_data['full_refunds'] = (array) $this->get_order_report_data( array(
|
||||
$this->report_data->full_refunds = (array) $this->get_order_report_data( array(
|
||||
'data' => array(
|
||||
'_order_total' => array(
|
||||
'type' => 'meta',
|
||||
|
@ -230,14 +240,26 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'name' => 'post_date'
|
||||
),
|
||||
),
|
||||
'group_by' => $this->group_by_query,
|
||||
'group_by' => $this->group_by_query,
|
||||
'order_by' => 'post_date ASC',
|
||||
'query_type' => 'get_results',
|
||||
'filter_range' => true,
|
||||
'order_status' => array( 'refunded' )
|
||||
) );
|
||||
|
||||
$this->report_data['refunds'] = array_merge( $this->report_data['partial_refunds'], $this->report_data['full_refunds'] );
|
||||
$this->report_data->refunds = array_merge( $this->report_data->partial_refunds, $this->report_data->full_refunds );
|
||||
$this->report_data->total_sales = wc_format_decimal( array_sum( wp_list_pluck( $this->report_data->orders, 'total_sales' ) ), 2 );
|
||||
$this->report_data->total_tax = wc_format_decimal( array_sum( wp_list_pluck( $this->report_data->orders, 'total_tax' ) ), 2 );
|
||||
$this->report_data->total_shipping = wc_format_decimal( array_sum( wp_list_pluck( $this->report_data->orders, 'total_shipping' ) ), 2 );
|
||||
$this->report_data->total_shipping_tax = wc_format_decimal( array_sum( wp_list_pluck( $this->report_data->orders, 'total_shipping_tax' ) ), 2 );
|
||||
$this->report_data->total_refunds = wc_format_decimal( array_sum( wp_list_pluck( $this->report_data->partial_refunds, 'total_refund' ) ) + array_sum( wp_list_pluck( $this->report_data->full_refunds, 'total_refund' ) ), 2 );
|
||||
$this->report_data->total_coupons = number_format( array_sum( wp_list_pluck( $this->report_data->coupons, 'discount_amount' ) ), 2 );
|
||||
$this->report_data->total_orders = absint( array_sum( wp_list_pluck( $this->report_data->order_counts, 'count' ) ) );
|
||||
$this->report_data->total_partial_refunds = array_sum( wp_list_pluck( $this->report_data->partial_refunds, 'order_item_count' ) ) * -1;
|
||||
$this->report_data->total_item_refunds = array_sum( wp_list_pluck( $this->report_data->refunded_order_items, 'order_item_count' ) ) * -1;
|
||||
$this->report_data->total_items = absint( array_sum( wp_list_pluck( $this->report_data->order_items, 'order_item_count' ) ) * -1 );
|
||||
$this->report_data->average_sales = wc_format_decimal( $this->report_data->total_sales / ( $this->chart_interval + 1 ), 2 );
|
||||
$this->report_data->net_sales = wc_format_decimal( $this->report_data->total_sales - $this->report_data->total_shipping - $this->report_data->total_tax - $this->report_data->total_shipping_tax, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -245,43 +267,27 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
* @return array
|
||||
*/
|
||||
public function get_chart_legend() {
|
||||
$legend = array();
|
||||
|
||||
$this->query_report_data();
|
||||
|
||||
$total_sales = array_sum( wp_list_pluck( $this->report_data['orders'], 'total_sales' ) );
|
||||
$total_shipping = array_sum( wp_list_pluck( $this->report_data['orders'], 'total_shipping' ) );
|
||||
$total_tax = array_sum( wp_list_pluck( $this->report_data['orders'], 'total_tax' ) );
|
||||
$total_shipping_tax = array_sum( wp_list_pluck( $this->report_data['orders'], 'total_shipping_tax' ) );
|
||||
$net_sales = $total_sales - $total_shipping - $total_tax - $total_shipping_tax;
|
||||
$total_refunds = array_sum( wp_list_pluck( $this->report_data['partial_refunds'], 'total_refund' ) ) + array_sum( wp_list_pluck( $this->report_data['full_refunds'], 'total_refund' ) );
|
||||
$this->average_sales = $total_sales / ( $this->chart_interval + 1 );
|
||||
$total_coupons = array_sum( wp_list_pluck( $this->report_data['coupons'], 'discount_amount' ) );
|
||||
$total_orders = array_sum( wp_list_pluck( $this->report_data['total_orders'], 'count' ) );
|
||||
$total_order_refunds = array_sum( $this->report_data['total_order_refunds'] );
|
||||
|
||||
$total_partial_refunds = array_sum( wp_list_pluck( $this->report_data['partial_refunds'], 'order_item_count' ) ) * -1;
|
||||
$total_item_refunds = array_sum( wp_list_pluck( $this->report_data['refunded_order_items'], 'order_item_count' ) );
|
||||
$total_items = array_sum( wp_list_pluck( $this->report_data['order_items'], 'order_item_count' ) );
|
||||
$legend = array();
|
||||
$data = $this->get_report_data();
|
||||
|
||||
switch ( $this->chart_groupby ) {
|
||||
case 'day' :
|
||||
$average_sales_title = sprintf( __( '%s average daily sales', 'woocommerce' ), '<strong>' . wc_price( $this->average_sales ) . '</strong>' );
|
||||
$average_sales_title = sprintf( __( '%s average daily sales', 'woocommerce' ), '<strong>' . wc_price( $data->average_sales ) . '</strong>' );
|
||||
break;
|
||||
case 'month' :
|
||||
default :
|
||||
$average_sales_title = sprintf( __( '%s average monthly sales', 'woocommerce' ), '<strong>' . wc_price( $this->average_sales ) . '</strong>' );
|
||||
$average_sales_title = sprintf( __( '%s average monthly sales', 'woocommerce' ), '<strong>' . wc_price( $data->average_sales ) . '</strong>' );
|
||||
break;
|
||||
}
|
||||
|
||||
$legend[] = array(
|
||||
'title' => sprintf( __( '%s gross sales in this period', 'woocommerce' ), '<strong>' . wc_price( $total_sales ) . '</strong>' ),
|
||||
'title' => sprintf( __( '%s gross sales in this period', 'woocommerce' ), '<strong>' . wc_price( $data->total_sales ) . '</strong>' ),
|
||||
'placeholder' => __( 'This is the sum of the order totals after any refunds and including shipping and taxes.', 'woocommerce' ),
|
||||
'color' => $this->chart_colours['sales_amount'],
|
||||
'highlight_series' => 6
|
||||
);
|
||||
$legend[] = array(
|
||||
'title' => sprintf( __( '%s net sales in this period', 'woocommerce' ), '<strong>' . wc_price( $net_sales ) . '</strong>' ),
|
||||
'title' => sprintf( __( '%s net sales in this period', 'woocommerce' ), '<strong>' . wc_price( $data->net_sales ) . '</strong>' ),
|
||||
'placeholder' => __( 'This is the net sales figure excluding refunds, shipping and taxes.', 'woocommerce' ),
|
||||
'color' => $this->chart_colours['net_sales_amount'],
|
||||
'highlight_series' => 7
|
||||
|
@ -292,29 +298,29 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'highlight_series' => 2
|
||||
);
|
||||
$legend[] = array(
|
||||
'title' => sprintf( __( '%s orders placed', 'woocommerce' ), '<strong>' . ( $total_order_refunds + $total_orders !== $total_orders ? '<del>' . ( $total_order_refunds + $total_orders ) . '</del> ' : '' ) . $total_orders . '</strong>' ),
|
||||
'title' => sprintf( __( '%s orders placed', 'woocommerce' ), '<strong>' . ( $data->total_order_refunds + $data->total_orders !== $data->total_orders ? '<del>' . ( $data->total_order_refunds + $data->total_orders ) . '</del> ' : '' ) . $data->total_orders . '</strong>' ),
|
||||
'color' => $this->chart_colours['order_count'],
|
||||
'highlight_series' => 1
|
||||
);
|
||||
|
||||
$legend[] = array(
|
||||
'title' => sprintf( __( '%s items purchased', 'woocommerce' ), '<strong>' . ( $total_item_refunds + $total_partial_refunds > 0 ? '<del>' . ( $total_item_refunds + $total_partial_refunds + $total_items ) . '</del> ' : '' ) . $total_items . '</strong>' ),
|
||||
'title' => sprintf( __( '%s items purchased', 'woocommerce' ), '<strong>' . ( $data->total_item_refunds + $data->total_partial_refunds > 0 ? '<del>' . ( $data->total_item_refunds + $data->total_partial_refunds + $data->total_items ) . '</del> ' : '' ) . $data->total_items . '</strong>' ),
|
||||
'color' => $this->chart_colours['item_count'],
|
||||
'highlight_series' => 0
|
||||
);
|
||||
|
||||
$legend[] = array(
|
||||
'title' => sprintf( __( '%s charged for shipping', 'woocommerce' ), '<strong>' . wc_price( $total_shipping ) . '</strong>' ),
|
||||
'title' => sprintf( __( '%s charged for shipping', 'woocommerce' ), '<strong>' . wc_price( $data->total_shipping ) . '</strong>' ),
|
||||
'color' => $this->chart_colours['shipping_amount'],
|
||||
'highlight_series' => 5
|
||||
);
|
||||
$legend[] = array(
|
||||
'title' => sprintf( __( '%s in refunds', 'woocommerce' ), '<strong>' . wc_price( $total_refunds ) . '</strong>' ),
|
||||
'title' => sprintf( __( '%s in refunds', 'woocommerce' ), '<strong>' . wc_price( $data->total_refunds ) . '</strong>' ),
|
||||
'color' => $this->chart_colours['refund_amount'],
|
||||
'highlight_series' => 4
|
||||
);
|
||||
$legend[] = array(
|
||||
'title' => sprintf( __( '%s worth of coupons used', 'woocommerce' ), '<strong>' . wc_price( $total_coupons ) . '</strong>' ),
|
||||
'title' => sprintf( __( '%s worth of coupons used', 'woocommerce' ), '<strong>' . wc_price( $data->total_coupons ) . '</strong>' ),
|
||||
'color' => $this->chart_colours['coupon_amount'],
|
||||
'highlight_series' => 3
|
||||
);
|
||||
|
@ -384,14 +390,14 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
global $wp_locale;
|
||||
|
||||
// Prepare data for report
|
||||
$order_counts = $this->prepare_chart_data( $this->report_data['total_orders'], 'post_date', 'count', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$order_item_counts = $this->prepare_chart_data( $this->report_data['order_items'], 'post_date', 'order_item_count', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$order_amounts = $this->prepare_chart_data( $this->report_data['orders'], 'post_date', 'total_sales', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$coupon_amounts = $this->prepare_chart_data( $this->report_data['coupons'], 'post_date', 'discount_amount', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$shipping_amounts = $this->prepare_chart_data( $this->report_data['orders'], 'post_date', 'total_shipping', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$refund_amounts = $this->prepare_chart_data( $this->report_data['refunds'], 'post_date', 'total_refund', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$shipping_tax_amounts = $this->prepare_chart_data( $this->report_data['orders'], 'post_date', 'total_shipping_tax', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$tax_amounts = $this->prepare_chart_data( $this->report_data['orders'], 'post_date', 'total_tax', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$order_counts = $this->prepare_chart_data( $this->report_data->order_counts, 'post_date', 'count', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$order_item_counts = $this->prepare_chart_data( $this->report_data->order_items, 'post_date', 'order_item_count', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$order_amounts = $this->prepare_chart_data( $this->report_data->orders, 'post_date', 'total_sales', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$coupon_amounts = $this->prepare_chart_data( $this->report_data->coupons, 'post_date', 'discount_amount', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$shipping_amounts = $this->prepare_chart_data( $this->report_data->orders, 'post_date', 'total_shipping', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$refund_amounts = $this->prepare_chart_data( $this->report_data->refunds, 'post_date', 'total_refund', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$shipping_tax_amounts = $this->prepare_chart_data( $this->report_data->orders, 'post_date', 'total_shipping_tax', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
$tax_amounts = $this->prepare_chart_data( $this->report_data->orders, 'post_date', 'total_tax', $this->chart_interval, $this->start_date, $this->chart_groupby );
|
||||
|
||||
$net_order_amounts = array();
|
||||
|
||||
|
@ -440,7 +446,7 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
},
|
||||
{
|
||||
label: "<?php echo esc_js( __( 'Average sales amount', 'woocommerce' ) ) ?>",
|
||||
data: [ [ <?php echo min( array_keys( $order_amounts ) ); ?>, <?php echo $this->average_sales; ?> ], [ <?php echo max( array_keys( $order_amounts ) ); ?>, <?php echo $this->average_sales; ?> ] ],
|
||||
data: [ [ <?php echo min( array_keys( $order_amounts ) ); ?>, <?php echo $this->report_data->average_sales; ?> ], [ <?php echo max( array_keys( $order_amounts ) ); ?>, <?php echo $this->report_data->average_sales; ?> ] ],
|
||||
yaxis: 2,
|
||||
color: '<?php echo $this->chart_colours['average']; ?>',
|
||||
points: { show: false },
|
||||
|
|
|
@ -59,7 +59,6 @@ class WC_API_Reports extends WC_API_Resource {
|
|||
* @return array
|
||||
*/
|
||||
public function get_reports() {
|
||||
|
||||
return array( 'reports' => array( 'sales', 'sales/top_sellers' ) );
|
||||
}
|
||||
|
||||
|
@ -84,191 +83,25 @@ class WC_API_Reports extends WC_API_Resource {
|
|||
// 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' => 'total_sales'
|
||||
),
|
||||
'_order_shipping' => array(
|
||||
'type' => 'meta',
|
||||
'function' => 'SUM',
|
||||
'name' => 'total_shipping'
|
||||
),
|
||||
'_order_tax' => array(
|
||||
'type' => 'meta',
|
||||
'function' => 'SUM',
|
||||
'name' => 'total_tax'
|
||||
),
|
||||
'_order_shipping_tax' => array(
|
||||
'type' => 'meta',
|
||||
'function' => 'SUM',
|
||||
'name' => 'total_shipping_tax'
|
||||
),
|
||||
|
||||
'ID' => array(
|
||||
'type' => 'post_data',
|
||||
'function' => 'COUNT',
|
||||
'name' => 'order_count'
|
||||
)
|
||||
),
|
||||
'order_types' => wc_get_order_types( 'sales-reports' ),
|
||||
'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
|
||||
'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,
|
||||
) );
|
||||
|
||||
// new customers
|
||||
$users_query = new WP_User_Query(
|
||||
array(
|
||||
'fields' => array( 'user_registered' ),
|
||||
'role' => 'customer',
|
||||
'fields' => array( 'user_registered' ),
|
||||
'role' => 'customer',
|
||||
)
|
||||
);
|
||||
|
||||
$customers = $users_query->get_results();
|
||||
|
||||
foreach ( $customers as $key => $customer ) {
|
||||
if ( strtotime( $customer->user_registered ) < $this->report->start_date || strtotime( $customer->user_registered ) > $this->report->end_date )
|
||||
if ( strtotime( $customer->user_registered ) < $this->report->start_date || strtotime( $customer->user_registered ) > $this->report->end_date ) {
|
||||
unset( $customers[ $key ] );
|
||||
}
|
||||
}
|
||||
|
||||
$total_customers = count( $customers );
|
||||
|
||||
// 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'
|
||||
),
|
||||
'_order_tax' => array(
|
||||
'type' => 'meta',
|
||||
'function' => 'SUM',
|
||||
'name' => 'total_tax'
|
||||
),
|
||||
'_order_shipping_tax' => array(
|
||||
'type' => 'meta',
|
||||
'function' => 'SUM',
|
||||
'name' => 'total_shipping_tax'
|
||||
),
|
||||
'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();
|
||||
$report_data = $this->report->get_report_data();
|
||||
$period_totals = array();
|
||||
|
||||
// setup period totals by ensuring each period in the interval has data
|
||||
for ( $i = 0; $i <= $this->report->chart_interval; $i ++ ) {
|
||||
|
@ -285,7 +118,6 @@ class WC_API_Reports extends WC_API_Resource {
|
|||
// set the customer signups for each period
|
||||
$customer_count = 0;
|
||||
foreach ( $customers as $customer ) {
|
||||
|
||||
if ( date( ( 'day' == $this->report->chart_groupby ) ? 'Y-m-d' : 'Y-m', strtotime( $customer->user_registered ) ) == $time ) {
|
||||
$customer_count++;
|
||||
}
|
||||
|
@ -303,8 +135,7 @@ class WC_API_Reports extends WC_API_Resource {
|
|||
}
|
||||
|
||||
// add total sales, total order count, total tax and total shipping for each period
|
||||
foreach ( $orders as $order ) {
|
||||
|
||||
foreach ( $report_data->orders as $order ) {
|
||||
$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $order->post_date ) ) : date( 'Y-m', strtotime( $order->post_date ) );
|
||||
|
||||
if ( ! isset( $period_totals[ $time ] ) ) {
|
||||
|
@ -312,14 +143,22 @@ class WC_API_Reports extends WC_API_Resource {
|
|||
}
|
||||
|
||||
$period_totals[ $time ]['sales'] = wc_format_decimal( $order->total_sales, 2 );
|
||||
$period_totals[ $time ]['orders'] = (int) $order->total_orders;
|
||||
$period_totals[ $time ]['tax'] = wc_format_decimal( $order->total_tax + $order->total_shipping_tax, 2 );
|
||||
$period_totals[ $time ]['shipping'] = wc_format_decimal( $order->total_shipping, 2 );
|
||||
}
|
||||
|
||||
// add total order items for each period
|
||||
foreach ( $order_items as $order_item ) {
|
||||
foreach ( $report_data->order_counts as $order ) {
|
||||
$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $order->post_date ) ) : date( 'Y-m', strtotime( $order->post_date ) );
|
||||
|
||||
if ( ! isset( $period_totals[ $time ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$period_totals[ $time ]['orders'] = (int) $order->count;
|
||||
}
|
||||
|
||||
// add total order items for each period
|
||||
foreach ( $report_data->order_items as $order_item ) {
|
||||
$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $order_item->post_date ) ) : date( 'Y-m', strtotime( $order_item->post_date ) );
|
||||
|
||||
if ( ! isset( $period_totals[ $time ] ) ) {
|
||||
|
@ -330,8 +169,7 @@ class WC_API_Reports extends WC_API_Resource {
|
|||
}
|
||||
|
||||
// add total discount for each period
|
||||
foreach ( $discounts as $discount ) {
|
||||
|
||||
foreach ( $report_data->coupons as $discount ) {
|
||||
$time = ( 'day' === $this->report->chart_groupby ) ? date( 'Y-m-d', strtotime( $discount->post_date ) ) : date( 'Y-m', strtotime( $discount->post_date ) );
|
||||
|
||||
if ( ! isset( $period_totals[ $time ] ) ) {
|
||||
|
@ -341,15 +179,15 @@ class WC_API_Reports extends WC_API_Resource {
|
|||
$period_totals[ $time ]['discount'] = wc_format_decimal( $discount->discount_amount, 2 );
|
||||
}
|
||||
|
||||
$sales_data = array(
|
||||
'total_sales' => wc_format_decimal( $totals->total_sales, 2 ),
|
||||
'net_sales' => wc_format_decimal( $totals->total_sales - $totals->total_shipping - $totals->total_tax - $totals->total_shipping_tax, 2 ),
|
||||
'average_sales' => wc_format_decimal( $totals->total_sales / ( $this->report->chart_interval + 1 ), 2 ),
|
||||
'total_orders' => (int) $totals->order_count,
|
||||
'total_items' => $total_items,
|
||||
'total_tax' => wc_format_decimal( $totals->total_tax + $totals->total_shipping_tax, 2 ),
|
||||
'total_shipping' => wc_format_decimal( $totals->total_shipping, 2 ),
|
||||
'total_discount' => is_null( $total_discount ) ? wc_format_decimal( 0.00, 2 ) : wc_format_decimal( $total_discount, 2 ),
|
||||
$sales_data = array(
|
||||
'total_sales' => $report_data->total_sales,
|
||||
'net_sales' => $report_data->net_sales,
|
||||
'average_sales' => $report_data->average_sales,
|
||||
'total_orders' => $report_data->total_orders,
|
||||
'total_items' => $report_data->total_items,
|
||||
'total_tax' => $report_data->total_tax,
|
||||
'total_shipping' => $report_data->total_shipping,
|
||||
'total_discount' => $report_data->total_coupons,
|
||||
'totals_grouped_by' => $this->report->chart_groupby,
|
||||
'totals' => $period_totals,
|
||||
'total_customers' => $total_customers,
|
||||
|
@ -427,8 +265,9 @@ class WC_API_Reports extends WC_API_Resource {
|
|||
private function setup_report( $filter ) {
|
||||
|
||||
include_once( WC()->plugin_path() . '/includes/admin/reports/class-wc-admin-report.php' );
|
||||
include_once( WC()->plugin_path() . '/includes/admin/reports/class-wc-report-sales-by-date.php' );
|
||||
|
||||
$this->report = new WC_Admin_Report();
|
||||
$this->report = new WC_Report_Sales_By_Date();
|
||||
|
||||
if ( empty( $filter['period'] ) ) {
|
||||
|
||||
|
|
Loading…
Reference in New Issue