2018-10-17 14:26:27 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
2018-10-17 14:26:27 +02:00
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Events\Client\ClientWasCreated;
|
2020-11-03 11:04:15 +01:00
|
|
|
use App\Events\Client\ClientWasUpdated;
|
2019-02-04 13:06:19 +01:00
|
|
|
use App\Factory\ClientFactory;
|
2019-03-28 11:07:45 +01:00
|
|
|
use App\Filters\ClientFilters;
|
2023-01-22 04:40:22 +01:00
|
|
|
use App\Http\Requests\Client\BulkClientRequest;
|
2019-01-21 15:06:49 +01:00
|
|
|
use App\Http\Requests\Client\CreateClientRequest;
|
2019-04-16 05:28:05 +02:00
|
|
|
use App\Http\Requests\Client\DestroyClientRequest;
|
2018-11-03 02:01:40 +01:00
|
|
|
use App\Http\Requests\Client\EditClientRequest;
|
2022-01-31 01:03:01 +01:00
|
|
|
use App\Http\Requests\Client\PurgeClientRequest;
|
2019-01-21 15:06:49 +01:00
|
|
|
use App\Http\Requests\Client\ShowClientRequest;
|
2018-12-07 11:57:20 +01:00
|
|
|
use App\Http\Requests\Client\StoreClientRequest;
|
2018-11-10 14:24:36 +01:00
|
|
|
use App\Http\Requests\Client\UpdateClientRequest;
|
2021-02-15 11:52:50 +01:00
|
|
|
use App\Http\Requests\Client\UploadClientRequest;
|
2021-03-07 22:32:38 +01:00
|
|
|
use App\Models\Account;
|
2018-10-29 04:16:17 +01:00
|
|
|
use App\Models\Client;
|
2018-11-22 12:12:41 +01:00
|
|
|
use App\Repositories\ClientRepository;
|
2019-04-16 05:28:05 +02:00
|
|
|
use App\Transformers\ClientTransformer;
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Utils\Ninja;
|
2019-12-17 11:58:23 +01:00
|
|
|
use App\Utils\Traits\BulkOptions;
|
2018-11-27 07:59:16 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2021-02-15 11:52:50 +01:00
|
|
|
use App\Utils\Traits\SavesDocuments;
|
2019-10-07 23:17:55 +02:00
|
|
|
use App\Utils\Traits\Uploadable;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Http\Response;
|
2022-01-31 01:03:01 +01:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-01-27 00:22:57 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class ClientController.
|
2019-04-04 11:28:53 +02:00
|
|
|
* @covers App\Http\Controllers\ClientController
|
2019-01-27 00:22:57 +01:00
|
|
|
*/
|
2019-03-28 22:34:58 +01:00
|
|
|
class ClientController extends BaseController
|
2018-10-17 14:26:27 +02:00
|
|
|
{
|
2018-11-27 07:59:16 +01:00
|
|
|
use MakesHash;
|
2019-10-07 23:17:55 +02:00
|
|
|
use Uploadable;
|
2019-12-17 11:58:23 +01:00
|
|
|
use BulkOptions;
|
2021-02-15 11:52:50 +01:00
|
|
|
use SavesDocuments;
|
2019-12-17 11:58:23 +01:00
|
|
|
|
2019-04-03 04:34:28 +02:00
|
|
|
protected $entity_type = Client::class;
|
2019-03-28 22:34:58 +01:00
|
|
|
|
2019-04-03 04:34:28 +02:00
|
|
|
protected $entity_transformer = ClientTransformer::class;
|
2019-03-28 22:34:58 +01:00
|
|
|
|
2019-01-27 00:22:57 +01:00
|
|
|
/**
|
|
|
|
* @var ClientRepository
|
|
|
|
*/
|
2019-04-23 08:19:45 +02:00
|
|
|
protected $client_repo;
|
2018-11-22 12:12:41 +01:00
|
|
|
|
2019-01-27 00:22:57 +01:00
|
|
|
/**
|
|
|
|
* ClientController constructor.
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param ClientRepository $client_repo
|
2019-01-27 00:22:57 +01:00
|
|
|
*/
|
2019-04-23 08:19:45 +02:00
|
|
|
public function __construct(ClientRepository $client_repo)
|
2018-11-22 12:12:41 +01:00
|
|
|
{
|
2019-03-28 22:34:58 +01:00
|
|
|
parent::__construct();
|
2019-02-04 13:06:19 +01:00
|
|
|
|
2019-04-23 08:19:45 +02:00
|
|
|
$this->client_repo = $client_repo;
|
2018-11-22 12:12:41 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 00:22:57 +01:00
|
|
|
/**
|
2020-10-28 11:10:49 +01:00
|
|
|
* @OA\Get(
|
2019-10-07 01:32:36 +02: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.
|
|
|
|
|
2023-02-10 10:21:10 +01: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",
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-10-07 01:32:36 +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 clients",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-10-07 01:32:36 +02: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(
|
2019-12-17 11:58:23 +01:00
|
|
|
* response="default",
|
2019-10-07 01:32:36 +02:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param ClientFilters $filters
|
|
|
|
* @return Response|mixed
|
2019-01-27 00:22:57 +01:00
|
|
|
*/
|
2019-03-28 11:07:45 +01:00
|
|
|
public function index(ClientFilters $filters)
|
2018-10-17 14:26:27 +02:00
|
|
|
{
|
2022-04-27 01:05:16 +02:00
|
|
|
set_time_limit(45);
|
|
|
|
|
2019-03-28 22:34:58 +01:00
|
|
|
$clients = Client::filter($filters);
|
2019-12-17 11:58:23 +01:00
|
|
|
|
2019-03-28 22:34:58 +01:00
|
|
|
return $this->listResponse($clients);
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-04 13:06:19 +01:00
|
|
|
* Display the specified resource.
|
2018-10-17 14:26:27 +02:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param ShowClientRequest $request
|
|
|
|
* @param Client $client
|
|
|
|
* @return Response
|
2019-10-07 06:03:01 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Get(
|
|
|
|
* path="/api/v1/clients/{id}",
|
|
|
|
* operationId="showClient",
|
|
|
|
* tags={"clients"},
|
2019-12-05 21:41:13 +01:00
|
|
|
* summary="Shows a client",
|
|
|
|
* description="Displays a client by id",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-10-07 06:03:01 +02:00
|
|
|
* @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,
|
2019-12-05 21:41:13 +01:00
|
|
|
* description="Returns the cl.ient object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-10-07 06:03:01 +02: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(
|
2019-12-17 11:58:23 +01:00
|
|
|
* response="default",
|
2019-10-07 06:03:01 +02:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2018-10-17 14:26:27 +02:00
|
|
|
*/
|
2019-02-04 13:06:19 +01:00
|
|
|
public function show(ShowClientRequest $request, Client $client)
|
2018-10-17 14:26:27 +02:00
|
|
|
{
|
2019-12-17 11:58:23 +01:00
|
|
|
return $this->itemResponse($client);
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-04 13:06:19 +01:00
|
|
|
* Show the form for editing the specified resource.
|
2018-10-17 14:26:27 +02:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param EditClientRequest $request
|
|
|
|
* @param Client $client
|
|
|
|
* @return Response
|
2019-10-07 06:03:01 +02:00
|
|
|
*
|
2019-12-17 11:58:23 +01:00
|
|
|
*
|
2019-10-07 06:03:01 +02:00
|
|
|
* @OA\Get(
|
|
|
|
* path="/api/v1/clients/{id}/edit",
|
|
|
|
* operationId="editClient",
|
|
|
|
* tags={"clients"},
|
2019-12-05 21:41:13 +01:00
|
|
|
* summary="Shows a client for editting",
|
|
|
|
* description="Displays a client by id",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-10-07 06:03:01 +02:00
|
|
|
* @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,
|
2019-12-05 21:41:13 +01:00
|
|
|
* description="Returns the client object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-10-07 06:03:01 +02: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(
|
2019-12-17 11:58:23 +01:00
|
|
|
* response="default",
|
2019-10-07 06:03:01 +02:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2018-10-17 14:26:27 +02:00
|
|
|
*/
|
2019-02-04 13:06:19 +01:00
|
|
|
public function edit(EditClientRequest $request, Client $client)
|
2018-10-17 14:26:27 +02:00
|
|
|
{
|
2019-04-03 03:17:21 +02:00
|
|
|
return $this->itemResponse($client);
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-04 13:06:19 +01:00
|
|
|
* Update the specified resource in storage.
|
2018-10-17 14:26:27 +02:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param UpdateClientRequest $request
|
|
|
|
* @param Client $client
|
|
|
|
* @return Response
|
2019-10-07 06:03:01 +02:00
|
|
|
*
|
2019-12-17 11:58:23 +01:00
|
|
|
*
|
|
|
|
*
|
2019-10-07 06:03:01 +02:00
|
|
|
* @OA\Put(
|
|
|
|
* path="/api/v1/clients/{id}",
|
|
|
|
* operationId="updateClient",
|
|
|
|
* tags={"clients"},
|
2019-12-05 21:41:13 +01:00
|
|
|
* summary="Updates a client",
|
|
|
|
* description="Handles the updating of a client by id",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-10-07 06:03:01 +02:00
|
|
|
* @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,
|
2019-12-05 21:41:13 +01:00
|
|
|
* description="Returns the client object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-10-07 06:03:01 +02: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(
|
2019-12-17 11:58:23 +01:00
|
|
|
* response="default",
|
2019-10-07 06:03:01 +02:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2018-10-17 14:26:27 +02:00
|
|
|
*/
|
2019-02-04 13:06:19 +01:00
|
|
|
public function update(UpdateClientRequest $request, Client $client)
|
2018-10-17 14:26:27 +02:00
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($request->entityIsDeleted($client)) {
|
2019-12-30 22:59:12 +01:00
|
|
|
return $request->disallowUpdate();
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2019-05-10 08:08:33 +02:00
|
|
|
|
|
|
|
$client = $this->client_repo->save($request->all(), $client);
|
2019-01-25 11:47:23 +01:00
|
|
|
|
2019-10-07 23:17:55 +02:00
|
|
|
$this->uploadLogo($request->file('company_logo'), $client->company, $client);
|
2019-10-07 13:05:06 +02:00
|
|
|
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new ClientWasUpdated($client, $client->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2020-11-03 11:04:15 +01:00
|
|
|
|
2019-11-29 11:41:07 +01:00
|
|
|
return $this->itemResponse($client->fresh());
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-04 13:06:19 +01:00
|
|
|
* Show the form for creating a new resource.
|
2018-10-17 14:26:27 +02:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param CreateClientRequest $request
|
|
|
|
* @return Response
|
2019-10-07 06:03:01 +02:00
|
|
|
*
|
2019-12-17 11:58:23 +01:00
|
|
|
*
|
|
|
|
*
|
2019-10-07 06:03:01 +02:00
|
|
|
* @OA\Get(
|
|
|
|
* path="/api/v1/clients/create",
|
|
|
|
* operationId="getClientsCreate",
|
|
|
|
* tags={"clients"},
|
2019-12-05 21:41:13 +01:00
|
|
|
* summary="Gets a new blank client object",
|
2019-10-07 06:03:01 +02:00
|
|
|
* description="Returns a blank object with default values",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-10-07 06:03:01 +02:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
2019-12-05 21:41:13 +01:00
|
|
|
* description="A blank client object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-10-07 06:03:01 +02: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(
|
2019-12-17 11:58:23 +01:00
|
|
|
* response="default",
|
2019-10-07 06:03:01 +02:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2018-10-17 14:26:27 +02:00
|
|
|
*/
|
2019-02-04 13:06:19 +01:00
|
|
|
public function create(CreateClientRequest $request)
|
2018-10-17 14:26:27 +02:00
|
|
|
{
|
2019-03-28 10:05:13 +01:00
|
|
|
$client = ClientFactory::create(auth()->user()->company()->id, auth()->user()->id);
|
2019-02-04 13:06:19 +01:00
|
|
|
|
2019-04-03 04:34:28 +02:00
|
|
|
return $this->itemResponse($client);
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-04 13:06:19 +01:00
|
|
|
* Store a newly created resource in storage.
|
2018-10-17 14:26:27 +02:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param StoreClientRequest $request
|
|
|
|
* @return Response
|
2019-10-07 06:03:01 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/clients",
|
|
|
|
* operationId="storeClient",
|
|
|
|
* tags={"clients"},
|
2019-12-05 07:22:20 +01:00
|
|
|
* summary="Adds a client",
|
2019-12-05 21:41:13 +01:00
|
|
|
* description="Adds an client to a company",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-10-07 06:03:01 +02:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
2019-12-05 21:41:13 +01:00
|
|
|
* description="Returns the saved client object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-10-07 06:03:01 +02: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(
|
2019-12-17 11:58:23 +01:00
|
|
|
* response="default",
|
2019-10-07 06:03:01 +02:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2018-10-17 14:26:27 +02:00
|
|
|
*/
|
2019-02-04 13:06:19 +01:00
|
|
|
public function store(StoreClientRequest $request)
|
2018-10-17 14:26:27 +02:00
|
|
|
{
|
2019-05-10 08:08:33 +02:00
|
|
|
$client = $this->client_repo->save($request->all(), ClientFactory::create(auth()->user()->company()->id, auth()->user()->id));
|
2019-02-04 13:06:19 +01:00
|
|
|
|
|
|
|
$client->load('contacts', 'primary_contact');
|
|
|
|
|
2021-06-10 12:18:01 +02:00
|
|
|
/* Set the client country to the company if none is set */
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $client->country_id && strlen($client->company->settings->country_id) > 1) {
|
2021-12-17 12:11:36 +01:00
|
|
|
$client->update(['country_id' => $client->company->settings->country_id]);
|
2021-06-10 12:18:01 +02:00
|
|
|
}
|
|
|
|
|
2019-10-07 23:17:55 +02:00
|
|
|
$this->uploadLogo($request->file('company_logo'), $client->company, $client);
|
|
|
|
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new ClientWasCreated($client, $client->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2020-07-08 14:02:16 +02:00
|
|
|
|
2019-04-03 04:34:28 +02:00
|
|
|
return $this->itemResponse($client);
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param DestroyClientRequest $request
|
|
|
|
* @param Client $client
|
|
|
|
* @return Response
|
2019-10-07 06:03:01 +02:00
|
|
|
*
|
2019-12-17 11:58:23 +01:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @throws \Exception
|
2019-10-07 06:03:01 +02:00
|
|
|
* @OA\Delete(
|
|
|
|
* path="/api/v1/clients/{id}",
|
|
|
|
* operationId="deleteClient",
|
|
|
|
* tags={"clients"},
|
2019-12-05 21:41:13 +01:00
|
|
|
* summary="Deletes a client",
|
|
|
|
* description="Handles the deletion of a client by id",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-10-07 06:03:01 +02:00
|
|
|
* @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",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-10-07 06:03:01 +02: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(
|
2019-12-17 11:58:23 +01:00
|
|
|
* response="default",
|
2019-10-07 06:03:01 +02:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2018-10-17 14:26:27 +02:00
|
|
|
*/
|
2019-04-16 05:28:05 +02:00
|
|
|
public function destroy(DestroyClientRequest $request, Client $client)
|
2018-10-17 14:26:27 +02:00
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->client_repo->delete($client);
|
2019-03-26 05:46:08 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return $this->itemResponse($client->fresh());
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
2018-10-29 04:16:17 +01:00
|
|
|
|
2019-01-19 11:35:21 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Perform bulk actions on the list view.
|
2019-10-07 06:03:01 +02:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Response
|
2019-12-17 11:58:23 +01:00
|
|
|
*
|
|
|
|
*
|
2019-10-07 06:03:01 +02:00
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/clients/bulk",
|
|
|
|
* operationId="bulkClients",
|
|
|
|
* tags={"clients"},
|
|
|
|
* summary="Performs bulk actions on an array of clients",
|
|
|
|
* description="",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2019-10-07 06:03:01 +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 Client User response",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-10-07 06:03:01 +02: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 06:57:14 +02:00
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Client"),
|
2019-10-07 06:03:01 +02:00
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
2019-12-17 11:58:23 +01:00
|
|
|
* response="default",
|
2019-10-07 06:03:01 +02:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2019-01-19 11:35:21 +01:00
|
|
|
*/
|
2023-01-22 04:40:22 +01:00
|
|
|
public function bulk(BulkClientRequest $request)
|
2019-01-13 11:42:03 +01:00
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2023-01-22 04:40:22 +01:00
|
|
|
$action = $request->action;
|
2021-09-15 07:58:02 +02:00
|
|
|
|
2023-01-22 06:34:47 +01:00
|
|
|
$clients = Client::withTrashed()
|
|
|
|
->company()
|
|
|
|
->whereIn('id', $request->ids)
|
|
|
|
->cursor()
|
|
|
|
->each(function ($client) use ($action) {
|
|
|
|
if (auth()->user()->can('edit', $client)) {
|
|
|
|
$this->client_repo->{$action}($client);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return $this->listResponse(Client::withTrashed()->company()->whereIn('id', $request->ids));
|
2019-01-13 11:42:03 +01:00
|
|
|
}
|
|
|
|
|
2021-02-15 11:52:50 +01: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",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2021-02-15 11:52:50 +01:00
|
|
|
* @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)
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $this->checkFeature(Account::FEATURE_DOCUMENTS)) {
|
2021-03-07 22:32:38 +01:00
|
|
|
return $this->featureFailure();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->has('documents')) {
|
2021-02-15 11:52:50 +01:00
|
|
|
$this->saveDocuments($request->file('documents'), $client);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-02-15 11:52:50 +01:00
|
|
|
|
|
|
|
return $this->itemResponse($client->fresh());
|
|
|
|
}
|
|
|
|
|
2022-01-31 01:03:01 +01:00
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param UploadClientRequest $request
|
|
|
|
* @param Client $client
|
|
|
|
* @return Response
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
2022-04-20 00:28:25 +02:00
|
|
|
* @OA\Post(
|
2022-01-31 01:03:01 +01:00
|
|
|
* path="/api/v1/clients/{id}/purge",
|
2022-04-01 04:46:55 +02:00
|
|
|
* operationId="purgeClient",
|
2022-01-31 01:03:01 +01:00
|
|
|
* tags={"clients"},
|
|
|
|
* summary="Purges a client from the system",
|
|
|
|
* description="Handles purging a client",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2022-01-31 01:03:01 +01:00
|
|
|
* @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\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 purge(PurgeClientRequest $request, Client $client)
|
|
|
|
{
|
|
|
|
//delete all documents
|
2022-06-21 11:57:17 +02:00
|
|
|
$client->documents->each(function ($document) {
|
2022-10-10 05:26:53 +02:00
|
|
|
|
|
|
|
try{
|
|
|
|
Storage::disk(config('filesystems.default'))->delete($document->url);
|
|
|
|
}
|
|
|
|
catch(\Exception $e){
|
|
|
|
nlog($e->getMessage());
|
|
|
|
}
|
|
|
|
|
2022-01-31 01:03:01 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
//force delete the client
|
|
|
|
$this->client_repo->purge($client);
|
|
|
|
|
|
|
|
return response()->json(['message' => 'Success'], 200);
|
|
|
|
|
|
|
|
//todo add an event here using the client name as reference for purge event
|
|
|
|
}
|
2021-05-31 07:27:26 +02:00
|
|
|
|
2022-07-15 07:54:19 +02:00
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param PurgeClientRequest $request
|
|
|
|
* @param Client $client
|
|
|
|
* @param string $mergeable client hashed_id
|
|
|
|
* @return Response
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Post(
|
2022-08-11 08:26:47 +02:00
|
|
|
* path="/api/v1/clients/{id}/{mergeable_client_hashed_id}/merge",
|
2022-07-15 07:54:19 +02:00
|
|
|
* operationId="mergeClient",
|
|
|
|
* tags={"clients"},
|
|
|
|
* summary="Merges two clients",
|
|
|
|
* description="Handles merging 2 clients",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2022-07-15 07:54:19 +02:00
|
|
|
* @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\Parameter(
|
2022-08-11 08:26:47 +02:00
|
|
|
* name="mergeable_client_hashed_id",
|
2022-07-15 07:54:19 +02:00
|
|
|
* in="path",
|
|
|
|
* description="The Mergeable 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\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 merge(PurgeClientRequest $request, Client $client, string $mergeable_client)
|
|
|
|
{
|
|
|
|
|
|
|
|
$m_client = Client::withTrashed()
|
|
|
|
->where('id', $this->decodePrimaryKey($mergeable_client))
|
|
|
|
->where('company_id', auth()->user()->company()->id)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if(!$m_client)
|
|
|
|
return response()->json(['message' => "Client not found"]);
|
|
|
|
|
|
|
|
$merged_client = $client->service()->merge($m_client)->save();
|
|
|
|
|
|
|
|
return $this->itemResponse($merged_client);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|