Ajuda para Salvar Produtos

Pessoal, bom dia…criei um sistema simples de vendas:

clico em novo para realizar a venda:

aponto no ComboBox o cliente, e seus dados são preenchidos

clico em “+ Item” para adicionar os produtos(quantos forem necessários)

clico em formas de pagamento e seleciono como o cliente ira pagar:

por fim clico em salvar para finalizar a venda:

até aqui perfeito…o problema é que cometi 2 erros e não sei como conserta-los, ambos ocorrem na mesma situação, vejam:

primeiro erro: Imagine que vc queria alterar/visualizar a venda que foi feita nas imagens acima, para isso clico em Alterar:

1º erro: o Label do ComboBox some toda vez que vou alterar a venda e ele está corretamente declarado no código

2º erro: os produtos não foram salvos( neste caso realmente foi falha minha, pois não estou salvando eles em nenhum lugar)

Agora minha duvida é, com o 2º erro:

Na classe entity devo criar uma

  • private List itensVenda;
  • private String itensVenda;
  • private List itensVenda = new ArrayList<>();

Qual destes?

E com o 1º erro: não tenho a menor ideia do que pode estar acontecendo

Código

View de Vendas:

package br.com.fjsistemas.cadastros.view;

import java.text.NumberFormat;
import java.text.ParseException;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.vaadin.textfieldformatter.CustomStringBlockFormatter;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.datepicker.DatePicker;
import com.vaadin.flow.component.dialog.Dialog;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.grid.GridVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.NumberField;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.data.binder.PropertyId;
import com.vaadin.flow.data.renderer.LocalDateRenderer;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;

import br.com.fjsistemas.backend.Cliente;
import br.com.fjsistemas.backend.Produto;
import br.com.fjsistemas.backend.Venda;
import br.com.fjsistemas.main.MainView;
import br.com.fjsistemas.repository.ClienteRepository;
import br.com.fjsistemas.repository.ProdutoRepository;
import br.com.fjsistemas.service.VendaService;

@Route(value = "venda-view", layout = MainView.class)
@PageTitle("Lançamento de Vendas")
public class VendaView extends VerticalLayout {

	private static final long serialVersionUID = 1L;

	private HorizontalLayout hltVenda = new HorizontalLayout();
	Grid<Venda> grdVenda = new Grid<>(Venda.class, false);

	private HorizontalLayout hltBarraBotoes = new HorizontalLayout();
	Button btnNovo = new Button("Novo");
	Button btnAlterar = new Button("Alterar");
	Button btnExcluir = new Button("Excluir");

	private Dialog dlgJanela = new Dialog();

	Div superior = new Div();
	Div centro = new Div();
	Div inferior = new Div();

	HorizontalLayout primeiraLinhaDivSuperior = new HorizontalLayout();
	HorizontalLayout segundaLinhaDivSuperior = new HorizontalLayout();
	HorizontalLayout adicionarProdutos = new HorizontalLayout();

	double somaValores;

	@PropertyId("dataVenda")
	private DatePicker txtDataVenda = new DatePicker("Data Venda");

	@PropertyId("cliente")
	private ComboBox<Cliente> txtNomeCliente = new ComboBox<>("Cliente");

	@PropertyId("telefone")
	private TextField txtTelefone = new TextField("Telefone");

	@PropertyId("celular")
	private TextField txtCelular = new TextField("Celular");

	@PropertyId("endereco")
	private TextField txtEndereco = new TextField("Endereço");

	@PropertyId("numero")
	private TextField txtNumero = new TextField("Nº");

	@PropertyId("bairro")
	private TextField txtBairro = new TextField("Bairro");

	@PropertyId("cidade")
	TextField txtCidade = new TextField("Cidade");

	@PropertyId("estado")
	TextField txtEstado = new TextField("Estado");

	@PropertyId("formasPagamento")
	private ComboBox<String> formasPagamento = new ComboBox<>("Formas de Pagamento");

	@PropertyId("valorTotalVenda")
	private TextField campoSomaValores = new TextField();

	private HorizontalLayout htlDlgBarraBotoes = new HorizontalLayout();
	private Button btnSalvar = new Button("Salvar");
	private Button btnFechar = new Button("Fechar");
	private Button btnAdicionarItem = new Button("+ Item");

	@Autowired
	VendaService vendaService;

	@Autowired
	ClienteRepository clienteRepository;

	@Autowired
	ProdutoRepository produtoRepository;

	private List<Venda> listaVendas;
	private List<TextField> valores = new ArrayList<>();
	private Venda venda;

	Binder<Venda> binderVenda = new Binder<>(Venda.class);

	public VendaView() {

	}

	@PostConstruct
	public void init() {
		configuraTela();

	}

	private void configuraTela() {

		setMargin(false);
		setPadding(false);

		configuraHltVenda();
		configuraFltBarraBotoes();
		configuraDlgJanela();
		populaGrdVenda();
		configuraBinder();

		add(hltVenda, hltBarraBotoes);
	}

	private void configuraFltBarraBotoes() {

		btnNovo.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnNovo.addClickListener(e -> {
			novoClick();
		});

		btnAlterar.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnAlterar.addClickListener(e -> {
			alterarClick();
		});

		btnExcluir.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnExcluir.addClickListener(e -> {
			excluirClick();
		});

		hltBarraBotoes.add(btnNovo, btnAlterar, btnExcluir);
	}

	private void excluirClick() {

		if (venda != null) {
			listaVendas.remove(venda);
			vendaService.delete(venda);
			atualizaGrdVenda();
		}
	}

	private void configuraHltVenda() {
		hltVenda.setWidthFull();
		configuraGrdVenda();
		hltVenda.add(grdVenda);
	}

