fixed bug #7674: Correção no cálculo de pedágio
adc.: adicionado icms ao cálculo de pedágio git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Model/trunk/modelWeb@58025 d1611594-4594-4d17-8e1d-87c2c4800839master
parent
7dd3d39f76
commit
13c94e4a8e
|
@ -1,98 +1,121 @@
|
|||
package com.rjconsultores.ventaboletos.dao.hibernate;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Query;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.rjconsultores.ventaboletos.constantes.Constantes;
|
||||
import com.rjconsultores.ventaboletos.dao.CalcularPeajeDAO;
|
||||
import com.rjconsultores.ventaboletos.entidad.CasetaPeaje;
|
||||
import com.rjconsultores.ventaboletos.entidad.EmpresaImposto;
|
||||
import com.rjconsultores.ventaboletos.entidad.Estado;
|
||||
import com.rjconsultores.ventaboletos.entidad.OrgaoConcedente;
|
||||
import com.rjconsultores.ventaboletos.entidad.Parada;
|
||||
import com.rjconsultores.ventaboletos.entidad.Peaje;
|
||||
import com.rjconsultores.ventaboletos.entidad.Ruta;
|
||||
import com.rjconsultores.ventaboletos.entidad.RutaCaseta;
|
||||
import com.rjconsultores.ventaboletos.entidad.RutaEmpresa;
|
||||
import com.rjconsultores.ventaboletos.entidad.RutaSecuencia;
|
||||
import com.rjconsultores.ventaboletos.entidad.Tramo;
|
||||
import com.rjconsultores.ventaboletos.exception.BusinessException;
|
||||
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
|
||||
|
||||
@Repository("calcularPeajeDAO")
|
||||
public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, String> implements CalcularPeajeDAO{
|
||||
|
||||
public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, String>
|
||||
implements CalcularPeajeDAO {
|
||||
|
||||
@Autowired
|
||||
public CalcularPeajeHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) {
|
||||
setSessionFactory(factory);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<Ruta> buscarRutaWithCasetaPeajeFromOrgao(OrgaoConcedente oc){
|
||||
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" select distinct r from Ruta r , RutaCaseta rc , RutaSecuencia rs , CasetaPeaje cp ");
|
||||
sql.append(" where rc.rutaSecuencia.rutasecuenciaId = rs.rutasecuenciaId ");
|
||||
sql.append(" and rc.casetaPeaje.casetaPeajeId = cp.casetaPeajeId ");
|
||||
sql.append(" and rs.ruta.rutaId = r.rutaId ");
|
||||
if(oc != null && oc.getOrgaoConcedenteId() != null){
|
||||
sql.append(" and r.orgaoConcedente.orgaoConcedenteId = :orgaoConcedenteId ");
|
||||
public List<Ruta> buscarRutaWithCasetaPeajeFromOrgao(OrgaoConcedente oc) {
|
||||
StringBuilder sql = new StringBuilder()
|
||||
.append("select distinct r ")
|
||||
.append("from Ruta r, RutaCaseta rc, RutaSecuencia rs, CasetaPeaje cp ")
|
||||
.append("where rc.rutaSecuencia.rutasecuenciaId = rs.rutasecuenciaId ")
|
||||
.append(" and rc.casetaPeaje.casetaPeajeId = cp.casetaPeajeId ")
|
||||
.append(" and rs.ruta.rutaId = r.rutaId ");
|
||||
if (oc != null && oc.getOrgaoConcedenteId() != null) {
|
||||
sql.append(" and r.orgaoConcedente.orgaoConcedenteId = :orgaoConcedenteId ");
|
||||
}
|
||||
|
||||
sql.append(" order by r.descruta ");
|
||||
|
||||
|
||||
Query query = getSession().createQuery(sql.toString());
|
||||
|
||||
if(oc != null && oc.getOrgaoConcedenteId() != null){
|
||||
if (oc != null && oc.getOrgaoConcedenteId() != null) {
|
||||
query.setInteger("orgaoConcedenteId", oc.getOrgaoConcedenteId());
|
||||
}
|
||||
return (List<Ruta>)query.list();
|
||||
|
||||
return query.list();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<Object> buscarCasetasPeajeWithinTramo(Ruta ruta){
|
||||
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
sql.append(" with temp_table as ( ");
|
||||
sql.append(" select tra.origen_id as orig , tra.DESTINO_ID as dest, tra.tramo_id as tra, tra.desctramo as traDesc, tra.origen_id as traorig, ");
|
||||
sql.append(" tra.destino_id as tradest, tra.via_id as travia, tra.cantkmreal as tracantkm, ");
|
||||
sql.append(" tra.cantkmpagoconductor as tracantkmpago, tra.activo as traactivo, tra.fecmodif as trafecmodif, tra.usuario_id as trausuario ");
|
||||
sql.append(" from RUTA_COMBINACION rcom inner join ruta r on r.RUTA_ID = rcom.RUTA_ID ");
|
||||
sql.append(" inner join ORGAO_CONCEDENTE oc on oc.ORGAOCONCEDENTE_ID = r.ORGAOCONCEDENTE_ID ");
|
||||
sql.append(" inner join tramo tra on tra.tramo_id = rcom.tramo_id ");
|
||||
sql.append(" where r.RUTA_ID = :rutaId ");
|
||||
sql.append(" ) , "); //select * from temp_table
|
||||
sql.append(" ");
|
||||
sql.append(" myTable as ( ");
|
||||
sql.append(" select rseq3.numsecuencia as origen_tramo_inicial_seq , rseq4.numsecuencia as destino_tramo_final_seq , T.tramo_id as ttramo, t2.tramo_id as tt2tramo , tt.tra as traComb, tt.orig as ttorig, tt.dest as ttdest, ");
|
||||
sql.append(" tt.tradesc as traCombdesc, tt.traorig as traComborig, tt.tradest as traCombdest, tt.travia as tracombvia, tt.tracantkm as tracombcantkm, ");
|
||||
sql.append(" tt.tracantkmpago as tracombcantkmpago, tt.traactivo as tracombactivo, tt.trafecmodif as tracombfecmodif, tt.trausuario as tracombusuario ");
|
||||
sql.append(" from ruta_secuencia rseq3 ");
|
||||
sql.append(" inner join tramo t on t.tramo_id = rseq3.tramo_id and rseq3.ruta_id = :rutaId ");
|
||||
sql.append(" INNER JOIN ruta_secuencia rseq4 ON rseq3.ruta_id = rseq4.ruta_id ");
|
||||
sql.append(" INNER JOIN TRAMO T2 ON T2.TRAMO_ID = rseq4.tramo_id and rseq4.ruta_id = :rutaId ");
|
||||
sql.append(" inner join temp_table tt on t.ORIGEN_ID = tt.orig and T2.DESTINO_ID = tt.dest ");
|
||||
// sql.append(" -- and t.ORIGEN_ID = 19231 ");
|
||||
// sql.append(" -- AND T2.DESTINO_ID = 21708 ");
|
||||
sql.append(" ) , ");
|
||||
sql.append(" "); //select * from myTable
|
||||
sql.append(" caseta_tramo_da_ruta as ( ");
|
||||
sql.append(" select rcas.rutacaseta_id as rutacasetaid, rcas.rutasecuencia_id as rutasecuenciaId, rcas.casetaPeaje_Id, rseq.numSecuencia as numSecuencia, rseq.tramo_id as tramoid, rseq2.numSecuencia as numSecuencia2, ");
|
||||
sql.append(" casp.casetaPeaje_id as casetaPeajeid, casp.desccaseta as desccaseta, casp.importeida as importeida, casp.importevuelta as importevuelta, casp.activo as caspActivo, ");
|
||||
sql.append(" casp.fecmodif as caspfecmodif, casp.usuario_id as caspusuario ");
|
||||
sql.append(" from RUTA_CASETA rcas inner join RUTA_SECUENCIA rseq on rcas.RUTASECUENCIA_ID = rseq.RUTASECUENCIA_ID ");
|
||||
sql.append(" inner join RUTA_SECUENCIA rseq2 on rseq2.TRAMO_ID = rseq.TRAMO_ID ");
|
||||
sql.append(" inner join tramo tra on tra.tramo_id = rseq.TRAMO_ID and rseq.ruta_id = :rutaId ");
|
||||
sql.append(" inner join caseta_peaje casp on casp.casetapeaje_id = rcas.casetapeaje_id ");
|
||||
sql.append(" ) ");
|
||||
sql.append(" select distinct ");
|
||||
sql.append(" origen_tramo_inicial_seq, destino_tramo_final_seq, ttramo, tt2tramo, traComb, ttorig, ttdest, traCombdesc, traComborig, traCombdest, tracombvia, tracombcantkm, tracombcantkmpago, tracombactivo, tracombfecmodif, tracombusuario, rutacasetaid, rutasecuenciaId, casetaPeaje_Id, tramoid, casetaPeajeid, desccaseta, importeida, importevuelta, caspActivo, caspfecmodif, caspusuario ");
|
||||
sql.append(" from myTable my inner join caseta_tramo_da_ruta ctr on ctr.numSecuencia between my.origen_tramo_inicial_seq and my.destino_tramo_final_seq order by tracomb ");
|
||||
|
||||
|
||||
public List<Object> buscarCasetasPeajeWithinTramo(Ruta ruta) {
|
||||
StringBuilder sql = new StringBuilder()
|
||||
.append("with tramo_tmp as ( ")
|
||||
.append(" select t.origen_id as orig, t.destino_id as dest, t.tramo_id as tra, ")
|
||||
.append(" t.desctramo as tradesc, t.origen_id as traorig, t.destino_id as tradest, ")
|
||||
.append(" t.via_id as travia, t.cantkmreal as tracantkm, t.cantkmpagoconductor as tracantkmpago, ")
|
||||
.append(" t.activo as traactivo, t.fecmodif as trafecmodif, t.usuario_id as trausuario ")
|
||||
.append(" from ruta_combinacion rcom ")
|
||||
.append(" inner join ruta r on r.ruta_id = rcom.ruta_id ")
|
||||
.append(" inner join orgao_concedente oc on oc.orgaoconcedente_id = r.orgaoconcedente_id ")
|
||||
.append(" inner join tramo t on t.tramo_id = rcom.tramo_id ")
|
||||
.append(" where r.ruta_id = :rutaId ")
|
||||
.append("), mytable as ( ")
|
||||
.append(" select eiorig.icms as icms_orig, eidest.icms as icms_dest, ")
|
||||
.append(" rseqorig.numsecuencia as origen_tramo_inicial_seq, rseqdest.numsecuencia as destino_tramo_final_seq, ")
|
||||
.append(" torig.tramo_id as tramo_orig, tdest.tramo_id as tramo_dest, ")
|
||||
.append(" tt.tra as tracomb, tt.orig as ttorig, tt.dest as ttdest, ")
|
||||
.append(" tt.tradesc as tracombdesc, tt.traorig as tracomborig, tt.tradest as tracombdest, ")
|
||||
.append(" tt.travia as tracombvia, tt.tracantkm as tracombcantkm, tt.tracantkmpago as tracombcantkmpago, ")
|
||||
.append(" tt.traactivo as tracombactivo, tt.trafecmodif as tracombfecmodif, tt.trausuario as tracombusuario ")
|
||||
.append(" from ruta_secuencia rseqorig ")
|
||||
.append(" inner join tramo torig on torig.tramo_id = rseqorig.tramo_id and rseqorig.ruta_id = :rutaId ")
|
||||
.append(" inner join ruta_secuencia rseqdest on rseqorig.ruta_id = rseqdest.ruta_id ")
|
||||
.append(" inner join tramo tdest on tdest.tramo_id = rseqdest.tramo_id and rseqdest.ruta_id = :rutaId ")
|
||||
.append(" inner join ruta_empresa re on re.ruta_id = :rutaId ")
|
||||
.append(" inner join empresa e on re.empresa_id = e.empresa_id ")
|
||||
.append(" inner join parada porig on torig.origen_id = porig.parada_id ")
|
||||
.append(" inner join ciudad corig on corig.ciudad_id = porig.ciudad_id ")
|
||||
.append(" inner join estado eorig on eorig.estado_id = corig.estado_id ")
|
||||
.append(" inner join empresa_imposto eiorig on eiorig.empresa_id = e.empresa_id and eiorig.estado_id = eorig.estado_id ")
|
||||
.append(" inner join parada pdest on tdest.destino_id = pdest.parada_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 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("), caseta_tramo_da_ruta as ( ")
|
||||
.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(" casp.casetapeaje_id as casetapeajeid, casp.desccaseta as desccaseta, casp.importeida as importeida, ")
|
||||
.append(" casp.importevuelta as importevuelta, casp.activo as caspactivo, casp.fecmodif as caspfecmodif, ")
|
||||
.append(" casp.usuario_id as caspusuario ")
|
||||
.append(" from ruta_caseta rcas ")
|
||||
.append(" inner join ruta_secuencia rseq1 on rcas.rutasecuencia_id = rseq1.rutasecuencia_id ")
|
||||
.append(" inner join ruta_secuencia rseq2 on rseq2.tramo_id = rseq1.tramo_id ")
|
||||
.append(" inner join tramo tra on tra.tramo_id = rseq1.tramo_id and rseq1.ruta_id = :rutaId ")
|
||||
.append(" inner join caseta_peaje casp on casp.casetapeaje_id = rcas.casetapeaje_id ")
|
||||
.append(") ")
|
||||
.append("select distinct origen_tramo_inicial_seq, destino_tramo_final_seq, tramo_orig, tramo_dest, tracomb, ttorig, ttdest, ")
|
||||
.append(" tracombdesc, tracomborig, tracombdest, tracombvia, tracombcantkm, tracombcantkmpago, tracombactivo, ")
|
||||
.append(" tracombfecmodif, tracombusuario, rutacasetaid, rutasecuenciaid, casetapeaje_id, tramoid, casetapeajeid, ")
|
||||
.append(" desccaseta, importeida, importevuelta, caspactivo, caspfecmodif, caspusuario, icms_orig, icms_dest ")
|
||||
.append("from mytable my ")
|
||||
.append(" inner join caseta_tramo_da_ruta ctr on ctr.numsecuencia between my.origen_tramo_inicial_seq and my.destino_tramo_final_seq ")
|
||||
.append(" order by tracomb ");
|
||||
|
||||
Query query = getSession().createSQLQuery(sql.toString());
|
||||
query.setInteger("rutaId", ruta.getRutaId());
|
||||
query.setInteger("rutaId", ruta.getRutaId());
|
||||
|
@ -100,148 +123,54 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
|
|||
query.setInteger("rutaId", ruta.getRutaId());
|
||||
|
||||
return query.list();
|
||||
|
||||
|
||||
|
||||
// return new ArrayList<Object>();
|
||||
}
|
||||
|
||||
public int[] gerarSQLInserirPeajes(List<Ruta> lsRuta, OrgaoConcedente orgao){
|
||||
|
||||
int[] retorno = {};
|
||||
//pra cada ruta obter as combinacoes de tramos, cada tramo com sua lista de casetaPeaje
|
||||
|
||||
|
||||
StringBuilder updateSQL = new StringBuilder();
|
||||
updateSQL.append(" update PEAJE set activo = 0 where ORGAOCONCEDENTE_ID = ").append(orgao.getOrgaoConcedenteId()).append(" ");
|
||||
|
||||
List<String> lsSql = new ArrayList<String>();
|
||||
|
||||
for(Ruta r : lsRuta){
|
||||
List<Object> lsObj = buscarCasetasPeajeWithinTramo(r);
|
||||
if(null != lsObj && 0 != lsObj.size()){
|
||||
|
||||
for(Object oArr : lsObj){
|
||||
StringBuilder insertSQL = new StringBuilder();
|
||||
|
||||
Object o[] = (Object[])oArr;
|
||||
|
||||
PeajeVO pvo = new PeajeVO();
|
||||
// pvo.setPeajeId(o[]);
|
||||
pvo.setRutaId(r.getRutaId());
|
||||
pvo.setOrigenId(Integer.parseInt(""+o[8]));
|
||||
pvo.setDestinoId(Integer.parseInt(""+o[9]));
|
||||
pvo.setOrgaoId(orgao.getOrgaoConcedenteId());
|
||||
pvo.setCasetaPeajeId(Integer.parseInt(""+o[20]));//
|
||||
pvo.setCantAsientos(r.getCantAsientos());
|
||||
pvo.setCantEixos(r.getCantEixos());
|
||||
if(r.getIndSentidoIda()){
|
||||
pvo.setImportePeaje(BigDecimal.valueOf(Double.parseDouble(""+o[22])));//
|
||||
}else{
|
||||
pvo.setImportePeaje(BigDecimal.valueOf(Double.parseDouble(""+o[23])));//
|
||||
}
|
||||
pvo.setIndicePeaje(orgao.getIndicePeaje());
|
||||
pvo.setActivo(1);
|
||||
pvo.setFecmodif(Calendar.getInstance().getTime());
|
||||
pvo.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
|
||||
|
||||
Integer qtdEixos = r.getCantEixos();
|
||||
Integer cantAsientos = r.getCantAsientos();
|
||||
BigDecimal indice = orgao.getIndicePeaje();
|
||||
BigDecimal pedagioPuro = pvo.getImportePeaje();
|
||||
BigDecimal pedagioCalculado = BigDecimal.valueOf(0);
|
||||
if(Constantes.ORGAOS_CONCEDENTES_CALCULO_ARTESP.contains(orgao.getOrgaoConcedenteId())){ // 21 = artesp
|
||||
if(qtdEixos == null ){
|
||||
int[] x ={-3,r.getRutaId()};
|
||||
return x;}else{
|
||||
if(indice == null){
|
||||
int[] x ={-2,orgao.getOrgaoConcedenteId()};
|
||||
return x;
|
||||
}
|
||||
}
|
||||
pedagioCalculado = BigDecimal.valueOf(((pedagioPuro.doubleValue() * qtdEixos) / indice.doubleValue()));
|
||||
}else if(Constantes.ORGAOS_CONCEDENTES_CALCULO_ANTT.contains(orgao.getOrgaoConcedenteId())){// 3 = antt
|
||||
if(qtdEixos == null || cantAsientos == null ){
|
||||
int[] x ={-1,r.getRutaId()};
|
||||
return x;}else{
|
||||
if(indice == null){
|
||||
int[] x ={-2,orgao.getOrgaoConcedenteId()};
|
||||
return x;
|
||||
}
|
||||
}
|
||||
pedagioCalculado = BigDecimal.valueOf(((pedagioPuro.doubleValue() * qtdEixos) / ( indice.doubleValue() * cantAsientos)));
|
||||
}
|
||||
|
||||
|
||||
pvo.setImportePeaje(pedagioCalculado);
|
||||
|
||||
|
||||
insertSQL.append(" insert into PEAJE ( PEAJE_ID , RUTA_ID , ORIGEN_ID , DESTINO_ID , ORGAOCONCEDENTE_ID , CASETAPEAJE_ID ,");
|
||||
insertSQL.append(" CANTASIENTOS , CANTEIXOS , IMPORTEPEAJE , INDICEPEAJE , ACTIVO , FECMODIF , USUARIO_ID ) ");
|
||||
insertSQL.append(" values ");
|
||||
insertSQL.append(" ( PEAJE_SEQ.NEXTVAL , ");
|
||||
insertSQL.append(pvo.getRutaId()).append(" , ");
|
||||
insertSQL.append(pvo.getOrigenId()).append(" , ");
|
||||
insertSQL.append(pvo.getDestinoId()).append(" , ");
|
||||
insertSQL.append(pvo.getOrgaoId()).append(" , ");
|
||||
insertSQL.append(pvo.getCasetaPeajeId()).append(" , ");
|
||||
insertSQL.append(pvo.getCantAsientos()).append(" , ");
|
||||
insertSQL.append(pvo.getCantEixos()).append(" , ");
|
||||
insertSQL.append(pvo.getImportePeaje()).append(" , ");
|
||||
insertSQL.append(pvo.getIndicePeaje()).append(" , ");
|
||||
insertSQL.append(pvo.getActivo()).append(" , ");
|
||||
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(pvo.getFecmodif());
|
||||
StringBuilder minhaData = new StringBuilder().append("TO_DATE('").append(calendar.get(Calendar.YEAR)).append("/");
|
||||
minhaData.append(String.format("%02d",calendar.get(Calendar.MONTH)+1)).append("/");
|
||||
minhaData.append(String.format("%02d",calendar.get(Calendar.DAY_OF_MONTH))).append(" ");
|
||||
minhaData.append(String.format("%02d",calendar.get(Calendar.HOUR_OF_DAY))).append(":");
|
||||
minhaData.append(String.format("%02d",calendar.get(Calendar.MINUTE))).append(":");
|
||||
minhaData.append(String.format("%02d",calendar.get(Calendar.SECOND))).append("', 'yyyy/mm/dd hh24:mi:ss')");
|
||||
insertSQL.append(minhaData).append(" , "); //TO_DATE('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss')
|
||||
|
||||
insertSQL.append(pvo.getUsuarioId()).append(" ) ");
|
||||
|
||||
lsSql.add(insertSQL.toString());
|
||||
//------------------------------------------- existia um if(arredondamento)
|
||||
// if (trechoPracaPedagio.getSentido().equals("0")) {//IDA // esse trecho veio do calculo da artesp e NAO da antt
|
||||
// va = (tp_ida * linhaEixo.getQuantidadeEixos()) / divisor;
|
||||
// } else {
|
||||
// va = (tp_volta * linhaEixo.getQuantidadeEixos()) / divisor;
|
||||
// }
|
||||
//---------------------
|
||||
// // calculo IDA // esse veio da antt , e estava dentro
|
||||
// va = ((tp_ida * qtdEixos) / (lot * iap)); // de um if desnecessário pois o corpo do true ou false
|
||||
// //arredondamento para cima ou para baixo com 3 casas decimais // faziam a mesma coisa
|
||||
// BigDecimal bd = new BigDecimal(va);
|
||||
// bd = bd.setScale(3, BigDecimal.ROUND_HALF_UP);
|
||||
// va = bd.doubleValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Connection con = getSession().connection();
|
||||
|
||||
try {
|
||||
Statement stmt = con.createStatement();
|
||||
con.setAutoCommit(false);
|
||||
stmt.addBatch(updateSQL.toString());
|
||||
for(String x : lsSql){
|
||||
stmt.addBatch(x);
|
||||
}
|
||||
retorno = stmt.executeBatch();
|
||||
con.commit();
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return retorno;
|
||||
// pra cada ruta obter as combinacoes de tramos, cada tramo com sua lista de casetaPeaje
|
||||
public int[] gerarSQLInserirPeajes(List<Ruta> lsRuta, OrgaoConcedente orgao) {
|
||||
List<String> batchSql = new ArrayList<String>();
|
||||
for (Ruta ruta : lsRuta) {
|
||||
|
||||
List<Object> lsObj = buscarCasetasPeajeWithinTramo(ruta);
|
||||
if (null != lsObj) {
|
||||
|
||||
int i = 0;
|
||||
for (Object arrObj : lsObj) {
|
||||
PeajeVO pvo = PeajeVO.create(ruta, orgao, (Object[]) arrObj);
|
||||
batchSql.add(pvo.toInsertQuery());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
StringBuilder updateSql = new StringBuilder()
|
||||
.append("update peaje set activo = 0 ")
|
||||
.append("where orgaoconcedente_id = ")
|
||||
.append(orgao.getOrgaoConcedenteId());
|
||||
|
||||
int[] result = {};
|
||||
Connection con = getSession().connection();
|
||||
try {
|
||||
Statement stmt = con.createStatement();
|
||||
con.setAutoCommit(false);
|
||||
stmt.addBatch(updateSql.toString());
|
||||
for (String sql : batchSql) {
|
||||
stmt.addBatch(sql);
|
||||
}
|
||||
result = stmt.executeBatch();
|
||||
con.commit();
|
||||
}
|
||||
catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private class PeajeVO {
|
||||
private Integer peajeId;
|
||||
|
||||
private static class PeajeVO {
|
||||
|
||||
private static final int LOW_PRECISION = 2;
|
||||
private static final int HIGH_PRECISION = 10;
|
||||
private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
|
||||
|
||||
private Integer rutaId;
|
||||
private Integer origenId;
|
||||
private Integer destinoId;
|
||||
|
@ -255,109 +184,116 @@ public class CalcularPeajeHibernateDAO extends GenericHibernateDAO<String, Strin
|
|||
private Date fecmodif;
|
||||
private Integer usuarioId;
|
||||
|
||||
public Integer getPeajeId() {
|
||||
return peajeId;
|
||||
private static BigDecimal getCantEixos(Ruta ruta) {
|
||||
try {
|
||||
return new BigDecimal(ruta.getCantEixos());
|
||||
}
|
||||
catch (NullPointerException e) {
|
||||
throw new RuntimeException("rutaSemQtdEixos;" + ruta.getRutaId());
|
||||
}
|
||||
}
|
||||
private static BigDecimal getRutaCantAsientos(Ruta ruta) {
|
||||
try {
|
||||
return new BigDecimal(ruta.getCantAsientos());
|
||||
}
|
||||
catch (NullPointerException e) {
|
||||
throw new RuntimeException("rutaSemQtdAsientos;" + ruta.getRutaId());
|
||||
}
|
||||
}
|
||||
|
||||
public void setPeajeId(Integer peajeId) {
|
||||
this.peajeId = peajeId;
|
||||
private static BigDecimal getImporte(Ruta ruta, Object[] obj) {
|
||||
if (ruta.getIndSentidoIda()) {
|
||||
return new BigDecimal(obj[22].toString());
|
||||
}
|
||||
else {
|
||||
return new BigDecimal(obj[23].toString());
|
||||
}
|
||||
}
|
||||
|
||||
public Integer getRutaId() {
|
||||
return rutaId;
|
||||
private static BigDecimal getIcms(Ruta ruta, Object[] obj) {
|
||||
if (ruta.getIndSentidoIda()) {
|
||||
return new BigDecimal(obj[27].toString());
|
||||
}
|
||||
else {
|
||||
return new BigDecimal(obj[28].toString());
|
||||
}
|
||||
}
|
||||
|
||||
public void setRutaId(Integer rutaId) {
|
||||
this.rutaId = rutaId;
|
||||
// ((importe * rutaCantEixos) / (indice * rutaCantAsientos)) / ( 1 - icms)
|
||||
private static BigDecimal calculateImportePeajeANTT(BigDecimal importe, BigDecimal icms,
|
||||
BigDecimal indice, BigDecimal rutaCantEixos, BigDecimal rutaCantAsientos) {
|
||||
|
||||
return importe.multiply(rutaCantEixos)
|
||||
.divide(indice.multiply(rutaCantAsientos), HIGH_PRECISION, RoundingMode.HALF_UP)
|
||||
.divide(BigDecimal.ONE.min(icms), LOW_PRECISION, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
public Integer getOrigenId() {
|
||||
return origenId;
|
||||
// ((importe * rutaCantEixos) / (indice)) / ( 1 - icms)
|
||||
private static BigDecimal calculateImportePeajeARTESP(BigDecimal importe, BigDecimal icms,
|
||||
BigDecimal indice, BigDecimal rutaCantEixos) {
|
||||
return importe.multiply(rutaCantEixos)
|
||||
.divide(indice, HIGH_PRECISION, RoundingMode.HALF_UP)
|
||||
.divide(BigDecimal.ONE.min(icms), LOW_PRECISION, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
public void setOrigenId(Integer origenId) {
|
||||
this.origenId = origenId;
|
||||
private static BigDecimal calculateImportePeaje(Ruta ruta, OrgaoConcedente orgao, Object[] obj) {
|
||||
BigDecimal rutaCantEixos = getCantEixos(ruta);
|
||||
BigDecimal rutaCantAsientos = getRutaCantAsientos(ruta);
|
||||
BigDecimal indice = orgao.getIndicePeaje();
|
||||
BigDecimal importe = getImporte(ruta, obj);
|
||||
BigDecimal icms = getIcms(ruta, obj);
|
||||
|
||||
if (Constantes.ORGAOS_CONCEDENTES_CALCULO_ARTESP.contains(orgao.getOrgaoConcedenteId())) {
|
||||
return calculateImportePeajeARTESP(importe, icms, indice, rutaCantEixos);
|
||||
}
|
||||
if (Constantes.ORGAOS_CONCEDENTES_CALCULO_ANTT.contains(orgao.getOrgaoConcedenteId())) {
|
||||
return calculateImportePeajeANTT(importe, icms, indice, rutaCantEixos, rutaCantAsientos);
|
||||
}
|
||||
|
||||
return BigDecimal.ZERO; // TODO throw an exception? como calcular quando é outro orgao concendente?
|
||||
}
|
||||
|
||||
public Integer getDestinoId() {
|
||||
return destinoId;
|
||||
private static PeajeVO create(Ruta ruta, OrgaoConcedente orgao, Object[] obj) {
|
||||
PeajeVO pvo = new PeajeVO();
|
||||
|
||||
pvo.rutaId = ruta.getRutaId();
|
||||
pvo.cantEixos = ruta.getCantEixos();
|
||||
pvo.cantAsientos = ruta.getCantAsientos();
|
||||
pvo.origenId = Integer.parseInt(obj[8].toString());
|
||||
pvo.destinoId = Integer.parseInt(obj[9].toString());
|
||||
pvo.orgaoId = orgao.getOrgaoConcedenteId();
|
||||
pvo.casetaPeajeId = Integer.parseInt(obj[20].toString());
|
||||
pvo.indicePeaje = orgao.getIndicePeaje();
|
||||
pvo.importePeaje = calculateImportePeaje(ruta, orgao, obj);
|
||||
|
||||
pvo.activo = 1;
|
||||
pvo.fecmodif = new Date();
|
||||
pvo.usuarioId = UsuarioLogado.getUsuarioLogado().getUsuarioId();
|
||||
|
||||
return pvo;
|
||||
}
|
||||
|
||||
public void setDestinoId(Integer destinoId) {
|
||||
this.destinoId = destinoId;
|
||||
private String toInsertQuery() {
|
||||
return new StringBuilder()
|
||||
.append("insert into peaje ( peaje_id, ruta_id, origen_id, destino_id, ")
|
||||
.append(" orgaoconcedente_id, casetapeaje_id, cantasientos, canteixos, ")
|
||||
.append(" importepeaje, indicepeaje, activo, fecmodif, usuario_id ) ")
|
||||
.append("values ( peaje_seq.nextval, ")
|
||||
.append(rutaId).append(", ")
|
||||
.append(origenId).append(", ")
|
||||
.append(destinoId).append(", ")
|
||||
.append(orgaoId).append(", ")
|
||||
.append(casetaPeajeId).append(", ")
|
||||
.append(cantAsientos).append(", ")
|
||||
.append(cantEixos).append(", ")
|
||||
.append(importePeaje).append(", ")
|
||||
.append(indicePeaje).append(", ")
|
||||
.append(activo).append(", ")
|
||||
.append("to_date('")
|
||||
.append(SDF.format(fecmodif))
|
||||
.append("', 'yyyy/mm/dd hh24:mi:ss'), ")
|
||||
.append(usuarioId).append(" ) ")
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public Integer getOrgaoId() {
|
||||
return orgaoId;
|
||||
}
|
||||
|
||||
public void setOrgaoId(Integer orgaoId) {
|
||||
this.orgaoId = orgaoId;
|
||||
}
|
||||
|
||||
public Integer getCasetaPeajeId() {
|
||||
return casetaPeajeId;
|
||||
}
|
||||
|
||||
public void setCasetaPeajeId(Integer casetaPeajeId) {
|
||||
this.casetaPeajeId = casetaPeajeId;
|
||||
}
|
||||
|
||||
public Integer getCantAsientos() {
|
||||
return cantAsientos;
|
||||
}
|
||||
|
||||
public void setCantAsientos(Integer cantAsientos) {
|
||||
this.cantAsientos = cantAsientos;
|
||||
}
|
||||
|
||||
public Integer getCantEixos() {
|
||||
return cantEixos;
|
||||
}
|
||||
|
||||
public void setCantEixos(Integer cantEixos) {
|
||||
this.cantEixos = cantEixos;
|
||||
}
|
||||
|
||||
public BigDecimal getImportePeaje() {
|
||||
return importePeaje;
|
||||
}
|
||||
|
||||
public void setImportePeaje(BigDecimal importePeaje) {
|
||||
this.importePeaje = importePeaje;
|
||||
}
|
||||
|
||||
public BigDecimal getIndicePeaje() {
|
||||
return indicePeaje;
|
||||
}
|
||||
|
||||
public void setIndicePeaje(BigDecimal indicePeaje) {
|
||||
this.indicePeaje = indicePeaje;
|
||||
}
|
||||
|
||||
public Integer getActivo() {
|
||||
return activo;
|
||||
}
|
||||
|
||||
public void setActivo(Integer activo) {
|
||||
this.activo = activo;
|
||||
}
|
||||
|
||||
public Date getFecmodif() {
|
||||
return fecmodif;
|
||||
}
|
||||
|
||||
public void setFecmodif(Date fecmodif) {
|
||||
this.fecmodif = fecmodif;
|
||||
}
|
||||
|
||||
public Integer getUsuarioId() {
|
||||
return usuarioId;
|
||||
}
|
||||
|
||||
public void setUsuarioId(Integer usuarioId) {
|
||||
this.usuarioId = usuarioId;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue