1440 lines
58 KiB
PHP
1440 lines
58 KiB
PHP
<?php
|
|
|
|
namespace App\Services\ExameLaboratorial;
|
|
|
|
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
|
use App\Models\Cadastro\Estabelecimento\Unidade;
|
|
use App\Models\ExameLaboratorial\LancamentoColeta\Status;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\Unidade as ExameLaboratorialUnidade;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\UnidadeFaturamento;
|
|
use App\Models\Intranet\Cliente;
|
|
use App\Services\Service;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ColetaAgendadaService extends Service
|
|
{
|
|
|
|
public static function list(Request $request)
|
|
{
|
|
$now = Carbon::now();
|
|
$currentDate = $now->format('Y-m-d');
|
|
$currentLotacao = self::getCurrentLotacao();
|
|
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
|
|
|
if (!$request->exame_id || $request->exame_id && $request->pertence_grupo) {
|
|
|
|
$agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
'exame_laboratorial_agendamento.id AS agendamento_id',
|
|
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
|
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
|
|
'exame_laboratorial_agendamento.coleta_unidade_id AS coleta_unidade_id',
|
|
'cadastro_unidade_coleta.nome_fantasia AS coleta_unidade_nome',
|
|
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
|
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
|
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id AS laboratorio_infraestrutura_id',
|
|
'exame_laboratorial_agendamento.laboratorio_unidade_id AS laboratorio_id',
|
|
'exame_laboratorial_agendamento.medico_id AS medico_id',
|
|
'cadastro_cidadao_medico.nome AS medico_nome',
|
|
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
|
|
'cadastro_grupo.id AS exame_grupo_id',
|
|
'cadastro_grupo.nome AS exame_grupo_nome',
|
|
DB::raw('group_concat(parametrizacao_exame.nome separator ", ") as exames'),
|
|
DB::raw('group_concat(parametrizacao_exame.sigla_exame separator ", ") as sigla_exame'),
|
|
DB::raw('true AS pertence_grupo'),
|
|
DB::raw('false AS selecionado'),
|
|
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
|
'cadastro_cidadao.nome AS paciente_nome',
|
|
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
|
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
|
'cadastro_paciente.prontuario AS paciente_prontuario',
|
|
'cadastro_carteira_identidade.numero AS rg',
|
|
DB::raw('CONCAT(
|
|
cadastro_logradouro_tipo.descricao,
|
|
" ",
|
|
cadastro_logradouro.nome,
|
|
", ",
|
|
cadastro_endereco.numero,
|
|
" - ",
|
|
cadastro_bairro.nome
|
|
) AS endereco'),
|
|
DB::raw('CONCAT(
|
|
TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data),
|
|
" anos"
|
|
) AS paciente_idade'),
|
|
DB::raw('
|
|
CASE WHEN cadastro_infraestrutura.principal THEN
|
|
cadastro_unidade.nome_fantasia
|
|
ELSE
|
|
cadastro_infraestrutura.nome
|
|
END AS infraestrutura_nome'),
|
|
DB::raw('
|
|
CASE WHEN cadastro_infraestrutura_coleta.principal THEN
|
|
cadastro_unidade_coleta.nome_fantasia
|
|
ELSE
|
|
cadastro_infraestrutura_coleta.nome
|
|
END AS coleta_infraestrutura_nome'),
|
|
DB::raw('
|
|
CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN
|
|
cadastro_unidade_laboratorio.nome_fantasia
|
|
ELSE
|
|
cadastro_infraestrutura_laboratorio.nome
|
|
END AS laboratorio_infraestrutura_nome'),
|
|
DB::raw("
|
|
(
|
|
SELECT cadastro_cidadao_telefone.numero
|
|
FROM cadastro_cidadao_telefone
|
|
WHERE cadastro_cidadao_telefone.principal = TRUE
|
|
AND cadastro_cidadao_telefone.cidadao_id = cadastro_cidadao.id
|
|
AND cadastro_cidadao_telefone.deleted_at IS NULL
|
|
LIMIT 1
|
|
) AS paciente_telefone
|
|
"),
|
|
])
|
|
->join(
|
|
'exame_laboratorial_agendamento',
|
|
'exame_laboratorial_agendamento.id',
|
|
'exame_laboratorial_agendamento_exame.agendamento_id'
|
|
)
|
|
->join(
|
|
'parametrizacao_exame',
|
|
'parametrizacao_exame.id',
|
|
'exame_laboratorial_agendamento_exame.exame_id'
|
|
)
|
|
->join(
|
|
'cadastro_grupo',
|
|
'cadastro_grupo.id',
|
|
'parametrizacao_exame.grupo_tipo_id'
|
|
)
|
|
->join(
|
|
'cadastro_paciente',
|
|
'cadastro_paciente.id',
|
|
'exame_laboratorial_agendamento.paciente_id'
|
|
)
|
|
->join(
|
|
'cadastro_usuario',
|
|
'cadastro_usuario.id',
|
|
'exame_laboratorial_agendamento.medico_id'
|
|
)
|
|
->join(
|
|
'cadastro_cidadao as cadastro_cidadao_medico',
|
|
'cadastro_cidadao_medico.id',
|
|
'cadastro_usuario.cidadao_id'
|
|
)
|
|
->join(
|
|
'cadastro_cidadao',
|
|
'cadastro_cidadao.id',
|
|
'cadastro_paciente.cidadao_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura',
|
|
'cadastro_infraestrutura.id',
|
|
'exame_laboratorial_agendamento.infraestrutura_id'
|
|
)
|
|
->join(
|
|
'cadastro_unidade',
|
|
'cadastro_unidade.id',
|
|
'cadastro_infraestrutura.unidade_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura as cadastro_infraestrutura_coleta',
|
|
'cadastro_infraestrutura_coleta.id',
|
|
'exame_laboratorial_agendamento.coleta_infraestrutura_id'
|
|
)
|
|
->join(
|
|
'cadastro_unidade as cadastro_unidade_coleta',
|
|
'cadastro_unidade_coleta.id',
|
|
'cadastro_infraestrutura_coleta.unidade_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura as cadastro_infraestrutura_laboratorio',
|
|
'cadastro_infraestrutura_laboratorio.id',
|
|
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id'
|
|
)
|
|
->join(
|
|
'cadastro_unidade as cadastro_unidade_laboratorio',
|
|
'cadastro_unidade_laboratorio.id',
|
|
'cadastro_infraestrutura_laboratorio.unidade_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_domicilio',
|
|
'cadastro_domicilio.id',
|
|
'cadastro_cidadao.domicilio_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_endereco',
|
|
'cadastro_endereco.id',
|
|
'cadastro_domicilio.endereco_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_logradouro',
|
|
'cadastro_logradouro.id',
|
|
'cadastro_endereco.logradouro_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_logradouro_tipo',
|
|
'cadastro_logradouro_tipo.id',
|
|
'cadastro_logradouro.tipo_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_bairro',
|
|
'cadastro_bairro.id',
|
|
'cadastro_logradouro.bairro_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_carteira_identidade',
|
|
function ($leftJoin) {
|
|
|
|
$leftJoin->on('cadastro_carteira_identidade.cidadao_id', 'cadastro_cidadao.id')
|
|
->whereNull('cadastro_carteira_identidade.deleted_at');
|
|
}
|
|
)
|
|
->groupBy([
|
|
'exame_laboratorial_agendamento.id',
|
|
'cadastro_grupo.id',
|
|
]);
|
|
|
|
$agendamentosComGrupo->where('parametrizacao_exame.mapa_agrupado', true);
|
|
|
|
if ($request->periodo) {
|
|
|
|
$agendamentosComGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
'>=',
|
|
$request->periodo['start']
|
|
);
|
|
|
|
$agendamentosComGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
'<=',
|
|
$request->periodo['end']
|
|
);
|
|
} else {
|
|
|
|
$agendamentosComGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
$currentDate
|
|
);
|
|
}
|
|
|
|
if ($request->paciente_id) {
|
|
|
|
$agendamentosComGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.paciente_id',
|
|
$request->paciente_id
|
|
);
|
|
}
|
|
|
|
if ($request->agendamento_numero) {
|
|
|
|
$agendamentosComGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.agendamento_numero',
|
|
$request->agendamento_numero
|
|
);
|
|
}
|
|
|
|
if ($request->prontuario) {
|
|
|
|
$agendamentosComGrupo
|
|
->where(
|
|
'cadastro_paciente.prontuario',
|
|
$request->prontuario
|
|
);
|
|
}
|
|
|
|
if ($request->unidade_id) {
|
|
|
|
$agendamentosComGrupo
|
|
->where('exame_laboratorial_agendamento.unidade_id',
|
|
$request->unidade_id
|
|
);
|
|
}
|
|
|
|
if ($request->infraestrutura_id) {
|
|
|
|
$agendamentosComGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.infraestrutura_id',
|
|
$request->infraestrutura_id
|
|
);
|
|
}
|
|
|
|
if ($request->coleta_unidade_id) {
|
|
|
|
$agendamentosComGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.coleta_unidade_id',
|
|
$request->coleta_unidade_id
|
|
);
|
|
}
|
|
|
|
if ($request->coleta_infraestrutura_id) {
|
|
|
|
$agendamentosComGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.coleta_infraestrutura_id',
|
|
$request->coleta_infraestrutura_id
|
|
);
|
|
}
|
|
|
|
if ($request->exame_id && $request->pertence_grupo) {
|
|
|
|
$agendamentosComGrupo
|
|
->where(
|
|
'cadastro_grupo.id',
|
|
$request->exame_id
|
|
);
|
|
}
|
|
|
|
$agendamentosComGrupo
|
|
->where(function($status) {
|
|
|
|
$status
|
|
->where(
|
|
'exame_laboratorial_agendamento_exame.status_id',
|
|
StatusAgendamento::AGENDADO
|
|
)
|
|
->orWhereNull(
|
|
'exame_laboratorial_agendamento_exame.status_id'
|
|
);
|
|
});
|
|
|
|
$agendamentosComGrupo = $agendamentosComGrupo->get()->toArray();
|
|
|
|
} else {
|
|
|
|
$agendamentosComGrupo = [];
|
|
}
|
|
|
|
if (!$request->exame_id || $request->exame_id && $request->pertence_grupo) {
|
|
|
|
$agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
'exame_laboratorial_agendamento.id AS agendamento_id',
|
|
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
|
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
|
|
'exame_laboratorial_agendamento.coleta_unidade_id AS coleta_unidade_id',
|
|
'cadastro_unidade_coleta.nome_fantasia AS coleta_unidade_nome',
|
|
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
|
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
|
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id AS laboratorio_infraestrutura_id',
|
|
'exame_laboratorial_agendamento.laboratorio_unidade_id AS laboratorio_id',
|
|
'exame_laboratorial_agendamento.medico_id AS medico_id',
|
|
'cadastro_cidadao_medico.nome AS medico_nome',
|
|
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
|
|
'parametrizacao_exame.id AS exame_grupo_id',
|
|
'parametrizacao_exame.nome AS exame_grupo_nome',
|
|
DB::raw('group_concat(parametrizacao_exame.nome separator ", ") as exames'),
|
|
DB::raw('group_concat(parametrizacao_exame.sigla_exame separator ", ") as sigla_exame'),
|
|
DB::raw('false AS pertence_grupo'),
|
|
DB::raw('false AS selecionado'),
|
|
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
|
'exame_laboratorial_agendamento_exame.usuario_id AS usuario_id',
|
|
'exame_laboratorial_agendamento_exame.ocupacao_id AS ocupacao_id',
|
|
'cadastro_cidadao.nome AS paciente_nome',
|
|
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
|
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
|
'cadastro_cidadao.sexo_id AS paciente_sexo_id',
|
|
'cadastro_paciente.prontuario AS paciente_prontuario',
|
|
'cadastro_carteira_identidade.numero AS rg',
|
|
DB::raw('CONCAT(
|
|
cadastro_logradouro_tipo.descricao,
|
|
" ",
|
|
cadastro_logradouro.nome,
|
|
", ",
|
|
cadastro_endereco.numero,
|
|
" - ",
|
|
cadastro_bairro.nome
|
|
) AS endereco'),
|
|
DB::raw('CONCAT(
|
|
TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data),
|
|
" anos"
|
|
) AS paciente_idade'),
|
|
DB::raw('CONCAT(
|
|
cadastro_unidade.nome_fantasia,
|
|
" - ",
|
|
CASE WHEN cadastro_infraestrutura.principal THEN
|
|
cadastro_unidade.nome_fantasia
|
|
ELSE
|
|
cadastro_infraestrutura.nome
|
|
END
|
|
) AS infraestrutura_nome'),
|
|
DB::raw('
|
|
CASE WHEN cadastro_infraestrutura_coleta.principal THEN
|
|
cadastro_unidade_coleta.nome_fantasia
|
|
ELSE
|
|
cadastro_infraestrutura_coleta.nome
|
|
END AS coleta_infraestrutura_nome'),
|
|
DB::raw('CONCAT(
|
|
cadastro_unidade_laboratorio.nome_fantasia,
|
|
" - ",
|
|
CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN
|
|
cadastro_unidade_laboratorio.nome_fantasia
|
|
ELSE
|
|
cadastro_infraestrutura_laboratorio.nome
|
|
END
|
|
) AS laboratorio_infraestrutura_nome'),
|
|
DB::raw("
|
|
(
|
|
SELECT cadastro_cidadao_telefone.numero
|
|
FROM cadastro_cidadao_telefone
|
|
WHERE cadastro_cidadao_telefone.principal = TRUE
|
|
AND cadastro_cidadao_telefone.cidadao_id = cadastro_cidadao.id
|
|
AND cadastro_cidadao_telefone.deleted_at IS NULL
|
|
LIMIT 1
|
|
) AS paciente_telefone
|
|
"),
|
|
])
|
|
->join(
|
|
'exame_laboratorial_agendamento',
|
|
'exame_laboratorial_agendamento.id',
|
|
'exame_laboratorial_agendamento_exame.agendamento_id'
|
|
)
|
|
->join(
|
|
'parametrizacao_exame',
|
|
'parametrizacao_exame.id',
|
|
'exame_laboratorial_agendamento_exame.exame_id'
|
|
)
|
|
->join(
|
|
'cadastro_grupo',
|
|
'cadastro_grupo.id',
|
|
'parametrizacao_exame.grupo_tipo_id'
|
|
)
|
|
->join(
|
|
'cadastro_paciente',
|
|
'cadastro_paciente.id',
|
|
'exame_laboratorial_agendamento.paciente_id'
|
|
)
|
|
->join(
|
|
'cadastro_usuario',
|
|
'cadastro_usuario.id',
|
|
'exame_laboratorial_agendamento.medico_id'
|
|
)
|
|
->join(
|
|
'cadastro_cidadao as cadastro_cidadao_medico',
|
|
'cadastro_cidadao_medico.id',
|
|
'cadastro_usuario.cidadao_id'
|
|
)
|
|
->join(
|
|
'cadastro_cidadao',
|
|
'cadastro_cidadao.id',
|
|
'cadastro_paciente.cidadao_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura',
|
|
'cadastro_infraestrutura.id',
|
|
'exame_laboratorial_agendamento.infraestrutura_id'
|
|
)
|
|
->join(
|
|
'cadastro_unidade',
|
|
'cadastro_unidade.id',
|
|
'cadastro_infraestrutura.unidade_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura as cadastro_infraestrutura_coleta',
|
|
'cadastro_infraestrutura_coleta.id',
|
|
'exame_laboratorial_agendamento.coleta_infraestrutura_id'
|
|
)
|
|
->join(
|
|
'cadastro_unidade as cadastro_unidade_coleta',
|
|
'cadastro_unidade_coleta.id',
|
|
'cadastro_infraestrutura_coleta.unidade_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura as cadastro_infraestrutura_laboratorio',
|
|
'cadastro_infraestrutura_laboratorio.id',
|
|
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id'
|
|
)
|
|
->join(
|
|
'cadastro_unidade as cadastro_unidade_laboratorio',
|
|
'cadastro_unidade_laboratorio.id',
|
|
'cadastro_infraestrutura_laboratorio.unidade_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_domicilio',
|
|
'cadastro_domicilio.id',
|
|
'cadastro_cidadao.domicilio_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_endereco',
|
|
'cadastro_endereco.id',
|
|
'cadastro_domicilio.endereco_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_logradouro',
|
|
'cadastro_logradouro.id',
|
|
'cadastro_endereco.logradouro_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_logradouro_tipo',
|
|
'cadastro_logradouro_tipo.id',
|
|
'cadastro_logradouro.tipo_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_bairro',
|
|
'cadastro_bairro.id',
|
|
'cadastro_logradouro.bairro_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_carteira_identidade',
|
|
function ($leftJoin) {
|
|
|
|
$leftJoin->on('cadastro_carteira_identidade.cidadao_id', 'cadastro_cidadao.id')
|
|
->whereNull('cadastro_carteira_identidade.deleted_at');
|
|
}
|
|
)
|
|
->groupBy([
|
|
'exame_laboratorial_agendamento.id',
|
|
'parametrizacao_exame.id',
|
|
]);
|
|
|
|
$agendamentosSemMapaAgrupado->where(function ($query) {
|
|
|
|
$query->where('parametrizacao_exame.mapa_agrupado', false)
|
|
->orWhereNull('parametrizacao_exame.mapa_agrupado');
|
|
});
|
|
|
|
if ($request->periodo) {
|
|
|
|
$agendamentosSemMapaAgrupado
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
'>=',
|
|
$request->periodo['start']
|
|
);
|
|
|
|
$agendamentosSemMapaAgrupado
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
'<=',
|
|
$request->periodo['end']
|
|
);
|
|
} else {
|
|
|
|
$agendamentosSemMapaAgrupado
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
$currentDate
|
|
);
|
|
}
|
|
|
|
if ($request->paciente_id) {
|
|
|
|
$agendamentosSemMapaAgrupado
|
|
->where(
|
|
'exame_laboratorial_agendamento.paciente_id',
|
|
$request->paciente_id
|
|
);
|
|
}
|
|
|
|
if ($request->agendamento_numero) {
|
|
|
|
$agendamentosSemMapaAgrupado
|
|
->where(
|
|
'exame_laboratorial_agendamento.agendamento_numero',
|
|
$request->agendamento_numero
|
|
);
|
|
}
|
|
|
|
if ($request->prontuario) {
|
|
|
|
$agendamentosSemMapaAgrupado
|
|
->where(
|
|
'cadastro_paciente.prontuario',
|
|
$request->prontuario
|
|
);
|
|
}
|
|
|
|
if ($request->unidade_id) {
|
|
|
|
$agendamentosSemMapaAgrupado
|
|
->where(
|
|
'exame_laboratorial_agendamento.unidade_id',
|
|
$request->unidade_id
|
|
);
|
|
}
|
|
|
|
if ($request->infraestrutura_id) {
|
|
|
|
$agendamentosSemMapaAgrupado
|
|
->where(
|
|
'exame_laboratorial_agendamento.infraestrutura_id',
|
|
$request->infraestrutura_id
|
|
);
|
|
}
|
|
|
|
if ($request->coleta_unidade_id) {
|
|
|
|
$agendamentosSemMapaAgrupado
|
|
->where(
|
|
'exame_laboratorial_agendamento.coleta_unidade_id',
|
|
$request->coleta_unidade_id
|
|
);
|
|
}
|
|
|
|
if ($request->coleta_infraestrutura_id) {
|
|
|
|
$agendamentosSemMapaAgrupado
|
|
->where(
|
|
'exame_laboratorial_agendamento.coleta_infraestrutura_id',
|
|
$request->coleta_infraestrutura_id
|
|
);
|
|
}
|
|
|
|
if ($request->exame_id && $request->pertence_grupo) {
|
|
|
|
$agendamentosSemMapaAgrupado
|
|
->where(
|
|
'parametrizacao_exame.id',
|
|
$request->exame_id
|
|
);
|
|
}
|
|
|
|
$agendamentosSemMapaAgrupado
|
|
->where(function($status) {
|
|
|
|
$status
|
|
->where(
|
|
'exame_laboratorial_agendamento_exame.status_id',
|
|
StatusAgendamento::AGENDADO
|
|
)
|
|
->orWhereNull(
|
|
'exame_laboratorial_agendamento_exame.status_id'
|
|
);
|
|
});
|
|
|
|
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->get()->toArray();
|
|
|
|
} else {
|
|
|
|
$agendamentosSemMapaAgrupado = [];
|
|
}
|
|
|
|
if (!$request->exame_id || $request->exame_id && !$request->pertence_grupo) {
|
|
|
|
$agendamentosSemGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
'exame_laboratorial_agendamento.id AS agendamento_id',
|
|
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
|
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
|
|
'cadastro_unidade_coleta.nome_fantasia AS coleta_unidade_nome',
|
|
'exame_laboratorial_agendamento.coleta_unidade_id AS coleta_unidade_id',
|
|
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
|
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
|
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id AS laboratorio_infraestrutura_id',
|
|
'exame_laboratorial_agendamento.laboratorio_unidade_id AS laboratorio_id',
|
|
'exame_laboratorial_agendamento.medico_id AS medico_id',
|
|
'cadastro_cidadao_medico.nome AS medico_nome',
|
|
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
|
|
'parametrizacao_exame.id AS exame_grupo_id',
|
|
'parametrizacao_exame.nome AS exame_grupo_nome',
|
|
'parametrizacao_exame.nome AS exames',
|
|
'parametrizacao_exame.sigla_exame AS sigla_exame',
|
|
DB::raw('false AS pertence_grupo'),
|
|
DB::raw('false AS selecionado'),
|
|
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
|
'cadastro_cidadao.nome AS paciente_nome',
|
|
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
|
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
|
'cadastro_cidadao.sexo_id AS paciente_sexo_id',
|
|
'cadastro_paciente.prontuario AS paciente_prontuario',
|
|
'cadastro_carteira_identidade.numero AS rg',
|
|
DB::raw('CONCAT(
|
|
cadastro_logradouro_tipo.descricao,
|
|
" ",
|
|
cadastro_logradouro.nome,
|
|
", ",
|
|
cadastro_endereco.numero,
|
|
" - ",
|
|
cadastro_bairro.nome
|
|
) AS endereco'),
|
|
DB::raw('CONCAT(
|
|
TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data),
|
|
" anos"
|
|
) AS paciente_idade'),
|
|
DB::raw('CONCAT(
|
|
cadastro_unidade.nome_fantasia,
|
|
" - ",
|
|
CASE WHEN cadastro_infraestrutura.principal THEN
|
|
cadastro_unidade.nome_fantasia
|
|
ELSE
|
|
cadastro_infraestrutura.nome
|
|
END
|
|
) AS infraestrutura_nome'),
|
|
DB::raw('
|
|
CASE WHEN cadastro_infraestrutura_coleta.principal THEN
|
|
cadastro_unidade_coleta.nome_fantasia
|
|
ELSE
|
|
cadastro_infraestrutura_coleta.nome
|
|
END AS coleta_infraestrutura_nome'),
|
|
DB::raw('CONCAT(
|
|
cadastro_unidade_laboratorio.nome_fantasia,
|
|
" - ",
|
|
CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN
|
|
cadastro_unidade_laboratorio.nome_fantasia
|
|
ELSE
|
|
cadastro_infraestrutura_laboratorio.nome
|
|
END
|
|
) AS laboratorio_infraestrutura_nome'),
|
|
DB::raw("
|
|
(
|
|
SELECT cadastro_cidadao_telefone.numero
|
|
FROM cadastro_cidadao_telefone
|
|
WHERE cadastro_cidadao_telefone.principal = TRUE
|
|
AND cadastro_cidadao_telefone.cidadao_id = cadastro_cidadao.id
|
|
AND cadastro_cidadao_telefone.deleted_at IS NULL
|
|
LIMIT 1
|
|
) AS paciente_telefone
|
|
"),
|
|
])
|
|
->join(
|
|
'exame_laboratorial_agendamento',
|
|
'exame_laboratorial_agendamento.id',
|
|
'exame_laboratorial_agendamento_exame.agendamento_id'
|
|
)
|
|
->join(
|
|
'parametrizacao_exame',
|
|
'parametrizacao_exame.id',
|
|
'exame_laboratorial_agendamento_exame.exame_id'
|
|
)
|
|
->join(
|
|
'cadastro_grupo',
|
|
'cadastro_grupo.id',
|
|
'parametrizacao_exame.grupo_tipo_id'
|
|
)
|
|
->join(
|
|
'cadastro_paciente',
|
|
'cadastro_paciente.id',
|
|
'exame_laboratorial_agendamento.paciente_id'
|
|
)
|
|
->join(
|
|
'cadastro_usuario',
|
|
'cadastro_usuario.id',
|
|
'exame_laboratorial_agendamento.medico_id'
|
|
)
|
|
->join(
|
|
'cadastro_cidadao as cadastro_cidadao_medico',
|
|
'cadastro_cidadao_medico.id',
|
|
'cadastro_usuario.cidadao_id'
|
|
)
|
|
->join(
|
|
'cadastro_cidadao',
|
|
'cadastro_cidadao.id',
|
|
'cadastro_paciente.cidadao_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura',
|
|
'cadastro_infraestrutura.id',
|
|
'exame_laboratorial_agendamento.infraestrutura_id'
|
|
)
|
|
->join(
|
|
'cadastro_unidade',
|
|
'cadastro_unidade.id',
|
|
'cadastro_infraestrutura.unidade_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura as cadastro_infraestrutura_coleta',
|
|
'cadastro_infraestrutura_coleta.id',
|
|
'exame_laboratorial_agendamento.coleta_infraestrutura_id'
|
|
)
|
|
->join(
|
|
'cadastro_unidade as cadastro_unidade_coleta',
|
|
'cadastro_unidade_coleta.id',
|
|
'cadastro_infraestrutura_coleta.unidade_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura as cadastro_infraestrutura_laboratorio',
|
|
'cadastro_infraestrutura_laboratorio.id',
|
|
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id'
|
|
)
|
|
->join(
|
|
'cadastro_unidade as cadastro_unidade_laboratorio',
|
|
'cadastro_unidade_laboratorio.id',
|
|
'cadastro_infraestrutura_laboratorio.unidade_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_domicilio',
|
|
'cadastro_domicilio.id',
|
|
'cadastro_cidadao.domicilio_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_endereco',
|
|
'cadastro_endereco.id',
|
|
'cadastro_domicilio.endereco_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_logradouro',
|
|
'cadastro_logradouro.id',
|
|
'cadastro_endereco.logradouro_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_logradouro_tipo',
|
|
'cadastro_logradouro_tipo.id',
|
|
'cadastro_logradouro.tipo_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_bairro',
|
|
'cadastro_bairro.id',
|
|
'cadastro_logradouro.bairro_id'
|
|
)
|
|
->leftJoin(
|
|
'cadastro_carteira_identidade',
|
|
function ($leftJoin) {
|
|
|
|
$leftJoin->on('cadastro_carteira_identidade.cidadao_id', 'cadastro_cidadao.id')
|
|
->whereNull('cadastro_carteira_identidade.deleted_at');
|
|
}
|
|
)
|
|
->whereNull('parametrizacao_exame.grupo_tipo_id');
|
|
|
|
if ($request->periodo) {
|
|
|
|
$agendamentosSemGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
'>=',
|
|
$request->periodo['start']
|
|
);
|
|
|
|
$agendamentosSemGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
'<=',
|
|
$request->periodo['end']
|
|
);
|
|
} else {
|
|
|
|
$agendamentosSemGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
$currentDate
|
|
);
|
|
}
|
|
|
|
if ($request->paciente_id) {
|
|
|
|
$agendamentosSemGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.paciente_id',
|
|
$request->paciente_id
|
|
);
|
|
}
|
|
|
|
if ($request->agendamento_numero) {
|
|
|
|
$agendamentosSemGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.agendamento_numero',
|
|
$request->agendamento_numero
|
|
);
|
|
}
|
|
|
|
if ($request->prontuario) {
|
|
|
|
$agendamentosSemGrupo
|
|
->where(
|
|
'cadastro_paciente.prontuario',
|
|
$request->prontuario
|
|
);
|
|
}
|
|
|
|
if ($request->unidade_id) {
|
|
|
|
$agendamentosSemGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.unidade_id',
|
|
$request->unidade_id
|
|
);
|
|
}
|
|
|
|
if ($request->infraestrutura_id) {
|
|
|
|
$agendamentosSemGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.infraestrutura_id',
|
|
$request->infraestrutura_id
|
|
);
|
|
}
|
|
|
|
if ($request->coleta_unidade_id) {
|
|
|
|
$agendamentosSemGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.coleta_unidade_id',
|
|
$request->coleta_unidade_id
|
|
);
|
|
}
|
|
|
|
if ($request->coleta_infraestrutura_id) {
|
|
|
|
$agendamentosSemGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.coleta_infraestrutura_id',
|
|
$request->coleta_infraestrutura_id
|
|
);
|
|
}
|
|
|
|
if ($request->exame_id && !$request->pertence_grupo) {
|
|
|
|
$agendamentosSemGrupo
|
|
->where(
|
|
'parametrizacao_exame.id',
|
|
$request->exame_id
|
|
);
|
|
}
|
|
|
|
$agendamentosSemGrupo
|
|
->where(function($status) {
|
|
|
|
$status
|
|
->where(
|
|
'exame_laboratorial_agendamento_exame.status_id',
|
|
StatusAgendamento::AGENDADO
|
|
)
|
|
->orWhereNull(
|
|
'exame_laboratorial_agendamento_exame.status_id'
|
|
);
|
|
});
|
|
|
|
$agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray();
|
|
} else {
|
|
|
|
$agendamentosSemGrupo = [];
|
|
}
|
|
|
|
$agendamentos = array_merge($agendamentosComGrupo, $agendamentosSemGrupo, $agendamentosSemMapaAgrupado);
|
|
|
|
return [
|
|
'form' => [
|
|
'unidades' => Unidade::orderBy('nome_fantasia')
|
|
->get()
|
|
->map(fn ($query) => [
|
|
'id' => $query->id,
|
|
'nome_fantasia' => $query->nome_fantasia,
|
|
'infraestruturas' => $query->infraestruturas->sortBy('nome')->map(fn ($infraestrutura) => [
|
|
'id' => $infraestrutura->id,
|
|
'nome' => $infraestrutura->nome,
|
|
'principal' => $infraestrutura->principal,
|
|
])->values()
|
|
]),
|
|
'unidades_coleta' => ExameLaboratorialUnidade::where('unidade_coleta', true)
|
|
->get()
|
|
->map(function($query) {
|
|
return [
|
|
'id' => $query->id ?? null,
|
|
'unidade_id' => $query->unidade_id ?? null,
|
|
'infraestrutura_id' => $query->infraestrutura_id ?? null,
|
|
'infraestrutura_nome' => $query->infraestrutura->nome,
|
|
'infraestrutura_principal' => $query->infraestrutura->principal,
|
|
'nome_fantasia' => $query->unidade->nome_fantasia ?? null,
|
|
'unidade_coleta' => $query->unidade_coleta ?? null,
|
|
];
|
|
}),
|
|
'periodo' => [
|
|
'start' => $currentDate,
|
|
'end' => $currentDate,
|
|
],
|
|
'data_atual' => Carbon::now()->format('d/m/Y'),
|
|
'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia,
|
|
'unidade_id' => $currentLotacao->infraestrutura->unidade->id,
|
|
'infraestrutura_id' => $currentLotacao->infraestrutura->id,
|
|
'infraestrutura' => $currentLotacao->infraestrutura->nome,
|
|
'status' => Status::where(function($query) {
|
|
$query->where('id', StatusAgendamento::AGENDADO)
|
|
->orWhere('id', StatusAgendamento::DUPLICIDADE)
|
|
->orWhere('id', StatusAgendamento::REALIZADO)
|
|
->orWhere('id', StatusAgendamento::OUTROS)
|
|
->orWhere('id', StatusAgendamento::FALTOU);
|
|
})
|
|
->orderBy('id')
|
|
->get()
|
|
->map(fn ($status) => [
|
|
'id' => $status->id,
|
|
'descricao' => $status->descricao
|
|
]),
|
|
'sistema' => [
|
|
'ramo_atividade' => [
|
|
'nome' => $cliente->sistema->ramoAtividade->nome ?? null,
|
|
],
|
|
'nome' => $cliente->sistema->nome ?? null,
|
|
'versao' => $cliente->sistema->versao ?? null,
|
|
],
|
|
'unidades_faturamento' => self::getUnidadesFaturamento(),
|
|
],
|
|
'model' => [
|
|
'periodo' => [
|
|
'start' => $currentDate,
|
|
'end' => $currentDate,
|
|
],
|
|
'agendamentos' => $agendamentos,
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function getUnidadesFaturamento()
|
|
{
|
|
return UnidadeFaturamento::get()
|
|
->map(fn($query) => [
|
|
'id' => $query->id,
|
|
'unidade_id' => $query->unidade_id,
|
|
'nomeFantasia' => $query->unidade->nome_fantasia,
|
|
'infraestrutura_id' => $query->infraestrutura_id,
|
|
'infraestrutura_nome' => $query->infraestrutura->nome,
|
|
'profissionais' => $query->unidadeFaturamentoUsuario
|
|
->map(function ($query) {
|
|
return [
|
|
'usuario' => [
|
|
'id' => $query->usuario_id,
|
|
'usuario_ocupacao' => $query->usuario()->withTrashed()->first()->nome . ' - ' . $query->ocupacao->codigo,
|
|
'ocupacao_id' => $query->ocupacao_id,
|
|
],
|
|
];
|
|
}),
|
|
'faturamento_relacionado' => $query->unidadeFaturamentoUnidade
|
|
->map(function ($relacionada) {
|
|
return [
|
|
'id' => $relacionada->id,
|
|
'relacionada_unidade_id' => $relacionada->unidade_id,
|
|
'unidade_id' => $relacionada->unidade_id_principal,
|
|
|
|
'relacionada_infraestrutura_id' => $relacionada->infraestrutura_id,
|
|
'infraestrutura_id' => $relacionada->infraestrutura_id_principal,
|
|
'unidade_relacionada' => $relacionada->infraestrutura->unidade->nome_fantasia,
|
|
'infraestrutura_nome' => $relacionada->infraestrutura->nome,
|
|
];
|
|
})
|
|
]);
|
|
}
|
|
|
|
public static function loadFields(Request $request)
|
|
{
|
|
$now = Carbon::now();
|
|
$currentDate = $now->format('Y-m-d');
|
|
|
|
$agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
'cadastro_grupo.id AS exame_grupo_id',
|
|
'cadastro_grupo.nome AS exame_grupo_nome',
|
|
DB::raw('group_concat(parametrizacao_exame.sigla_exame separator ", ") as sigla_exame'),
|
|
DB::raw('true AS pertence_grupo'),
|
|
DB::raw('true AS filter_grupo'),
|
|
])
|
|
->join(
|
|
'exame_laboratorial_agendamento',
|
|
'exame_laboratorial_agendamento.id',
|
|
'exame_laboratorial_agendamento_exame.agendamento_id'
|
|
)
|
|
->join(
|
|
'parametrizacao_exame',
|
|
'parametrizacao_exame.id',
|
|
'exame_laboratorial_agendamento_exame.exame_id'
|
|
)
|
|
->join(
|
|
'cadastro_grupo',
|
|
'cadastro_grupo.id',
|
|
'parametrizacao_exame.grupo_tipo_id'
|
|
)
|
|
->groupBy([
|
|
'exame_laboratorial_agendamento.id',
|
|
'cadastro_grupo.id',
|
|
]);
|
|
|
|
$agendamentosComGrupo->where('parametrizacao_exame.mapa_agrupado', true);
|
|
|
|
if ($request->periodo) {
|
|
|
|
$agendamentosComGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
'>=',
|
|
$request->periodo['start']
|
|
);
|
|
|
|
$agendamentosComGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
'<=',
|
|
$request->periodo['end']
|
|
);
|
|
} else {
|
|
|
|
$agendamentosComGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
$currentDate
|
|
);
|
|
}
|
|
|
|
$agendamentosComGrupo
|
|
->where(function($status) {
|
|
|
|
$status
|
|
->where(
|
|
'exame_laboratorial_agendamento_exame.status_id',
|
|
StatusAgendamento::AGENDADO
|
|
)
|
|
->orWhereNull(
|
|
'exame_laboratorial_agendamento_exame.status_id'
|
|
);
|
|
});
|
|
|
|
$agendamentosComGrupo = $agendamentosComGrupo->get()->toArray();
|
|
|
|
$agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
'parametrizacao_exame.id AS exame_grupo_id',
|
|
'parametrizacao_exame.nome AS exame_grupo_nome',
|
|
DB::raw('group_concat(parametrizacao_exame.sigla_exame separator ", ") as sigla_exame'),
|
|
DB::raw('true AS pertence_grupo'),
|
|
DB::raw('false AS filter_grupo'),
|
|
])
|
|
->join(
|
|
'exame_laboratorial_agendamento',
|
|
'exame_laboratorial_agendamento.id',
|
|
'exame_laboratorial_agendamento_exame.agendamento_id'
|
|
)
|
|
->join(
|
|
'parametrizacao_exame',
|
|
'parametrizacao_exame.id',
|
|
'exame_laboratorial_agendamento_exame.exame_id'
|
|
)
|
|
->join(
|
|
'cadastro_grupo',
|
|
'cadastro_grupo.id',
|
|
'parametrizacao_exame.grupo_tipo_id'
|
|
)
|
|
->groupBy([
|
|
'exame_laboratorial_agendamento.id',
|
|
'cadastro_grupo.id',
|
|
]);
|
|
|
|
$agendamentosSemMapaAgrupado->where('parametrizacao_exame.mapa_agrupado', false);
|
|
|
|
if ($request->periodo) {
|
|
|
|
$agendamentosSemMapaAgrupado
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
'>=',
|
|
$request->periodo['start']
|
|
);
|
|
|
|
$agendamentosSemMapaAgrupado
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
'<=',
|
|
$request->periodo['end']
|
|
);
|
|
} else {
|
|
|
|
$agendamentosSemMapaAgrupado
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
$currentDate
|
|
);
|
|
}
|
|
|
|
$agendamentosSemMapaAgrupado
|
|
->where(function($status) {
|
|
|
|
$status
|
|
->where(
|
|
'exame_laboratorial_agendamento_exame.status_id',
|
|
StatusAgendamento::AGENDADO
|
|
)
|
|
->orWhereNull(
|
|
'exame_laboratorial_agendamento_exame.status_id'
|
|
);
|
|
});
|
|
|
|
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->get()->toArray();
|
|
|
|
$agendamentosSemGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
'parametrizacao_exame.id AS exame_grupo_id',
|
|
'parametrizacao_exame.nome AS exame_grupo_nome',
|
|
'parametrizacao_exame.sigla_exame AS sigla_exame',
|
|
DB::raw('false AS pertence_grupo'),
|
|
DB::raw('false AS filter_grupo'),
|
|
])
|
|
->join(
|
|
'exame_laboratorial_agendamento',
|
|
'exame_laboratorial_agendamento.id',
|
|
'exame_laboratorial_agendamento_exame.agendamento_id'
|
|
)
|
|
->join(
|
|
'parametrizacao_exame',
|
|
'parametrizacao_exame.id',
|
|
'exame_laboratorial_agendamento_exame.exame_id'
|
|
)
|
|
->whereNull('parametrizacao_exame.grupo_tipo_id');
|
|
|
|
if ($request->periodo) {
|
|
|
|
$agendamentosSemGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
'>=',
|
|
$request->periodo['start']
|
|
);
|
|
|
|
$agendamentosSemGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
'<=',
|
|
$request->periodo['end']
|
|
);
|
|
} else {
|
|
|
|
$agendamentosSemGrupo
|
|
->where(
|
|
'exame_laboratorial_agendamento.data',
|
|
$currentDate
|
|
);
|
|
}
|
|
|
|
$agendamentosSemGrupo
|
|
->where(function($status) {
|
|
|
|
$status
|
|
->where(
|
|
'exame_laboratorial_agendamento_exame.status_id',
|
|
StatusAgendamento::AGENDADO
|
|
)
|
|
->orWhereNull(
|
|
'exame_laboratorial_agendamento_exame.status_id'
|
|
);
|
|
});
|
|
|
|
$agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray();
|
|
|
|
$agendamentos = array_merge($agendamentosComGrupo, $agendamentosSemGrupo, $agendamentosSemMapaAgrupado);
|
|
|
|
return [
|
|
'form' => [],
|
|
'model' => [
|
|
'agendamentos' => $agendamentos,
|
|
'unidades' => self::getUnidades()->map(fn ($unidade) => [
|
|
'unidade_id' => $unidade->unidade_id,
|
|
'unidade_nome' => $unidade->unidade,
|
|
'infraestrutura_id' => $unidade->infraestrutura_id,
|
|
'infraestrutura_nome' => $unidade->infraestrutura_nome,
|
|
]),
|
|
'coletas' => self::getColetas()->map(fn ($unidade) => [
|
|
'coleta_unidade_id' => $unidade->unidade_coleta_id,
|
|
'coleta_unidade_nome' => $unidade->unidade_coleta_nome,
|
|
'coleta_infraestrutura_id' => $unidade->coleta_infraestrutura_id,
|
|
'coleta_infraestrutura_nome' => $unidade->coleta_infraestrutura_nome,
|
|
]),
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function getUnidades() {
|
|
|
|
$now = Carbon::now();
|
|
$currentDate = $now->format('Y-m-d');
|
|
|
|
$unidades = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
|
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
|
|
'cadastro_unidade.nome_fantasia AS unidade',
|
|
DB::raw('
|
|
CASE WHEN cadastro_infraestrutura.principal THEN
|
|
cadastro_unidade.nome_fantasia
|
|
ELSE
|
|
cadastro_infraestrutura.nome
|
|
END AS infraestrutura_nome'),
|
|
])
|
|
->join(
|
|
'exame_laboratorial_agendamento',
|
|
'exame_laboratorial_agendamento.id',
|
|
'exame_laboratorial_agendamento_exame.agendamento_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura',
|
|
'cadastro_infraestrutura.id',
|
|
'exame_laboratorial_agendamento.infraestrutura_id'
|
|
)
|
|
->join(
|
|
'cadastro_unidade',
|
|
'cadastro_unidade.id',
|
|
'cadastro_infraestrutura.unidade_id'
|
|
)
|
|
->join(
|
|
'parametrizacao_exame',
|
|
'parametrizacao_exame.id',
|
|
'exame_laboratorial_agendamento_exame.exame_id'
|
|
)
|
|
->groupBy([
|
|
'exame_laboratorial_agendamento.id',
|
|
]);
|
|
|
|
if (request()->periodo) {
|
|
|
|
$unidades->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
|
$unidades->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
|
} else {
|
|
|
|
$unidades->where('exame_laboratorial_agendamento.data', $currentDate);
|
|
}
|
|
|
|
if (request()->exame_id && request()->filter_grupo) {
|
|
|
|
$unidades->where('parametrizacao_exame.mapa_agrupado', true);
|
|
|
|
$unidades
|
|
->where(
|
|
'parametrizacao_exame.grupo_tipo_id',
|
|
request()->exame_id
|
|
);
|
|
} else if (request()->exame_id && !request()->filter_grupo) {
|
|
|
|
$unidades
|
|
->where(
|
|
'parametrizacao_exame.id',
|
|
request()->exame_id
|
|
);
|
|
}
|
|
|
|
$unidades
|
|
->where(function($status) {
|
|
|
|
$status
|
|
->where(
|
|
'exame_laboratorial_agendamento_exame.status_id',
|
|
StatusAgendamento::AGENDADO
|
|
)
|
|
->orWhereNull(
|
|
'exame_laboratorial_agendamento_exame.status_id'
|
|
);
|
|
});
|
|
|
|
$unidades
|
|
->orderBy(
|
|
'cadastro_unidade.nome_fantasia'
|
|
);
|
|
|
|
return $unidades->get();
|
|
}
|
|
|
|
public static function getColetas() {
|
|
|
|
$now = Carbon::now();
|
|
$currentDate = $now->format('Y-m-d');
|
|
|
|
$coletas = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
|
|
'exame_laboratorial_agendamento.coleta_unidade_id AS unidade_coleta_id',
|
|
'cadastro_unidade_coleta.nome_fantasia AS unidade_coleta_nome',
|
|
DB::raw('
|
|
CASE WHEN cadastro_infraestrutura_coleta.principal THEN
|
|
cadastro_unidade_coleta.nome_fantasia
|
|
ELSE
|
|
cadastro_infraestrutura_coleta.nome
|
|
END AS coleta_infraestrutura_nome'),
|
|
])
|
|
->join(
|
|
'exame_laboratorial_agendamento',
|
|
'exame_laboratorial_agendamento.id',
|
|
'exame_laboratorial_agendamento_exame.agendamento_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura as cadastro_infraestrutura_coleta',
|
|
'cadastro_infraestrutura_coleta.id',
|
|
'exame_laboratorial_agendamento.coleta_infraestrutura_id'
|
|
)
|
|
->join(
|
|
'cadastro_unidade as cadastro_unidade_coleta',
|
|
'cadastro_unidade_coleta.id',
|
|
'cadastro_infraestrutura_coleta.unidade_id'
|
|
)
|
|
->join(
|
|
'parametrizacao_exame',
|
|
'parametrizacao_exame.id',
|
|
'exame_laboratorial_agendamento_exame.exame_id'
|
|
)
|
|
->groupBy([
|
|
'exame_laboratorial_agendamento.id',
|
|
]);
|
|
|
|
if (request()->periodo) {
|
|
|
|
$coletas->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
|
$coletas->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
|
} else {
|
|
|
|
$coletas->where('exame_laboratorial_agendamento.data', $currentDate);
|
|
}
|
|
|
|
if (request()->exame_id && request()->filter_grupo) {
|
|
|
|
$coletas->where('parametrizacao_exame.mapa_agrupado', true);
|
|
|
|
$coletas
|
|
->where(
|
|
'parametrizacao_exame.grupo_tipo_id',
|
|
request()->exame_id
|
|
);
|
|
} else if (request()->exame_id && !request()->filter_grupo) {
|
|
|
|
$coletas
|
|
->where(
|
|
'parametrizacao_exame.id',
|
|
request()->exame_id
|
|
);
|
|
}
|
|
|
|
if (request()->unidade_id) {
|
|
|
|
$coletas->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
|
}
|
|
|
|
if (request()->infraestrutura_id) {
|
|
|
|
$coletas->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
|
|
}
|
|
|
|
if (request()->status_id) {
|
|
|
|
$coletas->where(function($query) {
|
|
|
|
foreach (request()->status_id as $status) {
|
|
|
|
if ($status == StatusAgendamento::NAO_REALIZADO) {
|
|
|
|
$query->orWhereIn(
|
|
'status_id',
|
|
[
|
|
StatusAgendamento::NAO_REALIZADO,
|
|
StatusAgendamento::FALTOU,
|
|
StatusAgendamento::NOVA_COLETA,
|
|
StatusAgendamento::OUTROS,
|
|
]
|
|
);
|
|
} else if ($status == StatusAgendamento::AGENDADO) {
|
|
|
|
$query
|
|
->orWhere('status_id', StatusAgendamento::AGENDADO)
|
|
->orWhereNull('status_id');
|
|
} else {
|
|
|
|
$query->orWhere('status_id', $status);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
return $coletas->get();
|
|
}
|
|
}
|