/**
 * Output the customer overview stats.
 *
 * @access public
 * @return void
 */
function woocommerce_customer_overview()
{
    global $start_date, $end_date, $woocommerce, $wpdb, $wp_locale;
    $total_customers = 0;
    $total_customer_sales = 0;
    $total_guest_sales = 0;
    $total_customer_orders = 0;
    $total_guest_orders = 0;
    $users_query = new WP_User_Query(array('fields' => array('user_registered'), 'role' => 'customer'));
    $customers = $users_query->get_results();
    $total_customers = (int) sizeof($customers);
    $customer_orders = apply_filters('woocommerce_reports_customer_overview_customer_orders', $wpdb->get_row("\n\t\tSELECT SUM(meta.meta_value) AS total_sales, COUNT(posts.ID) AS total_orders FROM {$wpdb->posts} AS posts\n\n\t\tLEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id\n\t\tLEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID\n\t\tLEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )\n\t\tLEFT JOIN {$wpdb->terms} AS term USING( term_id )\n\n\t\tWHERE \tmeta.meta_key \t\t= '_order_total'\n\t\tAND \tposts.post_type \t= 'shop_order'\n\t\tAND \tposts.post_status \t= 'publish'\n\t\tAND \ttax.taxonomy\t\t= 'shop_order_status'\n\t\tAND\t\tterm.slug\t\t\tIN ('" . implode("','", apply_filters('woocommerce_reports_order_statuses', array('completed', 'processing', 'on-hold'))) . "')\n\t\tAND\t\tposts.ID\t\t\tIN (\n\t\t\tSELECT post_id FROM {$wpdb->postmeta}\n\t\t\tWHERE \tmeta_key \t\t= '_customer_user'\n\t\t\tAND\t\tmeta_value\t\t> 0\n\t\t)\n\t"));
    $total_customer_sales = $customer_orders->total_sales;
    $total_customer_orders = absint($customer_orders->total_orders);
    $guest_orders = apply_filters('woocommerce_reports_customer_overview_guest_orders', $wpdb->get_row("\n\t\tSELECT SUM(meta.meta_value) AS total_sales, COUNT(posts.ID) AS total_orders FROM {$wpdb->posts} AS posts\n\n\t\tLEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id\n\t\tLEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID\n\t\tLEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )\n\t\tLEFT JOIN {$wpdb->terms} AS term USING( term_id )\n\n\t\tWHERE \tmeta.meta_key \t\t= '_order_total'\n\t\tAND \tposts.post_type \t= 'shop_order'\n\t\tAND \tposts.post_status \t= 'publish'\n\t\tAND \ttax.taxonomy\t\t= 'shop_order_status'\n\t\tAND\t\tterm.slug\t\t\tIN ('" . implode("','", apply_filters('woocommerce_reports_order_statuses', array('completed', 'processing', 'on-hold'))) . "')\n\t\tAND\t\tposts.ID\t\t\tIN (\n\t\t\tSELECT post_id FROM {$wpdb->postmeta}\n\t\t\tWHERE \tmeta_key \t\t= '_customer_user'\n\t\t\tAND\t\tmeta_value\t\t= 0\n\t\t)\n\t"));
    $total_guest_sales = $guest_orders->total_sales;
    $total_guest_orders = absint($guest_orders->total_orders);
    ?>
	<div id="poststuff" class="woocommerce-reports-wrap">
		<div class="woocommerce-reports-sidebar">
			<div class="postbox">
				<h3><span><?php 
    _e('Total customers', 'woocommerce');
    ?>
</span></h3>
				<div class="inside">
					<p class="stat"><?php 
    if ($total_customers > 0) {
        echo $total_customers;
    } else {
        _e('n/a', 'woocommerce');
    }
    ?>
</p>
				</div>
			</div>
			<div class="postbox">
				<h3><span><?php 
    _e('Total customer sales', 'woocommerce');
    ?>
</span></h3>
				<div class="inside">
					<p class="stat"><?php 
    if ($total_customer_sales > 0) {
        echo woocommerce_price($total_customer_sales);
    } else {
        _e('n/a', 'woocommerce');
    }
    ?>
</p>
				</div>
			</div>
			<div class="postbox">
				<h3><span><?php 
    _e('Total guest sales', 'woocommerce');
    ?>
</span></h3>
				<div class="inside">
					<p class="stat"><?php 
    if ($total_guest_sales > 0) {
        echo woocommerce_price($total_guest_sales);
    } else {
        _e('n/a', 'woocommerce');
    }
    ?>
</p>
				</div>
			</div>
			<div class="postbox">
				<h3><span><?php 
    _e('Total customer orders', 'woocommerce');
    ?>
</span></h3>
				<div class="inside">
					<p class="stat"><?php 
    if ($total_customer_orders > 0) {
        echo $total_customer_orders;
    } else {
        _e('n/a', 'woocommerce');
    }
    ?>
</p>
				</div>
			</div>
			<div class="postbox">
				<h3><span><?php 
    _e('Total guest orders', 'woocommerce');
    ?>
</span></h3>
				<div class="inside">
					<p class="stat"><?php 
    if ($total_guest_orders > 0) {
        echo $total_guest_orders;
    } else {
        _e('n/a', 'woocommerce');
    }
    ?>
</p>
				</div>
			</div>
			<div class="postbox">
				<h3><span><?php 
    _e('Average orders per customer', 'woocommerce');
    ?>
</span></h3>
				<div class="inside">
					<p class="stat"><?php 
    if ($total_customer_orders > 0 && $total_customers > 0) {
        echo number_format($total_customer_orders / $total_customers, 2);
    } else {
        _e('n/a', 'woocommerce');
    }
    ?>
</p>
				</div>
			</div>
		</div>
		<div class="woocommerce-reports-main">
			<div class="postbox">
				<h3><span><?php 
    _e('Signups per day', 'woocommerce');
    ?>
</span></h3>
				<div class="inside chart">
					<div id="placeholder" style="width:100%; overflow:hidden; height:568px; position:relative;"></div>
					<div id="cart_legend"></div>
				</div>
			</div>
		</div>
	</div>
	<?php 
    $start_date = strtotime('-30 days', current_time('timestamp'));
    $end_date = current_time('timestamp');
    $signups = array();
    // Blank date ranges to begin
    $count = 0;
    $days = ($end_date - $start_date) / (60 * 60 * 24);
    if ($days == 0) {
        $days = 1;
    }
    while ($count < $days) {
        $time = strtotime(date('Ymd', strtotime('+ ' . $count . ' DAY', $start_date))) . '000';
        $signups[$time] = 0;
        $count++;
    }
    foreach ($customers as $customer) {
        if (strtotime($customer->user_registered) > $start_date) {
            $time = strtotime(date('Ymd', strtotime($customer->user_registered))) . '000';
            if (isset($signups[$time])) {
                $signups[$time]++;
            } else {
                $signups[$time] = 1;
            }
        }
    }
    $signups_array = array();
    foreach ($signups as $key => $count) {
        $signups_array[] = array(esc_js($key), esc_js($count));
    }
    $chart_data = json_encode($signups_array);
    ?>
	<script type="text/javascript">
		jQuery(function(){
			var d = jQuery.parseJSON( '<?php 
    echo $chart_data;
    ?>
' );

			for (var i = 0; i < d.length; ++i) d[i][0] += 60 * 60 * 1000;

			var placeholder = jQuery("#placeholder");

			var plot = jQuery.plot(placeholder, [ { data: d } ], {
				legend: {
					container: jQuery('#cart_legend'),
					noColumns: 2
				},
				series: {
					bars: {
						barWidth: 60 * 60 * 24 * 1000,
						align: "center",
						show: true
					}
				},
				grid: {
					show: true,
					aboveData: false,
					color: '#aaa',
					backgroundColor: '#fff',
					borderWidth: 2,
					borderColor: '#aaa',
					clickable: false,
					hoverable: true,
					markings: weekendAreas
				},
				xaxis: {
					mode: "time",
					timeformat: "%d %b",
					monthNames: <?php 
    echo json_encode(array_values($wp_locale->month_abbrev));
    ?>
,
					tickLength: 1,
					minTickSize: [1, "day"]
				},
				yaxes: [ { position: "right", min: 0, tickSize: 1, tickDecimals: 0 } ],
		   		colors: ["#8a4b75"]
		 	});

		 	placeholder.resize();

			<?php 
    woocommerce_weekend_area_js();
    ?>
		});
	</script>
	<?php 
}
/**
 * Customer overview
 */
function woocommerce_customer_overview()
{
    global $start_date, $end_date, $woocommerce, $wpdb;
    $total_customers = 0;
    $total_customer_sales = 0;
    $total_guest_sales = 0;
    $total_customer_orders = 0;
    $total_guest_orders = 0;
    $users_query = new WP_User_Query(array('fields' => array('user_registered'), 'role' => 'customer'));
    $customers = $users_query->get_results();
    $total_customers = (int) sizeof($customers);
    $args = array('numberposts' => -1, 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'shop_order', 'post_status' => 'publish', 'tax_query' => array(array('taxonomy' => 'shop_order_status', 'terms' => array('completed', 'processing', 'on-hold'), 'field' => 'slug', 'operator' => 'IN')));
    $orders = get_posts($args);
    foreach ($orders as $order) {
        if (get_post_meta($order->ID, '_customer_user', true) > 0) {
            $total_customer_sales += get_post_meta($order->ID, '_order_total', true);
            $total_customer_orders++;
        } else {
            $total_guest_sales += get_post_meta($order->ID, '_order_total', true);
            $total_guest_orders++;
        }
    }
    ?>
	<div id="poststuff" class="woocommerce-reports-wrap">
		<div class="woocommerce-reports-sidebar">
			<div class="postbox">
				<h3><span><?php 
    _e('Total customers', 'woothemes');
    ?>
</span></h3>
				<div class="inside">
					<p class="stat"><?php 
    if ($total_customers > 0) {
        echo $total_customers;
    } else {
        _e('n/a', 'woothemes');
    }
    ?>
</p>
				</div>
			</div>
			<div class="postbox">
				<h3><span><?php 
    _e('Total customer sales', 'woothemes');
    ?>
</span></h3>
				<div class="inside">
					<p class="stat"><?php 
    if ($total_customer_sales > 0) {
        echo woocommerce_price($total_customer_sales);
    } else {
        _e('n/a', 'woothemes');
    }
    ?>
</p>
				</div>
			</div>
			<div class="postbox">
				<h3><span><?php 
    _e('Total guest sales', 'woothemes');
    ?>
</span></h3>
				<div class="inside">
					<p class="stat"><?php 
    if ($total_guest_sales > 0) {
        echo woocommerce_price($total_guest_sales);
    } else {
        _e('n/a', 'woothemes');
    }
    ?>
</p>
				</div>
			</div>
			<div class="postbox">
				<h3><span><?php 
    _e('Total customer orders', 'woothemes');
    ?>
</span></h3>
				<div class="inside">
					<p class="stat"><?php 
    if ($total_customer_orders > 0) {
        echo $total_customer_orders;
    } else {
        _e('n/a', 'woothemes');
    }
    ?>
</p>
				</div>
			</div>
			<div class="postbox">
				<h3><span><?php 
    _e('Total guest orders', 'woothemes');
    ?>
</span></h3>
				<div class="inside">
					<p class="stat"><?php 
    if ($total_guest_orders > 0) {
        echo $total_guest_orders;
    } else {
        _e('n/a', 'woothemes');
    }
    ?>
</p>
				</div>
			</div>
			<div class="postbox">
				<h3><span><?php 
    _e('Average orders per customer', 'woothemes');
    ?>
</span></h3>
				<div class="inside">
					<p class="stat"><?php 
    if ($total_customer_orders > 0 && $total_customers > 0) {
        echo number_format($total_customer_orders / $total_customers, 2);
    } else {
        _e('n/a', 'woothemes');
    }
    ?>
</p>
				</div>
			</div>
		</div>
		<div class="woocommerce-reports-main">
			<div class="postbox">
				<h3><span><?php 
    _e('Signups per day', 'woothemes');
    ?>
</span></h3>
				<div class="inside chart">
					<div id="placeholder" style="width:100%; overflow:hidden; height:568px; position:relative;"></div>
				</div>
			</div>
		</div>
	</div>
	<?php 
    $start_date = strtotime('-30 days', current_time('timestamp'));
    $end_date = current_time('timestamp');
    $signups = array();
    // Blank date ranges to begin
    $count = 0;
    $days = ($end_date - $start_date) / (60 * 60 * 24);
    if ($days == 0) {
        $days = 1;
    }
    while ($count < $days) {
        $time = strtotime(date('Ymd', strtotime('+ ' . $count . ' DAY', $start_date))) . '000';
        $signups[$time] = 0;
        $count++;
    }
    foreach ($customers as $customer) {
        if (strtotime($customer->user_registered) > $start_date) {
            $time = strtotime(date('Ymd', strtotime($customer->user_registered))) . '000';
            if (isset($signups[$time])) {
                $signups[$time]++;
            } else {
                $signups[$time] = 1;
            }
        }
    }
    $signups_array = array();
    foreach ($signups as $key => $count) {
        $signups_array[] = array($key, $count);
    }
    $chart_data = json_encode($signups_array);
    ?>
	<script type="text/javascript">
		jQuery(function(){
			var d = jQuery.parseJSON( '<?php 
    echo $chart_data;
    ?>
' );

			for (var i = 0; i < d.length; ++i) d[i][0] += 60 * 60 * 1000;
			
			var placeholder = jQuery("#placeholder");
			 
			var plot = jQuery.plot(placeholder, [ { data: d } ], {
				series: {
					bars: { 
						barWidth: 60 * 60 * 24 * 1000,
						align: "center",
						show: true 
					}
				},
				grid: {
					show: true,
					aboveData: false,
					color: '#ccc',
					backgroundColor: '#fff',
					borderWidth: 2,
					borderColor: '#ccc',
					clickable: false,
					hoverable: true,
					markings: weekendAreas
				},
				xaxis: { 
					mode: "time",
					timeformat: "%d %b", 
					tickLength: 1,
					minTickSize: [1, "day"]
				},
				yaxes: [ { position: "right", min: 0, tickSize: 1, tickDecimals: 0 } ],
		   		colors: ["#8a4b75"]
		 	});
		 	
		 	placeholder.resize();
	 	
			<?php 
    woocommerce_weekend_area_js();
    ?>
		});
	</script>
	<?php 
}
/**
 *
 * 
 */
function woocommerce_carts_overview()
{
    global $start_date, $end_date, $woocommerce, $wpdb, $wp_locale;
    $current_month = date("j/n/Y", mktime(0, 0, 0, 1, date("m"), date("Y")));
    $start_date = isset($_POST['start_date']) ? $_POST['start_date'] : '';
    $end_date = isset($_POST['end_date']) ? $_POST['end_date'] : '';
    if (!$start_date) {
        $start_date = $current_month;
    }
    if (!$end_date) {
        $end_date = strtotime(date('Ymd', current_time('timestamp')));
    }
    $start_date = strtotime($start_date);
    $end_date = strtotime($end_date);
    //$start_date = strtotime(date('Ymd', strtotime( date('Ym', current_time('timestamp')).'01' )));
    $end_date = strtotime(date('Ymd', current_time('timestamp')));
    // Get orders to display in widget
    $args = array('numberposts' => -1, 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'carts', 'post_status' => 'publish', 'suppress_filters' => false, 'tax_query' => array(array('taxonomy' => 'shop_cart_status', 'terms' => apply_filters('woocommerce_reports_cart_statuses', array('open')), 'field' => 'slug', 'operator' => 'IN')));
    add_filter('posts_where', 'carts_abandoned_within_range');
    $open_carts = get_posts($args);
    $args = array('numberposts' => -1, 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'carts', 'post_status' => 'publish', 'suppress_filters' => false, 'tax_query' => array(array('taxonomy' => 'shop_cart_status', 'terms' => apply_filters('woocommerce_reports_cart_statuses', array('converted')), 'field' => 'slug', 'operator' => 'IN')));
    $converted_carts = get_posts($args);
    $converted_counts = array();
    $updated_counts = array();
    $total_converted = 0;
    $total_updated = 0;
    // Blank date ranges to begin
    $count = 0;
    $days = ($end_date - $start_date) / (60 * 60 * 24);
    if ($days == 0) {
        $days = 1;
    }
    while ($count < $days + 1) {
        $time = strtotime(date('Ymd', strtotime('+ ' . $count . ' DAY', $start_date))) . '000';
        $converted_counts[$time] = 0;
        $updated_counts[$time] = 0;
        $count++;
    }
    if ($converted_carts) {
        foreach ($converted_carts as $converted_cart) {
            $time = strtotime(date('Ymd', strtotime($converted_cart->post_date))) . '000';
            if (isset($converted_counts[$time])) {
                $converted_counts[$time]++;
                $total_converted++;
            } else {
                $converted_counts[$time] = 1;
            }
        }
    }
    if ($open_carts) {
        foreach ($open_carts as $open_cart) {
            $time = strtotime(date('Ymd', strtotime($open_cart->post_date))) . '000';
            if (isset($updated_counts[$time])) {
                $updated_counts[$time]++;
                $total_updated++;
            } else {
                $updated_counts[$time] = 1;
            }
        }
    }
    remove_filter('posts_where', 'carts_abandoned_within_range');
    /* Script variables */
    $params = array('currency_symbol' => get_woocommerce_currency_symbol(), 'number_of_converted_carts' => __('Converted Carts', 'woocommerce'), 'number_of_updated_carts' => __('Open and Abandoned Carts', 'woocommerce'));
    $converted_counts_array = array();
    foreach ($converted_counts as $key => $count) {
        $converted_counts_array[] = array($key, $count);
    }
    $updated_counts_array = array();
    foreach ($updated_counts as $key => $amount) {
        $updated_counts_array[] = array($key, $amount);
    }
    $cart_data = array('converted_counts' => $converted_counts_array, 'updated_counts' => $updated_counts_array);
    $cart_data_json = json_encode($cart_data);
    ?>
	

<form method="post" action="">
		<p><label for="from"><?php 
    _e('From:', 'woocommerce');
    ?>
</label> <input type="text" name="start_date" id="from" readonly="readonly" value="<?php 
    echo esc_attr(date('Y-m-d', $start_date));
    ?>
" /> <label for="to"><?php 
    _e('To:', 'woocommerce');
    ?>
</label> <input type="text" name="end_date" id="to" readonly="readonly" value="<?php 
    echo esc_attr(date('Y-m-d', $end_date));
    ?>
" /> <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">
<?php 
    $link = add_query_arg(array('post_type' => 'carts', 'mv' => 'OandA', 'start_date' => date('Y-m-d', time() - 60 * 60 * 24 * $days), 'end_date' => date('Y-m-d', time())), get_admin_url(null, 'edit.php'));
    $num_updated = '<a style = "color:#333;" href="' . $link . '">' . $total_updated . '</a>';
    ?>
				<h3><span><?php 
    _e('Open & Abandoned Carts In Range', 'woocommerce');
    ?>
</span></h3>
				<div class="inside">
					<p class="stat"><?php 
    if ($total_updated != '') {
        echo $num_updated . ' Carts';
    } else {
        _e('n/a', 'woocommerce');
    }
    ?>
</p>
				</div>
			</div>
			<div class="postbox">
<?php 
    $link = add_query_arg(array('post_type' => 'carts', 'mv' => 'Converted', 'start_date' => date('Y-m-d', time() - 60 * 60 * 24 * $days), 'end_date' => date('Y-m-d', time())), get_admin_url(null, 'edit.php'));
    $num_converted = '<a style = "color:#333;"href="' . $link . '">' . $total_converted . '</a>';
    ?>
				<h3><span><?php 
    _e('Converted Carts In Range', 'woocommerce');
    ?>
</span></h3>
				<div class="inside">
					<p class="stat"><?php 
    sizeof($total_converted);
    if ($total_converted != '') {
        echo $num_converted . ' Carts';
    } else {
        _e('n/a', 'woocommerce');
    }
    ?>
</p>
				</div>
			</div>

		</div>
		<div class="woocommerce-reports-main">
			<div class="postbox">
				<h3><span><?php 
    _e('Open & Abandoned Carts vs. Converted Carts', 'woocommerce');
    ?>
</span></h3>
				<div class="inside chart">
					<div id="placeholder" style="width:100%; overflow:hidden; height:568px; position:relative;"></div>
				</div>
			</div>
		</div>
	</div>
	<?php 
    ?>
	
		<script type="text/javascript">
		jQuery(function(){
			var cart_data = jQuery.parseJSON( '<?php 
    echo $cart_data_json;
    ?>
' );
		
			var d = cart_data.converted_counts;
		    var d2 = cart_data.updated_counts;
			
			for (var i = 0; i < d.length; ++i) d[i][0] += 60 * 60 * 1000;
		    for (var i = 0; i < d2.length; ++i) d2[i][0] += 60 * 60 * 1000;
			
			var placeholder = jQuery("#placeholder");
			 
			var plot = jQuery.plot(placeholder, [ { label: "<?php 
    echo esc_js(__('Converted Carts', 'woocommerce'));
    ?>
", data: d }, { label: "<?php 
    echo esc_js(__('Open & Abandoned Carts', 'woocommerce'));
    ?>
", data: d2, yaxis: 1 } ], {
				 series: {
            	stack:true,
                lines: {
                	fill: true,
                    show: !0
                },
                points: {
                    show: 0
                }
            },
				grid: {
					show: true,
					aboveData: false,
					color: '#ccc',
					backgroundColor: '#fff',
					borderWidth: 2,
					borderColor: '#ccc',
					clickable: false,
					hoverable: true,
					markings: weekendAreas
				},
				xaxis: { 
					mode: "time",
					timeformat: "%d %b", 
					monthNames: <?php 
    echo json_encode(array_values($wp_locale->month_abbrev));
    ?>
,
					tickLength: 1,
					minTickSize: [1, "day"]
				},
				yaxes: [{
				position: "right",
                min: 0,
                tickSize: 10,
                tickDecimals: 0
            }, {
                position: "right",
                min: 0,
                tickDecimals: 2
            }],
            
            colors: [ "green","red"]
		 	});
		 	
		 	placeholder.resize();
	 	
			<?php 
    woocommerce_weekend_area_js();
    ?>
			<?php 
    woocommerce_tooltip_js_carts();
    ?>
			<?php 
    woocommerce_datepicker_js();
    ?>
		});
	</script>
<?php 
    wp_enqueue_script('carts-flot-carts-resize', get_bloginfo('url') . '/wp-content/plugins/woocommerce-cart-reports/assets/js/jquery.flot.resize.js');
    wp_enqueue_script('flot-stack', get_bloginfo('url') . '/wp-content/plugins/woocommerce-cart-reports/assets/js/jquery.flot.stack.min.js');
}
function wcaf_report_daily()
{
    global $start_date, $end_date, $woocommerce, $wpdb;
    $start_date = isset($_POST['start_date']) ? $_POST['start_date'] : '';
    $end_date = isset($_POST['end_date']) ? $_POST['end_date'] : '';
    if (!$start_date) {
        $start_date = date('Ymd', strtotime(date('Ym', current_time('timestamp')) . '01'));
    }
    if (!$end_date) {
        $end_date = date('Ymd', current_time('timestamp'));
    }
    $start_date = strtotime($start_date);
    $end_date = strtotime($end_date);
    $amounts_deposited = 0;
    $num_deposits = 0;
    // Get orders to display in widget
    //add_filter( 'posts_where', 'orders_within_range' );
    if (version_compare(WC_VERSION, '2.2.0', '<')) {
        $args = array('numberposts' => -1, 'orderby' => 'post_date', 'order' => 'ASC', 'post_type' => 'shop_order', 'suppress_filters' => 0, 'meta_query' => array(array('key' => '_funds_deposited', 'value' => '1')), 'tax_query' => array(array('taxonomy' => 'shop_order_status', 'terms' => array('completed', 'processing', 'on-hold'), 'field' => 'slug', 'operator' => 'IN')));
    } else {
        $args = array('numberposts' => -1, 'orderby' => 'post_date', 'order' => 'ASC', 'post_type' => 'shop_order', 'post_status' => array('wc-completed', 'wc-processing', 'wc-on-hold'), 'suppress_filters' => 0, 'meta_query' => array(array('key' => '_funds_deposited', 'value' => '1')));
    }
    $orders = get_posts($args);
    $deposit_counts = array();
    $deposit_amounts = array();
    // Blank date ranges to begin
    $count = 0;
    $days = ($end_date - $start_date) / (60 * 60 * 24);
    if ($days == 0) {
        $days = 1;
    }
    while ($count < $days) {
        $time = strtotime(date('Ymd', strtotime('+ ' . $count . ' DAY', $start_date))) . '000';
        $deposit_counts[$time] = 0;
        $deposit_amounts[$time] = 0;
        $count++;
    }
    if ($orders) {
        foreach ($orders as $order) {
            $order_obj = new WC_Order($order->ID);
            $order_items_array = $order_obj->get_items();
            $time = strtotime(date('Ymd', strtotime($order->post_date))) . '000';
            foreach ($order_items_array as $item) {
                $item_id = isset($item['product_id']) ? $item['product_id'] : $item['id'];
                $product = X3M_AccountFunds::get_product($item_id);
                $is_deposit = get_post_meta($item_id, '_is_deposit', true);
                if ($is_deposit == 'yes') {
                    $num_deposits++;
                    $amounts_deposited += $product->get_price() * $item['qty'];
                    if (isset($deposit_counts[$time])) {
                        $deposit_counts[$time]++;
                    } else {
                        $deposit_counts[$time] = 1;
                    }
                    if (isset($deposit_amounts[$time])) {
                        $deposit_amounts[$time] = $deposit_amounts[$time] + $product->get_price() * $item['qty'];
                    } else {
                        $deposit_amounts[$time] = $product->get_price() * $item['qty'];
                    }
                }
                continue 2;
            }
        }
    }
    ?>
	<form method="post" action="">
		<p><label for="from"><?php 
    _e('From:', 'woocommerce');
    ?>
</label> <input type="text" name="start_date" id="from" readonly="readonly" value="<?php 
    echo esc_attr(date('Y-m-d', $start_date));
    ?>
" /> <label for="to"><?php 
    _e('To:', 'woocommerce');
    ?>
</label> <input type="text" name="end_date" id="to" readonly="readonly" value="<?php 
    echo esc_attr(date('Y-m-d', $end_date));
    ?>
" /> <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 deposits in range', 'wc_account_funds');
    ?>
</span></h3>
				<div class="inside">
					<p class="stat"><?php 
    if ($amounts_deposited > 0) {
        echo woocommerce_price($amounts_deposited);
    } else {
        _e('n/a', 'woocommerce');
    }
    ?>
</p>
				</div>
			</div>
			<div class="postbox">
				<h3><span><?php 
    _e('Total deposits in range', 'wc_account_funds');
    ?>
</span></h3>
				<div class="inside">
					<p class="stat"><?php 
    if ($num_deposits > 0) {
        echo $num_deposits;
    } else {
        _e('n/a', 'woocommerce');
    }
    ?>
</p>
				</div>
			</div>
			<div class="postbox">
				<h3><span><?php 
    _e('Average deposit in range', 'wc_account_funds');
    ?>
</span></h3>
				<div class="inside">
					<p class="stat"><?php 
    if ($amounts_deposited > 0) {
        echo woocommerce_price($amounts_deposited / $num_deposits);
    } else {
        _e('n/a', 'woocommerce');
    }
    ?>
</p>
				</div>
			</div>
		</div>
		<div class="woocommerce-reports-main">
			<div class="postbox">
				<h3><span><?php 
    _e('Deposits in range', 'wc_account_funds');
    ?>
</span></h3>
				<div class="inside chart">
					<div id="placeholder" style="width:100%; overflow:hidden; height:568px; position:relative;"></div>
				</div>
			</div>
		</div>
	</div>
	<?php 
    $deposit_counts_array = array();
    foreach ($deposit_counts as $key => $count) {
        $deposit_counts_array[] = array($key, $count);
    }
    $deposit_amounts_array = array();
    foreach ($deposit_amounts as $key => $amount) {
        $deposit_amounts_array[] = array($key, $amount);
    }
    $deposit_data = array('deposit_counts' => $deposit_counts_array, 'deposit_amounts' => $deposit_amounts_array);
    $chart_data = json_encode($deposit_data);
    ?>
	<script type="text/javascript">
		jQuery(function(){
			var deposit_data = jQuery.parseJSON( '<?php 
    echo $chart_data;
    ?>
' );
            console.log(deposit_data);
			var d = deposit_data.deposit_counts;
		    var d2 = deposit_data.deposit_amounts;

			for (var i = 0; i < d.length; ++i) d[i][0] += 60 * 60 * 1000;
		    for (var i = 0; i < d2.length; ++i) d2[i][0] += 60 * 60 * 1000;

			var placeholder = jQuery("#placeholder");

			var plot = jQuery.plot(placeholder, [ { label: "Number of deposits", data: d }, { label: "Deposit amount", data: d2, yaxis: 2 } ], {
				series: {
					lines: { show: true },
					points: { show: true }
				},
				grid: {
					show: true,
					aboveData: false,
					color: '#ccc',
					backgroundColor: '#fff',
					borderWidth: 2,
					borderColor: '#ccc',
					clickable: false,
					hoverable: true,
					markings: weekendAreas
				},
				xaxis: {
					mode: "time",
					timeformat: "%d %b",
					tickLength: 1,
					minTickSize: [1, "day"]
				},
				yaxes: [ { min: 0, tickSize: 10, tickDecimals: 0 }, { position: "right", min: 0, tickDecimals: 2 } ],
		   		colors: ["#8a4b75", "#47a03e"]
		 	});

		 	placeholder.resize();

			<?php 
    woocommerce_weekend_area_js();
    ?>
			<?php 
    woocommerce_tooltip_js();
    ?>
			<?php 
    woocommerce_datepicker_js();
    ?>
		});
	</script>
	<?php 
}