	private void configuraGrdVenda() {
		grdVenda.setHeight("820px");
		grdVenda.setWidthFull();

		grdVenda.addColumn(Venda::getId).setHeader("ID:").setAutoWidth(true);

		grdVenda.addColumn(new LocalDateRenderer<>(Venda::getDataVenda, DateTimeFormatter.ofPattern("dd/MM/yyy")))
				.setHeader("Data Venda").setAutoWidth(true);

		grdVenda.addColumn(venda -> venda.getCliente().getNome()).setHeader("Nome:").setAutoWidth(true)
				.setKey("cliente.nome");

		grdVenda.addColumn(Venda::getValorTotalVenda).setHeader("Valor Total:").setAutoWidth(true)
				.setKey("valorTotalVenda");

		grdVenda.addThemeVariants(GridVariant.LUMO_COMPACT, GridVariant.LUMO_COLUMN_BORDERS);

		grdVenda.getColumns().forEach(col -> col.setAutoWidth(true).setSortable(true).setResizable(true));

		grdVenda.addItemClickListener(e -> {
			venda = e.getItem();
		});

	}

	private void configuraDlgJanela() {

		dlgJanela.setHeightFull();
		dlgJanela.setWidthFull();
		dlgJanela.setCloseOnEsc(false);
		dlgJanela.setCloseOnOutsideClick(false);

		superior.setHeight("170px");
		superior.setWidthFull();

		txtNomeCliente.setWidth("350px");
		txtNomeCliente.setLabel("Nome Cliente");

		txtNomeCliente.setItemLabelGenerator(cliente -> {
			if (cliente == null || cliente.getNome() == null) {
				return " ";

			} else {
				return cliente.getNome();
			}
		});

		List<Cliente> listaDeClientes = clienteRepository.findAll();

		txtNomeCliente.setItems(listaDeClientes);
		txtNomeCliente.addValueChangeListener(event -> {
			if (event.getValue() == null || event.getValue().getFone() == null) {
				txtTelefone.setValue(" ");
			} else {
				txtTelefone.setValue(event.getValue().getFone());
			}
			if (event.getValue() == null || event.getValue().getCelular() == null) {
				txtCelular.setValue(" ");
			} else {
				txtCelular.setValue(event.getValue().getCelular());
			}
			if (event.getValue() == null || event.getValue().getEndereco() == null) {
				txtEndereco.setValue(" ");
			} else {
				txtEndereco.setValue(event.getValue().getEndereco());
			}
			if (event.getValue() == null || event.getValue().getNumero() == null) {
				txtNumero.setValue(" ");
			} else {
				txtNumero.setValue(event.getValue().getNumero());
			}
			if (event.getValue() == null || event.getValue().getBairro() == null) {
				txtBairro.setValue(" ");
			} else {
				txtBairro.setValue(event.getValue().getBairro());
			}
			if (event.getValue() == null || event.getValue().getCidade() == null) {
				txtCidade.setValue(" ");
			} else {
				txtCidade.setValue(event.getValue().getCidade());
			}
			if (event.getValue() == null || event.getValue().getEstado() == null) {
				txtEstado.setValue(" ");
			} else {
				txtEstado.setValue(event.getValue().getEstado());
			}

		});

		new CustomStringBlockFormatter.Builder().blocks(0, 2, 4, 4).delimiters("(", ")", "-").numeric().build()
				.extend(txtTelefone);

		new CustomStringBlockFormatter.Builder().blocks(0, 2, 5, 4).delimiters("(", ")", "-").numeric().build()
				.extend(txtCelular);

		primeiraLinhaDivSuperior.add(txtDataVenda, txtNomeCliente, txtTelefone, txtCelular, txtEndereco, txtNumero,
				txtBairro, txtCidade);

		txtNumero.setWidth("140px");
		formasPagamento.setLabel("Formas de Pagamento");
		formasPagamento.setItems("Boleto", "Cartão de Crédito", "Cartão Débito", "Cheque", "Dinheiro", "Pix",
				"TED / DOC");
		
		segundaLinhaDivSuperior.add(txtEstado, formasPagamento);
		superior.add(primeiraLinhaDivSuperior, segundaLinhaDivSuperior);

		centro.setHeight("660px");
		centro.getStyle().set("border-style", "ridge");
		centro.getStyle().set("overflow-y", "scroll");
		centro.setWidthFull();

		btnSalvar.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnSalvar.getStyle().set("margin-top", "0em");
		btnSalvar.getStyle().set("margin-left", "1em");
		btnSalvar.addClickListener(e -> {
			salvarClick();
			limparCampos();
			formasPagamento.setLabel("");
		});

		btnFechar.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnFechar.getStyle().set("margin-top", "0em");
		btnFechar.addClickListener(e -> {
			dlgJanela.close();
			limparCampos();
			formasPagamento.setLabel("");
		});

		btnAdicionarItem.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnAdicionarItem.getStyle().set("margin-top", "0em");
		btnAdicionarItem.addClickListener(e -> {
			adicionaProduto();
			
		});

		Label text = new Label("Valor Total");
		text.getElement().getStyle().set("fontWeight", "bold");
		text.getStyle().set("margin-top", "0.8em");
		text.getStyle().set("margin-left", "2em");
		text.getStyle().set("text-align", "center");
		campoSomaValores.getStyle().set("margin-top", "0em");
		campoSomaValores.getStyle().set("margin-right", "0.2em");
		campoSomaValores.setWidth("30em");

		htlDlgBarraBotoes.add(btnSalvar, btnFechar, btnAdicionarItem, text, campoSomaValores);

		inferior.getStyle().set("margin-top", "0px");
		inferior.setHeight("45px");
		inferior.setWidthFull();
		inferior.add(htlDlgBarraBotoes);

		dlgJanela.add(superior, centro, inferior);

	}

	private void salvarClick() {

		venda = binderVenda.getBean();

		boolean adicionarLista = venda.getId() == null ? true : false;

		vendaService.create(venda);

		if (adicionarLista) {
			listaVendas.add(venda);
		}
		atualizaGrdVenda();
		novaVenda();
		txtNomeCliente.focus();

		binderVenda.setBean(venda);

		if (adicionarLista) {
			dlgJanela.close();
		}
	}

	private void populaGrdVenda() {

		listaVendas = vendaService.read();
		atualizaGrdVenda();
	}

