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
+22 -1
View File
@@ -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);
}
}
+26
View File
@@ -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);
}
}
+32
View File
@@ -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');
}
}
+39
View File
@@ -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);
}
}