Files
autocrm/app/Filament/Tenant/Resources/WorkOrderResource.php
T
Vasyka 51a0bab39e Faza 3.2: Service modules — Norme-ore, Tehnicieni, Fișe lucru
Schema:
- users + specialization, color, hourly_rate (pentru maistri)
- labors: catalog manopere standard cu category/ore/preț (RO+RU)
- work_orders: nr unique per tenant, status workflow (9 stări),
  pay_status (3 stări), client/vehicle/master/deal/appointment refs,
  complaint/diagnosis/recommendations, total auto-calculat
- wo_works: manopere per fișă, recalc auto la save/delete
- wo_parts: piese per fișă (free-text deocamdată), discount/total auto

Filament resources (group Service):
- LaborResource: CRUD + grupare pe categorie + filter active
- WorkOrderResource: form complex în 4 secțiuni (antet, diagnostic, plată)
  + 2 RelationManagers (Works, Parts)
- MasterResource: vedere User filtrată role=mechanic, edit specializare/
  culoare calendar/tarif oră

Conversie auto: la adaugare manoperă din catalog Labor,
form populează numele + ore + preț/oră derivat (price/hours).

Number generator pentru WO: format WO-{YY}-{NNNN} per tenant per an,
calculat în CreateWorkOrder via WorkOrder::generateNumber().

Seed extins:
- 3 mecanici (Vasile/Andrei/Nicolae) cu culori + specializări
- 10 manopere standard din prototipul AutoCRM.html
- 1 fișă demo (BMW X5 plăcuțe Brembo) cu 1 manoperă + 1 piesă, total auto
2026-05-06 21:24:07 +00:00

160 lines
7.0 KiB
PHP

<?php
namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\WorkOrderResource\Pages;
use App\Filament\Tenant\Resources\WorkOrderResource\RelationManagers;
use App\Models\Tenant\Client;
use App\Models\Tenant\User;
use App\Models\Tenant\Vehicle;
use App\Models\Tenant\WorkOrder;
use App\Tenancy\TenantManager;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class WorkOrderResource extends Resource
{
protected static ?string $model = WorkOrder::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-wrench-screwdriver';
protected static ?string $navigationLabel = 'Fișe lucru';
protected static string|\UnitEnum|null $navigationGroup = 'Service';
protected static ?string $modelLabel = 'fișă';
protected static ?string $pluralModelLabel = 'fișe lucru';
protected static ?int $navigationSort = 30;
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make('Antet')
->columns(3)
->schema([
Forms\Components\TextInput::make('number')
->label('Nr.')
->disabled()
->dehydrated(false)
->placeholder('Generat automat'),
Forms\Components\DatePicker::make('opened_at')
->label('Deschis')
->default(today())
->required(),
Forms\Components\Select::make('status')
->options(WorkOrder::STATUSES)
->default('new')
->required(),
Forms\Components\Select::make('client_id')
->label('Client')
->options(fn () => Client::pluck('name', 'id'))
->searchable()
->live()
->required(),
Forms\Components\Select::make('vehicle_id')
->label('Auto')
->options(fn (Get $get) => $get('client_id')
? Vehicle::where('client_id', $get('client_id'))
->get()
->mapWithKeys(fn ($v) => [$v->id => "{$v->make} {$v->model} {$v->plate}"])
->toArray()
: [])
->searchable(),
Forms\Components\Select::make('master_id')
->label('Maistru')
->options(fn () => User::where('status', 'active')->pluck('name', 'id'))
->searchable(),
Forms\Components\TextInput::make('mileage_in')->label('Km la intrare')->numeric(),
Forms\Components\TextInput::make('mileage_out')->label('Km la ieșire')->numeric(),
]),
Schemas\Components\Section::make('Diagnostic')
->collapsible()
->schema([
Forms\Components\Textarea::make('complaint')->label('Plângere client')->rows(2)->columnSpanFull(),
Forms\Components\Textarea::make('diagnosis')->label('Diagnostic')->rows(3)->columnSpanFull(),
Forms\Components\Textarea::make('recommendations')->label('Recomandări')->rows(2)->columnSpanFull(),
]),
Schemas\Components\Section::make('Plată & total')
->columns(3)
->schema([
Forms\Components\Select::make('pay_status')
->options(WorkOrder::PAY_STATUSES)
->default('unpaid')
->required(),
Forms\Components\TextInput::make('discount_pct')->label('Discount %')->numeric()->default(0),
Forms\Components\TextInput::make('total')->label('Total')->numeric()->disabled()->dehydrated(false),
Forms\Components\Toggle::make('approved')->label('Aprobat de client'),
Forms\Components\DatePicker::make('closed_at')->label('Închis'),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('number')->label('Nr.')->searchable()->sortable(),
Tables\Columns\TextColumn::make('opened_at')->label('Deschis')->date('d.m.Y')->sortable(),
Tables\Columns\TextColumn::make('client.name')->label('Client')->searchable(),
Tables\Columns\TextColumn::make('vehicle.plate')->label('Auto')->placeholder('—'),
Tables\Columns\TextColumn::make('master.name')->label('Maistru')->placeholder('—'),
Tables\Columns\TextColumn::make('status')
->formatStateUsing(fn ($state) => WorkOrder::STATUSES[$state] ?? $state)
->badge()
->colors([
'gray' => ['new'],
'info' => ['diagnosis', 'agreement', 'approved'],
'warning' => ['in_work', 'awaiting_parts'],
'success' => ['ready', 'done'],
'danger' => ['cancelled'],
]),
Tables\Columns\TextColumn::make('pay_status')
->formatStateUsing(fn ($state) => WorkOrder::PAY_STATUSES[$state] ?? $state)
->badge()
->colors([
'danger' => ['unpaid'],
'warning' => ['partial'],
'success' => ['paid'],
]),
Tables\Columns\TextColumn::make('total')->money('MDL')->alignRight()->sortable(),
])
->filters([
Tables\Filters\SelectFilter::make('status')->options(WorkOrder::STATUSES),
Tables\Filters\SelectFilter::make('pay_status')->options(WorkOrder::PAY_STATUSES),
Tables\Filters\SelectFilter::make('master_id')
->label('Maistru')
->options(fn () => User::pluck('name', 'id')),
])
->actions([
Actions\EditAction::make(),
Actions\DeleteAction::make(),
])
->defaultSort('opened_at', 'desc');
}
public static function getRelations(): array
{
return [
RelationManagers\WorksRelationManager::class,
RelationManagers\PartsRelationManager::class,
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListWorkOrders::route('/'),
'create' => Pages\CreateWorkOrder::route('/create'),
'edit' => Pages\EditWorkOrder::route('/{record}/edit'),
];
}
}