/**
  * Get the Export Data
  *
  * @access public
  * @since 2.0
  * @return array $data The data for the CSV file
  */
 public function get_data()
 {
     $start_year = isset($_POST['start_year']) ? absint($_POST['start_year']) : date('Y');
     $end_year = isset($_POST['end_year']) ? absint($_POST['end_year']) : date('Y');
     $start_month = isset($_POST['start_month']) ? absint($_POST['start_month']) : date('n');
     $end_month = isset($_POST['end_month']) ? absint($_POST['end_month']) : date('n');
     $data = array();
     $year = $start_year;
     $stats = new EDD_Payment_Stats();
     while ($year <= $end_year) {
         if ($year == $start_year && $year == $end_year) {
             $m1 = $start_month;
             $m2 = $end_month;
         } elseif ($year == $start_year) {
             $m1 = $start_month;
             $m2 = 12;
         } elseif ($year == $end_year) {
             $m1 = 1;
             $m2 = $end_month;
         } else {
             $m1 = 1;
             $m2 = 12;
         }
         while ($m1 <= $m2) {
             $date1 = mktime(0, 0, 0, $m1, 1, $year);
             $date2 = mktime(0, 0, 0, $m1, cal_days_in_month(CAL_GREGORIAN, $m1, $year), $year);
             $data[] = array('date' => date_i18n('F Y', $date1), 'sales' => $stats->get_sales(0, $date1, $date2, array('publish', 'revoked')), 'earnings' => edd_format_amount($stats->get_earnings(0, $date1, $date2)));
             $m1++;
         }
         $year++;
     }
     $data = apply_filters('edd_export_get_data', $data);
     $data = apply_filters('edd_export_get_data_' . $this->export_type, $data);
     return $data;
 }
 /**
  * Tie into the heartbeat and append our stats
  *
  * @access public
  * @since 1.8
  * @return array
  */
 public static function heartbeat_received($response, $data)
 {
     if (!current_user_can('view_shop_reports')) {
         return $response;
         // Only modify heartbeat if current user can view show reports
     }
     // Make sure we only run our query if the edd_heartbeat key is present
     if (isset($data['edd_heartbeat']) && $data['edd_heartbeat'] == 'dashboard_summary') {
         // Instantiate the stats class
         $stats = new EDD_Payment_Stats();
         $earnings = edd_get_total_earnings();
         // Send back the number of complete payments
         $response['edd-total-payments'] = edd_format_amount(edd_get_total_sales(), false);
         $response['edd-total-earnings'] = html_entity_decode(edd_currency_filter(edd_format_amount($earnings)), ENT_COMPAT, 'UTF-8');
         $response['edd-payments-month'] = edd_format_amount($stats->get_sales(0, 'this_month', false, array('publish', 'revoked')), false);
         $response['edd-earnings-month'] = html_entity_decode(edd_currency_filter(edd_format_amount($stats->get_earnings(0, 'this_month'))), ENT_COMPAT, 'UTF-8');
         $response['edd-payments-today'] = edd_format_amount($stats->get_sales(0, 'today', false, array('publish', 'revoked')), false);
         $response['edd-earnings-today'] = html_entity_decode(edd_currency_filter(edd_format_amount($stats->get_earnings(0, 'today'))), ENT_COMPAT, 'UTF-8');
     }
     return $response;
 }
Exemplo n.º 3
0
/**
 * Retrieves estimated monthly earnings and sales
 *
 * @since 1.5
 *
 * @param bool  $include_taxes If the estimated earnings should include taxes
 * @return array
 */
