Intégration fonctionnalites V1
This commit is contained in:
@@ -15,21 +15,59 @@ class PromotionPageController extends Controller
|
||||
{
|
||||
use ScopesSupervisorResources;
|
||||
|
||||
public function index(): Response
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
$query = Promotion::query()->with('establishment:id,name');
|
||||
$this->scopeEstablishmentQuery($query);
|
||||
$this->applyEstablishmentFilter($request, $query);
|
||||
|
||||
if ($request->filled('machine_type')) {
|
||||
$query->where('machine_type', $request->string('machine_type'));
|
||||
}
|
||||
|
||||
if ($request->has('is_active') && $request->string('is_active')->toString() !== '') {
|
||||
$query->where('is_active', $request->boolean('is_active'));
|
||||
}
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$search = $request->string('search');
|
||||
$query->where('description', 'like', "%{$search}%");
|
||||
}
|
||||
|
||||
$promotions = $query
|
||||
->orderByDesc('starts_at')
|
||||
->get()
|
||||
->map(fn (Promotion $promotion) => $this->formatPromotion($promotion));
|
||||
->tap(fn ($builder) => $this->applySort($request, $builder, [
|
||||
'establishment' => fn ($builder, string $direction) => $this->sortByRelatedEstablishment(
|
||||
$builder,
|
||||
'promotions',
|
||||
'establishment_id',
|
||||
$direction,
|
||||
),
|
||||
'discount_value' => 'discount_value',
|
||||
'starts_at' => 'starts_at',
|
||||
'ends_at' => 'ends_at',
|
||||
'machine_type' => 'machine_type',
|
||||
'is_active' => 'is_active',
|
||||
], 'starts_at', 'desc'))
|
||||
->paginate($this->supervisorListPerPage($request))
|
||||
->withQueryString()
|
||||
->through(fn (Promotion $promotion) => $this->formatPromotion($promotion));
|
||||
|
||||
return Inertia::render('Supervisor/Promotions/Index', [
|
||||
'promotions' => $promotions,
|
||||
'establishments' => $this->establishmentOptions(),
|
||||
'machineTypes' => $this->machineTypeOptions(),
|
||||
'discountTypes' => $this->discountTypeOptions(),
|
||||
'filters' => [
|
||||
'establishment_id' => $request->filled('establishment_id') ? (int) $request->input('establishment_id') : null,
|
||||
'machine_type' => $request->string('machine_type')->toString() ?: null,
|
||||
'is_active' => $request->has('is_active') && $request->string('is_active')->toString() !== ''
|
||||
? ($request->boolean('is_active') ? '1' : '0')
|
||||
: null,
|
||||
'search' => $request->string('search')->toString() ?: null,
|
||||
'sort' => $request->string('sort')->toString() ?: 'starts_at',
|
||||
'direction' => $request->string('direction')->toString() === 'desc' ? 'desc' : 'asc',
|
||||
'per_page' => $this->supervisorListPerPage($request),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -124,22 +162,6 @@ class PromotionPageController extends Controller
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{id: int, name: string}>
|
||||
*/
|
||||
private function establishmentOptions(): array
|
||||
{
|
||||
$supervisor = $this->supervisor();
|
||||
|
||||
return Establishment::query()
|
||||
->where('organization_id', $supervisor->organization_id)
|
||||
->when($supervisor->establishment_id, fn ($q) => $q->where('id', $supervisor->establishment_id))
|
||||
->orderBy('name')
|
||||
->get(['id', 'name'])
|
||||
->map(fn (Establishment $e) => ['id' => $e->id, 'name' => $e->name])
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user