format('Y-m-d'); $currentLotacao = self::getCurrentLotacao(); $cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail(); if (!$request->exame_id || $request->exame_id && $request->pertence_grupo) { $agendamentosComGrupo = 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.coleta_infraestrutura_id AS coleta_infraestrutura_id', 'exame_laboratorial_agendamento.coleta_unidade_id AS coleta_unidade_id', 'cadastro_unidade_coleta.nome_fantasia AS coleta_unidade_nome', DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'), 'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id AS laboratorio_infraestrutura_id', 'exame_laboratorial_agendamento.laboratorio_unidade_id AS laboratorio_id', 'exame_laboratorial_agendamento.medico_id AS medico_id', 'cadastro_cidadao_medico.nome AS medico_nome', 'exame_laboratorial_agendamento.unidade_id AS unidade_id', 'cadastro_grupo.id AS exame_grupo_id', 'cadastro_grupo.nome AS exame_grupo_nome', DB::raw('group_concat(parametrizacao_exame.nome separator ", ") as exames'), DB::raw('group_concat(parametrizacao_exame.sigla_exame separator ", ") as sigla_exame'), DB::raw('true AS pertence_grupo'), DB::raw('false AS selecionado'), 'exame_laboratorial_agendamento_exame.status_id AS status_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_paciente.prontuario AS paciente_prontuario', 'cadastro_carteira_identidade.numero AS rg', DB::raw('CONCAT( cadastro_logradouro_tipo.descricao, " ", cadastro_logradouro.nome, ", ", cadastro_endereco.numero, " - ", cadastro_bairro.nome ) AS endereco'), DB::raw('CONCAT( TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos" ) AS paciente_idade'), DB::raw('CONCAT( cadastro_unidade.nome_fantasia, " - ", CASE WHEN cadastro_infraestrutura.principal THEN cadastro_unidade.nome_fantasia ELSE cadastro_infraestrutura.nome END ) AS infraestrutura_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'), DB::raw('CONCAT( cadastro_unidade_laboratorio.nome_fantasia, " - ", CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN cadastro_unidade_laboratorio.nome_fantasia ELSE cadastro_infraestrutura_laboratorio.nome END ) AS laboratorio_infraestrutura_nome'), DB::raw(" ( SELECT cadastro_cidadao_telefone.numero FROM cadastro_cidadao_telefone WHERE cadastro_cidadao_telefone.principal = TRUE AND cadastro_cidadao_telefone.cidadao_id = cadastro_cidadao.id AND cadastro_cidadao_telefone.deleted_at IS NULL LIMIT 1 ) AS paciente_telefone "), ]) ->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_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id' ) ->join( 'cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id' ) ->join( 'cadastro_usuario', 'cadastro_usuario.id', 'exame_laboratorial_agendamento.medico_id' ) ->join( 'cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'cadastro_usuario.cidadao_id' ) ->join( 'cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_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( 'cadastro_domicilio', 'cadastro_domicilio.id', 'cadastro_cidadao.domicilio_id' ) ->leftJoin( 'cadastro_endereco', 'cadastro_endereco.id', 'cadastro_domicilio.endereco_id' ) ->leftJoin( 'cadastro_logradouro', 'cadastro_logradouro.id', 'cadastro_endereco.logradouro_id' ) ->leftJoin( 'cadastro_logradouro_tipo', 'cadastro_logradouro_tipo.id', 'cadastro_logradouro.tipo_id' ) ->leftJoin( 'cadastro_bairro', 'cadastro_bairro.id', 'cadastro_logradouro.bairro_id' ) ->leftJoin( 'cadastro_carteira_identidade', function ($leftJoin) { $leftJoin->on('cadastro_carteira_identidade.cidadao_id', 'cadastro_cidadao.id') ->whereNull('cadastro_carteira_identidade.deleted_at'); } ) ->groupBy([ 'exame_laboratorial_agendamento.id', 'cadastro_grupo.id', ]); $agendamentosComGrupo->where('parametrizacao_exame.mapa_agrupado', true); if ($request->periodo) { $agendamentosComGrupo ->where( 'exame_laboratorial_agendamento.data', '>=', $request->periodo['start'] ); $agendamentosComGrupo ->where( 'exame_laboratorial_agendamento.data', '<=', $request->periodo['end'] ); } else { $agendamentosComGrupo ->where( 'exame_laboratorial_agendamento.data', $currentDate ); } if ($request->paciente_id) { $agendamentosComGrupo ->where( 'exame_laboratorial_agendamento.paciente_id', $request->paciente_id ); } if ($request->agendamento_numero) { $agendamentosComGrupo ->where( 'exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero ); } if ($request->prontuario) { $agendamentosComGrupo ->where( 'cadastro_paciente.prontuario', $request->prontuario ); } if ($request->unidade_id) { $agendamentosComGrupo ->where('exame_laboratorial_agendamento.unidade_id', $request->unidade_id ); } if ($request->infraestrutura_id) { $agendamentosComGrupo ->where( 'exame_laboratorial_agendamento.infraestrutura_id', $request->infraestrutura_id ); } if ($request->coleta_unidade_id) { $agendamentosComGrupo ->where( 'exame_laboratorial_agendamento.coleta_unidade_id', $request->coleta_unidade_id ); } if ($request->coleta_infraestrutura_id) { $agendamentosComGrupo ->where( 'exame_laboratorial_agendamento.coleta_infraestrutura_id', $request->coleta_infraestrutura_id ); } if ($request->exame_id && $request->pertence_grupo) { $agendamentosComGrupo ->where( 'cadastro_grupo.id', $request->exame_id ); } $agendamentosComGrupo ->where(function($status) { $status ->where( 'exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::AGENDADO ) ->orWhereNull( 'exame_laboratorial_agendamento_exame.status_id' ); }); if ($request->grupos) { $agendamentosComGrupo->where(function($query) use ($request) { foreach ($request->grupos as $grupo) { $query ->orWhere(function ($query) use ($grupo) { $query ->where('exame_laboratorial_agendamento.id', $grupo['agendamento_id']) ->where('cadastro_grupo.id', $grupo['id']); }); } }); } $agendamentosComGrupo = $agendamentosComGrupo->get()->toArray(); } else { $agendamentosComGrupo = []; } if (!$request->exame_id || $request->exame_id && $request->pertence_grupo) { $agendamentosSemMapaAgrupado = 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.coleta_infraestrutura_id AS coleta_infraestrutura_id', 'exame_laboratorial_agendamento.coleta_unidade_id AS coleta_unidade_id', 'cadastro_unidade_coleta.nome_fantasia AS coleta_unidade_nome', DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'), 'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id AS laboratorio_infraestrutura_id', 'exame_laboratorial_agendamento.laboratorio_unidade_id AS laboratorio_id', 'exame_laboratorial_agendamento.medico_id AS medico_id', 'cadastro_cidadao_medico.nome AS medico_nome', 'exame_laboratorial_agendamento.unidade_id AS unidade_id', 'parametrizacao_exame.id AS exame_grupo_id', 'parametrizacao_exame.nome AS exame_grupo_nome', DB::raw('group_concat(parametrizacao_exame.nome separator ", ") as exames'), DB::raw('group_concat(parametrizacao_exame.sigla_exame separator ", ") as sigla_exame'), DB::raw('false AS pertence_grupo'), DB::raw('false AS selecionado'), 'exame_laboratorial_agendamento_exame.status_id AS status_id', 'exame_laboratorial_agendamento_exame.usuario_id AS usuario_id', 'exame_laboratorial_agendamento_exame.ocupacao_id AS ocupacao_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', 'cadastro_paciente.prontuario AS paciente_prontuario', 'cadastro_carteira_identidade.numero AS rg', DB::raw('CONCAT( cadastro_logradouro_tipo.descricao, " ", cadastro_logradouro.nome, ", ", cadastro_endereco.numero, " - ", cadastro_bairro.nome ) AS endereco'), DB::raw('CONCAT( TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos" ) AS paciente_idade'), DB::raw('CONCAT( cadastro_unidade.nome_fantasia, " - ", CASE WHEN cadastro_infraestrutura.principal THEN cadastro_unidade.nome_fantasia ELSE cadastro_infraestrutura.nome END ) AS infraestrutura_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'), DB::raw('CONCAT( cadastro_unidade_laboratorio.nome_fantasia, " - ", CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN cadastro_unidade_laboratorio.nome_fantasia ELSE cadastro_infraestrutura_laboratorio.nome END ) AS laboratorio_infraestrutura_nome'), DB::raw(" ( SELECT cadastro_cidadao_telefone.numero FROM cadastro_cidadao_telefone WHERE cadastro_cidadao_telefone.principal = TRUE AND cadastro_cidadao_telefone.cidadao_id = cadastro_cidadao.id AND cadastro_cidadao_telefone.deleted_at IS NULL LIMIT 1 ) AS paciente_telefone "), ]) ->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_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id' ) ->join( 'cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id' ) ->join( 'cadastro_usuario', 'cadastro_usuario.id', 'exame_laboratorial_agendamento.medico_id' ) ->join( 'cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'cadastro_usuario.cidadao_id' ) ->join( 'cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_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( 'cadastro_domicilio', 'cadastro_domicilio.id', 'cadastro_cidadao.domicilio_id' ) ->leftJoin( 'cadastro_endereco', 'cadastro_endereco.id', 'cadastro_domicilio.endereco_id' ) ->leftJoin( 'cadastro_logradouro', 'cadastro_logradouro.id', 'cadastro_endereco.logradouro_id' ) ->leftJoin( 'cadastro_logradouro_tipo', 'cadastro_logradouro_tipo.id', 'cadastro_logradouro.tipo_id' ) ->leftJoin( 'cadastro_bairro', 'cadastro_bairro.id', 'cadastro_logradouro.bairro_id' ) ->leftJoin( 'cadastro_carteira_identidade', function ($leftJoin) { $leftJoin->on('cadastro_carteira_identidade.cidadao_id', 'cadastro_cidadao.id') ->whereNull('cadastro_carteira_identidade.deleted_at'); } ) ->groupBy([ 'exame_laboratorial_agendamento.id', 'parametrizacao_exame.id', ]); $agendamentosSemMapaAgrupado->where(function ($query) { $query->where('parametrizacao_exame.mapa_agrupado', false) ->orWhereNull('parametrizacao_exame.mapa_agrupado'); }); if ($request->periodo) { $agendamentosSemMapaAgrupado ->where( 'exame_laboratorial_agendamento.data', '>=', $request->periodo['start'] ); $agendamentosSemMapaAgrupado ->where( 'exame_laboratorial_agendamento.data', '<=', $request->periodo['end'] ); } else { $agendamentosSemMapaAgrupado ->where( 'exame_laboratorial_agendamento.data', $currentDate ); } if ($request->paciente_id) { $agendamentosSemMapaAgrupado ->where( 'exame_laboratorial_agendamento.paciente_id', $request->paciente_id ); } if ($request->agendamento_numero) { $agendamentosSemMapaAgrupado ->where( 'exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero ); } if ($request->prontuario) { $agendamentosSemMapaAgrupado ->where( 'cadastro_paciente.prontuario', $request->prontuario ); } if ($request->unidade_id) { $agendamentosSemMapaAgrupado ->where( 'exame_laboratorial_agendamento.unidade_id', $request->unidade_id ); } if ($request->infraestrutura_id) { $agendamentosSemMapaAgrupado ->where( 'exame_laboratorial_agendamento.infraestrutura_id', $request->infraestrutura_id ); } if ($request->coleta_unidade_id) { $agendamentosSemMapaAgrupado ->where( 'exame_laboratorial_agendamento.coleta_unidade_id', $request->coleta_unidade_id ); } if ($request->coleta_infraestrutura_id) { $agendamentosSemMapaAgrupado ->where( 'exame_laboratorial_agendamento.coleta_infraestrutura_id', $request->coleta_infraestrutura_id ); } if ($request->exame_id && $request->pertence_grupo) { $agendamentosSemMapaAgrupado ->where( 'parametrizacao_exame.id', $request->exame_id ); } $agendamentosSemMapaAgrupado ->where(function($status) { $status ->where( 'exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::AGENDADO ) ->orWhereNull( 'exame_laboratorial_agendamento_exame.status_id' ); }); if ($request->exames) { $agendamentosSemMapaAgrupado->where(function($query) use ($request) { foreach ($request->exames as $exame) { $query ->orWhere(function ($query) use ($exame) { $query ->where('exame_laboratorial_agendamento.id', $exame['agendamento_id']) ->where('parametrizacao_exame.id', $exame['id']); }); } }); } $agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->get()->toArray(); } else { $agendamentosSemMapaAgrupado = []; } if (!$request->exame_id || $request->exame_id && !$request->pertence_grupo) { $agendamentosSemGrupo = 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.coleta_infraestrutura_id AS coleta_infraestrutura_id', 'exame_laboratorial_agendamento.coleta_unidade_id AS coleta_unidade_id', 'cadastro_unidade_coleta.nome_fantasia AS coleta_unidade_nome', DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'), 'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id AS laboratorio_infraestrutura_id', 'exame_laboratorial_agendamento.laboratorio_unidade_id AS laboratorio_id', 'exame_laboratorial_agendamento.medico_id AS medico_id', 'cadastro_cidadao_medico.nome AS medico_nome', 'exame_laboratorial_agendamento.unidade_id AS unidade_id', 'parametrizacao_exame.id AS exame_grupo_id', 'parametrizacao_exame.nome AS exame_grupo_nome', 'parametrizacao_exame.nome AS exames', 'parametrizacao_exame.sigla_exame AS sigla_exame', DB::raw('false AS pertence_grupo'), DB::raw('false AS selecionado'), 'exame_laboratorial_agendamento_exame.status_id AS status_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', 'cadastro_paciente.prontuario AS paciente_prontuario', 'cadastro_carteira_identidade.numero AS rg', DB::raw('CONCAT( cadastro_logradouro_tipo.descricao, " ", cadastro_logradouro.nome, ", ", cadastro_endereco.numero, " - ", cadastro_bairro.nome ) AS endereco'), DB::raw('CONCAT( TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos" ) AS paciente_idade'), DB::raw('CONCAT( cadastro_unidade.nome_fantasia, " - ", CASE WHEN cadastro_infraestrutura.principal THEN cadastro_unidade.nome_fantasia ELSE cadastro_infraestrutura.nome END ) AS infraestrutura_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'), DB::raw('CONCAT( cadastro_unidade_laboratorio.nome_fantasia, " - ", CASE WHEN cadastro_infraestrutura_laboratorio.principal THEN cadastro_unidade_laboratorio.nome_fantasia ELSE cadastro_infraestrutura_laboratorio.nome END ) AS laboratorio_infraestrutura_nome'), DB::raw(" ( SELECT cadastro_cidadao_telefone.numero FROM cadastro_cidadao_telefone WHERE cadastro_cidadao_telefone.principal = TRUE AND cadastro_cidadao_telefone.cidadao_id = cadastro_cidadao.id AND cadastro_cidadao_telefone.deleted_at IS NULL LIMIT 1 ) AS paciente_telefone "), ]) ->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_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id' ) ->join( 'cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id' ) ->join( 'cadastro_usuario', 'cadastro_usuario.id', 'exame_laboratorial_agendamento.medico_id' ) ->join( 'cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'cadastro_usuario.cidadao_id' ) ->join( 'cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_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( 'cadastro_domicilio', 'cadastro_domicilio.id', 'cadastro_cidadao.domicilio_id' ) ->leftJoin( 'cadastro_endereco', 'cadastro_endereco.id', 'cadastro_domicilio.endereco_id' ) ->leftJoin( 'cadastro_logradouro', 'cadastro_logradouro.id', 'cadastro_endereco.logradouro_id' ) ->leftJoin( 'cadastro_logradouro_tipo', 'cadastro_logradouro_tipo.id', 'cadastro_logradouro.tipo_id' ) ->leftJoin( 'cadastro_bairro', 'cadastro_bairro.id', 'cadastro_logradouro.bairro_id' ) ->leftJoin( 'cadastro_carteira_identidade', function ($leftJoin) { $leftJoin->on('cadastro_carteira_identidade.cidadao_id', 'cadastro_cidadao.id') ->whereNull('cadastro_carteira_identidade.deleted_at'); } ) ->whereNull('parametrizacao_exame.grupo_tipo_id'); if ($request->periodo) { $agendamentosSemGrupo ->where( 'exame_laboratorial_agendamento.data', '>=', $request->periodo['start'] ); $agendamentosSemGrupo ->where( 'exame_laboratorial_agendamento.data', '<=', $request->periodo['end'] ); } else { $agendamentosSemGrupo ->where( 'exame_laboratorial_agendamento.data', $currentDate ); } if ($request->paciente_id) { $agendamentosSemGrupo ->where( 'exame_laboratorial_agendamento.paciente_id', $request->paciente_id ); } if ($request->agendamento_numero) { $agendamentosSemGrupo ->where( 'exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero ); } if ($request->prontuario) { $agendamentosSemGrupo ->where( 'cadastro_paciente.prontuario', $request->prontuario ); } if ($request->unidade_id) { $agendamentosSemGrupo ->where( 'exame_laboratorial_agendamento.unidade_id', $request->unidade_id ); } if ($request->infraestrutura_id) { $agendamentosSemGrupo ->where( 'exame_laboratorial_agendamento.infraestrutura_id', $request->infraestrutura_id ); } if ($request->coleta_unidade_id) { $agendamentosSemGrupo ->where( 'exame_laboratorial_agendamento.coleta_unidade_id', $request->coleta_unidade_id ); } if ($request->coleta_infraestrutura_id) { $agendamentosSemGrupo ->where( 'exame_laboratorial_agendamento.coleta_infraestrutura_id', $request->coleta_infraestrutura_id ); } if ($request->exame_id && !$request->pertence_grupo) { $agendamentosSemGrupo ->where( 'parametrizacao_exame.id', $request->exame_id ); } $agendamentosSemGrupo ->where(function($status) { $status ->where( 'exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::AGENDADO ) ->orWhereNull( 'exame_laboratorial_agendamento_exame.status_id' ); }); if ($request->exames) { $agendamentosSemGrupo->where(function($query) use ($request) { foreach ($request->exames as $exame) { $query ->orWhere(function ($query) use ($exame) { $query ->where('exame_laboratorial_agendamento.id', $exame['agendamento_id']) ->where('parametrizacao_exame.id', $exame['id']); }); } }); } $agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray(); } else { $agendamentosSemGrupo = []; } if ($request->periodo) { $perido_start = Carbon::create($request->periodo['start'])->format('d/m/Y'); $perido_end = Carbon::create($request->periodo['end'])->format('d/m/Y'); } else { $perido_start = $currentDate; $perido_end = $currentDate; } $agendamentos = collect(array_merge($agendamentosComGrupo, $agendamentosSemMapaAgrupado, $agendamentosSemGrupo)) ->sortBy(self::setOrderBy($request->sort_by,$request->sort_desc)) ->values(); return [ 'form' => [ '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', 'exame_grupo' => $request->exame_id && $request->exame_nome ? $request->exame_nome : 'TODOS', 'periodo' => [ 'start' => $perido_start, 'end' => $perido_end, ], '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, ], 'agendamentos' => $agendamentos, ], ]; } private static function setOrderBy($sortBy, $sortDesc = null) { if (is_null($sortBy)) { $padrao = [['agendamento_id', 'asc'], ['exame_grupo_nome', 'asc']]; return $padrao; } $columns = [ 'data' => 'data', 'pacienteNome' => 'paciente_nome', 'pacienteIdade' => 'paciente_idade', 'pacienteProntuario' => 'paciente_prontuario', 'agendamentoNumero' => 'agendamento_numero', 'medicoNome' => 'medico_nome', 'coletaUnidadeNome' => 'coleta_unidade_nome', 'coletaInfraestruturaNome' => 'coleta_infraestrutura_nome', 'exameGrupoNome' => 'exame_grupo_nome', ]; $column = $columns[$sortBy]; $direction = DataUtil::getBooleanValueFromStringBoolean($sortDesc) ? 'DESC' : 'ASC'; $sort = [$column, $direction]; return $sort; } }