Task report fixes

This commit is contained in:
Hillel Coren 2018-02-15 20:47:51 +02:00
parent 227019f95e
commit 77db357e17
2 changed files with 25 additions and 11 deletions

View File

@ -129,17 +129,27 @@ class Task extends EntityModel
* *
* @return int * @return int
*/ */
public static function calcDuration($task) public static function calcDuration($task, $startTimeCutoff = 0, $endTimeCutoff = 0)
{ {
$duration = 0; $duration = 0;
$parts = json_decode($task->time_log) ?: []; $parts = json_decode($task->time_log) ?: [];
foreach ($parts as $part) { foreach ($parts as $part) {
$startTime = $part[0];
if (count($part) == 1 || ! $part[1]) { if (count($part) == 1 || ! $part[1]) {
$duration += time() - $part[0]; $endTime = time();
} else { } 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; return $duration;
@ -148,9 +158,9 @@ class Task extends EntityModel
/** /**
* @return int * @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) 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; return $query;
} }

View File

@ -11,7 +11,7 @@ class TaskReport extends AbstractReport
{ {
return [ return [
'client' => [], 'client' => [],
'date' => [], 'start_date' => [],
'project' => [], 'project' => [],
'description' => [], 'description' => [],
'duration' => [], 'duration' => [],
@ -32,7 +32,8 @@ class TaskReport extends AbstractReport
->dateRange($startDate, $endDate); ->dateRange($startDate, $endDate);
foreach ($tasks->get() as $task) { 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) { if ($task->client && $task->client->currency_id) {
$currencyId = $task->client->currency_id; $currencyId = $task->client->currency_id;
} else { } else {
@ -44,12 +45,12 @@ class TaskReport extends AbstractReport
$this->isExport ? $task->getStartTime() : link_to($task->present()->url, $task->getStartTime()), $this->isExport ? $task->getStartTime() : link_to($task->present()->url, $task->getStartTime()),
$task->present()->project, $task->present()->project,
$task->description, $task->description,
Utils::formatTime($task->getDuration()), Utils::formatTime($duration),
Utils::formatMoney($amount, $currencyId), Utils::formatMoney($amount, $currencyId),
$task->user->getDisplayName(), $task->user->getDisplayName(),
]; ];
$this->addToTotals($currencyId, 'duration', $task->getDuration()); $this->addToTotals($currencyId, 'duration', $duration);
$this->addToTotals($currencyId, 'amount', $amount); $this->addToTotals($currencyId, 'amount', $amount);
} }
} }