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

594 lines
21 KiB
PHP
Raw Normal View History

2020-10-08 00:25:39 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2020-10-08 00:25:39 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-10-08 00:25:39 +02:00
*/
namespace App\Http\Controllers;
use App\Factory\ProjectFactory;
use App\Filters\ProjectFilters;
2023-11-26 08:41:42 +01:00
use App\Http\Requests\Project\BulkProjectRequest;
use App\Http\Requests\Project\CreateProjectRequest;
use App\Http\Requests\Project\DestroyProjectRequest;
2020-10-08 00:25:39 +02:00
use App\Http\Requests\Project\EditProjectRequest;
use App\Http\Requests\Project\ShowProjectRequest;
use App\Http\Requests\Project\StoreProjectRequest;
use App\Http\Requests\Project\UpdateProjectRequest;
2021-02-15 21:58:19 +01:00
use App\Http\Requests\Project\UploadProjectRequest;
2023-11-26 08:41:42 +01:00
use App\Models\Account;
use App\Models\Project;
use App\Repositories\ProjectRepository;
use App\Services\Template\TemplateAction;
use App\Transformers\ProjectTransformer;
use App\Utils\Traits\GeneratesCounter;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\SavesDocuments;
use Illuminate\Http\Response;
2020-10-08 00:25:39 +02:00
/**
* Class ProjectController.
*/
class ProjectController extends BaseController
{
use MakesHash;
use SavesDocuments;
2020-10-20 01:55:14 +02:00
use GeneratesCounter;
2020-10-28 11:10:49 +01:00
2020-10-08 00:25:39 +02:00
protected $entity_type = Project::class;
protected $entity_transformer = ProjectTransformer::class;
protected $project_repo;
/**
* ProjectController constructor.
2020-10-28 11:10:49 +01:00
* @param ProjectRepository $project_repo
2020-10-08 00:25:39 +02:00
*/
public function __construct(ProjectRepository $project_repo)
{
parent::__construct();
$this->project_repo = $project_repo;
}
/**
2020-10-28 11:10:49 +01:00
* @OA\Get(
2020-10-08 00:25:39 +02:00
* path="/api/v1/projects",
* operationId="getProjects",
* tags={"projects"},
* summary="Gets a list of projects",
* description="Lists projects",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2020-10-08 00:25:39 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(ref="#/components/parameters/index"),
* @OA\Response(
* response=200,
* description="A list of projects",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Project"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
2020-10-28 11:10:49 +01:00
* @param ProjectFilters $filters
* @return Response|mixed
2020-10-08 00:25:39 +02:00
*/
public function index(ProjectFilters $filters)
{
$projects = Project::filter($filters);
return $this->listResponse($projects);
}
/**
* Display the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param ShowProjectRequest $request
* @param Project $project
* @return Response
2020-10-08 00:25:39 +02:00
*
*
* @OA\Get(
* path="/api/v1/projects/{id}",
* operationId="showProject",
* tags={"projects"},
* summary="Shows a project",
* description="Displays a project by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2020-10-08 00:25:39 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Project Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the expense object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Project"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function show(ShowProjectRequest $request, Project $project)
{
return $this->itemResponse($project);
}
/**
* Show the form for editing the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param EditProjectRequest $request
* @param Project $project
* @return Response
2020-10-08 00:25:39 +02:00
*
*
* @OA\Get(
* path="/api/v1/projects/{id}/edit",
* operationId="editProject",
* tags={"projects"},
* summary="Shows a project for editting",
* description="Displays a project by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2020-10-08 00:25:39 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Project Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the project object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Project"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function edit(EditProjectRequest $request, Project $project)
{
return $this->itemResponse($project);
}
/**
* Update the specified resource in storage.
*
2020-10-28 11:10:49 +01:00
* @param UpdateProjectRequest $request
* @param Project $project
* @return Response
2020-10-08 00:25:39 +02:00
*
*
*
* @OA\Put(
* path="/api/v1/projects/{id}",
* operationId="updateProject",
* tags={"projects"},
* summary="Updates a project",
* description="Handles the updating of a project by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2020-10-08 00:25:39 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Project Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the project object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Project"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function update(UpdateProjectRequest $request, Project $project)
{
if ($request->entityIsDeleted($project)) {
return $request->disallowUpdate();
}
$project->fill($request->all());
2020-10-29 12:01:37 +01:00
$project->number = empty($project->number) ? $this->getNextProjectNumber($project) : $project->number;
2023-02-01 04:12:44 +01:00
$project->saveQuietly();
2020-10-08 00:25:39 +02:00
2021-02-03 13:29:44 +01:00
if ($request->has('documents')) {
$this->saveDocuments($request->input('documents'), $project, $request->input('is_public', true));
2021-02-03 13:29:44 +01:00
}
2023-02-01 04:12:44 +01:00
event('eloquent.updated: App\Models\Project', $project);
2020-10-08 00:25:39 +02:00
return $this->itemResponse($project->fresh());
}
/**
* Show the form for creating a new resource.
*
2020-10-28 11:10:49 +01:00
* @param CreateProjectRequest $request
* @return Response
2020-10-08 00:25:39 +02:00
*
*
*
* @OA\Get(
* path="/api/v1/projects/create",
* operationId="getProjectsCreate",
* tags={"projects"},
* summary="Gets a new blank project object",
* description="Returns a blank object with default values",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2020-10-08 00:25:39 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="A blank project object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Project"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function create(CreateProjectRequest $request)
{
2023-08-04 09:12:21 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
$project = ProjectFactory::create($user->company()->id, $user->id);
2020-10-08 00:25:39 +02:00
return $this->itemResponse($project);
}
/**
* Store a newly created resource in storage.
*
2020-10-28 11:10:49 +01:00
* @param StoreProjectRequest $request
* @return Response
2020-10-08 00:25:39 +02:00
*
*
*
* @OA\Post(
* path="/api/v1/projects",
* operationId="storeProject",
* tags={"projects"},
* summary="Adds a project",
* description="Adds an project to a company",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2020-10-08 00:25:39 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="Returns the saved project object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Project"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function store(StoreProjectRequest $request)
{
2023-08-04 09:12:21 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
$project = ProjectFactory::create($user->company()->id, $user->id);
2020-10-08 00:25:39 +02:00
$project->fill($request->all());
2023-02-01 04:12:44 +01:00
$project->saveQuietly();
2020-10-08 00:25:39 +02:00
2020-11-25 15:19:52 +01:00
if (empty($project->number)) {
2020-10-29 12:01:37 +01:00
$project->number = $this->getNextProjectNumber($project);
2023-02-01 04:12:44 +01:00
$project->saveQuietly();
2020-10-29 12:01:37 +01:00
}
2020-10-12 23:27:42 +02:00
if ($request->has('documents')) {
$this->saveDocuments($request->input('documents'), $project, $request->input('is_public', true));
2020-10-12 22:42:02 +02:00
}
2020-10-28 11:10:49 +01:00
2023-02-01 04:12:44 +01:00
event('eloquent.created: App\Models\Project', $project);
2020-10-08 00:25:39 +02:00
return $this->itemResponse($project->fresh());
}
/**
* Remove the specified resource from storage.
*
2020-10-28 11:10:49 +01:00
* @param DestroyProjectRequest $request
* @param Project $project
* @return Response
2020-10-08 00:25:39 +02:00
*
*
2020-10-28 11:10:49 +01:00
* @throws \Exception
2020-10-08 00:25:39 +02:00
* @OA\Delete(
* path="/api/v1/projects/{id}",
* operationId="deleteProject",
* tags={"projects"},
* summary="Deletes a project",
* description="Handles the deletion of a project by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2020-10-08 00:25:39 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Project Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns a HTTP status",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function destroy(DestroyProjectRequest $request, Project $project)
{
//may not need these destroy routes as we are using actions to 'archive/delete'
$project->is_deleted = true;
$project->delete();
$project->save();
return $this->itemResponse($project->fresh());
2020-10-08 00:25:39 +02:00
}
/**
* Perform bulk actions on the list view.
*
2020-10-28 11:10:49 +01:00
* @return Response
2020-10-08 00:25:39 +02:00
*
*
* @OA\Post(
* path="/api/v1/projects/bulk",
* operationId="bulkProjects",
* tags={"projects"},
* summary="Performs bulk actions on an array of projects",
* description="",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2020-10-08 00:25:39 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/index"),
* @OA\RequestBody(
* description="User credentials",
* required=true,
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* type="array",
* @OA\Items(
* type="integer",
* description="Array of hashed IDs to be bulk 'actioned",
* example="[0,1,2,3]",
* ),
* )
* )
* ),
* @OA\Response(
* response=200,
* description="The Project User response",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Project"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2023-11-18 08:28:28 +01:00
public function bulk(BulkProjectRequest $request)
2020-10-08 00:25:39 +02:00
{
2023-08-04 09:12:21 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2023-11-18 08:28:28 +01:00
$action = $request->input('action');
$ids = $request->input('ids');
$projects = Project::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()->get();
2020-10-08 00:25:39 +02:00
2023-11-18 08:28:28 +01:00
if($action == 'template' && $user->can('view', $projects->first())) {
2020-10-08 00:25:39 +02:00
2023-11-18 08:28:28 +01:00
$hash_or_response = $request->boolean('send_email') ? 'email sent' : \Illuminate\Support\Str::uuid();
TemplateAction::dispatch(
2023-11-18 08:45:26 +01:00
$projects->pluck('hashed_id')->toArray(),
2023-11-18 08:28:28 +01:00
$request->template_id,
Project::class,
$user->id,
$user->company(),
$user->company()->db,
$hash_or_response,
$request->boolean('send_email')
);
return response()->json(['message' => $hash_or_response], 200);
}
2020-10-08 00:25:39 +02:00
2023-11-18 08:28:28 +01:00
$projects->each(function ($project) use ($action, $user) {
2023-08-04 09:12:21 +02:00
if ($user->can('edit', $project)) {
2020-10-08 00:25:39 +02:00
$this->project_repo->{$action}($project);
}
});
return $this->listResponse(Project::withTrashed()->whereIn('id', $this->transformKeys($ids)));
}
2021-02-15 21:58:19 +01:00
/**
* Update the specified resource in storage.
*
2023-08-04 09:12:21 +02:00
* @param UploadProjectRequest $request
* @param Project $project
2021-02-15 21:58:19 +01:00
* @return Response
*
* @OA\Put(
* path="/api/v1/projects/{id}/upload",
* operationId="uploadProject",
* tags={"projects"},
* summary="Uploads a document to a project",
* description="Handles the uploading of a document to a project",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2021-02-15 21:58:19 +01:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Project Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the Project object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Project"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function upload(UploadProjectRequest $request, Project $project)
2021-02-15 21:58:19 +01:00
{
if (! $this->checkFeature(Account::FEATURE_DOCUMENTS)) {
2021-03-07 22:32:38 +01:00
return $this->featureFailure();
}
if ($request->has('documents')) {
$this->saveDocuments($request->file('documents'), $project, $request->input('is_public', true));
}
2021-02-15 21:58:19 +01:00
return $this->itemResponse($project->fresh());
}
2020-10-08 00:25:39 +02:00
}