class-wc-admin-report.php
1 year ago
class-wc-report-coupon-usage.php
5 years ago
class-wc-report-customer-list.php
9 months ago
class-wc-report-customers.php
9 months ago
class-wc-report-downloads.php
5 years ago
class-wc-report-low-in-stock.php
5 years ago
class-wc-report-most-stocked.php
5 years ago
class-wc-report-out-of-stock.php
5 years ago
class-wc-report-sales-by-category.php
2 years ago
class-wc-report-sales-by-date.php
1 year ago
class-wc-report-sales-by-product.php
3 months ago
class-wc-report-stock.php
1 year ago
class-wc-report-taxes-by-code.php
1 year ago
class-wc-report-taxes-by-date.php
1 year ago
class-wc-report-taxes-by-code.php
243 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Taxes by tax code report. |
| 4 | * |
| 5 | * @package WooCommerce\Admin\Reports |
| 6 | */ |
| 7 | |
| 8 | use Automattic\WooCommerce\Enums\OrderStatus; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; // Exit if accessed directly. |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * WC_Report_Taxes_By_Code |
| 16 | * |
| 17 | * @package WooCommerce\Admin\Reports |
| 18 | * @version 2.1.0 |
| 19 | */ |
| 20 | class WC_Report_Taxes_By_Code extends WC_Admin_Report { |
| 21 | |
| 22 | /** |
| 23 | * Get the legend for the main chart sidebar. |
| 24 | * |
| 25 | * @return array |
| 26 | */ |
| 27 | public function get_chart_legend() { |
| 28 | return array(); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Output an export link. |
| 33 | */ |
| 34 | public function get_export_button() { |
| 35 | |
| 36 | $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( wp_unslash( $_GET['range'] ) ) : 'last_month'; |
| 37 | ?> |
| 38 | <a |
| 39 | href="#" |
| 40 | download="report-<?php echo esc_attr( $current_range ); ?>-<?php echo esc_attr( date_i18n( 'Y-m-d', current_time( 'timestamp' ) ) ); ?>.csv" |
| 41 | class="export_csv" |
| 42 | data-export="table" |
| 43 | > |
| 44 | <?php esc_html_e( 'Export CSV', 'woocommerce' ); ?> |
| 45 | </a> |
| 46 | <?php |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Output the report. |
| 51 | */ |
| 52 | public function output_report() { |
| 53 | |
| 54 | $ranges = array( |
| 55 | 'year' => __( 'Year', 'woocommerce' ), |
| 56 | 'last_month' => __( 'Last month', 'woocommerce' ), |
| 57 | 'month' => __( 'This month', 'woocommerce' ), |
| 58 | ); |
| 59 | |
| 60 | $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( wp_unslash( $_GET['range'] ) ) : 'last_month'; |
| 61 | |
| 62 | if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', 'month', '7day' ) ) ) { |
| 63 | $current_range = 'last_month'; |
| 64 | } |
| 65 | |
| 66 | $this->check_current_range_nonce( $current_range ); |
| 67 | $this->calculate_current_range( $current_range ); |
| 68 | |
| 69 | $hide_sidebar = true; |
| 70 | |
| 71 | include WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php'; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get the main chart. |
| 76 | */ |
| 77 | public function get_main_chart() { |
| 78 | global $wpdb; |
| 79 | |
| 80 | $query_data = array( |
| 81 | 'order_item_name' => array( |
| 82 | 'type' => 'order_item', |
| 83 | 'function' => '', |
| 84 | 'name' => 'tax_rate', |
| 85 | ), |
| 86 | 'tax_amount' => array( |
| 87 | 'type' => 'order_item_meta', |
| 88 | 'order_item_type' => 'tax', |
| 89 | 'function' => '', |
| 90 | 'name' => 'tax_amount', |
| 91 | ), |
| 92 | 'shipping_tax_amount' => array( |
| 93 | 'type' => 'order_item_meta', |
| 94 | 'order_item_type' => 'tax', |
| 95 | 'function' => '', |
| 96 | 'name' => 'shipping_tax_amount', |
| 97 | ), |
| 98 | 'rate_id' => array( |
| 99 | 'type' => 'order_item_meta', |
| 100 | 'order_item_type' => 'tax', |
| 101 | 'function' => '', |
| 102 | 'name' => 'rate_id', |
| 103 | ), |
| 104 | 'ID' => array( |
| 105 | 'type' => 'post_data', |
| 106 | 'function' => '', |
| 107 | 'name' => 'post_id', |
| 108 | ), |
| 109 | ); |
| 110 | |
| 111 | $query_where = array( |
| 112 | array( |
| 113 | 'key' => 'order_item_type', |
| 114 | 'value' => 'tax', |
| 115 | 'operator' => '=', |
| 116 | ), |
| 117 | array( |
| 118 | 'key' => 'order_item_name', |
| 119 | 'value' => '', |
| 120 | 'operator' => '!=', |
| 121 | ), |
| 122 | ); |
| 123 | |
| 124 | // We exclude on-hold orders as they are still pending payment. |
| 125 | $tax_rows_orders = $this->get_order_report_data( |
| 126 | array( |
| 127 | 'data' => $query_data, |
| 128 | 'where' => $query_where, |
| 129 | 'order_by' => 'posts.post_date ASC', |
| 130 | 'query_type' => 'get_results', |
| 131 | 'filter_range' => true, |
| 132 | 'order_types' => wc_get_order_types( 'sales-reports' ), |
| 133 | 'order_status' => array( OrderStatus::COMPLETED, OrderStatus::PROCESSING, OrderStatus::REFUNDED ), |
| 134 | ) |
| 135 | ); |
| 136 | |
| 137 | $tax_rows_partial_refunds = $this->get_order_report_data( |
| 138 | array( |
| 139 | 'data' => $query_data, |
| 140 | 'where' => $query_where, |
| 141 | 'order_by' => 'posts.post_date ASC', |
| 142 | 'query_type' => 'get_results', |
| 143 | 'filter_range' => true, |
| 144 | 'order_types' => array( 'shop_order_refund' ), |
| 145 | 'parent_order_status' => array( OrderStatus::COMPLETED, OrderStatus::PROCESSING ), // Partial refunds inside refunded orders should be ignored. |
| 146 | ) |
| 147 | ); |
| 148 | |
| 149 | $tax_rows_full_refunds = $this->get_order_report_data( |
| 150 | array( |
| 151 | 'data' => $query_data, |
| 152 | 'where' => $query_where, |
| 153 | 'order_by' => 'posts.post_date ASC', |
| 154 | 'query_type' => 'get_results', |
| 155 | 'filter_range' => true, |
| 156 | 'order_types' => array( 'shop_order_refund' ), |
| 157 | 'parent_order_status' => array( OrderStatus::REFUNDED ), |
| 158 | ) |
| 159 | ); |
| 160 | |
| 161 | // Merge. |
| 162 | $tax_rows = array(); |
| 163 | // Initialize an associative array to store unique post_ids. |
| 164 | $unique_post_ids = array(); |
| 165 | |
| 166 | foreach ( $tax_rows_orders + $tax_rows_partial_refunds as $tax_row ) { |
| 167 | $key = $tax_row->tax_rate; |
| 168 | $tax_rows[ $key ] = isset( $tax_rows[ $key ] ) ? $tax_rows[ $key ] : (object) array( |
| 169 | 'tax_amount' => 0, |
| 170 | 'shipping_tax_amount' => 0, |
| 171 | 'total_orders' => 0, |
| 172 | ); |
| 173 | $tax_rows[ $key ]->tax_rate = $tax_row->tax_rate; |
| 174 | $tax_rows[ $key ]->tax_amount += wc_round_tax_total( $tax_row->tax_amount ); |
| 175 | $tax_rows[ $key ]->shipping_tax_amount += wc_round_tax_total( $tax_row->shipping_tax_amount ); |
| 176 | if ( ! isset( $unique_post_ids[ $key ] ) || ! in_array( $tax_row->post_id, $unique_post_ids[ $key ], true ) ) { |
| 177 | $unique_post_ids[ $key ] = isset( $unique_post_ids[ $key ] ) ? $unique_post_ids[ $key ] : array(); |
| 178 | $unique_post_ids[ $key ][] = $tax_row->post_id; |
| 179 | $tax_rows[ $key ]->total_orders += 1; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | foreach ( $tax_rows_full_refunds as $tax_row ) { |
| 184 | $key = $tax_row->tax_rate; |
| 185 | $tax_rows[ $key ] = isset( $tax_rows[ $key ] ) ? $tax_rows[ $key ] : (object) array( |
| 186 | 'tax_amount' => 0, |
| 187 | 'shipping_tax_amount' => 0, |
| 188 | 'total_orders' => 0, |
| 189 | ); |
| 190 | $tax_rows[ $key ]->tax_rate = $tax_row->tax_rate; |
| 191 | $tax_rows[ $key ]->tax_amount += wc_round_tax_total( $tax_row->tax_amount ); |
| 192 | $tax_rows[ $key ]->shipping_tax_amount += wc_round_tax_total( $tax_row->shipping_tax_amount ); |
| 193 | } |
| 194 | ?> |
| 195 | <table class="widefat"> |
| 196 | <thead> |
| 197 | <tr> |
| 198 | <th><?php esc_html_e( 'Tax', 'woocommerce' ); ?></th> |
| 199 | <th><?php esc_html_e( 'Rate', 'woocommerce' ); ?></th> |
| 200 | <th class="total_row"><?php esc_html_e( 'Number of orders', 'woocommerce' ); ?></th> |
| 201 | <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> |
| 202 | <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> |
| 203 | <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> |
| 204 | </tr> |
| 205 | </thead> |
| 206 | <?php if ( ! empty( $tax_rows ) ) : ?> |
| 207 | <tbody> |
| 208 | <?php |
| 209 | foreach ( $tax_rows as $rate_id => $tax_row ) { |
| 210 | $rate = $wpdb->get_var( $wpdb->prepare( "SELECT tax_rate FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %d;", $rate_id ) ); |
| 211 | ?> |
| 212 | <tr> |
| 213 | <th scope="row"><?php echo wp_kses_post( apply_filters( 'woocommerce_reports_taxes_tax_rate', $tax_row->tax_rate, $rate_id, $tax_row ) ); ?></th> |
| 214 | <td><?php echo wp_kses_post( apply_filters( 'woocommerce_reports_taxes_rate', $rate, $rate_id, $tax_row ) ); ?>%</td> |
| 215 | <td class="total_row"><?php echo esc_html( $tax_row->total_orders ); ?></td> |
| 216 | <td class="total_row"><?php echo wc_price( $tax_row->tax_amount ); // phpcs:ignore ?></td> |
| 217 | <td class="total_row"><?php echo wc_price( $tax_row->shipping_tax_amount ); // phpcs:ignore ?></td> |
| 218 | <td class="total_row"><?php echo wc_price( $tax_row->tax_amount + $tax_row->shipping_tax_amount ); // phpcs:ignore ?></td> |
| 219 | </tr> |
| 220 | <?php |
| 221 | } |
| 222 | ?> |
| 223 | </tbody> |
| 224 | <tfoot> |
| 225 | <tr> |
| 226 | <th scope="row" colspan="3"><?php esc_html_e( 'Total', 'woocommerce' ); ?></th> |
| 227 | <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> |
| 228 | <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> |
| 229 | <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> |
| 230 | </tr> |
| 231 | </tfoot> |
| 232 | <?php else : ?> |
| 233 | <tbody> |
| 234 | <tr> |
| 235 | <td><?php esc_html_e( 'No taxes found in this period', 'woocommerce' ); ?></td> |
| 236 | </tr> |
| 237 | </tbody> |
| 238 | <?php endif; ?> |
| 239 | </table> |
| 240 | <?php |
| 241 | } |
| 242 | } |
| 243 |