class-wc-report-taxes-by-date.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. /**
  6. * WC_Report_Taxes_By_Date
  7. *
  8. * @author WooThemes
  9. * @category Admin
  10. * @package WooCommerce/Admin/Reports
  11. * @version 2.1.0
  12. */
  13. class WC_Report_Taxes_By_Date 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. $query_data = array(
  61. '_order_tax' => array(
  62. 'type' => 'meta',
  63. 'function' => 'SUM',
  64. 'name' => 'tax_amount',
  65. ),
  66. '_order_shipping_tax' => array(
  67. 'type' => 'meta',
  68. 'function' => 'SUM',
  69. 'name' => 'shipping_tax_amount',
  70. ),
  71. '_order_total' => array(
  72. 'type' => 'meta',
  73. 'function' => 'SUM',
  74. 'name' => 'total_sales',
  75. ),
  76. '_order_shipping' => array(
  77. 'type' => 'meta',
  78. 'function' => 'SUM',
  79. 'name' => 'total_shipping',
  80. ),
  81. 'ID' => array(
  82. 'type' => 'post_data',
  83. 'function' => 'COUNT',
  84. 'name' => 'total_orders',
  85. 'distinct' => true,
  86. ),
  87. 'post_date' => array(
  88. 'type' => 'post_data',
  89. 'function' => '',
  90. 'name' => 'post_date',
  91. ),
  92. );
  93. $tax_rows_orders = $this->get_order_report_data(
  94. array(
  95. 'data' => $query_data,
  96. 'group_by' => $this->group_by_query,
  97. 'order_by' => 'post_date ASC',
  98. 'query_type' => 'get_results',
  99. 'filter_range' => true,
  100. 'order_types' => wc_get_order_types( 'sales-reports' ),
  101. 'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
  102. )
  103. );
  104. $tax_rows_full_refunds = $this->get_order_report_data(
  105. array(
  106. 'data' => array(
  107. 'ID' => array(
  108. 'type' => 'post_data',
  109. 'distinct' => true,
  110. 'function' => '',
  111. 'name' => 'ID',
  112. ),
  113. 'post_parent' => array(
  114. 'type' => 'post_data',
  115. 'function' => '',
  116. 'name' => 'post_parent',
  117. ),
  118. 'post_date' => array(
  119. 'type' => 'post_data',
  120. 'function' => '',
  121. 'name' => 'post_date',
  122. ),
  123. ),
  124. 'query_type' => 'get_results',
  125. 'filter_range' => true,
  126. 'order_types' => array( 'shop_order_refund' ),
  127. 'parent_order_status' => array( 'refunded' ),
  128. )
  129. );
  130. $tax_rows_partial_refunds = $this->get_order_report_data(
  131. array(
  132. 'data' => $query_data,
  133. 'group_by' => $this->group_by_query,
  134. 'order_by' => 'post_date ASC',
  135. 'query_type' => 'get_results',
  136. 'filter_range' => true,
  137. 'order_types' => array( 'shop_order_refund' ),
  138. 'parent_order_status' => array( 'completed', 'processing', 'on-hold' ), // Partial refunds inside refunded orders should be ignored.
  139. )
  140. );
  141. $tax_rows = array();
  142. foreach ( $tax_rows_orders + $tax_rows_partial_refunds as $tax_row ) {
  143. $key = date( ( 'month' === $this->chart_groupby ) ? 'Ym' : 'Ymd', strtotime( $tax_row->post_date ) );
  144. $tax_rows[ $key ] = isset( $tax_rows[ $key ] ) ? $tax_rows[ $key ] : (object) array(
  145. 'tax_amount' => 0,
  146. 'shipping_tax_amount' => 0,
  147. 'total_sales' => 0,
  148. 'total_shipping' => 0,
  149. 'total_orders' => 0,
  150. );
  151. }
  152. foreach ( $tax_rows_orders as $tax_row ) {
  153. $key = date( ( 'month' === $this->chart_groupby ) ? 'Ym' : 'Ymd', strtotime( $tax_row->post_date ) );
  154. $tax_rows[ $key ]->total_orders += $tax_row->total_orders;
  155. $tax_rows[ $key ]->tax_amount += $tax_row->tax_amount;
  156. $tax_rows[ $key ]->shipping_tax_amount += $tax_row->shipping_tax_amount;
  157. $tax_rows[ $key ]->total_sales += $tax_row->total_sales;
  158. $tax_rows[ $key ]->total_shipping += $tax_row->total_shipping;
  159. }
  160. foreach ( $tax_rows_partial_refunds as $tax_row ) {
  161. $key = date( ( 'month' === $this->chart_groupby ) ? 'Ym' : 'Ymd', strtotime( $tax_row->post_date ) );
  162. $tax_rows[ $key ]->tax_amount += $tax_row->tax_amount;
  163. $tax_rows[ $key ]->shipping_tax_amount += $tax_row->shipping_tax_amount;
  164. $tax_rows[ $key ]->total_sales += $tax_row->total_sales;
  165. $tax_rows[ $key ]->total_shipping += $tax_row->total_shipping;
  166. }
  167. foreach ( $tax_rows_full_refunds as $tax_row ) {
  168. $key = date( ( 'month' === $this->chart_groupby ) ? 'Ym' : 'Ymd', strtotime( $tax_row->post_date ) );
  169. $tax_rows[ $key ] = isset( $tax_rows[ $key ] ) ? $tax_rows[ $key ] : (object) array(
  170. 'tax_amount' => 0,
  171. 'shipping_tax_amount' => 0,
  172. 'total_sales' => 0,
  173. 'total_shipping' => 0,
  174. 'total_orders' => 0,
  175. );
  176. $parent_order = wc_get_order( $tax_row->post_parent );
  177. if ( $parent_order ) {
  178. $tax_rows[ $key ]->tax_amount += $parent_order->get_cart_tax() * -1;
  179. $tax_rows[ $key ]->shipping_tax_amount += $parent_order->get_shipping_tax() * -1;
  180. $tax_rows[ $key ]->total_sales += $parent_order->get_total() * -1;
  181. $tax_rows[ $key ]->total_shipping += $parent_order->get_shipping_total() * -1;
  182. }
  183. }
  184. ?>
  185. <table class="widefat">
  186. <thead>
  187. <tr>
  188. <th><?php _e( 'Period', 'woocommerce' ); ?></th>
  189. <th class="total_row"><?php _e( 'Number of orders', 'woocommerce' ); ?></th>
  190. <th class="total_row"><?php _e( 'Total sales', 'woocommerce' ); ?> <?php echo wc_help_tip( __( "This is the sum of the 'Order total' field within your orders.", 'woocommerce' ) ); ?></th>
  191. <th class="total_row"><?php _e( 'Total shipping', 'woocommerce' ); ?> <?php echo wc_help_tip( __( "This is the sum of the 'Shipping total' field within your orders.", 'woocommerce' ) ); ?></th>
  192. <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>
  193. <th class="total_row"><?php _e( 'Net profit', 'woocommerce' ); ?> <?php echo wc_help_tip( __( 'Total sales minus shipping and tax.', 'woocommerce' ) ); ?></th>
  194. </tr>
  195. </thead>
  196. <?php if ( ! empty( $tax_rows ) ) : ?>
  197. <tbody>
  198. <?php
  199. foreach ( $tax_rows as $date => $tax_row ) {
  200. $gross = $tax_row->total_sales - $tax_row->total_shipping;
  201. $total_tax = $tax_row->tax_amount + $tax_row->shipping_tax_amount;
  202. ?>
  203. <tr>
  204. <th scope="row">
  205. <?php echo ( 'month' === $this->chart_groupby ) ? date_i18n( 'F', strtotime( $date . '01' ) ) : date_i18n( get_option( 'date_format' ), strtotime( $date ) ); ?>
  206. </th>
  207. <td class="total_row"><?php echo $tax_row->total_orders; ?></td>
  208. <td class="total_row"><?php echo wc_price( $gross ); ?></td>
  209. <td class="total_row"><?php echo wc_price( $tax_row->total_shipping ); ?></td>
  210. <td class="total_row"><?php echo wc_price( $total_tax ); ?></td>
  211. <td class="total_row"><?php echo wc_price( $gross - $total_tax ); ?></td>
  212. </tr>
  213. <?php
  214. }
  215. ?>
  216. </tbody>
  217. <tfoot>
  218. <?php
  219. $gross = array_sum( wp_list_pluck( (array) $tax_rows, 'total_sales' ) ) - array_sum( wp_list_pluck( (array) $tax_rows, 'total_shipping' ) );
  220. $total_tax = array_sum( wp_list_pluck( (array) $tax_rows, 'tax_amount' ) ) + array_sum( wp_list_pluck( (array) $tax_rows, 'shipping_tax_amount' ) );
  221. ?>
  222. <tr>
  223. <th scope="row"><?php _e( 'Totals', 'woocommerce' ); ?></th>
  224. <th class="total_row"><?php echo array_sum( wp_list_pluck( (array) $tax_rows, 'total_orders' ) ); ?></th>
  225. <th class="total_row"><?php echo wc_price( $gross ); ?></th>
  226. <th class="total_row"><?php echo wc_price( array_sum( wp_list_pluck( (array) $tax_rows, 'total_shipping' ) ) ); ?></th>
  227. <th class="total_row"><?php echo wc_price( $total_tax ); ?></th>
  228. <th class="total_row"><?php echo wc_price( $gross - $total_tax ); ?></th>
  229. </tr>
  230. </tfoot>
  231. <?php else : ?>
  232. <tbody>
  233. <tr>
  234. <td><?php _e( 'No taxes found in this period', 'woocommerce' ); ?></td>
  235. </tr>
  236. </tbody>
  237. <?php endif; ?>
  238. </table>
  239. <?php
  240. }
  241. }