From 618411d2e39471b3283bfed959a430cdbca78d44 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Thu, 15 Jul 2021 21:17:48 +0200 Subject: [PATCH] Add PDF engine --- app/Pdf/BillType.php | 34 +++++++++++++++++++++ app/Pdf/MemberPdfController.php | 21 +++++++++++++ app/Pdf/PdfGenerator.php | 31 +++++++++++++++++++ app/Pdf/PdfRepository.php | 16 ++++++++++ app/Pdf/PdfRepositoryFactory.php | 51 ++++++++++++++++++++++++++++++++ routes/web.php | 14 +++++---- 6 files changed, 161 insertions(+), 6 deletions(-) create mode 100644 app/Pdf/BillType.php create mode 100644 app/Pdf/MemberPdfController.php create mode 100644 app/Pdf/PdfGenerator.php create mode 100644 app/Pdf/PdfRepository.php create mode 100644 app/Pdf/PdfRepositoryFactory.php diff --git a/app/Pdf/BillType.php b/app/Pdf/BillType.php new file mode 100644 index 00000000..1520f32e --- /dev/null +++ b/app/Pdf/BillType.php @@ -0,0 +1,34 @@ +pages = $pages; + } + + public function getSubject(): string + { + return 'Rechnung'; + } + + public function setFilename(string $filename): self + { + $this->filename = $filename; + + return $this; + } + + public function getFilename(): string + { + return $this->filename; + } + +} diff --git a/app/Pdf/MemberPdfController.php b/app/Pdf/MemberPdfController.php new file mode 100644 index 00000000..c68a4382 --- /dev/null +++ b/app/Pdf/MemberPdfController.php @@ -0,0 +1,21 @@ +fromSingleRequest($request->type, $member); + + return $repo === null + ? response()->noContent() + : app(PdfGenerator::class)->render($repo); + } + +} diff --git a/app/Pdf/PdfGenerator.php b/app/Pdf/PdfGenerator.php new file mode 100644 index 00000000..d1e6417c --- /dev/null +++ b/app/Pdf/PdfGenerator.php @@ -0,0 +1,31 @@ +make('pdf.pdf', [ + 'data' => $repo, + ]); + + $filename = $repo->getFilename(); + dd($filename); + + Storage::disk('temp')->put($repo->getBasename(), $content); + } + + public function toResponse($request) + { + return response()->file($this->filename); + } + +} + diff --git a/app/Pdf/PdfRepository.php b/app/Pdf/PdfRepository.php new file mode 100644 index 00000000..392c1ee8 --- /dev/null +++ b/app/Pdf/PdfRepository.php @@ -0,0 +1,16 @@ +singleMemberCollection($member); + + if ($members->isEmpty()) { + return null; + } + + $repo = $this->resolve($type, $members); + $firstMember = $members->first()->first(); + + return $repo->setFilename( + Str::slug("{$repo->getSubject()} für {$firstMember->firstname} {$firstMember->lastname}"), + ); + } + + public function singleMemberCollection(Member $member): Collection + { + $members = Member::where($member->only(['firstname', 'lastname', 'address', 'zip', 'location'])) + ->whereHas('payments', fn ($q) => $q->whereNeedsPayment()) + ->get(); + + return $this->toMemberGroup($members); + } + + 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( + "{$member->firstname}{$member->lastname}{$member->address}{$member->zip}{$member->location}", + ), + ); + } + +} diff --git a/routes/web.php b/routes/web.php index def18c7b..71494856 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,12 +1,13 @@ - 'App\\Http\\Controllers'], function() { Auth::routes(['register' => false]); @@ -20,5 +21,6 @@ Route::group(['middleware' => 'auth:web'], function () { Route::resource('allpayment', AllpaymentController::class); Route::resource('subscription', SubscriptionController::class); Route::post('/member/{member}/confirm', MemberConfirmController::class); + Route::post('/member/{member}/pdf', MemberPdfController::class)->name('member.singlepdf'); });