class-wc-report-taxes-by-code.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. /**
  6. * WC_Report_Taxes_By_Code
  7. *
  8. * @author WooThemes
  9. * @category Admin
  10. * @package WooCommerce/Admin/Reports
  11. * @version 2.1.0
  12. */
  13. class WC_Report_Taxes_By_Code extends WC_Admin_Report {
  14. /**
  15. * Get the legend for the main chart sidebar.
  16. *
  17. * @return array
  18. */
  19. public function get_chart_legend() {
  20. return array();
  21. }
  22. /**
  23. * Output an export link.
  24. */
  25. public function get_export_button() {
  26. $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : 'last_month';
  27. ?>
  28. <a
  29. href="#"
  30. download="report-<?php echo esc_attr( $current_range ); ?>-<?php echo date_i18n( 'Y-m-d', current_time( 'timestamp' ) ); ?>.csv"
  31. class="export_csv"
  32. data-export="table"
  33. >
  34. <?php _e( 'Export CSV', 'woocommerce' ); ?>
  35. </a>
  36. <?php
  37. }
  38. /**
  39. * Output the report.
  40. */
  41. public function output_report() {
  42. $ranges = array(
  43. 'year' => __( 'Year', 'woocommerce' ),
  44. 'last_month' => __( 'Last month', 'woocommerce' ),
  45. 'month' => __( 'This month', 'woocommerce' ),
  46. );
  47. $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : 'last_month';
  48. if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', 'month', '7day' ) ) ) {
  49. $current_range = 'last_month';
  50. }
  51. $this->check_current_range_nonce( $current_range );
  52. $this->calculate_current_range( $current_range );
  53. $hide_sidebar = true;
  54. include WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php';
  55. }
  56. /**
  57. * Get the main chart.
  58. */
  59. public function get_main_chart() {
  60. global $wpdb;
  61. $query_data = array(
  62. 'order_item_name' => array(
  63. 'type' => 'order_item',
  64. 'function' => '',
  65. 'name' => 'tax_rate',
  66. ),
  67. 'tax_amount' => array(
  68. 'type' => 'order_item_meta',
  69. 'order_item_type' => 'tax',
  70. 'function' => '',
  71. 'name' => 'tax_amount',
  72. ),
  73. 'shipping_tax_amount' => array(
  74. 'type' => 'order_item_meta',
  75. 'order_item_type' => 'tax',
  76. 'function' => '',
  77. 'name' => 'shipping_tax_amount',
  78. ),
  79. 'rate_id' => array(
  80. 'type' => 'order_item_meta',
  81. 'order_item_type' => 'tax',
  82. 'function' => '',
  83. 'name' => 'rate_id',
  84. ),
  85. 'ID' => array(
  86. 'type' => 'post_data',
  87. 'function' => '',
  88. 'name' => 'post_id',
  89. ),
  90. );
  91. $query_where = array(
  92. array(
  93. 'key' => 'order_item_type',
  94. 'value' => 'tax',
  95. 'operator' => '=',
  96. ),
  97. array(
  98. 'key' => 'order_item_name',
  99. 'value' => '',
  100. 'operator' => '!=',
  101. ),
  102. );
  103. $tax_rows_orders = $this->get_order_report_data(
  104. array(
  105. 'data' => $query_data,
  106. 'where' => $query_where,
  107. 'order_by' => 'posts.post_date ASC',
  108. 'query_type' => 'get_results',
  109. 'filter_range' => true,
  110. 'order_types' => array_merge( wc_get_order_types( 'sales-reports' ), array( 'shop_order_refund' ) ),
  111. 'order_status' => array( 'completed', 'processing', 'on-hold' ),
  112. 'parent_order_status' => array( 'completed', 'processing', 'on-hold' ), // Partial refunds inside refunded orders should be ignored
  113. )
  114. );
  115. // Merge
  116. $tax_rows = array();
  117. foreach ( $tax_rows_orders as $tax_row ) {
  118. $key = $tax_row->rate_id;
  119. $tax_rows[ $key ] = isset( $tax_rows[ $key ] ) ? $tax_rows[ $key ] : (object) array(
  120. 'tax_amount' => 0,
  121. 'shipping_tax_amount' => 0,
  122. 'total_orders' => 0,
  123. );
  124. if ( 'shop_order_refund' !== get_post_type( $tax_row->post_id ) ) {
  125. $tax_rows[ $key ]->total_orders += 1;
  126. }
  127. $tax_rows[ $key ]->tax_rate = $tax_row->tax_rate;
  128. $tax_rows[ $key ]->tax_amount += wc_round_tax_total( $tax_row->tax_amount );
  129. $tax_rows[ $key ]->shipping_tax_amount += wc_round_tax_total( $tax_row->shipping_tax_amount );
  130. }
  131. ?>
  132. <table class="widefat">
  133. <thead>
  134. <tr>
  135. <th><?php _e( 'Tax', 'woocommerce' ); ?></th>
  136. <th><?php _e( 'Rate', 'woocommerce' ); ?></th>
  137. <th class="total_row"><?php _e( 'Number of orders', 'woocommerce' ); ?></th>
  138. <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>
  139. <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>
  140. <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>
  141. </tr>
  142. </thead>
  143. <?php if ( ! empty( $tax_rows ) ) : ?>
  144. <tbody>
  145. <?php
  146. foreach ( $tax_rows as $rate_id => $tax_row ) {
  147. $rate = $wpdb->get_var( $wpdb->prepare( "SELECT tax_rate FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %d;", $rate_id ) );
  148. ?>
  149. <tr>
  150. <th scope="row"><?php echo apply_filters( 'woocommerce_reports_taxes_tax_rate', $tax_row->tax_rate, $rate_id, $tax_row ); ?></th>
  151. <td><?php echo apply_filters( 'woocommerce_reports_taxes_rate', $rate, $rate_id, $tax_row ); ?>%</td>
  152. <td class="total_row"><?php echo $tax_row->total_orders; ?></td>
  153. <td class="total_row"><?php echo wc_price( $tax_row->tax_amount ); ?></td>
  154. <td class="total_row"><?php echo wc_price( $tax_row->shipping_tax_amount ); ?></td>
  155. <td class="total_row"><?php echo wc_price( $tax_row->tax_amount + $tax_row->shipping_tax_amount ); ?></td>
  156. </tr>
  157. <?php
  158. }
  159. ?>
  160. </tbody>
  161. <tfoot>
  162. <tr>
  163. <th scope="row" colspan="3"><?php _e( 'Total', 'woocommerce' ); ?></th>
  164. <th class="total_row"><?php echo wc_price( wc_round_tax_total( array_sum( wp_list_pluck( (array) $tax_rows, 'tax_amount' ) ) ) ); ?></th>
  165. <th class="total_row"><?php echo wc_price( wc_round_tax_total( array_sum( wp_list_pluck( (array) $tax_rows, 'shipping_tax_amount' ) ) ) ); ?></th>
  166. <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>
  167. </tr>
  168. </tfoot>
  169. <?php else : ?>
  170. <tbody>
  171. <tr>
  172. <td><?php _e( 'No taxes found in this period', 'woocommerce' ); ?></td>
  173. </tr>
  174. </tbody>
  175. <?php endif; ?>
  176. </table>
  177. <?php
  178. }
  179. }