2024-07-01 10:22:38 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Club;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2024-07-30 16:05:27 +02:00
|
|
|
use App\Http\Requests\Club\Customers\UpdateCustomerRequest;
|
|
|
|
|
use App\Http\Requests\Club\Customers\UpdatePasswordRequest;
|
|
|
|
|
use App\Models\Customer;
|
2024-07-29 11:32:50 +02:00
|
|
|
use App\Models\Product;
|
|
|
|
|
use App\Models\Recipe;
|
2024-07-30 16:05:27 +02:00
|
|
|
use Illuminate\Auth\Events\PasswordReset;
|
2026-09-25 16:25:31 +02:00
|
|
|
use Illuminate\Contracts\View\View;
|
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
2024-07-30 16:05:27 +02:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
|
use LDC\LDCCRM\Enums\ApiError;
|
|
|
|
|
use LDC\LDCCRM\Exceptions\ApiErrorException;
|
2024-08-02 17:20:49 +02:00
|
|
|
use LDC\LDCCRM\Models\Answer;
|
2024-08-02 09:56:54 +02:00
|
|
|
use LDC\LDCCRM\Models\Question;
|
2024-07-01 10:22:38 +02:00
|
|
|
|
|
|
|
|
class CustomerController extends Controller
|
|
|
|
|
{
|
2024-07-05 10:35:26 +02:00
|
|
|
/**
|
2024-07-30 16:05:27 +02:00
|
|
|
* Display a listing of the resource.
|
2024-07-05 10:35:26 +02:00
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
public function index(): View
|
2024-07-05 10:35:26 +02:00
|
|
|
{
|
2024-07-30 16:05:27 +02:00
|
|
|
/** @var \App\Models\Customer $customer */
|
|
|
|
|
$customer = Auth::guard('club')->user();
|
|
|
|
|
/** @var \LDC\LDCCRM\Models\Customer $crmCustomer */
|
|
|
|
|
$crmCustomer = $customer->api_data;
|
|
|
|
|
$questions = Customer::cacheCrm(
|
|
|
|
|
$customer->hashcode,
|
|
|
|
|
'questions',
|
2026-09-25 16:25:31 +02:00
|
|
|
fn () => collect(Question::all())->filter(function ($question) {
|
2024-07-30 16:05:27 +02:00
|
|
|
return $question->id === intval(config('crm.question'));
|
|
|
|
|
})
|
|
|
|
|
);
|
2024-08-02 17:20:49 +02:00
|
|
|
/** @var array<int, \LDC\LDCCRM\Models\Answer> $crmAnswers */
|
|
|
|
|
$crmAnswers = [];
|
|
|
|
|
collect(Question::allWithCustomerAnswers($crmCustomer))->map(function ($question) use (&$crmAnswers) {
|
|
|
|
|
/** @var \LDC\LDCCRM\Models\Question $crmQuestion */
|
|
|
|
|
$crmQuestion = $question['question'];
|
|
|
|
|
if (intval($crmQuestion->id) === intval(config('crm.question'))) {
|
|
|
|
|
$crmAnswers = $question['customerAnswers'];
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-09-25 16:25:31 +02:00
|
|
|
|
2024-08-02 17:20:49 +02:00
|
|
|
return view('club.pages.customers.index', compact('customer', 'crmCustomer', 'questions', 'crmAnswers'));
|
2024-07-30 16:05:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
|
*/
|
|
|
|
|
public function update(
|
|
|
|
|
UpdateCustomerRequest $request,
|
2026-09-25 16:25:31 +02:00
|
|
|
): RedirectResponse {
|
2024-07-30 16:05:27 +02:00
|
|
|
return DB::transaction(function () use ($request) {
|
|
|
|
|
/** @var \App\Models\Customer $customer */
|
|
|
|
|
$customer = auth('club')->user();
|
2025-07-11 11:33:01 +02:00
|
|
|
$customer->fill(collect($request->validated())->only(['username'])->toArray());
|
2024-07-30 16:05:27 +02:00
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
$this->updateCrmCustomerInfo($customer, collect($request->validated())->except([
|
2024-10-23 16:05:53 +00:00
|
|
|
'password',
|
|
|
|
|
'ok_cgv',
|
2025-07-11 11:33:01 +02:00
|
|
|
'ok_cgu',
|
2026-09-25 16:25:31 +02:00
|
|
|
'email',
|
2024-07-30 16:05:27 +02:00
|
|
|
])->toArray())
|
|
|
|
|
) {
|
|
|
|
|
if ($customer->saveOrFail()) {
|
2024-08-02 17:20:49 +02:00
|
|
|
/** @var \LDC\LDCCRM\Models\Customer $crmCustomer */
|
|
|
|
|
$crmCustomer = $customer->api_data;
|
2026-09-25 16:25:31 +02:00
|
|
|
if (! Answer::setQuestionAnswers($crmCustomer->hashcode, $request->question_id, $request->answers)) {
|
2024-08-02 17:20:49 +02:00
|
|
|
return redirect()->route('club.customers.index')
|
|
|
|
|
->with('error', trans('Une erreur est survenue lors de l\'enregistrement de la réponse
|
|
|
|
|
à la question, merci de réessayer plus tard'));
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-02 12:56:52 +02:00
|
|
|
return redirect()->route('club.customers.index')
|
|
|
|
|
->with('success', trans('Vos informations ont bien été enregistrées.'));
|
2024-07-30 16:05:27 +02:00
|
|
|
}
|
2026-09-25 16:25:31 +02:00
|
|
|
|
2024-07-30 16:05:27 +02:00
|
|
|
return redirect()->back()
|
2024-08-02 12:56:52 +02:00
|
|
|
->with('error', trans('Une erreur est survenue, merci de réessayer ultérieurement.'));
|
2024-07-30 16:05:27 +02:00
|
|
|
}
|
2026-09-25 16:25:31 +02:00
|
|
|
|
2024-07-30 16:05:27 +02:00
|
|
|
return redirect()->back()
|
2024-08-02 12:56:52 +02:00
|
|
|
->with('error', trans('Une erreur est survenue, merci de réessayer ultérieurement.'));
|
2024-07-30 16:05:27 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates user info.
|
|
|
|
|
*
|
2026-09-25 16:25:31 +02:00
|
|
|
* @return bool
|
|
|
|
|
*
|
2024-07-30 16:05:27 +02:00
|
|
|
* @throws \Illuminate\Validation\ValidationException If something failed.
|
|
|
|
|
*/
|
|
|
|
|
private function updateCrmCustomerInfo(Customer $customer, array $data)
|
|
|
|
|
{
|
|
|
|
|
return DB::transaction(function () use ($customer, $data) {
|
|
|
|
|
try {
|
|
|
|
|
return $customer->updateApiData($data);
|
|
|
|
|
} catch (ApiErrorException $e) {
|
|
|
|
|
if ($e->getApiError()->equals(ApiError::validationFailed)) {
|
|
|
|
|
throw ValidationException::withMessages([
|
2026-09-25 16:25:31 +02:00
|
|
|
'error' => $e->getMessage(),
|
2024-07-30 16:05:27 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
report($e);
|
2026-09-25 16:25:31 +02:00
|
|
|
|
2024-07-30 16:05:27 +02:00
|
|
|
return false;
|
2026-09-25 16:25:31 +02:00
|
|
|
} // end try
|
2024-07-30 16:05:27 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update customer password.
|
|
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
public function passwordUpdate(UpdatePasswordRequest $request): RedirectResponse
|
2024-07-30 16:05:27 +02:00
|
|
|
{
|
|
|
|
|
/** @var \App\Models\Customer */
|
|
|
|
|
$customer = \auth('club')->user();
|
|
|
|
|
|
|
|
|
|
if ($this->updateCrmCustomerInfo($customer, ['password' => $request->input('password')])) {
|
|
|
|
|
event(new PasswordReset($customer));
|
2026-09-25 16:25:31 +02:00
|
|
|
|
2024-07-30 16:05:27 +02:00
|
|
|
return redirect()->back()->with('success', trans('crud.messages.has_been_updated', [
|
2026-09-25 16:25:31 +02:00
|
|
|
'model' => Str::ucfirst(trans('validation.attributes.password')),
|
2024-07-30 16:05:27 +02:00
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect()->back()->with('error', trans('crud.messages.cannot_be_updated', [
|
2026-09-25 16:25:31 +02:00
|
|
|
'model' => Str::ucfirst(trans('validation.attributes.password')),
|
2024-07-30 16:05:27 +02:00
|
|
|
]));
|
2024-07-05 10:35:26 +02:00
|
|
|
}
|
2024-07-29 11:32:50 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
public function indexProducts(): View
|
2024-07-29 11:32:50 +02:00
|
|
|
{
|
|
|
|
|
$models = Product::query()->where('published', true)->take(15)->paginate(9);
|
2026-09-25 16:25:31 +02:00
|
|
|
|
2024-07-29 11:32:50 +02:00
|
|
|
return view('club.pages.customers.favorites.products', compact('models'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
public function indexRecipes(): View
|
2024-07-29 11:32:50 +02:00
|
|
|
{
|
|
|
|
|
$models = Recipe::query()->with('products', 'consumptionMoment')
|
2024-10-23 16:05:53 +00:00
|
|
|
->where('published', true)->take(15)->paginate(9);
|
2026-09-25 16:25:31 +02:00
|
|
|
|
2024-07-29 11:32:50 +02:00
|
|
|
return view('club.pages.customers.favorites.recipes', compact('models'));
|
|
|
|
|
}
|
2024-07-01 10:22:38 +02:00
|
|
|
}
|