From 77db357e173fc586bf4ca6838f4789b5a72a0a70 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 15 Feb 2018 20:47:51 +0200 Subject: [PATCH] Task report fixes --- app/Models/Task.php | 27 ++++++++++++++++++++------- app/Ninja/Reports/TaskReport.php | 9 +++++---- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/Models/Task.php b/app/Models/Task.php index 43a24cc6f81d..d6b01e2a60b4 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -129,17 +129,27 @@ class Task extends EntityModel * * @return int */ - public static function calcDuration($task) + public static function calcDuration($task, $startTimeCutoff = 0, $endTimeCutoff = 0) { $duration = 0; $parts = json_decode($task->time_log) ?: []; foreach ($parts as $part) { + $startTime = $part[0]; if (count($part) == 1 || ! $part[1]) { - $duration += time() - $part[0]; + $endTime = time(); } else { - $duration += $part[1] - $part[0]; + $endTime = $part[1]; } + + if ($startTimeCutoff) { + $startTime = max($startTime, $startTimeCutoff); + } + if ($endTimeCutoff) { + $endTime = min($endTime, $endTimeCutoff); + } + + $duration += $endTime - $startTime; } return $duration; @@ -148,9 +158,9 @@ class Task extends EntityModel /** * @return int */ - public function getDuration() + public function getDuration($startTimeCutoff = 0, $endTimeCutoff = 0) { - return self::calcDuration($this); + return self::calcDuration($this, $startTimeCutoff, $endTimeCutoff); } /** @@ -230,8 +240,11 @@ class Task extends EntityModel public function scopeDateRange($query, $startDate, $endDate) { - $query->whereRaw('cast(substring(time_log, 3, 10) as unsigned) >= ' . $startDate->format('U')); - $query->whereRaw('cast(substring(time_log, 3, 10) as unsigned) <= ' . $endDate->modify('+1 day')->format('U')); + $query->whereRaw('cast(substring(time_log, 3, 10) as unsigned) <= ' . $endDate->modify('+1 day')->format('U')) + ->whereRaw('case + when is_running then unix_timestamp() + else cast(substring(time_log, length(time_log) - 11, 10) as unsigned) + end >= ' . $startDate->format('U')); return $query; } diff --git a/app/Ninja/Reports/TaskReport.php b/app/Ninja/Reports/TaskReport.php index 1b1980c213a1..9419af24b43c 100644 --- a/app/Ninja/Reports/TaskReport.php +++ b/app/Ninja/Reports/TaskReport.php @@ -11,7 +11,7 @@ class TaskReport extends AbstractReport { return [ 'client' => [], - 'date' => [], + 'start_date' => [], 'project' => [], 'description' => [], 'duration' => [], @@ -32,7 +32,8 @@ class TaskReport extends AbstractReport ->dateRange($startDate, $endDate); foreach ($tasks->get() as $task) { - $amount = $task->getRate() * ($task->getDuration() / 60 / 60); + $duration = $task->getDuration($startDate->format('U'), $endDate->modify('+1 day')->format('U')); + $amount = $task->getRate() * ($duration / 60 / 60); if ($task->client && $task->client->currency_id) { $currencyId = $task->client->currency_id; } else { @@ -44,12 +45,12 @@ class TaskReport extends AbstractReport $this->isExport ? $task->getStartTime() : link_to($task->present()->url, $task->getStartTime()), $task->present()->project, $task->description, - Utils::formatTime($task->getDuration()), + Utils::formatTime($duration), Utils::formatMoney($amount, $currencyId), $task->user->getDisplayName(), ]; - $this->addToTotals($currencyId, 'duration', $task->getDuration()); + $this->addToTotals($currencyId, 'duration', $duration); $this->addToTotals($currencyId, 'amount', $amount); } }