1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/app/Http/Controllers/ProjectController.php

177 lines
5.7 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2016-11-29 18:47:26 +01:00
2017-01-30 20:40:43 +01:00
namespace App\Http\Controllers;
2017-12-25 11:13:01 +01:00
use App\Jobs\GenerateProjectChartData;
2017-01-30 20:40:43 +01:00
use App\Http\Requests\CreateProjectRequest;
use App\Http\Requests\ProjectRequest;
use App\Http\Requests\UpdateProjectRequest;
use App\Models\Client;
use App\Models\Project;
2017-01-30 20:40:43 +01:00
use App\Ninja\Datatables\ProjectDatatable;
use App\Ninja\Repositories\ProjectRepository;
use App\Services\ProjectService;
2016-11-29 18:47:26 +01:00
use Auth;
use Input;
use Session;
2017-01-30 20:40:43 +01:00
use View;
2016-11-29 18:47:26 +01:00
class ProjectController extends BaseController
{
protected $projectRepo;
protected $projectService;
protected $entityType = ENTITY_PROJECT;
public function __construct(ProjectRepository $projectRepo, ProjectService $projectService)
{
$this->projectRepo = $projectRepo;
$this->projectService = $projectService;
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return View::make('list_wrapper', [
'entityType' => ENTITY_PROJECT,
'datatable' => new ProjectDatatable(),
'title' => trans('texts.projects'),
]);
}
public function getDatatable($expensePublicId = null)
{
$search = Input::get('sSearch');
$userId = Auth::user()->filterIdByEntity(ENTITY_PROJECT);
2016-11-29 18:47:26 +01:00
return $this->projectService->getDatatable($search, $userId);
}
2017-12-24 18:47:49 +01:00
public function show(ProjectRequest $request)
{
2017-12-24 22:54:19 +01:00
$account = auth()->user()->account;
2017-12-24 18:47:49 +01:00
$project = $request->entity();
2019-09-13 01:30:10 +02:00
$chartData = dispatch_now(new GenerateProjectChartData($project));
2017-12-24 18:47:49 +01:00
$data = [
'account' => auth()->user()->account,
'project' => $project,
'title' => trans('texts.view_project'),
'showBreadcrumbs' => false,
2017-12-25 11:13:01 +01:00
'chartData' => $chartData,
2017-12-24 18:47:49 +01:00
];
return View::make('projects.show', $data);
}
2016-11-29 18:47:26 +01:00
public function create(ProjectRequest $request)
{
$data = [
'account' => auth()->user()->account,
2016-11-29 18:47:26 +01:00
'project' => null,
'method' => 'POST',
'url' => 'projects',
'title' => trans('texts.new_project'),
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(),
'clientPublicId' => $request->client_id,
];
return View::make('projects.edit', $data);
}
public function edit(ProjectRequest $request)
{
$project = $request->entity();
$data = [
'account' => auth()->user()->account,
2016-11-29 18:47:26 +01:00
'project' => $project,
'method' => 'PUT',
'url' => 'projects/' . $project->public_id,
'title' => trans('texts.edit_project'),
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(),
'clientPublicId' => $project->client ? $project->client->public_id : null,
];
return View::make('projects.edit', $data);
}
public function store(CreateProjectRequest $request)
{
2016-12-15 13:17:10 +01:00
$project = $this->projectService->save($request->input());
2016-11-29 18:47:26 +01:00
Session::flash('message', trans('texts.created_project'));
return redirect()->to($project->getRoute());
}
public function update(UpdateProjectRequest $request)
{
2016-12-15 13:17:10 +01:00
$project = $this->projectService->save($request->input(), $request->entity());
2016-11-29 18:47:26 +01:00
Session::flash('message', trans('texts.updated_project'));
$action = Input::get('action');
if (in_array($action, ['archive', 'delete', 'restore', 'invoice'])) {
return self::bulk();
}
2016-11-29 18:47:26 +01:00
return redirect()->to($project->getRoute());
}
public function bulk()
{
$action = Input::get('action');
$ids = Input::get('public_id') ? Input::get('public_id') : Input::get('ids');
if ($action == 'invoice') {
$data = [];
$clientPublicId = false;
$lastClientId = false;
$lastProjectId = false;
$projects = Project::scope($ids)
->with(['client', 'tasks' => function ($query) {
$query->whereNull('invoice_id');
}])
->get();
foreach ($projects as $project) {
if (! $clientPublicId) {
$clientPublicId = $project->client->public_id;
}
if ($lastClientId && $lastClientId != $project->client_id) {
return redirect('projects')->withError(trans('texts.project_error_multiple_clients'));
}
$lastClientId = $project->client_id;
foreach ($project->tasks as $task) {
if ($task->is_running) {
return redirect('projects')->withError(trans('texts.task_error_running'));
}
$showProject = $lastProjectId != $task->project_id;
$data[] = [
'publicId' => $task->public_id,
'description' => $task->present()->invoiceDescription(auth()->user()->account, $showProject),
'duration' => $task->getHours(),
'cost' => $task->getRate(),
];
$lastProjectId = $task->project_id;
}
}
return redirect("invoices/create/{$clientPublicId}")->with('tasks', $data);
} else {
$count = $this->projectService->bulk($ids, $action);
if ($count > 0) {
$field = $count == 1 ? "{$action}d_project" : "{$action}d_projects";
$message = trans("texts.$field", ['count' => $count]);
Session::flash('message', $message);
}
return redirect()->to('/projects');
2016-11-29 18:47:26 +01:00
}
}
}