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