768 lines
40 KiB
Java
768 lines
40 KiB
Java
package com.rjconsultores.ventaboletos.dao.hibernate;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.apache.log4j.Logger;
|
|
import org.hibernate.SessionFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
|
|
import org.springframework.stereotype.Repository;
|
|
|
|
import com.rjconsultores.ventaboletos.dao.BpeDAO;
|
|
import com.rjconsultores.ventaboletos.entidad.Estado;
|
|
import com.rjconsultores.ventaboletos.utilerias.DateUtil;
|
|
import com.rjconsultores.ventaboletos.utilerias.exportacao.bpe.ExportacaoBPEBase;
|
|
import com.rjconsultores.ventaboletos.utilerias.exportacao.bpe.ExportacaoBPEVo;
|
|
import com.rjconsultores.ventaboletos.utilerias.exportacao.bpe.ExportacaoBPEVo.TipoComp;
|
|
import com.rjconsultores.ventaboletos.utilerias.fiscal.vo.DetalhadoFiscal;
|
|
import com.rjconsultores.ventaboletos.utilerias.fiscal.vo.FiscalRdi;
|
|
import com.rjconsultores.ventaboletos.utilerias.fiscal.vo.RdiValidacion;
|
|
import com.rjconsultores.ventaboletos.web.utilerias.NamedParameterStatement;
|
|
|
|
import br.inf.portalfiscal.bpe.TBPe;
|
|
|
|
@Repository("bpeDAO")
|
|
public class BpeHibernateDAO extends HibernateDaoSupport implements BpeDAO {
|
|
private static Logger log = Logger.getLogger(BpeHibernateDAO.class);
|
|
|
|
private static final String DATE_FORMAT_DMY = "dd/MM/yyyy";
|
|
|
|
@Autowired
|
|
public BpeHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) {
|
|
setSessionFactory(factory);
|
|
}
|
|
|
|
@Override
|
|
public ExportacaoBPEVo buscarRegistroExportacaoBpe(Connection connection, Date inicio, Date fim, Integer empresaId, String cveestado) {
|
|
ExportacaoBPEVo exportacaoBPEVo = new ExportacaoBPEVo();
|
|
|
|
List<ExportacaoBPEBase> registros = new ArrayList<ExportacaoBPEBase>();
|
|
|
|
StringBuilder sql = new StringBuilder();
|
|
sql.append("select distinct ");
|
|
sql.append(" c.boleto_id, ");
|
|
sql.append(" coalesce(c.num_bpe, c.numfoliosistema) as num_bpe, ");
|
|
sql.append(" c.numfoliosistema as numfoliosistema, ");
|
|
sql.append(" coalesce(c.preciopagado,0) + coalesce(c.importetaxaembarque,0) + coalesce(c.importeseguro,0) + coalesce(c.importepedagio,0) + coalesce(c.importeoutros,0) as valortotal, ");
|
|
sql.append(" bpe.codstat as codstatus, ");
|
|
sql.append(" DBMS_LOB.substr(coalesce(bpe.xmlregular, bpe.xmlcontingencia), 3000) as xml_bpe, ");
|
|
sql.append(" DBMS_LOB.substr(coalesce(bpe.xmlregular, bpe.xmlcontingencia), 3000, 3001) as xml_bpe_2, ");
|
|
sql.append(" DBMS_LOB.substr(coalesce(bpe.xmlregular, bpe.xmlcontingencia), 3000, 6001) as xml_bpe_3, ");
|
|
sql.append(" bpe.tipoevento as tipoevento, ");
|
|
sql.append(" bpe.desconto as v_desconto, ");
|
|
sql.append(" bpe.chbpe, ");
|
|
sql.append(" coalesce(chbpe_substituicao, ' ') as chbpe_sub, ");
|
|
sql.append(" to_char(c.feccreacion, 'yyyymmdd') as dhrecbto, ");
|
|
sql.append(" coalesce(to_char(c.fechorviaje, 'yyyymmdd'), to_char(to_timestamp_tz(bpe.dhvalidade, 'YYYY-MM-DD" + "\"T\"" + "HH24:MI:SSTZH:TZM'), 'yyyymmdd')) as dh_emisao, ");
|
|
sql.append(" coalesce(bpe.tiposubstituicao, '0') as tp_bpe, ");
|
|
sql.append(" bpe.retorno_id as retorno, ");
|
|
sql.append(" bpe.nprot, ");
|
|
sql.append(" coalesce(c.numserie_bpe, '1') as numserie_bpe, ");
|
|
sql.append(" c.categoria_id, ");
|
|
sql.append(" e.codestabelecimento as cod_estabelecimento, ");
|
|
sql.append(" ep.crtbpe as crt, ");
|
|
sql.append(" co.codibge as municipio_origem, ");
|
|
sql.append(" eo.cveestado as estado_origem, ");
|
|
sql.append(" cd.codibge as municipio_destino, ");
|
|
sql.append(" ed.cveestado as estado_destino, ");
|
|
sql.append(" ((case when coalesce(r.indtribtarifa,0) = 1 then c.preciopagado else 0 end) + (case when coalesce(r.indtribtaxaembarque,0) = 1 then c.importetaxaembarque else 0 end) + (case when coalesce(r.indtribpedagio,0) = 1 then c.importepedagio else 0 end)) as valortributado, ");
|
|
sql.append(" ei.porcredbaseicms as red_base_calc_estadual, ");
|
|
sql.append(" ei.porcredbaseicmsim as red_base_calc_municipal, ");
|
|
sql.append(" ei.porcredmunicipal as porc_red_municipal, ");
|
|
sql.append(" ei.porcredestadual as porc_red_estadual, ");
|
|
sql.append(" ei.tributacaoimportacao as tributacao_importacao, ");
|
|
sql.append(" ei.codigocontabilestadual as codigocontabil_estadual, ");
|
|
sql.append(" ei.codigocontabilmunicipal as codigocontabil_municipal ");
|
|
sql.append("from boleto c ");
|
|
sql.append(" join bpe bpe on c.boleto_id = bpe.boleto_id and bpe.activo = 1 ");
|
|
sql.append(" join estado e on e.codibge = bpe.uf ");
|
|
sql.append(" join marca ma on c.marca_id = ma.marca_id ");
|
|
sql.append(" join empresa ep on ma.empresa_id = ep.empresa_id ");
|
|
sql.append(" join ruta r on r.ruta_id = c.ruta_id ");
|
|
sql.append(" left join alias_servico ase on ase.origen_id = c.origen_id ");
|
|
sql.append(" and ase.destino_id = c.destino_id and ase.ruta_id = c.ruta_id and ase.activo = 1 and ase.corrida_id is null ");
|
|
sql.append(" inner join parada o on coalesce(ase.aliasorigen_id, c.origen_id) = o.parada_id ");
|
|
sql.append(" inner join ciudad co on o.ciudad_id = co.ciudad_id ");
|
|
sql.append(" inner join estado eo on eo.estado_id = co.estado_id ");
|
|
sql.append(" inner join parada d on coalesce(ase.aliasdestino_id, c.destino_id) = d.parada_id ");
|
|
sql.append(" inner join ciudad cd on d.ciudad_id = cd.ciudad_id ");
|
|
sql.append(" inner join estado ed on ed.estado_id = cd.estado_id ");
|
|
sql.append(" left join empresa_imposto ei on ei.empresa_id = ep.empresa_id and ei.estado_id = e.estado_id ");
|
|
sql.append("where c.activo = 1 and e.activo = 1 and ei.activo = 1 ");
|
|
sql.append(" and bpe.tipoamb = 1 and ep.activo = 1 ");
|
|
sql.append(" and bpe.codstat in (100,135,150) ");
|
|
sql.append(" and (bpe.tipoevento <> 110115 or bpe.tipoevento is null) ");
|
|
sql.append(" and (case when ei.indtribviagem = 1 then c.fechorviaje else c.feccreacion end) >= to_date(?,'DD/MM/YYYY HH24:MI:SS') ");
|
|
sql.append(" and (case when ei.indtribviagem = 1 then c.fechorviaje else c.feccreacion end) <= to_date(?,'DD/MM/YYYY HH24:MI:SS')");
|
|
sql.append(" and ep.empresa_id = ? ");
|
|
sql.append(" and e.cveestado = ? ");
|
|
sql.append("order by to_number(c.numfoliosistema), to_number(num_bpe), dhrecbto ");
|
|
|
|
try {
|
|
PreparedStatement ps = connection.prepareStatement(sql.toString());
|
|
ps.setString(1, DateUtil.getStringDate(inicio, DATE_FORMAT_DMY) + " 00:00:00");
|
|
ps.setString(2, DateUtil.getStringDate(fim, DATE_FORMAT_DMY) + " 23:59:59");
|
|
ps.setLong(3, Long.valueOf(empresaId));
|
|
ps.setString(4, cveestado);
|
|
|
|
ResultSet rs = ps.executeQuery();
|
|
|
|
while (rs.next()) {
|
|
BigDecimal valortributado = BigDecimal.ZERO;
|
|
BigDecimal valortotal = BigDecimal.ZERO;
|
|
|
|
if (rs.getBigDecimal("valortributado") != null) {
|
|
valortributado = rs.getBigDecimal("valortributado");
|
|
}
|
|
|
|
if (rs.getBigDecimal("valortotal") != null) {
|
|
valortotal = rs.getBigDecimal("valortotal");
|
|
}
|
|
|
|
ExportacaoBPEBase registro = new ExportacaoBPEBase();
|
|
registro.setBoletoId(rs.getLong("boleto_id"));
|
|
registro.setNumfoliosistema(rs.getString("numfoliosistema"));
|
|
registro.setvDesconto(rs.getBigDecimal("v_desconto"));
|
|
registro.setChbpe(rs.getString("chbpe"));
|
|
registro.setChbpeSub(rs.getString("chbpe_sub"));
|
|
registro.setDhrecbto(rs.getString("dhrecbto"));
|
|
registro.setDhEmisao(rs.getString("dh_emisao"));
|
|
registro.setTpBpe(rs.getString("tp_bpe"));
|
|
registro.setRetorno(rs.getString("retorno"));
|
|
registro.setNprot(rs.getString("nprot"));
|
|
registro.setNumBpe(rs.getString("num_bpe"));
|
|
registro.setNumSerieBpe(rs.getString("numserie_bpe"));
|
|
registro.setCodEstabelecimento(rs.getString("cod_estabelecimento"));
|
|
registro.setRedBaseCalcEstadual(rs.getBigDecimal("red_base_calc_estadual"));
|
|
registro.setRedBaseCalcMunicipal(rs.getBigDecimal("red_base_calc_municipal"));
|
|
registro.setPorcRedMunicipal(rs.getBigDecimal("porc_red_municipal"));
|
|
registro.setPorcRedEstadual(rs.getBigDecimal("porc_red_estadual"));
|
|
registro.setTributacaoImportacao(rs.getBigDecimal("tributacao_importacao"));
|
|
registro.setCodstatus(rs.getString("codstatus"));
|
|
registro.setTipoevento(rs.getString("tipoevento"));
|
|
registro.setCodigoContabilEstadual(rs.getString("codigocontabil_estadual"));
|
|
registro.setCodigocontabilMunicipal(rs.getString("codigocontabil_municipal"));
|
|
registro.setMunicipioOrigem(rs.getString("municipio_origem"));
|
|
registro.setEstadoOrigem(rs.getString("estado_origem"));
|
|
registro.setMunicipioDestino(rs.getString("municipio_destino"));
|
|
registro.setEstadoDestino(rs.getString("estado_destino"));
|
|
registro.setCrt(rs.getInt("crt"));
|
|
registro.setValortributado(valortributado);
|
|
registro.setValortotal(valortotal);
|
|
|
|
String xml_bpe = rs.getNString("xml_bpe");
|
|
String xml_bpe_2 = rs.getNString("xml_bpe_2");
|
|
String xml_bpe_3 = rs.getNString("xml_bpe_3");
|
|
registro.setXml(StringUtils.join(new String[] { xml_bpe, xml_bpe_2, xml_bpe_3 }));
|
|
|
|
if (exportacaoBPEVo.isBpeCanceladoENaoEmbarque(registro)) {
|
|
String xmlAnterior = buscarXmlBPE(connection, (StringUtils.isBlank(registro.getChbpeSub()) ? registro.getChbpe() : registro.getChbpeSub()));
|
|
registro.setXmlAnterior(xmlAnterior);
|
|
registro.setXml(null);
|
|
}
|
|
|
|
registros.add(registro);
|
|
}
|
|
|
|
rs.close();
|
|
ps.close();
|
|
|
|
} catch (Exception e) {
|
|
log.error(e.getMessage(), e);
|
|
}
|
|
|
|
List<ExportacaoBPEBase> aux = new ArrayList<ExportacaoBPEBase>();
|
|
|
|
for (ExportacaoBPEBase e1 : registros) {
|
|
if (exportacaoBPEVo.isBpeCanceladoENaoEmbarque(e1)) {
|
|
for (ExportacaoBPEBase e2 : registros) {
|
|
if (e1.getChbpe().equals(e2.getChbpe()) && !exportacaoBPEVo.isBpeCanceladoENaoEmbarque(e2)) {
|
|
aux.add(e1);
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
|
|
boolean temItemCancelado = false;
|
|
for (ExportacaoBPEBase e2 : registros) {
|
|
|
|
if (e1.getChbpe().equals(e2.getChbpe()) && exportacaoBPEVo.isBpeCanceladoENaoEmbarque(e2)) {
|
|
temItemCancelado = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!temItemCancelado) {
|
|
aux.add(e1);
|
|
}
|
|
}
|
|
}
|
|
|
|
for (ExportacaoBPEBase registro : aux) {
|
|
exportacaoBPEVo.gerarListas(registro);
|
|
}
|
|
|
|
return exportacaoBPEVo;
|
|
}
|
|
|
|
@Override
|
|
public String buscarXmlBPE(Connection connection, String chBpe) {
|
|
|
|
String xmlBpe = null;
|
|
StringBuilder sql = new StringBuilder();
|
|
sql.append(" select distinct ");
|
|
sql.append(" DBMS_LOB.substr(bpe.xmlregular, 3000) as xml_bpe, ");
|
|
sql.append(" DBMS_LOB.substr(bpe.xmlregular, 3000, 3001) as xml_bpe_2, ");
|
|
sql.append(" DBMS_LOB.substr(bpe.xmlregular, 3000, 6001) as xml_bpe_3 ");
|
|
sql.append(" from bpe bpe ");
|
|
sql.append(" where bpe.activo = 1 ");
|
|
sql.append(" and bpe.tipoamb = 1 ");
|
|
sql.append(" and bpe.codstat in (100) ");
|
|
sql.append(" and bpe.chbpe = ? ");
|
|
|
|
try {
|
|
PreparedStatement ps = connection.prepareStatement(sql.toString());
|
|
ps.setString(1, chBpe);
|
|
|
|
ResultSet rs = ps.executeQuery();
|
|
|
|
while (rs.next()) {
|
|
String xml_bpe = rs.getNString("xml_bpe");
|
|
String xml_bpe_2 = rs.getNString("xml_bpe_2");
|
|
String xml_bpe_3 = rs.getNString("xml_bpe_3");
|
|
xmlBpe = StringUtils.join(new String[] { xml_bpe, xml_bpe_2, xml_bpe_3 });
|
|
}
|
|
|
|
rs.close();
|
|
ps.close();
|
|
|
|
} catch (Exception e) {
|
|
log.error(e.getMessage(), e);
|
|
}
|
|
return xmlBpe;
|
|
}
|
|
|
|
@Override
|
|
public List<FiscalRdi> buscarBpeRDI(Connection connection, Date inicio, Date fim, Integer empresaId, List<Estado> estados, boolean isReceitaTerceiros) {
|
|
|
|
String ufs = null;
|
|
for (Estado estado : estados) {
|
|
if (ufs == null)
|
|
ufs = "'" + estado.getCveestado() + "'";
|
|
else
|
|
ufs = ufs + ",'" + estado.getCveestado() + "'";
|
|
}
|
|
|
|
List<DetalhadoFiscal> list = montaRelatorioRDI(connection, inicio, fim, empresaId, ufs);
|
|
List<FiscalRdi> rdis = calcularRelatorioRDI(list, isReceitaTerceiros);
|
|
return agrupaRdi(rdis);
|
|
}
|
|
|
|
private List<FiscalRdi> agrupaRdi(List<FiscalRdi> list) {
|
|
List<FiscalRdi> aux = new ArrayList<FiscalRdi>();
|
|
|
|
FiscalRdi rdi = null;
|
|
for (FiscalRdi detail : list) {
|
|
|
|
FiscalRdi atual = new FiscalRdi(detail.getEstado(), detail.getTipoViagem(), detail.getTipoReceita());
|
|
if (!aux.contains(atual)) {
|
|
rdi = new FiscalRdi(detail.getEstado(), detail.getTipoViagem(), detail.getTipoReceita());
|
|
aux.add(rdi);
|
|
}
|
|
|
|
int index = aux.indexOf(atual);
|
|
FiscalRdi agr = aux.get(index);
|
|
montarParametros(agr, detail);
|
|
aux.set(index, agr);
|
|
}
|
|
|
|
List<RdiValidacion> validacion = new ArrayList<RdiValidacion>();
|
|
for (FiscalRdi detail : aux) {
|
|
|
|
RdiValidacion rv = new RdiValidacion(detail.getEstado());
|
|
if (!validacion.contains(rv)) {
|
|
rv = new RdiValidacion(detail.getEstado());
|
|
validacion.add(rv);
|
|
}
|
|
|
|
int indice = validacion.indexOf(rv);
|
|
RdiValidacion rvAux = validacion.get(indice);
|
|
|
|
if (detail.getTipoReceita().equals("REC") && detail.getTipoViagem().equals("IM"))
|
|
rvAux.setTemIMRec(true);
|
|
|
|
if (detail.getTipoReceita().equals("DEV") && detail.getTipoViagem().equals("IM"))
|
|
rvAux.setTemIMDev(true);
|
|
|
|
if (detail.getTipoReceita().equals("REC") && detail.getTipoViagem().equals("IE"))
|
|
rvAux.setTemIERec(true);
|
|
|
|
if (detail.getTipoReceita().equals("DEV") && detail.getTipoViagem().equals("IE"))
|
|
rvAux.setTemIEDev(true);
|
|
|
|
validacion.set(indice, rvAux);
|
|
}
|
|
|
|
for (RdiValidacion val : validacion) {
|
|
|
|
if (!val.isTemIMRec()) {
|
|
FiscalRdi rdiAux = new FiscalRdi(val.getEstado(), "IM", "REC");
|
|
aux.add(montarParametros(rdiAux, new FiscalRdi()));
|
|
}
|
|
|
|
if (!val.isTemIMDev()) {
|
|
FiscalRdi rdiAux = new FiscalRdi(val.getEstado(), "IM", "DEV");
|
|
aux.add(montarParametros(rdiAux, new FiscalRdi()));
|
|
}
|
|
|
|
if (!val.isTemIERec()) {
|
|
FiscalRdi rdiAux = new FiscalRdi(val.getEstado(), "IE", "REC");
|
|
aux.add(montarParametros(rdiAux, new FiscalRdi()));
|
|
}
|
|
|
|
if (!val.isTemIEDev()) {
|
|
FiscalRdi rdiAux = new FiscalRdi(val.getEstado(), "IE", "DEV");
|
|
aux.add(montarParametros(rdiAux, new FiscalRdi()));
|
|
}
|
|
}
|
|
|
|
Collections.sort(aux);
|
|
return aux;
|
|
}
|
|
|
|
private FiscalRdi montarParametros(FiscalRdi agr, FiscalRdi detail) {
|
|
agr.setQuantBilhetes(agr.getQuantBilhetes().add(detail.getTarifa() == null ? BigDecimal.ZERO : BigDecimal.ONE));
|
|
agr.setTarifa(agr.getTarifa().add(detail.getTarifa() == null ? BigDecimal.ZERO : detail.getTarifa()));
|
|
agr.setSeguro(agr.getSeguro().add(detail.getSeguro() == null ? BigDecimal.ZERO : detail.getSeguro()));
|
|
agr.setTxEmbarque(agr.getTxEmbarque().add(detail.getTxEmbarque() == null ? BigDecimal.ZERO : detail.getTxEmbarque()));
|
|
agr.setPedagio(agr.getPedagio().add(detail.getPedagio() == null ? BigDecimal.ZERO : detail.getPedagio()));
|
|
agr.setPassagem(agr.getPassagem().add(detail.getPassagem() == null ? BigDecimal.ZERO : detail.getPassagem()));
|
|
agr.setIsentos(agr.getIsentos().add(detail.getIsentos() == null ? BigDecimal.ZERO : detail.getIsentos()));
|
|
agr.setOutros(agr.getOutros().add(detail.getOutros() == null ? BigDecimal.ZERO : detail.getOutros()));
|
|
agr.setRbc(detail.getRbc() == null ? BigDecimal.ZERO : detail.getRbc());
|
|
agr.setBaseCalculo(agr.getBaseCalculo().add(detail.getBaseCalculo() == null ? BigDecimal.ZERO : detail.getBaseCalculo()));
|
|
agr.setAliquota(detail.getAliquota() == null ? BigDecimal.ZERO : detail.getAliquota());
|
|
agr.setIcms(agr.getIcms().add(detail.getIcms() == null ? BigDecimal.ZERO : detail.getIcms()));
|
|
agr.setCp(detail.getCp() == null ? BigDecimal.ZERO : detail.getCp());
|
|
agr.setCredPres(agr.getCredPres().add(detail.getCredPres() == null ? BigDecimal.ZERO : detail.getCredPres()));
|
|
agr.setIcmsRec(agr.getIcmsRec().add(detail.getIcmsRec() == null ? BigDecimal.ZERO : detail.getIcmsRec()));
|
|
return agr;
|
|
}
|
|
|
|
private List<FiscalRdi> calcularRelatorioRDI(List<DetalhadoFiscal> list, boolean isReceitaTerceiros) {
|
|
|
|
List<FiscalRdi> rdis = new ArrayList<FiscalRdi>();
|
|
|
|
BigDecimal aliquotaAnt = null;
|
|
for (DetalhadoFiscal det : list) {
|
|
|
|
String xml = det.getXml();
|
|
|
|
BigDecimal tarifa = BigDecimal.ZERO;
|
|
BigDecimal taxaEmbarque = BigDecimal.ZERO;
|
|
BigDecimal pedagio = BigDecimal.ZERO;
|
|
BigDecimal seguro = BigDecimal.ZERO;
|
|
BigDecimal passagem = BigDecimal.ZERO;
|
|
BigDecimal isentos = BigDecimal.ZERO;
|
|
|
|
boolean isEstadual = det.isInterEstadual();
|
|
String tipoViagem = isEstadual ? "IE" : "IM";
|
|
|
|
ExportacaoBPEVo exportacaoBPEVo = new ExportacaoBPEVo();
|
|
TBPe tbpe = null;
|
|
if (StringUtils.isNotBlank(xml))
|
|
tbpe = exportacaoBPEVo.convertXmlToBpe(xml);
|
|
|
|
String vBC = null;
|
|
String pICMS = null;
|
|
String vICMS = null;
|
|
|
|
if (StringUtils.isNotBlank(xml)) {
|
|
if (tbpe.getInfBPe().getImp().getICMS().getICMS00() != null) {
|
|
vBC = tbpe.getInfBPe().getImp().getICMS().getICMS00().getVBC();
|
|
pICMS = tbpe.getInfBPe().getImp().getICMS().getICMS00().getPICMS();
|
|
vICMS = tbpe.getInfBPe().getImp().getICMS().getICMS00().getVICMS();
|
|
} else if (tbpe.getInfBPe().getImp().getICMS().getICMS20() != null) {
|
|
vBC = tbpe.getInfBPe().getImp().getICMS().getICMS20().getVBC();
|
|
pICMS = tbpe.getInfBPe().getImp().getICMS().getICMS20().getPICMS();
|
|
vICMS = tbpe.getInfBPe().getImp().getICMS().getICMS20().getVICMS();
|
|
}
|
|
|
|
if (tbpe.getInfBPe().getInfValorBPe() != null) {
|
|
|
|
String vPgto = tbpe.getInfBPe().getInfValorBPe().getVPgto();
|
|
passagem = new BigDecimal(vPgto == null ? "0.0" : vPgto);
|
|
|
|
for (TBPe.InfBPe.InfValorBPe.Comp comp : tbpe.getInfBPe().getInfValorBPe().getComp()) {
|
|
if (comp.getTpComp().equals(TipoComp.TARIFA)) {
|
|
String vDesconto = tbpe.getInfBPe().getInfValorBPe().getVDesconto();
|
|
tarifa = new BigDecimal(comp.getVComp() == null ? "0.0" : comp.getVComp());
|
|
tarifa = tarifa.subtract(new BigDecimal(vDesconto == null ? "0.0" : vDesconto));
|
|
}
|
|
|
|
if (comp.getTpComp().equals(TipoComp.PEDAGIO)) {
|
|
isentos = isentos.add(isEstadual && !det.getIndPedagioEstdual() ? new BigDecimal(comp.getVComp() == null ? "0.0" : comp.getVComp()) : BigDecimal.ZERO);
|
|
isentos = isentos.add(!isEstadual && !det.getIndPedagioMunicipal() ? new BigDecimal(comp.getVComp() == null ? "0.0" : comp.getVComp()) : BigDecimal.ZERO);
|
|
|
|
pedagio = isReceitaTerceiros ? new BigDecimal(comp.getVComp() == null ? "0.0" : comp.getVComp()) : BigDecimal.ZERO;
|
|
}
|
|
|
|
if (comp.getTpComp().equals(TipoComp.TAXA_EMBARQUE)) {
|
|
isentos = isentos.add(isEstadual && !det.getIndTxembarqueEstadual() ? new BigDecimal(comp.getVComp() == null ? "0.0" : comp.getVComp()) : BigDecimal.ZERO);
|
|
isentos = isentos.add(!isEstadual && !det.getIndTxembarqueMunicipal() ? new BigDecimal(comp.getVComp() == null ? "0.0" : comp.getVComp()) : BigDecimal.ZERO);
|
|
|
|
taxaEmbarque = isReceitaTerceiros ? new BigDecimal(comp.getVComp() == null ? "0.0" : comp.getVComp()) : BigDecimal.ZERO;
|
|
}
|
|
|
|
if (comp.getTpComp().equals(TipoComp.SEGURO)) {
|
|
isentos = isentos.add(isEstadual && !det.getIndSeguroEstadual() ? new BigDecimal(comp.getVComp() == null ? "0.0" : comp.getVComp()) : BigDecimal.ZERO);
|
|
isentos = isentos.add(!isEstadual && !det.getIndSeguroMunicipal() ? new BigDecimal(comp.getVComp() == null ? "0.0" : comp.getVComp()) : BigDecimal.ZERO);
|
|
|
|
seguro = isReceitaTerceiros ? new BigDecimal(comp.getVComp() == null ? "0.0" : comp.getVComp()) : BigDecimal.ZERO;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
BigDecimal baseCalculo = new BigDecimal(vBC == null ? "0.0" : vBC);
|
|
|
|
BigDecimal aliquota = new BigDecimal(pICMS == null ? "0.0" : pICMS);
|
|
aliquota = (aliquota.intValue() == 0 ? (aliquotaAnt == null ? aliquota : aliquotaAnt) : aliquota);
|
|
if (aliquota != null && aliquota.intValue() > 0)
|
|
aliquotaAnt = aliquota;
|
|
|
|
BigDecimal icms = new BigDecimal(vICMS == null ? "0.0" : vICMS);
|
|
|
|
BigDecimal cp = null;
|
|
if (isEstadual) {
|
|
cp = det.getPorcRedEstadual() == null ? BigDecimal.ZERO : det.getPorcRedEstadual();
|
|
} else {
|
|
cp = det.getPorcRedMunicipal() == null ? BigDecimal.ZERO : det.getPorcRedMunicipal();
|
|
}
|
|
|
|
BigDecimal credPres = icms.multiply(cp.divide(BigDecimal.TEN.multiply(BigDecimal.TEN)));
|
|
BigDecimal icmsRec = icms.subtract(credPres);
|
|
BigDecimal vRedBC = det.getRedBaseCalcIcms();
|
|
BigDecimal outros = isentos.equals(BigDecimal.ZERO) ? BigDecimal.ZERO : passagem.subtract(isentos).subtract(baseCalculo);
|
|
|
|
rdis.add(new FiscalRdi(det.getEstadoOrigem(), tipoViagem, det.getTipoReceita(), tarifa,
|
|
seguro, taxaEmbarque, pedagio, passagem, isentos, outros, vRedBC,
|
|
baseCalculo, aliquota, icms, cp, credPres, icmsRec));
|
|
}
|
|
|
|
return rdis;
|
|
}
|
|
|
|
private List<DetalhadoFiscal> montaRelatorioRDI(Connection connection, Date inicio, Date fim, Integer empresaId, String ufs) {
|
|
|
|
List<DetalhadoFiscal> list = new ArrayList<DetalhadoFiscal>();
|
|
try {
|
|
|
|
StringBuilder sql = new StringBuilder();
|
|
sql.append(sqlRelatorioRDIVendidos(ufs));
|
|
sql.append(" union all ");
|
|
sql.append(sqlRelatorioRDICancelados(ufs));
|
|
|
|
NamedParameterStatement ps = new NamedParameterStatement(connection, sql.toString());
|
|
ps.setLong("EMPRESA_ID", Long.valueOf(empresaId));
|
|
|
|
ps.setTimestamp("DATE_INICIO", new java.sql.Timestamp(DateUtil.inicioFecha(inicio).getTime()));
|
|
ps.setTimestamp("DATE_FIM", new java.sql.Timestamp(DateUtil.fimFecha(fim).getTime()));
|
|
|
|
ResultSet rs = ps.executeQuery();
|
|
while (rs.next()) {
|
|
|
|
String xml_bpe = rs.getNString("xml_bpe");
|
|
String xml_bpe_2 = rs.getNString("xml_bpe_2");
|
|
String xml_bpe_3 = rs.getNString("xml_bpe_3");
|
|
String xml = StringUtils.join(new String[] { xml_bpe, xml_bpe_2, xml_bpe_3 });
|
|
|
|
TBPe tbpe = null;
|
|
if (StringUtils.isNotBlank(xml))
|
|
tbpe = new ExportacaoBPEVo().convertXmlToBpe(xml);
|
|
|
|
DetalhadoFiscal df = new DetalhadoFiscal();
|
|
df.setXml(xml);
|
|
|
|
String municipioOrigem = StringUtils.isNotBlank(xml) ? tbpe.getInfBPe().getIde().getCMunIni() : rs.getString("municipioOrigem");
|
|
String estadoOrigem = (StringUtils.isNotBlank(xml) ? tbpe.getInfBPe().getIde().getUFIni().value() : rs.getString("estadoOrigem"));
|
|
String estadoDestino = (StringUtils.isNotBlank(xml) ? tbpe.getInfBPe().getIde().getUFFim().value() : rs.getString("estadoDestino"));
|
|
|
|
boolean isInterEstadual = !estadoOrigem.equals(estadoDestino);
|
|
df.setEstadoOrigem(estadoOrigem);
|
|
df.setEstadoDestino(estadoDestino);
|
|
df.setMunicipioOrigem(municipioOrigem);
|
|
df.setInterEstadual(isInterEstadual);
|
|
df.setCajaId(rs.getLong("cajaId"));
|
|
df.setTipoReceita(rs.getString("tipoReceita"));
|
|
df.setNumoperacion(rs.getString("numoperacion"));
|
|
df.setEstadoId(rs.getInt("estadoId"));
|
|
df.setFechorVenta(rs.getDate("fechorVenta"));
|
|
df.setIcmsInterestadual(rs.getBigDecimal("icmsInterestadual"));
|
|
df.setIcmsIntermunicipal(rs.getBigDecimal("icmsIntermunicipal"));
|
|
df.setIcmsInterestadual(rs.getBigDecimal("icmsInterestadual"));
|
|
df.setRedBaseCalcIcms(rs.getBigDecimal("redBaseCalcIcms"));
|
|
df.setPorcRedMunicipal(rs.getBigDecimal("porcRedMunicipal"));
|
|
df.setPorcRedEstadual(rs.getBigDecimal("porcRedEstadual"));
|
|
df.setTributacaoImportacao(rs.getBigDecimal("tributacaoImportacao"));
|
|
df.setIndTarifaMunicipal(rs.getBoolean("indtarifamunicipal"));
|
|
df.setIndSeguroMunicipal(rs.getBoolean("indseguromunicipal"));
|
|
df.setIndTxembarqueMunicipal(rs.getBoolean("indtxembarquemunicipal"));
|
|
df.setIndPedagioMunicipal(rs.getBoolean("indpedagiomunicipal"));
|
|
df.setIndTarifaEstadual(rs.getBoolean("indtarifaestadual"));
|
|
df.setIndSeguroEstadual(rs.getBoolean("indseguroestadual"));
|
|
df.setIndTxembarqueEstadual(rs.getBoolean("indtxembarqueestadual"));
|
|
df.setIndPedagioEstdual(rs.getBoolean("indpedagioestdual"));
|
|
df.setIsenta(rs.getBoolean("isenta"));
|
|
df.setTarifa(rs.getBigDecimal("tarifa"));
|
|
df.setTaxaEmbarque(rs.getBigDecimal("taxaEmbarque"));
|
|
df.setPedagio(rs.getBigDecimal("pedagio"));
|
|
df.setSeguro(rs.getBigDecimal("seguro"));
|
|
df.setOutros(rs.getBigDecimal("outros"));
|
|
df.setDatamov(rs.getString("datamov"));
|
|
list.add(df);
|
|
}
|
|
|
|
rs.close();
|
|
ps.close();
|
|
|
|
} catch (SQLException e) {
|
|
log.error("", e);
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
private String sqlRelatorioRDIVendidos(String ufs) {
|
|
|
|
StringBuilder sql = new StringBuilder();
|
|
sql.append("select distinct ");
|
|
sql.append(" c.boleto_id as cajaId, ");
|
|
sql.append(" 'REC' as tipoReceita, ");
|
|
sql.append(" c.numoperacion as numoperacion, ");
|
|
sql.append(" trunc(c.feccreacion) as fechorVenta, ");
|
|
sql.append(" e.estado_id as estadoId, ");
|
|
sql.append(" ei.icms as icmsInterestadual, ");
|
|
sql.append(" ei.icmsim as icmsIntermunicipal, ");
|
|
sql.append(" e.cveestado as estadoOrigem, ");
|
|
sql.append(" ed.cveestado as estadoDestino, ");
|
|
sql.append(" co.codibge as municipioOrigem, ");
|
|
sql.append(" case when (e.cveestado = ed.cveestado) then 0 else 1 end as isInterEstadual, ");
|
|
sql.append(" 0 as isenta, ");
|
|
sql.append(" ei.porcredbaseicms as redBaseCalcIcms, ");
|
|
sql.append(" ei.porcredmunicipal as porcRedMunicipal, ");
|
|
sql.append(" ei.porcredestadual as porcRedEstadual, ");
|
|
sql.append(" ei.indtarifamunicipal as indtarifamunicipal, ");
|
|
sql.append(" ei.indseguromunicipal as indseguromunicipal, ");
|
|
sql.append(" ei.indtxembarquemunicipal as indtxembarquemunicipal, ");
|
|
sql.append(" ei.indpedagiomunicipal as indpedagiomunicipal, ");
|
|
sql.append(" ei.indtarifaestadual as indtarifaestadual, ");
|
|
sql.append(" ei.indseguroestadual as indseguroestadual, ");
|
|
sql.append(" ei.indtxembarqueestadual as indtxembarqueestadual, ");
|
|
sql.append(" ei.indpedagioestdual as indpedagioestdual, ");
|
|
sql.append(" ei.tributacaoImportacao as tributacaoImportacao, ");
|
|
sql.append(" coalesce(c.preciopagado, 0) as tarifa, ");
|
|
sql.append(" coalesce(c.importetaxaembarque, 0) as taxaEmbarque, ");
|
|
sql.append(" coalesce(c.importepedagio, 0) as pedagio, ");
|
|
sql.append(" coalesce(c.importeseguro, 0) as seguro, ");
|
|
sql.append(" coalesce(c.importeoutros, 0) as outros, ");
|
|
sql.append(" to_char(trunc(c.feccreacion), 'yyyymmdd') as datamov, ");
|
|
sql.append(" DBMS_LOB.substr(coalesce(bpe.xmlregular, bpe.xmlcontingencia), 3000) as xml_bpe, ");
|
|
sql.append(" DBMS_LOB.substr(coalesce(bpe.xmlregular, bpe.xmlcontingencia), 3000, 3001) as xml_bpe_2, ");
|
|
sql.append(" DBMS_LOB.substr(coalesce(bpe.xmlregular, bpe.xmlcontingencia), 3000, 6001) as xml_bpe_3 ");
|
|
sql.append("from boleto c ");
|
|
sql.append(" join bpe bpe on c.boleto_id = bpe.boleto_id and bpe.activo = 1 ");
|
|
sql.append(" left join bpe bpecan on bpecan.chbpe = bpe.chbpe and bpecan.codstat = 135 and bpe.codstat = 100 ");
|
|
sql.append(" and (bpecan.tipoevento = 110111 or bpecan.tipoevento is null) and bpecan.activo = 1 ");
|
|
sql.append(" join estado e on e.codibge = bpe.uf ");
|
|
sql.append(" join marca ma on c.marca_id = ma.marca_id ");
|
|
sql.append(" join empresa ep on ma.empresa_id = ep.empresa_id ");
|
|
sql.append(" join ruta r on r.ruta_id = c.ruta_id ");
|
|
sql.append(" left join alias_servico ase on ase.origen_id = c.origen_id ");
|
|
sql.append(" and ase.destino_id = c.destino_id and ase.ruta_id = c.ruta_id and ase.activo = 1 and ase.corrida_id is null and ase.activo = 1 ");
|
|
sql.append(" inner join parada o on coalesce(ase.aliasorigen_id, c.origen_id) = o.parada_id ");
|
|
sql.append(" inner join ciudad co on o.ciudad_id = co.ciudad_id ");
|
|
sql.append(" inner join estado eo on eo.estado_id = co.estado_id ");
|
|
sql.append(" inner join parada d on coalesce(ase.aliasdestino_id, c.destino_id) = d.parada_id ");
|
|
sql.append(" inner join ciudad cd on d.ciudad_id = cd.ciudad_id ");
|
|
sql.append(" inner join estado ed on ed.estado_id = cd.estado_id ");
|
|
sql.append(" left join empresa_imposto ei on ei.empresa_id = ep.empresa_id and ei.estado_id = e.estado_id and ei.activo = 1 ");
|
|
sql.append("where c.activo = 1 and bpe.tipoamb = 1 and bpe.codstat in (100,150) ");
|
|
sql.append(" and (to_char(bpe.fecmodif, 'mm/yyyy') < to_char(bpecan.fecmodif, 'mm/yyyy') or bpecan.fecmodif is null) ");
|
|
sql.append(" and (case when ei.indtribviagem = 1 then c.fechorviaje else c.feccreacion end) >= :DATE_INICIO ");
|
|
sql.append(" and (case when ei.indtribviagem = 1 then c.fechorviaje else c.feccreacion end) <= :DATE_FIM ");
|
|
sql.append(" and ep.empresa_id = :EMPRESA_ID ");
|
|
sql.append(" and e.cveestado in ( ").append(ufs).append(" ) ");
|
|
|
|
return sql.toString();
|
|
}
|
|
|
|
private String sqlRelatorioRDICancelados(String ufs) {
|
|
|
|
StringBuilder sql = new StringBuilder();
|
|
sql.append("select distinct ");
|
|
sql.append(" b.boleto_id as cajaId, ");
|
|
sql.append(" 'DEV' as tipoReceita, ");
|
|
sql.append(" b.numoperacion as numoperacion, ");
|
|
sql.append(" trunc(bori.feccreacion) as fechorVenta, ");
|
|
sql.append(" est_bpe.estado_id as estadoId, ");
|
|
sql.append(" ei.icms as icmsInterestadual, ");
|
|
sql.append(" ei.icmsim as icmsIntermunicipal, ");
|
|
sql.append(" est_bpe.cveestado as estadoOrigem, ");
|
|
sql.append(" eds.cveestado as estadoDestino, ");
|
|
sql.append(" co.codibge as municipioOrigem, ");
|
|
sql.append(" case when (est_bpe.cveestado = eds.cveestado) then 0 else 1 end as isInterEstadual, ");
|
|
sql.append(" 0 as isenta, ");
|
|
sql.append(" ei.porcredbaseicms as redBaseCalcIcms, ");
|
|
sql.append(" ei.porcredmunicipal as porcRedMunicipal, ");
|
|
sql.append(" ei.porcredestadual as porcRedEstadual, ");
|
|
sql.append(" ei.indtarifamunicipal as indtarifamunicipal, ");
|
|
sql.append(" ei.indseguromunicipal as indseguromunicipal, ");
|
|
sql.append(" ei.indtxembarquemunicipal as indtxembarquemunicipal, ");
|
|
sql.append(" ei.indpedagiomunicipal as indpedagiomunicipal, ");
|
|
sql.append(" ei.indtarifaestadual as indtarifaestadual, ");
|
|
sql.append(" ei.indseguroestadual as indseguroestadual, ");
|
|
sql.append(" ei.indtxembarqueestadual as indtxembarqueestadual, ");
|
|
sql.append(" ei.indpedagioestdual as indpedagioestdual, ");
|
|
sql.append(" ei.tributacaoImportacao as tributacaoImportacao, ");
|
|
sql.append(" coalesce(b.preciopagado, 0) as tarifa, ");
|
|
sql.append(" coalesce(b.importetaxaembarque, 0) as taxaEmbarque, ");
|
|
sql.append(" coalesce(b.importepedagio, 0) as pedagio, ");
|
|
sql.append(" coalesce(b.importeseguro, 0) as seguro, ");
|
|
sql.append(" coalesce(b.importeoutros, 0) as outros, ");
|
|
sql.append(" to_char(trunc(bori.feccreacion), 'yyyymmdd') as datamov, ");
|
|
sql.append(" dbms_lob.substr((case when bpeori.tipoevento is not null then bpeant.xmlregular else bpeori.xmlregular end), 3000) as xml_bpe, ");
|
|
sql.append(" dbms_lob.substr((case when bpeori.tipoevento is not null then bpeant.xmlregular else bpeori.xmlregular end), 3000, 3001) as xml_bpe_2, ");
|
|
sql.append(" dbms_lob.substr((case when bpeori.tipoevento is not null then bpeant.xmlregular else bpeori.xmlregular end), 3000, 6001) as xml_bpe_3 ");
|
|
sql.append("from boleto bori ");
|
|
sql.append(" inner join marca m on m.marca_id = bori.marca_id and m.activo = 1 ");
|
|
sql.append(" inner join empresa e on e.empresa_id = m.empresa_id ");
|
|
sql.append(" inner join bpe bpe on bpe.boleto_id = bori.boleto_id and bpe.activo = 1 ");
|
|
sql.append(" inner join estado est_bpe on bpe.uf = est_bpe.codibge ");
|
|
sql.append(" left join bpe bpeori on bpeori.chbpe = bpe.chbpe_substituicao and bpeori.activo = 1 ");
|
|
sql.append(" left join boleto b on b.boleto_id = bpeori.boleto_id ");
|
|
sql.append(" inner join punto_venta ptv on ptv.puntoventa_id = b.puntoventa_id ");
|
|
sql.append(" inner join parada ori on (b.origen_id = ori.parada_id ) ");
|
|
sql.append(" inner join parada des on (b.destino_id = des.parada_id ) ");
|
|
sql.append(" inner join ciudad co on (co.ciudad_id = ori.ciudad_id ) ");
|
|
sql.append(" inner join ciudad cd on (cd.ciudad_id = des.ciudad_id ) ");
|
|
sql.append(" inner join estado est on est.estado_id = co.estado_id ");
|
|
sql.append(" left join alias_servico s on s.origen_id = b.origen_id ");
|
|
sql.append(" and s.destino_id = b.destino_id and (s.corrida_id = b.corrida_id or s.corrida_id is null) ");
|
|
sql.append(" and s.ruta_id = b.ruta_id ");
|
|
sql.append(" left join parada pos on pos.parada_id = s.aliasorigen_id ");
|
|
sql.append(" left join ciudad cos on cos.ciudad_id = pos.ciudad_id ");
|
|
sql.append(" left join estado eos on eos.estado_id = cos.estado_id ");
|
|
sql.append(" left join parada pds on pds.parada_id = s.aliasdestino_id ");
|
|
sql.append(" left join ciudad cds on cds.ciudad_id = pds.ciudad_id ");
|
|
sql.append(" left join estado eds on eds.estado_id = cds.estado_id ");
|
|
sql.append(" join inscricao_estadual ie on e.empresa_id = ie.empresa_id ");
|
|
sql.append(" and ie.estado_id = coalesce(eos.estado_id, est.estado_id) and ie.activo = 1 ");
|
|
sql.append(" join empresa_imposto ei on ei.empresa_id = e.empresa_id ");
|
|
sql.append(" and ei.estado_id = coalesce(eos.estado_id, est.estado_id) and ei.activo = 1 ");
|
|
sql.append(" left join aidf aidf on aidf.aidf_id = b.aidf_id and b.tipoventa_id = 3 ");
|
|
sql.append(" left join estado esaidf on esaidf.estado_id = aidf.estado_id ");
|
|
sql.append(" left join boleto bant on bant.boleto_id = (case when bpeori.tipoevento is not null then coalesce(b.boletoanterior_id, b.boletooriginal_id) else null end) ");
|
|
sql.append(" left join bpe bpeant on bpeant.boleto_id = bant.boleto_id ");
|
|
sql.append(" left join punto_venta ptvo on ptvo.puntoventa_id = coalesce(bant.puntoventa_id, bori.puntoventa_id,b.ptovtaventa_id) ");
|
|
sql.append("where ( (bpe.codstat in (100,135,150) and bpe.chbpe_substituicao is not null) ");
|
|
sql.append(" or (bpe.codstat = 135 and to_char(b.feccreacion, 'mm/yyyy') < to_char(bori.feccreacion, 'mm/yyyy')) ) ");
|
|
sql.append(" and (bpe.codstat in (100,135,150) and bpe.tipoevento <> 110115 or bpe.tipoevento is null) ");
|
|
sql.append(" and (bpeori.codstat in (100,135,150) and bpeori.tipoevento <> 110115 or bpeori.tipoevento is null) ");
|
|
sql.append(" and bori.feccreacion >= :DATE_INICIO ");
|
|
sql.append(" and bori.feccreacion <= :DATE_FIM ");
|
|
sql.append(" and est_bpe.cveestado in ( ").append(ufs).append(" ) ");
|
|
sql.append(" and e.empresa_id = :EMPRESA_ID ");
|
|
sql.append(" ");
|
|
sql.append(" union all ");
|
|
sql.append(" ");
|
|
sql.append("select distinct ");
|
|
sql.append(" b.boleto_id as cajaId, ");
|
|
sql.append(" 'DEV' as tipoReceita, ");
|
|
sql.append(" b.numoperacion as numoperacion, ");
|
|
sql.append(" trunc(bori.feccreacion) as fechorVenta, ");
|
|
sql.append(" est_bpe.estado_id as estadoId, ");
|
|
sql.append(" ei.icms as icmsInterestadual, ");
|
|
sql.append(" ei.icmsim as icmsIntermunicipal, ");
|
|
sql.append(" est_bpe.cveestado as estadoOrigem, ");
|
|
sql.append(" eds.cveestado as estadoDestino, ");
|
|
sql.append(" co.codibge as municipioOrigem, ");
|
|
sql.append(" case when (est_bpe.cveestado = eds.cveestado) then 0 else 1 end as isInterEstadual, ");
|
|
sql.append(" 0 as isenta, ");
|
|
sql.append(" ei.porcredbaseicms as redBaseCalcIcms, ");
|
|
sql.append(" ei.porcredmunicipal as porcRedMunicipal, ");
|
|
sql.append(" ei.porcredestadual as porcRedEstadual, ");
|
|
sql.append(" ei.indtarifamunicipal as indtarifamunicipal, ");
|
|
sql.append(" ei.indseguromunicipal as indseguromunicipal, ");
|
|
sql.append(" ei.indtxembarquemunicipal as indtxembarquemunicipal, ");
|
|
sql.append(" ei.indpedagiomunicipal as indpedagiomunicipal, ");
|
|
sql.append(" ei.indtarifaestadual as indtarifaestadual, ");
|
|
sql.append(" ei.indseguroestadual as indseguroestadual, ");
|
|
sql.append(" ei.indtxembarqueestadual as indtxembarqueestadual, ");
|
|
sql.append(" ei.indpedagioestdual as indpedagioestdual, ");
|
|
sql.append(" ei.tributacaoImportacao as tributacaoImportacao, ");
|
|
sql.append(" coalesce(b.preciopagado, 0) as tarifa, ");
|
|
sql.append(" coalesce(b.importetaxaembarque, 0) as taxaEmbarque, ");
|
|
sql.append(" coalesce(b.importepedagio, 0) as pedagio, ");
|
|
sql.append(" coalesce(b.importeseguro, 0) as seguro, ");
|
|
sql.append(" coalesce(b.importeoutros, 0) as outros, ");
|
|
sql.append(" to_char(trunc(bori.feccreacion), 'yyyymmdd') as datamov, ");
|
|
sql.append(" dbms_lob.substr((case when bpeori.tipoevento is not null then bpeant.xmlregular else bpeori.xmlregular end), 3000) as xml_bpe, ");
|
|
sql.append(" dbms_lob.substr((case when bpeori.tipoevento is not null then bpeant.xmlregular else bpeori.xmlregular end), 3000, 3001) as xml_bpe_2, ");
|
|
sql.append(" dbms_lob.substr((case when bpeori.tipoevento is not null then bpeant.xmlregular else bpeori.xmlregular end), 3000, 6001) as xml_bpe_3 ");
|
|
sql.append("from boleto bori ");
|
|
sql.append(" inner join marca m on m.marca_id = bori.marca_id and m.activo = 1 ");
|
|
sql.append(" inner join empresa e on e.empresa_id = m.empresa_id ");
|
|
sql.append(" inner join bpe bpe on bpe.boleto_id = bori.boleto_id and bpe.activo = 1 ");
|
|
sql.append(" inner join estado est_bpe on bpe.uf = est_bpe.codibge ");
|
|
sql.append(" left join boleto b on b.boleto_id = (case when bori.motivocancelacion_id = 99 then bori.boletoanterior_id else bori.boletooriginal_id end) ");
|
|
sql.append(" left join bpe bpeori on b.boleto_id = bpeori.boleto_id and bpeori.activo = 1 ");
|
|
sql.append(" inner join punto_venta ptv on ptv.puntoventa_id = bori.puntoventa_id ");
|
|
sql.append(" inner join parada ori on (b.origen_id = ori.parada_id ) ");
|
|
sql.append(" inner join parada des on (b.destino_id = des.parada_id ) ");
|
|
sql.append(" inner join ciudad co on (co.ciudad_id = ori.ciudad_id ) ");
|
|
sql.append(" inner join ciudad cd on (cd.ciudad_id = des.ciudad_id ) ");
|
|
sql.append(" inner join estado est on est.estado_id = co.estado_id ");
|
|
sql.append("left join alias_servico s on s.origen_id = b.origen_id ");
|
|
sql.append(" and s.destino_id = b.destino_id and (s.corrida_id = b.corrida_id or s.corrida_id is null) ");
|
|
sql.append(" and s.ruta_id = b.ruta_id and s.activo = 1 ");
|
|
sql.append("left join parada pos on pos.parada_id = s.aliasorigen_id ");
|
|
sql.append("left join ciudad cos on cos.ciudad_id = pos.ciudad_id ");
|
|
sql.append("left join estado eos on eos.estado_id = cos.estado_id ");
|
|
sql.append("left join parada pds on pds.parada_id = s.aliasdestino_id ");
|
|
sql.append("left join ciudad cds on cds.ciudad_id = pds.ciudad_id ");
|
|
sql.append("left join estado eds on eds.estado_id = cds.estado_id ");
|
|
sql.append("join inscricao_estadual ie on e.empresa_id = ie.empresa_id ");
|
|
sql.append(" and ie.estado_id = coalesce(eos.estado_id, est.estado_id) and ie.activo = 1 ");
|
|
sql.append("join empresa_imposto ei on ei.empresa_id = e.empresa_id ");
|
|
sql.append(" and ei.estado_id = coalesce(eos.estado_id, est.estado_id) and ei.activo = 1 ");
|
|
sql.append("left join aidf aidf on aidf.aidf_id = b.aidf_id and b.tipoventa_id = 3 ");
|
|
sql.append("left join estado esaidf on esaidf.estado_id = aidf.estado_id ");
|
|
sql.append("left join boleto bant on bant.boleto_id = (case when bpeori.tipoevento is not null then coalesce(b.boletoanterior_id, b.boletooriginal_id) else null end) ");
|
|
sql.append("left join bpe bpeant on bpeant.boleto_id = bant.boleto_id ");
|
|
sql.append("left join punto_venta ptvo on ptvo.puntoventa_id = coalesce(bant.puntoventa_id, bori.puntoventa_id,b.ptovtaventa_id) ");
|
|
sql.append("where ");
|
|
sql.append(" (bpe.codstat = 135 and to_char(b.feccreacion, 'mm/yyyy') < to_char(bori.feccreacion, 'mm/yyyy')) ");
|
|
sql.append(" and (bpe.codstat in (100,135,150) and bpe.tipoevento <> 110115 or bpe.tipoevento is null) ");
|
|
sql.append(" and (bpeori.codstat in (100,135,150) and bpeori.tipoevento <> 110115 or bpeori.tipoevento is null) ");
|
|
sql.append(" and bori.feccreacion >= :DATE_INICIO ");
|
|
sql.append(" and bori.feccreacion <= :DATE_FIM ");
|
|
sql.append(" and est_bpe.cveestado in ( ").append(ufs).append(" ) ");
|
|
sql.append(" and e.empresa_id = :EMPRESA_ID ");
|
|
|
|
sql.append("order by estadoId, isInterEstadual ");
|
|
|
|
return sql.toString();
|
|
}
|
|
|
|
}
|