format('Y-m-d'); $currentLotacao = self::getCurrentLotacao(); return [ 'form' => [ 'periodo' => [ 'start' => $currentDate, 'end' => $currentDate, ], 'data_atual' => Carbon::now()->format('d/m/Y'), 'unidade_id' => $currentLotacao->infraestrutura->unidade->id, 'infraestrutura_id' => $currentLotacao->infraestrutura->id, ], ]; } 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([ 'exame_laboratorial_agendamento.id AS agendamento_id', 'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero', 'exame_laboratorial_agendamento.observacao AS observacao', DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'), 'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id', 'cadastro_grupo.id AS exame_grupo_id', 'cadastro_grupo.nome AS exame_grupo_nome', DB::raw('group_concat(parametrizacao_exame.sigla_exame separator ", ") AS sigla_exame'), 'exame_laboratorial_agendamento_exame.status_id AS status_id', 'exame_laboratorial_agendamento_exame.usuario_id AS usuario_id', 'cadastro_cidadao.nome AS paciente_nome', 'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id', 'cadastro_cidadao.nome_social AS paciente_nome_social', 'cadastro_cidadao.sexo_id AS paciente_sexo_id', DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS paciente_nascimento'), DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'), DB::raw('CONCAT(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos") AS paciente_idade'), DB::raw('CASE WHEN cadastro_cidadao.sexo_id = '. Sexo::MASCULINO .' THEN "M" ELSE "F" END AS paciente_sexo_descricao'), ]) ->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id') ->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id') ->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id') ->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id') ->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id') ->groupBy([ 'exame_laboratorial_agendamento.id', 'cadastro_grupo.id', ]); $agendamentos->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO); $agendamentos->where('cadastro_grupo.impressao_mapa_producao', true); $agendamentos->where('parametrizacao_exame.mapa_agrupado', true); 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->unidade_id) { $agendamentos->where('exame_laboratorial_agendamento.unidade_id', $request->unidade_id); } if ($request->boolean('impressao_pendente')) { $agendamentos->where('exame_laboratorial_agendamento_exame.impressao_mapa_producao', false); } if ($request->infraestrutura_id) { $agendamentos->where('exame_laboratorial_agendamento.infraestrutura_id', $request->infraestrutura_id); } if ($request->exame_id) { $agendamentos->where('cadastro_grupo.id', $request->exame_id); } $agendamentos = $agendamentos->get()->toArray(); $agendamentosSiglas = DB::table('exame_laboratorial_agendamento_exame') ->select([ 'exame_laboratorial_agendamento.id AS agendamento_id', DB::raw('group_concat(parametrizacao_exame.sigla_exame ORDER BY parametrizacao_exame.sigla_exame ASC separator ", ") as sigla_exame'), ]) ->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id') ->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id') ->groupBy([ 'exame_laboratorial_agendamento.id', ]); $agendamentosSiglas->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO); if ($request->periodo) { $agendamentosSiglas->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']); $agendamentosSiglas->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']); } else { $agendamentosSiglas->where('exame_laboratorial_agendamento.data', $currentDate); } if ($request->unidade_id) { $agendamentosSiglas->where('exame_laboratorial_agendamento.unidade_id', $request->unidade_id); } if ($request->infraestrutura_id) { $agendamentosSiglas->where('exame_laboratorial_agendamento.infraestrutura_id', $request->infraestrutura_id); } $agendamentosSiglas = $agendamentosSiglas->get()->toArray(); return [ 'form' => [ '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() ]), 'periodo' => [ 'start' => $currentDate, 'end' => $currentDate, ], 'data_atual' => Carbon::now()->format('d/m/Y'), 'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia, 'unidade_id' => $currentLotacao->infraestrutura->unidade->id, 'infraestrutura_id' => $currentLotacao->infraestrutura->id, 'infraestrutura' => $currentLotacao->infraestrutura->nome, 'unidades_faturamento' => self::getUnidadesFaturamento() ], 'model' => [ 'periodo' => [ 'start' => $currentDate, 'end' => $currentDate, ], 'agendamentos' => $agendamentos, 'agendamentos_siglas' => $agendamentosSiglas, ], ]; } public static function loadFields(Request $request) { $now = Carbon::now(); $currentDate = $now->format('Y-m-d'); $grupos = DB::table('parametrizacao_exame') ->select([ 'cadastro_grupo.id AS id', 'cadastro_grupo.nome AS nome', ]) ->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id') ->whereExists(function ($query) use ($request, $currentDate) { $query->select(DB::raw(1)) ->from('exame_laboratorial_agendamento_exame') ->whereColumn('exame_laboratorial_agendamento_exame.exame_id', 'parametrizacao_exame.id') ->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id') ->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO) ->where(function($query) use ($request, $currentDate) { if ($request->periodo) { $query->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']); $query->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']); } else { $query->where('exame_laboratorial_agendamento.data', $currentDate); } }); }) ->where('parametrizacao_exame.tipo_id', Tipo::LABORATORIAL) ->where('cadastro_grupo.impressao_mapa_producao', true) ->where('parametrizacao_exame.mapa_agrupado', true) ->groupBy([ 'cadastro_grupo.id', ]); $grupos = $grupos->get()->toArray(); $unidades = DB::table('cadastro_infraestrutura') ->select([ 'cadastro_unidade.id AS unidade_id', 'cadastro_infraestrutura.id AS infraestrutura_id', 'cadastro_unidade.nome_fantasia as nome_fantasia', DB::raw(" CASE WHEN cadastro_infraestrutura.nome IS NOT NULL THEN cadastro_infraestrutura.nome ELSE cadastro_unidade.nome_fantasia END AS nome_infraestrutura "), 'cadastro_infraestrutura.principal as principal' ]) ->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id') ->whereExists(function ($query) { $query->select(DB::raw(1)) ->from('exame_laboratorial_agendamento') ->whereColumn( 'exame_laboratorial_agendamento.infraestrutura_id', 'cadastro_infraestrutura.id' ) ->leftJoin('exame_laboratorial_agendamento_exame', 'exame_laboratorial_agendamento_exame.agendamento_id', 'exame_laboratorial_agendamento.id') ->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id') ->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id') ->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::REALIZADO) ->where('cadastro_grupo.impressao_mapa_producao', true) ->where('parametrizacao_exame.mapa_agrupado', true) ->where(function($query) { $query->where('exame_laboratorial_agendamento.data', '>=', request()->periodo['start']); $query->where('exame_laboratorial_agendamento.data', '<=', request()->periodo['end']); }); }) ->whereNull('cadastro_unidade.deleted_at') ->whereNull('cadastro_infraestrutura.deleted_at') ->distinct('infraestrutura_id'); $unidades = $unidades->get()->toArray(); $unidadeInfraestrutura = []; foreach ($unidades as $item) { $unidadeId = $item->unidade_id; $infraestruturaId = $item->infraestrutura_id; $nomeInfraestrutura = $item->nome_infraestrutura; if (!isset($unidadeInfraestrutura[$unidadeId])) { $unidadeInfraestrutura[$unidadeId] = [ 'id' => $unidadeId, 'nome_fantasia' => $item->nome_fantasia, 'infraestruturas' => [] ]; } $unidadeInfraestrutura[$unidadeId]['infraestruturas'][] = [ 'id' => $infraestruturaId, 'nome' => $nomeInfraestrutura, 'principal' => $item->principal ]; } usort($unidadeInfraestrutura, function ($a, $b) { return strcmp($a['nome_fantasia'], $b['nome_fantasia']); }); return [ 'unidades' => $unidadeInfraestrutura, 'grupos' => $grupos, ]; } public static function store(Request $request) { DB::transaction(function () use ($request) { self::saveSImpressao($request); }); } private static function saveSImpressao($request) { foreach ($request->agendamentos as $params) { AgendamentoExame::whereHas('exame', function($query) use ($params) { $query->where('grupo_tipo_id', $params['exame_grupo_id']); }) ->where('agendamento_id', $params['agendamento_id']) ->update(['impressao_mapa_producao' => true]); } } public static function getUnidadesFaturamento() { return UnidadeFaturamento::get() ->map(fn($query) => [ 'id' => $query->id, 'unidade_id' => $query->unidade_id, 'nomeFantasia' => $query->unidade->nome_fantasia, 'infraestrutura_id' => $query->infraestrutura_id, 'infraestrutura_nome' => $query->infraestrutura->nome, 'profissionais' => $query->unidadeFaturamentoUsuario ->map(function ($query) { return [ 'usuario' => [ 'id' => $query->usuario_id, 'usuario_ocupacao' => $query->usuario->nome . ' - ' . $query->ocupacao->codigo, 'ocupacao_id' => $query->ocupacao_id, ], ]; }), 'faturamento_relacionado' => $query->unidadeFaturamentoUnidade ->map(function ($relacionada) { return [ 'id' => $relacionada->id, 'relacionada_unidade_id' => $relacionada->unidade_id, 'unidade_id' => $relacionada->unidade_id_principal, 'relacionada_infraestrutura_id' => $relacionada->infraestrutura_id, 'infraestrutura_id' => $relacionada->infraestrutura_id_principal, 'unidade_relacionada' => $relacionada->infraestrutura->unidade->nome_fantasia, 'infraestrutura_nome' => $relacionada->infraestrutura->nome, ]; }) ]); } }