2018-10-17 14:26:27 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2018-12-24 01:45:55 +01:00
|
|
|
use App\Datatables\ClientDatatable;
|
2019-01-21 15:06:49 +01:00
|
|
|
use App\Http\Requests\Client\CreateClientRequest;
|
2018-11-03 02:01:40 +01:00
|
|
|
use App\Http\Requests\Client\EditClientRequest;
|
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;
|
2018-12-02 11:42:06 +01:00
|
|
|
use App\Jobs\Client\StoreClient;
|
2018-11-27 07:59:16 +01:00
|
|
|
use App\Jobs\Client\UpdateClient;
|
2019-01-19 11:35:21 +01:00
|
|
|
use App\Jobs\Entity\ActionEntity;
|
2018-10-29 04:16:17 +01:00
|
|
|
use App\Models\Client;
|
2018-12-02 11:42:06 +01:00
|
|
|
use App\Models\ClientContact;
|
2019-01-25 11:47:23 +01:00
|
|
|
use App\Models\Country;
|
2018-11-22 12:12:41 +01:00
|
|
|
use App\Repositories\ClientRepository;
|
2018-11-27 07:59:16 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2018-12-13 00:23:21 +01:00
|
|
|
use App\Utils\Traits\MakesMenu;
|
2018-11-27 07:59:16 +01:00
|
|
|
use App\Utils\Traits\UserSessionAttributes;
|
2018-10-17 14:26:27 +02:00
|
|
|
use Illuminate\Http\Request;
|
2019-01-20 06:00:50 +01:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2018-12-07 11:57:20 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-10-29 04:16:17 +01:00
|
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
use Yajra\DataTables\Html\Builder;
|
2018-10-17 14:26:27 +02:00
|
|
|
|
|
|
|
class ClientController extends Controller
|
|
|
|
{
|
2018-11-27 07:59:16 +01:00
|
|
|
use UserSessionAttributes;
|
|
|
|
use MakesHash;
|
2018-12-13 00:23:21 +01:00
|
|
|
use MakesMenu;
|
2018-11-22 12:12:41 +01:00
|
|
|
|
|
|
|
protected $clientRepo;
|
|
|
|
|
2019-01-07 12:30:28 +01:00
|
|
|
protected $clientDatatable;
|
|
|
|
|
|
|
|
public function __construct(ClientRepository $clientRepo, ClientDatatable $clientDatatable)
|
2018-11-22 12:12:41 +01:00
|
|
|
{
|
|
|
|
$this->clientRepo = $clientRepo;
|
2019-01-07 12:30:28 +01:00
|
|
|
$this->clientDatatable = $clientDatatable;
|
2018-11-22 12:12:41 +01:00
|
|
|
}
|
|
|
|
|
2018-12-24 01:45:55 +01:00
|
|
|
public function index()
|
2018-10-17 14:26:27 +02:00
|
|
|
{
|
2019-01-25 11:47:23 +01:00
|
|
|
;
|
2018-12-24 01:45:55 +01:00
|
|
|
if(request('page'))
|
2019-01-07 12:30:28 +01:00
|
|
|
return $this->clientDatatable->query(request(), $this->getCurrentCompanyId());
|
2018-12-24 01:45:55 +01:00
|
|
|
|
2019-01-13 11:42:03 +01:00
|
|
|
$data = [
|
2019-01-19 11:35:21 +01:00
|
|
|
'datatable' => $this->clientDatatable->buildOptions(),
|
|
|
|
'listaction' => $this->clientDatatable->listActions()
|
2019-01-13 11:42:03 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return view('client.vue_list', $data);
|
2018-11-02 11:54:46 +01:00
|
|
|
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2019-01-21 15:06:49 +01:00
|
|
|
public function create(CreateClientRequest $request)
|
2018-10-17 14:26:27 +02:00
|
|
|
{
|
2018-12-02 11:42:06 +01:00
|
|
|
$client = new Client;
|
2018-12-07 11:57:20 +01:00
|
|
|
$client->name = '';
|
|
|
|
$client->company_id = $this->getCurrentCompanyId();
|
2018-12-02 11:42:06 +01:00
|
|
|
$client_contact = new ClientContact;
|
|
|
|
$client_contact->first_name = "";
|
2019-01-21 15:06:49 +01:00
|
|
|
$client_contact->user_id = auth()->user()->id;
|
|
|
|
$client_contact->company_id = $this->getCurrentCompanyId();
|
2018-12-07 11:57:20 +01:00
|
|
|
$client_contact->id = 0;
|
2018-12-02 11:42:06 +01:00
|
|
|
|
|
|
|
$client->contacts->add($client_contact);
|
|
|
|
|
|
|
|
$data = [
|
2019-01-19 11:35:21 +01:00
|
|
|
'client' => $client,
|
2019-01-25 11:47:23 +01:00
|
|
|
'hashed_id' => '',
|
|
|
|
'countries' => Country::all()
|
2018-12-02 11:42:06 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return view('client.create', $data);
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2018-12-07 11:57:20 +01:00
|
|
|
public function store(StoreClientRequest $request)
|
2018-10-17 14:26:27 +02:00
|
|
|
{
|
2018-12-02 11:42:06 +01:00
|
|
|
$client = StoreClient::dispatchNow($request, new Client);
|
2018-12-07 11:57:20 +01:00
|
|
|
$client->load('contacts', 'primary_contact');
|
|
|
|
|
|
|
|
$client->hashed_id = $this->encodePrimarykey($client->id);
|
2018-12-02 11:42:06 +01:00
|
|
|
|
2018-12-07 11:57:20 +01:00
|
|
|
/*
|
2018-12-02 11:42:06 +01:00
|
|
|
$data = [
|
|
|
|
'client' => $client,
|
|
|
|
'hashed_id' => $this->encodePrimarykey($client->id)
|
|
|
|
];
|
2019-01-25 11:47:23 +01:00
|
|
|
|
2018-12-07 11:57:20 +01:00
|
|
|
Log::error(print_r($client,1));
|
2019-01-25 11:47:23 +01:00
|
|
|
*/
|
2018-12-07 11:57:20 +01:00
|
|
|
return response()->json($client, 200);
|
2018-12-02 11:42:06 +01:00
|
|
|
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2019-01-21 15:06:49 +01:00
|
|
|
public function show(ShowClientRequest $request, Client $client)
|
2018-10-17 14:26:27 +02:00
|
|
|
{
|
2018-11-21 09:28:07 +01:00
|
|
|
|
2019-01-25 11:47:23 +01:00
|
|
|
$data = [
|
|
|
|
'client' => $client,
|
|
|
|
'company' => auth()->user()->company()
|
|
|
|
];
|
|
|
|
|
|
|
|
return view('client.show', $data);
|
|
|
|
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2018-11-12 08:52:20 +01:00
|
|
|
public function edit(EditClientRequest $request, Client $client)
|
2018-10-17 14:26:27 +02:00
|
|
|
{
|
2018-11-07 06:22:36 +01:00
|
|
|
$data = [
|
|
|
|
'client' => $client,
|
2018-12-13 00:23:21 +01:00
|
|
|
'settings' => [],
|
|
|
|
'pills' => $this->makeEntityTabMenu(Client::class),
|
2019-01-25 11:47:23 +01:00
|
|
|
'hashed_id' => $this->encodePrimarykey($client->id),
|
|
|
|
'countries' => Country::all(),
|
|
|
|
'company' => auth()->user()->company()
|
2018-11-07 06:22:36 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return view('client.edit', $data);
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
2018-11-27 07:59:16 +01:00
|
|
|
* @param App\Models\Client $client
|
2018-10-17 14:26:27 +02:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2018-11-22 12:12:41 +01:00
|
|
|
public function update(UpdateClientRequest $request, Client $client)
|
2018-10-17 14:26:27 +02:00
|
|
|
{
|
2018-11-27 07:59:16 +01:00
|
|
|
$client = UpdateClient::dispatchNow($request, $client);
|
2019-01-25 11:47:23 +01:00
|
|
|
//$client->load('contacts', 'primary_contact');
|
2018-11-10 14:24:36 +01:00
|
|
|
|
2019-01-25 11:47:23 +01:00
|
|
|
return response()->json($client->fresh(), 200);
|
2018-11-10 14:24:36 +01:00
|
|
|
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
2018-10-29 04:16:17 +01:00
|
|
|
|
2019-01-19 11:35:21 +01:00
|
|
|
/**
|
|
|
|
* Perform bulk actions on the list view
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2019-01-16 10:28:06 +01:00
|
|
|
public function bulk()
|
2019-01-13 11:42:03 +01:00
|
|
|
{
|
2019-01-21 15:06:49 +01:00
|
|
|
|
2019-01-19 11:35:21 +01:00
|
|
|
$action = request()->input('action');
|
|
|
|
$ids = request()->input('ids');
|
|
|
|
|
2019-01-20 06:00:50 +01:00
|
|
|
$clients = Client::withTrashed()->find($ids);
|
2019-01-19 11:35:21 +01:00
|
|
|
|
|
|
|
$clients->each(function ($client, $key) use($action){
|
2019-01-21 15:06:49 +01:00
|
|
|
|
|
|
|
if(auth()->user()->can('edit', $client))
|
|
|
|
ActionEntity::dispatchNow($client, $action);
|
|
|
|
|
2019-01-19 11:35:21 +01:00
|
|
|
});
|
2019-01-21 15:06:49 +01:00
|
|
|
|
|
|
|
//todo need to return the updated dataset
|
|
|
|
return response()->json('success', 200);
|
|
|
|
|
2019-01-13 11:42:03 +01:00
|
|
|
}
|
|
|
|
|
2019-01-21 15:06:49 +01:00
|
|
|
|
|
|
|
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|