From 3c936a932779c8d099645afdf2fb64018d3d95cb Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sat, 17 Jul 2021 19:21:27 +0200 Subject: [PATCH] Change state after allPAyment --- app/Payment/SendpaymentController.php | 5 ++++- app/Pdf/BillType.php | 4 ++-- app/Pdf/PdfRepository.php | 2 ++ app/Pdf/PdfRepositoryFactory.php | 16 ++++++++++++---- app/Pdf/Repository.php | 18 ++++++++++++++++++ 5 files changed, 38 insertions(+), 7 deletions(-) diff --git a/app/Payment/SendpaymentController.php b/app/Payment/SendpaymentController.php index 7855595e..846ab260 100644 --- a/app/Payment/SendpaymentController.php +++ b/app/Payment/SendpaymentController.php @@ -26,9 +26,12 @@ class SendpaymentController extends Controller { $repo = app(PdfRepositoryFactory::class)->forAll($request->type); + $pdfFile = app(PdfGenerator::class)->setRepository($repo)->render(); + app(PdfRepositoryFactory::class)->afterAll($request->type); + return $repo === null ? response()->noContent() - : app(PdfGenerator::class)->setRepository($repo)->render(); + : $pdfFile; } } diff --git a/app/Pdf/BillType.php b/app/Pdf/BillType.php index 372ed135..180d9671 100644 --- a/app/Pdf/BillType.php +++ b/app/Pdf/BillType.php @@ -17,9 +17,9 @@ class BillType extends Repository implements PdfRepository $this->pages = $pages; } - public function createable(Member $member): bool + public function getPayments(Member $member): Collection { - return $member->payments()->whereNeedsBill()->count() !== 0; + return $member->payments()->whereNeedsBill()->get(); } public function linkLabel(): string diff --git a/app/Pdf/PdfRepository.php b/app/Pdf/PdfRepository.php index cb89e745..99f0a1dd 100644 --- a/app/Pdf/PdfRepository.php +++ b/app/Pdf/PdfRepository.php @@ -31,6 +31,8 @@ interface PdfRepository public function createable(Member $member): bool; + public function getPayments(Member $member): Collection; + public function linkLabel(): string; public function getUntil(): Carbon; diff --git a/app/Pdf/PdfRepositoryFactory.php b/app/Pdf/PdfRepositoryFactory.php index a53b267b..71d55a3a 100644 --- a/app/Pdf/PdfRepositoryFactory.php +++ b/app/Pdf/PdfRepositoryFactory.php @@ -37,7 +37,7 @@ class PdfRepositoryFactory public function forAll(string $type): ?PdfRepository { - $members = $this->allMemberCollection($type); + $members = $this->toMemberGroup($this->allMemberCollection($type)); if ($members->isEmpty()) { return null; @@ -46,6 +46,16 @@ class PdfRepositoryFactory return $this->resolve($type, $members)->setFilename('alle-rechnungen'); } + 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]); + } + } + public function singleMemberCollection(Member $member, string $type): Collection { $members = Member::where($member->only(['lastname', 'address', 'zip', 'location'])) @@ -57,11 +67,9 @@ class PdfRepositoryFactory public function allMemberCollection(string $type): Collection { - $members = Member::whereHas('billKind', fn (Builder $q) => $q->where('name', 'Post')) + return Member::whereHas('billKind', fn (Builder $q) => $q->where('name', 'Post')) ->get() ->filter(fn (Member $member) => app($type)->createable($member)); - - return $this->toMemberGroup($members); } private function resolve(string $kind, Collection $members): PdfRepository diff --git a/app/Pdf/Repository.php b/app/Pdf/Repository.php index 1308c70e..a019107b 100644 --- a/app/Pdf/Repository.php +++ b/app/Pdf/Repository.php @@ -2,7 +2,9 @@ namespace App\Pdf; +use App\Member\Member; use Carbon\Carbon; +use Generator; abstract class Repository { @@ -17,4 +19,20 @@ abstract class Repository return now()->addWeeks(2); } + public function createable(Member $member): bool + { + return $this->getPayments($member)->count() !== 0; + } + + public function allPayments(): Generator + { + foreach ($this->pages as $page) { + foreach ($page as $member) { + foreach ($this->getPayments($member) as $payment) { + yield $payment; + } + } + } + } + }