Sales by date handling for refunds

- Exclude refunds from gross/net
- Correct order counts
- Display refunded order and item count, plot after refunds
- Combine query functions for legend and main chart
This commit is contained in:
Mike Jolley 2015-02-13 13:40:30 +00:00
parent 55969390f0
commit 50322801e5
1 changed files with 219 additions and 239 deletions

View File

@ -11,66 +11,15 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
public $chart_colours = array();
private $average_sales = 0;
private $report_data = array();
/**
* Get the legend for the main chart sidebar
* @return array
* Get all data needed for this report and store in the class
*/
public function get_chart_legend() {
$legend = array();
private function query_report_data() {
$this->report_data = array();
$total_orders = absint( $this->get_order_report_data( array(
'data' => array(
'ID' => array(
'type' => 'post_data',
'function' => 'COUNT',
'name' => 'total_orders'
)
),
'query_type' => 'get_var',
'filter_range' => true,
'order_types' => wc_get_order_types( 'order-count' ),
'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
) ) );
$total_items = absint( $this->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',
'order_types' => wc_get_order_types( 'order-count' ),
'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
'filter_range' => true,
) ) );
$total_coupons = $this->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_items.order_item_type',
'value' => 'coupon',
'operator' => '='
)
),
'query_type' => 'get_var',
'order_types' => wc_get_order_types( 'order-count' ),
'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
'filter_range' => true,
) );
$order_totals = $this->get_order_report_data( array(
$this->report_data['orders'] = (array) $this->get_order_report_data( array(
'data' => array(
'_order_total' => array(
'type' => 'meta',
@ -91,49 +40,229 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
'type' => 'meta',
'function' => 'SUM',
'name' => 'total_shipping_tax'
)
),
'post_date' => array(
'type' => 'post_data',
'function' => '',
'name' => 'post_date'
),
),
'order_types' => wc_get_order_types( 'sales-reports' ),
'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
'filter_range' => true
'group_by' => $this->group_by_query,
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true,
'order_types' => array_merge( array( 'shop_order_refund' ), wc_get_order_types( 'sales-reports' ) ),
'order_status' => array( 'completed', 'processing', 'on-hold' ),
'parent_order_status' => array( 'completed', 'processing', 'on-hold' ),
) );
$partial_refunds = $this->get_order_report_data( array(
$this->report_data['total_orders'] = (array) $this->get_order_report_data( array(
'data' => array(
'ID' => array(
'type' => 'post_data',
'function' => 'COUNT',
'name' => 'count',
'distinct' => true,
),
'post_date' => array(
'type' => 'post_data',
'function' => '',
'name' => 'post_date'
)
),
'group_by' => $this->group_by_query,
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true,
'order_types' => wc_get_order_types( 'order-count' ),
'order_status' => array( 'completed', 'processing', 'on-hold' )
) );
$this->report_data['coupons'] = (array) $this->get_order_report_data( array(
'data' => array(
'order_item_name' => array(
'type' => 'order_item',
'function' => '',
'name' => 'order_item_name'
),
'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_items.order_item_type',
'value' => 'coupon',
'operator' => '='
)
),
'group_by' => $this->group_by_query . ', order_item_name',
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true,
'order_types' => wc_get_order_types( 'order-count' ),
'order_status' => array( 'completed', 'processing', 'on-hold' ),
) );
$this->report_data['order_items'] = (array) $this->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_items.order_item_type',
'value' => 'line_item',
'operator' => '='
)
),
'group_by' => $this->group_by_query,
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true,
'order_types' => wc_get_order_types( 'order-count' ),
'order_status' => array( 'completed', 'processing', 'on-hold' ),
) );
$this->report_data['refunded_order_items'] = (array) $this->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_items.order_item_type',
'value' => 'line_item',
'operator' => '='
)
),
'group_by' => $this->group_by_query,
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true,
'order_types' => wc_get_order_types( 'order-count' ),
'order_status' => array( 'refunded' ),
) );
$this->report_data['partial_refunds'] = (array) $this->get_order_report_data( array(
'data' => array(
'_refund_amount' => array(
'type' => 'meta',
'function' => 'SUM',
'name' => 'total_refund'
),
'post_date' => array(
'type' => 'post_data',
'function' => '',
'name' => 'post_date'
),
'_qty' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => 'order_item_count'
)
),
'query_type' => 'get_var',
'order_types' => array( 'shop_order_refund' ),
'group_by' => $this->group_by_query,
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true,
'order_status' => false,
'parent_order_status' => array( 'completed', 'processing', 'on-hold' ),
) );
$full_refunds = $this->get_order_report_data( array(
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['total_order_refunds'] = (array) absint( $this->get_order_report_data( array(
'data' => array(
'ID' => array(
'type' => 'post_data',
'function' => 'COUNT',
'name' => 'total_orders'
)
),
'query_type' => 'get_var',
'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(
'data' => array(
'_order_total' => array(
'type' => 'meta',
'function' => 'SUM',
'name' => 'total_sales'
'name' => 'total_refund'
),
'post_date' => array(
'type' => 'post_data',
'function' => '',
'name' => 'post_date'
),
),
'query_type' => 'get_var',
'order_types' => wc_get_order_types( 'sales-reports' ),
'order_status' => array( 'refunded' ),
'filter_range' => true
'group_by' => $this->group_by_query,
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true,
'order_status' => array( 'refunded' )
) );
$total_sales = $order_totals->total_sales;
$total_shipping = $order_totals->total_shipping;
$total_tax = $order_totals->total_tax;
$total_shipping_tax = $order_totals->total_shipping_tax;
$net_sales = $order_totals->total_sales - $total_shipping - $total_tax - $total_shipping_tax;
$total_refunds = $partial_refunds + $full_refunds;
$this->average_sales = $total_sales / ( $this->chart_interval + 1 );
$this->report_data['refunds'] = array_merge( $this->report_data['partial_refunds'], $this->report_data['full_refunds'] );
}
/**
* Get the legend for the main chart sidebar
* @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' ) );
switch ( $this->chart_groupby ) {
case 'day' :
@ -147,13 +276,13 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
$legend[] = array(
'title' => sprintf( __( '%s gross sales in this period', 'woocommerce' ), '<strong>' . wc_price( $total_sales ) . '</strong>' ),
'placeholder' => __( 'This is the sum of the order totals, including shipping and taxes. This does not include refunds.', 'woocommerce' ),
'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>' ),
'placeholder' => __( 'This is the net sales figure excluding shipping and taxes. This does not include refunds.', 'woocommerce' ),
'placeholder' => __( 'This is the net sales figure excluding refunds, shipping and taxes.', 'woocommerce' ),
'color' => $this->chart_colours['net_sales_amount'],
'highlight_series' => 7
);
@ -163,15 +292,17 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
'highlight_series' => 2
);
$legend[] = array(
'title' => sprintf( __( '%s orders placed', 'woocommerce' ), '<strong>' . $total_orders . '</strong>' ),
'title' => sprintf( __( '%s orders placed', 'woocommerce' ), '<strong>' . ( $total_order_refunds + $total_orders !== $total_orders ? '<del>' . ( $total_order_refunds + $total_orders ) . '</del> ' : '' ) . $total_orders . '</strong>' ),
'color' => $this->chart_colours['order_count'],
'highlight_series' => 1
);
$legend[] = array(
'title' => sprintf( __( '%s items purchased', 'woocommerce' ), '<strong>' . $total_items . '</strong>' ),
'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>' ),
'color' => $this->chart_colours['item_count'],
'highlight_series' => 0
);
$legend[] = array(
'title' => sprintf( __( '%s charged for shipping', 'woocommerce' ), '<strong>' . wc_price( $total_shipping ) . '</strong>' ),
'color' => $this->chart_colours['shipping_amount'],
@ -195,7 +326,6 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
* Output the report
*/
public function output_report() {
$ranges = array(
'year' => __( 'Year', 'woocommerce' ),
'last_month' => __( 'Last Month', 'woocommerce' ),
@ -229,7 +359,6 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
* Output an export link
*/
public function get_export_button() {
$current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : '7day';
?>
<a
@ -254,164 +383,15 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
public function get_main_chart() {
global $wp_locale;
// Get orders and dates in range - we want the SUM of order totals, COUNT of order items, COUNT of orders, and the date
$orders = (array) $this->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->group_by_query,
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true,
'order_types' => wc_get_order_types( 'sales-reports' ),
'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
) );
// Order items
$order_items = (array) $this->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_items.order_item_type',
'value' => 'line_item',
'operator' => '='
)
),
'group_by' => $this->group_by_query,
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true,
'order_types' => wc_get_order_types( 'sales-reports' ),
'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
) );
// Get discount amounts in range
$coupons = (array) $this->get_order_report_data( array(
'data' => array(
'order_item_name' => array(
'type' => 'order_item',
'function' => '',
'name' => 'order_item_name'
),
'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_items.order_item_type',
'value' => 'coupon',
'operator' => '='
)
),
'group_by' => $this->group_by_query . ', order_item_name',
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true,
'order_types' => wc_get_order_types( 'sales-reports' ),
'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
) );
$partial_refunds = (array) $this->get_order_report_data( array(
'data' => array(
'_refund_amount' => array(
'type' => 'meta',
'function' => 'SUM',
'name' => 'total_refund'
),
'post_date' => array(
'type' => 'post_data',
'function' => '',
'name' => 'post_date'
)
),
'group_by' => $this->group_by_query,
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true,
'order_status' => false,
'parent_order_status' => array( 'completed', 'processing', 'on-hold' ),
) );
$full_refunds = (array) $this->get_order_report_data( array(
'data' => array(
'_order_total' => array(
'type' => 'meta',
'function' => 'SUM',
'name' => 'total_refund'
),
'post_date' => array(
'type' => 'post_data',
'function' => '',
'name' => 'post_date'
),
),
'group_by' => $this->group_by_query,
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true,
'order_status' => array( 'refunded' ),
) );
$refunds = array_merge( $partial_refunds, $full_refunds );
// Prepare data for report
$order_counts = $this->prepare_chart_data( $orders, 'post_date', 'total_orders', $this->chart_interval, $this->start_date, $this->chart_groupby );
$order_item_counts = $this->prepare_chart_data( $order_items, 'post_date', 'order_item_count', $this->chart_interval, $this->start_date, $this->chart_groupby );
$order_amounts = $this->prepare_chart_data( $orders, 'post_date', 'total_sales', $this->chart_interval, $this->start_date, $this->chart_groupby );
$coupon_amounts = $this->prepare_chart_data( $coupons, 'post_date', 'discount_amount', $this->chart_interval, $this->start_date, $this->chart_groupby );
$shipping_amounts = $this->prepare_chart_data( $orders, 'post_date', 'total_shipping', $this->chart_interval, $this->start_date, $this->chart_groupby );
$refund_amounts = $this->prepare_chart_data( $refunds, 'post_date', 'total_refund', $this->chart_interval, $this->start_date, $this->chart_groupby );
$shipping_tax_amounts = $this->prepare_chart_data( $orders, 'post_date', 'total_shipping_tax', $this->chart_interval, $this->start_date, $this->chart_groupby );
$tax_amounts = $this->prepare_chart_data( $orders, 'post_date', 'total_tax', $this->chart_interval, $this->start_date, $this->chart_groupby );
$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 );
$net_order_amounts = array();