From 9e951fa5ee3f410bf6b4d5ca9bcebd3fa2cb4ad0 Mon Sep 17 00:00:00 2001 From: wilian Date: Wed, 20 Jan 2016 19:05:57 +0000 Subject: [PATCH] fixes bug #6961 git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Model/trunk/modelWeb@52035 d1611594-4594-4d17-8e1d-87c2c4800839 --- .../ventaboletos/dao/FiscalDAO.java | 4 + .../dao/hibernate/FiscalHibernateDAO.java | 51 ++++++++++ .../ventaboletos/service/FiscalService.java | 3 + .../service/impl/FiscalServiceImpl.java | 13 +++ .../ventaboletos/utilerias/StringHelper.java | 2 + .../utilerias/UtileriasFiscal.java | 15 ++- .../ArchivoIntegracionNaoFiscal.java | 94 +++++++++++++++++++ .../ImportacionNaoFiscalVO.java | 51 ++++++++++ 8 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 src/com/rjconsultores/ventaboletos/utilerias/archivointegracion/ArchivoIntegracionNaoFiscal.java create mode 100644 src/com/rjconsultores/ventaboletos/vo/impressaofiscal/ImportacionNaoFiscalVO.java diff --git a/src/com/rjconsultores/ventaboletos/dao/FiscalDAO.java b/src/com/rjconsultores/ventaboletos/dao/FiscalDAO.java index 77593fbb1..0e6818261 100644 --- a/src/com/rjconsultores/ventaboletos/dao/FiscalDAO.java +++ b/src/com/rjconsultores/ventaboletos/dao/FiscalDAO.java @@ -1,11 +1,13 @@ package com.rjconsultores.ventaboletos.dao; +import java.sql.SQLException; import java.util.Date; import java.util.List; import com.rjconsultores.ventaboletos.entidad.EsquemaCorrida; import com.rjconsultores.ventaboletos.vo.impressaofiscal.ImportacionFiscalReducaoZVO; import com.rjconsultores.ventaboletos.vo.impressaofiscal.ImportacionManualFiscalVO; +import com.rjconsultores.ventaboletos.vo.impressaofiscal.ImportacionNaoFiscalVO; public interface FiscalDAO { @@ -17,4 +19,6 @@ public interface FiscalDAO { public List buscaDatosFiscaisECFManual(Date inicio, Date fim, Integer empresaId); + public List buscaDatosNaoFiscais(Date inicio, Date fim, Integer empresaId) throws SQLException; + } diff --git a/src/com/rjconsultores/ventaboletos/dao/hibernate/FiscalHibernateDAO.java b/src/com/rjconsultores/ventaboletos/dao/hibernate/FiscalHibernateDAO.java index 34a21997c..9d301b536 100644 --- a/src/com/rjconsultores/ventaboletos/dao/hibernate/FiscalHibernateDAO.java +++ b/src/com/rjconsultores/ventaboletos/dao/hibernate/FiscalHibernateDAO.java @@ -1,8 +1,15 @@ package com.rjconsultores.ventaboletos.dao.hibernate; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; import java.util.Date; import java.util.List; +import javax.sql.DataSource; + import org.hibernate.Query; import org.hibernate.SessionFactory; import org.hibernate.transform.AliasToBeanResultTransformer; @@ -19,12 +26,16 @@ import com.rjconsultores.ventaboletos.entidad.EsquemaCorrida; import com.rjconsultores.ventaboletos.utilerias.DateUtil; import com.rjconsultores.ventaboletos.vo.impressaofiscal.ImportacionFiscalReducaoZVO; import com.rjconsultores.ventaboletos.vo.impressaofiscal.ImportacionManualFiscalVO; +import com.rjconsultores.ventaboletos.vo.impressaofiscal.ImportacionNaoFiscalVO; import com.rjconsultores.ventaboletos.vo.impressaofiscal.SituacaoTributaria; @Repository("fiscalDAO") public class FiscalHibernateDAO extends GenericHibernateDAO implements FiscalDAO { public static final String DATE_FORMAT_FISCAL = "yyyyMMdd"; + + @Autowired + private DataSource dataSourceRead; @Autowired public FiscalHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) { @@ -434,4 +445,44 @@ public class FiscalHibernateDAO extends GenericHibernateDAO impl int gerarRegistroF2 = this.gerarRegistroF2(lsEsquemaCorrida, dataDe, dataAte); return gerarRegistroP2 + gerarRegistroF2; } + + @Override + public List buscaDatosNaoFiscais(Date inicio, Date fim, Integer empresaId) throws SQLException { + + StringBuilder sQuery = new StringBuilder(); + sQuery.append("SELECT TEE.TIPOEVENTOEXTRA_ID AS TIPOEVENTOEXTRAID, TEE.DESCTIPOEVENTO AS DESCTIPOEVENTO, TRUNC(CD.FECHORVTA) AS FECHORVTA, SUM(CD.PRECIO) AS TOTAL ") + .append("FROM TIPO_EVENTO_EXTRA TEE ") + .append("LEFT JOIN EVENTO_EXTRA EE ON TEE.TIPOEVENTOEXTRA_ID = EE.TIPOEVENTOEXTRA_ID ") + .append("LEFT JOIN CAJA_DIVERSOS CD ON EE.EVENTOEXTRA_ID = CD.EVENTOEXTRA_ID ") + .append("WHERE TEE.ACTIVO = 1 ") + .append("AND EE.EMPRESA_ID = ? ") + .append("AND TRUNC(CD.FECHORVTA) BETWEEN ? AND ? ") + .append("GROUP BY TEE.TIPOEVENTOEXTRA_ID, TEE.DESCTIPOEVENTO, TRUNC(CD.FECHORVTA) ") + .append("ORDER BY TRUNC(CD.FECHORVTA), TEE.DESCTIPOEVENTO "); + + PreparedStatement stmt = getConexao().prepareStatement(sQuery.toString()); + stmt.setInt(1, empresaId); + stmt.setDate(2, new java.sql.Date(inicio.getTime())); + stmt.setDate(3, new java.sql.Date(fim.getTime())); + + ResultSet rset = stmt.executeQuery(); + List importacionNaoFiscalVOs = new ArrayList(); + while (rset.next()) { + ImportacionNaoFiscalVO importacionNaoFiscal = new ImportacionNaoFiscalVO(); + importacionNaoFiscal.setTipoeventoextraId(rset.getInt("TIPOEVENTOEXTRAID")); + importacionNaoFiscal.setDesctipoevento(rset.getString("DESCTIPOEVENTO")); + importacionNaoFiscal.setFechorvta(rset.getDate("FECHORVTA")); + importacionNaoFiscal.setTotal(rset.getBigDecimal("TOTAL")); + + importacionNaoFiscalVOs.add(importacionNaoFiscal); + } + + + return importacionNaoFiscalVOs; + } + + public Connection getConexao() throws SQLException { + return dataSourceRead.getConnection(); + } + } diff --git a/src/com/rjconsultores/ventaboletos/service/FiscalService.java b/src/com/rjconsultores/ventaboletos/service/FiscalService.java index 644006849..fd749ca7d 100644 --- a/src/com/rjconsultores/ventaboletos/service/FiscalService.java +++ b/src/com/rjconsultores/ventaboletos/service/FiscalService.java @@ -7,6 +7,7 @@ import java.util.List; import com.rjconsultores.ventaboletos.entidad.Empresa; import com.rjconsultores.ventaboletos.entidad.EsquemaCorrida; +import com.rjconsultores.ventaboletos.vo.impressaofiscal.ImportacionNaoFiscalVO; public interface FiscalService { @@ -17,5 +18,7 @@ public interface FiscalService { public File importacionFiscalReducaoZ(Date inicio, Date fim, Empresa empresa); public File importacionFiscalECFManual(Date inicio, Date fim, Empresa empresa); + + public File importacionNaoFiscal(Date inicio, Date fim, Empresa empresa); } diff --git a/src/com/rjconsultores/ventaboletos/service/impl/FiscalServiceImpl.java b/src/com/rjconsultores/ventaboletos/service/impl/FiscalServiceImpl.java index 4c90127aa..a365fefa4 100644 --- a/src/com/rjconsultores/ventaboletos/service/impl/FiscalServiceImpl.java +++ b/src/com/rjconsultores/ventaboletos/service/impl/FiscalServiceImpl.java @@ -31,9 +31,11 @@ import com.rjconsultores.ventaboletos.utilerias.DateUtil; import com.rjconsultores.ventaboletos.utilerias.StringHelper; import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado; import com.rjconsultores.ventaboletos.utilerias.UtileriasFiscal; +import com.rjconsultores.ventaboletos.utilerias.archivointegracion.ArchivoIntegracionNaoFiscal; import com.rjconsultores.ventaboletos.vo.impressaofiscal.ImportacionFiscalReducaoZVO; import com.rjconsultores.ventaboletos.vo.impressaofiscal.ImportacionFiscalVO; import com.rjconsultores.ventaboletos.vo.impressaofiscal.ImportacionManualFiscalVO; +import com.rjconsultores.ventaboletos.vo.impressaofiscal.ImportacionNaoFiscalVO; import com.rjconsultores.ventaboletos.vo.impressaofiscal.ItemFiscalVO; import com.rjconsultores.ventaboletos.vo.impressaofiscal.SituacaoTributaria; import com.rjconsultores.ventaboletos.vo.impressaofiscal.SubItens; @@ -949,4 +951,15 @@ public class FiscalServiceImpl implements FiscalService { return fechamento.toString(); } + @Override + public File importacionNaoFiscal(Date inicio, Date fim, Empresa empresa) { + try { + List importacionNaoFiscalVOs = fiscalDAO.buscaDatosNaoFiscais(inicio, fim, empresa.getEmpresaId()); + return new ArchivoIntegracionNaoFiscal().gerarArquivo(empresa.getCnpj(), importacionNaoFiscalVOs); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + return null; + } + } diff --git a/src/com/rjconsultores/ventaboletos/utilerias/StringHelper.java b/src/com/rjconsultores/ventaboletos/utilerias/StringHelper.java index a29146769..320920710 100644 --- a/src/com/rjconsultores/ventaboletos/utilerias/StringHelper.java +++ b/src/com/rjconsultores/ventaboletos/utilerias/StringHelper.java @@ -9,6 +9,8 @@ import java.util.regex.Pattern; import org.apache.commons.lang.StringUtils; public class StringHelper { + + public static final String QUEBRA_LINHA = "\r\n"; private static String[] REPLACES = { "a", "e", "i", "o", "u", "c", "A", "E", "I", "O", "U", "C" }; private static Pattern[] PATTERNS = null; diff --git a/src/com/rjconsultores/ventaboletos/utilerias/UtileriasFiscal.java b/src/com/rjconsultores/ventaboletos/utilerias/UtileriasFiscal.java index 873d656ad..eb1862b0d 100644 --- a/src/com/rjconsultores/ventaboletos/utilerias/UtileriasFiscal.java +++ b/src/com/rjconsultores/ventaboletos/utilerias/UtileriasFiscal.java @@ -1,6 +1,8 @@ package com.rjconsultores.ventaboletos.utilerias; import java.math.BigDecimal; +import java.text.DecimalFormat; +import java.text.NumberFormat; import org.apache.commons.lang.StringUtils; @@ -30,7 +32,7 @@ public class UtileriasFiscal { } public static void main(String args[]) { - System.out.println(defineCodigoProduto(5611, 14199, 4212)); + System.out.println(formataZeroDecimal(new BigDecimal(2d), 2, 14)); } public static String formataNumerico(final String valor, final int tamanho) { @@ -82,4 +84,15 @@ public class UtileriasFiscal { public static BigDecimal arredondar(BigDecimal aNumber) { return aNumber.setScale(DECIMALS, ROUNDING_MODE); } + + public static String formataZeroDecimal(final BigDecimal valor, final int casasDecimais, final int tamanho) { + NumberFormat df = DecimalFormat.getInstance(); + df.setMinimumFractionDigits(casasDecimais); + df.setMaximumFractionDigits(casasDecimais); + + String valorFommat = valor == null ? "" : df.format(valor).replaceAll("[^0-9]", ""); + valorFommat = formataNumerico(valorFommat, tamanho); + return valorFommat; + } + } \ No newline at end of file diff --git a/src/com/rjconsultores/ventaboletos/utilerias/archivointegracion/ArchivoIntegracionNaoFiscal.java b/src/com/rjconsultores/ventaboletos/utilerias/archivointegracion/ArchivoIntegracionNaoFiscal.java new file mode 100644 index 000000000..a828a5da3 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/utilerias/archivointegracion/ArchivoIntegracionNaoFiscal.java @@ -0,0 +1,94 @@ +package com.rjconsultores.ventaboletos.utilerias.archivointegracion; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.util.Calendar; +import java.util.List; + +import org.apache.log4j.Logger; + +import com.rjconsultores.ventaboletos.constantes.Constantes; +import com.rjconsultores.ventaboletos.exception.BusinessException; +import com.rjconsultores.ventaboletos.utilerias.DateUtil; +import com.rjconsultores.ventaboletos.utilerias.StringHelper; +import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado; +import com.rjconsultores.ventaboletos.utilerias.UtileriasFiscal; +import com.rjconsultores.ventaboletos.vo.impressaofiscal.ImportacionNaoFiscalVO; + +public class ArchivoIntegracionNaoFiscal { + + private static Logger log = Logger.getLogger(ArchivoIntegracionNaoFiscal.class); + + private String cnpjFilial; + private Integer sequencial = 0; + + private List importacionNaoFiscalVOs; + + private StringBuilder getAberturaArquivo() { + StringBuilder aberturaArquivo = new StringBuilder(); + aberturaArquivo.append("0") //Identificador Registro = "0" + .append(DateUtil.getStringCurrentDate("yyyyMMdd")) //Data geração arquivo + .append(StringHelper.preencherStringEspacoEsquerda(cnpjFilial.replace("[^0-9]", ""), 14)) //Cnpj filial + .append(StringHelper.preencherStringEspacoEsquerda(null, 371)) //Brancos + .append(StringHelper.preencherZeroEsquerda((++sequencial).toString(), 6)) //Sequencial + .append(StringHelper.QUEBRA_LINHA); + return aberturaArquivo; + } + + private StringBuilder getHeaderArquivo() { + StringBuilder headerArquivo = new StringBuilder(); + for (ImportacionNaoFiscalVO importacionNaoFiscalVO : importacionNaoFiscalVOs) { + headerArquivo.append("1") //Identificador Registro = "1" + .append(DateUtil.getStringDate(importacionNaoFiscalVO.getFechorvta(),"yyyyMMdd")) //Data emissão documento + .append(StringHelper.preencherStringEspacoEsquerda(cnpjFilial.replace("[^0-9]", ""), 14)) //Cnpj filial + .append(StringHelper.preencherStringEspacoEsquerda(importacionNaoFiscalVO.getNumeroDocumento(), 9)) //Numero do Documento + .append(StringHelper.preencherStringEspacoEsquerda(null, 3)) //Série + .append(StringHelper.preencherStringEspacoEsquerda("SVP", 5)) //Prefixo = "SVP" + .append(StringHelper.preencherStringEsquerda(null, 6, "9")) //Codigo Cliente "999999" + .append(StringHelper.preencherStringEsquerda(null, 2, "9")) //Filial cliente="99" + .append(StringHelper.preencherStringEsquerda(null, 3, "9")) //Condição pagamento = "999" + .append(UtileriasFiscal.formataZeroDecimal(importacionNaoFiscalVO.getTotal(), 2, 14)) //Valor Total documento + .append(StringHelper.preencherStringEspacoEsquerda("REC", 3)) //Tipo = "REC" + .append(StringHelper.preencherStringEspacoEsquerda(importacionNaoFiscalVO.getTipoeventoextraId()+"", 10)) //Natureza + .append(StringHelper.preencherStringEspacoEsquerda(null, 316)) //Brancos + .append(StringHelper.preencherZeroEsquerda((++sequencial).toString(), 6)) //Sequencial + .append(StringHelper.QUEBRA_LINHA); + } + return headerArquivo; + } + + private StringBuilder getFechamentoArquivo() { + StringBuilder fechamentoArquivo = new StringBuilder(); + fechamentoArquivo.append("3") //Identificador Registro = "3" + .append(StringHelper.preencherZeroEsquerda((importacionNaoFiscalVOs.size())+"", 6)) //Qtde de Documentos gerados = 1 + .append(StringHelper.preencherZeroEsquerda((importacionNaoFiscalVOs.size())+"", 6)) //Qtde de Itens Documentos gerados = 2 + .append(StringHelper.preencherStringEspacoEsquerda(null, 381)) //Brancos + .append(StringHelper.preencherZeroEsquerda((++sequencial).toString(), 6)); //Sequencial + return fechamentoArquivo; + } + + public File gerarArquivo(String cnpjFilial, List importacionNaoFiscalVOs) throws BusinessException { + try { + String nomeArquivo = UsuarioLogado.getUsuarioLogado().getNombusuario() + "_" + Calendar.getInstance().getTime().getTime() + "_" + "naofiscal"; + File arquivo = File.createTempFile(nomeArquivo, ".tmp"); + PrintWriter gravarArq = new PrintWriter(new OutputStreamWriter(new FileOutputStream(arquivo), Constantes.UTF_8)); + + /* Gerando arquivo */ + this.cnpjFilial = cnpjFilial; + this.importacionNaoFiscalVOs = importacionNaoFiscalVOs; + gravarArq.print(getAberturaArquivo()); + gravarArq.print(getHeaderArquivo()); + gravarArq.print(getFechamentoArquivo()); + + gravarArq.close(); + + return arquivo; + } catch (Exception e) { + log.error(e.getMessage(), e); + throw new BusinessException(e.getMessage(), e); + } + } + +} diff --git a/src/com/rjconsultores/ventaboletos/vo/impressaofiscal/ImportacionNaoFiscalVO.java b/src/com/rjconsultores/ventaboletos/vo/impressaofiscal/ImportacionNaoFiscalVO.java new file mode 100644 index 000000000..2beaa2b7f --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/vo/impressaofiscal/ImportacionNaoFiscalVO.java @@ -0,0 +1,51 @@ +package com.rjconsultores.ventaboletos.vo.impressaofiscal; + +import java.math.BigDecimal; +import java.util.Date; + +import com.rjconsultores.ventaboletos.utilerias.DateUtil; + +public class ImportacionNaoFiscalVO { + + private Integer tipoeventoextraId; + private String desctipoevento; + private BigDecimal total; + private Date fechorvta; + + public Integer getTipoeventoextraId() { + return tipoeventoextraId; + } + + public void setTipoeventoextraId(Integer tipoeventoextraId) { + this.tipoeventoextraId = tipoeventoextraId; + } + + public String getDesctipoevento() { + return desctipoevento; + } + + public void setDesctipoevento(String desctipoevento) { + this.desctipoevento = desctipoevento; + } + + public BigDecimal getTotal() { + return total; + } + + public void setTotal(BigDecimal total) { + this.total = total; + } + + public Date getFechorvta() { + return fechorvta; + } + + public void setFechorvta(Date fechorvta) { + this.fechorvta = fechorvta; + } + + public String getNumeroDocumento() { + return getTipoeventoextraId() + DateUtil.getStringDate(getFechorvta(), "ddMMyy"); + } + +}