/** * 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(); }
/** * Show Commissions Graph * * @access public * @since 1.0 * @return void */ function edd_show_commissions_graph() { // retrieve the queried dates $dates = edd_get_report_dates(); $day_by_day = true; // Determine graph options switch ($dates['range']) { 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) { $day_by_day = false; } break; } $user = isset($_GET['user']) ? absint($_GET['user']) : 0; $total = (double) 0.0; // Total commissions for time period shown ob_start(); ?> <div class="tablenav top"> <div class="alignleft actions"><?php edd_report_views(); ?> </div> </div> <?php $data = array(); if ($dates['range'] == 'today') { // Hour by hour $hour = 1; $month = date('n'); while ($hour <= 23) { $commissions = edd_get_commissions_by_date($dates['day'], $month, $dates['year'], $hour, $user); $total += $commissions; $date = mktime($hour, 0, 0, $month, $dates['day'], $dates['year']); $data[] = array($date * 1000, (int) $commissions); $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) { $commissions = edd_get_commissions_by_date($day, $month, $dates['year'], null, $user); $total += $commissions; $date = mktime(0, 0, 0, $month, $day, $dates['year']); $data[] = array($date * 1000, (int) $commissions); $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']); $commissions = edd_get_commissions_by_date($d, $i, $dates['year'], null, $user); $total += $commissions; $data[] = array($date * 1000, (int) $commissions); $d++; } } else { $date = mktime(0, 0, 0, $i, 1, $dates['year']); $commissions = edd_get_commissions_by_date(null, $i, $dates['year'], null, $user); $total += $commissions; $data[] = array($date * 1000, (int) $commissions); } $i++; } } $data = array(__('Commissions', 'eddc') => $data); ?> <div class="metabox-holder" style="padding-top: 0;"> <div class="postbox"> <h3><span><?php _e('Commissions Paid Over Time', 'edd'); ?> </span></h3> <div class="inside"> <?php if (!empty($user)) { $user_data = get_userdata($user); ?> <p> <?php printf(__('Showing commissions paid to %s', 'eddc'), $user_data->display_name); ?> – <a href="<?php echo esc_url(remove_query_arg('user')); ?> "><?php _e('clear', 'eddc'); ?> </a> </p> <?php } ?> <?php edd_reports_graph_controls(); $graph = new EDD_Graph($data); $graph->set('x_mode', 'time'); $graph->display(); ?> <p id="edd_graph_totals"><strong><?php _e('Total commissions for period shown: ', 'edd'); echo edd_currency_filter(edd_format_amount($total)); ?> </strong></p> </div> </div> </div> <?php echo ob_get_clean(); }
/** * Given a user id, display a graph of commissions * * @since 3.2 * @param integer $user_id The user id to display commissions graph for * @return string HTML markup of the commissions graph for the user */ function eddc_user_commissions_graph($user_id = 0) { $user_id = empty($user_id) ? get_current_user_id() : $user_id; // If still empty, exit if (empty($user_id)) { return; } $graph = ''; if (eddc_user_has_commissions($user_id)) { include_once EDD_PLUGIN_DIR . 'includes/admin/reporting/class-edd-graph.php'; global $post; $month = !isset($_GET['month']) ? date('n') : absint($_GET['month']); $year = !isset($_GET['year']) ? date('Y') : absint($_GET['year']); $num_of_days = cal_days_in_month(CAL_GREGORIAN, $month, $year); ob_start(); ?> <script> if ( typeof( edd_vars ) === 'undefined' ) { edd_vars = { "currency": "<?php echo edd_get_currency(); ?> ", "currency_sign": "<?php echo edd_currency_filter(""); ?> ", "currency_pos": "<?php echo edd_get_option('currency_position', 'before'); ?> ", "currency_decimals": "<?php echo edd_currency_decimal_filter(); ?> ", }; } </script> <style> .tickLabel { width: 30px; } .legend > table { width: auto; } </style> <div id="eddc-dashboard-graphs"> <h4><?php _e('Commission Stats', 'eddc'); ?> </h4> <form id="edd-graphs-filter" method="get" action="<?php echo get_the_permalink($post->ID); ?> #eddc-dashboard-graphs"> <div class="tablenav top"> <div class="actions"> <?php echo EDD()->html->month_dropdown('month', $month); ?> <?php echo EDD()->html->year_dropdown('year', $year); ?> <input type="hidden" name="edd_action" value="filter_reports" /> <input type="submit" class="button-secondary" value="<?php _e('Filter', 'eddc'); ?> "/> </div> </div> </form> <?php $args = array('user_id' => $user_id, 'number' => -1, 'query_args' => array('date_query' => array('after' => array('year' => $year, 'month' => $month, 'day' => 1), 'before' => array('year' => $year, 'month' => $month, 'day' => $num_of_days), 'inclusive' => true))); $commissions = eddc_get_commissions($args); $grouped_data = array(); if (!empty($commissions)) { foreach ($commissions as $commission) { $key = date('njY', strtotime($commission->post_date)); $commission_meta = get_post_meta($commission->ID, '_edd_commission_info', true); if (!isset($grouped_data[$key])) { $grouped_data[$key] = array(); $grouped_data[$key]['earnings'] = (double) $commission_meta['amount']; $grouped_data[$key]['sales'] = 1; } else { $grouped_data[$key]['earnings'] += (double) $commission_meta['amount']; $grouped_data[$key]['sales']++; } } } $d = 1; while ($d <= $num_of_days) { $key = $month . $d . $year; $date = mktime(0, 0, 0, $month, $d, $year) * 1000; $sales = isset($grouped_data[$key]['sales']) ? $grouped_data[$key]['sales'] : 0; $earnings = isset($grouped_data[$key]['earnings']) ? round($grouped_data[$key]['earnings'], edd_currency_decimal_filter()) : 0; $sales_data[] = array($date, $sales); $earnings_data[] = array($date, $earnings); $d++; } $data = array(__('Earnings', 'edd') => $earnings_data, __('Sales', 'edd') => $sales_data); ?> <div class="inside"> <?php $graph = new EDD_Graph($data); $graph->set('x_mode', 'time'); $graph->set('multiple_y_axes', true); $graph->display(); ?> </div> </div> <?php $graph = apply_filters('edd_user_commissions_graph_display', ob_get_clean()); } return $graph; }
/** * Show license upgrades * * @access public * @since 3.3 * @return void */ function edd_sl_show_upgrades_graph() { if (!current_user_can('view_shop_reports')) { wp_die(__('You do not have permission to view this data', 'edd_sl'), __('Error', 'edd_sl'), array('response' => 401)); } $dates = edd_get_report_dates(); $day_by_day = true; // Determine graph options switch ($dates['range']) { 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) { $day_by_day = false; } break; } $total = (double) 0.0; // Total upgrades value for time period shown ob_start(); ?> <div class="tablenav top"> <div class="alignleft actions"><?php edd_report_views(); ?> </div> </div> <?php $data = array(); if ($dates['range'] == 'today') { // Hour by hour $hour = 1; $month = date('n'); while ($hour <= 23) { $upgrades = edd_sl_get_upgrades_by_date($dates['day'], $month, $dates['year'], $hour); $total += $upgrades['earnings']; $date = mktime($hour, 0, 0, $month, $dates['day'], $dates['year']); $data[] = array($date * 1000, (int) $upgrades['count']); $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) { $upgrades = edd_sl_get_upgrades_by_date($day, $month, $dates['year'], null); $total += $upgrades['earnings']; $date = mktime(0, 0, 0, $month, $day, $dates['year']); $data[] = array($date * 1000, (int) $upgrades['count']); $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']); $upgrades = edd_sl_get_upgrades_by_date($d, $i, $dates['year'], null); $total += $upgrades['earnings']; $data[] = array($date * 1000, (int) $upgrades['count']); $d++; } } else { $date = mktime(0, 0, 0, $i, 1, $dates['year']); $upgrades = edd_sl_get_upgrades_by_date(null, $i, $dates['year'], null); $total += $upgrades['earnings']; $data[] = array($date * 1000, (int) $upgrades['count']); } $i++; } } $data = array(__('License Upgrades', 'edd_sl') => $data); ?> <div class="metabox-holder" style="padding-top: 0;"> <div class="postbox"> <h3><span><?php _e('License Upgrades', 'edd_sl'); ?> </span></h3> <div class="inside"> <?php edd_reports_graph_controls(); $graph = new EDD_Graph($data); $graph->set('x_mode', 'time'); $graph->display(); ?> <p id="edd_graph_totals"> <strong><?php _e('Total earnings from upgrades period shown: ', 'edd_sl'); echo edd_currency_filter(edd_format_amount($total)); ?> </strong> </p> </div> </div> </div> <?php echo ob_get_clean(); }
/** * 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(); }