This commit is contained in:
@@ -0,0 +1,744 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ProntoAtendimento;
|
||||
|
||||
use App\Enums\ProntoAtendimento\Atendimento\MetodoProducao;
|
||||
use App\Services\Service;
|
||||
use App\Utils\Format;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RelatorioProducaoMedicoService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
$params = request()->all();
|
||||
|
||||
$metodoProducao = intval($params['metodo_producao']);
|
||||
|
||||
self::adjustPeriod($params);
|
||||
|
||||
if($metodoProducao === MetodoProducao::ATENDIMENTO) {
|
||||
return self::relatorioPorAtendimento($params);
|
||||
}
|
||||
|
||||
if($metodoProducao === MetodoProducao::FAA) {
|
||||
return self::relatorioPorFaa($params);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static function list()
|
||||
{
|
||||
return [
|
||||
'form' => self::getForm(),
|
||||
'model' => self::getModel(),
|
||||
'additionals' => [
|
||||
'data_atual' => Carbon::today()->format('d/m/Y'),
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
private static function relatorioPorAtendimento($params) {
|
||||
$atendimentos = DB::table('pronto_atendimento_plantao')
|
||||
->select(
|
||||
DB::raw("DATE_FORMAT(pronto_atendimento_plantao.inicio, '%d/%m/%Y %H:%i') AS inicio"),
|
||||
DB::raw("DATE_FORMAT(pronto_atendimento_plantao.fim, '%d/%m/%Y %H:%i') AS fim"),
|
||||
'cadastro_cidadao.nome AS nome_medico',
|
||||
'cadastro_unidade.nome_fantasia AS nome_unidade',
|
||||
DB::raw("
|
||||
CASE WHEN cadastro_infraestrutura.nome IS NOT NULL
|
||||
THEN cadastro_infraestrutura.nome
|
||||
ELSE cadastro_unidade.nome_fantasia END AS nome_infraestrutura
|
||||
"),
|
||||
DB::raw("COUNT(distinct pronto_atendimento_atendimento.id) AS quantidade")
|
||||
)
|
||||
->join(
|
||||
'pronto_atendimento_atendimento_medico',
|
||||
'pronto_atendimento_atendimento_medico.plantao_id',
|
||||
'pronto_atendimento_plantao.id'
|
||||
)
|
||||
->join(
|
||||
'pronto_atendimento_atendimento',
|
||||
'pronto_atendimento_atendimento.id',
|
||||
'pronto_atendimento_atendimento_medico.atendimento_id'
|
||||
)
|
||||
->join(
|
||||
'pronto_atendimento_faa',
|
||||
'pronto_atendimento_faa.id',
|
||||
'pronto_atendimento_atendimento.faa_id'
|
||||
)
|
||||
->join(
|
||||
'pronto_atendimento_atendimento_recepcao',
|
||||
'pronto_atendimento_atendimento_recepcao.atendimento_id',
|
||||
'pronto_atendimento_atendimento_medico.atendimento_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_lotacao',
|
||||
'cadastro_lotacao.id',
|
||||
'pronto_atendimento_plantao.lotacao_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_usuario',
|
||||
'cadastro_usuario.id',
|
||||
'cadastro_lotacao.usuario_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao',
|
||||
'cadastro_cidadao.id',
|
||||
'cadastro_usuario.cidadao_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'cadastro_lotacao.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_infraestrutura.unidade_id'
|
||||
)
|
||||
->where(function ($query) use ($params) {
|
||||
|
||||
$query->whereBetween(
|
||||
'inicio',
|
||||
[
|
||||
$params['periodo']['start'] ?? null,
|
||||
$params['periodo']['end'] ?? null
|
||||
]
|
||||
)
|
||||
->orWhereBetween(
|
||||
'fim',
|
||||
[
|
||||
$params['periodo']['start'] ?? null,
|
||||
$params['periodo']['end'] ?? null
|
||||
]
|
||||
);
|
||||
})
|
||||
->groupBy(
|
||||
DB::raw("DATE_FORMAT(pronto_atendimento_plantao.inicio, '%d/%m/%Y %H:%i')"),
|
||||
DB::raw("DATE_FORMAT(pronto_atendimento_plantao.fim, '%d/%m/%Y %H:%i')"),
|
||||
'cadastro_cidadao.nome',
|
||||
'cadastro_unidade.nome_fantasia',
|
||||
DB::raw("
|
||||
CASE WHEN cadastro_infraestrutura.nome IS NOT NULL
|
||||
THEN cadastro_infraestrutura.nome
|
||||
ELSE cadastro_unidade.nome_fantasia END
|
||||
")
|
||||
)
|
||||
->orderBy('pronto_atendimento_plantao.inicio');
|
||||
|
||||
|
||||
if (!is_null($params['unidade_id'] ?? null)) {
|
||||
|
||||
$atendimentos->where('cadastro_infraestrutura.unidade_id', $params['unidade_id']);
|
||||
}
|
||||
|
||||
if (!is_null($params['local_atendimento_id'] ?? null)) {
|
||||
|
||||
$atendimentos->where('cadastro_infraestrutura.id', $params['local_atendimento_id']);
|
||||
}
|
||||
|
||||
if (!is_null($params['usuario_id'] ?? null)) {
|
||||
|
||||
$atendimentos->where('cadastro_lotacao.usuario_id', $params['usuario_id']);
|
||||
}
|
||||
|
||||
if (!is_null($params['origem_entrada_id'] ?? null)) {
|
||||
|
||||
$atendimentos->where(
|
||||
'pronto_atendimento_atendimento_recepcao.origem_entrada_id',
|
||||
$params['origem_entrada_id']
|
||||
);
|
||||
}
|
||||
|
||||
if (!is_null($params['status'] ?? null)) {
|
||||
|
||||
$atendimentos->where(
|
||||
'pronto_atendimento_faa.finalizado',
|
||||
$params['status']
|
||||
);
|
||||
}
|
||||
|
||||
$atendimentos = $atendimentos->orderBy('pronto_atendimento_plantao.inicio')->get();
|
||||
|
||||
$quantidadeTotal = 0;
|
||||
|
||||
foreach($atendimentos as $atendimento) {
|
||||
|
||||
$quantidadeTotal += $atendimento->quantidade;
|
||||
}
|
||||
|
||||
return [
|
||||
'atendimentos' => $atendimentos,
|
||||
'quantidade_total' => $quantidadeTotal
|
||||
];
|
||||
}
|
||||
|
||||
private static function cbosMedicasPermitidas() {
|
||||
return [
|
||||
'221205', '2231A1', '2231F8', '2231F9', '2231G1', '223119', '223150', '223305', '225103', '225105',
|
||||
'225106', '225109', '225110', '225112', '225115', '225118', '225120', '225121', '225122', '225124',
|
||||
'225125', '225127', '225130', '225133', '225135', '225136', '225139', '225140', '225142', '225145',
|
||||
'225148', '225150', '225151', '225154', '225155', '225160', '225165', '225170', '225175', '225180',
|
||||
'225185', '225195', '225203', '225210', '225215', '225220', '225225', '225230', '225235', '225240',
|
||||
'225250', '225255', '225260', '225265', '225270', '225275', '225280', '225285', '225290', '225295',
|
||||
'225305', '225310', '225315', '225320', '225325', '225330', '225335', '225340', '225345', '225350',
|
||||
'225355'
|
||||
];
|
||||
}
|
||||
|
||||
private static function relatorioPorFaa($params) {
|
||||
$atendimentos = DB::table('pronto_atendimento_plantao')
|
||||
->select(
|
||||
DB::raw("DATE_FORMAT(pronto_atendimento_plantao.inicio, '%d/%m/%Y %H:%i') AS inicio"),
|
||||
DB::raw("DATE_FORMAT(pronto_atendimento_plantao.fim, '%d/%m/%Y %H:%i') AS fim"),
|
||||
'cadastro_cidadao.nome AS nome_medico',
|
||||
'cadastro_unidade.nome_fantasia AS nome_unidade',
|
||||
DB::raw("
|
||||
CASE WHEN cadastro_infraestrutura.nome IS NOT NULL
|
||||
THEN cadastro_infraestrutura.nome
|
||||
ELSE cadastro_unidade.nome_fantasia END AS nome_infraestrutura
|
||||
"),
|
||||
DB::raw("COUNT(distinct pronto_atendimento_atendimento.id) AS quantidade")
|
||||
)
|
||||
->join(
|
||||
'cadastro_lotacao',
|
||||
'cadastro_lotacao.id',
|
||||
'pronto_atendimento_plantao.lotacao_id'
|
||||
)
|
||||
->join(
|
||||
'sigtap.ocupacao',
|
||||
'sigtap.ocupacao.id',
|
||||
'cadastro_lotacao.ocupacao_id'
|
||||
)
|
||||
->join(
|
||||
'pronto_atendimento_atendimento_procedimento',
|
||||
'pronto_atendimento_atendimento_procedimento.plantao_id',
|
||||
'pronto_atendimento_plantao.id'
|
||||
)
|
||||
->join(
|
||||
'pronto_atendimento_atendimento',
|
||||
'pronto_atendimento_atendimento.id',
|
||||
'pronto_atendimento_atendimento_procedimento.atendimento_id'
|
||||
)
|
||||
->join(
|
||||
'pronto_atendimento_faa',
|
||||
'pronto_atendimento_faa.id',
|
||||
'pronto_atendimento_atendimento.faa_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_usuario',
|
||||
'cadastro_usuario.id',
|
||||
'cadastro_lotacao.usuario_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao',
|
||||
'cadastro_cidadao.id',
|
||||
'cadastro_usuario.cidadao_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'cadastro_lotacao.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_infraestrutura.unidade_id'
|
||||
)
|
||||
->where(function ($query) use ($params) {
|
||||
|
||||
$query->whereBetween(
|
||||
'inicio',
|
||||
[
|
||||
$params['periodo']['start'] ?? null,
|
||||
$params['periodo']['end'] ?? null
|
||||
]
|
||||
)
|
||||
->orWhereBetween(
|
||||
'fim',
|
||||
[
|
||||
$params['periodo']['start'] ?? null,
|
||||
$params['periodo']['end'] ?? null
|
||||
]
|
||||
);
|
||||
})
|
||||
->whereIn('sigtap.ocupacao.codigo', self::cbosMedicasPermitidas())
|
||||
->groupBy(
|
||||
DB::raw("DATE_FORMAT(pronto_atendimento_plantao.inicio, '%d/%m/%Y %H:%i')"),
|
||||
DB::raw("DATE_FORMAT(pronto_atendimento_plantao.fim, '%d/%m/%Y %H:%i')"),
|
||||
'cadastro_cidadao.nome',
|
||||
'cadastro_unidade.nome_fantasia',
|
||||
DB::raw("
|
||||
CASE WHEN cadastro_infraestrutura.nome IS NOT NULL
|
||||
THEN cadastro_infraestrutura.nome
|
||||
ELSE cadastro_unidade.nome_fantasia END
|
||||
")
|
||||
)
|
||||
->orderBy('pronto_atendimento_plantao.inicio');
|
||||
|
||||
if (!is_null($params['unidade_id'] ?? null)) {
|
||||
|
||||
$atendimentos->where('cadastro_infraestrutura.unidade_id', $params['unidade_id']);
|
||||
}
|
||||
|
||||
if (!is_null($params['local_atendimento_id'] ?? null)) {
|
||||
|
||||
$atendimentos->where('cadastro_infraestrutura.id', $params['local_atendimento_id']);
|
||||
}
|
||||
|
||||
if (!is_null($params['usuario_id'] ?? null)) {
|
||||
|
||||
$atendimentos->where('cadastro_lotacao.usuario_id', $params['usuario_id']);
|
||||
}
|
||||
|
||||
if (!is_null($params['status'] ?? null)) {
|
||||
|
||||
$atendimentos->where(
|
||||
'pronto_atendimento_faa.finalizado',
|
||||
$params['status']
|
||||
);
|
||||
}
|
||||
|
||||
$atendimentos = $atendimentos->orderBy('pronto_atendimento_plantao.inicio')->get();
|
||||
|
||||
$quantidadeTotal = 0;
|
||||
|
||||
foreach($atendimentos as $atendimento) {
|
||||
|
||||
$quantidadeTotal += $atendimento->quantidade;
|
||||
}
|
||||
|
||||
return [
|
||||
'atendimentos' => $atendimentos,
|
||||
'quantidade_total' => $quantidadeTotal
|
||||
];
|
||||
}
|
||||
|
||||
private static function getForm()
|
||||
{
|
||||
$params = request()->all();
|
||||
|
||||
self::adjustPeriod($params);
|
||||
|
||||
return [
|
||||
'unidades' => self::getUnidades($params),
|
||||
'locais_atendimento' => self::getLocaisAtendimento($params),
|
||||
'metodos_producao' => self::getMetodosProducao(),
|
||||
'usuarios' => self::getUsuarios($params),
|
||||
'origens_entrada' => self::getOrigensEntrada($params),
|
||||
'status' => self::getStatus($params),
|
||||
];
|
||||
}
|
||||
|
||||
private static function adjustPeriod(&$params)
|
||||
{
|
||||
if (!is_null($params['periodo']['start'] ?? null) && $params['periodo']['end']) {
|
||||
|
||||
$params['periodo']['start'] =
|
||||
(Carbon::createFromFormat('d/m/Y', "{$params['periodo']['start']}"))->startOfDay();
|
||||
$params['periodo']['end'] =
|
||||
(Carbon::createFromFormat('d/m/Y', "{$params['periodo']['end']}"))->endOfDay();
|
||||
}
|
||||
}
|
||||
|
||||
private static function getUnidades($params)
|
||||
{
|
||||
return DB::table('cadastro_unidade')
|
||||
->select(
|
||||
'cadastro_unidade.id',
|
||||
'cadastro_unidade.nome_fantasia',
|
||||
)
|
||||
->whereExists(function($query) use ($params) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('pronto_atendimento_plantao')
|
||||
->join(
|
||||
'cadastro_lotacao',
|
||||
'cadastro_lotacao.id',
|
||||
'pronto_atendimento_plantao.lotacao_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'cadastro_lotacao.infraestrutura_id'
|
||||
)
|
||||
->whereColumn('cadastro_infraestrutura.unidade_id', 'cadastro_unidade.id')
|
||||
->where(function ($query) use ($params) {
|
||||
|
||||
$query->whereBetween(
|
||||
'inicio',
|
||||
[
|
||||
$params['periodo']['start'] ?? null,
|
||||
$params['periodo']['end'] ?? null
|
||||
]
|
||||
)
|
||||
->orWhereBetween(
|
||||
'fim',
|
||||
[
|
||||
$params['periodo']['start'] ?? null,
|
||||
$params['periodo']['end'] ?? null
|
||||
]
|
||||
);
|
||||
});
|
||||
})
|
||||
->orderBy('cadastro_unidade.nome_fantasia')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getLocaisAtendimento($params)
|
||||
{
|
||||
$infras = DB::table('cadastro_infraestrutura')
|
||||
->select(
|
||||
'cadastro_infraestrutura.id',
|
||||
DB::raw(
|
||||
"CASE WHEN cadastro_infraestrutura.nome IS NOT NULL
|
||||
THEN cadastro_infraestrutura.nome
|
||||
ELSE cadastro_unidade.nome_fantasia END AS nome"
|
||||
),
|
||||
)
|
||||
->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
||||
->whereExists(function($query) use ($params) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('pronto_atendimento_plantao')
|
||||
->join(
|
||||
'cadastro_lotacao',
|
||||
'cadastro_lotacao.id',
|
||||
'pronto_atendimento_plantao.lotacao_id'
|
||||
)
|
||||
->whereColumn('cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id')
|
||||
->where(function ($query) use ($params) {
|
||||
|
||||
$query->whereBetween(
|
||||
'inicio',
|
||||
[
|
||||
$params['periodo']['start'] ?? null,
|
||||
$params['periodo']['end'] ?? null
|
||||
]
|
||||
)
|
||||
->orWhereBetween(
|
||||
'fim',
|
||||
[
|
||||
$params['periodo']['start'] ?? null,
|
||||
$params['periodo']['end'] ?? null
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
if (!is_null($params['unidade_id'] ?? null)) {
|
||||
|
||||
$infras->where('cadastro_unidade.id', $params['unidade_id']);
|
||||
}
|
||||
|
||||
return $infras->orderBy(
|
||||
DB::raw(
|
||||
"CASE WHEN cadastro_infraestrutura.nome IS NOT NULL
|
||||
THEN cadastro_infraestrutura.nome
|
||||
ELSE cadastro_unidade.nome_fantasia END"
|
||||
))
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getMetodosProducao()
|
||||
{
|
||||
$metodos = (new \ReflectionClass(MetodoProducao::class))->getConstants();
|
||||
|
||||
$metodosFormatados = [];
|
||||
foreach ($metodos as $nome => $valor) {
|
||||
$metodosFormatados[] = ['nome' => $nome, 'id' => $valor];
|
||||
}
|
||||
|
||||
return $metodosFormatados;
|
||||
}
|
||||
|
||||
private static function getUsuarios($params)
|
||||
{
|
||||
$joinReferenciaPlantao = 'pronto_atendimento_atendimento_medico';
|
||||
|
||||
if($params && $params['metodo_producao'] == MetodoProducao::FAA) {
|
||||
$joinReferenciaPlantao = 'pronto_atendimento_atendimento_procedimento';
|
||||
}
|
||||
|
||||
$usuarios = DB::table('cadastro_usuario')
|
||||
->select(
|
||||
'cadastro_usuario.id',
|
||||
'cadastro_cidadao.nome'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao',
|
||||
'cadastro_cidadao.id',
|
||||
'cadastro_usuario.cidadao_id'
|
||||
)
|
||||
->whereExists(function ($query) use ($params, $joinReferenciaPlantao) {
|
||||
|
||||
$query->select(DB::raw(1))
|
||||
->from('pronto_atendimento_plantao')
|
||||
->join(
|
||||
$joinReferenciaPlantao,
|
||||
"{$joinReferenciaPlantao}.plantao_id",
|
||||
'pronto_atendimento_plantao.id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_lotacao',
|
||||
'cadastro_lotacao.id',
|
||||
'pronto_atendimento_plantao.lotacao_id'
|
||||
)
|
||||
->join(
|
||||
'sigtap.ocupacao',
|
||||
'sigtap.ocupacao.id',
|
||||
'cadastro_lotacao.ocupacao_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'cadastro_lotacao.infraestrutura_id'
|
||||
)
|
||||
->whereColumn('cadastro_lotacao.usuario_id', 'cadastro_usuario.id')
|
||||
->where(function ($query) use ($params) {
|
||||
|
||||
$query->whereBetween(
|
||||
'inicio',
|
||||
[
|
||||
$params['periodo']['start'] ?? null,
|
||||
$params['periodo']['end'] ?? null
|
||||
]
|
||||
)
|
||||
->orWhereBetween(
|
||||
'fim',
|
||||
[
|
||||
$params['periodo']['start'] ?? null,
|
||||
$params['periodo']['end'] ?? null
|
||||
]
|
||||
);
|
||||
})
|
||||
->whereIn('sigtap.ocupacao.codigo', self::cbosMedicasPermitidas());
|
||||
|
||||
if (!is_null($params['unidade_id'] ?? null)) {
|
||||
|
||||
$query->where('cadastro_infraestrutura.unidade_id', $params['unidade_id']);
|
||||
}
|
||||
|
||||
if (!is_null($params['local_atendimento_id'] ?? null)) {
|
||||
|
||||
$query->where('cadastro_infraestrutura.id', $params['local_atendimento_id']);
|
||||
}
|
||||
});
|
||||
|
||||
return $usuarios
|
||||
->orderBy('cadastro_cidadao.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getOrigensEntrada($params)
|
||||
{
|
||||
|
||||
$joinReferenciaPlantao = 'pronto_atendimento_atendimento_medico';
|
||||
|
||||
if($params && $params['metodo_producao'] == MetodoProducao::FAA) {
|
||||
$joinReferenciaPlantao = 'pronto_atendimento_atendimento_procedimento';
|
||||
}
|
||||
|
||||
$origensEntrada = DB::table('pronto_atendimento_origem_entrada')
|
||||
->select(
|
||||
'pronto_atendimento_origem_entrada.id',
|
||||
'pronto_atendimento_origem_entrada.nome'
|
||||
)
|
||||
->whereExists(function ($query) use ($params, $joinReferenciaPlantao) {
|
||||
|
||||
$query->select(DB::raw(1))
|
||||
->from('pronto_atendimento_plantao')
|
||||
->join(
|
||||
$joinReferenciaPlantao,
|
||||
"{$joinReferenciaPlantao}.plantao_id",
|
||||
'pronto_atendimento_plantao.id'
|
||||
)
|
||||
->join(
|
||||
'pronto_atendimento_atendimento_recepcao',
|
||||
'pronto_atendimento_atendimento_recepcao.atendimento_id',
|
||||
"{$joinReferenciaPlantao}.atendimento_id"
|
||||
)
|
||||
->join(
|
||||
'cadastro_lotacao',
|
||||
'cadastro_lotacao.id',
|
||||
'pronto_atendimento_plantao.lotacao_id'
|
||||
)
|
||||
->join(
|
||||
'sigtap.ocupacao',
|
||||
'sigtap.ocupacao.id',
|
||||
'cadastro_lotacao.ocupacao_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'cadastro_lotacao.infraestrutura_id'
|
||||
)
|
||||
->whereColumn(
|
||||
'pronto_atendimento_atendimento_recepcao.origem_entrada_id',
|
||||
'pronto_atendimento_origem_entrada.id'
|
||||
)
|
||||
->where(function ($query) use ($params) {
|
||||
|
||||
$query->whereBetween(
|
||||
'inicio',
|
||||
[
|
||||
$params['periodo']['start'] ?? null,
|
||||
$params['periodo']['end'] ?? null
|
||||
]
|
||||
)
|
||||
->orWhereBetween(
|
||||
'fim',
|
||||
[
|
||||
$params['periodo']['start'] ?? null,
|
||||
$params['periodo']['end'] ?? null
|
||||
]
|
||||
);
|
||||
})
|
||||
->whereIn('sigtap.ocupacao.codigo', self::cbosMedicasPermitidas());
|
||||
|
||||
if (!is_null($params['unidade_id'] ?? null)) {
|
||||
|
||||
$query->where('cadastro_infraestrutura.unidade_id', $params['unidade_id']);
|
||||
}
|
||||
|
||||
if (!is_null($params['local_atendimento_id'] ?? null)) {
|
||||
|
||||
$query->where('cadastro_infraestrutura.id', $params['local_atendimento_id']);
|
||||
}
|
||||
|
||||
if (!is_null($params['usuario_id'] ?? null)) {
|
||||
|
||||
$query->where('cadastro_lotacao.usuario_id', $params['usuario_id']);
|
||||
}
|
||||
});
|
||||
|
||||
return $origensEntrada
|
||||
->orderBy('pronto_atendimento_origem_entrada.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getStatus($params)
|
||||
{
|
||||
$joinReferenciaPlantao = 'pronto_atendimento_atendimento_medico';
|
||||
|
||||
if($params && $params['metodo_producao'] == MetodoProducao::FAA) {
|
||||
$joinReferenciaPlantao = 'pronto_atendimento_atendimento_procedimento';
|
||||
}
|
||||
|
||||
$status = DB::table('pronto_atendimento_faa')
|
||||
->select(
|
||||
'pronto_atendimento_faa.finalizado',
|
||||
)
|
||||
->whereExists(function ($query) use ($params, $joinReferenciaPlantao) {
|
||||
|
||||
$query->select(DB::raw(1))
|
||||
->from('pronto_atendimento_plantao')
|
||||
->join(
|
||||
$joinReferenciaPlantao,
|
||||
"{$joinReferenciaPlantao}.plantao_id",
|
||||
'pronto_atendimento_plantao.id'
|
||||
)
|
||||
->join(
|
||||
'pronto_atendimento_atendimento_recepcao',
|
||||
'pronto_atendimento_atendimento_recepcao.atendimento_id',
|
||||
"{$joinReferenciaPlantao}.atendimento_id"
|
||||
)
|
||||
->join(
|
||||
'pronto_atendimento_atendimento',
|
||||
'pronto_atendimento_atendimento.id',
|
||||
"{$joinReferenciaPlantao}.atendimento_id"
|
||||
)
|
||||
->join(
|
||||
'cadastro_lotacao',
|
||||
'cadastro_lotacao.id',
|
||||
'pronto_atendimento_plantao.lotacao_id'
|
||||
)
|
||||
->join(
|
||||
'sigtap.ocupacao',
|
||||
'sigtap.ocupacao.id',
|
||||
'cadastro_lotacao.ocupacao_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'cadastro_lotacao.infraestrutura_id'
|
||||
)
|
||||
->whereColumn('pronto_atendimento_atendimento.faa_id', 'pronto_atendimento_faa.id')
|
||||
->where(function ($query) use ($params) {
|
||||
|
||||
$query->whereBetween(
|
||||
'inicio',
|
||||
[
|
||||
$params['periodo']['start'] ?? null,
|
||||
$params['periodo']['end'] ?? null
|
||||
]
|
||||
)
|
||||
->orWhereBetween(
|
||||
'fim',
|
||||
[
|
||||
$params['periodo']['start'] ?? null,
|
||||
$params['periodo']['end'] ?? null
|
||||
]
|
||||
);
|
||||
})
|
||||
->whereIn('sigtap.ocupacao.codigo', self::cbosMedicasPermitidas());
|
||||
|
||||
if (!is_null($params['unidade_id'] ?? null)) {
|
||||
|
||||
$query->where('cadastro_infraestrutura.unidade_id', $params['unidade_id']);
|
||||
}
|
||||
|
||||
if (!is_null($params['local_atendimento_id'] ?? null)) {
|
||||
|
||||
$query->where('cadastro_infraestrutura.id', $params['local_atendimento_id']);
|
||||
}
|
||||
|
||||
if (!is_null($params['usuario_id'] ?? null)) {
|
||||
|
||||
$query->where('cadastro_lotacao.usuario_id', $params['usuario_id']);
|
||||
}
|
||||
|
||||
if (!is_null($params['origem_entrada_id'] ?? null)) {
|
||||
|
||||
$query->where(
|
||||
'pronto_atendimento_atendimento_recepcao.origem_entrada_id',
|
||||
$params['origem_entrada_id']
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
return $status
|
||||
->distinct()
|
||||
->orderBy('pronto_atendimento_faa.finalizado')
|
||||
->get()
|
||||
->map(function($status) {
|
||||
return [
|
||||
'valor' => $status->finalizado ?? false,
|
||||
'nome' => $status->finalizado ? 'FINALIZADO' : 'EM ABERTO',
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
private static function hasParamId($params, $param)
|
||||
{
|
||||
return isset($params[$param]) && $params[$param] > 0;
|
||||
}
|
||||
|
||||
private static function getModel()
|
||||
{
|
||||
return [
|
||||
'periodo' => [
|
||||
'start' => null,
|
||||
'end' => null,
|
||||
],
|
||||
'unidade_id' => null,
|
||||
'localAtendimento_id' => null,
|
||||
'metodo_producao' => MetodoProducao::ATENDIMENTO,
|
||||
'usuario_id' => null,
|
||||
'origem_entrada_id' => null,
|
||||
'servico_id' => null,
|
||||
'status' => null,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user