ldc-legaulois/app/Http/Controllers/ClubController.php
2021-03-03 12:02:44 +01:00

126 lines
4.5 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() {
$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());
}
return view('front.club.index', compact('montantReductions', 'nbCustomers', 'articles', 'categories', 'notifications'));
}
public function infos() {
$customer = Auth::guard('customer')->user();
if (!$customer)
abort(404);
$crm = new ApiV1(config('crm.CRM_ENDPOINT'), config('crm.CRM_API_TOKEN'));
$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
$crm = new ApiV1(config('crm.CRM_ENDPOINT'), config('crm.CRM_API_TOKEN'));
$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 Notifications::count() - Auth::guard('customer')->user()->notifications_read()->count();
}
/**
* 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'));
}
}