Files
rkm-conectasus-ci-sandbox/api/app/Services/ExameLaboratorial/RelatorioCotaConsumoService.php
Antonio Lopes dos Santos afec302c3e
All checks were successful
build-api-image / build (push) Successful in 51s
init commit
2026-08-04 16:52:02 -03:00

752 lines
32 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 RelatorioCotaConsumoService extends Service
{
public static function list()
{
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
$currentLotacao = self::getCurrentLotacao();
$agendamentos = DB::table('exame_laboratorial_parametrizacao_cota_exame_unidade')
->select([
'cadastro_unidade.nome_fantasia AS coleta_nome_fantasia',
'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia',
'parametrizacao_exame.codigo AS procedimento_codigo',
'exame_laboratorial_parametrizacao_cota.infraestrutura_id AS infraestrutura_id',
'parametrizacao_exame.id as exame_id',
DB::raw('SUM(exame_laboratorial_parametrizacao_cota_exame_unidade.cota_valor) AS valor_utilizado'),
DB::raw('SUM(exame_laboratorial_parametrizacao_cota_exame_unidade.cota_quantidade) AS cota_utilizada'),
DB::raw('SUM(exame_laboratorial_parametrizacao_cota_exame_unidade.cota_valor) AS valor_definido'),
DB::raw('SUM(exame_laboratorial_parametrizacao_cota_exame_unidade.cota_quantidade) AS cota_quantidade'),
DB::raw('COUNT(parametrizacao_exame.id) AS quantidade'),
DB::raw('
CASE WHEN cadastro_infraestrutura.principal THEN
cadastro_unidade.nome_fantasia
ELSE
cadastro_infraestrutura.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
'),
])
->join(
'exame_laboratorial_parametrizacao_cota_exame',
'exame_laboratorial_parametrizacao_cota_exame.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame_unidade.cota_exame_id'
)
->join(
'exame_laboratorial_parametrizacao_cota',
'exame_laboratorial_parametrizacao_cota.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame.cota_id'
)
->join(
'cadastro_infraestrutura',
'cadastro_infraestrutura.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame_unidade.infraestrutura_solicitante_id'
)
->join(
'parametrizacao_exame',
'parametrizacao_exame.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame.exame_id'
)
->join(
'cadastro_unidade',
'cadastro_unidade.id',
'=',
'cadastro_infraestrutura.unidade_id'
)
->join(
'cadastro_infraestrutura as cadastro_infraestrutura_laboratorio',
'cadastro_infraestrutura_laboratorio.id',
'=',
'exame_laboratorial_parametrizacao_cota.infraestrutura_id'
)
->join(
'cadastro_unidade as cadastro_unidade_laboratorio',
'cadastro_unidade_laboratorio.id',
'=',
'cadastro_infraestrutura_laboratorio.unidade_id'
)
->whereNull('exame_laboratorial_parametrizacao_cota_exame_unidade.deleted_at')
->whereNull('exame_laboratorial_parametrizacao_cota_exame.deleted_at')
->whereNull('exame_laboratorial_parametrizacao_cota.deleted_at')
->groupBy([
'parametrizacao_exame.id',
]);
/**
* FILTRO DE PERÍODO (CORRIGIDO: INTERSEÇÃO DE DATAS)
*/
if (request()->periodo) {
$inicio = Carbon::parse(request()->periodo['start'])->startOfMonth();
$fim = Carbon::parse(request()->periodo['end'])->endOfMonth();
$agendamentos->where(
'exame_laboratorial_parametrizacao_cota.inicio',
'<=',
$fim->toDateString()
);
$agendamentos->where(
'exame_laboratorial_parametrizacao_cota.fim',
'>=',
$inicio->toDateString()
);
} else {
$agendamentos->where(
'exame_laboratorial_parametrizacao_cota.inicio',
'<=',
$currentDate
);
$agendamentos->where(
'exame_laboratorial_parametrizacao_cota.fim',
'>=',
$currentDate
);
}
if (request()->exame_id) {
$agendamentos->where('exame_laboratorial_parametrizacao_cota_exame.exame_id', request()->exame_id);
}
if (request()->unidade_id) {
$agendamentos->where('exame_laboratorial_parametrizacao_cota_exame_unidade.unidade_solicitante_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$agendamentos->where('exame_laboratorial_parametrizacao_cota_exame_unidade.infraestrutura_solicitante_id', request()->infraestrutura_id);
}
if (request()->laboratorio_id) {
$agendamentos->where('exame_laboratorial_parametrizacao_cota.unidade_id', request()->laboratorio_id);
}
if (request()->laboratorio_infraestrutura_id) {
$agendamentos->where('exame_laboratorial_parametrizacao_cota.infraestrutura_id', request()->laboratorio_infraestrutura_id);
}
$agendamentos = $agendamentos->get();
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(function ($query) {
return [
'id' => $query->id ?? null,
'valor_definido' => $query->valor_definido ?? null,
'quantidade_definida' => $query->cota_quantidade,
'exame_nome' => $query->exame_nome,
'exame_id' => $query->exame_id,
'procedimento_codigo' => !empty($query->procedimento_codigo) ? $query->procedimento_codigo : '-',
'coleta_nome_fantasia' => $query->coleta_nome_fantasia,
'coleta_infraestrutura_nome' => $query->coleta_infraestrutura_nome,
'laboratorio_nome_fantasia' => $query->laboratorio_nome_fantasia,
'laboratorio_infraestrutura_nome' => $query->laboratorio_infraestrutura_nome,
'valor_utilizado' => self::getValorUtilizado($query->exame_id),
'quantidade_utilizada' => self::getQuantidadeUtilizada($query->exame_id)
];
})
->values()
],
];
}
public static function getValorUtilizado($exame_id) {
$currentDate = Carbon::now()->format('Y-m-d');
$periodoStart = request()->periodo['start'] ?? $currentDate;
$periodoEnd = request()->periodo['end'] ?? $currentDate;
return AgendamentoExame::where('exame_id', $exame_id)
->where(function($query) {
$query
->whereIn(
'status_id',
[
StatusAgendamento::AGENDADO,
StatusAgendamento::REALIZADO,
StatusAgendamento::PENDENTE_ASSINATURA,
StatusAgendamento::ASSINADO,
StatusAgendamento::PENDENTE_ENTREGA,
StatusAgendamento::ENTREGUE,
StatusAgendamento::NOVA_COLETA
]
)
->orWhereNull('status_id');
})
->whereHas('agendamento', function ($query) use ($periodoStart, $periodoEnd) {
$query
->where(
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
'>=',
Carbon::createFromFormat('Y-m-d', $periodoStart)
->format('Y-m-d')
)
->where(
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
'<=',
Carbon::createFromFormat('Y-m-d', $periodoEnd)
->format('Y-m-d')
);
if (request()->unidade_id) {
$query->where('unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$query->where('infraestrutura_id', request()->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);
}
})
->sum('valor');
}
public static function getQuantidadeUtilizada($exame_id) {
$currentDate = Carbon::now()->format('Y-m-d');
$periodoStart = request()->periodo['start'] ?? $currentDate;
$periodoEnd = request()->periodo['end'] ?? $currentDate;
return AgendamentoExame::where('exame_id', $exame_id)
->where(function($query) {
$query
->whereIn(
'status_id',
[
StatusAgendamento::AGENDADO,
StatusAgendamento::REALIZADO,
StatusAgendamento::PENDENTE_ASSINATURA,
StatusAgendamento::ASSINADO,
StatusAgendamento::PENDENTE_ENTREGA,
StatusAgendamento::ENTREGUE,
StatusAgendamento::NOVA_COLETA
]
)
->orWhereNull('status_id');
})
->whereHas('agendamento', function ($query) use ($periodoStart, $periodoEnd) {
$query
->where(
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
'>=',
Carbon::createFromFormat('Y-m-d', $periodoStart)
->format('Y-m-d')
)
->where(
DB::raw('DATE_FORMAT(data, "%Y-%m-%d")'),
'<=',
Carbon::createFromFormat('Y-m-d', $periodoEnd)
->format('Y-m-d')
);
if (request()->unidade_id) {
$query->where('unidade_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$query->where('infraestrutura_id', request()->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);
}
})
->count('id');
}
public static function loadFields()
{
$now = Carbon::now();
$currentDate = $now->format('Y-m-d');
$inicio = null;
$fim = null;
if (request()->periodo) {
$inicio = Carbon::parse(request()->periodo['start'])->startOfMonth();
$fim = Carbon::parse(request()->periodo['end'])->endOfMonth();
}
$exames = DB::table('exame_laboratorial_parametrizacao_cota_exame_unidade')
->select([
'parametrizacao_exame.id as id',
DB::raw('UPPER(parametrizacao_exame.nome) AS nome'),
])
->join(
'exame_laboratorial_parametrizacao_cota_exame',
'exame_laboratorial_parametrizacao_cota_exame.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame_unidade.cota_exame_id'
)
->join(
'exame_laboratorial_parametrizacao_cota',
'exame_laboratorial_parametrizacao_cota.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame.cota_id'
)
->join(
'parametrizacao_exame',
'parametrizacao_exame.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame.exame_id'
)
->whereNull('exame_laboratorial_parametrizacao_cota_exame_unidade.deleted_at')
->whereNull('exame_laboratorial_parametrizacao_cota_exame.deleted_at')
->whereNull('exame_laboratorial_parametrizacao_cota.deleted_at')
->groupBy(['parametrizacao_exame.id']);
if ($inicio && $fim) {
$exames->where('exame_laboratorial_parametrizacao_cota.inicio', '<=', $fim->toDateString())
->where('exame_laboratorial_parametrizacao_cota.fim', '>=', $inicio->toDateString());
} else {
$exames->where('exame_laboratorial_parametrizacao_cota.inicio', $currentDate)
->where('exame_laboratorial_parametrizacao_cota.fim', $currentDate);
}
if (request()->exame_id) {
$exames->where('parametrizacao_exame.id', request()->exame_id);
}
$exames = $exames->get();
$unidades = DB::table('exame_laboratorial_parametrizacao_cota_exame_unidade')
->select([
'cadastro_unidade.nome_fantasia AS nome',
'cadastro_unidade.id AS id',
'cadastro_infraestrutura.id AS infraestrutura_id',
'cadastro_infraestrutura.principal AS principal',
DB::raw('
CASE WHEN cadastro_infraestrutura.principal THEN
cadastro_unidade.nome_fantasia
ELSE
cadastro_infraestrutura.nome
END AS infraestrutura_nome
'),
])
->join(
'exame_laboratorial_parametrizacao_cota_exame',
'exame_laboratorial_parametrizacao_cota_exame.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame_unidade.cota_exame_id'
)
->join(
'exame_laboratorial_parametrizacao_cota',
'exame_laboratorial_parametrizacao_cota.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame.cota_id'
)
->join(
'parametrizacao_exame',
'parametrizacao_exame.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame.exame_id'
)
->join(
'cadastro_infraestrutura',
'cadastro_infraestrutura.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame_unidade.infraestrutura_solicitante_id'
)
->join(
'cadastro_unidade',
'cadastro_unidade.id',
'=',
'cadastro_infraestrutura.unidade_id'
)
->whereNull('exame_laboratorial_parametrizacao_cota_exame_unidade.deleted_at')
->groupBy(['cadastro_infraestrutura.id']);
if ($inicio && $fim) {
$unidades->where('exame_laboratorial_parametrizacao_cota.inicio', '<=', $fim->toDateString())
->where('exame_laboratorial_parametrizacao_cota.fim', '>=', $inicio->toDateString());
} else {
$unidades->where('exame_laboratorial_parametrizacao_cota.inicio', $currentDate)
->where('exame_laboratorial_parametrizacao_cota.fim', $currentDate);
}
if (request()->exame_id) {
$unidades->where('parametrizacao_exame.id', request()->exame_id);
}
$unidades = $unidades->get();
$laboratorios = DB::table('exame_laboratorial_parametrizacao_cota_exame_unidade')
->select([
'cadastro_unidade_laboratorio.nome_fantasia AS nome',
'exame_laboratorial_parametrizacao_cota.infraestrutura_id AS infraestrutura_id',
'exame_laboratorial_parametrizacao_cota.unidade_id AS id',
'cadastro_infraestrutura_laboratorio.principal as principal',
DB::raw('
CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN
cadastro_unidade_laboratorio.nome_fantasia
ELSE
cadastro_infraestrutura_laboratorio.nome
END AS infraestrutura_nome
'),
])
->join(
'exame_laboratorial_parametrizacao_cota_exame',
'exame_laboratorial_parametrizacao_cota_exame.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame_unidade.cota_exame_id'
)
->join(
'exame_laboratorial_parametrizacao_cota',
'exame_laboratorial_parametrizacao_cota.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame.cota_id'
)
->join(
'parametrizacao_exame',
'parametrizacao_exame.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame.exame_id'
)
->join(
'cadastro_infraestrutura',
'cadastro_infraestrutura.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame_unidade.infraestrutura_solicitante_id'
)
->join(
'cadastro_unidade',
'cadastro_unidade.id',
'=',
'cadastro_infraestrutura.unidade_id'
)
->join(
'cadastro_infraestrutura as cadastro_infraestrutura_laboratorio',
'cadastro_infraestrutura_laboratorio.id',
'=',
'exame_laboratorial_parametrizacao_cota.infraestrutura_id'
)
->join(
'cadastro_unidade as cadastro_unidade_laboratorio',
'cadastro_unidade_laboratorio.id',
'=',
'cadastro_infraestrutura_laboratorio.unidade_id'
)
->whereNull('exame_laboratorial_parametrizacao_cota_exame_unidade.deleted_at')
->groupBy(['cadastro_infraestrutura_laboratorio.id']);
if ($inicio && $fim) {
$laboratorios->where('exame_laboratorial_parametrizacao_cota.inicio', '<=', $fim->toDateString())
->where('exame_laboratorial_parametrizacao_cota.fim', '>=', $inicio->toDateString());
} else {
$laboratorios->where('exame_laboratorial_parametrizacao_cota.inicio', $currentDate)
->where('exame_laboratorial_parametrizacao_cota.fim', $currentDate);
}
if (request()->exame_id) {
$laboratorios->where('exame_laboratorial_parametrizacao_cota_exame.exame_id', request()->exame_id);
}
if (request()->unidade_id) {
$laboratorios->where('exame_laboratorial_parametrizacao_cota_exame_unidade.unidade_solicitante_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$laboratorios->where('exame_laboratorial_parametrizacao_cota_exame_unidade.infraestrutura_solicitante_id', request()->infraestrutura_id);
}
$laboratorios = $laboratorios->get();
return [
'form' => [
'periodo' => [
'start' => $currentDate,
'end' => $currentDate,
],
'data_atual' => Carbon::now()->format('d/m/Y'),
],
'model' => [
'unidades' => $unidades,
'laboratorios' => $laboratorios,
'exames' => $exames,
],
];
}
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();
$inicio = null;
$fim = null;
if (request()->periodo) {
$inicio = Carbon::parse(request()->periodo['start'])->startOfMonth();
$fim = Carbon::parse(request()->periodo['end'])->endOfMonth();
}
$agendamentos = DB::table('exame_laboratorial_parametrizacao_cota_exame_unidade')
->select([
'cadastro_unidade.nome_fantasia AS coleta_nome_fantasia',
'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_nome_fantasia',
'parametrizacao_exame.codigo AS procedimento_codigo',
'exame_laboratorial_parametrizacao_cota.infraestrutura_id AS infraestrutura_id',
'parametrizacao_exame.id as exame_id',
DB::raw('SUM(exame_laboratorial_parametrizacao_cota_exame_unidade.cota_valor) AS valor_utilizado'),
DB::raw('SUM(exame_laboratorial_parametrizacao_cota_exame_unidade.cota_quantidade) AS cota_utilizada'),
DB::raw('SUM(exame_laboratorial_parametrizacao_cota_exame_unidade.cota_valor) AS valor_definido'),
DB::raw('SUM(exame_laboratorial_parametrizacao_cota_exame_unidade.cota_quantidade) AS cota_quantidade'),
DB::raw('COUNT(parametrizacao_exame.id) AS quantidade'),
DB::raw('
CASE WHEN cadastro_infraestrutura.principal THEN
cadastro_unidade.nome_fantasia
ELSE
cadastro_infraestrutura.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
'),
])
->join(
'exame_laboratorial_parametrizacao_cota_exame',
'exame_laboratorial_parametrizacao_cota_exame.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame_unidade.cota_exame_id'
)
->join(
'exame_laboratorial_parametrizacao_cota',
'exame_laboratorial_parametrizacao_cota.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame.cota_id'
)
->join(
'cadastro_infraestrutura',
'cadastro_infraestrutura.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame_unidade.infraestrutura_solicitante_id'
)
->join(
'parametrizacao_exame',
'parametrizacao_exame.id',
'=',
'exame_laboratorial_parametrizacao_cota_exame.exame_id'
)
->join(
'cadastro_unidade',
'cadastro_unidade.id',
'=',
'cadastro_infraestrutura.unidade_id'
)
->join(
'cadastro_infraestrutura as cadastro_infraestrutura_laboratorio',
'cadastro_infraestrutura_laboratorio.id',
'=',
'exame_laboratorial_parametrizacao_cota.infraestrutura_id'
)
->join(
'cadastro_unidade as cadastro_unidade_laboratorio',
'cadastro_unidade_laboratorio.id',
'=',
'cadastro_infraestrutura_laboratorio.unidade_id'
)
->whereNull('exame_laboratorial_parametrizacao_cota_exame_unidade.deleted_at')
->whereNull('exame_laboratorial_parametrizacao_cota_exame.deleted_at')
->whereNull('exame_laboratorial_parametrizacao_cota.deleted_at')
->groupBy(['parametrizacao_exame.id']);
if ($inicio && $fim) {
$agendamentos->where('exame_laboratorial_parametrizacao_cota.inicio', '<=', $fim->toDateString())
->where('exame_laboratorial_parametrizacao_cota.fim', '>=', $inicio->toDateString());
} else {
$agendamentos->where('exame_laboratorial_parametrizacao_cota.inicio', $currentDate)
->where('exame_laboratorial_parametrizacao_cota.fim', $currentDate);
}
if (request()->exame_id) {
$agendamentos->where('exame_laboratorial_parametrizacao_cota_exame.exame_id', request()->exame_id);
}
if (request()->unidade_id) {
$agendamentos->where('exame_laboratorial_parametrizacao_cota_exame_unidade.unidade_solicitante_id', request()->unidade_id);
}
if (request()->infraestrutura_id) {
$agendamentos->where('exame_laboratorial_parametrizacao_cota_exame_unidade.infraestrutura_solicitante_id', request()->infraestrutura_id);
}
if (request()->laboratorio_id) {
$agendamentos->where('exame_laboratorial_parametrizacao_cota.unidade_id', request()->laboratorio_id);
}
if (request()->laboratorio_infraestrutura_id) {
$agendamentos->where('exame_laboratorial_parametrizacao_cota.infraestrutura_id', request()->laboratorio_infraestrutura_id);
}
$agendamentos = $agendamentos->get();
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_numero ?? '-',
'prontuario' => request()->prontuario ?? '-',
'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',
'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,
],
'colunas' => collect(request()->selectedCols),
'agendamentos' => $agendamentos
->map(function ($query) {
return [
'id' => $query->id ?? null,
'valor_definido' => $query->valor_definido > 0
? 'R$ ' . number_format($query->valor_definido, 2, ',', '.')
: '-',
'quantidade_definida' => $query->cota_quantidade,
'exame_nome' => $query->exame_nome,
'exame_id' => $query->exame_id,
'procedimento_codigo' => !empty($query->procedimento_codigo) ? $query->procedimento_codigo : '-',
'coleta_nome_fantasia' => $query->coleta_nome_fantasia,
'coleta_infraestrutura_nome' => $query->coleta_infraestrutura_nome,
'laboratorio_nome_fantasia' => $query->laboratorio_nome_fantasia,
'laboratorio_infraestrutura_nome' => $query->laboratorio_infraestrutura_nome,
'valor_utilizado' => $query->valor_utilizado > 0
? 'R$ ' . number_format(self::getValorUtilizado($query->exame_id), 2, ',', '.')
: '-',
'quantidade_utilizada' => $query->cota_quantidade
? self::getQuantidadeUtilizada($query->exame_id)
: '-'
];
})
->values()
];
}
}