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

  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use App\Models\Task;
  5. use App\Models\Model;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Database\Eloquent\Factories\HasFactory;
  8. class Work extends Model
  9. {
  10. use HasFactory;
  11. protected $fillable = [
  12. 'business_id', 'project_id', 'task_id', 'user_id', 'message', 'minute_sum', 'started_at', 'ended_at',
  13. 'task'
  14. ];
  15. protected $fillable_relations = ['task'];
  16. protected $casts = [
  17. 'started_at' => 'datetime:Y-m-d H:i',
  18. 'ended_at' => 'datetime:Y-m-d H:i'
  19. ];
  20. protected $reportable = [
  21. 'message', 'minute_sum', 'started_at', 'ended_at',
  22. ];
  23. public function rules()
  24. {
  25. $started_at = request()->started_at ?? $this->started_at->format('Y-m-d H:i');
  26. $ended_at = request()->ended_at ?? $this->ended_at->format('Y-m-d H:i');
  27. return [
  28. 'message' => 'nullable|string|min:3|max:225',
  29. 'started_at' => 'required|date_format:Y-m-d H:i|after:'.$this->task()->first()->created_at,
  30. 'ended_at' => [
  31. 'required', 'date_format:Y-m-d H:i', 'after:'.$started_at,
  32. function ($attribute, $value, $fail) use ($ended_at, $started_at) {
  33. $works = $this::where([
  34. ['ended_at', '>', $started_at],
  35. ['ended_at', '<', $ended_at],
  36. ])->orWhere([
  37. ['started_at', '>', $started_at],
  38. ['started_at', '<', $ended_at],
  39. ])->orWhere([
  40. ['started_at', '>=', $started_at],
  41. ['ended_at', '<=', $ended_at],
  42. ])->when(isset($this->id), function ($query) {
  43. return $query->where('id', '!=', $this->id);
  44. })->exists();
  45. if ($works) {
  46. $fail('The selected work is invalid.');
  47. }
  48. }
  49. ],
  50. ];
  51. }
  52. public function getValueOf(?string $key)
  53. {
  54. $values = [
  55. 'business_id' => $this->business_id,
  56. 'project_id' => $this->project_id,
  57. 'sprint_id' => null,
  58. 'workflow_id' => null,
  59. 'status_id' => null,
  60. 'system_id' => null,
  61. 'task_id' => $this->task->id,
  62. 'subject_id' => $this->id,
  63. 'user_id' => null,
  64. ];
  65. if ($key && isset($values, $key)) {
  66. return $values[$key];
  67. }
  68. return $values;
  69. }
  70. public function updateRelations()
  71. {
  72. $this->filled_relations['task']['work_start'] = Work::where('task_id', $this->task_id)->orderBy('started_at')->first()->started_at ?? null;
  73. $this->task()->update($this->filled_relations['task']);
  74. }
  75. public function task()
  76. {
  77. return $this->belongsTo(Task::class, 'task_id', 'id');
  78. }
  79. public function scopeStartedAtIn($query, $days)
  80. {
  81. return $query->whereDate('started_at', '>=', Carbon::now()->modify('-'.$days.' day')->toDate());
  82. }
  83. public function scopeStartedAt($query, $date)
  84. {
  85. return $query->whereDate('started_at', '=', $date);
  86. }
  87. public function scopeSpentTimeFrom($query, $data)
  88. {
  89. return $query->where('minute_sum', '>=', $data);
  90. }
  91. public function scopeSpentTimeTo($query, $data)
  92. {
  93. return $query->where('minute_sum', '<=', $data);
  94. }
  95. public function scopeReport($query)
  96. {
  97. return $query->select(
  98. 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')
  99. )->groupBy('user_id');
  100. }
  101. public function scopeReportByDate($query)
  102. {
  103. return $query->select(
  104. DB::raw('DATE(started_at) as date, SUM(minute_sum) as work_minute_sum')
  105. )->groupBy('date');
  106. }
  107. }