File manager - Edit - /var/www/html/portal/app/Http/Controllers/CadastroDeAmbulantes/RepresentantesLegaisController.php
Back
<?php namespace App\Http\Controllers\CadastroDeAmbulantes; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use App\Http\Requests\CadastroDeAmbulantes\RepresentantesLegaisRequest; use App\Models\CadastroDeAmbulantes\Ambulante; use App\Models\User; use App\Models\Endereco; class RepresentantesLegaisController extends Controller { public function index(Ambulante $ambulante) { return view('admin.cadastroDeAmbulantes.ambulantes.forms', [ "paises" => $this->__paises(), "ocupacoes" => $this->__ocupacoes(), "ambulante" => $ambulante, "representante" => $ambulante?->representante ?? NULL, "endereco" => $ambulante->representante?->endereco ?? NULL, ]); } public function store(RepresentantesLegaisRequest $request, Ambulante $ambulante) { $request->validated(); try { if ($request->has('checkRepresentanteLegal')) { if (!$this->validaCPF($request->cpfRepresentanteLegal)) { return redirect() ->back() ->withInput() ->with('error', 'O número de CPF informado não é válido!'); } if ($this->clearCPF($ambulante->proprietario->cpf) == $this->clearCPF($request->cpfRepresentanteLegal)) { return redirect() ->back() ->withInput() ->with('error', 'O número de CPF do Representante Legal não pode ser o mesmo e-mail do Proprietário!'); } DB::beginTransaction(); $fullnameRepresentanteLegal = $this->resolveFullname($request->fullNameRepresentanteLegal); $representanteLegal = User::where("cpf", $this->clearCPF($request->cpfRepresentanteLegal))->first(); if (isset($representanteLegal->id)) { $representanteLegal->fill([ "firstName" => $fullnameRepresentanteLegal['firstName'], "lastName" => $fullnameRepresentanteLegal['lastName'], "phoneNumber" => $request->phoneNumberRepresentanteLegal, "email" => $request->emailRepresentanteLegal, "photo" => $this->getPathImageUserField($request, 'photoRepresentanteLegal', $representanteLegal), ]); $representanteLegal->save(); } else { $representanteLegal = User::create([ "structure_id" => null, "firstName" => $fullnameRepresentanteLegal['firstName'], "lastName" => $fullnameRepresentanteLegal['lastName'], "cpf" => $this->clearCPF($request->cpfRepresentanteLegal), "phoneNumber" => $request->phoneNumberRepresentanteLegal, "email" => $request->emailRepresentanteLegal, "photo" => $this->getPathImageUserField($request, 'photoRepresentanteLegal', null), "permission" => $this->setPermissions($request->permissionRepresentanteLegal ?? []), "password" => Hash::make($request->passwordRepresentanteLegal ?? "mudar123"), "isPasswordDefault" => 1, "papers" => $this->setPapers($request->papersRepresentanteLegal ?? []), ]); } $endereco_array = [ 'user_id' => Auth::id(), 'owner_id' => $representanteLegal->id, 'domicilio' => $request->domicilioRepresentanteLegal, 'cep' => $request->cepRepresentanteLegal, 'logradouro' => $request->logradouroRepresentanteLegal, 'numero' => $request->numeroRepresentanteLegal, 'complemento' => $request->complementoRepresentanteLegal ?? null, 'cidade' => $request->cidadeRepresentanteLegal, 'bairro' => $request->bairroRepresentanteLegal, 'uf' => $request->ufRepresentanteLegal, 'latitude' => $request->latitudeRepresentanteLegal, 'longitude' => $request->longitudeRepresentanteLegal, 'comprovante' => $this->getProofOfResidence($request, "comprovanteRepresentanteLegal", $representanteLegal->endereco), ]; Endereco::updateOrCreate( ['owner_id' => $representanteLegal->id], $endereco_array ); $ambulante->fill([ "representanteLegal_id" => $representanteLegal->id, ]); $ambulante->save(); DB::commit(); return redirect() ->route('admin.cadastroDeAmbulantes.ambulantes.dadosDoComercioIndex', $ambulante->id) ->with('success', 'Representante legal cadastrado com sucesso!'); } DB::beginTransaction(); $ambulante->fill([ "representanteLegal_id" => null, ]); $ambulante->save(); DB::commit(); return redirect() ->route('admin.cadastroDeAmbulantes.ambulantes.dadosDoComercioIndex', $ambulante->id); } catch (\Exception $e) { DB::rollBack(); return redirect() ->back() ->withInput() ->with('error', 'Erro ao tentar cadastrar o representante legal: ' . $e->getMessage()); } } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.01 |
proxy
|
phpinfo
|
Settings