AverageDonation.php
6 years ago
DonationsVsIncome.php
6 years ago
Endpoint.php
6 years ago
FormPerformance.php
6 years ago
Income.php
6 years ago
IncomeBreakdown.php
6 years ago
PaymentMethods.php
6 years ago
PaymentStatuses.php
6 years ago
RecentDonations.php
6 years ago
TopDonors.php
6 years ago
TotalDonors.php
6 years ago
TotalIncome.php
6 years ago
TotalRefunds.php
6 years ago
TotalIncome.php
190 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Income over time endpoint |
| 5 | * |
| 6 | * @package Give |
| 7 | */ |
| 8 | |
| 9 | namespace Give\API\Endpoints\Reports; |
| 10 | |
| 11 | class TotalIncome extends Endpoint { |
| 12 | |
| 13 | protected $payments; |
| 14 | |
| 15 | public function __construct() { |
| 16 | $this->endpoint = 'total-income'; |
| 17 | } |
| 18 | |
| 19 | public function get_report( $request ) { |
| 20 | |
| 21 | // Check if a cached version exists |
| 22 | $cached_report = $this->get_cached_report( $request ); |
| 23 | if ( $cached_report !== null ) { |
| 24 | // Bail and return the cached version |
| 25 | return new \WP_REST_Response( |
| 26 | [ |
| 27 | 'data' => $cached_report, |
| 28 | ] |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | $start = date_create( $request['start'] ); |
| 33 | $end = date_create( $request['end'] ); |
| 34 | $diff = date_diff( $start, $end ); |
| 35 | |
| 36 | $dataset = []; |
| 37 | |
| 38 | switch ( true ) { |
| 39 | case ( $diff->days > 12 ): |
| 40 | $interval = round( $diff->days / 12 ); |
| 41 | $data = $this->get_data( $start, $end, 'P' . $interval . 'D' ); |
| 42 | break; |
| 43 | case ( $diff->days > 7 ): |
| 44 | $data = $this->get_data( $start, $end, 'PT12H' ); |
| 45 | break; |
| 46 | case ( $diff->days > 2 ): |
| 47 | $data = $this->get_data( $start, $end, 'PT3H' ); |
| 48 | break; |
| 49 | case ( $diff->days >= 0 ): |
| 50 | $data = $this->get_data( $start, $end, 'PT1H' ); |
| 51 | break; |
| 52 | } |
| 53 | |
| 54 | // Cache the report data |
| 55 | $result = $this->cache_report( $request, $data ); |
| 56 | $status = $this->get_give_status(); |
| 57 | |
| 58 | return new \WP_REST_Response( |
| 59 | [ |
| 60 | 'data' => $data, |
| 61 | 'status' => $status, |
| 62 | ] |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | public function get_data( $start, $end, $intervalStr ) { |
| 67 | |
| 68 | $this->payments = $this->get_payments( $start->format( 'Y-m-d' ), $end->format( 'Y-m-d' ) ); |
| 69 | |
| 70 | $tooltips = []; |
| 71 | $income = []; |
| 72 | |
| 73 | $interval = new \DateInterval( $intervalStr ); |
| 74 | |
| 75 | $periodStart = clone $start; |
| 76 | $periodEnd = clone $start; |
| 77 | |
| 78 | // Subtract interval to set up period start |
| 79 | date_sub( $periodStart, $interval ); |
| 80 | |
| 81 | while ( $periodStart <= $end ) { |
| 82 | |
| 83 | $incomeForPeriod = $this->get_income( $periodStart->format( 'Y-m-d H:i:s' ), $periodEnd->format( 'Y-m-d H:i:s' ) ); |
| 84 | |
| 85 | switch ( $intervalStr ) { |
| 86 | case 'PT12H': |
| 87 | $periodLabel = $periodStart->format( 'D ga' ) . ' - ' . $periodEnd->format( 'D ga' ); |
| 88 | break; |
| 89 | case 'PT3H': |
| 90 | $periodLabel = $periodStart->format( 'D ga' ) . ' - ' . $periodEnd->format( 'D ga' ); |
| 91 | break; |
| 92 | case 'PT1H': |
| 93 | $periodLabel = $periodStart->format( 'D ga' ) . ' - ' . $periodEnd->format( 'D ga' ); |
| 94 | break; |
| 95 | default: |
| 96 | $periodLabel = $periodStart->format( 'M j, Y' ) . ' - ' . $periodEnd->format( 'M j, Y' ); |
| 97 | } |
| 98 | |
| 99 | $income[] = [ |
| 100 | 'x' => $periodEnd->format( 'Y-m-d H:i:s' ), |
| 101 | 'y' => $incomeForPeriod, |
| 102 | ]; |
| 103 | |
| 104 | $tooltips[] = [ |
| 105 | 'title' => give_currency_filter( give_format_amount( $incomeForPeriod ), [ 'decode_currency' => true ] ), |
| 106 | 'body' => __( 'Total Income', 'give' ), |
| 107 | 'footer' => $periodLabel, |
| 108 | ]; |
| 109 | |
| 110 | // Add interval to set up next period |
| 111 | date_add( $periodStart, $interval ); |
| 112 | date_add( $periodEnd, $interval ); |
| 113 | } |
| 114 | |
| 115 | $totalIncomeForPeriod = $this->get_earnings( $start->format( 'Y-m-d' ), $end->format( 'Y-m-d' ) ); |
| 116 | $trend = $this->get_trend( $start, $end, $income ); |
| 117 | |
| 118 | $diff = date_diff( $start, $end ); |
| 119 | $info = $diff->days > 1 ? __( 'VS previous', 'give' ) . ' ' . $diff->days . ' ' . __( 'days', 'give' ) : __( 'VS previous day', 'give' ); |
| 120 | |
| 121 | // Create data objec to be returned, with 'highlights' object containing total and average figures to display |
| 122 | $data = [ |
| 123 | 'datasets' => [ |
| 124 | [ |
| 125 | 'data' => $income, |
| 126 | 'tooltips' => $tooltips, |
| 127 | 'trend' => $trend, |
| 128 | 'info' => $info, |
| 129 | 'highlight' => give_currency_filter( give_format_amount( $totalIncomeForPeriod ), [ 'decode_currency' => true ] ), |
| 130 | ], |
| 131 | ], |
| 132 | ]; |
| 133 | |
| 134 | return $data; |
| 135 | |
| 136 | } |
| 137 | |
| 138 | public function get_trend( $start, $end, $income ) { |
| 139 | |
| 140 | $interval = $start->diff( $end ); |
| 141 | |
| 142 | $prevStart = clone $start; |
| 143 | $prevStart = date_sub( $prevStart, $interval ); |
| 144 | |
| 145 | $prevEnd = clone $start; |
| 146 | |
| 147 | $prevIncome = $this->get_earnings( $prevStart->format( 'Y-m-d' ), $prevEnd->format( 'Y-m-d' ) ); |
| 148 | $currentIncome = $this->get_earnings( $start->format( 'Y-m-d' ), $end->format( 'Y-m-d' ) ); |
| 149 | |
| 150 | // Set default trend to 0 |
| 151 | $trend = 0; |
| 152 | |
| 153 | // Check that prev value and current value are > 0 (can't divide by 0) |
| 154 | if ( $prevIncome > 0 && $currentIncome > 0 ) { |
| 155 | |
| 156 | // Check if it is a percent decreate, or increase |
| 157 | if ( $prevIncome > $currentIncome ) { |
| 158 | // Calculate a percent decrease |
| 159 | $trend = round( ( ( ( $prevIncome - $currentIncome ) / $prevIncome ) * 100 ), 1 ) * -1; |
| 160 | } elseif ( $currentIncome > $prevIncome ) { |
| 161 | // Calculate a percent increase |
| 162 | $trend = round( ( ( ( $currentIncome - $prevIncome ) / $prevIncome ) * 100 ), 1 ); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return $trend; |
| 167 | } |
| 168 | |
| 169 | public function get_income( $startStr, $endStr ) { |
| 170 | |
| 171 | $income = 0; |
| 172 | |
| 173 | foreach ( $this->payments as $payment ) { |
| 174 | if ( $payment->date > $startStr && $payment->date < $endStr ) { |
| 175 | if ( $payment->status === 'publish' || $payment->status === 'give_subscription' ) { |
| 176 | $income += $payment->total; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return $income; |
| 182 | } |
| 183 | |
| 184 | public function get_earnings( $startStr, $endStr ) { |
| 185 | $stats = new \Give_Payment_Stats(); |
| 186 | $income = $stats->get_earnings( 0, $startStr, $endStr ); |
| 187 | return $income; |
| 188 | } |
| 189 | } |
| 190 |