mirror of
https://bitbucket.org/studiotem/jandl-taskmanager-app.git
synced 2026-10-21 06:20:21 +00:00
91 lines
2.2 KiB
PHP
91 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class MasterTask extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
public $timestamps = true;
|
|
public $guarded = ['id'];
|
|
|
|
public static function generateMasterTaskColor()
|
|
{
|
|
// return random value from array
|
|
return collect(['#E9FFE5', '#F6ECD3', '#FDE1FF', '#FFC9C9', '#DADEF8', '#EDFFFE', '#FFDED7'])->random();
|
|
}
|
|
|
|
public function subtasks()
|
|
{
|
|
return $this->hasMany(SubTask::class);
|
|
}
|
|
|
|
public function project()
|
|
{
|
|
return $this->belongsTo(Project::class);
|
|
}
|
|
|
|
public function team()
|
|
{
|
|
return $this->belongsTo(Team::class);
|
|
}
|
|
|
|
// vracia userov, ktori su priradeni k subtasku ale nepatria do teamu
|
|
public function getUsersOutsideTeamAttribute()
|
|
{
|
|
$users = $this->subtasks->pluck('user')->unique();
|
|
|
|
// ak nie je uvedeny ziaden team, tak sa vratia vsetci useri
|
|
if ( ! $this->team) {
|
|
return $users;
|
|
}
|
|
|
|
return $users->filter(function ($user) {
|
|
return ! in_array($this->team->id, $user->teams->pluck('id')->unique()->toArray());
|
|
});
|
|
}
|
|
|
|
// vsetci useri, ktory pracuju na master tasku (tzn. maju vytvorene subtasky k danemu master tasku)
|
|
public function getAllUsersInMasterTaskAttribute()
|
|
{
|
|
return $this->subtasks->pluck('user')->unique();
|
|
}
|
|
|
|
public function deleteAttachments()
|
|
{
|
|
foreach ($this->attachments as $attachment) {
|
|
$attachment->deleteFromS3();
|
|
}
|
|
|
|
$this->attachments()->delete();
|
|
|
|
return true;
|
|
}
|
|
|
|
public function attachments()
|
|
{
|
|
return $this->hasMany(Attachment::class);
|
|
}
|
|
|
|
public function recalculateEstimatedTime()
|
|
{
|
|
$this->estimated_time = $this->subtasks->sum('estimated_time');
|
|
$this->save();
|
|
}
|
|
|
|
public function recalculateTrackedTime()
|
|
{
|
|
$trackedTime = 0.0;
|
|
|
|
foreach ($this->subtasks as $subtask) {
|
|
$trackedTime += $subtask->timesheets->sum('time');
|
|
}
|
|
|
|
$this->tracked_time = $trackedTime;
|
|
$this->save();
|
|
}
|
|
}
|