Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kjohnson committed Apr 25, 2024
1 parent abc1aa7 commit 4ea9650
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 12 deletions.
79 changes: 79 additions & 0 deletions src/DonationForms/DonationQuery.php
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)'
);
}
}
16 changes: 5 additions & 11 deletions src/DonationForms/Repositories/DonationFormRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Give\DonationForms\Actions\ConvertDonationFormBlocksToFieldsApi;
use Give\DonationForms\DonationQuery;
use Give\DonationForms\Models\DonationForm;
use Give\DonationForms\ValueObjects\DonationFormMetaKeys;
use Give\Donations\ValueObjects\DonationMetaKeys;
Expand Down Expand Up @@ -411,21 +412,14 @@ public function getTotalNumberOfSubscriptions(int $formId): int
}

/**
* @unreleased Update query to use intended amounts (without recovered fees).
* @since 3.0.0
*/
public function getTotalRevenue(int $formId): int
{
$query = DB::table('give_formmeta')
->select('meta_value as totalRevenue')
->where('form_id', $formId)
->where('meta_key', '_give_form_earnings')
->get();

if (!$query) {
return 0;
}

return (int)$query->totalRevenue;
return (int) (new DonationQuery)
->form($formId)
->sumIntendedAmount();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/DonationForms/ViewModels/DonationFormViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Give\DonationForms\Actions\GenerateDonateRouteUrl;
use Give\DonationForms\Actions\GenerateDonationFormValidationRouteUrl;
use Give\DonationForms\DataTransferObjects\DonationFormGoalData;
use Give\DonationForms\DonationQuery;
use Give\DonationForms\Properties\FormSettings;
use Give\DonationForms\Repositories\DonationFormRepository;
use Give\DonationForms\ValueObjects\GoalType;
Expand Down Expand Up @@ -171,7 +172,7 @@ private function getTotalRevenue(GoalType $goalType)
return $this->donationFormRepository->getTotalInitialAmountFromSubscriptions($this->donationFormId);
}

return $this->donationFormRepository->getTotalRevenue($this->donationFormId);
return $this->donationFormRepository->getTotalRevenue($this->donationFormId);
}

/**
Expand Down

0 comments on commit 4ea9650

Please sign in to comment.