Actions
1 year ago
AsyncData
1 year ago
Blocks
1 year ago
Controllers
1 year ago
DataTransferObjects
1 year ago
Exceptions
1 year ago
Factories
1 year ago
FormDesigns
1 year ago
FormPage
1 year ago
Listeners
1 year ago
Migrations
1 year ago
Models
2 years ago
OrphanedForms
1 year ago
Properties
1 year ago
Repositories
1 year ago
Routes
1 year ago
Rules
1 year ago
Shortcodes
2 years ago
V2
1 year ago
ValueObjects
1 year ago
ViewModels
1 year ago
resources
1 year ago
DonationFormDataQuery.php
1 year ago
DonationFormsAdminPage.php
1 year ago
DonationQuery.php
1 year ago
ServiceProvider.php
1 year ago
SubscriptionQuery.php
2 years ago
DonationFormDataQuery.php
127 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms; |
| 4 | |
| 5 | use Give\Donations\ValueObjects\DonationMetaKeys; |
| 6 | use Give\Framework\QueryBuilder\JoinQueryBuilder; |
| 7 | use Give\Framework\QueryBuilder\QueryBuilder; |
| 8 | |
| 9 | /** |
| 10 | * Class used for loading the number of donors, donations and revenue amounts for multiple forms |
| 11 | * |
| 12 | * @since 4.3.0 |
| 13 | */ |
| 14 | class DonationFormDataQuery extends QueryBuilder |
| 15 | { |
| 16 | /** |
| 17 | * @since 4.3.0 |
| 18 | */ |
| 19 | private function __construct() |
| 20 | { |
| 21 | $this->select(['formId.meta_value', 'form_id']); |
| 22 | $this->whereIn('donation.post_status', ['publish', 'give_subscription']); |
| 23 | $this->groupBy('form_id'); |
| 24 | } |
| 25 | |
| 26 | |
| 27 | /** |
| 28 | * Donations query for campaigns |
| 29 | * |
| 30 | * @param int[] $formIds |
| 31 | */ |
| 32 | public static function donations(array $formIds): DonationFormDataQuery |
| 33 | { |
| 34 | return (new self()) |
| 35 | ->from('posts', 'donation') |
| 36 | ->where('post_type', 'give_payment') |
| 37 | ->joinDonationMeta(DonationMetaKeys::FORM_ID, 'formId') |
| 38 | ->joinDonationMeta(DonationMetaKeys::MODE, 'paymentMode') |
| 39 | ->whereIn('formId.meta_value', $formIds) |
| 40 | ->where('paymentMode.meta_value', give_is_test_mode() ? 'test' : 'live'); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Subscriptions query for forms |
| 45 | * |
| 46 | * @since 4.3.0 |
| 47 | * |
| 48 | * @param int[] $formIds |
| 49 | */ |
| 50 | public static function subscriptions(array $formIds): DonationFormDataQuery |
| 51 | { |
| 52 | return (new self()) |
| 53 | ->from('give_subscriptions', 'subscription') |
| 54 | ->join(function (JoinQueryBuilder $builder) { |
| 55 | $builder |
| 56 | ->leftJoin('posts', 'donation') |
| 57 | ->on('subscription.parent_payment_id', 'donation.ID'); |
| 58 | }) |
| 59 | ->joinDonationMeta(DonationMetaKeys::FORM_ID, 'formId') |
| 60 | ->whereIn('formId.meta_value', $formIds) |
| 61 | ->where('payment_mode', give_is_test_mode() ? 'test' : 'live') |
| 62 | ->where('donation.post_type', 'give_payment'); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns a calculated sum of the intended amounts (without recovered fees) for the donations. |
| 67 | * |
| 68 | * @since 4.3.0 |
| 69 | */ |
| 70 | public function collectIntendedAmounts(): ?array |
| 71 | { |
| 72 | return (clone $this) |
| 73 | ->select('SUM(IFNULL(amount.meta_value, 0) - IFNULL(feeAmountRecovered.meta_value, 0)) as sum') |
| 74 | ->joinDonationMeta(DonationMetaKeys::AMOUNT, 'amount') |
| 75 | ->joinDonationMeta(DonationMetaKeys::FEE_AMOUNT_RECOVERED, 'feeAmountRecovered') |
| 76 | ->getAll(ARRAY_A); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns a calculated sum of the initial amounts |
| 81 | * |
| 82 | * @since 4.3.0 |
| 83 | */ |
| 84 | public function collectInitialAmounts(): ?array |
| 85 | { |
| 86 | return (clone $this) |
| 87 | ->select('SUM(initial_amount) as sum') |
| 88 | ->getAll(ARRAY_A); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @since 4.3.0 |
| 93 | */ |
| 94 | public function collectDonations(): ?array |
| 95 | { |
| 96 | return (clone $this) |
| 97 | ->select('COUNT(donation.ID) as count') |
| 98 | ->getAll(ARRAY_A); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @since 4.3.0 |
| 103 | */ |
| 104 | public function collectDonors(): ?array |
| 105 | { |
| 106 | return (clone $this) |
| 107 | ->select('COUNT(DISTINCT donorId.meta_value) as count') |
| 108 | ->joinDonationMeta(DonationMetaKeys::DONOR_ID, 'donorId') |
| 109 | ->getAll(ARRAY_A); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @since 4.3.0 |
| 114 | */ |
| 115 | private function joinDonationMeta($key, $alias): self |
| 116 | { |
| 117 | $this->join(function (JoinQueryBuilder $builder) use ($key, $alias) { |
| 118 | $builder |
| 119 | ->leftJoin('give_donationmeta', $alias) |
| 120 | ->on('donation.ID', $alias . '.donation_id') |
| 121 | ->andOn($alias . '.meta_key', $key, true); |
| 122 | }); |
| 123 | |
| 124 | return $this; |
| 125 | } |
| 126 | } |
| 127 |