1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Models/Task.php

94 lines
2.0 KiB
PHP
Raw Normal View History

2015-05-27 18:52:10 +02:00
<?php namespace App\Models;
use DB;
2015-07-12 21:43:45 +02:00
use Utils;
2015-05-27 18:52:10 +02:00
use Illuminate\Database\Eloquent\SoftDeletes;
2015-11-12 21:36:28 +01:00
use Laracasts\Presenter\PresentableTrait;
2015-05-27 18:52:10 +02:00
class Task extends EntityModel
{
use SoftDeletes;
2015-11-12 21:36:28 +01:00
use PresentableTrait;
protected $presenter = 'App\Ninja\Presenters\TaskPresenter';
2015-05-27 18:52:10 +02:00
public function account()
{
return $this->belongsTo('App\Models\Account');
}
2015-06-14 16:49:13 +02:00
public function invoice()
{
return $this->belongsTo('App\Models\Invoice');
}
2015-11-12 21:36:28 +01:00
public function user()
{
return $this->belongsTo('App\Models\User');
}
2015-05-27 18:52:10 +02:00
public function client()
{
return $this->belongsTo('App\Models\Client')->withTrashed();
}
2015-07-12 21:43:45 +02:00
public static function calcStartTime($task)
{
$parts = json_decode($task->time_log) ?: [];
if (count($parts)) {
return Utils::timestampToDateTimeString($parts[0][0]);
} else {
return '';
}
}
public function getStartTime()
{
return self::calcStartTime($this);
}
public static function calcDuration($task)
{
$duration = 0;
$parts = json_decode($task->time_log) ?: [];
foreach ($parts as $part) {
if (count($part) == 1 || !$part[1]) {
$duration += time() - $part[0];
} else {
$duration += $part[1] - $part[0];
}
}
return $duration;
}
public function getDuration()
{
return self::calcDuration($this);
}
public function getCurrentDuration()
{
$parts = json_decode($this->time_log) ?: [];
$part = $parts[count($parts)-1];
if (count($part) == 1 || !$part[1]) {
return time() - $part[0];
} else {
return 0;
}
}
public function hasPreviousDuration()
{
$parts = json_decode($this->time_log) ?: [];
return count($parts) && (count($parts[0]) && $parts[0][1]);
}
public function getHours()
{
return round($this->getDuration() / (60 * 60), 2);
}
2015-10-28 20:22:07 +01:00
}