d2d.emegrendeles.hu/app/Filament/Pages/CurrentPrice.php

207 lines
8.1 KiB
PHP

<?php
namespace App\Filament\Pages;
use App\Models\Producer;
use App\Models\Product;
use App\Models\ProductGroup;
use App\Models\Supplier;
use Filament\Forms\Components\Select;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Schema;
use Filament\Pages\Page;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
class CurrentPrice extends Page implements HasForms
{
use InteractsWithForms;
protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-currency-dollar';
protected static ?string $title = 'Aktuális ár';
protected static ?string $navigationLabel = 'Aktuális ár';
protected static string | \UnitEnum | null $navigationGroup = 'Statisztika';
public function getHeading(): string|\Illuminate\Contracts\Support\Htmlable
{
return '';
}
public function getBreadcrumbs(): array
{
return [];
}
protected string $view = 'filament.pages.current-price';
public ?array $data = [];
public function mount(): void
{
$this->form->fill();
}
public function form(Schema $form): Schema
{
return $form
->schema([
Select::make('supplier')
->label('Beszállító:')
->options(function (Get $get) {
$selectedIds = $this->data['supplier'] ?? [];
$query = Supplier::query();
$productQuery = $this->buildProductQuery($get, exclude: 'supplier');
if ($productQuery) {
$query->where(function ($q) use ($productQuery, $selectedIds) {
$q->whereIn('id', (clone $productQuery)->whereNotNull('supplier_id')->distinct()->pluck('supplier_id'))
->orWhereIn('id', $selectedIds);
});
}
return $query->orderBy('name')->pluck('name', 'id');
})
->multiple()
->searchable()
->live()
->placeholder('Válasszon beszállítót...'),
Select::make('productGroup')
->label('Termékcsoport:')
->options(function (Get $get) {
$selectedIds = $this->data['productGroup'] ?? [];
$query = ProductGroup::query();
$productQuery = $this->buildProductQuery($get, exclude: 'productGroup');
if ($productQuery) {
$query->where(function ($q) use ($productQuery, $selectedIds) {
$q->whereIn('id', (clone $productQuery)->whereNotNull('product_group_id')->distinct()->pluck('product_group_id'))
->orWhereIn('id', $selectedIds);
});
}
return $query->orderBy('name')->pluck('name', 'id');
})
->multiple()
->searchable()
->live()
->placeholder('Válasszon termékcsoportot...'),
Select::make('product')
->label('Termék:')
->multiple()
->searchable()
->live()
->getSearchResultsUsing(function (string $search, Get $get) {
$query = $this->buildProductQuery($get, exclude: 'product') ?? Product::query();
$query->where('name', 'like', "%{$search}%");
return $query->limit(50)->pluck('name', 'name')->toArray();
})
->getOptionLabelsUsing(fn (array $values): array => Product::whereIn('name', $values)
->pluck('name', 'name')
->toArray())
->placeholder('Válasszon terméket...'),
Select::make('hooreycaName')
->label('Hooreyca megnevezés :')
->options(function (Get $get) {
$selected = $this->data['hooreycaName'] ?? [];
DB::statement("SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));");
$query = ($this->buildProductQuery($get, exclude: 'hooreycaName') ?? Product::query())
->where('buyerProductName', '!=', '');
$options = $query->orderBy('buyerProductName')
->groupBy('buyerProductName')
->pluck('buyerProductName', 'buyerProductName')
->toArray();
DB::statement("SET sql_mode=(SELECT CONCAT(@@sql_mode, ',ONLY_FULL_GROUP_BY'));");
foreach ($selected as $name) {
if (filled($name) && ! array_key_exists($name, $options)) {
$options[$name] = $name;
}
}
return $options;
})
->multiple()
->searchable()
->live()
->placeholder('Válasszon megnevezést...'),
Select::make('producer')
->label('Gyártó :')
->options(function (Get $get) {
$selectedIds = $this->data['producer'] ?? [];
$query = Producer::query();
$productQuery = $this->buildProductQuery($get, exclude: 'producer');
if ($productQuery) {
$query->where(function ($q) use ($productQuery, $selectedIds) {
$q->whereIn('id', (clone $productQuery)->whereNotNull('producer_id')->distinct()->pluck('producer_id'))
->orWhereIn('id', $selectedIds);
});
}
return $query->orderBy('name')->pluck('name', 'id');
})
->multiple()
->searchable()
->live()
->placeholder('Válasszon gyártót...'),
])
->statePath('data');
}
public function submit(): void
{
$data = $this->form->getState();
// Itt lehet majd feldolgozni a szűrést
// dd($data);
}
/**
* Product query szűrve a szűrő űrlap összes mezőjének aktuális állapota alapján,
* a $exclude-ban megadott mező kihagyásával (kölcsönös/kereszt-szűréshez).
* Null-t ad vissza, ha egyik mező sincs kitöltve (nincs mit szűrni).
*/
private function buildProductQuery(Get $get, string $exclude): ?Builder
{
$supplierIds = $exclude === 'supplier' ? [] : ($get('supplier') ?? []);
$productGroupIds = $exclude === 'productGroup' ? [] : ($get('productGroup') ?? []);
$producerIds = $exclude === 'producer' ? [] : ($get('producer') ?? []);
$productNames = $exclude === 'product' ? [] : ($get('product') ?? []);
$hooreycaNames = $exclude === 'hooreycaName' ? [] : ($get('hooreycaName') ?? []);
if (blank($supplierIds) && blank($productGroupIds) && blank($producerIds) && blank($productNames) && blank($hooreycaNames)) {
return null;
}
$query = Product::query();
if (filled($supplierIds)) {
$query->whereIn('supplier_id', $supplierIds);
}
if (filled($productGroupIds)) {
$query->whereIn('product_group_id', $productGroupIds);
}
if (filled($producerIds)) {
$query->whereIn('producer_id', $producerIds);
}
if (filled($productNames)) {
$query->whereIn('name', $productNames);
}
if (filled($hooreycaNames)) {
$query->whereIn('buyerProductName', $hooreycaNames);
}
return $query;
}
}