1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Models/Task.php

209 lines
4.1 KiB
PHP
Raw Normal View History

2015-05-27 18:52:10 +02:00
<?php namespace App\Models;
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;
2016-07-10 12:20:22 +02:00
use App\Events\TaskWasCreated;
use App\Events\TaskWasUpdated;
2015-05-27 18:52:10 +02:00
/**
* Class Task
*/
2015-05-27 18:52:10 +02:00
class Task extends EntityModel
{
use SoftDeletes;
2015-11-12 21:36:28 +01:00
use PresentableTrait;
/**
* @var array
*/
protected $fillable = [
'client_id',
'description',
'time_log',
'is_running',
];
2016-08-09 08:48:41 +02:00
/**
* @return mixed
*/
public function getEntityType()
{
return ENTITY_TASK;
}
/**
* @var string
*/
2015-11-12 21:36:28 +01:00
protected $presenter = 'App\Ninja\Presenters\TaskPresenter';
2015-05-27 18:52:10 +02:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-05-27 18:52:10 +02:00
public function account()
{
return $this->belongsTo('App\Models\Account');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-06-14 16:49:13 +02:00
public function invoice()
{
return $this->belongsTo('App\Models\Invoice');
}
/**
* @return mixed
*/
2015-11-12 21:36:28 +01:00
public function user()
{
return $this->belongsTo('App\Models\User')->withTrashed();
2015-11-12 21:36:28 +01:00
}
/**
* @return mixed
*/
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
/**
* @param $task
* @return string
*/
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 '';
}
}
/**
* @return string
*/
2015-07-12 21:43:45 +02:00
public function getStartTime()
{
return self::calcStartTime($this);
}
2016-10-19 09:17:23 +02:00
public function getLastStartTime()
{
$parts = json_decode($this->time_log) ?: [];
if (count($parts)) {
$index = count($parts) - 1;
return $parts[$index][0];
} else {
return '';
}
}
/**
* @param $task
* @return int
*/
2015-07-12 21:43:45 +02:00
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;
}
/**
* @return int
*/
2015-07-12 21:43:45 +02:00
public function getDuration()
{
return self::calcDuration($this);
}
/**
* @return int
*/
2015-07-12 21:43:45 +02:00
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;
}
}
/**
* @return bool
*/
2015-07-12 21:43:45 +02:00
public function hasPreviousDuration()
{
$parts = json_decode($this->time_log) ?: [];
return count($parts) && (count($parts[0]) && $parts[0][1]);
}
/**
* @return float
*/
2015-07-12 21:43:45 +02:00
public function getHours()
{
return round($this->getDuration() / (60 * 60), 2);
}
/**
* Gets the route to the tasks edit action
*
* @return string
*/
public function getRoute()
{
return "/tasks/{$this->public_id}/edit";
}
2016-08-31 21:10:41 +02:00
public function getName()
{
return '#' . $this->public_id;
}
2016-09-11 16:30:23 +02:00
public function getDisplayName()
{
if ($this->description) {
return mb_strimwidth($this->description, 0, 16, "...");
}
return '#' . $this->public_id;
}
2016-09-18 19:45:41 +02:00
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->format('U'));
return $query;
}
}
2016-07-10 12:20:22 +02:00
2016-08-31 21:10:41 +02:00
2016-07-10 12:20:22 +02:00
Task::created(function ($task) {
event(new TaskWasCreated($task));
});
Task::updated(function ($task) {
event(new TaskWasUpdated($task));
});