d2d.emegrendeles.hu/app/Filament/Resources/Products/ProductResource.php
E98Developer 07481f75ce ADD EV3-464 profitcenter egyszerűsített, csak olvasható termékkereső
Filament ProductResource (/admin/products): lista keresővel és szűrőkkel,
csak olvasható adatlap. Profitcenternél szerver oldalon csak a saját,
rendelhető beszállítók termékei látszanak; mindenkinél csak a beszállító
jelenleg érvényes árlistáján szereplők (Product::scopeInActivePriceList).
ProfitCenterProductSearch feature flag mögött, menüpont mindkét nav barban.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-19 21:06:51 +02:00

141 lines
4.2 KiB
PHP

<?php
namespace App\Filament\Resources\Products;
use App\Filament\Resources\Products\Pages\ListProducts;
use App\Filament\Resources\Products\Pages\ViewProduct;
use App\Filament\Resources\Products\Schemas\ProductInfolist;
use App\Filament\Resources\Products\Tables\ProductsTable;
use App\Models\Product;
use App\Models\User;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Laravel\Pennant\Feature;
/**
* EV3-464: egyszerűsített, csak olvasható termékkereső a profitcenternek ("melyik
* beszállítónál kapható a csemege szalámi?"). Szándékosan nincs create/edit oldal.
*
* A láthatóság szerver oldalon szűkül (getEloquentQuery), nem csak a menüből:
* - profitcenter szerepkörnél csak a saját profitcenter beszállítóinak termékei, akiktől
* rendelni is lehet (suppliers.canSeeInOrder, mint az új rendelés beszállító-listáján);
* - mindenkinél csak a beszállító jelenleg érvényes árlistáján szereplő termékek (fix).
*
* Az admin használat később jön; addig a ProfitCenterProductSearch flag + a szerepkör-lista
* (root/developer a rollout-teszteléshez) szabja meg, ki éri el.
*/
class ProductResource extends Resource
{
public const FEATURE = 'ProfitCenterProductSearch';
protected static ?string $model = Product::class;
protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-magnifying-glass';
protected static ?string $navigationLabel = 'Termékkereső';
protected static ?string $modelLabel = 'Termék';
protected static ?string $pluralLabel = 'Termékkereső';
protected static ?string $breadcrumb = 'Termékkereső';
public static function canAccess(): bool
{
return static::canViewAny();
}
public static function canViewAny(): bool
{
/** @var User|null $user */
$user = auth()->user();
return $user
&& $user->hasRole(['profit-center', 'root', 'developer'])
&& Feature::for($user)->active(self::FEATURE);
}
/**
* A Product modellre a Laravel automatikusan a ProductPolicy-t köti, az viszont a
* (beszállítói/admin) szerkesztésre készült és a profit-center szerepkört nem ismeri.
* A rekord-szintű szűrést a getEloquentQuery() végzi (idegen termék 404), ezért itt
* a resource-szintű jogosultság dönt.
*/
public static function canView($record): bool
{
return static::canViewAny();
}
public static function canCreate(): bool
{
return false;
}
public static function canEdit($record): bool
{
return false;
}
public static function canDelete($record): bool
{
return false;
}
public static function canDeleteAny(): bool
{
return false;
}
public static function getEloquentQuery(): Builder
{
$query = parent::getEloquentQuery()
->with(['Supplier', 'Producer', 'ProductGroup'])
->inActivePriceList()
->withActivePrice();
/** @var User|null $user */
$user = auth()->user();
if ($user?->hasRole('profit-center')) {
$query->whereIn('products.supplier_id', static::profitCenterSupplierIds($user));
}
return $query;
}
/**
* A profitcenter beszállítói: a felhasználó első profitcentere (1:1 használat), csak
* a rendelésnél is látható beszállítók. Profitcenter nélküli felhasználó nem lát semmit.
*
* @return array<int, int>
*/
public static function profitCenterSupplierIds(User $user): array
{
return $user->profitCenters()->first()
?->suppliers()
->where('suppliers.canSeeInOrder', 1)
->pluck('suppliers.id')
->all() ?? [];
}
public static function infolist(Schema $schema): Schema
{
return ProductInfolist::configure($schema);
}
public static function table(Table $table): Table
{
return ProductsTable::configure($table);
}
public static function getPages(): array
{
return [
'index' => ListProducts::route('/'),
'view' => ViewProduct::route('/{record}'),
];
}
}