From a2ddea2529496e3394fc4d386f31dbe2fad7f540 Mon Sep 17 00:00:00 2001 From: Vasyka Date: Mon, 7 Sep 2026 12:25:19 +0000 Subject: [PATCH] =?UTF-8?q?Add=20LaborCategory=20resource=20=E2=80=94=20ma?= =?UTF-8?q?nage=20labor=20categories=20from=20UI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- .../Resources/LaborCategoryResource.php | 97 +++++++++++++++++++ .../Pages/CreateLaborCategory.php | 11 +++ .../Pages/EditLaborCategory.php | 17 ++++ .../Pages/ListLaborCategories.php | 17 ++++ .../Tenant/Resources/LaborResource.php | 4 +- app/Models/Tenant/LaborCategory.php | 15 +++ ...7_110000_create_labor_categories_table.php | 50 ++++++++++ 7 files changed, 209 insertions(+), 2 deletions(-) create mode 100644 app/Filament/Tenant/Resources/LaborCategoryResource.php create mode 100644 app/Filament/Tenant/Resources/LaborCategoryResource/Pages/CreateLaborCategory.php create mode 100644 app/Filament/Tenant/Resources/LaborCategoryResource/Pages/EditLaborCategory.php create mode 100644 app/Filament/Tenant/Resources/LaborCategoryResource/Pages/ListLaborCategories.php create mode 100644 app/Models/Tenant/LaborCategory.php create mode 100644 database/migrations/2026_09_07_110000_create_labor_categories_table.php diff --git a/app/Filament/Tenant/Resources/LaborCategoryResource.php b/app/Filament/Tenant/Resources/LaborCategoryResource.php new file mode 100644 index 0000000..a3464b0 --- /dev/null +++ b/app/Filament/Tenant/Resources/LaborCategoryResource.php @@ -0,0 +1,97 @@ +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'), + ]; + } +} diff --git a/app/Filament/Tenant/Resources/LaborCategoryResource/Pages/CreateLaborCategory.php b/app/Filament/Tenant/Resources/LaborCategoryResource/Pages/CreateLaborCategory.php new file mode 100644 index 0000000..053e1d3 --- /dev/null +++ b/app/Filament/Tenant/Resources/LaborCategoryResource/Pages/CreateLaborCategory.php @@ -0,0 +1,11 @@ +schema([ Forms\Components\Select::make('category') ->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() ->searchable(), Forms\Components\TextInput::make('code')->label(__('Cod'))->maxLength(32), @@ -100,7 +100,7 @@ class LaborResource extends Resource ]) ->filters([ 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')), ]) ->actions([ diff --git a/app/Models/Tenant/LaborCategory.php b/app/Models/Tenant/LaborCategory.php new file mode 100644 index 0000000..314b2e8 --- /dev/null +++ b/app/Models/Tenant/LaborCategory.php @@ -0,0 +1,15 @@ + 'integer']; +} diff --git a/database/migrations/2026_09_07_110000_create_labor_categories_table.php b/database/migrations/2026_09_07_110000_create_labor_categories_table.php new file mode 100644 index 0000000..754c45d --- /dev/null +++ b/database/migrations/2026_09_07_110000_create_labor_categories_table.php @@ -0,0 +1,50 @@ +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'); + } +};