0
I have a problem I can’t solve if you can help me thank you.
In short, it’s a prototype invoice registration with notafiscal items, I can enter the records through a dialog, the table shows that the records have been entered in the list, but the datatable shows the lines without the contents.
follows the files.
package br.com.beta.nfe.model;
import java.io.Serializable;
import java.math.BigDecimal;
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.MapsId;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
@Entity
@Table(name = "notaFiscal")
public class NotaFiscal implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Integer numero;
private Date dataEmissao;
private Emissor emissor;
private Cliente cliente;
private List<ItemNotaFiscal> itensNotaFiscal = new ArrayList<>();
@Column(columnDefinition = "double")
private BigDecimal vrTotalNotaFiscal;
private String observacao;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "notaFiscalGenerator")
@SequenceGenerator(name = "notaFiscalGenerator", sequenceName = "NOTAFISCAL_SEQ", allocationSize = 10)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getNumero() {
return numero;
}
public void setNumero(Integer numero) {
this.numero = numero;
}
@NotNull
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "data_emissao", nullable = false)
public Date getDataEmissao() {
return dataEmissao;
}
public void setDataEmissao(Date dataEmissao) {
this.dataEmissao = dataEmissao;
}
@NotNull
@ManyToOne
@JoinColumn(name = "emissor_id", nullable = false)
public Emissor getEmissor() {
return emissor;
}
public void setEmissor(Emissor emissor) {
this.emissor = emissor;
}
@NotNull
@ManyToOne
@JoinColumn(name = "cliente_id", nullable = false)
public Cliente getCliente() {
return cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
}
@OneToMany( cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
public List<ItemNotaFiscal> getItensNotaFiscal() {
for (ItemNotaFiscal inf : this.itensNotaFiscal) {
System.out
.println("listando os itens da table !" +inf.getId()+" "+ inf.getItem().getDescricao()+" quantidade : "+inf.getQuantidade()+"preco : "+inf.getValorUnitario());
}
return itensNotaFiscal;
}
public void setItensNotaFiscal(List<ItemNotaFiscal> itensNotaFiscal) {
this.itensNotaFiscal = itensNotaFiscal;
}
@NotNull
@Column(name = "valor_total_nota", nullable = false, precision = 10, scale = 2)
public BigDecimal getVrTotalNotaFiscal() {
return vrTotalNotaFiscal;
}
public void setVrTotalNotaFiscal(BigDecimal vrTotalNotaFiscal) {
this.vrTotalNotaFiscal = vrTotalNotaFiscal;
}
@Column(columnDefinition = "text")
public String getObservacao() {
return observacao;
}
public void setObservacao(String observacao) {
this.observacao = observacao;
}
public void recalcularValorTotal() {
BigDecimal total = BigDecimal.ZERO;
for (ItemNotaFiscal itemNotaFiscal : this.getItensNotaFiscal()) {
if (itemNotaFiscal.getItem() != null && itemNotaFiscal.getItem().getId() != null) {
total = total.add(itemNotaFiscal.getTotalItem());
}
}
this.setVrTotalNotaFiscal(total);
}
public void adicionarItemNotaFiscal(ItemNotaFiscal itemNotaFiscal) {
this.getItensNotaFiscal().add(itemNotaFiscal);
}
@Transient
public BigDecimal getValorSubtotal() {
//return this.getValorTotal().subtract(this.getValorFrete()).add(this.getValorDesconto());
return this.getVrTotalNotaFiscal();
}
@Transient
public boolean isNovo() {
return getId() == null;
}
@Transient
public boolean isExistente() {
return !isNovo();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.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;
NotaFiscal other = (NotaFiscal) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
The bean :
package br.com.beta.nfe.controller;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.faces.event.ActionEvent;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.SelectEvent;
import br.com.beta.nfe.model.Cliente;
import br.com.beta.nfe.model.Item;
import br.com.beta.nfe.model.ItemNotaFiscal;
import br.com.beta.nfe.model.NotaFiscal;
import br.com.beta.nfe.repository.Clientes;
import br.com.beta.nfe.repository.Itens;
import br.com.beta.nfe.service.CadastroNotaFiscalService;
import br.com.beta.nfe.service.NegocioException;
import br.com.beta.nfe.util.jsf.FacesUtil;
@Named
@ViewScoped
public class CadastroNotaFiscalBean implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private Clientes clientes;
@Inject
private Itens itens;
@Inject
private Item item;
@Inject
private CadastroNotaFiscalService cadastroNotaFiscalService;
@Inject NotaFiscal notaFiscal;
private String codigoSKU;
private BigDecimal valorUnitario;
private Double quantidade;
private Item itemLinhaEditavel;
public CadastroNotaFiscalBean() {
limpar();
}
public void inicializar() {
if (FacesUtil.isNotPostback()) {
if (this.notaFiscal == null) {
limpar();
// this.notaFiscal.adicionarItemVazio();
// this.recalcularNotaFiscal();
}
// this.notaFiscal.adicionarItemVazio();
this.recalcularNotaFiscal();
}
}
public void handleSelect(SelectEvent event) {
item = (Item) event.getObject();
System.out.println("selecionou o item - > " + item.getDescricao());
this.valorUnitario = item.getVrUnitario();
System.out.println("o Item " + item.getDescricao()
+ " tem o preço unitario de -> " + item.getVrUnitario());
}
public void onItemSelect(Item selecionado) {
item = selecionado;
}
public void insereItenNotaFiscal(ActionEvent actionEvent){
ItemNotaFiscal itemNotaFiscal = new ItemNotaFiscal();
itemNotaFiscal.setItem(item);
itemNotaFiscal.setValorUnitario(item.getVrUnitario());
itemNotaFiscal.setNotaFiscal(this.notaFiscal);
itemNotaFiscal.setQuantidade(this.quantidade);
Long ordem = (long) (this.notaFiscal.getItensNotaFiscal().size()+1);
itemNotaFiscal.setId(ordem);
this.notaFiscal.adicionarItemNotaFiscal(itemNotaFiscal);
for (ItemNotaFiscal inf : this.notaFiscal.getItensNotaFiscal()) {
System.out
.println("inseri o item !" + inf.getId()+" "+inf.getItem().getCodigoSKU()+" "+inf.getItem().getDescricao()+" "+inf.getValorUnitario()+" "+inf.getQuantidade());
}
}
private void limpar() {
notaFiscal = new NotaFiscal();
}
public void salvar() {
try {
this.notaFiscal = cadastroNotaFiscalService.salvar(this.notaFiscal);
limpar();
FacesUtil.addInfoMessage("Produto salvo com sucesso!");
} catch (NegocioException ne) {
FacesUtil.addErrorMessage(ne.getMessage());
}
}
public void recalcularNotaFiscal() {
if (this.notaFiscal != null) {
this.notaFiscal.recalcularValorTotal();
}
}
public void carregarItemPorCodigoSKU() {
if (StringUtils.isNotEmpty(this.codigoSKU)) {
this.itemLinhaEditavel = this.itens.porCodigoSKU(this.codigoSKU);
this.carregarItemLinhaEditavel();
}
}
public void carregarItemLinhaEditavel() {
ItemNotaFiscal itemNotaFiscal = this.notaFiscal.getItensNotaFiscal()
.get(0);
if (this.itemLinhaEditavel != null) {
if (this.existeItemNotaFiscalComItem(this.itemLinhaEditavel)) {
FacesUtil
.addErrorMessage("Já existe um item na Nota Fiscal com o item informado.");
} else {
itemNotaFiscal.setItem(this.itemLinhaEditavel);
itemNotaFiscal.setValorUnitario(this.itemLinhaEditavel
.getVrUnitario());
// this.notaFiscal.adicionarItemVazio();
this.itemLinhaEditavel = null;
this.codigoSKU = null;
this.notaFiscal.recalcularValorTotal();
}
}
}
private boolean existeItemNotaFiscalComItem(Item item) {
boolean existeItemNotaFiscal = false;
for (ItemNotaFiscal itemNotaFiscal : this.getNotaFiscal()
.getItensNotaFiscal()) {
if (item.equals(itemNotaFiscal.getItem())) {
existeItemNotaFiscal = true;
break;
}
}
return existeItemNotaFiscal;
}
public List<Item> completarItem(String descricao) {
return this.itens.porDescricao(descricao);
}
public List<Cliente> completarCliente(String nome) {
return this.clientes.porNome(nome);
}
public NotaFiscal getNotaFiscal() {
return notaFiscal;
}
public void setNotaFiscal(NotaFiscal notafiscal) {
this.notaFiscal = notaFiscal;
}
public boolean isEditando() {
return this.notaFiscal.getId() != null;
}
public Item getItemLinhaEditavel() {
return itemLinhaEditavel;
}
public void setItemLinhaEditavel(Item itemLinhaEditavel) {
this.itemLinhaEditavel = itemLinhaEditavel;
}
public String getCodigoSKU() {
return codigoSKU;
}
public void setCodigoSKU(String codigoSKU) {
this.codigoSKU = codigoSKU;
}
public Double getQuantidade() {
return quantidade;
}
public void setQuantidade(Double quantidade) {
this.quantidade = quantidade;
}
public BigDecimal getValorUnitario() {
return valorUnitario;
}
public void setValorUnitario(BigDecimal valorUnitario) {
this.valorUnitario = valorUnitario;
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
}
xhtml displaying the table :
<?xml version="1.0" encoding="UTF-8" ?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<p:commandButton value="Incluir Itens" type="button"
onclick="PF('insereItem').show();" />
<p:dataTable id="tabelaItens"
value="#{cadastroNotaFiscalBean.notaFiscal.itensNotaFiscal}"
var="tabelaItens" style="margin-top: 20px"
emptyMessage="Nenhum item encontrado." rows="20">
<p:column headerText="Ordem">
<h:outputText value="#{itemNotaFiscal.id}" />
</p:column>
<p:column headerText="Item">
<h:outputText value="#{itemNotaFiscal.item.descricao}" />
</p:column>
<p:column headerText="Valor unitario">
<h:outputText value="#{itemNotaFiscal.valorUnitario}">
<f:convertNumber type="currency" />
</h:outputText>
</p:column>
<p:column headerText="Quantidade">
<h:outputText size="8" value="#{itemNotaFiscal.quantidade}" />
</p:column>
</p:dataTable>
<p:dialog id="insereItem" header="Inclusão de Itens"
widgetVar="insereItem" modal="true" height="300" width="500">
<h:outputText value="Inclusão de Item" />
<p:panelGrid columns="2" id="painel"
style="width: 100%; margin-top: 20px" columnClasses="rotulo, campo">
<p:outputLabel value="Item" for="item" />
<p:autoComplete id="item" size="40" dropdown="true"
value="#{cadastroNotaFiscalBean.item}"
completeMethod="#{cadastroNotaFiscalBean.completarItem}" var="item"
itemLabel="#{item.descricao}" itemValue="#{item}"
forceSelection="true">
<p:ajax event="itemSelect"
listener="#{cadastroNotaFiscalBean.handleSelect}"
update="valorUnitario" render="valorUnitario" />
<p:ajax event="change"
listener="#{cadastroNotaFiscalBean.handleSelect}"
update="valorUnitario" render="valorUnitario" />
</p:autoComplete>
<p:outputLabel value="Valor Unitario" for="valorUnitario" />
<p:outputLabel autoUpdate="true" id="valorUnitario" size="15"
value="#{cadastroNotaFiscalBean.valorUnitario}" />
<p:outputLabel value="Quantidade" for="quantidade" />
<p:inputText id="quantidade" size="8"
value="#{cadastroNotaFiscalBean.quantidade}" />
</p:panelGrid>
<p:commandButton update="tabelaItens"
oncomplete="PF('insereItem').hide();"
actionListener="#{cadastroNotaFiscalBean.insereItenNotaFiscal}"
value="Inserir Item">
</p:commandButton>
</p:dialog>
</ui:composition>
I put the screenshots of the dialog screen and the table, if you observe it shows the line but empty. And the list is loaded correctly with the items, I tested the content with a for and this ok.
If you can shed some light, I’d appreciate it.
One thing I found strange is:
var="tabelaItens"
, but inside the datatable you use only the#{itemNotaFiscal}
.– Rodrigo