package br.com.ec.domain.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.annotations.ForeignKey;
import br.com.ec.core.interfaces.Alterar;
import br.com.ec.core.interfaces.Cadastrar;
import br.com.ec.core.util.DataUtils;
import br.com.ec.core.util.StringUtil;
import br.com.ec.core.util.VerificadorUtil;
@
Entity
@Table
(name=
"sec_venda", schema=
"sc_sec")
public class Venda
implements Serializable {
private static final long serialVersionUID = 1L
;
private Long sequencial
;
private Loja loja
;
// private Cliente cliente;
private Usuario usuario
;
// private Vendedor vendedor;
// private Vendedor indicacao;
private Date dataVenda
;
private String observacao
;
private String tipoSituacao
;
private String notaFiscal
;
private Maquineta maquineta
;
// private String tipoFrete;
// private Double valorFrete;
private List<Lancamento
> listaLancamentos
;
private List<VendaFormaPagamento
> listaVendaFormaPagamentos
;
private String tipoSituacaoFinanceira
;
private String justificativaParaExcluir
;
private Boolean permitirEmissaoNotaFiscal =
false;
private Boolean emitirNotaFiscal =
false;
private Boolean emitirComanda =
false;
private Boolean cpfCnpjNaNotaFiscal =
false;
public Venda
() {
// listaLancamentos = new HashSet<Lancamento>();
// listaVendaFormaPagamentos = new HashSet<VendaFormaPagamento>();
listaLancamentos =
new ArrayList<Lancamento
>();
listaVendaFormaPagamentos =
new ArrayList<VendaFormaPagamento
>();
setEmitirNotaFiscal
(false);
// setTipoFrete(ConstantesSEC.NotaFiscal.Transporte.SEM_OCORRENCIA_TRANSPORTE_9);
}
/*
public Venda(Loja loja, Usuario usuario) {
this.loja = loja;
this.usuario = usuario;
}
public Venda(Loja loja, Usuario usuario, Vendedor vendedor, Vendedor indicacao) {
this.loja = loja;
this.usuario = usuario;
this.vendedor = vendedor;
this.indicacao = indicacao;
}
public Venda(Loja loja, Usuario usuario, Vendedor vendedor, Vendedor indicacao, String observacao, String notaFiscal) {
this.loja = loja;
this.usuario = usuario;
this.vendedor = vendedor;
this.indicacao = indicacao;
this.observacao = StringUtil.setarUpperCaseComTrim(observacao);
this.notaFiscal = notaFiscal;
}
public Venda(Loja loja, Usuario usuario, Set<Lancamento> listaLancamentos, Set<VendaFormaPagamento> listaVendaFormaPagamentos) {
this.listaLancamentos = listaLancamentos;
this.listaVendaFormaPagamentos = listaVendaFormaPagamentos;
this.loja = loja;
this.usuario = usuario;
}
public Venda(ParametrosVendaDTO parametrosVenda) {
this.loja = parametrosVenda.getLoja();
this.usuario = parametrosVenda.getUsuario();
this.vendedor = parametrosVenda.getVendedor();
this.cliente = parametrosVenda.getCliente();
this.indicacao = parametrosVenda.getIndicacao();
this.observacao = StringUtil.setarUpperCaseComTrim(parametrosVenda.getObservacao());
this.notaFiscal = parametrosVenda.getNotaFiscal();
this.cpfCnpjNaNotaFiscal = parametrosVenda.getCpfCnpjNaNotaFiscal();
}
*/
@Id
@SequenceGenerator
(name =
"sq_venda")
@GeneratedValue
(strategy = GenerationType.
IDENTITY)
@Column
(name=
"seq_venda", nullable=
false)
public Long getSequencial
() {
return sequencial
;
}
public void setSequencial
(Long sequencial
) {
this.
sequencial = sequencial
;
}
@ManyToOne
@ForeignKey
(name=
"fk_venda_loja")
@NotNull
(message =
"Informe a loja", groups =
{Cadastrar.
class, Alterar.
class})
@JoinColumn
(name =
"seq_loja", nullable =
false)
public Loja getLoja
() {
return loja
;
}
public void setLoja
(Loja loja
) {
this.
loja = loja
;
}
/*
@ManyToOne
@ForeignKey(name="fk_venda_cliente")
@JoinColumn(name = "seq_cliente", nullable = true)
public Cliente getCliente() {
return cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
}
*/
@ManyToOne
@ForeignKey
(name=
"fk_venda_usuario")
@JoinColumn
(name =
"seq_usuario", nullable =
false)
public Usuario getUsuario
() {
return usuario
;
}
public void setUsuario
(Usuario usuario
) {
this.
usuario = usuario
;
}
/*
@ManyToOne
@ForeignKey(name="fk_venda_vendedor")
@NotNull(message = "Informe o vendedor", groups = {Cadastrar.class, Alterar.class})
@JoinColumn(name = "seq_vendedor", nullable = true)
public Vendedor getVendedor() {
return vendedor;
}
public void setVendedor(Vendedor vendedor) {
this.vendedor = vendedor;
}
@ManyToOne
@ForeignKey(name="fk_venda_indicacao")
@JoinColumn(name = "seq_indicacao", nullable = true)
public Vendedor getIndicacao() {
return indicacao;
}
public void setIndicacao(Vendedor indicacao) {
this.indicacao = indicacao;
}
*/
@Column
(name=
"dth_venda")
public Date getDataVenda
() {
return dataVenda
;
}
public void setDataVenda
(Date dataVenda
) {
this.
dataVenda = dataVenda
;
}
@Column
(name=
"dsc_observacao")
@Size
(max =
1000, message =
"Limite de caracteres ultrapassado: Observação")
public String getObservacao
() {
return observacao
;
}
public void setObservacao
(String observacao
) {
this.
observacao = StringUtil.
setarUpperCaseComTrim(observacao
);
}
@Column
(name=
"tip_situacao")
public String getTipoSituacao
() {
return tipoSituacao
;
}
public void setTipoSituacao
(String tipoSituacao
) {
this.
tipoSituacao = tipoSituacao
;
}
/*
@Transient
public String getTipoSituacaoDescricao() {
return TipoSituacaoVenda.parse(getTipoSituacao()).getDescricao();
}
*/
@Column
(name=
"dsc_nota_fiscal")
@Size
(max =
120, message =
"Limite de caracteres ultrapassado: Nota Fiscal")
public String getNotaFiscal
() {
return notaFiscal
;
}
public void setNotaFiscal
(String notaFiscal
) {
this.
notaFiscal = notaFiscal
;
}
@ManyToOne
@ForeignKey
(name =
"fk_venda_maquineta")
@JoinColumn
(name=
"seq_maquineta", referencedColumnName=
"seq_maquineta", insertable=
true, updatable=
true)
public Maquineta getMaquineta
() {
return maquineta
;
}
public void setMaquineta
(Maquineta maquineta
) {
this.
maquineta = maquineta
;
}
/*@Column(name="tip_frete")
public String getTipoFrete() {
return tipoFrete;
}
public void setTipoFrete(String tipoFrete) {
this.tipoFrete = tipoFrete;
}
@Column(name="val_frete")
public Double getValorFrete() {
return valorFrete;
}
public void setValorFrete(Double valorFrete) {
this.valorFrete = valorFrete;
}*/
@OneToMany
(mappedBy=
"venda", cascade=CascadeType.
ALL, fetch=FetchType.
LAZY, orphanRemoval=
true)
public List<Lancamento
> getListaLancamentos
() {
return listaLancamentos
;
}
public void setListaLancamentos
(List<Lancamento
> listaLancamentos
) {
this.
listaLancamentos = listaLancamentos
;
}
@OneToMany
(mappedBy=
"venda", cascade=CascadeType.
ALL, fetch=FetchType.
LAZY, orphanRemoval=
true)
public List<VendaFormaPagamento
> getListaVendaFormaPagamentos
() {
return listaVendaFormaPagamentos
;
}
public void setListaVendaFormaPagamentos
(List<VendaFormaPagamento
> listaVendaFormaPagamentos
) {
this.
listaVendaFormaPagamentos = listaVendaFormaPagamentos
;
}
@Transient
public String getTipoSituacaoFinanceira
() {
return tipoSituacaoFinanceira
;
}
@Transient
public void setTipoSituacaoFinanceira
(String tipoSituacaoFinanceira
) {
this.
tipoSituacaoFinanceira = tipoSituacaoFinanceira
;
}
@Transient
public String getJustificativaParaExcluir
() {
return justificativaParaExcluir
;
}
public void setJustificativaParaExcluir
(String justificativaParaExcluir
) {
this.
justificativaParaExcluir = StringUtil.
setarUpperCaseComTrim(justificativaParaExcluir
);
}
@Transient
public Boolean getPermitirEmissaoNotaFiscal
() {
return permitirEmissaoNotaFiscal
;
}
public void setPermitirEmissaoNotaFiscal
(Boolean permitirEmissaoNotaFiscal
) {
this.
permitirEmissaoNotaFiscal = permitirEmissaoNotaFiscal
;
}
public void verificarSePermiteEmissaoNotaFiscal
() {
setPermitirEmissaoNotaFiscal
(this.
getLoja().
emissaoNotaFiscalPermitida());
}
@Transient
public Boolean getEmitirNotaFiscal
() {
return emitirNotaFiscal
;
}
public void setEmitirNotaFiscal
(Boolean emitirNotaFiscal
) {
this.
emitirNotaFiscal = emitirNotaFiscal
;
}
@Transient
public Boolean getEmitirComanda
() {
return emitirComanda
;
}
public void setEmitirComanda
(Boolean emitirComanda
) {
this.
emitirComanda = emitirComanda
;
}
@Transient
public Boolean getCpfCnpjNaNotaFiscal
() {
return cpfCnpjNaNotaFiscal
;
}
public void setCpfCnpjNaNotaFiscal
(Boolean cpfCnpjNaNotaFiscal
) {
this.
cpfCnpjNaNotaFiscal = cpfCnpjNaNotaFiscal
;
}
/****************************************************************/
@Transient
public List<Lancamento
> getLancamentos
() {
List<Lancamento
> lancamentos =
new ArrayList<Lancamento
>();
lancamentos.
addAll(listaLancamentos
);
return lancamentos
;
}
@Transient
public List<Lancamento
> getLancamentosValidos
() {
List<Lancamento
> lancamentos =
new ArrayList<Lancamento
>();
for (Lancamento lancamento : listaLancamentos
) {
if (lancamento.
getAtivo()) {
lancamentos.
add(lancamento
);
}
}
return lancamentos
;
}
@Transient
public List<VendaFormaPagamento
> getVendaFormaPagamentos
() {
List<VendaFormaPagamento
> pagamentos =
new ArrayList<VendaFormaPagamento
>();
pagamentos.
addAll(listaVendaFormaPagamentos
);
return pagamentos
;
}
/*
@Transient
public List<Parcela> getParcelas() {
List<Parcela> parcelas = new ArrayList<Parcela>();
parcelas.addAll(listaParcelas);
return parcelas;
}
*/
@Transient
public Double getTotalLancamentos
() {
Double total =
0.0;
for (Lancamento lancamento : getLancamentos
()) {
total = total + lancamento.
getValorVenda();
}
return total
;
}
@Transient
public Double getTotalVenda
() {
Double total =
new Double(0.0);
for (Lancamento lancamento : getLancamentos
()) {
if (lancamento.
getAtivo()) {
total = total + lancamento.
getValorVenda();
}
}
return total
;
}
@Transient
public Double getTotalPago
() {
Double total =
new Double(0.0);
for (VendaFormaPagamento pagamento : getVendaFormaPagamentos
()) {
total = total + pagamento.
getValorPagamento();
}
return total
;
}
@Transient
public Double getTotalPagoEmDinheiro
() {
Double total =
new Double(0.0);
for (VendaFormaPagamento pagamento : getVendaFormaPagamentos
()) {
if (pagamento.
getFormaPagamento().
formaPagamentoEhDinheiro()) {
total = total + pagamento.
getValorPagamento();
}
}
return total
;
}
@Transient
public Double getTotalPagoEmCupom
() {
Double total =
new Double(0.0);
for (VendaFormaPagamento pagamento : getVendaFormaPagamentos
()) {
if (pagamento.
getFormaPagamento().
formaPagamentoEhCupom()) {
total = total + pagamento.
getValorPagamento();
}
}
return total
;
}
/*
@Transient
public String getProdutosVendidos() {
StringBuilder produtosVendidos = new StringBuilder();
for (Lancamento lancamento : getLancamentos()) {
if (lancamento.getAtivo()) {
produtosVendidos.append("[");
produtosVendidos.append(lancamento.getProduto().getCodigoProdutoPadrao());
produtosVendidos.append(": ");
produtosVendidos.append(lancamento.getProduto().getDescricaoDoModelo());
produtosVendidos.append(", ");
produtosVendidos.append(lancamento.getProduto().getDescricao());
produtosVendidos.append(" POR R$ ");
produtosVendidos.append(StringUtil.formatarValor(lancamento.getValorVenda()));
produtosVendidos.append("] ");
}
}
return produtosVendidos.toString();
}
*/
@Transient
public Long getSequencialDaLoja
() {
return VerificadorUtil.
naoEstaNulo(loja
)? loja.
getSequencial() :
null;
}
/*
@Transient
public Long getSequencialDoVendedor() {
return VerificadorUtil.naoEstaNulo(vendedor)? vendedor.getSequencial() : null;
}
@Transient
public String getLinhaPorSituacao() {
if (getTipoSituacao().equals(TipoSituacaoVenda.NOVO.getValor())) return "linha-novo";
if (getTipoSituacao().equals(TipoSituacaoVenda.CONFERIDO.getValor())) return "linha-conferido";
if (getTipoSituacao().equals(TipoSituacaoVenda.FINALIZADO.getValor())) return "linha-finalizado";
if (getTipoSituacao().equals(TipoSituacaoVenda.PENDENCIAS.getValor())) return "linha-pendencias";
return "even-row";
}
@Transient
public String getNomeVendedorDaVenda() {
return VerificadorUtil.naoEstaNulo(getVendedor())? getVendedor().getNomeDaPessoa() : null;
}
*/
@Transient
public Integer getQuantidadeDiasDaVenda
() {
return DataUtils.
calcularDiferenceEmDiasEntreDuasDatas(DataUtils.
getDataAtual(), getDataVenda
());
}
@
Override
public int hashCode
() {
final int prime =
31;
int result =
1;
result = prime
* result +
((sequencial ==
null) ? 0 : sequencial.
hashCode());
return result
;
}
@
Override
public boolean equals
(Object obj
) {
if (this == obj
)
return true;
if (obj ==
null)
return false;
if (getClass
() != obj.
getClass())
return false;
Venda other =
(Venda
) obj
;
if (sequencial ==
null) {
if (other.
sequencial !=
null)
return false;
} else if (!sequencial.
equals(other.
sequencial))
return false;
return true;
}
@Transient
public Pessoa getPessoaJuridicaDaLoja
() {
return VerificadorUtil.
naoEstaNulo(getLoja
())? getLoja
().
getPessoaJuridica() :
null;
}
/*
@Transient
public String montarDadosDaVenda(List<Lancamento> lancamentos, List<VendaFormaPagamento> pagamentos) {
StringBuilder conteudo = new StringBuilder();
//conteudo.append("OPERAÇÃO REALIZADA POR: " + this.getUsuario().getNome() + "\n");
conteudo.append("VENDA ID: " + this.getSequencial() + "\n");
conteudo.append("DATA: " + DataUtils.converterDataComHorarioParaString(this.getDataVenda()) + "\n");
conteudo.append("LOJA: " + this.getLoja().getDescricao() + "\n");
conteudo.append("VENDEDOR: " + this.getVendedor().getNomeDaPessoa() + "\n");
conteudo.append("TOTAL: " + valorTotalVenda(lancamentos) + "\n");
conteudo.append("OBS.: " + this.getObservacao() + "\n");
if (VerificadorUtil.naoEstaNuloOuVazio(this.getJustificativaParaExcluir())) {
conteudo.append("JUSTIFICATIVA: " + this.getJustificativaParaExcluir() + "\n");
} else {
conteudo.append("SEM JUSTIFICATIVA\n");
}
conteudo.append("__________________________________________________\n\n");
for (Lancamento lancamento : lancamentos) {
conteudo.append(lancamento.getProduto().getCodigoProdutoPadrao() + " | ");
conteudo.append(lancamento.getProduto().getDescricaoComModeloCompleta() + ", ");
conteudo.append("R$" + lancamento.getValorVenda());
if (lancamento.comDescontos()) {
conteudo.append(" (COM DESCONTO DE R$" + lancamento.valorDescontos() + ")");
}
conteudo.append("\n");
}
conteudo.append("__________________________________________________\n\n");
for (VendaFormaPagamento pagamento : pagamentos) {
if (VerificadorUtil.naoEstaNulo(pagamento.getBandeiraCartao())) {
conteudo.append(pagamento.getFormaPagamento().getDescricao() + " - " + pagamento.getBandeiraCartao().getDescricao() + ": R$" + pagamento.getValorPagamento() + "\n");
} else {
conteudo.append(pagamento.getFormaPagamento().getDescricao() + ": R$" + pagamento.getValorPagamento() + "\n");
}
conteudo.append("\n");
}
return conteudo.toString();
}
*
@Transient
private Double valorTotalVenda(List<Lancamento> lancamentos) {
Double total = new Double(0.0);
for (Lancamento lancamento : lancamentos) {
if (lancamento.getAtivo()) {
total = total + lancamento.getValorVenda();
}
}
return total;
}
@Transient
public String getEmailDoCliente() {
return VerificadorUtil.naoEstaNulo(getCliente())? getCliente().getEmail() : "";
}
*/
@Transient
public String getCertificadoDaLoja
() {
return VerificadorUtil.
naoEstaNulo(getLoja
())? getLoja
().
getDescricaoCertificado() :
"";
}
@Transient
public String getDescricaoMaquineta
() {
return VerificadorUtil.
naoEstaNulo(getMaquineta
())? getMaquineta
().
descricaoCompleta() :
"NENHUMA";
}
}