/includes/admin/reports/class-wc-report-taxes-by-code.php

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