201 lines
8.5 KiB
PHP
201 lines
8.5 KiB
PHP
<?php
|
|
|
|
namespace App\Resources;
|
|
|
|
use App\Enumerations\ParticipationFeeType;
|
|
use App\Models\Event;
|
|
use App\ValueObjects\Amount;
|
|
use DateTime;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class EventResource extends JsonResource{
|
|
private Event $event;
|
|
|
|
public function __construct(Event $event) {
|
|
$this->event = $event;
|
|
}
|
|
|
|
public function toArray(Request $request) : array
|
|
{
|
|
$duration = $this->event->end_date->diff($this->event->start_date)->days + 1;
|
|
|
|
$returnArray = [
|
|
'id' => $this->event->id,
|
|
'name' => $this->event->name,
|
|
'location' => $this->event->location,
|
|
'postalCode' => $this->event->postal_code,
|
|
'email' => $this->event->email,
|
|
'accountOwner' => $this->event->account_owner,
|
|
'accountIban' => $this->event->account_iban,
|
|
'accountOwner' => $this->event->account_owner,
|
|
'accountIban' => $this->event->account_iban,
|
|
'alcoholicsAge' => $this->event->alcoholics_age,
|
|
'sendWeeklyReports' => $this->event->send_weekly_report,
|
|
'registrationAllowed' => $this->event->registration_allowed,
|
|
'earlyBirdEnd' => ['internal' => $this->event->early_bird_end->format('Y-m-d'), 'formatted' => $this->event->early_bird_end->format('d.m.Y')],
|
|
'registrationFinalEnd' => ['internal' => $this->event->registration_final_end->format('Y-m-d'), 'formatted' => $this->event->registration_final_end->format('d.m.Y')],
|
|
'refundAfterEarlyBirdEnd' => 100 - $this->event->early_bird_end_amount_increase,
|
|
];
|
|
|
|
$returnArray['swimmingPermissions'] = \App\Enumerations\SwimmingPermission::query()
|
|
->get()
|
|
->map(fn ($permission) => [
|
|
'slug' => $permission->slug,
|
|
'name' => $permission->name,
|
|
])
|
|
->toArray();
|
|
|
|
$returnArray['firstAidPermissions'] = \App\Enumerations\FirstAidPermission::query()
|
|
->get()
|
|
->map(fn ($permission) => [
|
|
'slug' => $permission->slug,
|
|
'name' => $permission->name,
|
|
])
|
|
->toArray();
|
|
|
|
|
|
$returnArray['costUnit'] = new CostUnitResource($this->event->costUnit()->first())->toArray(true);
|
|
$returnArray['solidarityPayment'] = $this->event->participation_fee_type === ParticipationFeeType::PARTICIPATION_FEE_TYPE_SOLIDARITY;
|
|
$returnArray['payPerDay'] = $this->event->pay_per_day;
|
|
$returnArray['maxAmount'] = $this->event->total_max_amount->getFormattedAmount();
|
|
$returnArray['eventBegin'] = $this->event->start_date->format('d.m.Y');
|
|
$returnArray['eventBeginInternal'] = $this->event->start_date;
|
|
$returnArray['eventEnd'] = $this->event->end_date->format('d.m.Y');
|
|
$returnArray['eventEndInternal'] = $this->event->end_date;
|
|
$returnArray['duration'] = $duration;
|
|
#
|
|
$totalBalance = new Amount(0, 'Euro');
|
|
$totalBalance->addAmount($this->calculateIncomes());
|
|
$totalBalance->subtractAmount($returnArray['costUnit']['overAllAmount']['value']);
|
|
|
|
$returnArray['totalIncome'] = $this->calculateIncomes()->toString();
|
|
$returnArray['totalBalance'] = ['text' => $totalBalance->toString(), 'value' => $totalBalance->getAmount()];
|
|
$returnArray['flatSupport'] = $this->event->support_flat->toString();
|
|
$returnArray['supportPerson'] = $this->event->support_per_person->toString();
|
|
|
|
$returnArray['flatSupportEdit'] = $this->event->support_flat->getFormattedAmount();
|
|
$returnArray['supportPersonEdit'] = $this->event->support_per_person->getFormattedAmount();
|
|
$returnArray['managers'] = $this->event->eventManagers()->get()->map(fn($user) => new UserResource($user))->toArray();
|
|
|
|
$returnArray['supportPersonCalced'] = $this->event->support_per_person->toString();
|
|
$returnArray['contributingLocalGroups'] = $this->event->localGroups
|
|
->map(fn ($localGroup) => new LocalGroupResource($localGroup)->toArray($request))
|
|
->toArray();
|
|
|
|
$returnArray['eatingHabits'] = $this->event->eatingHabits()->get()->map(
|
|
fn($eatingHabit) => new EatingHabitResource($eatingHabit))->toArray();
|
|
|
|
|
|
$returnArray['participationTypes'] = [];
|
|
|
|
$multiplier = $this->getMultiplier();
|
|
|
|
for ($i = 1; $i <= 4; $i++) {
|
|
$returnArray['participationFee_' . $i] = [
|
|
'active' => false,
|
|
'name' => '',
|
|
'description' => '',
|
|
'amount_standard' => null,
|
|
'amount_reduced' => null,
|
|
'amount_solidarity' => null,
|
|
'type' => null,
|
|
];
|
|
|
|
if ($this->event->{'participation_fee_' . $i} === null) {
|
|
continue;
|
|
}
|
|
|
|
$participationFee = $this->event->{'participationFee' . $i}()->first();
|
|
|
|
$feeType = [
|
|
'active' => true,
|
|
'amount_standard_edit' => $participationFee->amount_standard->getFormattedAmount(),
|
|
'amount_standard' => [
|
|
'internal' => [
|
|
'amount' => $participationFee->amount_standard->getAmount(),
|
|
'currency' => $participationFee->amount_standard->getCurrency(),
|
|
],
|
|
'readable' => $participationFee->amount_standard->multiply($multiplier)->toString(),
|
|
],
|
|
'amount_reduced_edit' => $participationFee->amount_reduced === null ? null : $participationFee->amount_reduced->getFormattedAmount(),
|
|
'amount_reduced' => $participationFee->amount_reduced === null
|
|
? null
|
|
: [
|
|
'internal' => [
|
|
'amount' => $participationFee->amount_reduced->getAmount(),
|
|
'currency' => $participationFee->amount_reduced->getCurrency(),
|
|
],
|
|
'readable' => $participationFee->amount_reduced->multiply($multiplier)->toString(),
|
|
],
|
|
'amount_solidarity_edit' => $participationFee->amount_solidarity === null ? null : $participationFee->amount_solidarity->getFormattedAmount(),
|
|
'amount_solidarity' => $participationFee->amount_solidarity === null
|
|
? null
|
|
: [
|
|
'internal' => [
|
|
'amount' => $participationFee->amount_solidarity->getAmount(),
|
|
'currency' => $participationFee->amount_solidarity->getCurrency(),
|
|
],
|
|
'readable' => $participationFee->amount_solidarity->multiply($multiplier)->toString(),
|
|
],
|
|
'name' => $participationFee->name,
|
|
'description' => $participationFee->description,
|
|
'type' => (new ParticipationTypeResource($participationFee->type()->first()))->toArray($request),
|
|
];
|
|
|
|
$returnArray['participationFee_' . $i] = $feeType;
|
|
$returnArray['participationTypes'][] = $feeType;
|
|
|
|
}
|
|
|
|
return $returnArray;
|
|
}
|
|
|
|
public function getMultiplier() : float {
|
|
$earlyBirdEnd = $this->event->early_bird_end;
|
|
if ($earlyBirdEnd > now()) {
|
|
return 1;
|
|
}
|
|
|
|
return 1 + $this->event->early_bird_end_amount_increase / 100;
|
|
}
|
|
|
|
public function calculateIncomes() : Amount {
|
|
$amount = new Amount(0, 'Euro');
|
|
$amount->addAmount($this->event->support_flat);
|
|
return $amount;
|
|
}
|
|
|
|
public function calculateAmount(
|
|
string $participationType,
|
|
string $feeType,
|
|
DateTime $arrival,
|
|
DateTime $departure
|
|
) : Amount {
|
|
$fee = collect([
|
|
$this->event->participationFee1,
|
|
$this->event->participationFee2,
|
|
$this->event->participationFee3,
|
|
$this->event->participationFee4,
|
|
])->filter(fn ($participationFee) => $participationFee !== null)
|
|
->first(fn ($participationFee) => $participationFee->type === $participationType);
|
|
|
|
if ($fee === null) {
|
|
return new Amount(0, 'Euro');
|
|
}
|
|
|
|
/** @var Amount $basicFee */
|
|
$basicFee = $fee['amount_' . $feeType];
|
|
$basicFee = $basicFee->multiply($this->getMultiplier());
|
|
|
|
if ($this->event->pay_per_day) {
|
|
$days = $arrival->diff($departure)->days;
|
|
$basicFee = $basicFee->multiply($days);
|
|
}
|
|
|
|
return $basicFee;
|
|
}
|
|
}
|
|
|
|
|