/** * Draws Chart for PDF Report * * Draws the sales and earnings chart for the PDF report and then retrieves the * URL of that chart to display on the PDF Report * * @since 1.1.4.0 * @uses GoogleChart * @uses GoogleChartData * @uses GoogleChartShapeMarker * @uses GoogleChartTextMarker * @uses GoogleChartAxis * @author Sunny Ratilal * @return string $chart->getUrl() URL for the Google Chart */ function edd_draw_chart_image() { require_once EDD_PLUGIN_DIR . '/includes/libraries/googlechartlib/GoogleChart.php'; require_once EDD_PLUGIN_DIR . '/includes/libraries/googlechartlib/markers/GoogleChartShapeMarker.php'; require_once EDD_PLUGIN_DIR . '/includes/libraries/googlechartlib/markers/GoogleChartTextMarker.php'; $chart = new GoogleChart('lc', 900, 330); $i = 1; $earnings = ""; $sales = ""; while ($i <= 12) { $earnings .= edd_get_earnings_by_date(null, $i, date('Y')) . ","; $sales .= edd_get_sales_by_date(null, $i, date('Y')) . ","; $i++; } $earnings_array = explode(",", $earnings); $sales_array = explode(",", $sales); $i = 0; while ($i <= 11) { if (empty($sales_array[$i])) { $sales_array[$i] = 0; } $i++; } $min_earnings = 0; $max_earnings = max($earnings_array); $earnings_scale = round($max_earnings, -1); $data = new GoogleChartData(array($earnings_array[0], $earnings_array[1], $earnings_array[2], $earnings_array[3], $earnings_array[4], $earnings_array[5], $earnings_array[6], $earnings_array[7], $earnings_array[8], $earnings_array[9], $earnings_array[10], $earnings_array[11])); $data->setLegend(__('Earnings', 'edd')); $data->setColor('1b58a3'); $chart->addData($data); $shape_marker = new GoogleChartShapeMarker(GoogleChartShapeMarker::CIRCLE); $shape_marker->setColor('000000'); $shape_marker->setSize(7); $shape_marker->setBorder(2); $shape_marker->setData($data); $chart->addMarker($shape_marker); $value_marker = new GoogleChartTextMarker(GoogleChartTextMarker::VALUE); $value_marker->setColor('000000'); $value_marker->setData($data); $chart->addMarker($value_marker); $data = new GoogleChartData(array($sales_array[0], $sales_array[1], $sales_array[2], $sales_array[3], $sales_array[4], $sales_array[5], $sales_array[6], $sales_array[7], $sales_array[8], $sales_array[9], $sales_array[10], $sales_array[11])); $data->setLegend(__('Sales', 'edd')); $data->setColor('ff6c1c'); $chart->addData($data); $chart->setTitle(__('Sales and Earnings by Month for all Products', 'edd'), '336699', 18); $chart->setScale(0, $max_earnings); $y_axis = new GoogleChartAxis('y'); $y_axis->setDrawTickMarks(true)->setLabels(array(0, $max_earnings)); $chart->addAxis($y_axis); $x_axis = new GoogleChartAxis('x'); $x_axis->setTickMarks(5); $x_axis->setLabels(array(__('Jan', 'edd'), __('Feb', 'edd'), __('Mar', 'edd'), __('Apr', 'edd'), __('May', 'edd'), __('June', 'edd'), __('July', 'edd'), __('Aug', 'edd'), __('Sept', 'edd'), __('Oct', 'edd'), __('Nov', 'edd'), __('Dec', 'edd'))); $chart->addAxis($x_axis); $shape_marker = new GoogleChartShapeMarker(GoogleChartShapeMarker::CIRCLE); $shape_marker->setSize(6); $shape_marker->setBorder(2); $shape_marker->setData($data); $chart->addMarker($shape_marker); $value_marker = new GoogleChartTextMarker(GoogleChartTextMarker::VALUE); $value_marker->setData($data); $chart->addMarker($value_marker); return $chart->getUrl(); }
/** * Show Sales Per Month Graph * * @access public * @since 1.0 * @return void */ function edd_show_monthly_sales_graph($bgcolor = 'white') { ob_start(); ?> <script type="text/javascript"> google.load("visualization", "1", {packages:["corechart"]}); // sales chart google.setOnLoadCallback(drawSalesChart); function drawSalesChart() { var data = new google.visualization.DataTable(); data.addColumn('string', '<?php _e("Month", "edd"); ?> '); data.addColumn('number', '<?php _e("Sales", "edd"); ?> '); data.addRows([ <?php $i = 1; while ($i <= 12) { ?> ['<?php echo edd_month_num_to_name($i) . ' ' . date("Y"); ?> ', <?php echo edd_get_sales_by_date($i, date('Y')); ?> , ], <?php $i++; } ?> ]); var options = { title: "<?php _e('Sales per month', 'edd'); ?> ", colors:['#a3bcd3'], fontSize: 12, backgroundColor: '#ffffff' }; var chart = new google.visualization.ColumnChart(document.getElementById('monthly_sales_chart_div')); chart.draw(data, options); } </script> <div id="monthly_sales_chart_div"></div> <?php echo ob_get_clean(); }
/** * Process Get Stats API Request * * @author Daniel J Griffiths * @since 1.5 * * @global object $wpdb Used to query the database using the WordPress * * @param array $args Arguments provided by API Request * * @return array */ public function get_stats($args = array()) { $defaults = array('type' => null, 'product' => null, 'date' => null, 'startdate' => null, 'enddate' => null); $args = wp_parse_args($args, $defaults); $dates = $this->get_dates($args); $stats = array(); $earnings = array('earnings' => array()); $sales = array('sales' => array()); $error = array(); if (!user_can($this->user_id, 'view_shop_reports') && !$this->override) { return $stats; } if ($args['type'] == 'sales') { if ($args['product'] == null) { if ($args['date'] == null) { $sales = $this->get_default_sales_stats(); } elseif ($args['date'] === 'range') { // Return sales for a date range // Ensure the end date is later than the start date if ($args['enddate'] < $args['startdate']) { $error['error'] = __('The end date must be later than the start date!', 'easy-digital-downloads'); } // Ensure both the start and end date are specified if (empty($args['startdate']) || empty($args['enddate'])) { $error['error'] = __('Invalid or no date range specified!', 'easy-digital-downloads'); } $total = 0; // Loop through the years $y = $dates['year']; while ($y <= $dates['year_end']) { if ($dates['year'] == $dates['year_end']) { $month_start = $dates['m_start']; $month_end = $dates['m_end']; } elseif ($y == $dates['year'] && $dates['year_end'] > $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 ($i == $dates['m_start']) { $d = $dates['day_start']; } else { $d = 1; } if ($i == $dates['m_end']) { $num_of_days = $dates['day_end']; } else { $num_of_days = cal_days_in_month(CAL_GREGORIAN, $i, $y); } while ($d <= $num_of_days) { $sale_count = edd_get_sales_by_date($d, $i, $y); $date_key = date('Ymd', strtotime($y . '/' . $i . '/' . $d)); if (!isset($sales['sales'][$date_key])) { $sales['sales'][$date_key] = 0; } $sales['sales'][$date_key] += $sale_count; $total += $sale_count; $d++; } $i++; } $y++; } $sales['totals'] = $total; } else { if ($args['date'] == 'this_quarter' || $args['date'] == 'last_quarter') { $sales_count = 0; // Loop through the months $month = $dates['m_start']; while ($month <= $dates['m_end']) { $sales_count += edd_get_sales_by_date(null, $month, $dates['year']); $month++; } $sales['sales'][$args['date']] = $sales_count; } else { $sales['sales'][$args['date']] = edd_get_sales_by_date($dates['day'], $dates['m_start'], $dates['year']); } } } elseif ($args['product'] == 'all') { $products = get_posts(array('post_type' => 'download', 'nopaging' => true)); $i = 0; foreach ($products as $product_info) { $sales['sales'][$i] = array($product_info->post_name => edd_get_download_sales_stats($product_info->ID)); $i++; } } else { if (get_post_type($args['product']) == 'download') { $product_info = get_post($args['product']); $sales['sales'][0] = array($product_info->post_name => edd_get_download_sales_stats($args['product'])); } else { $error['error'] = sprintf(__('Product %s not found!', 'easy-digital-downloads'), $args['product']); } } if (!empty($error)) { return $error; } return $sales; } elseif ($args['type'] == 'earnings') { if ($args['product'] == null) { if ($args['date'] == null) { $earnings = $this->get_default_earnings_stats(); } elseif ($args['date'] === 'range') { // Return sales for a date range // Ensure the end date is later than the start date if ($args['enddate'] < $args['startdate']) { $error['error'] = __('The end date must be later than the start date!', 'easy-digital-downloads'); } // Ensure both the start and end date are specified if (empty($args['startdate']) || empty($args['enddate'])) { $error['error'] = __('Invalid or no date range specified!', 'easy-digital-downloads'); } $total = (double) 0.0; // Loop through the years $y = $dates['year']; if (!isset($earnings['earnings'])) { $earnings['earnings'] = array(); } while ($y <= $dates['year_end']) { if ($dates['year'] == $dates['year_end']) { $month_start = $dates['m_start']; $month_end = $dates['m_end']; } elseif ($y == $dates['year'] && $dates['year_end'] > $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 ($i == $dates['m_start']) { $d = $dates['day_start']; } else { $d = 1; } if ($i == $dates['m_end']) { $num_of_days = $dates['day_end']; } else { $num_of_days = cal_days_in_month(CAL_GREGORIAN, $i, $y); } while ($d <= $num_of_days) { $earnings_stat = edd_get_earnings_by_date($d, $i, $y); $date_key = date('Ymd', strtotime($y . '/' . $i . '/' . $d)); if (!isset($earnings['earnings'][$date_key])) { $earnings['earnings'][$date_key] = 0; } $earnings['earnings'][$date_key] += $earnings_stat; $total += $earnings_stat; $d++; } $i++; } $y++; } $earnings['totals'] = $total; } else { if ($args['date'] == 'this_quarter' || $args['date'] == 'last_quarter') { $earnings_count = (double) 0.0; // Loop through the months $month = $dates['m_start']; while ($month <= $dates['m_end']) { $earnings_count += edd_get_earnings_by_date(null, $month, $dates['year']); $month++; } $earnings['earnings'][$args['date']] = $earnings_count; } else { $earnings['earnings'][$args['date']] = edd_get_earnings_by_date($dates['day'], $dates['m_start'], $dates['year']); } } } elseif ($args['product'] == 'all') { $products = get_posts(array('post_type' => 'download', 'nopaging' => true)); $i = 0; foreach ($products as $product_info) { $earnings['earnings'][$i] = array($product_info->post_name => edd_get_download_earnings_stats($product_info->ID)); $i++; } } else { if (get_post_type($args['product']) == 'download') { $product_info = get_post($args['product']); $earnings['earnings'][0] = array($product_info->post_name => edd_get_download_earnings_stats($args['product'])); } else { $error['error'] = sprintf(__('Product %s not found!', 'easy-digital-downloads'), $args['product']); } } if (!empty($error)) { return $error; } return $earnings; } elseif ($args['type'] == 'customers') { if (version_compare($edd_version, '2.3', '<') || !edd_has_upgrade_completed('upgrade_customer_payments_association')) { global $wpdb; $stats = array(); $count = $wpdb->get_col("SELECT COUNT(DISTINCT meta_value) FROM {$wpdb->postmeta} WHERE meta_key = '_edd_payment_user_email'"); $stats['customers']['total_customers'] = $count[0]; return $stats; } else { $customers = new EDD_DB_Customers(); $stats['customers']['total_customers'] = $customers->count(); return $stats; } } elseif (empty($args['type'])) { $stats = array_merge($stats, $this->get_default_sales_stats()); $stats = array_merge($stats, $this->get_default_earnings_stats()); return array('stats' => $stats); } }
/** * Sales Summary Dashboard Widget * * Builds and renders the Sales Summary dashboard widget. This widget displays * the current month's sales and earnings, total sales and earnings best selling * downloads as well as recent purchases made on your EDD Store. * * @author Sunny Ratilal * @since 1.2.2 * @return void */ function edd_dashboard_sales_widget() { $top_selling_args = array('post_type' => 'download', 'posts_per_page' => 1, 'post_status' => 'publish', 'meta_key' => '_edd_download_sales', 'meta_compare' => '>', 'meta_value' => 0, 'orderby' => 'meta_value_num', 'update_post_term_cache' => false, 'order' => 'DESC'); $top_selling = get_posts($top_selling_args); ?> <div class="edd_dashboard_widget"> <div class="table table_left table_current_month"> <p class="sub"><?php _e('Current Month', 'edd'); ?> </p> <table> <tbody> <tr class="first"> <td class="first b"><?php echo edd_currency_filter(edd_format_amount(edd_get_earnings_by_date(null, date('n'), date('Y')))); ?> </td> <td class="t monthly_earnings"><?php _e('Earnings', 'edd'); ?> </td> </tr> <tr> <?php $monthly_sales = edd_get_sales_by_date(null, date('n'), date('Y')); ?> <td class="first b"><?php echo $monthly_sales; ?> </td> <td class="t monthly_sales"><?php echo _n('Sale', 'Sales', $monthly_sales, 'edd'); ?> </td> </tr> </tbody> </table> <p class="label_heading"><?php _e('Last Month', 'edd'); ?> </p> <?php $previous_month = date('n') == 1 ? 12 : date('n') - 1; $previous_year = $previous_month == 12 ? date('Y') - 1 : date('Y'); ?> <div> <?php echo __('Earnings', 'edd') . ': <span class="edd_price_label">' . edd_currency_filter(edd_format_amount(edd_get_earnings_by_date(null, $previous_month, $previous_year))) . '</span>'; ?> </div> <div> <?php $last_month_sales = edd_get_sales_by_date(null, $previous_month, $previous_year); ?> <?php echo _n('Sale', 'Sales', $last_month_sales, 'edd') . ': ' . '<span class="edd_price_label">' . $last_month_sales . '</span>'; ?> </div> </div> <div class="table table_right table_totals"> <p class="sub"><?php _e('Totals', 'edd'); ?> </p> <table> <tbody> <tr class="first"> <td class="b b-earnings"><?php echo edd_currency_filter(edd_format_amount(edd_get_total_earnings())); ?> </td> <td class="last t earnings"><?php _e('Total Earnings', 'edd'); ?> </td> </tr> <tr> <td class="b b-sales"><?php echo edd_get_total_sales(); ?> </td> <td class="last t sales"><?php _e('Total Sales', 'edd'); ?> </td> </tr> </tbody> </table> <?php if ($top_selling) { foreach ($top_selling as $list) { ?> <p class="lifetime_best_selling label_heading"><?php _e('Lifetime Best Selling', 'edd'); ?> </p> <p><span class="lifetime_best_selling_label"><?php echo edd_get_download_sales_stats($list->ID); ?> </span> <a href="<?php echo get_permalink($list->ID); ?> "><?php echo get_the_title($list->ID); ?> </a></p> <?php } } ?> </div> <div style="clear: both"></div> <?php $payments = edd_get_payments(array('number' => 5, 'mode' => 'live', 'orderby' => 'post_date', 'order' => 'DESC', 'user' => null, 'status' => 'publish', 'meta_key' => null, 'fields' => 'ids')); if ($payments) { ?> <p class="edd_dashboard_widget_subheading"><?php _e('Recent Purchases', 'edd'); ?> </p> <div class="table recent_purchases"> <table> <tbody> <?php foreach ($payments as $payment) { $payment_meta = edd_get_payment_meta($payment); ?> <tr> <td> <?php echo get_the_title($payment); ?> - (<?php echo $payment_meta['email']; ?> ) - <span class="edd_price_label"><?php echo edd_currency_filter(edd_format_amount(edd_get_payment_amount($payment))); ?> </span> - <a href="<?php echo add_query_arg('id', $payment, admin_url('edit.php?post_type=download&page=edd-payment-history&edd-action=view-order-details')); ?> " title="<?php printf(__('Purchase Details for Payment #%s', 'edd'), $payment); ?> "><?php _e('View Order Details', 'edd'); ?> </a> </td> </tr> <?php } // End foreach ?> </tbody> </table> </div> <?php } // End if ?> </div> <?php }
/** * Generate the default sales stats returned by the 'stats' endpoint * * @access private * @since 1.5.3 * @return array default sales statistics */ private function get_default_sales_stats() { // Default sales return $previous_month = date('n') == 1 ? 12 : date('n') - 1; $previous_year = date('n') == 1 ? date('Y') - 1 : date('Y'); $sales['sales']['current_month'] = edd_get_sales_by_date(null, date('n'), date('Y')); $sales['sales']['last_month'] = edd_get_sales_by_date(null, $previous_month, $previous_year); $sales['sales']['totals'] = edd_get_total_sales(); return $sales; }
/** * Sales Summary Dashboard Widget * * @access private * @author Sunny Ratilal * @since 1.2.2 */ function edd_dashboard_sales_widget() { $top_selling_args = array('post_type' => 'download', 'posts_per_page' => 1, 'post_status' => 'publish', 'meta_key' => '_edd_download_sales', 'meta_compare' => '>', 'meta_value' => 0, 'orderby' => 'meta_value_num', 'cache_results' => false, 'update_post_term_cache' => false, 'no_found_rows' => true, 'order' => 'DESC'); $top_selling = get_posts($top_selling_args); ?> <div class="table table_current_month"> <p class="sub"><?php _e('Current Month', 'edd'); ?> </p> <table> <tbody> <tr class="first"> <td class="first b"><?php echo edd_currency_filter(edd_format_amount(edd_get_earnings_by_date(null, date('n'), date('Y')))); ?> </td> <td class="t monthly_earnings"><?php _e('Earnings', 'edd'); ?> </td> </tr> <tr> <?php $monthly_sales = edd_get_sales_by_date(null, date('n'), date('Y')); ?> <td class="first b"><?php echo $monthly_sales; ?> </td> <td class="t monthly_sales"><?php echo _n('Sale', 'Sales', $monthly_sales, 'edd'); ?> </td> </tr> </tbody> </table> <p class="label_heading"><?php _e('Last Month', 'edd'); ?> </p> <div> <?php echo __('Earnings', 'edd') . ': <span class="edd_price_label">' . edd_currency_filter(edd_format_amount(edd_get_earnings_by_date(null, date('n') - 1, date('Y')))) . '</span>'; ?> </div> <div> <?php $last_month_sales = edd_get_sales_by_date(null, date('n') - 1, date('Y')); ?> <?php echo _n('Sale', 'Sales', $last_month_sales, 'edd') . ': ' . '<span class="edd_price_label">' . $last_month_sales . '</span>'; ?> </div> </div> <div class="table table_totals"> <p class="sub"><?php _e('Totals', 'edd'); ?> </p> <table> <tbody> <tr class="first"> <td class="b b-earnings"><?php echo edd_currency_filter(edd_format_amount(edd_get_total_earnings())); ?> </td> <td class="last t earnings"><?php _e('Total Earnings', 'edd'); ?> </td> </tr> <tr> <td class="b b-sales"><?php echo edd_get_total_sales(); ?> </td> <td class="last t sales"><?php _e('Total Sales', 'edd'); ?> </td> </tr> </tbody> </table> <?php if ($top_selling) { foreach ($top_selling as $list) { ?> <p class="lifetime_best_selling label_heading"><?php _e('Lifetime Best Selling', 'edd'); ?> </p> <p><span class="lifetime_best_selling_label"><?php echo edd_get_download_sales_stats($list->ID); ?> </span> <a href="<?php echo get_permalink($list->ID); ?> "><?php echo get_the_title($list->ID); ?> </a></p> <?php } } ?> </div> <div style="clear: both"></div> <?php $payments = edd_get_payments(array('number' => 5, 'mode' => 'live', 'orderby' => 'post_date', 'order' => 'DESC', 'user' => null, 'status' => 'publish', 'meta_key' => null)); if ($payments) { ?> <p class="edd_dashboard_widget_subheading"><?php _e('Recent Purchases', 'edd'); ?> </p> <div class="table recent_purchases"> <table> <tbody> <?php foreach ($payments as $payment) { $payment_meta = edd_get_payment_meta($payment->ID); ?> <tr> <td><?php echo get_the_title($payment->ID); ?> - (<?php echo $payment_meta['email']; ?> ) - <span class="edd_price_label"><?php echo edd_currency_filter(edd_format_amount(edd_get_payment_amount($payment->ID))); ?> </span> - <a href="#TB_inline?width=640&inlineId=purchased-files-<?php echo $payment->ID; ?> " class="thickbox" title="<?php printf(__('Purchase Details for Payment #%s', 'edd'), $payment->ID); ?> "><?php _e('View Order Details', 'edd'); ?> </a> <div id="purchased-files-<?php echo $payment->ID; ?> " style="display:none;"> <?php $cart_items = edd_get_payment_meta_cart_details($payment->ID); if (empty($cart_items) || !$cart_items) { $cart_items = maybe_unserialize($payment_meta['downloads']); } ?> <h4><?php echo _n(__('Purchased File', 'edd'), __('Purchased Files', 'edd'), count($cart_items)); ?> </h4> <ul class="purchased-files-list"> <?php if ($cart_items) { foreach ($cart_items as $key => $cart_item) { echo '<li>'; $id = isset($payment_meta['cart_details']) ? $cart_item['id'] : $cart_item; $price_override = isset($payment_meta['cart_details']) ? $cart_item['price'] : null; $user_info = edd_get_payment_meta_user_info($payment->ID); $price = edd_get_download_final_price($id, $user_info, $price_override); echo '<a href="' . admin_url('post.php?post=' . $id . '&action=edit') . '" target="_blank">' . get_the_title($id) . '</a>'; echo ' - '; if (isset($cart_items[$key]['item_number'])) { $price_options = $cart_items[$key]['item_number']['options']; if (isset($price_options['price_id'])) { echo edd_get_price_option_name($id, $price_options['price_id']); echo ' - '; } } echo edd_currency_filter(edd_format_amount($price)); echo '</li>'; } } ?> </ul> <?php $payment_date = strtotime($payment->post_date); ?> <p><?php echo __('Date and Time:', 'edd') . ' ' . date_i18n(get_option('date_format'), $payment_date) . ' ' . date_i18n(get_option('time_format'), $payment_date); ?> <p><?php echo __('Discount used:', 'edd') . ' '; if (isset($user_info['discount']) && $user_info['discount'] != 'none') { echo $user_info['discount']; } else { _e('none', 'edd'); } ?> <p><?php echo __('Total:', 'edd') . ' ' . edd_currency_filter(edd_format_amount(edd_get_payment_amount($payment->ID))); ?> </p> <div class="purcase-personal-details"> <h4><?php _e('Buyer\'s Personal Details:', 'edd'); ?> </h4> <ul> <li><?php echo __('Name:', 'edd') . ' ' . $user_info['first_name'] . ' ' . $user_info['last_name']; ?> </li> <li><?php echo __('Email:', 'edd') . ' ' . $payment_meta['email']; ?> </li> <?php do_action('edd_payment_personal_details_list', $payment_meta, $user_info); ?> </ul> </div> <?php $gateway = edd_get_payment_gateway($payment->ID); if ($gateway) { ?> <div class="payment-method"> <h4><?php _e('Payment Method:', 'edd'); ?> </h4> <span class="payment-method-name"><?php echo edd_get_gateway_admin_label($gateway); ?> </span> </div> <?php } ?> <div class="purchase-key-wrap"> <h4><?php _e('Purchase Key', 'edd'); ?> </h4> <span class="purchase-key"><?php echo $payment_meta['key']; ?> </span> </div> <p><a id="edd-close-purchase-details" class="button-secondary" onclick="tb_remove();" title="<?php _e('Close', 'edd'); ?> "><?php _e('Close', 'edd'); ?> </a></p> </div> </td> </tr> <?php } // end foreach ?> </tbody> </table> </div> <?php } // end if }
/** * 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>†</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>†</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>†</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 * * @access public * @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 '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 ob_start(); ?> <script type="text/javascript"> jQuery( document ).ready( function($) { $.plot( $("#edd_monthly_stats"), [{ data: [ <?php $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']); ?> [<?php echo $date * 1000; ?> , <?php echo edd_get_sales_by_date($d, $i, $dates['year']); ?> ], <?php $d++; } } else { $date = mktime(0, 0, 0, $i, 1, $dates['year']); ?> [<?php echo $date * 1000; ?> , <?php echo edd_get_sales_by_date(null, $i, $dates['year']); ?> ], <?php } $i++; } ?> , ], yaxis: 2, label: "<?php _e('Sales', 'edd'); ?> ", id: 'sales' }, { data: [ <?php $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(); ?> <div id="edd_monthly_stats" style="height: 300px;"></div> <p id="edd_graph_totals"><strong><?php _e('Total earnings for period shown: ', 'edd'); echo edd_currency_filter(edd_format_amount($totals)); ?> </strong></p> </div> </div> </div> <?php echo ob_get_clean(); }