Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 espaco 1
package br.com.ec.domain.model;
2
 
3
import java.io.Serializable;
4
import java.util.ArrayList;
5
import java.util.Date;
6
import java.util.HashSet;
7
import java.util.List;
8
import java.util.Set;
9
 
10
import javax.persistence.CascadeType;
11
import javax.persistence.Column;
12
import javax.persistence.Entity;
13
import javax.persistence.FetchType;
14
import javax.persistence.GeneratedValue;
15
import javax.persistence.GenerationType;
16
import javax.persistence.Id;
17
import javax.persistence.JoinColumn;
18
import javax.persistence.ManyToOne;
19
import javax.persistence.OneToMany;
20
import javax.persistence.Table;
21
import javax.persistence.Transient;
22
import javax.validation.constraints.NotNull;
23
 
24
import org.hibernate.annotations.ForeignKey;
25
 
26
import br.com.ec.domain.dto.ParametrosVendaDTO;
27
import br.com.ec.domain.model.tipos.TipoSituacaoVenda;
28
import br.edu.cesmac.core.interfaces.Alterar;
29
import br.edu.cesmac.core.interfaces.Cadastrar;
30
import br.edu.cesmac.core.util.DataUtils;
31
import br.edu.cesmac.core.util.StringUtil;
32
import br.edu.cesmac.core.util.VerificadorUtil;
33
 
