105 lines
3.9 KiB
PHP
105 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Brand;
|
|
use App\Models\Customer;
|
|
use App\Models\CustomerInfo;
|
|
use App\Models\CustomerLogin;
|
|
|
|
class CustomerController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$brands = Brand::select('id', 'name')->get();
|
|
$query = CustomerInfo::with(['customer']);
|
|
if (request()->brand) {
|
|
$query = $query->where('brand_id', request()->brand);
|
|
}
|
|
$customers = $query->paginate();
|
|
$modelTable = (new Customer())->getTable();
|
|
$modelClass = Customer::class;
|
|
|
|
// use a crud controller (any) to gather crudModels
|
|
$modelsClasses = (new BrandController())->getCrudModels();
|
|
return view('customer.index', compact(
|
|
'modelsClasses',
|
|
'modelClass',
|
|
'modelTable',
|
|
'customers',
|
|
'brands'
|
|
));
|
|
}
|
|
|
|
public function show(Customer $customer, Brand $brand)
|
|
{
|
|
$customerInfos = CustomerInfo::where('customer_id', $customer->id)
|
|
->where('brand_id', $brand->id)->firstOrFail();
|
|
$customerLogin = CustomerLogin::where('customer_id', $customer->id)
|
|
->where('brand_id', $brand->id)->first();
|
|
|
|
// use a crud controller (any) to gather crudModels
|
|
$modelsClasses = (new BrandController())->getCrudModels();
|
|
return view('customer.show', compact(
|
|
'modelsClasses',
|
|
'customerInfos',
|
|
'customerLogin'
|
|
));
|
|
}
|
|
|
|
public function filter($texte, Brand $brand = null)
|
|
{
|
|
if ($brand) {
|
|
$customers = Customer::join('customer_infos', 'customers.id', '=', 'customer_infos.customer_id')
|
|
->join('customer_logins', 'customers.id', '=', 'customer_logins.customer_id')->select(
|
|
'customer_infos.customer_id',
|
|
'customer_infos.firstname',
|
|
'customer_infos.lastname',
|
|
'customer_infos.opt_in',
|
|
'customers.email',
|
|
'customer_infos.brand_id',
|
|
'customer_logins.last_connection'
|
|
)->where([
|
|
['lastname', 'like', '%' . $texte . '%'],
|
|
['customer_infos.brand_id', $brand->id],
|
|
['customer_logins.brand_id', $brand->id]
|
|
])->orWhere([
|
|
['email', 'like', '%' . $texte . '%'],
|
|
['customer_infos.brand_id', $brand->id],
|
|
['customer_logins.brand_id', $brand->id]
|
|
])->get();
|
|
} else {
|
|
$customers = CustomerInfo::join('customers', 'customers.id', '=', 'customer_infos.customer_id')
|
|
->join('customer_logins', function ($join) {
|
|
$join->on('customer_logins.customer_id', '=', 'customer_infos.customer_id');
|
|
$join->on('customer_logins.brand_id', '=', 'customer_infos.brand_id');
|
|
})->select(
|
|
'customer_infos.customer_id',
|
|
'customer_infos.firstname',
|
|
'customer_infos.lastname',
|
|
'customer_infos.opt_in',
|
|
'customers.email',
|
|
'customer_infos.brand_id',
|
|
'customer_logins.last_connection'
|
|
)->where('lastname', 'like', '%' . $texte . '%')
|
|
->orWhere('email', 'like', '%' . $texte . '%')->get();
|
|
}
|
|
|
|
return response()->json($customers);
|
|
}
|
|
|
|
public function delete(Customer $customer, Brand $brand)
|
|
{
|
|
$nbBrand = $customer->infos()->count();
|
|
|
|
$customer->infos()->where('brand_id', $brand->id)->delete();
|
|
$customer->logins()->where('brand_id', $brand->id)->delete();
|
|
$customer->optInHistory()->where('brand_id', $brand->id)->delete();
|
|
|
|
if ($nbBrand === 1) {
|
|
$customer->delete();
|
|
}
|
|
|
|
return redirect()->route('customers.index')->with('success', "Compte supprimé.");
|
|
}
|
|
}
|