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