function edd_estimated_monthly_stats($include_taxes = true)
{
    $estimated = get_transient('edd_estimated_monthly_stats' . $include_taxes);
    if (false === $estimated) {
        $estimated = array('earnings' => 0, 'sales' => 0);
        $stats = new EDD_Payment_Stats();
        $to_date_earnings = $stats->get_earnings(0, 'this_month', null, $include_taxes);
        $to_date_sales = $stats->get_sales(0, 'this_month');
        $current_day = date('d', current_time('timestamp'));
        $current_month = date('n', current_time('timestamp'));
        $current_year = date('Y', current_time('timestamp'));
        $days_in_month = cal_days_in_month(CAL_GREGORIAN, $current_month, $current_year);
        $estimated['earnings'] = $to_date_earnings / $current_day * $days_in_month;
        $estimated['sales'] = $to_date_sales / $current_day * $days_in_month;
        // Cache for one day
        set_transient('edd_estimated_monthly_stats' . $include_taxes, $estimated, 86400);
    }
    return maybe_unserialize($estimated);
}
/**
 * Loads the dashboard sales widget via ajax
 *
 * @since 2.1
 * @return void
 */
function edd_load_dashboard_sales_widget()
{
    if (!current_user_can(apply_filters('edd_dashboard_stats_cap', 'view_shop_reports'))) {
        die;
    }
    $stats = new EDD_Payment_Stats();
    ?>
	<div class="edd_dashboard_widget">
		<div class="table table_left table_current_month">
			<table>
				<thead>
					<tr>
						<td colspan="2"><?php 
    _e('Current Month', 'edd');
    ?>
</td>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td class="first t monthly_earnings"><?php 
    _e('Earnings', 'edd');
    ?>
</td>
						<td class="b b-earnings"><?php 
    echo edd_currency_filter(edd_format_amount($stats->get_earnings(0, 'this_month')));
    ?>
</td>
					</tr>
					<tr>
						<?php 
    $monthly_sales = $stats->get_sales(0, 'this_month', false, array('publish', 'revoked'));
    ?>
						<td class="first t monthly_sales"><?php 
    echo _n('Sale', 'Sales', $monthly_sales, 'edd');
    ?>
</td>
						<td class="b b-sales"><?php 
    echo $monthly_sales;
    ?>
</td>
					</tr>
				</tbody>
			</table>
			<table>
				<thead>
					<tr>
						<td colspan="2"><?php 
    _e('Last Month', 'edd');
    ?>
</td>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td class="first t earnings"><?php 
    echo __('Earnings', 'edd');
    ?>
</td>
						<td class="b b-last-month-earnings"><?php 
    echo edd_currency_filter(edd_format_amount($stats->get_earnings(0, 'last_month')));
    ?>
</td>
					</tr>
					<tr>
						<td class="first t sales">
							<?php 
    $last_month_sales = $stats->get_sales(0, 'last_month', false, array('publish', 'revoked'));
    ?>
							<?php 
    echo _n('Sale', 'Sales', $last_month_sales, 'edd');
    ?>
						</td>
						<td class="b b-last-month-sales">
							<?php 
    echo $last_month_sales;
    ?>
						</td>
					</tr>
				</tbody>
			</table>
		</div>
		<div class="table table_right table_today">
			<table>
				<thead>
					<tr>
						<td colspan="2">
							<?php 
    _e('Today', 'edd');
    ?>
						</td>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td class="t sales"><?php 
    _e('Earnings', 'edd');
    ?>
</td>
						<td class="last b b-earnings">
							<?php 
    $earnings_today = $stats->get_earnings(0, 'today', false);
    ?>
							<?php 
    echo edd_currency_filter(edd_format_amount($earnings_today));
    ?>
						</td>
					</tr>
					<tr>
						<td class="t sales">
							<?php 
    _e('Sales', 'edd');
    ?>
						</td>
						<td class="last b b-sales">
							<?php 
    $sales_today = $stats->get_sales(0, 'today', false, array('publish', 'revoked'));
    ?>
							<?php 
    echo edd_format_amount($sales_today, false);
    ?>
						</td>
					</tr>
				</tbody>
			</table>
		</div>
		<div class="table table_right table_totals">
			<table>
				<thead>
					<tr>
						<td colspan="2"><?php 
    _e('Totals', 'edd');
    ?>
</td>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td class="t earnings"><?php 
    _e('Total Earnings', 'edd');
    ?>
</td>
						<td class="last b b-earnings"><?php 
    echo edd_currency_filter(edd_format_amount(edd_get_total_earnings()));
    ?>
</td>
					</tr>
					<tr>
						<td class="t sales"><?php 
    _e('Total Sales', 'edd');
    ?>
</td>
						<td class="last b b-sales"><?php 
    echo edd_format_amount(edd_get_total_sales(), false);
    ?>
</td>
					</tr>
				</tbody>
			</table>
		</div>
		<div style="clear: both"></div>
		<?php 
    do_action('edd_sales_summary_widget_after_stats', $stats);
    ?>
		<?php 
    $p_query = new EDD_Payments_Query(array('number' => 5, 'status' => 'publish'));
    $payments = $p_query->get_payments();
    if ($payments) {
        ?>
		<div class="table recent_purchases">
			<table>
				<thead>
					<tr>
						<td colspan="2">
							<?php 
        _e('Recent Purchases', 'edd');
        ?>
							<a href="<?php 
        echo admin_url('edit.php?post_type=download&page=edd-payment-history');
        ?>
">&nbsp;&ndash;&nbsp;<?php 
        _e('View All', 'edd');
        ?>
</a>
						</td>
					</tr>
				</thead>
				<tbody>
					<?php 
        foreach ($payments as $payment) {
            ?>
						<tr>
							<td class="edd_order_label">
								<a href="<?php 
            echo add_query_arg('id', $payment->ID, admin_url('edit.php?post_type=download&page=edd-payment-history&view=view-order-details'));
            ?>
" title="<?php 
            printf(__('Purchase Details for Payment #%s', 'edd'), $payment->ID);
            ?>
 ">
									<?php 
            echo get_the_title($payment->ID);
            ?>
									&mdash; <?php 
            echo $payment->user_info['email'];
            ?>
								</a>
								<?php 
            if ($payment->user_info['id'] > 0) {
                $user = get_user_by('id', $payment->user_info['id']);
                if ($user) {
                    echo "(" . $user->data->user_login . ")";
                }
            }
            ?>
							</td>
							<td class="edd_order_price">
								<a href="<?php 
            echo add_query_arg('id', $payment->ID, admin_url('edit.php?post_type=download&page=edd-payment-history&view=view-order-details'));
            ?>
" title="<?php 
            printf(__('Purchase Details for Payment #%s', 'edd'), $payment->ID);
            ?>
 ">
									<span class="edd_price_label"><?php 
            echo edd_currency_filter(edd_format_amount($payment->total), edd_get_payment_currency_code($payment->ID));
            ?>
</span>
								</a>
							</td>
						</tr>
						<?php 
        }
        // End foreach
        ?>
				</tbody>
			</table>
		</div>
		<?php 
    }
    // End if
    ?>
		<?php 
    do_action('edd_sales_summary_widget_after_purchases', $payments);
    ?>
	</div>
	<?php 
    die;
}
 /**
  * Get stats for your EDD site
  *
  * ## OPTIONS
  *
  * --product=<product_id>: The ID of a specific product to retrieve stats for, or all
  * --date=[range|this_month|last_month|today|yesterday|this_quarter|last_quarter|this_year|last_year]: A specific date range to retrieve stats for
  * --startdate=<date>: The start date of a date range to retrieve stats for
  * --enddate=<date>: The end date of a date range to retrieve stats for
  *
  * ## EXAMPLES
  *
  * wp edd stats --date=this_month
  * wp edd stats --start-date=01/02/2014 --end-date=02/23/2014
  * wp edd stats --date=last_year
  * wp edd stats --date=last_year --product=15
  */
 public function stats($args, $assoc_args)
 {
     $stats = new EDD_Payment_Stats();
     $date = isset($assoc_args) && array_key_exists('date', $assoc_args) ? $assoc_args['date'] : false;
     $start_date = isset($assoc_args) && array_key_exists('startdate', $assoc_args) ? $assoc_args['startdate'] : false;
     $end_date = isset($assoc_args) && array_key_exists('enddate', $assoc_args) ? $assoc_args['enddate'] : false;
     $download = isset($assoc_args) && array_key_exists('product', $assoc_args) ? $assoc_args['product'] : 0;
     if (!empty($date)) {
         $start_date = $date;
         $end_date = false;
     } elseif (empty($date) && empty($start_date)) {
         $start_date = 'this_month';
         $end_date = false;
     }
     $earnings = $stats->get_earnings($download, $start_date, $end_date);
     $sales = $stats->get_sales($download, $start_date, $end_date);
     WP_CLI::line(sprintf(__('Earnings: %s', 'easy-digital-downloads'), $earnings));
     WP_CLI::line(sprintf(__('Sales: %s', 'easy-digital-downloads'), $sales));
 }
