Rev 697 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 106 | espaco | 1 | package br.com.ec.domain.service.cupom.impl; |
| 2 | |||
| 3 | import org.springframework.beans.factory.annotation.Autowired; |
||
| 4 | import org.springframework.stereotype.Service; |
||
| 5 | |||
| 195 | espaco | 6 | import br.com.ec.core.exception.NegocioException; |
| 7 | import br.com.ec.core.generic.AbstractService; |
||
| 8 | import br.com.ec.core.generic.GenericRepository; |
||
| 9 | import br.com.ec.core.util.DataUtils; |
||
| 10 | import br.com.ec.core.util.VerificadorUtil; |
||
| 11 | import br.com.ec.core.validador.Validador; |
||
| 693 | blopes | 12 | import br.com.ec.domain.dto.ParametrosVendaDTO; |
| 106 | espaco | 13 | import br.com.ec.domain.model.Cupom; |
| 693 | blopes | 14 | import br.com.ec.domain.model.Lancamento; |
| 15 | import br.com.ec.domain.model.SubtipoProduto; |
||
| 106 | espaco | 16 | import br.com.ec.domain.model.tipos.TipoCupom; |
| 693 | blopes | 17 | import br.com.ec.domain.model.tipos.TipoEstiloProduto; |
| 18 | import br.com.ec.domain.model.tipos.TipoProduto; |
||
| 19 | import br.com.ec.domain.model.tipos.TipoRegraCupom; |
||
| 106 | espaco | 20 | import br.com.ec.domain.service.cupom.CupomService; |
| 21 | import br.com.ec.domain.service.lancamento.LancamentoService; |
||
| 693 | blopes | 22 | import br.com.ec.domain.shared.ConstantesSEC; |
| 106 | espaco | 23 | import br.com.ec.infrastructure.repository.CupomRepository; |
| 24 | |||
| 25 | @Service |
||
| 26 | public class CupomServiceImpl extends AbstractService<Cupom> implements CupomService { |
||
| 27 | |||
| 28 | private CupomRepository cupomRepository; |
||
| 29 | private LancamentoService lancamentoService; |
||
| 30 | |||
| 31 | @Autowired |
||
| 32 | public CupomServiceImpl(Validador validador, CupomRepository cupomRepository, LancamentoService lancamentoService) { |
||
| 33 | super(validador); |
||
| 34 | this.cupomRepository = cupomRepository; |
||
| 35 | this.lancamentoService = lancamentoService; |
||
| 36 | } |
||
| 37 | |||
| 38 | @Override |
||
| 39 | protected GenericRepository<Cupom> getRepository() { |
||
| 40 | return cupomRepository; |
||
| 41 | } |
||
| 42 | |||
| 43 | @Override |
||
| 44 | protected void regrasNegocioCadastrar(Cupom cupom) { |
||
| 45 | verificarSeCodigoJaExiste(cupom); |
||
| 46 | } |
||
| 47 | |||
| 48 | private void verificarSeCodigoJaExiste(Cupom cupom) { |
||
| 49 | if (VerificadorUtil.naoEstaNulo(consultarCupomPorCodigo(cupom.getCodigo()))) { |
||
| 50 | throw new NegocioException("CÓDIGO DO CUPOM JÁ EXISTE"); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | @Override |
||
| 55 | public Cupom consultarCupomPorCodigo(String codigo) { |
||
| 56 | return cupomRepository.consultarCupomPorCodigo(codigo); |
||
| 57 | } |
||
| 58 | |||
| 59 | @Override |
||
| 60 | public String gerarCodigoValido() { |
||
| 61 | Cupom cupomGerado = new Cupom(); |
||
| 62 | cupomGerado.gerarCodigo(); |
||
| 63 | if (VerificadorUtil.naoEstaNulo(consultarCupomPorCodigo(cupomGerado.getCodigo()))) { |
||
| 64 | return gerarCodigoValido(); |
||
| 65 | } |
||
| 66 | return cupomGerado.getCodigo(); |
||
| 67 | } |
||
| 68 | |||
| 69 | @Override |
||
| 70 | public void excluirCupom(Cupom cupom) { |
||
| 71 | if (existeCupomComLancamentoVendas(cupom)) { |
||
| 72 | throw new NegocioException("Não é permitido excluir um cupom com lançamentos em vendas"); |
||
| 73 | } |
||
| 74 | super.excluir(cupom); |
||
| 75 | } |
||
| 76 | |||
| 77 | private Boolean existeCupomComLancamentoVendas(Cupom cupom) { |
||
| 78 | Integer quantidadeUtilizada = lancamentoService.obterQuantidadeVendasComCupom(cupom); |
||
| 79 | if (VerificadorUtil.naoEstaNulo(quantidadeUtilizada)) { |
||
| 80 | if (quantidadeUtilizada > 0) { |
||
| 81 | return true; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | return false; |
||
| 85 | } |
||
| 86 | |||
| 87 | @Override |
||
| 88 | public Cupom separarCupom(Cupom cupom, Double valorSeparacao) { |
||
| 89 | verificarCupomValido(cupom); |
||
| 90 | if (0.0 >= valorSeparacao || valorSeparacao >= cupom.getValor()) { |
||
| 91 | throw new NegocioException("VALOR NÃO PERMITIDO"); |
||
| 92 | } |
||
| 93 | Cupom cupomNovo = new Cupom(); |
||
| 94 | try { |
||
| 95 | cupomNovo = cupom.clone(); |
||
| 96 | } catch (CloneNotSupportedException e) { |
||
| 97 | e.printStackTrace(); |
||
| 98 | } |
||
| 99 | cupomNovo.setSequencial(null); |
||
| 100 | cupomNovo.setCodigo(gerarCodigoValido()); |
||
| 101 | cupomNovo.setDataHoraEmissao(DataUtils.getDataAtual()); |
||
| 102 | cupomNovo.setValor(valorSeparacao); |
||
| 103 | cupomNovo.setObservacao("PARCIAL DO CUPOM: " + cupom.getCodigo() + "\n" + cupomNovo.getObservacao()); |
||
| 104 | cadastrar(cupomNovo); |
||
| 105 | |||
| 106 | cupom.setValor(cupom.getValor() - valorSeparacao); |
||
| 107 | cupom.setObservacao("PARCIAL DO CUPOM: " + cupomNovo.getCodigo() + "\n" + cupom.getObservacao()); |
||
| 108 | alterar(cupom); |
||
| 109 | |||
| 110 | return cupomNovo; |
||
| 111 | } |
||
| 112 | |||
| 113 | @Override |
||
| 114 | public Cupom unirCupons(Cupom cupom, Cupom cupomParaUnir) { |
||
| 115 | verificarCupomValido(cupom); |
||
| 116 | verificarCupomValido(cupomParaUnir); |
||
| 117 | if (!cupom.getCliente().equals(cupomParaUnir.getCliente())) { |
||
| 118 | throw new NegocioException("OPERAÇÃO NÃO PERMITIRDA: CUPONS PARA DIFERENTES CLIENTES"); |
||
| 119 | } |
||
| 120 | if (VerificadorUtil.naoEstaNulo(cupom.getPercentual()) || VerificadorUtil.naoEstaNulo(cupomParaUnir.getPercentual())) { |
||
| 121 | throw new NegocioException("OPERAÇÃO NÃO PERMITIRDA: CUPOM COM PERCENTUAL"); |
||
| 122 | } |
||
| 123 | Cupom cupomNovo = new Cupom(); |
||
| 124 | try { |
||
| 125 | cupomNovo = cupom.clone(); |
||
| 126 | } catch (CloneNotSupportedException e) { |
||
| 127 | e.printStackTrace(); |
||
| 128 | } |
||
| 129 | cupomNovo.setSequencial(null); |
||
| 130 | cupomNovo.setCodigo(gerarCodigoValido()); |
||
| 131 | cupomNovo.setDataHoraEmissao(DataUtils.getDataAtual()); |
||
| 132 | cupomNovo.setValor(cupom.getValor() + cupomParaUnir.getValor()); |
||
| 133 | cupomNovo.setObservacao("UNIÃO DOS CUPONS: " + cupom.getCodigo() + " E " + cupomParaUnir.getCodigo() + "\n"); |
||
| 134 | cupomNovo.setObservacao(cupomNovo.getObservacao() + "\n" + cupom.getObservacao() + "\n" + cupomParaUnir.getObservacao()); |
||
| 135 | cadastrar(cupomNovo); |
||
| 136 | |||
| 137 | cupom.setAtivo(false); |
||
| 138 | cupom.setObservacao("UNIDO NO CUPOM: " + cupomNovo.getCodigo() + "\n" + cupom.getObservacao()); |
||
| 139 | alterar(cupom); |
||
| 140 | |||
| 141 | cupomParaUnir.setAtivo(false); |
||
| 142 | cupomParaUnir.setObservacao("UNIDO NO CUPOM: " + cupomNovo.getCodigo() + "\n" + cupomParaUnir.getObservacao()); |
||
| 143 | alterar(cupomParaUnir); |
||
| 144 | return cupomNovo; |
||
| 145 | } |
||
| 146 | |||
| 147 | private void verificarCupomValido(Cupom cupom) { |
||
| 148 | if (VerificadorUtil.naoEstaNulo(cupom)) { |
||
| 149 | if (existeCupomComLancamentoVendas(cupom)) { |
||
| 150 | throw new NegocioException("OPERAÇÃO NÃO PERMITIDA; CUPOM JÁ UTILIZADO"); |
||
| 151 | } |
||
| 152 | if (!cupom.getAtivo() || !cupom.valido()) { |
||
| 153 | throw new NegocioException("OPERAÇÃO NÃO PERMITIDA; CUPOM INDISPONÍVEL"); |
||
| 154 | } |
||
| 155 | if (!cupom.getTipoCupom().equals(TipoCupom.REEMBOLSO.getValor())) { |
||
| 156 | throw new NegocioException("OPERAÇÃO NÃO PERMITIDA: CUPOM DEVE SER PARA REEMBOLSO"); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 693 | blopes | 160 | |
| 161 | @Override |
||
| 162 | public void validarRegrasCupom(ParametrosVendaDTO parametrosVenda, Cupom cupomSelecionado) { |
||
| 701 | blopes | 163 | // APENAS_CAPA_PELICULA_C0("C0", "REGRA: APENAS PARA CAPA OU PELÍCULA"); |
| 693 | blopes | 164 | // APENAS_PELICULA_TPUSOFT_P1("P1", "REGRA: APENAS PARA PELÍCULA TPU SOFT"), |
| 165 | // APENAS_PELICULA_TPUSOFTTPASEIRA_P2("P2", "REGRA: APENAS PARA PELÍCULA TPU SOFT TRASEIRA"), |
||
| 166 | // APENAS_PELICULA_CAMERACOMFRONTAL_P3("P3", "REGRA: APENAS PARA PELÍCULA DE CÂMERA NA COMPRA DA FRONTAL"), |
||
| 701 | blopes | 167 | // APENAS_PELICULA_VIDRO_COM_CAPA_P4("P4", "REGRA: APENAS PARA PELÍCULA DE VIDRO, NA COMPRA DE UMA CAPA"), |
| 693 | blopes | 168 | // APENAS_CAPA_C1("C1", "REGRA: APENAS PARA CAPAS"), |
| 169 | // APENAS_2CAPAS_C2("C2", "REGRA: APENAS PARA VENDAS COM 2 CAPAS"), |
||
| 170 | // APENAS_3CAPAS_C3("C3", "REGRA: APENAS PARA VENDAS COM 3 CAPAS"), |
||
| 171 | // APENAS_CAPA_SILICONE_C4("C4", "REGRA: APENAS PARA CAPAS DE SILICONE"); |
||
| 172 | |||
| 701 | blopes | 173 | Boolean verificarSeTemPeliculaTPUFrontalParaP2 = false; |
| 174 | Boolean verificarSeTemPeliculaFrontalParaP3 = false; |
||
| 693 | blopes | 175 | Integer quantidadeCapas = 0; |
| 176 | for (Lancamento lancamento : parametrosVenda.getLancamentos()) { |
||
| 177 | if (lancamento.getProduto().getTipo().equals(TipoProduto.CAPA.getValor())) { |
||
| 178 | quantidadeCapas++; |
||
| 179 | } |
||
| 180 | if (lancamento.getProduto().getTipo().equals(TipoProduto.PELICULA.getValor()) && |
||
| 181 | VerificadorUtil.naoEstaNuloOuVazio(lancamento.getProduto().getSubtipoProduto())) { |
||
| 701 | blopes | 182 | if (lancamento.getProduto().getSubtipoProduto().getSequencial().equals(ConstantesSEC.SubtipoProduto.SEQUENCIAL_SUBTIPO_TPU_SOFT_NORMAL_42) || |
| 183 | lancamento.getProduto().getSubtipoProduto().getSequencial().equals(ConstantesSEC.SubtipoProduto.SEQUENCIAL_SUBTIPO_TPU_SOFT_PRIVACIDADE_43)) { |
||
| 184 | verificarSeTemPeliculaTPUFrontalParaP2 = true; |
||
| 693 | blopes | 185 | } |
| 701 | blopes | 186 | if (lancamento.getProduto().getSubtipoProduto().getSequencial().equals(ConstantesSEC.SubtipoProduto.SEQUENCIAL_SUBTIPO_TPU_SOFT_NORMAL_42) || |
| 187 | lancamento.getProduto().getSubtipoProduto().getSequencial().equals(ConstantesSEC.SubtipoProduto.SEQUENCIAL_SUBTIPO_TPU_SOFT_PRIVACIDADE_43) || |
||
| 188 | lancamento.getProduto().getSubtipoProduto().getSequencial().equals(ConstantesSEC.SubtipoProduto.SEQUENCIAL_SUBTIPO_VIDRO_3D_NORMAL_44) || |
||
| 189 | lancamento.getProduto().getSubtipoProduto().getSequencial().equals(ConstantesSEC.SubtipoProduto.SEQUENCIAL_SUBTIPO_VIDRO_3D_PRIVACIDADE_45)) { |
||
| 190 | verificarSeTemPeliculaFrontalParaP3 = true; |
||
| 191 | } |
||
| 693 | blopes | 192 | } |
| 193 | } |
||
| 194 | |||
| 195 | for (Lancamento lancamento : parametrosVenda.getLancamentos()) { |
||
| 196 | if (lancamento.equals(cupomSelecionado.getLancamento()) && VerificadorUtil.naoEstaNuloOuVazio(cupomSelecionado.getTipoRegraCupom())) { |
||
| 197 | String tipoProdutoLancamento = lancamento.getProduto().getTipo(); |
||
| 198 | SubtipoProduto subtipoProdutoLancamento = lancamento.getProduto().getSubtipoProduto(); |
||
| 199 | |||
| 200 | // REGRAS PARA PELÍCULAS |
||
| 701 | blopes | 201 | if (ehPelicula(cupomSelecionado.getTipoRegraCupom())) { |
| 693 | blopes | 202 | if (!tipoProdutoLancamento.equals(TipoProduto.PELICULA.getValor())) { |
| 203 | throw new RuntimeException(cupomSelecionado.getDescricaoDoTipoRegraCupom()); |
||
| 204 | } |
||
| 205 | if (TipoRegraCupom.APENAS_PELICULA_TPUSOFT_P1.getValor().equals(cupomSelecionado.getTipoRegraCupom())) { |
||
| 206 | if (VerificadorUtil.naoEstaNulo(subtipoProdutoLancamento)) { |
||
| 697 | blopes | 207 | if (!subtipoProdutoLancamento.getSequencial().equals(ConstantesSEC.SubtipoProduto.SEQUENCIAL_SUBTIPO_TPU_SOFT_NORMAL_42) && |
| 693 | blopes | 208 | !subtipoProdutoLancamento.getSequencial().equals(ConstantesSEC.SubtipoProduto.SEQUENCIAL_SUBTIPO_TPU_SOFT_PRIVACIDADE_43)) { |
| 209 | throw new RuntimeException(cupomSelecionado.getDescricaoDoTipoRegraCupom()); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | if (TipoRegraCupom.APENAS_PELICULA_TPUSOFTTPASEIRA_P2.getValor().equals(cupomSelecionado.getTipoRegraCupom())) { |
||
| 215 | if (VerificadorUtil.naoEstaNulo(subtipoProdutoLancamento)) { |
||
| 701 | blopes | 216 | if (!verificarSeTemPeliculaTPUFrontalParaP2 || |
| 217 | !subtipoProdutoLancamento.getSequencial().equals(ConstantesSEC.SubtipoProduto.SEQUENCIAL_SUBTIPO_TPU_SOFT_TRASEIRA_46)) { |
||
| 693 | blopes | 218 | throw new RuntimeException(cupomSelecionado.getDescricaoDoTipoRegraCupom()); |
| 219 | } |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | if (TipoRegraCupom.APENAS_PELICULA_CAMERACOMFRONTAL_P3.getValor().equals(cupomSelecionado.getTipoRegraCupom())) { |
||
| 224 | if (VerificadorUtil.naoEstaNulo(subtipoProdutoLancamento)) { |
||
| 701 | blopes | 225 | if (!verificarSeTemPeliculaFrontalParaP3 || |
| 226 | !(subtipoProdutoLancamento.getSequencial().equals(ConstantesSEC.SubtipoProduto.SEQUENCIAL_SUBTIPO_TPU_SOFT_CAMERA_47) || |
||
| 227 | subtipoProdutoLancamento.getSequencial().equals(ConstantesSEC.SubtipoProduto.SEQUENCIAL_SUBTIPO_VIDRO_CAMERA_48))) { |
||
| 693 | blopes | 228 | throw new RuntimeException(cupomSelecionado.getDescricaoDoTipoRegraCupom()); |
| 229 | } |
||
| 230 | } |
||
| 231 | } |
||
| 701 | blopes | 232 | |
| 233 | if (TipoRegraCupom.APENAS_PELICULA_VIDRO_COM_CAPA_P4.getValor().equals(cupomSelecionado.getTipoRegraCupom())) { |
||
| 234 | if (!(quantidadeCapas > 0 && |
||
| 235 | subtipoProdutoLancamento.getSequencial().equals(ConstantesSEC.SubtipoProduto.SEQUENCIAL_SUBTIPO_VIDRO_3D_NORMAL_44))) { |
||
| 236 | throw new RuntimeException(cupomSelecionado.getDescricaoDoTipoRegraCupom()); |
||
| 237 | } |
||
| 238 | } |
||
| 693 | blopes | 239 | } |
| 106 | espaco | 240 | |
| 693 | blopes | 241 | // REGRAS PARA CAPAS |
| 701 | blopes | 242 | if (ehCapa(cupomSelecionado.getTipoRegraCupom())) { |
| 693 | blopes | 243 | if (!tipoProdutoLancamento.equals(TipoProduto.CAPA.getValor())) { |
| 244 | throw new RuntimeException(cupomSelecionado.getDescricaoDoTipoRegraCupom()); |
||
| 245 | } |
||
| 246 | if (TipoRegraCupom.APENAS_2CAPAS_C2.getValor().equals(cupomSelecionado.getTipoRegraCupom()) && quantidadeCapas<2) { |
||
| 247 | throw new RuntimeException(cupomSelecionado.getDescricaoDoTipoRegraCupom()); |
||
| 248 | } |
||
| 249 | if (TipoRegraCupom.APENAS_3CAPAS_C3.getValor().equals(cupomSelecionado.getTipoRegraCupom()) && quantidadeCapas<3) { |
||
| 250 | throw new RuntimeException(cupomSelecionado.getDescricaoDoTipoRegraCupom()); |
||
| 251 | } |
||
| 252 | if (TipoRegraCupom.APENAS_CAPA_SILICONE_C4.getValor().equals(cupomSelecionado.getTipoRegraCupom())) { |
||
| 253 | if (VerificadorUtil.naoEstaNuloOuVazio(lancamento.getProduto().getEstilo())) { |
||
| 254 | if (!lancamento.getProduto().getEstilo().equals(TipoEstiloProduto.SILICONE_CASE.getValor())) { |
||
| 255 | throw new RuntimeException(cupomSelecionado.getDescricaoDoTipoRegraCupom()); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | break; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | } |
||
| 701 | blopes | 266 | |
| 267 | private Boolean ehPelicula(String tipoRegraCupom) { |
||
| 268 | return TipoRegraCupom.APENAS_PELICULA_TPUSOFT_P1.getValor().equals(tipoRegraCupom) || |
||
| 269 | TipoRegraCupom.APENAS_PELICULA_TPUSOFTTPASEIRA_P2.getValor().equals(tipoRegraCupom) || |
||
| 270 | TipoRegraCupom.APENAS_PELICULA_CAMERACOMFRONTAL_P3.getValor().equals(tipoRegraCupom) || |
||
| 271 | TipoRegraCupom.APENAS_PELICULA_VIDRO_COM_CAPA_P4.getValor().equals(tipoRegraCupom); |
||
| 272 | } |
||
| 273 | |||
| 274 | private Boolean ehCapa(String tipoRegraCupom) { |
||
| 275 | return TipoRegraCupom.APENAS_CAPA_C1.getValor().equals(tipoRegraCupom) || |
||
| 276 | TipoRegraCupom.APENAS_2CAPAS_C2.getValor().equals(tipoRegraCupom) || |
||
| 277 | TipoRegraCupom.APENAS_3CAPAS_C3.getValor().equals(tipoRegraCupom) || |
||
| 278 | TipoRegraCupom.APENAS_CAPA_SILICONE_C4.getValor().equals(tipoRegraCupom); |
||
| 279 | } |
||
| 693 | blopes | 280 | |
| 106 | espaco | 281 | } |