ldc-gen-game-web/app/Http/Controllers/Fo/CustomerController.php

176 lines
6.5 KiB
PHP
Raw Normal View History

2023-12-08 18:55:55 +01:00
<?php
namespace App\Http\Controllers\Fo;
use App\Http\Controllers\Controller;
use App\Http\Requests\Fo\Customers\VerifyInformationsRequest;
use App\Http\Requests\Fo\Customers\SingleOptinRequest;
2025-10-27 16:23:02 +01:00
use App\Repositories\Fo\CrmRepository;
2023-12-13 18:45:43 +01:00
use Illuminate\Contracts\Database\Eloquent\Builder;
2023-12-08 18:55:55 +01:00
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\ValidationException as IlluminateValidationException;
use LDC\LDCCRM\Enums\ApiError;
use LDC\LDCCRM\Exceptions\ApiErrorException;
use LDC\LDCCRM\Exceptions\CrmException;
use LDC\LDCCRM\Exceptions\ValidationException as CrmValidationException;
use LDC\LDCCRM\Models\SingleOptin;
2023-12-08 18:55:55 +01:00
class CustomerController extends Controller
{
2025-10-27 16:23:02 +01:00
/** @var \App\Repositories\Fo\CrmRepository $crmRepository */
private $crmRepository;
/**
* ParticipateController
*
* @return void
*/
public function __construct()
{
$this->crmRepository = new CrmRepository();
}
2023-12-08 18:55:55 +01:00
/**
* Verify profil info form
*
* @return \Illuminate\Contracts\View\View
*/
public function profilVerify(): View
{
2023-12-13 18:45:43 +01:00
// ! Pas de Gate/Policies Ici volontairement, vu avec Vincent
2023-12-08 18:55:55 +01:00
/** @var \App\Models\Customer */
$customer = auth('frontend')->user();
$crmCustomer = $customer->api_data;
return view('theme::auth.profil-verify', compact('crmCustomer'));
}
/**
* Profile verify
*
* @param \App\Http\Requests\Fo\Customers\VerifyInformationsRequest $request
* @return \Illuminate\Http\RedirectResponse
* @throws IlluminateValidationException In case of any error validation or within the crm.
*/
public function profilVerifySubmit(VerifyInformationsRequest $request): RedirectResponse
{
2023-12-13 18:45:43 +01:00
// ! Pas de Gate/Policies Ici volontairement, vu avec Vincent
try {
return DB::transaction(function () use ($request) {
// * --- FILL AND SAVE CUSTOMER
/** @var \App\Models\Customer */
$customer = auth('frontend')->user();
// * Màj, on ne mets pas à jour le mdp ou l'adresse email, juste les champs pour mesBr
$customer->updateApiData(\array_merge(Arr::only(
$request->validated(),
[
'firstname',
'lastname',
'birth_date',
'address',
'country',
'city',
'phone',
'opt_in',
]
), ['postcode' => $request->input('postal_code')]));
Session::flash('success', trans('Vos informations ont bien été mis à jour'));
return response()->redirectToRoute('fo.participate');
});
} catch (CrmValidationException $e) {
// * Local Validation failure
throw IlluminateValidationException::withMessages([
'error' => $e->getMessage()
]);
} catch (ApiErrorException $e) {
if ($e->getApiError()->equals(ApiError::validationFailed)) {
throw IlluminateValidationException::withMessages([
'error' => $e->getMessage()
]);
}
report($e);
Log::error(__CLASS__ . '::update reached end of possibilities.');
} catch (CrmException $e) {
report($e);
Log::error(__CLASS__ . '::error occured on calling crm.');
throw IlluminateValidationException::withMessages([
'error' => trans('Une erreur est survenue, veuillez réssayer plus tard')
]);
} //end try
2024-01-31 09:03:18 +00:00
return back()->withInput()->withErrors(trans('Une erreur est survenue, merci de réessayer ultérieurement.'));
}
2023-12-13 18:45:43 +01:00
/**
* View all customer winnig participations
*
* @return \Illuminate\Contracts\View\View
*/
public function viewWinningParticipations(): View
{
$this->authorize('viewWonList', \App\Models\Participation::class);
/** @var \App\Models\Customer */
$customer = auth('frontend')->user();
2025-10-27 16:23:02 +01:00
// * Recupération des informations CRM
/** @var \LDC\LDCCRM\Models\Customer*/
$crmUser = $this->crmRepository->fetchCrmCustomer($customer);
/** @var \LDC\MESBR\MesBr */
$mesBr = app('mesBrAPI');
$brBasket = $mesBr->getBasket($crmUser);
if (!empty($brBasket['coupons'])) {
2025-11-05 19:12:03 +01:00
$canPrint = (collect($brBasket['coupons'])->sum('quantity') - collect($brBasket['coupons'])->sum('printed'));
2025-10-27 16:23:02 +01:00
}
2023-12-13 18:45:43 +01:00
$winningList = $customer->participations()
2025-10-27 16:23:02 +01:00
->with(['instantWin'])->where(function (Builder $q) {
$q->whereHas('instantWin');
2024-01-31 09:03:18 +00:00
})->get();
2023-12-13 18:45:43 +01:00
return view('theme::pages.participation.winning-participations', [
'winningList' => $winningList,
'canPrint' => $canPrint ?? null
]);
2023-12-13 18:45:43 +01:00
}
/**
* Call the CRM to add a single optin
*
* @param \App\Http\Requests\Fo\Customers\SingleOptinRequest $request
* @return string|array|null|\Illuminate\Contracts\Translation\Translator
* @throws \LDC\LDCCRM\Exceptions\ValidationException ValidationException.
* @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException.
*/
public function addOptIn(SingleOptinRequest $request)
{
try {
SingleOptin::setStatus($request->email, true);
return \response(trans("Votre inscription à la newsletter a été prise en compte"), 200);
} catch (ApiErrorException $e) {
// Il y a deja un customerInfo avec optin a true pour ce customer.
if ($e->getApiError()->equals(ApiError::singleOptinCustomerExists)) {
return trans('Vous êtes déjà inscrit avec cette adresse email');
}
if (app()->bound('sentry')) {
app('sentry')->captureException($e);
}
return \response(trans('Une erreur est survenue, merci de réessayer ultérieurement'), 404);
} catch (CrmException $e) {
if (app()->bound('sentry')) {
app('sentry')->captureException($e);
}
return \response(trans('Une erreur est survenue, merci de réessayer ultérieurement'), 404);
}
}
2023-12-08 18:55:55 +01:00
}