722 lines
31 KiB
PHP
722 lines
31 KiB
PHP
<?php
|
|
|
|
namespace App\Services\ExameLaboratorial;
|
|
|
|
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
|
use App\Models\Cadastro\Estabelecimento\Unidade;
|
|
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\Unidade as ExameLaboratorialUnidade;
|
|
use App\Models\Intranet\Cliente;
|
|
use App\Services\Service;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class RelatorioQuantitativoExameSolicitadoService extends Service
|
|
{
|
|
|
|
public static function list(Request $request)
|
|
{
|
|
$now = Carbon::now();
|
|
$currentDate = $now->format('Y-m-d');
|
|
$currentLotacao = self::getCurrentLotacao();
|
|
|
|
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
'cadastro_unidade_coleta.nome_fantasia AS coleta_nome_fantasia',
|
|
'cadastro_unidade_coleta.id AS unidade_id_coleta',
|
|
'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia',
|
|
'parametrizacao_exame.codigo AS procedimento_codigo',
|
|
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
|
|
|
|
'exame_laboratorial_agendamento_exame.agendamento_id',
|
|
'exame_laboratorial_agendamento_exame.exame_id',
|
|
|
|
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('upper(parametrizacao_exame.nome) AS exame_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("COUNT(parametrizacao_exame.id) AS quantidade"),
|
|
])
|
|
->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(
|
|
'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'
|
|
)
|
|
->join(
|
|
'parametrizacao_exame',
|
|
'parametrizacao_exame.id',
|
|
'exame_laboratorial_agendamento_exame.exame_id'
|
|
)
|
|
->groupBy([
|
|
'exame_laboratorial_agendamento.unidade_id',
|
|
]);
|
|
|
|
if ($request->periodo) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
|
|
$agendamentos->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
|
|
} else {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.data', $currentDate);
|
|
}
|
|
|
|
if ($request->exame_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento_exame.exame_id', $request->exame_id);
|
|
}
|
|
|
|
if ($request->coleta_unidade_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.coleta_unidade_id', $request->coleta_unidade_id);
|
|
}
|
|
|
|
if ($request->coleta_infraestrutura_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', $request->coleta_infraestrutura_id);
|
|
}
|
|
|
|
if ($request->laboratorio_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.laboratorio_unidade_id', $request->laboratorio_id);
|
|
}
|
|
|
|
if ($request->laboratorio_infraestrutura_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', $request->laboratorio_infraestrutura_id);
|
|
}
|
|
|
|
$agendamentos = $agendamentos->get();
|
|
|
|
if (request()->periodo) {
|
|
|
|
$periodoStart = Carbon::create(request()->periodo['start'])->format('Y-m-d');
|
|
$periodoEnd = Carbon::create(request()->periodo['end'])->format('Y-m-d');
|
|
} else {
|
|
|
|
$periodoStart = $currentDate;
|
|
$periodoEnd = $currentDate;
|
|
}
|
|
|
|
return [
|
|
'form' => [
|
|
'periodo' => [
|
|
'start' => $currentDate,
|
|
'end' => $currentDate,
|
|
],
|
|
'data_atual' => Carbon::now()->format('d/m/Y'),
|
|
'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,
|
|
];
|
|
}),
|
|
'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia,
|
|
'unidade_id' => $currentLotacao->infraestrutura->unidade->id,
|
|
'infraestrutura_id' => $currentLotacao->infraestrutura->id,
|
|
'infraestrutura' => $currentLotacao->infraestrutura->nome,
|
|
],
|
|
'model' => [
|
|
'unidade_coleta' => $agendamentos->map(
|
|
fn ($coleta) => [
|
|
'coleta_nome_fantasia' => $coleta->coleta_nome_fantasia,
|
|
'coleta_infraestrutura_nome' => $coleta->coleta_infraestrutura_nome,
|
|
'laboratorio_nome_fantasia' => $coleta->laboratorio_nome_fantasia,
|
|
'laboratorio_infraestrutura_nome' => $coleta->laboratorio_infraestrutura_nome,
|
|
'exames' => AgendamentoExame::whereHas('agendamento', function ($query) use ($periodoStart, $periodoEnd, $coleta) {
|
|
|
|
$query
|
|
->where('data', '>=', $periodoStart)
|
|
->where('data', '<=', $periodoEnd)
|
|
->where('coleta_infraestrutura_id', $coleta->coleta_infraestrutura_id);
|
|
|
|
if (request()->coleta_unidade_id) {
|
|
|
|
$query->where('coleta_unidade_id', request()->coleta_unidade_id);
|
|
}
|
|
|
|
if (request()->coleta_infraestrutura_id) {
|
|
|
|
$query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
|
|
}
|
|
|
|
if (request()->laboratorio_id) {
|
|
|
|
$query->where('laboratorio_unidade_id', request()->laboratorio_id);
|
|
}
|
|
|
|
if (request()->laboratorio_infraestrutura_id) {
|
|
|
|
$query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
|
|
}
|
|
})
|
|
->where(function ($query) {
|
|
|
|
if (request()->exame_id) {
|
|
|
|
$query->where('exame_id', request()->exame_id);
|
|
}
|
|
})
|
|
->join(
|
|
'parametrizacao_exame',
|
|
'parametrizacao_exame.id',
|
|
'exame_laboratorial_agendamento_exame.exame_id'
|
|
)
|
|
->leftJoin('sigtap.procedimento', function ($join) {
|
|
|
|
$join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo')
|
|
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
|
|
|
$query->select('sigtap.competencia.id')
|
|
->from('sigtap.competencia')
|
|
->orderByDesc('sigtap.competencia.valor')
|
|
->limit(1);
|
|
});
|
|
})
|
|
->groupBy('exame_id')
|
|
->select([
|
|
'parametrizacao_exame.id',
|
|
'parametrizacao_exame.nome',
|
|
'procedimento.codigo as procedimento_codigo',
|
|
'procedimento.nome AS procedimento_nome',
|
|
DB::raw('count(exame_laboratorial_agendamento_exame.id) AS quantidade'),
|
|
])
|
|
->get(),
|
|
'total_exames' => AgendamentoExame::whereHas('agendamento', function ($query) use ($coleta, $periodoStart, $periodoEnd) {
|
|
|
|
$query
|
|
->where('coleta_infraestrutura_id', $coleta->coleta_infraestrutura_id)
|
|
->where('data', '>=', $periodoStart)
|
|
->where('data', '<=', $periodoEnd);
|
|
|
|
if (request()->agendamento_id) {
|
|
|
|
$query->where('agendamento_id', request()->agendamento_id);
|
|
}
|
|
|
|
if (request()->unidade_id) {
|
|
|
|
$query->where('unidade_id', request()->unidade_id);
|
|
}
|
|
|
|
if (request()->infraestrutura_id) {
|
|
|
|
$query->where('infraestrutura_id', request()->infraestrutura_id);
|
|
}
|
|
|
|
if (request()->coleta_unidade_id) {
|
|
|
|
$query->where('coleta_unidade_id', request()->coleta_unidade_id);
|
|
}
|
|
|
|
if (request()->coleta_infraestrutura_id) {
|
|
|
|
$query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
|
|
}
|
|
|
|
if (request()->laboratorio_id) {
|
|
|
|
$query->where('laboratorio_unidade_id', request()->laboratorio_id);
|
|
}
|
|
|
|
if (request()->laboratorio_infraestrutura_id) {
|
|
|
|
$query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
|
|
}
|
|
})
|
|
->where(function ($query) {
|
|
|
|
if (request()->exame_id) {
|
|
|
|
$query->where('exame_id', request()->exame_id);
|
|
}
|
|
})
|
|
->count(),
|
|
]
|
|
),
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function loadFields(Request $request)
|
|
{
|
|
$now = Carbon::now();
|
|
$currentDate = $now->format('Y-m-d');
|
|
|
|
$exames = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
'exame_laboratorial_agendamento.id AS agendamento_id',
|
|
])
|
|
->join(
|
|
'exame_laboratorial_agendamento',
|
|
'exame_laboratorial_agendamento.id',
|
|
'exame_laboratorial_agendamento_exame.agendamento_id'
|
|
)
|
|
->groupBy([
|
|
'exame_laboratorial_agendamento.id',
|
|
]);
|
|
|
|
if ($request->periodo) {
|
|
|
|
$exames->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
|
|
$exames->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
|
|
} else {
|
|
|
|
$exames->where('exame_laboratorial_agendamento.data', $currentDate);
|
|
}
|
|
|
|
$exames = $exames->get();
|
|
|
|
$unidades = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
'exame_laboratorial_agendamento.id AS agendamento_id',
|
|
'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',
|
|
'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'
|
|
)
|
|
->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) {
|
|
|
|
$unidades->where('exame_laboratorial_agendamento_exame.exame_id', $request->exame_id);
|
|
}
|
|
|
|
$unidades = $unidades->get();
|
|
|
|
$laboratorios = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
'exame_laboratorial_agendamento.id AS agendamento_id',
|
|
'cadastro_unidade_laboratorio.nome_fantasia AS unidade_laboratorio_nome',
|
|
'exame_laboratorial_agendamento.laboratorio_unidade_id AS laboratorio_unidade_id',
|
|
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id AS laboratorio_infraestrutura_id',
|
|
DB::raw('
|
|
CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN
|
|
cadastro_unidade_laboratorio.nome_fantasia
|
|
ELSE
|
|
cadastro_infraestrutura_laboratorio.nome
|
|
END AS laboratorio_infraestrutura_nome'),
|
|
])
|
|
->join(
|
|
'exame_laboratorial_agendamento',
|
|
'exame_laboratorial_agendamento.id',
|
|
'exame_laboratorial_agendamento_exame.agendamento_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'
|
|
)
|
|
->groupBy([
|
|
'exame_laboratorial_agendamento.id',
|
|
]);
|
|
|
|
if ($request->periodo) {
|
|
|
|
$laboratorios->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
|
|
$laboratorios->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
|
|
} else {
|
|
|
|
$laboratorios->where('exame_laboratorial_agendamento.data', $currentDate);
|
|
}
|
|
|
|
if ($request->exame_id) {
|
|
|
|
$laboratorios->where('exame_laboratorial_agendamento_exame.exame_id', $request->exame_id);
|
|
}
|
|
|
|
if ($request->coleta_unidade_id) {
|
|
|
|
$laboratorios->where('exame_laboratorial_agendamento.coleta_unidade_id', $request->coleta_unidade_id);
|
|
}
|
|
|
|
if ($request->coleta_infraestrutura_id) {
|
|
|
|
$laboratorios->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', $request->coleta_infraestrutura_id);
|
|
}
|
|
|
|
$laboratorios = $laboratorios->get();
|
|
|
|
return [
|
|
'form' => [
|
|
'periodo' => [
|
|
'start' => $currentDate,
|
|
'end' => $currentDate,
|
|
],
|
|
'data_atual' => Carbon::now()->format('d/m/Y'),
|
|
],
|
|
'model' => [
|
|
'unidades' => $unidades->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,
|
|
]),
|
|
'laboratorios' => $laboratorios->map(fn ($laboratorio) => [
|
|
'laboratorio_id' => $laboratorio->laboratorio_unidade_id,
|
|
'unidade_laboratorio_nome' => $laboratorio->unidade_laboratorio_nome,
|
|
'laboratorio_infraestrutura_id' => $laboratorio->laboratorio_infraestrutura_id,
|
|
'laboratorio_infraestrutura_nome' => $laboratorio->laboratorio_infraestrutura_nome,
|
|
]),
|
|
'exames' => $exames->map(fn ($exame) => [
|
|
'exames' => AgendamentoExame::where('agendamento_id', $exame->agendamento_id)
|
|
->get()
|
|
->map(function ($query) {
|
|
return [
|
|
'id' => $query->exame_id,
|
|
'nome' => mb_strtoupper($query->exame->nome),
|
|
];
|
|
})
|
|
->values(),
|
|
]),
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function index()
|
|
{
|
|
$now = Carbon::now();
|
|
$currentDate = $now->format('Y-m-d');
|
|
$currentLotacao = self::getCurrentLotacao();
|
|
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
|
|
|
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
'cadastro_unidade_coleta.nome_fantasia AS coleta_nome_fantasia',
|
|
'cadastro_unidade_coleta.id AS unidade_id_coleta',
|
|
'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia',
|
|
'parametrizacao_exame.codigo AS procedimento_codigo',
|
|
'exame_laboratorial_agendamento.coleta_infraestrutura_id AS coleta_infraestrutura_id',
|
|
|
|
'exame_laboratorial_agendamento_exame.agendamento_id',
|
|
'exame_laboratorial_agendamento_exame.exame_id',
|
|
|
|
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('upper(parametrizacao_exame.nome) AS exame_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("COUNT(parametrizacao_exame.id) AS quantidade"),
|
|
])
|
|
->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(
|
|
'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'
|
|
)
|
|
->join(
|
|
'parametrizacao_exame',
|
|
'parametrizacao_exame.id',
|
|
'exame_laboratorial_agendamento_exame.exame_id'
|
|
)
|
|
->groupBy([
|
|
'exame_laboratorial_agendamento.unidade_id',
|
|
]);
|
|
|
|
if (request()->periodo) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
|
$agendamentos->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
|
} else {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.data', $currentDate);
|
|
}
|
|
|
|
if (request()->exame_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento_exame.exame_id', request()->exame_id);
|
|
}
|
|
|
|
if (request()->coleta_unidade_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.coleta_unidade_id', request()->coleta_unidade_id);
|
|
}
|
|
|
|
if (request()->coleta_infraestrutura_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
|
|
}
|
|
|
|
if (request()->laboratorio_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id);
|
|
}
|
|
|
|
if (request()->laboratorio_infraestrutura_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
|
|
}
|
|
|
|
$agendamentos = $agendamentos->get();
|
|
|
|
if (request()->periodo) {
|
|
|
|
$periodoStart = Carbon::create(request()->periodo['start'])->format('Y-m-d');
|
|
$periodoEnd = Carbon::create(request()->periodo['end'])->format('Y-m-d');
|
|
|
|
$periodoStartFilter = Carbon::create(request()->periodo['start'])->format('d/m/Y');
|
|
$periodoEndFilter = Carbon::create(request()->periodo['end'])->format('d/m/Y');
|
|
} else {
|
|
|
|
$periodoStart = $currentDate;
|
|
$periodoEnd = $currentDate;
|
|
|
|
$periodoStartFilter = $currentDate;
|
|
$periodoEndFilter = $currentDate;
|
|
}
|
|
|
|
return [
|
|
'coleta_unidade_nome' => request()->coleta_unidade_nome ?? 'TODAS',
|
|
'coleta_infraestrutura_nome' => request()->coleta_infraestrutura_nome ?? 'TODOS',
|
|
'laboratorio_nome_fantasia' => request()->laboratorio_nome ?? 'TODOS',
|
|
'laboratorio_infraestrutura_nome' => request()->laboratorio_infraestrutura_nome ?? 'TODOS',
|
|
'exame_nome' => request()->exame_id && request()->exame_nome ? request()->exame_nome : 'TODOS',
|
|
'periodo' => [
|
|
'start' => $periodoStartFilter,
|
|
'end' => $periodoEndFilter,
|
|
],
|
|
'colunas' => collect(request()->selectedCols),
|
|
'unidade_coleta' => $agendamentos->map(
|
|
fn ($coleta) => [
|
|
'coleta_nome_fantasia' => $coleta->coleta_nome_fantasia,
|
|
'coleta_infraestrutura_nome' => $coleta->coleta_infraestrutura_nome,
|
|
'laboratorio_nome_fantasia' => $coleta->laboratorio_nome_fantasia,
|
|
'laboratorio_infraestrutura_nome' => $coleta->laboratorio_infraestrutura_nome,
|
|
'exames' => AgendamentoExame::whereHas('agendamento', function ($query) use ($periodoStart, $periodoEnd, $coleta) {
|
|
|
|
$query
|
|
->where('data', '>=', $periodoStart)
|
|
->where('data', '<=', $periodoEnd)
|
|
->where('coleta_infraestrutura_id', $coleta->coleta_infraestrutura_id);
|
|
|
|
if (request()->coleta_unidade_id) {
|
|
|
|
$query->where('coleta_unidade_id', request()->coleta_unidade_id);
|
|
}
|
|
|
|
if (request()->coleta_infraestrutura_id) {
|
|
|
|
$query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
|
|
}
|
|
|
|
if (request()->laboratorio_id) {
|
|
|
|
$query->where('laboratorio_unidade_id', request()->laboratorio_id);
|
|
}
|
|
|
|
if (request()->laboratorio_infraestrutura_id) {
|
|
|
|
$query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
|
|
}
|
|
})
|
|
->where(function ($query) {
|
|
|
|
if (request()->exame_id) {
|
|
|
|
$query->where('exame_id', request()->exame_id);
|
|
}
|
|
})
|
|
->join(
|
|
'parametrizacao_exame',
|
|
'parametrizacao_exame.id',
|
|
'exame_laboratorial_agendamento_exame.exame_id'
|
|
)
|
|
->leftJoin('sigtap.procedimento', function ($join) {
|
|
|
|
$join->on('sigtap.procedimento.codigo', 'parametrizacao_exame.codigo')
|
|
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
|
|
|
$query->select('sigtap.competencia.id')
|
|
->from('sigtap.competencia')
|
|
->orderByDesc('sigtap.competencia.valor')
|
|
->limit(1);
|
|
});
|
|
})
|
|
->groupBy('exame_id')
|
|
->select([
|
|
'parametrizacao_exame.id',
|
|
'parametrizacao_exame.nome',
|
|
'procedimento.codigo as procedimento_codigo',
|
|
'procedimento.nome AS procedimento_nome',
|
|
DB::raw('count(exame_laboratorial_agendamento_exame.id) AS quantidade'),
|
|
])
|
|
->get(),
|
|
'total_exames' => AgendamentoExame::whereHas('agendamento', function ($query) use ($coleta, $periodoStart, $periodoEnd) {
|
|
|
|
$query
|
|
->where('coleta_infraestrutura_id', $coleta->coleta_infraestrutura_id)
|
|
->where('data', '>=', $periodoStart)
|
|
->where('data', '<=', $periodoEnd);
|
|
|
|
if (request()->agendamento_id) {
|
|
|
|
$query->where('agendamento_id', request()->agendamento_id);
|
|
}
|
|
|
|
if (request()->unidade_id) {
|
|
|
|
$query->where('unidade_id', request()->unidade_id);
|
|
}
|
|
|
|
if (request()->infraestrutura_id) {
|
|
|
|
$query->where('infraestrutura_id', request()->infraestrutura_id);
|
|
}
|
|
|
|
if (request()->coleta_unidade_id) {
|
|
|
|
$query->where('coleta_unidade_id', request()->coleta_unidade_id);
|
|
}
|
|
|
|
if (request()->coleta_infraestrutura_id) {
|
|
|
|
$query->where('coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
|
|
}
|
|
|
|
if (request()->laboratorio_id) {
|
|
|
|
$query->where('laboratorio_unidade_id', request()->laboratorio_id);
|
|
}
|
|
|
|
if (request()->laboratorio_infraestrutura_id) {
|
|
|
|
$query->where('laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
|
|
}
|
|
})
|
|
->where(function ($query) {
|
|
|
|
if (request()->exame_id) {
|
|
|
|
$query->where('exame_id', request()->exame_id);
|
|
}
|
|
})->count(),
|
|
]
|
|
),
|
|
];
|
|
}
|
|
}
|