Relatorio Taxa por Linha - Cálculo do ICMS a deduzir

git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Web/trunk/ventaboletos@30478 d1611594-4594-4d17-8e1d-87c2c4800839
master
julio 2013-09-02 18:39:30 +00:00
parent f08e654c03
commit d04d07e726
2 changed files with 170 additions and 9 deletions

View File

@ -37,8 +37,11 @@ public class RelatorioTaxasLinha extends Relatorio {
Connection conexao = this.relatorio.getConexao(); Connection conexao = this.relatorio.getConexao();
Map<String, Object> parametros = this.relatorio.getParametros(); Map<String, Object> parametros = this.relatorio.getParametros();
String puntosVentaIds = (String) parametros.get("NUMPUNTOVENTA"); String puntosVentaIds = (String) parametros.get("NUMPUNTOVENTA");
String sql = getSql(puntosVentaIds); String sql = getSql(puntosVentaIds);
System.out.println(sql);
NamedParameterStatement stmt = new NamedParameterStatement(conexao, sql); NamedParameterStatement stmt = new NamedParameterStatement(conexao, sql);
stmt.setInt("TIPO_DATA", (Integer) parametros.get("TIPO_DATA")); stmt.setInt("TIPO_DATA", (Integer) parametros.get("TIPO_DATA"));
@ -69,31 +72,31 @@ public class RelatorioTaxasLinha extends Relatorio {
if ((Boolean) this.relatorio.getParametros().get("B_SUBTRAIR_ICMS")) { if ((Boolean) this.relatorio.getParametros().get("B_SUBTRAIR_ICMS")) {
BigDecimal valorIcms = BigDecimal.ZERO; BigDecimal valorIcms = BigDecimal.ZERO;
String indInterestadual = this.resultSet.getString("INTERESTADUAL"); String indInterestadual = this.resultSet.getString("INTERESTADUAL");
Integer puntoVentaId = this.resultSet.getInt("PUNTOVENTA_ID"); Integer idEstado = this.resultSet.getInt("ORIGEM_ESTADO_ID");
try { try {
if (fieldName.equals("IMPORTETAXAEMBARQUE") && this.resultSet.getBigDecimal("IMPORTETAXAEMBARQUE") != null && !this.resultSet.getBigDecimal("IMPORTETAXAEMBARQUE").equals(BigDecimal.ZERO)) { if (fieldName.equals("IMPORTETAXAEMBARQUE") && this.resultSet.getBigDecimal("IMPORTETAXAEMBARQUE") != null && !this.resultSet.getBigDecimal("IMPORTETAXAEMBARQUE").equals(BigDecimal.ZERO)) {
valorIcms = CalculoImposto.getValorImposto(this.relatorio.getConexao(), puntoVentaId, indInterestadual, BigDecimal.ZERO, BigDecimal.ZERO, this.resultSet.getBigDecimal("IMPORTETAXAEMBARQUE"), BigDecimal.ZERO); valorIcms = CalculoImposto.getValorImpostoByEstadoOrigem(this.relatorio.getConexao(), idEstado, indInterestadual, BigDecimal.ZERO, BigDecimal.ZERO, this.resultSet.getBigDecimal("IMPORTETAXAEMBARQUE"), BigDecimal.ZERO);
return this.resultSet.getBigDecimal("IMPORTETAXAEMBARQUE").subtract(valorIcms); return this.resultSet.getBigDecimal("IMPORTETAXAEMBARQUE").subtract(valorIcms);
} }
else if (fieldName.equals("IMPORTEPEDAGIO") && this.resultSet.getBigDecimal("IMPORTEPEDAGIO") != null && !this.resultSet.getBigDecimal("IMPORTEPEDAGIO").equals(BigDecimal.ZERO)) { else if (fieldName.equals("IMPORTEPEDAGIO") && this.resultSet.getBigDecimal("IMPORTEPEDAGIO") != null && !this.resultSet.getBigDecimal("IMPORTEPEDAGIO").equals(BigDecimal.ZERO)) {
valorIcms = CalculoImposto.getValorImposto(this.relatorio.getConexao(), puntoVentaId, indInterestadual, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, this.resultSet.getBigDecimal("IMPORTEPEDAGIO")); valorIcms = CalculoImposto.getValorImpostoByEstadoOrigem(this.relatorio.getConexao(), idEstado, indInterestadual, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, this.resultSet.getBigDecimal("IMPORTEPEDAGIO"));
return this.resultSet.getBigDecimal("IMPORTEPEDAGIO").subtract(valorIcms); return this.resultSet.getBigDecimal("IMPORTEPEDAGIO").subtract(valorIcms);
} }
else if (fieldName.equals("IMPORTESEGURO") && this.resultSet.getBigDecimal("IMPORTESEGURO") != null && !this.resultSet.getBigDecimal("IMPORTESEGURO").equals(BigDecimal.ZERO)) { else if (fieldName.equals("IMPORTESEGURO") && this.resultSet.getBigDecimal("IMPORTESEGURO") != null && !this.resultSet.getBigDecimal("IMPORTESEGURO").equals(BigDecimal.ZERO)) {
valorIcms = CalculoImposto.getValorImposto(this.relatorio.getConexao(), puntoVentaId, indInterestadual, BigDecimal.ZERO, this.resultSet.getBigDecimal("IMPORTESEGURO"), BigDecimal.ZERO, BigDecimal.ZERO); valorIcms = CalculoImposto.getValorImpostoByEstadoOrigem(this.relatorio.getConexao(), idEstado, indInterestadual, BigDecimal.ZERO, this.resultSet.getBigDecimal("IMPORTESEGURO"), BigDecimal.ZERO, BigDecimal.ZERO);
return this.resultSet.getBigDecimal("IMPORTESEGURO").subtract(valorIcms); return this.resultSet.getBigDecimal("IMPORTESEGURO").subtract(valorIcms);
} }
else if (fieldName.equals("TOTAL_EMBARQUE") && !this.resultSet.getBigDecimal("TOTAL_EMBARQUE").equals(BigDecimal.ZERO)) { else if (fieldName.equals("TOTAL_EMBARQUE") && !this.resultSet.getBigDecimal("TOTAL_EMBARQUE").equals(BigDecimal.ZERO)) {
valorIcms = CalculoImposto.getValorImposto(this.relatorio.getConexao(), puntoVentaId, indInterestadual, BigDecimal.ZERO, this.resultSet.getBigDecimal("TOTAL_EMBARQUE"), BigDecimal.ZERO, BigDecimal.ZERO); valorIcms = CalculoImposto.getValorImpostoByEstadoOrigem(this.relatorio.getConexao(), idEstado, indInterestadual, BigDecimal.ZERO, this.resultSet.getBigDecimal("TOTAL_EMBARQUE"), BigDecimal.ZERO, BigDecimal.ZERO);
return this.resultSet.getBigDecimal("TOTAL_EMBARQUE").subtract(valorIcms); return this.resultSet.getBigDecimal("TOTAL_EMBARQUE").subtract(valorIcms);
} }
else if (fieldName.equals("TOTAL_PEDAGIO") && !this.resultSet.getBigDecimal("TOTAL_PEDAGIO").equals(BigDecimal.ZERO)) { else if (fieldName.equals("TOTAL_PEDAGIO") && !this.resultSet.getBigDecimal("TOTAL_PEDAGIO").equals(BigDecimal.ZERO)) {
valorIcms = CalculoImposto.getValorImposto(this.relatorio.getConexao(), puntoVentaId, indInterestadual, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, this.resultSet.getBigDecimal("TOTAL_PEDAGIO")); valorIcms = CalculoImposto.getValorImpostoByEstadoOrigem(this.relatorio.getConexao(), idEstado, indInterestadual, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, this.resultSet.getBigDecimal("TOTAL_PEDAGIO"));
return this.resultSet.getBigDecimal("TOTAL_PEDAGIO").subtract(valorIcms); return this.resultSet.getBigDecimal("TOTAL_PEDAGIO").subtract(valorIcms);
} }
else if (fieldName.equals("TOTAL_SEGURO") && !this.resultSet.getBigDecimal("TOTAL_SEGURO").equals(BigDecimal.ZERO)) { else if (fieldName.equals("TOTAL_SEGURO") && !this.resultSet.getBigDecimal("TOTAL_SEGURO").equals(BigDecimal.ZERO)) {
valorIcms = CalculoImposto.getValorImposto(this.relatorio.getConexao(), puntoVentaId, indInterestadual, BigDecimal.ZERO, this.resultSet.getBigDecimal("TOTAL_SEGURO"), BigDecimal.ZERO, BigDecimal.ZERO); valorIcms = CalculoImposto.getValorImpostoByEstadoOrigem(this.relatorio.getConexao(), idEstado, indInterestadual, BigDecimal.ZERO, this.resultSet.getBigDecimal("TOTAL_SEGURO"), BigDecimal.ZERO, BigDecimal.ZERO);
return this.resultSet.getBigDecimal("TOTAL_SEGURO").subtract(valorIcms); return this.resultSet.getBigDecimal("TOTAL_SEGURO").subtract(valorIcms);
} }
} catch (Exception e) { } catch (Exception e) {
@ -118,11 +121,11 @@ public class RelatorioTaxasLinha extends Relatorio {
protected void processaParametros() throws Exception { protected void processaParametros() throws Exception {
} }
public HashMap<String, Object> getConfigImposto(Integer puntoVentaId) throws Exception { public HashMap<String, Object> getConfigImposto(Integer idEstado) throws Exception {
HashMap<String, Object> cacheConfig = null; HashMap<String, Object> cacheConfig = null;
try { try {
cacheConfig = (HashMap<String, Object>) CalculoImposto.getConfigImpostoByPuntoVenta(this.getConexao(), puntoVentaId); cacheConfig = (HashMap<String, Object>) CalculoImposto.getConfigImpostoByEstadoOrigem(this.getConexao(), idEstado);
} catch (Exception e) { } catch (Exception e) {
if (e instanceof ExceptionConfiguracao) if (e instanceof ExceptionConfiguracao)
this.addInfoMsg(e.getMessage()); this.addInfoMsg(e.getMessage());
@ -148,6 +151,8 @@ public class RelatorioTaxasLinha extends Relatorio {
sql.append(" SUM(CASE WHEN CJ.IMPORTETAXAEMBARQUE>0 THEN 1 ELSE 0 END) EMBARQUE_VENDIDOS, "); sql.append(" SUM(CASE WHEN CJ.IMPORTETAXAEMBARQUE>0 THEN 1 ELSE 0 END) EMBARQUE_VENDIDOS, ");
sql.append(" SUM(CASE WHEN CJ.IMPORTEPEDAGIO>0 THEN 1 ELSE 0 END) PEDAGIO_VENDIDOS, "); sql.append(" SUM(CASE WHEN CJ.IMPORTEPEDAGIO>0 THEN 1 ELSE 0 END) PEDAGIO_VENDIDOS, ");
sql.append(" SUM(CASE WHEN CJ.IMPORTESEGURO>0 THEN 1 ELSE 0 END) SEGURO_VENDIDOS, "); sql.append(" SUM(CASE WHEN CJ.IMPORTESEGURO>0 THEN 1 ELSE 0 END) SEGURO_VENDIDOS, ");
sql.append(" CO.ESTADO_ID ORIGEM_ESTADO_ID, ");
sql.append(" CD.ESTADO_ID DESTINO_ESTADO_ID, ");
sql.append(" CASE "); sql.append(" CASE ");
sql.append(" WHEN CO.ESTADO_ID <> CD.ESTADO_ID THEN "); sql.append(" WHEN CO.ESTADO_ID <> CD.ESTADO_ID THEN ");
sql.append(" 'S' "); sql.append(" 'S' ");

View File

@ -221,4 +221,160 @@ public class CalculoImposto {
return valorIcms; return valorIcms;
} }
public static Map<String, Object> getConfigImpostoByEstadoOrigem(Connection conexao, Integer idEstado) throws SQLException, ExceptionConfiguracao {
HashMap<String, Object> cacheConfig = null;
// Verifica se já existe configuração na memoria, caso não exista, realiza busca no banco
if (!mapCacheConfigImposto.containsKey(idEstado.toString())) {
Boolean isBuscouConfig = false;
StringBuilder sql = new StringBuilder();
sql.append(" SELECT EI.*, ES.NOMBESTADO, EM.NOMBEMPRESA ");
sql.append(" FROM PUNTO_VENTA PV, PARADA PR, CIUDAD CD, EMPRESA_IMPOSTO EI, ESTADO ES, EMPRESA EM ");
sql.append(" WHERE ES.ESTADO_ID = :ORIGEM_ESTADO_ID ");
sql.append(" AND PV.PARADA_ID = PR.PARADA_ID ");
sql.append(" AND PR.CIUDAD_ID = CD.CIUDAD_ID ");
sql.append(" AND CD.ESTADO_ID = EI.ESTADO_ID (+) ");
sql.append(" AND PV.EMPRESA_ID = NVL(EI.EMPRESA_ID, PV.EMPRESA_ID) ");
sql.append(" AND CD.ESTADO_ID = ES.ESTADO_ID ");
sql.append(" AND PV.EMPRESA_ID = EM.EMPRESA_ID ");
sql.append(" AND NVL(EI.ACTIVO, 1) = 1 ");
NamedParameterStatement stmt = new NamedParameterStatement(conexao, sql.toString());
stmt.setInt("ORIGEM_ESTADO_ID", idEstado);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
if (rs.getBigDecimal("ICMS") != null) {
isBuscouConfig = true;
cacheConfig = new HashMap<String, Object>();
cacheConfig.put("PORCREDBASEICMS", rs.getBigDecimal("PORCREDBASEICMS"));
cacheConfig.put("PORCREDESTADUAL", rs.getBigDecimal("PORCREDESTADUAL"));
cacheConfig.put("INDTARIFAESTADUAL", rs.getBoolean("INDTARIFAESTADUAL"));
cacheConfig.put("INDSEGUROESTADUAL", rs.getBoolean("INDSEGUROESTADUAL"));
cacheConfig.put("INDTXEMBARQUEESTADUAL", rs.getBoolean("INDTXEMBARQUEESTADUAL"));
cacheConfig.put("INDPEDAGIOESTDUAL", rs.getBoolean("INDPEDAGIOESTDUAL"));
cacheConfig.put("PORCREDMUNICIPAL", rs.getBigDecimal("PORCREDMUNICIPAL"));
cacheConfig.put("INDTARIFAMUNICIPAL", rs.getBoolean("INDTARIFAMUNICIPAL"));
cacheConfig.put("INDSEGUROMUNICIPAL", rs.getBoolean("INDSEGUROMUNICIPAL"));
cacheConfig.put("INDTXEMBARQUEMUNICIPAL", rs.getBoolean("INDTXEMBARQUEMUNICIPAL"));
cacheConfig.put("INDPEDAGIOMUNICIPAL", rs.getBoolean("INDPEDAGIOMUNICIPAL"));
cacheConfig.put("INDJANEIRO", rs.getBoolean("INDJANEIRO"));
cacheConfig.put("INDFEVEREIRO", rs.getBoolean("INDFEVEREIRO"));
cacheConfig.put("INDMARCO", rs.getBoolean("INDMARCO"));
cacheConfig.put("INDABRIL", rs.getBoolean("INDABRIL"));
cacheConfig.put("INDMAIO", rs.getBoolean("INDMAIO"));
cacheConfig.put("INDJUNHO", rs.getBoolean("INDJUNHO"));
cacheConfig.put("INDJULHO", rs.getBoolean("INDJULHO"));
cacheConfig.put("INDAGOSTO", rs.getBoolean("INDAGOSTO"));
cacheConfig.put("INDSETEMBRO", rs.getBoolean("INDSETEMBRO"));
cacheConfig.put("INDOUTUBRO", rs.getBoolean("INDOUTUBRO"));
cacheConfig.put("INDNOVEMBRO", rs.getBoolean("INDNOVEMBRO"));
cacheConfig.put("INDDEZEMBRO", rs.getBoolean("INDDEZEMBRO"));
cacheConfig.put("ICMS", rs.getBigDecimal("ICMS"));
cacheConfig.put("PORCREDBASEICMS", rs.getBigDecimal("PORCREDBASEICMS"));
}
String estado = rs.getString("NOMBESTADO");
String empresa = rs.getString("NOMBEMPRESA");
if (!isBuscouConfig)
throw new ExceptionConfiguracao("Não foi possivel obter a configuração de imposto para o estado: " + estado + " empresa: " + empresa);
}
;
if (!rs.isClosed())
rs.close();
if (!stmt.isClosed())
stmt.close();
if (!isBuscouConfig)
throw new ExceptionConfiguracao("Não foi possivel obter a configuração de imposto para agência " + idEstado.toString());
mapCacheConfigImposto.put(idEstado.toString(), cacheConfig);
} else
cacheConfig = mapCacheConfigImposto.get(idEstado.toString());
return cacheConfig;
}
public static BigDecimal getValorImpostoByEstadoOrigem(Connection conexao, Integer idEstado, String indInterestadual, BigDecimal tarifa, BigDecimal seguro, BigDecimal embarque, BigDecimal pedagio) throws Exception {
HashMap<String, Object> configImposto = new HashMap<String, Object>();
BigDecimal baseCalculo = BigDecimal.ZERO;
BigDecimal icms;
BigDecimal porcRedEstadualMunicipal;
BigDecimal porcRedBaseIcms;
BigDecimal valorIcms = BigDecimal.ZERO;
Boolean indTarifaMunicipal;
Boolean indSeguroMunicipal;
Boolean indTxEmbarqueMunicipal;
Boolean indPedagioMunicipal;
Boolean indTarifaEstadual;
Boolean indSeguroEstadual;
Boolean indTxEmbarqueEstadual;
Boolean indPedagioEstadual;
configImposto = (HashMap<String, Object>) getConfigImpostoByEstadoOrigem(conexao, idEstado);
if (configImposto == null)
return BigDecimal.ZERO;
icms = (BigDecimal) configImposto.get("ICMS");
porcRedBaseIcms = (BigDecimal) configImposto.get("PORCREDBASEICMS");
if (indInterestadual.equals("S")) {
porcRedEstadualMunicipal = (BigDecimal) configImposto.get("PORCREDESTADUAL");
indTarifaEstadual = (Boolean) configImposto.get("INDTARIFAESTADUAL");
indSeguroEstadual = (Boolean) configImposto.get("INDSEGUROESTADUAL");
indTxEmbarqueEstadual = (Boolean) configImposto.get("INDTXEMBARQUEESTADUAL");
indPedagioEstadual = (Boolean) configImposto.get("INDPEDAGIOESTDUAL");
// Criação da base de calculo
baseCalculo = indTarifaEstadual && tarifa != null ? baseCalculo.add(tarifa) : baseCalculo;
baseCalculo = indSeguroEstadual && seguro != null ? baseCalculo.add(seguro) : baseCalculo;
baseCalculo = indTxEmbarqueEstadual && embarque != null ? baseCalculo.add(embarque) : baseCalculo;
baseCalculo = indPedagioEstadual && pedagio != null ? baseCalculo.add(pedagio) : baseCalculo;
} else {
porcRedEstadualMunicipal = (BigDecimal) configImposto.get("PORCREDMUNICIPAL");
indTarifaMunicipal = (Boolean) configImposto.get("INDTARIFAMUNICIPAL");
indSeguroMunicipal = (Boolean) configImposto.get("INDSEGUROMUNICIPAL");
indTxEmbarqueMunicipal = (Boolean) configImposto.get("INDTXEMBARQUEMUNICIPAL");
indPedagioMunicipal = (Boolean) configImposto.get("INDPEDAGIOMUNICIPAL");
baseCalculo = indTarifaMunicipal && tarifa != null ? baseCalculo.add(tarifa) : baseCalculo;
baseCalculo = indSeguroMunicipal && seguro != null ? baseCalculo.add(seguro) : baseCalculo;
baseCalculo = indTxEmbarqueMunicipal && embarque != null ? baseCalculo.add(embarque) : baseCalculo;
baseCalculo = indPedagioMunicipal && pedagio != null ? baseCalculo.add(pedagio) : baseCalculo;
}
// Redução da base de calculo
if (porcRedBaseIcms != null && porcRedBaseIcms.equals(BigDecimal.ZERO))
// BASE DE CALCULO = BASE DE CALCULO - ((PERCENTUAL DE REDUÇÃO / 100) * BASE DE CALCULO ))
baseCalculo = baseCalculo.subtract(porcRedBaseIcms.divide(BigDecimal.valueOf(100)).multiply(baseCalculo));
// Calcula o valor do ICMS
valorIcms = baseCalculo.multiply(icms.divide(BigDecimal.valueOf(100)));
// Redução estadual
if (porcRedEstadualMunicipal != null && porcRedEstadualMunicipal.equals(BigDecimal.ZERO))
valorIcms = valorIcms.subtract(porcRedEstadualMunicipal.divide(BigDecimal.valueOf(100)).multiply(valorIcms));
return valorIcms;
}
} }