890 lines
35 KiB
Java
890 lines
35 KiB
Java
package com.rjconsultores.ventaboletos.service.impl;
|
||
|
||
import java.io.BufferedReader;
|
||
import java.io.ByteArrayOutputStream;
|
||
import java.io.File;
|
||
import java.io.FileOutputStream;
|
||
import java.io.FileReader;
|
||
import java.io.IOException;
|
||
import java.math.BigDecimal;
|
||
import java.sql.Connection;
|
||
import java.sql.SQLException;
|
||
import java.util.ArrayList;
|
||
import java.util.Calendar;
|
||
import java.util.Collections;
|
||
import java.util.Date;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.concurrent.TimeUnit;
|
||
|
||
import javax.sql.DataSource;
|
||
|
||
import org.apache.log4j.Logger;
|
||
import org.apache.poi.hssf.usermodel.HSSFRow;
|
||
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.jdbc.datasource.DataSourceUtils;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
|
||
import com.rjconsultores.ventaboletos.dao.FiscalDAO;
|
||
import com.rjconsultores.ventaboletos.entidad.Aidf;
|
||
import com.rjconsultores.ventaboletos.entidad.Empresa;
|
||
import com.rjconsultores.ventaboletos.entidad.EsquemaCorrida;
|
||
import com.rjconsultores.ventaboletos.entidad.Estado;
|
||
import com.rjconsultores.ventaboletos.entidad.TipoEventoExtra;
|
||
import com.rjconsultores.ventaboletos.service.FiscalService;
|
||
import com.rjconsultores.ventaboletos.service.TipoEventoExtraService;
|
||
import com.rjconsultores.ventaboletos.utilerias.BigDecimalUtil;
|
||
import com.rjconsultores.ventaboletos.utilerias.LocaleUtil;
|
||
import com.rjconsultores.ventaboletos.utilerias.archivointegracion.ArchivoIntegracionECF;
|
||
import com.rjconsultores.ventaboletos.utilerias.archivointegracion.ArchivoIntegracionECFManual;
|
||
import com.rjconsultores.ventaboletos.utilerias.archivointegracion.ArchivoIntegracionECFReducaoZ;
|
||
import com.rjconsultores.ventaboletos.utilerias.archivointegracion.ArchivoIntegracionNaoFiscal;
|
||
import com.rjconsultores.ventaboletos.utilerias.archivointegracion.ArchivoIntegracionReducaoZ;
|
||
import com.rjconsultores.ventaboletos.utilerias.exportacaofiscal.ExportacaoFiscal;
|
||
import com.rjconsultores.ventaboletos.utilerias.exportacaofiscal.vo.ExportacaoECFTipoCCF;
|
||
import com.rjconsultores.ventaboletos.utilerias.exportacaofiscal.vo.ExportacaoECFTipoCFC;
|
||
import com.rjconsultores.ventaboletos.utilerias.exportacaofiscal.vo.ExportacaoECFTipoICF;
|
||
import com.rjconsultores.ventaboletos.utilerias.exportacaofiscal.vo.ExportacaoECFTipoPRC;
|
||
import com.rjconsultores.ventaboletos.utilerias.exportacaofiscal.vo.ExportacaoRMDTipoBPS;
|
||
import com.rjconsultores.ventaboletos.utilerias.exportacaofiscal.vo.ExportacaoRMDTipoDBP;
|
||
import com.rjconsultores.ventaboletos.utilerias.exportacaofiscal.vo.ExportacaoRMDTipoPAR;
|
||
import com.rjconsultores.ventaboletos.utilerias.exportacaofiscal.vo.ExportacaoRMDTipoRMD;
|
||
import com.rjconsultores.ventaboletos.utilerias.impressaormd.vo.ImpressaoRMD;
|
||
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.LeitorFiscalReducaoZVO;
|
||
import com.rjconsultores.ventaboletos.vo.impressaofiscal.LeitorFiscalVO;
|
||
import com.rjconsultores.ventaboletos.vo.impressaofiscal.LeitorManualFiscalVO;
|
||
import com.rjconsultores.ventaboletos.vo.impressaofiscal.LeitorNaoFiscalVO;
|
||
|
||
@Service("fiscalService")
|
||
public class FiscalServiceImpl implements FiscalService {
|
||
private static Logger log = Logger.getLogger(FiscalServiceImpl.class);
|
||
|
||
public static final String DATE_FORMAT_FISCAL = "yyyyMMdd";
|
||
public static final BigDecimal CEM = BigDecimal.valueOf(100);
|
||
public static final String QUEBRA_LINHA = "\r\n";
|
||
|
||
private List<TipoEventoExtra> lsTipoEventoExtra;
|
||
|
||
@Autowired
|
||
private TipoEventoExtraService tipoEventoExtraService;
|
||
|
||
@Autowired
|
||
private FiscalDAO fiscalDAO;
|
||
|
||
@Autowired
|
||
private DataSource dataSourceRead;
|
||
|
||
@Autowired
|
||
private ExportacaoFiscal exportacaoFiscal;
|
||
|
||
@Override
|
||
@Transactional
|
||
public int gerarRegistroP2_F2(List<EsquemaCorrida> lsEsquemaCorrida, Date dataDe, Date dataAte) throws SQLException {
|
||
return fiscalDAO.gerarRegistroP2_F2(lsEsquemaCorrida, dataDe, dataAte);
|
||
}
|
||
|
||
@Override
|
||
public void gravarRMDBoleto(Connection connection, List<ImpressaoRMD> rmds, Aidf aidf) {
|
||
fiscalDAO.gravarRMDBoleto(connection, rmds, aidf);
|
||
}
|
||
|
||
@Override
|
||
public List<ImpressaoRMD> getRegistroImpressaoRMDTipoDVB(Connection connection, Date inicio, Date fim, Empresa empresa, Estado estado, String rmd) {
|
||
return fiscalDAO.getRegistroImpressaoRMDTipoDVB(connection, inicio, fim, empresa.getEmpresaId(), estado.getCveestado(), rmd);
|
||
}
|
||
|
||
@Override
|
||
public List<ImpressaoRMD> getRegistroImpressaoRMDTipoECF(Connection connection, Date inicio, Date fim, Empresa empresa, Estado estado, String rmd) {
|
||
return fiscalDAO.getRegistroImpressaoRMDTipoECF(connection, inicio, fim, empresa.getEmpresaId(), estado.getCveestado(), rmd);
|
||
}
|
||
|
||
@Override
|
||
public File importacionFiscalECFManual(Date inicio, Date fim, Empresa empresa) {
|
||
|
||
Connection connection = null;
|
||
try {
|
||
connection = getConnection();
|
||
|
||
List<ImportacionManualFiscalVO> list = fiscalDAO.buscaDatosFiscaisECFManual(inicio, fim, empresa.getEmpresaId(), connection);
|
||
return new ArchivoIntegracionECFManual().importacionFiscalECFManual(inicio, fim, empresa, list);
|
||
|
||
} catch (Exception e) {
|
||
log.error(e.getMessage(), e);
|
||
} finally {
|
||
try {
|
||
if (connection != null && !connection.isClosed()) {
|
||
connection.close();
|
||
}
|
||
} catch (SQLException e) {
|
||
log.error("error fechar conex<65>o", e);
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
@Override
|
||
public File importacionFiscalReducaoZPendencia(Date inicio, Date fim, Empresa empresa) {
|
||
|
||
Connection connection = null;
|
||
try {
|
||
connection = getConnection();
|
||
|
||
List<ImportacionFiscalReducaoZVO> list = fiscalDAO.buscaDatosFiscaisReducaoZ(inicio, fim, empresa.getEmpresaId(), true, connection);
|
||
return new ArchivoIntegracionReducaoZ().importacionReducaoZ(inicio, fim, empresa, true, list);
|
||
|
||
} catch (Exception e) {
|
||
log.error(e.getMessage(), e);
|
||
} finally {
|
||
try {
|
||
if (connection != null && !connection.isClosed()) {
|
||
connection.close();
|
||
}
|
||
} catch (SQLException e) {
|
||
log.error("error fechar conex<65>o", e);
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
@Override
|
||
public File importacionFiscalReducaoZ(Date inicio, Date fim, Empresa empresa) {
|
||
|
||
Connection connection = null;
|
||
try {
|
||
connection = getConnection();
|
||
|
||
List<ImportacionFiscalReducaoZVO> list = fiscalDAO.buscaDatosFiscaisReducaoZ(inicio, fim, empresa.getEmpresaId(), false, connection);
|
||
return new ArchivoIntegracionReducaoZ().importacionReducaoZ(inicio, fim, empresa, false, list);
|
||
|
||
} catch (Exception e) {
|
||
log.error(e.getMessage(), e);
|
||
} finally {
|
||
try {
|
||
if (connection != null && !connection.isClosed()) {
|
||
connection.close();
|
||
}
|
||
} catch (SQLException e) {
|
||
log.error("error fechar conex<65>o", e);
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
@Override
|
||
public File importacionFiscalECFPendencias(Date inicio, Date fim, Empresa empresa) {
|
||
Connection connection = null;
|
||
try {
|
||
connection = getConnection();
|
||
|
||
List<ImportacionFiscalVO> list = fiscalDAO.buscaDatosFiscaisECF(inicio, fim, empresa.getEmpresaId(), false, false, true, connection);
|
||
return new ArchivoIntegracionECF().importacionFiscalECFGenerico(inicio, fim, empresa, false, false, true, list);
|
||
|
||
} catch (Exception e) {
|
||
log.error(e.getMessage(), e);
|
||
} finally {
|
||
try {
|
||
if (connection != null && !connection.isClosed()) {
|
||
connection.close();
|
||
}
|
||
} catch (SQLException e) {
|
||
log.error("error fechar conex<65>o", e);
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
@Override
|
||
public File importacionFiscalECFCancelados(Date inicio, Date fim, Empresa empresa) {
|
||
Connection connection = null;
|
||
try {
|
||
connection = getConnection();
|
||
|
||
List<ImportacionFiscalVO> list = fiscalDAO.buscaDatosFiscaisECF(inicio, fim, empresa.getEmpresaId(), false, true, false, connection);
|
||
return new ArchivoIntegracionECF().importacionFiscalECFGenerico(inicio, fim, empresa, false, true, false, list);
|
||
|
||
} catch (Exception e) {
|
||
log.error(e.getMessage(), e);
|
||
} finally {
|
||
try {
|
||
if (connection != null && !connection.isClosed()) {
|
||
connection.close();
|
||
}
|
||
} catch (SQLException e) {
|
||
log.error("error fechar conex<65>o", e);
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
@Override
|
||
public HashMap<String, Object> importacionFiscalECFValidaReducaoZ(Date inicio, Date fim, Empresa empresa) {
|
||
|
||
Connection connection = null;
|
||
try {
|
||
connection = getConnection();
|
||
|
||
List<ImportacionFiscalVO> listEcf = fiscalDAO.buscaDatosFiscaisECF(inicio, fim, empresa.getEmpresaId(), true, false, false, connection);
|
||
|
||
List<ImportacionFiscalReducaoZVO> listRedZ = fiscalDAO.buscaDatosFiscaisReducaoZ(inicio, fim, empresa.getEmpresaId(), false, connection);
|
||
|
||
ArchivoIntegracionECFReducaoZ ecfRedZ = new ArchivoIntegracionECFReducaoZ();
|
||
ecfRedZ.importacionFiscalECFValidaReducaoZ(listEcf, listRedZ);
|
||
|
||
List<File> files = new ArrayList<File>();
|
||
files.add(new ArchivoIntegracionECF().importacionFiscalECFGenerico(inicio, fim, empresa, true, false, false, ecfRedZ.getListEcfValidos()));
|
||
files.add(new ArchivoIntegracionReducaoZ().importacionReducaoZ(inicio, fim, empresa, false, ecfRedZ.getListRedZValidos()));
|
||
|
||
files.add(new ArchivoIntegracionECF().importacionFiscalECFGenerico(inicio, fim, empresa, false, false, true, ecfRedZ.getListEcfInvalidos()));
|
||
files.add(new ArchivoIntegracionReducaoZ().importacionReducaoZ(inicio, fim, empresa, true, ecfRedZ.getListRedZInvalidos()));
|
||
|
||
HashMap<String, Object> result = new HashMap<String, Object>();
|
||
result.put("arquivos", files);
|
||
result.put("ecfInvalidos", ecfRedZ.getListEcfInvalidos());
|
||
result.put("redZInvalido", ecfRedZ.getListRedZInvalidos());
|
||
|
||
return result;
|
||
|
||
} catch (Exception e) {
|
||
log.error(e.getMessage(), e);
|
||
} finally {
|
||
try {
|
||
if (connection != null && !connection.isClosed()) {
|
||
connection.close();
|
||
}
|
||
} catch (SQLException e) {
|
||
log.error("error fechar conex<65>o", e);
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
@Override
|
||
public File importacionFiscalECF(Date inicio, Date fim, Empresa empresa) {
|
||
Connection connection = null;
|
||
try {
|
||
connection = getConnection();
|
||
|
||
List<ImportacionFiscalVO> list = fiscalDAO.buscaDatosFiscaisECF(inicio, fim, empresa.getEmpresaId(), true, false, false, connection);
|
||
return new ArchivoIntegracionECF().importacionFiscalECFGenerico(inicio, fim, empresa, true, false, false, list);
|
||
|
||
} catch (Exception e) {
|
||
log.error(e.getMessage(), e);
|
||
} finally {
|
||
try {
|
||
if (connection != null && !connection.isClosed()) {
|
||
connection.close();
|
||
}
|
||
} catch (SQLException e) {
|
||
log.error("error fechar conex<65>o", e);
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
@Override
|
||
public File importacionNaoFiscal(Date inicio, Date fim, Empresa empresa) {
|
||
Connection connection = null;
|
||
try {
|
||
connection = getConnection();
|
||
|
||
List<ImportacionNaoFiscalVO> importacionNaoFiscalVOs = fiscalDAO.buscaDatosNaoFiscais(inicio, fim, empresa.getEmpresaId(), connection);
|
||
|
||
return new ArchivoIntegracionNaoFiscal().gerarArquivo(empresa.getCnpj(), importacionNaoFiscalVOs);
|
||
|
||
} catch (Exception e) {
|
||
log.error(e.getMessage(), e);
|
||
} finally {
|
||
try {
|
||
if (connection != null && !connection.isClosed()) {
|
||
connection.close();
|
||
}
|
||
} catch (SQLException e) {
|
||
log.error("error fechar conex<65>o", e);
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
private Connection getConnection() {
|
||
return DataSourceUtils.getConnection(dataSourceRead);
|
||
}
|
||
|
||
@Override
|
||
public File importacionFiscalECFXls(Empresa empresa, File arquivoTxt) {
|
||
return gerarArquivoEcfXlsGenerico(empresa, arquivoTxt);
|
||
}
|
||
|
||
@Override
|
||
public File importacionFiscalReducaoZXls(Empresa empresa, File arquivoTxt) {
|
||
try {
|
||
List<LeitorFiscalReducaoZVO> registros = carregarRegistrosReducaoZ(empresa, arquivoTxt);
|
||
return gerarArquivoReducaoZXls(registros, arquivoTxt.getName());
|
||
} catch (Exception e) {
|
||
log.error(e.getMessage(), e);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
private List<LeitorFiscalReducaoZVO> carregarRegistrosReducaoZ(Empresa empresa, File arquivoTxt) throws Exception {
|
||
List<LeitorFiscalReducaoZVO> registros = new ArrayList<LeitorFiscalReducaoZVO>();
|
||
BufferedReader br = new BufferedReader(new FileReader(arquivoTxt));
|
||
String linha = br.readLine();
|
||
while (linha != null) {
|
||
LeitorFiscalReducaoZVO leitorFiscalReducaoZ = null;
|
||
int indice = -1;
|
||
if (linha.startsWith("1")) {
|
||
leitorFiscalReducaoZ = new LeitorFiscalReducaoZVO();
|
||
leitorFiscalReducaoZ.setEmpresaId(empresa.getEmpresaId());
|
||
leitorFiscalReducaoZ.setNombempresa(empresa.getNombempresa());
|
||
leitorFiscalReducaoZ.setDatamov(linha.substring(1, 9).trim());
|
||
leitorFiscalReducaoZ.setCnpj(linha.substring(9, 23).trim());
|
||
leitorFiscalReducaoZ.setAliquota(BigDecimalUtil.getStringToBigDecimal(linha.substring(196, 210).trim(), 2, LocaleUtil.getLocale()));
|
||
|
||
if (registros.contains(leitorFiscalReducaoZ)) {
|
||
indice = registros.indexOf(leitorFiscalReducaoZ);
|
||
leitorFiscalReducaoZ = registros.get(indice);
|
||
}
|
||
|
||
leitorFiscalReducaoZ.setImposto(leitorFiscalReducaoZ.getImposto().add(BigDecimalUtil.getStringToBigDecimal(linha.substring(272, 285).trim(), 2, LocaleUtil.getLocale())));
|
||
leitorFiscalReducaoZ.setValorNaoTributado(leitorFiscalReducaoZ.getValorNaoTributado().add(BigDecimalUtil.getStringToBigDecimal(linha.substring(182, 196).trim(), 2, LocaleUtil.getLocale())));
|
||
leitorFiscalReducaoZ.setVendabrutadiaria(leitorFiscalReducaoZ.getVendabrutadiaria().add(BigDecimalUtil.getStringToBigDecimal(linha.substring(126, 140).trim(), 2, LocaleUtil.getLocale())));
|
||
|
||
linha = br.readLine();
|
||
} else {
|
||
linha = br.readLine();
|
||
}
|
||
|
||
if (leitorFiscalReducaoZ != null && indice > -1) {
|
||
registros.set(indice, leitorFiscalReducaoZ);
|
||
} else if (leitorFiscalReducaoZ != null) {
|
||
registros.add(leitorFiscalReducaoZ);
|
||
}
|
||
}
|
||
|
||
br.close();
|
||
|
||
Collections.sort(registros);
|
||
|
||
return registros;
|
||
}
|
||
|
||
private File gerarArquivoReducaoZXls(List<LeitorFiscalReducaoZVO> registros, String nomeArquivo) throws IOException {
|
||
HSSFWorkbook workbook = new HSSFWorkbook();
|
||
HSSFSheet firstSheet = workbook.createSheet();
|
||
|
||
int linha = 0;
|
||
|
||
// Cabecalho
|
||
HSSFRow row = firstSheet.createRow(linha++);
|
||
row.createCell(0).setCellValue("DATA_MOVIMENTO");
|
||
row.createCell(1).setCellValue("EMPRESA");
|
||
row.createCell(2).setCellValue("CNPJ");
|
||
row.createCell(3).setCellValue("VENDA_BRUTA_DIARIA");
|
||
row.createCell(4).setCellValue("VALOR_NAO_TRIBUTADO");
|
||
row.createCell(5).setCellValue("ALIQUOTA");
|
||
row.createCell(6).setCellValue("VALOR_ICMS");
|
||
|
||
for (LeitorFiscalReducaoZVO leitorFiscal : registros) {
|
||
row = firstSheet.createRow(linha++);
|
||
row.createCell(0).setCellValue(leitorFiscal.getDatamov());
|
||
row.createCell(1).setCellValue(leitorFiscal.getNombempresa());
|
||
row.createCell(2).setCellValue(leitorFiscal.getCnpj());
|
||
row.createCell(3).setCellValue(leitorFiscal.getVendabrutadiaria().doubleValue());
|
||
row.createCell(4).setCellValue(leitorFiscal.getValorNaoTributado().doubleValue());
|
||
row.createCell(5).setCellValue(leitorFiscal.getAliquota().doubleValue());
|
||
row.createCell(6).setCellValue(leitorFiscal.getImposto().doubleValue());
|
||
}
|
||
|
||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||
workbook.write(bos);
|
||
|
||
nomeArquivo = "consolidado_" + nomeArquivo.split("\\.")[0];
|
||
File arquivo = File.createTempFile(nomeArquivo, ".xls");
|
||
|
||
FileOutputStream fos = new FileOutputStream(arquivo);
|
||
fos.write(bos.toByteArray());
|
||
fos.close();
|
||
|
||
return arquivo;
|
||
}
|
||
|
||
@Override
|
||
public File importacionFiscalECFManualXls(Empresa empresa, File arquivoTxt) {
|
||
try {
|
||
List<LeitorManualFiscalVO> registros = carregarRegistrosManualFiscal(empresa, arquivoTxt);
|
||
return gerarArquivoEcfManualXls(registros, arquivoTxt.getName());
|
||
} catch (Exception e) {
|
||
log.error(e.getMessage(), e);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
private File gerarArquivoEcfManualXls(List<LeitorManualFiscalVO> registros, String nomeArquivo) throws Exception {
|
||
HSSFWorkbook workbook = new HSSFWorkbook();
|
||
HSSFSheet firstSheet = workbook.createSheet();
|
||
|
||
int linha = 0;
|
||
|
||
// Cabecalho
|
||
HSSFRow row = firstSheet.createRow(linha++);
|
||
row.createCell(0).setCellValue("DATA_EMISSAO");
|
||
row.createCell(1).setCellValue("EMPRESA");
|
||
row.createCell(2).setCellValue("UF_ORIGEM");
|
||
row.createCell(3).setCellValue("UF_DESTINO");
|
||
row.createCell(4).setCellValue("CLASSE");
|
||
row.createCell(5).setCellValue("STATUS");
|
||
row.createCell(6).setCellValue("TARIFA");
|
||
row.createCell(7).setCellValue("TX_EMBARQUE");
|
||
row.createCell(8).setCellValue("PEDAGIO");
|
||
row.createCell(9).setCellValue("SEGURO");
|
||
row.createCell(10).setCellValue("TOTAL");
|
||
row.createCell(11).setCellValue("ALIQUOTA");
|
||
row.createCell(12).setCellValue("VALOR_ICMS");
|
||
row.createCell(13).setCellValue("QTDE");
|
||
|
||
for (LeitorManualFiscalVO leitorFiscal : registros) {
|
||
row = firstSheet.createRow(linha++);
|
||
row.createCell(0).setCellValue(leitorFiscal.getDataEmissao());
|
||
row.createCell(1).setCellValue(leitorFiscal.getNombempresa());
|
||
row.createCell(2).setCellValue(leitorFiscal.getOrigenUf());
|
||
row.createCell(3).setCellValue(leitorFiscal.getDestinoUf());
|
||
row.createCell(4).setCellValue(leitorFiscal.getClaseServicio());
|
||
row.createCell(5).setCellValue(leitorFiscal.getStatus());
|
||
row.createCell(6).setCellValue(leitorFiscal.getValorItem().doubleValue());
|
||
row.createCell(7).setCellValue(leitorFiscal.getImportetaxaembarque().doubleValue());
|
||
row.createCell(8).setCellValue(leitorFiscal.getImportepedagio().doubleValue());
|
||
row.createCell(9).setCellValue(leitorFiscal.getImporteseguro().doubleValue());
|
||
row.createCell(10).setCellValue(leitorFiscal.getValorTotal().doubleValue());
|
||
row.createCell(11).setCellValue(leitorFiscal.getIcms().doubleValue());
|
||
row.createCell(12).setCellValue(leitorFiscal.getValorIcms().doubleValue());
|
||
row.createCell(13).setCellValue(leitorFiscal.getQtdeItens());
|
||
}
|
||
|
||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||
workbook.write(bos);
|
||
|
||
nomeArquivo = "consolidado_" + nomeArquivo.split("\\.")[0];
|
||
File arquivo = File.createTempFile(nomeArquivo, ".xls");
|
||
|
||
FileOutputStream fos = new FileOutputStream(arquivo);
|
||
fos.write(bos.toByteArray());
|
||
fos.close();
|
||
|
||
return arquivo;
|
||
}
|
||
|
||
@Override
|
||
public File importacionNaoFiscalXls(Empresa empresa, File arquivoTxt) {
|
||
return gerarArquivoNaoFiscalXls(empresa, arquivoTxt);
|
||
}
|
||
|
||
@Override
|
||
public File importacionFiscalECFCanceladosXls(Empresa empresa, File arquivoTxt) {
|
||
return gerarArquivoEcfXlsGenerico(empresa, arquivoTxt);
|
||
}
|
||
|
||
private File gerarArquivoNaoFiscalXls(Empresa empresa, File arquivoTxt) {
|
||
try {
|
||
List<LeitorNaoFiscalVO> registros = carregarRegistrosNaoFiscais(empresa, arquivoTxt);
|
||
return gerarArquivoNaoFiscaisXls(registros, arquivoTxt.getName());
|
||
} catch (Exception e) {
|
||
log.error(e.getMessage(), e);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
private File gerarArquivoEcfXlsGenerico(Empresa empresa, File arquivoTxt) {
|
||
try {
|
||
List<LeitorFiscalVO> registros = carregarRegistrosEcf(empresa, arquivoTxt);
|
||
return gerarArquivoEcfXls(registros, arquivoTxt.getName());
|
||
} catch (Exception e) {
|
||
log.error(e.getMessage(), e);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
private File gerarArquivoEcfXls(List<LeitorFiscalVO> registros, String nomeArquivo) throws IOException {
|
||
HSSFWorkbook workbook = new HSSFWorkbook();
|
||
HSSFSheet firstSheet = workbook.createSheet();
|
||
|
||
int linha = 0;
|
||
|
||
// Cabecalho
|
||
HSSFRow row = firstSheet.createRow(linha++);
|
||
row.createCell(0).setCellValue("DATA_EMISSAO");
|
||
row.createCell(1).setCellValue("EMPRESA");
|
||
row.createCell(2).setCellValue("UF_ORIGEM");
|
||
row.createCell(3).setCellValue("UF_DESTINO");
|
||
row.createCell(4).setCellValue("CLASSE");
|
||
row.createCell(5).setCellValue("MOTIVO_CANCELAMENTO");
|
||
row.createCell(6).setCellValue("TARIFA");
|
||
row.createCell(7).setCellValue("TX_EMBARQUE");
|
||
row.createCell(8).setCellValue("PEDAGIO");
|
||
row.createCell(9).setCellValue("SEGURO");
|
||
row.createCell(10).setCellValue("MULTA");
|
||
row.createCell(11).setCellValue("TOTAL");
|
||
row.createCell(12).setCellValue("ALIQUOTA_ICMS");
|
||
row.createCell(13).setCellValue("VALOR_ICMS");
|
||
row.createCell(14).setCellValue("QTDE");
|
||
|
||
for (LeitorFiscalVO leitorFiscal : registros) {
|
||
row = firstSheet.createRow(linha++);
|
||
row.createCell(0).setCellValue(leitorFiscal.getDataEmissao());
|
||
row.createCell(1).setCellValue(leitorFiscal.getNombempresa());
|
||
row.createCell(2).setCellValue(leitorFiscal.getOrigenUf());
|
||
row.createCell(3).setCellValue(leitorFiscal.getDestinoUf());
|
||
row.createCell(4).setCellValue(leitorFiscal.getClaseServicio());
|
||
row.createCell(5).setCellValue(leitorFiscal.getDescmotivocancelamento());
|
||
row.createCell(6).setCellValue(leitorFiscal.getValorTarifa().doubleValue());
|
||
row.createCell(7).setCellValue(leitorFiscal.getValorEmbarque().doubleValue());
|
||
row.createCell(8).setCellValue(leitorFiscal.getValorPedagio().doubleValue());
|
||
row.createCell(9).setCellValue(leitorFiscal.getValorSeguro().doubleValue());
|
||
row.createCell(10).setCellValue(leitorFiscal.getValorMulta().doubleValue());
|
||
row.createCell(11).setCellValue(leitorFiscal.getTotal().doubleValue());
|
||
row.createCell(12).setCellValue(leitorFiscal.getAliquota().doubleValue());
|
||
row.createCell(13).setCellValue(leitorFiscal.getValorIcms().doubleValue());
|
||
row.createCell(14).setCellValue(leitorFiscal.getQtdeItens());
|
||
}
|
||
|
||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||
workbook.write(bos);
|
||
|
||
nomeArquivo = "consolidado_" + nomeArquivo.split("\\.")[0];
|
||
File arquivo = File.createTempFile(nomeArquivo, ".xls");
|
||
|
||
FileOutputStream fos = new FileOutputStream(arquivo);
|
||
fos.write(bos.toByteArray());
|
||
fos.close();
|
||
|
||
return arquivo;
|
||
|
||
}
|
||
|
||
private List<LeitorFiscalVO> carregarRegistrosEcf(Empresa empresa, File arquivoTxt) throws Exception {
|
||
List<LeitorFiscalVO> registros = new ArrayList<LeitorFiscalVO>();
|
||
BufferedReader br = new BufferedReader(new FileReader(arquivoTxt));
|
||
String linha = br.readLine();
|
||
while (linha != null) {
|
||
LeitorFiscalVO leitorFiscal = null;
|
||
int indice = -1;
|
||
if (linha.startsWith("1")) {
|
||
leitorFiscal = new LeitorFiscalVO();
|
||
leitorFiscal.setEmpresaId(empresa.getEmpresaId());
|
||
leitorFiscal.setNombempresa(empresa.getNombempresa());
|
||
leitorFiscal.setDataEmissao(linha.substring(1, 9).trim());
|
||
leitorFiscal.setOrigenUf(linha.substring(177, 179).trim());
|
||
leitorFiscal.setDestinoUf(linha.substring(184, 186).trim());
|
||
leitorFiscal.setClaseServicio(linha.substring(226, 256).trim());
|
||
leitorFiscal.setDescmotivocancelamento(linha.substring(192, 212).trim());
|
||
leitorFiscal.setAliquota(BigDecimalUtil.getStringToBigDecimal(linha.substring(79, 93).trim(), 2, LocaleUtil.getLocale()));
|
||
|
||
if (registros.contains(leitorFiscal)) {
|
||
indice = registros.indexOf(leitorFiscal);
|
||
leitorFiscal = registros.get(indice);
|
||
}
|
||
|
||
leitorFiscal.setQtdeItens(leitorFiscal.getQtdeItens() + 1);
|
||
linha = br.readLine();
|
||
while (linha != null && linha.startsWith("2")) {
|
||
if (linha.indexOf("TA") >= 0) {
|
||
leitorFiscal.setValorTarifa(leitorFiscal.getValorTarifa().add(BigDecimalUtil.getStringToBigDecimal(linha.substring(47, 62).trim(), 2, LocaleUtil.getLocale())));
|
||
}
|
||
|
||
if (linha.indexOf("TX") >= 0) {
|
||
leitorFiscal.setValorEmbarque(leitorFiscal.getValorEmbarque().add(BigDecimalUtil.getStringToBigDecimal(linha.substring(47, 62).trim(), 2, LocaleUtil.getLocale())));
|
||
}
|
||
|
||
if (linha.indexOf("PE") >= 0) {
|
||
leitorFiscal.setValorPedagio(leitorFiscal.getValorPedagio().add(BigDecimalUtil.getStringToBigDecimal(linha.substring(47, 62).trim(), 2, LocaleUtil.getLocale())));
|
||
}
|
||
|
||
if (linha.indexOf("SE") >= 0) {
|
||
leitorFiscal.setValorSeguro(leitorFiscal.getValorSeguro().add(BigDecimalUtil.getStringToBigDecimal(linha.substring(47, 62).trim(), 2, LocaleUtil.getLocale())));
|
||
}
|
||
|
||
linha = br.readLine();
|
||
}
|
||
} else {
|
||
linha = br.readLine();
|
||
}
|
||
|
||
if (leitorFiscal != null && indice > -1) {
|
||
registros.set(indice, leitorFiscal);
|
||
} else if (leitorFiscal != null) {
|
||
registros.add(leitorFiscal);
|
||
}
|
||
}
|
||
|
||
br.close();
|
||
|
||
Collections.sort(registros);
|
||
|
||
return registros;
|
||
}
|
||
|
||
private List<LeitorManualFiscalVO> carregarRegistrosManualFiscal(Empresa empresa, File arquivoTxt) throws Exception {
|
||
List<LeitorManualFiscalVO> registros = new ArrayList<LeitorManualFiscalVO>();
|
||
BufferedReader br = new BufferedReader(new FileReader(arquivoTxt));
|
||
String linha = br.readLine();
|
||
while (linha != null) {
|
||
LeitorManualFiscalVO leitorManualFiscal = null;
|
||
int indice = -1;
|
||
if (linha.startsWith("1")) {
|
||
leitorManualFiscal = new LeitorManualFiscalVO();
|
||
leitorManualFiscal.setEmpresaId(empresa.getEmpresaId());
|
||
leitorManualFiscal.setNombempresa(empresa.getNombempresa());
|
||
leitorManualFiscal.setDataEmissao(linha.substring(1, 9).trim());
|
||
leitorManualFiscal.setOrigenUf(linha.substring(177, 179).trim());
|
||
leitorManualFiscal.setDestinoUf(linha.substring(184, 186).trim());
|
||
leitorManualFiscal.setClaseServicio(linha.substring(226, 256).trim());
|
||
leitorManualFiscal.setStatus(linha.substring(223, 224).trim());
|
||
leitorManualFiscal.setIcms(BigDecimalUtil.getStringToBigDecimal(linha.substring(79, 93).trim(), 2, LocaleUtil.getLocale()));
|
||
|
||
if (registros.contains(leitorManualFiscal)) {
|
||
indice = registros.indexOf(leitorManualFiscal);
|
||
leitorManualFiscal = registros.get(indice);
|
||
}
|
||
|
||
leitorManualFiscal.setValorIcms(leitorManualFiscal.getValorIcms().add(BigDecimalUtil.getStringToBigDecimal(linha.substring(93, 107).trim(), 2, LocaleUtil.getLocale())));
|
||
leitorManualFiscal.setValorTotal(leitorManualFiscal.getValorTotal().add(BigDecimalUtil.getStringToBigDecimal(linha.substring(51, 65).trim(), 2, LocaleUtil.getLocale())));
|
||
|
||
leitorManualFiscal.setQtdeItens(leitorManualFiscal.getQtdeItens() + 1);
|
||
linha = br.readLine();
|
||
while (linha != null && linha.startsWith("2")) {
|
||
if (linha.indexOf("TA") >= 0) {
|
||
leitorManualFiscal.setValorItem(leitorManualFiscal.getValorItem().add(BigDecimalUtil.getStringToBigDecimal(linha.substring(47, 62).trim(), 2, LocaleUtil.getLocale())));
|
||
}
|
||
|
||
if (linha.indexOf("TX") >= 0) {
|
||
leitorManualFiscal.setImportetaxaembarque(leitorManualFiscal.getImportetaxaembarque().add(BigDecimalUtil.getStringToBigDecimal(linha.substring(47, 62).trim(), 2, LocaleUtil.getLocale())));
|
||
}
|
||
|
||
if (linha.indexOf("PE") >= 0) {
|
||
leitorManualFiscal.setImportepedagio(leitorManualFiscal.getImportepedagio().add(BigDecimalUtil.getStringToBigDecimal(linha.substring(47, 62).trim(), 2, LocaleUtil.getLocale())));
|
||
}
|
||
|
||
if (linha.indexOf("SE") >= 0) {
|
||
leitorManualFiscal.setImporteseguro(leitorManualFiscal.getImporteseguro().add(BigDecimalUtil.getStringToBigDecimal(linha.substring(47, 62).trim(), 2, LocaleUtil.getLocale())));
|
||
}
|
||
|
||
linha = br.readLine();
|
||
}
|
||
} else {
|
||
linha = br.readLine();
|
||
}
|
||
|
||
if (leitorManualFiscal != null && indice > -1) {
|
||
registros.set(indice, leitorManualFiscal);
|
||
} else if (leitorManualFiscal != null) {
|
||
registros.add(leitorManualFiscal);
|
||
}
|
||
}
|
||
|
||
br.close();
|
||
|
||
Collections.sort(registros);
|
||
|
||
return registros;
|
||
}
|
||
|
||
private List<LeitorNaoFiscalVO> carregarRegistrosNaoFiscais(Empresa empresa, File arquivoTxt) throws Exception {
|
||
List<LeitorNaoFiscalVO> registros = new ArrayList<LeitorNaoFiscalVO>();
|
||
BufferedReader br = new BufferedReader(new FileReader(arquivoTxt));
|
||
String linha = br.readLine();
|
||
while (linha != null) {
|
||
LeitorNaoFiscalVO naoFiscais = null;
|
||
int indice = -1;
|
||
if (linha.startsWith("1")) {
|
||
naoFiscais = new LeitorNaoFiscalVO();
|
||
naoFiscais.setEmpresaId(empresa.getEmpresaId());
|
||
naoFiscais.setNombempresa(empresa.getNombempresa());
|
||
naoFiscais.setDatamov(linha.substring(1, 9).trim());
|
||
|
||
Integer tipoEventoExtraId = Integer.valueOf(linha.substring(69, 78).trim());
|
||
TipoEventoExtra tipoEvento = getTipoEventoExtra(tipoEventoExtraId);
|
||
|
||
naoFiscais.setTipoeventoextraId(tipoEvento.getTipoeventoextraId());
|
||
naoFiscais.setDesctipoevento(tipoEvento.getDescTipoEvento());
|
||
|
||
if (registros.contains(naoFiscais)) {
|
||
indice = registros.indexOf(naoFiscais);
|
||
naoFiscais = registros.get(indice);
|
||
}
|
||
|
||
naoFiscais.setValorTotal(naoFiscais.getValorTotal().add(BigDecimalUtil.getStringToBigDecimal(linha.substring(52, 65).trim(), 2, LocaleUtil.getLocale())));
|
||
|
||
linha = br.readLine();
|
||
} else {
|
||
linha = br.readLine();
|
||
}
|
||
|
||
if (naoFiscais != null && indice > -1) {
|
||
registros.set(indice, naoFiscais);
|
||
} else if (naoFiscais != null) {
|
||
registros.add(naoFiscais);
|
||
}
|
||
}
|
||
|
||
br.close();
|
||
|
||
Collections.sort(registros);
|
||
|
||
return registros;
|
||
}
|
||
|
||
private File gerarArquivoNaoFiscaisXls(List<LeitorNaoFiscalVO> registros, String nomeArquivo) throws IOException {
|
||
HSSFWorkbook workbook = new HSSFWorkbook();
|
||
HSSFSheet firstSheet = workbook.createSheet();
|
||
|
||
int linha = 0;
|
||
|
||
// Cabecalho
|
||
HSSFRow row = firstSheet.createRow(linha++);
|
||
row.createCell(0).setCellValue("DATA_EMISSAO");
|
||
row.createCell(1).setCellValue("EMPRESA");
|
||
row.createCell(2).setCellValue("CODIGO_NATUREZA");
|
||
row.createCell(3).setCellValue("DESCRICAO_NATUREZA");
|
||
row.createCell(4).setCellValue("VALOR_TOTAL");
|
||
|
||
for (LeitorNaoFiscalVO leitorNaoFiscal : registros) {
|
||
row = firstSheet.createRow(linha++);
|
||
row.createCell(0).setCellValue(leitorNaoFiscal.getDatamov());
|
||
row.createCell(1).setCellValue(leitorNaoFiscal.getNombempresa());
|
||
row.createCell(2).setCellValue(leitorNaoFiscal.getTipoeventoextraId());
|
||
row.createCell(3).setCellValue(leitorNaoFiscal.getDesctipoevento());
|
||
row.createCell(4).setCellValue(leitorNaoFiscal.getValorTotal().doubleValue());
|
||
}
|
||
|
||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||
workbook.write(bos);
|
||
|
||
nomeArquivo = "consolidado_" + nomeArquivo.split("\\.")[0];
|
||
File arquivo = File.createTempFile(nomeArquivo, ".xls");
|
||
|
||
FileOutputStream fos = new FileOutputStream(arquivo);
|
||
fos.write(bos.toByteArray());
|
||
fos.close();
|
||
|
||
return arquivo;
|
||
|
||
}
|
||
|
||
private TipoEventoExtra getTipoEventoExtra(Integer tipoEventoExtraId) {
|
||
for (TipoEventoExtra tipoEvento : getListTipoEventoExtra()) {
|
||
if (tipoEventoExtraId.equals(tipoEvento.getTipoeventoextraId()))
|
||
return tipoEvento;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
private List<TipoEventoExtra> getListTipoEventoExtra() {
|
||
if (lsTipoEventoExtra == null) {
|
||
lsTipoEventoExtra = tipoEventoExtraService.obtenerTodos();
|
||
}
|
||
return lsTipoEventoExtra;
|
||
}
|
||
|
||
@Override
|
||
public File buscarArquivoExportacaoECF(Date inicio, Date fim, Empresa empresa, Estado estado, String nomeArquivo) {
|
||
Connection connection = null;
|
||
|
||
try {
|
||
connection = getConnection();
|
||
|
||
List<String> tempos = new ArrayList<String>(0);
|
||
Long begin = 0L;
|
||
|
||
begin = Calendar.getInstance().getTimeInMillis();
|
||
List<ExportacaoECFTipoCFC> listaRegistroECFTipoCFC = fiscalDAO.buscarRegistroECFTipoCFC(connection, inicio, fim, empresa.getEmpresaId(), estado.getCveestado());
|
||
tempos.add(infoTempo("CFC", begin));
|
||
|
||
begin = Calendar.getInstance().getTimeInMillis();
|
||
List<ExportacaoECFTipoCCF> listaRegistroECFTipoCCF = fiscalDAO.buscarRegistroECFTipoCCF(connection, inicio, fim, empresa.getEmpresaId(), estado.getCveestado());
|
||
tempos.add(infoTempo("CCF", begin));
|
||
|
||
begin = Calendar.getInstance().getTimeInMillis();
|
||
List<ExportacaoECFTipoICF> listaRegistroECFTipoICF = fiscalDAO.buscarRegistroECFTipoICF(connection, inicio, fim, empresa.getEmpresaId(), estado.getCveestado());
|
||
tempos.add(infoTempo("ICF", begin));
|
||
|
||
begin = Calendar.getInstance().getTimeInMillis();
|
||
List<ExportacaoECFTipoPRC> listaRegistroECFTipoPRC = fiscalDAO.buscarRegistroECFTipoPRC(connection, inicio, fim, empresa.getEmpresaId(), estado.getCveestado());
|
||
tempos.add(infoTempo("PRC", begin));
|
||
|
||
return exportacaoFiscal.gerarArquivoECF(tempos, nomeArquivo, inicio, fim, estado.getCveestado(), empresa.getNombempresa(), listaRegistroECFTipoCFC, listaRegistroECFTipoCCF, listaRegistroECFTipoICF,
|
||
listaRegistroECFTipoPRC);
|
||
|
||
} catch (Exception e) {
|
||
log.error(e.getMessage(), e);
|
||
|
||
} finally {
|
||
try {
|
||
if (connection != null && !connection.isClosed()) {
|
||
connection.close();
|
||
}
|
||
} catch (SQLException e) {
|
||
log.error("error fechar conex<65>o", e);
|
||
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
private String infoTempo(final String descricao, final Long inicio) {
|
||
final Long termino = Calendar.getInstance().getTimeInMillis();
|
||
final Long tempo = termino - inicio;
|
||
|
||
return descricao + ": " + String.format("%dm %ds", TimeUnit.MILLISECONDS.toMinutes(tempo), TimeUnit.MILLISECONDS.toSeconds(tempo) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(tempo)));
|
||
}
|
||
|
||
@Override
|
||
public File buscarArquivoExportacaoRMD(Date inicio, Date fim, Empresa empresa, Estado estado) {
|
||
Connection connection = null;
|
||
|
||
try {
|
||
connection = getConnection();
|
||
|
||
List<ExportacaoRMDTipoPAR> listaRegistroRMDTipoPAR = fiscalDAO.buscarRegistroRMDTipoPAR(connection, inicio, fim, empresa.getEmpresaId(), estado.getCveestado());
|
||
List<ExportacaoRMDTipoRMD> listaRegistroRMDTipoRMD = fiscalDAO.buscarRegistroRMDTipoRMD(connection, inicio, fim, empresa.getEmpresaId(), estado.getCveestado());
|
||
List<ExportacaoRMDTipoBPS> listaRegistroRMDTipoBPS = fiscalDAO.buscarRegistroRMDTipoBPS(connection, inicio, fim, empresa.getEmpresaId(), estado.getCveestado());
|
||
List<ExportacaoRMDTipoDBP> listaRegistroRMDTipoDBP = fiscalDAO.buscarRegistroRMDTipoDBP(connection, inicio, fim, empresa.getEmpresaId(), estado.getCveestado());
|
||
|
||
return exportacaoFiscal.gerarArquivoRMD(inicio, fim, empresa.getNombempresa(), listaRegistroRMDTipoPAR, listaRegistroRMDTipoRMD, listaRegistroRMDTipoBPS,
|
||
listaRegistroRMDTipoDBP);
|
||
|
||
} catch (Exception e) {
|
||
log.error(e.getMessage(), e);
|
||
|
||
} finally {
|
||
try {
|
||
if (connection != null && !connection.isClosed()) {
|
||
connection.close();
|
||
}
|
||
} catch (SQLException e) {
|
||
log.error("error fechar conex<65>o", e);
|
||
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
}
|