From a71d9bd0ea86a29a32b5a41ec9f6082d4fa7deb2 Mon Sep 17 00:00:00 2001 From: Fabio Date: Sat, 19 Oct 2024 12:17:31 -0300 Subject: [PATCH] Legalizacao Massiva por arquivo feat #AL-5054 --- pom.xml | 2 +- .../entidad/ContratoCorporativo.java | 5 + .../ventaboletos/entidad/Transportadora.java | 7 +- .../ventaboletos/service/VoucherService.java | 2 + .../MudancaMassivaContratoServiceImpl.java | 5 +- .../service/impl/VoucherServiceImpl.java | 200 ++++++++++++++---- .../configuracioneccomerciales/VoucherVO.java | 11 + 7 files changed, 183 insertions(+), 49 deletions(-) diff --git a/pom.xml b/pom.xml index cfe69d57a..9d9453277 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 br.com.rjconsultores ModelWeb - 1.120.0 + 1.121.0 diff --git a/src/com/rjconsultores/ventaboletos/entidad/ContratoCorporativo.java b/src/com/rjconsultores/ventaboletos/entidad/ContratoCorporativo.java index e0ad74489..e7b9a9f1e 100644 --- a/src/com/rjconsultores/ventaboletos/entidad/ContratoCorporativo.java +++ b/src/com/rjconsultores/ventaboletos/entidad/ContratoCorporativo.java @@ -145,6 +145,11 @@ public class ContratoCorporativo implements Serializable { this.clienteCorporativoId = clienteCorporativoId; this.nomeClienteCorp = nomeClienteCorp; } + + public ContratoCorporativo(String numContrato) { + this(); + this.numContrato = numContrato; + } public ContratoCorporativo(Long clienteCorporativoId, Date fecModif) { this(); diff --git a/src/com/rjconsultores/ventaboletos/entidad/Transportadora.java b/src/com/rjconsultores/ventaboletos/entidad/Transportadora.java index 7faaee1cc..8bf64d6ae 100644 --- a/src/com/rjconsultores/ventaboletos/entidad/Transportadora.java +++ b/src/com/rjconsultores/ventaboletos/entidad/Transportadora.java @@ -33,7 +33,8 @@ public class Transportadora implements Serializable{ private static final long serialVersionUID = -8441764653321195183L; @Id - @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "TRANSPORTADORA_SEQ") + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "TRANSPORTADORA_SEQ") + @Renderizado() @Column(name = "TRANSPORTADORA_ID") private Long transportadoraId; @@ -70,4 +71,8 @@ public class Transportadora implements Serializable{ public Transportadora(String nomeTransportadora) { this.nomeTransportadora = nomeTransportadora; } + + public Transportadora(Long transportadoraId) { + this.transportadoraId = transportadoraId; + } } \ No newline at end of file diff --git a/src/com/rjconsultores/ventaboletos/service/VoucherService.java b/src/com/rjconsultores/ventaboletos/service/VoucherService.java index fa3e92b20..64a87f9af 100644 --- a/src/com/rjconsultores/ventaboletos/service/VoucherService.java +++ b/src/com/rjconsultores/ventaboletos/service/VoucherService.java @@ -19,4 +19,6 @@ public interface VoucherService extends GenericService { public List faturar(List lista, Long numFatura, Date datCorte); + public List lerArquivo(String data) throws Exception; + } diff --git a/src/com/rjconsultores/ventaboletos/service/impl/MudancaMassivaContratoServiceImpl.java b/src/com/rjconsultores/ventaboletos/service/impl/MudancaMassivaContratoServiceImpl.java index 3418a8390..385e32588 100644 --- a/src/com/rjconsultores/ventaboletos/service/impl/MudancaMassivaContratoServiceImpl.java +++ b/src/com/rjconsultores/ventaboletos/service/impl/MudancaMassivaContratoServiceImpl.java @@ -121,7 +121,7 @@ public class MudancaMassivaContratoServiceImpl implements MudancaMassivaContrato if( linha.startsWith("ID;")) { continue; } - + TarifaGrupoContratoVO tarifa = new TarifaGrupoContratoVO(); String[] split = linha.split(";"); tarifa.setTarifaGrupoContratoId( split[0] ); @@ -137,7 +137,6 @@ public class MudancaMassivaContratoServiceImpl implements MudancaMassivaContrato return arquivo; } - @Override @Transactional(readOnly = false) @@ -174,7 +173,7 @@ public class MudancaMassivaContratoServiceImpl implements MudancaMassivaContrato return novo; } - private InputStream converterInputStream(String in) throws IOException { + private InputStream converterInputStream(String in){ byte[] buff = in.getBytes(StandardCharsets.UTF_8); return new ByteArrayInputStream(buff); } diff --git a/src/com/rjconsultores/ventaboletos/service/impl/VoucherServiceImpl.java b/src/com/rjconsultores/ventaboletos/service/impl/VoucherServiceImpl.java index 0b9d5de0e..83d01ddd3 100644 --- a/src/com/rjconsultores/ventaboletos/service/impl/VoucherServiceImpl.java +++ b/src/com/rjconsultores/ventaboletos/service/impl/VoucherServiceImpl.java @@ -1,6 +1,13 @@ package com.rjconsultores.ventaboletos.service.impl; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; +import java.text.ParseException; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; @@ -13,20 +20,26 @@ import org.springframework.transaction.annotation.Transactional; import org.zkoss.lang.Strings; import org.zkoss.util.resource.Labels; +import com.rjconsultores.ventaboletos.dao.TransportadoraDAO; import com.rjconsultores.ventaboletos.dao.VoucherDAO; +import com.rjconsultores.ventaboletos.entidad.ContratoCorporativo; import com.rjconsultores.ventaboletos.entidad.Parada; import com.rjconsultores.ventaboletos.entidad.Transportadora; import com.rjconsultores.ventaboletos.entidad.Voucher; import com.rjconsultores.ventaboletos.enums.SituacaoVoucher; import com.rjconsultores.ventaboletos.service.VoucherService; +import com.rjconsultores.ventaboletos.utilerias.DateUtil; import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado; import com.rjconsultores.ventaboletos.vo.configuracioneccomerciales.VoucherVO; @Service("voucherService") public class VoucherServiceImpl implements VoucherService { - + @Autowired private VoucherDAO voucherDAO; + + @Autowired + private TransportadoraDAO transportadoraDAO; public List obtenerTodos() { return voucherDAO.obtenerTodos(); @@ -117,58 +130,84 @@ public class VoucherServiceImpl implements VoucherService { null, null); + return efetuarLegalizacao(numContrato, transportadora, valor, origem, destino, dados); + } + + private List efetuarLegalizacao(String numContrato, + Transportadora transportadora, + BigDecimal valor, + Parada origem, + Parada destino, + List dados) { + List retorno = new ArrayList(); for (Voucher vou : dados) { - VoucherVO vo; - - if( StringUtils.isNotEmpty(numContrato) && !vou.getContrato().getNumContrato().equals(numContrato)) { - vo = VoucherVO.converteVoucher(vou); - vo.setMensagem(Labels.getLabel("legalizacaoMassivaController.MSG.contratoDiferente")); - retorno.add(vo); - continue; - } - - if( !vou.getStatus().equals(SituacaoVoucher.EMITIDO.getValor())) { - vo = VoucherVO.converteVoucher(vou); - vo.setMensagem(Labels.getLabel("legalizacaoMassivaController.MSG.statusDiferente")); - retorno.add(vo); - continue; - } - - if( origem != null && !vou.getOrigenId().equals(origem.getParadaId())) { - vo = VoucherVO.converteVoucher(vou); - vo.setMensagem(Labels.getLabel("legalizacaoMassivaController.MSG.origemDiferente")); - retorno.add(vo); - continue; - } - - if( destino != null && !vou.getDestinoId().equals(destino.getParadaId())) { - vo = VoucherVO.converteVoucher(vou); - vo.setMensagem(Labels.getLabel("legalizacaoMassivaController.MSG.destinoDiferente")); - retorno.add(vo); - continue; - } - - try { - vou.setValorLegalizado(valor); - vou.setTransportadora(transportadora); - vou.setStatus(SituacaoVoucher.LEGALIZADO.getValor()); - vou.setDataLegaliza(Calendar.getInstance().getTime()); - vou = voucherDAO.actualizacion(vou); - vo = VoucherVO.converteVoucher(vou); - vo.setMensagem(Labels.getLabel("label.sucesso")); - }catch (Exception e) { - vo = VoucherVO.converteVoucher(vou); - vo.setMensagem(e.getMessage()); + if( validarLegalizacao(numContrato, origem, destino, retorno, vou) ) { + retorno.add(legalizar(transportadora, valor, vou)); } - - retorno.add(vo); } return retorno; } + private boolean validarLegalizacao(String numContrato, + Parada origem, + Parada destino, + List retorno, + Voucher vou) { + VoucherVO vo; + if( StringUtils.isNotEmpty(numContrato) && !vou.getContrato().getNumContrato().equals(numContrato)) { + vo = VoucherVO.converteVoucher(vou); + vo.setMensagem(Labels.getLabel("legalizacaoMassivaController.MSG.contratoDiferente")); + retorno.add(vo); + return false; + } + + if( !vou.getStatus().equals(SituacaoVoucher.EMITIDO.getValor())) { + vo = VoucherVO.converteVoucher(vou); + vo.setMensagem(Labels.getLabel("legalizacaoMassivaController.MSG.statusDiferente")); + retorno.add(vo); + return false; + } + + if( origem != null && !vou.getOrigenId().equals(origem.getParadaId())) { + vo = VoucherVO.converteVoucher(vou); + vo.setMensagem(Labels.getLabel("legalizacaoMassivaController.MSG.origemDiferente")); + retorno.add(vo); + return false; + } + + if( destino != null && !vou.getDestinoId().equals(destino.getParadaId())) { + vo = VoucherVO.converteVoucher(vou); + vo.setMensagem(Labels.getLabel("legalizacaoMassivaController.MSG.destinoDiferente")); + retorno.add(vo); + return false; + } + + return true; + } + + private VoucherVO legalizar(Transportadora transportadora, BigDecimal valor, Voucher vou) { + VoucherVO vo; + + try { + vou.setValorLegalizado(valor); + vou.setTransportadora(transportadora); + vou.setStatus(SituacaoVoucher.LEGALIZADO.getValor()); + vou.setDataLegaliza(Calendar.getInstance().getTime()); + + vou = voucherDAO.actualizacion(vou); + vo = VoucherVO.converteVoucher(vou); + vo.setMensagem(Labels.getLabel("label.sucesso")); + }catch (Exception e) { + vo = VoucherVO.converteVoucher(vou); + vo.setMensagem(e.getMessage()); + } + + return vo; + } + @Override @Transactional public List faturar(List lista, Long numFatura, Date datCorte) { @@ -196,4 +235,77 @@ public class VoucherServiceImpl implements VoucherService { return lista; } + + @Override + @Transactional + public List lerArquivo(String stream) throws Exception{ + List retorno = new ArrayList(); + + List arquivosEnviados = processarArquivoEnviado(stream); + + for (VoucherVO vo : arquivosEnviados) { + if( StringUtils.isNotEmpty(vo.getMensagem()) ) { + retorno.add(vo); + continue; + } + + Voucher voucher = voucherDAO.obtenerID(vo.getVoucherId()); + + if ( !validarLegalizacao(null, null, null, retorno, voucher)) { + continue; + } + + Transportadora transportadora = transportadoraDAO.obtenerID(vo.getTransportadora().getTransportadoraId()); + + retorno.add(legalizar(transportadora, vo.getValorLegalizado(), voucher)); + } + + return retorno; + } + + private List processarArquivoEnviado(String stream) throws IOException, ParseException { + String linha = null; + List arquivo = new ArrayList(); + + InputStream inputStream = converterInputStream(stream); + + try( BufferedReader leitor = new BufferedReader(new InputStreamReader(inputStream))){ + + while ((linha = leitor.readLine()) != null) { + if( linha.isEmpty() || linha.startsWith("ID;")) { + continue; + } + + VoucherVO voucher = new VoucherVO(); + String[] split = linha.split(";"); + voucher.setVoucherId( Long.valueOf(split[0]) ); + voucher.setContrato( new ContratoCorporativo(split[1]) ); + voucher.setDataValidade( DateUtil.getDateFromString(split[2], DateUtil.ddMMaaaa)); + voucher.setStatus(SituacaoVoucher.EMITIDO.getValor()); + voucher.setDescOrigem(split[3]); + voucher.setDescDestino(split[4]); + voucher.setValorLicitado( new BigDecimal(split[5].replace(',', '.')) ); + + if( split[6].equals("INFORMAR")) { + voucher.setMensagem(Labels.getLabel("legalizacaoMassivaController.MSG.faltaValor")); + }else { + voucher.setValorLegalizado( new BigDecimal(split[6].replace(',', '.')) ); + } + + if( split[7].equals("INFORMAR")) { + voucher.setMensagem(Labels.getLabel("legalizacaoMassivaController.MSG.faltaTransportadora")); + }else { + voucher.setTransportadora( new Transportadora( Long.valueOf( split[7])) ); + } + arquivo.add(voucher); + } + } + + return arquivo; + } + + private InputStream converterInputStream(String in){ + byte[] buff = in.getBytes(StandardCharsets.UTF_8); + return new ByteArrayInputStream(buff); + } } diff --git a/src/com/rjconsultores/ventaboletos/vo/configuracioneccomerciales/VoucherVO.java b/src/com/rjconsultores/ventaboletos/vo/configuracioneccomerciales/VoucherVO.java index 835bb02a8..7feefbcdc 100644 --- a/src/com/rjconsultores/ventaboletos/vo/configuracioneccomerciales/VoucherVO.java +++ b/src/com/rjconsultores/ventaboletos/vo/configuracioneccomerciales/VoucherVO.java @@ -12,6 +12,7 @@ import com.rjconsultores.ventaboletos.entidad.ContratoCorporativo; import com.rjconsultores.ventaboletos.entidad.Transportadora; import com.rjconsultores.ventaboletos.entidad.Voucher; import com.rjconsultores.ventaboletos.enums.SituacaoVoucher; +import com.rjconsultores.ventaboletos.utilerias.DateUtil; import lombok.AllArgsConstructor; import lombok.Getter; @@ -149,4 +150,14 @@ public class VoucherVO implements Serializable { return new VoucherVO(pai); } + public String toCsv() { + String base = "\r\n%s;%s;%s;%s;%s;INFORMAR;INFORMAR"; + return String.format(base, getVoucherId(), + getNumContrato(), + DateUtil.getStringDate(getDataValidade()), + getDescOrigem(), + getDescDestino(), + getValorLicitado()); + } + }