invoiceninja/app/Http/Controllers/ClientApiController.php

200 lines
5.2 KiB
PHP
Raw Normal View History

2017-01-30 21:40:43 +02:00
<?php
namespace App\Http\Controllers;
2015-03-17 07:45:25 +10:00
use App\Http\Requests\ClientRequest;
2015-10-28 21:22:07 +02:00
use App\Http\Requests\CreateClientRequest;
2016-01-26 22:36:00 +11:00
use App\Http\Requests\UpdateClientRequest;
2017-01-30 21:40:43 +02:00
use App\Models\Client;
use App\Ninja\Repositories\ClientRepository;
use Input;
use Response;
2015-03-17 07:45:25 +10:00
class ClientApiController extends BaseAPIController
2015-03-17 07:45:25 +10:00
{
protected $clientRepo;
2016-05-01 23:55:13 +03:00
protected $entityType = ENTITY_CLIENT;
2016-05-02 11:38:01 +03:00
public function __construct(ClientRepository $clientRepo)
2015-03-17 07:45:25 +10:00
{
2016-03-02 16:36:46 +02:00
parent::__construct();
2015-03-17 07:45:25 +10:00
$this->clientRepo = $clientRepo;
}
2015-11-08 23:57:28 +02:00
/**
* @SWG\Get(
* path="/clients",
* summary="List clients",
* operationId="listClients",
2015-11-08 23:57:28 +02:00
* tags={"client"},
* @SWG\Response(
* response=200,
* description="A list of clients",
2015-11-08 23:57:28 +02:00
* @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Client"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
2015-03-17 07:45:25 +10:00
public function index()
{
2015-07-02 23:21:29 +03:00
$clients = Client::scope()
2016-05-01 23:55:13 +03:00
->orderBy('created_at', 'desc')
->withTrashed();
2016-01-24 16:59:01 +00:00
2016-05-02 09:33:48 +03:00
if ($email = Input::get('email')) {
2016-01-24 16:59:01 +00:00
$clients = $clients->whereHas('contacts', function ($query) use ($email) {
$query->where('email', $email);
});
} elseif ($idNumber = Input::get('id_number')) {
$clients = $clients->whereIdNumber($idNumber);
2016-01-24 16:59:01 +00:00
}
2016-06-09 15:20:15 +03:00
2016-05-02 11:38:01 +03:00
return $this->listResponse($clients);
2015-03-17 07:45:25 +10:00
}
/**
* @SWG\Get(
* path="/clients/{client_id}",
* summary="Retrieve a client",
* operationId="getClient",
* tags={"client"},
* @SWG\Parameter(
* in="path",
* name="client_id",
* type="integer",
* required=true
* ),
* @SWG\Response(
* response=200,
* description="A single client",
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Client"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
public function show(ClientRequest $request)
{
2018-08-03 10:04:08 +03:00
$client = $request->entity();
if (strpos(request()->include, 'activities') !== false) {
$client->load('activities.client.contacts', 'activities.user', 'activities.invoice', 'activities.payment', 'activities.credit', 'activities.account', 'activities.task', 'activities.expense', 'activities.contact');
}
return $this->itemResponse($client);
}
2015-11-08 23:57:28 +02:00
/**
* @SWG\Post(
* path="/clients",
* summary="Create a client",
* operationId="createClient",
* tags={"client"},
2015-11-08 23:57:28 +02:00
* @SWG\Parameter(
* in="body",
* name="client",
2015-11-08 23:57:28 +02:00
* @SWG\Schema(ref="#/definitions/Client")
* ),
* @SWG\Response(
* response=200,
* description="New client",
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Client"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
2015-10-28 21:22:07 +02:00
public function store(CreateClientRequest $request)
2015-03-17 07:45:25 +10:00
{
2016-01-24 17:06:34 +00:00
$client = $this->clientRepo->save($request->input());
2016-05-02 11:38:01 +03:00
return $this->itemResponse($client);
2015-03-17 07:45:25 +10:00
}
2016-01-26 22:36:00 +11:00
/**
2016-01-26 22:37:16 +11:00
* @SWG\Put(
2016-01-26 22:36:00 +11:00
* path="/clients/{client_id}",
* summary="Update a client",
* operationId="updateClient",
* tags={"client"},
2016-01-26 22:36:00 +11:00
* @SWG\Parameter(
* in="path",
* name="client_id",
* type="integer",
* required=true
* ),
* @SWG\Parameter(
2016-01-26 22:36:00 +11:00
* in="body",
* name="client",
2016-01-26 22:36:00 +11:00
* @SWG\Schema(ref="#/definitions/Client")
* ),
* @SWG\Response(
* response=200,
* description="Updated client",
2016-01-26 22:36:00 +11:00
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Client"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
2017-01-30 21:54:09 +02:00
*
2017-01-30 21:49:42 +02:00
* @param mixed $publicId
2016-01-26 22:36:00 +11:00
*/
2016-01-27 07:52:30 +11:00
public function update(UpdateClientRequest $request, $publicId)
2016-01-26 22:36:00 +11:00
{
2016-05-02 11:38:01 +03:00
if ($request->action) {
return $this->handleAction($request);
2016-01-27 11:36:21 +11:00
}
2016-06-09 15:20:15 +03:00
2016-01-27 07:52:30 +11:00
$data = $request->input();
$data['public_id'] = $publicId;
$client = $this->clientRepo->save($data, $request->entity());
2016-01-26 22:36:00 +11:00
2016-06-09 15:20:15 +03:00
$client->load(['contacts']);
2016-05-02 11:38:01 +03:00
return $this->itemResponse($client);
2016-01-26 22:36:00 +11:00
}
2016-02-07 21:24:21 +11:00
2016-02-08 15:27:42 +11:00
/**
* @SWG\Delete(
* path="/clients/{client_id}",
* summary="Delete a client",
* operationId="deleteClient",
* tags={"client"},
2016-02-08 15:27:42 +11:00
* @SWG\Parameter(
* in="path",
* name="client_id",
* type="integer",
* required=true
2016-02-08 15:27:42 +11:00
* ),
* @SWG\Response(
* response=200,
* description="Deleted client",
2016-02-08 15:27:42 +11:00
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Client"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
public function destroy(UpdateClientRequest $request)
2016-02-08 15:27:42 +11:00
{
2016-05-03 11:53:00 +03:00
$client = $request->entity();
2016-06-09 15:20:15 +03:00
2016-02-08 15:27:42 +11:00
$this->clientRepo->delete($client);
2016-05-03 11:53:00 +03:00
return $this->itemResponse($client);
2016-02-08 15:27:42 +11:00
}
2016-06-09 15:20:15 +03:00
}