This commit is contained in:
600
api/app/Services/TransportePaciente/FechamentoService.php
Normal file
600
api/app/Services/TransportePaciente/FechamentoService.php
Normal file
@@ -0,0 +1,600 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TransportePaciente;
|
||||
|
||||
use App\Enums\Sigtap\Procedimento;
|
||||
use App\Enums\TransportePaciente\Status as EnumStatus;
|
||||
use App\Enums\TransportePaciente\ViagemStatus as EnumViagemStatus;
|
||||
use App\Models\Sigtap\Procedimento as ProcedimentoSigtap;
|
||||
use App\Models\TransportePaciente\Agendar\Agendar;
|
||||
use App\Models\TransportePaciente\Faturamento\Faturamento;
|
||||
use App\Models\TransportePaciente\Parametrizacao\Faturamento as FaturamentoParametrizacao;
|
||||
use App\Models\TransportePaciente\Parametrizacao\Parametrizacao;
|
||||
use App\Models\TransportePaciente\Status\Status;
|
||||
use App\Models\TransportePaciente\Viagem;
|
||||
use App\Models\TransportePaciente\ViagemStatus;
|
||||
use App\Services\Service;
|
||||
use App\Services\TransportePaciente\Agendar\HistoricoService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class FechamentoService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => self::getModelData()
|
||||
];
|
||||
}
|
||||
|
||||
private static function getFormData()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('d/m/Y');
|
||||
|
||||
return [
|
||||
'data_atual' => $currentDate,
|
||||
'status' => ViagemStatus::find([
|
||||
EnumViagemStatus::ABERTO,
|
||||
EnumViagemStatus::FINALIZADO,
|
||||
]),
|
||||
'veiculos' => [],
|
||||
'motoristas' => [],
|
||||
'horarios' => [],
|
||||
'situacao' => Status::where('id', EnumStatus::TRANSPORTADO)->orWhere('id', EnumStatus::FALTOU)->get(),
|
||||
];
|
||||
}
|
||||
|
||||
private static function getModelData()
|
||||
{
|
||||
return [
|
||||
'id' => null,
|
||||
'status' => 1,
|
||||
'data' => null,
|
||||
'veiculoId' => null,
|
||||
'motoristaId' => null,
|
||||
'horario' => null,
|
||||
'horarioSaida' => null,
|
||||
'horarioChegadaDestino' => null,
|
||||
'horarioSaidaDestino' => null,
|
||||
'dataChegada' => null,
|
||||
'horarioChegada' => null,
|
||||
'kmSaida' => null,
|
||||
'kmChegada' => null,
|
||||
'diaria' => null,
|
||||
'refeicao' => null,
|
||||
'pernoite' => null,
|
||||
'pedagio' => null,
|
||||
'combustivel' => null,
|
||||
'outrasDespesas' => null,
|
||||
'descricaoOutrasDespesas' => null,
|
||||
'totalDespesas' => null,
|
||||
];
|
||||
}
|
||||
|
||||
public static function update($id, $request)
|
||||
{
|
||||
$viagem = $request->all()['viagem'];
|
||||
|
||||
$pacientes = $request->all()['pacientes'];
|
||||
|
||||
if(empty($pacientes)){
|
||||
Viagem::where('id', $id)->update($viagem);
|
||||
}
|
||||
|
||||
foreach ($pacientes as $key => $paciente) {
|
||||
|
||||
Viagem::where('id', $paciente['viagem_id'])->update($viagem);
|
||||
|
||||
$agenda = Agendar::find($paciente['id']);
|
||||
$agenda->status_id = $paciente['status_id'];
|
||||
$agenda->save();
|
||||
|
||||
HistoricoService::create($agenda, $paciente['status_id'], $agenda->updated_lotacao_id);
|
||||
|
||||
$distancia = $paciente['distancia'];
|
||||
$qtdeAcompanhante = $paciente['quantidade_acompanhante'];
|
||||
|
||||
$parametrizacao = Parametrizacao::first();
|
||||
|
||||
if(isset($parametrizacao->faturamento) && $parametrizacao->faturamento == true) {
|
||||
|
||||
$faturamentoParametrizado = FaturamentoParametrizacao::first();
|
||||
|
||||
if (isset($faturamentoParametrizado->id)) {
|
||||
|
||||
$unidadeFaturamento = $faturamentoParametrizado->unidade_id;
|
||||
$ocupacaoFaturamento = $faturamentoParametrizado->ocupacao_id;
|
||||
$usuarioFaturamento = $faturamentoParametrizado->usuario_id;
|
||||
|
||||
if (isset($distancia) && $distancia != '') {
|
||||
|
||||
$qtdeProcedimento = (int) ceil($distancia / 50);
|
||||
$qtdeProcAcompanhante = $qtdeProcedimento * $qtdeAcompanhante;
|
||||
|
||||
if ($qtdeProcedimento >= 1 && $qtdeProcAcompanhante >= 1) {
|
||||
|
||||
if ($agenda->status_id == EnumStatus::TRANSPORTADO) {
|
||||
|
||||
$verificaFaturamento = Faturamento::where('agendamento_id', $paciente['id'])
|
||||
->first();
|
||||
|
||||
if (!$verificaFaturamento) {
|
||||
|
||||
$procedimentoPorPaciente = ProcedimentoSigtap::where('codigo',
|
||||
Procedimento::UNIDADE_DE_REMUNERACAO_PARA_DESLOCAMENTO_DE_PACIENTE_POR_TRANSPORTE_TERRESTRE
|
||||
)
|
||||
->orderByDesc('competencia_id')
|
||||
->first();
|
||||
|
||||
$procedimentoPorAcompanhante = ProcedimentoSigtap::where('codigo',
|
||||
Procedimento::UNIDADE_DE_REMUNERACAO_PARA_DESLOCAMENTO_DE_ACOMPANHANTE_POR_TRANSPORTE_TERRESTRE
|
||||
)
|
||||
->orderByDesc('competencia_id')
|
||||
->first();
|
||||
|
||||
for ($i = 0; $i < $qtdeProcedimento; $i++) {
|
||||
|
||||
$faturamento = new Faturamento;
|
||||
$faturamento->agendamento_id = $paciente['id'];
|
||||
$faturamento->paciente_id = $paciente['paciente_id'];
|
||||
$faturamento->lotacao_id = $unidadeFaturamento;
|
||||
$faturamento->ocupacao_id = $ocupacaoFaturamento;
|
||||
$faturamento->distancia = $paciente['distancia'];
|
||||
$faturamento->motivo_id = $paciente['motivo_id'];
|
||||
$faturamento->destino_id = $paciente['destino_id'];
|
||||
$faturamento->usuario_id = $usuarioFaturamento;
|
||||
$faturamento->procedimento_id = $procedimentoPorPaciente->id;
|
||||
$faturamento->quantidade = 1;
|
||||
$faturamento->save();
|
||||
}
|
||||
|
||||
if ($parametrizacao->auxilio_alimentacao == 1) {
|
||||
|
||||
$procedimentoAlimentacaoPorPaciente = ProcedimentoSigtap::where('codigo',
|
||||
Procedimento::AJUDA_DE_CUSTO_PARA_ALIMENTACAO_DE_PACIENTE_SEM_PERNOITE
|
||||
)
|
||||
->orderByDesc('competencia_id')
|
||||
->first();
|
||||
|
||||
$faturamento = new Faturamento;
|
||||
$faturamento->agendamento_id = $paciente['id'];
|
||||
$faturamento->paciente_id = $paciente['paciente_id'];
|
||||
$faturamento->lotacao_id = $unidadeFaturamento;
|
||||
$faturamento->ocupacao_id = $ocupacaoFaturamento;
|
||||
$faturamento->distancia = $paciente['distancia'];
|
||||
$faturamento->motivo_id = $paciente['motivo_id'];
|
||||
$faturamento->destino_id = $paciente['destino_id'];
|
||||
$faturamento->usuario_id = $usuarioFaturamento;
|
||||
$faturamento->procedimento_id = $procedimentoAlimentacaoPorPaciente->id;
|
||||
$faturamento->quantidade = 1;
|
||||
$faturamento->save();
|
||||
}
|
||||
|
||||
$qtdeInsercaoAcompanhante = $qtdeProcAcompanhante / $qtdeAcompanhante;
|
||||
|
||||
for ($q = 0; $q < $qtdeInsercaoAcompanhante; $q++) {
|
||||
|
||||
if ($paciente['idade'] >= 18 && $paciente['idade'] <= 80) {
|
||||
|
||||
if ($paciente['acompanhante_id_1']) {
|
||||
|
||||
$faturamento = new Faturamento;
|
||||
$faturamento->agendamento_id = $paciente['id'];
|
||||
$faturamento->paciente_id = $paciente['paciente_id'];
|
||||
$faturamento->lotacao_id = $unidadeFaturamento;
|
||||
$faturamento->ocupacao_id = $ocupacaoFaturamento;
|
||||
$faturamento->distancia = $paciente['distancia'];
|
||||
$faturamento->motivo_id = $paciente['motivo_id'];
|
||||
$faturamento->destino_id = $paciente['destino_id'];
|
||||
$faturamento->usuario_id = $usuarioFaturamento;
|
||||
$faturamento->procedimento_id = $procedimentoPorAcompanhante->id;
|
||||
$faturamento->quantidade = 1;
|
||||
$faturamento->save();
|
||||
}
|
||||
|
||||
if ($paciente['acompanhante_id_2']) {
|
||||
|
||||
$faturamento = new Faturamento;
|
||||
$faturamento->agendamento_id = $paciente['id'];
|
||||
$faturamento->paciente_id = $paciente['paciente_id'];
|
||||
$faturamento->lotacao_id = $unidadeFaturamento;
|
||||
$faturamento->ocupacao_id = $ocupacaoFaturamento;
|
||||
$faturamento->distancia = $paciente['distancia'];
|
||||
$faturamento->motivo_id = $paciente['motivo_id'];
|
||||
$faturamento->destino_id = $paciente['destino_id'];
|
||||
$faturamento->usuario_id = $usuarioFaturamento;
|
||||
$faturamento->procedimento_id = $procedimentoPorAcompanhante->id;
|
||||
$faturamento->quantidade = 1;
|
||||
$faturamento->save();
|
||||
}
|
||||
} else {
|
||||
|
||||
if($parametrizacao->dados_acompanhante && !$parametrizacao->campo_livre){
|
||||
|
||||
if ($paciente['acompanhante_1']) {
|
||||
|
||||
$faturamento = new Faturamento;
|
||||
$faturamento->agendamento_id = $paciente['id'];
|
||||
$faturamento->paciente_id = $paciente['acompanhante_1'];
|
||||
$faturamento->lotacao_id = $unidadeFaturamento;
|
||||
$faturamento->ocupacao_id = $ocupacaoFaturamento;
|
||||
$faturamento->distancia = $paciente['distancia'];
|
||||
$faturamento->motivo_id = $paciente['motivo_id'];
|
||||
$faturamento->destino_id = $paciente['destino_id'];
|
||||
$faturamento->usuario_id = $usuarioFaturamento;
|
||||
$faturamento->procedimento_id = $procedimentoPorAcompanhante->id;
|
||||
$faturamento->quantidade = 1;
|
||||
$faturamento->save();
|
||||
}
|
||||
|
||||
if ($paciente['acompanhante_2']) {
|
||||
|
||||
$faturamento = new Faturamento;
|
||||
$faturamento->agendamento_id = $paciente['id'];
|
||||
$faturamento->paciente_id = $paciente['acompanhante_2'];
|
||||
$faturamento->lotacao_id = $unidadeFaturamento;
|
||||
$faturamento->ocupacao_id = $ocupacaoFaturamento;
|
||||
$faturamento->distancia = $paciente['distancia'];
|
||||
$faturamento->motivo_id = $paciente['motivo_id'];
|
||||
$faturamento->destino_id = $paciente['destino_id'];
|
||||
$faturamento->usuario_id = $usuarioFaturamento;
|
||||
$faturamento->procedimento_id = $procedimentoPorAcompanhante->id;
|
||||
$faturamento->quantidade = 1;
|
||||
$faturamento->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($parametrizacao->auxilio_alimentacao == 1) {
|
||||
|
||||
$procedimentoAlimentacaoAcompanhante = ProcedimentoSigtap::where('codigo',
|
||||
Procedimento::AJUDA_DE_CUSTO_PARA_ALIMENTACAO_DE_ACOMPANHANTE_SEM_PERNOITE
|
||||
)
|
||||
->orderByDesc('competencia_id')
|
||||
->first();
|
||||
|
||||
if ($paciente['idade'] >= 18 && $paciente['idade'] <= 80) {
|
||||
|
||||
if ($paciente['acompanhante_1']) {
|
||||
|
||||
$faturamento = new Faturamento;
|
||||
$faturamento->agendamento_id = $paciente['id'];
|
||||
$faturamento->paciente_id = $paciente['paciente_id'];
|
||||
$faturamento->lotacao_id = $unidadeFaturamento;
|
||||
$faturamento->ocupacao_id = $ocupacaoFaturamento;
|
||||
$faturamento->distancia = $paciente['distancia'];
|
||||
$faturamento->motivo_id = $paciente['motivo_id'];
|
||||
$faturamento->destino_id = $paciente['destino_id'];
|
||||
$faturamento->usuario_id = $usuarioFaturamento;
|
||||
$faturamento->procedimento_id = $procedimentoAlimentacaoAcompanhante->id;
|
||||
$faturamento->quantidade = 1;
|
||||
$faturamento->save();
|
||||
}
|
||||
|
||||
if ($paciente['acompanhante_2']) {
|
||||
|
||||
$faturamento = new Faturamento;
|
||||
$faturamento->agendamento_id = $paciente['id'];
|
||||
$faturamento->paciente_id = $paciente['paciente_id'];
|
||||
$faturamento->lotacao_id = $unidadeFaturamento;
|
||||
$faturamento->ocupacao_id = $ocupacaoFaturamento;
|
||||
$faturamento->distancia = $paciente['distancia'];
|
||||
$faturamento->motivo_id = $paciente['motivo_id'];
|
||||
$faturamento->destino_id = $paciente['destino_id'];
|
||||
$faturamento->usuario_id = $usuarioFaturamento;
|
||||
$faturamento->procedimento_id = $procedimentoAlimentacaoAcompanhante->id;
|
||||
$faturamento->quantidade = 1;
|
||||
$faturamento->save();
|
||||
}
|
||||
} else {
|
||||
|
||||
if($parametrizacao->dados_acompanhante && !$parametrizacao->campo_livre){
|
||||
|
||||
if ($paciente['acompanhante_1']) {
|
||||
|
||||
$faturamento = new Faturamento;
|
||||
$faturamento->agendamento_id = $paciente['id'];
|
||||
$faturamento->paciente_id = $paciente['acompanhante_1'];
|
||||
$faturamento->lotacao_id = $unidadeFaturamento;
|
||||
$faturamento->ocupacao_id = $ocupacaoFaturamento;
|
||||
$faturamento->distancia = $paciente['distancia'];
|
||||
$faturamento->motivo_id = $paciente['motivo_id'];
|
||||
$faturamento->destino_id = $paciente['destino_id'];
|
||||
$faturamento->usuario_id = $usuarioFaturamento;
|
||||
$faturamento->procedimento_id = $procedimentoAlimentacaoAcompanhante->id;
|
||||
$faturamento->quantidade = 1;
|
||||
$faturamento->save();
|
||||
}
|
||||
|
||||
if ($paciente['acompanhante_2']) {
|
||||
|
||||
$faturamento = new Faturamento;
|
||||
$faturamento->agendamento_id = $paciente['id'];
|
||||
$faturamento->paciente_id = $paciente['acompanhante_2'];
|
||||
$faturamento->lotacao_id = $unidadeFaturamento;
|
||||
$faturamento->ocupacao_id = $ocupacaoFaturamento;
|
||||
$faturamento->distancia = $paciente['distancia'];
|
||||
$faturamento->motivo_id = $paciente['motivo_id'];
|
||||
$faturamento->destino_id = $paciente['destino_id'];
|
||||
$faturamento->usuario_id = $usuarioFaturamento;
|
||||
$faturamento->procedimento_id = $procedimentoAlimentacaoAcompanhante->id;
|
||||
$faturamento->quantidade = 1;
|
||||
$faturamento->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
Faturamento::destroy(Faturamento::where('agendamento_id', $paciente['id'])->pluck('id'));
|
||||
}
|
||||
} else if ($qtdeProcedimento >= 1) {
|
||||
|
||||
if ($agenda->status_id == EnumStatus::TRANSPORTADO) {
|
||||
|
||||
if ($paciente['idade'] >= 18 && $paciente['idade'] <= 80) {
|
||||
|
||||
$verificaFaturamento = Faturamento::where('agendamento_id', $paciente['id'])->first();
|
||||
|
||||
$procedimentoPorPaciente = ProcedimentoSigtap::where('codigo',
|
||||
Procedimento::UNIDADE_DE_REMUNERACAO_PARA_DESLOCAMENTO_DE_PACIENTE_POR_TRANSPORTE_TERRESTRE
|
||||
)
|
||||
->orderByDesc('competencia_id')
|
||||
->first();
|
||||
|
||||
if (!$verificaFaturamento) {
|
||||
|
||||
for ($i = 0; $i < $qtdeProcedimento; $i++) {
|
||||
|
||||
$faturamento = new Faturamento;
|
||||
$faturamento->agendamento_id = $paciente['id'];
|
||||
$faturamento->paciente_id = $paciente['paciente_id'];
|
||||
$faturamento->lotacao_id = $unidadeFaturamento;
|
||||
$faturamento->ocupacao_id = $ocupacaoFaturamento;
|
||||
$faturamento->distancia = $paciente['distancia'];
|
||||
$faturamento->motivo_id = $paciente['motivo_id'];
|
||||
$faturamento->destino_id = $paciente['destino_id'];
|
||||
$faturamento->usuario_id = $usuarioFaturamento;
|
||||
$faturamento->procedimento_id = $procedimentoPorPaciente->id;
|
||||
$faturamento->quantidade = 1;
|
||||
$faturamento->save();
|
||||
}
|
||||
|
||||
if ($parametrizacao->auxilio_alimentacao == 1) {
|
||||
|
||||
$procedimentoAlimentacao = ProcedimentoSigtap::where('codigo',
|
||||
Procedimento::AJUDA_DE_CUSTO_PARA_ALIMENTACAO_DE_PACIENTE_SEM_PERNOITE
|
||||
)
|
||||
->orderByDesc('competencia_id')
|
||||
->first();
|
||||
|
||||
$faturamento = new Faturamento;
|
||||
$faturamento->agendamento_id = $paciente['id'];
|
||||
$faturamento->paciente_id = $paciente['paciente_id'];
|
||||
$faturamento->lotacao_id = $unidadeFaturamento;
|
||||
$faturamento->ocupacao_id = $ocupacaoFaturamento;
|
||||
$faturamento->distancia = $paciente['distancia'];
|
||||
$faturamento->motivo_id = $paciente['motivo_id'];
|
||||
$faturamento->destino_id = $paciente['destino_id'];
|
||||
$faturamento->usuario_id = $usuarioFaturamento;
|
||||
$faturamento->procedimento_id = $procedimentoAlimentacao->id;
|
||||
$faturamento->quantidade = 1;
|
||||
$faturamento->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
Faturamento::destroy(Faturamento::where('agendamento_id', $paciente['id'])->pluck('id'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function getVeiculos()
|
||||
{
|
||||
$veiculos = DB::table('transporte_paciente_viagem as viagem')
|
||||
->select(
|
||||
'viagem.status_id',
|
||||
'viagem.id as id_viagem',
|
||||
'viagem.data',
|
||||
'veiculo.id',
|
||||
DB::raw('CONCAT(veiculo.numero_placa, " - ", veiculo.descricao_modelo) AS descricao'),
|
||||
'agendar.id as agendar_id',
|
||||
)
|
||||
->join('transporte_paciente_veiculo as veiculo', 'veiculo.id', 'viagem.veiculo_id')
|
||||
->leftJoin('transporte_paciente_agendar_transporte as agendar', 'agendar.viagem_id', 'viagem.id')
|
||||
->where('viagem.status_id', request()->status)
|
||||
->where('agendar.data', request()->data)
|
||||
->orderBy('veiculo.numero_placa')
|
||||
->groupBy('veiculo.id')
|
||||
->where('veiculo.bloqueado', false)
|
||||
->whereNotNull('agendar.id')
|
||||
->where('viagem.status_id', '<>', EnumViagemStatus::CANCELADO)
|
||||
->get();
|
||||
|
||||
return $veiculos;
|
||||
}
|
||||
|
||||
public static function getMotoristas()
|
||||
{
|
||||
|
||||
$motoristas = DB::table('transporte_paciente_viagem as viagem')
|
||||
->join('transporte_paciente_agendar_transporte as agendar', 'agendar.viagem_id', 'viagem.id')
|
||||
->join('transporte_paciente_condutor as condutor', 'condutor.id', 'viagem.condutor_id')
|
||||
->join('cadastro_usuario as usuario', 'usuario.id', 'condutor.usuario_id')
|
||||
->join('cadastro_cidadao as cidadao', 'cidadao.id', 'usuario.cidadao_id')
|
||||
->select('viagem.condutor_id', 'cidadao.nome', 'condutor.id')
|
||||
->where('agendar.data', request()->data)
|
||||
->where('viagem.veiculo_id', request()->veiculo_id)
|
||||
->orderBy('cidadao.nome')
|
||||
->groupBy('condutor.id')
|
||||
->get();
|
||||
|
||||
return $motoristas;
|
||||
}
|
||||
|
||||
public static function getHorarios()
|
||||
{
|
||||
return DB::table('transporte_paciente_viagem as viagem')
|
||||
->select(
|
||||
DB::raw('
|
||||
IF(
|
||||
(
|
||||
SELECT
|
||||
agendar_transporte_jornada
|
||||
FROM
|
||||
transporte_paciente_parametrizacao
|
||||
) = 1,
|
||||
viagem.jornada_id,
|
||||
viagem.id
|
||||
) as id
|
||||
'),
|
||||
DB::raw('
|
||||
IF(
|
||||
(
|
||||
SELECT
|
||||
agendar_transporte_jornada
|
||||
FROM
|
||||
transporte_paciente_parametrizacao
|
||||
) = 1,
|
||||
DATE_FORMAT(jornada.data_inicial, "%H:%i"),
|
||||
DATE_FORMAT(viagem.horario_saida_temp, "%H:%i")
|
||||
) as horario_saida_temp
|
||||
')
|
||||
)
|
||||
->leftJoin('transporte_paciente_jornada as jornada', 'viagem.jornada_id', 'jornada.id')
|
||||
->where('viagem.data', request()->data)
|
||||
->where('viagem.veiculo_id', request()->veiculo_id)
|
||||
->where('viagem.condutor_id', request()->motorista_id)
|
||||
->where('viagem.status_id', request()->status)
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function search()
|
||||
{
|
||||
return [
|
||||
'viagem' => self::getViagem(),
|
||||
'pacientes' => self::getPacientes(),
|
||||
];
|
||||
}
|
||||
|
||||
private static function getPacientes()
|
||||
{
|
||||
$pacientes = DB::table('transporte_paciente_agendar_transporte as agendar')
|
||||
->join('transporte_paciente_destino as destino', 'agendar.destino_id', 'destino.id')
|
||||
->join('transporte_paciente_motivo_viagem_procedimento as motivo', 'agendar.motivo_id', 'motivo.id')
|
||||
->join('cadastro_paciente as paciente', 'agendar.paciente_id', 'paciente.id')
|
||||
->join('cadastro_cidadao as cidadao', 'paciente.cidadao_id', 'cidadao.id')
|
||||
->leftJoin('transporte_paciente_viagem as viagem', 'viagem.id', 'agendar.viagem_id')
|
||||
->leftJoin('transporte_paciente_viagem_status as viagem_status', 'viagem_status.id', 'viagem.status_id')
|
||||
->leftJoin('transporte_paciente_acompanhante as paciente_acompanhante_1', 'paciente_acompanhante_1.id', 'agendar.acompanhante_1_id')
|
||||
->leftJoin('transporte_paciente_acompanhante as paciente_acompanhante_2', 'paciente_acompanhante_2.id', 'agendar.acompanhante_2_id')
|
||||
->select(
|
||||
'agendar.id as id',
|
||||
'agendar.lotacao_id as lotacao_id',
|
||||
'agendar.ocupacao_id as ocupacao_id',
|
||||
'agendar.paciente_id as paciente_id',
|
||||
'agendar.usuario_id as usuario_id',
|
||||
'agendar.acompanhante as quantidade_acompanhante',
|
||||
'cidadao.nome as nome',
|
||||
'cidadao.id as cidadao_id',
|
||||
'destino.descricao_destino as localConsulta',
|
||||
'destino.id as destino_id',
|
||||
'destino.distancia as distancia',
|
||||
'motivo.descricao as motivo',
|
||||
'motivo.id as motivo_id',
|
||||
'agendar.viagem_id as viagem_id',
|
||||
'viagem.status_id as viagem_status_id',
|
||||
'viagem_status.descricao as nome_status',
|
||||
'viagem.condutor_id',
|
||||
DB::raw("DATE_FORMAT(cidadao.data_nascimento, '%d/%m/%Y') as dataNasc"),
|
||||
DB::raw('TIMESTAMPDIFF(YEAR, cidadao.data_nascimento, NOW()) AS idade'),
|
||||
DB::raw('IF(agendar.status_id = ' . EnumStatus::FALTOU . ', ' . EnumStatus::FALTOU . ', ' . EnumStatus::TRANSPORTADO . ') as situacao'),
|
||||
'paciente_acompanhante_1.usuario_id as acompanhante_usuarioId1',
|
||||
'paciente_acompanhante_2.usuario_id as acompanhante_usuarioId2',
|
||||
'agendar.acompanhante_1_id as acompanhante_id1',
|
||||
'agendar.acompanhante_2_id as acompanhante_id2',
|
||||
)
|
||||
->where('viagem.status_id', '<>', EnumViagemStatus::CANCELADO);
|
||||
|
||||
if (Parametrizacao::exists()) {
|
||||
if (Parametrizacao::first()->agendar_transporte_jornada === true && request()->horario !== null) {
|
||||
$pacientes = $pacientes->where('viagem.jornada_id', request()->horario);
|
||||
} else if (request()->horario !== null) {
|
||||
$pacientes = $pacientes->where('viagem.id', request()->horario);
|
||||
}
|
||||
}
|
||||
|
||||
$pacientes = $pacientes
|
||||
->where('agendar.data', request()->data)
|
||||
->where('agendar.status_id', '<>', EnumStatus::CANCELADO)
|
||||
->where('viagem.veiculo_id', request()->veiculo_id)
|
||||
->where('viagem.status_id', request()->status)
|
||||
->where('viagem.condutor_id', request()->motorista_id)
|
||||
->get();
|
||||
|
||||
return $pacientes;
|
||||
}
|
||||
|
||||
private static function getViagem()
|
||||
{
|
||||
$consulta = DB::table('transporte_paciente_viagem as viagem')
|
||||
->select(
|
||||
'viagem.id',
|
||||
'viagem.condutor_id',
|
||||
'viagem.veiculo_id',
|
||||
'viagem.data',
|
||||
'viagem.horario_saida_temp as horarioSaida',
|
||||
'viagem.horario_saida_real',
|
||||
'viagem.horario_chegada_destino',
|
||||
'viagem.horario_saida_destino',
|
||||
'viagem.data_chegada',
|
||||
'viagem.horario_chegada',
|
||||
'viagem.km_saida',
|
||||
'viagem.km_chegada',
|
||||
'viagem.diaria',
|
||||
'viagem.refeicao',
|
||||
'viagem.pernoite',
|
||||
'viagem.pedagio',
|
||||
'viagem.combustivel',
|
||||
'viagem.outras_despesas',
|
||||
'viagem.descricao_outras_despesas',
|
||||
'viagem.total_despesa',
|
||||
'viagem.status_id as viagem_status_id',
|
||||
'viagem.jornada_id',
|
||||
'viagem.destino_id',
|
||||
'cidadao.nome as motorista',
|
||||
'status.descricao as nome_status',
|
||||
'condutor.usuario_id as condutor_usuario_id'
|
||||
)
|
||||
->leftJoin('transporte_paciente_agendar_transporte as agendar', 'agendar.viagem_id', 'viagem.id')
|
||||
->leftJoin('transporte_paciente_condutor as condutor', 'viagem.condutor_id', 'condutor.id')
|
||||
->leftJoin('cadastro_usuario as usuario', 'usuario.id', 'condutor.usuario_id')
|
||||
->leftJoin('cadastro_cidadao as cidadao', 'cidadao.id', 'usuario.cidadao_id')
|
||||
->leftJoin('transporte_paciente_viagem_status as status', 'status.id', 'viagem.status_id')
|
||||
->where('viagem.status_id', '<>', EnumViagemStatus::CANCELADO);
|
||||
|
||||
if (Parametrizacao::exists() && Parametrizacao::first()->agendar_transporte_jornada === true && request()->horario !== null) {
|
||||
$consulta = $consulta->where('viagem.jornada_id', request()->horario);
|
||||
} else if (Parametrizacao::exists() && request()->horario !== null) {
|
||||
$consulta = $consulta->where('viagem.id', request()->horario);
|
||||
}
|
||||
|
||||
$consulta = $consulta
|
||||
->where('agendar.data', request()->data)
|
||||
->where('viagem.veiculo_id', request()->veiculo_id)
|
||||
->where('viagem.condutor_id', request()->motorista_id)
|
||||
->where('viagem.status_id', request()->status)
|
||||
->first();
|
||||
|
||||
return $consulta;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user