Add LaborCategory resource — manage labor categories from UI
- Creates labor_categories table (company-scoped) seeded with 11 defaults - LaborCategory model with BelongsToTenant - Full Filament CRUD resource with drag-to-reorder (sort_order) - LaborResource category Select now reads from DB instead of hardcoded const - Navigation: 'Categorii manopere' in Service group, sort 33 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,97 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Tenant\Resources;
|
||||||
|
|
||||||
|
use App\Filament\Tenant\Resources\LaborCategoryResource\Pages;
|
||||||
|
use App\Models\Tenant\LaborCategory;
|
||||||
|
use Filament\Actions;
|
||||||
|
use Filament\Forms;
|
||||||
|
use Filament\Resources\Resource;
|
||||||
|
use Filament\Schemas\Schema;
|
||||||
|
use Filament\Tables;
|
||||||
|
use Filament\Tables\Table;
|
||||||
|
|
||||||
|
class LaborCategoryResource extends Resource
|
||||||
|
{
|
||||||
|
protected static ?string $model = LaborCategory::class;
|
||||||
|
|
||||||
|
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-tag';
|
||||||
|
|
||||||
|
public static function getNavigationLabel(): string
|
||||||
|
{
|
||||||
|
return __('Categorii manopere');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getNavigationGroup(): ?string
|
||||||
|
{
|
||||||
|
return __('nav.group.Service');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getModelLabel(): string
|
||||||
|
{
|
||||||
|
return __('categorie');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getPluralModelLabel(): string
|
||||||
|
{
|
||||||
|
return __('Categorii manopere');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static ?int $navigationSort = 33;
|
||||||
|
|
||||||
|
public static function form(Schema $schema): Schema
|
||||||
|
{
|
||||||
|
return $schema->components([
|
||||||
|
Forms\Components\TextInput::make('name')
|
||||||
|
->label(__('Denumire'))
|
||||||
|
->required()
|
||||||
|
->maxLength(80)
|
||||||
|
->columnSpanFull(),
|
||||||
|
Forms\Components\TextInput::make('sort_order')
|
||||||
|
->label(__('Ordine'))
|
||||||
|
->numeric()
|
||||||
|
->default(0),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function table(Table $table): Table
|
||||||
|
{
|
||||||
|
return $table
|
||||||
|
->columns([
|
||||||
|
Tables\Columns\TextColumn::make('sort_order')
|
||||||
|
->label('#')
|
||||||
|
->sortable()
|
||||||
|
->alignRight()
|
||||||
|
->width(50),
|
||||||
|
Tables\Columns\TextColumn::make('name')
|
||||||
|
->label(__('Categorie'))
|
||||||
|
->searchable()
|
||||||
|
->sortable(),
|
||||||
|
Tables\Columns\TextColumn::make('updated_at')
|
||||||
|
->label(__('Modificat'))
|
||||||
|
->dateTime('d.m.Y')
|
||||||
|
->sortable()
|
||||||
|
->toggleable(isToggledHiddenByDefault: true),
|
||||||
|
])
|
||||||
|
->defaultSort('sort_order')
|
||||||
|
->reorderable('sort_order')
|
||||||
|
->actions([
|
||||||
|
Actions\EditAction::make(),
|
||||||
|
Actions\DeleteAction::make(),
|
||||||
|
])
|
||||||
|
->bulkActions([
|
||||||
|
Tables\Actions\BulkActionGroup::make([
|
||||||
|
Tables\Actions\DeleteBulkAction::make(),
|
||||||
|
]),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getPages(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'index' => Pages\ListLaborCategories::route('/'),
|
||||||
|
'create' => Pages\CreateLaborCategory::route('/create'),
|
||||||
|
'edit' => Pages\EditLaborCategory::route('/{record}/edit'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Tenant\Resources\LaborCategoryResource\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Tenant\Resources\LaborCategoryResource;
|
||||||
|
use Filament\Resources\Pages\CreateRecord;
|
||||||
|
|
||||||
|
class CreateLaborCategory extends CreateRecord
|
||||||
|
{
|
||||||
|
protected static string $resource = LaborCategoryResource::class;
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Tenant\Resources\LaborCategoryResource\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Tenant\Resources\LaborCategoryResource;
|
||||||
|
use Filament\Actions;
|
||||||
|
use Filament\Resources\Pages\EditRecord;
|
||||||
|
|
||||||
|
class EditLaborCategory extends EditRecord
|
||||||
|
{
|
||||||
|
protected static string $resource = LaborCategoryResource::class;
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [Actions\DeleteAction::make()];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Tenant\Resources\LaborCategoryResource\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Tenant\Resources\LaborCategoryResource;
|
||||||
|
use Filament\Actions;
|
||||||
|
use Filament\Resources\Pages\ListRecords;
|
||||||
|
|
||||||
|
class ListLaborCategories extends ListRecords
|
||||||
|
{
|
||||||
|
protected static string $resource = LaborCategoryResource::class;
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [Actions\CreateAction::make()];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -53,7 +53,7 @@ class LaborResource extends Resource
|
|||||||
->schema([
|
->schema([
|
||||||
Forms\Components\Select::make('category')
|
Forms\Components\Select::make('category')
|
||||||
->label(__('Categorie'))
|
->label(__('Categorie'))
|
||||||
->options(\App\Support\I18n::opts(array_combine(Labor::CATEGORIES, Labor::CATEGORIES)))
|
->options(fn () => \App\Models\Tenant\LaborCategory::orderBy('sort_order')->orderBy('name')->pluck('name', 'name'))
|
||||||
->required()
|
->required()
|
||||||
->searchable(),
|
->searchable(),
|
||||||
Forms\Components\TextInput::make('code')->label(__('Cod'))->maxLength(32),
|
Forms\Components\TextInput::make('code')->label(__('Cod'))->maxLength(32),
|
||||||
@@ -100,7 +100,7 @@ class LaborResource extends Resource
|
|||||||
])
|
])
|
||||||
->filters([
|
->filters([
|
||||||
Tables\Filters\SelectFilter::make('category')
|
Tables\Filters\SelectFilter::make('category')
|
||||||
->options(\App\Support\I18n::opts(array_combine(Labor::CATEGORIES, Labor::CATEGORIES))),
|
->options(fn () => \App\Models\Tenant\LaborCategory::orderBy('sort_order')->orderBy('name')->pluck('name', 'name')),
|
||||||
Tables\Filters\TernaryFilter::make('is_active')->label(__('Doar active')),
|
Tables\Filters\TernaryFilter::make('is_active')->label(__('Doar active')),
|
||||||
])
|
])
|
||||||
->actions([
|
->actions([
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models\Tenant;
|
||||||
|
|
||||||
|
use App\Models\Concerns\BelongsToTenant;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class LaborCategory extends Model
|
||||||
|
{
|
||||||
|
use BelongsToTenant;
|
||||||
|
|
||||||
|
protected $fillable = ['company_id', 'name', 'sort_order'];
|
||||||
|
|
||||||
|
protected $casts = ['sort_order' => 'integer'];
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('labor_categories', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('company_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->string('name');
|
||||||
|
$table->unsignedSmallInteger('sort_order')->default(0);
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['company_id', 'name']);
|
||||||
|
$table->index('company_id');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Seed default categories for all existing companies
|
||||||
|
$defaults = [
|
||||||
|
'Motor', 'Frâne', 'Suspensie', 'Anvelope', 'ITP',
|
||||||
|
'Cutie viteze', 'Caroserie', 'Electrică', 'Climatizare',
|
||||||
|
'Eșapament', 'Altele',
|
||||||
|
];
|
||||||
|
|
||||||
|
$companies = DB::table('companies')->pluck('id');
|
||||||
|
$now = now();
|
||||||
|
|
||||||
|
foreach ($companies as $companyId) {
|
||||||
|
foreach ($defaults as $i => $name) {
|
||||||
|
DB::table('labor_categories')->insertOrIgnore([
|
||||||
|
'company_id' => $companyId,
|
||||||
|
'name' => $name,
|
||||||
|
'sort_order' => $i,
|
||||||
|
'created_at' => $now,
|
||||||
|
'updated_at' => $now,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('labor_categories');
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user