Skip to content

Commit

Permalink
Adding payment item types parameter to payment segment criteria
Browse files Browse the repository at this point in the history
remp/crm#1130
  • Loading branch information
rootpd committed Mar 31, 2020
1 parent a59a3e0 commit d3d30d3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/model/Repositories/PaymentItemsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,9 @@ final public function getByType(IRow $payment, string $paymentItemType): array
{
return $payment->related('payment_items')->where('type = ?', $paymentItemType)->fetchAll();
}

final public function getTypes(): array
{
return $this->getTable()->select('DISTINCT type')->fetchPairs('type', 'type');
}
}
29 changes: 27 additions & 2 deletions src/segment/PaymentCriteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Crm\PaymentsModule\Segment;

use Crm\PaymentsModule\Repository\PaymentsRepository;
use Crm\ApplicationModule\Criteria\CriteriaInterface;
use Crm\PaymentsModule\Repository\PaymentItemsRepository;
use Crm\PaymentsModule\Repository\PaymentsRepository;
use Crm\SegmentModule\Criteria\Fields;
use Crm\SegmentModule\Params\BooleanParam;
use Crm\SegmentModule\Params\DateTimeParam;
Expand All @@ -16,13 +17,17 @@ class PaymentCriteria implements CriteriaInterface
{
private $paymentsRepository;

private $paymentItemsRepository;

private $subscriptionTypesRepository;

public function __construct(
PaymentsRepository $paymentsRepository,
PaymentItemsRepository $paymentItemsRepository,
SubscriptionTypesRepository $subscriptionTypesRepository
) {
$this->paymentsRepository = $paymentsRepository;
$this->paymentItemsRepository = $paymentItemsRepository;
$this->subscriptionTypesRepository = $subscriptionTypesRepository;
}

Expand Down Expand Up @@ -58,6 +63,15 @@ public function params(): array
null,
array_keys($this->paymentsRepository->getStatusPairs())
),
new StringArrayParam(
"item_types",
"Payment item types",
"Filters users with payments with specific types of payment items",
false,
[],
null,
array_keys($this->paymentItemsRepository->getTypes())
),
new NumberArrayParam(
"subscription_type",
"Subscription type",
Expand Down Expand Up @@ -86,8 +100,12 @@ public function join(ParamsBag $params): string
$where[] = " payments.status IN ({$params->stringArray('status')->escapedString()}) ";
}

if ($params->has('item_types')) {
$where[] = " payment_items.type IN ({$params->stringArray('item_types')->escapedString()}) ";
}

if ($params->has('created')) {
$where += $params->datetime('created')->escapedConditions('payments.created_at');
$where = array_merge($where, $params->datetime('created')->escapedConditions('payments.created_at'));
}

if ($params->has('subscription_type')) {
Expand All @@ -97,6 +115,7 @@ public function join(ParamsBag $params): string

return "SELECT DISTINCT(payments.user_id) AS id, " . Fields::formatSql($this->fields()) . "
FROM payments
LEFT JOIN payment_items ON payment_items.payment_id = payments.id
WHERE " . implode(" AND ", $where);
}

Expand All @@ -110,6 +129,12 @@ public function title(ParamsBag $paramBag): string
$result .= ' with payment';
}

if ($paramBag->has('item_types')) {
$result .= " with {$paramBag->stringArray('item_types')->escapedString()} items";
} else {
$result .= ' with all item types';
}

if ($paramBag->has('additional_amount')) {
if ($paramBag->boolean('additional_amount')->isTrue()) {
$result .= ' with additional amount';
Expand Down

0 comments on commit d3d30d3

Please sign in to comment.