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
Income.php
142 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 Income extends Endpoint { |
| 12 | |
| 13 | protected $payments; |
| 14 | |
| 15 | public function __construct() { |
| 16 | $this->endpoint = 'income'; |
| 17 | } |
| 18 | |
| 19 | public function get_report( $request ) { |
| 20 | $start = date_create( $request->get_param( 'start' ) ); |
| 21 | $end = date_create( $request->get_param( 'end' ) ); |
| 22 | $diff = date_diff( $start, $end ); |
| 23 | |
| 24 | $dataset = array(); |
| 25 | |
| 26 | switch ( true ) { |
| 27 | case ( $diff->days > 12 ): |
| 28 | $interval = round( $diff->days / 12 ); |
| 29 | $data = $this->get_data( $start, $end, 'P' . $interval . 'D' ); |
| 30 | break; |
| 31 | case ( $diff->days > 5 ): |
| 32 | $data = $this->get_data( $start, $end, 'P1D' ); |
| 33 | break; |
| 34 | case ( $diff->days > 4 ): |
| 35 | $data = $this->get_data( $start, $end, 'PT12H' ); |
| 36 | break; |
| 37 | case ( $diff->days > 2 ): |
| 38 | $data = $this->get_data( $start, $end, 'PT3H' ); |
| 39 | break; |
| 40 | case ( $diff->days >= 0 ): |
| 41 | $data = $this->get_data( $start, $end, 'PT1H' ); |
| 42 | break; |
| 43 | } |
| 44 | |
| 45 | return $data; |
| 46 | } |
| 47 | |
| 48 | public function get_data( $start, $end, $intervalStr ) { |
| 49 | |
| 50 | $this->payments = $this->get_payments( $start->format( 'Y-m-d' ), $end->format( 'Y-m-d' ) ); |
| 51 | |
| 52 | $tooltips = array(); |
| 53 | $income = array(); |
| 54 | |
| 55 | $interval = new \DateInterval( $intervalStr ); |
| 56 | |
| 57 | $periodStart = clone $start; |
| 58 | $periodEnd = clone $start; |
| 59 | |
| 60 | // Subtract interval to set up period start |
| 61 | date_sub( $periodStart, $interval ); |
| 62 | |
| 63 | while ( $periodStart < $end ) { |
| 64 | |
| 65 | $values = $this->get_values( $periodStart->format( 'Y-m-d H:i:s' ), $periodEnd->format( 'Y-m-d H:i:s' ) ); |
| 66 | $incomeForPeriod = $values['earnings']; |
| 67 | $donorsForPeriod = $values['donor_count']; |
| 68 | $time = $periodEnd->format( 'Y-m-d H:i:s' ); |
| 69 | |
| 70 | switch ( $intervalStr ) { |
| 71 | case 'P1D': |
| 72 | $time = $periodStart->format( 'Y-m-d H:i:s' ); |
| 73 | $periodLabel = $periodStart->format( 'l' ); |
| 74 | break; |
| 75 | case 'PT12H': |
| 76 | case 'PT3H': |
| 77 | case 'PT1H': |
| 78 | $periodLabel = $periodStart->format( 'D ga' ) . ' - ' . $periodEnd->format( 'D ga' ); |
| 79 | break; |
| 80 | default: |
| 81 | $periodLabel = $periodStart->format( 'M j, Y' ) . ' - ' . $periodEnd->format( 'M j, Y' ); |
| 82 | } |
| 83 | |
| 84 | $income[] = array( |
| 85 | 'x' => $time, |
| 86 | 'y' => $incomeForPeriod, |
| 87 | ); |
| 88 | |
| 89 | $tooltips[] = array( |
| 90 | 'title' => give_currency_filter( give_format_amount( $incomeForPeriod ), array( 'decode_currency' => true ) ), |
| 91 | 'body' => $donorsForPeriod . ' ' . __( 'Donors', 'give' ), |
| 92 | 'footer' => $periodLabel, |
| 93 | ); |
| 94 | |
| 95 | // Add interval to set up next period |
| 96 | date_add( $periodStart, $interval ); |
| 97 | date_add( $periodEnd, $interval ); |
| 98 | } |
| 99 | |
| 100 | if ( $intervalStr === 'P1D' ) { |
| 101 | $income = array_slice( $income, 1 ); |
| 102 | $tooltips = array_slice( $tooltips, 1 ); |
| 103 | } |
| 104 | |
| 105 | // Create data objec to be returned, with 'highlights' object containing total and average figures to display |
| 106 | $data = array( |
| 107 | 'datasets' => array( |
| 108 | array( |
| 109 | 'data' => $income, |
| 110 | 'tooltips' => $tooltips, |
| 111 | ), |
| 112 | ), |
| 113 | ); |
| 114 | |
| 115 | return $data; |
| 116 | |
| 117 | } |
| 118 | |
| 119 | public function get_values( $startStr, $endStr ) { |
| 120 | |
| 121 | $earnings = 0; |
| 122 | $donors = array(); |
| 123 | |
| 124 | foreach ( $this->payments as $payment ) { |
| 125 | if ( $payment->date > $startStr && $payment->date < $endStr ) { |
| 126 | if ( $payment->status == 'publish' || $payment->status == 'give_subscription' ) { |
| 127 | $earnings += $payment->total; |
| 128 | $donors[] = $payment->donor_id; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | $unique = array_unique( $donors ); |
| 134 | |
| 135 | return array( |
| 136 | 'earnings' => $earnings, |
| 137 | 'donor_count' => count( $unique ), |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | } |
| 142 |