Rev 195 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 106 | espaco | 1 | package br.com.ec.domain.model; |
| 2 | |||
| 3 | import java.io.Serializable; |
||
| 4 | import java.util.Date; |
||
| 5 | import java.util.Random; |
||
| 6 | |||
| 7 | import javax.persistence.Column; |
||
| 8 | import javax.persistence.Entity; |
||
| 9 | import javax.persistence.GeneratedValue; |
||
| 10 | import javax.persistence.GenerationType; |
||
| 11 | import javax.persistence.Id; |
||
| 12 | import javax.persistence.JoinColumn; |
||
| 13 | import javax.persistence.ManyToOne; |
||
| 14 | import javax.persistence.SequenceGenerator; |
||
| 15 | import javax.persistence.Table; |
||
| 16 | import javax.persistence.Transient; |
||
| 17 | import javax.validation.constraints.NotNull; |
||
| 18 | import javax.validation.constraints.Size; |
||
| 19 | |||
| 20 | import org.hibernate.annotations.ForeignKey; |
||
| 21 | import org.hibernate.validator.constraints.NotEmpty; |
||
| 22 | |||
| 195 | espaco | 23 | import br.com.ec.core.interfaces.Alterar; |
| 24 | import br.com.ec.core.interfaces.Cadastrar; |
||
| 25 | import br.com.ec.core.util.DataUtils; |
||
| 26 | import br.com.ec.core.util.StringUtil; |
||
| 27 | import br.com.ec.core.util.VerificadorUtil; |
||
| 106 | espaco | 28 | import br.com.ec.domain.model.tipos.TipoCupom; |
| 693 | blopes | 29 | import br.com.ec.domain.model.tipos.TipoRegraCupom; |
| 106 | espaco | 30 | |
| 31 | @Entity |
||
| 32 | @Table(name="sec_cupom", schema="sc_sec") |
||
| 33 | public class Cupom implements Serializable, Cloneable { |
||
| 34 | |||
| 35 | private static final long serialVersionUID = 1L; |
||
| 36 | |||
| 37 | private Long sequencial; |
||
| 38 | private String codigo; |
||
| 39 | private String descricao; |
||
| 40 | private String observacao; |
||
| 41 | private String tipoCupom; |
||
| 693 | blopes | 42 | private String tipoRegraCupom; |
| 106 | espaco | 43 | private Double valor; |
| 44 | private Double percentual; |
||
| 45 | private Integer quantidadeUtilizada; |
||
| 46 | private Date dataHoraUtilizado; |
||
| 47 | private Date dataHoraValidade; |
||
| 48 | private Date dataHoraEmissao; |
||
| 49 | private Boolean limiteUtilizacao; |
||
| 50 | private Boolean publico; |
||
| 51 | private Boolean ativo; |
||
| 52 | |||
| 53 | private Cliente cliente; |
||
| 54 | |||
| 55 | private Lancamento lancamento; |
||
| 56 | |||
| 57 | public Cupom() {} |
||
| 58 | |||
| 59 | public Cupom(Lancamento lancamento) { |
||
| 60 | this.lancamento = lancamento; |
||
| 61 | } |
||
| 62 | |||
| 63 | @Id |
||
| 64 | @SequenceGenerator(name = "sq_cupom") |
||
| 65 | @GeneratedValue(strategy = GenerationType.IDENTITY) |
||
| 66 | @Column(name="seq_cupom", nullable=false) |
||
| 67 | public Long getSequencial() { |
||
| 68 | return sequencial; |
||
| 69 | } |
||
| 70 | public void setSequencial(Long sequencial) { |
||
| 71 | this.sequencial = sequencial; |
||
| 72 | } |
||
| 73 | |||
| 74 | @Column(name="cod_cupom", nullable=false) |
||
| 75 | @Size(max = 20, message = "Limite de caracteres ultrapassado: Código") |
||
| 76 | @NotEmpty(message = "Parâmetro obrigatório não preenchido: Nome", groups = {Cadastrar.class, Alterar.class}) |
||
| 77 | public String getCodigo() { |
||
| 78 | return codigo; |
||
| 79 | } |
||
| 80 | public void setCodigo(String codigo) { |
||
| 81 | this.codigo = StringUtil.setarUpperCaseComTrim(codigo); |
||
| 82 | } |
||
| 83 | |||
| 84 | @Column(name="dsc_cupom", nullable=false) |
||
| 85 | @Size(max = 120, message = "Limite de caracteres ultrapassado: Descrição") |
||
| 86 | public String getDescricao() { |
||
| 87 | return descricao; |
||
| 88 | } |
||
| 89 | public void setDescricao(String descricao) { |
||
| 90 | this.descricao = StringUtil.setarUpperCaseComTrim(descricao); |
||
| 91 | } |
||
| 92 | |||
| 93 | @Column(name="dsc_observacao", nullable=false) |
||
| 94 | @Size(max = 1000, message = "Limite de caracteres ultrapassado: Observação") |
||
| 95 | public String getObservacao() { |
||
| 96 | return observacao; |
||
| 97 | } |
||
| 98 | public void setObservacao(String observacao) { |
||
| 99 | this.observacao = StringUtil.setarUpperCaseComTrim(observacao); |
||
| 100 | } |
||
| 101 | |||
| 102 | @Column(name="tip_cupom", nullable=false) |
||
| 103 | @NotNull(message = "Parâmetro obrigatório não preenchido: Tipo do cupom", groups = {Cadastrar.class, Alterar.class}) |
||
| 104 | public String getTipoCupom() { |
||
| 105 | return tipoCupom; |
||
| 106 | } |
||
| 107 | public void setTipoCupom(String tipoCupom) { |
||
| 108 | this.tipoCupom = tipoCupom; |
||
| 109 | } |
||
| 110 | @Transient |
||
| 111 | public String getDescricaoDoTipoCupom() { |
||
| 112 | return VerificadorUtil.naoEstaNulo(tipoCupom)? TipoCupom.parse(tipoCupom).getDescricao() : ""; |
||
| 113 | } |
||
| 114 | |||
| 693 | blopes | 115 | @Column(name="tip_regracupom", nullable=true) |
| 116 | public String getTipoRegraCupom() { |
||
| 117 | return tipoRegraCupom; |
||
| 118 | } |
||
| 119 | public void setTipoRegraCupom(String tipoRegraCupom) { |
||
| 120 | this.tipoRegraCupom = tipoRegraCupom; |
||
| 121 | } |
||
| 122 | @Transient |
||
| 123 | public String getDescricaoDoTipoRegraCupom() { |
||
| 124 | return VerificadorUtil.naoEstaNulo(tipoRegraCupom)? TipoRegraCupom.parse(tipoRegraCupom).getDescricao() : ""; |
||
| 125 | } |
||
| 126 | |||
| 106 | espaco | 127 | @Column(name="val_cupom") |
| 128 | public Double getValor() { |
||
| 129 | return valor; |
||
| 130 | } |
||
| 131 | public void setValor(Double valor) { |
||
| 132 | this.valor = valor; |
||
| 133 | } |
||
| 134 | |||
| 135 | @Column(name="val_percentual_cupom") |
||
| 136 | public Double getPercentual() { |
||
| 137 | return percentual; |
||
| 138 | } |
||
| 139 | public void setPercentual(Double percentual) { |
||
| 140 | this.percentual = percentual; |
||
| 141 | } |
||
| 142 | |||
| 143 | @Column(name="qtd_utilizado") |
||
| 144 | public Integer getQuantidadeUtilizada() { |
||
| 145 | return quantidadeUtilizada; |
||
| 146 | } |
||
| 147 | public void setQuantidadeUtilizada(Integer quantidadeUtilizada) { |
||
| 148 | this.quantidadeUtilizada = quantidadeUtilizada; |
||
| 149 | } |
||
| 150 | |||
| 151 | @Column(name="dth_utilizado") |
||
| 152 | public Date getDataHoraUtilizado() { |
||
| 153 | return dataHoraUtilizado; |
||
| 154 | } |
||
| 155 | public void setDataHoraUtilizado(Date dataHoraUtilizado) { |
||
| 156 | this.dataHoraUtilizado = dataHoraUtilizado; |
||
| 157 | } |
||
| 158 | |||
| 159 | @Column(name="dth_validade") |
||
| 160 | public Date getDataHoraValidade() { |
||
| 161 | return dataHoraValidade; |
||
| 162 | } |
||
| 163 | public void setDataHoraValidade(Date dataHoraValidade) { |
||
| 164 | this.dataHoraValidade = dataHoraValidade; |
||
| 165 | } |
||
| 166 | |||
| 167 | @NotNull(message = "Informe a data de emissão", groups = {Cadastrar.class, Alterar.class}) |
||
| 168 | @Column(name="dth_emissao", nullable=false) |
||
| 169 | public Date getDataHoraEmissao() { |
||
| 170 | return dataHoraEmissao; |
||
| 171 | } |
||
| 172 | public void setDataHoraEmissao(Date dataHoraEmissao) { |
||
| 173 | this.dataHoraEmissao = dataHoraEmissao; |
||
| 174 | } |
||
| 175 | |||
| 176 | @NotNull(message = "Informe se a utilização é limitada", groups = {Cadastrar.class, Alterar.class}) |
||
| 177 | @Column(name="ind_limite_utilizacao", nullable=false) |
||
| 178 | public Boolean getLimiteUtilizacao() { |
||
| 179 | return limiteUtilizacao; |
||
| 180 | } |
||
| 181 | public void setLimiteUtilizacao(Boolean limiteUtilizacao) { |
||
| 182 | this.limiteUtilizacao = limiteUtilizacao; |
||
| 183 | } |
||
| 184 | |||
| 185 | @NotNull(message = "Informe se o cupom é público", groups = {Cadastrar.class, Alterar.class}) |
||
| 186 | @Column(name="ind_publico", nullable=false) |
||
| 187 | public Boolean getPublico() { |
||
| 188 | return publico; |
||
| 189 | } |
||
| 190 | public void setPublico(Boolean publico) { |
||
| 191 | this.publico = publico; |
||
| 192 | } |
||
| 193 | |||
| 194 | @Column(name="ind_ativo", nullable=false) |
||
| 195 | public Boolean getAtivo() { |
||
| 196 | return ativo; |
||
| 197 | } |
||
| 198 | public void setAtivo(Boolean ativo) { |
||
| 199 | this.ativo = ativo; |
||
| 200 | } |
||
| 201 | |||
| 202 | @Transient |
||
| 203 | public String getValorCupomFormatado() { |
||
| 204 | if (VerificadorUtil.naoEstaNulo(getValor())) { |
||
| 205 | return "R$ " + StringUtil.formatarValor(getValor()); |
||
| 206 | } |
||
| 207 | if (VerificadorUtil.naoEstaNulo(getPercentual())) { |
||
| 208 | return StringUtil.formatarPercentual(getPercentual()/100); |
||
| 209 | } |
||
| 210 | return null; |
||
| 211 | } |
||
| 212 | |||
| 213 | @ManyToOne |
||
| 214 | @ForeignKey(name="fk_cupom_cliente") |
||
| 215 | @JoinColumn(name = "seq_cliente", nullable=true) |
||
| 216 | public Cliente getCliente() { |
||
| 217 | return cliente; |
||
| 218 | } |
||
| 219 | public void setCliente(Cliente cliente) { |
||
| 220 | this.cliente = cliente; |
||
| 221 | } |
||
| 222 | |||
| 223 | @Transient |
||
| 224 | public Lancamento getLancamento() { |
||
| 225 | return lancamento; |
||
| 226 | } |
||
| 227 | public void setLancamento(Lancamento lancamento) { |
||
| 228 | this.lancamento = lancamento; |
||
| 229 | } |
||
| 230 | |||
| 231 | @Override |
||
| 232 | public int hashCode() { |
||
| 233 | final int prime = 31; |
||
| 234 | int result = 1; |
||
| 235 | result = prime * result |
||
| 236 | + ((sequencial == null) ? 0 : sequencial.hashCode()); |
||
| 237 | return result; |
||
| 238 | } |
||
| 239 | |||
| 240 | @Override |
||
| 241 | public boolean equals(Object obj) { |
||
| 242 | if (this == obj) |
||
| 243 | return true; |
||
| 244 | if (obj == null) |
||
| 245 | return false; |
||
| 246 | if (getClass() != obj.getClass()) |
||
| 247 | return false; |
||
| 248 | Cupom other = (Cupom) obj; |
||
| 249 | if (sequencial == null) { |
||
| 250 | if (other.sequencial != null) |
||
| 251 | return false; |
||
| 252 | } else if (!sequencial.equals(other.sequencial)) |
||
| 253 | return false; |
||
| 254 | return true; |
||
| 255 | } |
||
| 256 | |||
| 257 | @Override |
||
| 258 | public Cupom clone() throws CloneNotSupportedException { |
||
| 259 | return (Cupom) super.clone(); |
||
| 260 | } |
||
| 261 | |||
| 262 | @Transient |
||
| 263 | public String getNomeDoCliente() { |
||
| 264 | return VerificadorUtil.naoEstaNulo(getCliente())? getCliente().getNome() : ""; |
||
| 265 | } |
||
| 266 | |||
| 267 | @Transient |
||
| 268 | public void gerarCodigo() { |
||
| 269 | Random random = new Random(); |
||
| 270 | Integer codigoGerado = random.nextInt(999999); |
||
| 271 | setCodigo(codigoGerado.toString()); |
||
| 272 | } |
||
| 273 | |||
| 274 | @Transient |
||
| 275 | public Boolean valido() { |
||
| 276 | if (VerificadorUtil.naoEstaNulo(getDataHoraValidade())) { |
||
| 277 | return DataUtils.getDataAtual().before(getDataHoraValidade()); |
||
| 278 | } |
||
| 279 | return getAtivo(); |
||
| 280 | } |
||
| 281 | |||
| 282 | } |