adrema/app/Invoice/Queries/InvoiceMemberQuery.php

60 lines
1.4 KiB
PHP
Raw Normal View History

2022-12-06 23:58:44 +01:00
<?php
2023-04-18 22:08:45 +02:00
namespace App\Invoice\Queries;
2022-12-06 23:58:44 +01:00
2023-04-18 22:08:45 +02:00
use App\Invoice\Invoice;
2022-12-06 23:58:44 +01:00
use App\Member\Member;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
2023-04-18 22:08:45 +02:00
abstract class InvoiceMemberQuery
2022-12-06 23:58:44 +01:00
{
2023-11-30 23:54:16 +01:00
/**
* @param class-string<Invoice> $type
*/
public string $type;
2022-12-06 23:58:44 +01:00
/**
* @return Builder<Member>
*/
abstract protected function getQuery(): Builder;
/**
2023-11-30 23:54:16 +01:00
* @return Collection<(int|string), EloquentCollection<(int|string), Member>>
2022-12-06 23:58:44 +01:00
*/
2023-11-30 23:54:16 +01:00
public function getMembers(): Collection
2022-12-06 23:58:44 +01:00
{
2023-11-30 23:54:16 +01:00
return $this->get()->groupBy(
2022-12-06 23:58:44 +01:00
fn ($member) => Str::slug(
"{$member->lastname}{$member->address}{$member->zip}{$member->location}",
),
2023-11-30 23:54:16 +01:00
)->toBase();
2022-12-06 23:58:44 +01:00
}
/**
2023-04-18 22:08:45 +02:00
* @param class-string<Invoice> $type
2023-11-30 23:54:16 +01:00
*/
public function type(string $type): self
{
$this->type = $type;
return $this;
}
/**
2023-02-17 18:57:11 +01:00
* @return EloquentCollection<int, Member>
2022-12-06 23:58:44 +01:00
*/
2023-11-30 23:54:16 +01:00
private function get(): EloquentCollection
2022-12-06 23:58:44 +01:00
{
return $this->getQuery()
->with([
2023-11-30 23:54:16 +01:00
'payments' => fn ($query) => $this->type::paymentsQuery($query)
2022-12-06 23:58:44 +01:00
->orderByRaw('nr, member_id'),
])
->get()
->filter(fn (Member $member) => $member->payments->count() > 0);
}
}