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

156 lines
5.9 KiB
PHP
Raw Normal View History

2016-07-21 14:35:23 +02:00
<?php namespace App\Ninja\Repositories;
2015-05-27 18:52:10 +02:00
use Auth;
use Session;
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;
class TaskRepository extends BaseRepository
2015-05-27 18:52:10 +02:00
{
public function getClassName()
{
return 'App\Models\Task';
}
2015-05-27 18:52:10 +02:00
public function find($clientPublicId = null, $filter = null)
{
$query = \DB::table('tasks')
->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')
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',
\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',
'tasks.created_at as date',
2016-11-29 18:47:26 +01:00
'tasks.user_id',
'projects.name as project',
'projects.public_id as project_public_id',
'projects.user_id as project_user_id'
2016-01-23 22:36:31 +01:00
);
2015-05-27 18:52:10 +02:00
if ($clientPublicId) {
$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);
if ( ! in_array(TASK_STATUS_PAID, $statuses)) {
$query->where('invoices.balance', '>', 0);
}
}
if (in_array(TASK_STATUS_PAID, $statuses)) {
$query->orWhere('invoices.balance', '=', 0);
}
});
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;
}
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();
}
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;
2015-05-27 18:52:10 +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
}
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];
} else if ($data['action'] == 'resume') {
$task->is_running = true;
$timeLog[] = [strtotime('now'), false];
} else if ($data['action'] == 'stop' && $task->is_running) {
$timeLog[count($timeLog)-1][1] = time();
$task->is_running = false;
}
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;
}
}