injection customer crm

This commit is contained in:
Olivier Legat 2021-05-05 09:08:57 +02:00
parent 9878b69f2d
commit 07bbcb3a5c
3 changed files with 55 additions and 70 deletions

View file

@ -24,10 +24,10 @@ class LoginController extends Controller
if (Auth::attempt($credentials)) {
$user = Auth::user();
if ($user->role === 1 || $user->role === 2){
if ($user->role === 1 || $user->role === 2) {
return redirect()->route('bo.dashboard');
}else{
if ($user->email_verified_at === NULL) {
} else {
if ($user->email_verified_at === null) {
Auth::logout();
return view('auth.login')->withErrors('Compte non activé.');
}

View file

@ -110,64 +110,48 @@ class CustomerController extends Controller
$password = $request->password;
$remember = $request->remember !== null ? true : false;
/** @var Customers */
$customer = Customers::where('email', $email)->first();
if ($customer) {
/** @var \App\Lib\LDCCRM\ApiV1 */
$crm = app('crmApi');
$result = $crm->loginCustomer($email, $password);
if ($result->status === "OK") {
/** @var \Illuminate\Auth\SessionGuard */
$guard = Auth::guard('customer');
$guard->login($customer, $remember);
$url = Session::pull('url.intended');
Session::forget('url.intended');
Session::save();
if (empty($url)) {
return redirect()->route('account.dashboard');
}
return redirect()->to($url);
} else {
switch ($result->code) {
case 115:
// @phpcs:ignore Generic.Files.LineLength
return back()->withErrors("Veuillez vérifier que vous avez correctement rempli les champs obligatoires.");
case 116:
Mail::to($email)->send(new RegisterConfirmMail($customer, null));
// @phpcs:ignore Generic.Files.LineLength
return back()->withErrors("Votre compte n'est pas activé, vérifier dans vos emails pour y trouver le mail de confirmation. Un lien d'activation vous a été renvoyer.");
case 117 || 118 || 119:
return back()->withErrors("Email ou mot de passe incorrect.");
default:
return back()->withErrors("Une erreur est survenue, merci de réessayer ultérieurement.");
}
}
} else {
//check if customer exists in CRM to auto create
/** @var \App\Lib\LDCCRM\ApiV1 */
$crm = app('crmApi');
$result = $crm->loginCustomer($email, $password);
if ($result->status === "OK") {
//check CRM
/** @var \App\Lib\LDCCRM\ApiV1 */
$crm = app('crmApi');
$result = $crm->loginCustomer($email, $password);
if ($result->status === "OK") {
//exists in crm
//do we have the customer in local DB ?
/** @var Customers */
$customer = Customers::where('email', $email)->first();
if (!$customer) {
//create user
$customer = new Customers($request->all());
$customer->crm_id = $result->user->crm_id;
$customer->hashcode = $result->user->hashcode;
$customer->save();
//connect
/** @var \Illuminate\Auth\SessionGuard */
$guard = Auth::guard('customer');
$guard->login($customer, $remember);
$url = Session::pull('url.intended');
Session::forget('url.intended');
Session::save();
if (empty($url)) {
return redirect()->route('account.dashboard');
}
return redirect()->to($url);
} else {
//n'existe pas en CRM
}
return back()->withErrors("Email ou mot de passe incorrect.");
//connect and redirect
/** @var \Illuminate\Auth\SessionGuard */
$guard = Auth::guard('customer');
$guard->login($customer, $remember);
$url = Session::pull('url.intended');
Session::forget('url.intended');
Session::save();
if (empty($url)) {
return redirect()->route('account.dashboard');
}
return redirect()->to($url);
} else {
//doesn't exist or is invalid
switch ($result->code) {
case 115:
// @phpcs:ignore Generic.Files.LineLength
return back()->withErrors("Veuillez vérifier que vous avez correctement rempli les champs obligatoires.");
case 116:
Mail::to($email)->send(new RegisterConfirmMail($customer, null));
// @phpcs:ignore Generic.Files.LineLength
return back()->withErrors("Votre compte n'est pas activé, vérifier dans vos emails pour y trouver le mail de confirmation. Un lien d'activation vous a été renvoyer.");
case 117 || 118 || 119:
return back()->withErrors("Email ou mot de passe incorrect.");
default:
return back()->withErrors("Une erreur est survenue, merci de réessayer ultérieurement.");
}
}
}
@ -389,7 +373,7 @@ class CustomerController extends Controller
public function orderDetail($reference)
{
$order = Order::where('reference', '=', $reference)
$order = Order::where('reference', '=', $reference)
->where('customer_id', Auth::guard('customer')->user()->id)
->firstOrFail();
$magasin = Magasin::first();

View file

@ -28,34 +28,35 @@ class ProductsController extends Controller
public function range(Request $request, $range_slug)
{
$range = Range::where('slug', $range_slug)->first();
if(!$range || !$range->status) {
if (!$range || !$range->status) {
abort(404);
}
$products = Product::where('range_id', $range->id)->where('status',1)->paginate(9);
$products = Product::where('range_id', $range->id)->where('status', 1)->paginate(9);
//find promo within range
$offres = Product::where('range_id', $range->id)->where('status',1)->has('promotions')->with('promotions')->get();
return view('range', compact('range','products','offres'));
$offres = Product::where('range_id', $range->id)->where('status', 1)->has('promotions')->with('promotions')->get();
return view('range', compact('range', 'products', 'offres'));
}
/**
* page produit
*/
public function product(Request $request, $product_slug) {
public function product(Request $request, $product_slug)
{
$product = Product::where('slug', $product_slug)->first();
if(!$product || !$product->status) {
if (!$product || !$product->status) {
abort(404);
}
$product->additionnalProductInfo = $product->additionnalProductInfo($product->product_type);
if ($product->additionnalProductInfo === null && !empty($product_type)) {
//n'existe pas on renseigne un vide
$product_type = $product->product_type;
$modelClass = 'App\Models\\' . ucfirst($product_type);
$product_type = $product->product_type;
$modelClass = 'App\Models\\' . ucfirst($product_type);
$product->$product_type = new $modelClass();
}
$associated = $product->products;
return view ('product', compact('product', 'associated'));
return view('product', compact('product', 'associated'));
}
/**
@ -69,13 +70,13 @@ class ProductsController extends Controller
if (!empty($filter)) {
$products = Product::where('status', 1)
->where('name', 'like', '%'.$filter.'%')
->where('name', 'like', '%' . $filter . '%')
->paginate(12);
} else {
$products = Product::where('status', 1)->paginate(12);
}
$search_title = 'Recherche : ' .$filter;
$search_title = 'Recherche : ' . $filter;
return view('search', compact('products', 'search_title', 'filter'));
}
@ -86,12 +87,12 @@ class ProductsController extends Controller
public function tagFiltered(Request $request, $tag_slug)
{
$products = Product::where('status', 1)
->whereHas('tags', function($q) use($tag_slug) {
->whereHas('tags', function ($q) use ($tag_slug) {
$q->where('slug', '=', $tag_slug);
})
->paginate(12);
$tag = Tag::where('slug', $tag_slug)->first();
$tag = Tag::where('slug', $tag_slug)->first();
$search_title = $tag->label;
return view('search', compact('products', 'search_title', 'tag'));