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();
|
|
|
|
$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');
|
|
|
|
$participants = Participant::where('event_id', $this->event->id)->orderByRaw($orderBy)->get()->groupBy(fn ($p) => data_get($p->payload, $groupBy));
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($participants as $group => $groupParticipants) {
|
|
|
|
$this->makeSheet($s, $group, $groupParticipants);
|
|
|
|
}
|
|
|
|
|
|
|
|
$spreadsheetFile = $s->generate();
|
|
|
|
$filesystem->write($this->event->slug.'/anmeldungen.xlsx', file_get_contents($spreadsheetFile));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
$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-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';
|
|
|
|
}
|
|
|
|
}
|