157 lines
5.8 KiB
PHP
157 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\FiestaFutbolInfantil\Resources;
|
|
|
|
use App\Domains\Catalog\Models\CatalogItem;
|
|
use App\Domains\Catalog\Models\Variant;
|
|
use App\Domains\Event\Enums\EventDateChangeType;
|
|
use App\Domains\Event\Enums\EventDateStatus;
|
|
use App\Domains\Event\Models\EventDate;
|
|
use App\Domains\Event\Models\EventDateChange;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
use Illuminate\Support\Collection;
|
|
|
|
/** @mixin CatalogItem */
|
|
class FoodResource extends JsonResource
|
|
{
|
|
/** @return array<string, mixed> */
|
|
public function toArray(Request $request): array
|
|
{
|
|
if ($this->resource === null) {
|
|
return [
|
|
'id' => null,
|
|
'name' => 'Comida',
|
|
'variants' => [],
|
|
'history' => [],
|
|
];
|
|
}
|
|
|
|
$currentVariants = $this->variants
|
|
->filter(fn (Variant $variant): bool => $this->isCurrent($variant));
|
|
$historicalVariants = $this->variants
|
|
->filter(fn (Variant $variant): bool => $this->isHistorical($variant));
|
|
|
|
return [
|
|
'id' => $this->id,
|
|
'name' => $this->nombre,
|
|
'variants' => $currentVariants->map($this->variantData(...))->values(),
|
|
'history' => $this->historyData($historicalVariants),
|
|
];
|
|
}
|
|
|
|
private function isCurrent(Variant $variant): bool
|
|
{
|
|
if ($variant->sales_disabled_at !== null || $variant->replaced_by_variant_id !== null) {
|
|
return false;
|
|
}
|
|
|
|
$status = $variant->selectedEventDates()->first()?->status;
|
|
|
|
return $status === null || in_array(
|
|
$status,
|
|
[EventDateStatus::Scheduled, EventDateStatus::InProgress],
|
|
true,
|
|
);
|
|
}
|
|
|
|
private function isHistorical(Variant $variant): bool
|
|
{
|
|
return in_array(
|
|
$variant->selectedEventDates()->first()?->status,
|
|
[
|
|
EventDateStatus::Rescheduled,
|
|
EventDateStatus::Suspended,
|
|
EventDateStatus::Completed,
|
|
],
|
|
true,
|
|
);
|
|
}
|
|
|
|
/** @return array<string, mixed> */
|
|
private function variantData(Variant $variant): array
|
|
{
|
|
$values = $variant->selectionValues();
|
|
$eventDate = $variant->selectedEventDates()->first();
|
|
|
|
return [
|
|
'id' => $variant->id,
|
|
'event_date_id' => $eventDate?->id,
|
|
'event_date' => $eventDate?->date?->format('Y-m-d'),
|
|
'schedule' => $values->get('horario'),
|
|
'service' => $values->get('servicio'),
|
|
'description' => $variant->descripcion,
|
|
'stock' => $variant->inventory->real_stock,
|
|
'price' => number_format($variant->getPrice(), 2, '.', ''),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param Collection<int, Variant> $variants
|
|
* @return Collection<int, array<string, mixed>>
|
|
*/
|
|
private function historyData(Collection $variants): Collection
|
|
{
|
|
return $variants
|
|
->filter(fn (Variant $variant): bool => $variant->selectedEventDates()->first() !== null)
|
|
->groupBy(fn (Variant $variant): int => (int) $variant->selectedEventDates()->first()->id)
|
|
->map(function (Collection $dateVariants): array {
|
|
/** @var EventDate $eventDate */
|
|
$eventDate = $dateVariants->first()->selectedEventDates()->first();
|
|
$status = $this->historicalStatus($eventDate);
|
|
$change = $this->changeForStatus($eventDate, $status);
|
|
|
|
return [
|
|
'id' => $eventDate->id,
|
|
'change_id' => $change?->id,
|
|
'status' => $status->value,
|
|
'status_text' => match ($status) {
|
|
EventDateStatus::Rescheduled => 'REPROGRAMADA',
|
|
EventDateStatus::Suspended => 'CANCELADA',
|
|
EventDateStatus::Completed => 'FINALIZADA',
|
|
default => '',
|
|
},
|
|
'event_date_id' => $eventDate->id,
|
|
'event_date' => $eventDate->date->format('Y-m-d'),
|
|
'replacement_event_date_id' => $status === EventDateStatus::Rescheduled
|
|
? ($change?->destination_event_date_id
|
|
?? $eventDate->rescheduled_to_event_date_id)
|
|
: null,
|
|
'replacement_event_date' => $status === EventDateStatus::Rescheduled
|
|
? ($change?->new_date?->format('Y-m-d')
|
|
?? $eventDate->rescheduledTo?->date?->format('Y-m-d'))
|
|
: null,
|
|
'occurred_at' => ($change?->created_at ?? $eventDate->endsAt())->toISOString(),
|
|
'variants' => $dateVariants->map($this->variantData(...))->values(),
|
|
];
|
|
})
|
|
->sortByDesc('occurred_at')
|
|
->values();
|
|
}
|
|
|
|
private function historicalStatus(EventDate $eventDate): EventDateStatus
|
|
{
|
|
return match ($eventDate->status) {
|
|
EventDateStatus::Rescheduled => EventDateStatus::Rescheduled,
|
|
EventDateStatus::Suspended => EventDateStatus::Suspended,
|
|
default => EventDateStatus::Completed,
|
|
};
|
|
}
|
|
|
|
private function changeForStatus(
|
|
EventDate $eventDate,
|
|
EventDateStatus $status,
|
|
): ?EventDateChange {
|
|
$changeType = match ($status) {
|
|
EventDateStatus::Rescheduled => EventDateChangeType::Rescheduled,
|
|
EventDateStatus::Suspended => EventDateChangeType::Suspended,
|
|
default => null,
|
|
};
|
|
|
|
return $changeType === null
|
|
? null
|
|
: $eventDate->changeHistory
|
|
->first(fn (EventDateChange $change): bool => $change->change_type === $changeType);
|
|
}
|
|
}
|