Tax reports

This commit is contained in:
Mike Jolley 2013-07-10 12:05:45 +01:00
parent b085df3302
commit b5c779aef8
7 changed files with 436 additions and 331 deletions

View File

@ -146,11 +146,18 @@ class WC_Admin_Reports {
$reports['taxes'] = array(
'title' => __( 'Tax', 'woocommerce' ),
'reports' => array(
"taxes_by_month" => array(
'title' => __( 'Taxes by month', 'woocommerce' ),
"taxes_by_code" => array(
'title' => __( 'Taxes by code', 'woocommerce' ),
'description' => '',
'callback' => 'woocommerce_monthly_taxes'
)
'hide_title' => true,
'callback' => array( $this, 'get_report' )
),
"taxes_by_date" => array(
'title' => __( 'Taxes by date', 'woocommerce' ),
'description' => '',
'hide_title' => true,
'callback' => array( $this, 'get_report' )
),
)
);
}
@ -203,309 +210,4 @@ class WC_Admin_Reports {
}
}
new WC_Admin_Reports();
/**
* Output the monthly tax stats.
*
* @access public
* @return void
*/
function woocommerce_monthly_taxes() {
global $start_date, $end_date, $woocommerce, $wpdb;
$first_year = $wpdb->get_var( "SELECT post_date FROM $wpdb->posts WHERE post_date != 0 ORDER BY post_date ASC LIMIT 1;" );
if ( $first_year )
$first_year = date( 'Y', strtotime( $first_year ) );
else
$first_year = date( 'Y' );
$current_year = isset( $_POST['show_year'] ) ? $_POST['show_year'] : date( 'Y', current_time( 'timestamp' ) );
$start_date = strtotime( $current_year . '0101' );
$total_tax = $total_sales_tax = $total_shipping_tax = $count = 0;
$taxes = $tax_rows = $tax_row_labels = array();
for ( $count = 0; $count < 12; $count++ ) {
$time = strtotime( date('Ym', strtotime( '+ ' . $count . ' MONTH', $start_date ) ) . '01' );
if ( $time > current_time( 'timestamp' ) )
continue;
$month = date( 'Ym', strtotime( date( 'Ym', strtotime( '+ ' . $count . ' MONTH', $start_date ) ) . '01' ) );
$gross = $wpdb->get_var( $wpdb->prepare( "
SELECT SUM( meta.meta_value ) AS order_tax
FROM {$wpdb->posts} AS posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID
LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
LEFT JOIN {$wpdb->terms} AS term USING( term_id )
WHERE meta.meta_key = '_order_total'
AND posts.post_type = 'shop_order'
AND posts.post_status = 'publish'
AND tax.taxonomy = 'shop_order_status'
AND term.slug IN ('" . implode( "','", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "')
AND %s = date_format(posts.post_date,'%%Y%%m')
", $month ) );
$shipping = $wpdb->get_var( $wpdb->prepare( "
SELECT SUM( meta.meta_value ) AS order_tax
FROM {$wpdb->posts} AS posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID
LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
LEFT JOIN {$wpdb->terms} AS term USING( term_id )
WHERE meta.meta_key = '_order_shipping'
AND posts.post_type = 'shop_order'
AND posts.post_status = 'publish'
AND tax.taxonomy = 'shop_order_status'
AND term.slug IN ('" . implode( "','", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "')
AND %s = date_format(posts.post_date,'%%Y%%m')
", $month ) );
$order_tax = $wpdb->get_var( $wpdb->prepare( "
SELECT SUM( meta.meta_value ) AS order_tax
FROM {$wpdb->posts} AS posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID
LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
LEFT JOIN {$wpdb->terms} AS term USING( term_id )
WHERE meta.meta_key = '_order_tax'
AND posts.post_type = 'shop_order'
AND posts.post_status = 'publish'
AND tax.taxonomy = 'shop_order_status'
AND term.slug IN ('" . implode( "','", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "')
AND %s = date_format(posts.post_date,'%%Y%%m')
", $month ) );
$shipping_tax = $wpdb->get_var( $wpdb->prepare( "
SELECT SUM( meta.meta_value ) AS order_tax
FROM {$wpdb->posts} AS posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID
LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
LEFT JOIN {$wpdb->terms} AS term USING( term_id )
WHERE meta.meta_key = '_order_shipping_tax'
AND posts.post_type = 'shop_order'
AND posts.post_status = 'publish'
AND tax.taxonomy = 'shop_order_status'
AND term.slug IN ('" . implode( "','", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "')
AND %s = date_format(posts.post_date,'%%Y%%m')
", $month ) );
$tax_rows = $wpdb->get_results( $wpdb->prepare( "
SELECT
order_items.order_item_name as name,
SUM( order_item_meta.meta_value ) as tax_amount,
SUM( order_item_meta_2.meta_value ) as shipping_tax_amount,
SUM( order_item_meta.meta_value + order_item_meta_2.meta_value ) as total_tax_amount
FROM {$wpdb->prefix}woocommerce_order_items as order_items
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta_2 ON order_items.order_item_id = order_item_meta_2.order_item_id
LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID
LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
LEFT JOIN {$wpdb->terms} AS term USING( term_id )
WHERE order_items.order_item_type = 'tax'
AND posts.post_type = 'shop_order'
AND posts.post_status = 'publish'
AND tax.taxonomy = 'shop_order_status'
AND term.slug IN ('" . implode( "','", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "')
AND %s = date_format( posts.post_date,'%%Y%%m' )
AND order_item_meta.meta_key = 'tax_amount'
AND order_item_meta_2.meta_key = 'shipping_tax_amount'
GROUP BY order_items.order_item_name
", $month ) );
if ( $tax_rows ) {
foreach ( $tax_rows as $tax_row ) {
if ( $tax_row->total_tax_amount > 0 )
$tax_row_labels[] = $tax_row->name;
}
}
$taxes[ date( 'M', strtotime( $month . '01' ) ) ] = array(
'gross' => $gross,
'shipping' => $shipping,
'order_tax' => $order_tax,
'shipping_tax' => $shipping_tax,
'total_tax' => $shipping_tax + $order_tax,
'tax_rows' => $tax_rows
);
$total_sales_tax += $order_tax;
$total_shipping_tax += $shipping_tax;
}
$total_tax = $total_sales_tax + $total_shipping_tax;
?>
<form method="post" action="">
<p><label for="show_year"><?php _e( 'Year:', 'woocommerce' ); ?></label>
<select name="show_year" id="show_year">
<?php
for ( $i = $first_year; $i <= date('Y'); $i++ )
printf( '<option value="%s" %s>%s</option>', $i, selected( $current_year, $i, false ), $i );
?>
</select> <input type="submit" class="button" value="<?php _e( 'Show', 'woocommerce' ); ?>" /></p>
</form>
<div id="poststuff" class="woocommerce-reports-wrap">
<div class="woocommerce-reports-sidebar">
<div class="postbox">
<h3><span><?php _e( 'Total taxes for year', 'woocommerce' ); ?></span></h3>
<div class="inside">
<p class="stat"><?php
if ( $total_tax > 0 )
echo woocommerce_price( $total_tax );
else
_e( 'n/a', 'woocommerce' );
?></p>
</div>
</div>
<div class="postbox">
<h3><span><?php _e( 'Total product taxes for year', 'woocommerce' ); ?></span></h3>
<div class="inside">
<p class="stat"><?php
if ( $total_sales_tax > 0 )
echo woocommerce_price( $total_sales_tax );
else
_e( 'n/a', 'woocommerce' );
?></p>
</div>
</div>
<div class="postbox">
<h3><span><?php _e( 'Total shipping tax for year', 'woocommerce' ); ?></span></h3>
<div class="inside">
<p class="stat"><?php
if ( $total_shipping_tax > 0 )
echo woocommerce_price( $total_shipping_tax );
else
_e( 'n/a', 'woocommerce' );
?></p>
</div>
</div>
</div>
<div class="woocommerce-reports-main">
<table class="widefat">
<thead>
<tr>
<th><?php _e( 'Month', 'woocommerce' ); ?></th>
<th class="total_row"><?php _e( 'Total Sales', 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e("This is the sum of the 'Order Total' field within your orders.", 'woocommerce'); ?>" href="#">[?]</a></th>
<th class="total_row"><?php _e( 'Total Shipping', 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e("This is the sum of the 'Shipping Total' field within your orders.", 'woocommerce'); ?>" href="#">[?]</a></th>
<th class="total_row"><?php _e( 'Total Product Taxes', 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e("This is the sum of the 'Cart Tax' field within your orders.", 'woocommerce'); ?>" href="#">[?]</a></th>
<th class="total_row"><?php _e( 'Total Shipping Taxes', 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e("This is the sum of the 'Shipping Tax' field within your orders.", 'woocommerce'); ?>" href="#">[?]</a></th>
<th class="total_row"><?php _e( 'Total Taxes', 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e("This is the sum of the 'Cart Tax' and 'Shipping Tax' fields within your orders.", 'woocommerce'); ?>" href="#">[?]</a></th>
<th class="total_row"><?php _e( 'Net profit', 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e("Total sales minus shipping and tax.", 'woocommerce'); ?>" href="#">[?]</a></th>
<?php
$tax_row_labels = array_filter( array_unique( $tax_row_labels ) );
foreach ( $tax_row_labels as $label )
echo '<th class="tax_row">' . $label . '</th>';
?>
</tr>
</thead>
<tfoot>
<tr>
<?php
$total = array();
foreach ( $taxes as $month => $tax ) {
$total['gross'] = isset( $total['gross'] ) ? $total['gross'] + $tax['gross'] : $tax['gross'];
$total['shipping'] = isset( $total['shipping'] ) ? $total['shipping'] + $tax['shipping'] : $tax['shipping'];
$total['order_tax'] = isset( $total['order_tax'] ) ? $total['order_tax'] + $tax['order_tax'] : $tax['order_tax'];
$total['shipping_tax'] = isset( $total['shipping_tax'] ) ? $total['shipping_tax'] + $tax['shipping_tax'] : $tax['shipping_tax'];
$total['total_tax'] = isset( $total['total_tax'] ) ? $total['total_tax'] + $tax['total_tax'] : $tax['total_tax'];
foreach ( $tax_row_labels as $label )
foreach ( $tax['tax_rows'] as $tax_row )
if ( $tax_row->name == $label ) {
$total['tax_rows'][ $label ] = isset( $total['tax_rows'][ $label ] ) ? $total['tax_rows'][ $label ] + $tax_row->total_tax_amount : $tax_row->total_tax_amount;
}
}
echo '
<td>' . __( 'Total', 'woocommerce' ) . '</td>
<td class="total_row">' . woocommerce_price( $total['gross'] ) . '</td>
<td class="total_row">' . woocommerce_price( $total['shipping'] ) . '</td>
<td class="total_row">' . woocommerce_price( $total['order_tax'] ) . '</td>
<td class="total_row">' . woocommerce_price( $total['shipping_tax'] ) . '</td>
<td class="total_row">' . woocommerce_price( $total['total_tax'] ) . '</td>
<td class="total_row">' . woocommerce_price( $total['gross'] - $total['shipping'] - $total['total_tax'] ) . '</td>';
foreach ( $tax_row_labels as $label )
if ( isset( $total['tax_rows'][ $label ] ) )
echo '<td class="tax_row">' . woocommerce_price( $total['tax_rows'][ $label ] ) . '</td>';
else
echo '<td class="tax_row">' . woocommerce_price( 0 ) . '</td>';
?>
</tr>
<tr>
<th colspan="<?php echo 7 + sizeof( $tax_row_labels ); ?>"><button class="button toggle_tax_rows"><?php _e( 'Toggle tax rows', 'woocommerce' ); ?></button></th>
</tr>
</tfoot>
<tbody>
<?php
foreach ( $taxes as $month => $tax ) {
$alt = ( isset( $alt ) && $alt == 'alt' ) ? '' : 'alt';
echo '<tr class="' . $alt . '">
<td>' . $month . '</td>
<td class="total_row">' . woocommerce_price( $tax['gross'] ) . '</td>
<td class="total_row">' . woocommerce_price( $tax['shipping'] ) . '</td>
<td class="total_row">' . woocommerce_price( $tax['order_tax'] ) . '</td>
<td class="total_row">' . woocommerce_price( $tax['shipping_tax'] ) . '</td>
<td class="total_row">' . woocommerce_price( $tax['total_tax'] ) . '</td>
<td class="total_row">' . woocommerce_price( $tax['gross'] - $tax['shipping'] - $tax['total_tax'] ) . '</td>';
foreach ( $tax_row_labels as $label ) {
$row_total = 0;
foreach ( $tax['tax_rows'] as $tax_row ) {
if ( $tax_row->name == $label ) {
$row_total = $tax_row->total_tax_amount;
}
}
echo '<td class="tax_row">' . woocommerce_price( $row_total ) . '</td>';
}
echo '</tr>';
}
?>
</tbody>
</table>
<script type="text/javascript">
jQuery('.toggle_tax_rows').click(function(){
jQuery('.tax_row').toggle();
jQuery('.total_row').toggle();
});
jQuery('.tax_row').hide();
</script>
</div>
</div>
<?php
}
new WC_Admin_Reports();

View File

@ -304,7 +304,7 @@ class WC_Report_Customer_List extends WP_List_Table {
);
$query = new WP_User_Query( array(
//'exclude' => array_merge( $admin_users->get_results(), $manager_users->get_results() ),
'exclude' => array_merge( $admin_users->get_results(), $manager_users->get_results() ),
'number' => $per_page,
'offset' => ( $current_page - 1 ) * $per_page
) );

View File

@ -0,0 +1,196 @@
<?php
/**
* WC_Report_Taxes_By_Code class
*/
class WC_Report_Taxes_By_Code extends WC_Admin_Report {
/**
* Get the legend for the main chart sidebar
* @return array
*/
public function get_chart_legend() {
$legend = array();
return array();
}
/**
* Output the report
*/
public function output_report() {
global $woocommerce, $wpdb, $wp_locale;
$ranges = array(
'year' => __( 'Year', 'woocommerce' ),
'last_month' => __( 'Last Month', 'woocommerce' ),
'month' => __( 'This Month', 'woocommerce' ),
);
$current_range = ! empty( $_GET['range'] ) ? $_GET['range'] : 'last_month';
switch ( $current_range ) {
case 'custom' :
$this->start_date = strtotime( sanitize_text_field( $_GET['start_date'] ) );
$this->end_date = strtotime( '12am + 1 day', strtotime( sanitize_text_field( $_GET['end_date'] ) ) );
if ( ! $this->end_date )
$this->end_date = current_time('timestamp');
$interval = 0;
$min_date = $this->start_date;
while ( ( $min_date = strtotime( "+1 MONTH", $min_date ) ) <= $this->end_date ) {
$interval ++;
}
// 3 months max for day view
if ( $interval > 3 )
$this->chart_groupby = 'month';
else
$this->chart_groupby = 'day';
break;
case 'year' :
$this->start_date = strtotime( 'first day of january', current_time('timestamp') );
$this->end_date = strtotime( '12am + 1 day', current_time( 'timestamp' ) );
$this->chart_groupby = 'month';
break;
default :
case 'last_month' :
$this->start_date = strtotime( 'first day of last month', current_time('timestamp') );
$this->end_date = strtotime( 'last day of last month', current_time('timestamp') );
$this->chart_groupby = 'day';
break;
case 'month' :
$this->start_date = strtotime( 'first day of this month', current_time('timestamp') );
$this->end_date = strtotime( '12am + 1 day', current_time( 'timestamp' ) );
$this->chart_groupby = 'day';
break;
}
// Group by
switch ( $this->chart_groupby ) {
case 'day' :
$this->group_by_query = 'YEAR(post_date), MONTH(post_date), DAY(post_date)';
$this->chart_interval = max( 0, ( $this->end_date - $this->start_date ) / ( 60 * 60 * 24 ) );
$this->barwidth = 60 * 60 * 24 * 1000;
break;
case 'month' :
$this->group_by_query = 'YEAR(post_date), MONTH(post_date)';
$this->chart_interval = 0;
$min_date = $this->start_date;
while ( ( $min_date = strtotime( "+1 MONTH", $min_date ) ) <= $this->end_date ) {
$this->chart_interval ++;
}
$this->barwidth = 60 * 60 * 24 * 7 * 4 * 1000;
break;
}
$hide_sidebar = true;
include( WC()->plugin_path() . '/admin/views/html-report-by-date.php');
}
/**
* Get the main chart
* @return string
*/
public function get_main_chart() {
global $wpdb;
$tax_rows = $this->get_order_report_data( array(
'data' => array(
'order_item_name' => array(
'type' => 'order_item',
'function' => '',
'name' => 'tax_rate'
),
'tax_amount' => array(
'type' => 'order_item_meta',
'order_item_type' => 'tax',
'function' => 'SUM',
'name' => 'tax_amount'
),
'shipping_tax_amount' => array(
'type' => 'order_item_meta',
'order_item_type' => 'tax',
'function' => 'SUM',
'name' => 'shipping_tax_amount'
),
'rate_id' => array(
'type' => 'order_item_meta',
'order_item_type' => 'tax',
'function' => '',
'name' => 'rate_id'
),
'ID' => array(
'type' => 'post_data',
'function' => 'COUNT',
'name' => 'total_orders',
'distinct' => true,
),
),
'where' => array(
array(
'key' => 'order_item_type',
'value' => 'tax',
'operator' => '='
),
array(
'key' => 'order_item_name',
'value' => '',
'operator' => '!='
)
),
'group_by' => 'tax_rate',
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true
) );
?>
<table class="widefat">
<thead>
<tr>
<th><?php _e( 'Tax', 'woocommerce' ); ?></th>
<th><?php _e( 'Rate', 'woocommerce' ); ?></th>
<th class="total_row"><?php _e( 'Number of orders', 'woocommerce' ); ?></th>
<th class="total_row"><?php _e( 'Tax Amount', 'woocommerce' ); ?> <a class="tips" data-tip="<?php esc_attr_e( 'This is the sum of the "Tax Rows" tax amount within your orders.', 'woocommerce' ); ?>" href="#">[?]</a></th>
<th class="total_row"><?php _e( 'Shipping Tax Amount', 'woocommerce' ); ?> <a class="tips" data-tip="<?php esc_attr_e( 'This is the sum of the "Tax Rows" shipping tax amount within your orders.', 'woocommerce' ); ?>" href="#">[?]</a></th>
<th class="total_row"><?php _e( 'Total Tax', 'woocommerce' ); ?> <a class="tips" data-tip="<?php esc_attr_e( 'This is the total tax for the rate (shipping tax + product tax).', 'woocommerce' ); ?>" href="#">[?]</a></th>
</tr>
</thead>
<?php if ( $tax_rows ) : ?>
<tfoot>
<tr>
<th scope="row" colspan="3"><?php _e( 'Total', 'woocommerce' ); ?></th>
<th class="total_row"><?php echo woocommerce_price( array_sum( wp_list_pluck( (array) $tax_rows, 'tax_amount' ) ) ); ?></th>
<th class="total_row"><?php echo woocommerce_price( array_sum( wp_list_pluck( (array) $tax_rows, 'shipping_tax_amount' ) ) ); ?></th>
<th class="total_row"><strong><?php echo woocommerce_price( array_sum( wp_list_pluck( (array) $tax_rows, 'tax_amount' ) ) + array_sum( wp_list_pluck( (array) $tax_rows, 'shipping_tax_amount' ) ) ); ?></strong></th>
</tr>
</tfoot>
<tbody>
<?php
foreach ( $tax_rows as $tax_row ) {
$rate = $wpdb->get_var( $wpdb->prepare( "SELECT tax_rate FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %d;", $tax_row->rate_id ) );
?>
<tr>
<th scope="row"><?php echo $tax_row->tax_rate; ?></th>
<td><?php echo $rate; ?>%</td>
<td class="total_row"><?php echo $tax_row->total_orders; ?></td>
<td class="total_row"><?php echo woocommerce_price( $tax_row->tax_amount ); ?></td>
<td class="total_row"><?php echo woocommerce_price( $tax_row->shipping_tax_amount ); ?></td>
<td class="total_row"><?php echo woocommerce_price( $tax_row->tax_amount + $tax_row->shipping_tax_amount ); ?></td>
</tr>
<?php
}
?>
</tbody>
<?php else : ?>
<tbody>
<tr>
<td><?php _e( 'No taxes found in this period', 'woocommerce' ); ?></td>
</tr>
</tbody>
<?php endif; ?>
</table>
<?php
}
}

View File

@ -0,0 +1,197 @@
<?php
/**
* WC_Report_Taxes_By_Date class
*/
class WC_Report_Taxes_By_Date extends WC_Admin_Report {
/**
* Get the legend for the main chart sidebar
* @return array
*/
public function get_chart_legend() {
$legend = array();
return array();
}
/**
* Output the report
*/
public function output_report() {
global $woocommerce, $wpdb, $wp_locale;
$ranges = array(
'year' => __( 'Year', 'woocommerce' ),
'last_month' => __( 'Last Month', 'woocommerce' ),
'month' => __( 'This Month', 'woocommerce' ),
);
$current_range = ! empty( $_GET['range'] ) ? $_GET['range'] : 'last_month';
switch ( $current_range ) {
case 'custom' :
$this->start_date = strtotime( sanitize_text_field( $_GET['start_date'] ) );
$this->end_date = strtotime( '12am + 1 day', strtotime( sanitize_text_field( $_GET['end_date'] ) ) );
if ( ! $this->end_date )
$this->end_date = current_time('timestamp');
$interval = 0;
$min_date = $this->start_date;
while ( ( $min_date = strtotime( "+1 MONTH", $min_date ) ) <= $this->end_date ) {
$interval ++;
}
// 3 months max for day view
if ( $interval > 3 )
$this->chart_groupby = 'month';
else
$this->chart_groupby = 'day';
break;
case 'year' :
$this->start_date = strtotime( 'first day of january', current_time('timestamp') );
$this->end_date = strtotime( '12am + 1 day', current_time( 'timestamp' ) );
$this->chart_groupby = 'month';
break;
default :
case 'last_month' :
$this->start_date = strtotime( 'first day of last month', current_time('timestamp') );
$this->end_date = strtotime( 'last day of last month', current_time('timestamp') );
$this->chart_groupby = 'day';
break;
case 'month' :
$this->start_date = strtotime( 'first day of this month', current_time('timestamp') );
$this->end_date = strtotime( '12am + 1 day', current_time( 'timestamp' ) );
$this->chart_groupby = 'day';
break;
}
// Group by
switch ( $this->chart_groupby ) {
case 'day' :
$this->group_by_query = 'YEAR(post_date), MONTH(post_date), DAY(post_date)';
$this->chart_interval = max( 0, ( $this->end_date - $this->start_date ) / ( 60 * 60 * 24 ) );
$this->barwidth = 60 * 60 * 24 * 1000;
break;
case 'month' :
$this->group_by_query = 'YEAR(post_date), MONTH(post_date)';
$this->chart_interval = 0;
$min_date = $this->start_date;
while ( ( $min_date = strtotime( "+1 MONTH", $min_date ) ) <= $this->end_date ) {
$this->chart_interval ++;
}
$this->barwidth = 60 * 60 * 24 * 7 * 4 * 1000;
break;
}
$hide_sidebar = true;
include( WC()->plugin_path() . '/admin/views/html-report-by-date.php');
}
/**
* Get the main chart
* @return string
*/
public function get_main_chart() {
global $wpdb;
$tax_rows = $this->get_order_report_data( array(
'data' => array(
'_order_tax' => array(
'type' => 'meta',
'function' => 'SUM',
'name' => 'tax_amount'
),
'_order_shipping_tax' => array(
'type' => 'meta',
'function' => 'SUM',
'name' => 'shipping_tax_amount'
),
'_order_total' => array(
'type' => 'meta',
'function' => 'SUM',
'name' => 'total_sales'
),
'_order_shipping' => array(
'type' => 'meta',
'function' => 'SUM',
'name' => 'total_shipping'
),
'ID' => array(
'type' => 'post_data',
'function' => 'COUNT',
'name' => 'total_orders',
'distinct' => true,
),
'post_date' => array(
'type' => 'post_data',
'function' => '',
'name' => 'post_date'
),
),
'group_by' => $this->group_by_query,
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true
) );
?>
<table class="widefat">
<thead>
<tr>
<th><?php _e( 'Period', 'woocommerce' ); ?></th>
<th class="total_row"><?php _e( 'Number of orders', 'woocommerce' ); ?></th>
<th class="total_row"><?php _e( 'Total Sales', 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e("This is the sum of the 'Order Total' field within your orders.", 'woocommerce'); ?>" href="#">[?]</a></th>
<th class="total_row"><?php _e( 'Total Shipping', 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e("This is the sum of the 'Shipping Total' field within your orders.", 'woocommerce'); ?>" href="#">[?]</a></th>
<th class="total_row"><?php _e( 'Total Tax', 'woocommerce' ); ?> <a class="tips" data-tip="<?php esc_attr_e( 'This is the total tax for the rate (shipping tax + product tax).', 'woocommerce' ); ?>" href="#">[?]</a></th>
<th class="total_row"><?php _e( 'Net profit', 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e("Total sales minus shipping and tax.", 'woocommerce'); ?>" href="#">[?]</a></th>
</tr>
</thead>
<?php if ( $tax_rows ) :
$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' ) );
?>
<tfoot>
<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 woocommerce_price( $gross ); ?></th>
<th class="total_row"><?php echo woocommerce_price( array_sum( wp_list_pluck( (array) $tax_rows, 'total_shipping' ) ) ); ?></th>
<th class="total_row"><?php echo woocommerce_price( $total_tax ); ?></th>
<th class="total_row"><?php echo woocommerce_price( $gross - $total_tax ); ?></th>
</tr>
</tfoot>
<tbody>
<?php
foreach ( $tax_rows as $tax_row ) {
$gross = $tax_row->total_sales - $tax_row->total_shipping;
$total_tax = $tax_row->tax_amount + $tax_row->shipping_tax_amount;
?>
<tr>
<th scope="row"><?php
if ( $this->chart_groupby == 'month' )
echo date_i18n( 'F', strtotime( $tax_row->post_date ) );
else
echo date_i18n( get_option( 'date_format' ), strtotime( $tax_row->post_date ) );
?></th>
<td class="total_row"><?php echo $tax_row->total_orders; ?></td>
<td class="total_row"><?php echo woocommerce_price( $gross ); ?></td>
<td class="total_row"><?php echo woocommerce_price( $tax_row->total_shipping ); ?></td>
<td class="total_row"><?php echo woocommerce_price( $total_tax ); ?></td>
<td class="total_row"><?php echo woocommerce_price( $gross - $total_tax ); ?></td>
</tr>
<?php
}
?>
</tbody>
<?php else : ?>
<tbody>
<tr>
<td><?php _e( 'No taxes found in this period', 'woocommerce' ); ?></td>
</tr>
</tbody>
<?php endif; ?>
</table>
<?php
}
}

View File

@ -33,27 +33,33 @@
</li>
</ul>
</h3>
<div class="inside chart-with-sidebar">
<div class="chart-sidebar">
<ul class="chart-legend">
<?php foreach ( $this->get_chart_legend() as $legend ) : ?>
<li style="border-color: <?php echo $legend['color']; ?>">
<?php echo $legend['title']; ?>
</li>
<?php endforeach; ?>
</ul>
<ul class="chart-widgets">
<?php foreach ( $this->get_chart_widgets() as $widget ) : ?>
<li class="chart-widget">
<?php if ( $widget['title'] ) : ?><h4><?php echo $widget['title']; ?></h4><?php endif; ?>
<?php call_user_func( $widget['callback'] ); ?>
</li>
<?php endforeach; ?>
</ul>
<?php if ( empty( $hide_sidebar ) ) : ?>
<div class="inside chart-with-sidebar">
<div class="chart-sidebar">
<ul class="chart-legend">
<?php foreach ( $this->get_chart_legend() as $legend ) : ?>
<li style="border-color: <?php echo $legend['color']; ?>">
<?php echo $legend['title']; ?>
</li>
<?php endforeach; ?>
</ul>
<ul class="chart-widgets">
<?php foreach ( $this->get_chart_widgets() as $widget ) : ?>
<li class="chart-widget">
<?php if ( $widget['title'] ) : ?><h4><?php echo $widget['title']; ?></h4><?php endif; ?>
<?php call_user_func( $widget['callback'] ); ?>
</li>
<?php endforeach; ?>
</ul>
</div>
<div class="main">
<?php $this->get_main_chart(); ?>
</div>
</div>
<div class="main">
<?php else : ?>
<div class="inside">
<?php $this->get_main_chart(); ?>
</div>
</div>
<?php endif; ?>
</div>
</div>

File diff suppressed because one or more lines are too long

View File

@ -2606,6 +2606,10 @@ img.ui-datepicker-trigger { vertical-align: middle; margin-top: -1px; cursor: po
h3 {
cursor: default !important;
}
.inside {
padding: 10px;
margin: 0 !important;
}
h3.stats_range {
padding: 0 !important;
ul {