You appear to be a bot. Output may be restricted
Description
Retrieve sales stats based on range provided (used for Reporting)
Usage
$array = EDD_Payment_Stats::get_earnings_by_range( $range, $day_by_day, $start_date, $end_date, $include_taxes );
Parameters
- $range
- ( mixed ) optional default: today –
- $day_by_day
- ( mixed ) optional –
- $start_date
- ( string|bool ) optional – The starting date for which we'd like to filter our earnings stats. If false, we'll use the default start date of
this_month
- $end_date
- ( string|bool ) optional – The end date for which we'd like to filter our earnings stats. If false, we'll use the default end date of
this_month
- $include_taxes
- ( bool ) optional default: 1 – If taxes should be included in the earnings graphs
Returns
array Total amount of earnings based on the passed arguments.
Source
File name: easy-digital-downloads/includes/payments/class-payment-stats.php
Lines:
1 to 77 of 77
public function get_earnings_by_range( $range = 'today', $day_by_day = false, $start_date = false, $end_date = false, $include_taxes = true ) { global $wpdb; $this->setup_dates( $start_date, $end_date ); $this->end_date = strtotime( 'midnight', $this->end_date ); // Make sure start date is valid if ( is_wp_error( $this->start_date ) ) { return $this->start_date; } // Make sure end date is valid if ( is_wp_error( $this->end_date ) ) { return $this->end_date; } $earnings = array(); $cached = get_transient( 'edd_stats_earnings' ); $key = md5( $range . '_' . date( 'Y-m-d', $this->start_date ) . '_' . date( 'Y-m-d', strtotime( '+1 DAY', $this->end_date ) ) ); $sales = isset( $cached[ $key ] ) ? $cached[ $key ] : false; if ( false === $sales || ! $this->is_cacheable( $range ) ) { if ( ! $day_by_day ) { $select = "DATE_FORMAT(posts.post_date, '%%m') AS m, YEAR(posts.post_date) AS y, COUNT(DISTINCT posts.ID) as count"; $grouping = "YEAR(posts.post_date), MONTH(posts.post_date)"; } else { if ( $range == 'today' || $range == 'yesterday' ) { $select = "DATE_FORMAT(posts.post_date, '%%d') AS d, DATE_FORMAT(posts.post_date, '%%m') AS m, YEAR(posts.post_date) AS y, HOUR(posts.post_date) AS h"; $grouping = "YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date), HOUR(posts.post_date)"; } else { $select = "DATE_FORMAT(posts.post_date, '%%d') AS d, DATE_FORMAT(posts.post_date, '%%m') AS m, YEAR(posts.post_date) AS y"; $grouping = "YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)"; } } if ( $range == 'today' || $range == 'yesterday' ) { $grouping = "YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date), HOUR(posts.post_date)"; } $statuses = apply_filters( 'edd_payment_stats_post_statuses', array( 'publish', 'revoked' ) ); $statuses = "'" . implode( "', '", $statuses ) . "'"; $earnings = $wpdb->get_results( $wpdb->prepare( "SELECT SUM(meta_value) AS total, $select FROM {$wpdb->posts} AS posts INNER JOIN {$wpdb->postmeta} ON posts.ID = {$wpdb->postmeta}.post_ID WHERE posts.post_type IN ('edd_payment') AND {$wpdb->postmeta}.meta_key = '_edd_payment_total' AND posts.post_date >= %s AND posts.post_date < %s AND posts.post_status IN ($statuses) GROUP BY $grouping ORDER by posts.post_date ASC", date( 'Y-m-d', $this->start_date ), date( 'Y-m-d', strtotime( '+1 day', $this->end_date ) ) ), ARRAY_A ); if ( ! $include_taxes ) { $taxes = $wpdb->get_results( $wpdb->prepare( "SELECT SUM(meta_value) AS tax, $select FROM {$wpdb->posts} AS posts INNER JOIN {$wpdb->postmeta} ON posts.ID = {$wpdb->postmeta}.post_ID WHERE posts.post_type IN ('edd_payment') AND {$wpdb->postmeta}.meta_key = '_edd_payment_tax' AND posts.post_date >= %s AND posts.post_date < %s AND posts.post_status IN ($statuses) GROUP BY $grouping ORDER by posts.post_date ASC", date( 'Y-m-d', $this->start_date ), date( 'Y-m-d', strtotime( '+1 day', $this->end_date ) ) ), ARRAY_A ); foreach ( $earnings as $key => $value ) { $earnings[ $key ]['total'] -= $taxes[ $key ]['tax']; } } return $earnings; } }