diff --git a/app/Letter/DocumentFactory.php b/app/Letter/DocumentFactory.php index 3505704c..a575f312 100644 --- a/app/Letter/DocumentFactory.php +++ b/app/Letter/DocumentFactory.php @@ -2,6 +2,8 @@ namespace App\Letter; +use App\Letter\Queries\BillKindQuery; +use App\Letter\Queries\SingleMemberQuery; use App\Member\Member; use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Support\Collection; @@ -94,15 +96,7 @@ class DocumentFactory */ private function singleMemberPages(Member $member, string $type): Collection { - $members = Member::where($member->only(['lastname', 'address', 'zip', 'location'])) - ->with([ - 'payments' => fn ($query) => $type::paymentsQuery($query) - ->orderByRaw('nr, member_id'), - ]) - ->get() - ->filter(fn (Member $member) => $member->payments->count() > 0); - - return $this->toPages($members); + return (new SingleMemberQuery($member))->getPages($type); } /** @@ -112,15 +106,7 @@ class DocumentFactory */ private function allMemberPages(string $type, BillKind $billKind): Collection { - $members = Member::where('bill_kind', $billKind) - ->with([ - 'payments' => fn ($query) => $type::paymentsQuery($query) - ->orderByRaw('nr, member_id'), - ]) - ->get() - ->filter(fn (Member $member) => $member->payments->count() > 0); - - return $this->toPages($members); + return (new BillKindQuery($billKind))->getPages($type); } /** diff --git a/app/Letter/Queries/BillKindQuery.php b/app/Letter/Queries/BillKindQuery.php new file mode 100644 index 00000000..0727c214 --- /dev/null +++ b/app/Letter/Queries/BillKindQuery.php @@ -0,0 +1,23 @@ + + */ + protected function getQuery(): Builder + { + return Member::where('bill_kind', $this->billKind); + } +} diff --git a/app/Letter/Queries/LetterMemberQuery.php b/app/Letter/Queries/LetterMemberQuery.php new file mode 100644 index 00000000..e3014e7f --- /dev/null +++ b/app/Letter/Queries/LetterMemberQuery.php @@ -0,0 +1,49 @@ + + */ + abstract protected function getQuery(): Builder; + + /** + * @param class-string $type + * + * @return Collection + */ + 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)); + } + + /** + * @param class-string $type + * + * @return EloquentCollection + */ + 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); + } +} diff --git a/app/Letter/Queries/SingleMemberQuery.php b/app/Letter/Queries/SingleMemberQuery.php new file mode 100644 index 00000000..8e5d6520 --- /dev/null +++ b/app/Letter/Queries/SingleMemberQuery.php @@ -0,0 +1,22 @@ + + */ + protected function getQuery(): Builder + { + return Member::where($this->member->only(['lastname', 'address', 'zip', 'location'])); + } +}