Rev 635 | Rev 686 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 149 | espaco | 1 | package br.com.ec.domain.service.venda.impl; |
| 2 | |||
| 3 | import java.util.ArrayList; |
||
| 4 | import java.util.Date; |
||
| 5 | import java.util.List; |
||
| 6 | import java.util.Map; |
||
| 7 | |||
| 8 | import org.springframework.beans.factory.annotation.Autowired; |
||
| 9 | import org.springframework.stereotype.Service; |
||
| 10 | |||
| 195 | espaco | 11 | import br.com.ec.core.consulta.ParametrosConsulta; |
| 12 | import br.com.ec.core.exception.NegocioException; |
||
| 13 | import br.com.ec.core.generic.AbstractService; |
||
| 14 | import br.com.ec.core.generic.GenericRepository; |
||
| 15 | import br.com.ec.core.util.DataUtils; |
||
| 16 | import br.com.ec.core.util.StringUtil; |
||
| 17 | import br.com.ec.core.util.VerificadorUtil; |
||
| 18 | import br.com.ec.core.validador.Validador; |
||
| 149 | espaco | 19 | import br.com.ec.domain.dto.GraficoAcumuladoDTO; |
| 20 | import br.com.ec.domain.dto.ParametrosConsultaLancamentosDTO; |
||
| 21 | import br.com.ec.domain.dto.ParametrosConsultaModelosMaisVendidosDTO; |
||
| 22 | import br.com.ec.domain.dto.ParametrosConsultaParaComprasTransferenciasDTO; |
||
| 23 | import br.com.ec.domain.dto.ParametrosConsultaPedidoDTO; |
||
| 24 | import br.com.ec.domain.dto.ParametrosConsultaPorPeriodoDTO; |
||
| 25 | import br.com.ec.domain.dto.ParametrosConsultaProdutosVendidosDTO; |
||
| 26 | import br.com.ec.domain.dto.ParametrosConsultaVendasDTO; |
||
| 27 | import br.com.ec.domain.dto.ParametrosConsultaVendasVivoDTO; |
||
| 28 | import br.com.ec.domain.dto.ParametrosVendaDTO; |
||
| 29 | import br.com.ec.domain.dto.RankingComprasTransferenciasDTO; |
||
| 30 | import br.com.ec.domain.dto.RankingComprasTransferenciasEstiloDTO; |
||
| 600 | blopes | 31 | import br.com.ec.domain.dto.RankingComprasTransferenciasSubtipoProdutoDTO; |
| 149 | espaco | 32 | import br.com.ec.domain.dto.RankingLojaDTO; |
| 33 | import br.com.ec.domain.dto.RankingModeloDTO; |
||
| 34 | import br.com.ec.domain.dto.RankingProdutoDTO; |
||
| 35 | import br.com.ec.domain.dto.RankingVendedorDTO; |
||
| 36 | import br.com.ec.domain.dto.VendasDTO; |
||
| 37 | import br.com.ec.domain.model.Avaliacao; |
||
| 38 | import br.com.ec.domain.model.AvaliacaoFuncionario; |
||
| 356 | espaco | 39 | import br.com.ec.domain.model.AvaliacaoProduto; |
| 149 | espaco | 40 | import br.com.ec.domain.model.Categoria; |
| 41 | import br.com.ec.domain.model.Cupom; |
||
| 42 | import br.com.ec.domain.model.EstoqueAuditoria; |
||
| 43 | import br.com.ec.domain.model.FormaPagamento; |
||
| 44 | import br.com.ec.domain.model.Lancamento; |
||
| 45 | import br.com.ec.domain.model.Loja; |
||
| 46 | import br.com.ec.domain.model.Marca; |
||
| 47 | import br.com.ec.domain.model.Modelo; |
||
| 48 | import br.com.ec.domain.model.NotaFiscal; |
||
| 49 | import br.com.ec.domain.model.Pedido; |
||
| 50 | import br.com.ec.domain.model.PedidoHistorico; |
||
| 51 | import br.com.ec.domain.model.Produto; |
||
| 600 | blopes | 52 | import br.com.ec.domain.model.SubtipoProduto; |
| 149 | espaco | 53 | import br.com.ec.domain.model.Usuario; |
| 577 | blopes | 54 | import br.com.ec.domain.model.UsuarioPerfil; |
| 149 | espaco | 55 | import br.com.ec.domain.model.Venda; |
| 56 | import br.com.ec.domain.model.VendaFormaPagamento; |
||
| 57 | import br.com.ec.domain.model.Vendedor; |
||
| 58 | import br.com.ec.domain.model.Vigencia; |
||
| 59 | import br.com.ec.domain.model.tipos.TipoEmitirNotaFiscal; |
||
| 60 | import br.com.ec.domain.model.tipos.TipoEstiloProduto; |
||
| 61 | import br.com.ec.domain.model.tipos.TipoEstoqueAuditoria; |
||
| 62 | import br.com.ec.domain.model.tipos.TipoOrdenacaoPlanejamentoCompras; |
||
| 63 | import br.com.ec.domain.model.tipos.TipoProduto; |
||
| 64 | import br.com.ec.domain.model.tipos.TipoSituacaoPedido; |
||
| 65 | import br.com.ec.domain.model.tipos.TipoSituacaoVenda; |
||
| 66 | import br.com.ec.domain.service.avaliacao.AvaliacaoService; |
||
| 67 | import br.com.ec.domain.service.avaliacaofuncionario.AvaliacaoFuncionarioService; |
||
| 68 | import br.com.ec.domain.service.avaliacaoloja.AvaliacaoLojaService; |
||
| 69 | import br.com.ec.domain.service.bancohoras.BancoHorasService; |
||
| 685 | blopes | 70 | import br.com.ec.domain.service.cashback.CashbackService; |
| 149 | espaco | 71 | import br.com.ec.domain.service.cupom.CupomService; |
| 72 | import br.com.ec.domain.service.email.impl.GerenciadorEmailImpl; |
||
| 73 | import br.com.ec.domain.service.estoqueauditoria.EstoqueAuditoriaService; |
||
| 74 | import br.com.ec.domain.service.funcionario.FuncionarioService; |
||
| 75 | import br.com.ec.domain.service.lancamento.LancamentoService; |
||
| 76 | import br.com.ec.domain.service.loja.LojaService; |
||
| 77 | import br.com.ec.domain.service.modelo.ModeloService; |
||
| 78 | import br.com.ec.domain.service.parcela.ParcelaService; |
||
| 79 | import br.com.ec.domain.service.pedido.PedidoService; |
||
| 635 | blopes | 80 | import br.com.ec.domain.service.posvenda.PosVendaService; |
| 149 | espaco | 81 | import br.com.ec.domain.service.produto.ProdutoService; |
| 82 | import br.com.ec.domain.service.produtodaloja.ProdutoLojaService; |
||
| 577 | blopes | 83 | import br.com.ec.domain.service.usuario.UsuarioService; |
| 149 | espaco | 84 | import br.com.ec.domain.service.venda.VendaService; |
| 85 | import br.com.ec.domain.service.vendaformapagamento.VendaFormaPagamentoService; |
||
| 86 | import br.com.ec.domain.service.vendavivo.VendaVivoService; |
||
| 87 | import br.com.ec.domain.service.vendedor.VendedorService; |
||
| 356 | espaco | 88 | import br.com.ec.domain.service.vigencia.VigenciaService; |
| 149 | espaco | 89 | import br.com.ec.domain.shared.ConstantesSEC; |
| 90 | import br.com.ec.domain.shared.Ordenador; |
||
| 91 | import br.com.ec.domain.shared.OrdenadorImpl; |
||
| 92 | import br.com.ec.infrastructure.repository.NotaFiscalRepository; |
||
| 93 | import br.com.ec.infrastructure.repository.VendaRepository; |
||
| 94 | |||
| 95 | @Service |
||
| 96 | public class VendaServiceImpl extends AbstractService<Venda> implements VendaService { |
||
| 97 | |||
| 98 | private VendaRepository vendaRepository; |
||
| 99 | |||
| 100 | private LancamentoService lancamentoService; |
||
| 101 | private VendaFormaPagamentoService vendaFormaPagamentoService; |
||
| 102 | private ProdutoService produtoService; |
||
| 103 | private ProdutoLojaService produtoLojaService; |
||
| 104 | private VendedorService vendedorService; |
||
| 105 | private FuncionarioService funcionarioService; |
||
| 106 | private LojaService lojaService; |
||
| 107 | private PedidoService pedidoService; |
||
| 108 | private ParcelaService parcelaService; |
||
| 109 | private AvaliacaoService avaliacaoService; |
||
| 110 | private AvaliacaoFuncionarioService avaliacaoFuncionarioService; |
||
| 111 | private AvaliacaoLojaService avaliacaoLojaService; |
||
| 112 | private VendaVivoService vendaVivoService; |
||
| 113 | private ModeloService modeloService; |
||
| 114 | private CupomService cupomService; |
||
| 115 | private BancoHorasService bancoHorasService; |
||
| 116 | private EstoqueAuditoriaService estoqueAuditoriaService; |
||
| 356 | espaco | 117 | private VigenciaService vigenciaService; |
| 577 | blopes | 118 | private UsuarioService usuarioService; |
| 635 | blopes | 119 | private PosVendaService posVendaService; |
| 685 | blopes | 120 | private CashbackService cashbackService; |
| 149 | espaco | 121 | |
| 122 | private Ordenador ordenador; |
||
| 123 | private NotaFiscalRepository notaFiscalRepository; |
||
| 124 | |||
| 125 | @Autowired |
||
| 126 | public VendaServiceImpl(Validador validador, VendaRepository vendaRepository, LancamentoService lancamentoService, |
||
| 127 | VendaFormaPagamentoService vendaFormaPagamentoService, ProdutoService produtoService, ProdutoLojaService produtoLojaService, |
||
| 128 | VendedorService vendedorService, FuncionarioService funcionarioService, LojaService lojaService, PedidoService pedidoService, |
||
| 129 | ParcelaService parcelaService, AvaliacaoService avaliacaoService, AvaliacaoFuncionarioService avaliacaoFuncionarioService, |
||
| 130 | AvaliacaoLojaService avaliacaoLojaService, VendaVivoService vendaVivoService, ModeloService modeloService, CupomService cupomService, |
||
| 356 | espaco | 131 | BancoHorasService bancoHorasService, EstoqueAuditoriaService estoqueAuditoriaService, VigenciaService vigenciaService, |
| 685 | blopes | 132 | UsuarioService usuarioService, PosVendaService posVendaService, CashbackService cashbackService, NotaFiscalRepository notaFiscalRepository) { |
| 149 | espaco | 133 | super(validador); |
| 134 | this.vendaRepository = vendaRepository; |
||
| 135 | this.funcionarioService = funcionarioService; |
||
| 136 | this.lancamentoService = lancamentoService; |
||
| 137 | this.vendaFormaPagamentoService = vendaFormaPagamentoService; |
||
| 138 | this.produtoService = produtoService; |
||
| 139 | this.produtoLojaService = produtoLojaService; |
||
| 140 | this.vendedorService = vendedorService; |
||
| 141 | this.lojaService = lojaService; |
||
| 142 | this.pedidoService = pedidoService; |
||
| 143 | this.parcelaService = parcelaService; |
||
| 144 | this.avaliacaoService = avaliacaoService; |
||
| 145 | this.avaliacaoFuncionarioService = avaliacaoFuncionarioService; |
||
| 146 | this.avaliacaoLojaService = avaliacaoLojaService; |
||
| 147 | this.vendaVivoService = vendaVivoService; |
||
| 148 | this.modeloService = modeloService; |
||
| 149 | this.cupomService = cupomService; |
||
| 150 | this.bancoHorasService = bancoHorasService; |
||
| 151 | this.estoqueAuditoriaService = estoqueAuditoriaService; |
||
| 356 | espaco | 152 | this.vigenciaService = vigenciaService; |
| 577 | blopes | 153 | this.usuarioService = usuarioService; |
| 635 | blopes | 154 | this.posVendaService = posVendaService; |
| 685 | blopes | 155 | this.cashbackService = cashbackService; |
| 356 | espaco | 156 | this.notaFiscalRepository = notaFiscalRepository; |
| 149 | espaco | 157 | this.ordenador = new OrdenadorImpl(); |
| 158 | } |
||
| 159 | |||
| 160 | @Override |
||
| 161 | protected GenericRepository<Venda> getRepository() { |
||
| 162 | return vendaRepository; |
||
| 163 | } |
||
| 164 | |||
| 165 | @Override |
||
| 166 | protected void regrasNegocioCadastrar(Venda venda) { |
||
| 167 | if (VerificadorUtil.estaNulo(venda.getVendedor())) { |
||
| 168 | throw new NegocioException("Obrigatório informar o vendedor."); |
||
| 169 | } |
||
| 170 | venda.setDataVenda(DataUtils.getDataAtual()); |
||
| 171 | venda.setTipoSituacao(TipoSituacaoVenda.NOVO.getValor()); |
||
| 172 | } |
||
| 173 | |||
| 174 | /***************************************************************/ |
||
| 175 | |||
| 176 | @Override |
||
| 177 | public Venda detalharVenda(Venda venda) { |
||
| 178 | return vendaRepository.detalharVenda(venda); |
||
| 179 | } |
||
| 180 | |||
| 181 | @Override |
||
| 182 | public Venda detalharVendaCompleta(Venda venda) { |
||
| 183 | return vendaRepository.detalharVendaCompleta(venda); |
||
| 184 | } |
||
| 185 | |||
| 186 | public void excluirVenda(Venda venda, Usuario usuario) { |
||
| 187 | List<NotaFiscal> notasVinculadas = desvincularNotaFiscalEmitida(venda, usuario); |
||
| 188 | verificarSeExisteLancamentoFinanceiro(venda); |
||
| 189 | reporEstoqueAoExcluirVenda(venda); |
||
| 635 | blopes | 190 | excluirPosVenda(venda); |
| 149 | espaco | 191 | excluirPagamentos(venda); |
| 237 | espaco | 192 | desvincularPedido(venda, usuario); |
| 149 | espaco | 193 | super.excluir(venda); |
| 194 | enviarEmail(venda, usuario, notasVinculadas); |
||
| 195 | } |
||
| 196 | |||
| 197 | private List<NotaFiscal> desvincularNotaFiscalEmitida(Venda venda, Usuario usuario) { |
||
| 198 | List<NotaFiscal> notasFiscais = notaFiscalRepository.consultarNotasFiscais(venda); |
||
| 199 | for (NotaFiscal nf : notasFiscais) { |
||
| 200 | nf.setObservacaoVenda("VENDA ID: " + venda.getSequencial() + " em " + |
||
| 201 | DataUtils.converterDataComHorarioParaString(venda.getDataVenda()) + ". " + |
||
| 202 | venda.getJustificativaParaExcluir()); |
||
| 203 | notaFiscalRepository.desvincularVenda(nf); |
||
| 204 | } |
||
| 205 | return notasFiscais; |
||
| 206 | } |
||
| 207 | |||
| 208 | private void verificarSeExisteLancamentoFinanceiro(Venda venda) { |
||
| 209 | if (parcelaService.verificarPossuiParcela(venda)) { |
||
| 210 | throw new NegocioException("Não é permitido excluir uma venda com lançamentos financeiros."); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | public void reporEstoqueAoExcluirVenda(Venda venda) { |
||
| 215 | for (Lancamento lancamento : venda.getLancamentos()) { |
||
| 305 | espaco | 216 | alterarEstoqueProduto(1, venda.getLoja(), lancamento.getProduto().getCodigo(), lancamento.getTipoPersonalizacao()); |
| 149 | espaco | 217 | } |
| 218 | } |
||
| 219 | |||
| 635 | blopes | 220 | public void excluirPosVenda(Venda venda) { |
| 221 | posVendaService.excluirPosVenda(venda.getSequencial()); |
||
| 222 | } |
||
| 223 | |||
| 149 | espaco | 224 | private void excluirPagamentos(Venda venda) { |
| 225 | for (VendaFormaPagamento vendaFormaPagamento : venda.getListaVendaFormaPagamentos()) { |
||
| 226 | // vendaFormaPagamentoService.excluirPorId(vendaFormaPagamento); |
||
| 227 | vendaFormaPagamentoService.excluir(vendaFormaPagamento); |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 237 | espaco | 231 | private void desvincularPedido(Venda venda, Usuario usuario) { |
| 232 | Pedido pedidoCompleto = pedidoService.consultarPedidoCompletoDaVenda(venda); |
||
| 246 | espaco | 233 | if (VerificadorUtil.naoEstaNulo(pedidoCompleto)) { |
| 234 | PedidoHistorico pedidoHistorico = new PedidoHistorico(); |
||
| 235 | pedidoHistorico.setPedido(pedidoCompleto); |
||
| 236 | pedidoHistorico.setDataHistorico(DataUtils.getDataAtual()); |
||
| 237 | pedidoHistorico.setObservacao("VENDA: " + venda.getSequencial() + " FOI ESTORNADA."); |
||
| 238 | if (VerificadorUtil.naoEstaNuloOuVazio(venda.getJustificativaParaExcluir())) { |
||
| 239 | pedidoHistorico.setObservacao(pedidoHistorico.getObservacao() + " JUSTIFICATIVA: " + venda.getJustificativaParaExcluir()); |
||
| 240 | } |
||
| 241 | pedidoHistorico.setTipoSituacao(TipoSituacaoPedido.PENDENTE.getValor()); |
||
| 242 | pedidoHistorico.setUsuario(usuario); |
||
| 243 | pedidoCompleto.getHistorico().add(pedidoHistorico); |
||
| 244 | pedidoCompleto.setTipoSituacao(TipoSituacaoPedido.PENDENTE.getValor()); |
||
| 245 | pedidoCompleto.setDataUltimaAlteracaoSituacao(DataUtils.getDataAtual()); |
||
| 246 | pedidoCompleto.setVenda(null); |
||
| 247 | pedidoService.alterar(pedidoCompleto); |
||
| 237 | espaco | 248 | } |
| 249 | } |
||
| 250 | |||
| 149 | espaco | 251 | private void enviarEmail(Venda venda, Usuario usuario, List<NotaFiscal> notasFiscaisVinculadas) { |
| 252 | String diaAtual = DataUtils.converterDataParaString(DataUtils.getDataAtual()); |
||
| 253 | StringBuilder conteudo = new StringBuilder(); |
||
| 254 | conteudo.append(montarDadosDaVenda(venda, usuario, venda.getLancamentos(), venda.getVendaFormaPagamentos())); |
||
| 255 | if (!notasFiscaisVinculadas.isEmpty()) { |
||
| 256 | conteudo.append("--------------------------------------------------\n\n"); |
||
| 257 | conteudo.append("NOTAS FISCAIS DESVINCULADAS:\n"); |
||
| 258 | for (NotaFiscal nota : notasFiscaisVinculadas) { |
||
| 259 | conteudo.append("NÚMERO: " + nota.getNumeroNotaFiscal() + "\n"); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | new GerenciadorEmailImpl() |
||
| 263 | .comEmailsDestino(ConstantesSEC.DESTINATARIOS_EMAIL_DIRECAO) |
||
| 264 | .comAssunto("ESPAÇO CASE - OPERAÇÃO DE EXCLUSÃO DE VENDA: " + diaAtual) |
||
| 265 | .comConteudo(conteudo.toString()) |
||
| 266 | .enviar(); |
||
| 267 | } |
||
| 268 | |||
| 269 | @Override |
||
| 270 | public Venda cadastrarVendaPeloPDV(ParametrosVendaDTO parametrosVenda) { |
||
| 271 | verificarLancamentos(parametrosVenda.getLancamentos()); |
||
| 272 | // lancarExcecaoCasoFreteContraditorio(parametrosVenda); |
||
| 273 | lancarExcecaoCasoNaoSejaInformadoCamposObrigatorios(parametrosVenda); |
||
| 274 | lancarExcecaoSeValoresNaoSaoIguais(parametrosVenda); |
||
| 275 | Venda venda = new Venda(parametrosVenda); |
||
| 276 | cadastrar(venda); |
||
| 277 | cadastrarLancamentos(parametrosVenda, venda); |
||
| 278 | cadastrarPagamentos(parametrosVenda, venda); |
||
| 635 | blopes | 279 | cadastrarPosVenda(venda); |
| 685 | blopes | 280 | gerarCashback(parametrosVenda, venda); |
| 149 | espaco | 281 | alterarSituacaoPedido(parametrosVenda.getPedidoVinculado(), venda); |
| 282 | enviarAlertasPorEmail(parametrosVenda, venda); |
||
| 283 | venda.verificarSePermiteEmissaoNotaFiscal(); |
||
| 284 | venda.setEmitirNotaFiscal(verificarEmissaoNotaFiscal(parametrosVenda, venda)); |
||
| 285 | return venda; |
||
| 286 | } |
||
| 287 | |||
| 288 | private Boolean verificarEmissaoNotaFiscal(ParametrosVendaDTO parametrosVenda, Venda venda) { |
||
| 289 | if (venda.getPermitirEmissaoNotaFiscal() == false) {return false;} |
||
| 290 | Boolean emitir = null; |
||
| 291 | emitir = naoEmitirLojaSemPermissao(parametrosVenda); |
||
| 292 | if (VerificadorUtil.naoEstaNulo(emitir)) {return emitir;} |
||
| 293 | |||
| 294 | emitir = naoEmitirCpfCnpjInvalido(parametrosVenda); |
||
| 295 | if (VerificadorUtil.naoEstaNulo(emitir)) {return emitir;} |
||
| 296 | |||
| 297 | emitir = emitirLojaComEmissaoObrigatoria(parametrosVenda); |
||
| 298 | if (VerificadorUtil.naoEstaNulo(emitir)) {return emitir;} |
||
| 299 | |||
| 300 | emitir = emitirProdutoComEmissaoObrigatoria(parametrosVenda); |
||
| 301 | if (VerificadorUtil.naoEstaNulo(emitir)) {return emitir;} |
||
| 302 | |||
| 303 | emitir = emitirVendasAcimaQuinhetosReais(parametrosVenda); |
||
| 304 | if (VerificadorUtil.naoEstaNulo(emitir)) {return emitir;} |
||
| 305 | |||
| 306 | emitir = emitirVendaPagamentoCartao(parametrosVenda); |
||
| 307 | if (VerificadorUtil.naoEstaNulo(emitir)) {return emitir;} |
||
| 308 | return false; |
||
| 309 | } |
||
| 310 | |||
| 311 | private Boolean naoEmitirLojaSemPermissao(ParametrosVendaDTO parametrosVenda) { |
||
| 312 | if (parametrosVenda.getLoja().getTipoEmitirNotaFiscal().equals(TipoEmitirNotaFiscal.NAO_PERMITIR_EMISSAO.getValor()) || |
||
| 313 | parametrosVenda.getLoja().getTipoEmitirNotaFiscal().equals(TipoEmitirNotaFiscal.NAO_EMITIR.getValor())) { |
||
| 314 | return false; |
||
| 315 | } |
||
| 316 | return null; |
||
| 317 | } |
||
| 318 | |||
| 319 | private Boolean naoEmitirCpfCnpjInvalido(ParametrosVendaDTO parametrosVenda) { |
||
| 320 | if (VerificadorUtil.naoEstaNulo(parametrosVenda.getCliente())) { |
||
| 321 | if (!parametrosVenda.getCliente().cpfCnpjEhValido()) { |
||
| 322 | return false; |
||
| 323 | } |
||
| 324 | } |
||
| 325 | return null; |
||
| 326 | } |
||
| 327 | |||
| 328 | private Boolean emitirLojaComEmissaoObrigatoria(ParametrosVendaDTO parametrosVenda) { |
||
| 329 | if (parametrosVenda.getLoja().getTipoEmitirNotaFiscal().equals(TipoEmitirNotaFiscal.TODAS_VENDAS.getValor())) { |
||
| 330 | return true; |
||
| 331 | } |
||
| 332 | return null; |
||
| 333 | } |
||
| 334 | |||
| 335 | |||
| 336 | |||
| 337 | private Boolean emitirProdutoComEmissaoObrigatoria(ParametrosVendaDTO parametrosVenda) { |
||
| 338 | for (Lancamento lancamento : parametrosVenda.getLancamentos()) { |
||
| 339 | if (lancamento.getProduto().getIndicadorEmissaoNotaFiscal()) { |
||
| 340 | return true; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | return null; |
||
| 344 | } |
||
| 345 | |||
| 346 | private Boolean emitirVendasAcimaQuinhetosReais(ParametrosVendaDTO parametrosVenda) { |
||
| 347 | Double valorVenda = new Double(0.0); |
||
| 348 | for (Lancamento lancamento : parametrosVenda.getLancamentos()) { |
||
| 349 | valorVenda = valorVenda + lancamento.getValorVenda(); |
||
| 350 | } |
||
| 351 | if (valorVenda > 499.9) { |
||
| 352 | return true; |
||
| 353 | } |
||
| 354 | return null; |
||
| 355 | } |
||
| 356 | |||
| 357 | private Boolean emitirVendaPagamentoCartao(ParametrosVendaDTO parametrosVenda) { |
||
| 358 | if (parametrosVenda.getLoja().getTipoEmitirNotaFiscal().equals(TipoEmitirNotaFiscal.VENDAS_OBRIGATORIAS.getValor())) { |
||
| 359 | for (VendaFormaPagamento pagamento : parametrosVenda.getLancamentosPagamentos()) { |
||
| 360 | if (pagamento.getFormaPagamento().formaPagamentoEhCredito() || |
||
| 361 | pagamento.getFormaPagamento().formaPagamentoEhDebito() || |
||
| 362 | pagamento.getFormaPagamento().formaPagamentoEhPagseguro()) { |
||
| 363 | return true; |
||
| 364 | } |
||
| 365 | } |
||
| 366 | } |
||
| 367 | return null; |
||
| 368 | } |
||
| 369 | |||
| 370 | private void alterarSituacaoPedido(Pedido pedidoVinculado, Venda venda) { |
||
| 371 | if (VerificadorUtil.naoEstaNulo(pedidoVinculado)) { |
||
| 372 | PedidoHistorico pedidoHistorico = new PedidoHistorico(); |
||
| 373 | pedidoHistorico.setDataHistorico(DataUtils.getDataAtual()); |
||
| 374 | pedidoHistorico.setObservacao("VENDA: " + venda.getSequencial()); |
||
| 375 | pedidoHistorico.setTipoSituacao(TipoSituacaoPedido.FINALIZADO_COM_VENDAS.getValor()); |
||
| 376 | pedidoHistorico.setUsuario(venda.getUsuario()); |
||
| 377 | pedidoHistorico.setPedido(pedidoVinculado); |
||
| 378 | if (VerificadorUtil.estaNulo(pedidoVinculado.getHistorico())) { |
||
| 379 | pedidoVinculado.setHistorico(new ArrayList<PedidoHistorico>()); |
||
| 380 | } |
||
| 381 | pedidoVinculado.getHistorico().add(pedidoHistorico); |
||
| 382 | pedidoVinculado.setTipoSituacao(TipoSituacaoPedido.FINALIZADO_COM_VENDAS.getValor()); |
||
| 383 | pedidoVinculado.setDataUltimaAlteracaoSituacao(DataUtils.getDataAtual()); |
||
| 165 | espaco | 384 | pedidoVinculado.setVenda(venda); |
| 149 | espaco | 385 | pedidoService.alterar(pedidoVinculado); |
| 386 | } |
||
| 387 | } |
||
| 388 | |||
| 389 | private void enviarAlertasPorEmail(ParametrosVendaDTO parametrosVenda, Venda venda) { |
||
| 577 | blopes | 390 | Boolean enviarAlerta = false; |
| 391 | String emailsDestino = ""; |
||
| 392 | String assuntoAlerta = ""; |
||
| 149 | espaco | 393 | if (descontosEmProdutosSuperiores(parametrosVenda)) { |
| 577 | blopes | 394 | enviarAlerta = true; |
| 395 | emailsDestino = ConstantesSEC.DESTINATARIOS_EMAIL_SUPERVISAO; |
||
| 396 | assuntoAlerta = "ESPAÇO CASE - VENDA COM DESCONTO SUPERIOR : " + DataUtils.converterDataParaString(DataUtils.getDataAtual()); |
||
| 149 | espaco | 397 | } |
| 398 | if (produtosComDescontoECupom(parametrosVenda)) { |
||
| 577 | blopes | 399 | enviarAlerta = true; |
| 400 | emailsDestino = ConstantesSEC.DESTINATARIOS_EMAIL_SUPERVISAO; |
||
| 401 | assuntoAlerta = "ESPAÇO CASE - VENDA COM PRODUTO COM DESCONTO E CUPOM PROMOCIONAL : " + DataUtils.converterDataParaString(DataUtils.getDataAtual()); |
||
| 402 | } |
||
| 403 | if (possuiMaisDeDuasFormasDePagamento(parametrosVenda)) { |
||
| 404 | enviarAlerta = true; |
||
| 405 | emailsDestino = ConstantesSEC.DESTINATARIOS_EMAIL_SUPERVISAO; |
||
| 406 | assuntoAlerta = "ESPAÇO CASE - VENDA SUSPEITA COM MAIS DE 2 FORMAS DE PAGAMENTO : " + DataUtils.converterDataParaString(DataUtils.getDataAtual()); |
||
| 407 | } |
||
| 408 | if (enviarAlerta) { |
||
| 149 | espaco | 409 | new GerenciadorEmailImpl() |
| 577 | blopes | 410 | .comEmailsDestino(emailsDestino) |
| 411 | .comAssunto(assuntoAlerta) |
||
| 149 | espaco | 412 | .comConteudo(montarDadosDaVenda(venda, venda.getUsuario(), parametrosVenda.getLancamentos(), parametrosVenda.getLancamentosPagamentos())) |
| 413 | .enviar(); |
||
| 414 | } |
||
| 415 | } |
||
| 416 | |||
| 577 | blopes | 417 | private Boolean possuiMaisDeDuasFormasDePagamento(ParametrosVendaDTO parametrosVenda) { |
| 418 | return parametrosVenda.getLancamentosPagamentos().size() > 2; |
||
| 419 | } |
||
| 420 | |||
| 149 | espaco | 421 | /* |
| 422 | private void enviarAlertasPorEmail(ParametrosVendaDTO parametrosVenda, Venda venda) { |
||
| 423 | if (pagamentoDividido(parametrosVenda)) { |
||
| 424 | if (descontoEmProdutos(parametrosVenda)) { |
||
| 425 | String diaAtual = DataUtils.converterDataParaString(DataUtils.getDataAtual()); |
||
| 426 | new GerenciadorEmailImpl() |
||
| 427 | .comEmailsDestino(ConstantesSEC.DESTINATARIOS_EMAIL_DIRECAO) |
||
| 428 | .comAssunto("ESPAÇO CASE - VENDA DIVIDIDA E COM DESCONTO : " + diaAtual) |
||
| 429 | .comConteudo(montarDadosDaVenda(venda, venda.getUsuario(), parametrosVenda.getLancamentos(), parametrosVenda.getLancamentosPagamentos())) |
||
| 430 | .enviar(); |
||
| 431 | } |
||
| 432 | } |
||
| 433 | } |
||
| 320 | espaco | 434 | |
| 149 | espaco | 435 | |
| 436 | private Boolean pagamentoDividido(ParametrosVendaDTO parametrosVenda) { |
||
| 437 | for (VendaFormaPagamento pagamento : parametrosVenda.getLancamentosPagamentos()) { |
||
| 438 | if (pagamento.getFormaPagamento().formaPagamentoEhCreditoDividido()) { |
||
| 439 | return true; |
||
| 440 | } |
||
| 441 | } |
||
| 442 | return false; |
||
| 443 | } |
||
| 444 | |||
| 445 | private boolean descontoEmProdutos(ParametrosVendaDTO parametrosVenda) { |
||
| 446 | for (Lancamento lancamento : parametrosVenda.getLancamentos()) { |
||
| 447 | if (lancamento.comDescontos()) { |
||
| 448 | return true; |
||
| 449 | } |
||
| 450 | } |
||
| 451 | return false; |
||
| 452 | } |
||
| 320 | espaco | 453 | */ |
| 149 | espaco | 454 | |
| 455 | private boolean descontosEmProdutosSuperiores(ParametrosVendaDTO parametrosVenda) { |
||
| 320 | espaco | 456 | if (produtoJBLComDesconto(parametrosVenda.getLancamentos())) { |
| 457 | return true; |
||
| 458 | } |
||
| 459 | |||
| 460 | Double totalDescontoPermitido = 0.0; |
||
| 461 | Double totalDescontoConcebido = 0.0; |
||
| 149 | espaco | 462 | for (Lancamento lancamento : parametrosVenda.getLancamentos()) { |
| 320 | espaco | 463 | if (VerificadorUtil.naoEstaNulo(lancamento.getValorVarejo()) && VerificadorUtil.naoEstaNulo(lancamento.getValorVenda())) { |
| 464 | totalDescontoConcebido = totalDescontoConcebido + (lancamento.getValorVarejo() - lancamento.getValorVenda()); |
||
| 465 | if (lancamento.getValorVarejo() < 100) { |
||
| 466 | totalDescontoPermitido = totalDescontoPermitido + 5.0; |
||
| 467 | } else { |
||
| 468 | totalDescontoPermitido = totalDescontoPermitido + (lancamento.getValorVarejo()*10/100); |
||
| 469 | } |
||
| 470 | } |
||
| 471 | } |
||
| 472 | if (totalDescontoConcebido > totalDescontoPermitido) { |
||
| 473 | return true; |
||
| 474 | } |
||
| 475 | return false; |
||
| 476 | } |
||
| 477 | |||
| 478 | private boolean produtoJBLComDesconto(List<Lancamento> lancamentos) { |
||
| 479 | for (Lancamento lancamento : lancamentos) { |
||
| 480 | if (lancamento.getProduto().getDescricaoCompleta().contains("JBL") && lancamento.comDescontos()) { |
||
| 149 | espaco | 481 | return true; |
| 482 | } |
||
| 483 | } |
||
| 484 | return false; |
||
| 485 | } |
||
| 320 | espaco | 486 | |
| 149 | espaco | 487 | private boolean produtosComDescontoECupom(ParametrosVendaDTO parametrosVenda) { |
| 488 | for (Lancamento lancamento : parametrosVenda.getLancamentos()) { |
||
| 489 | if (VerificadorUtil.naoEstaNulo(lancamento.getCupom()) && lancamento.comDescontos()) { |
||
| 490 | return true; |
||
| 491 | } |
||
| 492 | } |
||
| 493 | return false; |
||
| 494 | } |
||
| 495 | |||
| 496 | @Override |
||
| 497 | public void alterarVenda(ParametrosVendaDTO parametrosVenda, Usuario usuario, Boolean verificarJustificativa) { |
||
| 498 | verificarLancamentos(parametrosVenda.getLancamentos()); |
||
| 577 | blopes | 499 | lancarExcecaoSeUsuarioNaoTiverPermissao(parametrosVenda, usuario, verificarJustificativa); |
| 149 | espaco | 500 | lancarExcecaoCasoNaoSejaInformadoCamposObrigatorios(parametrosVenda); |
| 501 | lancarExcecaoSeValoresNaoSaoIguais(parametrosVenda); |
||
| 502 | if (verificarJustificativa) {enviarEmailAlteracoes(parametrosVenda, usuario);} |
||
| 503 | atualizarDadosDaVenda(parametrosVenda); |
||
| 504 | alterarLancamentos(parametrosVenda); |
||
| 505 | alterarPagamentos(parametrosVenda); |
||
| 506 | alterar(parametrosVenda.getVenda()); |
||
| 507 | alterarSituacaoPedido(parametrosVenda.getPedidoVinculado(), parametrosVenda.getVenda()); |
||
| 508 | } |
||
| 509 | |||
| 577 | blopes | 510 | private void lancarExcecaoSeUsuarioNaoTiverPermissao(ParametrosVendaDTO parametrosVenda, Usuario usuario, Boolean verificarJustificativa) { |
| 511 | if (verificarJustificativa) { |
||
| 512 | for (UsuarioPerfil usuarioPerfil : usuario.getPerfis()) { |
||
| 513 | if (usuarioPerfil.getPerfil().getCodigo().equals(ConstantesSEC.PERFIL_LOJA_9)) { |
||
| 514 | throw new NegocioException("USUÁRIO SEM PERMISSÃO PARA ESTA OPERAÇÃO"); |
||
| 515 | } |
||
| 516 | } |
||
| 517 | |||
| 518 | if (!parametrosVenda.getVendedor().equals(parametrosVenda.getVenda().getVendedor())) { |
||
| 519 | Usuario usuarioDetalhe = usuarioService.detalhar(usuario); |
||
| 520 | if (VerificadorUtil.naoEstaNulo(usuarioDetalhe.getPessoa())) { |
||
| 521 | if (!parametrosVenda.getVenda().getVendedor().getPessoa().equals(usuarioDetalhe.getPessoa())) { |
||
| 522 | throw new NegocioException("OPERAÇÃO SÓ PODE SER REALIZADA PELO VENDEDOR ATUAL"); |
||
| 523 | } |
||
| 524 | } else { |
||
| 525 | throw new NegocioException("USUÁRIO SEM PERMISSÃO PARA ESTA OPERAÇÃO"); |
||
| 526 | } |
||
| 527 | } |
||
| 528 | } |
||
| 529 | } |
||
| 530 | |||
| 149 | espaco | 531 | private void enviarEmailAlteracoes(ParametrosVendaDTO parametrosVenda, Usuario usuario) { |
| 532 | String diaAtual = DataUtils.converterDataParaString(DataUtils.getDataAtual()); |
||
| 533 | StringBuilder conteudo = new StringBuilder(); |
||
| 534 | conteudo.append("OPERAÇÃO REALIZADA POR: " + usuario.getNome() + "\n"); |
||
| 535 | conteudo.append("ID: " + parametrosVenda.getVenda().getSequencial() + "\n"); |
||
| 536 | conteudo.append("DATA DA VENDA: " + DataUtils.converterDataComHorarioParaString(parametrosVenda.getVenda().getDataVenda()) + "\n"); |
||
| 537 | conteudo.append("ALTERAÇÕES:\n"); |
||
| 538 | if (!parametrosVenda.getLoja().equals(parametrosVenda.getVenda().getLoja())) { |
||
| 539 | conteudo.append("LOJA: " + parametrosVenda.getVenda().getLoja().getDescricao() + " -> " + parametrosVenda.getLoja().getDescricao() + "\n"); |
||
| 540 | } |
||
| 541 | if (!parametrosVenda.getVendedor().equals(parametrosVenda.getVenda().getVendedor())) { |
||
| 542 | conteudo.append("VENDEDOR: " + parametrosVenda.getVenda().getVendedor().getNomeDaPessoa() + " -> " + parametrosVenda.getVendedor().getNomeDaPessoa() + "\n"); |
||
| 543 | } |
||
| 544 | |||
| 545 | String indicacao = "NÃO INFORMADO"; |
||
| 546 | String novaIndicacao = "NÃO INFORMADO"; |
||
| 547 | if (VerificadorUtil.naoEstaNulo(parametrosVenda.getVenda().getIndicacao())) { |
||
| 548 | indicacao = parametrosVenda.getVenda().getIndicacao().getNome(); |
||
| 549 | } |
||
| 550 | if (VerificadorUtil.naoEstaNulo(parametrosVenda.getIndicacao())) { |
||
| 551 | novaIndicacao = parametrosVenda.getIndicacao().getNome(); |
||
| 552 | } |
||
| 553 | if (!indicacao.equals(novaIndicacao)) { |
||
| 554 | conteudo.append("INDICAÇÃO: " + indicacao + " -> " + novaIndicacao + "\n"); |
||
| 555 | } |
||
| 556 | |||
| 557 | String cliente = "NÃO INFORMADO"; |
||
| 558 | String novoCliente = "NÃO INFORMADO"; |
||
| 559 | if (VerificadorUtil.naoEstaNulo(parametrosVenda.getVenda().getCliente())) { |
||
| 560 | cliente = parametrosVenda.getVenda().getCliente().getNome(); |
||
| 561 | } |
||
| 562 | if (VerificadorUtil.naoEstaNulo(parametrosVenda.getCliente())) { |
||
| 563 | novoCliente = parametrosVenda.getCliente().getNome(); |
||
| 564 | } |
||
| 565 | if (!cliente.equals(novoCliente)) { |
||
| 566 | conteudo.append("CLIENTE: " + cliente + " -> " + novoCliente + "\n"); |
||
| 567 | } |
||
| 568 | |||
| 569 | if (!parametrosVenda.getObservacao().equals(parametrosVenda.getVenda().getObservacao())) { |
||
| 570 | conteudo.append("OBS: " + parametrosVenda.getVenda().getObservacao() + " -> " + parametrosVenda.getObservacao() + "\n"); |
||
| 571 | } |
||
| 572 | |||
| 573 | String formasPagamento = ""; |
||
| 574 | String novasFormasPagamento = ""; |
||
| 575 | List<VendaFormaPagamento> pagamentosAnteriores = consultarPagamentosCadastrados(parametrosVenda); |
||
| 576 | if (!VerificadorUtil.isListaNulaOuVazia(pagamentosAnteriores)) { |
||
| 577 | for (VendaFormaPagamento pagamento : pagamentosAnteriores) { |
||
| 578 | formasPagamento = formasPagamento + " R$" + pagamento.getValorPagamento() + " (" + pagamento.getFormaPagamento().getDescricao(); |
||
| 579 | if (VerificadorUtil.naoEstaNulo(pagamento.getBandeiraCartao())) { |
||
| 580 | formasPagamento = formasPagamento + " - " + pagamento.getBandeiraCartao().getTipoCartaoDescricao() + " - " + pagamento.getDescricaoDaBandeiraCartao(); |
||
| 581 | } |
||
| 582 | formasPagamento = formasPagamento + "); "; |
||
| 583 | } |
||
| 584 | } |
||
| 585 | if (!VerificadorUtil.isListaNulaOuVazia(parametrosVenda.getLancamentosPagamentos())) { |
||
| 586 | for (VendaFormaPagamento pagamento : parametrosVenda.getLancamentosPagamentos()) { |
||
| 587 | novasFormasPagamento = novasFormasPagamento + " R$" + pagamento.getValorPagamento() + " (" + pagamento.getFormaPagamento().getDescricao(); |
||
| 588 | if (VerificadorUtil.naoEstaNulo(pagamento.getBandeiraCartao())) { |
||
| 589 | novasFormasPagamento = novasFormasPagamento + " - " + pagamento.getBandeiraCartao().getTipoCartaoDescricao() + " - " + pagamento.getDescricaoDaBandeiraCartao(); |
||
| 590 | } |
||
| 591 | novasFormasPagamento = novasFormasPagamento + "); "; |
||
| 592 | } |
||
| 593 | } |
||
| 594 | if (!formasPagamento.equals(novasFormasPagamento)) { |
||
| 595 | conteudo.append("FORMAS DE PAGAMENTO: " + formasPagamento + " -> " + novasFormasPagamento + "\n"); |
||
| 596 | } |
||
| 597 | |||
| 598 | conteudo.append("\n"); |
||
| 599 | conteudo.append("JUSTIFICATIVA: " + parametrosVenda.getVenda().getJustificativaParaExcluir() + "\n"); |
||
| 600 | new GerenciadorEmailImpl() |
||
| 601 | .comEmailsDestino(ConstantesSEC.DESTINATARIOS_EMAIL_SUPERVISAO) |
||
| 602 | .comAssunto("ESPAÇO CASE - ALTERAÇÃO DE VENDA: " + diaAtual) |
||
| 603 | .comConteudo(conteudo.toString()) |
||
| 604 | .enviar(); |
||
| 605 | } |
||
| 606 | |||
| 607 | private String montarDadosDaVenda(Venda venda, Usuario usuario, List<Lancamento> lancamentos, List<VendaFormaPagamento> pagamentos) { |
||
| 608 | StringBuilder conteudo = new StringBuilder(); |
||
| 609 | conteudo.append("OPERAÇÃO REALIZADA POR: " + usuario.getNome() + "\n"); |
||
| 610 | conteudo.append("ID: " + venda.getSequencial() + "\n"); |
||
| 611 | conteudo.append("DATA DA VENDA: " + DataUtils.converterDataComHorarioParaString(venda.getDataVenda()) + "\n"); |
||
| 612 | conteudo.append("LOJA: " + venda.getLoja().getDescricao() + "\n"); |
||
| 613 | conteudo.append("VENDEDOR: " + venda.getVendedor().getNomeDaPessoa() + "\n"); |
||
| 614 | conteudo.append("TOTAL: " + valorTotalVenda(lancamentos) + "\n"); |
||
| 615 | conteudo.append("OBS.: " + venda.getObservacao() + "\n"); |
||
| 616 | if (VerificadorUtil.naoEstaNuloOuVazio(venda.getJustificativaParaExcluir())) { |
||
| 617 | conteudo.append("JUSTIFICATIVA: " + venda.getJustificativaParaExcluir() + "\n"); |
||
| 618 | } else { |
||
| 619 | conteudo.append("SEM JUSTIFICATIVA\n"); |
||
| 620 | } |
||
| 621 | conteudo.append("--------------------------------------------------\n"); |
||
| 622 | for (Lancamento lancamento : lancamentos) { |
||
| 623 | conteudo.append(lancamento.getProduto().getCodigoProdutoPadrao() + " | "); |
||
| 624 | conteudo.append(lancamento.getProduto().getDescricaoComModeloCompleta() + ", "); |
||
| 625 | conteudo.append("R$" + lancamento.getValorVenda()); |
||
| 626 | if (lancamento.comDescontos()) { |
||
| 627 | conteudo.append(" (COM DESCONTO DE R$" + lancamento.valorDescontos()); |
||
| 628 | conteudo.append(" [" + lancamento.valorPercentualDescontos() + "])"); |
||
| 629 | } |
||
| 630 | if (VerificadorUtil.naoEstaNuloOuVazio(lancamento.getObservacao())) { |
||
| 631 | conteudo.append(" (OBS.: " + lancamento.getObservacao() + ")"); |
||
| 632 | } |
||
| 633 | if (VerificadorUtil.naoEstaNulo(lancamento.getCupom())) { |
||
| 634 | String descricaoCupom = lancamento.getCupom().getCodigo(); |
||
| 635 | if (VerificadorUtil.naoEstaNulo(lancamento.getCupom().getDescricao())) { |
||
| 636 | descricaoCupom = descricaoCupom + " - " + lancamento.getCupom().getDescricao(); |
||
| 637 | } |
||
| 638 | conteudo.append(" (CUPOM: " + descricaoCupom + ")"); |
||
| 639 | } |
||
| 640 | conteudo.append("\n"); |
||
| 641 | } |
||
| 642 | conteudo.append("--------------------------------------------------\n"); |
||
| 643 | for (VendaFormaPagamento pagamento : pagamentos) { |
||
| 644 | if (VerificadorUtil.naoEstaNulo(pagamento.getBandeiraCartao())) { |
||
| 645 | conteudo.append(pagamento.getFormaPagamento().getDescricao() + " - " + pagamento.getBandeiraCartao().getDescricao() + ": R$" + pagamento.getValorPagamento() + "\n"); |
||
| 646 | } else { |
||
| 647 | conteudo.append(pagamento.getFormaPagamento().getDescricao() + ": R$" + pagamento.getValorPagamento() + "\n"); |
||
| 648 | } |
||
| 649 | conteudo.append("\n"); |
||
| 650 | } |
||
| 651 | conteudo.append("--------------------------------------------------\n"); |
||
| 652 | if (VerificadorUtil.naoEstaNulo(venda.getCliente())) { |
||
| 653 | conteudo.append("DADOS DO CLIENTE:\n"); |
||
| 654 | conteudo.append(venda.getCliente().getNome() + "\n"); |
||
| 655 | conteudo.append(venda.getCliente().getCpfCnpjFormatado() + "\n"); |
||
| 656 | } |
||
| 657 | return conteudo.toString(); |
||
| 658 | } |
||
| 659 | |||
| 660 | private Double valorTotalVenda(List<Lancamento> lancamentos) { |
||
| 661 | Double total = new Double(0.0); |
||
| 662 | for (Lancamento lancamento : lancamentos) { |
||
| 663 | if (lancamento.getAtivo()) { |
||
| 664 | total = total + lancamento.getValorVenda(); |
||
| 665 | } |
||
| 666 | } |
||
| 667 | return total; |
||
| 668 | } |
||
| 669 | |||
| 670 | private void atualizarDadosDaVenda(ParametrosVendaDTO parametrosVenda) { |
||
| 671 | parametrosVenda.getVenda().setVendedor(parametrosVenda.getVendedor()); |
||
| 672 | parametrosVenda.getVenda().setIndicacao(parametrosVenda.getIndicacao()); |
||
| 673 | parametrosVenda.getVenda().setCliente(parametrosVenda.getCliente()); |
||
| 674 | parametrosVenda.getVenda().setObservacao(parametrosVenda.getObservacao()); |
||
| 675 | parametrosVenda.getVenda().setNotaFiscal(parametrosVenda.getNotaFiscal()); |
||
| 676 | parametrosVenda.getVenda().setMaquineta(parametrosVenda.getMaquineta()); |
||
| 677 | if (!parametrosVenda.getVenda().getLoja().equals(parametrosVenda.getLoja())) { |
||
| 678 | for (Lancamento lancamento : parametrosVenda.getVenda().getLancamentos()) { |
||
| 305 | espaco | 679 | alterarEstoqueProduto(1, parametrosVenda.getVenda().getLoja(), lancamento.getProduto().getCodigo(), lancamento.getTipoPersonalizacao()); |
| 680 | alterarEstoqueProduto(-1, parametrosVenda.getLoja(), lancamento.getProduto().getCodigo(), lancamento.getTipoPersonalizacao()); |
||
| 149 | espaco | 681 | } |
| 682 | parametrosVenda.getVenda().setLoja(parametrosVenda.getLoja()); |
||
| 683 | parametrosVenda.getVenda().setMaquineta(parametrosVenda.getLoja().getMaquineta()); |
||
| 684 | } |
||
| 685 | parametrosVenda.getVenda().setTipoSituacao(parametrosVenda.getTipoSituacao()); |
||
| 686 | parametrosVenda.getVenda().setDataVenda(parametrosVenda.getDataVenda()); |
||
| 687 | } |
||
| 688 | |||
| 689 | @Override |
||
| 690 | public List<Venda> consultarVendasDoDia(Long sequencialLoja, Date dataVendas) { |
||
| 691 | return vendaRepository.consultarVendasDoDia(sequencialLoja, dataVendas); |
||
| 692 | } |
||
| 693 | |||
| 694 | @Override |
||
| 695 | public Integer obterQuantidadeDeVendasPorLojaEePeriodo(Venda venda, Date dataInicial, Date dataFinal) { |
||
| 696 | return vendaRepository.obterQuantidadeDeVendasPorLojaEePeriodo(venda, dataInicial, dataFinal); |
||
| 697 | } |
||
| 698 | |||
| 699 | @Override |
||
| 700 | public Double obterValorTotalBrutoDeVendasPorLojaEePeriodo(Loja loja, Date dataInicial, Date dataFinal) { |
||
| 701 | Double valorTotal = vendaRepository.obterValorTotalBrutoDeVendasPorLojaEePeriodo(loja, dataInicial, dataFinal); |
||
| 702 | valorTotal = valorTotal - vendaRepository.obterValorDeCuponsUtilizadosPorLojaEePeriodo(new ParametrosConsultaVendasDTO(loja, dataInicial, dataFinal)); |
||
| 703 | return valorTotal; |
||
| 704 | } |
||
| 705 | |||
| 706 | @Override |
||
| 707 | public Double obterValorTotalBrutoDeVendasPorLojaEePeriodo(ParametrosConsultaVendasDTO parametrosConsulta) { |
||
| 708 | Double valorTotal = vendaRepository.obterValorTotalBrutoDeVendasPorLojaEePeriodo(parametrosConsulta); |
||
| 709 | valorTotal = valorTotal - vendaRepository.obterValorDeCuponsUtilizadosPorLojaEePeriodo(parametrosConsulta); |
||
| 710 | return valorTotal; |
||
| 711 | } |
||
| 712 | |||
| 713 | @Override |
||
| 714 | public Double obterValorTotalDeDescontosPorLojaEePeriodo(ParametrosConsultaVendasDTO parametrosConsulta) { |
||
| 715 | return vendaRepository.obterValorTotalDeDescontosPorLojaEePeriodo(parametrosConsulta); |
||
| 716 | } |
||
| 717 | |||
| 718 | @Override |
||
| 719 | public Double obterValorTotalBrutoDeVendasPorLojaEePeriodoEeFormasDePagamento(ParametrosConsultaVendasDTO parametrosConsultaVendas, List<FormaPagamento> formasDePagamento) { |
||
| 720 | return vendaRepository.obterValorTotalBrutoDeVendasPorLojaEePeriodoEeFormasDePagamento(parametrosConsultaVendas, formasDePagamento); |
||
| 721 | } |
||
| 722 | |||
| 723 | @Override |
||
| 724 | public Double obterValorTotalLiquidoDeVendasPorLojaEePeriodo(ParametrosConsultaVendasDTO parametrosConsulta) { |
||
| 725 | return vendaRepository.obterValorTotalLiquidoDeVendasPorLojaEePeriodo(parametrosConsulta); |
||
| 726 | } |
||
| 727 | |||
| 728 | @Override |
||
| 729 | public Integer obterQuantidadeProdutosVendidosPorLojaEePeriodo(ParametrosConsultaVendasDTO parametrosConsultaVendas) { |
||
| 730 | return vendaRepository.obterQuantidadeProdutosVendidosPorLojaEePeriodo(parametrosConsultaVendas); |
||
| 731 | } |
||
| 732 | |||
| 733 | @Override |
||
| 734 | public Integer obterQuantidadeProdutosVendidosPorLojaEePeriodoNaoVivo(ParametrosConsultaVendasDTO parametrosConsultaVendas) { |
||
| 735 | return vendaRepository.obterQuantidadeProdutosVendidosPorLojaEePeriodoNaoVivo(parametrosConsultaVendas); |
||
| 736 | } |
||
| 737 | |||
| 738 | @Override |
||
| 739 | public Integer obterQuantidadeProdutosVendidosPorEstiloEeGenero(Date dataInicial, Date dataFinal, Long sequencialLoja, Long sequencialModelo, String tipoProduto, String estiloProduto, String tipoGenero) { |
||
| 740 | return vendaRepository.obterQuantidadeProdutosVendidosPorEstiloEeGenero(dataInicial, dataFinal, sequencialLoja, sequencialModelo, tipoProduto, estiloProduto, tipoGenero); |
||
| 741 | } |
||
| 742 | |||
| 743 | @Override |
||
| 744 | public Double obterValorProdutosUtilizadosEmVendasPorLojaEePeriodo(ParametrosConsultaVendasDTO parametrosConsulta) { |
||
| 745 | return vendaRepository.obterValorProdutosUtilizadosEmVendasPorLojaEePeriodo(parametrosConsulta); |
||
| 746 | } |
||
| 747 | |||
| 748 | private void verificarLancamentos(List<Lancamento> lancamentos) { |
||
| 749 | if (lancamentos.isEmpty()) { |
||
| 750 | throw new NegocioException("Obrigatório informar ao menos um produto"); |
||
| 751 | } |
||
| 752 | } |
||
| 753 | |||
| 754 | // TODO: EM DESENVOLVIMENTO |
||
| 755 | /* |
||
| 756 | private void lancarExcecaoCasoFreteContraditorio(ParametrosVendaDTO parametrosVenda) { |
||
| 757 | if (VerificadorUtil.naoEstaNulo(parametrosVenda.getTipoFrete())) { |
||
| 758 | if (parametrosVenda.getTipoFrete().equals(ConstantesSEC.NotaFiscal.Transporte.SEM_OCORRENCIA_TRANSPORTE_9) |
||
| 759 | && parametrosVenda.getValorFrete() > 0.0) { |
||
| 760 | throw new NegocioException("Não é possível informar o frete sem a ocorrência."); |
||
| 761 | } |
||
| 762 | if (parametrosVenda.getTipoFrete().equals(ConstantesSEC.NotaFiscal.Transporte.FRETE_POR_CONTA_REMETENTE_TRANSPORTE_PROPRIO_3) |
||
| 763 | && parametrosVenda.getValorFrete() == 0.0) { |
||
| 764 | throw new NegocioException("É necessário informar o valor do frete."); |
||
| 765 | } |
||
| 766 | |||
| 767 | } |
||
| 768 | // CASO TENHA VALOR E: FRETE CIF, FRETE DE TERCEIROS, FRETE REMETENTE (TP) OU SEM OCORRÊNCIA |
||
| 769 | // CASO NÃO TENHA VALOR E: FRETE FOB. |
||
| 770 | } |
||
| 771 | */ |
||
| 772 | |||
| 773 | private void lancarExcecaoCasoNaoSejaInformadoCamposObrigatorios(ParametrosVendaDTO parametrosVenda) { |
||
| 774 | if (VerificadorUtil.isListaNulaOuVazia(parametrosVenda.getLancamentosPagamentos()) && VerificadorUtil.estaNuloOuVazio(parametrosVenda.getObservacao())) { |
||
| 775 | throw new NegocioException("Obrigatório informar as observações em vendas sem pagamentos"); |
||
| 776 | } |
||
| 777 | if (VerificadorUtil.estaNulo(parametrosVenda.getLoja())) { |
||
| 778 | throw new NegocioException("Informe a loja"); |
||
| 779 | } |
||
| 277 | espaco | 780 | for (VendaFormaPagamento vendaFormaPagamento : parametrosVenda.getLancamentosPagamentos()) { |
| 781 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEhCartao()) { |
||
| 782 | if (VerificadorUtil.estaNulo(parametrosVenda.getMaquineta())) { |
||
| 783 | throw new NegocioException("Informe a maquineta"); |
||
| 784 | } |
||
| 785 | } |
||
| 786 | } |
||
| 305 | espaco | 787 | if (possuiLancamentoPersonalizacao(parametrosVenda.getLancamentos())) { |
| 788 | if (VerificadorUtil.estaNulo(parametrosVenda.getCliente()) && VerificadorUtil.estaNuloOuVazio(parametrosVenda.getObservacao())) { |
||
| 789 | throw new NegocioException("Informe o cliente ou o contato do cliente nas observações"); |
||
| 790 | } |
||
| 791 | } |
||
| 792 | if (VerificadorUtil.estaNulo(parametrosVenda.getLoja())) { |
||
| 793 | throw new NegocioException("Informe a loja"); |
||
| 794 | } |
||
| 149 | espaco | 795 | } |
| 305 | espaco | 796 | |
| 797 | private Boolean possuiLancamentoPersonalizacao(List<Lancamento> lancamentos) { |
||
| 798 | for (Lancamento lancamento : lancamentos) { |
||
| 799 | if (VerificadorUtil.naoEstaNuloOuVazio(lancamento.getTipoPersonalizacao())) { |
||
| 800 | return true; |
||
| 801 | } |
||
| 802 | } |
||
| 803 | return false; |
||
| 804 | } |
||
| 149 | espaco | 805 | |
| 806 | private void lancarExcecaoSeValoresNaoSaoIguais(ParametrosVendaDTO parametrosVenda) { |
||
| 807 | final double TOLERANCE = 0.01; |
||
| 808 | if(Math.abs(parametrosVenda.valorTotalLancamentos() /*+ parametrosVenda.getValorFrete() */- parametrosVenda.valorTotalPago()) > TOLERANCE) { |
||
| 809 | throw new NegocioException("Valores pagos não corresponde ao valor vendido."); |
||
| 810 | } |
||
| 811 | } |
||
| 812 | |||
| 813 | private void cadastrarLancamentos(ParametrosVendaDTO parametrosVenda, Venda venda) { |
||
| 814 | for (Lancamento lancamento : parametrosVenda.getLancamentos()) { |
||
| 815 | cadastrarLancamento(parametrosVenda.getLoja(), venda, lancamento); |
||
| 816 | } |
||
| 817 | } |
||
| 818 | |||
| 819 | private void alterarLancamentos(ParametrosVendaDTO parametrosVenda) { |
||
| 820 | excluirLancamentosRetiradosDaLista(parametrosVenda, consultarLancamentosCadastrados(parametrosVenda)); |
||
| 821 | cadastrarNovosLancamentos(parametrosVenda); |
||
| 822 | } |
||
| 823 | |||
| 824 | private void excluirLancamentosRetiradosDaLista(ParametrosVendaDTO parametrosVenda, List<Lancamento> lancamentosAntigos) { |
||
| 825 | for (Lancamento lancamentoAntigo : lancamentosAntigos) { |
||
| 826 | if (!parametrosVenda.getLancamentos().contains(lancamentoAntigo)) { |
||
| 827 | lancamentoService.excluir(lancamentoAntigo); |
||
| 305 | espaco | 828 | alterarEstoqueProduto(1, parametrosVenda.getLoja(), lancamentoAntigo.getProduto().getCodigo(), lancamentoAntigo.getTipoPersonalizacao()); |
| 149 | espaco | 829 | } |
| 830 | } |
||
| 831 | } |
||
| 832 | |||
| 833 | private void cadastrarNovosLancamentos(ParametrosVendaDTO parametrosVenda) { |
||
| 834 | for (Lancamento lancamento : parametrosVenda.getLancamentos()) { |
||
| 835 | if (VerificadorUtil.estaNulo(lancamento.getSequencial())) { |
||
| 836 | cadastrarLancamento(parametrosVenda.getLoja(), parametrosVenda.getVenda(), lancamento); |
||
| 837 | } |
||
| 838 | } |
||
| 839 | } |
||
| 840 | |||
| 841 | private List<Lancamento> consultarLancamentosCadastrados(ParametrosVendaDTO parametrosVenda) { |
||
| 842 | Lancamento lancamentoDaVenda = new Lancamento(); |
||
| 843 | lancamentoDaVenda.setVenda(parametrosVenda.getVenda()); |
||
| 844 | return lancamentoService.consultarPassandoEntidade(lancamentoDaVenda, 0, lancamentoService.obterQuantidadeDeRegistrosPassandoEntidade(lancamentoDaVenda)); |
||
| 845 | } |
||
| 846 | |||
| 847 | private void cadastrarLancamento(Loja loja, Venda venda, Lancamento lancamento) { |
||
| 848 | lancamento.setVenda(venda); |
||
| 849 | igualarValorVarejoComCompraSeProdutoForPadrao(lancamento); |
||
| 850 | acrescentarValorIPISeHouver(lancamento); |
||
| 851 | lancamentoService.cadastrar(lancamento); |
||
| 305 | espaco | 852 | alterarEstoqueProduto(-1, loja, lancamento.getProduto().getCodigo(), lancamento.getTipoPersonalizacao()); |
| 149 | espaco | 853 | utilizarCupom(lancamento.getCupom()); |
| 854 | |||
| 855 | } |
||
| 856 | |||
| 857 | private void igualarValorVarejoComCompraSeProdutoForPadrao(Lancamento lancamento) { |
||
| 858 | if (VerificadorUtil.naoEstaNulo(lancamento.getProduto())) { |
||
| 859 | if (lancamento.getProduto().getCodigo().equals(ConstantesSEC.CODIGO_PRODUTO_PADRAO_000000)) { |
||
| 860 | lancamento.setValorVarejo(lancamento.getValorVenda()); |
||
| 861 | } |
||
| 862 | } |
||
| 863 | } |
||
| 864 | |||
| 865 | private void acrescentarValorIPISeHouver(Lancamento lancamento) { |
||
| 866 | if (VerificadorUtil.naoEstaNulo(lancamento.getProduto())) { |
||
| 212 | espaco | 867 | lancamento.setValorCompra(lancamento.getProduto().getValorCompra()); |
| 868 | // lancamento.setValorCompra(lancamento.getProduto().calculoOperacao()); |
||
| 149 | espaco | 869 | } |
| 870 | } |
||
| 871 | |||
| 872 | private void cadastrarPagamentos(ParametrosVendaDTO parametrosVenda, Venda venda) { |
||
| 873 | for (VendaFormaPagamento vendaFormaPagamento : parametrosVenda.getLancamentosPagamentos()) { |
||
| 874 | adicionarMaquineta(vendaFormaPagamento, parametrosVenda, venda); |
||
| 875 | vendaFormaPagamento.setVenda(venda); |
||
| 876 | vendaFormaPagamentoService.cadastrar(vendaFormaPagamento); |
||
| 877 | utilizarCupom(vendaFormaPagamento.getCupom()); |
||
| 878 | } |
||
| 879 | } |
||
| 635 | blopes | 880 | |
| 149 | espaco | 881 | private void adicionarMaquineta(VendaFormaPagamento vendaFormaPagamento, ParametrosVendaDTO parametrosVenda, Venda venda) { |
| 882 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEhCartao()) { |
||
| 883 | if (VerificadorUtil.estaNulo(parametrosVenda.getMaquineta())) { |
||
| 884 | parametrosVenda.setMaquineta(parametrosVenda.getLoja().getMaquineta()); |
||
| 885 | } |
||
| 886 | venda.setMaquineta(parametrosVenda.getMaquineta()); |
||
| 887 | } |
||
| 888 | } |
||
| 889 | |||
| 635 | blopes | 890 | private void cadastrarPosVenda(Venda venda) { |
| 891 | posVendaService.iniciarPosVenda(venda); |
||
| 892 | } |
||
| 893 | |||
| 685 | blopes | 894 | private void gerarCashback(ParametrosVendaDTO parametrosVenda, Venda venda) { |
| 895 | cashbackService.gerarCashback(parametrosVenda, venda); |
||
| 896 | } |
||
| 897 | |||
| 149 | espaco | 898 | private void alterarPagamentos(ParametrosVendaDTO parametrosVenda) { |
| 899 | excluirPagamentosRetiradosDaLista(parametrosVenda, consultarPagamentosCadastrados(parametrosVenda)); |
||
| 900 | cadastrarNovosPagamentos(parametrosVenda); |
||
| 901 | } |
||
| 902 | |||
| 903 | private void excluirPagamentosRetiradosDaLista(ParametrosVendaDTO parametrosVenda, List<VendaFormaPagamento> pagamentosAntigos) { |
||
| 904 | for (VendaFormaPagamento pagamentoAntigo : pagamentosAntigos) { |
||
| 905 | if (!parametrosVenda.getLancamentosPagamentos().contains(pagamentoAntigo)) { |
||
| 906 | vendaFormaPagamentoService.excluir(pagamentoAntigo); |
||
| 907 | } |
||
| 908 | // if (verificarSePagamentoFoiRetiradoDaLista(parametrosVenda.getLancamentosPagamentos(), pagamentoAntigo)) { |
||
| 909 | // vendaFormaPagamentoService.excluirPorId(pagamentoAntigo); |
||
| 910 | // } |
||
| 911 | } |
||
| 912 | } |
||
| 913 | |||
| 914 | private boolean verificarSePagamentoFoiRetiradoDaLista(List<VendaFormaPagamento> pagamentos, VendaFormaPagamento pagamentoAntigo) { |
||
| 915 | boolean pagamentoRetirado = true; |
||
| 916 | for (VendaFormaPagamento pagamento : pagamentos) { |
||
| 917 | if (pagamento.getCodigoDaFormaPagamento().equals(pagamentoAntigo.getCodigoDaFormaPagamento())) { |
||
| 918 | if (verificarSeBandeiraCartaoEhIgual(pagamento.getCodigoDaBandeiraCartao(), pagamentoAntigo.getCodigoDaBandeiraCartao()) && |
||
| 919 | pagamento.getValorPagamento().equals(pagamentoAntigo.getValorPagamento())) { |
||
| 920 | return false; |
||
| 921 | } else { |
||
| 922 | pagamento.setVenda(pagamentoAntigo.getVenda()); |
||
| 923 | vendaFormaPagamentoService.alterar(pagamento); |
||
| 924 | return false; |
||
| 925 | } |
||
| 926 | } |
||
| 927 | } |
||
| 928 | return pagamentoRetirado; |
||
| 929 | } |
||
| 930 | |||
| 931 | private boolean verificarSeBandeiraCartaoEhIgual(Long codigoDaBandeiraCartao, Long codigoDaBandeiraCartaoSegundo) { |
||
| 932 | if (VerificadorUtil.naoEstaNulo(codigoDaBandeiraCartao) && VerificadorUtil.naoEstaNulo(codigoDaBandeiraCartaoSegundo)) { |
||
| 933 | return codigoDaBandeiraCartao.equals(codigoDaBandeiraCartaoSegundo); |
||
| 934 | } |
||
| 935 | if ((VerificadorUtil.estaNulo(codigoDaBandeiraCartao) && VerificadorUtil.naoEstaNulo(codigoDaBandeiraCartaoSegundo)) || |
||
| 936 | (VerificadorUtil.naoEstaNulo(codigoDaBandeiraCartao) && VerificadorUtil.estaNulo(codigoDaBandeiraCartaoSegundo))) { |
||
| 937 | return false; |
||
| 938 | } |
||
| 939 | return true; |
||
| 940 | } |
||
| 941 | |||
| 942 | private void cadastrarNovosPagamentos(ParametrosVendaDTO parametrosVenda) { |
||
| 943 | for (VendaFormaPagamento pagamento : parametrosVenda.getLancamentosPagamentos()) { |
||
| 944 | if (VerificadorUtil.estaNulo(pagamento.getSequencial())) { |
||
| 945 | adicionarMaquineta(pagamento, parametrosVenda, parametrosVenda.getVenda()); |
||
| 946 | pagamento.setVenda(parametrosVenda.getVenda()); |
||
| 947 | vendaFormaPagamentoService.cadastrar(pagamento); |
||
| 948 | utilizarCupom(pagamento.getCupom()); |
||
| 949 | } |
||
| 950 | } |
||
| 951 | } |
||
| 952 | |||
| 953 | private List<VendaFormaPagamento> consultarPagamentosCadastrados(ParametrosVendaDTO parametrosVenda) { |
||
| 954 | VendaFormaPagamento pagamentoDaVenda = new VendaFormaPagamento(); |
||
| 955 | pagamentoDaVenda.setVenda(parametrosVenda.getVenda()); |
||
| 956 | return vendaFormaPagamentoService.consultarPassandoEntidade(pagamentoDaVenda, 0, vendaFormaPagamentoService.obterQuantidadeDeRegistrosPassandoEntidade(pagamentoDaVenda)); |
||
| 957 | } |
||
| 958 | |||
| 305 | espaco | 959 | private void alterarEstoqueProduto(Integer quantidadeAlterar, Loja loja, String codigoProduto, String tipoPersonalizacao) { |
| 149 | espaco | 960 | if (VerificadorUtil.naoEstaNulo(codigoProduto)) { |
| 961 | Produto produto = produtoService.consultarProdutoPorCodigo(codigoProduto); |
||
| 305 | espaco | 962 | Loja lojaAlterarEstoque = retornarLojaParaAlteracaoEstoque(loja, tipoPersonalizacao); |
| 963 | produtoLojaService.alterarEstoqueAcrescentandoQuantidadeInformada(quantidadeAlterar, produto, lojaAlterarEstoque); |
||
| 149 | espaco | 964 | } |
| 965 | } |
||
| 966 | |||
| 305 | espaco | 967 | private Loja retornarLojaParaAlteracaoEstoque(Loja loja, String tipoPersonalizacao) { |
| 968 | if (VerificadorUtil.naoEstaNulo(tipoPersonalizacao)) { |
||
| 969 | return new Loja(ConstantesSEC.Personalizacao.SEQUENCIAL_LOJA_PERSONALIZACAO); |
||
| 970 | } |
||
| 971 | return loja; |
||
| 972 | } |
||
| 973 | |||
| 149 | espaco | 974 | private void utilizarCupom(Cupom cupom) { |
| 975 | if (VerificadorUtil.naoEstaNulo(cupom)) { |
||
| 976 | Cupom cupomUtilizado = cupomService.consultarPorId(cupom); |
||
| 977 | cupomUtilizado.setDataHoraUtilizado(DataUtils.getDataAtual()); |
||
| 978 | if (VerificadorUtil.estaNulo(cupomUtilizado.getQuantidadeUtilizada())) { |
||
| 979 | cupomUtilizado.setQuantidadeUtilizada(0); |
||
| 980 | } |
||
| 981 | cupomUtilizado.setQuantidadeUtilizada(cupomUtilizado.getQuantidadeUtilizada() + 1); |
||
| 982 | cupomService.alterar(cupomUtilizado); |
||
| 983 | } |
||
| 984 | } |
||
| 985 | |||
| 986 | @Override |
||
| 987 | public List<Venda> consultarVendasPorDemanda(ParametrosConsulta<Venda> parametrosConsulta, Integer primeiroRegistro, |
||
| 988 | Integer tamanhoPagina, String sortField, String orderSort, Map<String, Object> filters) { |
||
| 989 | return vendaRepository.consultarVendasPorDemanda(parametrosConsulta, primeiroRegistro, tamanhoPagina, sortField, orderSort, filters); |
||
| 990 | } |
||
| 991 | |||
| 992 | @Override |
||
| 993 | public List<Venda> consultarVendaPorParametros(ParametrosConsultaVendasDTO parametrosConsultaVendas) { |
||
| 994 | return vendaRepository.consultarVendaPorParametros(parametrosConsultaVendas); |
||
| 995 | } |
||
| 996 | |||
| 997 | @Override |
||
| 998 | public GraficoAcumuladoDTO consultarGrafico(String mes, String ano) { |
||
| 999 | return vendaRepository.consultarGrafico(mes, ano); |
||
| 1000 | } |
||
| 1001 | |||
| 1002 | @Override |
||
| 1003 | public List<RankingVendedorDTO> consultarRankingVendedores(Date dataInicial, Date dataFinal, Boolean produtosComissionados, Loja lojaSelecionada) { |
||
| 1004 | List<RankingVendedorDTO> rankingVendedores = new ArrayList<RankingVendedorDTO>(); |
||
| 1005 | List<Vendedor> vendedores = vendedorService.consultarVendedoresColaboradoresAtivos(lojaSelecionada); |
||
| 1006 | for (Vendedor vendedor : vendedores) { |
||
| 1007 | RankingVendedorDTO vendedorConsultado = consultarRankingVendedor(vendedor, dataInicial, dataFinal, produtosComissionados); |
||
| 1008 | rankingVendedores.add(vendedorConsultado); |
||
| 1009 | } |
||
| 1010 | return (List<RankingVendedorDTO>) ordenador.ordenar(rankingVendedores); |
||
| 1011 | } |
||
| 1012 | |||
| 1013 | @Override |
||
| 1014 | public RankingVendedorDTO consultarRankingVendedor(Vendedor vendedor, Vigencia vigencia, Boolean produtosComissionados) { |
||
| 1015 | Avaliacao avaliacao = avaliacaoService.consultarAvaliacaoDaVigencia(vigencia); |
||
| 1016 | RankingVendedorDTO rankingVendedor = consultarRankingVendedor(vendedor, vigencia.getDataInicio(), vigencia.getDataFim(), produtosComissionados); |
||
| 1017 | rankingVendedor.setFuncionario(funcionarioService.consultarFuncionarioPorPessoa(vendedor.getPessoa())); |
||
| 1018 | rankingVendedor.setQuantidadeBancoHoras(bancoHorasService.consultarQuantidadeHoras(rankingVendedor.getFuncionario())); |
||
| 1019 | AvaliacaoFuncionario avaliacaoFuncionario = avaliacaoFuncionarioService.detalhar(vendedor.getPessoa(), avaliacao); |
||
| 1020 | if (VerificadorUtil.naoEstaNuloOuVazio(avaliacaoFuncionario)) { |
||
| 1021 | rankingVendedor.setAvaliacaoFuncionario(avaliacaoFuncionario); |
||
| 337 | espaco | 1022 | rankingVendedor.setPontuacaoGeral(avaliacaoFuncionario.getPontuacao()); |
| 149 | espaco | 1023 | if (VerificadorUtil.naoEstaNulo(avaliacaoFuncionario.getPorcentagemComissao())) { |
| 1024 | rankingVendedor.setComissao(calcularComissao(rankingVendedor.getValorTotalVendido(), avaliacaoFuncionario.getPorcentagemComissao()/100)); |
||
| 1025 | } else { |
||
| 1026 | rankingVendedor.setComissao(calcularComissao(rankingVendedor.getValorTotalVendido(), avaliacaoFuncionario.retornarFaixaComissao(rankingVendedor.getValorTotalVendido(), rankingVendedor.getFuncionario().getTipoComissionamento()))); |
||
| 1027 | } |
||
| 1028 | } |
||
| 1029 | return rankingVendedor; |
||
| 1030 | } |
||
| 1031 | |||
| 1032 | public Double calcularComissao(Double valorTotalVendido, Double porcentagemComissao) { |
||
| 1033 | if (VerificadorUtil.naoEstaNuloOuVazio(porcentagemComissao)) { |
||
| 1034 | return valorTotalVendido * porcentagemComissao; |
||
| 1035 | } |
||
| 1036 | return 0.0; |
||
| 1037 | } |
||
| 1038 | |||
| 1039 | @Override |
||
| 1040 | public RankingVendedorDTO consultarRankingVendedor(Vendedor vendedor, Date dataInicial, Date dataFinal, Boolean produtosComissionados) { |
||
| 1041 | ParametrosConsultaVendasDTO parametrosConsultaVendas = new ParametrosConsultaVendasDTO(); |
||
| 1042 | parametrosConsultaVendas.setDataInicial(DataUtils.getDataComHorarioMinimo(dataInicial)); |
||
| 1043 | parametrosConsultaVendas.setDataFinal(DataUtils.getDataComHorarioMaximo(dataFinal)); |
||
| 1044 | parametrosConsultaVendas.setVendedor(vendedor); |
||
| 1045 | if (VerificadorUtil.naoEstaNulo(produtosComissionados)) { |
||
| 1046 | parametrosConsultaVendas.setIndicadorComissionado(produtosComissionados); |
||
| 1047 | } |
||
| 1048 | |||
| 1049 | RankingVendedorDTO rankingVendedor = new RankingVendedorDTO(); |
||
| 1050 | rankingVendedor.setVendedor(vendedor); |
||
| 1051 | try { |
||
| 1052 | rankingVendedor.setFuncionario(funcionarioService.consultarFuncionarioPorPessoa(vendedor.getPessoa())); |
||
| 1053 | rankingVendedor.setQuantidadeBancoHoras(bancoHorasService.consultarQuantidadeHoras(rankingVendedor.getFuncionario())); |
||
| 1054 | } catch (RuntimeException e) { |
||
| 1055 | rankingVendedor.setFuncionario(null); |
||
| 1056 | } |
||
| 1057 | |||
| 1058 | rankingVendedor.setValorTotalVendido(obterValorTotalBrutoDeVendasPorLojaEePeriodo(parametrosConsultaVendas)); |
||
| 1059 | rankingVendedor.setValorTotalDescontos(obterValorTotalDeDescontosPorLojaEePeriodo(parametrosConsultaVendas)); |
||
| 1060 | rankingVendedor.setQuantidadeTotalProdutosVendidos(obterQuantidadeProdutosVendidosPorLojaEePeriodo(parametrosConsultaVendas)); |
||
| 1061 | |||
| 1062 | Venda venda = new Venda(); |
||
| 1063 | venda.setVendedor(vendedor); |
||
| 1064 | rankingVendedor.setQuantidadeVendas(obterQuantidadeDeVendasPorLojaEePeriodo(venda, dataInicial, dataFinal)); |
||
| 1065 | rankingVendedor.setQuantidadePedidos(obterQuantidadePedidosValidos(vendedor, null, dataInicial, dataFinal)); |
||
| 1066 | rankingVendedor.setQuantidadePedidosInvalidos(obterQuantidadePedidos(vendedor, null, dataInicial, dataFinal, TipoSituacaoPedido.INVALIDO.getValor())); |
||
| 1067 | rankingVendedor.setQuantidadeContagens(obterQuantidadeDeContagens(vendedor, dataInicial, dataFinal)); |
||
| 1068 | rankingVendedor.setQuantidadeAtendimentos(obterQuantidadeDeAtendimentos(vendedor, dataInicial, dataFinal)); |
||
| 1069 | rankingVendedor.setValorTotalVendidoVivo(obterValorTotalBrutoDeVendasVivoPorLojaEePeriodo(parametrosConsultaVendas)); |
||
| 1070 | |||
| 1071 | rankingVendedor.setAvaliacaoFuncionario(avaliacaoFuncionarioService.consultarAvaliacaoFuncionarioPelaData(vendedor, DataUtils.getDataComHorarioMinimo(dataFinal))); |
||
| 1072 | if (VerificadorUtil.naoEstaNulo(rankingVendedor.getAvaliacaoFuncionario())) { |
||
| 1073 | rankingVendedor.setPercentualDescontos(rankingVendedor.getAvaliacaoFuncionario().retornarPercentual(rankingVendedor.getValorTotalDescontos(), rankingVendedor.getValorTotalVendido())); |
||
| 1074 | rankingVendedor.setPercentualMetaMensal(rankingVendedor.getAvaliacaoFuncionario().retornarPercentualRealizado(rankingVendedor.getValorTotalVendido())); |
||
| 1075 | rankingVendedor.setPercentualMetaMensalVivo(rankingVendedor.getAvaliacaoFuncionario().retornarPercentualVivoRealizado(rankingVendedor.getValorTotalVendidoVivo())); |
||
| 333 | espaco | 1076 | rankingVendedor.setPercentualMetaTicketMedio(rankingVendedor.getAvaliacaoFuncionario().retornarPercentualTicketMedioRealizado(rankingVendedor.getTicketMedio())); |
| 149 | espaco | 1077 | } |
| 1078 | |||
| 1079 | ParametrosConsultaVendasDTO parametrosConsultaVendasDoDia = new ParametrosConsultaVendasDTO(); |
||
| 1080 | parametrosConsultaVendasDoDia.setDataInicial(DataUtils.getDataComHorarioMinimo(DataUtils.getDataAtual())); |
||
| 1081 | parametrosConsultaVendasDoDia.setDataFinal(DataUtils.getDataComHorarioMaximo(DataUtils.getDataAtual())); |
||
| 1082 | parametrosConsultaVendasDoDia.setVendedor(vendedor); |
||
| 1083 | if (VerificadorUtil.naoEstaNulo(produtosComissionados)) { |
||
| 1084 | parametrosConsultaVendasDoDia.setIndicadorComissionado(produtosComissionados); |
||
| 1085 | } |
||
| 1086 | rankingVendedor.setValorTotalVendidoDoDia(obterValorTotalBrutoDeVendasPorLojaEePeriodo(parametrosConsultaVendasDoDia)); |
||
| 1087 | |||
| 1088 | return rankingVendedor; |
||
| 1089 | } |
||
| 1090 | |||
| 1091 | private Integer obterQuantidadePedidos(Vendedor vendedor, Loja loja, Date dataInicial, Date dataFinal, String tipoSituacao) { |
||
| 1092 | if (VerificadorUtil.naoEstaNulo(vendedor)) { |
||
| 1093 | if (VerificadorUtil.estaNulo(vendedor.getPessoa())) { |
||
| 1094 | return 0; |
||
| 1095 | } |
||
| 1096 | } |
||
| 1097 | ParametrosConsultaPedidoDTO parametrosPedidos = new ParametrosConsultaPedidoDTO(); |
||
| 1098 | if (VerificadorUtil.naoEstaNulo(vendedor)) { |
||
| 1099 | parametrosPedidos.setPessoa(vendedor.getPessoa()); |
||
| 1100 | } |
||
| 1101 | parametrosPedidos.setEntidade(new Pedido()); |
||
| 1102 | parametrosPedidos.getEntidade().setTipoSituacao(tipoSituacao); |
||
| 1103 | parametrosPedidos.getEntidade().setLoja(loja); |
||
| 1104 | parametrosPedidos.setDataInicial(dataInicial); |
||
| 1105 | parametrosPedidos.setDataFinal(dataFinal); |
||
| 1106 | return pedidoService.obterQuantidadeDeRegistrosParametrosConsulta(parametrosPedidos); |
||
| 1107 | } |
||
| 1108 | |||
| 1109 | private Integer obterQuantidadePedidosValidos(Vendedor vendedor, Loja loja, Date dataInicial, Date dataFinal) { |
||
| 1110 | if (VerificadorUtil.naoEstaNulo(vendedor)) { |
||
| 1111 | if (VerificadorUtil.estaNulo(vendedor.getPessoa())) { |
||
| 1112 | return 0; |
||
| 1113 | } |
||
| 1114 | } |
||
| 1115 | ParametrosConsultaPedidoDTO parametrosPedidos = new ParametrosConsultaPedidoDTO(); |
||
| 1116 | if (VerificadorUtil.naoEstaNulo(vendedor)) { |
||
| 1117 | parametrosPedidos.setPessoa(vendedor.getPessoa()); |
||
| 1118 | } |
||
| 1119 | parametrosPedidos.setEntidade(new Pedido()); |
||
| 1120 | parametrosPedidos.getEntidade().setLoja(loja); |
||
| 1121 | parametrosPedidos.setDataInicial(dataInicial); |
||
| 1122 | parametrosPedidos.setDataFinal(dataFinal); |
||
| 1123 | parametrosPedidos.setValidos(true); |
||
| 1124 | return pedidoService.obterQuantidadeDeRegistrosParametrosConsulta(parametrosPedidos); |
||
| 1125 | } |
||
| 1126 | |||
| 1127 | private Integer obterQuantidadeDeContagens(Vendedor vendedor, Date dataInicial, Date dataFinal) { |
||
| 1128 | if (VerificadorUtil.naoEstaNulo(vendedor)) { |
||
| 1129 | if (VerificadorUtil.estaNulo(vendedor.getPessoa())) { |
||
| 1130 | return 0; |
||
| 1131 | } |
||
| 1132 | } |
||
| 1133 | return produtoLojaService.obterQuantidadeDeContagens(vendedor.getPessoa(), dataInicial, dataFinal); |
||
| 1134 | } |
||
| 1135 | |||
| 1136 | private Integer obterQuantidadeDeAtendimentos(Vendedor vendedor, Date dataInicial, Date dataFinal) { |
||
| 1137 | if (VerificadorUtil.estaNulo(vendedor.getPessoa())) { |
||
| 1138 | return 0; |
||
| 1139 | } |
||
| 1140 | return produtoLojaService.obterQuantidadeDeAtendimentos(vendedor.getPessoa(), dataInicial, dataFinal); |
||
| 1141 | } |
||
| 1142 | |||
| 1143 | private Integer obterQuantidadeDeAtendimentos(Loja loja, Date dataInicial, Date dataFinal) { |
||
| 1144 | return produtoLojaService.obterQuantidadeDeAtendimentos(loja, dataInicial, dataFinal); |
||
| 1145 | } |
||
| 1146 | |||
| 1147 | private Double obterValorTotalBrutoDeVendasVivoPorLojaEePeriodo(ParametrosConsultaVendasDTO parametrosConsultaVendas) { |
||
| 1148 | ParametrosConsultaVendasVivoDTO parametrosConsultaVendasVivo = new ParametrosConsultaVendasVivoDTO(); |
||
| 1149 | parametrosConsultaVendasVivo.setVendedor(parametrosConsultaVendas.getVendedor()); |
||
| 1150 | parametrosConsultaVendasVivo.setDataInicial(parametrosConsultaVendas.getDataInicial()); |
||
| 1151 | parametrosConsultaVendasVivo.setDataFinal(parametrosConsultaVendas.getDataFinal()); |
||
| 1152 | return vendaVivoService.obterValorTotalBrutoDeVendasPorLojaEePeriodo(parametrosConsultaVendasVivo); |
||
| 1153 | } |
||
| 1154 | |||
| 1155 | @Override |
||
| 1156 | public List<RankingLojaDTO> consultarRankingLojas(Date dataInicial, Date dataFinal) { |
||
| 1157 | List<RankingLojaDTO> rankingLojas = new ArrayList<RankingLojaDTO>(); |
||
| 1158 | List<Loja> lojas = lojaService.listarLojasAtivas(); |
||
| 1159 | ParametrosConsultaVendasDTO parametrosConsultaVendas = new ParametrosConsultaVendasDTO(); |
||
| 1160 | parametrosConsultaVendas.setDataInicial(DataUtils.getDataComHorarioMinimo(dataInicial)); |
||
| 1161 | parametrosConsultaVendas.setDataFinal(DataUtils.getDataComHorarioMaximo(dataFinal)); |
||
| 1162 | for (Loja loja : lojas) { |
||
| 1163 | if (!loja.getSequencial().equals(ConstantesSEC.SEQUENCIAL_LOJA_FARMACIA_7) |
||
| 1164 | && !loja.getSequencial().equals(ConstantesSEC.SEQUENCIAL_RESTAURANTE_16)) { |
||
| 1165 | parametrosConsultaVendas.setLoja(loja); |
||
| 1166 | |||
| 1167 | RankingLojaDTO rankingLoja = new RankingLojaDTO(); |
||
| 1168 | rankingLoja.setLoja(loja); |
||
| 1169 | rankingLoja.setValorTotalVendido(obterValorTotalBrutoDeVendasPorLojaEePeriodo(parametrosConsultaVendas) |
||
| 1170 | - obterValorTotalBrutoDeVendasPorLojaEePeriodoServicos(parametrosConsultaVendas)); |
||
| 1171 | rankingLoja.setAvaliacaoLoja(avaliacaoLojaService.consultarAvaliacaoLojaPelaData(loja, DataUtils.getDataComHorarioMinimo(dataFinal))); |
||
| 1172 | if (VerificadorUtil.naoEstaNulo(rankingLoja.getAvaliacaoLoja())) { |
||
| 1173 | rankingLoja.setValorMetaMensal(rankingLoja.getAvaliacaoLoja().getValorMetaMensal()); |
||
| 1174 | } |
||
| 1175 | rankingLojas.add(rankingLoja); |
||
| 1176 | } |
||
| 1177 | } |
||
| 1178 | return (List<RankingLojaDTO>) ordenador.ordenar(rankingLojas); |
||
| 1179 | } |
||
| 1180 | |||
| 1181 | @Override |
||
| 1182 | public List<RankingLojaDTO> consultarRankingLojasCompleto(Date dataInicial, Date dataFinal) { |
||
| 1183 | List<RankingLojaDTO> rankingLojasCompleto = new ArrayList<RankingLojaDTO>(); |
||
| 1184 | List<RankingLojaDTO> rankingLojas = consultarRankingLojas(dataInicial, dataFinal); |
||
| 1185 | for (RankingLojaDTO rankingLoja : rankingLojas) { |
||
| 1186 | rankingLoja.setQuantidadeVendas(obterQuantidadeDeVendasPorLojaEePeriodo(new Venda(rankingLoja.getLoja(), null), dataInicial, dataFinal)); |
||
| 1187 | rankingLoja.setQuantidadeAtendimentos(obterQuantidadeDeAtendimentos(rankingLoja.getLoja(), dataInicial, dataFinal)); |
||
| 1188 | rankingLoja.setQuantidadePedidos(obterQuantidadePedidosValidos(null, rankingLoja.getLoja(), dataInicial, dataFinal)); |
||
| 1189 | rankingLoja.setQuantidadePedidosInvalidos(obterQuantidadePedidos(null, rankingLoja.getLoja(), dataInicial, dataFinal, TipoSituacaoPedido.INVALIDO.getValor())); |
||
| 1190 | |||
| 1191 | ParametrosConsultaPorPeriodoDTO parametrosConsulta = new ParametrosConsultaPorPeriodoDTO(); |
||
| 1192 | ((ParametrosConsultaPorPeriodoDTO)parametrosConsulta).setDataInicial(dataInicial); |
||
| 1193 | ((ParametrosConsultaPorPeriodoDTO)parametrosConsulta).setDataFinal(dataFinal); |
||
| 1194 | EstoqueAuditoria estoqueAuditoria = new EstoqueAuditoria(); |
||
| 1195 | estoqueAuditoria.setLoja(rankingLoja.getLoja()); |
||
| 1196 | estoqueAuditoria.setTipoEstoqueAuditoria(TipoEstoqueAuditoria.AVARIA.getValor()); |
||
| 1197 | parametrosConsulta.setEntidade(estoqueAuditoria); |
||
| 1198 | rankingLoja.setQuantidadeAvarias(estoqueAuditoriaService.obterQuantidadeTotalAuditoria(parametrosConsulta)); |
||
| 1199 | |||
| 1200 | estoqueAuditoria.setTipoEstoqueAuditoria(TipoEstoqueAuditoria.TROCA.getValor()); |
||
| 1201 | parametrosConsulta.setEntidade(estoqueAuditoria); |
||
| 1202 | rankingLoja.setQuantidadeTrocas(estoqueAuditoriaService.obterQuantidadeTotalAuditoria(parametrosConsulta)); |
||
| 1203 | |||
| 1204 | ParametrosConsultaVendasDTO parametrosConsultaVendas = new ParametrosConsultaVendasDTO(); |
||
| 1205 | parametrosConsultaVendas.setDataInicial(DataUtils.getDataComHorarioMinimo(dataInicial)); |
||
| 1206 | parametrosConsultaVendas.setDataFinal(DataUtils.getDataComHorarioMaximo(dataFinal)); |
||
| 1207 | parametrosConsultaVendas.setLoja(rankingLoja.getLoja()); |
||
| 1208 | rankingLoja.setValorTotalDescontos(obterValorTotalDeDescontosPorLojaEePeriodo(parametrosConsultaVendas)); |
||
| 1209 | rankingLoja.setCustoProdutosVendidos(obterValorProdutosUtilizadosEmVendasPorLojaEePeriodo(parametrosConsultaVendas)); |
||
| 1210 | rankingLoja.setValorTotalEstoque(produtoService.valorTotalEstoque(rankingLoja.getLoja(), null)); |
||
| 1211 | rankingLoja.setQuantidadeTotalEstoque(produtoService.quantidadeTotalEstoque(rankingLoja.getLoja())); |
||
| 1212 | |||
| 1213 | ParametrosConsultaLancamentosDTO parametros = new ParametrosConsultaLancamentosDTO(); |
||
| 1214 | parametros.setCategoria(new Categoria(ConstantesSEC.Categoria.SEQUENCIAL_CATEGORIA_ALUGUEL_11)); |
||
| 1215 | parametros.setCentroDeCustos(rankingLoja.getLoja()); |
||
| 1216 | parametros.setPagamentoRealizado(true); |
||
| 1217 | parametros.setDataInicial(dataInicial); |
||
| 1218 | parametros.setDataFinal(dataFinal); |
||
| 1219 | Double valorCac = 0.0; |
||
| 1220 | |||
| 1221 | valorCac = somarValor(valorCac, parcelaService.obterValorPassandoParametrosConsulta(parametros)); |
||
| 1222 | parametros.setCategoria(new Categoria(ConstantesSEC.Categoria.SEQUENCIAL_CATEGORIA_HORA_EXTRA_2)); |
||
| 1223 | valorCac = somarValor(valorCac, parcelaService.obterValorPassandoParametrosConsulta(parametros)); |
||
| 1224 | parametros.setCategoria(new Categoria(ConstantesSEC.Categoria.SEQUENCIAL_CATEGORIA_SALARIO_9)); |
||
| 1225 | valorCac = somarValor(valorCac, parcelaService.obterValorPassandoParametrosConsulta(parametros)); |
||
| 1226 | parametros.setCategoria(new Categoria(ConstantesSEC.Categoria.SEQUENCIAL_CATEGORIA_FERIAS_12)); |
||
| 1227 | valorCac = somarValor(valorCac, parcelaService.obterValorPassandoParametrosConsulta(parametros)); |
||
| 1228 | parametros.setCategoria(new Categoria(ConstantesSEC.Categoria.SEQUENCIAL_CATEGORIA_COMISSOES_8)); |
||
| 1229 | valorCac = somarValor(valorCac, parcelaService.obterValorPassandoParametrosConsulta(parametros)); |
||
| 1230 | parametros.setCategoria(new Categoria(ConstantesSEC.Categoria.SEQUENCIAL_CATEGORIA_VALE_TRANSPORTE_46)); |
||
| 1231 | valorCac = somarValor(valorCac, parcelaService.obterValorPassandoParametrosConsulta(parametros)); |
||
| 1232 | valorCac = valorCac / rankingLoja.getQuantidadeVendas(); |
||
| 1233 | rankingLoja.setValorCac(valorCac); |
||
| 1234 | |||
| 1235 | rankingLojasCompleto.add(rankingLoja); |
||
| 1236 | } |
||
| 1237 | return (List<RankingLojaDTO>) ordenador.ordenar(rankingLojasCompleto); |
||
| 1238 | } |
||
| 1239 | |||
| 1240 | private Double somarValor(Double valor, Double valorAcrescentar) { |
||
| 1241 | if (VerificadorUtil.naoEstaNulo(valorAcrescentar)) { |
||
| 1242 | return valor + valorAcrescentar; |
||
| 1243 | } |
||
| 1244 | return valor; |
||
| 1245 | } |
||
| 1246 | |||
| 1247 | @Override |
||
| 1248 | public List<RankingLojaDTO> consultarRankingLojasExtras(Date dataInicial, Date dataFinal) { |
||
| 1249 | List<RankingLojaDTO> rankingLojas = new ArrayList<RankingLojaDTO>(); |
||
| 164 | espaco | 1250 | |
| 149 | espaco | 1251 | ParametrosConsultaVendasDTO parametrosConsultaVendas = new ParametrosConsultaVendasDTO(); |
| 1252 | parametrosConsultaVendas.setDataInicial(DataUtils.getDataComHorarioMinimo(dataInicial)); |
||
| 1253 | parametrosConsultaVendas.setDataFinal(DataUtils.getDataComHorarioMaximo(dataFinal)); |
||
| 164 | espaco | 1254 | // rankingLojas.addAll(preenchendoLojasExtras(parametrosConsultaVendas)); |
| 1255 | |||
| 1256 | parametrosConsultaVendas.setLoja(null); |
||
| 1257 | // rankingLojas.add(preencherLojaAssistencia(parametrosConsultaVendas)); |
||
| 1258 | |||
| 1259 | return (List<RankingLojaDTO>) ordenador.ordenar(rankingLojas); |
||
| 1260 | } |
||
| 1261 | |||
| 1262 | private RankingLojaDTO preencherLojaAssistencia(ParametrosConsultaVendasDTO parametrosConsultaVendas) { |
||
| 1263 | RankingLojaDTO rankingLojaServico = new RankingLojaDTO(); |
||
| 1264 | Loja lojaServico = new Loja(); |
||
| 1265 | lojaServico.setDescricao("ASSISTÊNCIA"); |
||
| 1266 | rankingLojaServico.setValorMetaMensal(new Double(0.0)); |
||
| 1267 | rankingLojaServico.setLoja(lojaServico); |
||
| 1268 | rankingLojaServico.setValorTotalVendido(obterValorTotalBrutoDeVendasPorLojaEePeriodoServicos(parametrosConsultaVendas)); |
||
| 1269 | return rankingLojaServico; |
||
| 1270 | } |
||
| 1271 | |||
| 1272 | private RankingLojaDTO preencherVendasComPedidos(ParametrosConsultaVendasDTO parametrosConsultaVendas) { |
||
| 1273 | RankingLojaDTO rankingLojaServico = new RankingLojaDTO(); |
||
| 1274 | Loja lojaServico = new Loja(); |
||
| 1275 | lojaServico.setDescricao("E-COMMERCE"); |
||
| 1276 | rankingLojaServico.setValorMetaMensal(new Double(0.0)); |
||
| 1277 | rankingLojaServico.setLoja(lojaServico); |
||
| 1278 | rankingLojaServico.setValorTotalVendido(obterValorTotalBrutoDeVendasPorLojaEePeriodoServicos(parametrosConsultaVendas)); |
||
| 1279 | return rankingLojaServico; |
||
| 1280 | } |
||
| 1281 | |||
| 1282 | private List<RankingLojaDTO> preenchendoLojasExtras(ParametrosConsultaVendasDTO parametrosConsultaVendas) { |
||
| 1283 | List<RankingLojaDTO> rankingLojas = new ArrayList<RankingLojaDTO>(); |
||
| 1284 | List<Loja> lojas = lojaService.listarLojasAtivas(); |
||
| 149 | espaco | 1285 | for (Loja loja : lojas) { |
| 1286 | if (loja.getSequencial().equals(ConstantesSEC.SEQUENCIAL_LOJA_FARMACIA_7) |
||
| 1287 | || loja.getSequencial().equals(ConstantesSEC.SEQUENCIAL_RESTAURANTE_16)) { |
||
| 1288 | parametrosConsultaVendas.setLoja(loja); |
||
| 1289 | |||
| 1290 | RankingLojaDTO rankingLoja = new RankingLojaDTO(); |
||
| 1291 | rankingLoja.setLoja(loja); |
||
| 1292 | rankingLoja.setValorTotalVendido(obterValorTotalBrutoDeVendasPorLojaEePeriodo(parametrosConsultaVendas) |
||
| 1293 | - obterValorTotalBrutoDeVendasPorLojaEePeriodoServicos(parametrosConsultaVendas)); |
||
| 164 | espaco | 1294 | rankingLoja.setAvaliacaoLoja(avaliacaoLojaService.consultarAvaliacaoLojaPelaData(loja, DataUtils.getDataComHorarioMinimo(parametrosConsultaVendas.getDataFinal()))); |
| 149 | espaco | 1295 | if (VerificadorUtil.naoEstaNulo(rankingLoja.getAvaliacaoLoja())) { |
| 1296 | rankingLoja.setValorMetaMensal(rankingLoja.getAvaliacaoLoja().getValorMetaMensal()); |
||
| 1297 | } |
||
| 1298 | rankingLojas.add(rankingLoja); |
||
| 1299 | } |
||
| 1300 | } |
||
| 164 | espaco | 1301 | return rankingLojas; |
| 149 | espaco | 1302 | } |
| 1303 | |||
| 1304 | @Override |
||
| 1305 | public Double obterValorTotalBrutoDeVendasPorLojaEePeriodoServicos(ParametrosConsultaVendasDTO parametrosConsultaVendas) { |
||
| 1306 | parametrosConsultaVendas.setTipoProduto(TipoProduto.SERVICO.getValor()); |
||
| 1307 | Double valorTotal = vendaRepository.obterValorTotalBrutoDeVendasPorLojaEePeriodo(parametrosConsultaVendas); |
||
| 1308 | parametrosConsultaVendas.setTipoProduto(null); |
||
| 1309 | return VerificadorUtil.estaNulo(valorTotal)? new Double(0.0) : valorTotal; |
||
| 1310 | } |
||
| 1311 | |||
| 1312 | @Override |
||
| 1313 | public List<RankingProdutoDTO> consultarProdutosVendidos(ParametrosConsultaProdutosVendidosDTO parametrosConsulta) { |
||
| 1314 | List<RankingProdutoDTO> produtosVendidosConsultados = vendaRepository.consultarProdutosVendidos(parametrosConsulta); |
||
| 1315 | for (RankingProdutoDTO produtoVendido : produtosVendidosConsultados) { |
||
| 1316 | produtoVendido.atualizarTempoEstoque(parametrosConsulta.getDataInicial(), parametrosConsulta.getDataFinal()); |
||
| 1317 | } |
||
| 1318 | return produtosVendidosConsultados; |
||
| 1319 | } |
||
| 1320 | |||
| 1321 | @Override |
||
| 1322 | public List<RankingProdutoDTO> consultarProdutosVendidosResumido(ParametrosConsultaProdutosVendidosDTO parametrosConsulta) { |
||
| 1323 | return vendaRepository.consultarProdutosVendidosResumido(parametrosConsulta); |
||
| 1324 | } |
||
| 1325 | |||
| 1326 | @Override |
||
| 1327 | public List<RankingProdutoDTO> consultarProdutosParaTransferir(ParametrosConsultaProdutosVendidosDTO parametrosConsulta) { |
||
| 1328 | return vendaRepository.consultarProdutosParaTransferir(parametrosConsulta); |
||
| 1329 | } |
||
| 1330 | |||
| 1331 | @Override |
||
| 1332 | public List<RankingProdutoDTO> consultarRankingProdutos(Date dataInicial, Date dataFinal, Integer quantidadeProdutos, String tipoOrdenacao, Long sequencialLoja, String tipoProduto, Produto produto, Marca marca) { |
||
| 1333 | return vendaRepository.consultarProdutosMaisVendidos(dataInicial, dataFinal, quantidadeProdutos, tipoOrdenacao, sequencialLoja, tipoProduto, produto, marca); |
||
| 1334 | } |
||
| 1335 | |||
| 1336 | @Override |
||
| 1337 | public List<RankingModeloDTO> consultarRankingModelos(ParametrosConsultaModelosMaisVendidosDTO parametrosConsulta) { |
||
| 1338 | return vendaRepository.consultarModelosMaisVendidos(parametrosConsulta); |
||
| 1339 | } |
||
| 1340 | |||
| 1341 | @Override |
||
| 1342 | public List<RankingModeloDTO> consultarRankingModelos(Date dataInicial, Date dataFinal, Integer quantidadeModelos, String tipoOrdenacao, String tipoProduto, Long sequencialLoja) { |
||
| 151 | espaco | 1343 | ParametrosConsultaModelosMaisVendidosDTO parametros = new ParametrosConsultaModelosMaisVendidosDTO(dataInicial, dataFinal, quantidadeModelos, tipoOrdenacao, tipoProduto, sequencialLoja); |
| 1344 | return this.consultarRankingModelos(parametros); |
||
| 149 | espaco | 1345 | } |
| 1346 | |||
| 1347 | @Override |
||
| 1348 | public List<RankingComprasTransferenciasDTO> consultarPlanejamentoParaCompras(ParametrosConsultaParaComprasTransferenciasDTO parametrosParaConsulta) { |
||
| 1349 | Integer limite = parametrosParaConsulta.getQuantidadeModelos(); |
||
| 1350 | if (parametrosParaConsulta.getIndicadorConsultarTodosModelos()) { |
||
| 1351 | limite = null; |
||
| 1352 | } |
||
| 1353 | List<RankingComprasTransferenciasDTO> planejamentos = new ArrayList<RankingComprasTransferenciasDTO>(); |
||
| 1354 | ParametrosConsultaModelosMaisVendidosDTO parametrosConsulta = new ParametrosConsultaModelosMaisVendidosDTO(parametrosParaConsulta.getDataInicial(), parametrosParaConsulta.getDataFinal(), limite, |
||
| 1355 | parametrosParaConsulta.getTipoOrdenacao(), parametrosParaConsulta.getTipoProduto(), parametrosParaConsulta.getSequencialLoja(), parametrosParaConsulta.getModelo(), parametrosParaConsulta.getEstilo()); |
||
| 1356 | List<RankingModeloDTO> modelos = vendaRepository.consultarModelosMaisVendidos(parametrosConsulta); |
||
| 1357 | for (RankingModeloDTO modelo : modelos) { |
||
| 1358 | RankingComprasTransferenciasDTO planejamento = new RankingComprasTransferenciasDTO(); |
||
| 1359 | planejamento.setModelo(modelo.getModelo()); |
||
| 1360 | planejamento.setValorTotalVendido(modelo.getValorTotalVendido()); |
||
| 1361 | planejamento.setValorTotalComprado(modelo.getValorTotalComprado()); |
||
| 1362 | planejamento.setQuantidadeVendas(modelo.getQuantidadeVendas()); |
||
| 1363 | planejamento.setQuantidadeMinimaComprar(parametrosParaConsulta.getQuantidadeMinimaComprar()); |
||
| 1364 | if (VerificadorUtil.naoEstaNuloOuVazio(modelo.getQuantidadeEstoqueLocal())) { |
||
| 1365 | planejamento.setQuantidadeEstoqueLocal(modelo.getQuantidadeEstoqueLocal().intValue()); |
||
| 1366 | } |
||
| 1367 | planejamento.setQuantidadeEstoqueGeral(modelo.getQuantidadeEstoqueGeral().intValue()); |
||
| 1368 | planejamentos.add(planejamento); |
||
| 1369 | } |
||
| 1370 | |||
| 1371 | if (parametrosParaConsulta.getIndicadorConsultarTodosModelos()) { |
||
| 1372 | List<Modelo> modelosAtivos = new ArrayList<Modelo>(); |
||
| 1373 | if (VerificadorUtil.estaNulo(parametrosParaConsulta.getModelo())) { |
||
| 1374 | modelosAtivos = modeloService.consultarTodosAtivos(); |
||
| 1375 | } else { |
||
| 1376 | modelosAtivos = modeloService.consultarPassandoEntidade(parametrosParaConsulta.getModelo(), 0, modeloService.obterQuantidadeDeRegistrosPassandoEntidade(parametrosParaConsulta.getModelo())); |
||
| 1377 | } |
||
| 1378 | for (Modelo modeloAtivo : modelosAtivos) { |
||
| 1379 | if (modeloNaoEstaPresenteNaLista(modelos, modeloAtivo)) { |
||
| 1380 | RankingComprasTransferenciasDTO planejamento = new RankingComprasTransferenciasDTO(); |
||
| 1381 | planejamento.setModelo(modeloAtivo); |
||
| 1382 | planejamento.setValorTotalVendido(new Double(0.0)); |
||
| 1383 | planejamento.setValorTotalComprado(new Double(0.0)); |
||
| 1384 | planejamento.setQuantidadeVendas(new Long(0)); |
||
| 1385 | planejamento.setQuantidadeMinimaComprar(parametrosParaConsulta.getQuantidadeMinimaComprar()); |
||
| 1386 | if (VerificadorUtil.naoEstaNuloOuVazio(parametrosParaConsulta.getSequencialLoja())) { |
||
| 1387 | planejamento.setQuantidadeEstoqueLocal(produtoLojaService.consultarEstoqueModeloEeLoja(modeloAtivo, parametrosParaConsulta.getSequencialLoja(), null, parametrosParaConsulta.getTipoProduto(), null, null)); |
||
| 1388 | } |
||
| 1389 | planejamento.setQuantidadeEstoqueGeral(produtoLojaService.consultarEstoqueModeloEeLoja(modeloAtivo, null, null, parametrosParaConsulta.getTipoProduto(), null, null)); |
||
| 1390 | if (planejamento.getQuantidadeEstoqueGeral() > 0) { |
||
| 1391 | planejamentos.add(planejamento); |
||
| 1392 | } |
||
| 1393 | } |
||
| 1394 | } |
||
| 1395 | } |
||
| 1396 | |||
| 1397 | if (parametrosParaConsulta.getTipoOrdenacao().equals(TipoOrdenacaoPlanejamentoCompras.QUANTIDADE_COMPRAR.getValor())) { |
||
| 1398 | return new ArrayList<RankingComprasTransferenciasDTO>(ordenador.ordenar(planejamentos)); |
||
| 1399 | } |
||
| 1400 | return planejamentos; |
||
| 1401 | } |
||
| 1402 | |||
| 1403 | private Boolean modeloNaoEstaPresenteNaLista(List<RankingModeloDTO> rankingModelos, Modelo modeloAtivo) { |
||
| 1404 | for (RankingModeloDTO rankingModelo : rankingModelos) { |
||
| 1405 | if (rankingModelo.getModelo().getSequencial().equals(modeloAtivo.getSequencial())) { |
||
| 1406 | return false; |
||
| 1407 | } |
||
| 1408 | } |
||
| 1409 | return true; |
||
| 1410 | } |
||
| 1411 | |||
| 1412 | @Override |
||
| 1413 | public List<RankingComprasTransferenciasEstiloDTO> consultarDetalhamentoPlanejamentoParaCompras(ParametrosConsultaParaComprasTransferenciasDTO parametrosParaConsulta, Modelo modelo) { |
||
| 600 | blopes | 1414 | List<RankingComprasTransferenciasEstiloDTO> consulta = new ArrayList<RankingComprasTransferenciasEstiloDTO>(); |
| 149 | espaco | 1415 | for (TipoEstiloProduto estiloProduto : TipoEstiloProduto.values()) { |
| 1416 | parametrosParaConsulta.setEstilo(StringUtil.setarUpperCase(estiloProduto.getValor())); |
||
| 602 | blopes | 1417 | parametrosParaConsulta.setSubtipo(null); |
| 149 | espaco | 1418 | RankingComprasTransferenciasEstiloDTO estilo = vendaRepository.consultarEstoqueEeVendasPorEstilo(parametrosParaConsulta, modelo); |
| 1419 | estilo.setRankingsPorLoja(vendaRepository.consultarEstoqueEeVendasEstiloPorLoja(parametrosParaConsulta, modelo)); |
||
| 1420 | estilo.setQuantidadeMinimaComprar(parametrosParaConsulta.getQuantidadeMinimaComprar()); |
||
| 600 | blopes | 1421 | consulta.add(estilo); |
| 149 | espaco | 1422 | } |
| 600 | blopes | 1423 | return consulta; |
| 149 | espaco | 1424 | } |
| 600 | blopes | 1425 | |
| 1426 | @Override |
||
| 1427 | public List<RankingComprasTransferenciasSubtipoProdutoDTO> consultarDetalhamentoPlanejamentoParaCompras(List<SubtipoProduto> subtiposParaConsultar, ParametrosConsultaParaComprasTransferenciasDTO parametrosParaConsulta, Modelo modelo) { |
||
| 1428 | List<RankingComprasTransferenciasSubtipoProdutoDTO> consulta = new ArrayList<RankingComprasTransferenciasSubtipoProdutoDTO>(); |
||
| 1429 | for (SubtipoProduto subtipoProduto : subtiposParaConsultar) { |
||
| 1430 | parametrosParaConsulta.setSubtipo(subtipoProduto); |
||
| 602 | blopes | 1431 | parametrosParaConsulta.setEstilo(null);; |
| 600 | blopes | 1432 | RankingComprasTransferenciasSubtipoProdutoDTO subtipo = vendaRepository.consultarEstoqueEeVendasPorSubtipo(parametrosParaConsulta, modelo); |
| 1433 | subtipo.setRankingsPorLoja(vendaRepository.consultarEstoqueEeVendasSubtipoPorLoja(parametrosParaConsulta, modelo)); |
||
| 1434 | subtipo.setQuantidadeMinimaComprar(parametrosParaConsulta.getQuantidadeMinimaComprar()); |
||
| 1435 | consulta.add(subtipo); |
||
| 1436 | } |
||
| 1437 | return consulta; |
||
| 1438 | } |
||
| 149 | espaco | 1439 | |
| 1440 | @Override |
||
| 356 | espaco | 1441 | public List<RankingVendedorDTO> consultarRankingPromocaoInterna(Date dataInicial, Date dataFinal) { |
| 149 | espaco | 1442 | List<RankingVendedorDTO> rankingVendedores = new ArrayList<RankingVendedorDTO>(); |
| 356 | espaco | 1443 | Vigencia vigencia = vigenciaService.consultarVigenciaDoPeriodo(dataInicial); |
| 1444 | if (VerificadorUtil.naoEstaNulo(vigencia)) { |
||
| 1445 | Avaliacao avaliacao = avaliacaoService.consultarAvaliacaoDaVigencia(vigencia); |
||
| 1446 | if (VerificadorUtil.naoEstaNulo(avaliacao)) { |
||
| 1447 | List<AvaliacaoProduto> produtosAvaliacaoPromocaoInterna = avaliacaoService.consultarProdutosPromocaoInterna(avaliacao.getSequencial()); |
||
| 1448 | List<Produto> produtosPromocaoInterna = new ArrayList<Produto>(); |
||
| 1449 | for (AvaliacaoProduto produtoAvaliacaoPromocaoInterna : produtosAvaliacaoPromocaoInterna) { |
||
| 1450 | produtosPromocaoInterna.add(produtoAvaliacaoPromocaoInterna.getProduto()); |
||
| 1451 | } |
||
| 1452 | |||
| 1453 | List<Vendedor> vendedores = vendedorService.consultarVendedoresColaboradoresAtivos(null); |
||
| 1454 | for (Vendedor vendedor : vendedores) { |
||
| 1455 | RankingVendedorDTO ranking = consultarRankingPromocaoInternaDoVendedor(vendedor, dataInicial, dataFinal, produtosPromocaoInterna); |
||
| 422 | blopes | 1456 | rankingVendedores.add(ranking); |
| 356 | espaco | 1457 | } |
| 149 | espaco | 1458 | } |
| 1459 | } |
||
| 1460 | return (List<RankingVendedorDTO>) ordenador.ordenar(rankingVendedores); |
||
| 1461 | } |
||
| 1462 | |||
| 1463 | @Override |
||
| 252 | espaco | 1464 | public RankingVendedorDTO consultarRankingPromocaoInternaDoVendedor(Vendedor vendedor, Date dataInicial, Date dataFinal, |
| 356 | espaco | 1465 | List<Produto> produtosPromocaoInterna) { |
| 149 | espaco | 1466 | ParametrosConsultaVendasDTO parametrosConsultaVendas = new ParametrosConsultaVendasDTO(); |
| 1467 | parametrosConsultaVendas.setDataInicial(DataUtils.getDataComHorarioMinimo(dataInicial)); |
||
| 1468 | parametrosConsultaVendas.setDataFinal(DataUtils.getDataComHorarioMaximo(dataFinal)); |
||
| 1469 | parametrosConsultaVendas.setVendedor(vendedor); |
||
| 1470 | RankingVendedorDTO rankingVendedor = new RankingVendedorDTO(); |
||
| 1471 | rankingVendedor.setVendedor(vendedor); |
||
| 1472 | |||
| 252 | espaco | 1473 | parametrosConsultaVendas.setProdutosPromocaoInterna(produtosPromocaoInterna); |
| 1474 | rankingVendedor.setQuantidadeProdutosPromocaoInterna(lancamentoService.obterQuantidadeVendasDosProdutosDaPromocaoInterna(parametrosConsultaVendas)); |
||
| 1475 | |||
| 1476 | parametrosConsultaVendas.setProdutosPromocaoInterna(null); |
||
| 1477 | parametrosConsultaVendas.setIndicadorPesquisarPeliculasTpuSoft(true); |
||
| 1478 | parametrosConsultaVendas.setIndicadorPesquisarPeliculasTpuSoftCameras(false); |
||
| 1479 | rankingVendedor.setQuantidadeTpuSoftNormal(lancamentoService.obterQuantidadeVendasDosProdutosDaPromocaoInterna(parametrosConsultaVendas)); |
||
| 1480 | |||
| 1481 | parametrosConsultaVendas.setProdutosPromocaoInterna(null); |
||
| 1482 | parametrosConsultaVendas.setIndicadorPesquisarPeliculasTpuSoft(true); |
||
| 1483 | parametrosConsultaVendas.setIndicadorPesquisarPeliculasTpuSoftCameras(true); |
||
| 1484 | rankingVendedor.setQuantidadeTpuSoftCamera(lancamentoService.obterQuantidadeVendasDosProdutosDaPromocaoInterna(parametrosConsultaVendas)); |
||
| 1485 | |||
| 149 | espaco | 1486 | // if (rankingVendedor.getQuantidadePedidos() >= ConstantesSEC.ProdutosPromocaoInterna.QUANTIDADE_MINIMA_RECEBIMENTO) { |
| 1487 | rankingVendedor.setValorTotalVendido(lancamentoService.obterValorTotalVendidoDosProdutosDaPromocaoInterna(parametrosConsultaVendas) * ConstantesSEC.ProdutosPromocaoInterna.PERCENTUAL); |
||
| 1488 | // } else { |
||
| 1489 | // rankingVendedor.setValorTotalVendido(new Double(0.0)); |
||
| 1490 | // } |
||
| 1491 | |||
| 1492 | ParametrosConsultaVendasDTO parametrosConsultaVendasDoDia = new ParametrosConsultaVendasDTO(); |
||
| 1493 | parametrosConsultaVendasDoDia.setDataInicial(DataUtils.getDataComHorarioMinimo(DataUtils.getDataAtual())); |
||
| 1494 | parametrosConsultaVendasDoDia.setDataFinal(DataUtils.getDataComHorarioMaximo(DataUtils.getDataAtual())); |
||
| 1495 | parametrosConsultaVendasDoDia.setVendedor(vendedor); |
||
| 1496 | rankingVendedor.setValorTotalVendidoDoDia(lancamentoService.obterValorTotalVendidoDosProdutosDaPromocaoInterna(parametrosConsultaVendasDoDia)); |
||
| 1497 | return rankingVendedor; |
||
| 1498 | } |
||
| 1499 | |||
| 1500 | @Override |
||
| 1501 | public List<VendasDTO> consultarVendas(ParametrosConsultaVendasDTO parametrosConsultaVendasDTO) { |
||
| 1502 | return vendaRepository.consultarVendas(parametrosConsultaVendasDTO); |
||
| 1503 | } |
||
| 1504 | |||
| 1505 | /* |
||
| 1506 | @Override |
||
| 1507 | public List<RankingVendedorDTO> consultarRankingVendedores(Date dataInicial, Date dataFinal) { |
||
| 1508 | List<RankingVendedorDTO> rankingVendedores = new ArrayList<RankingVendedorDTO>(); |
||
| 1509 | List<Vendedor> vendedores = vendedorService.consultarVendedoresColaboradores(); |
||
| 1510 | for (Vendedor vendedor : vendedores) { |
||
| 1511 | ParametrosConsultaVendasDTO parametrosConsultaVendas = new ParametrosConsultaVendasDTO(); |
||
| 1512 | parametrosConsultaVendas.setDataInicial(DataUtils.getDataComHorarioMinimo(dataInicial)); |
||
| 1513 | parametrosConsultaVendas.setDataFinal(DataUtils.getDataComHorarioMaximo(dataFinal)); |
||
| 1514 | parametrosConsultaVendas.setVendedor(vendedor); |
||
| 1515 | RankingVendedorDTO rankingVendedor = new RankingVendedorDTO(); |
||
| 1516 | rankingVendedor.setVendedor(vendedor); |
||
| 1517 | rankingVendedor.setValorTotalVendido(obterValorTotalBrutoDeVendasPorLojaEePeriodo(parametrosConsultaVendas)); |
||
| 1518 | rankingVendedor.setQuantidadePedidos(obterQuantidadePedidos(vendedor, dataInicial, dataFinal)); |
||
| 1519 | rankingVendedores.add(rankingVendedor); |
||
| 1520 | } |
||
| 1521 | return (List<RankingVendedorDTO>) ordenador.ordenar(rankingVendedores); |
||
| 1522 | } |
||
| 1523 | */ |
||
| 1524 | |||
| 1525 | } |