/**
 * Show report graphs of a specific product
 *
 * @since 1.9
 * @return void
*/
function edd_reports_graph_of_download($download_id = 0)
{
    // 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':
            $day_by_day = false;
            break;
        case 'this_year':
            $day_by_day = false;
            break;
        case 'last_quarter':
            $day_by_day = false;
            break;
        case 'this_quarter':
            $day_by_day = false;
            break;
        case 'other':
            if ($dates['m_end'] - $dates['m_start'] >= 2 || $dates['year_end'] > $dates['year']) {
                $day_by_day = false;
            } else {
                $day_by_day = true;
            }
            break;
        default:
            $day_by_day = true;
            break;
    }
    $earnings_totals = (double) 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();
    $stats = new EDD_Payment_Stats();
    if ($dates['range'] == 'today' || $dates['range'] == 'yesterday') {
        // Hour by hour
        $month = $dates['m_start'];
        $hour = 0;
        $minute = 0;
        $second = 0;
        while ($hour <= 23) {
            if ($hour == 23) {
                $minute = $second = 59;
            }
            $date = mktime($hour, $minute, $second, $month, $dates['day'], $dates['year']);
            $date_end = mktime($hour + 1, $minute, $second, $month, $dates['day'], $dates['year']);
            $sales = $stats->get_sales($download_id, $date, $date_end);
            $sales_totals += $sales;
            $earnings = $stats->get_earnings($download_id, $date, $date_end, $include_taxes);
            $earnings_totals += $earnings;
            $sales_data[] = array($date * 1000, $sales);
            $earnings_data[] = array($date * 1000, $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) {
            $date = mktime(0, 0, 0, $report_date['month'], $report_date['day'], $report_date['year']);
            $date_end = mktime(23, 59, 59, $report_date['month'], $report_date['day'], $report_date['year']);
            $sales = $stats->get_sales($download_id, $date, $date_end);
            $sales_totals += $sales;
            $earnings = $stats->get_earnings($download_id, $date, $date_end, $include_taxes);
            $earnings_totals += $earnings;
            $sales_data[] = array($date * 1000, $sales);
            $earnings_data[] = array($date * 1000, $earnings);
        }
    } else {
        $y = $dates['year'];
        $temp_data = array();
        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) {
                $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) {
                    $date = mktime(0, 0, 0, $i, $d, $y);
                    $end_date = mktime(23, 59, 59, $i, $d, $y);
                    $earnings = $stats->get_earnings($download_id, $date, $end_date, $include_taxes);
                    $earnings_totals += $earnings;
                    $sales = $stats->get_sales($download_id, $date, $end_date);
                    $sales_totals += $sales;
                    $temp_data['earnings'][$y][$i][$d] = $earnings;
                    $temp_data['sales'][$y][$i][$d] = $sales;
                    $d++;
                }
                $i++;
            }
            $y++;
        }
        $sales_data = array();
        $earnings_data = array();
        // When using 2 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 => $dates) {
                    foreach ($dates as $day => $sales) {
                        $date = mktime(0, 0, 0, $month, $day, $year) * 1000;
                        $sales_data[] = array($date, $sales);
                    }
                }
            }
            foreach ($temp_data['earnings'] as $year => $months) {
                foreach ($months as $month => $dates) {
                    foreach ($dates as $day => $earnings) {
                        $date = mktime(0, 0, 0, $month, $day, $year) * 1000;
                        $earnings_data[] = array($date, $earnings);
                    }
                }
            }
            // When showing more than 2 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);
                foreach ($months as $month => $days) {
                    $day_keys = array_keys($days);
                    $last_day = end($day_keys);
                    $consolidated_date = $month === $last_month ? $last_day : 1;
                    $sales = array_sum($days);
                    $date = mktime(0, 0, 0, $month, $consolidated_date, $year) * 1000;
                    $sales_data[] = array($date, $sales);
                }
            }
            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);
    ?>
	<div class="metabox-holder" style="padding-top: 0;">
		<div class="postbox">
			<h3><span><?php 
    printf(__('Earnings Over Time for %s', 'easy-digital-downloads'), get_the_title($download_id));
    ?>
