edgar 2016-09-08 14:01:53 +00:00
parent 7cc6ff1dcf
commit 8d16dde676
1 changed files with 62 additions and 45 deletions

View File

@ -26,17 +26,23 @@ import com.rjconsultores.ventaboletos.constantes.Constantes;
import com.rjconsultores.ventaboletos.dao.CalcularPeajeDAO; import com.rjconsultores.ventaboletos.dao.CalcularPeajeDAO;
import com.rjconsultores.ventaboletos.entidad.CasetaPeaje; import com.rjconsultores.ventaboletos.entidad.CasetaPeaje;
import com.rjconsultores.ventaboletos.entidad.CasetaPeajeExcepcion; import com.rjconsultores.ventaboletos.entidad.CasetaPeajeExcepcion;
import com.rjconsultores.ventaboletos.entidad.ClaseServicio;
import com.rjconsultores.ventaboletos.entidad.ClasseIndicePeaje;
import com.rjconsultores.ventaboletos.entidad.ExcepcionPeaje; import com.rjconsultores.ventaboletos.entidad.ExcepcionPeaje;
import com.rjconsultores.ventaboletos.entidad.ExcepcionPeajeVigencia; import com.rjconsultores.ventaboletos.entidad.ExcepcionPeajeVigencia;
import com.rjconsultores.ventaboletos.entidad.OrgaoConcedente; import com.rjconsultores.ventaboletos.entidad.OrgaoConcedente;
import com.rjconsultores.ventaboletos.entidad.Parada; import com.rjconsultores.ventaboletos.entidad.Parada;
import com.rjconsultores.ventaboletos.entidad.Ruta; import com.rjconsultores.ventaboletos.entidad.Ruta;
import com.rjconsultores.ventaboletos.service.OrgaoConcedenteService;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado; import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
@Repository("calcularPeajeDAO") @Repository("calcularPeajeDAO")
public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, String> public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, String>
implements CalcularPeajeDAO { implements CalcularPeajeDAO {
@Autowired
private OrgaoConcedenteService orgaoConcedenteService;
@Autowired @Autowired
public CalcularPeajeHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) { public CalcularPeajeHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) {
setSessionFactory(factory); setSessionFactory(factory);
@ -98,7 +104,7 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
.append(" inner join ciudad cdest on cdest.ciudad_id = pdest.ciudad_id ") .append(" inner join ciudad cdest on cdest.ciudad_id = pdest.ciudad_id ")
.append(" inner join estado edest on edest.estado_id = cdest.estado_id ") .append(" inner join estado edest on edest.estado_id = cdest.estado_id ")
.append(" inner join empresa_imposto eidest on eidest.empresa_id = e.empresa_id and eidest.estado_id = edest.estado_id ") .append(" inner join empresa_imposto eidest on eidest.empresa_id = e.empresa_id and eidest.estado_id = edest.estado_id ")
.append(" inner join tramo_tmp tt on torig.origen_id = tt.orig and tdest.destino_id = tt.dest " ) .append(" inner join tramo_tmp tt on torig.origen_id = tt.orig and tdest.destino_id = tt.dest ")
.append("), caseta_tramo_da_ruta as ( ") .append("), caseta_tramo_da_ruta as ( ")
.append(" select rcas.rutacaseta_id as rutacasetaid, rcas.rutasecuencia_id as rutasecuenciaid, rcas.casetapeaje_id, ") .append(" select rcas.rutacaseta_id as rutacasetaid, rcas.rutasecuencia_id as rutasecuenciaid, rcas.casetapeaje_id, ")
.append(" rseq1.numsecuencia as numsecuencia, rseq1.tramo_id as tramoid, rseq2.numsecuencia as numsecuencia2, ") .append(" rseq1.numsecuencia as numsecuencia, rseq1.tramo_id as tramoid, rseq2.numsecuencia as numsecuencia2, ")
@ -131,11 +137,13 @@ 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<PeajeVO> peajes = new ArrayList<PeajeVO>(); List<PeajeVO> peajes = new ArrayList<PeajeVO>();
OrgaoConcedente orgaoConcedente = orgaoConcedenteService.obtenerID(orgao.getOrgaoConcedenteId());
List<ClasseIndicePeaje> classeIndicePeajes = orgaoConcedente.getClassesIndicePeaje();
for (Ruta ruta : lsRuta) { for (Ruta ruta : lsRuta) {
List<Object> lsObj = buscarCasetasPeajeWithinTramo(ruta); List<Object> lsObj = buscarCasetasPeajeWithinTramo(ruta);
if (null != lsObj) { if (null != lsObj) {
for (Object arrObj : lsObj) { for (Object arrObj : lsObj) {
peajes.add(PeajeVO.create(ruta, orgao, (Object[]) arrObj)); peajes.add(PeajeVO.create(ruta, orgaoConcedente, (Object[]) arrObj, classeIndicePeajes));
} }
} }
} }
@ -161,8 +169,7 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
con.commit(); con.commit();
recalcularExcecoesPedagio(peajes); recalcularExcecoesPedagio(peajes);
} } catch (SQLException e) {
catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -188,33 +195,33 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
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);
query.setInteger("destinoId", destinoId); query.setInteger("destinoId", destinoId);
return (ExcepcionPeaje)query.uniqueResult(); return (ExcepcionPeaje) query.uniqueResult();
} }
private Parada getParada(Integer paradaId){ private Parada getParada(Integer paradaId) {
return (Parada)getSession().get(Parada.class, paradaId); return (Parada) getSession().get(Parada.class, paradaId);
} }
private Ruta getRuta(Integer rutaId){ private Ruta getRuta(Integer rutaId) {
return (Ruta)getSession().get(Ruta.class, rutaId); return (Ruta) getSession().get(Ruta.class, rutaId);
} }
@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);
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);
for (CasetaPeajeExcepcion cpe : caseta.getLsCasetaPeajeExcepcion()){ for (CasetaPeajeExcepcion cpe : caseta.getLsCasetaPeajeExcepcion()) {
if (excepcion == null){ if (excepcion == null) {
excepcion = new ExcepcionPeaje(); excepcion = new ExcepcionPeaje();
excepcion.setActivo(true); excepcion.setActivo(true);
excepcion.setDestino(getParada(peaje.destinoId)); excepcion.setDestino(getParada(peaje.destinoId));
@ -243,7 +250,7 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
epv.setCasetaPeaje(caseta); epv.setCasetaPeaje(caseta);
excepcion.getLsExcepcionPeajeVigencia().add(epv); excepcion.getLsExcepcionPeajeVigencia().add(epv);
} }
if (excepcion != null){ if (excepcion != null) {
getSession().saveOrUpdate(excepcion); getSession().saveOrUpdate(excepcion);
} }
} }
@ -271,16 +278,15 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
private static BigDecimal getCantEixos(Ruta ruta) { private static BigDecimal getCantEixos(Ruta ruta) {
try { try {
return new BigDecimal(ruta.getCantEixos()); return new BigDecimal(ruta.getCantEixos());
} } catch (NullPointerException e) {
catch (NullPointerException e) {
throw new RuntimeException("rutaSemQtdEixos;" + ruta.getRutaId()); throw new RuntimeException("rutaSemQtdEixos;" + ruta.getRutaId());
} }
} }
private static BigDecimal getRutaCantAsientos(Ruta ruta) { private static BigDecimal getRutaCantAsientos(Ruta ruta) {
try { try {
return new BigDecimal(ruta.getCantAsientos()); return new BigDecimal(ruta.getCantAsientos());
} } catch (NullPointerException e) {
catch (NullPointerException e) {
throw new RuntimeException("rutaSemQtdAsientos;" + ruta.getRutaId()); throw new RuntimeException("rutaSemQtdAsientos;" + ruta.getRutaId());
} }
} }
@ -288,8 +294,7 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
private static BigDecimal getImporte(Ruta ruta, Object[] obj) { private static BigDecimal getImporte(Ruta ruta, Object[] obj) {
if (ruta.getIndSentidoIda()) { if (ruta.getIndSentidoIda()) {
return new BigDecimal(obj[22].toString()); return new BigDecimal(obj[22].toString());
} } else {
else {
return new BigDecimal(obj[23].toString()); return new BigDecimal(obj[23].toString());
} }
} }
@ -298,8 +303,7 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
String icms = null; String icms = null;
if (ruta.getIndSentidoIda()) { if (ruta.getIndSentidoIda()) {
icms = obj[27].toString(); icms = obj[27].toString();
} } else {
else {
icms = obj[28].toString(); icms = obj[28].toString();
} }
@ -323,10 +327,9 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
.divide(BigDecimal.ONE.subtract(icms), HIGH_PRECISION, RoundingMode.HALF_UP); .divide(BigDecimal.ONE.subtract(icms), HIGH_PRECISION, RoundingMode.HALF_UP);
} }
private static BigDecimal calculateImportePeaje(Ruta ruta, OrgaoConcedente orgao, Object[] obj) { private static BigDecimal calculateImportePeaje(Ruta ruta, OrgaoConcedente orgao, Object[] obj, BigDecimal indicePeaje) {
BigDecimal rutaCantEixos = getCantEixos(ruta); BigDecimal rutaCantEixos = getCantEixos(ruta);
BigDecimal rutaCantAsientos = getRutaCantAsientos(ruta); BigDecimal indice = indicePeaje;
BigDecimal indice = orgao.getIndicePeaje();
BigDecimal importe = getImporte(ruta, obj); BigDecimal importe = getImporte(ruta, obj);
BigDecimal icms = getIcms(ruta, obj); BigDecimal icms = getIcms(ruta, obj);
@ -334,13 +337,27 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
return calculateImportePeajeARTESP(importe, icms, indice, rutaCantEixos); return calculateImportePeajeARTESP(importe, icms, indice, rutaCantEixos);
} }
if (Constantes.ORGAOS_CONCEDENTES_CALCULO_ANTT.contains(orgao.getOrgaoConcedenteId())) { if (Constantes.ORGAOS_CONCEDENTES_CALCULO_ANTT.contains(orgao.getOrgaoConcedenteId())) {
return calculateImportePeajeANTT(importe, icms, indice, rutaCantEixos, rutaCantAsientos); return calculateImportePeajeANTT(importe, icms, indice, rutaCantEixos, getRutaCantAsientos(ruta));
} }
return BigDecimal.ZERO; // TODO throw an exception? como calcular quando é outro orgao concendente? return BigDecimal.ZERO; // TODO throw an exception? como calcular quando é outro orgao concendente?
} }
private static PeajeVO create(Ruta ruta, OrgaoConcedente orgao, Object[] obj) { private static BigDecimal getIndicePeaje(ClaseServicio claseServicio,
OrgaoConcedente orgao,
List<ClasseIndicePeaje> lsClasseIndicePeaje) {
for (ClasseIndicePeaje classeIndicePeaje : lsClasseIndicePeaje) {
if (claseServicio.equals(classeIndicePeaje.getClasseServicio())) {
return classeIndicePeaje.getIndicePeaje();
}
}
return orgao.getIndicePeaje();
}
private static PeajeVO create(Ruta ruta, OrgaoConcedente orgao,
Object[] obj, List<ClasseIndicePeaje> lsClasseIndicePeaje) {
PeajeVO pvo = new PeajeVO(); PeajeVO pvo = new PeajeVO();
pvo.rutaId = ruta.getRutaId(); pvo.rutaId = ruta.getRutaId();
@ -350,8 +367,8 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
pvo.destinoId = Integer.parseInt(obj[9].toString()); pvo.destinoId = Integer.parseInt(obj[9].toString());
pvo.orgaoId = orgao.getOrgaoConcedenteId(); pvo.orgaoId = orgao.getOrgaoConcedenteId();
pvo.casetaPeajeId = Integer.parseInt(obj[20].toString()); pvo.casetaPeajeId = Integer.parseInt(obj[20].toString());
pvo.indicePeaje = orgao.getIndicePeaje(); pvo.indicePeaje = getIndicePeaje(ruta.getClaseServicio(), orgao, lsClasseIndicePeaje);
pvo.importePeaje = calculateImportePeaje(ruta, orgao, obj); pvo.importePeaje = calculateImportePeaje(ruta, orgao, obj, pvo.indicePeaje);
pvo.activo = 1; pvo.activo = 1;
pvo.fecmodif = new Date(); pvo.fecmodif = new Date();