1074 lines
42 KiB
PHP
1074 lines
42 KiB
PHP
<?php
|
|
|
|
namespace App\Services\ExameLaboratorial;
|
|
|
|
use App\Enums\Cadastro\SujeitoAtencao\PreferenciaNome;
|
|
|
|
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\Support\Facades\DB;
|
|
|
|
class RelatorioFinanceiroPacienteService extends Service
|
|
{
|
|
|
|
public static function list()
|
|
{
|
|
$now = Carbon::now();
|
|
$currentDate = $now->format('Y-m-d');
|
|
$currentLotacao = self::getCurrentLotacao();
|
|
|
|
$agendamentos = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
'cadastro_cidadao.nome AS paciente_nome',
|
|
'cadastro_paciente.id AS paciente_id',
|
|
'cadastro_cidadao.cns AS paciente_cns',
|
|
'cadastro_cidadao.cpf AS paciente_cpf',
|
|
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
|
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
|
'cadastro_paciente.prontuario AS paciente_prontuario',
|
|
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d/%m/%Y") AS paciente_nascimento'),
|
|
'exame_laboratorial_agendamento.id as agendamento_id',
|
|
])
|
|
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
|
->join('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
|
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
|
->groupBy([
|
|
'cadastro_paciente.id',
|
|
'exame_laboratorial_agendamento.id'
|
|
]);
|
|
|
|
if (request()->prontuario) {
|
|
|
|
$agendamentos->where('cadastro_paciente.prontuario', request()->prontuario);
|
|
}
|
|
|
|
if (request()->paciente_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.paciente_id', request()->paciente_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()->agendamento_numero) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.agendamento_numero', request()->agendamento_numero);
|
|
}
|
|
|
|
if (request()->exame_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento_exame.exame_id', request()->exame_id);
|
|
}
|
|
|
|
if (request()->unidade_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
|
}
|
|
|
|
if (request()->infraestrutura_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_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);
|
|
}
|
|
|
|
if (request()->status_id) {
|
|
|
|
$agendamentos->where(function($query) {
|
|
|
|
foreach (request()->status_id as $status) {
|
|
|
|
if ($status == StatusAgendamento::AGENDADO) {
|
|
|
|
$query
|
|
->orWhere('status_id', StatusAgendamento::AGENDADO)
|
|
->orWhereNull('status_id');
|
|
} else {
|
|
|
|
$query->orWhere('status_id', $status);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
$agendamentos = $agendamentos->orderBy('exame_laboratorial_agendamento.id')->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' => [
|
|
'agendamentos' => $agendamentos->map(fn ($agendamento) => [
|
|
'paciente_nome' => $agendamento->paciente_nome ?? null,
|
|
'paciente_nome_social' => $agendamento->paciente_nome_social,
|
|
'paciente_preferencia_nome' => $agendamento->paciente_preferencia_nome_id === PreferenciaNome::NOME_SOCIAL,
|
|
'paciente_prontuario' => $agendamento->paciente_prontuario,
|
|
'paciente_nascimento' => $agendamento->paciente_nascimento,
|
|
'paciente_cns' => $agendamento->paciente_cns ?? null,
|
|
'paciente_cpf' => $agendamento->paciente_cpf ?? null,
|
|
'data' => $agendamento->data ?? null,
|
|
'exames' => self::getExames($periodoStart, $periodoEnd, $agendamento),
|
|
'total' => self::getTotal($periodoStart, $periodoEnd, $agendamento)
|
|
]),
|
|
'total' => self::getTotalGeral($periodoStart, $periodoEnd)
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function loadFields()
|
|
{
|
|
$now = Carbon::now();
|
|
$currentDate = $now->format('Y-m-d');
|
|
|
|
$exames = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
'exame_laboratorial_agendamento.id AS agendamento_id',
|
|
'parametrizacao_exame.id AS exame_id',
|
|
'parametrizacao_exame.nome AS exame_nome',
|
|
])
|
|
->join(
|
|
'exame_laboratorial_agendamento',
|
|
'exame_laboratorial_agendamento.id',
|
|
'exame_laboratorial_agendamento_exame.agendamento_id'
|
|
)
|
|
->join(
|
|
'cadastro_paciente',
|
|
'cadastro_paciente.id',
|
|
'exame_laboratorial_agendamento.paciente_id'
|
|
)
|
|
->join(
|
|
'parametrizacao_exame',
|
|
'parametrizacao_exame.id',
|
|
'exame_laboratorial_agendamento_exame.exame_id'
|
|
)
|
|
->groupBy([
|
|
'exame_laboratorial_agendamento_exame.exame_id',
|
|
]);
|
|
|
|
if (request()->prontuario) {
|
|
|
|
$exames->where('cadastro_paciente.prontuario', request()->prontuario);
|
|
}
|
|
|
|
if (request()->paciente_id) {
|
|
|
|
$exames->where('exame_laboratorial_agendamento.paciente_id', request()->paciente_id);
|
|
}
|
|
|
|
if (request()->agendamento_numero) {
|
|
|
|
$exames->where('exame_laboratorial_agendamento.agendamento_numero', request()->agendamento_numero);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
if (request()->status_id) {
|
|
|
|
$exames->where(function($query) {
|
|
|
|
foreach (request()->status_id as $status) {
|
|
|
|
if ($status == StatusAgendamento::AGENDADO) {
|
|
|
|
$query
|
|
->orWhere('status_id', StatusAgendamento::AGENDADO)
|
|
->orWhereNull('status_id');
|
|
} else {
|
|
|
|
$query->orWhere('status_id', $status);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
$exames = $exames->get();
|
|
|
|
return [
|
|
'form' => [
|
|
'periodo' => [
|
|
'start' => $currentDate,
|
|
'end' => $currentDate,
|
|
],
|
|
'data_atual' => Carbon::now()->format('d/m/Y'),
|
|
],
|
|
'model' => [
|
|
'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 ($coleta) => [
|
|
'coleta_unidade_id' => $coleta->unidade_coleta_id,
|
|
'coleta_unidade_nome' => $coleta->unidade_coleta_nome,
|
|
'coleta_infraestrutura_id' => $coleta->coleta_infraestrutura_id,
|
|
'coleta_infraestrutura_nome' => $coleta->coleta_infraestrutura_nome,
|
|
]),
|
|
'laboratorios' => self::getLaboratorios()->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) => [
|
|
'id' => $exame->exame_id,
|
|
'nome' => mb_strtoupper($exame->exame_nome),
|
|
])->values(),
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function getUnidades() {
|
|
|
|
$now = Carbon::now();
|
|
$currentDate = $now->format('Y-m-d');
|
|
|
|
$unidades = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
'exame_laboratorial_agendamento.id AS agendamento_id',
|
|
'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_paciente',
|
|
'cadastro_paciente.id',
|
|
'exame_laboratorial_agendamento.paciente_id'
|
|
)
|
|
->join(
|
|
'cadastro_infraestrutura',
|
|
'cadastro_infraestrutura.id',
|
|
'exame_laboratorial_agendamento.infraestrutura_id'
|
|
)
|
|
->join(
|
|
'cadastro_unidade',
|
|
'cadastro_unidade.id',
|
|
'cadastro_infraestrutura.unidade_id'
|
|
)
|
|
->groupBy([
|
|
'cadastro_infraestrutura.unidade_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()->prontuario) {
|
|
|
|
$unidades->where('cadastro_paciente.prontuario', request()->prontuario);
|
|
}
|
|
|
|
if (request()->agendamento_numero) {
|
|
|
|
$unidades->where('exame_laboratorial_agendamento.agendamento_numero', request()->agendamento_numero);
|
|
}
|
|
|
|
if (request()->exame_id) {
|
|
|
|
$unidades->where('exame_laboratorial_agendamento_exame.exame_id', request()->exame_id);
|
|
}
|
|
|
|
if (request()->paciente_id) {
|
|
|
|
$unidades->where('exame_laboratorial_agendamento.paciente_id', request()->paciente_id);
|
|
}
|
|
|
|
if (request()->status_id) {
|
|
|
|
$unidades->where(function($query) {
|
|
|
|
foreach (request()->status_id as $status) {
|
|
|
|
if ($status == StatusAgendamento::AGENDADO) {
|
|
|
|
$query
|
|
->orWhere('status_id', StatusAgendamento::AGENDADO)
|
|
->orWhereNull('status_id');
|
|
} else {
|
|
|
|
$query->orWhere('status_id', $status);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
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_paciente',
|
|
'cadastro_paciente.id',
|
|
'exame_laboratorial_agendamento.paciente_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([
|
|
'cadastro_infraestrutura_coleta.unidade_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()->prontuario) {
|
|
|
|
$coletas->where('cadastro_paciente.prontuario', request()->prontuario);
|
|
}
|
|
|
|
if (request()->agendamento_numero) {
|
|
|
|
$coletas->where('exame_laboratorial_agendamento.agendamento_numero', request()->agendamento_numero);
|
|
}
|
|
|
|
if (request()->exame_id) {
|
|
|
|
$coletas->where('exame_laboratorial_agendamento_exame.exame_id', request()->exame_id);
|
|
}
|
|
|
|
if (request()->paciente_id) {
|
|
|
|
$coletas->where('exame_laboratorial_agendamento.paciente_id', request()->paciente_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::AGENDADO) {
|
|
|
|
$query
|
|
->orWhere('status_id', StatusAgendamento::AGENDADO)
|
|
->orWhereNull('status_id');
|
|
} else {
|
|
|
|
$query->orWhere('status_id', $status);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
return $coletas->get();
|
|
}
|
|
|
|
public static function getLaboratorios() {
|
|
|
|
$now = Carbon::now();
|
|
$currentDate = $now->format('Y-m-d');
|
|
|
|
$laboratorio = DB::table('exame_laboratorial_agendamento_exame')
|
|
->select([
|
|
'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_paciente',
|
|
'cadastro_paciente.id',
|
|
'exame_laboratorial_agendamento.paciente_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([
|
|
'cadastro_infraestrutura_laboratorio.unidade_id',
|
|
]);
|
|
|
|
|
|
if (request()->periodo) {
|
|
|
|
$laboratorio->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
|
|
$laboratorio->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
|
|
} else {
|
|
|
|
$laboratorio->where('exame_laboratorial_agendamento.data', $currentDate);
|
|
}
|
|
|
|
if (request()->prontuario) {
|
|
|
|
$laboratorio->where('cadastro_paciente.prontuario', request()->prontuario);
|
|
}
|
|
|
|
if (request()->agendamento_numero) {
|
|
|
|
$laboratorio->where('exame_laboratorial_agendamento.agendamento_numero', request()->agendamento_numero);
|
|
}
|
|
|
|
if (request()->exame_id) {
|
|
|
|
$laboratorio->where('exame_laboratorial_agendamento_exame.exame_id', request()->exame_id);
|
|
}
|
|
|
|
if (request()->paciente_id) {
|
|
|
|
$laboratorio->where('exame_laboratorial_agendamento.paciente_id', request()->paciente_id);
|
|
}
|
|
|
|
if (request()->unidade_id) {
|
|
|
|
$laboratorio->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
|
}
|
|
|
|
if (request()->infraestrutura_id) {
|
|
|
|
$laboratorio->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
|
|
}
|
|
|
|
if (request()->coleta_unidade_id) {
|
|
|
|
$laboratorio->where('exame_laboratorial_agendamento.coleta_unidade_id', request()->coleta_unidade_id);
|
|
}
|
|
|
|
if (request()->coleta_infraestrutura_id) {
|
|
|
|
$laboratorio->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
|
|
}
|
|
|
|
if (request()->status_id) {
|
|
|
|
$laboratorio->where(function($query) {
|
|
|
|
foreach (request()->status_id as $status) {
|
|
|
|
if ($status == StatusAgendamento::AGENDADO) {
|
|
|
|
$query
|
|
->orWhere('status_id', StatusAgendamento::AGENDADO)
|
|
->orWhereNull('status_id');
|
|
} else {
|
|
|
|
$query->orWhere('status_id', $status);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
return $laboratorio->get();
|
|
}
|
|
|
|
public static function getStatus($query) {
|
|
|
|
if ($query->status_id == StatusAgendamento::REALIZADO) {
|
|
|
|
return 'COLETA REALIZADA';
|
|
}
|
|
|
|
return $query->status->descricao;
|
|
}
|
|
|
|
public static function getExames($periodoStart, $periodoEnd, $agendamento) {
|
|
|
|
return AgendamentoExame::whereHas('agendamento', function($query) use ($periodoStart, $periodoEnd, $agendamento) {
|
|
|
|
$hasAgendamento = $agendamento->agendamento_id ?? null;
|
|
|
|
$query
|
|
->where('data', '>=', $periodoStart)
|
|
->where('data', '<=', $periodoEnd)
|
|
->where('paciente_id', $agendamento->paciente_id)
|
|
->when($hasAgendamento, function($query) use ($agendamento) {
|
|
|
|
$query->where('agendamento_id', $agendamento->agendamento_id);
|
|
});
|
|
|
|
if (request()->agendamento_numero) {
|
|
|
|
$query->where('agendamento_numero', request()->agendamento_numero);
|
|
}
|
|
})
|
|
->where(function($query) {
|
|
|
|
if (request()->exame_id) {
|
|
|
|
$query->where('exame_id', request()->exame_id);
|
|
}
|
|
|
|
if (request()->status_id) {
|
|
|
|
$query->where(function($query) {
|
|
|
|
foreach (request()->status_id as $status) {
|
|
|
|
if ($status == StatusAgendamento::AGENDADO) {
|
|
|
|
$query
|
|
->orWhere('status_id', StatusAgendamento::AGENDADO)
|
|
->orWhereNull('status_id');
|
|
} else {
|
|
$query->orWhere('status_id', $status);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
})
|
|
->select(
|
|
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
|
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
|
DB::raw('SUM(exame_laboratorial_agendamento_exame.valor) AS valor_total'),
|
|
'exame_laboratorial_agendamento_exame.valor as valor_unitario',
|
|
'procedimento.codigo as procedimento_codigo',
|
|
'procedimento.nome AS procedimento_nome',
|
|
'parametrizacao_exame.nome AS exame_nome',
|
|
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
|
'cadastro_unidade.nome_fantasia AS nome_fantasia',
|
|
'cadastro_unidade_coleta.nome_fantasia AS coleta_nome_fantasia',
|
|
'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia',
|
|
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.principal THEN
|
|
cadastro_unidade.nome_fantasia
|
|
ELSE
|
|
cadastro_infraestrutura.nome
|
|
END AS 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'),
|
|
)
|
|
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
|
->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')
|
|
->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('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_laboratorial_agendamento_exame.exame_id',
|
|
'exame_laboratorial_agendamento.id',
|
|
])
|
|
->orderBy('exame_laboratorial_agendamento.id')
|
|
->get()
|
|
->map(function ($query) {
|
|
return [
|
|
'exame_nome' => mb_strtoupper($query->exame_nome),
|
|
'procedimento' => $query->procedimento_codigo
|
|
? $query->procedimento_codigo . ' - ' . $query->procedimento_nome
|
|
: 'SEM PROCEDIMENTO',
|
|
'agendamento_numero' => $query->agendamento_numero,
|
|
'data_agendamento' => $query->data,
|
|
'status' => $query->status_id ? self::getStatus($query) : 'AGENDADO',
|
|
'valor_unitario' => number_format($query->valor_unitario, 2, ',', '.'),
|
|
'valor_total' => number_format($query->valor_total, 2, ',', '.'),
|
|
'unidade' => $query->nome_fantasia,
|
|
'infraestrutura_nome' => $query->infraestrutura_nome,
|
|
'unidade_coleta' => $query->coleta_nome_fantasia,
|
|
'coleta_infraestrutura_nome' => $query->coleta_infraestrutura_nome,
|
|
'laboratorio' => $query->laboratorio_nome_fantasia,
|
|
'laboratorio_infraestrutura_nome' => $query->laboratorio_infraestrutura_nome,
|
|
];
|
|
});
|
|
}
|
|
|
|
public static function getTotal($periodoStart, $periodoEnd, $agendamento) {
|
|
|
|
$total = AgendamentoExame::whereHas('agendamento', function($query) use ($periodoStart, $periodoEnd, $agendamento) {
|
|
|
|
$hasAgendamento = $agendamento->agendamento_id ?? null;
|
|
|
|
$query
|
|
->where('data', '>=', $periodoStart)
|
|
->where('data', '<=', $periodoEnd)
|
|
->where('paciente_id', $agendamento->paciente_id)
|
|
->when($hasAgendamento, function($query) use ($agendamento) {
|
|
|
|
$query->where('agendamento_id', $agendamento->agendamento_id);
|
|
});
|
|
|
|
if (request()->agendamento_numero) {
|
|
|
|
$query->where('agendamento_numero', request()->agendamento_numero);
|
|
}
|
|
|
|
})
|
|
->where(function($query) {
|
|
|
|
if (request()->exame_id) {
|
|
|
|
$query->where('exame_id', request()->exame_id);
|
|
}
|
|
|
|
if (request()->status_id) {
|
|
|
|
$query->where(function($query) {
|
|
|
|
foreach (request()->status_id as $status) {
|
|
|
|
if ($status == StatusAgendamento::AGENDADO) {
|
|
|
|
$query
|
|
->orWhere('status_id', StatusAgendamento::AGENDADO)
|
|
->orWhereNull('status_id');
|
|
} else {
|
|
$query->orWhere('status_id', $status);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
})
|
|
->select(
|
|
DB::raw('SUM(exame_laboratorial_agendamento_exame.valor) AS valor_total'),
|
|
DB::raw('AVG(exame_laboratorial_agendamento_exame.valor) AS valor_medio'),
|
|
DB::raw('count(exame_laboratorial_agendamento_exame.id) AS quantidade'),
|
|
'exame_laboratorial_agendamento_exame.valor as valor',
|
|
'exame_laboratorial_agendamento.paciente_id as paciente_id',
|
|
)
|
|
->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')
|
|
->groupBy([
|
|
'exame_laboratorial_agendamento.paciente_id',
|
|
])
|
|
->first();
|
|
|
|
return [
|
|
'valor_total' => number_format($total->valor_total, 2, ',', '.'),
|
|
'valor_medio' => number_format($total->valor_medio, 2, ',', '.'),
|
|
'quantidade' => $total->quantidade,
|
|
];
|
|
}
|
|
|
|
public static function getTotalGeral($periodoStart, $periodoEnd) {
|
|
|
|
$total = AgendamentoExame::whereHas('agendamento', function($query) use ($periodoStart, $periodoEnd) {
|
|
|
|
$query
|
|
->where('data', '>=', $periodoStart)
|
|
->where('data', '<=', $periodoEnd);
|
|
|
|
if (request()->agendamento_numero) {
|
|
|
|
$query->where('agendamento_numero', request()->agendamento_numero);
|
|
}
|
|
|
|
if (request()->unidade_id) {
|
|
|
|
$query->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
|
}
|
|
|
|
if (request()->infraestrutura_id) {
|
|
|
|
$query->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
|
|
}
|
|
|
|
if (request()->coleta_unidade_id) {
|
|
|
|
$query->where('exame_laboratorial_agendamento.coleta_unidade_id', request()->coleta_unidade_id);
|
|
}
|
|
|
|
if (request()->coleta_infraestrutura_id) {
|
|
|
|
$query->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
|
|
}
|
|
|
|
if (request()->laboratorio_id) {
|
|
|
|
$query->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id);
|
|
}
|
|
|
|
if (request()->laboratorio_infraestrutura_id) {
|
|
|
|
$query->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
|
|
}
|
|
})
|
|
->where(function($query) {
|
|
|
|
if (request()->paciente_id) {
|
|
|
|
$query->where('exame_laboratorial_agendamento.paciente_id', request()->paciente_id);
|
|
}
|
|
|
|
if (request()->prontuario) {
|
|
|
|
$query->where('cadastro_paciente.prontuario', request()->prontuario);
|
|
}
|
|
|
|
if (request()->exame_id) {
|
|
|
|
$query->where('exame_id', request()->exame_id);
|
|
}
|
|
|
|
if (request()->status_id) {
|
|
|
|
$query->where(function($query) {
|
|
|
|
foreach (request()->status_id as $status) {
|
|
|
|
if ($status == StatusAgendamento::AGENDADO) {
|
|
|
|
$query
|
|
->orWhere('status_id', StatusAgendamento::AGENDADO)
|
|
->orWhereNull('status_id');
|
|
} else {
|
|
$query->orWhere('status_id', $status);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
})
|
|
->select(
|
|
DB::raw('SUM(exame_laboratorial_agendamento_exame.valor) AS total'),
|
|
DB::raw('AVG(exame_laboratorial_agendamento_exame.valor) AS medio'),
|
|
DB::raw('count(exame_laboratorial_agendamento_exame.id) AS quantidade'),
|
|
'exame_laboratorial_agendamento_exame.valor as valor',
|
|
'exame_laboratorial_agendamento.paciente_id as paciente_id',
|
|
)
|
|
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
|
->join('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
|
->join('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
|
->first();
|
|
|
|
return [
|
|
'valor_total' => number_format($total->total, 2, ',', '.'),
|
|
'valor_medio' => number_format($total->medio, 2, ',', '.'),
|
|
'quantidade' => $total->quantidade,
|
|
];
|
|
}
|
|
|
|
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_cidadao.nome AS paciente_nome',
|
|
'cadastro_paciente.id AS paciente_id',
|
|
'cadastro_cidadao.cns AS paciente_cns',
|
|
'cadastro_cidadao.cpf AS paciente_cpf',
|
|
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
|
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
|
'cadastro_paciente.prontuario AS paciente_prontuario',
|
|
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento,"%d/%m/%Y") AS paciente_nascimento'),
|
|
'exame_laboratorial_agendamento.id as agendamento_id',
|
|
])
|
|
->join('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
|
->join('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
|
->join('cadastro_cidadao as cadastro_cidadao_mae', 'cadastro_cidadao_mae.id', 'cadastro_paciente.cidadao_id')
|
|
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
|
->groupBy([
|
|
'cadastro_paciente.id',
|
|
'exame_laboratorial_agendamento.id'
|
|
]);
|
|
|
|
if (request()->prontuario) {
|
|
|
|
$agendamentos->where('cadastro_paciente.prontuario', request()->prontuario);
|
|
}
|
|
|
|
if (request()->paciente_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.paciente_id', request()->paciente_id);
|
|
}
|
|
|
|
if (request()->agendamento_numero) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.agendamento_numero', request()->agendamento_numero);
|
|
}
|
|
|
|
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()->unidade_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
|
|
}
|
|
|
|
if (request()->infraestrutura_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_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);
|
|
}
|
|
|
|
if (request()->status_id) {
|
|
|
|
$agendamentos->where(function($query) {
|
|
|
|
foreach (request()->status_id as $status) {
|
|
|
|
if ($status == StatusAgendamento::AGENDADO) {
|
|
|
|
$query
|
|
->orWhere('status_id', StatusAgendamento::AGENDADO)
|
|
->orWhereNull('status_id');
|
|
} else {
|
|
|
|
$query->orWhere('status_id', $status);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
$agendamentos = $agendamentos->orderBy('exame_laboratorial_agendamento.id')->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 [
|
|
'data_atual' => Carbon::now()->format('d/m/Y'),
|
|
'paciente_nome' => request()->paciente_nome ?? '-',
|
|
'agendamento_numero' => request()->agendamento_numero ?? '-',
|
|
'prontuario' => request()->prontuario ?? '-',
|
|
'unidade_nome' => request()->unidade_nome ?? 'TODAS',
|
|
'infraestrutura_nome' => request()->infraestrutura_nome ?? 'TODOS',
|
|
'coleta_unidade_nome' => request()->coleta_unidade_nome ?? 'TODAS',
|
|
'coleta_infraestrutura_nome' => request()->coleta_infraestrutura_nome ?? 'TODOS',
|
|
'laboratorio_nome' => request()->laboratorio_nome ?? 'TODOS',
|
|
'laboratorio_infraestrutura_nome' => request()->laboratorio_infraestrutura_nome ?? 'TODOS',
|
|
'exame_grupo' => request()->exame_id && request()->exame_nome ? request()->exame_nome : 'TODOS',
|
|
'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia,
|
|
'unidade_id' => $currentLotacao->infraestrutura->unidade->id,
|
|
'infraestrutura_id' => $currentLotacao->infraestrutura->id,
|
|
'infraestrutura' => $currentLotacao->infraestrutura->nome,
|
|
'sistema' => [
|
|
'ramo_atividade' => [
|
|
'nome' => $cliente->sistema->ramoAtividade->nome ?? null,
|
|
],
|
|
'nome' => $cliente->sistema->nome ?? null,
|
|
'versao' => $cliente->sistema->versao ?? null,
|
|
],
|
|
'periodo' => [
|
|
'start' => $periodoStartFilter,
|
|
'end' => $periodoEndFilter,
|
|
],
|
|
'status' => collect(request()->status_selecionado),
|
|
'colunas' => collect(request()->selectedCols),
|
|
'agendamentos' => $agendamentos->map(fn ($agendamento) => [
|
|
'paciente_nome' => $agendamento->paciente_nome ?? null,
|
|
'paciente_nome_social' => $agendamento->paciente_nome_social,
|
|
'paciente_preferencia_nome' => $agendamento->paciente_preferencia_nome_id === PreferenciaNome::NOME_SOCIAL,
|
|
'paciente_prontuario' => $agendamento->paciente_prontuario,
|
|
'paciente_cns' => $agendamento->paciente_cns ?? null,
|
|
'paciente_cpf' => $agendamento->paciente_cpf ?? null,
|
|
'paciente_nascimento' => $agendamento->paciente_nascimento,
|
|
'data' => $agendamento->data ?? null,
|
|
'exames' => self::getExames($periodoStart, $periodoEnd, $agendamento),
|
|
'total' => self::getTotal($periodoStart, $periodoEnd, $agendamento)
|
|
]),
|
|
'total' => self::getTotalGeral($periodoStart, $periodoEnd)
|
|
];
|
|
}
|
|
}
|