diff --git a/src/com/rjconsultores/ventaboletos/dao/BpeDAO.java b/src/com/rjconsultores/ventaboletos/dao/BpeDAO.java index d9603ad35..c0f27d0f5 100644 --- a/src/com/rjconsultores/ventaboletos/dao/BpeDAO.java +++ b/src/com/rjconsultores/ventaboletos/dao/BpeDAO.java @@ -21,4 +21,6 @@ public interface BpeDAO { public List buscarBPeRejeitadosContingencia(Integer empresaId, String numBpe, String chbpe, Date dtVendaInicio, Date dtVendaFim, List estados, List codigosRejeicoes); public void definirBPeRejeitadoSefazReenvio(Integer bpeId, String codstat) throws BusinessException; + + public List buscarBPeVendaEEventosAutorizados(Integer empresaId, Date dtVendaInicio, Date dtVendaFim, Integer estadoId); } diff --git a/src/com/rjconsultores/ventaboletos/dao/hibernate/BpeHibernateDAO.java b/src/com/rjconsultores/ventaboletos/dao/hibernate/BpeHibernateDAO.java index 46839ca16..746d7fcd0 100644 --- a/src/com/rjconsultores/ventaboletos/dao/hibernate/BpeHibernateDAO.java +++ b/src/com/rjconsultores/ventaboletos/dao/hibernate/BpeHibernateDAO.java @@ -1641,4 +1641,64 @@ public class BpeHibernateDAO extends HibernateDaoSupport implements BpeDAO { } } + @Override + public List buscarBPeVendaEEventosAutorizados(Integer empresaId, Date dtVendaInicio, Date dtVendaFim, Integer estadoId) { + StringBuilder sQuery = new StringBuilder(); + sQuery.append("select bpe.chbpe, bpe.tipoevento, b.num_bpe numBpe, b.numserie_bpe numserieBpe, b.fechorventa, ") + .append("case when bpe.xmlcontingencia is not null and bpe.indcontingencia = 1 then bpe.xmlcontingencia else bpe.xmlregular end xmlEnvio, ") + .append("bpe.xmlresposta xmlResposta ") + .append("from bpe bpe ") + .append("join boleto b on b.boleto_id = bpe.boleto_id ") + .append("join marca m on m.marca_id = b.marca_id ") + .append("left join estado e on e.codibge = bpe.uf ") + .append("where bpe.activo = 1 ") + .append("and bpe.tipoamb = :tipoamb ") + .append("and bpe.codstat in ('100','101','102','135','150') "); + + if(empresaId != null) { + sQuery.append("and m.empresa_id = :empresaId "); + } + if(dtVendaInicio != null && dtVendaFim != null) { + sQuery.append("and b.fechorventa between to_date(:dtVendaInicio,'dd/mm/yyyy hh24:mi') and to_date(:dtVendaFim,'dd/mm/yyyy hh24:mi') "); + } + if(estadoId != null) { + sQuery.append("and e.estado_id = :estadoId "); + } + + Query qr = getSession().createSQLQuery(sQuery.toString()) + .addScalar("chbpe", StringType.INSTANCE) + .addScalar("tipoevento", StringType.INSTANCE) + .addScalar("chbpe", StringType.INSTANCE) + .addScalar("numBpe", StringType.INSTANCE) + .addScalar("numserieBpe", StringType.INSTANCE) + .addScalar("xmlEnvio", StringType.INSTANCE) + .addScalar("xmlResposta", StringType.INSTANCE) + .addScalar("fechorventa", TimestampType.INSTANCE) + .setResultTransformer(new AliasToBeanResultTransformer(BPeVO.class)); + + qr.setParameter("tipoamb", getAmbiente()); + if(empresaId != null) { + qr.setParameter("empresaId", empresaId); + } + if(dtVendaInicio != null && dtVendaFim != null) { + qr.setParameter("dtVendaInicio", DateUtil.getStringDate(DateUtil.inicioFecha(dtVendaInicio), "dd/MM/yyyy HH:mm")); + qr.setParameter("dtVendaFim", DateUtil.getStringDate(DateUtil.fimFecha(dtVendaFim), "dd/MM/yyyy HH:mm")); + } + if(estadoId != null) { + qr.setParameter("estadoId", estadoId); + } + + return qr.list(); + } + + public String getAmbiente() { + ConstanteService constanteService = (ConstanteService) AppContext.getApplicationContext().getBean("constanteService"); + Constante contante = constanteService.buscarPorNomeConstante("BPE_AMBIENTE"); + String valorConstante = contante == null ? null : contante.getValorconstante(); + if(valorConstante != null) { + return valorConstante; + } + return "1"; + } + } \ No newline at end of file diff --git a/src/com/rjconsultores/ventaboletos/service/BpeService.java b/src/com/rjconsultores/ventaboletos/service/BpeService.java index b0a43f2ee..1d1f0c2d4 100644 --- a/src/com/rjconsultores/ventaboletos/service/BpeService.java +++ b/src/com/rjconsultores/ventaboletos/service/BpeService.java @@ -20,5 +20,9 @@ public interface BpeService { public List buscarBPeRejeitadosContingencia(Integer empresaId, String numBpe, String chbpe, Date dtVendaInicio, Date dtVendaFim, List estados, List codigosRejeicoes); public void definirBPeRejeitadoSefazReenvio(List bpesReenvio) throws BusinessException; + + public List buscarBPeVendaEEventosAutorizados(Integer empresaId, Date dtVendaInicio, Date dtVendaFim, Integer estadoId); + + public byte[] extrairXmlsBPe(List bpes); } diff --git a/src/com/rjconsultores/ventaboletos/service/impl/BpeServiceImpl.java b/src/com/rjconsultores/ventaboletos/service/impl/BpeServiceImpl.java index 656a81b75..be8a63002 100644 --- a/src/com/rjconsultores/ventaboletos/service/impl/BpeServiceImpl.java +++ b/src/com/rjconsultores/ventaboletos/service/impl/BpeServiceImpl.java @@ -1,12 +1,16 @@ package com.rjconsultores.ventaboletos.service.impl; +import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; import javax.sql.DataSource; +import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.datasource.DataSourceUtils; @@ -20,6 +24,7 @@ import com.rjconsultores.ventaboletos.exception.BusinessException; import com.rjconsultores.ventaboletos.service.BpeService; import com.rjconsultores.ventaboletos.utilerias.exportacao.ExportacaoBpe; import com.rjconsultores.ventaboletos.utilerias.exportacao.bpe.ExportacaoBPEVo; +import com.rjconsultores.ventaboletos.utilerias.exportacao.bpe.xml.BPeUtil; import com.rjconsultores.ventaboletos.utilerias.fiscal.vo.FiscalRdi; import com.rjconsultores.ventaboletos.vo.bpe.BPeVO; @@ -118,4 +123,32 @@ public class BpeServiceImpl implements BpeService { } } + @Override + public List buscarBPeVendaEEventosAutorizados(Integer empresaId, Date dtVendaInicio, Date dtVendaFim, Integer estadoId) { + return bpeDAO.buscarBPeVendaEEventosAutorizados(empresaId, dtVendaInicio, dtVendaFim, estadoId); + } + + @Override + public byte[] extrairXmlsBPe(List bpes) { + if(bpes != null && !bpes.isEmpty()) { + Map arquivos = new HashMap(); + for (BPeVO bpe : bpes) { + Map arquivo = null; + if(StringUtils.isNotBlank(bpe.getTipoevento())) { + arquivo = BPeUtil.convertBPeXmlRegularEventoToArquivoXml(bpe); + arquivos.put(arquivo.entrySet().iterator().next().getKey(), arquivo.entrySet().iterator().next().getValue()); + arquivo = BPeUtil.convertBPeXmlRepostaEventoToArquivoXml(bpe); + arquivos.put(arquivo.entrySet().iterator().next().getKey(), arquivo.entrySet().iterator().next().getValue()); + } else { + arquivo = BPeUtil.convertBPeXmlRegularToArquivoXml(bpe); + arquivos.put(arquivo.entrySet().iterator().next().getKey(), arquivo.entrySet().iterator().next().getValue()); + arquivo = BPeUtil.convertBPeXmlRepostaToArquivoXml(bpe); + arquivos.put(arquivo.entrySet().iterator().next().getKey(), arquivo.entrySet().iterator().next().getValue()); + } + } + return BPeUtil.zipFiles(arquivos); + } + return null; + } + } diff --git a/src/com/rjconsultores/ventaboletos/utilerias/exportacao/bpe/xml/BPeUtil.java b/src/com/rjconsultores/ventaboletos/utilerias/exportacao/bpe/xml/BPeUtil.java index e8232700b..6284aa732 100644 --- a/src/com/rjconsultores/ventaboletos/utilerias/exportacao/bpe/xml/BPeUtil.java +++ b/src/com/rjconsultores/ventaboletos/utilerias/exportacao/bpe/xml/BPeUtil.java @@ -1,18 +1,28 @@ package com.rjconsultores.ventaboletos.utilerias.exportacao.bpe.xml; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import javax.xml.transform.stream.StreamSource; +import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; import com.rjconsultores.ventaboletos.constantes.Constantes; +import com.rjconsultores.ventaboletos.utilerias.DateUtil; import com.rjconsultores.ventaboletos.utilerias.exportacao.bpe.ExportacaoBPEVo; +import com.rjconsultores.ventaboletos.vo.bpe.BPeVO; import br.inf.portalfiscal.bpe.TBPe; @@ -37,5 +47,118 @@ public class BPeUtil { } return objeto; } + + public static Map convertBPeXmlRegularToArquivoXml(BPeVO bpe) { + try { + String complemento = "BPeRecepacao_E"; + String nomeArquivo = getNomeArquivo(bpe, complemento); + return gerarArquivo(bpe.getXmlEnvio(), nomeArquivo); + } catch (IOException e) { + log.error(e.getMessage(), e); + } + return null; + } + + public static Map convertBPeXmlRepostaToArquivoXml(BPeVO bpe) { + try { + String complemento = "BPeRecepacao_R"; + String nomeArquivo = getNomeArquivo(bpe, complemento); + return gerarArquivo(bpe.getXmlResposta(), nomeArquivo); + } catch (IOException e) { + log.error(e.getMessage(), e); + } + return null; + } + + public static Map convertBPeXmlRegularEventoToArquivoXml(BPeVO bpe) { + try { + String complemento = "BPeRecepacaoEvento_" + bpe.getTipoevento() + "_E"; + String nomeArquivo = getNomeArquivo(bpe, complemento); + return gerarArquivo(bpe.getXmlEnvio(), nomeArquivo); + } catch (IOException e) { + log.error(e.getMessage(), e); + } + return null; + } + + public static Map convertBPeXmlRepostaEventoToArquivoXml(BPeVO bpe) { + try { + String complemento = "BPeRecepacaoEvento_" + bpe.getTipoevento() + "_R"; + String nomeArquivo = getNomeArquivo(bpe, complemento); + return gerarArquivo(bpe.getXmlResposta(), nomeArquivo); + } catch (IOException e) { + log.error(e.getMessage(), e); + } + return null; + } + + private static Map gerarArquivo(String xml, String nomeArquivo) throws IOException { + InputStream file = IOUtils.toInputStream(xml, Constantes.UTF_8); + Map arquivo = new HashMap(); + arquivo.put(nomeArquivo, file); + return arquivo; + } + + private static String getNomeArquivo(BPeVO bpe, String complemento) { + StringBuilder nomeArquivo = new StringBuilder(); + nomeArquivo.append(DateUtil.getStringDate(bpe.getFechorventa(), "yyyyMMddHHmmss")) + .append("_") + .append(bpe.getChbpe()) + .append("_") + .append(bpe.getNumserieBpe()) + .append("_") + .append(bpe.getNumBpe()) + .append("_") + .append(complemento) + .append(".xml"); + + return nomeArquivo.toString(); + + } + + public static byte[] zipFiles(Map arquivos) { + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ZipOutputStream outputStream = new ZipOutputStream(baos); + InputStream inputStream = null; + + try { + for (Entry arquivo : arquivos.entrySet()) { + outputStream.putNextEntry(new ZipEntry(arquivo.getKey())); + byte[] readBuff = new byte[4096]; + int readLen = -1; + + while ((readLen = arquivo.getValue().read(readBuff)) != -1) { + outputStream.write(readBuff, 0, readLen); + } + outputStream.closeEntry(); + arquivo.getValue().close(); + } + + outputStream.finish(); + + } catch (Exception e) { + log.error(e.getMessage(), e); + + } finally { + if (outputStream != null) { + try { + outputStream.close(); + } catch (IOException e) { + log.error(e.getMessage(), e); + } + } + + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + log.error(e.getMessage(), e); + } + } + } + + return baos.toByteArray(); + } } diff --git a/src/com/rjconsultores/ventaboletos/vo/bpe/BPeVO.java b/src/com/rjconsultores/ventaboletos/vo/bpe/BPeVO.java index 0cfca15c2..bc68a3d53 100644 --- a/src/com/rjconsultores/ventaboletos/vo/bpe/BPeVO.java +++ b/src/com/rjconsultores/ventaboletos/vo/bpe/BPeVO.java @@ -16,6 +16,9 @@ public class BPeVO { private String errocontingencia; private Date fechorventa; private String nombempresa; + private String xmlEnvio; + private String xmlResposta; + private String tipoevento; public Integer getBpeId() { return bpeId; @@ -107,5 +110,29 @@ public class BPeVO { public void setNombempresa(String nombempresa) { this.nombempresa = nombempresa; } - + + public String getXmlResposta() { + return xmlResposta; + } + + public void setXmlResposta(String xmlResposta) { + this.xmlResposta = xmlResposta; + } + + public String getTipoevento() { + return tipoevento; + } + + public void setTipoevento(String tipoevento) { + this.tipoevento = tipoevento; + } + + public String getXmlEnvio() { + return xmlEnvio; + } + + public void setXmlEnvio(String xmlEnvio) { + this.xmlEnvio = xmlEnvio; + } + }