2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
2015-05-27 18:52:10 +02:00
|
|
|
|
2016-07-10 12:20:22 +02:00
|
|
|
use App\Events\TaskWasCreated;
|
|
|
|
use App\Events\TaskWasUpdated;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
use Laracasts\Presenter\PresentableTrait;
|
|
|
|
use Utils;
|
2015-05-27 18:52:10 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class Task.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2015-05-27 18:52:10 +02:00
|
|
|
class Task extends EntityModel
|
|
|
|
{
|
|
|
|
use SoftDeletes;
|
2015-11-12 21:36:28 +01:00
|
|
|
use PresentableTrait;
|
|
|
|
|
2016-10-21 08:54:28 +02:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-12 21:36:28 +01:00
|
|
|
protected $presenter = 'App\Ninja\Presenters\TaskPresenter';
|
2015-05-27 18:52:10 +02:00
|
|
|
|
2016-07-03 18:11:58 +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');
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2015-06-14 16:49:13 +02:00
|
|
|
public function invoice()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Invoice');
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-11-12 21:36:28 +01:00
|
|
|
public function user()
|
|
|
|
{
|
2016-06-08 07:11:05 +02:00
|
|
|
return $this->belongsTo('App\Models\User')->withTrashed();
|
2015-11-12 21:36:28 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02: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
|
|
|
|
2016-11-29 18:47:26 +01:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function project()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Project')->withTrashed();
|
|
|
|
}
|
|
|
|
|
2017-12-18 12:05:34 +01:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function task_status()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\TaskStatus')->withTrashed();
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $task
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @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 '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @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()
|
|
|
|
{
|
2017-01-30 17:05:31 +01:00
|
|
|
$parts = json_decode($this->time_log) ?: [];
|
2016-10-19 09:17:23 +02:00
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
if (count($parts)) {
|
|
|
|
$index = count($parts) - 1;
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
return $parts[$index][0];
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
2016-10-19 09:17:23 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $task
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @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) {
|
2017-01-30 20:40:43 +01:00
|
|
|
if (count($part) == 1 || ! $part[1]) {
|
2015-07-12 21:43:45 +02:00
|
|
|
$duration += time() - $part[0];
|
|
|
|
} else {
|
|
|
|
$duration += $part[1] - $part[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $duration;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
2015-07-12 21:43:45 +02:00
|
|
|
public function getDuration()
|
|
|
|
{
|
|
|
|
return self::calcDuration($this);
|
|
|
|
}
|
|
|
|
|
2017-10-17 12:57:20 +02:00
|
|
|
/**
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function getRate()
|
|
|
|
{
|
|
|
|
$value = 0;
|
|
|
|
|
|
|
|
if ($this->project && floatval($this->project->task_rate)) {
|
|
|
|
$value = $this->project->task_rate;
|
|
|
|
} elseif ($this->client && floatval($this->client->task_rate)) {
|
|
|
|
$value = $this->client->task_rate;
|
|
|
|
} else {
|
|
|
|
$value = $this->account->task_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Utils::roundSignificant($value);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
2015-07-12 21:43:45 +02:00
|
|
|
public function getCurrentDuration()
|
|
|
|
{
|
|
|
|
$parts = json_decode($this->time_log) ?: [];
|
2017-01-30 20:40:43 +01:00
|
|
|
$part = $parts[count($parts) - 1];
|
2015-07-12 21:43:45 +02:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
if (count($part) == 1 || ! $part[1]) {
|
2015-07-12 21:43:45 +02:00
|
|
|
return time() - $part[0];
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-07-12 21:43:45 +02:00
|
|
|
public function hasPreviousDuration()
|
|
|
|
{
|
|
|
|
$parts = json_decode($this->time_log) ?: [];
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2015-07-12 21:43:45 +02:00
|
|
|
return count($parts) && (count($parts[0]) && $parts[0][1]);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return float
|
|
|
|
*/
|
2015-07-12 21:43:45 +02:00
|
|
|
public function getHours()
|
|
|
|
{
|
|
|
|
return round($this->getDuration() / (60 * 60), 2);
|
|
|
|
}
|
2016-07-05 18:30:29 +02:00
|
|
|
|
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Gets the route to the tasks edit action.
|
2016-07-05 18:30:29 +02:00
|
|
|
*
|
|
|
|
* @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) {
|
2017-03-08 07:46:24 +01:00
|
|
|
return Utils::truncateString($this->description, 16);
|
2016-09-11 16:30:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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'));
|
2017-03-08 10:41:24 +01:00
|
|
|
$query->whereRaw('cast(substring(time_log, 3, 10) as unsigned) <= ' . $endDate->modify('+1 day')->format('U'));
|
2016-09-18 19:45:41 +02:00
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
2016-11-18 14:31:43 +01:00
|
|
|
|
|
|
|
public static function getStatuses($entityType = false)
|
|
|
|
{
|
2016-11-20 15:08:36 +01:00
|
|
|
$statuses = [];
|
2017-12-19 20:08:27 +01:00
|
|
|
|
|
|
|
$taskStatues = TaskStatus::scope()->orderBy('sort_order')->get();
|
|
|
|
|
|
|
|
foreach ($taskStatues as $status) {
|
2017-12-20 10:16:40 +01:00
|
|
|
$statuses[$status->public_id] = $status->name;
|
2017-12-19 20:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (! $taskStatues->count()) {
|
|
|
|
$statuses[TASK_STATUS_LOGGED] = trans('texts.logged');
|
|
|
|
}
|
|
|
|
|
2016-11-18 14:31:43 +01:00
|
|
|
$statuses[TASK_STATUS_RUNNING] = trans('texts.running');
|
|
|
|
$statuses[TASK_STATUS_INVOICED] = trans('texts.invoiced');
|
|
|
|
$statuses[TASK_STATUS_PAID] = trans('texts.paid');
|
|
|
|
|
|
|
|
return $statuses;
|
|
|
|
}
|
|
|
|
|
2017-12-19 19:36:25 +01:00
|
|
|
public static function calcStatusLabel($isRunning, $balance, $invoiceNumber, $taskStatus)
|
2016-12-26 19:09:49 +01:00
|
|
|
{
|
|
|
|
if ($invoiceNumber) {
|
|
|
|
if (floatval($balance) > 0) {
|
2017-12-19 19:36:25 +01:00
|
|
|
$label = trans('texts.invoiced');
|
2016-12-26 19:09:49 +01:00
|
|
|
} else {
|
2017-12-19 19:36:25 +01:00
|
|
|
$label = trans('texts.paid');
|
2016-12-26 19:09:49 +01:00
|
|
|
}
|
2017-12-19 19:36:25 +01:00
|
|
|
} elseif ($taskStatus) {
|
|
|
|
$label = $taskStatus;
|
2016-12-26 19:09:49 +01:00
|
|
|
} else {
|
2017-12-19 19:36:25 +01:00
|
|
|
$label = trans('texts.logged');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($isRunning) {
|
|
|
|
$label .= ' | ' . trans('texts.running');
|
2016-12-26 19:09:49 +01:00
|
|
|
}
|
|
|
|
|
2017-12-19 19:36:25 +01:00
|
|
|
return $label;
|
2016-12-26 19:09:49 +01:00
|
|
|
}
|
2016-07-10 12:20:22 +02:00
|
|
|
|
2016-12-26 19:09:49 +01:00
|
|
|
public static function calcStatusClass($isRunning, $balance, $invoiceNumber)
|
|
|
|
{
|
|
|
|
if ($invoiceNumber) {
|
|
|
|
if (floatval($balance)) {
|
|
|
|
return 'default';
|
|
|
|
} else {
|
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
} elseif ($isRunning) {
|
|
|
|
return 'primary';
|
|
|
|
} else {
|
2017-12-19 19:36:25 +01:00
|
|
|
return 'info';
|
2016-12-26 19:09:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function statusClass()
|
|
|
|
{
|
|
|
|
if ($this->invoice) {
|
|
|
|
$balance = $this->invoice->balance;
|
|
|
|
$invoiceNumber = $this->invoice->invoice_number;
|
|
|
|
} else {
|
|
|
|
$balance = 0;
|
|
|
|
$invoiceNumber = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return static::calcStatusClass($this->is_running, $balance, $invoiceNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function statusLabel()
|
|
|
|
{
|
|
|
|
if ($this->invoice) {
|
|
|
|
$balance = $this->invoice->balance;
|
|
|
|
$invoiceNumber = $this->invoice->invoice_number;
|
|
|
|
} else {
|
|
|
|
$balance = 0;
|
|
|
|
$invoiceNumber = false;
|
|
|
|
}
|
|
|
|
|
2017-12-19 19:36:25 +01:00
|
|
|
$taskStatus = $this->task_status ? $this->task_status->name : false;
|
|
|
|
|
|
|
|
return static::calcStatusLabel($this->is_running, $balance, $invoiceNumber, $taskStatus);
|
2016-12-26 19:09:49 +01: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));
|
|
|
|
});
|