34
@Entity
35
@Table(name="sec_venda", schema="sc_sec")
36
public class Venda implements Serializable {
37
 
38
        private static final long serialVersionUID = 1L;
39
 
40
        private Long sequencial;
41
        private Loja loja;
42
        private Cliente cliente;
43
        private Usuario usuario;
44
        private Vendedor vendedor;
45
        private Vendedor indicacao;
46
        private Date dataVenda;
47
        private String observacao;
48
        private String tipoSituacao;
49
        private String notaFiscal;
50
        private Maquineta maquineta;
51
        private Set<Lancamento> listaLancamentos;
52
        private Set<VendaFormaPagamento> listaVendaFormaPagamentos;
53
 
54
        private String tipoSituacaoFinanceira;
55
        private String justificativaParaExcluir;
56
 
57
        private Boolean permitirEmissaoNotaFiscal = false;
58
        private Boolean emitirNotaFiscal = false;
59
        private Boolean emitirComanda = false;
60
        private Boolean cpfCnpjNaNotaFiscal = false;
61
 
62
        public Venda() {
63
                listaLancamentos = new HashSet<Lancamento>();
64
                listaVendaFormaPagamentos = new HashSet<VendaFormaPagamento>();
65
                setEmitirNotaFiscal(false);
66
        }
67
 
68
        public Venda(Loja loja, Usuario usuario) {
69
                this.loja = loja;
70
                this.usuario = usuario;
71
        }
72
 
73
        public Venda(Loja loja, Usuario usuario, Vendedor vendedor, Vendedor indicacao) {
74
                this.loja = loja;
75
                this.usuario = usuario;
76
                this.vendedor = vendedor;
77
                this.indicacao = indicacao;
78
        }
79
 
80
        public Venda(Loja loja, Usuario usuario, Vendedor vendedor, Vendedor indicacao, String observacao, String notaFiscal) {
81
                this.loja = loja;
82
                this.usuario = usuario;
83
                this.vendedor = vendedor;
84
                this.indicacao = indicacao;
85
                this.observacao = StringUtil.setarUpperCase(observacao);
86
                this.notaFiscal = notaFiscal;
87
        }
88
 
89
        public Venda(Loja loja, Usuario usuario, Set<Lancamento> listaLancamentos, Set<VendaFormaPagamento> listaVendaFormaPagamentos) {
90
                this.listaLancamentos = listaLancamentos;
91
                this.listaVendaFormaPagamentos = listaVendaFormaPagamentos;
92
                this.loja = loja;
93
                this.usuario = usuario;
94
        }
95
 
96
        public Venda(ParametrosVendaDTO parametrosVenda) {
97
                this.loja = parametrosVenda.getLoja();
98
                this.usuario = parametrosVenda.getUsuario();
99
                this.vendedor = parametrosVenda.getVendedor();
100
                this.cliente = parametrosVenda.getCliente();
101
                this.indicacao = parametrosVenda.getIndicacao();
102
                this.observacao = StringUtil.setarUpperCase(parametrosVenda.getObservacao());
103
                this.notaFiscal = parametrosVenda.getNotaFiscal();
104
                this.cpfCnpjNaNotaFiscal = parametrosVenda.getCpfCnpjNaNotaFiscal();
105
        }
106
 
107
        @Id
108
        @GeneratedValue(strategy = GenerationType.IDENTITY)
109
        @Column(name="seq_venda", nullable=false)
110
        public Long getSequencial() {
111
                return sequencial;
112
        }
113
        public void setSequencial(Long sequencial) {
114
                this.sequencial = sequencial;
115
        }
116
 
117
        @ManyToOne
118
        @ForeignKey(name="fk_venda_loja")
119
        @NotNull(message = "Informe a loja", groups = {Cadastrar.class, Alterar.class})
120
        @JoinColumn(name = "seq_loja", nullable = false)
121
        public Loja getLoja() {
122
                return loja;
123
        }
124
        public void setLoja(Loja loja) {
125
                this.loja = loja;
126
        }
127
 
128
        @ManyToOne
129
        @ForeignKey(name="fk_venda_cliente")
130
        @JoinColumn(name = "seq_cliente", nullable = true)
131
        public Cliente getCliente() {
132
                return cliente;
133
        }
134
        public void setCliente(Cliente cliente) {
135
                this.cliente = cliente;
136
        }
137
 
138
        @ManyToOne
139
        @ForeignKey(name="fk_venda_usuario")
140
        @JoinColumn(name = "seq_usuario", nullable = false)
141
        public Usuario getUsuario() {
142
                return usuario;
143
        }
144
        public void setUsuario(Usuario usuario) {
145
                this.usuario = usuario;
146
        }
147
 
148
        @ManyToOne
149
        @ForeignKey(name="fk_venda_vendedor")
150
        @NotNull(message = "Informe o vendedor", groups = {Cadastrar.class, Alterar.class})
151
        @JoinColumn(name = "seq_vendedor", nullable = true)
152
        public Vendedor getVendedor() {
153
                return vendedor;
154
        }
155
        public void setVendedor(Vendedor vendedor) {
156
                this.vendedor = vendedor;
157
        }
158
 
159
        @ManyToOne
160
        @ForeignKey(name="fk_venda_indicacao")
161
        @JoinColumn(name = "seq_indicacao", nullable = true)
162
        public Vendedor getIndicacao() {
163
                return indicacao;
164
        }
165
        public void setIndicacao(Vendedor indicacao) {
166
                this.indicacao = indicacao;
167
        }
168
 
169
        @Column(name="dth_venda")
170
        public Date getDataVenda() {
171
                return dataVenda;
172
        }
173
        public void setDataVenda(Date dataVenda) {
174
                this.dataVenda = dataVenda;
175
        }
176
 
177
        @Column(name="dsc_observacao")
178
        public String getObservacao() {
179
                return observacao;
180
        }
181
        public void setObservacao(String observacao) {
182
                this.observacao = StringUtil.setarUpperCase(observacao);
183
        }
184
 
185
        @Column(name="tip_situacao")
186
        public String getTipoSituacao() {
187
                return tipoSituacao;
188
        }
189
        public void setTipoSituacao(String tipoSituacao) {
190
                this.tipoSituacao = tipoSituacao;
191
        }
192
 
193
        @Transient
194
        public String getTipoSituacaoDescricao() {
195
                return TipoSituacaoVenda.parse(getTipoSituacao()).getDescricao();
196
        }
197
 
198
        @Column(name="dsc_nota_fiscal")
199
        public String getNotaFiscal() {
200
                return notaFiscal;
201
        }
202
        public void setNotaFiscal(String notaFiscal) {
203
                this.notaFiscal = notaFiscal;
204
        }
205
 
206
        @ManyToOne
207
        @ForeignKey(name = "fk_venda_maquineta")
208
        @JoinColumn(name="seq_maquineta", referencedColumnName="seq_maquineta", insertable=true, updatable=true)
209
        public Maquineta getMaquineta() {
210
                return maquineta;
211
        }
212
        public void setMaquineta(Maquineta maquineta) {
213
                this.maquineta = maquineta;
214
        }
215
 
216
        @OneToMany(mappedBy="venda", cascade=CascadeType.ALL, fetch=FetchType.LAZY, orphanRemoval=true)
217
        public Set<Lancamento> getListaLancamentos() {
218
                return listaLancamentos;
219
        }
220
        public void setListaLancamentos(Set<Lancamento> listaLancamentos) {
221
                this.listaLancamentos = listaLancamentos;
222
        }
223
 
224
        @OneToMany(mappedBy="venda", cascade=CascadeType.ALL, fetch=FetchType.LAZY, orphanRemoval=true)
225
        public Set<VendaFormaPagamento> getListaVendaFormaPagamentos() {
226
                return listaVendaFormaPagamentos;
227
        }
228
        public void setListaVendaFormaPagamentos(Set<VendaFormaPagamento> listaVendaFormaPagamentos) {
229
                this.listaVendaFormaPagamentos = listaVendaFormaPagamentos;
230
        }
231
 
232
        @Transient
233
        public String getTipoSituacaoFinanceira() {
234
                return tipoSituacaoFinanceira;
235
        }
236
        @Transient
237
        public void setTipoSituacaoFinanceira(String tipoSituacaoFinanceira) {
238
                this.tipoSituacaoFinanceira = tipoSituacaoFinanceira;
239
        }
240
 
241
        @Transient
242
        public String getJustificativaParaExcluir() {
243
                return justificativaParaExcluir;
244
        }
245
        public void setJustificativaParaExcluir(String justificativaParaExcluir) {
246
                this.justificativaParaExcluir = StringUtil.setarUpperCase(justificativaParaExcluir);
247
        }
248
 
249
        @Transient
250
        public Boolean getPermitirEmissaoNotaFiscal() {
251
                return permitirEmissaoNotaFiscal;
252
        }
253
        public void setPermitirEmissaoNotaFiscal(Boolean permitirEmissaoNotaFiscal) {
254
                this.permitirEmissaoNotaFiscal = permitirEmissaoNotaFiscal;
255
        }
256
        public void verificarSePermiteEmissaoNotaFiscal() {
257
                setPermitirEmissaoNotaFiscal(this.getLoja().emissaoNotaFiscalPermitida());
258
        }
259
 
260
        @Transient
261
        public Boolean getEmitirNotaFiscal() {
262
                return emitirNotaFiscal;
263
        }
264
        public void setEmitirNotaFiscal(Boolean emitirNotaFiscal) {
265
                this.emitirNotaFiscal = emitirNotaFiscal;
266
        }
267
 
268
        @Transient
269
        public Boolean getEmitirComanda() {
270
                return emitirComanda;
271
        }
272
        public void setEmitirComanda(Boolean emitirComanda) {
273
                this.emitirComanda = emitirComanda;
274
        }
275
 
276
        @Transient
277
        public Boolean getCpfCnpjNaNotaFiscal() {
278
                return cpfCnpjNaNotaFiscal;
279
        }
280
        public void setCpfCnpjNaNotaFiscal(Boolean cpfCnpjNaNotaFiscal) {
281
                this.cpfCnpjNaNotaFiscal = cpfCnpjNaNotaFiscal;
282
        }
283
 
284
        @Transient
285
        public List<Lancamento> getLancamentos() {
286
                List<Lancamento> lancamentos = new ArrayList<Lancamento>();
287
                lancamentos.addAll(listaLancamentos);
288
                return lancamentos;
289
        }
290
 
291
        @Transient
292
        public List<Lancamento> getLancamentosValidos() {
293
                List<Lancamento> lancamentos = new ArrayList<Lancamento>();
294
                for (Lancamento lancamento : listaLancamentos) {
295
                        if (lancamento.getAtivo()) {
296
                                lancamentos.add(lancamento);
297
                        }
298
                }
299
                return lancamentos;
300
        }
301
 
302
        @Transient
303
        public List<VendaFormaPagamento> getVendaFormaPagamentos() {
304
                List<VendaFormaPagamento> pagamentos = new ArrayList<VendaFormaPagamento>();
305
                pagamentos.addAll(listaVendaFormaPagamentos);
306
                return pagamentos;
307
        }
308
        /*
309
        @Transient
310
        public List<Parcela> getParcelas() {
311
                List<Parcela> parcelas = new ArrayList<Parcela>();
312
                parcelas.addAll(listaParcelas);
313
                return parcelas;
314
        }
315
        */
316
        @Transient
317
        public Double getTotalLancamentos() {
318
                Double total = 0.0;
319
                for (Lancamento lancamento : getLancamentos()) {
320
                        total = total + lancamento.getValorVenda();
321
                }
322
                return total;  
323
        }
324
 
325
        @Transient
326
        public Double getTotalVenda() {
327
                Double total = new Double(0.0);
328
                for (Lancamento lancamento : getLancamentos()) {
329
                        if (lancamento.getAtivo()) {
330
                                total = total + lancamento.getValorVenda();
331
                        }
332
                }
333
                return total;
334
        }
335
 
336
        @Transient
337
        public Double getTotalPago() {
338
                Double total = new Double(0.0);
339
                for (VendaFormaPagamento pagamento : getVendaFormaPagamentos()) {
340
                        total = total + pagamento.getValorPagamento();
341
                }
342
                return total;
343
        }
344
 
345
        @Transient
346
        public Double getTotalPagoEmDinheiro() {
347
                Double total = new Double(0.0);
348
                for (VendaFormaPagamento pagamento : getVendaFormaPagamentos()) {
349
                        if (pagamento.getFormaPagamento().formaPagamentoEhDinheiro()) {
350
                                total = total + pagamento.getValorPagamento();
351
                        }
352
                }
353
                return total;
354
        }
355
 
356
        @Transient
357
        public Double getTotalPagoEmCupom() {
358
                Double total = new Double(0.0);
359
                for (VendaFormaPagamento pagamento : getVendaFormaPagamentos()) {
360
                        if (pagamento.getFormaPagamento().formaPagamentoEhCupom()) {
361
                                total = total + pagamento.getValorPagamento();
362
                        }
363
                }
364
                return total;
365
        }
366
 
367
        @Transient
368
        public String getProdutosVendidos() {
369
                StringBuilder produtosVendidos = new StringBuilder();
370
                for (Lancamento lancamento : getLancamentos()) {
371
                        if (lancamento.getAtivo()) {
372
                                produtosVendidos.append("[");
373
                                produtosVendidos.append(lancamento.getProduto().getCodigoProdutoPadrao());
374
                                produtosVendidos.append(": ");
375
                                produtosVendidos.append(lancamento.getProduto().getDescricaoDoModelo());
376
                                produtosVendidos.append(", ");
377
                                produtosVendidos.append(lancamento.getProduto().getDescricao());
378
                                produtosVendidos.append(" POR R$ ");
379
                                produtosVendidos.append(StringUtil.formatarValor(lancamento.getValorVenda()));
380
                                produtosVendidos.append("] ");
381
                        }
382
                }
383
                return produtosVendidos.toString();
384
        }
385
 
386
        @Transient
387
        public Long getSequencialDaLoja() {
388
                return VerificadorUtil.naoEstaNulo(loja)? loja.getSequencial() : null;
389
        }
390
 
391
        @Transient
392
        public Long getSequencialDoVendedor() {
393
                return VerificadorUtil.naoEstaNulo(vendedor)? vendedor.getSequencial() : null;
394
        }
395
 
396
        @Transient
397
        public String getLinhaPorSituacao() {
398
                if (getTipoSituacao().equals(TipoSituacaoVenda.NOVO.getValor())) return "linha-novo";
399
                if (getTipoSituacao().equals(TipoSituacaoVenda.CONFERIDO.getValor())) return "linha-conferido";
400
                if (getTipoSituacao().equals(TipoSituacaoVenda.FINALIZADO.getValor())) return "linha-finalizado";
401
                if (getTipoSituacao().equals(TipoSituacaoVenda.PENDENCIAS.getValor())) return "linha-pendencias";
402
                return "even-row";
403
        }
404
 
405
        @Transient
406
        public String getNomeVendedorDaVenda() {
407
                return VerificadorUtil.naoEstaNulo(getVendedor())? getVendedor().getNomeDaPessoa() : null;
408
        }
409
 
410
        @Transient
411
        public Integer getQuantidadeDiasDaVenda() {
412
                return DataUtils.calcularDiferenceEmDiasEntreDuasDatas(DataUtils.getDataAtual(), getDataVenda());
413
        }
414
 
415
        @Override
416
        public int hashCode() {
417
                final int prime = 31;
418
                int result = 1;
419
                result = prime * result + ((sequencial == null) ? 0 : sequencial.hashCode());
420
                return result;
421
        }
422
 
423
        @Override
424
        public boolean equals(Object obj) {
425
                if (this == obj)
426
                        return true;
427
                if (obj == null)
428
                        return false;
429
                if (getClass() != obj.getClass())
430
                        return false;
431
                Venda other = (Venda) obj;
432
                if (sequencial == null) {
433
                        if (other.sequencial != null)
434
                                return false;
435
                } else if (!sequencial.equals(other.sequencial))
436
                        return false;
437
                return true;
438
        }
439
 
440
        @Transient
441
        public Pessoa getPessoaJuridicaDaLoja() {
442
                return VerificadorUtil.naoEstaNulo(getLoja())? getLoja().getPessoaJuridica() : null;
443
        }
444
 
445
        @Transient
446
        public String montarDadosDaVenda(List<Lancamento> lancamentos, List<VendaFormaPagamento> pagamentos) {
447
                StringBuilder conteudo = new StringBuilder();
448
                //conteudo.append("OPERAÇÃO REALIZADA POR: " + this.getUsuario().getNome() + "\n");
449
                conteudo.append("VENDA ID: " + this.getSequencial() + "\n");
450
                conteudo.append("DATA: " + DataUtils.converterDataComHorarioParaString(this.getDataVenda()) + "\n");
451
                conteudo.append("LOJA: " + this.getLoja().getDescricao() + "\n");
452
                conteudo.append("VENDEDOR: " + this.getVendedor().getNomeDaPessoa() + "\n");
453
                conteudo.append("TOTAL: " + valorTotalVenda(lancamentos) + "\n");
454
                conteudo.append("OBS.: " + this.getObservacao() + "\n");
455
                if (VerificadorUtil.naoEstaNuloOuVazio(this.getJustificativaParaExcluir())) {
456
                        conteudo.append("JUSTIFICATIVA: " + this.getJustificativaParaExcluir() + "\n");
457
                } else {
458
                        conteudo.append("SEM JUSTIFICATIVA\n");
459
                }
460
                conteudo.append("__________________________________________________\n\n");
461
                for (Lancamento lancamento : lancamentos) {
462
                        conteudo.append(lancamento.getProduto().getCodigoProdutoPadrao() + " | ");
463
                        conteudo.append(lancamento.getProduto().getDescricaoComModeloCompleta() + ", ");
464
                        conteudo.append("R$" + lancamento.getValorVenda());
465
                        if (lancamento.comDescontos()) {
466
                                conteudo.append(" (COM DESCONTO DE R$" + lancamento.valorDescontos() + ")");
467
                        }
468
                        conteudo.append("\n");
469
                }
470
                conteudo.append("__________________________________________________\n\n");
471
                for (VendaFormaPagamento pagamento : pagamentos) {
472
                        if (VerificadorUtil.naoEstaNulo(pagamento.getBandeiraCartao())) {
473
                                conteudo.append(pagamento.getFormaPagamento().getDescricao() + " - " + pagamento.getBandeiraCartao().getDescricao() + ": R$" +  pagamento.getValorPagamento() + "\n");
474
                        } else {
475
                                conteudo.append(pagamento.getFormaPagamento().getDescricao() + ": R$" + pagamento.getValorPagamento() + "\n");
476
                        }
477
                        conteudo.append("\n");
478
                }
479
                return conteudo.toString();
480
        }
481
 
482
        @Transient
483
        private Double valorTotalVenda(List<Lancamento> lancamentos) {
484
                Double total = new Double(0.0);
485
                for (Lancamento lancamento : lancamentos) {
486
                        if (lancamento.getAtivo()) {
487
                                total = total + lancamento.getValorVenda();
488
                        }
489
                }
490
                return total;
491
        }
492
 
493
        @Transient
494
        public String getEmailDoCliente() {
495
                return VerificadorUtil.naoEstaNulo(getCliente())? getCliente().getEmail() : "";
496
        }
497
 
498
        @Transient
499
        public String getCertificadoDaLoja() {
500
                return VerificadorUtil.naoEstaNulo(getLoja())? getLoja().getDescricaoCertificado() : "";
501
        }
502
 
503
        @Transient
504
        public String getDescricaoMaquineta() {
505
                return VerificadorUtil.naoEstaNulo(getMaquineta())? getMaquineta().descricaoCompleta() : "NENHUMA";
506
        }
507
 
508
}