Subversion Repositories Integrator Subversion

Rev

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