2013-07-10 11:05:45 +00:00
< ? php
2016-01-05 11:47:44 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ; // Exit if accessed directly
}
2013-07-10 11:05:45 +00:00
/**
2015-11-03 13:53:50 +00:00
* WC_Report_Taxes_By_Date
2014-02-14 13:02:37 +00:00
*
2014-08-31 07:19:13 +00:00
* @ author WooThemes
* @ category Admin
* @ package WooCommerce / Admin / Reports
2014-02-14 13:02:37 +00:00
* @ version 2.1 . 0
2013-07-10 11:05:45 +00:00
*/
class WC_Report_Taxes_By_Date extends WC_Admin_Report {
/**
2015-11-03 13:31:20 +00:00
* Get the legend for the main chart sidebar .
2013-07-10 11:05:45 +00:00
* @ return array
*/
public function get_chart_legend () {
return array ();
}
2013-07-18 11:56:12 +00:00
/**
2015-11-03 13:31:20 +00:00
* Output an export link .
2013-07-18 11:56:12 +00:00
*/
public function get_export_button () {
2014-08-31 07:19:13 +00:00
2014-09-16 09:33:47 +00:00
$current_range = ! empty ( $_GET [ 'range' ] ) ? sanitize_text_field ( $_GET [ 'range' ] ) : 'last_month' ;
2013-07-18 11:56:12 +00:00
?>
< a
href = " # "
2016-09-02 01:51:31 +00:00
download = " report-<?php echo esc_attr( $current_range ); ?>-<?php echo date_i18n( 'Y-m-d', current_time( 'timestamp' ) ); ?>.csv "
2013-07-18 11:56:12 +00:00
class = " export_csv "
data - export = " table "
>
< ? php _e ( 'Export CSV' , 'woocommerce' ); ?>
</ a >
< ? php
}
2013-07-10 11:05:45 +00:00
/**
2015-11-03 13:31:20 +00:00
* Output the report .
2013-07-10 11:05:45 +00:00
*/
public function output_report () {
2014-08-31 07:19:13 +00:00
2013-07-10 11:05:45 +00:00
$ranges = array (
'year' => __ ( 'Year' , 'woocommerce' ),
2016-10-12 10:16:30 +00:00
'last_month' => __ ( 'Last month' , 'woocommerce' ),
'month' => __ ( 'This month' , 'woocommerce' ),
2013-07-10 11:05:45 +00:00
);
2014-09-16 09:33:47 +00:00
$current_range = ! empty ( $_GET [ 'range' ] ) ? sanitize_text_field ( $_GET [ 'range' ] ) : 'last_month' ;
2013-07-10 11:05:45 +00:00
2014-08-31 07:19:13 +00:00
if ( ! in_array ( $current_range , array ( 'custom' , 'year' , 'last_month' , 'month' , '7day' ) ) ) {
2013-07-30 10:12:42 +00:00
$current_range = 'last_month' ;
2014-08-31 07:19:13 +00:00
}
2013-07-10 11:05:45 +00:00
2017-04-18 23:33:25 +00:00
$this -> check_current_range_nonce ( $current_range );
2013-07-30 10:12:42 +00:00
$this -> calculate_current_range ( $current_range );
2013-07-10 11:05:45 +00:00
$hide_sidebar = true ;
2016-09-02 01:33:57 +00:00
include ( WC () -> plugin_path () . '/includes/admin/views/html-report-by-date.php' );
2013-07-10 11:05:45 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Get the main chart .
2013-07-10 11:05:45 +00:00
*/
public function get_main_chart () {
2015-02-13 13:39:41 +00:00
$query_data = array (
'_order_tax' => array (
'type' => 'meta' ,
'function' => 'SUM' ,
2016-08-27 01:46:45 +00:00
'name' => 'tax_amount' ,
2015-02-13 13:39:41 +00:00
),
'_order_shipping_tax' => array (
'type' => 'meta' ,
'function' => 'SUM' ,
2016-08-27 01:46:45 +00:00
'name' => 'shipping_tax_amount' ,
2015-02-13 13:39:41 +00:00
),
'_order_total' => array (
'type' => 'meta' ,
'function' => 'SUM' ,
2016-08-27 01:46:45 +00:00
'name' => 'total_sales' ,
2015-02-13 13:39:41 +00:00
),
'_order_shipping' => array (
'type' => 'meta' ,
'function' => 'SUM' ,
2016-08-27 01:46:45 +00:00
'name' => 'total_shipping' ,
2013-07-10 11:05:45 +00:00
),
2015-02-13 13:39:41 +00:00
'ID' => array (
'type' => 'post_data' ,
'function' => 'COUNT' ,
'name' => 'total_orders' ,
'distinct' => true ,
),
'post_date' => array (
'type' => 'post_data' ,
'function' => '' ,
2016-08-27 01:46:45 +00:00
'name' => 'post_date' ,
2015-02-13 13:39:41 +00:00
),
);
$tax_rows_orders = $this -> get_order_report_data ( array (
'data' => $query_data ,
2013-07-10 11:05:45 +00:00
'group_by' => $this -> group_by_query ,
'order_by' => 'post_date ASC' ,
'query_type' => 'get_results' ,
2015-02-10 18:24:46 +00:00
'filter_range' => true ,
2015-02-13 13:39:41 +00:00
'order_types' => wc_get_order_types ( 'sales-reports' ),
2017-09-26 17:27:53 +00:00
'order_status' => array ( 'completed' , 'processing' , 'on-hold' , 'refunded' ),
) );
$tax_rows_full_refunds = $this -> get_order_report_data ( array (
'data' => array (
'ID' => array (
'type' => 'post_data' ,
'distinct' => true ,
'function' => '' ,
'name' => 'ID' ,
),
'post_parent' => array (
'type' => 'post_data' ,
'function' => '' ,
'name' => 'post_parent' ,
),
'post_date' => array (
'type' => 'post_data' ,
'function' => '' ,
'name' => 'post_date' ,
),
),
'query_type' => 'get_results' ,
'filter_range' => true ,
'order_types' => array ( 'shop_order_refund' ),
'parent_order_status' => array ( 'refunded' ),
2013-07-10 11:05:45 +00:00
) );
2015-02-13 13:39:41 +00:00
$tax_rows_partial_refunds = $this -> get_order_report_data ( array (
'data' => $query_data ,
'group_by' => $this -> group_by_query ,
'order_by' => 'post_date ASC' ,
'query_type' => 'get_results' ,
'filter_range' => true ,
'order_types' => array ( 'shop_order_refund' ),
2017-09-26 17:27:53 +00:00
'parent_order_status' => array ( 'completed' , 'processing' , 'on-hold' ), // Partial refunds inside refunded orders should be ignored.
2015-02-13 13:39:41 +00:00
) );
$tax_rows = array ();
2017-09-26 17:27:53 +00:00
foreach ( $tax_rows_orders + $tax_rows_partial_refunds as $tax_row ) {
2016-09-09 00:14:28 +00:00
$key = date ( ( 'month' === $this -> chart_groupby ) ? 'Ym' : 'Ymd' , strtotime ( $tax_row -> post_date ) );
2015-02-13 13:39:41 +00:00
$tax_rows [ $key ] = isset ( $tax_rows [ $key ] ) ? $tax_rows [ $key ] : ( object ) array ( 'tax_amount' => 0 , 'shipping_tax_amount' => 0 , 'total_sales' => 0 , 'total_shipping' => 0 , 'total_orders' => 0 );
$tax_rows [ $key ] -> tax_amount += $tax_row -> tax_amount ;
$tax_rows [ $key ] -> shipping_tax_amount += $tax_row -> shipping_tax_amount ;
$tax_rows [ $key ] -> total_sales += $tax_row -> total_sales ;
$tax_rows [ $key ] -> total_shipping += $tax_row -> total_shipping ;
}
2017-09-26 17:27:53 +00:00
foreach ( $tax_rows_orders as $tax_row ) {
$key = date ( ( 'month' === $this -> chart_groupby ) ? 'Ym' : 'Ymd' , strtotime ( $tax_row -> post_date ) );
$tax_rows [ $key ] -> total_orders += $tax_row -> total_orders ;
}
foreach ( $tax_rows_full_refunds as $tax_row ) {
$key = date ( ( 'month' === $this -> chart_groupby ) ? 'Ym' : 'Ymd' , strtotime ( $tax_row -> post_date ) );
$tax_rows [ $key ] = isset ( $tax_rows [ $key ] ) ? $tax_rows [ $key ] : ( object ) array ( 'tax_amount' => 0 , 'shipping_tax_amount' => 0 , 'total_sales' => 0 , 'total_shipping' => 0 , 'total_orders' => 0 );
$parent_order = wc_get_order ( $tax_row -> post_parent );
if ( $parent_order ) {
$tax_rows [ $key ] -> tax_amount += $parent_order -> get_cart_tax () * - 1 ;
$tax_rows [ $key ] -> shipping_tax_amount += $parent_order -> get_shipping_tax () * - 1 ;
$tax_rows [ $key ] -> total_sales += $parent_order -> get_total () * - 1 ;
$tax_rows [ $key ] -> total_shipping += $parent_order -> get_shipping_total () * - 1 ;
}
2015-02-13 13:39:41 +00:00
}
2013-07-10 11:05:45 +00:00
?>
< table class = " widefat " >
< thead >
< tr >
< th >< ? php _e ( 'Period' , 'woocommerce' ); ?> </th>
2016-10-12 10:16:30 +00:00
< th class = " total_row " >< ? php _e ( 'Number of orders' , 'woocommerce' ); ?> </th>
< 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>
< 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>
< 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>
2015-10-23 15:09:08 +00:00
< th class = " total_row " >< ? php _e ( 'Net profit' , 'woocommerce' ); ?> <?php echo wc_help_tip( __( "Total sales minus shipping and tax.", 'woocommerce' ) ); ?></th>
2013-07-10 11:05:45 +00:00
</ tr >
</ thead >
2016-06-06 16:24:31 +00:00
< ? php if ( ! empty ( $tax_rows ) ) : ?>
2013-07-10 11:05:45 +00:00
< tbody >
< ? php
2015-02-13 13:39:41 +00:00
foreach ( $tax_rows as $date => $tax_row ) {
2013-07-10 11:05:45 +00:00
$gross = $tax_row -> total_sales - $tax_row -> total_shipping ;
$total_tax = $tax_row -> tax_amount + $tax_row -> shipping_tax_amount ;
?>
< tr >
2016-09-09 00:14:28 +00:00
< th scope = " row " >
< ? php echo ( 'month' === $this -> chart_groupby ) ? date_i18n ( 'F' , strtotime ( $date . '01' ) ) : date_i18n ( get_option ( 'date_format' ), strtotime ( $date ) ); ?>
</ th >
2013-07-10 11:05:45 +00:00
< td class = " total_row " >< ? php echo $tax_row -> total_orders ; ?> </td>
2013-11-25 13:34:21 +00:00
< td class = " total_row " >< ? php echo wc_price ( $gross ); ?> </td>
< td class = " total_row " >< ? php echo wc_price ( $tax_row -> total_shipping ); ?> </td>
< td class = " total_row " >< ? php echo wc_price ( $total_tax ); ?> </td>
< td class = " total_row " >< ? php echo wc_price ( $gross - $total_tax ); ?> </td>
2013-07-10 11:05:45 +00:00
</ tr >
< ? php
}
?>
</ tbody >
2014-07-11 01:50:51 +00:00
< tfoot >
2014-09-18 09:28:41 +00:00
< ? php
$gross = array_sum ( wp_list_pluck ( ( array ) $tax_rows , 'total_sales' ) ) - array_sum ( wp_list_pluck ( ( array ) $tax_rows , 'total_shipping' ) );
$total_tax = array_sum ( wp_list_pluck ( ( array ) $tax_rows , 'tax_amount' ) ) + array_sum ( wp_list_pluck ( ( array ) $tax_rows , 'shipping_tax_amount' ) );
?>
2014-07-11 01:50:51 +00:00
< tr >
< th scope = " row " >< ? php _e ( 'Totals' , 'woocommerce' ); ?> </th>
< th class = " total_row " >< ? php echo array_sum ( wp_list_pluck ( ( array ) $tax_rows , 'total_orders' ) ); ?> </th>
< th class = " total_row " >< ? php echo wc_price ( $gross ); ?> </th>
< th class = " total_row " >< ? php echo wc_price ( array_sum ( wp_list_pluck ( ( array ) $tax_rows , 'total_shipping' ) ) ); ?> </th>
< th class = " total_row " >< ? php echo wc_price ( $total_tax ); ?> </th>
< th class = " total_row " >< ? php echo wc_price ( $gross - $total_tax ); ?> </th>
</ tr >
</ tfoot >
2013-07-10 11:05:45 +00:00
< ? php else : ?>
< tbody >
< tr >
< td >< ? php _e ( 'No taxes found in this period' , 'woocommerce' ); ?> </td>
</ tr >
</ tbody >
< ? php endif ; ?>
</ table >
< ? php
}
2014-08-31 07:19:13 +00:00
}