From e6ab3344e340f013f4d63397ddd1d72e25241eb4 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Fri, 31 Mar 2023 00:05:01 +0200 Subject: [PATCH] Add stamm grouping --- jobs/ProcessSubmitJob.php | 40 +++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/jobs/ProcessSubmitJob.php b/jobs/ProcessSubmitJob.php index df40a66..9d3f739 100644 --- a/jobs/ProcessSubmitJob.php +++ b/jobs/ProcessSubmitJob.php @@ -40,7 +40,8 @@ class ProcessSubmitJob implements ShouldQueue $this->participant = Participant::find($this->participantId); $this->event = $this->participant->event; - $this->uploadFile(); + $this->uploadFileAll(); + $this->uploadFileGroup(); $this->sendMail(); } @@ -51,18 +52,13 @@ class ProcessSubmitJob implements ShouldQueue }); } - private function uploadFile(): void + private function uploadFileAll(): void { $filesystem = app(Filesystem::class)->client(); - - $headers = $this->getFields()->map(fn ($field) => new SpreadsheetHeader($field['label']))->toArray(); - $orderBy = collect($this->event->loadConfig('orderBy'))->map(fn ($order) => "{$order['key']} {$order['direction']}")->implode(','); $groupBy = $this->event->loadConfig('groupBy'); - $participants = Participant::where('event_id', $this->event->id)->orderByRaw($orderBy)->get()->groupBy(fn ($p) => data_get($p->payload, $groupBy)); - - $s = (new Spreadsheet('Anmeldezahlen '.$this->event->title))->headers($headers); + $s = $this->newSpreadsheet(); if ($this->event->loadConfig('groupAll')) { $this->makeSheet($s, 'Alle', Participant::where('event_id', $this->event->id)->orderByRaw($orderBy)->get()); @@ -73,16 +69,38 @@ class ProcessSubmitJob implements ShouldQueue } $spreadsheetFile = $s->generate(); - $filesystem->write($this->event->slug.'/anmeldungen.xlsx', file_get_contents($spreadsheetFile)); } + private function uploadFileGroup(): void + { + $filesystem = app(Filesystem::class)->client(); + $orderBy = collect($this->event->loadConfig('orderBy'))->map(fn ($order) => "{$order['key']} {$order['direction']}")->implode(','); + $groupBy = $this->event->loadConfig('groupBy'); + $participantGroup = data_get($this->participant->payload, $groupBy); + $participants = Participant::where('event_id', $this->event->id)->orderByRaw($orderBy)->get() + ->filter(fn ($p) => data_get($p->payload, $groupBy) === $participantGroup); + $s = $this->newSpreadsheet(); + + $this->makeSheet($s, 'Alle', $participants); + + $spreadsheetFile = $s->generate(); + $filesystem->write($participantGroup.'/anmeldungen-'.$this->event->slug.'.xlsx', file_get_contents($spreadsheetFile)); + } + + private function newSpreadsheet() + { + $headers = $this->getFields()->map(fn ($field) => new SpreadsheetHeader($field['label']))->toArray(); + + return (new Spreadsheet('Anmeldezahlen '.$this->event->title))->headers($headers); + } + private function getfields() { return collect($this->event->loadConfig('fields')); } - private function makeSheet(Spreadsheet $s, $group, $groupParticipants) + private function makeSheet(Spreadsheet $s, $group, $groupParticipants): Spreadsheet { $groupParticipants = $groupParticipants->map(function ($participant) { $payload = $participant->payload; @@ -97,6 +115,8 @@ class ProcessSubmitJob implements ShouldQueue })->toArray(); $s->sheet($group, $groupParticipants); + + return $s; } private function formatString(?string $input = null): string