2018-10-15 14:40:34 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
2018-10-15 14:40:34 +02:00
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2018-11-20 05:36:56 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2024-03-20 10:05:22 +01:00
|
|
|
use Carbon\CarbonInterval;
|
2020-01-21 10:22:10 +01:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2020-10-29 03:24:12 +01:00
|
|
|
use Illuminate\Support\Carbon;
|
2018-10-15 14:40:34 +02:00
|
|
|
|
2023-03-08 08:33:42 +01:00
|
|
|
/**
|
|
|
|
* App\Models\Task
|
|
|
|
*
|
|
|
|
* @property int $id
|
2023-11-27 11:26:56 +01:00
|
|
|
* @property string|null $hash
|
|
|
|
* @property object|null $meta
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property int $user_id
|
|
|
|
* @property int|null $assigned_user_id
|
|
|
|
* @property int $company_id
|
|
|
|
* @property int|null $client_id
|
|
|
|
* @property int|null $invoice_id
|
|
|
|
* @property int|null $project_id
|
|
|
|
* @property int|null $status_id
|
|
|
|
* @property int|null $status_sort_order
|
|
|
|
* @property int|null $created_at
|
|
|
|
* @property int|null $updated_at
|
|
|
|
* @property int|null $deleted_at
|
|
|
|
* @property string|null $custom_value1
|
|
|
|
* @property string|null $custom_value2
|
|
|
|
* @property string|null $custom_value3
|
|
|
|
* @property string|null $custom_value4
|
|
|
|
* @property int|null $duration
|
|
|
|
* @property string|null $description
|
2023-07-26 11:00:47 +02:00
|
|
|
* @property bool $is_deleted
|
|
|
|
* @property bool $is_running
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property string|null $time_log
|
|
|
|
* @property string|null $number
|
2024-06-16 06:35:56 +02:00
|
|
|
* @property float $rate
|
2024-06-15 09:34:08 +02:00
|
|
|
* @property bool $invoice_documents
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property int $is_date_based
|
|
|
|
* @property int|null $status_order
|
|
|
|
* @property-read \App\Models\User|null $assigned_user
|
|
|
|
* @property-read \App\Models\Client|null $client
|
2023-04-26 14:17:40 +02:00
|
|
|
* @property-read \App\Models\Company|null $company
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
|
|
|
|
* @property-read int|null $documents_count
|
|
|
|
* @property-read mixed $hashed_id
|
|
|
|
* @property-read \App\Models\Invoice|null $invoice
|
|
|
|
* @property-read \App\Models\Project|null $project
|
|
|
|
* @property-read \App\Models\TaskStatus|null $status
|
|
|
|
* @property-read \App\Models\User $user
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel exclude($columns)
|
|
|
|
* @method static \Database\Factories\TaskFactory factory($count = null, $state = [])
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task filter(\App\Filters\QueryFilters $filters)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task newModelQuery()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task newQuery()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task onlyTrashed()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task query()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel scope()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereAssignedUserId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereClientId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereCompanyId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereCreatedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereCustomValue1($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereCustomValue2($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereCustomValue3($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereCustomValue4($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereDeletedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereDescription($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereDuration($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereInvoiceDocuments($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereInvoiceId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereIsDateBased($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereIsDeleted($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereIsRunning($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereNumber($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereProjectId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereRate($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereStatusId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereStatusOrder($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereStatusSortOrder($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereTimeLog($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereUpdatedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task whereUserId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task withTrashed()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Task withoutTrashed()
|
2023-03-16 05:20:38 +01:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
|
2023-03-08 08:33:42 +01:00
|
|
|
* @mixin \Eloquent
|
|
|
|
*/
|
2018-11-02 11:54:46 +01:00
|
|
|
class Task extends BaseModel
|
2018-10-15 14:40:34 +02:00
|
|
|
{
|
2018-11-20 05:36:56 +01:00
|
|
|
use MakesHash;
|
2020-01-21 10:22:10 +01:00
|
|
|
use SoftDeletes;
|
2020-10-12 22:42:02 +02:00
|
|
|
use Filterable;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-06-26 06:04:10 +02:00
|
|
|
protected $fillable = [
|
2019-12-30 22:59:12 +01:00
|
|
|
'client_id',
|
2019-06-26 06:04:10 +02:00
|
|
|
'invoice_id',
|
2020-10-12 22:42:02 +02:00
|
|
|
'project_id',
|
2020-10-28 00:02:32 +01:00
|
|
|
'assigned_user_id',
|
2019-06-26 06:04:10 +02:00
|
|
|
'custom_value1',
|
|
|
|
'custom_value2',
|
2020-10-12 22:42:02 +02:00
|
|
|
'custom_value3',
|
|
|
|
'custom_value4',
|
2019-06-26 06:04:10 +02:00
|
|
|
'description',
|
|
|
|
'is_running',
|
|
|
|
'time_log',
|
2020-10-26 20:10:04 +01:00
|
|
|
'status_id',
|
2021-01-06 00:36:20 +01:00
|
|
|
'status_sort_order', //deprecated
|
2020-10-27 03:27:38 +01:00
|
|
|
'invoice_documents',
|
2020-10-29 00:11:52 +01:00
|
|
|
'rate',
|
2020-10-29 10:40:13 +01:00
|
|
|
'number',
|
2020-12-14 22:52:14 +01:00
|
|
|
'is_date_based',
|
2021-01-06 00:36:20 +01:00
|
|
|
'status_order',
|
2023-11-27 11:26:56 +01:00
|
|
|
'hash',
|
|
|
|
'meta',
|
|
|
|
];
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-11-27 11:26:56 +01:00
|
|
|
protected $casts = [
|
|
|
|
'meta' => 'object',
|
|
|
|
'updated_at' => 'timestamp',
|
|
|
|
'created_at' => 'timestamp',
|
|
|
|
'deleted_at' => 'timestamp',
|
2019-12-30 22:59:12 +01:00
|
|
|
];
|
2018-11-20 05:36:56 +01:00
|
|
|
|
2024-02-18 06:19:09 +01:00
|
|
|
protected $with = [
|
|
|
|
// 'project',
|
|
|
|
];
|
|
|
|
|
2020-07-23 05:55:11 +02:00
|
|
|
protected $touches = [];
|
2020-07-16 13:01:39 +02:00
|
|
|
|
2020-05-06 13:49:42 +02:00
|
|
|
public function getEntityType()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
return self::class;
|
2020-05-06 13:49:42 +02:00
|
|
|
}
|
|
|
|
|
2023-04-26 14:17:40 +02:00
|
|
|
/**
|
|
|
|
* Get all of the users that belong to the team.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2020-07-16 13:01:39 +02:00
|
|
|
{
|
|
|
|
return $this->belongsTo(Company::class);
|
|
|
|
}
|
|
|
|
|
2019-04-28 07:31:32 +02:00
|
|
|
public function documents()
|
|
|
|
{
|
|
|
|
return $this->morphMany(Document::class, 'documentable');
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:16:38 +01:00
|
|
|
public function assigned_user()
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed();
|
2019-11-05 11:16:38 +01:00
|
|
|
}
|
2019-11-25 10:38:55 +01:00
|
|
|
|
|
|
|
public function user()
|
|
|
|
{
|
2021-09-08 01:29:20 +02:00
|
|
|
return $this->belongsTo(User::class)->withTrashed();
|
2019-11-25 10:38:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function client()
|
|
|
|
{
|
2022-07-19 12:07:23 +02:00
|
|
|
return $this->belongsTo(Client::class)->withTrashed();
|
2019-11-25 10:38:55 +01:00
|
|
|
}
|
2020-10-12 22:42:02 +02:00
|
|
|
|
2021-04-27 01:34:35 +02:00
|
|
|
public function status()
|
|
|
|
{
|
2022-07-19 12:07:23 +02:00
|
|
|
return $this->belongsTo(TaskStatus::class)->withTrashed();
|
2021-04-27 01:34:35 +02:00
|
|
|
}
|
|
|
|
|
2023-11-15 10:47:56 +01:00
|
|
|
public function stringStatus()
|
|
|
|
{
|
2023-11-26 08:41:42 +01:00
|
|
|
if($this->invoice_id) {
|
2023-11-15 10:47:56 +01:00
|
|
|
return '<h5><span class="badge badge-success">'.ctrans('texts.invoiced').'</span></h5>';
|
2023-11-26 08:41:42 +01:00
|
|
|
}
|
2023-11-15 10:47:56 +01:00
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
if($this->status) {
|
2023-11-15 10:47:56 +01:00
|
|
|
return '<h5><span class="badge badge-primary">' . $this->status?->name ?? '';
|
2023-11-26 08:41:42 +01:00
|
|
|
}
|
2023-11-15 10:47:56 +01:00
|
|
|
|
|
|
|
return '';
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-11-15 10:47:56 +01:00
|
|
|
}
|
|
|
|
|
2020-10-12 22:42:02 +02:00
|
|
|
public function invoice()
|
|
|
|
{
|
2022-07-19 12:07:23 +02:00
|
|
|
return $this->belongsTo(Invoice::class)->withTrashed();
|
2020-10-12 22:42:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function project()
|
|
|
|
{
|
2022-07-19 12:07:23 +02:00
|
|
|
return $this->belongsTo(Project::class)->withTrashed();
|
2020-10-12 22:42:02 +02:00
|
|
|
}
|
2020-10-29 03:24:12 +01:00
|
|
|
|
2020-10-29 04:34:29 +01:00
|
|
|
public function calcStartTime()
|
2020-10-29 03:24:12 +01:00
|
|
|
{
|
|
|
|
$parts = json_decode($this->time_log) ?: [];
|
|
|
|
|
|
|
|
if (count($parts)) {
|
2020-10-29 10:56:37 +01:00
|
|
|
return Carbon::createFromTimeStamp($parts[0][0])->timestamp;
|
2020-10-29 03:24:12 +01:00
|
|
|
} else {
|
2020-10-29 10:56:37 +01:00
|
|
|
return null;
|
2020-10-29 03:24:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 04:34:29 +01:00
|
|
|
public function getLastStartTime()
|
2020-10-29 03:24:12 +01:00
|
|
|
{
|
|
|
|
$parts = json_decode($this->time_log) ?: [];
|
|
|
|
|
|
|
|
if (count($parts)) {
|
|
|
|
$index = count($parts) - 1;
|
|
|
|
|
|
|
|
return $parts[$index][0];
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 04:34:29 +01:00
|
|
|
public function calcDuration($start_time_cutoff = 0, $end_time_cutoff = 0)
|
2020-10-29 03:24:12 +01:00
|
|
|
{
|
|
|
|
$duration = 0;
|
|
|
|
$parts = json_decode($this->time_log) ?: [];
|
|
|
|
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
$start_time = $part[0];
|
|
|
|
if (count($part) == 1 || ! $part[1]) {
|
|
|
|
$end_time = time();
|
|
|
|
} else {
|
|
|
|
$end_time = $part[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($start_time_cutoff) {
|
|
|
|
$start_time = max($start_time, $start_time_cutoff);
|
|
|
|
}
|
|
|
|
if ($end_time_cutoff) {
|
|
|
|
$end_time = min($end_time, $end_time_cutoff);
|
|
|
|
}
|
|
|
|
|
|
|
|
$duration += max($end_time - $start_time, 0);
|
|
|
|
}
|
|
|
|
|
2024-03-20 10:05:22 +01:00
|
|
|
// return CarbonInterval::seconds(round($duration))->locale($this->company->locale())->cascade()->forHumans();
|
2020-10-29 03:24:12 +01:00
|
|
|
return round($duration);
|
|
|
|
}
|
2022-04-06 02:38:01 +02:00
|
|
|
|
|
|
|
public function translate_entity()
|
|
|
|
{
|
|
|
|
return ctrans('texts.task');
|
|
|
|
}
|
2023-09-07 06:51:42 +02:00
|
|
|
|
|
|
|
public function getRate(): float
|
|
|
|
{
|
2023-10-26 04:57:44 +02:00
|
|
|
if($this->project && $this->project->task_rate > 0) {
|
2023-09-07 06:51:42 +02:00
|
|
|
return $this->project->task_rate;
|
2023-10-26 04:57:44 +02:00
|
|
|
}
|
2023-09-07 06:51:42 +02:00
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if($this->client) {
|
2023-09-07 06:51:42 +02:00
|
|
|
return $this->client->getSetting('default_task_rate');
|
2023-10-26 04:57:44 +02:00
|
|
|
}
|
2023-09-07 06:51:42 +02:00
|
|
|
|
|
|
|
return $this->company->settings->default_task_rate ?? 0;
|
|
|
|
}
|
2023-11-09 07:59:30 +01:00
|
|
|
|
|
|
|
public function processLogs()
|
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
return
|
|
|
|
collect(json_decode($this->time_log, true))->map(function ($log) {
|
2023-11-16 10:10:16 +01:00
|
|
|
|
2023-11-09 07:59:30 +01:00
|
|
|
$parent_entity = $this->client ?? $this->company;
|
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
if($log[0]) {
|
2023-11-20 03:53:39 +01:00
|
|
|
$log[0] = Carbon::createFromTimestamp($log[0])->format($parent_entity->date_format().' H:i:s');
|
2023-11-26 08:41:42 +01:00
|
|
|
}
|
2023-11-09 07:59:30 +01:00
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
if($log[1] && $log[1] != 0) {
|
2023-11-20 03:53:39 +01:00
|
|
|
$log[1] = Carbon::createFromTimestamp($log[1])->format($parent_entity->date_format().' H:i:s');
|
2023-11-26 08:41:42 +01:00
|
|
|
} else {
|
2023-11-09 07:59:30 +01:00
|
|
|
$log[1] = ctrans('texts.running');
|
2023-11-26 08:41:42 +01:00
|
|
|
}
|
2023-11-09 07:59:30 +01:00
|
|
|
|
|
|
|
return $log;
|
|
|
|
})->toArray();
|
|
|
|
}
|
2023-11-16 10:51:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
public function processLogsExpandedNotation()
|
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
return
|
|
|
|
collect(json_decode($this->time_log, true))->map(function ($log) {
|
2023-11-16 10:51:20 +01:00
|
|
|
|
|
|
|
$parent_entity = $this->client ?? $this->company;
|
|
|
|
$logged = [];
|
2024-01-14 05:05:00 +01:00
|
|
|
|
|
|
|
if($log[0] && $log[1] != 0) {
|
2023-11-16 10:51:20 +01:00
|
|
|
$duration = $log[1] - $log[0];
|
2023-11-26 08:41:42 +01:00
|
|
|
} else {
|
2023-11-16 10:51:20 +01:00
|
|
|
$duration = 0;
|
|
|
|
}
|
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
if($log[0]) {
|
2023-11-16 10:51:20 +01:00
|
|
|
$logged['start_date_raw'] = $log[0];
|
2023-11-26 08:41:42 +01:00
|
|
|
}
|
|
|
|
$logged['start_date'] = Carbon::createFromTimestamp($log[0])->setTimeZone($this->company->timezone()->name)->format($parent_entity->date_format().' H:i:s');
|
2023-11-16 10:51:20 +01:00
|
|
|
|
|
|
|
if($log[1] && $log[1] != 0) {
|
|
|
|
$logged['end_date_raw'] = $log[1];
|
2023-11-20 03:53:39 +01:00
|
|
|
$logged['end_date'] = Carbon::createFromTimestamp($log[1])->setTimeZone($this->company->timezone()->name)->format($parent_entity->date_format().' H:i:s');
|
2023-11-26 08:41:42 +01:00
|
|
|
} else {
|
2023-11-16 10:51:20 +01:00
|
|
|
$logged['end_date_raw'] = 0;
|
|
|
|
$logged['end_date'] = ctrans('texts.running');
|
|
|
|
}
|
2023-11-19 23:04:11 +01:00
|
|
|
|
2024-01-25 04:06:26 +01:00
|
|
|
$logged['description'] = $log[2] ?? '';
|
|
|
|
$logged['billable'] = $log[3] ?? false;
|
2023-11-20 03:53:39 +01:00
|
|
|
$logged['duration_raw'] = $duration;
|
|
|
|
$logged['duration'] = gmdate("H:i:s", $duration);
|
2023-11-16 10:51:20 +01:00
|
|
|
|
|
|
|
return $logged;
|
|
|
|
|
|
|
|
})->toArray();
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|