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;
|
2018-11-03 02:01:40 +01:00
|
|
|
use App\Http\Requests\Client\EditClientRequest;
|
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;
|
2018-10-29 04:16:17 +01:00
|
|
|
use App\Models\Client;
|
2018-12-02 11:42:06 +01:00
|
|
|
use App\Models\ClientContact;
|
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;
|
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
|
|
|
{
|
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 = [
|
|
|
|
'datatable' => $this->clientDatatable->buildOptions()
|
|
|
|
];
|
|
|
|
|
|
|
|
return view('client.vue_list', $data);
|
2018-12-24 01:45:55 +01:00
|
|
|
/*
|
2018-10-29 04:16:17 +01:00
|
|
|
if (request()->ajax()) {
|
2018-10-17 14:26:27 +02:00
|
|
|
|
2018-11-02 11:54:46 +01:00
|
|
|
/*
|
|
|
|
$clients = Client::query('clients.*', DB::raw("CONCAT(client_contacts.first_name,' ',client_contacts.last_name) as full_name"), 'client_contacts.email')
|
2018-10-29 04:16:17 +01:00
|
|
|
->leftJoin('client_contacts', function($leftJoin)
|
|
|
|
{
|
|
|
|
$leftJoin->on('clients.id', '=', 'client_contacts.client_id')
|
|
|
|
->where('client_contacts.is_primary', '=', true);
|
|
|
|
});
|
2018-11-02 11:54:46 +01:00
|
|
|
*/
|
|
|
|
|
2018-12-24 01:45:55 +01:00
|
|
|
/*
|
|
|
|
$clients = Client::query();
|
2018-10-29 04:16:17 +01:00
|
|
|
|
|
|
|
return DataTables::of($clients->get())
|
2018-11-02 11:54:46 +01:00
|
|
|
->addColumn('full_name', function ($clients) {
|
|
|
|
return $clients->contacts->where('is_primary', true)->map(function ($contact){
|
|
|
|
return $contact->first_name . ' ' . $contact->last_name;
|
|
|
|
})->all();
|
|
|
|
})
|
|
|
|
->addColumn('email', function ($clients) {
|
|
|
|
return $clients->contacts->where('is_primary', true)->map(function ($contact){
|
|
|
|
return $contact->email;
|
|
|
|
})->all();
|
|
|
|
})
|
2018-10-29 04:16:17 +01:00
|
|
|
->addColumn('action', function ($client) {
|
2018-11-02 11:54:46 +01:00
|
|
|
return '<a href="/clients/'. $client->present()->id .'/edit" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a>';
|
2018-10-29 04:16:17 +01:00
|
|
|
})
|
|
|
|
->addColumn('checkbox', function ($client){
|
|
|
|
return '<input type="checkbox" name="bulk" value="'. $client->id .'"/>';
|
|
|
|
})
|
|
|
|
->rawColumns(['checkbox', 'action'])
|
|
|
|
->make(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$builder->addAction();
|
|
|
|
$builder->addCheckbox();
|
|
|
|
|
|
|
|
$html = $builder->columns([
|
|
|
|
['data' => 'checkbox', 'name' => 'checkbox', 'title' => '', 'searchable' => false, 'orderable' => false],
|
|
|
|
['data' => 'name', 'name' => 'name', 'title' => trans('texts.name'), 'visible'=> true],
|
|
|
|
['data' => 'full_name', 'name' => 'full_name', 'title' => trans('texts.contact'), 'visible'=> true],
|
|
|
|
['data' => 'email', 'name' => 'email', 'title' => trans('texts.email'), 'visible'=> true],
|
|
|
|
['data' => 'created_at', 'name' => 'created_at', 'title' => trans('texts.date_created'), 'visible'=> true],
|
|
|
|
['data' => 'last_login', 'name' => 'last_login', 'title' => trans('texts.last_login'), 'visible'=> true],
|
|
|
|
['data' => 'balance', 'name' => 'balance', 'title' => trans('texts.balance'), 'visible'=> true],
|
|
|
|
['data' => 'action', 'name' => 'action', 'title' => '', 'searchable' => false, 'orderable' => false],
|
|
|
|
]);
|
|
|
|
|
2018-11-02 11:54:46 +01:00
|
|
|
$builder->ajax([
|
|
|
|
'url' => route('clients.index'),
|
|
|
|
'type' => 'GET',
|
|
|
|
'data' => 'function(d) { d.key = "value"; }',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$data['html'] = $html;
|
2018-10-29 04:16:17 +01:00
|
|
|
|
2018-11-02 11:54:46 +01:00
|
|
|
return view('client.list', $data);
|
2018-12-24 01:45:55 +01:00
|
|
|
*/
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
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 = "";
|
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 = [
|
|
|
|
'client' => $client,
|
|
|
|
'hashed_id' => ''
|
|
|
|
];
|
|
|
|
|
|
|
|
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)
|
|
|
|
];
|
2018-12-07 11:57:20 +01:00
|
|
|
*/
|
|
|
|
Log::error(print_r($client,1));
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
2018-11-21 09:28:07 +01:00
|
|
|
$client = Client::find(2);
|
|
|
|
$client->load('contacts', 'primary_contact');
|
|
|
|
|
|
|
|
return response()->json($client, 200);
|
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-10 14:24:36 +01: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),
|
2018-11-27 07:59:16 +01:00
|
|
|
'hashed_id' => $this->encodePrimarykey($client->id)
|
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);
|
2018-11-21 09:28:07 +01:00
|
|
|
$client->load('contacts', 'primary_contact');
|
2018-11-10 14:24:36 +01:00
|
|
|
|
|
|
|
return response()->json($client, 200);
|
|
|
|
|
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-13 11:42:03 +01:00
|
|
|
public function builk()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|