	private void atualizaGrdVenda() {
		grdVenda.setItems(listaVendas);
	}

	private void configuraBinder() {

		binderVenda.bindInstanceFields(this);

	}

	private void novoClick() {

		novaVenda();
		binderVenda.setBean(venda);

		dlgJanela.open();
		txtNomeCliente.focus();
	}

	private void alterarClick() {

		if (venda != null) {
			binderVenda.setBean(venda);

			dlgJanela.open();

		}
	}

	private void adicionaProduto() {

		ComboBox<Produto> txtProdutos = new ComboBox<>();

		NumberField txtQuantidade = new NumberField("Quantidade");

		TextField txtValorUnitario = new TextField("Valor Unitário");

		TextField txtValorTotalItem = new TextField("Valor Total Item");

		txtProdutos.setWidth("370px");
		txtProdutos.setLabel("Produtos");
		List<Produto> listaDeProdutos = produtoRepository.findAll();
		txtProdutos.setItemLabelGenerator(Produto::getNome);
		txtProdutos.setItems(listaDeProdutos);
		txtProdutos.addValueChangeListener(event -> {

			NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("pt", "BR"));
			try {

				txtValorUnitario.setValue(formatter.format(event.getValue().getValor()));

			} catch (Exception e) {
				e.printStackTrace();
			}

		});

//==========================================================================================================================

		txtQuantidade.setHasControls(true);
		txtQuantidade.setValue(null);
		txtQuantidade.setMin(1);

		txtQuantidade.addValueChangeListener(event -> {

			NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("pt", "BR"));
			double valorTotal = 0;

			try {
				valorTotal = formatter.parse(txtValorUnitario.getValue()).doubleValue() * txtQuantidade.getValue();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			txtValorTotalItem.setValue(formatter.format(valorTotal));

			double soma = 0;
			for (TextField tf : valores) {
				try {
					soma += formatter.parse(tf.getValue()).doubleValue();

				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			campoSomaValores.setValue(formatter.format(soma));

		});

		adicionarProdutos = new HorizontalLayout();
		adicionarProdutos.add(txtProdutos, txtQuantidade, txtValorUnitario, txtValorTotalItem);

		centro.add(adicionarProdutos);
		valores.add(txtValorTotalItem);
	}

	private void limparCampos() {
		centro.removeAll();
	}

	private void novaVenda() {
		venda = new Venda();
		venda.setCliente(null);
		dlgJanela.close();

	}
}

Classe Entity Venda

package br.com.fjsistemas.backend;

import java.time.LocalDate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
public class Venda {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	private LocalDate dataVenda = LocalDate.now();

	@ManyToOne
	private Cliente cliente;

	private String valorTotalVenda;
}

O que seria essa classe entity?

1 curtida

@Lucas_Camara esta aqui é a classe entity:

package br.com.fjsistemas.backend;

import java.time.LocalDate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
public class Venda {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	private LocalDate dataVenda = LocalDate.now();

	@ManyToOne
	private Cliente cliente;

	private String formasDePagamento;

	private String valorTotalVenda;
}

Para o erro 2, realmente vc deve salvar os itens da venda. Eu colocaria a lista de itens na Venda dessa forma:

1 curtida

certo @Lucas_Camara…não querendo abusar da sua boa vontade, mas ja abusando…como faço para salvar os produtos nesta lista?

Tenho a classe VendaView, ela é UI para vendas, nela tenho o metodo que adiciona os produtos, este metodo:

private void adicionaProduto() {

		ComboBox<Produto> txtProdutos = new ComboBox<>();

		NumberField txtQuantidade = new NumberField("Quantidade");

		TextField txtValorUnitario = new TextField("Valor Unitário");

		TextField txtValorTotalItem = new TextField("Valor Total Item");

		txtProdutos.setWidth("370px");
		txtProdutos.setLabel("Produtos");
		List<Produto> listaDeProdutos = produtoRepository.findAll();
		txtProdutos.setItemLabelGenerator(Produto::getNome);
		txtProdutos.setItems(listaDeProdutos);
		txtProdutos.addValueChangeListener(event -> {

			NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("pt", "BR"));
			try {

				txtValorUnitario.setValue(formatter.format(event.getValue().getValor()));

			} catch (Exception e) {
				e.printStackTrace();
			}

		});

//==========================================================================================================================

		txtQuantidade.setHasControls(true);
		txtQuantidade.setValue(null);
		txtQuantidade.setMin(1);

