'datetime', 'assigned_at' => 'datetime', 'started_at' => 'datetime', 'completed_at' => 'datetime', 'labor_hours' => 'decimal:2', 'labor_cost' => 'decimal:2', 'parts_cost' => 'decimal:2', 'total_cost' => 'decimal:2', ]; public function vehicle(): BelongsTo { return $this->belongsTo(Vehicle::class); } public function technicians(): BelongsToMany { return $this->belongsToMany(Technician::class) ->withPivot('is_lead') ->withTimestamps(); } public function parts(): BelongsToMany { return $this->belongsToMany(Part::class) ->withPivot('quantity_used', 'unit_cost') ->withTimestamps(); } public function repairTasks(): HasMany { return $this->hasMany(RepairTask::class); } public function documents(): MorphMany { return $this->morphMany(Document::class, 'documentable'); } public function getLeadTechnician() { return $this->technicians()->wherePivot('is_lead', true)->first(); } public function calculateCosts() { // Calculate parts cost $this->parts_cost = $this->parts() ->withPivot('quantity_used', 'unit_cost') ->get() ->sum(function ($part) { return $part->pivot->quantity_used * $part->pivot->unit_cost; }); // Calculate labor cost based on tasks $this->labor_hours = $this->repairTasks()->sum('actual_hours'); $this->labor_cost = $this->labor_hours * config('workshop.hourly_rate', 85.00); // Calculate total cost $this->total_cost = $this->parts_cost + $this->labor_cost; $this->save(); } }