1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00
invoiceninja/app/Http/Controllers/TaskController.php

343 lines
11 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2015-05-27 18:52:10 +02:00
2017-01-30 20:40:43 +01:00
namespace App\Http\Controllers;
use App\Http\Requests\CreateTaskRequest;
use App\Http\Requests\TaskRequest;
use App\Http\Requests\UpdateTaskRequest;
2015-05-27 18:52:10 +02:00
use App\Models\Client;
2016-11-29 18:47:26 +01:00
use App\Models\Project;
2017-01-30 20:40:43 +01:00
use App\Models\Task;
use App\Models\TaskStatus;
2017-01-30 20:40:43 +01:00
use App\Ninja\Datatables\TaskDatatable;
2015-07-12 21:43:45 +02:00
use App\Ninja\Repositories\InvoiceRepository;
2017-01-30 20:40:43 +01:00
use App\Ninja\Repositories\TaskRepository;
2015-11-05 23:37:04 +01:00
use App\Services\TaskService;
2017-01-30 20:40:43 +01:00
use Auth;
use DropdownButton;
use Input;
use Redirect;
use Request;
2017-01-30 20:40:43 +01:00
use Session;
use URL;
use Utils;
use View;
2016-05-01 14:04:55 +02:00
/**
2017-01-30 20:40:43 +01:00
* Class TaskController.
*/
2015-05-27 18:52:10 +02:00
class TaskController extends BaseController
{
/**
* @var TaskRepository
*/
2015-05-27 18:52:10 +02:00
protected $taskRepo;
/**
* @var TaskService
*/
2015-11-05 23:37:04 +01:00
protected $taskService;
/**
* @var
*/
2016-04-28 14:16:33 +02:00
protected $entityType = ENTITY_TASK;
2015-05-27 18:52:10 +02:00
/**
* @var InvoiceRepository
*/
protected $invoiceRepo;
/**
* TaskController constructor.
2017-01-30 20:40:43 +01:00
*
* @param TaskRepository $taskRepo
* @param InvoiceRepository $invoiceRepo
2017-01-30 20:40:43 +01:00
* @param TaskService $taskService
*/
public function __construct(
TaskRepository $taskRepo,
InvoiceRepository $invoiceRepo,
TaskService $taskService
2017-01-30 17:05:31 +01:00
) {
2016-03-16 00:08:00 +01:00
// parent::__construct();
2015-05-27 18:52:10 +02:00
$this->taskRepo = $taskRepo;
2015-07-12 21:43:45 +02:00
$this->invoiceRepo = $invoiceRepo;
2015-11-05 23:37:04 +01:00
$this->taskService = $taskService;
2015-05-27 18:52:10 +02:00
}
/**
* @return \Illuminate\Contracts\View\View
2015-05-27 18:52:10 +02:00
*/
public function index()
2015-06-14 16:49:13 +02:00
{
return View::make('list_wrapper', [
2015-05-27 18:52:10 +02:00
'entityType' => ENTITY_TASK,
'datatable' => new TaskDatatable(),
2015-05-27 18:52:10 +02:00
'title' => trans('texts.tasks'),
]);
2015-05-27 18:52:10 +02:00
}
/**
* @param null $clientPublicId
2017-01-30 20:40:43 +01:00
*
* @return \Illuminate\Http\JsonResponse
*/
2017-12-24 20:01:17 +01:00
public function getDatatable($clientPublicId = null, $projectPublicId = null)
2015-05-27 18:52:10 +02:00
{
2017-12-24 20:01:17 +01:00
return $this->taskService->getDatatable($clientPublicId, $projectPublicId, Input::get('sSearch'));
2015-05-27 18:52:10 +02:00
}
/**
* Store a newly created resource in storage.
*
* @param CreateTaskRequest $request
*
* @return \Illuminate\Http\RedirectResponse
2015-05-27 18:52:10 +02:00
*/
2016-05-01 14:04:55 +02:00
public function store(CreateTaskRequest $request)
2015-05-27 18:52:10 +02:00
{
return $this->save($request);
2015-05-27 18:52:10 +02:00
}
/**
* @param $publicId
2017-01-30 20:40:43 +01:00
*
* @return \Illuminate\Http\RedirectResponse
*/
2016-05-01 21:30:39 +02:00
public function show($publicId)
2015-09-29 20:19:18 +02:00
{
Session::reflash();
2016-05-01 21:30:39 +02:00
return Redirect::to("tasks/{$publicId}/edit");
2015-09-29 20:19:18 +02:00
}
2015-05-27 18:52:10 +02:00
/**
* Show the form for creating a new resource.
*
* @param TaskRequest $request
*
* @return \Illuminate\Contracts\View\View
2015-05-27 18:52:10 +02:00
*/
2016-05-01 14:04:55 +02:00
public function create(TaskRequest $request)
2015-05-27 18:52:10 +02:00
{
2015-12-13 21:12:54 +01:00
$this->checkTimezone();
2015-05-27 18:52:10 +02:00
$data = [
'task' => null,
2016-05-01 14:04:55 +02:00
'clientPublicId' => Input::old('client') ? Input::old('client') : ($request->client_id ?: 0),
2016-11-29 18:47:26 +01:00
'projectPublicId' => Input::old('project_id') ? Input::old('project_id') : ($request->project_id ?: 0),
2015-05-27 18:52:10 +02:00
'method' => 'POST',
'url' => 'tasks',
'title' => trans('texts.new_task'),
2015-09-03 09:32:39 +02:00
'timezone' => Auth::user()->account->timezone ? Auth::user()->account->timezone->name : DEFAULT_TIMEZONE,
'datetimeFormat' => Auth::user()->account->getMomentDateTimeFormat(),
2015-05-27 18:52:10 +02:00
];
$data = array_merge($data, self::getViewModel());
return View::make('tasks.edit', $data);
}
/**
* Show the form for editing the specified resource.
*
* @param TaskRequest $request
*
* @return \Illuminate\Contracts\View\View
2015-05-27 18:52:10 +02:00
*/
2016-05-01 14:04:55 +02:00
public function edit(TaskRequest $request)
2015-05-27 18:52:10 +02:00
{
2015-12-13 21:12:54 +01:00
$this->checkTimezone();
2016-05-01 14:04:55 +02:00
$task = $request->entity();
2016-06-08 16:56:13 +02:00
2017-12-19 21:05:35 +01:00
if (! $task) {
return redirect('/');
}
2015-06-14 16:49:13 +02:00
$actions = [];
if ($task->invoice) {
$actions[] = ['url' => URL::to("invoices/{$task->invoice->public_id}/edit"), 'label' => trans('texts.view_invoice')];
2015-06-14 16:49:13 +02:00
} else {
$actions[] = ['url' => 'javascript:submitAction("invoice")', 'label' => trans('texts.invoice_task')];
2015-07-12 21:43:45 +02:00
// check for any open invoices
$invoices = $task->client_id ? $this->invoiceRepo->findOpenInvoices($task->client_id) : [];
2015-07-12 21:43:45 +02:00
foreach ($invoices as $invoice) {
2017-08-04 15:39:11 +02:00
$actions[] = ['url' => 'javascript:submitAction("add_to_invoice", '.$invoice->public_id.')', 'label' => trans('texts.add_to_invoice', ['invoice' => e($invoice->invoice_number)])];
2015-07-12 21:43:45 +02:00
}
2015-06-14 16:49:13 +02:00
}
2015-07-12 21:43:45 +02:00
2015-06-14 16:49:13 +02:00
$actions[] = DropdownButton::DIVIDER;
2017-01-30 20:40:43 +01:00
if (! $task->trashed()) {
2015-06-14 16:49:13 +02:00
$actions[] = ['url' => 'javascript:submitAction("archive")', 'label' => trans('texts.archive_task')];
$actions[] = ['url' => 'javascript:onDeleteClick()', 'label' => trans('texts.delete_task')];
} else {
$actions[] = ['url' => 'javascript:submitAction("restore")', 'label' => trans('texts.restore_task')];
}
2015-05-27 18:52:10 +02:00
$data = [
'task' => $task,
2016-10-18 16:55:07 +02:00
'entity' => $task,
2015-05-27 18:52:10 +02:00
'clientPublicId' => $task->client ? $task->client->public_id : 0,
2016-11-29 18:47:26 +01:00
'projectPublicId' => $task->project ? $task->project->public_id : 0,
2015-05-27 18:52:10 +02:00
'method' => 'PUT',
2016-05-01 14:04:55 +02:00
'url' => 'tasks/'.$task->public_id,
2015-06-14 16:03:37 +02:00
'title' => trans('texts.edit_task'),
2015-07-12 21:43:45 +02:00
'actions' => $actions,
2015-09-03 09:32:39 +02:00
'timezone' => Auth::user()->account->timezone ? Auth::user()->account->timezone->name : DEFAULT_TIMEZONE,
'datetimeFormat' => Auth::user()->account->getMomentDateTimeFormat(),
2015-05-27 18:52:10 +02:00
];
2018-03-14 11:21:50 +01:00
$data = array_merge($data, self::getViewModel($task));
2015-05-27 18:52:10 +02:00
return View::make('tasks.edit', $data);
}
/**
* Update the specified resource in storage.
*
* @param UpdateTaskRequest $request
*
* @return \Illuminate\Http\RedirectResponse
2015-05-27 18:52:10 +02:00
*/
2016-05-01 14:04:55 +02:00
public function update(UpdateTaskRequest $request)
2015-05-27 18:52:10 +02:00
{
2016-05-01 14:04:55 +02:00
$task = $request->entity();
2016-06-08 16:56:13 +02:00
return $this->save($request, $task->public_id);
2015-05-27 18:52:10 +02:00
}
/**
* @return array
*/
2018-03-14 11:21:50 +01:00
private static function getViewModel($task = false)
2015-05-27 18:52:10 +02:00
{
return [
2018-03-14 11:21:50 +01:00
'clients' => Client::scope()->withActiveOrSelected($task ? $task->client_id : false)->with('contacts')->orderBy('name')->get(),
2015-12-13 21:12:54 +01:00
'account' => Auth::user()->account,
2018-03-14 11:21:50 +01:00
'projects' => Project::scope()->withActiveOrSelected($task ? $task->project_id : false)->with('client.contacts')->orderBy('name')->get(),
2015-05-27 18:52:10 +02:00
];
}
/**
* @param null $publicId
2017-01-30 20:40:43 +01:00
*
* @return \Illuminate\Http\RedirectResponse
*/
private function save($request, $publicId = null)
2015-05-27 18:52:10 +02:00
{
2015-06-14 16:49:13 +02:00
$action = Input::get('action');
2016-06-08 16:56:13 +02:00
2015-12-29 18:49:40 +01:00
if (in_array($action, ['archive', 'delete', 'restore'])) {
2015-06-14 16:49:13 +02:00
return self::bulk();
}
$task = $this->taskRepo->save($publicId, $request->input());
2015-12-29 18:49:40 +01:00
if (in_array($action, ['invoice', 'add_to_invoice'])) {
return self::bulk();
}
2017-09-19 16:25:48 +02:00
if (request()->wantsJson()) {
2017-09-25 15:30:11 +02:00
$task->time_log = json_decode($task->time_log);
2017-09-20 08:23:41 +02:00
return $task->load(['client.contacts', 'project'])->toJson();
2017-09-19 16:25:48 +02:00
} else {
2017-10-01 21:07:15 +02:00
if ($publicId) {
Session::flash('message', trans('texts.updated_task'));
} else {
Session::flash('message', trans('texts.created_task'));
}
2017-09-19 16:25:48 +02:00
return Redirect::to("tasks/{$task->public_id}/edit");
}
2015-05-27 18:52:10 +02:00
}
/**
* @return \Illuminate\Http\RedirectResponse
*/
2015-05-27 18:52:10 +02:00
public function bulk()
{
$action = Input::get('action');
2015-10-28 20:22:07 +01:00
$ids = Input::get('public_id') ?: (Input::get('id') ?: Input::get('ids'));
$referer = Request::server('HTTP_REFERER');
2015-05-27 18:52:10 +02:00
2017-05-22 20:35:50 +02:00
if (in_array($action, ['resume', 'stop'])) {
2015-05-27 18:52:10 +02:00
$this->taskRepo->save($ids, ['action' => $action]);
2017-12-25 11:39:00 +01:00
Session::flash('message', trans($action == 'stop' ? 'texts.stopped_task' : 'texts.resumed_task'));
return $this->returnBulk($this->entityType, $action, $ids);
} elseif (strpos($action, 'update_status') === 0) {
list($action, $statusPublicId) = explode(':', $action);
Task::scope($ids)->update([
'task_status_id' => TaskStatus::getPrivateId($statusPublicId),
'task_status_sort_order' => 9999,
]);
Session::flash('message', trans('texts.updated_task_status'));
return $this->returnBulk($this->entityType, $action, $ids);
2017-01-30 17:05:31 +01:00
} elseif ($action == 'invoice' || $action == 'add_to_invoice') {
$tasks = Task::scope($ids)->with('account', 'client', 'project')->orderBy('project_id', 'id')->get();
2015-05-27 18:52:10 +02:00
$clientPublicId = false;
$data = [];
2016-06-08 16:56:13 +02:00
$lastProjectId = false;
2015-05-27 18:52:10 +02:00
foreach ($tasks as $task) {
if ($task->client) {
if ($task->client->trashed()) {
return redirect($referer)->withError(trans('texts.client_must_be_active'));
}
2017-01-30 20:40:43 +01:00
if (! $clientPublicId) {
2015-05-27 18:52:10 +02:00
$clientPublicId = $task->client->public_id;
2017-01-30 17:05:31 +01:00
} elseif ($clientPublicId != $task->client->public_id) {
return redirect($referer)->withError(trans('texts.task_error_multiple_clients'));
2015-05-27 18:52:10 +02:00
}
}
2015-06-14 14:21:29 +02:00
if ($task->is_running) {
return redirect($referer)->withError(trans('texts.task_error_running'));
2017-01-30 17:05:31 +01:00
} elseif ($task->invoice_id) {
return redirect($referer)->withError(trans('texts.task_error_invoiced'));
2015-05-27 18:52:10 +02:00
}
2016-06-08 16:56:13 +02:00
2015-12-13 21:12:54 +01:00
$account = Auth::user()->account;
$showProject = $lastProjectId != $task->project_id;
2015-05-27 18:52:10 +02:00
$data[] = [
'publicId' => $task->public_id,
'description' => $task->present()->invoiceDescription($account, $showProject),
2015-07-12 21:43:45 +02:00
'duration' => $task->getHours(),
'cost' => $task->getRate(),
2015-05-27 18:52:10 +02:00
];
$lastProjectId = $task->project_id;
2015-05-27 18:52:10 +02:00
}
2015-07-12 21:43:45 +02:00
if ($action == 'invoice') {
return Redirect::to("invoices/create/{$clientPublicId}")->with('tasks', $data);
} else {
$invoiceId = Input::get('invoice_id');
2017-01-30 20:40:43 +01:00
2015-07-12 21:43:45 +02:00
return Redirect::to("invoices/{$invoiceId}/edit")->with('tasks', $data);
}
2015-05-27 18:52:10 +02:00
} else {
$count = $this->taskService->bulk($ids, $action);
2017-09-20 13:10:05 +02:00
if (request()->wantsJson()) {
return response()->json($count);
} else {
$message = Utils::pluralize($action.'d_task', $count);
Session::flash('message', $message);
2015-05-27 18:52:10 +02:00
2017-09-20 13:10:05 +02:00
return $this->returnBulk($this->entityType, $action, $ids);
}
2015-05-27 18:52:10 +02:00
}
}
2016-07-04 18:49:01 +02:00
2015-12-13 21:12:54 +01:00
private function checkTimezone()
{
2017-01-30 20:40:43 +01:00
if (! Auth::user()->account->timezone) {
2015-12-13 21:12:54 +01:00
$link = link_to('/settings/localization?focus=timezone_id', trans('texts.click_here'), ['target' => '_blank']);
Session::now('warning', trans('texts.timezone_unset', ['link' => $link]));
2015-12-13 21:12:54 +01:00
}
}
2015-05-27 18:52:10 +02:00
}