ldc-crm/app/Http/Controllers/CustomerController.php

106 lines
3.9 KiB
PHP
Raw Normal View History

2020-12-01 17:02:10 +01:00
<?php
namespace App\Http\Controllers;
use App\Models\Brand;
use App\Models\Customer;
use App\Models\CustomerInfo;
2020-12-01 17:02:10 +01:00
use App\Models\CustomerLogin;
class CustomerController extends Controller
{
public function index()
{
2020-12-01 17:02:10 +01:00
$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'
));
2020-12-01 17:02:10 +01:00
}
2021-07-22 15:14:07 +02:00
public function show(Customer $customer, Brand $brand)
{
$customerInfos = CustomerInfo::where('customer_id', $customer->id)
2021-07-22 15:14:07 +02:00
->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'
));
2020-12-01 17:02:10 +01:00
}
2021-07-22 15:14:07 +02:00
public function filter($texte, Brand $brand = null)
{
2021-07-22 15:14:07 +02:00
if ($brand) {
2020-12-01 17:02:10 +01:00
$customers = Customer::join('customer_infos', 'customers.id', '=', 'customer_infos.customer_id')
->join('customer_logins', 'customers.id', '=', 'customer_logins.customer_id')->select(
2021-08-06 16:12:41 +02:00
'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 . '%'],
2021-07-22 15:14:07 +02:00
['customer_infos.brand_id', $brand->id],
['customer_logins.brand_id', $brand->id]
2021-03-14 10:45:42 +01:00
])->orWhere([
['email', 'like', '%' . $texte . '%'],
2021-07-22 15:14:07 +02:00
['customer_infos.brand_id', $brand->id],
['customer_logins.brand_id', $brand->id]
2021-03-14 10:45:42 +01:00
])->get();
2020-12-01 17:02:10 +01:00
} else {
2021-07-22 15:14:07 +02:00
$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');
2021-07-22 15:14:07 +02:00
})->select(
'customer_infos.customer_id',
'customer_infos.firstname',
'customer_infos.lastname',
'customer_infos.opt_in',
'customers.email',
'customer_infos.brand_id',
2021-08-06 16:12:41 +02:00
'customer_logins.last_connection'
2021-07-22 15:14:07 +02:00
)->where('lastname', 'like', '%' . $texte . '%')
->orWhere('email', 'like', '%' . $texte . '%')->get();
2020-12-01 17:02:10 +01:00
}
return response()->json($customers);
}
2021-08-02 17:22:56 +02:00
2021-08-23 20:00:07 +02:00
public function delete(Customer $customer, Brand $brand)
2021-08-02 17:22:56 +02:00
{
$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é.");
2021-08-02 17:22:56 +02:00
}
2020-12-01 17:02:10 +01:00
}