1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00
invoiceninja/app/Ninja/Repositories/TaskRepository.php

215 lines
8.4 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Ninja\Repositories;
2015-05-27 18:52:10 +02:00
use App\Models\Client;
2016-11-29 18:47:26 +01:00
use App\Models\Project;
2015-05-27 18:52:10 +02:00
use App\Models\Task;
2017-12-18 22:28:07 +01:00
use App\Models\TaskStatus;
2017-01-30 20:40:43 +01:00
use Auth;
use Session;
2017-04-30 11:49:45 +02:00
use DB;
2017-11-21 08:35:28 +01:00
use Utils;
2015-05-27 18:52:10 +02:00
class TaskRepository extends BaseRepository
2015-05-27 18:52:10 +02:00
{
public function getClassName()
{
return 'App\Models\Task';
}
2017-12-24 20:01:17 +01:00
public function find($clientPublicId = null, $projectPublicId = null, $filter = null)
2015-05-27 18:52:10 +02:00
{
2017-04-30 11:49:45 +02:00
$query = DB::table('tasks')
2015-05-27 18:52:10 +02:00
->leftJoin('clients', 'tasks.client_id', '=', 'clients.id')
->leftJoin('contacts', 'contacts.client_id', '=', 'clients.id')
->leftJoin('invoices', 'invoices.id', '=', 'tasks.invoice_id')
2016-11-29 18:47:26 +01:00
->leftJoin('projects', 'projects.id', '=', 'tasks.project_id')
2017-12-19 19:36:25 +01:00
->leftJoin('task_statuses', 'task_statuses.id', '=', 'tasks.task_status_id')
2015-05-27 18:52:10 +02:00
->where('tasks.account_id', '=', Auth::user()->account_id)
2016-11-29 18:47:26 +01:00
->where(function ($query) { // handle when client isn't set
2015-05-27 18:52:10 +02:00
$query->where('contacts.is_primary', '=', true)
->orWhere('contacts.is_primary', '=', null);
})
->where('contacts.deleted_at', '=', null)
2016-01-23 22:36:31 +01:00
->select(
'tasks.public_id',
2017-04-30 11:49:45 +02:00
DB::raw("COALESCE(NULLIF(clients.name,''), NULLIF(CONCAT(contacts.first_name, ' ', contacts.last_name),''), NULLIF(contacts.email,'')) client_name"),
2016-01-23 22:36:31 +01:00
'clients.public_id as client_public_id',
'clients.user_id as client_user_id',
2016-01-23 22:36:31 +01:00
'contacts.first_name',
'contacts.email',
'contacts.last_name',
'invoices.invoice_status_id',
'tasks.description',
'tasks.is_deleted',
'tasks.deleted_at',
'invoices.invoice_number',
'invoices.invoice_number as status',
2016-01-23 22:36:31 +01:00
'invoices.public_id as invoice_public_id',
'invoices.user_id as invoice_user_id',
2016-07-03 11:33:35 +02:00
'invoices.balance',
2016-01-23 22:36:31 +01:00
'tasks.is_running',
'tasks.time_log',
'tasks.time_log as duration',
2016-03-16 00:08:00 +01:00
'tasks.created_at',
2017-04-30 11:49:45 +02:00
DB::raw("SUBSTRING(time_log, 3, 10) date"),
2016-11-29 18:47:26 +01:00
'tasks.user_id',
'projects.name as project',
'projects.public_id as project_public_id',
2017-12-19 19:36:25 +01:00
'projects.user_id as project_user_id',
'task_statuses.name as task_status'
2016-01-23 22:36:31 +01:00
);
2015-05-27 18:52:10 +02:00
2017-12-24 20:01:17 +01:00
if ($projectPublicId) {
$query->where('projects.public_id', '=', $projectPublicId);
} elseif ($clientPublicId) {
2015-05-27 18:52:10 +02:00
$query->where('clients.public_id', '=', $clientPublicId);
2016-11-27 13:20:58 +01:00
} else {
$query->whereNull('clients.deleted_at');
2015-05-27 18:52:10 +02:00
}
2016-11-18 14:31:43 +01:00
$this->applyFilters($query, ENTITY_TASK);
2016-11-20 15:08:36 +01:00
if ($statuses = session('entity_status_filter:' . ENTITY_TASK)) {
$statuses = explode(',', $statuses);
2016-11-18 14:31:43 +01:00
$query->where(function ($query) use ($statuses) {
if (in_array(TASK_STATUS_LOGGED, $statuses)) {
$query->orWhere('tasks.invoice_id', '=', 0)
->orWhereNull('tasks.invoice_id');
}
if (in_array(TASK_STATUS_RUNNING, $statuses)) {
$query->orWhere('tasks.is_running', '=', 1);
}
if (in_array(TASK_STATUS_INVOICED, $statuses)) {
$query->orWhere('tasks.invoice_id', '>', 0);
2017-01-30 17:05:31 +01:00
if (! in_array(TASK_STATUS_PAID, $statuses)) {
2016-11-18 14:31:43 +01:00
$query->where('invoices.balance', '>', 0);
}
}
if (in_array(TASK_STATUS_PAID, $statuses)) {
$query->orWhere('invoices.balance', '=', 0);
}
2017-12-19 20:08:27 +01:00
$query->orWhere(function ($query) use ($statuses) {
2017-12-20 10:16:40 +01:00
$query->whereIn('task_statuses.public_id', $statuses)
2017-12-19 20:08:27 +01:00
->whereNull('tasks.invoice_id');
});
2016-11-18 14:31:43 +01:00
});
2015-05-27 18:52:10 +02:00
}
if ($filter) {
$query->where(function ($query) use ($filter) {
$query->where('clients.name', 'like', '%'.$filter.'%')
->orWhere('contacts.first_name', 'like', '%'.$filter.'%')
->orWhere('contacts.last_name', 'like', '%'.$filter.'%')
2016-11-29 18:47:26 +01:00
->orWhere('tasks.description', 'like', '%'.$filter.'%')
->orWhere('contacts.email', 'like', '%'.$filter.'%')
->orWhere('projects.name', 'like', '%'.$filter.'%');
2015-05-27 18:52:10 +02:00
});
}
return $query;
}
2017-11-21 08:35:28 +01:00
public function getClientDatatable($clientId)
{
$query = DB::table('tasks')
->leftJoin('projects', 'projects.id', '=', 'tasks.project_id')
->where('tasks.client_id', '=', $clientId)
->where('tasks.is_deleted', '=', false)
->whereNull('tasks.invoice_id')
->select(
'tasks.description',
'tasks.time_log',
'tasks.time_log as duration',
DB::raw("SUBSTRING(time_log, 3, 10) date"),
'projects.name as project'
);
$table = \Datatable::query($query)
->addColumn('project', function ($model) {
return $model->project;
})
->addColumn('date', function ($model) {
return Task::calcStartTime($model);
})
->addColumn('duration', function ($model) {
return Utils::formatTime(Task::calcDuration($model));
})
->addColumn('description', function ($model) {
return $model->description;
});
return $table->make();
}
2016-07-21 14:35:23 +02:00
public function save($publicId, $data, $task = null)
2015-09-25 11:57:40 +02:00
{
if ($task) {
// do nothing
} elseif ($publicId) {
2016-10-10 10:40:04 +02:00
$task = Task::scope($publicId)->withTrashed()->firstOrFail();
2015-05-27 18:52:10 +02:00
} else {
$task = Task::createNew();
2018-01-08 10:55:23 +01:00
$task->task_status_sort_order = 9999;
2015-05-27 18:52:10 +02:00
}
2016-10-10 10:40:04 +02:00
if ($task->is_deleted) {
return $task;
}
2016-11-29 18:47:26 +01:00
if (isset($data['client'])) {
$task->client_id = $data['client'] ? Client::getPrivateId($data['client']) : null;
2017-09-19 16:25:48 +02:00
} elseif (isset($data['client_id'])) {
$task->client_id = $data['client_id'] ? Client::getPrivateId($data['client_id']) : null;
2015-05-27 18:52:10 +02:00
}
2017-09-19 16:25:48 +02:00
2016-11-29 18:47:26 +01:00
if (isset($data['project_id'])) {
$task->project_id = $data['project_id'] ? Project::getPrivateId($data['project_id']) : null;
}
2015-05-27 18:52:10 +02:00
if (isset($data['description'])) {
$task->description = trim($data['description']);
2015-06-14 14:21:29 +02:00
}
2017-12-18 22:28:07 +01:00
if (isset($data['task_status_id'])) {
$task->task_status_id = $data['task_status_id'] ? TaskStatus::getPrivateId($data['task_status_id']) : null;
}
if (isset($data['task_status_sort_order'])) {
$task->task_status_sort_order = $data['task_status_sort_order'];
}
2015-05-27 18:52:10 +02:00
2015-08-11 16:38:36 +02:00
if (isset($data['time_log'])) {
$timeLog = json_decode($data['time_log']);
} elseif ($task->time_log) {
$timeLog = json_decode($task->time_log);
} else {
$timeLog = [];
}
2016-07-03 11:33:35 +02:00
2015-09-20 23:05:02 +02:00
array_multisort($timeLog);
2015-09-07 11:07:55 +02:00
if (isset($data['action'])) {
if ($data['action'] == 'start') {
$task->is_running = true;
$timeLog[] = [strtotime('now'), false];
2017-01-30 17:05:31 +01:00
} elseif ($data['action'] == 'resume') {
2015-09-07 11:07:55 +02:00
$task->is_running = true;
$timeLog[] = [strtotime('now'), false];
2017-01-30 17:05:31 +01:00
} elseif ($data['action'] == 'stop' && $task->is_running) {
2017-01-30 20:40:43 +01:00
$timeLog[count($timeLog) - 1][1] = time();
2015-09-07 11:07:55 +02:00
$task->is_running = false;
2017-05-03 05:51:52 +02:00
} elseif ($data['action'] == 'offline'){
$task->is_running = $data['is_running'] ? 1 : 0;
2015-09-07 11:07:55 +02:00
}
2017-09-24 20:44:37 +02:00
} elseif (isset($data['is_running'])) {
$task->is_running = $data['is_running'] ? 1 : 0;
2015-05-27 18:52:10 +02:00
}
2015-06-14 16:03:37 +02:00
$task->time_log = json_encode($timeLog);
2015-05-27 18:52:10 +02:00
$task->save();
return $task;
}
}