feat(tickets): refine FFI variant filters

This commit is contained in:
2026-09-01 14:29:26 -03:00
parent 6896646c54
commit 836aa1afd7
6 changed files with 300 additions and 74 deletions

View File

@@ -23,7 +23,7 @@ class TicketFilterFormService
if ($tenant->codigo === self::FIESTA_FUTBOL_INFANTIL) {
$fields = [
...$this->fiestaFutbolInfantilFields($tenant),
...$fields,
...$this->commonFields(includeDate: false),
];
}
@@ -55,20 +55,40 @@ class TicketFilterFormService
'value' => $category['value'],
'label' => $category['label'],
'children' => [
'field' => 'product',
'disabled' => $category['products'] === [],
'options' => array_map(
fn (array $product): array => [
'value' => $product['value'],
'label' => $product['label'],
'children' => [
'field' => 'type',
'disabled' => $product['types'] === [],
'options' => $product['types'],
[
'field' => 'product',
'disabled' => $category['products'] === [],
'options' => array_map(
fn (array $product): array => [
'value' => $product['value'],
'label' => $product['label'],
'children' => [
[
'field' => 'type',
'disabled' => $product['types'] === [],
'options' => array_map(
fn (array $type): array => [
'value' => $type['value'],
'label' => $type['label'],
'children' => [[
'field' => 'size',
'disabled' => ($type['sizes'] ?? []) === [],
'options' => $type['sizes'] ?? [],
]],
],
$product['types'],
),
],
],
],
],
$category['products'],
),
$category['products'],
),
],
[
'field' => 'date',
'disabled' => ! in_array($category['value'], ['comidas', 'comida'], true),
'options' => [],
],
],
],
$form['categories'],
@@ -76,21 +96,26 @@ class TicketFilterFormService
],
$this->dependentSelect('product', 'Producto', 'category'),
$this->dependentSelect('type', 'Tipo', 'product'),
$this->dependentSelect('size', 'Talle', 'type'),
[
...$this->dependentSelect('date', 'Fecha', 'category'),
'type' => 'date',
],
];
}
/** @return list<array<string, mixed>> */
private function commonFields(): array
private function commonFields(bool $includeDate = true): array
{
return [
[
...($includeDate ? [[
'name' => 'date',
'query_param' => 'date',
'label' => 'Fecha',
'type' => 'date',
'required' => false,
'default' => null,
],
]] : []),
[
'name' => 'status',
'query_param' => 'status',

View File

@@ -54,6 +54,18 @@ class TicketFormService
],
];
/**
* @var array<string, array{label: string|null, product: string, type: string|null, size: string|null, order: int}>
*/
private const FILTER_CATEGORY_PRESENTATIONS = [
'entradas' => ['label' => null, 'product' => self::PRODUCT, 'type' => null, 'size' => null, 'order' => 1],
'alojamientos' => ['label' => 'Camping', 'product' => 'tipo_alojamiento', 'type' => null, 'size' => null, 'order' => 2],
'camping' => ['label' => null, 'product' => 'tipo_alojamiento', 'type' => null, 'size' => null, 'order' => 2],
'comidas' => ['label' => 'Comida', 'product' => 'horario', 'type' => 'servicio', 'size' => null, 'order' => 3],
'comida' => ['label' => null, 'product' => 'horario', 'type' => 'servicio', 'size' => null, 'order' => 3],
'merchandising' => ['label' => null, 'product' => self::PRODUCT, 'type' => 'color', 'size' => 'talle', 'order' => 4],
];
/**
* @return array{
* statuses: list<array{value: string, label: string}>,
@@ -79,7 +91,7 @@ class TicketFormService
->orderBy('nombre')
->get();
return $this->build($items);
return $this->build($items, self::CATEGORY_PRESENTATIONS);
}
/**
@@ -154,7 +166,7 @@ class TicketFormService
->orderBy('nombre')
->get();
return $this->build($items);
return $this->build($items, self::FILTER_CATEGORY_PRESENTATIONS, includeSizes: true);
}
/**
@@ -172,17 +184,18 @@ class TicketFormService
* }>
* }
*/
private function build(Collection $items): array
private function build(Collection $items, array $presentations, bool $includeSizes = false): array
{
$categories = [];
foreach ($items as $item) {
$sourceCategory = trim((string) $item->category?->nombre);
$categoryValue = mb_strtolower($sourceCategory);
$presentation = self::CATEGORY_PRESENTATIONS[$categoryValue] ?? [
$presentation = $presentations[$categoryValue] ?? [
'label' => null,
'product' => self::PRODUCT,
'type' => null,
'size' => null,
'order' => PHP_INT_MAX,
];
@@ -193,18 +206,28 @@ class TicketFormService
'products' => [],
];
foreach ($this->products($item, $presentation['product'], $presentation['type']) as $product) {
foreach ($this->products(
$item,
$presentation['product'],
$presentation['type'],
$presentation['size'] ?? null,
) as $product) {
$productValue = $product['value'];
$existingProduct = $categories[$categoryValue]['products'][$productValue] ?? [
'value' => $productValue,
'label' => $product['label'],
'types' => [],
'sizes' => [],
];
foreach ($product['types'] as $type) {
$existingProduct['types'][$type['value']] = $type;
}
foreach ($product['sizes'] as $size) {
$existingProduct['sizes'][$size['value']] = $size;
}
$categories[$categoryValue]['products'][$productValue] = $existingProduct;
}
}
@@ -227,6 +250,7 @@ class TicketFormService
'value' => $product['value'],
'label' => $product['label'],
'types' => array_values($product['types']),
...($includeSizes ? ['sizes' => array_values($product['sizes'])] : []),
],
$category['products'],
)),
@@ -252,16 +276,18 @@ class TicketFormService
* @return list<array{
* value: string,
* label: string,
* types: list<array{value: string, label: string}>
* types: list<array{value: string, label: string}>,
* sizes: list<array{value: string, label: string}>
* }>
*/
private function products(CatalogItem $item, string $productCode, ?string $typeCode): array
private function products(CatalogItem $item, string $productCode, ?string $typeCode, ?string $sizeCode): array
{
if ($productCode === self::PRODUCT) {
return [[
'value' => $item->slug,
'label' => $item->nombre,
'types' => $this->types($item, $typeCode),
'types' => $this->types($item, $typeCode, $sizeCode),
'sizes' => $this->types($item, $sizeCode),
]];
}
@@ -274,10 +300,20 @@ class TicketFormService
'value' => $productValue,
'label' => $this->optionLabel($productOption['label'], $productCode),
'types' => [],
'sizes' => [],
];
foreach ($this->variantOptions($variant, $typeCode) as $typeOption) {
$products[$productValue]['types'][$typeOption['value']] = $typeOption;
$this->mergeTypeOption(
$products[$productValue]['types'],
$typeOption,
$variant,
$sizeCode,
);
}
foreach ($this->variantOptions($variant, $sizeCode) as $sizeOption) {
$products[$productValue]['sizes'][$sizeOption['value']] = $sizeOption;
}
}
}
@@ -287,23 +323,51 @@ class TicketFormService
'value' => $product['value'],
'label' => $product['label'],
'types' => array_values($product['types']),
'sizes' => array_values($product['sizes']),
],
$products,
));
}
/** @return list<array{value: string, label: string}> */
private function types(CatalogItem $item, ?string $typeCode): array
/** @return list<array<string, mixed>> */
private function types(CatalogItem $item, ?string $typeCode, ?string $sizeCode = null): array
{
$types = [];
foreach ($item->variants as $variant) {
foreach ($this->variantOptions($variant, $typeCode) as $typeOption) {
$types[$typeOption['value']] = $typeOption;
$this->mergeTypeOption($types, $typeOption, $variant, $sizeCode);
}
}
return array_values($types);
return array_values(array_map(function (array $type) use ($sizeCode): array {
if ($sizeCode !== null) {
$type['sizes'] = array_values($type['sizes']);
}
return $type;
}, $types));
}
/**
* @param array<string, array<string, mixed>> $types
* @param array{value: string, label: string} $typeOption
*/
private function mergeTypeOption(
array &$types,
array $typeOption,
Variant $variant,
?string $sizeCode,
): void {
$typeValue = $typeOption['value'];
$types[$typeValue] ??= [
...$typeOption,
...($sizeCode !== null ? ['sizes' => []] : []),
];
foreach ($this->variantOptions($variant, $sizeCode) as $sizeOption) {
$types[$typeValue]['sizes'][$sizeOption['value']] = $sizeOption;
}
}
/** @return list<array{value: string, label: string}> */