</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();
    ?>
				<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></p>
				<p class="edd_graph_totals"><strong><?php 
    _e('Total sales for period shown: ', 'easy-digital-downloads');
    echo $sales_totals;
    ?>
</strong></p>
				<p class="edd_graph_totals"><strong><?php 
    printf(__('Average monthly earnings: %s', 'easy-digital-downloads'), edd_currency_filter(edd_format_amount(edd_get_average_monthly_download_earnings($download_id))));
    ?>
				<p class="edd_graph_totals"><strong><?php 
    printf(__('Average monthly sales: %s', 'easy-digital-downloads'), number_format(edd_get_average_monthly_download_sales($download_id), 0));
    ?>
			</div>
		</div>
	</div>
	<?php 
    echo ob_get_clean();
}
 /**
  * Build all the reports data
  *
  * @access public
  * @since  2.4
  * @return array $reports_data All the data for customer reports
  */
 public function reports_data()
 {
     /*
      * Date filtering
      */
     $dates = edd_get_report_dates();
     if (!empty($dates['year'])) {
         $date = new DateTime();
         $date->setDate($dates['year'], $dates['m_start'], $dates['day']);
         $start_date = $date->format('Y-m-d');
         $date->setDate($dates['year_end'], $dates['m_end'], $dates['day_end']);
         $end_date = $date->format('Y-m-d');
         $cached_report_key = 'edd_earnings_by_category_data' . $start_date . '_' . $end_date;
     } else {
         $start_date = false;
         $end_date = false;
         $cached_report_key = 'edd_earnings_by_category_data';
     }
     $cached_reports = get_transient($cached_report_key);
     if (false !== $cached_reports) {
         $reports_data = $cached_reports;
     } else {
         $reports_data = array();
         $term_args = array('parent' => 0, 'hierarchical' => 0);
         $categories = get_terms('download_category', $term_args);
         foreach ($categories as $category_id => $category) {
             $category_slugs = array($category->slug);
             $child_args = array('parent' => $category->term_id, 'hierarchical' => 0);
             $child_terms = get_terms('download_category', $child_args);
             if (!empty($child_terms)) {
                 foreach ($child_terms as $child_term) {
                     $category_slugs[] = $child_term->slug;
                 }
             }
             $download_args = array('post_type' => 'download', 'posts_per_page' => -1, 'fields' => 'ids', 'tax_query' => array(array('taxonomy' => 'download_category', 'field' => 'slug', 'terms' => $category_slugs)));
             $downloads = get_posts($download_args);
             $sales = 0;
             $earnings = 0.0;
             $avg_sales = 0;
             $avg_earnings = 0.0;
             $payment_stats = new EDD_Payment_Stats();
             foreach ($downloads as $download) {
                 $current_average_sales = $current_sales = $payment_stats->get_sales($download, $start_date, $end_date);
                 $current_average_earnings = $current_earnings = $payment_stats->get_earnings($download, $start_date, $end_date);
                 $release_date = get_post_field('post_date', $download);
                 $diff = abs(current_time('timestamp') - strtotime($release_date));
                 $months = floor($diff / (30 * 60 * 60 * 24));
                 // Number of months since publication
                 if ($months > 0) {
                     $current_average_sales = $current_sales / $months;
                     $current_average_earnings = $current_earnings / $months;
                 }
                 $sales += $current_sales;
                 $earnings += $current_earnings;
                 $avg_sales += $current_average_sales;
                 $avg_earnings += $current_average_earnings;
             }
             $avg_sales = round($avg_sales / count($downloads));
             $avg_earnings = round($avg_earnings / count($downloads), edd_currency_decimal_filter());
             $reports_data[] = array('ID' => $category->term_id, 'label' => $category->name, 'total_sales' => edd_format_amount($sales, false), 'total_sales_raw' => $sales, 'total_earnings' => edd_currency_filter(edd_format_amount($earnings)), 'total_earnings_raw' => $earnings, 'avg_sales' => edd_format_amount($avg_sales, false), 'avg_earnings' => edd_currency_filter(edd_format_amount($avg_earnings)), 'is_child' => false);
             if (!empty($child_terms)) {
                 foreach ($child_terms as $child_term) {
                     $child_args = array('post_type' => 'download', 'posts_per_page' => -1, 'fields' => 'ids', 'tax_query' => array(array('taxonomy' => 'download_category', 'field' => 'slug', 'terms' => $child_term->slug)));
                     $child_downloads = get_posts($child_args);
                     $child_sales = 0;
                     $child_earnings = 0.0;
                     $child_avg_sales = 0;
                     $child_avg_earnings = 0.0;
                     foreach ($child_downloads as $child_download) {
                         $current_average_sales = $current_sales = $payment_stats->get_sales($child_download, $start_date, $end_date);
                         $current_average_earnings = $current_earnings = $payment_stats->get_earnings($child_download, $start_date, $end_date);
                         $release_date = get_post_field('post_date', $child_download);
                         $diff = abs(current_time('timestamp') - strtotime($release_date));
                         $months = floor($diff / (30 * 60 * 60 * 24));
                         // Number of months since publication
                         if ($months > 0) {
                             $current_average_sales = $current_sales / $months;
                             $current_average_earnings = $current_earnings / $months;
                         }
                         $child_sales += $current_sales;
                         $child_earnings += $current_earnings;
                         $child_avg_sales += $current_average_sales;
                         $child_avg_earnings += $current_average_earnings;
                     }
                     $child_avg_sales = round($child_avg_sales / count($child_downloads));
                     $child_avg_earnings = round($child_avg_earnings / count($child_downloads), edd_currency_decimal_filter());
                     $reports_data[] = array('ID' => $child_term->term_id, 'label' => '&#8212; ' . $child_term->name, 'total_sales' => edd_format_amount($child_sales, false), 'total_sales_raw' => $child_sales, 'total_earnings' => edd_currency_filter(edd_format_amount($child_earnings)), 'total_earnings_raw' => $child_earnings, 'avg_sales' => edd_format_amount($child_avg_sales, false), 'avg_earnings' => edd_currency_filter(edd_format_amount($child_avg_earnings)), 'is_child' => true);
                 }
             }
         }
     }
     return $reports_data;
 }
