$now->format('d/m/Y'), 'estados' => Estado::all(), 'tipos_destino' => DestinoTipo::all(), 'tiposLogradouro' => LogradouroTipo::orderBy('descricao')->get()->map(fn ($tipo) => [ 'id' => $tipo->id, 'descricao' => $tipo->descricao, ]) ]; } private static function obterMunicipio($municipioId = null) { $municipio = [ 'id' => '', 'nome' => '', 'ibge' => '', ]; if (!is_null($municipioId)) { $dadosMunicipio = Municipio::where('id', $municipioId)->get(); $municipio['id'] = $dadosMunicipio[0]->id; $municipio['nome'] = $dadosMunicipio[0]->nome; $municipio['ibge'] = $dadosMunicipio[0]->ibge; } return $municipio; } private static function getModelData($destino = null) { if (!is_null($destino)) { if ($destino->documento_tipo_id && ($destino->numero_cnpj || $destino->numero_cpf)) { $destino->numero_documento = $destino->documento_tipo_id === DocumentoTipo::CNPJ ? MaskUtil::getCnpjMasked($destino->numero_cnpj) : MaskUtil::getCpfMasked($destino->numero_cpf); } } $destino = [ 'id' => $destino->id ?? null, 'descricao_destino' => $destino->descricao_destino ?? null, 'numero_telefone_fax' => $destino->numero_telefone_fax ?? null !== null ? MaskUtil::getPhoneMasked($destino->numero_telefone_fax) : '', 'numero_documento' => $destino->numero_documento ?? null, 'nome_logradouro' => $destino->nome_logradouro ?? null, 'numero_logradouro' => $destino->numero_logradouro ?? null, 'nome_complemento' => $destino->nome_complemento ?? null, 'nome_bairro' => $destino->nome_bairro ?? null, 'numero_cep' => $destino->numero_cep ?? null !== null ? MaskUtil::getCepMasked($destino->numero_cep) : null, 'numero_telefone_comercial' => $destino->numero_telefone_comercial ?? null !== null ? MaskUtil::getPhoneMasked($destino->numero_telefone_comercial) : '', 'observacao' => $destino->observacao ?? null, 'cnes' => $destino->cnes ?? null, 'bloqueado' => $destino->bloqueado ?? null, 'tipo_prestador' => $destino->tipo_prestador ?? null, 'distancia' => $destino->distancia ?? null, 'destino_tipo_id' => $destino->destino_tipo_id ?? null, 'cadastro_logradouro_tipo_id' => $destino->cadastro_logradouro_tipo_id ?? null, 'documento_tipo_id' => $destino->documento_tipo_id ?? DocumentoTipo::CNPJ, 'usuario_id' => $destino->usuario_id ?? null, 'estado_id' => $destino->estado_id ?? null, 'municipio_id' => $destino->municipio_id ?? null, 'municipio' => self::obterMunicipio($destino->municipio_id ?? null), ]; return $destino; } public static function create() { return [ 'form' => self::getFormData(), 'model' => self::getModelData(), ]; } public static function show($id) { return Destino::find($id); } public static function update($id) { self::validate(); return Destino::where('id', $id)->update(request()->all()); } public static function bloquear($id) { $destino = self::show($id); return Destino::where('id', $id)->update(['bloqueado' => $destino->bloqueado == 1 ? 0 : 1]); } public static function edit($id) { return [ 'form' => self::getFormData(), 'model' => self::getModelData(self::show($id)), ]; } public static function store() { self::validate(); return Destino::create(request()->all())->fresh(); } public static function destroy($id) { try { return self::show($id)->delete(); } catch (\Throwable $th) { throw new DeletingModelException; } } public static function obterDestinos() { return Destino::all(); } public static function datatable() { $tipoCpf = DocumentoTipo::CPF; $tipoCnpj = DocumentoTipo::CNPJ; $destinos = DB::table('transporte_paciente_destino') ->leftJoin( 'transporte_paciente_destino_tipo', 'transporte_paciente_destino.destino_tipo_id', '=', 'transporte_paciente_destino_tipo.id' ) ->leftJoin('intranet.cadastro_municipio as municipio', 'municipio.id', 'transporte_paciente_destino.municipio_id') ->select( 'transporte_paciente_destino.id', 'transporte_paciente_destino.descricao_destino', 'transporte_paciente_destino.documento_tipo_id', 'municipio.nome as municipio', 'transporte_paciente_destino.bloqueado', 'transporte_paciente_destino_tipo.descricao as descricao_tipo_destino', DB::raw(" case when transporte_paciente_destino.documento_tipo_id is null then '-' when transporte_paciente_destino.documento_tipo_id = {$tipoCpf} then transporte_paciente_destino.numero_cpf when transporte_paciente_destino.documento_tipo_id = {$tipoCnpj} then transporte_paciente_destino.numero_cnpj else '-' end as numero_documento "), DB::raw(" CASE WHEN transporte_paciente_destino.bloqueado THEN 'INATIVO' ELSE 'ATIVO' END AS status "), ); return datatables() ->of($destinos) ->filterColumn('transporte_paciente_destino.bloqueado', function ($query, $keyword) { $query->whereRaw(" CASE WHEN transporte_paciente_destino.bloqueado THEN 'INATIVO' ELSE 'ATIVO' END LIKE ?", ["%{$keyword}%"] ); }) ->filterColumn('transporte_paciente_destino.documento_tipo_id', function ($query, $keyword) { $query ->whereRaw("transporte_paciente_destino.numero_cpf like ?", ["%{$keyword}%"]) ->orWhereRaw( "CONCAT( SUBSTR(transporte_paciente_destino.numero_cpf,1,3), '.', SUBSTR(transporte_paciente_destino.numero_cpf,4,3), '.', SUBSTR(transporte_paciente_destino.numero_cpf,7,3), '-', SUBSTR(transporte_paciente_destino.numero_cpf,10,2) ) like ?", ["%{$keyword}%"] ) ->orWhereRaw("transporte_paciente_destino.numero_cnpj like ?", ["%{$keyword}%"]) ->orWhereRaw( "CONCAT( SUBSTR(transporte_paciente_destino.numero_cnpj, 1, 2), '.', SUBSTR(transporte_paciente_destino.numero_cnpj, 3, 3), '.', SUBSTR(transporte_paciente_destino.numero_cnpj, 6, 3), '/', SUBSTR(transporte_paciente_destino.numero_cnpj, 9, 4), '-', SUBSTR(transporte_paciente_destino.numero_cnpj, 13, 2) ) like ?", ["%{$keyword}%"] ); }); } private static function validate() { return request()->validate( [ 'descricao_destino' => [ 'required', 'min:3', 'max:100', Rule::unique('transporte_paciente_destino') ->where(function ($query) { return $query->where('municipio_id', request()->municipio_id); }) ->ignore(request()->destino), ], ], [ 'descricao_destino.unique' => 'O destino já foi inserido anteriormente para este município.', 'descricao_destino.min' => 'A descrição do destino precisa ter no mínimo 3 caracteres.' ] ); } }