2018-10-17 23:26:27 +11:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
2019-03-03 08:44:08 +11:00
|
|
|
use App\DataMapper\ClientSettings;
|
2019-02-04 23:06:19 +11:00
|
|
|
use App\Factory\ClientFactory;
|
2019-01-22 01:06:49 +11:00
|
|
|
use App\Http\Requests\Client\CreateClientRequest;
|
2018-11-03 12:01:40 +11:00
|
|
|
use App\Http\Requests\Client\EditClientRequest;
|
2019-01-22 01:06:49 +11:00
|
|
|
use App\Http\Requests\Client\ShowClientRequest;
|
2018-12-07 21:57:20 +11:00
|
|
|
use App\Http\Requests\Client\StoreClientRequest;
|
2018-11-11 00:24:36 +11:00
|
|
|
use App\Http\Requests\Client\UpdateClientRequest;
|
2018-12-02 21:42:06 +11:00
|
|
|
use App\Jobs\Client\StoreClient;
|
2018-11-27 17:59:16 +11:00
|
|
|
use App\Jobs\Client\UpdateClient;
|
2019-01-19 21:35:21 +11:00
|
|
|
use App\Jobs\Entity\ActionEntity;
|
2018-10-29 14:16:17 +11:00
|
|
|
use App\Models\Client;
|
2018-12-02 21:42:06 +11:00
|
|
|
use App\Models\ClientContact;
|
2019-01-25 21:47:23 +11:00
|
|
|
use App\Models\Country;
|
2019-02-17 21:34:46 +11:00
|
|
|
use App\Models\Currency;
|
|
|
|
|
use App\Models\Size;
|
2018-11-22 22:12:41 +11:00
|
|
|
use App\Repositories\ClientRepository;
|
2018-11-27 17:59:16 +11:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
|
use App\Utils\Traits\UserSessionAttributes;
|
2018-10-17 23:26:27 +11:00
|
|
|
use Illuminate\Http\Request;
|
2019-02-17 21:34:46 +11:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
|
2018-10-17 23:26:27 +11:00
|
|
|
|
2019-01-27 10:22:57 +11:00
|
|
|
/**
|
|
|
|
|
* Class ClientController
|
|
|
|
|
* @package App\Http\Controllers
|
|
|
|
|
*/
|
2018-10-17 23:26:27 +11:00
|
|
|
class ClientController extends Controller
|
|
|
|
|
{
|
2018-11-27 17:59:16 +11:00
|
|
|
use UserSessionAttributes;
|
|
|
|
|
use MakesHash;
|
2018-11-22 22:12:41 +11:00
|
|
|
|
2019-01-27 10:22:57 +11:00
|
|
|
/**
|
|
|
|
|
* @var ClientRepository
|
|
|
|
|
*/
|
2018-11-22 22:12:41 +11:00
|
|
|
protected $clientRepo;
|
|
|
|
|
|
2019-01-27 10:22:57 +11:00
|
|
|
/**
|
|
|
|
|
* ClientController constructor.
|
|
|
|
|
* @param ClientRepository $clientRepo
|
|
|
|
|
*/
|
2019-03-26 14:08:19 +11:00
|
|
|
public function __construct(ClientRepository $clientRepo)
|
2018-11-22 22:12:41 +11:00
|
|
|
{
|
2019-02-04 23:06:19 +11:00
|
|
|
|
2018-11-22 22:12:41 +11:00
|
|
|
$this->clientRepo = $clientRepo;
|
2019-02-04 23:06:19 +11:00
|
|
|
|
2018-11-22 22:12:41 +11:00
|
|
|
}
|
|
|
|
|
|
2019-01-27 10:22:57 +11:00
|
|
|
/**
|
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
|
|
|
|
|
*/
|
2018-12-24 08:45:55 +08:00
|
|
|
public function index()
|
2018-10-17 23:26:27 +11:00
|
|
|
{
|
2019-03-26 14:08:19 +11:00
|
|
|
return response()->json(Client::all());
|
2018-10-17 23:26:27 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-02-04 23:06:19 +11:00
|
|
|
* Display the specified resource.
|
2018-10-17 23:26:27 +11:00
|
|
|
*
|
2019-02-04 23:06:19 +11:00
|
|
|
* @param int $id
|
2018-10-17 23:26:27 +11:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
2019-02-04 23:06:19 +11:00
|
|
|
public function show(ShowClientRequest $request, Client $client)
|
2018-10-17 23:26:27 +11:00
|
|
|
{
|
2018-12-02 21:42:06 +11:00
|
|
|
|
2019-02-04 23:06:19 +11:00
|
|
|
$data = [
|
2019-01-19 21:35:21 +11:00
|
|
|
'client' => $client,
|
2019-03-27 15:50:13 +11:00
|
|
|
'company' => $client->company(),
|
2019-02-04 23:06:19 +11:00
|
|
|
'meta' => collect([
|
2019-03-26 14:08:19 +11:00
|
|
|
'google_maps_api_key' => config('ninja.google_maps_api_key')
|
2019-02-04 23:06:19 +11:00
|
|
|
])
|
2018-12-02 21:42:06 +11:00
|
|
|
];
|
|
|
|
|
|
2019-03-27 15:50:13 +11:00
|
|
|
//return response()->json($data);
|
|
|
|
|
return redirect()->route('clients.edit', ['id' => $this->encodePrimarykey($client->id)]);
|
2018-10-17 23:26:27 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-02-04 23:06:19 +11:00
|
|
|
* Show the form for editing the specified resource.
|
2018-10-17 23:26:27 +11:00
|
|
|
*
|
2019-02-04 23:06:19 +11:00
|
|
|
* @param int $id
|
2018-10-17 23:26:27 +11:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
2019-02-04 23:06:19 +11:00
|
|
|
public function edit(EditClientRequest $request, Client $client)
|
2018-10-17 23:26:27 +11:00
|
|
|
{
|
2018-12-02 21:42:06 +11:00
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
|
'client' => $client,
|
2019-03-27 15:50:13 +11:00
|
|
|
'settings' => collect(ClientSettings::buildClientSettings($client->company()->settings_object, $client->client_settings_object)),
|
2019-02-04 23:06:19 +11:00
|
|
|
'hashed_id' => $this->encodePrimarykey($client->id),
|
2019-03-27 15:50:13 +11:00
|
|
|
'company' => $client->company(),
|
2019-02-17 21:34:46 +11:00
|
|
|
'sizes' => Size::all(),
|
2018-12-02 21:42:06 +11:00
|
|
|
];
|
2019-01-25 21:47:23 +11:00
|
|
|
|
2019-03-26 14:08:19 +11:00
|
|
|
return response()->json($data);
|
2018-12-02 21:42:06 +11:00
|
|
|
|
2018-10-17 23:26:27 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-02-04 23:06:19 +11:00
|
|
|
* Update the specified resource in storage.
|
2018-10-17 23:26:27 +11:00
|
|
|
*
|
2019-02-04 23:06:19 +11:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @param App\Models\Client $client
|
2018-10-17 23:26:27 +11:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
2019-02-04 23:06:19 +11:00
|
|
|
public function update(UpdateClientRequest $request, Client $client)
|
2018-10-17 23:26:27 +11:00
|
|
|
{
|
2018-11-21 19:28:07 +11:00
|
|
|
|
2019-02-04 23:06:19 +11:00
|
|
|
$client = UpdateClient::dispatchNow($request, $client);
|
2019-01-25 21:47:23 +11:00
|
|
|
|
2019-02-04 23:06:19 +11:00
|
|
|
return response()->json($client, 200);
|
2019-01-25 21:47:23 +11:00
|
|
|
|
2018-10-17 23:26:27 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-02-04 23:06:19 +11:00
|
|
|
* Show the form for creating a new resource.
|
2018-10-17 23:26:27 +11:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
2019-02-04 23:06:19 +11:00
|
|
|
public function create(CreateClientRequest $request)
|
2018-10-17 23:26:27 +11:00
|
|
|
{
|
2019-02-04 23:06:19 +11:00
|
|
|
$client = ClientFactory::create($this->getCurrentCompanyId(), auth()->user()->id);
|
|
|
|
|
|
2018-11-07 16:22:36 +11:00
|
|
|
$data = [
|
2019-02-04 23:06:19 +11:00
|
|
|
'client' => $client,
|
|
|
|
|
'hashed_id' => '',
|
|
|
|
|
'countries' => Country::all()
|
2018-11-07 16:22:36 +11:00
|
|
|
];
|
|
|
|
|
|
2019-03-26 14:08:19 +11:00
|
|
|
return response()->json($data);
|
2018-10-17 23:26:27 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-02-04 23:06:19 +11:00
|
|
|
* Store a newly created resource in storage.
|
2018-10-17 23:26:27 +11:00
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
2019-02-04 23:06:19 +11:00
|
|
|
public function store(StoreClientRequest $request)
|
2018-10-17 23:26:27 +11:00
|
|
|
{
|
2019-02-04 23:06:19 +11:00
|
|
|
|
|
|
|
|
$client = StoreClient::dispatchNow($request, new Client);
|
|
|
|
|
|
|
|
|
|
$client->load('contacts', 'primary_contact');
|
|
|
|
|
|
|
|
|
|
$client->hashed_id = $this->encodePrimarykey($client->id);
|
2018-11-11 00:24:36 +11:00
|
|
|
|
2019-01-26 20:34:38 +11:00
|
|
|
return response()->json($client, 200);
|
2018-11-11 00:24:36 +11:00
|
|
|
|
2018-10-17 23:26:27 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
2019-03-26 15:46:08 +11:00
|
|
|
public function destroy(Client $client)
|
2018-10-17 23:26:27 +11:00
|
|
|
{
|
2019-03-26 15:46:08 +11:00
|
|
|
$client->delete();
|
|
|
|
|
|
|
|
|
|
return response()->json([], 200);
|
2018-10-17 23:26:27 +11:00
|
|
|
}
|
2018-10-29 14:16:17 +11:00
|
|
|
|
2019-01-19 21:35:21 +11:00
|
|
|
/**
|
|
|
|
|
* Perform bulk actions on the list view
|
|
|
|
|
*
|
|
|
|
|
* @return Collection
|
|
|
|
|
*/
|
2019-01-16 20:28:06 +11:00
|
|
|
public function bulk()
|
2019-01-13 12:42:03 +02:00
|
|
|
{
|
2019-01-22 01:06:49 +11:00
|
|
|
|
2019-01-19 21:35:21 +11:00
|
|
|
$action = request()->input('action');
|
2019-02-04 23:06:19 +11:00
|
|
|
|
2019-01-19 21:35:21 +11:00
|
|
|
$ids = request()->input('ids');
|
|
|
|
|
|
2019-01-20 16:00:50 +11:00
|
|
|
$clients = Client::withTrashed()->find($ids);
|
2019-01-19 21:35:21 +11:00
|
|
|
|
|
|
|
|
$clients->each(function ($client, $key) use($action){
|
2019-01-22 01:06:49 +11:00
|
|
|
|
|
|
|
|
if(auth()->user()->can('edit', $client))
|
|
|
|
|
ActionEntity::dispatchNow($client, $action);
|
|
|
|
|
|
2019-01-19 21:35:21 +11:00
|
|
|
});
|
2019-01-22 01:06:49 +11:00
|
|
|
|
|
|
|
|
//todo need to return the updated dataset
|
|
|
|
|
return response()->json('success', 200);
|
|
|
|
|
|
2019-01-13 12:42:03 +02:00
|
|
|
}
|
|
|
|
|
|
2019-02-04 23:06:19 +11:00
|
|
|
/**
|
|
|
|
|
* Returns a client statement
|
|
|
|
|
*
|
|
|
|
|
* @return [type] [description]
|
|
|
|
|
*/
|
|
|
|
|
public function statement()
|
|
|
|
|
{
|
|
|
|
|
//todo
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-22 01:06:49 +11:00
|
|
|
|
|
|
|
|
|
2018-10-17 23:26:27 +11:00
|
|
|
}
|