198 lines
6.7 KiB
PHP
198 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\NotificationType;
|
|
use App\Http\Requests\UpdateInfosRequest;
|
|
use App\Models\Articles;
|
|
use App\Models\ArticlesCategories;
|
|
use App\Models\Customers;
|
|
use App\Models\Notifications;
|
|
use App\Models\Reductions;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use LDCCRM\ApiV1;
|
|
|
|
class ClubController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$reductions = Reductions::where([
|
|
['end_date_broadcast', '>', now()],
|
|
['status', 1]
|
|
])->get();
|
|
|
|
$articles = Articles::where('status', 1)->orderBy('updated_at', 'DESC')->get();
|
|
$categories = ArticlesCategories::whereHas('articles', function ($q) {
|
|
$q->where('status', 1);
|
|
})->get();
|
|
|
|
$montantReductions = 0.0;
|
|
foreach ($reductions as $reduc)
|
|
$montantReductions += $reduc->face_value;
|
|
|
|
$nbCustomers = Customers::count();
|
|
|
|
$notifications = 0;
|
|
if (Auth::guard('customer')->check()) {
|
|
$notifications = $this->getNotifications(Auth::guard('customer')->user());
|
|
}
|
|
|
|
// Cas de catégorie d'article
|
|
$category = $request->category !== null ? $request->category : null;
|
|
|
|
return view('front.club.index', compact('montantReductions', 'nbCustomers', 'articles', 'categories', 'notifications', 'category'));
|
|
}
|
|
|
|
public function infos()
|
|
{
|
|
$customer = Auth::guard('customer')->user();
|
|
if (!$customer)
|
|
abort(404);
|
|
|
|
$crm = app('crmApi');
|
|
$crmCustomer = $crm->getCustomer($customer->crm_id);
|
|
if ($crmCustomer->status === "KO")
|
|
abort(503);
|
|
$crmInfos = $crmCustomer->user;
|
|
$birthDate = explode('-', $crmInfos->birth_date);
|
|
|
|
$notifications = $this->getNotifications($customer);
|
|
return view('front.club.informations', compact('customer', 'notifications', 'crmInfos', 'birthDate'));
|
|
}
|
|
|
|
/**
|
|
* Send a request to the CRM to change the given customer's informations
|
|
* @param Request $request
|
|
*/
|
|
public function updateInfos(UpdateInfosRequest $request)
|
|
{
|
|
// TODO : Activer le changement d'email quand ce sera possible dans la CRM
|
|
$crm = app('crmApi');
|
|
|
|
$crmCustomer = $crm->updateCustomer(
|
|
Auth::guard('customer')->user()->crm_id,
|
|
$request->firstname,
|
|
$request->lastname,
|
|
$request->optin !== null ? 1 : 0,
|
|
$request->year . '-' . $request->month . '-' . $request->day,
|
|
$request->phone,
|
|
$request->address,
|
|
null,
|
|
$request->city,
|
|
$request->postcode,
|
|
null
|
|
);
|
|
|
|
if ($crmCustomer->status === "KO")
|
|
return redirect()->route('account.infos')->withErrors(__("Une erreur est survenue, merci de réessayer ultérieurement."));
|
|
return redirect()->route('account.infos')->with('success', __("Vos informations ont bien été enregistrées."));
|
|
}
|
|
|
|
/**
|
|
* Retrieve the number of notifications the current authenticated user has
|
|
* @param Customer $customer The user whose number of notifications we want to retrieve
|
|
*/
|
|
private function getNotifications()
|
|
{
|
|
return Auth::guard('customer')->check() ? Notifications::count() - Auth::guard('customer')->user()->notifications_read()->count() : 0;
|
|
}
|
|
|
|
/**
|
|
* Call the notifications page
|
|
*/
|
|
public function notifications()
|
|
{
|
|
$notificationsList = Notifications::orderBy('created_at', 'DESC')->with('notifiable')->get();
|
|
$notificationsIds = Notifications::pluck('id')->toArray();
|
|
Auth::guard('customer')->user()->notifications_read()->sync($notificationsIds);
|
|
$notifications = $this->getNotifications();
|
|
return view('front.club.notifications', compact('notifications', 'notificationsList'));
|
|
}
|
|
|
|
/**
|
|
* Call the cart page
|
|
*/
|
|
public function cart()
|
|
{
|
|
$products = Auth::guard('customer')->user()->products;
|
|
$notifications = $this->getNotifications();
|
|
|
|
return view('front.club.cart', compact('products', 'notifications'));
|
|
}
|
|
|
|
/**
|
|
* Call the favorites recipes page
|
|
*/
|
|
public function favoritesRecipes()
|
|
{
|
|
$recipes = Auth::guard('customer')->user()->recipes;
|
|
$notifications = $this->getNotifications();
|
|
|
|
return view('front.club.favorites-recipes', compact('recipes', 'notifications'));
|
|
}
|
|
|
|
/**
|
|
* Call the reductions page
|
|
*/
|
|
public function reductions()
|
|
{
|
|
$notifications = $this->getNotifications();
|
|
// TODO: get url from MesBr class
|
|
/** @var \App\Lib\MesBR */
|
|
$mesBR = app('mesBr');
|
|
$urlReductions = "https://www.mesbr.eu/?refsite=" . config('app.REF_SITE') . "&refcamp=" . config('app.REF_CAMP') . "&hash=" . Auth::guard('customer')->user()->hashcode . "&now=" . time();
|
|
return view('front.club.reductions', compact('urlReductions', 'notifications'));
|
|
}
|
|
|
|
/**
|
|
* Call the article page with a slug for the article to print
|
|
* @param $slug Slug of the article
|
|
* @return View The view that the article have to print
|
|
*/
|
|
public function article($slug)
|
|
{
|
|
$notifications = $this->getNotifications();
|
|
|
|
$article = Articles::where([['slug', $slug], ['status', 1]])->first();
|
|
if (!$article)
|
|
abort(404);
|
|
|
|
// Check si le customer connecté a déjà répondu
|
|
$alreadyAnswered = false;
|
|
if (Auth::guard('customer')->check()) {
|
|
$answersIds = $article->answers()->pluck('id')->toArray();
|
|
$alreadyAnswered = Auth::guard('customer')->user()->surveys_answers()->whereIn('surveys_answer_id', $answersIds)->count() > 0 ? true : false;
|
|
}
|
|
|
|
$linkedArticles = Articles::where([['status', 1], ['category_id', $article->category_id], ['id', '<>', $article->id]])->get();
|
|
|
|
$categories = ArticlesCategories::whereHas('articles', function ($q) {
|
|
$q->where('status', 1);
|
|
})->get();
|
|
|
|
return view('front.club.article', compact('article', 'categories', 'notifications', 'linkedArticles', 'alreadyAnswered'));
|
|
}
|
|
|
|
/**
|
|
* Handle the request of a customer that answered a survey
|
|
* @param $request Request
|
|
* @return View View to go to
|
|
*/
|
|
public function answerSurvey(Request $request)
|
|
{
|
|
$answers = $request->answers;
|
|
|
|
if (empty($answers))
|
|
return back()->withErrors(__("Votre réponse est incomplète. Merci de corriger"));
|
|
|
|
$customer = Auth::guard('customer')->user();
|
|
if (is_array($answers))
|
|
$customer->surveys_answers()->syncWithoutDetaching($answers);
|
|
else
|
|
$customer->surveys_answers()->syncWithoutDetaching($answers);
|
|
|
|
return back();
|
|
}
|
|
}
|