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