init commit
All checks were successful
build-api-image / build (push) Successful in 51s

This commit is contained in:
Antonio Lopes dos Santos
2026-08-04 16:52:02 -03:00
commit afec302c3e
11594 changed files with 1636728 additions and 0 deletions

View File

@@ -0,0 +1,768 @@
<?php
namespace App\Services\ExameLaboratorial;
use App\Enums\ExameLaboratorial\StatusAgendamento;
use App\Models\Cadastro\Estabelecimento\Unidade;
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 RelatorioFinanceiroExameService extends Service
{
public static function list()
{
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
$currentLotacao = self::getCurrentLotacao();
$exames = DB::table('exame_laboratorial_agendamento_exame')
->select([
DB::raw('SUM(exame_laboratorial_agendamento_exame.valor) AS valor_total'),
DB::raw('count(exame_laboratorial_agendamento_exame.id) AS quantidade'),
'exame_laboratorial_agendamento_exame.valor as valor_unitario',
'procedimento.codigo as procedimento_codigo',
'procedimento.nome AS procedimento_nome',
'parametrizacao_exame.nome AS exame_nome',
'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('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_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_exame.valor',
]);
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()->unidade_id) {
$exames->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$exames->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
}
if (request()->coleta_unidade_id) {
$exames->where('exame_laboratorial_agendamento.coleta_unidade_id', request()->coleta_unidade_id);
}
if (request()->coleta_infraestrutura_id) {
$exames->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
}
if (request()->laboratorio_id) {
$exames->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id);
}
if (request()->laboratorio_infraestrutura_id) {
$exames->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
}
if (request()->order) {
$exames->orderBy('exame_nome', request()->order);
$exames->orderBy('valor_unitario', request()->order);
}
$exames->where(function($query) {
$query
->whereIn(
'exame_laboratorial_agendamento_exame.status_id',
[
StatusAgendamento::REALIZADO,
StatusAgendamento::PENDENTE_ASSINATURA,
StatusAgendamento::ASSINADO,
StatusAgendamento::PENDENTE_ENTREGA,
StatusAgendamento::ENTREGUE,
StatusAgendamento::AGENDADO,
StatusAgendamento::DUPLICIDADE,
StatusAgendamento::OUTROS,
]
);
});
$exames = $exames->get();
$total = DB::table('exame_laboratorial_agendamento_exame')
->select([
DB::raw('SUM(exame_laboratorial_agendamento_exame.valor) AS valor_total'),
])
->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');
if (request()->periodo) {
$total->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
$total->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
} else {
$total->where('exame_laboratorial_agendamento.data', $currentDate);
}
if (request()->unidade_id) {
$total->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$total->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
}
if (request()->coleta_unidade_id) {
$total->where('exame_laboratorial_agendamento.coleta_unidade_id', request()->coleta_unidade_id);
}
if (request()->coleta_infraestrutura_id) {
$total->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
}
if (request()->laboratorio_id) {
$total->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id);
}
if (request()->laboratorio_infraestrutura_id) {
$total->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
}
$total->where(function($query) {
$query
->whereIn(
'exame_laboratorial_agendamento_exame.status_id',
[
StatusAgendamento::REALIZADO,
StatusAgendamento::PENDENTE_ASSINATURA,
StatusAgendamento::ASSINADO,
StatusAgendamento::PENDENTE_ENTREGA,
StatusAgendamento::ENTREGUE,
StatusAgendamento::AGENDADO,
StatusAgendamento::DUPLICIDADE,
StatusAgendamento::OUTROS,
]
);
});
$total = $total->first();
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' => [
'exames' => $exames->map(fn ($exame) => [
'exame_nome' => mb_strtoupper($exame->exame_nome),
'procedimento' => $exame->procedimento_codigo
? $exame->procedimento_codigo . ' - ' . $exame->procedimento_nome
: 'SEM PROCEDIMENTO',
'valor_unitario' => number_format($exame->valor_unitario, 2, ',', '.'),
'valor_total' => number_format($exame->valor_total, 2, ',', '.'),
'quantidade' => number_format($exame->quantidade, 0),
'unidade' => $exame->nome_fantasia,
'infraestrutura_nome' => $exame->infraestrutura_nome,
'unidade_coleta' => $exame->coleta_nome_fantasia,
'coleta_infraestrutura_nome' => $exame->coleta_infraestrutura_nome,
'laboratorio' => $exame->laboratorio_nome_fantasia,
'laboratorio_infraestrutura_nome' => $exame->laboratorio_infraestrutura_nome,
]),
'total' => number_format($total->valor_total, 2, ',', '.'),
],
];
}
public static function loadFields()
{
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
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,
]),
],
];
}
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'
)
->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);
}
$unidades->where(function($query) {
$query
->whereIn(
'exame_laboratorial_agendamento_exame.status_id',
[
StatusAgendamento::REALIZADO,
StatusAgendamento::PENDENTE_ASSINATURA,
StatusAgendamento::ASSINADO,
StatusAgendamento::PENDENTE_ENTREGA,
StatusAgendamento::ENTREGUE,
]
);
});
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'
)
->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()->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);
}
$coletas->where(function($query) {
$query
->whereIn(
'exame_laboratorial_agendamento_exame.status_id',
[
StatusAgendamento::REALIZADO,
StatusAgendamento::PENDENTE_ASSINATURA,
StatusAgendamento::ASSINADO,
StatusAgendamento::PENDENTE_ENTREGA,
StatusAgendamento::ENTREGUE,
]
);
});
return $coletas->get();
}
public static function getLaboratorios() {
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
$laboratorios = 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_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) {
$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()->unidade_id) {
$laboratorios->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$laboratorios->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_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->where(function($query) {
$query
->whereIn(
'exame_laboratorial_agendamento_exame.status_id',
[
StatusAgendamento::REALIZADO,
StatusAgendamento::PENDENTE_ASSINATURA,
StatusAgendamento::ASSINADO,
StatusAgendamento::PENDENTE_ENTREGA,
StatusAgendamento::ENTREGUE,
]
);
});
return $laboratorios->get();
}
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();
$exames = DB::table('exame_laboratorial_agendamento_exame')
->select([
DB::raw('SUM(exame_laboratorial_agendamento_exame.valor) AS valor_total'),
DB::raw('count(exame_laboratorial_agendamento_exame.id) AS quantidade'),
'exame_laboratorial_agendamento_exame.valor as valor_unitario',
'procedimento.codigo as procedimento_codigo',
'procedimento.nome AS procedimento_nome',
'parametrizacao_exame.nome AS exame_nome',
'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('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_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_exame.valor',
]);
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()->unidade_id) {
$exames->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$exames->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
}
if (request()->coleta_unidade_id) {
$exames->where('exame_laboratorial_agendamento.coleta_unidade_id', request()->coleta_unidade_id);
}
if (request()->coleta_infraestrutura_id) {
$exames->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
}
if (request()->laboratorio_id) {
$exames->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id);
}
if (request()->laboratorio_infraestrutura_id) {
$exames->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
}
if (request()->order) {
$exames->orderBy('exame_nome', request()->order);
$exames->orderBy('valor_unitario', request()->order);
}
$exames->where(function($query) {
$query
->whereIn(
'exame_laboratorial_agendamento_exame.status_id',
[
StatusAgendamento::REALIZADO,
StatusAgendamento::PENDENTE_ASSINATURA,
StatusAgendamento::ASSINADO,
StatusAgendamento::PENDENTE_ENTREGA,
StatusAgendamento::ENTREGUE,
StatusAgendamento::AGENDADO,
StatusAgendamento::DUPLICIDADE,
StatusAgendamento::OUTROS
]
);
});
$exames = $exames->get();
$total = DB::table('exame_laboratorial_agendamento_exame')
->select([
DB::raw('SUM(exame_laboratorial_agendamento_exame.valor) AS valor_total'),
])
->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');
if (request()->periodo) {
$total->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']);
$total->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']);
} else {
$total->where('exame_laboratorial_agendamento.data', $currentDate);
}
if (request()->unidade_id) {
$total->where('exame_laboratorial_agendamento.unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$total->where('exame_laboratorial_agendamento.infraestrutura_id', request()->infraestrutura_id);
}
if (request()->coleta_unidade_id) {
$total->where('exame_laboratorial_agendamento.coleta_unidade_id', request()->coleta_unidade_id);
}
if (request()->coleta_infraestrutura_id) {
$total->where('exame_laboratorial_agendamento.coleta_infraestrutura_id', request()->coleta_infraestrutura_id);
}
if (request()->laboratorio_id) {
$total->where('exame_laboratorial_agendamento.laboratorio_unidade_id', request()->laboratorio_id);
}
if (request()->laboratorio_infraestrutura_id) {
$total->where('exame_laboratorial_agendamento.laboratorio_infraestrutura_id', request()->laboratorio_infraestrutura_id);
}
$total->where(function ($query) {
$query
->whereIn(
'exame_laboratorial_agendamento_exame.status_id',
[
StatusAgendamento::REALIZADO,
StatusAgendamento::PENDENTE_ASSINATURA,
StatusAgendamento::ASSINADO,
StatusAgendamento::PENDENTE_ENTREGA,
StatusAgendamento::ENTREGUE,
StatusAgendamento::AGENDADO,
StatusAgendamento::DUPLICIDADE,
StatusAgendamento::OUTROS,
]
);
});
$total = $total->first();
if (request()->periodo) {
$periodoStartFilter = Carbon::create(request()->periodo['start'])->format('d/m/Y');
$periodoEndFilter = Carbon::create(request()->periodo['end'])->format('d/m/Y');
} else {
$periodoStartFilter = $currentDate;
$periodoEndFilter = $currentDate;
}
return [
'data_atual' => Carbon::now()->format('d/m/Y'),
'paciente_nome' => request()->paciente_nome ?? '-',
'agendamento_numero' => request()->agendamento_id ?? '-',
'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),
'exames' => $exames->map(fn ($exame) => [
'exame_nome' => mb_strtoupper($exame->exame_nome),
'procedimento' => $exame->procedimento_codigo
? $exame->procedimento_codigo . ' - ' . $exame->procedimento_nome
: 'SEM PROCEDIMENTO',
'valor_unitario' => number_format($exame->valor_unitario, 2, ',', '.'),
'valor_total' => number_format($exame->valor_total, 2, ',', '.'),
'quantidade' => number_format($exame->quantidade, 0),
'unidade' => $exame->nome_fantasia,
'infraestrutura_nome' => $exame->infraestrutura_nome,
'unidade_coleta' => $exame->coleta_nome_fantasia,
'coleta_infraestrutura_nome' => $exame->coleta_infraestrutura_nome,
'laboratorio' => $exame->laboratorio_nome_fantasia,
'laboratorio_infraestrutura_nome' => $exame->laboratorio_infraestrutura_nome,
]),
'total' => number_format($total->valor_total, 2, ',', '.'),
];
}
}