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