*/ public function rules(): array { return [ 'year' => 'required|numeric', ]; } public function handle(int $year): void { /** @var Collection */ $invoices = collect([]); $memberGroup = Member::payable()->get() ->groupBy(fn ($member) => "{$member->bill_kind->value}{$member->lastname}{$member->address}{$member->zip}{$member->location}"); foreach ($memberGroup as $members) { $invoice = Invoice::createForMember($members->first()); foreach ($members as $member) { foreach ($member->subscription->children as $child) { $invoice->positions()->create([ 'description' => str($child->name)->replace('{name}', $member->firstname . ' ' . $member->lastname)->replace('{year}', (string) $year), 'price' => $child->amount, 'member_id' => $member->id, ]); } } $invoices->push($invoice); } event(new InvoicesMassStored($year, $invoices)); } public function asController(ActionRequest $request): JsonResponse { $this->startJob($request->year); return response()->json([]); } /** * @param mixed $parameters */ public function jobState(WithJobState $jobState, ...$parameters): WithJobState { return $jobState ->after('Zahlungen erstellt') ->failed('Fehler beim Erstellen von Zahlungen') ->shouldReload(JobChannels::make()->add('invoice')); } }