83 lines
3.3 KiB
PHP
83 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\CustomerDeleteRequest;
|
|
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();
|
|
return view('customers.index', compact('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();
|
|
return view('customers.show', compact('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(CustomerDeleteRequest $request, 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')->with('success', "Compte supprimé.");
|
|
}
|
|
}
|