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

103 lines
2.9 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) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @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.
*
* @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
{
$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
2020-10-29 10:56:37 +01:00
if (isset($data['status_sort_order'])) {
$task->status_sort_order = $data['status_sort_order'];
}
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);
2020-10-29 11:44:05 +01:00
$task->start_time = $task->start_time ?: $task->calcStartTime();
2020-10-29 03:24:12 +01:00
$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)
);
}
}