Exemplo n.º 8
0
/**
 * Show report graphs of a specific product
 *
 * @since 1.9
 * @return void
*/
function edd_reports_graph_of_download($download_id = 0)
{
    // 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':
            $day_by_day = false;
            break;
        case 'this_year':
            $day_by_day = false;
            break;
        case 'last_quarter':
            $day_by_day = false;
            break;
        case 'this_quarter':
            $day_by_day = false;
            break;
        case 'other':
            if ($dates['m_end'] - $dates['m_start'] >= 2 || $dates['year_end'] > $dates['year']) {
                $day_by_day = false;
            } else {
                $day_by_day = true;
            }
            break;
        default:
            $day_by_day = true;
            break;
    }
    $earnings_totals = (double) 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();
    $stats = new EDD_Payment_Stats();
    if ($dates['range'] == 'today' || $dates['range'] == 'yesterday') {
        // Hour by hour
        $month = $dates['m_start'];
        $hour = 1;
        $minute = 0;
        $second = 0;
        while ($hour <= 23) {
            if ($hour == 23) {
                $minute = $second = 59;
            }
            $date = mktime($hour, $minute, $second, $month, $dates['day'], $dates['year']);
            $date_end = mktime($hour + 1, $minute, $second, $month, $dates['day'], $dates['year']);
            $sales = $stats->get_sales($download_id, $date, $date_end);
            $sales_totals += $sales;
            $earnings = $stats->get_earnings($download_id, $date, $date_end, $include_taxes);
            $earnings_totals += $earnings;
            $sales_data[] = array($date * 1000, $sales);
            $earnings_data[] = array($date * 1000, $earnings);
            $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) {
            $date = mktime(0, 0, 0, $month, $day, $dates['year']);
            $date_end = mktime(0, 0, 0, $month, $day + 1, $dates['year']);
            $sales = $stats->get_sales($download_id, $date, $date_end);
            $sales_totals += $sales;
            $earnings = $stats->get_earnings($download_id, $date, $date_end, $include_taxes);
            $earnings_totals += $earnings;
            $sales_data[] = array($date * 1000, $sales);
            $earnings_data[] = array($date * 1000, $earnings);
            $day++;
        }
    } 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;
            } else {
                $month_start = 1;
                $month_end = 12;
            }
            $i = $month_start;
            while ($i <= $month_end) {
                if ($day_by_day) {
                    if ($i == $month_end && $last_year) {
                        $num_of_days = $dates['day_end'];
                    } else {
                        $num_of_days = cal_days_in_month(CAL_GREGORIAN, $i, $y);
                    }
                    $d = $dates['day'];
                    while ($d <= $num_of_days) {
                        $date = mktime(0, 0, 0, $i, $d, $y);
                        $end_date = mktime(23, 59, 59, $i, $d, $y);
                        $sales = $stats->get_sales($download_id, $date, $end_date);
                        $sales_totals += $sales;
                        $earnings = $stats->get_earnings($download_id, $date, $end_date, $include_taxes);
                        $earnings_totals += $earnings;
                        $sales_data[] = array($date * 1000, $sales);
                        $earnings_data[] = array($date * 1000, $earnings);
                        $d++;
                    }
                } else {
                    $num_of_days = cal_days_in_month(CAL_GREGORIAN, $i, $y);
                    $date = mktime(0, 0, 0, $i, 1, $y);
                    $end_date = mktime(23, 59, 59, $i, $num_of_days, $y);
                    $sales = $stats->get_sales($download_id, $date, $end_date);
                    $sales_totals += $sales;
                    $earnings = $stats->get_earnings($download_id, $date, $end_date, $include_taxes);
                    $earnings_totals += $earnings;
                    $sales_data[] = array($date * 1000, $sales);
                    $earnings_data[] = array($date * 1000, $earnings);
                }
                $i++;
            }
            $y++;
        }
    }
    $data = array(__('Earnings', 'easy-digital-downloads') => $earnings_data, __('Sales', 'easy-digital-downloads') => $sales_data);
    ?>
	<div class="metabox-holder" style="padding-top: 0;">
		<div class="postbox">
			<h3><span><?php 
    printf(__('Earnings Over Time for %s', 'easy-digital-downloads'), get_the_title($download_id));
    ?>
</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();
    ?>
				<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></p>
				<p class="edd_graph_totals"><strong><?php 
    _e('Total sales for period shown: ', 'easy-digital-downloads');
    echo $sales_totals;
    ?>
</strong></p>
				<p class="edd_graph_totals"><strong><?php 
    printf(__('Average monthly earnings: %s', 'easy-digital-downloads'), edd_currency_filter(edd_format_amount(edd_get_average_monthly_download_earnings($download_id))));
    ?>
				<p class="edd_graph_totals"><strong><?php 
    printf(__('Average monthly sales: %s', 'easy-digital-downloads'), number_format(edd_get_average_monthly_download_sales($download_id), 0));
    ?>
			</div>
		</div>
	</div>
	<?php 
    echo ob_get_clean();
}