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

186 lines
5.6 KiB
PHP
Raw Normal View History

2020-10-12 22:42:02 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-10-12 22:42:02 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-10-12 22:42:02 +02:00
*/
namespace App\Repositories;
use App\Factory\TaskFactory;
use App\Models\Task;
use App\Utils\Traits\GeneratesCounter;
/**
* TaskRepository.
*/
class TaskRepository extends BaseRepository
{
use GeneratesCounter;
public $new_task = true;
2020-10-12 22:42:02 +02:00
/**
* Saves the task and its contacts.
*
* @param array $data The data
* @param \App\Models\Task $task The task
2020-10-12 22:42:02 +02:00
*
2020-10-28 11:10:49 +01:00
* @return task|null task Object
2020-10-12 22:42:02 +02:00
*/
public function save(array $data, Task $task) : ?Task
{
if($task->id)
$this->new_task = false;
2021-02-24 03:12:23 +01:00
2020-10-12 22:42:02 +02:00
$task->fill($data);
2020-10-29 10:56:37 +01:00
$task->save();
if($this->new_task && !$task->status_id)
$this->setDefaultStatus($task);
2020-11-03 13:35:05 +01:00
$task->number = empty($task->number) || !array_key_exists('number', $data) ? $this->getNextTaskNumber($task) : $data['number'];
2020-10-29 10:56:37 +01:00
if (isset($data['description'])) {
$task->description = trim($data['description']);
}
2020-10-29 03:24:12 +01:00
2021-01-06 00:36:20 +01:00
//todo i can't set it - i need to calculate it.
if (isset($data['status_order'])) {
$task->status_order = $data['status_order'];
2020-10-29 10:56:37 +01:00
}
2020-10-29 03:24:12 +01:00
2021-12-02 10:26:23 +01:00
/*V4 override*/
if (! empty($data['time_details'])) {
$timeLog = [];
foreach ($data['time_details'] as $detail) {
$startTime = strtotime($detail['start_datetime']);
$endTime = false;
if (! empty($detail['end_datetime'])) {
$endTime = strtotime($detail['end_datetime']);
} else {
$duration = 0;
if (! empty($detail['duration_seconds'])) {
$duration += $detail['duration_seconds'];
}
if (! empty($detail['duration_minutes'])) {
$duration += $detail['duration_minutes'] * 60;
}
if (! empty($detail['duration_hours'])) {
$duration += $detail['duration_hours'] * 60 * 60;
}
if ($duration) {
$endTime = $startTime + $duration;
}
}
$timeLog[] = [$startTime, $endTime];
if (! $endTime) {
$data['is_running'] = true;
}
}
$data['time_log'] = json_encode($timeLog);
}
2020-10-29 10:56:37 +01:00
if (isset($data['time_log'])) {
$time_log = json_decode($data['time_log']);
} elseif ($task->time_log) {
$time_log = json_decode($task->time_log);
} else {
$time_log = [];
}
array_multisort($time_log);
if (isset($data['action'])) {
if ($data['action'] == 'start') {
$task->is_running = true;
$time_log[] = [strtotime('now'), false];
} elseif ($data['action'] == 'resume') {
$task->is_running = true;
$time_log[] = [strtotime('now'), false];
} elseif ($data['action'] == 'stop' && $task->is_running) {
$time_log[count($time_log) - 1][1] = time();
$task->is_running = false;
2020-11-25 15:19:52 +01:00
} elseif ($data['action'] == 'offline') {
2020-10-29 10:56:37 +01:00
$task->is_running = $data['is_running'] ? 1 : 0;
}
} elseif (isset($data['is_running'])) {
$task->is_running = $data['is_running'] ? 1 : 0;
}
$task->time_log = json_encode($time_log);
// $task->start_time = $task->start_time ?: $task->calcStartTime();
// $task->duration = $task->calcDuration();
2020-10-29 10:56:37 +01:00
2020-10-12 22:42:02 +02:00
$task->save();
if (array_key_exists('documents', $data)) {
$this->saveDocuments($data['documents'], $task);
}
return $task;
}
/**
* Store tasks in bulk.
*
* @param array $task
* @return task|null
*/
public function create($task): ?Task
{
return $this->save(
$task,
TaskFactory::create(auth()->user()->company()->id, auth()->user()->id)
);
2021-02-24 03:12:23 +01:00
}
private function setDefaultStatus(Task $task)
{
$first_status = $task->company->task_statuses()
->whereNull('deleted_at')
->orderBy('id','asc')
->first();
if($first_status)
return $first_status->id;
return null;
}
2021-02-24 03:12:23 +01:00
/**
* Sorts the task status order IF the old status has changed between requests
*
* @param stdCLass $old_task The old task object
* @param Task $new_task The new Task model
* @return void
*/
public function sortStatuses($old_task, $new_task)
{
if(!$new_task->project()->exists())
return;
$index = $new_task->status_order;
$tasks = $new_task->project->tasks->reject(function ($task)use($new_task){
return $task->id == $new_task->id;
});
$sorted_tasks = $tasks->filter(function($task, $key)use($index){
return $key < $index;
})->push($new_task)->merge($tasks->filter(function($task, $key)use($index){
return $key >= $index;
}))->each(function ($item,$key){
$item->status_order = $key;
$item->save();
});
2020-10-12 22:42:02 +02:00
}
}