@SuppressWarnings("unchecked")
  @Override
  public List<FiseTipDocRef> buscarFiseTipDocRef(String id, String descripcion)
      throws SQLException {

    String q = "SELECT d FROM " + FiseTipDocRef.class.getName() + " d WHERE 1=1 ";
    if (FormatoUtil.isNotBlank(id)) {
      q = q.concat(" AND d.idTipDocRef = :id ");
    }
    if (FormatoUtil.isNotBlank(descripcion)) {
      q = q.concat(" AND d.descripcion LIKE :descripcion ");
    }
    q = q.concat(" ORDER BY d.descripcion ASC");

    Query query = em.createQuery(q);
    if (FormatoUtil.isNotBlank(id)) {
      query.setParameter("id", id);
    }
    if (FormatoUtil.isNotBlank(descripcion)) {
      String des = "%" + descripcion + "%";
      query.setParameter("descripcion", des);
    }
    List<FiseTipDocRef> lista = query.getResultList();
    if (lista == null) {
      return Collections.EMPTY_LIST;
    } else {
      return lista;
    }
  }
  @SuppressWarnings("unchecked")
  @Override
  public List<FiseFormato12CC> buscarFormato12CCReporteObs(
      String codEmpresa, long idGrupoInf, String etapa) throws SQLException {

    String q = "SELECT f FROM " + FiseFormato12CC.class.getName() + " f WHERE 1=1 ";
    if (FormatoUtil.isNotBlank(codEmpresa)) {
      q = q.concat(" AND f.id.codEmpresa = :codEmpresa ");
    }
    if (idGrupoInf != 0) {
      q = q.concat(" AND f.fiseGrupoInformacion.idGrupoInformacion = :idGrupo ");
    }
    if (FormatoUtil.isNotBlank(etapa)) {
      q = q.concat(" AND f.id.etapa = :etapa ");
    }
    // q = q.concat(" ORDER BY f.id.codEmpresa");
    Query query = em.createQuery(q);
    if (FormatoUtil.isNotBlank(codEmpresa)) {
      String codEmpreCompleta = FormatoUtil.rellenaDerecha(codEmpresa, ' ', 4);
      query.setParameter("codEmpresa", codEmpreCompleta);
    }
    if (idGrupoInf != 0) {
      query.setParameter("idGrupo", idGrupoInf);
    }
    if (FormatoUtil.isNotBlank(etapa)) {
      query.setParameter("etapa", etapa);
    }
    List<FiseFormato12CC> lista = query.getResultList();
    if (lista == null) {
      return Collections.EMPTY_LIST;
    } else {
      return lista;
    }
  }
  @SuppressWarnings("unchecked")
  @Override
  public List<FiseFormato12CC> buscarFormato12CC(
      String codEmpresa,
      long anioDesde,
      long mesDesde,
      long anioHasta,
      long mesHasta,
      String etapa) {
    List<FiseFormato12CC> lista = null;
    try {
      String q = "SELECT t FROM FiseFormato12CC t WHERE 1=1 ";
      if (FormatoUtil.isNotBlank(codEmpresa)) {
        q = q + " AND t.id.codEmpresa = :codEmpresa ";
      }
      q = q + " AND t.id.anoPresentacion*100+t.id.mesPresentacion >= :fechaDesde ";
      q = q + " AND t.id.anoPresentacion*100+t.id.mesPresentacion <= :fechaHasta ";
      if (FormatoUtil.isNotBlank(etapa)) {
        q = q + " AND t.id.etapa = :etapa ";
      }
      Query query = em.createQuery(q);
      if (FormatoUtil.isNotBlank(codEmpresa)) {
        query.setParameter("codEmpresa", codEmpresa);
      }
      long fechaDesde = 0;
      if (anioDesde != 0) {
        fechaDesde = anioDesde * 100;
      }
      if (mesDesde != 0) {
        fechaDesde = fechaDesde + mesDesde;
      }
      long fechaHasta = 0;
      if (anioHasta != 0) {
        fechaHasta = anioHasta * 100;
      }
      if (mesHasta != 0) {
        fechaHasta = fechaHasta + mesHasta;
      }
      query.setParameter("fechaDesde", fechaDesde);
      query.setParameter("fechaHasta", fechaHasta);
      if (FormatoUtil.isNotBlank(etapa)) {
        query.setParameter("etapa", etapa);
      }

      lista = query.getResultList();
      System.out.println("SQL   > " + query.toString());

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      em.close();
    }
    return lista;
  }
  @SuppressWarnings("unchecked")
  @Override
  public List<FiseFormato12CC> buscarFormato12CCReenvio(
      String codEmpresa, long anioPres, long mesPres, String etapa) throws SQLException {

    String q = "SELECT f FROM " + FiseFormato12CC.class.getName() + " f WHERE 1=1 ";
    if (FormatoUtil.isNotBlank(codEmpresa)) {
      q = q.concat(" AND f.id.codEmpresa = :codEmpresa ");
    }
    if (anioPres != 0) {
      q = q.concat(" AND f.id.anoPresentacion =:anioPres ");
    }
    if (mesPres != 0) {
      q = q.concat(" AND f.id.mesPresentacion = :mesPres ");
    }
    if (FormatoUtil.isNotBlank(etapa)) {
      q = q.concat(" AND f.id.etapa = :etapa ");
    }
    q = q.concat(" AND f.fechaEnvioDefinitivo IS NOT NULL ");
    Query query = em.createQuery(q);
    if (FormatoUtil.isNotBlank(codEmpresa)) {
      String codEmpreCompleta = FormatoUtil.rellenaDerecha(codEmpresa, ' ', 4);
      query.setParameter("codEmpresa", codEmpreCompleta);
    }
    if (anioPres != 0) {
      query.setParameter("anioPres", anioPres);
    }
    if (mesPres != 0) {
      query.setParameter("mesPres", mesPres);
    }
    if (FormatoUtil.isNotBlank(etapa)) {
      query.setParameter("etapa", etapa);
    }
    List<FiseFormato12CC> lista = query.getResultList();
    if (lista == null) {
      return Collections.EMPTY_LIST;
    } else {
      return lista;
    }
  }
 @Override
 public FiseFormato12CC obtenerFormato12CCByPK(FiseFormato12CCPK fiseFormato12CCPK) {
   FiseFormato12CC formato = null;
   try {
     fiseFormato12CCPK.setCodEmpresa(
         FormatoUtil.rellenaDerecha(fiseFormato12CCPK.getCodEmpresa(), ' ', 4));
     formato = em.find(FiseFormato12CC.class, fiseFormato12CCPK);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     em.close();
   }
   return formato;
 }
 @Override
 public boolean existeFormato12CC(FiseFormato12CC fiseFormato12CC) {
   boolean existe = false;
   try {
     fiseFormato12CC
         .getId()
         .setCodEmpresa(
             FormatoUtil.rellenaDerecha(fiseFormato12CC.getId().getCodEmpresa(), ' ', 4));
     FiseFormato12CC formato = em.find(FiseFormato12CC.class, fiseFormato12CC.getId());
     if (formato != null) {
       existe = true;
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     em.close();
   }
   return existe;
 }