mirror of
https://bitbucket.org/studiotem/jandl-taskmanager-app.git
synced 2026-10-21 14:29:34 +00:00
87 lines
1.9 KiB
PHP
87 lines
1.9 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 function subtasks()
|
|
{
|
|
return $this->hasMany(SubTask::class);
|
|
}
|
|
|
|
public function project()
|
|
{
|
|
return $this->belongsTo(Project::class);
|
|
}
|
|
|
|
public function attachments()
|
|
{
|
|
return $this->hasMany(Attachment::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());
|
|
});
|
|
}
|
|
|
|
// TODO dorobit logiku
|
|
public static function generateMasterTaskColor()
|
|
{
|
|
// return random value from array
|
|
return collect(['#E9FFE5', '#F6ECD3', '#FDE1FF', '#FFC9C9', '#DADEF8', '#EDFFFE'])->random();
|
|
}
|
|
|
|
public function deleteAttachments()
|
|
{
|
|
foreach ( $this->attachments as $attachment ) {
|
|
$attachment->deleteFromS3();
|
|
}
|
|
|
|
$this->attachments()->delete();
|
|
|
|
return true;
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
}
|