AverageDonation.php
5 years ago
DonationsVsIncome.php
5 years ago
Endpoint.php
5 years ago
FormPerformance.php
5 years ago
Income.php
5 years ago
IncomeBreakdown.php
5 years ago
PaymentMethods.php
5 years ago
PaymentStatuses.php
5 years ago
RecentDonations.php
5 years ago
TopDonors.php
5 years ago
TotalDonors.php
5 years ago
TotalIncome.php
5 years ago
TotalRefunds.php
5 years ago
DonationsVsIncome.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Reports base endpoint |
| 5 | * |
| 6 | * @package Give |
| 7 | */ |
| 8 | |
| 9 | namespace Give\API\Endpoints\Reports; |
| 10 | |
| 11 | class DonationsVsIncome extends Endpoint { |
| 12 | |
| 13 | public function __construct() { |
| 14 | $this->endpoint = 'donations-vs-income'; |
| 15 | } |
| 16 | |
| 17 | public function getReport( $request ) { |
| 18 | |
| 19 | $start = date_create( $request->get_param( 'start' ) ); |
| 20 | $end = date_create( $request->get_param( 'end' ) ); |
| 21 | $diff = date_diff( $start, $end ); |
| 22 | |
| 23 | $data = []; |
| 24 | |
| 25 | switch ( true ) { |
| 26 | case ( $diff->days > 900 ): |
| 27 | $data = $this->get_data( $start, $end, 'P1Y', 'Y' ); |
| 28 | break; |
| 29 | case ( $diff->days > 700 ): |
| 30 | $data = $this->get_data( $start, $end, 'P6M', 'F Y' ); |
| 31 | break; |
| 32 | case ( $diff->days > 400 ): |
| 33 | $data = $this->get_data( $start, $end, 'P3M', 'F Y' ); |
| 34 | break; |
| 35 | case ( $diff->days > 120 ): |
| 36 | $data = $this->get_data( $start, $end, 'P1M', 'M Y' ); |
| 37 | break; |
| 38 | case ( $diff->days > 30 ): |
| 39 | $data = $this->get_data( $start, $end, 'P7D', 'M jS' ); |
| 40 | break; |
| 41 | case ( $diff->days > 10 ): |
| 42 | $data = $this->get_data( $start, $end, 'P3D', 'M jS' ); |
| 43 | break; |
| 44 | case ( $diff->days > 4 ): |
| 45 | $data = $this->get_data( $start, $end, 'P1D', 'l' ); |
| 46 | break; |
| 47 | case ( $diff->days > 1 ): |
| 48 | $data = $this->get_data( $start, $end, 'PT6H', 'D ga' ); |
| 49 | break; |
| 50 | case ( $diff->days >= 0 ): |
| 51 | $data = $this->get_data( $start, $end, 'PT1H', 'D ga' ); |
| 52 | break; |
| 53 | } |
| 54 | |
| 55 | return $data; |
| 56 | } |
| 57 | |
| 58 | public function get_data( $start, $end, $interval, $format ) { |
| 59 | |
| 60 | $stats = new \Give_Payment_Stats(); |
| 61 | |
| 62 | $labels = []; |
| 63 | $donations = []; |
| 64 | $income = []; |
| 65 | $periods = []; |
| 66 | |
| 67 | $dateInterval = new \DateInterval( $interval ); |
| 68 | while ( $start < $end ) { |
| 69 | |
| 70 | $periodStart = $start->format( 'Y-m-d H:i:s' ); |
| 71 | |
| 72 | // Add interval to get period end |
| 73 | $periodEnd = clone $start; |
| 74 | date_add( $periodEnd, $dateInterval ); |
| 75 | |
| 76 | $label = $periodEnd->format( $format ); |
| 77 | $periodEnd = $periodEnd->format( 'Y-m-d H:i:s' ); |
| 78 | |
| 79 | $donationsForPeriod = $stats->get_sales( 0, $periodStart, $periodEnd ); |
| 80 | $incomeForPeriod = $stats->get_earnings( 0, $periodStart, $periodEnd ); |
| 81 | |
| 82 | $donations[] = $donationsForPeriod; |
| 83 | $income[] = $incomeForPeriod; |
| 84 | $labels[] = $label; |
| 85 | $periods[] = $periodStart; |
| 86 | |
| 87 | date_add( $start, $dateInterval ); |
| 88 | } |
| 89 | |
| 90 | return [ |
| 91 | 'periods' => $periods, |
| 92 | 'labels' => $labels, |
| 93 | 'datasets' => [ |
| 94 | [ |
| 95 | 'label' => 'Donations', |
| 96 | 'data' => $donations, |
| 97 | ], |
| 98 | [ |
| 99 | 'label' => 'Income', |
| 100 | 'data' => $income, |
| 101 | ], |
| 102 | ], |
| 103 | ]; |
| 104 | } |
| 105 | } |
| 106 |