authorizeResource(Customer::class); } /** * Display a listing of the resource. * * @param \App\Models\Customer $customer * @return \Illuminate\Http\Response */ public function edit(Customer $customer) { return view('fo.pro.customer.edit', [ 'customer' => $customer ]); } /** * Update the specified resource in storage. * * @param \App\Http\Requests\Fo\Customers\UpdateInfosRequest $request * @param \App\Models\Customer $customer * @return \Illuminate\Http\Response */ public function update(UpdateInfosRequest $request, Customer $customer) { return DB::transaction(function () use ($request, $customer) { $data = \array_merge($request->validated(), ['tc_id' => $request->post('tc_id')]); if ($this->updateCrmCustomerInfo($customer, $data)) { // * envoi du mail de modification $email = "contact.jeusselin@jeusselin.fr"; // TODO : should be an app setting. Mail::to($email)->send(new InformProChangeInfos($customer)); return back()->with('success', trans('Vos informations ont bien été enregistrées.')); } return back()->withInput()->with( 'faillure', trans( 'Une erreur est survenue, merci de réessayer ultérieurement.' ) ); }); } /** * Customer change password view * * @param \App\Models\Customer $customer * @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory */ public function password(Customer $customer) { $this->authorize('updatePassword', $customer); return view('fo.pro.customer.change-mdp', ['customer' => $customer]); } /** * Changement de mot de passe depuis le profil * * @param \App\Http\Requests\Fo\Customers\UpdatePasswordRequest $request * @param \App\Models\Customer $customer * @return \Illuminate\Http\RedirectResponse */ public function updatePassword(UpdatePasswordRequest $request, Customer $customer) { $this->authorize('updatePassword', $customer); if ($this->updateCrmCustomerInfo($customer, ['password' => $request->post('password')])) { event(new PasswordReset($customer)); return back()->with('success', trans('Vos informations ont bien été enregistrées.')); } return back()->withInput()->with( 'faillure', trans('Une erreur est survenue, merci de réessayer ultérieurement.') ); } /** * Updates user info * * @param \App\Models\Customer $customer * @param array $data * @return boolean * @throws \Illuminate\Validation\ValidationException If something failed. */ private function updateCrmCustomerInfo(Customer $customer, array $data) { return DB::transaction(function () use ($customer, $data) { try { /** @var \LDC\LDCCRM\Models\Customer */ $crmCustomer = $customer->apiData; if (\array_key_exists('email', $data)) { $customer->email = $data['email']; $customer->saveOrFail(); } $crmCustomer->fill($data); $saved = $crmCustomer->save(); $customer->api_data->fill($data); $customer->fill(\array_intersect_key( $data, \array_flip($customer->getFillable()), )); if (Arr::has($data, 'tc_id')) { // * Theses props arent fillable $customer->tc_id = \intval(Arr::get($data, 'tc_id')); } $customer->updateApiData(new CustomerCrmResource($customer)); $customer->saveOrFail(); return $saved; } catch (CrmValidationException $e) { throw ValidationException::withMessages([ 'error' => $e->getMessage() ]); } catch (CrmException $e) { return false; } catch (ApiErrorException $e) { if ($e->getApiError()->equals(ApiError::validationFailed)) { throw ValidationException::withMessages([ 'error' => $e->getMessage() ]); } report($e); return false; } //end try }); } }