2023-03-14 23:27:15 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Member;
|
|
|
|
|
2023-04-18 22:08:45 +02:00
|
|
|
use App\Invoice\BillKind;
|
2023-03-14 23:27:15 +01:00
|
|
|
use App\Lib\Filter;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Spatie\LaravelData\Attributes\MapInputName;
|
|
|
|
use Spatie\LaravelData\Attributes\MapOutputName;
|
|
|
|
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @extends Filter<Member>
|
|
|
|
*/
|
|
|
|
#[MapInputName(SnakeCaseMapper::class)]
|
|
|
|
#[MapOutputName(SnakeCaseMapper::class)]
|
|
|
|
class FilterScope extends Filter
|
|
|
|
{
|
2023-06-14 17:11:42 +02:00
|
|
|
/**
|
|
|
|
* @param array<int, int> $activityIds
|
|
|
|
* @param array<int, int> $subactivityIds
|
|
|
|
*/
|
2023-03-14 23:27:15 +01:00
|
|
|
public function __construct(
|
|
|
|
public bool $ausstand = false,
|
|
|
|
public ?string $billKind = null,
|
2023-06-14 16:20:18 +02:00
|
|
|
public array $activityIds = [],
|
|
|
|
public array $subactivityIds = [],
|
2023-03-15 11:16:52 +01:00
|
|
|
public string $search = '',
|
2023-04-25 00:28:44 +02:00
|
|
|
public ?int $groupId = null,
|
2023-03-14 23:27:15 +01:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function locks(): array
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Builder<Member> $query
|
|
|
|
*
|
|
|
|
* @return Builder<Member>
|
|
|
|
*/
|
|
|
|
public function apply(Builder $query): Builder
|
|
|
|
{
|
|
|
|
if ($this->ausstand) {
|
|
|
|
$query->whereAusstand();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->billKind) {
|
|
|
|
$query->where('bill_kind', BillKind::fromValue($this->billKind));
|
|
|
|
}
|
2023-04-25 00:28:44 +02:00
|
|
|
|
|
|
|
if ($this->groupId) {
|
|
|
|
$query->where('group_id', $this->groupId);
|
|
|
|
}
|
|
|
|
|
2023-06-14 16:20:18 +02:00
|
|
|
if (count($this->subactivityIds) + count($this->activityIds) > 0) {
|
2023-03-14 23:27:15 +01:00
|
|
|
$query->whereHas('memberships', function ($q) {
|
|
|
|
$q->active();
|
2023-06-14 16:20:18 +02:00
|
|
|
if (count($this->subactivityIds)) {
|
|
|
|
$q->whereIn('subactivity_id', $this->subactivityIds);
|
2023-03-14 23:27:15 +01:00
|
|
|
}
|
2023-06-14 16:20:18 +02:00
|
|
|
if (count($this->activityIds)) {
|
|
|
|
$q->whereIn('activity_id', $this->activityIds);
|
2023-03-14 23:27:15 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
}
|