feat(desfile): enhance entry reservation form and service with tenant-specific data
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Domains\Commerce\Catalog\Models;
|
||||
|
||||
use App\Shared\Attachable\Models\Attachment;
|
||||
use App\Domains\Commerce\Catalog\Enums\CatalogItemType;
|
||||
use App\Domains\Commerce\Catalog\Enums\InventoryPolicy;
|
||||
use App\Domains\Commerce\Catalog\Enums\InventorySubject;
|
||||
@@ -10,6 +9,7 @@ use App\Domains\Commerce\Catalog\Services\CatalogInventoryService;
|
||||
use App\Domains\Core\Tenant\Models\Tenant;
|
||||
use App\Domains\Ticketing\Event\Models\Event;
|
||||
use App\Domains\Ticketing\Ticket\Models\Ticket;
|
||||
use App\Shared\Attachable\Models\Attachment;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
@@ -264,9 +264,15 @@ class CatalogItem extends Model
|
||||
/** @return Collection<int, Variant> */
|
||||
public function visibleVariants(?int $includedVariantId = null): Collection
|
||||
{
|
||||
$this->variants
|
||||
->filter(fn (Variant $variant): bool => $variant->exists)
|
||||
->loadMissing('desfileEntryReservations');
|
||||
|
||||
return $this->variants
|
||||
->each(fn (Variant $variant) => $variant->setRelation('catalogItem', $this))
|
||||
->filter(fn (Variant $variant): bool => $variant->hasOnlyActiveEventDates()
|
||||
&& (! $variant->relationLoaded('desfileEntryReservations')
|
||||
|| $variant->desfileEntryReservations->isEmpty())
|
||||
&& (($includedVariantId !== null && $variant->id === $includedVariantId)
|
||||
|| ($variant->isSellable() && (
|
||||
$this->inventory_policy === InventoryPolicy::Unlimited
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Shared\Forms\Controllers\AdminApp;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Shared\Forms\Resources\DesfileEntryReservationFormResource;
|
||||
use App\Shared\Forms\Services\DesfileEntryReservationFormService;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class DesfileEntryReservationFormController extends Controller
|
||||
{
|
||||
@@ -12,8 +13,10 @@ class DesfileEntryReservationFormController extends Controller
|
||||
private readonly DesfileEntryReservationFormService $formService,
|
||||
) {}
|
||||
|
||||
public function __invoke(): DesfileEntryReservationFormResource
|
||||
public function __invoke(Request $request): DesfileEntryReservationFormResource
|
||||
{
|
||||
return DesfileEntryReservationFormResource::make($this->formService->get());
|
||||
return DesfileEntryReservationFormResource::make(
|
||||
$this->formService->get($request->user()->tenant()->firstOrFail()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ class DesfileEntryReservationFormResource extends JsonResource
|
||||
{
|
||||
return [
|
||||
'payment_types' => $this->resource['payment_types'],
|
||||
'fields' => $this->resource['fields'],
|
||||
'variants' => $this->resource['variants'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,15 +2,48 @@
|
||||
|
||||
namespace App\Shared\Forms\Services;
|
||||
|
||||
use App\Domains\Commerce\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Commerce\Catalog\Models\Variant;
|
||||
use App\Domains\Core\Tenant\Models\Tenant;
|
||||
use App\Domains\Ticketing\Desfile\Enums\EntryReservationPaymentType;
|
||||
|
||||
class DesfileEntryReservationFormService
|
||||
{
|
||||
/** @return array{payment_types: list<array{value: string, label: string}>} */
|
||||
public function get(): array
|
||||
/** @return array<string, mixed> */
|
||||
public function get(Tenant $tenant): array
|
||||
{
|
||||
$entry = CatalogItem::query()->forTenantCatalog($tenant)->where('slug', 'entrada')
|
||||
->with([
|
||||
'variants' => fn ($query) => $query->orderBy('id'),
|
||||
'variants.inventory',
|
||||
'variants.definitions.itemAttribute.attribute',
|
||||
'variants.eventDates',
|
||||
'variants.eventDate',
|
||||
])->first();
|
||||
$variants = $entry?->visibleVariants()
|
||||
->map(function (Variant $variant): array {
|
||||
$values = $variant->selectionValues();
|
||||
|
||||
return [
|
||||
'id' => $variant->id,
|
||||
'tipo' => (string) $values->get('tipo'),
|
||||
'sector' => (string) $values->get('sector'),
|
||||
'fila' => (string) $values->get('fila'),
|
||||
'asiento' => (string) $values->get('asiento'),
|
||||
'price' => $variant->getPrice(),
|
||||
];
|
||||
})->values() ?? collect();
|
||||
|
||||
return [
|
||||
'payment_types' => EntryReservationPaymentType::options(),
|
||||
'fields' => collect(['tipo' => 'Tipo', 'sector' => 'Sector', 'fila' => 'Fila', 'asiento' => 'Asiento'])
|
||||
->map(fn (string $label, string $key): array => [
|
||||
'key' => $key,
|
||||
'label' => $label,
|
||||
'options' => $variants->pluck($key)->unique()->sort(SORT_NATURAL)->values()
|
||||
->map(fn (string $value): array => ['value' => $value, 'label' => $value])->all(),
|
||||
])->values()->all(),
|
||||
'variants' => $variants->all(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user