You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

126 lines
3.8 KiB

<?php
namespace App\Models;
use Carbon\Carbon;
use App\Models\Task;
use App\Models\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Work extends Model
{
use HasFactory;
protected $fillable = [
'business_id', 'project_id', 'task_id', 'user_id', 'message', 'minute_sum', 'started_at', 'ended_at',
'task'
];
protected $fillable_relations = ['task'];
protected $casts = [
'started_at' => 'datetime:Y-m-d H:i',
'ended_at' => 'datetime:Y-m-d H:i'
];
protected $reportable = [
'message', 'minute_sum', 'started_at', 'ended_at',
];
public function rules()
{
$started_at = request()->started_at ?? $this->started_at->format('Y-m-d H:i');
$ended_at = request()->ended_at ?? $this->ended_at->format('Y-m-d H:i');
return [
'message' => 'nullable|string|min:3|max:225',
'started_at' => 'required|date_format:Y-m-d H:i|after:'.$this->task()->first()->created_at,
'ended_at' => [
'required', 'date_format:Y-m-d H:i', 'after:'.$started_at,
function ($attribute, $value, $fail) use ($ended_at, $started_at) {
$works = $this::where([
['ended_at', '>', $started_at],
['ended_at', '<', $ended_at],
])->orWhere([
['started_at', '>', $started_at],
['started_at', '<', $ended_at],
])->orWhere([
['started_at', '>=', $started_at],
['ended_at', '<=', $ended_at],
])->when(isset($this->id), function ($query) {
return $query->where('id', '!=', $this->id);
})->exists();
if ($works) {
$fail('The selected work is invalid.');
}
}
],
];
}
public function getValueOf(?string $key)
{
$values = [
'business_id' => $this->business_id,
'project_id' => $this->project_id,
'sprint_id' => null,
'workflow_id' => null,
'status_id' => null,
'system_id' => null,
'task_id' => $this->task->id,
'subject_id' => $this->id,
'user_id' => null,
];
if ($key && isset($values, $key)) {
return $values[$key];
}
return $values;
}
public function updateRelations()
{
$this->filled_relations['task']['work_start'] = Work::where('task_id', $this->task_id)->orderBy('started_at')->first()->started_at ?? null;
$this->task()->update($this->filled_relations['task']);
}
public function task()
{
return $this->belongsTo(Task::class, 'task_id', 'id');
}
public function scopeStartedAtIn($query, $days)
{
return $query->whereDate('started_at', '>=', Carbon::now()->modify('-'.$days.' day')->toDate());
}
public function scopeStartedAt($query, $date)
{
return $query->whereDate('started_at', '=', $date);
}
public function scopeSpentTimeFrom($query, $data)
{
return $query->where('minute_sum', '>=', $data);
}
public function scopeSpentTimeTo($query, $data)
{
return $query->where('minute_sum', '<=', $data);
}
public function scopeReport($query)
{
return $query->select(
DB::raw('user_id , MIN(started_at) as started_at_min, MAX(ended_at) as ended_at_max, SUM(minute_sum) as work_minute_sum')
)->groupBy('user_id');
}
public function scopeReportByDate($query)
{
return $query->select(
DB::raw('DATE(started_at) as date, SUM(minute_sum) as work_minute_sum')
)->groupBy('date');
}
}