Subversion Repositories Integrator Subversion

Rev

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