Files
autocrm/app/Filament/Tenant/Resources/SubcontractJobResource.php
T
Vasyka 29e5481fc3 refactor(nav): collapse single-item groups + fix ungrouped CRM resources
Previously nav had 12 groups, several with just 1 item. Now 8 clean
groups, no orphans:

- Anvelope (1 item) → merged into Service (Seturi anvelope)
- Stoc & Finanțe (1 item) → merged into Depozit (Import factură Excel)
- Tinichigerie (1 item) → merged into Service (Tinichigerie / Detailing)
- Subcontractare (2 items) → merged into Service (Lucrări terți,
  Subcontractori)
- Integrări moved from Marketing → Admin (it's a config concern, not
  a marketing feature)
- ClientResource + VehicleResource were ungrouped → added to CRM

Final groups:
  Admin(8) · Analiză(4) · CRM(5) · Depozit(10) · Finanțe(6) ·
  Magazin(2) · Marketing(3) · Service(11)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-08-10 19:29:03 +00:00

146 lines
6.9 KiB
PHP

<?php
namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\SubcontractJobResource\Pages;
use App\Models\Tenant\Subcontractor;
use App\Models\Tenant\SubcontractJob;
use App\Models\Tenant\WorkOrder;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class SubcontractJobResource extends Resource
{
protected static ?string $model = SubcontractJob::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-arrow-top-right-on-square';
public static function getNavigationLabel(): string
{
return __('nav.label.Lucrări terți');
}
public static function getNavigationGroup(): ?string
{
return __('nav.group.Service');
}
protected static ?int $navigationSort = 71;
public static function getNavigationBadge(): ?string
{
$open = static::getModel()::query()->whereNotIn('status', ['done', 'returned', 'cancelled'])->count();
return $open > 0 ? (string) $open : null;
}
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make(__('Lucrare'))
->columns(2)
->schema([
Forms\Components\TextInput::make('number')->label(__('Nr.'))->disabled()->dehydrated(false)->placeholder(__('Generat automat')),
Forms\Components\Select::make('status')->options(\App\Support\I18n::opts(SubcontractJob::STATUSES))->default('sent')->required(),
Forms\Components\Select::make('subcontractor_id')
->label(__('Subcontractor'))
->options(fn () => Subcontractor::where('is_active', true)->pluck('name', 'id'))
->searchable(),
Forms\Components\Select::make('work_order_id')
->label(__('Fișă asociată'))
->options(fn () => WorkOrder::whereNotIn('status', ['done', 'cancelled'])
->get()->mapWithKeys(fn ($w) => [$w->id => "#{$w->number} · " . ($w->vehicle?->plate ?? '')])->toArray())
->searchable(),
Forms\Components\Select::make('category')
->label(__('Categorie'))
->options(\App\Support\I18n::opts(array_combine(Subcontractor::SPECIALTIES, Subcontractor::SPECIALTIES)))
->searchable(),
Forms\Components\Textarea::make('description')->label(__('Descriere'))->rows(2)->columnSpanFull(),
]),
Schemas\Components\Section::make(__('Cost & marjă'))
->columns(3)
->schema([
Forms\Components\TextInput::make('cost')->label(__('Cost (de la terț)'))->numeric()->default(0)->required(),
Forms\Components\TextInput::make('markup_pct')->label(__('Markup %'))->numeric()->default(0)
->helperText(__('> 0 calculează automat prețul client.')),
Forms\Components\TextInput::make('client_price')->label(__('Preț client'))->numeric()->default(0)
->helperText(__('Setat manual dacă markup = 0.')),
Forms\Components\Toggle::make('paid_to_sub')->label(__('Plătit către terț')),
]),
Schemas\Components\Section::make(__('Termene'))
->columns(3)
->schema([
Forms\Components\DatePicker::make('sent_at')->label(__('Trimis'))->default(today()),
Forms\Components\DatePicker::make('eta')->label(__('ETA')),
Forms\Components\DatePicker::make('returned_at')->label(__('Returnat')),
Forms\Components\Textarea::make('notes')->label(__('Observații'))->columnSpanFull()->rows(2),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('number')->label(__('Nr.'))->searchable()->sortable(),
Tables\Columns\TextColumn::make('subcontractor.name')->label(__('Terț'))->placeholder('—'),
Tables\Columns\TextColumn::make('category')->badge()->placeholder('—'),
Tables\Columns\TextColumn::make('workOrder.number')->label(__('Fișă'))->placeholder('—'),
Tables\Columns\TextColumn::make('cost')->label(__('Cost'))->money('MDL')->alignRight(),
Tables\Columns\TextColumn::make('client_price')->label(__('Preț client'))->money('MDL')->alignRight(),
Tables\Columns\TextColumn::make('margin')
->label(__('Marjă'))
->state(fn (SubcontractJob $r) => $r->margin())
->money('MDL')
->alignRight()
->color(fn ($state) => (float) $state > 0 ? 'success' : ((float) $state < 0 ? 'danger' : 'gray')),
Tables\Columns\TextColumn::make('status')
->formatStateUsing(fn ($s) => __(SubcontractJob::STATUSES[$s] ?? $s))
->badge()
->colors([
'warning' => ['sent', 'in_progress'],
'success' => ['done', 'returned'],
'danger' => ['cancelled'],
]),
Tables\Columns\IconColumn::make('paid_to_sub')->label(__('Plătit terț'))->boolean()->toggleable(),
])
->filters([
Tables\Filters\SelectFilter::make('status')->options(\App\Support\I18n::opts(SubcontractJob::STATUSES)),
Tables\Filters\SelectFilter::make('subcontractor_id')
->label(__('Subcontractor'))
->options(fn () => Subcontractor::pluck('name', 'id')),
])
->actions([
Actions\EditAction::make(),
Actions\DeleteAction::make(),
])
->emptyStateHeading(__('Nicio lucrare la terți'))
->emptyStateDescription(__('Înregistrează lucrările trimise la ateliere externe (turbo, cutii, vopsitorie). Costul terțului + markup intră automat în totalul fișei asociate.'))
->emptyStateIcon('heroicon-o-arrow-top-right-on-square')
->defaultSort('created_at', 'desc');
}
public static function getModelLabel(): string
{
return __('lucrare terți');
}
public static function getPluralModelLabel(): string
{
return __('lucrări terți');
}
public static function getPages(): array
{
return [
'index' => Pages\ListSubcontractJobs::route('/'),
'create' => Pages\CreateSubcontractJob::route('/create'),
'edit' => Pages\EditSubcontractJob::route('/{record}/edit'),
];
}
}