wi-events/jobs/ProcessSubmitJob.php

176 lines
5.5 KiB
PHP
Raw Normal View History

2023-03-27 23:10:51 +02:00
<?php
namespace Zoomyboy\Event\Jobs;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Mail;
use Zoomyboy\Event\Classes\Spreadsheet;
use Zoomyboy\Event\Classes\SpreadsheetHeader;
use Zoomyboy\Event\Models\Event;
use Zoomyboy\Event\Models\Participant;
use Zoomyboy\Owncloud\Classes\Filesystem;
class ProcessSubmitJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
public Participant $participant;
public Event $event;
/**
* Create a new job instance.
*/
public function __construct(public int $participantId)
{
}
/**
* Execute the job.
*/
public function handle(): void
{
$this->participant = Participant::find($this->participantId);
$this->event = $this->participant->event;
2023-03-31 00:05:01 +02:00
$this->uploadFileAll();
2023-07-10 22:25:08 +02:00
if ($this->event->loadConfig('uploadGroup')) {
$this->uploadFileGroup();
}
2023-03-27 23:10:51 +02:00
$this->sendMail();
}
private function sendMail(): void
{
Mail::send('confirm_'.$this->event->slug, ['data' => $this->participant->payload], function ($message) {
$message->to($this->participant->email, $this->participant->firstname.' '.$this->participant->lastname);
});
}
2023-03-31 00:05:01 +02:00
private function uploadFileAll(): void
2023-03-27 23:10:51 +02:00
{
$filesystem = app(Filesystem::class)->client();
$orderBy = collect($this->event->loadConfig('orderBy'))->map(fn ($order) => "{$order['key']} {$order['direction']}")->implode(',');
$groupBy = $this->event->loadConfig('groupBy');
2024-03-05 21:37:54 +01:00
2023-03-31 00:05:01 +02:00
$s = $this->newSpreadsheet();
2023-03-27 23:10:51 +02:00
if ($this->event->loadConfig('groupAll')) {
$this->makeSheet($s, 'Alle', Participant::where('event_id', $this->event->id)->orderByRaw($orderBy)->get());
}
2024-03-05 21:37:54 +01:00
if ($groupBy) {
$participants = Participant::where('event_id', $this->event->id)->orderByRaw($orderBy)->get()->groupBy(fn ($p) => data_get($p->payload, $groupBy));
foreach ($participants as $group => $groupParticipants) {
$this->makeSheet($s, $group, $groupParticipants);
}
2023-03-27 23:10:51 +02:00
}
2023-03-31 00:41:28 +02:00
$s = $this->addStat($s);
2023-03-27 23:10:51 +02:00
$spreadsheetFile = $s->generate();
2023-04-20 22:27:26 +02:00
$filesystem->write($this->event->slug.'/anmeldungen-'.$this->event->slug.'.xlsx', file_get_contents($spreadsheetFile));
2023-03-27 23:10:51 +02:00
}
2023-03-31 00:05:01 +02:00
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);
2023-03-31 00:41:28 +02:00
$s->headers([new SpreadsheetHeader('Stamm'), new SpreadsheetHeader('TN')]);
$s = $this->addStat($s);
2023-03-31 00:05:01 +02:00
$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);
}
2023-03-31 00:41:28 +02:00
private function addStat(Spreadsheet $s): Spreadsheet
{
2023-08-20 10:26:51 +02:00
$payload = collect([]);
foreach ($this->event->loadConfig('stat') as $stat) {
$stat = Participant::where('event_id', $this->event->id)
->get()
->groupBy(fn ($p) => data_get($p->payload, $stat))
->mapWithKeys(fn ($participants, $groupKey) => [$groupKey => [$groupKey, $participants->count()]])
->values();
$payload = $payload->merge($stat);
}
$s->sheet('Statistik', $payload->toArray());
2023-03-31 00:41:28 +02:00
return $s;
}
2023-03-27 23:10:51 +02:00
private function getfields()
{
return collect($this->event->loadConfig('fields'));
}
2023-03-31 00:05:01 +02:00
private function makeSheet(Spreadsheet $s, $group, $groupParticipants): Spreadsheet
2023-03-27 23:10:51 +02:00
{
$groupParticipants = $groupParticipants->map(function ($participant) {
$payload = $participant->payload;
$content = $this->getFields()->map(function ($field) use ($payload) {
$type = $field['type'];
$method = 'format'.ucfirst($type);
return $this->{$method}(data_get($payload, $field['key']));
});
return $content->toArray();
})->toArray();
$s->sheet($group, $groupParticipants);
2023-03-31 00:05:01 +02:00
return $s;
2023-03-27 23:10:51 +02:00
}
private function formatString(?string $input = null): string
{
return $input ?: '';
}
private function formatDate(?string $input = null): string
{
if (!$input) {
return '';
}
return Carbon::parse($input)->format('d.m.Y');
}
private function formatEnum(?array $input = []): string
{
if (!$input) {
return '';
}
return collect($input)->implode(', ');
}
private function formatBool($input = false): string
{
return true === $input ? 'Ja' : 'Nein';
}
}