PageRenderTime 32ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/admin/reports/class-wc-report-taxes-by-date.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 264 lines | 219 code | 22 blank | 23 comment | 5 complexity | 5c4cefcbb64adc8b075331952c5756c9 MD5 | raw file
  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. // We exlude on-hold orders are they are still pending payment.
  94. $tax_rows_orders = $this->get_order_report_data(
  95. array(
  96. 'data' => $query_data,
  97. 'group_by' => $this->group_by_query,
  98. 'order_by' => 'post_date ASC',
  99. 'query_type' => 'get_results',
  100. 'filter_range' => true,
  101. 'order_types' => wc_get_order_types( 'sales-reports' ),
  102. 'order_status' => array( 'completed', 'processing', 'refunded' ),
  103. )
  104. );
  105. $tax_rows_full_refunds = $this->get_order_report_data(
  106. array(
  107. 'data' => array(
  108. 'ID' => array(
  109. 'type' => 'post_data',
  110. 'distinct' => true,
  111. 'function' => '',
  112. 'name' => 'ID',
  113. ),
  114. 'post_parent' => array(
  115. 'type' => 'post_data',
  116. 'function' => '',
  117. 'name' => 'post_parent',
  118. ),
  119. 'post_date' => array(
  120. 'type' => 'post_data',
  121. 'function' => '',
  122. 'name' => 'post_date',
  123. ),
  124. ),
  125. 'query_type' => 'get_results',
  126. 'filter_range' => true,
  127. 'order_types' => array( 'shop_order_refund' ),
  128. 'parent_order_status' => array( 'refunded' ),
  129. )
  130. );
  131. $tax_rows_partial_refunds = $this->get_order_report_data(
  132. array(
  133. 'data' => $query_data,
  134. 'group_by' => $this->group_by_query,
  135. 'order_by' => 'post_date ASC',
  136. 'query_type' => 'get_results',
  137. 'filter_range' => true,
  138. 'order_types' => array( 'shop_order_refund' ),
  139. 'parent_order_status' => array( 'completed', 'processing' ), // Partial refunds inside refunded orders should be ignored.
  140. )
  141. );
  142. $tax_rows = array();
  143. foreach ( $tax_rows_orders + $tax_rows_partial_refunds as $tax_row ) {
  144. $key = date( ( 'month' === $this->chart_groupby ) ? 'Ym' : 'Ymd', strtotime( $tax_row->post_date ) );
  145. $tax_rows[ $key ] = isset( $tax_rows[ $key ] ) ? $tax_rows[ $key ] : (object) array(
  146. 'tax_amount' => 0,
  147. 'shipping_tax_amount' => 0,
  148. 'total_sales' => 0,
  149. 'total_shipping' => 0,
  150. 'total_orders' => 0,
  151. );
  152. }
  153. foreach ( $tax_rows_orders as $tax_row ) {
  154. $key = date( ( 'month' === $this->chart_groupby ) ? 'Ym' : 'Ymd', strtotime( $tax_row->post_date ) );
  155. $tax_rows[ $key ]->total_orders += $tax_row->total_orders;
  156. $tax_rows[ $key ]->tax_amount += $tax_row->tax_amount;
  157. $tax_rows[ $key ]->shipping_tax_amount += $tax_row->shipping_tax_amount;
  158. $tax_rows[ $key ]->total_sales += $tax_row->total_sales;
  159. $tax_rows[ $key ]->total_shipping += $tax_row->total_shipping;
  160. }
  161. foreach ( $tax_rows_partial_refunds as $tax_row ) {
  162. $key = date( ( 'month' === $this->chart_groupby ) ? 'Ym' : 'Ymd', strtotime( $tax_row->post_date ) );
  163. $tax_rows[ $key ]->tax_amount += $tax_row->tax_amount;
  164. $tax_rows[ $key ]->shipping_tax_amount += $tax_row->shipping_tax_amount;
  165. $tax_rows[ $key ]->total_sales += $tax_row->total_sales;
  166. $tax_rows[ $key ]->total_shipping += $tax_row->total_shipping;
  167. }
  168. foreach ( $tax_rows_full_refunds as $tax_row ) {
  169. $key = date( ( 'month' === $this->chart_groupby ) ? 'Ym' : 'Ymd', strtotime( $tax_row->post_date ) );
  170. $tax_rows[ $key ] = isset( $tax_rows[ $key ] ) ? $tax_rows[ $key ] : (object) array(
  171. 'tax_amount' => 0,
  172. 'shipping_tax_amount' => 0,
  173. 'total_sales' => 0,
  174. 'total_shipping' => 0,
  175. 'total_orders' => 0,
  176. );
  177. $parent_order = wc_get_order( $tax_row->post_parent );
  178. if ( $parent_order ) {
  179. $tax_rows[ $key ]->tax_amount += $parent_order->get_cart_tax() * -1;
  180. $tax_rows[ $key ]->shipping_tax_amount += $parent_order->get_shipping_tax() * -1;
  181. $tax_rows[ $key ]->total_sales += $parent_order->get_total() * -1;
  182. $tax_rows[ $key ]->total_shipping += $parent_order->get_shipping_total() * -1;
  183. }
  184. }
  185. ?>
  186. <table class="widefat">
  187. <thead>
  188. <tr>
  189. <th><?php _e( 'Period', 'woocommerce' ); ?></th>
  190. <th class="total_row"><?php _e( 'Number of orders', 'woocommerce' ); ?></th>
  191. <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>
  192. <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>
  193. <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>
  194. <th class="total_row"><?php _e( 'Net profit', 'woocommerce' ); ?> <?php echo wc_help_tip( __( 'Total sales minus shipping and tax.', 'woocommerce' ) ); ?></th>
  195. </tr>
  196. </thead>
  197. <?php if ( ! empty( $tax_rows ) ) : ?>
  198. <tbody>
  199. <?php
  200. foreach ( $tax_rows as $date => $tax_row ) {
  201. $gross = $tax_row->total_sales - $tax_row->total_shipping;
  202. $total_tax = $tax_row->tax_amount + $tax_row->shipping_tax_amount;
  203. ?>
  204. <tr>
  205. <th scope="row">
  206. <?php echo ( 'month' === $this->chart_groupby ) ? date_i18n( 'F', strtotime( $date . '01' ) ) : date_i18n( get_option( 'date_format' ), strtotime( $date ) ); ?>
  207. </th>
  208. <td class="total_row"><?php echo $tax_row->total_orders; ?></td>
  209. <td class="total_row"><?php echo wc_price( $gross ); ?></td>
  210. <td class="total_row"><?php echo wc_price( $tax_row->total_shipping ); ?></td>
  211. <td class="total_row"><?php echo wc_price( $total_tax ); ?></td>
  212. <td class="total_row"><?php echo wc_price( $gross - $total_tax ); ?></td>
  213. </tr>
  214. <?php
  215. }
  216. ?>
  217. </tbody>
  218. <tfoot>
  219. <?php
  220. $gross = array_sum( wp_list_pluck( (array) $tax_rows, 'total_sales' ) ) - array_sum( wp_list_pluck( (array) $tax_rows, 'total_shipping' ) );
  221. $total_tax = array_sum( wp_list_pluck( (array) $tax_rows, 'tax_amount' ) ) + array_sum( wp_list_pluck( (array) $tax_rows, 'shipping_tax_amount' ) );
  222. ?>
  223. <tr>
  224. <th scope="row"><?php _e( 'Totals', 'woocommerce' ); ?></th>
  225. <th class="total_row"><?php echo array_sum( wp_list_pluck( (array) $tax_rows, 'total_orders' ) ); ?></th>
  226. <th class="total_row"><?php echo wc_price( $gross ); ?></th>
  227. <th class="total_row"><?php echo wc_price( array_sum( wp_list_pluck( (array) $tax_rows, 'total_shipping' ) ) ); ?></th>
  228. <th class="total_row"><?php echo wc_price( $total_tax ); ?></th>
  229. <th class="total_row"><?php echo wc_price( $gross - $total_tax ); ?></th>
  230. </tr>
  231. </tfoot>
  232. <?php else : ?>
  233. <tbody>
  234. <tr>
  235. <td><?php _e( 'No taxes found in this period', 'woocommerce' ); ?></td>
  236. </tr>
  237. </tbody>
  238. <?php endif; ?>
  239. </table>
  240. <?php
  241. }
  242. }