adrema/app/Invoice/Queries/InvoiceMemberQuery.php

50 lines
1.3 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;
use App\Invoice\Page;
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
{
/**
* @return Builder<Member>
*/
abstract protected function getQuery(): Builder;
/**
2023-04-18 22:08:45 +02:00
* @param class-string<Invoice> $type
2022-12-06 23:58:44 +01:00
*
* @return Collection<int, Page>
*/
public function getPages(string $type): Collection
{
return $this->get($type)->groupBy(
fn ($member) => Str::slug(
"{$member->lastname}{$member->address}{$member->zip}{$member->location}",
),
)->map(fn ($page) => new Page($page));
}
/**
2023-04-18 22:08:45 +02:00
* @param class-string<Invoice> $type
2022-12-06 23:58:44 +01:00
*
2023-02-17 18:57:11 +01:00
* @return EloquentCollection<int, Member>
2022-12-06 23:58:44 +01:00
*/
private function get(string $type): EloquentCollection
{
return $this->getQuery()
->with([
'payments' => fn ($query) => $type::paymentsQuery($query)
->orderByRaw('nr, member_id'),
])
->get()
->filter(fn (Member $member) => $member->payments->count() > 0);
}
}