package com.rjconsultores.ventaboletos.service.impl; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.rjconsultores.ventaboletos.dao.ConferenciaComissaoDAO; import com.rjconsultores.ventaboletos.dao.EmpresaDAO; import com.rjconsultores.ventaboletos.dao.PuntoVentaDAO; import com.rjconsultores.ventaboletos.entidad.ComEmpConferencia; import com.rjconsultores.ventaboletos.entidad.Conferencia; import com.rjconsultores.ventaboletos.entidad.Empresa; import com.rjconsultores.ventaboletos.entidad.LogConferencia; import com.rjconsultores.ventaboletos.entidad.PuntoVenta; import com.rjconsultores.ventaboletos.enums.IndStatusBoleto; import com.rjconsultores.ventaboletos.enums.comissao.BoletoStatusComissao; import com.rjconsultores.ventaboletos.exception.BusinessException; import com.rjconsultores.ventaboletos.service.ConferenciaComissaoService; import com.rjconsultores.ventaboletos.utilerias.DateUtil; import com.rjconsultores.ventaboletos.utilerias.LocaleUtil; import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado; import com.rjconsultores.ventaboletos.vo.comissao.BoletoComissao; import com.rjconsultores.ventaboletos.vo.comissao.ConferenciaComissaoVO; import com.rjconsultores.ventaboletos.vo.comissao.DiaConferenciaComissaoVO; import com.rjconsultores.ventaboletos.vo.comissao.EtiquetaMalote; import com.rjconsultores.ventaboletos.vo.comissao.EventosFinanceirosVO; import com.rjconsultores.ventaboletos.vo.comissao.FormapagoVO; import com.rjconsultores.ventaboletos.vo.comissao.LogConferenciaVO; import com.rjconsultores.ventaboletos.vo.comissao.OcdVO; import com.rjconsultores.ventaboletos.vo.comissao.ResumoComissao; @Service("conferenciaComissaoService") public class ConferenciaComissaoServiceImpl implements ConferenciaComissaoService { private static Logger log = Logger.getLogger(ConferenciaComissaoService.class); @Autowired private ConferenciaComissaoDAO conferenciaComissaoDAO; @Autowired private EmpresaDAO empresaDAO; @Autowired private PuntoVentaDAO puntoVentaDAO; @Override public List obtenerTodos() { return conferenciaComissaoDAO.obtenerTodos(); } @Override public Conferencia obtenerID(Long id) { return conferenciaComissaoDAO.obtenerID(id); } @Override public Conferencia suscribir(Conferencia entidad) { entidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId()); entidad.setFecmodif(Calendar.getInstance().getTime()); entidad.setActivo(Boolean.TRUE); return conferenciaComissaoDAO.suscribir(entidad); } @Override public Conferencia actualizacion(Conferencia entidad) { return conferenciaComissaoDAO.actualizacion(entidad); } @Override public void borrar(Conferencia entidad) { entidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId()); entidad.setFecmodif(Calendar.getInstance().getTime()); entidad.setActivo(Boolean.FALSE); conferenciaComissaoDAO.borrar(entidad); } @Override public List carregarConferenciaComissao(String competencia, Empresa empresa, PuntoVenta puntoVenta) throws BusinessException { return conferenciaComissaoDAO.carregarConferenciaComissao(competencia, empresa, puntoVenta); } @Override public Conferencia confirmarChegadaMalote(Conferencia conferencia) throws BusinessException { conferencia.setDatamalote(new Date()); conferencia.setUsuarioMaloteId(UsuarioLogado.getUsuarioLogado().getUsuarioId()); return conferenciaComissaoDAO.confirmarChegadaMalote(conferencia); } @Override public Conferencia suscribirOrActualizacion(Conferencia entidad) { entidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId()); entidad.setFecmodif(Calendar.getInstance().getTime()); entidad.setActivo(Boolean.TRUE); return conferenciaComissaoDAO.suscribirOrActualizacion(entidad); } @Override public Conferencia encerrarMovimentoDiario(Conferencia conferencia) throws BusinessException { conferencia.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId()); conferencia.setFecmodif(Calendar.getInstance().getTime()); return conferenciaComissaoDAO.encerrarMovimentoDiario(conferencia); } @Override public List carregarEventosFinanceiros(Conferencia conferencia) throws BusinessException { return conferenciaComissaoDAO.carregarEventosFinanceiros(conferencia); } @Override public List carregarLogConferencia(Conferencia conferencia) throws BusinessException { return conferenciaComissaoDAO.carregarLogConferencia(conferencia); } @Override public LogConferencia suscribirLogConferencia(LogConferencia logConferencia) throws BusinessException { logConferencia.setActivo(Boolean.TRUE); logConferencia.setUsuario(UsuarioLogado.getUsuarioLogado()); logConferencia.setFecmodif(new Date()); return conferenciaComissaoDAO.suscribirLogConferencia(logConferencia); } @Override public void borrarLogConferencia(LogConferencia logConferencia) throws BusinessException { logConferencia.setActivo(Boolean.FALSE); logConferencia.setUsuario(UsuarioLogado.getUsuarioLogado()); logConferencia.setFecmodif(new Date()); conferenciaComissaoDAO.borrarLogConferencia(logConferencia); } @Override public LogConferencia obtenerLogConferenciaID(Long logconferenciaId) { return conferenciaComissaoDAO.obtenerLogConferenciaID(logconferenciaId); } @Override public List carregarBilhetesComissao(List boletoComissaos, Conferencia conferencia, BoletoStatusComissao boletoStatusComissao) throws BusinessException { if(boletoComissaos == null) { boletoComissaos = conferenciaComissaoDAO.carregarBilhetesComissao(conferencia); } if(boletoStatusComissao == null) { return boletoComissaos; } Empresa empresa = empresaDAO.obtenerID(conferencia.getEmpresa().getEmpresaId()); ComEmpConferencia comEmpConferencia = null; if(empresa.getComEmpConferencias() != null && !empresa.getComEmpConferencias().isEmpty()) { comEmpConferencia = empresa.getComEmpConferencias().iterator().next(); } List retorno = new ArrayList(); switch (boletoStatusComissao) { case BOLETOS_VENDIDOS: retorno.addAll(carregarBilhetesVendidos(boletoComissaos, comEmpConferencia)); break; case BOLETO_MANUAL: retorno.addAll(carregarBilhetesManuais(boletoComissaos, comEmpConferencia)); break; case BOLETO_CANCELADO: retorno.addAll(carregarBilhetesCancelados(boletoComissaos, comEmpConferencia)); break; case BOLETO_DEVOLVIDO: retorno.addAll(carregarBilhetesDevolvidos(boletoComissaos, StringUtils.isNotBlank(conferencia.getNumfoliosistema()), comEmpConferencia)); break; case GAP_VENDIDOS: retorno.addAll(carregarBilhetesGapsVendidos(boletoComissaos, comEmpConferencia)); break; case GAP_CANCELADO: retorno.addAll(carregarBilhetesGapCancelados(boletoComissaos, comEmpConferencia)); break; case GAP_DEVOLVIDO: retorno.addAll(carregarBilhetesGapDevolvidos(boletoComissaos, comEmpConferencia)); break; default: break; } return retorno; } private Set carregarBilhetesManuais(List boletoComissaos, ComEmpConferencia comEmpConferencia) { Set boletos = new HashSet(); for (BoletoComissao boletoComissao : boletoComissaos) { if(boletoComissao.isBilhetesManualVendido()) { if(comEmpConferencia != null) { boletoComissao.setExigeConferenciaAba(comEmpConferencia.getIndbilhetemanual()); } boletos.add(boletoComissao); } } return boletos; } private Set carregarBilhetesVendidos(List boletoComissaos, ComEmpConferencia comEmpConferencia) { Set boletos = new HashSet(); for (BoletoComissao boletoComissao : boletoComissaos) { if(boletoComissao.isBilheteNormalVendido() || boletoComissao.isBilheteEntregaGap() || boletoComissao.isBilheteNormalChekin() || boletoComissao.isBilheteAberto() || boletoComissao.isBilheteEntregaAberto()) { if(comEmpConferencia != null) { boletoComissao.setExigeConferenciaAba(comEmpConferencia.getIndbilhetevendido()); } boletos.add(boletoComissao); } } return boletos; } private Set carregarBilhetesCancelados(List boletoComissaos, ComEmpConferencia comEmpConferencia) { Set boletos = new HashSet(); for (BoletoComissao boletoComissao : boletoComissaos) { if(boletoComissao.isBilheteNormalCancelado() || boletoComissao.isBilheteNormalTrocadoCancelado() || boletoComissao.isBilheteAbertoCancelado() || boletoComissao.isBilheteEntregaGapCancelado()) { if(comEmpConferencia != null) { boletoComissao.setExigeConferenciaAba(comEmpConferencia.getIndbilhetecancelado()); } boletos.add(boletoComissao); } } return boletos; } private Set carregarBilhetesDevolvidos(List boletoComissaos, boolean pesquisa, ComEmpConferencia comEmpConferencia) { Set boletos = new HashSet(); for (BoletoComissao boletoComissao : boletoComissaos) { if(boletoComissao.isBilheteNormalDevolvido() || boletoComissao.isBilheteNormalTrocado() || boletoComissao.isBilheteNormalTrocadoDevolvido() || boletoComissao.isBilheteNormalTransferidoDevolvido() || boletoComissao.isBilheteAbertoDevolvido() || boletoComissao.isBilheteEntregaGapDevolvido() || boletoComissao.isBilheteEntregaGapTrocado()) { boletoComissao.setExigeConferenciaDevolvido(boletoComissao.getIndcarboletosdevolvidosconf() != null && !boletoComissao.getIndcarboletosdevolvidosconf()); if(comEmpConferencia != null) { boletoComissao.setExigeConferenciaAba(comEmpConferencia.getIndbilhetedevolvido()); } if(pesquisa || ((boletoComissao.getIndcarboletosdevolvidosconf() != null && boletoComissao.getIndcarboletosdevolvidosconf()) || boletoComissao.getLogconferenciaId() != null)) { boletos.add(boletoComissao); } } } return boletos; } private Set carregarBilhetesGapsVendidos(List boletoComissaos, ComEmpConferencia comEmpConferencia) { Set boletos = new HashSet(); for (BoletoComissao boletoComissao : boletoComissaos) { if(boletoComissao.isBilheteGapVendido()) { if(comEmpConferencia != null) { boletoComissao.setExigeConferenciaAba(comEmpConferencia.getIndgapvendido()); } boletos.add(boletoComissao); } } return boletos; } private Set carregarBilhetesGapCancelados(List boletoComissaos, ComEmpConferencia comEmpConferencia) { Set boletos = new HashSet(); for (BoletoComissao boletoComissao : boletoComissaos) { if(boletoComissao.isBilheteGapCancelado()) { if(comEmpConferencia != null) { boletoComissao.setExigeConferenciaAba(comEmpConferencia.getIndgapcancelado()); } boletos.add(boletoComissao); } } return boletos; } private Set carregarBilhetesGapDevolvidos(List boletoComissaos, ComEmpConferencia comEmpConferencia) { Set boletos = new HashSet(); for (BoletoComissao boletoComissao : boletoComissaos) { if(boletoComissao.isBilheteGapDevolvido() || boletoComissao.isBilheteGapTrocado()) { if(comEmpConferencia != null) { boletoComissao.setExigeConferenciaAba(comEmpConferencia.getIndgapdevolvido()); } boletos.add(boletoComissao); } } return boletos; } @Override public List carregarOcds(Conferencia conferencia) throws BusinessException { return conferenciaComissaoDAO.carregarOcds(conferencia); } @Override public Conferencia reabrirMovimentoDiario(Conferencia conferencia) throws BusinessException { conferencia.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId()); conferencia.setFecmodif(Calendar.getInstance().getTime()); return conferenciaComissaoDAO.reabrirMovimentoDiario(conferencia); } @Override public Conferencia obtenerConferenciaDataMovimento(Date datamovimento, Integer puntoventaId, Integer empresaId) throws BusinessException { return conferenciaComissaoDAO.obtenerConferenciaDataMovimento(datamovimento, puntoventaId, empresaId); } @Override public boolean isBilhetesSemConferencia(List lsBoletoComissao) throws BusinessException { for (BoletoComissao boletoComissao : lsBoletoComissao) { if((boletoComissao.isExigeConferencia() || boletoComissao.isExigeConferenciaAba() || boletoComissao.isExigeConferenciaBoletoDevolvido()) && (!boletoComissao.isConferido() && boletoComissao.getLogconferenciaId() == null)) { return true; } } return false; } @Override public boolean isEventosFinanceirosSemConferencia(List lsEventosFinanceiros) throws BusinessException { for (EventosFinanceirosVO eventosFinanceiros : lsEventosFinanceiros) { if((eventosFinanceiros.isExigeConferencia() || (eventosFinanceiros.getExigeConferenciaAba() != null && eventosFinanceiros.getExigeConferenciaAba())) && (!eventosFinanceiros.isConferido() && eventosFinanceiros.getLogconferenciaId() == null)) { return true; } } return false; } @Override public BigDecimal totalizarBoletoComissao(List lsBoletoComissao, IndStatusBoleto... statusBilhete) throws BusinessException { List lStatusBilhete = null; if(statusBilhete != null && statusBilhete.length > 0) { lStatusBilhete = Arrays.asList(statusBilhete); } BigDecimal total = BigDecimal.ZERO; if(lsBoletoComissao != null) { for (BoletoComissao boletoComissao : lsBoletoComissao) { if(lStatusBilhete == null || lStatusBilhete.contains(boletoComissao.getIndstatusboletoEnum())) { total = total.add(boletoComissao.getTotal()); } } } return total; } @Override public Map totalizarBoletoComissaoDevolvidosTrocaOcd(List lsBoletoComissao) throws BusinessException { BigDecimal totalDevolvidos = BigDecimal.ZERO; BigDecimal totalTroca = BigDecimal.ZERO; BigDecimal totalOcd = BigDecimal.ZERO; if(lsBoletoComissao != null) { for (BoletoComissao boletoComissao : lsBoletoComissao) { if(boletoComissao.isOcd()) { totalOcd = totalOcd.add(boletoComissao.getTotal()); } else if(boletoComissao.isStatusCancelado() && boletoComissao.isMotivocancelacionDevolvido()) { totalDevolvidos = totalDevolvidos.add(boletoComissao.getTotal()); } else if(boletoComissao.isStatusCancelado() && boletoComissao.isMotivocancelacionTrocado()) { totalTroca = totalTroca.add(boletoComissao.getTotal()); } } } Map maps = new HashMap(); maps.put(BoletoStatusComissao.BOLETO_DEVOLVIDO, totalDevolvidos); maps.put(BoletoStatusComissao.BOLETO_DEVOLVIDO_OCD, totalOcd); maps.put(BoletoStatusComissao.BOLETO_DEVOLVIDO_TROCADO, totalTroca); return maps; } @Override public BigDecimal totalizarEventosFinanceiros(List lsEventosFinanceiros) throws BusinessException { BigDecimal total = BigDecimal.ZERO; if(lsEventosFinanceiros != null) { for (EventosFinanceirosVO eventosFinanceiros : lsEventosFinanceiros) { total = total.add(eventosFinanceiros.getImpingreso()); } } return total; } @Override public OcdVO totalizarOcd(List lsOcd) throws BusinessException { OcdVO total = new OcdVO(); total.setValorPagar(BigDecimal.ZERO); total.setPenalizacion(BigDecimal.ZERO); if(lsOcd != null) { for (OcdVO ocd : lsOcd) { total.setValorPagar(total.getValorPagar().add(ocd.getValorPagar())); } } return total; } @Override public ResumoComissao gerarResumo(Conferencia conferencia, List boletoComissaos, List lsEventosFinanceiros, BigDecimal totalBilhetesManual, BigDecimal totalBilhetesVendidos, BigDecimal totalBilhetesCancelados, BigDecimal totalBilhetesDevolvidos, BigDecimal totalBilhetesGap, BigDecimal totalBilhetesGapCancelados, BigDecimal totalBilhetesGapDevolvidos, BigDecimal totalCreditosEventoFinanceiros, BigDecimal totalDebitosEventoFinanceiros, OcdVO totalOcd) throws BusinessException { ResumoComissao resumoComissao = new ResumoComissao(); resumoComissao.setTotalDevolvidos(totalBilhetesDevolvidos.add(totalBilhetesGapDevolvidos)); resumoComissao.setTotalCancelados(totalBilhetesCancelados.add(totalBilhetesGapCancelados)); resumoComissao.setTotalVendas(totalBilhetesVendidos.add(totalBilhetesManual).add(totalBilhetesGap)); resumoComissao.setTotalReceitas(totalCreditosEventoFinanceiros); resumoComissao.setTotalDespesas(totalDebitosEventoFinanceiros); resumoComissao.setTotalOcd(totalOcd); carregarTotalFormapago(resumoComissao, boletoComissaos, lsEventosFinanceiros); resumoComissao.setValorDeposito(conferenciaComissaoDAO.carregarValorDepositoContaCorrente(conferencia.getEmpresa().getEmpresaId(), conferencia.getPuntoVenta().getPuntoventaId(), conferencia.getDatamovimento())); return resumoComissao; } private void carregarTotalFormapago(ResumoComissao resumoComissao, List boletoComissaos, List lsEventosFinanceiros) { List totalFormapagos = new ArrayList(); for (BoletoComissao boletoComissao : boletoComissaos) { if(boletoComissao.isTotalizarFormapago()) { for (FormapagoVO formapagoBoleto : boletoComissao.getFormapagos()) { FormapagoVO formapago; if(totalFormapagos.contains(formapagoBoleto)) { formapago = totalFormapagos.get(totalFormapagos.indexOf(formapagoBoleto)); } else { formapago = new FormapagoVO(formapagoBoleto.getFormapagoId(), formapagoBoleto.getDescpago(), null, BigDecimal.ZERO); totalFormapagos.add(formapagoBoleto); } if(boletoComissao.isStatusVendido() || boletoComissao.isStatusTroca()) { formapago.add(formapagoBoleto.getImporte()); } else if(boletoComissao.isStatusCancelado() && (boletoComissao.isMotivocancelacionCancelado() || boletoComissao.isMotivocancelacionDevolvido() || boletoComissao.isMotivocancelacionTrocado())) { formapago.subtract(formapagoBoleto.getImporte()); } } } } for (EventosFinanceirosVO eventosFinanceiros : lsEventosFinanceiros) { FormapagoVO formapagoEventoFinanceiro = new FormapagoVO(eventosFinanceiros.getFormapagoId(), eventosFinanceiros.getDescpago(), null, BigDecimal.ZERO); if(totalFormapagos.contains(formapagoEventoFinanceiro)) { formapagoEventoFinanceiro = totalFormapagos.get(totalFormapagos.indexOf(formapagoEventoFinanceiro)); } else { totalFormapagos.add(formapagoEventoFinanceiro); } if(eventosFinanceiros.isCredito()) { formapagoEventoFinanceiro.add(eventosFinanceiros.getImpingreso()); } else if(eventosFinanceiros.isDebito()) { formapagoEventoFinanceiro.subtract(eventosFinanceiros.getImpingreso()); } } resumoComissao.setTotalFormapago(totalFormapagos); } @Override public EtiquetaMalote decodificarEtiquetaMalote(String codigoBarras) throws BusinessException { try { EtiquetaMalote etiquetaMalote = new EtiquetaMalote(); log.debug("Empresa: " + codigoBarras.substring(0,4)); etiquetaMalote.setEmpresaId(Integer.valueOf(codigoBarras.substring(0,4))); log.debug("Punto Venta: " + codigoBarras.substring(4,9)); etiquetaMalote.setPuntoventaId(Integer.valueOf(codigoBarras.substring(4,9))); log.debug("Numero Malote: " + codigoBarras.substring(9,19)); etiquetaMalote.setNumeroMalote(codigoBarras.substring(9,19)); log.debug("Data inicial: " + codigoBarras.substring(19,27)); etiquetaMalote.setDataInicial(DateUtil.getDateFromString(codigoBarras.substring(19,27), "ddMMyyyy")); log.debug("Data final: " + codigoBarras.substring(27,35)); etiquetaMalote.setDataFinal(DateUtil.getDateFromString(codigoBarras.substring(27,35), "ddMMyyyy")); Empresa empresa = empresaDAO.obtenerID(etiquetaMalote.getEmpresaId()); if(empresa == null) { throw new BusinessException("recebimentoMaloteController.msg.erro.empresaNaoLocalizada"); } etiquetaMalote.setNombempresa(empresa.getNombempresa()); PuntoVenta puntoVenta = puntoVentaDAO.obtenerID(etiquetaMalote.getPuntoventaId()); if(puntoVenta == null) { throw new BusinessException("recebimentoMaloteController.msg.erro.puntoVentaNaoLocalizado"); } etiquetaMalote.setNombpuntoventa(puntoVenta.getNombpuntoventa()); return etiquetaMalote; } catch (BusinessException e) { log.error(e.getMessage(), e); throw e; } catch (Exception e) { log.error(e.getMessage(), e); } return null; } @Override public void confirmarChegadaMalote(EtiquetaMalote etiquetaMalote) throws BusinessException { try { Calendar cDataInicial = Calendar.getInstance(LocaleUtil.getLocale()); cDataInicial.setTime(DateUtil.normalizar(etiquetaMalote.getDataInicial())); Calendar cDataFinal = Calendar.getInstance(LocaleUtil.getLocale()); cDataFinal.setTime(DateUtil.normalizar(etiquetaMalote.getDataFinal())); Empresa empresa = empresaDAO.obtenerID(etiquetaMalote.getEmpresaId()); PuntoVenta puntoVenta = puntoVentaDAO.obtenerID(etiquetaMalote.getPuntoventaId()); while (cDataInicial.compareTo(cDataFinal) <= 0) { Conferencia conferencia = obtenerConferenciaDataMovimento(cDataInicial.getTime(), etiquetaMalote.getPuntoventaId(), etiquetaMalote.getEmpresaId()); if(conferencia == null) { conferencia = new Conferencia(); conferencia.setDatamovimento(cDataInicial.getTime()); conferencia.setEmpresa(empresa); conferencia.setPuntoVenta(puntoVenta); conferencia = suscribirOrActualizacion(conferencia); } cDataInicial.add(Calendar.DAY_OF_MONTH, 1); if(conferencia != null && conferencia.getIndmaloterecebido()) { continue; } confirmarChegadaMalote(conferencia); } } catch (BusinessException e) { log.error(e.getMessage(), e); throw e; } catch (Exception e) { log.error(e.getMessage(), e); } } @Override public boolean isConferenciaCompetenciaEncerrada(String competencia, Empresa empresa, PuntoVenta puntoVenta) throws BusinessException { return conferenciaComissaoDAO.isConferenciaCompetenciaEncerrada(competencia, empresa, puntoVenta); } @Override public boolean isOcdSemConferencia(List lsOcd) throws BusinessException { for (OcdVO ocd : lsOcd) { if(ocd.isExigeConferenciaAba() && (!ocd.isConferido() && ocd.getLogconferenciaId() == null)) { return true; } } return false; } @Override public DiaConferenciaComissaoVO carregarConferenciaRegistrada(Date datamovimento, Empresa empresa, PuntoVenta puntoVenta) throws BusinessException { return conferenciaComissaoDAO.carregarConferenciaRegistrada(datamovimento, empresa, puntoVenta); } }