select([ DB::raw("DATE_FORMAT(ambulatorio_jornada.data, '%d/%m/%Y') AS data"), 'ambulatorio_atendimento.id AS atendimento_id', 'cadastro_paciente.id AS paciente_id', 'cadastro_cidadao_paciente.nome AS paciente_nome', DB::raw("DATE_FORMAT(cadastro_cidadao_paciente.data_nascimento, '%d/%m/%Y') AS paciente_data_nascimento"), DB::raw("LPAD(cadastro_paciente.prontuario, 6, '0') AS paciente_prontuario"), 'cadastro_unidade.nome_fantasia AS unidade_nome_fantasia', 'cadastro_cidadao_usuario.nome AS usuario_nome', 'cadastro_ferida.nome AS ferida_nome', 'cadastro_especialidade.nome AS especialidade_nome', 'ocupacao.codigo AS ocupacao_codigo', DB::raw(" CASE WHEN ambulatorio_atendimento_soap_ferida.cicatrizada = TRUE THEN 'Cicatrizada' WHEN ambulatorio_atendimento_soap_ferida.cicatrizada = FALSE THEN 'Não Cicatrizada' ELSE 'Indefinido' END AS status "), DB::raw("GROUP_CONCAT(procedimento.codigo SEPARATOR ', ') AS procedimentos"), DB::raw("GROUP_CONCAT(cadastro_material.descricao SEPARATOR ', ') AS materiais"), DB::raw("SUM(COALESCE(procedimento.valor_servico_ambilatorial, 0)) AS valor_custo_procedimento"), DB::raw(" COALESCE(( SELECT SUM(almoxarifado_entrada_item.valor_unitario * almoxarifado_saida_item_entrada_item.quantidade_saida) FROM almoxarifado_saida_item_entrada_item INNER JOIN almoxarifado_entrada_item ON almoxarifado_entrada_item.id = almoxarifado_saida_item_entrada_item.entrada_item_id WHERE almoxarifado_saida_item_entrada_item.saida_item_id = ambulatorio_atendimento_soap_ferida_material.saida_item_id ), 0) AS valor_custo_material "), DB::raw(" ( SUM(COALESCE(procedimento.valor_servico_ambilatorial, 0)) + COALESCE(( SELECT SUM(almoxarifado_entrada_item.valor_unitario * almoxarifado_saida_item_entrada_item.quantidade_saida) FROM almoxarifado_saida_item_entrada_item INNER JOIN almoxarifado_entrada_item ON almoxarifado_entrada_item.id = almoxarifado_saida_item_entrada_item.entrada_item_id WHERE almoxarifado_saida_item_entrada_item.saida_item_id = ambulatorio_atendimento_soap_ferida_material.saida_item_id ), 0) ) AS valor_total "), DB::raw("COUNT(DISTINCT ambulatorio_atendimento_soap.id) AS soaps_geradas"), DB::Raw("COUNT(DISTINCT ambulatorio_atendimento_soap_ferida.id) AS feridas_registradas"), DB::Raw("COUNT(ambulatorio_atendimento_soap_ferida_procedimento.id) AS procedimentos_lancados"), DB::Raw("COUNT(ambulatorio_atendimento_soap_ferida_material.id) AS kits_materiais_consumidos"), ]) ->from('ambulatorio_atendimento_soap_ferida_procedimento') ->join('ambulatorio_atendimento_procedimento', 'ambulatorio_atendimento_procedimento.id', 'ambulatorio_atendimento_soap_ferida_procedimento.atendimento_procedimento_id') ->join('ambulatorio_atendimento_soap_ferida', 'ambulatorio_atendimento_soap_ferida.id', 'ambulatorio_atendimento_soap_ferida_procedimento.soap_ferida_id') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id') ->join('cadastro_paciente', 'cadastro_paciente.id', 'ambulatorio_agendamento.paciente_id') ->join('cadastro_cidadao AS cadastro_cidadao_paciente', 'cadastro_cidadao_paciente.id', 'cadastro_paciente.cidadao_id') ->join('cadastro_usuario', 'cadastro_usuario.id', 'cadastro_lotacao.usuario_id') ->join('cadastro_cidadao AS cadastro_cidadao_usuario', 'cadastro_cidadao_usuario.id', 'cadastro_usuario.cidadao_id') ->join('cadastro_ferida', 'cadastro_ferida.id', 'ambulatorio_atendimento_soap_ferida.ferida_id') ->leftJoin('cadastro_especialidade', 'cadastro_especialidade.id', 'ambulatorio_atendimento_procedimento.especialidade_id') ->leftJoin('sigtap.ocupacao', 'ocupacao.id', 'ambulatorio_atendimento_procedimento.ocupacao_id') ->leftJoin('sigtap.procedimento', 'procedimento.id', 'ambulatorio_atendimento_procedimento.procedimento_id') ->leftJoin('ambulatorio_atendimento_soap_ferida_material', 'ambulatorio_atendimento_soap_ferida_material.atendimento_procedimento_id', 'ambulatorio_atendimento_procedimento.id') ->leftJoin('cadastro_material', 'cadastro_material.id', 'ambulatorio_atendimento_soap_ferida_material.material_id') ->whereBetween('ambulatorio_jornada.data', $params->periodo) ->when($params->unidade_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.unidade_id', $id)) ->when($params->infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.id', $id)) ->when($params->ocupacao_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.ocupacao_id', $id)) ->when($params->especialidade_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.especialidade_id', $id)) ->when($params->usuario_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.usuario_id', $id)) ->when($params->feridas, fn ($q, $ids) => $q->whereIn('ambulatorio_atendimento_soap_ferida.ferida_id', $ids)) ->when($params->status, fn ($q, $status) => $q->where(function ($q) use ($status) { if ($status === 'cicatrizada') { $q->orWhere('ambulatorio_atendimento_soap_ferida.cicatrizada', true); } else if ($status === 'não_cicatrizada') { $q->orWhere('ambulatorio_atendimento_soap_ferida.cicatrizada', false); } else if ($status === 'indefinido') { $q->orWhereNull('ambulatorio_atendimento_soap_ferida.cicatrizada'); } })) ->when($params->procedimentos, fn ($q, $ids) => $q->whereIn('ambulatorio_atendimento_procedimento.procedimento_id', $ids)) ->when($params->paciente_id, fn ($q, $id) => $q->where('cadastro_paciente.id', $id)) ->unionAll( DB::table('ambulatorio_atendimento_soap_ferida_procedimento') ->select([ DB::raw("DATE_FORMAT(ambulatorio_demanda_espontanea.data, '%d/%m/%Y') AS data"), 'ambulatorio_atendimento.id AS atendimento_id', 'cadastro_paciente.id AS paciente_id', 'cadastro_cidadao_paciente.nome AS paciente_nome', DB::raw("DATE_FORMAT(cadastro_cidadao_paciente.data_nascimento, '%d/%m/%Y') AS paciente_data_nascimento"), DB::raw("LPAD(cadastro_paciente.prontuario, 6, '0') AS paciente_prontuario"), 'cadastro_unidade.nome_fantasia AS unidade_nome_fantasia', 'cadastro_cidadao_usuario.nome AS usuario_nome', 'cadastro_ferida.nome AS ferida_nome', 'cadastro_especialidade.nome AS especialidade_nome', 'ocupacao.codigo AS ocupacao_codigo', DB::raw(" CASE WHEN ambulatorio_atendimento_soap_ferida.cicatrizada = TRUE THEN 'Cicatrizada' WHEN ambulatorio_atendimento_soap_ferida.cicatrizada = FALSE THEN 'Não Cicatrizada' ELSE 'Indefinido' END AS status "), DB::raw("GROUP_CONCAT(procedimento.codigo SEPARATOR ', ') AS procedimentos"), DB::raw("GROUP_CONCAT(cadastro_material.descricao SEPARATOR ', ') AS materiais"), DB::raw("SUM(COALESCE(procedimento.valor_servico_ambilatorial, 0)) AS valor_custo_procedimento"), DB::raw(" COALESCE(( SELECT SUM(almoxarifado_entrada_item.valor_unitario * almoxarifado_saida_item_entrada_item.quantidade_saida) FROM almoxarifado_saida_item_entrada_item INNER JOIN almoxarifado_entrada_item ON almoxarifado_entrada_item.id = almoxarifado_saida_item_entrada_item.entrada_item_id WHERE almoxarifado_saida_item_entrada_item.saida_item_id = ambulatorio_atendimento_soap_ferida_material.saida_item_id ), 0) AS valor_custo_material "), DB::raw(" ( SUM(COALESCE(procedimento.valor_servico_ambilatorial, 0)) + COALESCE(( SELECT SUM(almoxarifado_entrada_item.valor_unitario * almoxarifado_saida_item_entrada_item.quantidade_saida) FROM almoxarifado_saida_item_entrada_item INNER JOIN almoxarifado_entrada_item ON almoxarifado_entrada_item.id = almoxarifado_saida_item_entrada_item.entrada_item_id WHERE almoxarifado_saida_item_entrada_item.saida_item_id = ambulatorio_atendimento_soap_ferida_material.saida_item_id ), 0) ) AS valor_total "), DB::raw("COUNT(DISTINCT ambulatorio_atendimento_soap.id) AS soaps_geradas"), DB::Raw("COUNT(DISTINCT ambulatorio_atendimento_soap_ferida.id) AS feridas_registradas"), DB::Raw("COUNT(ambulatorio_atendimento_soap_ferida_procedimento.id) AS procedimentos_lancados"), DB::Raw("COUNT(ambulatorio_atendimento_soap_ferida_material.id) AS kits_materiais_consumidos"), ]) ->join('ambulatorio_atendimento_procedimento', 'ambulatorio_atendimento_procedimento.id', 'ambulatorio_atendimento_soap_ferida_procedimento.atendimento_procedimento_id') ->join('ambulatorio_atendimento_soap_ferida', 'ambulatorio_atendimento_soap_ferida.id', 'ambulatorio_atendimento_soap_ferida_procedimento.soap_ferida_id') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id') ->join('cadastro_paciente', 'cadastro_paciente.id', 'ambulatorio_demanda_espontanea.paciente_id') ->join('cadastro_cidadao AS cadastro_cidadao_paciente', 'cadastro_cidadao_paciente.id', 'cadastro_paciente.cidadao_id') ->join('cadastro_usuario', 'cadastro_usuario.id', 'cadastro_lotacao.usuario_id') ->join('cadastro_cidadao AS cadastro_cidadao_usuario', 'cadastro_cidadao_usuario.id', 'cadastro_usuario.cidadao_id') ->join('cadastro_ferida', 'cadastro_ferida.id', 'ambulatorio_atendimento_soap_ferida.ferida_id') ->leftJoin('cadastro_especialidade', 'cadastro_especialidade.id', 'ambulatorio_atendimento_procedimento.especialidade_id') ->leftJoin('sigtap.ocupacao', 'ocupacao.id', 'ambulatorio_atendimento_procedimento.ocupacao_id') ->leftJoin('sigtap.procedimento', 'procedimento.id', 'ambulatorio_atendimento_procedimento.procedimento_id') ->leftJoin('ambulatorio_atendimento_soap_ferida_material', 'ambulatorio_atendimento_soap_ferida_material.atendimento_procedimento_id', 'ambulatorio_atendimento_procedimento.id') ->leftJoin('cadastro_material', 'cadastro_material.id', 'ambulatorio_atendimento_soap_ferida_material.material_id') ->where('ambulatorio_atendimento_soap.is_rascunho', false) ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ->when($params->unidade_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.unidade_id', $id)) ->when($params->infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.id', $id)) ->when($params->ocupacao_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.ocupacao_id', $id)) ->when($params->especialidade_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.especialidade_id', $id)) ->when($params->usuario_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.usuario_id', $id)) ->when($params->feridas, fn ($q, $ids) => $q->whereIn('ambulatorio_atendimento_soap_ferida.ferida_id', $ids)) ->when($params->status, fn ($q, $status) => $q->where(function ($q) use ($status) { if ($status === 'cicatrizada') { $q->orWhere('ambulatorio_atendimento_soap_ferida.cicatrizada', true); } else if ($status === 'não_cicatrizada') { $q->orWhere('ambulatorio_atendimento_soap_ferida.cicatrizada', false); } else if ($status === 'indefinido') { $q->orWhereNull('ambulatorio_atendimento_soap_ferida.cicatrizada'); } })) ->when($params->procedimentos, fn ($q, $ids) => $q->whereIn('ambulatorio_atendimento_procedimento.procedimento_id', $ids)) ->when($params->paciente_id, fn ($q, $id) => $q->where('cadastro_paciente.id', $id)) ->groupBy([ DB::raw("DATE_FORMAT(ambulatorio_demanda_espontanea.data, '%d/%m/%Y')"), 'ambulatorio_atendimento.id', 'cadastro_paciente.id', 'cadastro_cidadao_paciente.nome', DB::raw("DATE_FORMAT(cadastro_cidadao_paciente.data_nascimento, '%d/%m/%Y')"), DB::raw("LPAD(cadastro_paciente.prontuario, 6, '0')"), 'cadastro_unidade.nome_fantasia', 'cadastro_cidadao_usuario.nome', 'cadastro_ferida.nome', 'cadastro_especialidade.nome', 'ocupacao.codigo', DB::raw(" CASE WHEN ambulatorio_atendimento_soap_ferida.cicatrizada = TRUE THEN 'Cicatrizada' WHEN ambulatorio_atendimento_soap_ferida.cicatrizada = FALSE THEN 'Não Cicatrizada' ELSE 'Indefinido' END "), ]) ) ->groupBy([ DB::raw("DATE_FORMAT(ambulatorio_jornada.data, '%d/%m/%Y')"), 'ambulatorio_atendimento.id', 'cadastro_paciente.id', 'cadastro_cidadao_paciente.nome', DB::raw("DATE_FORMAT(cadastro_cidadao_paciente.data_nascimento, '%d/%m/%Y')"), DB::raw("LPAD(cadastro_paciente.prontuario, 6, '0')"), 'cadastro_unidade.nome_fantasia', 'cadastro_cidadao_usuario.nome', 'cadastro_ferida.nome', 'cadastro_especialidade.nome', 'ocupacao.codigo', DB::raw(" CASE WHEN ambulatorio_atendimento_soap_ferida.cicatrizada = TRUE THEN 'Cicatrizada' WHEN ambulatorio_atendimento_soap_ferida.cicatrizada = FALSE THEN 'Não Cicatrizada' ELSE 'Indefinido' END "), ]) ->get(); return [ 'totalizadores' => [ 'pacientes_distintos_atendidos' => count($itens->pluck('paciente_id')->unique()), 'atendimentos_realizados' => count($itens->pluck('atendimento_id')->unique()), 'soaps_geradas' => $itens->sum('soaps_geradas'), 'feridas_registradas' => $itens->sum('feridas_registradas'), 'procedimentos_lancados' => $itens->sum('procedimentos_lancados'), 'kits_materiais_consumidos' => $itens->sum('kits_materiais_consumidos'), 'valor_custo_procedimento' => number_format($itens->sum('valor_custo_procedimento'), 2, ',', '.'), 'valor_custo_material' => number_format($itens->sum('valor_custo_material'), 2, ',', '.'), 'valor_custo_total' => number_format($itens->sum('valor_total'), 2, ',', '.'), ], 'itens' => $itens->map(function ($item) { return [ 'data' => $item->data, 'paciente_nome' => $item->paciente_nome, 'paciente_data_nascimento' => $item->paciente_data_nascimento, 'paciente_prontuario' => $item->paciente_prontuario, 'unidade_nome_fantasia' => $item->unidade_nome_fantasia, 'usuario_nome' => $item->usuario_nome, 'ferida_nome' => $item->ferida_nome, 'especialidade_nome' => $item->especialidade_nome, 'ocupacao_codigo' => $item->ocupacao_codigo, 'status' => $item->status, 'procedimentos' => $item->procedimentos, 'materiais' => $item->materiais, 'valor_total' => number_format($item->valor_total, 2, ',', '.'), ]; }), ]; } public static function create() { return [ 'additionals' => [ 'data_atual' => Carbon::now()->format('d/m/Y'), ], 'form' => [ 'unidades' => [], 'infraestruturas' => [], 'ocupacoes' => [], 'especialidades' => [], 'usuarios' => [], 'feridas' => [], 'status' => [], 'procedimentos' => [], ], 'model' => [ 'periodo' => ['start' => null, 'end' => null], 'unidade_id' => null, 'infraestrutura_id' => null, 'ocupacao_id' => null, 'especialidade_id' => null, 'usuario_id' => null, 'feridas' => null, 'status' => null, 'procedimentos' => null, ], ]; } public static function autocomplete() { $params = self::getParams(); return DB::table('cadastro_paciente') ->select([ 'cadastro_paciente.id', 'cadastro_cidadao.nome', 'cadastro_cidadao.nome_social', 'cadastro_cidadao.cns', ]) ->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id') ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_ferida_procedimento') ->join('ambulatorio_atendimento_procedimento', 'ambulatorio_atendimento_procedimento.id', 'ambulatorio_atendimento_soap_ferida_procedimento.atendimento_procedimento_id') ->join('ambulatorio_atendimento_soap_ferida', 'ambulatorio_atendimento_soap_ferida.id', 'ambulatorio_atendimento_soap_ferida_procedimento.soap_ferida_id') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->whereBetween('ambulatorio_jornada.data', $params->periodo) ->when($params->unidade_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.unidade_id', $id)) ->when($params->infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.id', $id)) ->when($params->ocupacao_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.ocupacao_id', $id)) ->when($params->especialidade_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.especialidade_id', $id)) ->when($params->usuario_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.usuario_id', $id)) ->when($params->feridas, fn ($q, $ids) => $q->whereIn('ambulatorio_atendimento_soap_ferida.ferida_id', $ids)) ->when($params->status, fn ($q, $status) => $q->where(function ($q) use ($status) { if ($status === 'cicatrizada') { $q->orWhere('ambulatorio_atendimento_soap_ferida.cicatrizada', true); } else if ($status === 'não_cicatrizada') { $q->orWhere('ambulatorio_atendimento_soap_ferida.cicatrizada', false); } else if ($status === 'indefinido') { $q->orWhereNull('ambulatorio_atendimento_soap_ferida.cicatrizada'); } })) ->when($params->procedimentos, fn ($q, $ids) => $q->whereIn('ambulatorio_atendimento_procedimento.procedimento_id', $ids)) ->whereColumn('ambulatorio_agendamento.paciente_id', 'cadastro_paciente.id') ) ->orWhereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_ferida_procedimento') ->join('ambulatorio_atendimento_procedimento', 'ambulatorio_atendimento_procedimento.id', 'ambulatorio_atendimento_soap_ferida_procedimento.atendimento_procedimento_id') ->join('ambulatorio_atendimento_soap_ferida', 'ambulatorio_atendimento_soap_ferida.id', 'ambulatorio_atendimento_soap_ferida_procedimento.soap_ferida_id') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->where('ambulatorio_atendimento_soap.is_rascunho', false) ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ->when($params->unidade_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.unidade_id', $id)) ->when($params->infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.id', $id)) ->when($params->ocupacao_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.ocupacao_id', $id)) ->when($params->especialidade_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.especialidade_id', $id)) ->when($params->usuario_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.usuario_id', $id)) ->when($params->feridas, fn ($q, $ids) => $q->whereIn('ambulatorio_atendimento_soap_ferida.ferida_id', $ids)) ->when($params->status, fn ($q, $status) => $q->where(function ($q) use ($status) { if ($status === 'cicatrizada') { $q->orWhere('ambulatorio_atendimento_soap_ferida.cicatrizada', true); } else if ($status === 'não_cicatrizada') { $q->orWhere('ambulatorio_atendimento_soap_ferida.cicatrizada', false); } else if ($status === 'indefinido') { $q->orWhereNull('ambulatorio_atendimento_soap_ferida.cicatrizada'); } })) ->when($params->procedimentos, fn ($q, $ids) => $q->whereIn('ambulatorio_atendimento_procedimento.procedimento_id', $ids)) ->whereColumn('ambulatorio_demanda_espontanea.paciente_id', 'cadastro_paciente.id') ); } public static function list() { $params = self::getParams(); switch (request()->listar) { case 'unidades': { return [ 'unidades' => self::getUnidades($params), 'infraestruturas' => self::getInfraestruturas($params), 'ocupacoes' => self::getOcupacoes($params), 'especialidades' => self::getEspecialidades($params), 'usuarios' => self::getUsuarios($params), 'feridas' => self::getFeridas($params), 'status' => self::getStatus($params), 'procedimentos' => self::getProcedimentos($params), ]; } case 'infraestruturas': { return [ 'infraestruturas' => self::getInfraestruturas($params), 'ocupacoes' => self::getOcupacoes($params), 'especialidades' => self::getEspecialidades($params), 'usuarios' => self::getUsuarios($params), 'feridas' => self::getFeridas($params), 'status' => self::getStatus($params), 'procedimentos' => self::getProcedimentos($params), ]; } case 'ocupacoes': { return [ 'ocupacoes' => self::getOcupacoes($params), 'especialidades' => self::getEspecialidades($params), 'usuarios' => self::getUsuarios($params), 'feridas' => self::getFeridas($params), 'status' => self::getStatus($params), 'procedimentos' => self::getProcedimentos($params), ]; } case 'especialidades': { return [ 'especialidades' => self::getEspecialidades($params), 'usuarios' => self::getUsuarios($params), 'feridas' => self::getFeridas($params), 'status' => self::getStatus($params), 'procedimentos' => self::getProcedimentos($params), ]; } case 'usuarios': { return [ 'usuarios' => self::getUsuarios($params), 'feridas' => self::getFeridas($params), 'status' => self::getStatus($params), 'procedimentos' => self::getProcedimentos($params), ]; } case 'feridas': { return [ 'feridas' => self::getFeridas($params), 'status' => self::getStatus($params), 'procedimentos' => self::getProcedimentos($params), ]; } case 'status': { return [ 'status' => self::getStatus($params), 'procedimentos' => self::getProcedimentos($params), ]; } case 'procedimentos': { return [ 'procedimentos' => self::getProcedimentos($params), ]; } } } private static function getParams() { return (object) [ 'periodo' => [ 'start' => Carbon::createFromFormat('d/m/Y', request()->get('periodo')['start'])->setTime(0, 0, 0), 'end' => Carbon::createFromFormat('d/m/Y', request()->get('periodo')['end'])->setTime(23, 59, 59) ], 'unidade_id' => request()->get('unidade_id'), 'infraestrutura_id' => request()->get('infraestrutura_id'), 'ocupacao_id' => request()->get('ocupacao_id'), 'especialidade_id' => request()->get('especialidade_id'), 'usuario_id' => request()->get('usuario_id'), 'feridas' => request()->get('feridas'), 'status' => request()->get('status'), 'procedimentos' => request()->get('procedimentos'), 'paciente_id' => request()->get('paciente')['id'] ?? null, ]; } private static function getUnidades($params) { return DB::table('cadastro_unidade') ->select([ 'cadastro_unidade.id', 'cadastro_unidade.nome_fantasia' ]) ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_ferida') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->whereBetween('ambulatorio_jornada.data', $params->periodo) ->whereColumn('cadastro_infraestrutura.unidade_id', 'cadastro_unidade.id') ) ->orWhereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_ferida') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->where('ambulatorio_atendimento_soap.is_rascunho', false) ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ->whereColumn('cadastro_infraestrutura.unidade_id', 'cadastro_unidade.id') ) ->orderBy('cadastro_unidade.nome_fantasia') ->get(); } private static function getInfraestruturas($params) { return DB::table('cadastro_infraestrutura') ->select([ 'cadastro_infraestrutura.id', DB::raw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia) as nome") ]) ->join('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id') ->when($params->unidade_id, fn ($q, $id) => $q->where('cadastro_unidade.id', $id)) ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_ferida') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id') ->whereBetween('ambulatorio_jornada.data', $params->periodo) ->whereColumn('cadastro_lotacao.infraestrutura_id', 'cadastro_infraestrutura.id') ) ->orWhereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_ferida') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->where('ambulatorio_atendimento_soap.is_rascunho', false) ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ->whereColumn('cadastro_lotacao.infraestrutura_id', 'cadastro_infraestrutura.id') ) ->orderByRaw("COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia)") ->get(); } private static function getOcupacoes($params) { return DB::table('sigtap.ocupacao') ->select([ 'ocupacao.id', DB::raw("CONCAT(ocupacao.codigo, ' - ', ocupacao.nome) AS nome"), ]) ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_ferida_procedimento') ->join('ambulatorio_atendimento_procedimento', 'ambulatorio_atendimento_procedimento.id', 'ambulatorio_atendimento_soap_ferida_procedimento.atendimento_procedimento_id') ->join('ambulatorio_atendimento_soap_ferida', 'ambulatorio_atendimento_soap_ferida.id', 'ambulatorio_atendimento_soap_ferida_procedimento.soap_ferida_id') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->whereBetween('ambulatorio_jornada.data', $params->periodo) ->when($params->unidade_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.unidade_id', $id)) ->when($params->infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.id', $id)) ->whereColumn('ambulatorio_atendimento_procedimento.ocupacao_id', 'ocupacao.id') ) ->orWhereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_ferida_procedimento') ->join('ambulatorio_atendimento_procedimento', 'ambulatorio_atendimento_procedimento.id', 'ambulatorio_atendimento_soap_ferida_procedimento.atendimento_procedimento_id') ->join('ambulatorio_atendimento_soap_ferida', 'ambulatorio_atendimento_soap_ferida.id', 'ambulatorio_atendimento_soap_ferida_procedimento.soap_ferida_id') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->where('ambulatorio_atendimento_soap.is_rascunho', false) ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ->when($params->unidade_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.unidade_id', $id)) ->when($params->infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.id', $id)) ->whereColumn('ambulatorio_atendimento_procedimento.ocupacao_id', 'ocupacao.id') ) ->orderByRaw("CONCAT(ocupacao.codigo, ' - ', ocupacao.nome)") ->get(); } private static function getEspecialidades($params) { return DB::table('cadastro_especialidade') ->select([ 'cadastro_especialidade.id', 'cadastro_especialidade.nome', ]) ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_ferida_procedimento') ->join('ambulatorio_atendimento_procedimento', 'ambulatorio_atendimento_procedimento.id', 'ambulatorio_atendimento_soap_ferida_procedimento.atendimento_procedimento_id') ->join('ambulatorio_atendimento_soap_ferida', 'ambulatorio_atendimento_soap_ferida.id', 'ambulatorio_atendimento_soap_ferida_procedimento.soap_ferida_id') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->whereBetween('ambulatorio_jornada.data', $params->periodo) ->when($params->unidade_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.unidade_id', $id)) ->when($params->infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.id', $id)) ->when($params->ocupacao_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.ocupacao_id', $id)) ->whereColumn('ambulatorio_atendimento_procedimento.especialidade_id', 'cadastro_especialidade.id') ) ->orWhereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_ferida_procedimento') ->join('ambulatorio_atendimento_procedimento', 'ambulatorio_atendimento_procedimento.id', 'ambulatorio_atendimento_soap_ferida_procedimento.atendimento_procedimento_id') ->join('ambulatorio_atendimento_soap_ferida', 'ambulatorio_atendimento_soap_ferida.id', 'ambulatorio_atendimento_soap_ferida_procedimento.soap_ferida_id') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->where('ambulatorio_atendimento_soap.is_rascunho', false) ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ->when($params->unidade_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.unidade_id', $id)) ->when($params->infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.id', $id)) ->when($params->ocupacao_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.ocupacao_id', $id)) ->whereColumn('ambulatorio_atendimento_procedimento.especialidade_id', 'cadastro_especialidade.id') ) ->orderBy('cadastro_especialidade.nome') ->get(); } private static function getUsuarios($params) { return DB::table('cadastro_usuario') ->select([ 'cadastro_usuario.id', 'cadastro_cidadao.nome', ]) ->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id') ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_ferida_procedimento') ->join('ambulatorio_atendimento_procedimento', 'ambulatorio_atendimento_procedimento.id', 'ambulatorio_atendimento_soap_ferida_procedimento.atendimento_procedimento_id') ->join('ambulatorio_atendimento_soap_ferida', 'ambulatorio_atendimento_soap_ferida.id', 'ambulatorio_atendimento_soap_ferida_procedimento.soap_ferida_id') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->whereBetween('ambulatorio_jornada.data', $params->periodo) ->when($params->unidade_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.unidade_id', $id)) ->when($params->infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.id', $id)) ->when($params->ocupacao_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.ocupacao_id', $id)) ->when($params->especialidade_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.especialidade_id', $id)) ->whereColumn('ambulatorio_atendimento_procedimento.usuario_id', 'cadastro_usuario.id') ) ->orWhereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_ferida_procedimento') ->join('ambulatorio_atendimento_procedimento', 'ambulatorio_atendimento_procedimento.id', 'ambulatorio_atendimento_soap_ferida_procedimento.atendimento_procedimento_id') ->join('ambulatorio_atendimento_soap_ferida', 'ambulatorio_atendimento_soap_ferida.id', 'ambulatorio_atendimento_soap_ferida_procedimento.soap_ferida_id') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->where('ambulatorio_atendimento_soap.is_rascunho', false) ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ->when($params->unidade_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.unidade_id', $id)) ->when($params->infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.id', $id)) ->when($params->ocupacao_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.ocupacao_id', $id)) ->when($params->especialidade_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.especialidade_id', $id)) ->whereColumn('ambulatorio_atendimento_procedimento.usuario_id', 'cadastro_usuario.id') ) ->orderBy('cadastro_cidadao.nome') ->get(); } private static function getFeridas($params) { return DB::table('cadastro_ferida') ->select([ 'cadastro_ferida.id', 'cadastro_ferida.nome', ]) ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_ferida_procedimento') ->join('ambulatorio_atendimento_procedimento', 'ambulatorio_atendimento_procedimento.id', 'ambulatorio_atendimento_soap_ferida_procedimento.atendimento_procedimento_id') ->join('ambulatorio_atendimento_soap_ferida', 'ambulatorio_atendimento_soap_ferida.id', 'ambulatorio_atendimento_soap_ferida_procedimento.soap_ferida_id') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->whereBetween('ambulatorio_jornada.data', $params->periodo) ->when($params->unidade_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.unidade_id', $id)) ->when($params->infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.id', $id)) ->when($params->ocupacao_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.ocupacao_id', $id)) ->when($params->especialidade_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.especialidade_id', $id)) ->when($params->usuario_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.usuario_id', $id)) ->whereColumn('ambulatorio_atendimento_soap_ferida.ferida_id', 'cadastro_ferida.id') ) ->orWhereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_ferida_procedimento') ->join('ambulatorio_atendimento_procedimento', 'ambulatorio_atendimento_procedimento.id', 'ambulatorio_atendimento_soap_ferida_procedimento.atendimento_procedimento_id') ->join('ambulatorio_atendimento_soap_ferida', 'ambulatorio_atendimento_soap_ferida.id', 'ambulatorio_atendimento_soap_ferida_procedimento.soap_ferida_id') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->where('ambulatorio_atendimento_soap.is_rascunho', false) ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ->when($params->unidade_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.unidade_id', $id)) ->when($params->infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.id', $id)) ->when($params->ocupacao_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.ocupacao_id', $id)) ->when($params->especialidade_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.especialidade_id', $id)) ->when($params->usuario_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.usuario_id', $id)) ->whereColumn('ambulatorio_atendimento_soap_ferida.ferida_id', 'cadastro_ferida.id') ) ->orderBy('cadastro_ferida.nome') ->get(); } private static function getStatus($params) { return DB::table('ambulatorio_atendimento_soap_ferida_procedimento') ->select([ DB::raw(" CASE WHEN ambulatorio_atendimento_soap_ferida.cicatrizada = TRUE THEN 'cicatrizada' WHEN ambulatorio_atendimento_soap_ferida.cicatrizada = FALSE THEN 'não_cicatrizada' ELSE 'indefinido' END AS value "), DB::raw(" CASE WHEN ambulatorio_atendimento_soap_ferida.cicatrizada = TRUE THEN 'Cicatrizada' WHEN ambulatorio_atendimento_soap_ferida.cicatrizada = FALSE THEN 'Não Cicatrizada' ELSE 'Indefinido' END AS text "), ]) ->join('ambulatorio_atendimento_procedimento', 'ambulatorio_atendimento_procedimento.id', 'ambulatorio_atendimento_soap_ferida_procedimento.atendimento_procedimento_id') ->join('ambulatorio_atendimento_soap_ferida', 'ambulatorio_atendimento_soap_ferida.id', 'ambulatorio_atendimento_soap_ferida_procedimento.soap_ferida_id') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->whereBetween('ambulatorio_jornada.data', $params->periodo) ->when($params->unidade_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.unidade_id', $id)) ->when($params->infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.id', $id)) ->when($params->ocupacao_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.ocupacao_id', $id)) ->when($params->especialidade_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.especialidade_id', $id)) ->when($params->usuario_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.usuario_id', $id)) ->when($params->feridas, fn ($q, $ids) => $q->whereIn('ambulatorio_atendimento_soap_ferida.ferida_id', $ids)) ->unionAll( DB::table('ambulatorio_atendimento_soap_ferida_procedimento') ->select([ DB::raw(" CASE WHEN ambulatorio_atendimento_soap_ferida.cicatrizada = TRUE THEN 'cicatrizada' WHEN ambulatorio_atendimento_soap_ferida.cicatrizada = FALSE THEN 'não_cicatrizada' ELSE 'indefinido' END AS value "), DB::raw(" CASE WHEN ambulatorio_atendimento_soap_ferida.cicatrizada = TRUE THEN 'Cicatrizada' WHEN ambulatorio_atendimento_soap_ferida.cicatrizada = FALSE THEN 'Não Cicatrizada' ELSE 'Indefinido' END AS text "), ]) ->join('ambulatorio_atendimento_procedimento', 'ambulatorio_atendimento_procedimento.id', 'ambulatorio_atendimento_soap_ferida_procedimento.atendimento_procedimento_id') ->join('ambulatorio_atendimento_soap_ferida', 'ambulatorio_atendimento_soap_ferida.id', 'ambulatorio_atendimento_soap_ferida_procedimento.soap_ferida_id') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->where('ambulatorio_atendimento_soap.is_rascunho', false) ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ->when($params->unidade_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.unidade_id', $id)) ->when($params->infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.id', $id)) ->when($params->ocupacao_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.ocupacao_id', $id)) ->when($params->especialidade_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.especialidade_id', $id)) ->when($params->usuario_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.usuario_id', $id)) ->when($params->feridas, fn ($q, $ids) => $q->whereIn('ambulatorio_atendimento_soap_ferida.ferida_id', $ids)) ->distinct() ) ->distinct() ->get(); } private static function getProcedimentos($params) { return DB::table('sigtap.procedimento') ->select([ 'procedimento.id', 'procedimento.codigo', 'procedimento.nome', ]) ->whereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_ferida_procedimento') ->join('ambulatorio_atendimento_procedimento', 'ambulatorio_atendimento_procedimento.id', 'ambulatorio_atendimento_soap_ferida_procedimento.atendimento_procedimento_id') ->join('ambulatorio_atendimento_soap_ferida', 'ambulatorio_atendimento_soap_ferida.id', 'ambulatorio_atendimento_soap_ferida_procedimento.soap_ferida_id') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id') ->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->whereBetween('ambulatorio_jornada.data', $params->periodo) ->when($params->unidade_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.unidade_id', $id)) ->when($params->infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.id', $id)) ->when($params->ocupacao_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.ocupacao_id', $id)) ->when($params->especialidade_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.especialidade_id', $id)) ->when($params->usuario_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.usuario_id', $id)) ->when($params->feridas, fn ($q, $ids) => $q->whereIn('ambulatorio_atendimento_soap_ferida.ferida_id', $ids)) ->when($params->status, fn ($q, $status) => $q->where(function ($q) use ($status) { if ($status === 'cicatrizada') { $q->orWhere('ambulatorio_atendimento_soap_ferida.cicatrizada', true); } else if ($status === 'não_cicatrizada') { $q->orWhere('ambulatorio_atendimento_soap_ferida.cicatrizada', false); } else if ($status === 'indefinido') { $q->orWhereNull('ambulatorio_atendimento_soap_ferida.cicatrizada'); } })) ->whereColumn('ambulatorio_atendimento_procedimento.procedimento_id', 'procedimento.id') ) ->orWhereExists(fn ($q) => $q ->select(DB::raw(1)) ->from('ambulatorio_atendimento_soap_ferida_procedimento') ->join('ambulatorio_atendimento_procedimento', 'ambulatorio_atendimento_procedimento.id', 'ambulatorio_atendimento_soap_ferida_procedimento.atendimento_procedimento_id') ->join('ambulatorio_atendimento_soap_ferida', 'ambulatorio_atendimento_soap_ferida.id', 'ambulatorio_atendimento_soap_ferida_procedimento.soap_ferida_id') ->join('ambulatorio_atendimento_soap', 'ambulatorio_atendimento_soap.id', 'ambulatorio_atendimento_soap_ferida.soap_id') ->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id') ->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id') ->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id') ->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id') ->where('ambulatorio_atendimento_soap.is_rascunho', false) ->whereBetween('ambulatorio_demanda_espontanea.data', $params->periodo) ->when($params->unidade_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.unidade_id', $id)) ->when($params->infraestrutura_id, fn ($q, $id) => $q->where('cadastro_infraestrutura.id', $id)) ->when($params->ocupacao_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.ocupacao_id', $id)) ->when($params->especialidade_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.especialidade_id', $id)) ->when($params->usuario_id, fn ($q, $id) => $q->where('ambulatorio_atendimento_procedimento.usuario_id', $id)) ->when($params->feridas, fn ($q, $ids) => $q->whereIn('ambulatorio_atendimento_soap_ferida.ferida_id', $ids)) ->when($params->status, fn ($q, $status) => $q->where(function ($q) use ($status) { if ($status === 'cicatrizada') { $q->orWhere('ambulatorio_atendimento_soap_ferida.cicatrizada', true); } else if ($status === 'não_cicatrizada') { $q->orWhere('ambulatorio_atendimento_soap_ferida.cicatrizada', false); } else if ($status === 'indefinido') { $q->orWhereNull('ambulatorio_atendimento_soap_ferida.cicatrizada'); } })) ->whereColumn('ambulatorio_atendimento_procedimento.procedimento_id', 'procedimento.id') ) ->orderBy('procedimento.nome') ->get() ->map(function ($procedimento) { $procedimento->nome = MaskUtil::getProcedimentoMasked($procedimento->codigo) . " - {$procedimento->nome}"; return $procedimento; }); } public static function getPrintExportData() { $itens = RelatorioRastreioFeridaService::index(); $unidade = Unidade::find(request()->unidade_id); $infraestrutura = Infraestrutura::find(request()->infraestrutura_id); $especialidade = Especialidade::find(request()->especialidade_id); $usuario = Usuario::find(request()->usuario_id); $ocupacao = Ocupacao::find(request()->ocupacao_id); $status = request()->status_id; if (!is_null($status)) { switch ($status) { case FeridaStatus::ATIVO: $status = (object) ['nome' => 'Ativo']; break; case FeridaStatus::CICATRIZADA: $status = (object) ['nome' => 'Cicatrizada']; break; default: break; } }else{ $status = (object) ['nome' => 'Todos']; } if (isset($itens['atendimentos']) && is_array($itens['atendimentos'])) { $itens['atendimentos'] = collect($itens['atendimentos'])->map(function ($atendimento) { switch ((int) ($atendimento['cicatrizada'] ?? -1)) { case FeridaStatus::ATIVO: $atendimento['cicatrizada'] = 'Ativo'; break; case FeridaStatus::CICATRIZADA: $atendimento['cicatrizada'] = 'Cicatrizada'; break; default: $atendimento['cicatrizada'] = '-'; break; } return $atendimento; })->toArray(); } return [ 'periodo' => [ 'inicio' => request()->periodo['start'], 'fim' => request()->periodo['end'], ], 'unidade' => $unidade->nome_fantasia ?? 'Todas', 'infraestrutura' => $infraestrutura->nome ?? 'Todas', 'especialidade' => $especialidade->nome ?? 'Todas', 'usuario' => $usuario->nome ?? 'Todos', 'cbo' => $ocupacao->codigo ?? 'Todos', 'status' => $status->nome, 'colunas' => collect(request()->columns), 'itens' => $itens, ]; } }