Correct report handling for full and partial refunds.

Fixes #6530

@claudiosmweb look good?
This commit is contained in:
Mike Jolley 2014-10-14 16:39:57 +01:00
parent b353c75d43
commit c01bc63020
2 changed files with 97 additions and 35 deletions

View File

@ -41,17 +41,19 @@ class WC_Admin_Report {
global $wpdb;
$default_args = array(
'data' => array(),
'where' => array(),
'where_meta' => array(),
'query_type' => 'get_row',
'group_by' => '',
'order_by' => '',
'limit' => '',
'filter_range' => false,
'nocache' => false,
'debug' => false,
'order_types' => wc_get_order_types( 'reports' )
'data' => array(),
'where' => array(),
'where_meta' => array(),
'query_type' => 'get_row',
'group_by' => '',
'order_by' => '',
'limit' => '',
'filter_range' => false,
'nocache' => true,
'debug' => false,
'order_types' => wc_get_order_types( 'reports' ),
'order_status' => array( 'completed', 'processing', 'on-hold' ),
'parent_order_status' => false,
);
$args = apply_filters( 'woocommerce_reports_get_order_report_data_args', $args );
$args = wp_parse_args( $args, $default_args );
@ -62,6 +64,8 @@ class WC_Admin_Report {
return '';
}
$order_status = apply_filters( 'woocommerce_reports_order_statuses', $order_status );
$query = array();
$select = array();
@ -139,18 +143,33 @@ class WC_Admin_Report {
}
}
if ( ! empty( $parent_order_status ) ) {
$joins["parent"] = "LEFT JOIN {$wpdb->posts} AS parent ON posts.post_parent = parent.ID";
}
$query['join'] = implode( ' ', $joins );
$query['where'] = "
WHERE posts.post_type IN ( '" . implode( "','", $order_types ) . "' )
AND posts.post_status IN ( 'wc-" . implode( "','wc-", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "')
";
if ( ! empty( $order_status ) ) {
$query['where'] .= "
AND posts.post_status IN ( 'wc-" . implode( "','wc-", $order_status ) . "')
";
}
if ( ! empty( $parent_order_status ) ) {
$query['where'] .= "
AND parent.post_status IN ( 'wc-" . implode( "','wc-", $parent_order_status ) . "')
";
}
if ( $filter_range ) {
$query['where'] .= "
AND post_date >= '" . date('Y-m-d', $this->start_date ) . "'
AND post_date < '" . date('Y-m-d', strtotime( '+1 DAY', $this->end_date ) ) . "'
AND posts.post_date >= '" . date('Y-m-d', $this->start_date ) . "'
AND posts.post_date < '" . date('Y-m-d', strtotime( '+1 DAY', $this->end_date ) ) . "'
";
}
@ -386,7 +405,7 @@ class WC_Admin_Report {
'operator' => '='
)
),
'group_by' => 'YEAR(post_date), MONTH(post_date), DAY(post_date)',
'group_by' => 'YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)',
'query_type' => 'get_results',
'filter_range' => false
) );
@ -412,7 +431,7 @@ class WC_Admin_Report {
'operator' => '>'
)
),
'group_by' => 'YEAR(post_date), MONTH(post_date), DAY(post_date)',
'group_by' => 'YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)',
'query_type' => 'get_results',
'filter_range' => false
) );
@ -495,13 +514,13 @@ class WC_Admin_Report {
switch ( $this->chart_groupby ) {
case 'day' :
$this->group_by_query = 'YEAR(post_date), MONTH(post_date), DAY(post_date)';
$this->group_by_query = 'YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)';
$this->chart_interval = ceil( max( 0, ( $this->end_date - $this->start_date ) / ( 60 * 60 * 24 ) ) );
$this->barwidth = 60 * 60 * 24 * 1000;
break;
case 'month' :
$this->group_by_query = 'YEAR(post_date), MONTH(post_date)';
$this->group_by_query = 'YEAR(posts.post_date), MONTH(posts.post_date)';
$this->chart_interval = 0;
$min_date = $this->start_date;

View File

@ -16,7 +16,6 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
* @return array
*/
public function get_chart_legend() {
$legend = array();
$order_totals = $this->get_order_report_data( array(
@ -33,6 +32,7 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
)
),
'order_types' => wc_get_order_types( 'sales-reports' ),
'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
'filter_range' => true
) );
@ -48,7 +48,8 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
),
'query_type' => 'get_var',
'filter_range' => true,
'order_types' => wc_get_order_types( 'order-count' )
'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(
@ -60,9 +61,10 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
'name' => 'order_item_qty'
)
),
'query_type' => 'get_var',
'query_type' => 'get_var',
'order_types' => wc_get_order_types( 'order-count' ),
'filter_range' => true
'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
'filter_range' => true,
) ) );
// Get discount amounts in range
@ -84,10 +86,10 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
),
'query_type' => 'get_var',
'order_types' => wc_get_order_types( 'order-count' ),
'filter_range' => true
'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
'filter_range' => true,
) );
$total_refunds = $this->get_order_report_data( array(
$partial_refunds = $this->get_order_report_data( array(
'data' => array(
'_refund_amount' => array(
'type' => 'meta',
@ -95,10 +97,26 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
'name' => 'total_refund'
)
),
'query_type' => 'get_var',
'order_types' => array( 'shop_order_refund' ),
'filter_range' => true,
'order_status' => false,
'parent_order_status' => array( 'completed', 'processing', 'on-hold' ),
) );
$full_refunds = $this->get_order_report_data( array(
'data' => array(
'_order_total' => array(
'type' => 'meta',
'function' => 'SUM',
'name' => 'total_sales'
),
),
'query_type' => 'get_var',
'order_types' => array( 'shop_order_refund' ),
'order_types' => wc_get_order_types( 'sales-reports' ),
'order_status' => array( 'refunded' ),
'filter_range' => true
) );
$total_refunds = $partial_refunds + $full_refunds;
$this->average_sales = $total_sales / ( $this->chart_interval + 1 );
@ -248,7 +266,9 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
'group_by' => $this->group_by_query,
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true
'filter_range' => true,
'order_types' => wc_get_order_types( 'sales-reports' ),
'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
) );
// Order items
@ -276,7 +296,9 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
'group_by' => $this->group_by_query,
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true
'filter_range' => true,
'order_types' => wc_get_order_types( 'sales-reports' ),
'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
) );
// Get discount amounts in range
@ -309,10 +331,12 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
'group_by' => $this->group_by_query . ', order_item_name',
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true
'filter_range' => true,
'order_types' => wc_get_order_types( 'sales-reports' ),
'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
) );
$refunds = $this->get_order_report_data( array(
$partial_refunds = $this->get_order_report_data( array(
'data' => array(
'_refund_amount' => array(
'type' => 'meta',
@ -325,13 +349,33 @@ 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' => false,
'parent_order_status' => array( 'completed', 'processing', 'on-hold' ),
) );
$full_refunds = $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
'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 );
@ -341,7 +385,6 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
$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 );
// Encode in json format
$chart_data = json_encode( array(
'order_counts' => array_values( $order_counts ),