Compare commits
3 Commits
ba167559a6
...
27544a23be
| Author | SHA1 | Date |
|---|---|---|
|
|
27544a23be | |
|
|
a71c80248c | |
|
|
1ca8fb20af |
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace App\Domains\Forms\Controllers\AdminApp;
|
||||
|
||||
use App\Domains\Forms\Resources\TicketFilterFormResource;
|
||||
use App\Domains\Forms\Services\TicketFilterFormService;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class TicketFilterFormController extends Controller
|
||||
{
|
||||
public function __construct(private readonly TicketFilterFormService $formService) {}
|
||||
|
||||
public function __invoke(Request $request): TicketFilterFormResource
|
||||
{
|
||||
$tenant = $request->user('sanctum')->tenant()->firstOrFail();
|
||||
|
||||
return TicketFilterFormResource::make($this->formService->get($tenant));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace App\Domains\Forms\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class TicketFilterFormResource extends JsonResource
|
||||
{
|
||||
/** @return array<string, mixed> */
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'code' => $this->resource['code'],
|
||||
'action' => $this->resource['action'],
|
||||
'method' => $this->resource['method'],
|
||||
'fields' => $this->resource['fields'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,118 @@
|
|||
<?php
|
||||
|
||||
namespace App\Domains\Forms\Services;
|
||||
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Domains\Ticket\Models\Ticket;
|
||||
|
||||
class TicketFilterFormService
|
||||
{
|
||||
private const FIESTA_FUTBOL_INFANTIL = 'fiesta_futbol_infantil';
|
||||
|
||||
public function __construct(private readonly TicketFormService $ticketFormService) {}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function get(Tenant $tenant): array
|
||||
{
|
||||
$fields = $this->commonFields();
|
||||
|
||||
if ($tenant->codigo === self::FIESTA_FUTBOL_INFANTIL) {
|
||||
$fields = [
|
||||
...$this->fiestaFutbolInfantilFields($tenant),
|
||||
...$fields,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'code' => 'tickets_filter',
|
||||
'action' => '/api/v1/adminapp/tenant/tickets',
|
||||
'method' => 'GET',
|
||||
'fields' => $fields,
|
||||
];
|
||||
}
|
||||
|
||||
/** @return list<array<string, mixed>> */
|
||||
private function fiestaFutbolInfantilFields(Tenant $tenant): array
|
||||
{
|
||||
$form = $this->ticketFormService->getForFilters($tenant);
|
||||
|
||||
return [
|
||||
[
|
||||
'name' => 'category',
|
||||
'label' => 'Categoría',
|
||||
'type' => 'select',
|
||||
'required' => false,
|
||||
'default' => null,
|
||||
'placeholder' => 'Categoría',
|
||||
'options' => array_map(
|
||||
fn (array $category): array => [
|
||||
'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'],
|
||||
],
|
||||
],
|
||||
$category['products'],
|
||||
),
|
||||
],
|
||||
],
|
||||
$form['categories'],
|
||||
),
|
||||
],
|
||||
$this->dependentSelect('product', 'Producto', 'category'),
|
||||
$this->dependentSelect('type', 'Tipo', 'product'),
|
||||
];
|
||||
}
|
||||
|
||||
/** @return list<array<string, mixed>> */
|
||||
private function commonFields(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'name' => 'date',
|
||||
'label' => 'Fecha',
|
||||
'type' => 'date',
|
||||
'required' => false,
|
||||
'default' => null,
|
||||
],
|
||||
[
|
||||
'name' => 'status',
|
||||
'label' => 'Estado',
|
||||
'type' => 'select',
|
||||
'required' => false,
|
||||
'default' => null,
|
||||
'placeholder' => 'Estado',
|
||||
'options' => [
|
||||
['value' => Ticket::STATUS_ACTIVE, 'label' => 'Activo'],
|
||||
['value' => Ticket::STATUS_USED, 'label' => 'Usado'],
|
||||
['value' => Ticket::STATUS_EXPIRED, 'label' => 'Vencido'],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function dependentSelect(string $name, string $label, string $dependency): array
|
||||
{
|
||||
return [
|
||||
'name' => $name,
|
||||
'label' => $label,
|
||||
'type' => 'select',
|
||||
'required' => false,
|
||||
'default' => null,
|
||||
'placeholder' => $label,
|
||||
'depends_on' => $dependency,
|
||||
'disabled' => true,
|
||||
'options' => [],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ use App\Domains\Catalog\Models\CatalogItem;
|
|||
use App\Domains\Catalog\Models\Variant;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Domains\Ticket\Models\Ticket;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class TicketFormService
|
||||
{
|
||||
|
|
@ -69,15 +70,82 @@ class TicketFormService
|
|||
*/
|
||||
public function get(Tenant $tenant): array
|
||||
{
|
||||
$categories = [];
|
||||
|
||||
$items = CatalogItem::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->where('has_tickets', true)
|
||||
->whereHas('category')
|
||||
->with($this->relations())
|
||||
->orderBy('group_order')
|
||||
->orderBy('nombre')
|
||||
->get();
|
||||
|
||||
return $this->build($items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return active catalog options plus soft-deleted sources still referenced by
|
||||
* tickets, so historical tickets never become impossible to filter.
|
||||
*
|
||||
* @return array{
|
||||
* statuses: list<array{value: string, label: string}>,
|
||||
* categories: list<array{
|
||||
* value: string,
|
||||
* label: string,
|
||||
* products: list<array{
|
||||
* value: string,
|
||||
* label: string,
|
||||
* types: list<array{value: string, label: string}>
|
||||
* }>
|
||||
* }>
|
||||
* }
|
||||
*/
|
||||
public function getForFilters(Tenant $tenant): array
|
||||
{
|
||||
$historicalVariantIds = Ticket::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->whereNotNull('source_variant_id')
|
||||
->distinct()
|
||||
->pluck('source_variant_id')
|
||||
->map(fn ($id): int => (int) $id)
|
||||
->all();
|
||||
$historicalCatalogItemIds = Ticket::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->whereNotNull('source_catalog_item_id')
|
||||
->distinct()
|
||||
->pluck('source_catalog_item_id')
|
||||
->map(fn ($id): int => (int) $id)
|
||||
->merge(
|
||||
Variant::withTrashed()
|
||||
->whereKey($historicalVariantIds)
|
||||
->pluck('catalog_item_id')
|
||||
->map(fn ($id): int => (int) $id),
|
||||
)
|
||||
->unique()
|
||||
->values()
|
||||
->all();
|
||||
|
||||
$items = CatalogItem::withTrashed()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->whereHas('category')
|
||||
->where(function ($query) use ($historicalCatalogItemIds): void {
|
||||
$query
|
||||
->where(function ($activeQuery): void {
|
||||
$activeQuery
|
||||
->whereNull('catalog_items.deleted_at')
|
||||
->where('has_tickets', true);
|
||||
})
|
||||
->orWhereIn('catalog_items.id', $historicalCatalogItemIds);
|
||||
})
|
||||
->with([
|
||||
'category',
|
||||
'itemAttributes.attribute.options',
|
||||
'variants' => fn ($query) => $query
|
||||
->withTrashed()
|
||||
->where(function ($variantQuery) use ($historicalVariantIds): void {
|
||||
$variantQuery
|
||||
->whereNull('variantes.deleted_at')
|
||||
->orWhereIn('variantes.id', $historicalVariantIds);
|
||||
}),
|
||||
'variants.definitions.itemAttribute.attribute.options',
|
||||
'variants.eventDates',
|
||||
'variants.eventDate',
|
||||
|
|
@ -86,6 +154,28 @@ class TicketFormService
|
|||
->orderBy('nombre')
|
||||
->get();
|
||||
|
||||
return $this->build($items);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, CatalogItem> $items
|
||||
* @return array{
|
||||
* statuses: list<array{value: string, label: string}>,
|
||||
* categories: list<array{
|
||||
* value: string,
|
||||
* label: string,
|
||||
* products: list<array{
|
||||
* value: string,
|
||||
* label: string,
|
||||
* types: list<array{value: string, label: string}>
|
||||
* }>
|
||||
* }>
|
||||
* }
|
||||
*/
|
||||
private function build(Collection $items): array
|
||||
{
|
||||
$categories = [];
|
||||
|
||||
foreach ($items as $item) {
|
||||
$sourceCategory = trim((string) $item->category?->nombre);
|
||||
$categoryValue = mb_strtolower($sourceCategory);
|
||||
|
|
@ -146,6 +236,18 @@ class TicketFormService
|
|||
];
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
private function relations(): array
|
||||
{
|
||||
return [
|
||||
'category',
|
||||
'itemAttributes.attribute.options',
|
||||
'variants.definitions.itemAttribute.attribute.options',
|
||||
'variants.eventDates',
|
||||
'variants.eventDate',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{
|
||||
* value: string,
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ use App\Domains\Forms\Controllers\AdminApp\FoodFormController;
|
|||
use App\Domains\Forms\Controllers\AdminApp\MerchandiseFormController;
|
||||
use App\Domains\Forms\Controllers\AdminApp\SaleFormController;
|
||||
use App\Domains\Forms\Controllers\AdminApp\StaffFormController;
|
||||
use App\Domains\Forms\Controllers\AdminApp\TicketFilterFormController;
|
||||
use App\Domains\Forms\Controllers\AdminApp\TicketFormController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
|
|
@ -14,6 +15,9 @@ Route::prefix('v1/adminapp/forms')
|
|||
Route::get('event', EventFormController::class);
|
||||
Route::get('sale', SaleFormController::class);
|
||||
Route::get('staff', StaffFormController::class);
|
||||
Route::get('tickets-filter', TicketFilterFormController::class)
|
||||
->middleware('tenant.menu:adminapp.tickets')
|
||||
->name('adminapp.forms.tickets-filter');
|
||||
Route::get(
|
||||
'fiesta-futbol-infantil/ticket',
|
||||
TicketFormController::class
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@
|
|||
|
||||
namespace App\Domains\Ticket\Requests;
|
||||
|
||||
use App\Domains\Ticket\Models\Ticket;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class AdminAppTicketIndexRequest extends FormRequest
|
||||
{
|
||||
|
|
@ -16,6 +18,19 @@ class AdminAppTicketIndexRequest extends FormRequest
|
|||
{
|
||||
return [
|
||||
'q' => ['sometimes', 'nullable', 'string', 'max:255'],
|
||||
'category' => ['sometimes', 'nullable', 'string', 'max:255'],
|
||||
'product' => ['sometimes', 'nullable', 'string', 'max:255'],
|
||||
'type' => ['sometimes', 'nullable', 'string', 'max:255'],
|
||||
'date' => ['sometimes', 'nullable', 'date_format:Y-m-d'],
|
||||
'status' => [
|
||||
'sometimes',
|
||||
'nullable',
|
||||
Rule::in([
|
||||
Ticket::STATUS_ACTIVE,
|
||||
Ticket::STATUS_USED,
|
||||
Ticket::STATUS_EXPIRED,
|
||||
]),
|
||||
],
|
||||
'page' => ['sometimes', 'integer', 'min:1'],
|
||||
'per_page' => ['sometimes', 'integer', 'min:1', 'max:100'],
|
||||
];
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use Illuminate\Database\Eloquent\Builder;
|
|||
class AdminAppTicketService
|
||||
{
|
||||
/**
|
||||
* @param array{q?: 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, status?: string|null, page?: int, per_page?: int} $filters
|
||||
*/
|
||||
public function search(Tenant $tenant, array $filters = []): AdminAppTicketResult
|
||||
{
|
||||
|
|
@ -36,14 +36,14 @@ class AdminAppTicketService
|
|||
}
|
||||
|
||||
/**
|
||||
* @param array{q?: 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, status?: string|null, page?: int, per_page?: int} $filters
|
||||
* @return Builder<Ticket>
|
||||
*/
|
||||
private function baseQuery(Tenant $tenant, array $filters): Builder
|
||||
{
|
||||
$search = trim((string) ($filters['q'] ?? ''));
|
||||
|
||||
return Ticket::query()
|
||||
$query = Ticket::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->when($search !== '', function (Builder $query) use ($search): void {
|
||||
$query->when(
|
||||
|
|
@ -53,6 +53,102 @@ class AdminAppTicketService
|
|||
fn (Builder $searchQuery): Builder => $searchQuery
|
||||
->where('ticket', 'like', "%{$search}%"),
|
||||
);
|
||||
})
|
||||
->when($filters['category'] ?? null, function (Builder $query, string $category): void {
|
||||
$query->whereHas('sourceCatalogItem.category', fn (Builder $categoryQuery): Builder => $categoryQuery
|
||||
->whereRaw('LOWER(nombre) = ?', [mb_strtolower(trim($category))]));
|
||||
})
|
||||
->when($filters['product'] ?? null, function (Builder $query, string $product) use ($filters): void {
|
||||
$this->applyProductFilter($query, (string) ($filters['category'] ?? ''), $product);
|
||||
})
|
||||
->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)));
|
||||
|
||||
$this->applyStatusFilter($query, $filters['status'] ?? null);
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/** @param Builder<Ticket> $query */
|
||||
private function applyProductFilter(Builder $query, string $category, string $product): void
|
||||
{
|
||||
$category = $this->normalizedCategory($category);
|
||||
|
||||
if (in_array($category, ['alojamientos', 'camping'], true)) {
|
||||
$this->whereVariantDefinition($query, 'tipo_alojamiento', $product);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
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));
|
||||
});
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$query->whereHas('sourceCatalogItem', fn (Builder $itemQuery): Builder => $itemQuery
|
||||
->where('slug', $product));
|
||||
}
|
||||
|
||||
/** @param Builder<Ticket> $query */
|
||||
private function applyTypeFilter(Builder $query, string $category, string $type): void
|
||||
{
|
||||
$attribute = match ($this->normalizedCategory($category)) {
|
||||
'comidas', 'comida' => 'horario',
|
||||
'merchandising' => 'color',
|
||||
default => null,
|
||||
};
|
||||
|
||||
if ($attribute !== null) {
|
||||
$this->whereVariantDefinition($query, $attribute, $type);
|
||||
}
|
||||
}
|
||||
|
||||
/** @param Builder<Ticket> $query */
|
||||
private function whereVariantDefinition(Builder $query, string $attribute, string $value): void
|
||||
{
|
||||
$query->whereHas('sourceVariant.definitions', fn (Builder $definitionQuery): Builder => $definitionQuery
|
||||
->where('value', $value)
|
||||
->whereHas('itemAttribute.attribute', fn (Builder $attributeQuery): Builder => $attributeQuery
|
||||
->where('codigo', $attribute)));
|
||||
}
|
||||
|
||||
/** @param Builder<Ticket> $query */
|
||||
private function applyStatusFilter(Builder $query, ?string $status): void
|
||||
{
|
||||
if ($status === null || $status === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($status === Ticket::STATUS_USED) {
|
||||
$query->whereNotNull('used_at');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$matchingIds = (clone $query)
|
||||
->whereNull('used_at')
|
||||
->with(TicketValidityResolver::RELATIONS)
|
||||
->get()
|
||||
->filter(fn (Ticket $ticket): bool => $ticket->status === $status)
|
||||
->pluck('id');
|
||||
|
||||
$query->whereIn('tickets.id', $matchingIds);
|
||||
}
|
||||
|
||||
private function normalizedCategory(string $category): string
|
||||
{
|
||||
return mb_strtolower(trim($category));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,276 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Forms;
|
||||
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Auth\Models\User;
|
||||
use App\Domains\Authorization\Enums\RoleCode;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Services\CatalogService;
|
||||
use App\Domains\Menu\Models\Menu;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Domains\Ticket\Models\Ticket;
|
||||
use Database\Seeders\AttributeSeeder;
|
||||
use Database\Seeders\AuthorizationSeeder;
|
||||
use Database\Seeders\FiestaFutbolInfantilProductSeeder;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AdminAppTicketFilterFormControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->seed(AuthorizationSeeder::class);
|
||||
}
|
||||
|
||||
public function test_authentication_is_required(): void
|
||||
{
|
||||
$this->getJson('/api/v1/adminapp/forms/tickets-filter')->assertUnauthorized();
|
||||
}
|
||||
|
||||
public function test_the_tenant_must_have_the_tickets_menu(): void
|
||||
{
|
||||
$tenant = $this->createTenant('tenant_without_tickets');
|
||||
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
||||
|
||||
$this->getJson('/api/v1/adminapp/forms/tickets-filter')->assertNotFound();
|
||||
}
|
||||
|
||||
public function test_it_returns_the_common_ticket_filter_fields_for_other_tenants(): void
|
||||
{
|
||||
$tenant = $this->createTenant('another_tenant');
|
||||
$this->grantTicketsMenu($tenant);
|
||||
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
||||
|
||||
$this->getJson('/api/v1/adminapp/forms/tickets-filter')
|
||||
->assertOk()
|
||||
->assertExactJson([
|
||||
'data' => [
|
||||
'code' => 'tickets_filter',
|
||||
'action' => '/api/v1/adminapp/tenant/tickets',
|
||||
'method' => 'GET',
|
||||
'fields' => [
|
||||
[
|
||||
'name' => 'date',
|
||||
'label' => 'Fecha',
|
||||
'type' => 'date',
|
||||
'required' => false,
|
||||
'default' => null,
|
||||
],
|
||||
[
|
||||
'name' => 'status',
|
||||
'label' => 'Estado',
|
||||
'type' => 'select',
|
||||
'required' => false,
|
||||
'default' => null,
|
||||
'placeholder' => 'Estado',
|
||||
'options' => [
|
||||
['value' => 'active', 'label' => 'Activo'],
|
||||
['value' => 'used', 'label' => 'Usado'],
|
||||
['value' => 'expired', 'label' => 'Vencido'],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_returns_the_complete_nested_form_for_fiesta_futbol_infantil(): void
|
||||
{
|
||||
$tenant = $this->createFiestaFutbolInfantilTenant();
|
||||
$this->grantTicketsMenu($tenant);
|
||||
$admin = $this->createAdminAppUser($tenant);
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$response = $this->getJson('/api/v1/adminapp/forms/tickets-filter')
|
||||
->assertOk()
|
||||
->assertJsonPath('data.fields.0.name', 'category')
|
||||
->assertJsonPath('data.fields.1.name', 'product')
|
||||
->assertJsonPath('data.fields.1.depends_on', 'category')
|
||||
->assertJsonPath('data.fields.2.name', 'type')
|
||||
->assertJsonPath('data.fields.2.depends_on', 'product')
|
||||
->assertJsonPath('data.fields.3.name', 'date')
|
||||
->assertJsonPath('data.fields.4.name', 'status');
|
||||
|
||||
$categories = collect($response->json('data.fields.0.options'));
|
||||
$this->assertSame(
|
||||
['Entradas', 'Camping', 'Comida', 'Merchandising'],
|
||||
$categories->pluck('label')->all(),
|
||||
);
|
||||
|
||||
$entries = $categories->firstWhere('value', 'entradas');
|
||||
$this->assertSame('Abono', $entries['children']['options'][0]['label']);
|
||||
$this->assertTrue($entries['children']['options'][0]['children']['disabled']);
|
||||
|
||||
$camping = $categories->firstWhere('value', 'alojamientos');
|
||||
$this->assertSame(
|
||||
['Carpa', 'Motorhome'],
|
||||
collect($camping['children']['options'])->pluck('label')->all(),
|
||||
);
|
||||
$this->assertTrue($camping['children']['options'][0]['children']['disabled']);
|
||||
|
||||
$merchandise = $categories->firstWhere('value', 'merchandising');
|
||||
$this->assertSame('Camiseta', $merchandise['children']['options'][0]['label']);
|
||||
$this->assertSame(
|
||||
['Verde', 'Blanco'],
|
||||
collect($merchandise['children']['options'][0]['children']['options'])->pluck('label')->all(),
|
||||
);
|
||||
}
|
||||
|
||||
public function test_a_food_schedule_is_only_returned_when_a_variant_exists_for_the_date(): void
|
||||
{
|
||||
$tenant = $this->createFiestaFutbolInfantilTenant();
|
||||
$this->grantTicketsMenu($tenant);
|
||||
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
||||
|
||||
$date = $tenant->eventDates()->orderByDesc('date')->firstOrFail();
|
||||
$food = CatalogItem::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->where('slug', 'comida')
|
||||
->with([
|
||||
'itemAttributes.attribute.options',
|
||||
'variants.definitions.itemAttribute.attribute.options',
|
||||
'variants.eventDates',
|
||||
'variants.eventDate',
|
||||
])
|
||||
->firstOrFail();
|
||||
|
||||
$food->variants
|
||||
->filter(fn ($variant): bool => $variant->selectedEventDates()->contains('id', $date->id)
|
||||
&& $variant->selectionValues()->get('horario') === 'Almuerzo')
|
||||
->each->delete();
|
||||
|
||||
$response = $this->getJson('/api/v1/adminapp/forms/tickets-filter')->assertOk();
|
||||
$foodCategory = collect($response->json('data.fields.0.options'))->firstWhere('value', 'comidas');
|
||||
$dateProduct = collect($foodCategory['children']['options'])
|
||||
->firstWhere('value', (string) $date->id);
|
||||
$schedules = collect($dateProduct['children']['options'])->pluck('value');
|
||||
|
||||
$this->assertNotContains('Almuerzo', $schedules);
|
||||
$this->assertContains('Desayuno', $schedules);
|
||||
$this->assertContains('Cena', $schedules);
|
||||
}
|
||||
|
||||
public function test_a_deleted_food_variant_remains_in_the_filter_when_a_ticket_references_it(): void
|
||||
{
|
||||
$tenant = $this->createFiestaFutbolInfantilTenant();
|
||||
$this->grantTicketsMenu($tenant);
|
||||
$admin = $this->createAdminAppUser($tenant);
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$food = CatalogItem::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->where('slug', 'comida')
|
||||
->with([
|
||||
'itemAttributes.attribute.options',
|
||||
'variants.definitions.itemAttribute.attribute.options',
|
||||
'variants.eventDates',
|
||||
'variants.eventDate',
|
||||
])
|
||||
->firstOrFail();
|
||||
$historicalVariant = $food->variants->first(function ($variant): bool {
|
||||
return $variant->selectedEventDates()->first()?->date->format('d/m') === '12/10'
|
||||
&& $variant->selectionValues()->get('horario') === 'Cena';
|
||||
});
|
||||
$this->assertNotNull($historicalVariant);
|
||||
|
||||
$ticket = Ticket::query()->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'ticket' => (string) Str::uuid(),
|
||||
'user_id' => $admin->id,
|
||||
'source_catalog_item_id' => $food->id,
|
||||
'source_variant_id' => $historicalVariant->id,
|
||||
]);
|
||||
|
||||
$catalogService = app(CatalogService::class);
|
||||
foreach ($food->variants as $variant) {
|
||||
$catalogService->deleteVariant($variant);
|
||||
}
|
||||
|
||||
$this->assertTrue(CatalogItem::withTrashed()->findOrFail($food->id)->trashed());
|
||||
|
||||
$response = $this->getJson('/api/v1/adminapp/forms/tickets-filter')->assertOk();
|
||||
$foodCategory = collect($response->json('data.fields.0.options'))->firstWhere('value', 'comidas');
|
||||
$products = collect($foodCategory['children']['options']);
|
||||
|
||||
$this->assertCount(1, $products);
|
||||
$this->assertSame('12/10', $products->first()['label']);
|
||||
$this->assertSame(
|
||||
['Cena'],
|
||||
collect($products->first()['children']['options'])->pluck('label')->all(),
|
||||
);
|
||||
|
||||
$this->getJson('/api/v1/adminapp/tenant/tickets?'.http_build_query([
|
||||
'category' => 'comidas',
|
||||
'product' => $products->first()['value'],
|
||||
'type' => 'Cena',
|
||||
]))
|
||||
->assertOk()
|
||||
->assertJsonCount(1, 'data')
|
||||
->assertJsonPath('data.0.id', $ticket->id);
|
||||
}
|
||||
|
||||
private function createFiestaFutbolInfantilTenant(): Tenant
|
||||
{
|
||||
$tenant = $this->createTenant('fiesta_futbol_infantil');
|
||||
$this->seed([AttributeSeeder::class, FiestaFutbolInfantilProductSeeder::class]);
|
||||
|
||||
return $tenant->refresh();
|
||||
}
|
||||
|
||||
private function createTenant(string $code): Tenant
|
||||
{
|
||||
$headerLogo = $this->createAttachment("{$code}-header.png");
|
||||
$footerLogo = $this->createAttachment("{$code}-footer.png");
|
||||
|
||||
return Tenant::query()->create([
|
||||
'codigo' => $code,
|
||||
'nombre' => ucfirst($code),
|
||||
'dominio' => "{$code}.test",
|
||||
'primary_color' => '#111111',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'success_color' => '#444444',
|
||||
'header_bg_color' => '#ffffff',
|
||||
'footer_bg_color' => '#ffffff',
|
||||
'header_logo_id' => $headerLogo->id,
|
||||
'footer_logo_id' => $footerLogo->id,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createAttachment(string $filename): Attachment
|
||||
{
|
||||
return Attachment::query()->create([
|
||||
'path' => "tests/{$filename}",
|
||||
'filename' => $filename,
|
||||
'type' => AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
}
|
||||
|
||||
private function createAdminAppUser(Tenant $tenant): User
|
||||
{
|
||||
return User::factory()->create([
|
||||
'rol_codigo' => RoleCode::AdminApp->value,
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
]);
|
||||
}
|
||||
|
||||
private function grantTicketsMenu(Tenant $tenant): void
|
||||
{
|
||||
$menu = Menu::query()->create([
|
||||
'code' => 'adminapp.tickets',
|
||||
'label' => 'Tickets',
|
||||
'route' => '/admin/tickets',
|
||||
]);
|
||||
|
||||
$tenant->menues()->attach($menu->code);
|
||||
}
|
||||
}
|
||||
|
|
@ -17,7 +17,9 @@ use App\Domains\Shared\Enums\FieldType;
|
|||
use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Domains\Tenant\Models\WebsiteType;
|
||||
use App\Domains\Ticket\Models\Ticket;
|
||||
use Database\Seeders\AttributeSeeder;
|
||||
use Database\Seeders\AuthorizationSeeder;
|
||||
use Database\Seeders\FiestaFutbolInfantilProductSeeder;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
|
@ -188,6 +190,100 @@ class AdminAppTicketControllerTest extends TestCase
|
|||
->assertJsonPath('data.0.variant_properties.0.values.0.label', 'XL');
|
||||
}
|
||||
|
||||
public function test_it_applies_the_ticket_filter_form_values(): void
|
||||
{
|
||||
$tenant = $this->createTenant('fiesta_futbol_infantil');
|
||||
$admin = $this->createAdminAppUser($tenant);
|
||||
$this->grantTicketsMenu($tenant);
|
||||
$this->seed([AttributeSeeder::class, FiestaFutbolInfantilProductSeeder::class]);
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$date = $tenant->eventDates()->orderByDesc('date')->firstOrFail();
|
||||
$food = CatalogItem::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->where('slug', 'comida')
|
||||
->with([
|
||||
'variants.definitions.itemAttribute.attribute.options',
|
||||
'variants.eventDates',
|
||||
'variants.eventDate',
|
||||
])
|
||||
->firstOrFail();
|
||||
$dinner = $food->variants->first(fn (Variant $variant): bool => $variant
|
||||
->selectedEventDates()->contains('id', $date->id)
|
||||
&& $variant->selectionValues()->get('horario') === 'Cena');
|
||||
$lunch = $food->variants->first(fn (Variant $variant): bool => $variant
|
||||
->selectedEventDates()->contains('id', $date->id)
|
||||
&& $variant->selectionValues()->get('horario') === 'Almuerzo');
|
||||
$this->assertNotNull($dinner);
|
||||
$this->assertNotNull($lunch);
|
||||
|
||||
$matchingPurchase = $this->createPurchase($tenant, $admin, '2026-08-20 10:00:00');
|
||||
$otherPurchase = $this->createPurchase($tenant, $admin, '2026-08-21 10:00:00');
|
||||
$matching = $this->createTicket($tenant, $admin, [
|
||||
'source_purchase_id' => $matchingPurchase->id,
|
||||
'source_catalog_item_id' => $food->id,
|
||||
'source_variant_id' => $dinner->id,
|
||||
'used_at' => now(),
|
||||
]);
|
||||
$this->createTicket($tenant, $admin, [
|
||||
'source_purchase_id' => $matchingPurchase->id,
|
||||
'source_catalog_item_id' => $food->id,
|
||||
'source_variant_id' => $lunch->id,
|
||||
'used_at' => now(),
|
||||
]);
|
||||
$this->createTicket($tenant, $admin, [
|
||||
'source_purchase_id' => $otherPurchase->id,
|
||||
'source_catalog_item_id' => $food->id,
|
||||
'source_variant_id' => $dinner->id,
|
||||
'used_at' => now(),
|
||||
]);
|
||||
|
||||
$this->getJson('/api/v1/adminapp/tenant/tickets?'.http_build_query([
|
||||
'category' => 'comidas',
|
||||
'product' => (string) $date->id,
|
||||
'type' => 'Cena',
|
||||
'date' => '2026-08-20',
|
||||
'status' => Ticket::STATUS_USED,
|
||||
]))
|
||||
->assertOk()
|
||||
->assertJsonCount(1, 'data')
|
||||
->assertJsonPath('data.0.id', $matching->id)
|
||||
->assertJsonPath('scanned_tickets', 1)
|
||||
->assertJsonPath('total_tickets', 1);
|
||||
}
|
||||
|
||||
public function test_it_filters_computed_active_and_expired_statuses(): void
|
||||
{
|
||||
$tenant = $this->createTenant('fiesta_futbol_infantil');
|
||||
$admin = $this->createAdminAppUser($tenant);
|
||||
$this->grantTicketsMenu($tenant);
|
||||
$this->seed([AttributeSeeder::class, FiestaFutbolInfantilProductSeeder::class]);
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$food = CatalogItem::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->where('slug', 'comida')
|
||||
->with('variants')
|
||||
->firstOrFail();
|
||||
$expired = $this->createTicket($tenant, $admin, [
|
||||
'source_catalog_item_id' => $food->id,
|
||||
'source_variant_id' => $food->variants->firstOrFail()->id,
|
||||
]);
|
||||
$active = $this->createTicket($tenant, $admin);
|
||||
|
||||
$this->travelTo('2026-10-20 12:00:00');
|
||||
|
||||
$this->getJson('/api/v1/adminapp/tenant/tickets?status=expired')
|
||||
->assertOk()
|
||||
->assertJsonCount(1, 'data')
|
||||
->assertJsonPath('data.0.id', $expired->id);
|
||||
|
||||
$this->getJson('/api/v1/adminapp/tenant/tickets?status=active')
|
||||
->assertOk()
|
||||
->assertJsonCount(1, 'data')
|
||||
->assertJsonPath('data.0.id', $active->id);
|
||||
}
|
||||
|
||||
private function createTenant(string $code): Tenant
|
||||
{
|
||||
$headerLogo = $this->createAttachment("{$code}-header.png");
|
||||
|
|
@ -227,6 +323,20 @@ class AdminAppTicketControllerTest extends TestCase
|
|||
]);
|
||||
}
|
||||
|
||||
private function createPurchase(Tenant $tenant, User $user, string $createdAt): Purchase
|
||||
{
|
||||
$purchase = Purchase::query()->create([
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
'user_id' => $user->id,
|
||||
'status' => Purchase::STATUS_PAID,
|
||||
'nombre_apellido' => $user->nombre_apellido,
|
||||
'total' => '0.00',
|
||||
]);
|
||||
$purchase->forceFill(['created_at' => $createdAt, 'updated_at' => $createdAt])->saveQuietly();
|
||||
|
||||
return $purchase;
|
||||
}
|
||||
|
||||
private function grantTicketsMenu(Tenant $tenant): void
|
||||
{
|
||||
$menu = Menu::query()->create([
|
||||
|
|
|
|||
Loading…
Reference in New Issue