204 lines
7.1 KiB
PHP
204 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\NotificationType;
|
|
use App\Http\Requests\SurveyAnswersRequest;
|
|
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;
|
|
|
|
class ClubController extends Controller
|
|
{
|
|
public function index(ArticlesCategories $category)
|
|
{
|
|
$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', true);
|
|
})->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 = $category->id ?? 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);
|
|
|
|
/** @var \App\Lib\LDCCRM\ApiV1 */
|
|
$crm = app('crmApi');
|
|
$crmCustomer = $crm->getCustomer($customer->crm_id);
|
|
// TODO : Gérer l'erreur de non connexion a la CRM dans la vue
|
|
if ($crmCustomer->status === "KO")
|
|
return back()->withErrors(__("Une erreur est survenue, merci de réessayer ultérieurement."));
|
|
$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
|
|
/** @var \App\Lib\LDCCRM\ApiV1 */
|
|
$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(Articles $publishedArticle)
|
|
{
|
|
$notifications = $this->getNotifications();
|
|
|
|
// Check si le customer connecté a déjà répondu
|
|
$alreadyAnswered = false;
|
|
if (Auth::guard('customer')->check()) {
|
|
$answersIds = $publishedArticle->answers()->pluck('id')->toArray();
|
|
/** @var \App\Models\Customers */
|
|
$customer = Auth::guard('customer')->user();
|
|
$alreadyAnswered = $customer->surveys_answers()
|
|
->whereIn('surveys_answer_id', $answersIds)->count() > 0 ? true : false;
|
|
}
|
|
|
|
$linkedArticles = Articles::where([
|
|
['status', true], ['category_id', $publishedArticle->category_id], ['id', '<>', $publishedArticle->id]
|
|
])->get();
|
|
|
|
$categories = ArticlesCategories::whereHas('articles', function ($q) {
|
|
$q->where('status', 1);
|
|
})->get();
|
|
|
|
return view('front.club.article', compact(
|
|
'publishedArticle',
|
|
'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(SurveyAnswersRequest $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();
|
|
}
|
|
}
|