diff --git a/includes/admin/reports/class-wc-report-taxes-by-code.php b/includes/admin/reports/class-wc-report-taxes-by-code.php index e2f1cbef337..d43bfc693be 100644 --- a/includes/admin/reports/class-wc-report-taxes-by-code.php +++ b/includes/admin/reports/class-wc-report-taxes-by-code.php @@ -1,14 +1,17 @@ - + __( 'This month', 'woocommerce' ), ); - $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : 'last_month'; + $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( wp_unslash( $_GET['range'] ) ) : 'last_month'; if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', 'month', '7day' ) ) ) { $current_range = 'last_month'; @@ -124,27 +127,58 @@ class WC_Report_Taxes_By_Code extends WC_Admin_Report { 'order_by' => 'posts.post_date ASC', 'query_type' => 'get_results', 'filter_range' => true, - 'order_types' => array_merge( wc_get_order_types( 'sales-reports' ), array( 'shop_order_refund' ) ), - 'order_status' => array( 'completed', 'processing' ), - 'parent_order_status' => array( 'completed', 'processing' ), // Partial refunds inside refunded orders should be ignored + 'order_types' => wc_get_order_types( 'sales-reports' ), + 'order_status' => array( 'completed', 'processing', 'refunded' ), ) ); - // Merge + $tax_rows_partial_refunds = $this->get_order_report_data( + array( + 'data' => $query_data, + 'where' => $query_where, + 'order_by' => 'posts.post_date ASC', + 'query_type' => 'get_results', + 'filter_range' => true, + 'order_types' => array( 'shop_order_refund' ), + 'parent_order_status' => array( 'completed', 'processing' ), // Partial refunds inside refunded orders should be ignored. + ) + ); + + $tax_rows_full_refunds = $this->get_order_report_data( + array( + 'data' => $query_data, + 'where' => $query_where, + 'order_by' => 'posts.post_date ASC', + 'query_type' => 'get_results', + 'filter_range' => true, + 'order_types' => array( 'shop_order_refund' ), + 'parent_order_status' => array( 'refunded' ), + ) + ); + + // Merge. $tax_rows = array(); - foreach ( $tax_rows_orders as $tax_row ) { + foreach ( $tax_rows_orders + $tax_rows_partial_refunds as $tax_row ) { $key = $tax_row->rate_id; $tax_rows[ $key ] = isset( $tax_rows[ $key ] ) ? $tax_rows[ $key ] : (object) array( 'tax_amount' => 0, 'shipping_tax_amount' => 0, 'total_orders' => 0, ); + $tax_rows[ $key ]->total_orders += 1; + $tax_rows[ $key ]->tax_rate = $tax_row->tax_rate; + $tax_rows[ $key ]->tax_amount += wc_round_tax_total( $tax_row->tax_amount ); + $tax_rows[ $key ]->shipping_tax_amount += wc_round_tax_total( $tax_row->shipping_tax_amount ); + } - if ( 'shop_order_refund' !== get_post_type( $tax_row->post_id ) ) { - $tax_rows[ $key ]->total_orders += 1; - } - + foreach ( $tax_rows_full_refunds as $tax_row ) { + $key = $tax_row->rate_id; + $tax_rows[ $key ] = isset( $tax_rows[ $key ] ) ? $tax_rows[ $key ] : (object) array( + 'tax_amount' => 0, + 'shipping_tax_amount' => 0, + 'total_orders' => 0, + ); $tax_rows[ $key ]->tax_rate = $tax_row->tax_rate; $tax_rows[ $key ]->tax_amount += wc_round_tax_total( $tax_row->tax_amount ); $tax_rows[ $key ]->shipping_tax_amount += wc_round_tax_total( $tax_row->shipping_tax_amount ); @@ -153,12 +187,12 @@ class WC_Report_Taxes_By_Code extends WC_Admin_Report { - - - - - - + + + + + + @@ -168,12 +202,12 @@ class WC_Report_Taxes_By_Code extends WC_Admin_Report { $rate = $wpdb->get_var( $wpdb->prepare( "SELECT tax_rate FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %d;", $rate_id ) ); ?> - - - - - - + + + + + + - - - - + + + + - +
tax_rate, $rate_id, $tax_row ); ?>%total_orders; ?>tax_amount ); ?>shipping_tax_amount ); ?>tax_amount + $tax_row->shipping_tax_amount ); ?>tax_rate, $rate_id, $tax_row ) ); ?>%total_orders ); ?>tax_amount ); // phpcs:ignore ?>shipping_tax_amount ); // phpcs:ignore ?>tax_amount + $tax_row->shipping_tax_amount ); // phpcs:ignore ?>