Stage 4 — Labor Catalog: fixed price + default parts + service templates

Schema:
- labors.pricing_mode (hourly/fixed) + fixed_price
- labor_parts (default parts auto-added with a labor)
- service_templates + service_template_items (labor/part bundles)

ServiceComposer:
- addLabor(wo, labor, withParts) — hourly (hours×rate) or fixed (fixed_price),
  then auto-adds the labor's default parts
- addPart(wo, part, qty) — catalog price snapshot
- applyTemplate(wo, template) — adds all labor+part lines, recalcs total
- hourlyRate from settings.labor_rate

Filament:
- LaborResource: pricing_mode (live) toggles hours/fixed_price fields,
  DefaultPartsRelationManager
- ServiceTemplateResource (Service group) with ItemsRelationManager
- WorkOrder edit "Aplică șablon" action → applyTemplate
- WorksRelationManager CreateAction auto-adds labor default parts

Tests (6 new):
- hourly rate×hours; fixed uses fixed_price; default parts auto-added;
  withParts=false skips; applyTemplate adds lines + recalcs total;
  templates tenant-isolated

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-28 06:16:50 +00:00
parent c90c35d930
commit a1be01b0d5
16 changed files with 788 additions and 5 deletions
@@ -3,6 +3,7 @@
namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\LaborResource\Pages;
use App\Filament\Tenant\Resources\LaborResource\RelationManagers;
use App\Models\Tenant\Labor;
use Filament\Actions;
use Filament\Forms;
@@ -42,8 +43,17 @@ class LaborResource extends Resource
Forms\Components\TextInput::make('code')->label('Cod')->maxLength(32),
Forms\Components\TextInput::make('name_ro')->label('Nume (RO)')->required()->maxLength(160),
Forms\Components\TextInput::make('name_ru')->label('Nume (RU)')->maxLength(160),
Forms\Components\TextInput::make('hours')->label('Ore')->numeric()->default(1)->required(),
Forms\Components\TextInput::make('price')->label('Preț (MDL)')->numeric()->default(0),
Forms\Components\Select::make('pricing_mode')
->label('Mod tarifare')
->options(Labor::PRICING_MODES)
->default('hourly')
->live()
->required(),
Forms\Components\TextInput::make('hours')->label('Ore (normă)')->numeric()->default(1)
->visible(fn (Schemas\Components\Utilities\Get $get) => $get('pricing_mode') !== 'fixed'),
Forms\Components\TextInput::make('fixed_price')->label('Preț fix (MDL)')->numeric()->default(0)
->visible(fn (Schemas\Components\Utilities\Get $get) => $get('pricing_mode') === 'fixed'),
Forms\Components\TextInput::make('price')->label('Preț orientativ (MDL)')->numeric()->default(0),
Forms\Components\Toggle::make('is_active')->label('Activă')->default(true),
]),
Forms\Components\Textarea::make('notes')->label('Observații')->columnSpanFull()->rows(2),
@@ -56,8 +66,15 @@ class LaborResource extends Resource
->columns([
Tables\Columns\TextColumn::make('category')->label('Categorie')->badge()->sortable(),
Tables\Columns\TextColumn::make('name_ro')->label('Manoperă')->searchable()->sortable(),
Tables\Columns\TextColumn::make('pricing_mode')
->label('Tarifare')
->formatStateUsing(fn ($s) => $s === 'fixed' ? 'Fix' : 'Pe oră')
->badge()
->color(fn ($s) => $s === 'fixed' ? 'info' : 'gray'),
Tables\Columns\TextColumn::make('hours')->label('Ore')->numeric(decimalPlaces: 2)->alignRight(),
Tables\Columns\TextColumn::make('price')->label('Preț')->money('MDL')->alignRight(),
Tables\Columns\TextColumn::make('fixed_price')->label('Preț fix')->money('MDL')->alignRight()
->placeholder('—')->toggleable(),
Tables\Columns\TextColumn::make('laborParts_count')->counts('laborParts')->label('Piese impl.')->alignRight()->toggleable(),
Tables\Columns\IconColumn::make('is_active')->label('Activă')->boolean(),
])
->filters([
@@ -73,6 +90,13 @@ class LaborResource extends Resource
->defaultGroup('category');
}
public static function getRelations(): array
{
return [
RelationManagers\DefaultPartsRelationManager::class,
];
}
public static function getPages(): array
{
return [
@@ -0,0 +1,57 @@
<?php
namespace App\Filament\Tenant\Resources\LaborResource\RelationManagers;
use App\Models\Tenant\Part;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class DefaultPartsRelationManager extends RelationManager
{
protected static string $relationship = 'laborParts';
protected static ?string $title = 'Piese implicite';
public function form(Schema $schema): Schema
{
return $schema->components([
Forms\Components\Select::make('part_id')
->label('Piesă')
->options(fn () => Part::where('is_active', true)
->get()
->mapWithKeys(fn ($p) => [$p->id => "{$p->name} " . ($p->article ? "[{$p->article}]" : '')])
->toArray())
->searchable()
->required()
->live()
->afterStateUpdated(function ($state, Set $set) {
if ($state && $p = Part::find($state)) {
$set('unit', $p->unit);
}
}),
Forms\Components\TextInput::make('qty')->label('Cantitate')->numeric()->default(1)->required(),
Forms\Components\TextInput::make('unit')->label('UM')->default('buc')->maxLength(16),
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('part.name')
->columns([
Tables\Columns\TextColumn::make('part.name')->label('Piesă')->wrap(),
Tables\Columns\TextColumn::make('part.article')->label('Cod')->placeholder('—'),
Tables\Columns\TextColumn::make('qty')->label('Cant.')->alignRight(),
Tables\Columns\TextColumn::make('unit')->label('UM'),
])
->headerActions([Actions\CreateAction::make()])
->actions([Actions\EditAction::make(), Actions\DeleteAction::make()])
->emptyStateHeading('Nicio piesă implicită')
->emptyStateDescription('Adaugă piesele care se montează de obicei la această manoperă — se adaugă automat în fișă când selectezi manopera.');
}
}
@@ -0,0 +1,88 @@
<?php
namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\ServiceTemplateResource\Pages;
use App\Filament\Tenant\Resources\ServiceTemplateResource\RelationManagers;
use App\Models\Tenant\Labor;
use App\Models\Tenant\ServiceTemplate;
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 ServiceTemplateResource extends Resource
{
protected static ?string $model = ServiceTemplate::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-clipboard-document-list';
protected static ?string $navigationLabel = 'Șabloane servicii';
protected static string|\UnitEnum|null $navigationGroup = 'Service';
protected static ?string $modelLabel = 'șablon';
protected static ?string $pluralModelLabel = 'șabloane servicii';
protected static ?int $navigationSort = 33;
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make()
->columns(2)
->schema([
Forms\Components\TextInput::make('name')->label('Denumire')->required()
->placeholder('ex: Revizie completă 15.000 km')->columnSpanFull(),
Forms\Components\Select::make('category')
->label('Categorie')
->options(array_combine(Labor::CATEGORIES, Labor::CATEGORIES))
->searchable(),
Forms\Components\Toggle::make('is_active')->label('Activ')->default(true),
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('name')->searchable()->sortable(),
Tables\Columns\TextColumn::make('category')->badge()->placeholder('—'),
Tables\Columns\TextColumn::make('items_count')->counts('items')->label('Linii')->alignRight(),
Tables\Columns\IconColumn::make('is_active')->label('Activ')->boolean(),
])
->filters([
Tables\Filters\TernaryFilter::make('is_active')->label('Active'),
])
->actions([
Actions\EditAction::make(),
Actions\DeleteAction::make(),
])
->emptyStateHeading('Niciun șablon')
->emptyStateDescription('Grupează manopere + piese frecvente într-un șablon (ex: „Schimb ulei complet") și aplică-l pe o fișă cu un click.')
->emptyStateIcon('heroicon-o-clipboard-document-list')
->defaultSort('name');
}
public static function getRelations(): array
{
return [
RelationManagers\ItemsRelationManager::class,
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListServiceTemplates::route('/'),
'create' => Pages\CreateServiceTemplate::route('/create'),
'edit' => Pages\EditServiceTemplate::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Tenant\Resources\ServiceTemplateResource\Pages;
use App\Filament\Tenant\Resources\ServiceTemplateResource;
use Filament\Resources\Pages\CreateRecord;
class CreateServiceTemplate extends CreateRecord
{
protected static string $resource = ServiceTemplateResource::class;
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\ServiceTemplateResource\Pages;
use App\Filament\Tenant\Resources\ServiceTemplateResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditServiceTemplate extends EditRecord
{
protected static string $resource = ServiceTemplateResource::class;
protected function getHeaderActions(): array
{
return [Actions\DeleteAction::make()];
}
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\ServiceTemplateResource\Pages;
use App\Filament\Tenant\Resources\ServiceTemplateResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListServiceTemplates extends ListRecords
{
protected static string $resource = ServiceTemplateResource::class;
protected function getHeaderActions(): array
{
return [Actions\CreateAction::make()];
}
}
@@ -0,0 +1,79 @@
<?php
namespace App\Filament\Tenant\Resources\ServiceTemplateResource\RelationManagers;
use App\Models\Tenant\Labor;
use App\Models\Tenant\Part;
use App\Models\Tenant\ServiceTemplateItem;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class ItemsRelationManager extends RelationManager
{
protected static string $relationship = 'items';
protected static ?string $title = 'Conținut șablon';
public function form(Schema $schema): Schema
{
return $schema->components([
Forms\Components\Select::make('kind')
->label('Tip')
->options(ServiceTemplateItem::KINDS)
->default('labor')
->live()
->required(),
Forms\Components\Select::make('labor_id')
->label('Manoperă')
->options(fn () => Labor::where('is_active', true)->pluck('name_ro', 'id'))
->searchable()
->visible(fn (Get $get) => $get('kind') === 'labor')
->live()
->afterStateUpdated(function ($state, Set $set) {
if ($state && $l = Labor::find($state)) {
$set('name', $l->name_ro);
$set('hours', $l->hours);
}
}),
Forms\Components\Select::make('part_id')
->label('Piesă')
->options(fn () => Part::where('is_active', true)
->get()->mapWithKeys(fn ($p) => [$p->id => "{$p->name} " . ($p->article ? "[{$p->article}]" : '')])->toArray())
->searchable()
->visible(fn (Get $get) => $get('kind') === 'part')
->live()
->afterStateUpdated(function ($state, Set $set) {
if ($state && $p = Part::find($state)) $set('name', $p->name);
}),
Forms\Components\TextInput::make('name')->label('Denumire')->required()->columnSpanFull(),
Forms\Components\TextInput::make('hours')->label('Ore')->numeric()
->visible(fn (Get $get) => $get('kind') === 'labor'),
Forms\Components\TextInput::make('qty')->label('Cantitate')->numeric()->default(1)
->visible(fn (Get $get) => $get('kind') === 'part'),
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('name')
->columns([
Tables\Columns\TextColumn::make('kind')
->label('Tip')
->formatStateUsing(fn ($s) => ServiceTemplateItem::KINDS[$s] ?? $s)
->badge()
->color(fn ($s) => $s === 'labor' ? 'info' : 'gray'),
Tables\Columns\TextColumn::make('name')->wrap(),
Tables\Columns\TextColumn::make('hours')->label('Ore')->placeholder('—')->alignRight(),
Tables\Columns\TextColumn::make('qty')->label('Cant.')->placeholder('—')->alignRight(),
])
->headerActions([Actions\CreateAction::make()])
->actions([Actions\EditAction::make(), Actions\DeleteAction::make()]);
}
}
@@ -15,6 +15,26 @@ class EditWorkOrder extends EditRecord
protected function getHeaderActions(): array
{
return [
Actions\Action::make('apply_template')
->label('Aplică șablon')
->icon('heroicon-m-clipboard-document-list')
->color('gray')
->schema([
\Filament\Forms\Components\Select::make('template_id')
->label('Șablon serviciu')
->options(fn () => \App\Models\Tenant\ServiceTemplate::where('is_active', true)->pluck('name', 'id'))
->searchable()
->required(),
])
->action(function (array $data) {
$template = \App\Models\Tenant\ServiceTemplate::with('items')->find($data['template_id']);
if (! $template) return;
$r = app(\App\Services\ServiceComposer::class)->applyTemplate($this->record, $template);
$this->fillForm();
\Filament\Notifications\Notification::make()
->title("Șablon aplicat: {$r['labor']} manopere, {$r['parts']} piese")
->success()->send();
}),
Actions\Action::make('ai_diagnose')
->label('AI: sugerează diagnostic')
->icon('heroicon-m-sparkles')
@@ -69,7 +69,23 @@ class WorksRelationManager extends RelationManager
->colors(['gray' => ['todo'], 'warning' => ['in_progress'], 'success' => ['done']]),
])
->headerActions([
Actions\CreateAction::make(),
Actions\CreateAction::make()
->after(function (WorkOrderWork $record) {
// Auto-add the labor's default parts to the parent WO.
if (! $record->labor_id) return;
$labor = Labor::with('laborParts.part')->find($record->labor_id);
$wo = $record->workOrder;
if (! $labor || ! $wo || $labor->laborParts->isEmpty()) return;
$composer = app(\App\Services\ServiceComposer::class);
foreach ($labor->laborParts as $lp) {
if ($lp->part) {
$composer->addPart($wo, $lp->part, (float) $lp->qty, $lp->unit);
}
}
\Filament\Notifications\Notification::make()
->title('Piese implicite adăugate (' . $labor->laborParts->count() . ')')
->success()->send();
}),
])
->actions([
Actions\EditAction::make(),