Handle full refunds in the taxes by code report (#21843)
* Add full refund support to the taxes by code report. * Some PHPCS fixes * Do not include refund in initial tax lookup for report. * Correct logic which loops orders * match date report
This commit is contained in:
parent
548b00c660
commit
f77e2d7316
|
@ -1,14 +1,17 @@
|
|||
<?php
|
||||
/**
|
||||
* Taxes by tax code report.
|
||||
*
|
||||
* @package WooCommerce/Admin/Reports
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WC_Report_Taxes_By_Code
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category Admin
|
||||
* @package WooCommerce/Admin/Reports
|
||||
* @version 2.1.0
|
||||
*/
|
||||
|
@ -28,15 +31,15 @@ class WC_Report_Taxes_By_Code extends WC_Admin_Report {
|
|||
*/
|
||||
public function get_export_button() {
|
||||
|
||||
$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';
|
||||
?>
|
||||
<a
|
||||
href="#"
|
||||
download="report-<?php echo esc_attr( $current_range ); ?>-<?php echo date_i18n( 'Y-m-d', current_time( 'timestamp' ) ); ?>.csv"
|
||||
download="report-<?php echo esc_attr( $current_range ); ?>-<?php echo esc_attr( date_i18n( 'Y-m-d', current_time( 'timestamp' ) ) ); ?>.csv"
|
||||
class="export_csv"
|
||||
data-export="table"
|
||||
>
|
||||
<?php _e( 'Export CSV', 'woocommerce' ); ?>
|
||||
<?php esc_html_e( 'Export CSV', 'woocommerce' ); ?>
|
||||
</a>
|
||||
<?php
|
||||
}
|
||||
|
@ -52,7 +55,7 @@ class WC_Report_Taxes_By_Code extends WC_Admin_Report {
|
|||
'month' => __( '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 {
|
|||
<table class="widefat">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php _e( 'Tax', 'woocommerce' ); ?></th>
|
||||
<th><?php _e( 'Rate', 'woocommerce' ); ?></th>
|
||||
<th class="total_row"><?php _e( 'Number of orders', 'woocommerce' ); ?></th>
|
||||
<th class="total_row"><?php _e( 'Tax amount', 'woocommerce' ); ?> <?php echo wc_help_tip( __( 'This is the sum of the "Tax rows" tax amount within your orders.', 'woocommerce' ) ); ?></th>
|
||||
<th class="total_row"><?php _e( 'Shipping tax amount', 'woocommerce' ); ?> <?php echo wc_help_tip( __( 'This is the sum of the "Tax rows" shipping tax amount within your orders.', 'woocommerce' ) ); ?></th>
|
||||
<th class="total_row"><?php _e( 'Total tax', 'woocommerce' ); ?> <?php echo wc_help_tip( __( 'This is the total tax for the rate (shipping tax + product tax).', 'woocommerce' ) ); ?></th>
|
||||
<th><?php esc_html_e( 'Tax', 'woocommerce' ); ?></th>
|
||||
<th><?php esc_html_e( 'Rate', 'woocommerce' ); ?></th>
|
||||
<th class="total_row"><?php esc_html_e( 'Number of orders', 'woocommerce' ); ?></th>
|
||||
<th class="total_row"><?php esc_html_e( 'Tax amount', 'woocommerce' ); ?> <?php echo wc_help_tip( __( 'This is the sum of the "Tax rows" tax amount within your orders.', 'woocommerce' ) ); ?></th>
|
||||
<th class="total_row"><?php esc_html_e( 'Shipping tax amount', 'woocommerce' ); ?> <?php echo wc_help_tip( __( 'This is the sum of the "Tax rows" shipping tax amount within your orders.', 'woocommerce' ) ); ?></th>
|
||||
<th class="total_row"><?php esc_html_e( 'Total tax', 'woocommerce' ); ?> <?php echo wc_help_tip( __( 'This is the total tax for the rate (shipping tax + product tax).', 'woocommerce' ) ); ?></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<?php if ( ! empty( $tax_rows ) ) : ?>
|
||||
|
@ -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 ) );
|
||||
?>
|
||||
<tr>
|
||||
<th scope="row"><?php echo apply_filters( 'woocommerce_reports_taxes_tax_rate', $tax_row->tax_rate, $rate_id, $tax_row ); ?></th>
|
||||
<td><?php echo apply_filters( 'woocommerce_reports_taxes_rate', $rate, $rate_id, $tax_row ); ?>%</td>
|
||||
<td class="total_row"><?php echo $tax_row->total_orders; ?></td>
|
||||
<td class="total_row"><?php echo wc_price( $tax_row->tax_amount ); ?></td>
|
||||
<td class="total_row"><?php echo wc_price( $tax_row->shipping_tax_amount ); ?></td>
|
||||
<td class="total_row"><?php echo wc_price( $tax_row->tax_amount + $tax_row->shipping_tax_amount ); ?></td>
|
||||
<th scope="row"><?php echo wp_kses_post( apply_filters( 'woocommerce_reports_taxes_tax_rate', $tax_row->tax_rate, $rate_id, $tax_row ) ); ?></th>
|
||||
<td><?php echo wp_kses_post( apply_filters( 'woocommerce_reports_taxes_rate', $rate, $rate_id, $tax_row ) ); ?>%</td>
|
||||
<td class="total_row"><?php echo esc_html( $tax_row->total_orders ); ?></td>
|
||||
<td class="total_row"><?php echo wc_price( $tax_row->tax_amount ); // phpcs:ignore ?></td>
|
||||
<td class="total_row"><?php echo wc_price( $tax_row->shipping_tax_amount ); // phpcs:ignore ?></td>
|
||||
<td class="total_row"><?php echo wc_price( $tax_row->tax_amount + $tax_row->shipping_tax_amount ); // phpcs:ignore ?></td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
|
@ -181,16 +215,16 @@ class WC_Report_Taxes_By_Code extends WC_Admin_Report {
|
|||
</tbody>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<th scope="row" colspan="3"><?php _e( 'Total', 'woocommerce' ); ?></th>
|
||||
<th class="total_row"><?php echo wc_price( wc_round_tax_total( array_sum( wp_list_pluck( (array) $tax_rows, 'tax_amount' ) ) ) ); ?></th>
|
||||
<th class="total_row"><?php echo wc_price( wc_round_tax_total( array_sum( wp_list_pluck( (array) $tax_rows, 'shipping_tax_amount' ) ) ) ); ?></th>
|
||||
<th class="total_row"><strong><?php echo wc_price( wc_round_tax_total( array_sum( wp_list_pluck( (array) $tax_rows, 'tax_amount' ) ) + array_sum( wp_list_pluck( (array) $tax_rows, 'shipping_tax_amount' ) ) ) ); ?></strong></th>
|
||||
<th scope="row" colspan="3"><?php esc_html_e( 'Total', 'woocommerce' ); ?></th>
|
||||
<th class="total_row"><?php echo wc_price( wc_round_tax_total( array_sum( wp_list_pluck( (array) $tax_rows, 'tax_amount' ) ) ) ); // phpcs:ignore ?></th>
|
||||
<th class="total_row"><?php echo wc_price( wc_round_tax_total( array_sum( wp_list_pluck( (array) $tax_rows, 'shipping_tax_amount' ) ) ) ); // phpcs:ignore ?></th>
|
||||
<th class="total_row"><strong><?php echo wc_price( wc_round_tax_total( array_sum( wp_list_pluck( (array) $tax_rows, 'tax_amount' ) ) + array_sum( wp_list_pluck( (array) $tax_rows, 'shipping_tax_amount' ) ) ) ); // phpcs:ignore ?></strong></th>
|
||||
</tr>
|
||||
</tfoot>
|
||||
<?php else : ?>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><?php _e( 'No taxes found in this period', 'woocommerce' ); ?></td>
|
||||
<td><?php esc_html_e( 'No taxes found in this period', 'woocommerce' ); ?></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
<?php endif; ?>
|
||||
|
|
Loading…
Reference in New Issue