Files
mareike/app/Resources/EventParticipantResource.php
2026-03-25 20:28:04 +01:00

52 lines
1.7 KiB
PHP

<?php
namespace App\Resources;
use App\Enumerations\ParticipationType;
use App\Models\EventParticipant;
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(),
[
'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,
]
);
}
}