fixed bug #7674: Correção no cálculo de pedágio

git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Model/trunk/modelWeb@59024 d1611594-4594-4d17-8e1d-87c2c4800839
master
tharcisio 2016-08-11 18:25:57 +00:00
parent 80eba63a15
commit 89c8d07ff3
1 changed files with 52 additions and 38 deletions

View File

@ -128,29 +128,12 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
// pra cada ruta obter as combinacoes de tramos, cada tramo com sua lista de casetaPeaje // pra cada ruta obter as combinacoes de tramos, cada tramo com sua lista de casetaPeaje
public int[] gerarSQLInserirPeajes(List<Ruta> lsRuta, OrgaoConcedente orgao) { public int[] gerarSQLInserirPeajes(List<Ruta> lsRuta, OrgaoConcedente orgao) {
List<String> batchSql = new ArrayList<String>();
List<String> batchExcepcionPeajeVigenciaSql = new ArrayList<String>();
List<PeajeVO> peajes = new ArrayList<PeajeVO>(); List<PeajeVO> peajes = new ArrayList<PeajeVO>();
for (Ruta ruta : lsRuta) { for (Ruta ruta : lsRuta) {
List<Object> lsObj = buscarCasetasPeajeWithinTramo(ruta); List<Object> lsObj = buscarCasetasPeajeWithinTramo(ruta);
if (null != lsObj) { if (null != lsObj) {
int i = 0;
for (Object arrObj : lsObj) { for (Object arrObj : lsObj) {
PeajeVO pvo = PeajeVO.create(ruta, orgao, (Object[]) arrObj); peajes.add(PeajeVO.create(ruta, orgao, (Object[]) arrObj));
if (pvo != null){
batchSql.add(pvo.toInsertQuery());
batchExcepcionPeajeVigenciaSql.add("update EXCEPCION_PEAJE_VIGENCIA set activo = 0 " +
"where excepcionpeaje_id in (select excepcionpeaje_id from EXCEPCION_PEAJE "+
"where ruta_id = " + pvo.rutaId +
" and origen_id = " + pvo.origenId + " and " +
"destino_id = " + pvo.destinoId + ")");
batchExcepcionPeajeVigenciaSql.add("update EXCEPCION_PEAJE set activo = 0 where ruta_id = " + pvo.rutaId +
" and origen_id = " + pvo.origenId + " and " +
"destino_id = " + pvo.destinoId + "");
peajes.add(pvo);
}
} }
} }
} }
@ -166,15 +149,16 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
Statement stmt = con.createStatement(); Statement stmt = con.createStatement();
con.setAutoCommit(false); con.setAutoCommit(false);
stmt.addBatch(updateSql.toString()); stmt.addBatch(updateSql.toString());
for (String sql : batchSql) { for (PeajeVO pvo : peajes) {
stmt.addBatch(sql); stmt.addBatch(pvo.toInsertQuery());
} stmt.addBatch(pvo.toUpdateExcepcionPeajeQuery());
for (String sql : batchExcepcionPeajeVigenciaSql) { stmt.addBatch(pvo.toUpdateExcepcionPeajeVigenciaQuery());
stmt.addBatch(sql);
} }
result = stmt.executeBatch(); result = stmt.executeBatch();
con.commit(); con.commit();
recalcularExcecoesPedagio(peajes); recalcularExcecoesPedagio(peajes);
} }
catch (SQLException e) { catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
@ -183,23 +167,24 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
return result; return result;
} }
private Map<Integer, CasetaPeaje> buscaCasetas(List<PeajeVO> pvo){ private Map<Integer, CasetaPeaje> buscaCasetas(List<PeajeVO> pvo) {
List<Integer> ids = new ArrayList<Integer>(); List<Integer> ids = new ArrayList<Integer>();
Map<Integer, CasetaPeaje> casetas = new HashMap<Integer, CasetaPeaje>(); Map<Integer, CasetaPeaje> casetas = new HashMap<Integer, CasetaPeaje>();
for(PeajeVO p : pvo){ for (PeajeVO p : pvo) {
ids.add(p.casetaPeajeId); ids.add(p.casetaPeajeId);
} }
Query query = getSession().createQuery("select c from CasetaPeaje c where c.casetaPeajeId in (:ids) "); Query query = getSession()
.createQuery("select c from CasetaPeaje c where c.casetaPeajeId in (:ids) ");
query.setParameterList("ids", ids); query.setParameterList("ids", ids);
for (CasetaPeaje c : (List<CasetaPeaje>)query.list()){ for (CasetaPeaje c : (List<CasetaPeaje>) query.list()) {
casetas.put(c.getCasetaPeajeId(), c); casetas.put(c.getCasetaPeajeId(), c);
} }
return casetas; return casetas;
} }
private ExcepcionPeaje getExcepcion(Integer rutaId, Integer origenId, Integer destinoId){ private ExcepcionPeaje getExcepcion(Integer rutaId, Integer origenId, Integer destinoId){
Query query = getSession().createQuery("select e from ExcepcionPeaje e where e.ruta.rutaId = :rutaId "+ Query query = getSession().createQuery("select e from ExcepcionPeaje e where e.ruta.rutaId = :rutaId " +
"and e.origem.paradaId = :origenId and e.destino.paradaId = :destinoId and e.activo = 1 "); "and e.origem.paradaId = :origenId and e.destino.paradaId = :destinoId and e.activo = 1 ");
query.setInteger("rutaId", rutaId); query.setInteger("rutaId", rutaId);
query.setInteger("origenId", origenId); query.setInteger("origenId", origenId);
@ -218,7 +203,6 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
@Transactional @Transactional
private void recalcularExcecoesPedagio(List<PeajeVO> pvos){ private void recalcularExcecoesPedagio(List<PeajeVO> pvos){
Map<Integer, CasetaPeaje> casetas = buscaCasetas(pvos); Map<Integer, CasetaPeaje> casetas = buscaCasetas(pvos);
Map<Integer, ExcepcionPeaje> excepciones = new HashMap<Integer, ExcepcionPeaje>();
for (PeajeVO peaje : pvos){ for (PeajeVO peaje : pvos){
ExcepcionPeaje excepcion = null; ExcepcionPeaje excepcion = null;
CasetaPeaje caseta = casetas.get(peaje.casetaPeajeId); CasetaPeaje caseta = casetas.get(peaje.casetaPeajeId);
@ -263,6 +247,7 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
private static final int LOW_PRECISION = 2; private static final int LOW_PRECISION = 2;
private static final int HIGH_PRECISION = 10; private static final int HIGH_PRECISION = 10;
private static final BigDecimal A_HUNDRED = new BigDecimal(100);
private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
private Integer rutaId; private Integer rutaId;
@ -305,21 +290,25 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
} }
private static BigDecimal getIcms(Ruta ruta, Object[] obj) { private static BigDecimal getIcms(Ruta ruta, Object[] obj) {
String icms = null;
if (ruta.getIndSentidoIda()) { if (ruta.getIndSentidoIda()) {
return new BigDecimal(obj[27].toString()); icms = obj[27].toString();
} }
else { else {
return new BigDecimal(obj[28].toString()); icms = obj[28].toString();
} }
return new BigDecimal(icms)
.divide(A_HUNDRED, HIGH_PRECISION, RoundingMode.HALF_UP);
} }
// ((importe * rutaCantEixos) / (indice * rutaCantAsientos)) / ( 1 - icms) // ((importe * rutaCantEixos) / (indice * rutaCantAsientos)) / ( 1 - icms)
private static BigDecimal calculateImportePeajeANTT(BigDecimal importe, BigDecimal icms, private static BigDecimal calculateImportePeajeANTT(BigDecimal importe, BigDecimal icms,
BigDecimal indice, BigDecimal rutaCantEixos, BigDecimal rutaCantAsientos) { BigDecimal indice, BigDecimal rutaCantEixos, BigDecimal rutaCantAsientos) {
return importe.multiply(rutaCantEixos) return importe.multiply(rutaCantEixos)
.divide(indice.multiply(rutaCantAsientos), HIGH_PRECISION, RoundingMode.HALF_UP) .divide(indice.multiply(rutaCantAsientos), HIGH_PRECISION, RoundingMode.HALF_UP)
.divide(BigDecimal.ONE.min(icms), LOW_PRECISION, RoundingMode.HALF_UP); .divide(BigDecimal.ONE.subtract(icms), HIGH_PRECISION, RoundingMode.HALF_UP)
.setScale(LOW_PRECISION, RoundingMode.HALF_UP);
} }
// ((importe * rutaCantEixos) / (indice)) / ( 1 - icms) // ((importe * rutaCantEixos) / (indice)) / ( 1 - icms)
@ -327,7 +316,8 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
BigDecimal indice, BigDecimal rutaCantEixos) { BigDecimal indice, BigDecimal rutaCantEixos) {
return importe.multiply(rutaCantEixos) return importe.multiply(rutaCantEixos)
.divide(indice, HIGH_PRECISION, RoundingMode.HALF_UP) .divide(indice, HIGH_PRECISION, RoundingMode.HALF_UP)
.divide(BigDecimal.ONE.min(icms), LOW_PRECISION, RoundingMode.HALF_UP); .divide(BigDecimal.ONE.subtract(icms), HIGH_PRECISION, RoundingMode.HALF_UP)
.setScale(LOW_PRECISION, RoundingMode.HALF_UP);
} }
private static BigDecimal calculateImportePeaje(Ruta ruta, OrgaoConcedente orgao, Object[] obj) { private static BigDecimal calculateImportePeaje(Ruta ruta, OrgaoConcedente orgao, Object[] obj) {
@ -388,6 +378,30 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
.append("', 'yyyy/mm/dd hh24:mi:ss'), ") .append("', 'yyyy/mm/dd hh24:mi:ss'), ")
.append(usuarioId).append(" ) ") .append(usuarioId).append(" ) ")
.toString(); .toString();
}
} }
private String toUpdateExcepcionPeajeQuery() {
return new StringBuilder()
.append("update excepcion_peaje ")
.append("set activo = 0 ")
.append("where ruta_id = ").append(rutaId)
.append(" and origen_id = ").append(origenId)
.append(" and destino_id = ").append(origenId)
.toString();
}
private String toUpdateExcepcionPeajeVigenciaQuery() {
return new StringBuilder()
.append("update excepcion_peaje_vigencia ")
.append("set activo = 0 ")
.append("where excepcionpeaje_id in ( ")
.append(" select excepcionpeaje_id ")
.append(" from excepcion_peaje ")
.append(" where ruta_id = ").append(rutaId)
.append(" and origen_id = ").append(origenId)
.append(" and destino_id = ").append(origenId)
.append(")")
.toString();
}
}
} }