63 lines
2.9 KiB
PHP
63 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\CustomerDeleteRequest;
|
|
use App\Models\Brand;
|
|
use App\Models\Customer;
|
|
use App\Models\CustomerInfos;
|
|
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($id, $brandId) {
|
|
$customerInfos = CustomerInfos::where([['customer_id', $id], ['brand_id', $brandId]])->first();
|
|
$customerLogin = CustomerLogin::where([['customer_id', $id], ['brand_id', $brandId]])->first();
|
|
if (!$customerInfos)
|
|
abort(404);
|
|
return view('customers.show', compact('customerInfos', 'customerLogin'));
|
|
}
|
|
|
|
public function filter($texte, $brandId = null) {
|
|
if ($brandId !== null) {
|
|
$customers = Customer::join('customer_infos', 'customers.id', '=', 'customer_infos.customer_id')
|
|
->join('customer_login', 'customers.id', '=', 'customer_login.customer_id')
|
|
->select('customer_infos.customer_id', 'customer_infos.firstname', 'customer_infos.lastname', 'customer_infos.opt_in', 'customers.email', 'customer_infos.brand_id', 'customer_login.last_connection')
|
|
->where([['lastname', 'like', '%' . $texte . '%'], ['customer_infos.brand_id', $brandId], ['customer_login.brand_id', $brandId]])
|
|
->orWhere([['email', 'like', '%' . $texte . '%'], ['customer_infos.brand_id', $brandId], ['customer_login.brand_id', $brandId]])
|
|
->get();
|
|
} else {
|
|
$customers = CustomerInfos::join('customers', 'customers.id', '=', 'customer_infos.customer_id')
|
|
->join('customer_login', function($join) {
|
|
$join->on('customer_login.customer_id', '=', 'customer_infos.customer_id');
|
|
$join->on('customer_login.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_login.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é.");
|
|
}
|
|
}
|