adrema/app/Member/FilterScope.php

109 lines
3.4 KiB
PHP
Raw Normal View History

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-06-14 17:29:22 +02:00
* @param array<int, int> $groupIds
2023-07-10 11:36:58 +02:00
* @param array<int, int> $include
* @param array<int, int> $exclude
2023-06-14 17:11:42 +02:00
*/
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 = [],
public ?string $search = '',
2023-06-14 17:29:22 +02:00
public array $groupIds = [],
2023-07-10 11:36:58 +02:00
public array $include = [],
public array $exclude = [],
2023-07-24 16:55:07 +02:00
public ?bool $hasFullAddress = null,
public ?bool $hasBirthday = 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
{
return $query->where(function ($query) {
$query->orWhere(function ($query) {
if ($this->ausstand) {
$query->whereAusstand();
}
2023-03-14 23:27:15 +01:00
if ($this->billKind) {
$query->where('bill_kind', BillKind::fromValue($this->billKind));
}
2023-07-24 16:55:07 +02:00
if (true === $this->hasFullAddress) {
$query->whereNotNull('address')->whereNotNull('zip')->whereNotNull('location')->where('address', '!=', '')->where('zip', '!=', '')->where('location', '!=', '');
}
if (false === $this->hasFullAddress) {
2023-08-25 00:23:38 +02:00
$query->where(
fn ($q) => $q
->orWhere('address', '')->orWhereNull('address')
->orWhere('zip', '')->orWhereNull('zip')
->orWhere('location', '')->orWhereNull('location')
2023-07-24 16:55:07 +02:00
);
}
if (true === $this->hasBirthday) {
$query->whereNotNull('birthday');
}
2023-04-25 00:28:44 +02:00
if (count($this->groupIds)) {
$query->whereIn('group_id', $this->groupIds);
}
2023-04-25 00:28:44 +02:00
if (count($this->subactivityIds) + count($this->activityIds) > 0) {
$query->whereHas('memberships', function ($q) {
$q->active();
if (count($this->subactivityIds)) {
$q->whereIn('subactivity_id', $this->subactivityIds);
}
if (count($this->activityIds)) {
$q->whereIn('activity_id', $this->activityIds);
}
});
}
2023-07-10 11:36:58 +02:00
if (count($this->exclude)) {
$query->whereNotIn('id', $this->exclude);
}
})->orWhere(function ($query) {
2023-07-10 11:36:58 +02:00
if (count($this->include)) {
$query->whereIn('id', $this->include);
}
});
2023-06-14 17:29:22 +02:00
});
2023-03-14 23:27:15 +01:00
}
}