Task duration / start time helpers

This commit is contained in:
David Bomba 2020-10-29 13:24:12 +11:00
parent dbcfce8bb9
commit 73802c3647
2 changed files with 71 additions and 0 deletions

View File

@ -15,6 +15,7 @@ use App\Models\Filterable;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
class Task extends BaseModel
{
@ -81,4 +82,69 @@ class Task extends BaseModel
{
return $this->belongsTo(Project::class);
}
public static function calcstart_time()
{
$parts = json_decode($this->time_log) ?: [];
if (count($parts)) {
return Carbon::createFromTimeStamp($parts[0][0]);
} else {
return '';
}
}
public function getLaststart_time()
{
$parts = json_decode($this->time_log) ?: [];
if (count($parts)) {
$index = count($parts) - 1;
return $parts[$index][0];
} else {
return '';
}
}
public static function calcDuration($start_time_cutoff = 0, $end_time_cutoff = 0)
{
$duration = 0;
$parts = json_decode($this->time_log) ?: [];
foreach ($parts as $part) {
$start_time = $part[0];
if (count($part) == 1 || ! $part[1]) {
$end_time = time();
} else {
$end_time = $part[1];
}
if ($start_time_cutoff) {
$start_time = max($start_time, $start_time_cutoff);
}
if ($end_time_cutoff) {
$end_time = min($end_time, $end_time_cutoff);
}
$duration += max($end_time - $start_time, 0);
}
return round($duration);
}
}

View File

@ -50,6 +50,11 @@ class TaskRepository extends BaseRepository
{
$task->fill($data);
if(!$task->start_time)
$task->start_time = $task->calcStartTime();
$task->duration = $task->calcDuration();
$task->save();
if (array_key_exists('documents', $data)) {