View File

@@ -28,6 +28,7 @@ class AdminAppTicketIndexRequest extends FormRequest
'product' => ['sometimes', 'nullable', 'string', 'max:255'],
'type' => ['sometimes', 'nullable', 'string', 'max:255'],
'date' => ['sometimes', 'nullable', 'date_format:Y-m-d'],
'size' => ['sometimes', 'nullable', 'string', 'max:255'],
'status' => [
'sometimes',
'nullable',

View File

@@ -27,7 +27,7 @@ class AdminAppTicketService
) {}
/**
* @param array{q?: string|null, category?: string|null, product?: string|null, type?: string|null, date?: string|null, status?: string|null, page?: int, per_page?: int, sort_by?: string|null, sort_direction?: string|null} $filters
* @param array{q?: string|null, category?: string|null, product?: string|null, type?: string|null, date?: string|null, size?: string|null, status?: string|null, page?: int, per_page?: int, sort_by?: string|null, sort_direction?: string|null} $filters
*/
public function search(Tenant $tenant, array $filters = []): AdminAppTicketResult
{
@@ -60,7 +60,7 @@ class AdminAppTicketService
}
/**
* @param array{q?: string|null, category?: string|null, product?: string|null, type?: string|null, date?: string|null, status?: string|null, sort_by?: string|null, sort_direction?: string|null} $filters
* @param array{q?: string|null, category?: string|null, product?: string|null, type?: string|null, date?: string|null, size?: string|null, status?: string|null, sort_by?: string|null, sort_direction?: string|null} $filters
* @return Collection<int, Ticket>
*/
public function ticketsForExport(Tenant $tenant, array $filters = []): Collection
@@ -76,7 +76,7 @@ class AdminAppTicketService
}
/**
* @param array{q?: string|null, category?: string|null, product?: string|null, type?: string|null, date?: string|null, status?: string|null, page?: int, per_page?: int} $filters
* @param array{q?: string|null, category?: string|null, product?: string|null, type?: string|null, date?: string|null, size?: string|null, status?: string|null, page?: int, per_page?: int} $filters
* @return Builder<Ticket>
*/
private function baseQuery(Tenant $tenant, array $filters): Builder
@@ -104,9 +104,19 @@ class AdminAppTicketService
->when($filters['type'] ?? null, function (Builder $query, string $type) use ($filters): void {
$this->applyTypeFilter($query, (string) ($filters['category'] ?? ''), $type);
})
->when($filters['date'] ?? null, fn (Builder $query, string $date): Builder => $query
->whereHas('sourcePurchase', fn (Builder $purchaseQuery): Builder => $purchaseQuery
->whereDate('created_at', $date)));
->when($filters['date'] ?? null, function (Builder $query, string $date) use ($tenant): void {
if ($tenant->codigo === 'fiesta_futbol_infantil') {
$this->applyEventDateFilter($query, $date);
return;
}
$query->whereHas('sourcePurchase', fn (Builder $purchaseQuery): Builder => $purchaseQuery
->whereDate('created_at', $date));
})
->when($filters['size'] ?? null, function (Builder $query, string $size) use ($filters): void {
$this->applySizeFilter($query, (string) ($filters['category'] ?? ''), $size);
});
$this->applyStatusFilter($query, $filters['status'] ?? null);
@@ -125,14 +135,7 @@ class AdminAppTicketService
}
if (in_array($category, ['comidas', 'comida'], true)) {
$query->whereHas('sourceVariant', function (Builder $variantQuery) use ($product): void {
$variantQuery->where(function (Builder $dateQuery) use ($product): void {
$dateQuery
->where('event_date_id', $product)
->orWhereHas('eventDates', fn (Builder $eventDateQuery): Builder => $eventDateQuery
->whereKey($product));
});
});
$this->whereVariantDefinition($query, 'horario', $product);
return;
}
@@ -145,7 +148,7 @@ class AdminAppTicketService
private function applyTypeFilter(Builder $query, string $category, string $type): void
{
$attribute = match ($this->normalizedCategory($category)) {
'comidas', 'comida' => 'horario',
'comidas', 'comida' => 'servicio',
'merchandising' => 'color',
default => null,
};
@@ -155,6 +158,31 @@ class AdminAppTicketService
}
}
/** @param Builder<Ticket> $query */
private function applyEventDateFilter(Builder $query, string $date): void
{
$query
->whereHas('sourceCatalogItem.category', fn (Builder $categoryQuery): Builder => $categoryQuery
->whereRaw('LOWER(nombre) IN (?, ?)', ['comidas', 'comida']))
->whereHas('sourceVariant', function (Builder $variantQuery) use ($date): void {
$variantQuery->where(function (Builder $dateQuery) use ($date): void {
$dateQuery
->whereHas('eventDate', fn (Builder $eventDateQuery): Builder => $eventDateQuery
->whereDate('date', $date))
->orWhereHas('eventDates', fn (Builder $eventDateQuery): Builder => $eventDateQuery
->whereDate('date', $date));
});
});
}
/** @param Builder<Ticket> $query */
private function applySizeFilter(Builder $query, string $category, string $size): void
{
if ($this->normalizedCategory($category) === 'merchandising') {
$this->whereVariantDefinition($query, 'talle', $size);
}
}
/** @param Builder<Ticket> $query */
private function whereVariantDefinition(Builder $query, string $attribute, string $value): void
{