invoiceninja/app/Http/Controllers/ClientController.php

591 lines
22 KiB
PHP
Raw Normal View History

<?php
2019-05-11 13:32:07 +10:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 13:32:07 +10:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 13:32:07 +10:00
*
2021-06-16 16:58:16 +10:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 13:32:07 +10:00
*/
namespace App\Http\Controllers;
2020-07-08 22:02:16 +10:00
use App\Events\Client\ClientWasCreated;
2020-11-03 21:04:15 +11:00
use App\Events\Client\ClientWasUpdated;
use App\Factory\ClientFactory;
2019-03-28 21:07:45 +11:00
use App\Filters\ClientFilters;
2021-05-31 20:40:34 +10:00
use App\Http\Requests\Client\AdjustClientLedgerRequest;
use App\Http\Requests\Client\CreateClientRequest;
use App\Http\Requests\Client\DestroyClientRequest;
use App\Http\Requests\Client\EditClientRequest;
use App\Http\Requests\Client\ShowClientRequest;
use App\Http\Requests\Client\StoreClientRequest;
use App\Http\Requests\Client\UpdateClientRequest;
2021-02-15 21:52:50 +11:00
use App\Http\Requests\Client\UploadClientRequest;
use App\Jobs\Client\StoreClient;
use App\Jobs\Client\UpdateClient;
2021-03-08 08:32:38 +11:00
use App\Models\Account;
use App\Models\Client;
use App\Repositories\ClientRepository;
use App\Transformers\ClientTransformer;
2020-07-08 22:02:16 +10:00
use App\Utils\Ninja;
use App\Utils\Traits\BulkOptions;
use App\Utils\Traits\MakesHash;
2021-02-15 21:52:50 +11:00
use App\Utils\Traits\SavesDocuments;
2019-10-08 07:17:55 +10:00
use App\Utils\Traits\Uploadable;
use Illuminate\Http\Request;
2020-10-28 21:10:49 +11:00
use Illuminate\Http\Response;
2019-01-27 10:22:57 +11:00
/**
* Class ClientController.
2019-04-04 20:28:53 +11:00
* @covers App\Http\Controllers\ClientController
2019-01-27 10:22:57 +11:00
*/
2019-03-29 08:34:58 +11:00
class ClientController extends BaseController
{
use MakesHash;
2019-10-08 07:17:55 +10:00
use Uploadable;
use BulkOptions;
2021-02-15 21:52:50 +11:00
use SavesDocuments;
2019-04-03 13:34:28 +11:00
protected $entity_type = Client::class;
2019-03-29 08:34:58 +11:00
2019-04-03 13:34:28 +11:00
protected $entity_transformer = ClientTransformer::class;
2019-03-29 08:34:58 +11:00
2019-01-27 10:22:57 +11:00
/**
* @var ClientRepository
*/
2019-04-23 16:19:45 +10:00
protected $client_repo;
2019-01-27 10:22:57 +11:00
/**
* ClientController constructor.
2020-10-28 21:10:49 +11:00
* @param ClientRepository $client_repo
2019-01-27 10:22:57 +11:00
*/
2019-04-23 16:19:45 +10:00
public function __construct(ClientRepository $client_repo)
{
2019-03-29 08:34:58 +11:00
parent::__construct();
2019-04-23 16:19:45 +10:00
$this->client_repo = $client_repo;
}
2019-01-27 10:22:57 +11:00
/**
2020-10-28 21:10:49 +11:00
* @OA\Get(
2019-10-07 10:32:36 +11:00
* path="/api/v1/clients",
* operationId="getClients",
* tags={"clients"},
* summary="Gets a list of clients",
* description="Lists clients, search and filters allow fine grained lists to be generated.
2020-10-28 21:10:49 +11:00
Query parameters can be added to performed more fine grained filtering of the clients, these are handled by the ClientFilters class which defines the methods available",
2019-10-07 10:32:36 +11:00
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @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 clients",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 10:32:36 +11:00
* @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/Client"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
2019-10-07 10:32:36 +11:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
2020-10-28 21:10:49 +11:00
* @param ClientFilters $filters
* @return Response|mixed
2019-01-27 10:22:57 +11:00
*/
2019-03-28 21:07:45 +11:00
public function index(ClientFilters $filters)
{
2019-03-29 08:34:58 +11:00
$clients = Client::filter($filters);
2019-03-29 08:34:58 +11:00
return $this->listResponse($clients);
}
/**
* Display the specified resource.
*
2020-10-28 21:10:49 +11:00
* @param ShowClientRequest $request
* @param Client $client
* @return Response
2019-10-07 15:03:01 +11:00
*
*
* @OA\Get(
* path="/api/v1/clients/{id}",
* operationId="showClient",
* tags={"clients"},
* summary="Shows a client",
* description="Displays a client by id",
2019-10-07 15:03:01 +11:00
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Client Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the cl.ient object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 15:03:01 +11:00
* @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/Client"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
2019-10-07 15:03:01 +11:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function show(ShowClientRequest $request, Client $client)
{
return $this->itemResponse($client);
}
/**
* Show the form for editing the specified resource.
*
2020-10-28 21:10:49 +11:00
* @param EditClientRequest $request
* @param Client $client
* @return Response
2019-10-07 15:03:01 +11:00
*
*
2019-10-07 15:03:01 +11:00
* @OA\Get(
* path="/api/v1/clients/{id}/edit",
* operationId="editClient",
* tags={"clients"},
* summary="Shows a client for editting",
* description="Displays a client by id",
2019-10-07 15:03:01 +11:00
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Client Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the client object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 15:03:01 +11:00
* @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/Client"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
2019-10-07 15:03:01 +11:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function edit(EditClientRequest $request, Client $client)
{
2019-04-03 12:17:21 +11:00
return $this->itemResponse($client);
}
/**
* Update the specified resource in storage.
*
2020-10-28 21:10:49 +11:00
* @param UpdateClientRequest $request
* @param Client $client
* @return Response
2019-10-07 15:03:01 +11:00
*
*
*
2019-10-07 15:03:01 +11:00
* @OA\Put(
* path="/api/v1/clients/{id}",
* operationId="updateClient",
* tags={"clients"},
* summary="Updates a client",
* description="Handles the updating of a client by id",
2019-10-07 15:03:01 +11:00
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Client Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the client object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 15:03:01 +11:00
* @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/Client"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
2019-10-07 15:03:01 +11:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function update(UpdateClientRequest $request, Client $client)
{
2021-02-15 21:52:50 +11:00
if ($request->entityIsDeleted($client)) {
return $request->disallowUpdate();
}
2019-05-10 16:08:33 +10:00
$client = $this->client_repo->save($request->all(), $client);
2019-10-08 07:17:55 +10:00
$this->uploadLogo($request->file('company_logo'), $client->company, $client);
2019-10-07 22:05:06 +11:00
2021-05-07 07:12:07 +10:00
event(new ClientWasUpdated($client, $client->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
2020-11-03 21:04:15 +11:00
return $this->itemResponse($client->fresh());
}
/**
* Show the form for creating a new resource.
*
2020-10-28 21:10:49 +11:00
* @param CreateClientRequest $request
* @return Response
2019-10-07 15:03:01 +11:00
*
*
*
2019-10-07 15:03:01 +11:00
* @OA\Get(
* path="/api/v1/clients/create",
* operationId="getClientsCreate",
* tags={"clients"},
* summary="Gets a new blank client object",
2019-10-07 15:03:01 +11:00
* description="Returns a blank object with default values",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="A blank client object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 15:03:01 +11:00
* @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/Client"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
2019-10-07 15:03:01 +11:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function create(CreateClientRequest $request)
{
2019-03-28 20:05:13 +11:00
$client = ClientFactory::create(auth()->user()->company()->id, auth()->user()->id);
2019-04-03 13:34:28 +11:00
return $this->itemResponse($client);
}
/**
* Store a newly created resource in storage.
*
2020-10-28 21:10:49 +11:00
* @param StoreClientRequest $request
* @return Response
2019-10-07 15:03:01 +11:00
*
*
*
* @OA\Post(
* path="/api/v1/clients",
* operationId="storeClient",
* tags={"clients"},
* summary="Adds a client",
* description="Adds an client to a company",
2019-10-07 15:03:01 +11:00
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="Returns the saved client object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 15:03:01 +11:00
* @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/Client"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
2019-10-07 15:03:01 +11:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function store(StoreClientRequest $request)
{
2019-05-10 16:08:33 +10:00
$client = $this->client_repo->save($request->all(), ClientFactory::create(auth()->user()->company()->id, auth()->user()->id));
$client->load('contacts', 'primary_contact');
2021-06-10 20:18:01 +10:00
/* Set the client country to the company if none is set */
if(!$client->country_id && strlen($client->company->settings->country_id) > 1){
2021-12-17 22:11:36 +11:00
$client->update(['country_id' => $client->company->settings->country_id]);
2021-06-10 20:18:01 +10:00
}
2019-10-08 07:17:55 +10:00
$this->uploadLogo($request->file('company_logo'), $client->company, $client);
2021-05-07 07:12:07 +10:00
event(new ClientWasCreated($client, $client->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
2020-07-08 22:02:16 +10:00
2019-04-03 13:34:28 +11:00
return $this->itemResponse($client);
}
/**
* Remove the specified resource from storage.
*
2020-10-28 21:10:49 +11:00
* @param DestroyClientRequest $request
* @param Client $client
* @return Response
2019-10-07 15:03:01 +11:00
*
*
2020-10-28 21:10:49 +11:00
* @throws \Exception
2019-10-07 15:03:01 +11:00
* @OA\Delete(
* path="/api/v1/clients/{id}",
* operationId="deleteClient",
* tags={"clients"},
* summary="Deletes a client",
* description="Handles the deletion of a client by id",
2019-10-07 15:03:01 +11:00
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Client 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"),
2019-10-07 15:03:01 +11:00
* @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",
2019-10-07 15:03:01 +11:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function destroy(DestroyClientRequest $request, Client $client)
{
2019-03-26 15:46:08 +11:00
$this->client_repo->delete($client);
return $this->itemResponse($client->fresh());
}
/**
* Perform bulk actions on the list view.
2019-10-07 15:03:01 +11:00
*
2020-10-28 21:10:49 +11:00
* @return Response
*
*
2019-10-07 15:03:01 +11:00
* @OA\Post(
* path="/api/v1/clients/bulk",
* operationId="bulkClients",
* tags={"clients"},
* summary="Performs bulk actions on an array of clients",
* description="",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @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 Client User response",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 15:03:01 +11:00
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
2019-10-07 15:57:14 +11:00
* @OA\JsonContent(ref="#/components/schemas/Client"),
2019-10-07 15:03:01 +11:00
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
2019-10-07 15:03:01 +11:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function bulk()
{
$action = request()->input('action');
$ids = request()->input('ids');
$clients = Client::withTrashed()->whereIn('id', $this->transformKeys($ids))->cursor();
2022-01-30 10:46:39 +11:00
if(!in_array($action, ['restore','archive','delete','purge']))
2021-09-15 15:58:02 +10:00
return response()->json(['message' => 'That action is not available.'], 400);
$clients->each(function ($client, $key) use ($action) {
if (auth()->user()->can('edit', $client)) {
$this->client_repo->{$action}($client);
}
});
return $this->listResponse(Client::withTrashed()->whereIn('id', $this->transformKeys($ids)));
}
2021-02-15 21:52:50 +11:00
/**
* Update the specified resource in storage.
*
* @param UploadClientRequest $request
* @param Client $client
* @return Response
*
*
*
* @OA\Put(
* path="/api/v1/clients/{id}/upload",
* operationId="uploadClient",
* tags={"clients"},
* summary="Uploads a document to a client",
* description="Handles the uploading of a document to a client",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Client Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the client 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/Client"),
* ),
* @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(UploadClientRequest $request, Client $client)
{
2021-03-08 08:32:38 +11:00
if(!$this->checkFeature(Account::FEATURE_DOCUMENTS))
return $this->featureFailure();
2021-02-15 21:52:50 +11:00
if ($request->has('documents'))
$this->saveDocuments($request->file('documents'), $client);
return $this->itemResponse($client->fresh());
}
2021-05-31 15:27:26 +10:00
}