Initial commit
This commit is contained in:
59
app/Models/IssueTimeLog.php
Normal file
59
app/Models/IssueTimeLog.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class IssueTimeLog extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'issue_id',
|
||||
'user_id',
|
||||
'hours',
|
||||
'description',
|
||||
'logged_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'hours' => 'decimal:2',
|
||||
'logged_at' => 'datetime',
|
||||
];
|
||||
|
||||
public function issue(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Issue::class);
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
// Update issue actual_hours when time log is created, updated, or deleted
|
||||
static::created(function ($timeLog) {
|
||||
$timeLog->updateIssueActualHours();
|
||||
});
|
||||
|
||||
static::updated(function ($timeLog) {
|
||||
$timeLog->updateIssueActualHours();
|
||||
});
|
||||
|
||||
static::deleted(function ($timeLog) {
|
||||
$timeLog->updateIssueActualHours();
|
||||
});
|
||||
}
|
||||
|
||||
protected function updateIssueActualHours(): void
|
||||
{
|
||||
$totalHours = $this->issue->timeLogs()->sum('hours');
|
||||
$this->issue->update(['actual_hours' => $totalHours]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user