		txtQuantidade.addValueChangeListener(event -> {

			NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("pt", "BR"));
			double valorTotal = 0;

			try {
				valorTotal = formatter.parse(txtValorUnitario.getValue()).doubleValue() * txtQuantidade.getValue();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			txtValorTotalItem.setValue(formatter.format(valorTotal));

			double soma = 0;
			for (TextField tf : valores) {
				try {
					soma += formatter.parse(tf.getValue()).doubleValue();

				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			campoSomaValores.setValue(formatter.format(soma));

		});

		adicionarProdutos = new HorizontalLayout();
		adicionarProdutos.add(txtProdutos, txtQuantidade, txtValorUnitario, txtValorTotalItem);

		centro.add(adicionarProdutos);
		valores.add(txtValorTotalItem);
	}

Como faço para salvar os produtos na lista criada?

Imagino que, em algum canto, vc está usando uma instancia da classe Venda, algo assim:

// Caso seja uma nova venda
Venda venda = new Venda();

// Caso seja uma venda recuperada do banco para edição
Venda venda = // recupera a venda do banco de dados

Com essa referência da venda, tu pode ter um método que faz algo assim:

ItemVenda novoItem = // monta o item com base nos campos da tela
venda.addItem(novoItem);

E dentro da Venda seria algo assim:

public class Venda {
	
	// outras propriedades da venda
	private List<ItemVenda> itens = new ArrayList<>();
	
	public void addItem(ItemVenda item) {
		itens.add(item);
	}
}
1 curtida

@Lucas_Camara

Esta é a classe completa de VendaView…

package br.com.fjsistemas.compraVenda;

import java.text.NumberFormat;
import java.text.ParseException;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.vaadin.textfieldformatter.CustomStringBlockFormatter;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.datepicker.DatePicker;
import com.vaadin.flow.component.dialog.Dialog;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.grid.GridVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.NumberField;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.data.binder.PropertyId;
import com.vaadin.flow.data.renderer.LocalDateRenderer;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;

import br.com.fjsistemas.backend.Cliente;
import br.com.fjsistemas.backend.Produto;
import br.com.fjsistemas.backend.Venda;
import br.com.fjsistemas.main.MainView;
import br.com.fjsistemas.repository.ClienteRepository;
import br.com.fjsistemas.repository.ProdutoRepository;
import br.com.fjsistemas.service.VendaService;

@Route(value = "venda-view", layout = MainView.class)
@PageTitle("Lançamento de Vendas")
public class VendaView extends VerticalLayout {

	private static final long serialVersionUID = 1L;

	private HorizontalLayout hltVenda = new HorizontalLayout();
	Grid<Venda> grdVenda = new Grid<>(Venda.class, false);

	private HorizontalLayout hltBarraBotoes = new HorizontalLayout();
	Button btnNovo = new Button("Novo");
	Button btnAlterar = new Button("Alterar");
	Button btnExcluir = new Button("Excluir");

	private Dialog dlgJanela = new Dialog();

	Div superior = new Div();
	Div centro = new Div();
	Div inferior = new Div();

	HorizontalLayout primeiraLinhaDivSuperior = new HorizontalLayout();
	HorizontalLayout segundaLinhaDivSuperior = new HorizontalLayout();
	HorizontalLayout adicionarProdutos = new HorizontalLayout();

	double somaValores;

	@PropertyId("dataVenda")
	private DatePicker txtDataVenda = new DatePicker("Data Venda");

	@PropertyId("cliente")
	private ComboBox<Cliente> txtNomeCliente = new ComboBox<>("Cliente");

	@PropertyId("telefone")
	private TextField txtTelefone = new TextField("Telefone");

	@PropertyId("celular")
	private TextField txtCelular = new TextField("Celular");

	@PropertyId("endereco")
	private TextField txtEndereco = new TextField("Endereço");

	@PropertyId("numero")
	private TextField txtNumero = new TextField("Nº");

	@PropertyId("bairro")
	private TextField txtBairro = new TextField("Bairro");

	@PropertyId("cidade")
	TextField txtCidade = new TextField("Cidade");

	@PropertyId("estado")
	TextField txtEstado = new TextField("Estado");

	@PropertyId("formasPagamento")
	private ComboBox<String> formasPagamento = new ComboBox<>("Formas de Pagamento");

	@PropertyId("parcelas")
	private ComboBox<String> qtdParcelas = new ComboBox<>("Nº Parcelas");

	@PropertyId("valorTotalVenda")
	private TextField campoSomaValores = new TextField();

	private HorizontalLayout htlDlgBarraBotoes = new HorizontalLayout();
	private Button btnSalvar = new Button("Salvar");
	private Button btnFechar = new Button("Fechar");
	private Button btnAdicionarItem = new Button("+ Item");

	@Autowired
	VendaService vendaService;

	@Autowired
	ClienteRepository clienteRepository;

	@Autowired
	ProdutoRepository produtoRepository;

	private List<Venda> listaVendas;
	private List<TextField> valores = new ArrayList<>();
	private Venda venda;

	Binder<Venda> binderVenda = new Binder<>(Venda.class);

	public VendaView() {

	}

	@PostConstruct
	public void init() {
		configuraTela();

	}

	private void configuraTela() {

		setMargin(false);
		setPadding(false);

		configuraHltVenda();
		configuraFltBarraBotoes();
		configuraDlgJanela();
		populaGrdVenda();
		configuraBinder();

		add(hltVenda, hltBarraBotoes);
	}

	private void configuraFltBarraBotoes() {

		btnNovo.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnNovo.addClickListener(e -> {
			novoClick();
		});

		btnAlterar.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnAlterar.addClickListener(e -> {
			alterarClick();
		});

		btnExcluir.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnExcluir.addClickListener(e -> {
			excluirClick();
		});

		hltBarraBotoes.add(btnNovo, btnAlterar, btnExcluir);
	}

	private void excluirClick() {

		if (venda != null) {
			listaVendas.remove(venda);
			vendaService.delete(venda);
			atualizaGrdVenda();
		}
	}

	private void configuraHltVenda() {
		hltVenda.setWidthFull();
		configuraGrdVenda();
		hltVenda.add(grdVenda);
	}

	private void configuraGrdVenda() {
		grdVenda.setHeight("820px");
		grdVenda.setWidthFull();

		grdVenda.addColumn(Venda::getId).setHeader("ID:").setAutoWidth(true);

		grdVenda.addColumn(new LocalDateRenderer<>(Venda::getDataVenda, DateTimeFormatter.ofPattern("dd/MM/yyy")))
				.setHeader("Data Venda").setAutoWidth(true);

		grdVenda.addColumn(venda -> venda.getCliente().getNome()).setHeader("Nome:").setAutoWidth(true)
				.setKey("cliente.nome");

		grdVenda.addColumn(Venda::getValorTotalVenda).setHeader("Valor Total:").setAutoWidth(true)
				.setKey("valorTotalVenda");

		grdVenda.addThemeVariants(GridVariant.LUMO_COMPACT, GridVariant.LUMO_COLUMN_BORDERS);

		grdVenda.getColumns().forEach(col -> col.setAutoWidth(true).setSortable(true).setResizable(true));

		grdVenda.addItemClickListener(e -> {
			venda = e.getItem();
		});

	}

	private void configuraDlgJanela() {

		dlgJanela.setHeightFull();
		dlgJanela.setWidthFull();
		dlgJanela.setCloseOnEsc(false);
		dlgJanela.setCloseOnOutsideClick(false);

		superior.setHeight("170px");
		superior.setWidthFull();

		txtNomeCliente.setWidth("350px");
		txtNomeCliente.setLabel("Nome Cliente");

		txtNomeCliente.setItemLabelGenerator(cliente -> {
			if (cliente == null || cliente.getNome() == null) {
				return " ";

			} else {
				return cliente.getNome();
			}
		});

		List<Cliente> listaDeClientes = clienteRepository.findAll();

		txtNomeCliente.setItems(listaDeClientes);
		txtNomeCliente.addValueChangeListener(event -> {
			if (event.getValue() == null || event.getValue().getFone() == null) {
				txtTelefone.setValue(" ");
			} else {
				txtTelefone.setValue(event.getValue().getFone());
			}
			if (event.getValue() == null || event.getValue().getCelular() == null) {
				txtCelular.setValue(" ");
			} else {
				txtCelular.setValue(event.getValue().getCelular());
			}
			if (event.getValue() == null || event.getValue().getEndereco() == null) {
				txtEndereco.setValue(" ");
			} else {
				txtEndereco.setValue(event.getValue().getEndereco());
			}
			if (event.getValue() == null || event.getValue().getNumero() == null) {
				txtNumero.setValue(" ");
			} else {
				txtNumero.setValue(event.getValue().getNumero());
			}
			if (event.getValue() == null || event.getValue().getBairro() == null) {
				txtBairro.setValue(" ");
			} else {
				txtBairro.setValue(event.getValue().getBairro());
			}
			if (event.getValue() == null || event.getValue().getCidade() == null) {
				txtCidade.setValue(" ");
			} else {
				txtCidade.setValue(event.getValue().getCidade());
			}
			if (event.getValue() == null || event.getValue().getEstado() == null) {
				txtEstado.setValue(" ");
			} else {
				txtEstado.setValue(event.getValue().getEstado());
			}
		});

		new CustomStringBlockFormatter.Builder().blocks(0, 2, 4, 4).delimiters("(", ")", "-").numeric().build()
				.extend(txtTelefone);

		new CustomStringBlockFormatter.Builder().blocks(0, 2, 5, 4).delimiters("(", ")", "-").numeric().build()
				.extend(txtCelular);

		primeiraLinhaDivSuperior.add(txtDataVenda, txtNomeCliente, txtTelefone, txtCelular, txtEndereco, txtNumero,
				txtBairro, txtCidade);

		txtNumero.setWidth("140px");
		formasPagamento.setItems("Boleto", "Cartão de Crédito", "Cartão Débito", "Cheque", "Dinheiro", "Pix",
				"TED / DOC");
		formasPagamento.addValueChangeListener(event -> {
			if (event.getValue() == "Cartão de Crédito") {
				qtdParcelas.setItems("A Vista", "2x", "3x", "4x", "5x", "6x", "7x", "8x", "9x", "10x", "11x", "12x");
				qtdParcelas.setWidth("141px");
				segundaLinhaDivSuperior.add(qtdParcelas);
			} else {
				segundaLinhaDivSuperior.remove(qtdParcelas);
			}
		});
		segundaLinhaDivSuperior.add(txtEstado, formasPagamento);
		superior.add(primeiraLinhaDivSuperior, segundaLinhaDivSuperior);

		centro.setHeight("660px");
		centro.getStyle().set("border-style", "ridge");
		centro.getStyle().set("overflow-y", "scroll");
		centro.setWidthFull();

		btnSalvar.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnSalvar.getStyle().set("margin-top", "0em");
		btnSalvar.getStyle().set("margin-left", "1em");
		btnSalvar.addClickListener(e -> {
			salvarClick();
			limparCampos();
		});

		btnFechar.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnFechar.getStyle().set("margin-top", "0em");
		btnFechar.addClickListener(e -> {
			dlgJanela.close();
			limparCampos();
		});

		btnAdicionarItem.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnAdicionarItem.getStyle().set("margin-top", "0em");
		btnAdicionarItem.addClickListener(e -> {
			adicionaProduto();
		});

		Label text = new Label("Valor Total");
		text.getElement().getStyle().set("fontWeight", "bold");
		text.getStyle().set("margin-top", "0.8em");
		text.getStyle().set("margin-left", "2em");
		text.getStyle().set("text-align", "center");
		campoSomaValores.getStyle().set("margin-top", "0em");
		campoSomaValores.getStyle().set("margin-right", "0.2em");
		campoSomaValores.setWidth("30em");

		htlDlgBarraBotoes.add(btnSalvar, btnFechar, btnAdicionarItem, text, campoSomaValores);

		inferior.getStyle().set("margin-top", "0px");
		inferior.setHeight("45px");
		inferior.setWidthFull();
		inferior.add(htlDlgBarraBotoes);

		dlgJanela.add(superior, centro, inferior);

	}

	private void salvarClick() {

		venda = binderVenda.getBean();

		boolean adicionarLista = venda.getId() == null ? true : false;

		vendaService.create(venda);

		if (adicionarLista) {
			listaVendas.add(venda);
		}
		atualizaGrdVenda();
		novaVenda();
		txtNomeCliente.focus();

		binderVenda.setBean(venda);

		if (adicionarLista) {
			dlgJanela.close();
		}
	}

	private void populaGrdVenda() {

		listaVendas = vendaService.read();
		atualizaGrdVenda();
	}

	private void atualizaGrdVenda() {
		grdVenda.setItems(listaVendas);
	}

	private void configuraBinder() {

		binderVenda.bindInstanceFields(this);

	}

	private void novoClick() {

		novaVenda();
		binderVenda.setBean(venda);

		dlgJanela.open();
		txtNomeCliente.focus();
	}

	private void alterarClick() {

		if (venda != null) {
			binderVenda.setBean(venda);

			dlgJanela.open();

		}
	}

	private void adicionaProduto() {

		ComboBox<Produto> txtProdutos = new ComboBox<>();

		NumberField txtQuantidade = new NumberField("Quantidade");

		TextField txtValorUnitario = new TextField("Valor Unitário");

		TextField txtValorTotalItem = new TextField("Valor Total Item");

		txtProdutos.setWidth("370px");
		txtProdutos.setLabel("Produtos");
		List<Produto> listaDeProdutos = produtoRepository.findAll();
		txtProdutos.setItemLabelGenerator(Produto::getNome);
		txtProdutos.setItems(listaDeProdutos);
		txtProdutos.addValueChangeListener(event -> {

			NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("pt", "BR"));
			try {

				txtValorUnitario.setValue(formatter.format(event.getValue().getValor()));

			} catch (Exception e) {
				e.printStackTrace();
			}

		});

//==========================================================================================================================

		txtQuantidade.setHasControls(true);
		txtQuantidade.setValue(null);
		txtQuantidade.setMin(1);

		txtQuantidade.addValueChangeListener(event -> {

			NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("pt", "BR"));
			double valorTotal = 0;

			try {
				valorTotal = formatter.parse(txtValorUnitario.getValue()).doubleValue() * txtQuantidade.getValue();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			txtValorTotalItem.setValue(formatter.format(valorTotal));

			double soma = 0;
			for (TextField tf : valores) {
				try {
					soma += formatter.parse(tf.getValue()).doubleValue();

				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			campoSomaValores.setValue(formatter.format(soma));

		});

		adicionarProdutos = new HorizontalLayout();
		adicionarProdutos.add(txtProdutos, txtQuantidade, txtValorUnitario, txtValorTotalItem);

		centro.add(adicionarProdutos);
		valores.add(txtValorTotalItem);
	}

	private void limparCampos() {
		centro.removeAll();
	}

	private void novaVenda() {
		venda = new Venda();
		venda.setCliente(null);
		dlgJanela.close();

	}
}

A lógica que passei, acredito que ficaria nesse método adicionaProduto. Vc soh precisa recuperar a referência da venda nesse método.

1 curtida

@Lucas_Camara vou ver se consigo codificar tudo isso, mas desde ja agradeço sua ajuda…sou novo em programação, tudo que sei aprendi aqui neste forum…rs

vou tentar, se conseguir te aviso!! Obg!!!

@Lucas_Camara

private List<ItemVenda> itens = new ArrayList<>();

neste exemplo que vc passou, sua sugestão é eu criar um classe ItemVenda?

Soh coloquei esse nome pq tu mencionou os itens de uma venda. O que vc considera itens de uma venda nesse seu sistema? Vi que tem o método “adicionaProduto”, mas não vi nenhuma classe que representa um Produto.

1 curtida

@Lucas_Camara

eu tenho esta classe Produto

package br.com.fjsistemas.backend;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
public class Produto {
	

    	@Id
    	@GeneratedValue(strategy = GenerationType.AUTO)
    	private Long id;
    	private String nome;
    	private String marca;
    	private String modelo;
    	private Double valor;

    }

Esta classe uso para Cadastrar os Produtos

No seu cenário, seria correto dizer, que: Cada Venda possui zero, um ou vários Produtos?

1 curtida

@Lucas_Camara

Pode variar…Cada venda possui ao menos 1 produto…zero nunca…mas pode ter mais de 100 produtos tbm

@Lucas_Camara

Veja como ficou:

package br.com.fjsistemas.backend;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
public class Venda {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	private LocalDate dataVenda = LocalDate.now();
	
	@ManyToMany
	private List<Produto> itens = new ArrayList<>();
	
	public void addItem(Produto item) {
		itens.add(item);
	}
	
	@ManyToOne
	private Cliente cliente;

	private String formasDePagamento;

	private String valorTotalVenda;
}

VendaView(veja o metodo adicionaProduto)

package br.com.fjsistemas.compraVenda;

import java.text.NumberFormat;
import java.text.ParseException;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.vaadin.textfieldformatter.CustomStringBlockFormatter;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.datepicker.DatePicker;
import com.vaadin.flow.component.dialog.Dialog;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.grid.GridVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.NumberField;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.data.binder.PropertyId;
import com.vaadin.flow.data.renderer.LocalDateRenderer;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;

import br.com.fjsistemas.backend.Cliente;
import br.com.fjsistemas.backend.Produto;
import br.com.fjsistemas.backend.Venda;
import br.com.fjsistemas.main.MainView;
import br.com.fjsistemas.repository.ClienteRepository;
import br.com.fjsistemas.repository.ProdutoRepository;
import br.com.fjsistemas.service.VendaService;

@Route(value = "venda-view", layout = MainView.class)
@PageTitle("Lançamento de Vendas")
public class VendaView extends VerticalLayout {

	private static final long serialVersionUID = 1L;

	private HorizontalLayout hltVenda = new HorizontalLayout();
	Grid<Venda> grdVenda = new Grid<>(Venda.class, false);

	private HorizontalLayout hltBarraBotoes = new HorizontalLayout();
	Button btnNovo = new Button("Novo");
	Button btnAlterar = new Button("Alterar");
	Button btnExcluir = new Button("Excluir");

	private Dialog dlgJanela = new Dialog();

	Div superior = new Div();
	Div centro = new Div();
	Div inferior = new Div();

	HorizontalLayout primeiraLinhaDivSuperior = new HorizontalLayout();
	HorizontalLayout segundaLinhaDivSuperior = new HorizontalLayout();
	HorizontalLayout adicionarProdutos = new HorizontalLayout();

	double somaValores;

	@PropertyId("dataVenda")
	private DatePicker txtDataVenda = new DatePicker("Data Venda");

	@PropertyId("cliente")
	private ComboBox<Cliente> txtNomeCliente = new ComboBox<>("Cliente");

	@PropertyId("telefone")
	private TextField txtTelefone = new TextField("Telefone");

	@PropertyId("celular")
	private TextField txtCelular = new TextField("Celular");

	@PropertyId("endereco")
	private TextField txtEndereco = new TextField("Endereço");

	@PropertyId("numero")
	private TextField txtNumero = new TextField("Nº");

	@PropertyId("bairro")
	private TextField txtBairro = new TextField("Bairro");

	@PropertyId("cidade")
	TextField txtCidade = new TextField("Cidade");

	@PropertyId("estado")
	TextField txtEstado = new TextField("Estado");

	@PropertyId("formasPagamento")
	private ComboBox<String> formasPagamento = new ComboBox<>("Formas de Pagamento");

	@PropertyId("parcelas")
	private ComboBox<String> qtdParcelas = new ComboBox<>("Nº Parcelas");

	@PropertyId("valorTotalVenda")
	private TextField campoSomaValores = new TextField();

	private HorizontalLayout htlDlgBarraBotoes = new HorizontalLayout();
	private Button btnSalvar = new Button("Salvar");
	private Button btnFechar = new Button("Fechar");
	private Button btnAdicionarItem = new Button("+ Item");

	@Autowired
	VendaService vendaService;

	@Autowired
	ClienteRepository clienteRepository;

	@Autowired
	ProdutoRepository produtoRepository;

	private List<Venda> listaVendas;
	private List<TextField> valores = new ArrayList<>();
	private Venda venda;

	Binder<Venda> binderVenda = new Binder<>(Venda.class);

	public VendaView() {

	}

	@PostConstruct
	public void init() {
		configuraTela();

	}

	private void configuraTela() {

		setMargin(false);
		setPadding(false);

		configuraHltVenda();
		configuraFltBarraBotoes();
		configuraDlgJanela();
		populaGrdVenda();
		configuraBinder();

		add(hltVenda, hltBarraBotoes);
	}

	private void configuraFltBarraBotoes() {

		btnNovo.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnNovo.addClickListener(e -> {
			novoClick();
		});

		btnAlterar.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnAlterar.addClickListener(e -> {
			alterarClick();
		});

		btnExcluir.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnExcluir.addClickListener(e -> {
			excluirClick();
		});

		hltBarraBotoes.add(btnNovo, btnAlterar, btnExcluir);
	}

	private void excluirClick() {

		if (venda != null) {
			listaVendas.remove(venda);
			vendaService.delete(venda);
			atualizaGrdVenda();
		}
	}

	private void configuraHltVenda() {
		hltVenda.setWidthFull();
		configuraGrdVenda();
		hltVenda.add(grdVenda);
	}

	private void configuraGrdVenda() {
		grdVenda.setHeight("820px");
		grdVenda.setWidthFull();

		grdVenda.addColumn(Venda::getId).setHeader("ID:").setAutoWidth(true);

		grdVenda.addColumn(new LocalDateRenderer<>(Venda::getDataVenda, DateTimeFormatter.ofPattern("dd/MM/yyy")))
				.setHeader("Data Venda").setAutoWidth(true);

		grdVenda.addColumn(venda -> venda.getCliente().getNome()).setHeader("Nome:").setAutoWidth(true)
				.setKey("cliente.nome");

		grdVenda.addColumn(Venda::getValorTotalVenda).setHeader("Valor Total:").setAutoWidth(true)
				.setKey("valorTotalVenda");

		grdVenda.addThemeVariants(GridVariant.LUMO_COMPACT, GridVariant.LUMO_COLUMN_BORDERS);

		grdVenda.getColumns().forEach(col -> col.setAutoWidth(true).setSortable(true).setResizable(true));

		grdVenda.addItemClickListener(e -> {
			venda = e.getItem();
		});

	}

	private void configuraDlgJanela() {

		dlgJanela.setHeightFull();
		dlgJanela.setWidthFull();
		dlgJanela.setCloseOnEsc(false);
		dlgJanela.setCloseOnOutsideClick(false);

		superior.setHeight("170px");
		superior.setWidthFull();

		txtNomeCliente.setWidth("350px");
		txtNomeCliente.setLabel("Nome Cliente");

		txtNomeCliente.setItemLabelGenerator(cliente -> {
			if (cliente == null || cliente.getNome() == null) {
				return " ";

			} else {
				return cliente.getNome();
			}
		});

		List<Cliente> listaDeClientes = clienteRepository.findAll();

		txtNomeCliente.setItems(listaDeClientes);
		txtNomeCliente.addValueChangeListener(event -> {
			if (event.getValue() == null || event.getValue().getFone() == null) {
				txtTelefone.setValue(" ");
			} else {
				txtTelefone.setValue(event.getValue().getFone());
			}
			if (event.getValue() == null || event.getValue().getCelular() == null) {
				txtCelular.setValue(" ");
			} else {
				txtCelular.setValue(event.getValue().getCelular());
			}
			if (event.getValue() == null || event.getValue().getEndereco() == null) {
				txtEndereco.setValue(" ");
			} else {
				txtEndereco.setValue(event.getValue().getEndereco());
			}
			if (event.getValue() == null || event.getValue().getNumero() == null) {
				txtNumero.setValue(" ");
			} else {
				txtNumero.setValue(event.getValue().getNumero());
			}
			if (event.getValue() == null || event.getValue().getBairro() == null) {
				txtBairro.setValue(" ");
			} else {
				txtBairro.setValue(event.getValue().getBairro());
			}
			if (event.getValue() == null || event.getValue().getCidade() == null) {
				txtCidade.setValue(" ");
			} else {
				txtCidade.setValue(event.getValue().getCidade());
			}
			if (event.getValue() == null || event.getValue().getEstado() == null) {
				txtEstado.setValue(" ");
			} else {
				txtEstado.setValue(event.getValue().getEstado());
			}
		});

		new CustomStringBlockFormatter.Builder().blocks(0, 2, 4, 4).delimiters("(", ")", "-").numeric().build()
				.extend(txtTelefone);

		new CustomStringBlockFormatter.Builder().blocks(0, 2, 5, 4).delimiters("(", ")", "-").numeric().build()
				.extend(txtCelular);

		primeiraLinhaDivSuperior.add(txtDataVenda, txtNomeCliente, txtTelefone, txtCelular, txtEndereco, txtNumero,
				txtBairro, txtCidade);

		txtNumero.setWidth("140px");
		formasPagamento.setItems("Boleto", "Cartão de Crédito", "Cartão Débito", "Cheque", "Dinheiro", "Pix",
				"TED / DOC");
		formasPagamento.addValueChangeListener(event -> {
			if (event.getValue() == "Cartão de Crédito") {
				qtdParcelas.setItems("A Vista", "2x", "3x", "4x", "5x", "6x", "7x", "8x", "9x", "10x", "11x", "12x");
				qtdParcelas.setWidth("141px");
				segundaLinhaDivSuperior.add(qtdParcelas);
			} else {
				segundaLinhaDivSuperior.remove(qtdParcelas);
			}
		});
		segundaLinhaDivSuperior.add(txtEstado, formasPagamento);
		superior.add(primeiraLinhaDivSuperior, segundaLinhaDivSuperior);

		centro.setHeight("660px");
		centro.getStyle().set("border-style", "ridge");
		centro.getStyle().set("overflow-y", "scroll");
		centro.setWidthFull();

		btnSalvar.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnSalvar.getStyle().set("margin-top", "0em");
		btnSalvar.getStyle().set("margin-left", "1em");
		btnSalvar.addClickListener(e -> {
			salvarClick();
			limparCampos();
		});

		btnFechar.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnFechar.getStyle().set("margin-top", "0em");
		btnFechar.addClickListener(e -> {
			dlgJanela.close();
			limparCampos();
		});

		btnAdicionarItem.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
		btnAdicionarItem.getStyle().set("margin-top", "0em");
		btnAdicionarItem.addClickListener(e -> {
			adicionaProduto();
		});

		Label text = new Label("Valor Total");
		text.getElement().getStyle().set("fontWeight", "bold");
		text.getStyle().set("margin-top", "0.8em");
		text.getStyle().set("margin-left", "2em");
		text.getStyle().set("text-align", "center");
		campoSomaValores.getStyle().set("margin-top", "0em");
		campoSomaValores.getStyle().set("margin-right", "0.2em");
		campoSomaValores.setWidth("30em");

		htlDlgBarraBotoes.add(btnSalvar, btnFechar, btnAdicionarItem, text, campoSomaValores);

		inferior.getStyle().set("margin-top", "0px");
		inferior.setHeight("45px");
		inferior.setWidthFull();
		inferior.add(htlDlgBarraBotoes);

		dlgJanela.add(superior, centro, inferior);

	}

	private void salvarClick() {

		venda = binderVenda.getBean();

		boolean adicionarLista = venda.getId() == null ? true : false;

		vendaService.create(venda);

		if (adicionarLista) {
			listaVendas.add(venda);
		}
		atualizaGrdVenda();
		novaVenda();
		txtNomeCliente.focus();

		binderVenda.setBean(venda);

		if (adicionarLista) {
			dlgJanela.close();
		}
	}

	private void populaGrdVenda() {

		listaVendas = vendaService.read();
		atualizaGrdVenda();
	}

	private void atualizaGrdVenda() {
		grdVenda.setItems(listaVendas);
	}

	private void configuraBinder() {

		binderVenda.bindInstanceFields(this);

	}

	private void novoClick() {

		novaVenda();
		binderVenda.setBean(venda);

		dlgJanela.open();
		txtNomeCliente.focus();
	}

	private void alterarClick() {

		if (venda != null) {
			binderVenda.setBean(venda);

			dlgJanela.open();

		}
	}

	private void adicionaProduto() {

		ComboBox<Produto> txtProdutos = new ComboBox<>();

		NumberField txtQuantidade = new NumberField("Quantidade");

		TextField txtValorUnitario = new TextField("Valor Unitário");

		TextField txtValorTotalItem = new TextField("Valor Total Item");

		txtProdutos.setWidth("370px");
		txtProdutos.setLabel("Produtos");
		List<Produto> listaDeProdutos = produtoRepository.findAll();
		txtProdutos.setItemLabelGenerator(Produto::getNome);
		txtProdutos.setItems(listaDeProdutos);
		txtProdutos.addValueChangeListener(event -> {

			NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("pt", "BR"));
			try {

				txtValorUnitario.setValue(formatter.format(event.getValue().getValor()));

			} catch (Exception e) {
				e.printStackTrace();
			}

		});

//==========================================================================================================================

		txtQuantidade.setHasControls(true);
		txtQuantidade.setValue(null);
		txtQuantidade.setMin(1);

		txtQuantidade.addValueChangeListener(event -> {

			NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("pt", "BR"));
			double valorTotal = 0;

			try {
				valorTotal = formatter.parse(txtValorUnitario.getValue()).doubleValue() * txtQuantidade.getValue();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			txtValorTotalItem.setValue(formatter.format(valorTotal));

			double soma = 0;
			for (TextField tf : valores) {
				try {
					soma += formatter.parse(tf.getValue()).doubleValue();

				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			campoSomaValores.setValue(formatter.format(soma));

		});

		adicionarProdutos = new HorizontalLayout();
		adicionarProdutos.add(txtProdutos, txtQuantidade, txtValorUnitario, txtValorTotalItem);
		
		Produto novoItem = txtProdutos.getValue();
		venda.addItem(novoItem);

		centro.add(adicionarProdutos);
		valores.add(txtValorTotalItem);
	}

	private void limparCampos() {
		centro.removeAll();
	}

	private void novaVenda() {
		venda = new Venda();
		venda.setCliente(null);
		dlgJanela.close();

	}
}

Acho que está no caminho. O mapeamento do produto com a venda é @ManyToMany mesmo? Como está seu modelo de dados?

1 curtida

@Lucas_Camara

Não criei uma modelagem…fui apenas elaborando conforme vinha na mente mesmo…rs…não sei te responder se ManyToMany é o correto, mas não deu erro

mas continua sem salvar

Tu tem que configurar o cascade na @ManyToMany.

1 curtida

@Lucas_Camara

Tentei de tudo nada funciona…vou dar mais uma pesquisada…se tiver outra ideia, é só falar!! Muito obg pelo seu tempo!!!