1491 lines
61 KiB
Java
1491 lines
61 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.io.OutputStreamWriter;
|
||
import java.io.PrintWriter;
|
||
import java.math.BigDecimal;
|
||
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 org.apache.commons.lang.StringUtils;
|
||
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.stereotype.Service;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
|
||
import com.rjconsultores.ventaboletos.constantes.Constantes;
|
||
import com.rjconsultores.ventaboletos.dao.FiscalDAO;
|
||
import com.rjconsultores.ventaboletos.entidad.Empresa;
|
||
import com.rjconsultores.ventaboletos.entidad.EsquemaCorrida;
|
||
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.DateUtil;
|
||
import com.rjconsultores.ventaboletos.utilerias.LocaleUtil;
|
||
import com.rjconsultores.ventaboletos.utilerias.StringHelper;
|
||
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.LeitorFiscalReducaoZVO;
|
||
import com.rjconsultores.ventaboletos.vo.impressaofiscal.LeitorFiscalVO;
|
||
import com.rjconsultores.ventaboletos.vo.impressaofiscal.LeitorManualFiscalVO;
|
||
import com.rjconsultores.ventaboletos.vo.impressaofiscal.LeitorNaoFiscalVO;
|
||
import com.rjconsultores.ventaboletos.vo.impressaofiscal.SiglaMotivoCancelacion;
|
||
import com.rjconsultores.ventaboletos.vo.impressaofiscal.SituacaoTributaria;
|
||
import com.rjconsultores.ventaboletos.vo.impressaofiscal.SubItens;
|
||
|
||
@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;
|
||
|
||
@Override
|
||
@Transactional
|
||
public int gerarRegistroP2_F2(List<EsquemaCorrida> lsEsquemaCorrida, Date dataDe, Date dataAte) {
|
||
return fiscalDAO.gerarRegistroP2_F2(lsEsquemaCorrida, dataDe, dataAte);
|
||
}
|
||
|
||
@Override
|
||
public File importacionFiscalECFManual(Date inicio, Date fim, Empresa empresa) {
|
||
|
||
try {
|
||
String nomeArquivo = "fiscal_ecf_manual_" + DateUtil.getStringDate(Calendar.getInstance().getTime(), "yyyyMMddhhmmss");
|
||
File arquivo = File.createTempFile(nomeArquivo, ".txt");
|
||
|
||
PrintWriter gravarArq = new PrintWriter(new OutputStreamWriter(new FileOutputStream(arquivo), Constantes.UTF_8));
|
||
|
||
Integer seq = 1;
|
||
|
||
String cnpjFilial = empresa.getCnpj();
|
||
String brancos371 = StringHelper.preencherStringEspacoEsquerda(null, 371);
|
||
String dataAgora = DateUtil.getStringDate(Calendar.getInstance().getTime(), DATE_FORMAT_FISCAL);
|
||
String sequencial = StringHelper.preencherZeroEsquerda(seq.toString(), 6);
|
||
|
||
String abertura = aberturaDeArquivo("0", dataAgora, cnpjFilial, brancos371, sequencial);
|
||
gravarArq.print(abertura + QUEBRA_LINHA);
|
||
|
||
Integer qtdDoc = 0;
|
||
Integer qtdItens = 0;
|
||
|
||
inicio = DateUtil.inicioFecha(inicio);
|
||
fim = DateUtil.fimFecha(fim);
|
||
|
||
List<ImportacionManualFiscalVO> list = agruparItensFiscaisManual(fiscalDAO.buscaDatosFiscaisECFManual(inicio, fim, empresa.getEmpresaId()));
|
||
for (ImportacionManualFiscalVO imf : list) {
|
||
|
||
qtdDoc++;
|
||
|
||
String data = imf.getDataEmissao();
|
||
|
||
String coo = StringHelper.preencherZeroEsquerda(imf.getCoo(), 9);
|
||
String serie = StringHelper.preencherStringEspacoEsquerda(imf.getSerie(), 3);
|
||
String especie = StringHelper.preencherStringEspacoDireita("RMD", 5);
|
||
|
||
String codigoCliente = StringHelper.preencherStringEsquerda(null, 6, "9");
|
||
String filialCliente = StringHelper.preencherStringEsquerda(null, 2, "9");
|
||
String condicaoPagamento = StringHelper.preencherStringEsquerda(null, 3, "9");
|
||
|
||
BigDecimal valorTotal = UtileriasFiscal.arredondar(calculaValorTotalECFManual(imf));
|
||
String valorTotalDocumanto = UtileriasFiscal.formataEspacosDecimalDireita(valorTotal, 14);
|
||
String valorItens = valorTotalDocumanto;
|
||
|
||
BigDecimal valorICMS = imf.getIcms() == null ? BigDecimal.ZERO : UtileriasFiscal.arredondar(imf.getIcms());
|
||
String baseCalICMS = UtileriasFiscal.formataZeroDecimal(valorICMS, 14);
|
||
|
||
BigDecimal aliquotaCalc = imf.getIcms() == null ? BigDecimal.ZERO : UtileriasFiscal.arredondar(imf.getIcms());
|
||
BigDecimal baseCalculo = UtileriasFiscal.arredondar(imf.getValorTotal());
|
||
BigDecimal valorImposto = UtileriasFiscal.arredondar(baseCalculo.multiply(aliquotaCalc.divide(CEM)));
|
||
String valorTotalICMS = UtileriasFiscal.formataZeroDecimal(valorImposto, 14);
|
||
|
||
String valorPIS = StringHelper.preencherZeroEsquerda(null, 14);
|
||
String valorCofins = StringHelper.preencherZeroEsquerda(null, 14);
|
||
String valorCSLL = StringHelper.preencherZeroEsquerda(null, 14);
|
||
String totalICMSIsento = StringHelper.preencherZeroEsquerda(null, 14);
|
||
String totalICMSNaotributado = StringHelper.preencherZeroEsquerda(null, 14);
|
||
|
||
String origenUf = StringHelper.preencherStringEspacoEsquerda(imf.getOrigenUf(), 2);
|
||
String origen = imf.getOrigenId().toString();
|
||
if (origen.length() > 2) {
|
||
origen = origen.substring(2);
|
||
origen = StringHelper.preencherStringEspacoEsquerda(origen, 5);
|
||
} else {
|
||
origen = StringHelper.preencherStringEspacoEsquerda(origen, 5);
|
||
}
|
||
|
||
String destinoUf = StringHelper.preencherStringEspacoEsquerda(imf.getDestinoUf(), 2);
|
||
String destino = imf.getDestinoId().toString();
|
||
if (destino.length() > 2) {
|
||
destino = destino.substring(2);
|
||
destino = StringHelper.preencherStringEspacoEsquerda(destino, 5);
|
||
} else {
|
||
destino = StringHelper.preencherStringEspacoEsquerda(destino, 5);
|
||
}
|
||
|
||
String subSerie = StringHelper.preencherStringEspacoEsquerda(imf.getSubSerie(), 2);
|
||
String aidf = StringHelper.preencherStringEspacoEsquerda(imf.getAidf(), 30);
|
||
String status = StringHelper.preencherStringEspacoEsquerda(imf.getStatus(), 1);
|
||
String claseServicio = StringHelper.preencherStringEspacoDireita(imf.getClaseServicio(), 30);
|
||
String brancos170 = StringHelper.preencherStringEspacoEsquerda(null, 170);
|
||
|
||
seq++;
|
||
sequencial = StringHelper.preencherZeroEsquerda(seq.toString(), 6);
|
||
|
||
String header = headerDocumentoFiscal("1", data, cnpjFilial, coo, serie, especie, codigoCliente,
|
||
filialCliente, condicaoPagamento, valorTotalDocumanto, valorItens, baseCalICMS, valorTotalICMS,
|
||
valorPIS, valorCofins, valorCSLL, totalICMSIsento, totalICMSNaotributado, origenUf, origen,
|
||
destinoUf, destino, subSerie,
|
||
aidf, status, null, null, brancos170, claseServicio, sequencial);
|
||
gravarArq.print(header + QUEBRA_LINHA);
|
||
|
||
for (String itemDoc : montarItensFiscaisManual(imf, imf.getSubItens(), seq)) {
|
||
|
||
qtdItens++;
|
||
gravarArq.print(itemDoc + QUEBRA_LINHA);
|
||
seq++;
|
||
}
|
||
}
|
||
|
||
seq++;
|
||
|
||
String qtdeDocGerados = StringHelper.preencherZeroEsquerda(qtdDoc.toString(), 6);
|
||
String qtdeItensDocGerados = StringHelper.preencherZeroEsquerda(qtdItens.toString(), 6);
|
||
String brancos381 = StringHelper.preencherStringEspacoEsquerda(null, 381);
|
||
sequencial = StringHelper.preencherZeroEsquerda(seq.toString(), 6);
|
||
|
||
String fechamento = fechamentoDeArquivo("3", qtdeDocGerados, qtdeItensDocGerados, brancos381, sequencial);
|
||
gravarArq.print(fechamento + QUEBRA_LINHA);
|
||
|
||
gravarArq.close();
|
||
return arquivo;
|
||
|
||
} catch (IOException e) {
|
||
log.error("", e);
|
||
} catch (SQLException e) {
|
||
log.error("", e);
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
private BigDecimal calculaValorTotalECFManual(ImportacionManualFiscalVO imf) {
|
||
BigDecimal valorTotal = BigDecimal.ZERO;
|
||
valorTotal = valorTotal.add(imf.getValorTotal());
|
||
valorTotal = valorTotal.add(imf.getImporteoutros() == null ? BigDecimal.ZERO : imf.getImporteoutros());
|
||
valorTotal = valorTotal.add(imf.getImportepedagio() == null ? BigDecimal.ZERO : imf.getImportepedagio());
|
||
valorTotal = valorTotal.add(imf.getImporteseguro() == null ? BigDecimal.ZERO : imf.getImporteseguro());
|
||
valorTotal = valorTotal.add(imf.getImportetaxaembarque() == null ? BigDecimal.ZERO : imf.getImportetaxaembarque());
|
||
return valorTotal;
|
||
}
|
||
|
||
private List<String> montarItensFiscaisManual(ImportacionManualFiscalVO imf, List<SubItens> itensFiscais, Integer seq) {
|
||
|
||
List<String> itensDocs = new ArrayList<String>();
|
||
Integer num = 0;
|
||
|
||
for (SubItens item : itensFiscais) {
|
||
|
||
num++;
|
||
String numItem = StringHelper.preencherZeroEsquerda(num.toString(), 3);
|
||
|
||
String cfop = null;
|
||
if (imf.getEstadoIdOrigen() != imf.getEstadoIdDestino()) {
|
||
cfop = StringHelper.preencherStringEspacoDireita("6357", 5);
|
||
} else {
|
||
cfop = StringHelper.preencherStringEspacoDireita("5357", 5);
|
||
}
|
||
|
||
String codigoTES = StringHelper.preencherStringEsquerda(null, 3, "9");
|
||
String prod = imf.getCodProduto() == null ? item.getCodProduto() : imf.getCodProduto();
|
||
String codProduto = StringHelper.preencherStringEspacoEsquerda(prod, 15);
|
||
|
||
String unidade = "UN";
|
||
String quantidade = "001";
|
||
|
||
BigDecimal valorItem = UtileriasFiscal.arredondar(item.getValor());
|
||
String valorUnitario = UtileriasFiscal.formataZeroDecimal(valorItem, 15);
|
||
String valorTotal = UtileriasFiscal.formataZeroDecimal(valorItem, 15);
|
||
|
||
String aliquotaItem = UtileriasFiscal.formataZeroDecimal(null, 7);
|
||
String impostoItem = UtileriasFiscal.formataZeroDecimal(null, 15);
|
||
String totalICMSIsentoItem = StringHelper.preencherZeroEsquerda(null, 15);
|
||
String totalICMSNaotributadoItem = StringHelper.preencherZeroEsquerda(null, 15);
|
||
|
||
if (codProduto.indexOf("TA") >= 0) {
|
||
BigDecimal aliquotaCalc = UtileriasFiscal.arredondar(imf.getIcms() == null ? BigDecimal.ZERO : imf.getIcms());
|
||
BigDecimal baseCalculo = UtileriasFiscal.arredondar(imf.getValorTotal() == null ? BigDecimal.ZERO : imf.getValorTotal());
|
||
BigDecimal valorImposto = UtileriasFiscal.arredondar(baseCalculo.multiply(aliquotaCalc.divide(CEM)));
|
||
impostoItem = UtileriasFiscal.formataZeroDecimal(valorImposto, 15);
|
||
} else {
|
||
totalICMSNaotributadoItem = StringHelper.preencherZeroEsquerda(valorTotal, 15);
|
||
}
|
||
|
||
String aliquotaPIS = StringHelper.preencherZeroEsquerda(null, 7);
|
||
String valorPIS = StringHelper.preencherZeroEsquerda(null, 15);
|
||
String aliquotaCofins = StringHelper.preencherZeroEsquerda(null, 7);
|
||
String valorCofins = StringHelper.preencherZeroEsquerda(null, 15);
|
||
String aliquotaCSLL = StringHelper.preencherZeroEsquerda(null, 7);
|
||
String valorCSLL = StringHelper.preencherZeroEsquerda(null, 15);
|
||
String aliquotaISS = StringHelper.preencherZeroEsquerda(null, 7);
|
||
String valorISS = StringHelper.preencherZeroEsquerda(null, 15);
|
||
|
||
String numeroPDV = StringHelper.preencherStringEspacoDireita(imf.getNumpdv(), 10);
|
||
String numeroSeriePDV = StringHelper.preencherStringEspacoDireita(imf.getNumImpressora(), 20);
|
||
String modeloImpressora = StringHelper.preencherStringEspacoDireita(imf.getModeloImpressora(), 40);
|
||
String numCaixa = StringHelper.preencherStringEspacoDireita(imf.getNumCaixa(), 10);
|
||
String status = StringHelper.preencherStringEspacoEsquerda(imf.getStatus(), 1);
|
||
|
||
seq++;
|
||
String sequencial = StringHelper.preencherZeroEsquerda(seq.toString(), 6);
|
||
String brancos111 = StringHelper.preencherStringEspacoDireita(null, 111);
|
||
|
||
String itemDoc = itensDocumentoFiscal("2", numItem, cfop, codigoTES, codProduto,
|
||
unidade, quantidade, valorUnitario, valorTotal, aliquotaItem, impostoItem,
|
||
aliquotaPIS, valorPIS, aliquotaCofins, valorCofins, aliquotaCSLL, valorCSLL,
|
||
aliquotaISS, valorISS, totalICMSIsentoItem, totalICMSNaotributadoItem,
|
||
numeroPDV, numeroSeriePDV, modeloImpressora, numCaixa, status, null, null,
|
||
brancos111, sequencial);
|
||
itensDocs.add(itemDoc);
|
||
}
|
||
|
||
return itensDocs;
|
||
}
|
||
|
||
private List<ImportacionManualFiscalVO> agruparItensFiscaisManual(List<ImportacionManualFiscalVO> list) {
|
||
|
||
List<ImportacionManualFiscalVO> aux = new ArrayList<ImportacionManualFiscalVO>();
|
||
|
||
for (ImportacionManualFiscalVO imf : list) {
|
||
|
||
List<SubItens> itens = new ArrayList<SubItens>();
|
||
|
||
// if (imf.getImporteoutros() != null) {
|
||
// itens.add(new SubItens(imf.getImporteoutros(), "00000000000-OT"));
|
||
// }
|
||
|
||
if (imf.getImportepedagio() != null) {
|
||
itens.add(new SubItens(imf.getImportepedagio(), "00000000000-PE"));
|
||
}
|
||
|
||
if (imf.getImporteseguro() != null) {
|
||
itens.add(new SubItens(imf.getImporteseguro(), "00000000000-SE"));
|
||
}
|
||
|
||
if (imf.getImportetaxaembarque() != null) {
|
||
itens.add(new SubItens(imf.getImportetaxaembarque(), "00000000000-TX"));
|
||
}
|
||
if (imf.getValorItem() != null) {
|
||
itens.add(new SubItens(imf.getValorTotal(), "00000000000-TA"));
|
||
}
|
||
|
||
imf.setSubItens(itens);
|
||
aux.add(imf);
|
||
}
|
||
|
||
return aux;
|
||
}
|
||
|
||
public File importacionFiscalReducaoZ(Date inicio, Date fim, Empresa empresa) {
|
||
|
||
try {
|
||
String nomeArquivo = "fiscal_reducao_z_" + DateUtil.getStringDate(Calendar.getInstance().getTime(), "yyyyMMddhhmmss");
|
||
File arquivo = File.createTempFile(nomeArquivo, ".txt");
|
||
|
||
PrintWriter gravarArq = new PrintWriter(new OutputStreamWriter(new FileOutputStream(arquivo), Constantes.UTF_8));
|
||
|
||
Integer seq = 1;
|
||
|
||
String cnpjFilial = StringHelper.retornaSomenteNumeros(empresa.getCnpj() == null ? StringHelper.preencherStringEspacoEsquerda(null, 14) : empresa.getCnpj());
|
||
String brancos371 = StringHelper.preencherStringEspacoEsquerda(null, 371);
|
||
String dataAgora = DateUtil.getStringDate(Calendar.getInstance().getTime(), DATE_FORMAT_FISCAL);
|
||
String sequencial = StringHelper.preencherZeroEsquerda(seq.toString(), 6);
|
||
|
||
String abertura = aberturaDeArquivo("0", dataAgora, cnpjFilial, brancos371, sequencial);
|
||
gravarArq.print(abertura + QUEBRA_LINHA);
|
||
|
||
List<ImportacionFiscalReducaoZVO> list = agruparReducaoZ(fiscalDAO.buscaDatosFiscaisReducaoZ(inicio, fim, empresa.getEmpresaId()));
|
||
|
||
for (ImportacionFiscalReducaoZVO ifrZ : list) {
|
||
|
||
String dataMovimento = ifrZ.getDatamov();
|
||
String cnpj = StringHelper.retornaSomenteNumeros(ifrZ.getCnpj() == null ? StringHelper.preencherStringEspacoEsquerda(null, 14) : ifrZ.getCnpj());
|
||
|
||
String impAliquota = ifrZ.getAliquota() == null ? "" : ifrZ.getAliquota().substring(0, 2);
|
||
String numRelatorio = StringHelper.preencherZeroEsquerda(impAliquota, 6);
|
||
|
||
String numPDV = StringHelper.preencherStringEspacoEsquerda(ifrZ.getNumpdv(), 10);
|
||
String numSeriePDV = StringHelper.preencherStringEspacoEsquerda(ifrZ.getNumserie20(), 20);
|
||
String numReducaoZ = StringHelper.preencherStringEspacoEsquerda(ifrZ.getCrz(), 5);
|
||
|
||
String gtInicial = StringHelper.preencherZeroEsquerda(ifrZ.getGtInicial().toString(), 18);
|
||
String gtFinal = StringHelper.preencherZeroEsquerda(ifrZ.getGtFinal().toString(), 18);
|
||
|
||
String docFiscalInic = StringHelper.preencherStringEspacoEsquerda(ifrZ.getCooinicial(), 9);
|
||
String docFiscalFinal = StringHelper.preencherStringEspacoEsquerda(ifrZ.getCoofinal(), 9);
|
||
|
||
String valorCancel = StringHelper.preencherZeroEsquerda(null, 8);
|
||
String valorContabil = StringHelper.preencherZeroEsquerda(ifrZ.getVendabrutadiaria().toString(), 14);
|
||
|
||
String subtributaria = StringHelper.preencherZeroEsquerda(null, 14);
|
||
String descontos = StringHelper.preencherZeroEsquerda(null, 14);
|
||
String isento = StringHelper.preencherZeroEsquerda(null, 14);
|
||
|
||
String valorNaoTributado = ifrZ.getValorNaoTributado() == null ? "" : ifrZ.getValorNaoTributado().toString();
|
||
String naoTributado = StringHelper.preencherZeroEsquerda(valorNaoTributado, 14);
|
||
|
||
String aliquota = StringHelper.preencherZeroEsquerda(null, 14);
|
||
if (ifrZ.getAliquota().length() == 7 && ifrZ.getAliquota().substring(2, 3).equals(SituacaoTributaria.TRIBUTADO.getValue())) {
|
||
aliquota = ifrZ.getAliquota().substring(3, 7);
|
||
aliquota = StringHelper.preencherZeroEsquerda(aliquota, 14);
|
||
}
|
||
|
||
String aliquota2 = StringHelper.preencherZeroEsquerda(null, 14);
|
||
String aliquota3 = StringHelper.preencherZeroEsquerda(null, 14);
|
||
String aliquota4 = StringHelper.preencherZeroEsquerda(null, 14);
|
||
|
||
String coo = StringHelper.preencherStringEspacoEsquerda(ifrZ.getCoo(), 6);
|
||
String outrosRecebimentos = StringHelper.preencherZeroEsquerda(null, 14);
|
||
|
||
BigDecimal aliquotaCalc = BigDecimal.valueOf(Double.valueOf(aliquota) / CEM.doubleValue());
|
||
aliquotaCalc = UtileriasFiscal.arredondar(aliquotaCalc);
|
||
|
||
BigDecimal baseCalculo = UtileriasFiscal.arredondar(ifrZ.getImposto().divide(CEM));
|
||
baseCalculo = UtileriasFiscal.arredondar(baseCalculo);
|
||
|
||
BigDecimal valorImposto = baseCalculo.multiply(aliquotaCalc.divide(CEM));
|
||
valorImposto = UtileriasFiscal.arredondar(valorImposto);
|
||
String impostoDebitado = UtileriasFiscal.formataZeroDecimal(valorImposto, 13);
|
||
|
||
String dataReducaoZ = ifrZ.getDatareducao();
|
||
String horaReducaoZ = ifrZ.getHorareducao();
|
||
|
||
String valorDocFiscal = StringHelper.preencherZeroEsquerda(ifrZ.getImposto().toString(), 13);
|
||
String brancos82 = StringHelper.preencherStringEspacoEsquerda(null, 82);
|
||
|
||
seq++;
|
||
sequencial = StringHelper.preencherZeroEsquerda(seq.toString(), 6);
|
||
|
||
String redZ = reducaoZ("1", dataMovimento, cnpj, numRelatorio, numPDV, numSeriePDV, numReducaoZ,
|
||
gtInicial, gtFinal, docFiscalInic, docFiscalFinal, valorCancel, valorContabil, subtributaria,
|
||
descontos, isento, naoTributado, aliquota, aliquota2, aliquota3, aliquota4, coo, outrosRecebimentos,
|
||
impostoDebitado, dataReducaoZ, horaReducaoZ, valorDocFiscal, brancos82, sequencial);
|
||
gravarArq.print(redZ + QUEBRA_LINHA);
|
||
}
|
||
|
||
Integer qtdeDoc = seq - 1;
|
||
seq++;
|
||
|
||
String qtdeDocGerados = StringHelper.preencherZeroEsquerda(qtdeDoc.toString(), 6);
|
||
String brancos387 = StringHelper.preencherStringEspacoEsquerda(null, 387);
|
||
sequencial = StringHelper.preencherZeroEsquerda(seq.toString(), 6);
|
||
|
||
String fechamento = fechamentoDeArquivo("3", qtdeDocGerados, null, brancos387, sequencial);
|
||
gravarArq.print(fechamento + QUEBRA_LINHA);
|
||
|
||
gravarArq.close();
|
||
return arquivo;
|
||
|
||
} catch (IOException e) {
|
||
log.error("", e);
|
||
} catch (SQLException e) {
|
||
log.error("", e);
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
private List<ImportacionFiscalReducaoZVO> agruparReducaoZ(List<ImportacionFiscalReducaoZVO> list) {
|
||
|
||
List<ImportacionFiscalReducaoZVO> aux = new ArrayList<ImportacionFiscalReducaoZVO>();
|
||
|
||
BigDecimal valorNaoTributado = null;
|
||
for (ImportacionFiscalReducaoZVO ifr : list) {
|
||
if (ifr.getAliquota().equals(SituacaoTributaria.NAO_TRIBUTADO.getValue())) {
|
||
valorNaoTributado = ifr.getImposto();
|
||
} else {
|
||
ifr.setValorNaoTributado(valorNaoTributado);
|
||
aux.add(ifr);
|
||
}
|
||
}
|
||
|
||
return aux;
|
||
}
|
||
|
||
@Override
|
||
public File importacionFiscalECFCancelados(Date inicio, Date fim, Empresa empresa) {
|
||
return importacionFiscalECFGenerico(inicio, fim, empresa, true);
|
||
}
|
||
|
||
@Override
|
||
public File importacionFiscalECF(Date inicio, Date fim, Empresa empresa) {
|
||
return importacionFiscalECFGenerico(inicio, fim, empresa, false);
|
||
}
|
||
|
||
private File importacionFiscalECFGenerico(Date inicio, Date fim, Empresa empresa, boolean isCancelados) {
|
||
|
||
try {
|
||
String nomeArquivo = "fiscal_ecf_" + DateUtil.getStringDate(Calendar.getInstance().getTime(), "yyyyMMddhhmmss");
|
||
File arquivo = File.createTempFile(nomeArquivo, ".txt");
|
||
|
||
PrintWriter gravarArq = new PrintWriter(new OutputStreamWriter(new FileOutputStream(arquivo), Constantes.UTF_8));
|
||
|
||
Integer seq = 1;
|
||
|
||
String cnpjFilial = empresa.getCnpj();
|
||
String brancos371 = StringHelper.preencherStringEspacoEsquerda(null, 371);
|
||
String dataAgora = DateUtil.getStringDate(Calendar.getInstance().getTime(), DATE_FORMAT_FISCAL);
|
||
String seqAbertura = StringHelper.preencherZeroEsquerda(seq.toString(), 6);
|
||
|
||
String abertura = aberturaDeArquivo("0", dataAgora, cnpjFilial, brancos371, seqAbertura);
|
||
gravarArq.print(abertura + QUEBRA_LINHA);
|
||
|
||
Integer qtdDoc = 0;
|
||
Integer qtdItens = 0;
|
||
|
||
String header = null;
|
||
List<String> itensDocs = new ArrayList<String>();
|
||
|
||
List<ImportacionFiscalVO> list = fiscalDAO.buscaDatosFiscaisECF(inicio, fim, empresa.getEmpresaId(), isCancelados);
|
||
|
||
for (ImportacionFiscalVO item : list) {
|
||
|
||
qtdDoc++;
|
||
header = null;
|
||
itensDocs = new ArrayList<String>();
|
||
|
||
String data = item.getDataEmissao();
|
||
String brancos = StringHelper.preencherStringEspacoEsquerda(null, 202);
|
||
|
||
String serie = item.getNumImpressora();
|
||
if (serie.length() >= 20) {
|
||
serie = serie.substring(17);
|
||
serie = StringHelper.preencherStringEspacoEsquerda(serie, 3);
|
||
} else {
|
||
serie = StringHelper.preencherStringEspacoEsquerda(null, 3);
|
||
}
|
||
|
||
String especie = StringHelper.preencherStringEspacoDireita("CF", 5);
|
||
|
||
String codigoCliente = StringHelper.preencherStringEsquerda(null, 6, "9");
|
||
String filialCliente = StringHelper.preencherStringEsquerda(null, 2, "9");
|
||
String condicaoPagamento = StringHelper.preencherStringEsquerda(null, 3, "9");
|
||
|
||
String valorPIS = StringHelper.preencherZeroEsquerda(null, 14);
|
||
String valorCofins = StringHelper.preencherZeroEsquerda(null, 14);
|
||
String valorCSLL = StringHelper.preencherZeroEsquerda(null, 14);
|
||
|
||
String coo = StringHelper.preencherStringEspacoDireita(item.getCoo(), 9);
|
||
|
||
BigDecimal valorTotal = UtileriasFiscal.arredondar(valorTotalItensECF(item));
|
||
String valorTotalDocumanto = UtileriasFiscal.formataEspacosDecimalDireita(valorTotal, 14);
|
||
String valorItens = valorTotalDocumanto;
|
||
|
||
HashMap<String, String> aliquotaBaseCalculo = getAliquotaBaseCalculo(item);
|
||
String aliquota = aliquotaBaseCalculo.get("aliquota");
|
||
String imposto = aliquotaBaseCalculo.get("imposto");
|
||
String totalICMSIsento = aliquotaBaseCalculo.get("totalICMSIsento");
|
||
String totalICMSNaotributado = aliquotaBaseCalculo.get("totalICMSNaotributado");
|
||
|
||
String origenUf = StringHelper.preencherStringEspacoEsquerda(item.getOrigenUf(), 2);
|
||
String origen = item.getOrigenId().toString();
|
||
if (origen.length() > 2) {
|
||
origen = origen.substring(2);
|
||
origen = StringHelper.preencherStringEspacoEsquerda(origen, 5);
|
||
} else {
|
||
origen = StringHelper.preencherStringEspacoEsquerda(origen, 5);
|
||
}
|
||
|
||
String destinoUf = StringHelper.preencherStringEspacoEsquerda(item.getDestinoUf(), 2);
|
||
String destino = item.getDestinoId().toString();
|
||
if (destino.length() > 2) {
|
||
destino = destino.substring(2);
|
||
destino = StringHelper.preencherStringEspacoEsquerda(destino, 5);
|
||
} else {
|
||
destino = StringHelper.preencherStringEspacoEsquerda(destino, 5);
|
||
}
|
||
|
||
String motivocancelacion = null;
|
||
String valorMulta = null;
|
||
if (isCancelados) {
|
||
SiglaMotivoCancelacion sigla = SiglaMotivoCancelacion.valueOf(item.getMotivocancelacion());
|
||
motivocancelacion = StringHelper.preencherStringEspacoEsquerda(sigla.toString(), 20);
|
||
valorMulta = UtileriasFiscal.formataZeroDecimal(item.getValorMulta(), 15);
|
||
brancos = StringHelper.preencherStringEspacoEsquerda(null, 167);
|
||
}
|
||
|
||
seq++;
|
||
String status = StringHelper.preencherStringEspacoEsquerda(item.getStatus(), 1);
|
||
String claseServicio = StringHelper.preencherStringEspacoDireita(item.getClaseServicio(), 30);
|
||
String seqHeader = StringHelper.preencherZeroEsquerda(Integer.valueOf(seq).toString(), 6);
|
||
|
||
header = headerDocumentoFiscal("1", data, cnpjFilial, coo, serie, especie, codigoCliente,
|
||
filialCliente, condicaoPagamento, valorTotalDocumanto, valorItens, aliquota, imposto,
|
||
valorPIS, valorCofins, valorCSLL, totalICMSIsento, totalICMSNaotributado, origenUf, origen,
|
||
destinoUf, destino, null, null, status, motivocancelacion, valorMulta, brancos, claseServicio, seqHeader);
|
||
|
||
/*
|
||
* Item da importacion Fiscal ECF
|
||
*/
|
||
int seqItem = 1;
|
||
for (ItemFiscalVO subItens : item.getItensFiscais()) {
|
||
String itemDoc = montarItensFiscais(subItens, seq++, seqItem);
|
||
itensDocs.add(itemDoc);
|
||
qtdItens++;
|
||
seqItem++;
|
||
}
|
||
|
||
grabarHeaderItens(gravarArq, header, itensDocs);
|
||
|
||
}
|
||
|
||
seq++;
|
||
String qtdeDocGerados = StringHelper.preencherZeroEsquerda(qtdDoc.toString(), 6);
|
||
String qtdeItensDocGerados = StringHelper.preencherZeroEsquerda(qtdItens.toString(), 6);
|
||
String brancos381 = StringHelper.preencherStringEspacoEsquerda(null, 381);
|
||
String seqFechamento = StringHelper.preencherZeroEsquerda(seq.toString(), 6);
|
||
|
||
String fechamento = fechamentoDeArquivo("3", qtdeDocGerados, qtdeItensDocGerados, brancos381, seqFechamento);
|
||
gravarArq.print(fechamento + QUEBRA_LINHA);
|
||
|
||
gravarArq.close();
|
||
return arquivo;
|
||
|
||
} catch (IOException e) {
|
||
log.error("", e);
|
||
} catch (SQLException e) {
|
||
log.error("", e);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
private BigDecimal valorTotalItensECF(ImportacionFiscalVO item) {
|
||
BigDecimal valorTotal = BigDecimal.ZERO;
|
||
|
||
BigDecimal valorPedagio = item.getValorPedagio() == null ? BigDecimal.ZERO : item.getValorPedagio();
|
||
BigDecimal valorEmbarque = item.getValorEmbarque() == null ? BigDecimal.ZERO : item.getValorEmbarque();
|
||
BigDecimal valorTarifa = item.getValorTarifa() == null ? BigDecimal.ZERO : item.getValorTarifa();
|
||
BigDecimal valorSeguro = item.getValorSeguro() == null ? BigDecimal.ZERO : item.getValorSeguro();
|
||
if (valorTarifa.equals(BigDecimal.ZERO))
|
||
valorTarifa = BigDecimal.ONE;
|
||
|
||
valorTotal = valorTotal.add(valorTarifa).add(valorPedagio).add(valorEmbarque).add(valorSeguro);
|
||
|
||
return valorTotal;
|
||
}
|
||
|
||
private void grabarHeaderItens(PrintWriter gravarArq, String header, List<String> itensDocs) {
|
||
gravarArq.print(header + QUEBRA_LINHA);
|
||
|
||
for (String doc : itensDocs) {
|
||
gravarArq.print(doc + QUEBRA_LINHA);
|
||
}
|
||
}
|
||
|
||
private String montarItensFiscais(ItemFiscalVO item, Integer seq, Integer seqItem) {
|
||
|
||
String tipoPassagem = null;
|
||
if (item.getRepTributado().length() == 7 && item.getRepTributado().substring(2, 3).equals(SituacaoTributaria.TRIBUTADO.getValue())) {
|
||
tipoPassagem = item.getTipoPassagem();
|
||
|
||
if (tipoPassagem == null)
|
||
tipoPassagem = "E";
|
||
|
||
} else {
|
||
tipoPassagem = "E";
|
||
}
|
||
|
||
String numItem = StringHelper.preencherZeroEsquerda(seqItem.toString(), 2);
|
||
|
||
String cfop = null;
|
||
if (tipoPassagem.equals("E")) {
|
||
cfop = StringHelper.preencherStringEspacoDireita("6357", 5);
|
||
} else if (tipoPassagem.equals("M")) {
|
||
cfop = StringHelper.preencherStringEspacoDireita("5357", 5);
|
||
}
|
||
|
||
String codigoTES = StringHelper.preencherStringEsquerda(null, 3, "9");
|
||
String codProduto = StringHelper.preencherStringEspacoEsquerda(item.getCodProduto(), 15);
|
||
String unidade = "UN";
|
||
String quantidade = "001";
|
||
|
||
// TODO: A forma de enviar o valor do item <20> baseada no codProduto, pelo fato da FISCAL_R5 estar gravando os registros de tarifa errado
|
||
BigDecimal valorItem = BigDecimal.ZERO;
|
||
// VALIDA TARIFA
|
||
if (codProduto.indexOf("TA") >= 0) {
|
||
valorItem = item.getValorTarifa() == null ? valorItem : item.getValorTarifa();
|
||
if (valorItem.equals(BigDecimal.ZERO))
|
||
valorItem = BigDecimal.ONE;
|
||
|
||
// TAXA DE EMBARQUE
|
||
} else if (codProduto.indexOf("TX") >= 0) {
|
||
valorItem = item.getValorEmbarque() == null ? valorItem : item.getValorEmbarque();
|
||
|
||
// PEDAGIO
|
||
} else if (codProduto.indexOf("PE") >= 0) {
|
||
valorItem = item.getValorPedagio() == null ? valorItem : item.getValorPedagio();
|
||
|
||
// SEGURO
|
||
} else if (codProduto.indexOf("SE") >= 0) {
|
||
valorItem = item.getValorSeguro() == null ? valorItem : item.getValorSeguro();
|
||
}
|
||
|
||
valorItem = UtileriasFiscal.arredondar(valorItem);
|
||
String valorUnitario = UtileriasFiscal.formataZeroDecimal(valorItem, 15);
|
||
item.setValorItem(new BigDecimal(valorUnitario));
|
||
String valorTotal = valorUnitario;
|
||
|
||
String aliquotaItem = StringHelper.preencherZeroEsquerda(null, 7);
|
||
String impostoItem = StringHelper.preencherZeroEsquerda(null, 15);
|
||
String totalICMSIsentoItem = StringHelper.preencherZeroEsquerda(null, 15);
|
||
String totalICMSNaotributadoItem = StringHelper.preencherZeroEsquerda(null, 15);
|
||
|
||
HashMap<String, BigDecimal> aliquotaItens = new HashMap<String, BigDecimal>(0);
|
||
|
||
// Verificando se <20> um produto tribut<75>vel. Formato: XXTYYYY (XX - Posicao / YYYY - Aliquota)
|
||
if (item.getRepTributado().length() == 7 && item.getRepTributado().substring(2, 3).equals(SituacaoTributaria.TRIBUTADO.getValue())) {
|
||
|
||
String key = tipoPassagem + item.getRepTributado().substring(3, 7);
|
||
|
||
if (!aliquotaItens.containsKey(key)) {
|
||
aliquotaItens.put(key, BigDecimal.ZERO);
|
||
}
|
||
|
||
BigDecimal soma = item.getValorItem().divide(BigDecimal.TEN.multiply(BigDecimal.TEN));
|
||
aliquotaItens.put(key, soma.add(aliquotaItens.get(key)));
|
||
|
||
} else if (item.getRepTributado().equals(SituacaoTributaria.NAO_TRIBUTADO.getValue())) {
|
||
|
||
BigDecimal soma = item.getValorItem();
|
||
totalICMSNaotributadoItem = StringHelper.preencherZeroEsquerda(soma.toString(), 15);
|
||
}
|
||
|
||
for (String key : aliquotaItens.keySet()) {
|
||
aliquotaItem = key.substring(1, 5);
|
||
|
||
BigDecimal aliquotaCalc = BigDecimal.valueOf(Double.valueOf(aliquotaItem) / CEM.doubleValue());
|
||
aliquotaCalc = UtileriasFiscal.arredondar(aliquotaCalc);
|
||
|
||
BigDecimal baseCalculo = aliquotaItens.get(key);
|
||
baseCalculo = UtileriasFiscal.arredondar(baseCalculo);
|
||
|
||
BigDecimal valorImposto = baseCalculo.multiply(aliquotaCalc.divide(CEM));
|
||
valorImposto = UtileriasFiscal.arredondar(valorImposto);
|
||
|
||
impostoItem = UtileriasFiscal.formataZeroDecimal(valorImposto, 15);
|
||
aliquotaItem = UtileriasFiscal.formataZeroDecimal(null, 7);
|
||
}
|
||
|
||
String aliquotaPIS = StringHelper.preencherZeroEsquerda(null, 7);
|
||
String valorPIS = StringHelper.preencherZeroEsquerda(null, 15);
|
||
String aliquotaCofins = StringHelper.preencherZeroEsquerda(null, 7);
|
||
String valorCofins = StringHelper.preencherZeroEsquerda(null, 15);
|
||
String aliquotaCSLL = StringHelper.preencherZeroEsquerda(null, 7);
|
||
String valorCSLL = StringHelper.preencherZeroEsquerda(null, 15);
|
||
String aliquotaISS = StringHelper.preencherZeroEsquerda(null, 7);
|
||
String valorISS = StringHelper.preencherZeroEsquerda(null, 15);
|
||
|
||
String numeroPDV = StringHelper.preencherStringEspacoDireita(item.getNumpdv(), 10);
|
||
String numeroSeriePDV = StringHelper.preencherStringEspacoDireita(item.getNumImpressora(), 20);
|
||
String modeloImpressora = StringHelper.preencherStringEspacoDireita(item.getModeloImpressora(), 40);
|
||
String numCaixa = StringHelper.preencherStringEspacoDireita(item.getNumCaixa(), 10);
|
||
String status = StringHelper.preencherStringEspacoEsquerda(item.getStatus(), 1);
|
||
|
||
String coo = null;
|
||
String serie = null;
|
||
|
||
coo = StringHelper.preencherStringEspacoDireita(item.getCoo(), 9);
|
||
serie = item.getNumImpressora();
|
||
if (serie.length() >= 20) {
|
||
serie = serie.substring(17);
|
||
serie = StringHelper.preencherStringEspacoEsquerda(serie, 3);
|
||
} else {
|
||
serie = StringHelper.preencherStringEspacoEsquerda(null, 3);
|
||
}
|
||
|
||
String brancos99 = StringHelper.preencherStringEspacoEsquerda(null, 99);
|
||
|
||
seq++;
|
||
String sequencial = StringHelper.preencherZeroEsquerda(seq.toString(), 6);
|
||
|
||
String itemDoc = itensDocumentoFiscal("2", numItem, cfop, codigoTES, codProduto,
|
||
unidade, quantidade, valorUnitario, valorTotal, aliquotaItem, impostoItem,
|
||
aliquotaPIS, valorPIS, aliquotaCofins, valorCofins, aliquotaCSLL, valorCSLL,
|
||
aliquotaISS, valorISS, totalICMSIsentoItem, totalICMSNaotributadoItem,
|
||
numeroPDV, numeroSeriePDV, modeloImpressora, numCaixa, status, coo, serie,
|
||
brancos99, sequencial);
|
||
return itemDoc;
|
||
|
||
}
|
||
|
||
private HashMap<String, String> getAliquotaBaseCalculo(ImportacionFiscalVO fiscal) {
|
||
|
||
HashMap<String, String> resp = new HashMap<String, String>();
|
||
|
||
String aliquota = null;
|
||
String imposto = null;
|
||
String totalICMSIsento = null;
|
||
String totalICMSNaotributado = null;
|
||
|
||
for (ItemFiscalVO item : fiscal.getItensFiscais()) {
|
||
|
||
BigDecimal valorItem = BigDecimal.ZERO;
|
||
String codProduto = StringHelper.preencherStringEspacoEsquerda(item.getCodProduto(), 15);
|
||
// VALIDA TARIFA
|
||
if (codProduto.indexOf("TA") >= 0) {
|
||
valorItem = item.getValorTarifa() == null ? valorItem : item.getValorTarifa();
|
||
if (valorItem.equals(BigDecimal.ZERO))
|
||
valorItem = BigDecimal.ONE;
|
||
|
||
// TAXA DE EMBARQUE
|
||
} else if (codProduto.indexOf("TX") >= 0) {
|
||
valorItem = item.getValorEmbarque() == null ? valorItem : item.getValorEmbarque();
|
||
|
||
// PEDAGIO
|
||
} else if (codProduto.indexOf("PE") >= 0) {
|
||
valorItem = item.getValorPedagio() == null ? valorItem : item.getValorPedagio();
|
||
|
||
// SEGURO
|
||
} else if (codProduto.indexOf("SE") >= 0) {
|
||
valorItem = item.getValorSeguro() == null ? valorItem : item.getValorSeguro();
|
||
}
|
||
|
||
valorItem = UtileriasFiscal.arredondar(valorItem);
|
||
item.setValorItem(new BigDecimal(UtileriasFiscal.formataZeroDecimal(valorItem, 15)));
|
||
|
||
HashMap<String, BigDecimal> aliquotaBaseCalculo = new HashMap<String, BigDecimal>(0);
|
||
|
||
// Verificando se <20> um produto tribut<75>vel. Formato: XXTYYYY (XX - Posicao / YYYY - Aliquota)
|
||
if (item.getRepTributado().length() == 7 && item.getRepTributado().substring(2, 3).equals(SituacaoTributaria.TRIBUTADO.getValue())) {
|
||
|
||
String tipoPassagem = item.getTipoPassagem();
|
||
|
||
if (tipoPassagem == null) {
|
||
tipoPassagem = "E";
|
||
}
|
||
|
||
String key = tipoPassagem + item.getRepTributado().substring(3, 7);
|
||
|
||
if (!aliquotaBaseCalculo.containsKey(key)) {
|
||
aliquotaBaseCalculo.put(key, BigDecimal.ZERO);
|
||
}
|
||
|
||
BigDecimal soma = item.getValorItem().divide(BigDecimal.TEN.multiply(BigDecimal.TEN));
|
||
aliquotaBaseCalculo.put(key, soma.add(aliquotaBaseCalculo.get(key)));
|
||
|
||
} else if (item.getRepTributado().equals(SituacaoTributaria.NAO_TRIBUTADO.getValue())) {
|
||
|
||
BigDecimal soma = item.getValorItem();
|
||
totalICMSNaotributado = soma.toString();
|
||
|
||
if (totalICMSNaotributado.length() < 14) {
|
||
totalICMSNaotributado = StringHelper.preencherZeroEsquerda(totalICMSNaotributado, 14);
|
||
}
|
||
|
||
}
|
||
|
||
for (String key : aliquotaBaseCalculo.keySet()) {
|
||
aliquota = key.substring(1, 5);
|
||
|
||
BigDecimal aliquotaCalc = BigDecimal.valueOf(Double.valueOf(aliquota) / CEM.doubleValue());
|
||
aliquotaCalc = UtileriasFiscal.arredondar(aliquotaCalc);
|
||
|
||
BigDecimal baseCalculo = aliquotaBaseCalculo.get(key);
|
||
baseCalculo = UtileriasFiscal.arredondar(baseCalculo);
|
||
|
||
BigDecimal valorImposto = baseCalculo.multiply(aliquotaCalc.divide(CEM));
|
||
valorImposto = UtileriasFiscal.arredondar(valorImposto);
|
||
|
||
imposto = UtileriasFiscal.formataZeroDecimal(valorImposto, 14);
|
||
aliquota = UtileriasFiscal.formataZeroDecimal(aliquotaCalc, 14);
|
||
}
|
||
}
|
||
|
||
if (StringUtils.isBlank(aliquota))
|
||
aliquota = StringHelper.preencherZeroEsquerda(null, 14);
|
||
|
||
if (StringUtils.isBlank(imposto))
|
||
imposto = StringHelper.preencherZeroEsquerda(null, 14);
|
||
|
||
if (StringUtils.isBlank(totalICMSIsento))
|
||
totalICMSIsento = StringHelper.preencherZeroEsquerda(null, 14);
|
||
|
||
if (StringUtils.isBlank(totalICMSNaotributado))
|
||
totalICMSNaotributado = StringHelper.preencherZeroEsquerda(null, 14);
|
||
|
||
resp.put("aliquota", aliquota);
|
||
resp.put("imposto", imposto);
|
||
resp.put("totalICMSIsento", totalICMSIsento);
|
||
resp.put("totalICMSNaotributado", totalICMSNaotributado);
|
||
|
||
return resp;
|
||
}
|
||
|
||
private String reducaoZ(String identificador, String dataMovimento, String cnpj, String numRelatorio, String numPDV,
|
||
String numSeriePDV, String numReducaoZ, String gtInicial, String gtFinal, String docFiscalInic,
|
||
String docFiscalFinal, String valorCancel, String valorContabil, String subtributaria, String descontos,
|
||
String isento, String naoTributado, String aliquota, String aliquota2, String aliquota3, String aliquota4,
|
||
String coo, String outrosRecebimentos, String impostoDebitado, String dataReducaoZ, String horaReducaoZ,
|
||
String valorDocFiscal, String brancos, String sequencial) {
|
||
|
||
StringBuilder reducaoZ = new StringBuilder();
|
||
reducaoZ.append(identificador);
|
||
reducaoZ.append(dataMovimento);
|
||
reducaoZ.append(cnpj);
|
||
reducaoZ.append(numRelatorio);
|
||
reducaoZ.append(numPDV);
|
||
reducaoZ.append(numSeriePDV);
|
||
reducaoZ.append(numReducaoZ);
|
||
reducaoZ.append(gtInicial);
|
||
reducaoZ.append(gtFinal);
|
||
reducaoZ.append(docFiscalInic);
|
||
reducaoZ.append(docFiscalFinal);
|
||
reducaoZ.append(valorCancel);
|
||
reducaoZ.append(valorContabil);
|
||
reducaoZ.append(subtributaria);
|
||
reducaoZ.append(descontos);
|
||
reducaoZ.append(isento);
|
||
reducaoZ.append(naoTributado);
|
||
reducaoZ.append(aliquota);
|
||
reducaoZ.append(aliquota2);
|
||
reducaoZ.append(aliquota3);
|
||
reducaoZ.append(aliquota4);
|
||
reducaoZ.append(coo);
|
||
reducaoZ.append(outrosRecebimentos);
|
||
reducaoZ.append(impostoDebitado);
|
||
reducaoZ.append(dataReducaoZ);
|
||
reducaoZ.append(horaReducaoZ);
|
||
reducaoZ.append(valorDocFiscal);
|
||
reducaoZ.append(brancos);
|
||
reducaoZ.append(sequencial);
|
||
|
||
return reducaoZ.toString();
|
||
}
|
||
|
||
private String aberturaDeArquivo(String identificador, String data, String cnpjFilial, String brancos372,
|
||
String sequencial) {
|
||
|
||
StringBuilder abertura = new StringBuilder();
|
||
abertura.append(identificador);
|
||
abertura.append(data);
|
||
abertura.append(cnpjFilial);
|
||
abertura.append(brancos372);
|
||
abertura.append(sequencial);
|
||
|
||
return abertura.toString();
|
||
}
|
||
|
||
private String headerDocumentoFiscal(String identificador, String data, String cnpjImpressora, String coo,
|
||
String serie, String especie, String codigoCliente, String filialCliente, String condicaoPagamento,
|
||
String valorTotalDocumanto, String valorItens, String aliquota, String imposto, String valorPIS,
|
||
String valorCofins, String valorCSLL, String totalICMSIsento, String totalICMSNaotributado, String origenUf,
|
||
String origen, String destinoUf, String destino, String subSerie, String aidf, String status,
|
||
String motivocancelacion, String valorMulta, String brancos, String claseServicio, String sequencial) {
|
||
|
||
StringBuilder header = new StringBuilder();
|
||
header.append(identificador);
|
||
header.append(data);
|
||
header.append(cnpjImpressora);
|
||
header.append(coo);
|
||
header.append(serie);
|
||
header.append(especie);
|
||
header.append(codigoCliente);
|
||
header.append(filialCliente);
|
||
header.append(condicaoPagamento);
|
||
header.append(valorTotalDocumanto);
|
||
header.append(valorItens);
|
||
header.append(aliquota);
|
||
header.append(imposto);
|
||
header.append(valorPIS);
|
||
header.append(valorCofins);
|
||
header.append(valorCSLL);
|
||
header.append(totalICMSIsento);
|
||
header.append(totalICMSNaotributado);
|
||
header.append(origenUf);
|
||
header.append(origen);
|
||
header.append(destinoUf);
|
||
header.append(destino);
|
||
header.append(subSerie == null ? "" : subSerie);
|
||
header.append(aidf == null ? "" : aidf);
|
||
header.append(status);
|
||
header.append(motivocancelacion == null ? "" : motivocancelacion);
|
||
header.append(valorMulta == null ? "" : valorMulta);
|
||
header.append(brancos);
|
||
header.replace(226, 256, claseServicio);
|
||
|
||
header.append(sequencial);
|
||
|
||
return header.toString();
|
||
}
|
||
|
||
private String itensDocumentoFiscal(String identificador, String numItem, String cfop, String codigoTES,
|
||
String codProduto, String unidade, String quantidade, String valorUnitario, String valorTotal,
|
||
String aliquotaItem, String impostoItem, String valorPIS, String aliquotaPIS, String aliquotaCofins,
|
||
String valorCofins, String aliquotaCSLL, String valorCSLL, String aliquotaISS, String valorISS,
|
||
String totalICMSIsentoItem, String totalICMSNaotributadoItem, String numeroPDV, String numeroSeriePDV,
|
||
String modeloImpressora, String numCaixa, String status, String coo, String serie,
|
||
String brancos, String sequencial) {
|
||
|
||
StringBuilder item = new StringBuilder();
|
||
item.append(identificador);
|
||
item.append(numItem);
|
||
item.append(cfop);
|
||
item.append(codigoTES);
|
||
item.append(codProduto);
|
||
item.append(unidade);
|
||
item.append(quantidade);
|
||
item.append(valorUnitario);
|
||
item.append(valorTotal);
|
||
item.append(aliquotaItem);
|
||
item.append(impostoItem);
|
||
item.append(aliquotaPIS);
|
||
item.append(valorPIS);
|
||
item.append(aliquotaCofins);
|
||
item.append(valorCofins);
|
||
item.append(aliquotaCSLL);
|
||
item.append(valorCSLL);
|
||
item.append(aliquotaISS);
|
||
item.append(valorISS);
|
||
item.append(totalICMSIsentoItem);
|
||
item.append(totalICMSNaotributadoItem);
|
||
item.append(numeroPDV == null ? "" : numeroPDV);
|
||
item.append(numeroSeriePDV == null ? "" : numeroSeriePDV);
|
||
item.append(modeloImpressora);
|
||
item.append(numCaixa);
|
||
item.append(status);
|
||
item.append(coo == null ? "" : coo);
|
||
item.append(serie == null ? "" : serie);
|
||
item.append(brancos);
|
||
item.append(sequencial);
|
||
|
||
return item.toString();
|
||
}
|
||
|
||
private String fechamentoDeArquivo(String identificador, String qtdeDocGerados, String qtdeItensDocGerados,
|
||
String brancos, String sequencial) {
|
||
|
||
StringBuilder fechamento = new StringBuilder();
|
||
fechamento.append(identificador);
|
||
fechamento.append(qtdeDocGerados == null ? "" : qtdeDocGerados);
|
||
fechamento.append(qtdeItensDocGerados == null ? "" : qtdeItensDocGerados);
|
||
fechamento.append(brancos);
|
||
fechamento.append(sequencial);
|
||
|
||
return fechamento.toString();
|
||
}
|
||
|
||
@Override
|
||
public File importacionNaoFiscal(Date inicio, Date fim, Empresa empresa) {
|
||
try {
|
||
List<ImportacionNaoFiscalVO> importacionNaoFiscalVOs = fiscalDAO.buscaDatosNaoFiscais(inicio, fim, empresa.getEmpresaId());
|
||
return new ArchivoIntegracionNaoFiscal().gerarArquivo(empresa.getCnpj(), importacionNaoFiscalVOs);
|
||
} catch (Exception e) {
|
||
log.error(e.getMessage(), e);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
@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;
|
||
}
|
||
|
||
}
|