2020-10-12 22:42:02 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-10-12 22:42:02 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
|
|
|
use App\Factory\TaskFactory;
|
|
|
|
use App\Models\Task;
|
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TaskRepository.
|
|
|
|
*/
|
|
|
|
class TaskRepository extends BaseRepository
|
|
|
|
{
|
|
|
|
use GeneratesCounter;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the task and its contacts.
|
|
|
|
*
|
2020-11-01 06:09:09 +01:00
|
|
|
* @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
|
|
|
|
{
|
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();
|
|
|
|
|
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
|
|
|
|
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);
|
2021-01-11 11:45:38 +01:00
|
|
|
// $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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
}
|