Reports for order lines

This commit is contained in:
Mike Jolley 2012-10-22 16:13:23 +01:00
parent 98bf973bf6
commit 5883701729
1 changed files with 310 additions and 328 deletions

View File

@ -254,8 +254,6 @@ function woocommerce_datepicker_js() {
var dates = jQuery( "#from, #to" ).datepicker({
defaultDate: "",
dateFormat: "yy-mm-dd",
//changeMonth: true,
//changeYear: true,
numberOfMonths: 1,
minDate: "-12M",
maxDate: "+0D",
@ -558,42 +556,54 @@ function woocommerce_daily_sales() {
// Get order ids and dates in range
$orders = $wpdb->get_results( $wpdb->prepare( "
SELECT posts.ID, posts.post_date FROM {$wpdb->posts} AS posts
SELECT posts.ID, posts.post_date, meta.meta_value AS total_sales 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 posts.post_type = 'shop_order'
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 post_date > '" . date('Y-m-d', $start_date ) . "'
AND post_date < '" . date('Y-m-d', strtotime('+1 day', $end_date ) ) . "'
GROUP BY posts.ID
ORDER BY post_date ASC
" ) );
if ( $orders ) {
$total_orders = sizeof( $orders );
foreach ( $orders as $order ) {
$order_total = get_post_meta($order->ID, '_order_total', true);
// get order timestamp
$time = strtotime( date( 'Ymd', strtotime( $order->post_date ) ) ) . '000';
$order_items_array = (array) get_post_meta( $order->ID, '_order_items', true );
foreach ( $order_items_array as $item )
$order_items += absint( $item['qty'] );
$total_sales += $order_total;
$total_orders++;
// Add order total
$total_sales += $order->total_sales;
// Get items
$order_items += absint( $wpdb->get_var( $wpdb->prepare( "
SELECT SUM( order_items.order_item_qty )
FROM {$wpdb->prefix}woocommerce_order_items as order_items
WHERE order_id = %d
", $order->ID ) ) );
// Set times
if ( isset( $order_counts[ $time ] ) )
$order_counts[ $time ]++;
else
$order_counts[ $time ] = 1;
if ( isset( $order_amounts[ $time ] ) )
$order_amounts[ $time ] = $order_amounts[ $time ] + $order_total;
$order_amounts[ $time ] = $order_amounts[ $time ] + $order->total_sales;
else
$order_amounts[ $time ] = floatval( $order_total );
$order_amounts[ $time ] = floatval( $order->total_sales );
}
}
?>
@ -606,7 +616,7 @@ function woocommerce_daily_sales() {
<div class="postbox">
<h3><span><?php _e( 'Total sales in range', 'woocommerce' ); ?></span></h3>
<div class="inside">
<p class="stat"><?php if ($total_sales>0) echo woocommerce_price($total_sales); else _e( 'n/a', 'woocommerce' ); ?></p>
<p class="stat"><?php if ( $total_sales > 0 ) echo woocommerce_price( $total_sales ); else _e( 'n/a', 'woocommerce' ); ?></p>
</div>
</div>
<div class="postbox">
@ -618,13 +628,13 @@ function woocommerce_daily_sales() {
<div class="postbox">
<h3><span><?php _e( 'Average order total in range', 'woocommerce' ); ?></span></h3>
<div class="inside">
<p class="stat"><?php if ($total_orders>0) echo woocommerce_price($total_sales/$total_orders); else _e( 'n/a', 'woocommerce' ); ?></p>
<p class="stat"><?php if ( $total_orders > 0 ) echo woocommerce_price( $total_sales / $total_orders ); else _e( 'n/a', 'woocommerce' ); ?></p>
</div>
</div>
<div class="postbox">
<h3><span><?php _e( 'Average order items in range', 'woocommerce' ); ?></span></h3>
<div class="inside">
<p class="stat"><?php if ($total_orders>0) echo number_format($order_items/$total_orders, 2); else _e( 'n/a', 'woocommerce' ); ?></p>
<p class="stat"><?php if ( $total_orders > 0 ) echo number_format( $order_items / $total_orders, 2 ); else _e( 'n/a', 'woocommerce' ); ?></p>
</div>
</div>
</div>
@ -891,9 +901,10 @@ function woocommerce_top_sellers() {
$end_date = strtotime( $end_date );
// Get order ids and dates in range
$orders = $wpdb->get_results( $wpdb->prepare( "
SELECT posts.ID, posts.post_date FROM {$wpdb->posts} AS posts
$order_items = $wpdb->get_results( $wpdb->prepare( "
SELECT order_items.product_id, SUM( order_items.order_item_qty ) as item_quantity FROM {$wpdb->prefix}woocommerce_order_items as order_items
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 )
@ -904,16 +915,14 @@ function woocommerce_top_sellers() {
AND term.slug IN ('" . implode( "','", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "')
AND post_date > '" . date('Y-m-d', $start_date ) . "'
AND post_date < '" . date('Y-m-d', strtotime('+1 day', $end_date ) ) . "'
ORDER BY post_date ASC
GROUP BY order_items.product_id
" ) );
$found_products = array();
if ( $orders ) {
foreach ($orders as $order) {
$order_items = (array) get_post_meta( $order->ID, '_order_items', true );
foreach ( $order_items as $item )
$found_products[ $item['id'] ] = isset( $found_products[ $item['id'] ] ) ? $found_products[ $item['id'] ] + $item['qty'] : $item['qty'];
if ( $order_items ) {
foreach ( $order_items as $order_item ) {
$found_products[ $order_item->product_id ] = $order_item->item_quantity;
}
}
@ -983,9 +992,10 @@ function woocommerce_top_earners() {
$end_date = strtotime( $end_date );
// Get order ids and dates in range
$orders = $wpdb->get_results( $wpdb->prepare( "
SELECT posts.ID, posts.post_date FROM {$wpdb->posts} AS posts
$order_items = $wpdb->get_results( $wpdb->prepare( "
SELECT order_items.product_id, SUM( order_items.line_total ) as line_total FROM {$wpdb->prefix}woocommerce_order_items as order_items
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 )
@ -996,21 +1006,14 @@ function woocommerce_top_earners() {
AND term.slug IN ('" . implode( "','", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "')
AND post_date > '" . date('Y-m-d', $start_date ) . "'
AND post_date < '" . date('Y-m-d', strtotime('+1 day', $end_date ) ) . "'
ORDER BY post_date ASC
GROUP BY order_items.product_id
" ) );
$found_products = array();
if ( $orders ) {
foreach ($orders as $order) {
$order_items = (array) get_post_meta( $order->ID, '_order_items', true );
foreach ( $order_items as $item ) {
if ( isset( $item['line_total'] ) )
$row_cost = $item['line_total'];
else
$row_cost = $item['cost'] * $item['qty'];
$found_products[ $item['id'] ] = isset( $found_products[ $item['id'] ] ) ? $found_products[ $item['id'] ] + $row_cost : $row_cost;
}
if ( $order_items ) {
foreach ( $order_items as $order_item ) {
$found_products[ $order_item->product_id ] = $order_item->line_total;
}
}
@ -1091,48 +1094,44 @@ function woocommerce_product_sales() {
// Get order items
$order_items = $wpdb->get_results( $wpdb->prepare( "
SELECT meta.meta_value AS items, posts.post_date FROM {$wpdb->posts} AS posts
SELECT order_items.product_id, posts.post_date, SUM( order_items.order_item_qty ) as item_quantity, SUM( order_items.line_total ) as line_total
FROM {$wpdb->prefix}woocommerce_order_items as order_items
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->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 meta.meta_key = '_order_items'
AND posts.post_type = 'shop_order'
WHERE posts.post_type = 'shop_order'
AND order_items.product_id IN ('" . implode( "','", array_merge( $chosen_product_ids, $children_ids ) ) . "')
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 posts.post_date > date_sub( NOW(), INTERVAL 1 YEAR )
GROUP BY order_items.order_id
ORDER BY posts.post_date ASC
" ) );
if ( $order_items ) {
$found_products = array();
if ( $order_items ) {
foreach ( $order_items as $order_item ) {
$date = date( 'Ym', strtotime( $order_item->post_date ) );
$items = maybe_unserialize( $order_item->items );
foreach ( $items as $item ) {
if ( ! in_array( $item['id'], $chosen_product_ids ) && ! in_array( $item['id'], $children_ids ) )
if ( $order_item->line_total == 0 && $order_item->item_quantity == 0 )
continue;
if ( isset( $item['line_total'] ) ) $row_cost = $item['line_total'];
else $row_cost = $item['cost'] * $item['qty'];
// Get date
$date = date( 'Ym', strtotime( $order_item->post_date ) );
if ( ! $row_cost ) continue;
// Set values
$product_sales[ $date ] = isset( $product_sales[ $date ] ) ? $product_sales[ $date ] + $order_item->item_quantity : $order_item->item_quantity;
$product_totals[ $date ] = isset( $product_totals[ $date ] ) ? $product_totals[ $date ] + $order_item->line_total : $order_item->line_total;
$product_sales[ $date ] = isset( $product_sales[ $date ] ) ? $product_sales[$date] + $item['qty'] : $item['qty'];
$product_totals[ $date ] = isset( $product_totals[ $date ] ) ? $product_totals[ $date ] + $row_cost : $row_cost;
if ( $product_sales[ $date ] > $max_sales )
$max_sales = $product_sales[ $date ];
if ( $product_sales[ $date ] > $max_sales ) $max_sales = $product_sales[ $date ];
if ( $product_totals[ $date ] > $max_totals ) $max_totals = $product_totals[ $date ];
if ( $product_totals[ $date ] > $max_totals )
$max_totals = $product_totals[ $date ];
}
}
}
?>
<h4><?php printf( __( 'Sales for %s:', 'woocommerce' ), implode( ', ', $chosen_product_titles ) ); ?></h4>
@ -1173,6 +1172,7 @@ function woocommerce_product_sales() {
<p><select id="product_ids" name="product_ids[]" class="ajax_chosen_select_products" multiple="multiple" data-placeholder="<?php _e( 'Search for a product&hellip;', 'woocommerce' ); ?>" style="width: 400px;"></select> <input type="submit" style="vertical-align: top;" class="button" value="<?php _e( 'Show', 'woocommerce' ); ?>" /></p>
<script type="text/javascript">
jQuery(function(){
// Ajax Chosen Product Selectors
jQuery("select.ajax_chosen_select_products").ajaxChosen({
method: 'GET',
@ -1193,6 +1193,7 @@ function woocommerce_product_sales() {
return terms;
});
});
</script>
</form>
@ -1903,45 +1904,33 @@ function woocommerce_category_sales() {
$item_sales = array();
for ( $count = 0; $count < 12; $count++ ) {
$time = strtotime( date( 'Ym', strtotime( '+ ' . $count . ' MONTH', $start_date ) ) . '01' ) . '000';
if ( $time > current_time( 'timestamp' ) . '000' )
continue;
$month = date( 'Ym', strtotime( date( 'Ym', strtotime( '+ '. $count . ' MONTH', $start_date ) ) . '01' ) );
// Get order items
$order_items_serialized = $wpdb->get_col( $wpdb->prepare( "
SELECT meta.meta_value AS items FROM {$wpdb->posts} AS posts
$start_date = date( 'Ym', strtotime( date( 'Ym', strtotime( '-1 year', $start_date ) ) . '01' ) );
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
$order_items = $wpdb->get_results( $wpdb->prepare( "
SELECT order_items.product_id, posts.post_date, SUM( order_items.line_total ) as line_total
FROM {$wpdb->prefix}woocommerce_order_items as order_items
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 meta.meta_key = '_order_items'
AND posts.post_type = 'shop_order'
WHERE 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 '{$month}' = date_format(posts.post_date,'%Y%m')
AND date_format(posts.post_date,'%%Y%%m') >= '{$start_date}'
GROUP BY order_items.order_id
ORDER BY posts.post_date ASC
" ) );
if ( $order_items_serialized ) {
foreach ( $order_items_serialized as $order_items_array ) {
$order_items_array = maybe_unserialize( $order_items_array );
if ( is_array( $order_items_array ) )
foreach ( $order_items_array as $item ) {
if ( $order_items ) {
foreach ( $order_items as $order_item ) {
if ( ! isset( $item_sales[ $count ][ $item['id'] ] ) )
$item_sales[ $count ][ $item['id'] ] = 0;
$month = date( 'm', strtotime( $order_item->post_date ) ) - 1;
if ( ! empty( $item['line_total'] ) )
$item_sales[ $count ][ $item['id'] ] += $item['line_total'];
}
}
$item_sales[ $month ][ $order_item->product_id ] = isset( $item_sales[ $month ][ $order_item->product_id ] ) ? $item_sales[ $month ][ $order_item->product_id ] + $order_item->line_total : $order_item->line_total;
}
}
@ -1971,13 +1960,6 @@ function woocommerce_category_sales() {
<th><strong><?php _e( 'Total', 'woocommerce' ); ?></strong></th>
</tr>
</thead>
<!--<tfoot>
<tr>
<th colspan="<?php echo $column_count + 2; ?>">
<a class="button export-data" href="<?php echo add_query_arg( 'export', 'true' ); ?>"><?php _e( 'Export data', 'woocommerce' ); ?></a>
</th>
</tr>
</tfoot>-->
<tbody><?php
// While outputting, lets store them for the chart
$chart_data = $month_totals = $category_totals = array();