2020-10-12 22:42:02 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. 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;
|
2022-09-13 11:32:53 +02:00
|
|
|
use Illuminate\Database\QueryException;
|
2020-10-12 22:42:02 +02:00
|
|
|
|
|
|
|
/**
|
2023-09-27 10:10:22 +02:00
|
|
|
* App\Repositories\TaskRepository.
|
2020-10-12 22:42:02 +02:00
|
|
|
*/
|
|
|
|
class TaskRepository extends BaseRepository
|
|
|
|
{
|
|
|
|
use GeneratesCounter;
|
|
|
|
|
2021-04-27 01:34:35 +02:00
|
|
|
public $new_task = true;
|
2022-09-21 04:43:24 +02:00
|
|
|
|
|
|
|
private $completed = true;
|
2020-10-12 22:42:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2023-04-11 04:23:09 +02:00
|
|
|
public function save(array $data, Task $task): ?Task
|
2020-10-12 22:42:02 +02:00
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($task->id) {
|
2021-04-27 01:34:35 +02:00
|
|
|
$this->new_task = false;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2023-09-07 07:14:03 +02:00
|
|
|
|
2020-10-12 22:42:02 +02:00
|
|
|
$task->fill($data);
|
2023-02-01 04:12:44 +01:00
|
|
|
$task->saveQuietly();
|
2020-10-29 10:56:37 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->new_task && ! $task->status_id) {
|
2023-09-07 06:20:32 +02:00
|
|
|
$task->status_id = $this->setDefaultStatus($task);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-04-27 01:34:35 +02:00
|
|
|
|
2023-09-07 06:51:42 +02:00
|
|
|
if($this->new_task && (!$task->rate || $task->rate <= 0)) {
|
|
|
|
$task->rate = $task->getRate();
|
|
|
|
}
|
|
|
|
|
2022-09-13 11:32:53 +02:00
|
|
|
$task->number = empty($task->number) || ! array_key_exists('number', $data) ? $this->trySaving($task) : $data['number'];
|
2020-10-29 10:56:37 +01:00
|
|
|
|
|
|
|
if (isset($data['description'])) {
|
|
|
|
$task->description = trim($data['description']);
|
|
|
|
}
|
2023-09-07 07:14:03 +02: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 = [];
|
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-04-11 09:27:37 +02:00
|
|
|
$key_values = array_column($time_log, 0);
|
|
|
|
array_multisort($key_values, SORT_ASC, $time_log);
|
2020-10-29 10:56:37 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-21 00:20:51 +02:00
|
|
|
$task->calculated_start_date = $this->harvestStartDate($time_log, $task);
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2020-10-29 10:56:37 +01:00
|
|
|
$task->time_log = json_encode($time_log);
|
|
|
|
|
2023-07-19 00:01:47 +02:00
|
|
|
|
|
|
|
|
2023-02-01 04:12:44 +01:00
|
|
|
$task->saveQuietly();
|
2020-10-12 22:42:02 +02:00
|
|
|
|
|
|
|
if (array_key_exists('documents', $data)) {
|
|
|
|
$this->saveDocuments($data['documents'], $task);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $task;
|
|
|
|
}
|
|
|
|
|
2023-08-21 00:20:51 +02:00
|
|
|
private function harvestStartDate($time_log, $task)
|
2023-07-19 00:01:47 +02:00
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if(isset($time_log[0][0])) {
|
2023-08-21 00:20:51 +02:00
|
|
|
return \Carbon\Carbon::createFromTimestamp($time_log[0][0])->addSeconds($task->company->utc_offset());
|
2023-07-19 00:01:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-12 22:42:02 +02:00
|
|
|
/**
|
|
|
|
* Store tasks in bulk.
|
|
|
|
*
|
|
|
|
* @param array $task
|
2023-08-03 00:24:30 +02:00
|
|
|
* @return Task|null
|
2020-10-12 22:42:02 +02:00
|
|
|
*/
|
|
|
|
public function create($task): ?Task
|
|
|
|
{
|
2023-08-03 00:24:30 +02:00
|
|
|
/** @var \App\Models\User $user **/
|
|
|
|
$user = auth()->user();
|
|
|
|
|
2020-10-12 22:42:02 +02:00
|
|
|
return $this->save(
|
|
|
|
$task,
|
2023-08-03 00:24:30 +02:00
|
|
|
TaskFactory::create($user->company()->id, $user->id)
|
2020-10-12 22:42:02 +02:00
|
|
|
);
|
2021-02-24 03:12:23 +01:00
|
|
|
}
|
|
|
|
|
2021-04-27 01:34:35 +02:00
|
|
|
private function setDefaultStatus(Task $task)
|
|
|
|
{
|
|
|
|
$first_status = $task->company->task_statuses()
|
|
|
|
->whereNull('deleted_at')
|
2022-06-21 11:57:17 +02:00
|
|
|
->orderBy('id', 'asc')
|
2021-04-27 01:34:35 +02:00
|
|
|
->first();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($first_status) {
|
2021-04-27 01:34:35 +02:00
|
|
|
return $first_status->id;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-04-27 01:34:35 +02:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-02-24 03:12:23 +01:00
|
|
|
/**
|
|
|
|
* Sorts the task status order IF the old status has changed between requests
|
2022-06-21 11:57:17 +02:00
|
|
|
*
|
2023-08-03 00:24:30 +02:00
|
|
|
* @param \stdCLass $old_task The old task object
|
2021-02-24 03:12:23 +01:00
|
|
|
* @param Task $new_task The new Task model
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function sortStatuses($old_task, $new_task)
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $new_task->project()->exists()) {
|
2021-02-24 03:12:23 +01:00
|
|
|
return;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-02-24 03:12:23 +01:00
|
|
|
|
|
|
|
$index = $new_task->status_order;
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$tasks = $new_task->project->tasks->reject(function ($task) use ($new_task) {
|
2021-02-24 03:12:23 +01:00
|
|
|
return $task->id == $new_task->id;
|
|
|
|
});
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$sorted_tasks = $tasks->filter(function ($task, $key) use ($index) {
|
2021-02-24 03:12:23 +01:00
|
|
|
return $key < $index;
|
2022-06-21 11:57:17 +02:00
|
|
|
})->push($new_task)->merge($tasks->filter(function ($task, $key) use ($index) {
|
2021-02-24 03:12:23 +01:00
|
|
|
return $key >= $index;
|
2022-06-21 11:57:17 +02:00
|
|
|
}))->each(function ($item, $key) {
|
2021-02-24 03:12:23 +01:00
|
|
|
$item->status_order = $key;
|
2023-02-01 04:12:44 +01:00
|
|
|
$item->saveQuietly();
|
2021-02-24 03:12:23 +01:00
|
|
|
});
|
2020-10-12 22:42:02 +02:00
|
|
|
}
|
2022-03-10 07:17:40 +01:00
|
|
|
|
|
|
|
public function start(Task $task)
|
|
|
|
{
|
2022-03-17 03:41:46 +01:00
|
|
|
//do no allow an task to be restarted if it has been invoiced
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($task->invoice_id) {
|
2022-03-17 03:41:46 +01:00
|
|
|
return;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-03-17 03:41:46 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (strlen($task->time_log) < 5) {
|
2022-03-11 02:39:17 +01:00
|
|
|
$log = [];
|
|
|
|
|
2023-07-19 00:01:47 +02:00
|
|
|
$start_time = time();
|
|
|
|
|
|
|
|
$log = array_merge($log, [[$start_time, 0]]);
|
2022-03-11 02:39:17 +01:00
|
|
|
$task->time_log = json_encode($log);
|
2023-08-21 00:20:51 +02:00
|
|
|
$task->calculated_start_date = \Carbon\Carbon::createFromTimestamp($start_time)->addSeconds($task->company->utc_offset());
|
2023-07-19 00:01:47 +02:00
|
|
|
|
2023-02-01 04:12:44 +01:00
|
|
|
$task->saveQuietly();
|
2022-03-11 02:39:17 +01:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$log = json_decode($task->time_log, true);
|
2022-03-10 07:26:03 +01:00
|
|
|
|
|
|
|
$last = end($log);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2023-04-11 04:23:09 +02:00
|
|
|
if (is_array($last) && $last[1] !== 0) { // this line is a disaster
|
2022-03-10 07:26:03 +01:00
|
|
|
$new = [time(), 0];
|
2023-04-11 04:23:09 +02:00
|
|
|
|
2022-03-11 02:39:17 +01:00
|
|
|
$log = array_merge($log, [$new]);
|
|
|
|
$task->time_log = json_encode($log);
|
2023-04-11 04:23:09 +02:00
|
|
|
|
2023-02-01 04:12:44 +01:00
|
|
|
$task->saveQuietly();
|
2022-03-10 07:26:03 +01:00
|
|
|
}
|
2022-03-10 07:17:40 +01:00
|
|
|
|
2022-03-30 04:54:40 +02:00
|
|
|
return $task;
|
2022-03-10 07:17:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function stop(Task $task)
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$log = json_decode($task->time_log, true);
|
2022-03-10 07:26:03 +01:00
|
|
|
|
|
|
|
$last = end($log);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (is_array($last) && $last[1] === 0) {
|
2022-03-10 07:26:03 +01:00
|
|
|
$last[1] = time();
|
|
|
|
|
|
|
|
array_pop($log);
|
2023-04-11 04:23:09 +02:00
|
|
|
$log = array_merge($log, [$last]);//check at this point, it may be prepending here.
|
2022-03-10 07:26:03 +01:00
|
|
|
|
2022-03-11 02:39:17 +01:00
|
|
|
$task->time_log = json_encode($log);
|
2023-02-01 04:12:44 +01:00
|
|
|
$task->saveQuietly();
|
2022-03-10 07:26:03 +01:00
|
|
|
}
|
2022-03-10 07:17:40 +01:00
|
|
|
|
2022-03-30 04:54:40 +02:00
|
|
|
return $task;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function triggeredActions($request, $task)
|
|
|
|
{
|
|
|
|
if ($request->has('start') && $request->input('start') == 'true') {
|
|
|
|
$task = $this->start($task);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->has('stop') && $request->input('stop') == 'true') {
|
|
|
|
$task = $this->stop($task);
|
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-03-30 04:54:40 +02:00
|
|
|
return $task;
|
2022-03-10 07:17:40 +01:00
|
|
|
}
|
2022-09-13 11:32:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
private function trySaving(Task $task)
|
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
$x = 1;
|
2022-09-13 11:32:53 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
do {
|
|
|
|
try {
|
2022-09-13 11:32:53 +02:00
|
|
|
$task->number = $this->getNextTaskNumber($task);
|
|
|
|
$task->saveQuietly();
|
|
|
|
$this->completed = false;
|
2023-02-16 02:36:09 +01:00
|
|
|
} catch(QueryException $e) {
|
2022-09-13 11:32:53 +02:00
|
|
|
$x++;
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
if ($x > 50) {
|
2022-09-13 11:32:53 +02:00
|
|
|
$this->completed = false;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-09-13 11:32:53 +02:00
|
|
|
}
|
2023-02-16 02:36:09 +01:00
|
|
|
} while ($this->completed);
|
2022-09-13 11:32:53 +02:00
|
|
|
|
|
|
|
return $task->number;
|
|
|
|
}
|
2020-10-12 22:42:02 +02:00
|
|
|
}
|