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:
@@ -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 [
|
||||
|
||||
+57
@@ -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()];
|
||||
}
|
||||
}
|
||||
+79
@@ -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')
|
||||
|
||||
+17
-1
@@ -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(),
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Models\Tenant;
|
||||
|
||||
use App\Models\Concerns\BelongsToTenant;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class Labor extends Model
|
||||
@@ -15,14 +16,34 @@ class Labor extends Model
|
||||
'Caroserie', 'Electrică', 'Climatizare', 'Eșapament', 'Altele',
|
||||
];
|
||||
|
||||
public const PRICING_MODES = [
|
||||
'hourly' => 'Pe oră (normă × tarif)',
|
||||
'fixed' => 'Preț fix',
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'company_id', 'category', 'name_ro', 'name_ru', 'code',
|
||||
'hours', 'price', 'is_active', 'notes',
|
||||
'hours', 'pricing_mode', 'fixed_price', 'price', 'is_active', 'notes',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'hours' => 'decimal:2',
|
||||
'fixed_price' => 'decimal:2',
|
||||
'price' => 'decimal:2',
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
|
||||
public function laborParts(): HasMany
|
||||
{
|
||||
return $this->hasMany(LaborPart::class);
|
||||
}
|
||||
|
||||
/** Effective line total for this labor given the tenant hourly rate. */
|
||||
public function effectiveTotal(float $hourlyRate): float
|
||||
{
|
||||
if ($this->pricing_mode === 'fixed') {
|
||||
return (float) $this->fixed_price;
|
||||
}
|
||||
return round((float) $this->hours * $hourlyRate, 2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Tenant;
|
||||
|
||||
use App\Models\Concerns\BelongsToTenant;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class LaborPart extends Model
|
||||
{
|
||||
use BelongsToTenant;
|
||||
|
||||
protected $fillable = ['company_id', 'labor_id', 'part_id', 'qty', 'unit'];
|
||||
|
||||
protected $casts = ['qty' => 'decimal:2'];
|
||||
|
||||
public function labor(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Labor::class);
|
||||
}
|
||||
|
||||
public function part(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Part::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Tenant;
|
||||
|
||||
use App\Models\Concerns\BelongsToTenant;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class ServiceTemplate extends Model
|
||||
{
|
||||
use BelongsToTenant, SoftDeletes;
|
||||
|
||||
protected $fillable = ['company_id', 'name', 'category', 'notes', 'is_active'];
|
||||
|
||||
protected $casts = ['is_active' => 'boolean'];
|
||||
|
||||
public function items(): HasMany
|
||||
{
|
||||
return $this->hasMany(ServiceTemplateItem::class);
|
||||
}
|
||||
|
||||
public function laborItems(): HasMany
|
||||
{
|
||||
return $this->items()->where('kind', 'labor');
|
||||
}
|
||||
|
||||
public function partItems(): HasMany
|
||||
{
|
||||
return $this->items()->where('kind', 'part');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Tenant;
|
||||
|
||||
use App\Models\Concerns\BelongsToTenant;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class ServiceTemplateItem extends Model
|
||||
{
|
||||
use BelongsToTenant;
|
||||
|
||||
public const KINDS = ['labor' => 'Manoperă', 'part' => 'Piesă'];
|
||||
|
||||
protected $fillable = [
|
||||
'company_id', 'service_template_id', 'kind',
|
||||
'labor_id', 'part_id', 'name', 'qty', 'hours',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'qty' => 'decimal:2',
|
||||
'hours' => 'decimal:2',
|
||||
];
|
||||
|
||||
public function template(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(ServiceTemplate::class, 'service_template_id');
|
||||
}
|
||||
|
||||
public function labor(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Labor::class);
|
||||
}
|
||||
|
||||
public function part(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Part::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Central\Company;
|
||||
use App\Models\Tenant\Labor;
|
||||
use App\Models\Tenant\Part;
|
||||
use App\Models\Tenant\ServiceTemplate;
|
||||
use App\Models\Tenant\WorkOrder;
|
||||
use App\Models\Tenant\WorkOrderPart;
|
||||
use App\Models\Tenant\WorkOrderWork;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Composes work order lines from the labor catalog + templates:
|
||||
* - adds a labor (hourly or fixed) and optionally its default parts
|
||||
* - applies a full service template (labor + part lines) in one shot
|
||||
*/
|
||||
class ServiceComposer
|
||||
{
|
||||
public function hourlyRate(int $companyId): float
|
||||
{
|
||||
$company = Company::withoutGlobalScopes()->find($companyId);
|
||||
return (float) data_get($company?->settings, 'labor_rate', 400);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a labor line to a WO. Fixed-price labors set total directly via
|
||||
* hours=1 × price_per_hour=fixed_price. Returns the created work line.
|
||||
*/
|
||||
public function addLabor(WorkOrder $wo, Labor $labor, bool $withParts = true): WorkOrderWork
|
||||
{
|
||||
$rate = $this->hourlyRate($wo->company_id);
|
||||
|
||||
return DB::transaction(function () use ($wo, $labor, $withParts, $rate) {
|
||||
if ($labor->pricing_mode === 'fixed') {
|
||||
$hours = 1;
|
||||
$pricePerHour = (float) $labor->fixed_price;
|
||||
} else {
|
||||
$hours = (float) $labor->hours ?: 1;
|
||||
$pricePerHour = $rate;
|
||||
}
|
||||
|
||||
$work = WorkOrderWork::create([
|
||||
'work_order_id' => $wo->id,
|
||||
'labor_id' => $labor->id,
|
||||
'name' => $labor->name_ro,
|
||||
'hours' => $hours,
|
||||
'price_per_hour' => $pricePerHour,
|
||||
'status' => 'todo',
|
||||
]);
|
||||
|
||||
if ($withParts) {
|
||||
foreach ($labor->laborParts as $lp) {
|
||||
$part = $lp->part;
|
||||
if (! $part) continue;
|
||||
$this->addPart($wo, $part, (float) $lp->qty, $lp->unit ?: $part->unit);
|
||||
}
|
||||
}
|
||||
|
||||
return $work;
|
||||
});
|
||||
}
|
||||
|
||||
public function addPart(WorkOrder $wo, Part $part, float $qty, ?string $unit = null): WorkOrderPart
|
||||
{
|
||||
return WorkOrderPart::create([
|
||||
'work_order_id' => $wo->id,
|
||||
'part_id' => $part->id,
|
||||
'name' => $part->name,
|
||||
'article' => $part->article,
|
||||
'brand' => $part->brand,
|
||||
'qty' => $qty,
|
||||
'unit' => $unit ?: $part->unit ?: 'buc',
|
||||
'buy_price' => (float) $part->buy_price,
|
||||
'sell_price' => (float) $part->sell_price,
|
||||
'status' => 'needed',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a full template to a WO: every labor + part item becomes a WO line.
|
||||
* Labor items are added WITHOUT their own default parts (template is explicit).
|
||||
*
|
||||
* @return array{labor:int, parts:int}
|
||||
*/
|
||||
public function applyTemplate(WorkOrder $wo, ServiceTemplate $template): array
|
||||
{
|
||||
return DB::transaction(function () use ($wo, $template) {
|
||||
$laborCount = 0;
|
||||
$partCount = 0;
|
||||
|
||||
foreach ($template->items as $item) {
|
||||
if ($item->kind === 'labor') {
|
||||
if ($item->labor_id && ($labor = Labor::find($item->labor_id))) {
|
||||
$work = $this->addLabor($wo, $labor, withParts: false);
|
||||
if ($item->hours) {
|
||||
$work->hours = (float) $item->hours;
|
||||
$work->save();
|
||||
}
|
||||
} else {
|
||||
// Free-text labor line from snapshot.
|
||||
WorkOrderWork::create([
|
||||
'work_order_id' => $wo->id,
|
||||
'name' => $item->name,
|
||||
'hours' => (float) ($item->hours ?: 1),
|
||||
'price_per_hour' => $this->hourlyRate($wo->company_id),
|
||||
'status' => 'todo',
|
||||
]);
|
||||
}
|
||||
$laborCount++;
|
||||
} elseif ($item->kind === 'part') {
|
||||
if ($item->part_id && ($part = Part::find($item->part_id))) {
|
||||
$this->addPart($wo, $part, (float) ($item->qty ?: 1));
|
||||
} else {
|
||||
WorkOrderPart::create([
|
||||
'work_order_id' => $wo->id,
|
||||
'name' => $item->name,
|
||||
'qty' => (float) ($item->qty ?: 1),
|
||||
'unit' => 'buc',
|
||||
'sell_price' => 0,
|
||||
'status' => 'needed',
|
||||
]);
|
||||
}
|
||||
$partCount++;
|
||||
}
|
||||
}
|
||||
|
||||
$wo->recalcTotal();
|
||||
|
||||
return ['labor' => $laborCount, 'parts' => $partCount];
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user