diff --git a/app/Filament/Pages/CurrentPrice.php b/app/Filament/Pages/CurrentPrice.php index 4eada96..d351ca3 100644 --- a/app/Filament/Pages/CurrentPrice.php +++ b/app/Filament/Pages/CurrentPrice.php @@ -12,6 +12,7 @@ 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 @@ -51,50 +52,53 @@ public function form(Schema $form): Schema ->schema([ Select::make('supplier') ->label('Beszállító:') - ->options(Supplier::orderBy('name')->pluck('name', 'id')) - ->multiple() - ->searchable() - ->live() - ->afterStateUpdated(function (callable $set) { - $set('productGroup', []); - $set('product', []); - $set('hooreycaName', []); - $set('producer', []); - }) - ->placeholder('Válasszon beszállítót...'), - Select::make('productGroup') - ->label('Termékcsoport:') ->options(function (Get $get) { - $supplierIds = $get('supplier') ?? []; - $query = ProductGroup::query(); - if (filled($supplierIds)) { - $query->whereIn('id', Product::whereIn('supplier_id', $supplierIds)->whereNotNull('product_group_id')->distinct()->pluck('product_group_id')); + $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() - ->afterStateUpdated(function (callable $set) { - $set('product', []); - $set('hooreycaName', []); - $set('producer', []); + ->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) { - $supplierIds = $get('supplier') ?? []; - $productGroupIds = $get('productGroup') ?? []; - $query = Product::where('name', 'like', "%{$search}%"); - if (filled($supplierIds)) { - $query->whereIn('supplier_id', $supplierIds); - } - if (filled($productGroupIds)) { - $query->whereIn('product_group_id', $productGroupIds); - } + $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) @@ -104,46 +108,48 @@ public function form(Schema $form): Schema Select::make('hooreycaName') ->label('Hooreyca megnevezés :') ->options(function (Get $get) { - $supplierIds = $get('supplier') ?? []; - $productGroupIds = $get('productGroup') ?? []; + $selected = $this->data['hooreycaName'] ?? []; + DB::statement("SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));"); - $query = Product::where('buyerProductName', '!=', ''); - if (filled($supplierIds)) { - $query->whereIn('supplier_id', $supplierIds); - } - if (filled($productGroupIds)) { - $query->whereIn('product_group_id', $productGroupIds); - } + $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) { - $supplierIds = $get('supplier') ?? []; - $productGroupIds = $get('productGroup') ?? []; + $selectedIds = $this->data['producer'] ?? []; $query = Producer::query(); - if (filled($supplierIds) || filled($productGroupIds)) { - $productQuery = Product::query(); - if (filled($supplierIds)) { - $productQuery->whereIn('supplier_id', $supplierIds); - } - if (filled($productGroupIds)) { - $productQuery->whereIn('product_group_id', $productGroupIds); - } - $query->whereIn('id', $productQuery->whereNotNull('producer_id')->distinct()->pluck('producer_id')); + + $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'); @@ -155,4 +161,46 @@ public function submit(): void // 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; + } }