| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Campaigns; |
| 4 |
|
| 5 |
use DateTimeInterface; |
| 6 |
use Give\Campaigns\Models\Campaign; |
| 7 |
use Give\Donations\ValueObjects\DonationMetaKeys; |
| 8 |
use Give\Framework\QueryBuilder\JoinQueryBuilder; |
| 9 |
use Give\Framework\QueryBuilder\QueryBuilder; |
| 10 |
|
| 11 |
/** |
| 12 |
* @since 4.0.0 |
| 13 |
*/ |
| 14 |
class CampaignSubscriptionQuery extends QueryBuilder |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @since 4.0.0 |
| 18 |
*/ |
| 19 |
public function __construct(Campaign $campaign) |
| 20 |
{ |
| 21 |
$this->from('give_subscriptions', 'subscription'); |
| 22 |
|
| 23 |
// Include only current payment "mode" |
| 24 |
$this->where('payment_mode', give_is_test_mode() ? 'test' : 'live'); |
| 25 |
|
| 26 |
// Include only valid statuses |
| 27 |
$this->joinDonation('donation'); |
| 28 |
$this->whereIn('donation.post_status', ['publish', 'give_subscription']); |
| 29 |
|
| 30 |
// Include only forms associated with the Campaign. |
| 31 |
$this->joinDonationMeta(DonationMetaKeys::CAMPAIGN_ID, 'campaignId'); |
| 32 |
$this->where('campaignId.meta_value', $campaign->id); |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @since 4.0.0 |
| 38 |
*/ |
| 39 |
public function between(DateTimeInterface $startDate, DateTimeInterface $endDate): self |
| 40 |
{ |
| 41 |
$query = clone $this; |
| 42 |
$query->whereBetween( |
| 43 |
'created', |
| 44 |
$startDate->format('Y-m-d H:i:s'), |
| 45 |
$endDate->format('Y-m-d H:i:s') |
| 46 |
); |
| 47 |
return $query; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns a calculated sum of the intended amounts (without recovered fees) for the donations. |
| 52 |
* |
| 53 |
* @since 4.0.0 |
| 54 |
* |
| 55 |
* @return int|float |
| 56 |
*/ |
| 57 |
public function sumInitialAmount() |
| 58 |
{ |
| 59 |
return (clone $this)->sum('initial_amount'); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @since 4.0.0 |
| 64 |
*/ |
| 65 |
public function countDonations(): int |
| 66 |
{ |
| 67 |
return (clone $this)->count('donation.ID'); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @since 4.0.0 |
| 72 |
*/ |
| 73 |
public function countDonors(): int |
| 74 |
{ |
| 75 |
$query = clone $this; |
| 76 |
$query->joinDonationMeta(DonationMetaKeys::DONOR_ID, 'donorId'); |
| 77 |
return $query->count('DISTINCT donorId.meta_value'); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @since 4.0.0 |
| 82 |
*/ |
| 83 |
public function joinDonation($alias): self |
| 84 |
{ |
| 85 |
$this->join(function (JoinQueryBuilder $builder) use ($alias) { |
| 86 |
$builder |
| 87 |
->leftJoin('posts', $alias) |
| 88 |
->on('subscription.parent_payment_id', $alias . '.ID'); |
| 89 |
}); |
| 90 |
$this->where($alias . '.post_type', 'give_payment'); |
| 91 |
|
| 92 |
return $this; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* An opinionated join method for the donation meta table. |
| 97 |
* @since 4.0.0 |
| 98 |
*/ |
| 99 |
public function joinDonationMeta($key, $alias): self |
| 100 |
{ |
| 101 |
$this->join(function (JoinQueryBuilder $builder) use ($key, $alias) { |
| 102 |
$builder |
| 103 |
->leftJoin('give_donationmeta', $alias) |
| 104 |
->on('subscription.parent_payment_id', $alias . '.donation_id') |
| 105 |
->andOn($alias . '.meta_key', $key, true); |
| 106 |
}); |
| 107 |
return $this; |
| 108 |
} |
| 109 |
} |
| 110 |
|