ADD EV3-358 statisztika modul bővítése aktuális ár megjelenítéssel phase 5 kereszbe ható szűrők.

This commit is contained in:
E98Developer 2026-07-06 07:53:41 +02:00
parent e0876e4f25
commit 4f11e59fce

View File

@ -12,6 +12,7 @@
use Filament\Schemas\Components\Utilities\Get; use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Schema; use Filament\Schemas\Schema;
use Filament\Pages\Page; use Filament\Pages\Page;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
class CurrentPrice extends Page implements HasForms class CurrentPrice extends Page implements HasForms
@ -51,50 +52,53 @@ public function form(Schema $form): Schema
->schema([ ->schema([
Select::make('supplier') Select::make('supplier')
->label('Beszállító:') ->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) { ->options(function (Get $get) {
$supplierIds = $get('supplier') ?? []; $selectedIds = $this->data['supplier'] ?? [];
$query = ProductGroup::query(); $query = Supplier::query();
if (filled($supplierIds)) {
$query->whereIn('id', Product::whereIn('supplier_id', $supplierIds)->whereNotNull('product_group_id')->distinct()->pluck('product_group_id')); $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'); return $query->orderBy('name')->pluck('name', 'id');
}) })
->multiple() ->multiple()
->searchable() ->searchable()
->live() ->live()
->afterStateUpdated(function (callable $set) { ->placeholder('Válasszon beszállítót...'),
$set('product', []); Select::make('productGroup')
$set('hooreycaName', []); ->label('Termékcsoport:')
$set('producer', []); ->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...'), ->placeholder('Válasszon termékcsoportot...'),
Select::make('product') Select::make('product')
->label('Termék:') ->label('Termék:')
->multiple() ->multiple()
->searchable() ->searchable()
->live()
->getSearchResultsUsing(function (string $search, Get $get) { ->getSearchResultsUsing(function (string $search, Get $get) {
$supplierIds = $get('supplier') ?? []; $query = $this->buildProductQuery($get, exclude: 'product') ?? Product::query();
$productGroupIds = $get('productGroup') ?? []; $query->where('name', 'like', "%{$search}%");
$query = Product::where('name', 'like', "%{$search}%");
if (filled($supplierIds)) {
$query->whereIn('supplier_id', $supplierIds);
}
if (filled($productGroupIds)) {
$query->whereIn('product_group_id', $productGroupIds);
}
return $query->limit(50)->pluck('name', 'name')->toArray(); return $query->limit(50)->pluck('name', 'name')->toArray();
}) })
->getOptionLabelsUsing(fn (array $values): array => Product::whereIn('name', $values) ->getOptionLabelsUsing(fn (array $values): array => Product::whereIn('name', $values)
@ -104,46 +108,48 @@ public function form(Schema $form): Schema
Select::make('hooreycaName') Select::make('hooreycaName')
->label('Hooreyca megnevezés :') ->label('Hooreyca megnevezés :')
->options(function (Get $get) { ->options(function (Get $get) {
$supplierIds = $get('supplier') ?? []; $selected = $this->data['hooreycaName'] ?? [];
$productGroupIds = $get('productGroup') ?? [];
DB::statement("SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));"); DB::statement("SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));");
$query = Product::where('buyerProductName', '!=', ''); $query = ($this->buildProductQuery($get, exclude: 'hooreycaName') ?? Product::query())
if (filled($supplierIds)) { ->where('buyerProductName', '!=', '');
$query->whereIn('supplier_id', $supplierIds);
}
if (filled($productGroupIds)) {
$query->whereIn('product_group_id', $productGroupIds);
}
$options = $query->orderBy('buyerProductName') $options = $query->orderBy('buyerProductName')
->groupBy('buyerProductName') ->groupBy('buyerProductName')
->pluck('buyerProductName', 'buyerProductName') ->pluck('buyerProductName', 'buyerProductName')
->toArray(); ->toArray();
DB::statement("SET sql_mode=(SELECT CONCAT(@@sql_mode, ',ONLY_FULL_GROUP_BY'));"); 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; return $options;
}) })
->multiple() ->multiple()
->searchable() ->searchable()
->live()
->placeholder('Válasszon megnevezést...'), ->placeholder('Válasszon megnevezést...'),
Select::make('producer') Select::make('producer')
->label('Gyártó :') ->label('Gyártó :')
->options(function (Get $get) { ->options(function (Get $get) {
$supplierIds = $get('supplier') ?? []; $selectedIds = $this->data['producer'] ?? [];
$productGroupIds = $get('productGroup') ?? [];
$query = Producer::query(); $query = Producer::query();
if (filled($supplierIds) || filled($productGroupIds)) {
$productQuery = Product::query(); $productQuery = $this->buildProductQuery($get, exclude: 'producer');
if (filled($supplierIds)) { if ($productQuery) {
$productQuery->whereIn('supplier_id', $supplierIds); $query->where(function ($q) use ($productQuery, $selectedIds) {
} $q->whereIn('id', (clone $productQuery)->whereNotNull('producer_id')->distinct()->pluck('producer_id'))
if (filled($productGroupIds)) { ->orWhereIn('id', $selectedIds);
$productQuery->whereIn('product_group_id', $productGroupIds); });
}
$query->whereIn('id', $productQuery->whereNotNull('producer_id')->distinct()->pluck('producer_id'));
} }
return $query->orderBy('name')->pluck('name', 'id'); return $query->orderBy('name')->pluck('name', 'id');
}) })
->multiple() ->multiple()
->searchable() ->searchable()
->live()
->placeholder('Válasszon gyártót...'), ->placeholder('Válasszon gyártót...'),
]) ])
->statePath('data'); ->statePath('data');
@ -155,4 +161,46 @@ public function submit(): void
// Itt lehet majd feldolgozni a szűrést // Itt lehet majd feldolgozni a szűrést
// dd($data); // 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;
}
} }