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

159 lines
6.6 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\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(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'));
}
return $query->orderBy('name')->pluck('name', 'id');
})
->multiple()
->searchable()
->live()
->afterStateUpdated(function (callable $set) {
$set('product', []);
$set('hooreycaName', []);
$set('producer', []);
})
->placeholder('Válasszon termékcsoportot...'),
Select::make('product')
->label('Termék:')
->multiple()
->searchable()
->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);
}
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) {
$supplierIds = $get('supplier') ?? [];
$productGroupIds = $get('productGroup') ?? [];
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);
}
$options = $query->orderBy('buyerProductName')
->groupBy('buyerProductName')
->pluck('buyerProductName', 'buyerProductName')
->toArray();
DB::statement("SET sql_mode=(SELECT CONCAT(@@sql_mode, ',ONLY_FULL_GROUP_BY'));");
return $options;
})
->multiple()
->searchable()
->placeholder('Válasszon megnevezést...'),
Select::make('producer')
->label('Gyártó :')
->options(function (Get $get) {
$supplierIds = $get('supplier') ?? [];
$productGroupIds = $get('productGroup') ?? [];
$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'));
}
return $query->orderBy('name')->pluck('name', 'id');
})
->multiple()
->searchable()
->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);
}
}