-
Notifications
You must be signed in to change notification settings - Fork 193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor: Update goal/stats to use Donation Query #7376
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Give\DonationForms; | ||
|
||
use Give\Framework\QueryBuilder\JoinQueryBuilder; | ||
use Give\Framework\QueryBuilder\QueryBuilder; | ||
|
||
/** | ||
* An opinionated Query Builder for GiveWP donations and meta fields. | ||
* | ||
* @unreleased | ||
* | ||
* Example usage: | ||
* (new DonationQuery) | ||
* ->form(1816) | ||
* ->between('2024-02-00', '2024-02-23') | ||
* ->sumIntendedAmount(); | ||
*/ | ||
class DonationQuery extends QueryBuilder | ||
{ | ||
/** | ||
* @unreleased | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->from('posts', 'donation'); | ||
} | ||
|
||
/** | ||
* An opinionated join method for the donation meta table. | ||
* @unreleased | ||
*/ | ||
public function joinMeta($key, $alias) | ||
{ | ||
$this->join(function (JoinQueryBuilder $builder) use ($key, $alias) { | ||
$builder | ||
->leftJoin('give_donationmeta', $alias) | ||
->on('donation.ID', $alias . '.donation_id') | ||
->andOn($alias . '.meta_key', $key, true); | ||
}); | ||
return $this; | ||
} | ||
|
||
/** | ||
* An opinionated where method for the donation form ID meta field. | ||
* @unreleased | ||
*/ | ||
public function form($formId) | ||
{ | ||
$this->joinMeta('_give_payment_form_id', 'formId'); | ||
$this->where('formId.meta_value', $formId); | ||
return $this; | ||
} | ||
|
||
/** | ||
* An opinionated whereBetween method for the completed date meta field. | ||
* @unreleased | ||
*/ | ||
public function between($startDate, $endDate) | ||
{ | ||
$this->joinMeta('_give_completed_date', 'completed'); | ||
$this->whereBetween('completed.meta_value', $startDate, $endDate); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Returns a calculated sum of the intended amounts (without recovered fees) for the donations. | ||
* @unreleased | ||
* @return int|float | ||
*/ | ||
public function sumIntendedAmount() | ||
{ | ||
$this->joinMeta('_give_payment_total', 'amount'); | ||
$this->joinMeta('_give_fee_donation_amount', 'intendedAmount'); | ||
return $this->sum( | ||
'COALESCE(intendedAmount.meta_value, amount.meta_value)' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm, I'm not sure I fully understand this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, it works as expected. |
||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't test it on my end, but I'm very surprised that this works because the
meta_value
type islongtext
and notdatetime
🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested this on my end and it works 😅
Interesting