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
DonationsVsIncome.php
111 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 get_report($request) { |
| 18 | |
| 19 | $start = date_create($request['start']); |
| 20 | $end = date_create($request['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 new \WP_REST_Response([ |
| 56 | 'data' => $data |
| 57 | ]); |
| 58 | } |
| 59 | |
| 60 | public function get_data($start, $end, $interval, $format) { |
| 61 | |
| 62 | $stats = new \Give_Payment_Stats(); |
| 63 | |
| 64 | $labels = []; |
| 65 | $donations = []; |
| 66 | $income = []; |
| 67 | $periods = []; |
| 68 | |
| 69 | $dateInterval = new \DateInterval($interval); |
| 70 | while ( $start < $end ) { |
| 71 | |
| 72 | $periodStart = $start->format('Y-m-d H:i:s'); |
| 73 | |
| 74 | // Add interval to get period end |
| 75 | $periodEnd = clone $start; |
| 76 | date_add($periodEnd, $dateInterval); |
| 77 | |
| 78 | $label = $periodEnd->format($format); |
| 79 | $periodEnd = $periodEnd->format('Y-m-d H:i:s'); |
| 80 | |
| 81 | $donationsForPeriod = $stats->get_sales( 0, $periodStart, $periodEnd ); |
| 82 | $incomeForPeriod = $stats->get_earnings( 0, $periodStart, $periodEnd ); |
| 83 | |
| 84 | array_push($donations, $donationsForPeriod); |
| 85 | array_push($income, $incomeForPeriod); |
| 86 | array_push($labels, $label); |
| 87 | array_push($periods, $periodStart); |
| 88 | |
| 89 | date_add($start, $dateInterval); |
| 90 | } |
| 91 | |
| 92 | $data = [ |
| 93 | 'periods' => $periods, |
| 94 | 'labels' => $labels, |
| 95 | 'datasets' => [ |
| 96 | [ |
| 97 | 'label' => 'Donations', |
| 98 | 'data' => $donations |
| 99 | ], |
| 100 | [ |
| 101 | 'label' => 'Income', |
| 102 | 'data' => $income |
| 103 | ] |
| 104 | ] |
| 105 | ]; |
| 106 | |
| 107 | return $data; |
| 108 | |
| 109 | } |
| 110 | } |
| 111 |