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
|
|
|
|
{
|
2022-02-12 16:44:43 +01:00
|
|
|
/**
|
|
|
|
* @var array<int, class-string<PdfRepository>>
|
|
|
|
*/
|
2021-07-17 16:57:37 +02:00
|
|
|
private array $types = [
|
|
|
|
BillType::class,
|
2021-10-29 21:44:23 +02:00
|
|
|
RememberType::class,
|
2021-07-17 16:57:37 +02:00
|
|
|
];
|
|
|
|
|
2022-02-12 16:44:43 +01:00
|
|
|
/**
|
|
|
|
* @return Collection<int, PdfRepository>
|
|
|
|
*/
|
2021-07-17 16:57:37 +02:00
|
|
|
public function getTypes(): Collection
|
|
|
|
{
|
2022-03-11 20:19:17 +01:00
|
|
|
return collect(array_map(fn ($classString) => new $classString(), $this->types));
|
2021-07-17 16:57:37 +02:00
|
|
|
}
|
|
|
|
|
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-18 21:38:00 +02:00
|
|
|
public function forAll(string $type, string $billKind): ?PdfRepository
|
2021-07-17 18:46:02 +02:00
|
|
|
{
|
2021-07-18 21:38:00 +02:00
|
|
|
$members = $this->toMemberGroup($this->allMemberCollection($type, $billKind));
|
2021-07-17 18:46:02 +02:00
|
|
|
|
|
|
|
if ($members->isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->resolve($type, $members)->setFilename('alle-rechnungen');
|
|
|
|
}
|
|
|
|
|
2021-07-18 21:38:00 +02:00
|
|
|
public function repoCollection(string $type, string $billKind): Collection
|
2021-07-17 19:21:27 +02:00
|
|
|
{
|
2021-07-18 21:38:00 +02:00
|
|
|
$members = $this->toMemberGroup($this->allMemberCollection($type, $billKind));
|
2021-07-17 19:21:27 +02:00
|
|
|
|
2021-07-18 21:38:00 +02:00
|
|
|
return $members->map(function (Collection $members) use ($type) {
|
|
|
|
$repo = $this->resolve($type, collect([$members]));
|
|
|
|
|
|
|
|
return $repo->setFilename(Str::slug("{$repo->getSubject()} für {$members->first()->lastname}"));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function afterSingle(PdfRepository $repo): void
|
|
|
|
{
|
2021-07-17 19:21:27 +02:00
|
|
|
foreach ($repo->allPayments() as $payment) {
|
2021-10-29 21:44:23 +02:00
|
|
|
$repo->afterSingle($payment);
|
2021-07-17 19:21:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 21:38:00 +02:00
|
|
|
public function afterAll(string $type, string $billKind): void
|
|
|
|
{
|
|
|
|
$members = $this->allMemberCollection($type, $billKind);
|
|
|
|
$repo = $this->resolve($type, $this->toMemberGroup($members));
|
|
|
|
|
|
|
|
$this->afterSingle($repo);
|
|
|
|
}
|
|
|
|
|
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-18 21:38:00 +02:00
|
|
|
private function allMemberCollection(string $type, string $billKind): Collection
|
2021-07-17 18:46:02 +02:00
|
|
|
{
|
2021-07-18 21:38:00 +02:00
|
|
|
return Member::whereHas('billKind', fn (Builder $q) => $q->where('name', $billKind))
|
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
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|