Exemplo n.º 1
0
/**
 * Show report graphs
 *
 * @since 1.3
 * @return void
*/
function edd_reports_graph()
{
    // Retrieve the queried dates
    $dates = edd_get_report_dates();
    // Determine graph options
    switch ($dates['range']) {
        case 'today':
        case 'yesterday':
            $day_by_day = true;
            break;
        case 'last_year':
        case 'this_year':
        case 'last_quarter':
        case 'this_quarter':
            $day_by_day = false;
            break;
        case 'other':
            if ($dates['m_end'] - $dates['m_start'] >= 2 || $dates['year_end'] > $dates['year'] && ($dates['m_start'] != '12' && $dates['m_end'] != '1')) {
                $day_by_day = false;
            } else {
                $day_by_day = true;
            }
            break;
        default:
            $day_by_day = true;
            break;
    }
    $earnings_totals = 0.0;
    // Total earnings for time period shown
    $sales_totals = 0;
    // Total sales for time period shown
    $include_taxes = empty($_GET['exclude_taxes']) ? true : false;
    $earnings_data = array();
    $sales_data = array();
    if ($dates['range'] == 'today' || $dates['range'] == 'yesterday') {
        // Hour by hour
        $hour = 1;
        $month = $dates['m_start'];
        while ($hour <= 23) {
            $sales = edd_get_sales_by_date($dates['day'], $month, $dates['year'], $hour);
            $earnings = edd_get_earnings_by_date($dates['day'], $month, $dates['year'], $hour, $include_taxes);
            $sales_totals += $sales;
            $earnings_totals += $earnings;
            $date = mktime($hour, 0, 0, $month, $dates['day'], $dates['year']) * 1000;
            $sales_data[] = array($date, $sales);
            $earnings_data[] = array($date, $earnings);
            $hour++;
        }
    } elseif ($dates['range'] == 'this_week' || $dates['range'] == 'last_week') {
        $num_of_days = cal_days_in_month(CAL_GREGORIAN, $dates['m_start'], $dates['year']);
        $report_dates = array();
        $i = 0;
        while ($i <= 6) {
            if ($dates['day'] + $i <= $num_of_days) {
                $report_dates[$i] = array('day' => (string) $dates['day'] + $i, 'month' => $dates['m_start'], 'year' => $dates['year']);
            } else {
                $report_dates[$i] = array('day' => (string) $i, 'month' => $dates['m_end'], 'year' => $dates['year_end']);
            }
            $i++;
        }
        foreach ($report_dates as $report_date) {
            $sales = edd_get_sales_by_date($report_date['day'], $report_date['month'], $report_date['year']);
            $sales_totals += $sales;
            $earnings = edd_get_earnings_by_date($report_date['day'], $report_date['month'], $report_date['year'], null, $include_taxes);
            $earnings_totals += $earnings;
            $date = mktime(0, 0, 0, $report_date['month'], $report_date['day'], $report_date['year']) * 1000;
            $sales_data[] = array($date, $sales);
            $earnings_data[] = array($date, $earnings);
        }
    } else {
        $y = $dates['year'];
        while ($y <= $dates['year_end']) {
            $last_year = false;
            if ($dates['year'] == $dates['year_end']) {
                $month_start = $dates['m_start'];
                $month_end = $dates['m_end'];
                $last_year = true;
            } elseif ($y == $dates['year']) {
                $month_start = $dates['m_start'];
                $month_end = 12;
            } elseif ($y == $dates['year_end']) {
                $month_start = 1;
                $month_end = $dates['m_end'];
            } else {
                $month_start = 1;
                $month_end = 12;
            }
            $i = $month_start;
            while ($i <= $month_end) {
                if ($day_by_day) {
                    $d = $dates['day'];
                    if ($i == $month_end) {
                        $num_of_days = $dates['day_end'];
                        if ($month_start < $month_end) {
                            $d = 1;
                        }
                    } else {
                        $num_of_days = cal_days_in_month(CAL_GREGORIAN, $i, $y);
                    }
                    while ($d <= $num_of_days) {
                        $sales = edd_get_sales_by_date($d, $i, $y);
                        $sales_totals += $sales;
                        $earnings = edd_get_earnings_by_date($d, $i, $y, null, $include_taxes);
                        $earnings_totals += $earnings;
                        $date = mktime(0, 0, 0, $i, $d, $y) * 1000;
                        $sales_data[] = array($date, $sales);
                        $earnings_data[] = array($date, $earnings);
                        $d++;
                    }
                } else {
                    $sales = edd_get_sales_by_date(null, $i, $y);
                    $sales_totals += $sales;
                    $earnings = edd_get_earnings_by_date(null, $i, $y, null, $include_taxes);
                    $earnings_totals += $earnings;
                    if ($i == $month_end && $last_year) {
                        $num_of_days = cal_days_in_month(CAL_GREGORIAN, $i, $y);
                    } else {
                        $num_of_days = 1;
                    }
                    $date = mktime(0, 0, 0, $i, $num_of_days, $y) * 1000;
                    $sales_data[] = array($date, $sales);
                    $earnings_data[] = array($date, $earnings);
                }
                $i++;
            }
            $y++;
        }
    }
    $data = array(__('Earnings', 'easy-digital-downloads') => $earnings_data, __('Sales', 'easy-digital-downloads') => $sales_data);
    // start our own output buffer
    ob_start();
    ?>
	<div id="edd-dashboard-widgets-wrap">
		<div class="metabox-holder" style="padding-top: 0;">
			<div class="postbox">
				<h3><span><?php 
    _e('Earnings Over Time', 'easy-digital-downloads');
    ?>
</span></h3>

				<div class="inside">
					<?php 
    edd_reports_graph_controls();
    $graph = new EDD_Graph($data);
    $graph->set('x_mode', 'time');
    $graph->set('multiple_y_axes', true);
    $graph->display();
    if ('this_month' == $dates['range']) {
        $estimated = edd_estimated_monthly_stats($include_taxes);
    }
    ?>

					<p class="edd_graph_totals">
						<strong>
							<?php 
    _e('Total earnings for period shown: ', 'easy-digital-downloads');
    echo edd_currency_filter(edd_format_amount($earnings_totals));
    ?>
						</strong>
						<?php 
    if (!$include_taxes) {
        ?>
							<sup>&dagger;</sup>
						<?php 
    }
    ?>
					</p>
					<p class="edd_graph_totals"><strong><?php 
    _e('Total sales for period shown: ', 'easy-digital-downloads');
    echo edd_format_amount($sales_totals, false);
    ?>
</strong></p>

					<?php 
    if ('this_month' == $dates['range']) {
        ?>
						<p class="edd_graph_totals">
							<strong>
								<?php 
        _e('Estimated monthly earnings: ', 'easy-digital-downloads');
        echo edd_currency_filter(edd_format_amount($estimated['earnings']));
        ?>
							</strong>
							<?php 
        if (!$include_taxes) {
            ?>
								<sup>&dagger;</sup>
							<?php 
        }
        ?>
						</p>
						<p class="edd_graph_totals"><strong><?php 
        _e('Estimated monthly sales: ', 'easy-digital-downloads');
        echo edd_format_amount($estimated['sales'], false);
        ?>
</strong></p>
					<?php 
    }
    ?>

					<?php 
    do_action('edd_reports_graph_additional_stats');
    ?>

					<p class="edd_graph_notes">
						<?php 
    if (false === $include_taxes) {
        ?>
							<em><sup>&dagger;</sup> <?php 
        _e('Excludes sales tax.', 'easy-digital-downloads');
        ?>
</em>
						<?php 
    }
    ?>
					</p>

				</div>
			</div>
		</div>
	</div>
	<?php 
    // get output buffer contents and end our own buffer
    $output = ob_get_contents();
    ob_end_clean();
    echo $output;
}
/**
 * Show report graphs
 *
 * @since 1.3
 * @return void
*/
function edd_reports_graph()
{
    // Retrieve the queried dates
    $dates = edd_get_report_dates();
    // Determine graph options
    switch ($dates['range']) {
        case 'today':
        case 'yesterday':
            $day_by_day = true;
            break;
        case 'last_year':
        case 'this_year':
            $day_by_day = false;
            break;
        case 'last_quarter':
        case 'this_quarter':
            $day_by_day = true;
            break;
        case 'other':
            if ($dates['m_start'] == 12 && $dates['m_end'] == 1) {
                $day_by_day = true;
            } elseif ($dates['m_end'] - $dates['m_start'] >= 3 || $dates['year_end'] > $dates['year'] && $dates['m_start'] - $dates['m_end'] != 10) {
                $day_by_day = false;
            } else {
                $day_by_day = true;
            }
            break;
        default:
            $day_by_day = true;
            break;
    }
    $earnings_totals = 0.0;
    // Total earnings for time period shown
    $sales_totals = 0;
    // Total sales for time period shown
    $include_taxes = empty($_GET['exclude_taxes']) ? true : false;
    if ($dates['range'] == 'today' || $dates['range'] == 'yesterday') {
        // Hour by hour
        $hour = 0;
        $month = $dates['m_start'];
        $i = 0;
        $start = $dates['year'] . '-' . $dates['m_start'] . '-' . $dates['day'];
        $end = $dates['year_end'] . '-' . $dates['m_end'] . '-' . $dates['day_end'];
        $sales = EDD()->payment_stats->get_sales_by_range($dates['range'], true, $start, $end);
        while ($hour <= 23) {
            $date = mktime($hour, 0, 0, $month, $dates['day'], $dates['year']) * 1000;
            $earnings = edd_get_earnings_by_date($dates['day'], $month, $dates['year'], $hour, $include_taxes);
            $earnings_totals += $earnings;
            $earnings_data[] = array($date, $earnings);
            if (isset($sales[$i]) && $sales[$i]['h'] == $hour) {
                $sales_data[] = array($date, $sales[$i]['count']);
                $sales_totals += $sales[$i]['count'];
                $i++;
            } else {
                $sales_data[] = array($date, 0);
            }
            $hour++;
        }
    } elseif ($dates['range'] == 'this_week' || $dates['range'] == 'last_week') {
        $num_of_days = cal_days_in_month(CAL_GREGORIAN, $dates['m_start'], $dates['year']);
        $report_dates = array();
        $i = 0;
        while ($i <= 6) {
            if ($dates['day'] + $i <= $num_of_days) {
                $report_dates[$i] = array('day' => (string) $dates['day'] + $i, 'month' => $dates['m_start'], 'year' => $dates['year']);
            } else {
                $report_dates[$i] = array('day' => (string) $i, 'month' => $dates['m_end'], 'year' => $dates['year_end']);
            }
            $i++;
        }
        $start_date = $report_dates[0];
        $end_date = end($report_dates);
        $sales = EDD()->payment_stats->get_sales_by_range($dates['range'], true, $start_date['year'] . '-' . date('m', strtotime($start_date['month'])) . '-' . date('d', strtotime($start_date['day'])), $end_date['year'] . '-' . date('m', strtotime($end_date['month'])) . '-' . date('d', strtotime($end_date['day'])));
        $i = 0;
        foreach ($report_dates as $report_date) {
            $date = mktime(0, 0, 0, $report_date['month'], $report_date['day'], $report_date['year']) * 1000;
            if ($report_date['day'] == $sales[$i]['d'] && $report_date['month'] == $sales[$i]['m'] && $report_date['year'] == $sales[$i]['y']) {
                $sales_data[] = array($date, $sales[$i]['count']);
                $sales_totals += $sales[$i]['count'];
                $i++;
            } else {
                $sales_data[] = array($date, 0);
            }
            $earnings = edd_get_earnings_by_date($report_date['day'], $report_date['month'], $report_date['year'], null, $include_taxes);
            $earnings_totals += $earnings;
            $earnings_data[] = array($date, $earnings);
        }
    } else {
        if (cal_days_in_month(CAL_GREGORIAN, $dates['m_start'], $dates['year']) < $dates['day']) {
            $next_day = mktime(0, 0, 0, $dates['m_start'] + 1, 1, $dates['year']);
            $day = date('d', $next_day);
            $month = date('m', $next_day);
            $year = date('Y', $next_day);
            $date_start = $year . '-' . $month . '-' . $day;
        } else {
            $date_start = $dates['year'] . '-' . $dates['m_start'] . '-' . $dates['day'];
        }
        if (cal_days_in_month(CAL_GREGORIAN, $dates['m_end'], $dates['year']) < $dates['day_end']) {
            $date_end = $dates['year_end'] . '-' . $dates['m_end'] . '-' . cal_days_in_month(CAL_GREGORIAN, $dates['m_end'], $dates['year']);
        } else {
            $date_end = $dates['year_end'] . '-' . $dates['m_end'] . '-' . $dates['day_end'];
        }
        $sales = EDD()->payment_stats->get_sales_by_range($dates['range'], $day_by_day, $date_start, $date_end);
        $y = $dates['year'];
        $temp_data = array('sales' => array(), 'earnings' => array());
        foreach ($sales as $sale) {
            if ($day_by_day) {
                $temp_data['sales'][$sale['y']][$sale['m']][$sale['d']] = $sale['count'];
            } else {
                $temp_data['sales'][$sale['y']][$sale['m']] = $sale['count'];
            }
            $sales_totals += $sale['count'];
        }
        while ($dates['range'] !== 'other' && $day_by_day && strtotime($date_start) <= strtotime($date_end)) {
            $d = date('d', strtotime($date_start));
            $m = date('m', strtotime($date_start));
            $y = date('Y', strtotime($date_start));
            if (!isset($temp_data['sales'][$y][$m][$d])) {
                $temp_data['sales'][$y][$m][$d] = 0;
            }
            $date_start = date('Y-m-d', strtotime('+1 day', strtotime($date_start)));
        }
        while ($dates['range'] !== 'other' && !$day_by_day && strtotime($date_start) <= strtotime($date_end)) {
            $m = date('m', strtotime($date_start));
            $y = date('Y', strtotime($date_start));
            if (!isset($temp_data['sales'][$y][$m])) {
                $temp_data['sales'][$y][$m] = 0;
            }
            $date_start = date('Y-m', strtotime('+1 month', strtotime($date_start)));
        }
        if (cal_days_in_month(CAL_GREGORIAN, $dates['m_start'], $dates['year']) < $dates['day']) {
            $next_day = mktime(0, 0, 0, $dates['m_start'] + 1, 1, $dates['year']);
            $day = date('d', $next_day);
            $month = date('m', $next_day);
            $year = date('Y', $next_day);
            $date_start = $year . '-' . $month . '-' . $day;
        } else {
            $date_start = $dates['year'] . '-' . $dates['m_start'] . '-' . $dates['day'];
        }
        while (strtotime($date_start) <= strtotime($date_end)) {
            $m = date('m', strtotime($date_start));
            $y = date('Y', strtotime($date_start));
            $d = date('d', strtotime($date_start));
            $earnings = edd_get_earnings_by_date($d, $m, $y, null, $include_taxes);
            $earnings_totals += $earnings;
            $temp_data['earnings'][$y][$m][$d] = $earnings;
            $date_start = date('Y-m-d', strtotime('+1 day', strtotime($date_start)));
        }
        $sales_data = array();
        $earnings_data = array();
        // When using 3 months or smaller as the custom range, show each day individually on the graph
        if ($day_by_day) {
            foreach ($temp_data['sales'] as $year => $months) {
                foreach ($months as $month => $days) {
                    foreach ($days as $day => $count) {
                        $date = mktime(0, 0, 0, $month, $day, $year) * 1000;
                        $sales_data[] = array($date, $count);
                    }
                }
            }
            // Sort dates in ascending order
            foreach ($sales_data as $key => $value) {
                $timestamps[$key] = $value[0];
            }
            if (!empty($timestamps)) {
                array_multisort($timestamps, SORT_ASC, $sales_data);
            }
            foreach ($temp_data['earnings'] as $year => $months) {
                foreach ($months as $month => $days) {
                    foreach ($days as $day => $earnings) {
                        $date = mktime(0, 0, 0, $month, $day, $year) * 1000;
                        $earnings_data[] = array($date, $earnings);
                    }
                }
            }
            // When showing more than 3 months of results, group them by month, by the first (except for the last month, group on the last day of the month selected)
        } else {
            foreach ($temp_data['sales'] as $year => $months) {
                $month_keys = array_keys($months);
                $last_month = end($month_keys);
                if ($day_by_day) {
                    foreach ($months as $month => $days) {
                        $day_keys = array_keys($days);
                        $last_day = end($day_keys);
                        $month_keys = array_keys($months);
                        $consolidated_date = $month === end($month_keys) ? cal_days_in_month(CAL_GREGORIAN, $month, $year) : 1;
                        $sales = array_sum($days);
                        $date = mktime(0, 0, 0, $month, $consolidated_date, $year) * 1000;
                        $sales_data[] = array($date, $sales);
                    }
                } else {
                    foreach ($months as $month => $count) {
                        $month_keys = array_keys($months);
                        $consolidated_date = $month === end($month_keys) ? cal_days_in_month(CAL_GREGORIAN, $month, $year) : 1;
                        $date = mktime(0, 0, 0, $month, $consolidated_date, $year) * 1000;
                        $sales_data[] = array($date, $count);
                    }
                }
            }
            // Sort dates in ascending order
            foreach ($sales_data as $key => $value) {
                $timestamps[$key] = $value[0];
            }
            if (!empty($timestamps)) {
                array_multisort($timestamps, SORT_ASC, $sales_data);
            }
            foreach ($temp_data['earnings'] as $year => $months) {
                $month_keys = array_keys($months);
                $last_month = end($month_keys);
                foreach ($months as $month => $days) {
                    $day_keys = array_keys($days);
                    $last_day = end($day_keys);
                    $consolidated_date = $month === $last_month ? $last_day : 1;
                    $earnings = array_sum($days);
                    $date = mktime(0, 0, 0, $month, $consolidated_date, $year) * 1000;
                    $earnings_data[] = array($date, $earnings);
                }
            }
        }
    }
    $data = array(__('Earnings', 'easy-digital-downloads') => $earnings_data, __('Sales', 'easy-digital-downloads') => $sales_data);
    // start our own output buffer
    ob_start();
    ?>
	<div id="edd-dashboard-widgets-wrap">
		<div class="metabox-holder" style="padding-top: 0;">
			<div class="postbox">
				<h3><span><?php 
    _e('Earnings Over Time', 'easy-digital-downloads');
    ?>
</span></h3>

				<div class="inside">
					<?php 
    edd_reports_graph_controls();
    $graph = new EDD_Graph($data);
    $graph->set('x_mode', 'time');
    $graph->set('multiple_y_axes', true);
    $graph->display();
    if (!empty($dates['range']) && 'this_month' == $dates['range']) {
        $estimated = edd_estimated_monthly_stats($include_taxes);
    }
    ?>

					<p class="edd_graph_totals">
						<strong>
							<?php 
    _e('Total earnings for period shown: ', 'easy-digital-downloads');
    echo edd_currency_filter(edd_format_amount($earnings_totals));
    ?>
						</strong>
						<?php 
    if (!$include_taxes) {
        ?>
							<sup>&dagger;</sup>
						<?php 
    }
    ?>
					</p>
					<p class="edd_graph_totals"><strong><?php 
    _e('Total sales for period shown: ', 'easy-digital-downloads');
    echo edd_format_amount($sales_totals, false);
    ?>
</strong></p>

					<?php 
    if (!empty($dates['range']) && 'this_month' == $dates['range']) {
        ?>
						<p class="edd_graph_totals">
							<strong>
								<?php 
        _e('Estimated monthly earnings: ', 'easy-digital-downloads');
        echo edd_currency_filter(edd_format_amount($estimated['earnings']));
        ?>
							</strong>
							<?php 
        if (!$include_taxes) {
            ?>
								<sup>&dagger;</sup>
							<?php 
        }
        ?>
						</p>
						<p class="edd_graph_totals"><strong><?php 
        _e('Estimated monthly sales: ', 'easy-digital-downloads');
        echo edd_format_amount($estimated['sales'], false);
        ?>
</strong></p>
					<?php 
    }
    ?>

					<?php 
    do_action('edd_reports_graph_additional_stats');
    ?>

					<p class="edd_graph_notes">
						<?php 
    if (false === $include_taxes) {
        ?>
							<em><sup>&dagger;</sup> <?php 
        _e('Excludes sales tax.', 'easy-digital-downloads');
        ?>
</em>
						<?php 
    }
    ?>
					</p>

				</div>
			</div>
		</div>
	</div>
	<?php 
    // get output buffer contents and end our own buffer
    $output = ob_get_contents();
    ob_end_clean();
    echo $output;
}
/**
 * Show report graphs
 *
 * @since 1.3
 * @return void
*/
function edd_reports_graph()
{
    // Retrieve the queried dates
    $dates = edd_get_report_dates();
    // Determine graph options
    switch ($dates['range']) {
        case 'today':
            $time_format = '%d/%b';
            $tick_size = 'hour';
            $day_by_day = true;
            break;
        case 'last_year':
            $time_format = '%b';
            $tick_size = 'month';
            $day_by_day = false;
            break;
        case 'this_year':
            $time_format = '%b';
            $tick_size = 'month';
            $day_by_day = false;
            break;
        case 'last_quarter':
            $time_format = '%b';
            $tick_size = 'month';
            $day_by_day = false;
            break;
        case 'this_quarter':
            $time_format = '%b';
            $tick_size = 'month';
            $day_by_day = false;
            break;
        case 'other':
            if ($dates['m_end'] - $dates['m_start'] >= 2) {
                $time_format = '%b';
                $tick_size = 'month';
                $day_by_day = false;
            } else {
                $time_format = '%d/%b';
                $tick_size = 'day';
                $day_by_day = true;
            }
            break;
        default:
            $time_format = '%d/%b';
            // Show days by default
            $tick_size = 'day';
            // Default graph interval
            $day_by_day = true;
            break;
    }
    $time_format = apply_filters('edd_graph_timeformat', $time_format);
    $tick_size = apply_filters('edd_graph_ticksize', $tick_size);
    $totals = (double) 0.0;
    // Total earnings for time period shown
    $sales_totals = 0;
    // Total sales for time period shown
    ob_start();
    ?>
	<script type="text/javascript">
	   jQuery( document ).ready( function($) {
	   		$.plot(
	   			$("#edd_monthly_stats"),
	   			[{
   					data: [
	   					<?php 
    if ($dates['range'] == 'today') {
        // Hour by hour
        $hour = 1;
        $month = date('n');
        while ($hour <= 23) {
            $sales = edd_get_sales_by_date($dates['day'], $month, $dates['year'], $hour);
            $sales_totals += $sales;
            $date = mktime($hour, 0, 0, $month, $dates['day'], $dates['year']);
            ?>
								[<?php 
            echo $date * 1000;
            ?>
, <?php 
            echo $sales;
            ?>
],
								<?php 
            $hour++;
        }
    } elseif ($dates['range'] == 'this_week' || $dates['range'] == 'last_week') {
        //Day by day
        $day = $dates['day'];
        $day_end = $dates['day_end'];
        $month = $dates['m_start'];
        while ($day <= $day_end) {
            $sales = edd_get_sales_by_date($day, $month, $dates['year']);
            $sales_totals += $sales;
            $date = mktime(0, 0, 0, $month, $day, $dates['year']);
            ?>
								[<?php 
            echo $date * 1000;
            ?>
, <?php 
            echo $sales;
            ?>
],
								<?php 
            $day++;
        }
    } else {
        $i = $dates['m_start'];
        while ($i <= $dates['m_end']) {
            if ($day_by_day) {
                $num_of_days = cal_days_in_month(CAL_GREGORIAN, $i, $dates['year']);
                $d = 1;
                while ($d <= $num_of_days) {
                    $sales = edd_get_sales_by_date($d, $i, $dates['year']);
                    $sales_totals += $sales;
                    $date = mktime(0, 0, 0, $i, $d, $dates['year']);
                    ?>
										[<?php 
                    echo $date * 1000;
                    ?>
, <?php 
                    echo $sales;
                    ?>
],
									<?php 
                    $d++;
                }
            } else {
                $sales = edd_get_sales_by_date(null, $i, $dates['year']);
                $sales_totals += $sales;
                $date = mktime(0, 0, 0, $i, 1, $dates['year']);
                ?>
									[<?php 
                echo $date * 1000;
                ?>
, <?php 
                echo $sales;
                ?>
],
								<?php 
            }
            $i++;
        }
    }
    ?>
,
	   				],
	   				yaxis: 2,
   					label: "<?php 
    _e('Sales', 'edd');
    ?>
",
   					id: 'sales'
   				},
   				{
   					data: [
	   					<?php 
    if ($dates['range'] == 'today') {
        // Hour by hour
        $hour = 1;
        $month = date('n');
        while ($hour <= 23) {
            $earnings = edd_get_earnings_by_date($dates['day'], $month, $dates['year'], $hour);
            $totals += $earnings;
            $date = mktime($hour, 0, 0, $month, $dates['day'], $dates['year']);
            ?>
								[<?php 
            echo $date * 1000;
            ?>
, <?php 
            echo $earnings;
            ?>
],
								<?php 
            $hour++;
        }
    } elseif ($dates['range'] == 'this_week' || $dates['range'] == 'last_week') {
        //Day by day
        $day = $dates['day'];
        $day_end = $dates['day_end'];
        $month = $dates['m_start'];
        while ($day <= $day_end) {
            $earnings = edd_get_earnings_by_date($day, $month, $dates['year']);
            $totals += $earnings;
            $date = mktime(0, 0, 0, $month, $day, $dates['year']);
            ?>
								[<?php 
            echo $date * 1000;
            ?>
, <?php 
            echo $earnings;
            ?>
],
								<?php 
            $day++;
        }
    } else {
        $i = $dates['m_start'];
        while ($i <= $dates['m_end']) {
            if ($day_by_day) {
                $num_of_days = cal_days_in_month(CAL_GREGORIAN, $i, $dates['year']);
                $d = 1;
                while ($d <= $num_of_days) {
                    $date = mktime(0, 0, 0, $i, $d, $dates['year']);
                    $earnings = edd_get_earnings_by_date($d, $i, $dates['year']);
                    $totals += $earnings;
                    ?>
										[<?php 
                    echo $date * 1000;
                    ?>
, <?php 
                    echo $earnings;
                    ?>
],
									<?php 
                    $d++;
                }
            } else {
                $date = mktime(0, 0, 0, $i, 1, $dates['year']);
                $earnings = edd_get_earnings_by_date(null, $i, $dates['year']);
                $totals += $earnings;
                ?>
									[<?php 
                echo $date * 1000;
                ?>
, <?php 
                echo $earnings;
                ?>
],
								<?php 
            }
            $i++;
        }
    }
    ?>
	   				],
   					label: "<?php 
    _e('Earnings', 'edd');
    ?>
",
   					id: 'earnings'
	   			}],
	   		{
               	series: {
                   lines: { show: true },
                   points: { show: true }
            	},
            	grid: {
           			show: true,
					aboveData: false,
					color: '#ccc',
					backgroundColor: '#fff',
					borderWidth: 2,
					borderColor: '#ccc',
					clickable: false,
					hoverable: true
           		},
            	xaxis: {
	   				mode: "time",
	   				timeFormat: "<?php 
    echo $time_format;
    ?>
",
	   				minTickSize: [1, "<?php 
    echo $tick_size;
    ?>
"]
   				},
   				yaxis: [
   					{ min: 0, tickSize: 1, tickDecimals: 2 },
   					{ min: 0, tickDecimals: 0 }
   				]

            });

	   		function edd_flot_tooltip(x, y, contents) {
		        $('<div id="edd-flot-tooltip">' + contents + '</div>').css( {
		            position: 'absolute',
		            display: 'none',
		            top: y + 5,
		            left: x + 5,
		            border: '1px solid #fdd',
		            padding: '2px',
		            'background-color': '#fee',
		            opacity: 0.80
		        }).appendTo("body").fadeIn(200);
		    }

		    var previousPoint = null;
		    $("#edd_monthly_stats").bind("plothover", function (event, pos, item) {
		        $("#x").text(pos.x.toFixed(2));
		        $("#y").text(pos.y.toFixed(2));
	            if (item) {
	                if (previousPoint != item.dataIndex) {
	                    previousPoint = item.dataIndex;
	                    $("#edd-flot-tooltip").remove();
	                    var x = item.datapoint[0].toFixed(2),
	                        y = item.datapoint[1].toFixed(2);
	                    if( item.series.id == 'earnings' ) {
	                    	if( edd_vars.currency_pos == 'before' ) {
								edd_flot_tooltip( item.pageX, item.pageY, item.series.label + ' ' + edd_vars.currency_sign + y );
	                    	} else {
								edd_flot_tooltip( item.pageX, item.pageY, item.series.label + ' ' + y + edd_vars.currency_sign );
	                    	}
	                    } else {
		                    edd_flot_tooltip( item.pageX, item.pageY, item.series.label + ' ' + y.replace( '.00', '' ) );
	                    }
	                }
	            } else {
	                $("#edd-flot-tooltip").remove();
	                previousPoint = null;
	            }
		    });
	   });
    </script>

	<div class="metabox-holder" style="padding-top: 0;">
		<div class="postbox">
			<h3><span><?php 
    _e('Earnings Over Time', 'edd');
    ?>
</span></h3>

			<div class="inside">
				<?php 
    edd_reports_graph_controls();
    ?>
				<?php 
    $estimated = edd_estimated_monthly_stats();
    ?>
				<div id="edd_monthly_stats" style="height: 300px;"></div>
				<p class="edd_graph_totals"><strong><?php 
    _e('Total earnings for period shown: ', 'edd');
    echo edd_currency_filter(edd_format_amount($totals));
    ?>
</strong></p>
				<p class="edd_graph_totals"><strong><?php 
    _e('Total sales for period shown: ', 'edd');
    echo $sales_totals;
    ?>
</strong></p>
				<p class="edd_graph_totals"><strong><?php 
    _e('Estimated monthly earnings: ', 'edd');
    echo edd_currency_filter(edd_format_amount($estimated['earnings']));
    ?>
</strong></p>
				<p class="edd_graph_totals"><strong><?php 
    _e('Estimated monthly sales: ', 'edd');
    echo $estimated['sales'];
    ?>
</strong></p>
			</div>
		</div>
	</div>
	<?php 
    echo ob_get_clean();
}