64 lines
3.0 KiB
PHP
64 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Resources;
|
|
|
|
use App\Enumerations\ParticipationType;
|
|
use App\Models\EventParticipant;
|
|
use App\ValueObjects\Age;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class EventParticipantResource extends JsonResource
|
|
{
|
|
function __construct(EventParticipant $participant)
|
|
{
|
|
parent::__construct($participant);
|
|
}
|
|
|
|
public function toArray($request) : array
|
|
{
|
|
$event = $this->resource->event;
|
|
|
|
$amountLeft = $this->resource->amount;
|
|
if ($this->resource->amount_paid !== null) {
|
|
$amountLeft->subtractAmount($this->resource->amount_paid);
|
|
}
|
|
|
|
$presenceDays = $this->resource->arrival_date->diff($this->resource->departure_date)->days;
|
|
$presenceDaysSupport = $presenceDays;
|
|
|
|
if ($presenceDaysSupport === 0) {
|
|
$presenceDaysSupport = 1;
|
|
} else {
|
|
$presenceDaysSupport = $presenceDaysSupport - 1;
|
|
}
|
|
|
|
return array_merge(
|
|
$this->resource->toArray(),
|
|
[
|
|
'age' => new Age($this->resource->birthday)->getAge(),
|
|
'localgroup' => $this->resource->localGroup()->first()->name,
|
|
'swimmingPermission' => $this->resource->swimmingPermission()->first()->short,
|
|
'extendedFirstAid' => $this->resource->firstAidPermission()->first()->name,
|
|
'tetanusVaccination' => $this->resource->tetanus_vaccination?->format('d.m.Y'),
|
|
'presenceDays' => ['real' => $presenceDays, 'support' => $presenceDaysSupport],
|
|
'participationType' => ParticipationType::where(['slug' => $this->resource->participation_type])->first()->name,
|
|
'needs_payment' => $this->resource->amount->getAmount() > 0 && $event->pay_direct,
|
|
'nicename' => $this->resource->getNicename(),
|
|
'arrival' => $this->resource->arrival_date->format('d.m.Y'),
|
|
'departure' => $this->resource->departure_date->format('d.m.Y'),
|
|
'amount_left_string' => $amountLeft->toString(),
|
|
'amount_left_value' => $amountLeft->getAmount(),
|
|
'email_1' => $this->resource->email_1,
|
|
'amountPaid' => ['value' => $this->resource->amount_paid, 'readable' => $this->resource->amount_paid?->toString() ?? '0,00 Euro'],
|
|
'amountExpected' => ['value' => $this->resource->amount, 'readable' => $this->resource->amount?->toString() ?? '0,00 Euro'],
|
|
'alcoholicsAllowed' => new Age($this->resource->birthday)->getAge() >= $event->alcoholics_age,
|
|
'localGroupPostcode' => $this->resource->localGroup()->first()->postcode,
|
|
'localGroupCity' => $this->resource->localGroup()->first()->city,
|
|
'state' => config('postCode.map.' . $this->resource->postcode),
|
|
'localGroupState' => config('postCode.map.' . $this->resource->localGroup()->first()->postcode),
|
|
'birthday' => $this->resource->birthday->format('d.m.Y'),
|
|
]
|
|
);
|
|
}
|
|
}
|