2021-07-15 21:17:48 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Pdf;
|
|
|
|
|
|
|
|
use App\Member\Member;
|
2021-07-17 18:46:02 +02:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2021-07-15 21:17:48 +02:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
class PdfRepositoryFactory
|
|
|
|
{
|
|
|
|
|
2021-07-17 16:57:37 +02:00
|
|
|
private array $types = [
|
|
|
|
BillType::class,
|
|
|
|
];
|
|
|
|
|
|
|
|
public function getTypes(): Collection
|
|
|
|
{
|
|
|
|
return collect($this->types);
|
|
|
|
}
|
|
|
|
|
2021-07-15 21:17:48 +02:00
|
|
|
public function fromSingleRequest(string $type, Member $member): ?PdfRepository
|
|
|
|
{
|
2021-07-17 18:10:13 +02:00
|
|
|
$members = $this->singleMemberCollection($member, $type);
|
2021-07-15 21:17:48 +02:00
|
|
|
|
|
|
|
if ($members->isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$repo = $this->resolve($type, $members);
|
|
|
|
$firstMember = $members->first()->first();
|
|
|
|
|
|
|
|
return $repo->setFilename(
|
2021-07-17 17:29:59 +02:00
|
|
|
Str::slug("{$repo->getSubject()} für {$firstMember->lastname}"),
|
2021-07-15 21:17:48 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-17 18:46:02 +02:00
|
|
|
public function forAll(string $type): ?PdfRepository
|
|
|
|
{
|
2021-07-17 19:21:27 +02:00
|
|
|
$members = $this->toMemberGroup($this->allMemberCollection($type));
|
2021-07-17 18:46:02 +02:00
|
|
|
|
|
|
|
if ($members->isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->resolve($type, $members)->setFilename('alle-rechnungen');
|
|
|
|
}
|
|
|
|
|
2021-07-17 19:21:27 +02:00
|
|
|
public function afterAll(string $type): void
|
|
|
|
{
|
|
|
|
$members = $this->allMemberCollection($type);
|
|
|
|
$repo = $this->resolve($type, $this->toMemberGroup($this->allMemberCollection($type)));
|
|
|
|
|
|
|
|
foreach ($repo->allPayments() as $payment) {
|
|
|
|
$payment->update(['status_id' => 2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 18:10:13 +02:00
|
|
|
public function singleMemberCollection(Member $member, string $type): Collection
|
2021-07-15 21:17:48 +02:00
|
|
|
{
|
2021-07-17 18:10:13 +02:00
|
|
|
$members = Member::where($member->only(['lastname', 'address', 'zip', 'location']))
|
|
|
|
->get()
|
|
|
|
->filter(fn (Member $member) => app($type)->createable($member));
|
2021-07-15 21:17:48 +02:00
|
|
|
|
|
|
|
return $this->toMemberGroup($members);
|
|
|
|
}
|
|
|
|
|
2021-07-17 18:46:02 +02:00
|
|
|
public function allMemberCollection(string $type): Collection
|
|
|
|
{
|
2021-07-17 19:21:27 +02:00
|
|
|
return Member::whereHas('billKind', fn (Builder $q) => $q->where('name', 'Post'))
|
2021-07-17 18:46:02 +02:00
|
|
|
->get()
|
|
|
|
->filter(fn (Member $member) => app($type)->createable($member));
|
|
|
|
}
|
|
|
|
|
2021-07-15 21:17:48 +02:00
|
|
|
private function resolve(string $kind, Collection $members): PdfRepository
|
|
|
|
{
|
|
|
|
return new $kind($members);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function toMemberGroup(Collection $members): Collection
|
|
|
|
{
|
|
|
|
return $members->groupBy(
|
|
|
|
fn ($member) => Str::slug(
|
2021-07-17 18:10:13 +02:00
|
|
|
"{$member->lastname}{$member->address}{$member->zip}{$member->location}",
|
2021-07-15 21:17:48 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|