sistemaVendas.RealizarVenda.java Source code

Java tutorial

Introduction

Here is the source code for sistemaVendas.RealizarVenda.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package sistemaVendas;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

/**
 *
 * @author BRUNO
 */
public class RealizarVenda extends javax.swing.JInternalFrame {

    /**
     * Creates new form RealizarVenda
     */
    JDesktopPane telas;

    public RealizarVenda(JDesktopPane tela) {
        telas = tela;
        initComponents();
        preencherComboBoxCliente();
        model3.setNumRows(0);
        model4.setNumRows(0);
        model5.setNumRows(0);
        tabItensVenda.setModel(model3);
        tabProdutoBusca.setModel(model4);
        tabClienteBusca.setModel(model5);
        lblNumeroParcela.setVisible(false);
        cbxNumParcela.setVisible(false);
        lblQuantParcela.setVisible(false);
        lblX.setVisible(false);
        btnRemoverItem.setVisible(false);
        spnData.setEditor(new JSpinner.DateEditor(spnData, "dd/MM/yyyy"));

        /*Tamanho das Colunas*/
        tabItensVenda.getColumnModel().getColumn(0).setMaxWidth(25);
        tabItensVenda.getColumnModel().getColumn(1).setMaxWidth(200);
        tabItensVenda.getColumnModel().getColumn(1).setMinWidth(200);

        tabClienteBusca.getColumnModel().getColumn(0).setMaxWidth(25);
        tabClienteBusca.getColumnModel().getColumn(1).setMaxWidth(200);
        tabClienteBusca.getColumnModel().getColumn(1).setMinWidth(200);
        tabClienteBusca.getColumnModel().getColumn(3).setMaxWidth(55);

        tabProdutoBusca.getColumnModel().getColumn(0).setMaxWidth(25);
        tabProdutoBusca.getColumnModel().getColumn(1).setMaxWidth(250);
        tabProdutoBusca.getColumnModel().getColumn(1).setMinWidth(250);
    }

    Document doc;
    Element root;

    public void preencherComboBoxCliente() {
        File diretorio = new File("Bancos de Dados");
        File arquivo = new File(diretorio, "BancoCliente.xml");
        cbxClienteVenda.addItem("---------Selecionar Cliente---------");
        try {
            SAXBuilder builder = new SAXBuilder();
            doc = builder.build(arquivo);
            root = (Element) doc.getRootElement();
            List<Element> clientes = root.getChildren();
            for (int i = 0; i < clientes.size(); i++) {
                Element cliente = clientes.get(i);
                cbxClienteVenda.addItem(cliente.getChildText("nome"));
            }
        } catch (JDOMException | IOException e) {
        }
    }

    public String formatarData() {// Retorna a data com uma String legvel
        SimpleDateFormat formater = new SimpleDateFormat("dd/MM/yyyy");
        String valorDaData = formater.format(spnData.getValue());
        return valorDaData;
    }

    NotaFiscal notaFiscal;

    public void criarNotaFiscal(String numNota) {
        notaFiscal = new NotaFiscal(numNota);
        /*Centralizar tela*/
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension tamanhoTela = toolkit.getScreenSize();
        int x = (tamanhoTela.width - notaFiscal.getWidth()) / 2;
        int y = (tamanhoTela.height - notaFiscal.getHeight()) / 2;
        telas.add(notaFiscal);
        notaFiscal.setVisible(true);
        notaFiscal.setLocation(x, y - 60);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grupoPagamento = new javax.swing.ButtonGroup();
        tabGuiasVenda = new javax.swing.JTabbedPane();
        pnlControle = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabItensVenda = new javax.swing.JTable();
        lblIdVenda = new javax.swing.JLabel();
        lblNomeProdutoVenda = new javax.swing.JLabel();
        lblPrecoUnit = new javax.swing.JLabel();
        lblQtdeVenda = new javax.swing.JLabel();
        txtIdVenda = new javax.swing.JTextField();
        txtNomeProdutoVenda = new javax.swing.JTextField();
        txtPrecoUnit = new javax.swing.JTextField();
        txtQtdeVenda = new javax.swing.JTextField();
        lblTotalItens = new javax.swing.JLabel();
        txtTotalItens = new javax.swing.JTextField();
        btnAdicionarItem = new javax.swing.JButton();
        btnRemoverItem = new javax.swing.JButton();
        rbAvista = new javax.swing.JRadioButton();
        rbDebito = new javax.swing.JRadioButton();
        rbCredito = new javax.swing.JRadioButton();
        lblNumeroParcela = new javax.swing.JLabel();
        lblTotalPagar = new javax.swing.JLabel();
        txtTotalPagar = new javax.swing.JTextField();
        btnFinalizar = new javax.swing.JButton();
        btnCancelarVenda = new javax.swing.JButton();
        btnRemoverTodosItens = new javax.swing.JButton();
        cbxNumParcela = new javax.swing.JComboBox();
        lblQuantParcela = new javax.swing.JLabel();
        lblX = new javax.swing.JLabel();
        pnlBusca = new javax.swing.JPanel();
        lblNomeProdutoBusca = new javax.swing.JLabel();
        txtNomeProdutoBusca = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabProdutoBusca = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        lblNomeClienteBusca = new javax.swing.JLabel();
        txtNomeClienteBusca = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        tabClienteBusca = new javax.swing.JTable();
        lblVenda = new javax.swing.JLabel();
        lblClienteVenda = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        cbxClienteVenda = new javax.swing.JComboBox();
        lblIdClienteVenda = new javax.swing.JLabel();
        lblNomeClienteVenda = new javax.swing.JLabel();
        lblFoneClienteVenda = new javax.swing.JLabel();
        lblSexoClienteVenda = new javax.swing.JLabel();
        lblIdadeClienteVenda = new javax.swing.JLabel();
        lblCpfClienteVenda = new javax.swing.JLabel();
        lblData = new javax.swing.JLabel();
        spnData = new javax.swing.JSpinner();

        setClosable(true);
        setTitle("Realizao de Vendas");
        setPreferredSize(new java.awt.Dimension(695, 585));

        tabItensVenda
                .setModel(new javax.swing.table.DefaultTableModel(
                        new Object[][] { { null, null, null, null }, { null, null, null, null },
                                { null, null, null, null }, { null, null, null, null } },
                        new String[] { "Title 1", "Title 2", "Title 3", "Title 4" }));
        tabItensVenda.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabItensVendaMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tabItensVenda);

        lblIdVenda.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblIdVenda.setText("ID:");

        lblNomeProdutoVenda.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblNomeProdutoVenda.setText("Nome do Produto:");

        lblPrecoUnit.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblPrecoUnit.setText("Preo Unitrio:");

        lblQtdeVenda.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblQtdeVenda.setText("Qtde:");

        txtIdVenda.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtIdVendaFocusLost(evt);
            }
        });

        txtNomeProdutoVenda.setEnabled(false);

        txtPrecoUnit.setEnabled(false);

        lblTotalItens.setFont(new java.awt.Font("Tahoma", 2, 14)); // NOI18N
        lblTotalItens.setText("Total: R$");

        txtTotalItens.setEnabled(false);

        btnAdicionarItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/AddItem.jpg"))); // NOI18N
        btnAdicionarItem.setText("Add Item");
        btnAdicionarItem.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                btnAdicionarItemStateChanged(evt);
            }
        });
        btnAdicionarItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdicionarItemActionPerformed(evt);
            }
        });

        btnRemoverItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/RemoveItem.jpg"))); // NOI18N
        btnRemoverItem.setText("Remover Item");
        btnRemoverItem.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                btnRemoverItemStateChanged(evt);
            }
        });
        btnRemoverItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoverItemActionPerformed(evt);
            }
        });

        grupoPagamento.add(rbAvista);
        rbAvista.setText("? Vista 10%");
        rbAvista.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbAvistaActionPerformed(evt);
            }
        });

        grupoPagamento.add(rbDebito);
        rbDebito.setText("Dbito 5%");
        rbDebito.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbDebitoActionPerformed(evt);
            }
        });

        grupoPagamento.add(rbCredito);
        rbCredito.setText("Crdito (Parcela at 3x)");
        rbCredito.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                rbCreditoItemStateChanged(evt);
            }
        });
        rbCredito.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbCreditoActionPerformed(evt);
            }
        });

        lblNumeroParcela.setText("N Parcela:");

        lblTotalPagar.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        lblTotalPagar.setForeground(new java.awt.Color(255, 0, 0));
        lblTotalPagar.setText("Total A Pagar");

        txtTotalPagar.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        txtTotalPagar.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        btnFinalizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/ok.png"))); // NOI18N
        btnFinalizar.setText("Finalizar Venda");
        btnFinalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFinalizarActionPerformed(evt);
            }
        });

        btnCancelarVenda.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancel.png"))); // NOI18N
        btnCancelarVenda.setText("Cancelar");
        btnCancelarVenda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarVendaActionPerformed(evt);
            }
        });

        btnRemoverTodosItens.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/RemoveAll.jpg"))); // NOI18N
        btnRemoverTodosItens.setText("Remover All");
        btnRemoverTodosItens.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                btnRemoverTodosItensStateChanged(evt);
            }
        });
        btnRemoverTodosItens.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoverTodosItensActionPerformed(evt);
            }
        });

        cbxNumParcela.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "---", "1", "2", "3" }));
        cbxNumParcela.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxNumParcelaActionPerformed(evt);
            }
        });

        lblQuantParcela.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        lblQuantParcela.setText("0");

        lblX.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblX.setText("x");

        javax.swing.GroupLayout pnlControleLayout = new javax.swing.GroupLayout(pnlControle);
        pnlControle.setLayout(pnlControleLayout);
        pnlControleLayout.setHorizontalGroup(pnlControleLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlControleLayout.createSequentialGroup().addGroup(pnlControleLayout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnlControleLayout.createSequentialGroup().addGroup(pnlControleLayout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(pnlControleLayout.createSequentialGroup().addGap(21, 21, 21)
                                        .addGroup(pnlControleLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(pnlControleLayout.createSequentialGroup()
                                                        .addComponent(lblIdVenda)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(txtIdVenda,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 35,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addGap(18, 18, 18).addComponent(lblNomeProdutoVenda))
                                                .addGroup(pnlControleLayout.createSequentialGroup()
                                                        .addComponent(lblPrecoUnit)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(txtPrecoUnit,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 73,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(pnlControleLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(pnlControleLayout.createSequentialGroup()
                                                        .addComponent(lblQtdeVenda)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(txtQtdeVenda,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 67,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                Short.MAX_VALUE))
                                                .addGroup(pnlControleLayout.createSequentialGroup()
                                                        .addComponent(txtNomeProdutoVenda,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 375,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                                73, Short.MAX_VALUE))))
                                .addGroup(pnlControleLayout.createSequentialGroup().addContainerGap()
                                        .addGroup(pnlControleLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(pnlControleLayout.createSequentialGroup()
                                                        .addGroup(pnlControleLayout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.TRAILING)
                                                                .addComponent(jScrollPane2,
                                                                        javax.swing.GroupLayout.Alignment.LEADING,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGroup(pnlControleLayout.createSequentialGroup()
                                                                        .addComponent(rbAvista)
                                                                        .addPreferredGap(
                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addComponent(rbDebito)
                                                                        .addPreferredGap(
                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addComponent(rbCredito).addGap(8, 8, 8)
                                                                        .addGroup(pnlControleLayout
                                                                                .createParallelGroup(
                                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addComponent(lblTotalItens)
                                                                                .addComponent(lblNumeroParcela))
                                                                        .addPreferredGap(
                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addGroup(pnlControleLayout
                                                                                .createParallelGroup(
                                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addComponent(txtTotalItens,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                        81,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                .addGroup(pnlControleLayout
                                                                                        .createSequentialGroup()
                                                                                        .addComponent(cbxNumParcela,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                        .addPreferredGap(
                                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                                                                42, Short.MAX_VALUE)
                                                                                        .addComponent(
                                                                                                lblQuantParcela)))))
                                                        .addGap(7, 7, 7).addComponent(lblX).addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                                        pnlControleLayout.createSequentialGroup()
                                                                .addComponent(btnCancelarVenda)
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(btnFinalizar).addGap(48, 48, 48)))
                                        .addGap(18, 18, 18)
                                        .addGroup(pnlControleLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(txtTotalPagar, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGroup(pnlControleLayout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(btnRemoverItem,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                Short.MAX_VALUE)
                                                        .addComponent(btnAdicionarItem,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                Short.MAX_VALUE)
                                                        .addComponent(btnRemoverTodosItens,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 135,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))))
                                .addGap(0, 4, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                pnlControleLayout.createSequentialGroup()
                                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(lblTotalPagar)))
                        .addContainerGap()));
        pnlControleLayout
                .setVerticalGroup(pnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                pnlControleLayout.createSequentialGroup().addGap(28, 28, 28)
                                        .addGroup(pnlControleLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(lblIdVenda).addComponent(lblNomeProdutoVenda)
                                                .addComponent(txtIdVenda, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(
                                                        txtNomeProdutoVenda, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(18, 18, 18)
                                        .addGroup(pnlControleLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addGroup(pnlControleLayout.createSequentialGroup()
                                                        .addGroup(pnlControleLayout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.BASELINE)
                                                                .addComponent(lblPrecoUnit)
                                                                .addComponent(lblQtdeVenda)
                                                                .addComponent(txtPrecoUnit,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addComponent(txtQtdeVenda,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addGap(33, 33, 33)
                                                        .addComponent(jScrollPane2,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 139,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addGroup(pnlControleLayout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.BASELINE)
                                                                .addComponent(lblTotalItens)
                                                                .addComponent(txtTotalItens,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                .addGroup(pnlControleLayout.createSequentialGroup()
                                                        .addComponent(btnAdicionarItem)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(btnRemoverItem)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(btnRemoverTodosItens)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                        .addComponent(
                                                                lblTotalPagar,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 45,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(pnlControleLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(pnlControleLayout.createSequentialGroup()
                                                        .addGroup(pnlControleLayout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.BASELINE)
                                                                .addComponent(rbAvista).addComponent(rbDebito)
                                                                .addComponent(rbCredito)
                                                                .addComponent(lblNumeroParcela)
                                                                .addComponent(cbxNumParcela,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addGap(18, 18, 18).addGroup(pnlControleLayout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.BASELINE)
                                                                .addComponent(btnCancelarVenda)
                                                                .addComponent(btnFinalizar)))
                                                .addGroup(pnlControleLayout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addGroup(pnlControleLayout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.BASELINE)
                                                                .addComponent(lblQuantParcela).addComponent(lblX))
                                                        .addComponent(txtTotalPagar,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 39,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addContainerGap(96, Short.MAX_VALUE)));

        tabGuiasVenda.addTab("Controle", pnlControle);

        lblNomeProdutoBusca.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblNomeProdutoBusca.setText("Nome do Produto:");

        txtNomeProdutoBusca.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtNomeProdutoBuscaKeyReleased(evt);
            }
        });

        tabProdutoBusca
                .setModel(new javax.swing.table.DefaultTableModel(
                        new Object[][] { { null, null, null, null }, { null, null, null, null },
                                { null, null, null, null }, { null, null, null, null } },
                        new String[] { "Title 1", "Title 2", "Title 3", "Title 4" }));
        tabProdutoBusca.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabProdutoBuscaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabProdutoBusca);

        javax.swing.GroupLayout pnlBuscaLayout = new javax.swing.GroupLayout(pnlBusca);
        pnlBusca.setLayout(pnlBuscaLayout);
        pnlBuscaLayout.setHorizontalGroup(pnlBuscaLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlBuscaLayout.createSequentialGroup()
                        .addContainerGap(75, Short.MAX_VALUE)
                        .addGroup(pnlBuscaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 537,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(pnlBuscaLayout.createSequentialGroup().addComponent(lblNomeProdutoBusca)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtNomeProdutoBusca, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                418, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(60, 60, 60)));
        pnlBuscaLayout.setVerticalGroup(pnlBuscaLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlBuscaLayout.createSequentialGroup().addGap(29, 29, 29)
                        .addGroup(pnlBuscaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblNomeProdutoBusca)
                                .addComponent(txtNomeProdutoBusca, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(37, 37, 37)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 191,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        tabGuiasVenda.addTab("Buscar Produto", pnlBusca);

        lblNomeClienteBusca.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblNomeClienteBusca.setText("Nome do Cliente:");

        txtNomeClienteBusca.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtNomeClienteBuscaKeyReleased(evt);
            }
        });

        tabClienteBusca
                .setModel(new javax.swing.table.DefaultTableModel(
                        new Object[][] { { null, null, null, null }, { null, null, null, null },
                                { null, null, null, null }, { null, null, null, null } },
                        new String[] { "Title 1", "Title 2", "Title 3", "Title 4" }));
        tabClienteBusca.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabClienteBuscaMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tabClienteBusca);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout
                .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup().addGap(51, 51, 51).addGroup(jPanel1Layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jScrollPane3)
                                .addGroup(jPanel1Layout.createSequentialGroup().addComponent(lblNomeClienteBusca)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtNomeClienteBusca, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                427, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(85, Short.MAX_VALUE)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addGap(28, 28, 28)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblNomeClienteBusca).addComponent(txtNomeClienteBusca,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(40, 40, 40).addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 191,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(185, Short.MAX_VALUE)));

        tabGuiasVenda.addTab("Buscar Cliente", jPanel1);

        lblVenda.setFont(new java.awt.Font("Tahoma", 3, 24)); // NOI18N
        lblVenda.setText("Venda");

        lblClienteVenda.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblClienteVenda.setText("Cliente:");

        cbxClienteVenda.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbxClienteVendaItemStateChanged(evt);
            }
        });

        lblIdClienteVenda.setText("ID:");

        lblNomeClienteVenda.setText("Nome:");

        lblFoneClienteVenda.setText("Fone:");

        lblSexoClienteVenda.setText("Sexo:");

        lblIdadeClienteVenda.setText("Idade:");

        lblCpfClienteVenda.setText("CPF:");

        lblData.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblData.setText("Data:");

        spnData.setModel(new javax.swing.SpinnerDateModel());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addGap(305, 305, 305)
                                .addComponent(lblVenda, javax.swing.GroupLayout.PREFERRED_SIZE, 84,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lblData)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(spnData, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(lblClienteVenda)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbxClienteVenda, javax.swing.GroupLayout.PREFERRED_SIZE, 228,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap())
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jSeparator1)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                layout.createSequentialGroup().addComponent(lblSexoClienteVenda)
                                        .addGap(112, 112, 112).addComponent(lblIdadeClienteVenda).addGap(77, 77, 77)
                                        .addComponent(lblFoneClienteVenda)
                                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                layout.createSequentialGroup().addComponent(lblIdClienteVenda).addGap(61, 61, 61)
                                        .addComponent(lblNomeClienteVenda)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(lblCpfClienteVenda).addGap(207, 207, 207))
                        .addComponent(tabGuiasVenda, javax.swing.GroupLayout.DEFAULT_SIZE, 677, Short.MAX_VALUE))));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblData).addComponent(spnData, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(lblVenda)).addGap(16, 16, 16)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblClienteVenda).addComponent(cbxClienteVenda,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblIdClienteVenda).addComponent(lblNomeClienteVenda)
                                .addComponent(lblCpfClienteVenda))
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblSexoClienteVenda).addComponent(lblIdadeClienteVenda)
                                .addComponent(lblFoneClienteVenda))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tabGuiasVenda, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(44, 44, 44)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarVendaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarVendaActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarVendaActionPerformed

    private void cbxClienteVendaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbxClienteVendaItemStateChanged
        /*Se o primeiro item "-----Selecionar Cliente-----" estiver selecionado, seta vazio nas labels*/
        if (cbxClienteVenda.getSelectedItem().equals("---------Selecionar Cliente---------")) {
            lblIdClienteVenda.setText("ID: ");
            lblNomeClienteVenda.setText("Nome: ");
            lblCpfClienteVenda.setText("CPF: ");
            lblSexoClienteVenda.setText("Sexo: ");
            lblIdadeClienteVenda.setText("Idade: ");
            lblFoneClienteVenda.setText("Fone: ");
        }
        /*Faz a Busca e modifica as Labels*/
        File diretorio = new File("Bancos de Dados");
        File arquivo = new File(diretorio, "BancoCliente.xml");
        try {
            SAXBuilder builder = new SAXBuilder();
            doc = builder.build(arquivo);
            root = (Element) doc.getRootElement();
            List<Element> clientes = root.getChildren();
            for (int i = 0; i < clientes.size(); i++) {
                Element cliente = clientes.get(i);
                if (cliente.getChildText("nome").equals((String) cbxClienteVenda.getSelectedItem())) {
                    lblIdClienteVenda.setText("ID: " + cliente.getAttributeValue("id"));
                    lblNomeClienteVenda.setText("Nome: " + cliente.getChildText("nome"));
                    lblCpfClienteVenda.setText("CPF: " + cliente.getChildText("cpf"));
                    lblSexoClienteVenda.setText("Sexo: " + cliente.getChildText("sexo"));
                    lblIdadeClienteVenda.setText("Idade: " + cliente.getChildText("idade"));
                    lblFoneClienteVenda.setText("Fone: " + cliente.getChildText("telefone"));
                }
            }
        } catch (JDOMException | IOException e) {
        }
    }//GEN-LAST:event_cbxClienteVendaItemStateChanged

    private void btnAdicionarItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdicionarItemActionPerformed
        String idProduto = txtIdVenda.getText();
        String qtdeProduto = txtQtdeVenda.getText();
        String dados[] = new String[5];
        File diretorio = new File("Bancos de Dados");
        File arquivo = new File(diretorio, "BancoProduto.xml");

        String total = "";
        if (idProduto.equals("") || qtdeProduto.equals("")) {
            JOptionPane.showMessageDialog(null, "CAMPOS EM BRANCO!", "ERRO", JOptionPane.ERROR_MESSAGE);

        } else {
            try {
                SAXBuilder builder = new SAXBuilder();
                doc = builder.build(arquivo);
                root = (Element) doc.getRootElement();
                List<Element> produtos = root.getChildren();
                for (int i = 0; i < produtos.size(); i++) {
                    Element produto = produtos.get(i);
                    if (idProduto.equals(produto.getAttributeValue("id"))) {
                        dados[0] = produto.getAttributeValue("id");
                        dados[1] = produto.getChildText("nome").toUpperCase();
                        dados[2] = txtQtdeVenda.getText();
                        dados[3] = produto.getChildText("preoUnitrio");
                        String precoaux = produto.getChildText("preoUnitrio").replaceAll(",", ".");
                        Double precodouble = Double.parseDouble(precoaux);
                        int quantidadeint = Integer.parseInt(txtQtdeVenda.getText());
                        double totaldouble = precodouble * quantidadeint;
                        total = String.valueOf(totaldouble);
                        dados[4] = total;
                    }
                }
                model3.addRow(new Object[] { dados[0], dados[1], dados[3], dados[2], dados[4] });
            } catch (JDOMException | IOException e) {
            }
        }
        txtIdVenda.setText("");
        txtNomeProdutoVenda.setText("");
        txtPrecoUnit.setText("");
        txtQtdeVenda.setText("");
    }//GEN-LAST:event_btnAdicionarItemActionPerformed

    private void btnRemoverTodosItensActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoverTodosItensActionPerformed
        model3.setNumRows(0);
    }//GEN-LAST:event_btnRemoverTodosItensActionPerformed

    private void txtIdVendaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtIdVendaFocusLost
        String idProduto = txtIdVenda.getText();
        File diretorio = new File("Bancos de Dados");
        File arquivo = new File(diretorio, "BancoProduto.xml");

        if (idProduto.equals("")) {
            txtNomeProdutoVenda.setText("");
            txtPrecoUnit.setText("");
            txtQtdeVenda.setText("");
        } else {
            try {
                SAXBuilder builder = new SAXBuilder();
                doc = builder.build(arquivo);
                root = (Element) doc.getRootElement();
                List<Element> produtos = root.getChildren();
                for (int i = 0; i < produtos.size(); i++) {
                    Element produto = produtos.get(i);
                    if (idProduto.equals(produto.getAttributeValue("id"))) {
                        txtNomeProdutoVenda.setText(produto.getChildText("nome"));
                        txtPrecoUnit.setText(produto.getChildText("preoUnitrio"));
                    }
                }
            } catch (JDOMException | IOException e) {
            }
        }
    }//GEN-LAST:event_txtIdVendaFocusLost

    int linhaTabela;

    private void btnRemoverItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoverItemActionPerformed
        if (model3.getRowCount() == 0) {
            JOptionPane.showMessageDialog(null, "SEM ITENS NA TABELA!", "ERRO", JOptionPane.ERROR_MESSAGE);
        } else {
            ((DefaultTableModel) tabItensVenda.getModel()).removeRow(linhaTabela);
            tabItensVenda.validate();
        }
    }//GEN-LAST:event_btnRemoverItemActionPerformed

    private void rbCreditoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbCreditoActionPerformed
        lblNumeroParcela.setVisible(true);
        cbxNumParcela.setVisible(true);
        lblX.setVisible(true);
        lblQuantParcela.setVisible(true);
    }//GEN-LAST:event_rbCreditoActionPerformed

    private void rbCreditoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_rbCreditoItemStateChanged
        lblNumeroParcela.setVisible(false);
        cbxNumParcela.setVisible(false);
        lblQtdeVenda.setVisible(false);
        lblX.setVisible(false);
        lblQuantParcela.setVisible(false);
    }//GEN-LAST:event_rbCreditoItemStateChanged

    private void tabItensVendaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabItensVendaMouseClicked
        btnRemoverItem.setVisible(true);
        linhaTabela = tabItensVenda.getSelectedRow();
    }//GEN-LAST:event_tabItensVendaMouseClicked

    private void btnAdicionarItemStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_btnAdicionarItemStateChanged
        double total = 0;
        String totalaux;
        for (int i = 0; i < model3.getRowCount(); i++) {
            totalaux = (String) tabItensVenda.getValueAt(i, 4);
            total += Double.parseDouble(totalaux);
        }
        totalaux = String.valueOf(total);
        txtTotalItens.setText(totalaux);
        txtTotalPagar.setText(totalaux);
    }//GEN-LAST:event_btnAdicionarItemStateChanged

    private void btnRemoverItemStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_btnRemoverItemStateChanged
        double total = 0;
        String totalaux;
        for (int i = 0; i < model3.getRowCount(); i++) {
            totalaux = (String) tabItensVenda.getValueAt(i, 4);
            total += Double.parseDouble(totalaux);
        }
        totalaux = String.valueOf(total);
        txtTotalItens.setText(totalaux);
        txtTotalPagar.setText(totalaux);
    }//GEN-LAST:event_btnRemoverItemStateChanged

    private void btnRemoverTodosItensStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_btnRemoverTodosItensStateChanged
        double total = 0;
        String totalaux;
        for (int i = 0; i < model3.getRowCount(); i++) {
            totalaux = (String) tabItensVenda.getValueAt(i, 4);
            total += Double.parseDouble(totalaux);
        }
        totalaux = String.valueOf(total);
        txtTotalItens.setText(totalaux);
        txtTotalPagar.setText(totalaux);
    }//GEN-LAST:event_btnRemoverTodosItensStateChanged

    private void rbAvistaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbAvistaActionPerformed
        double totalpagar;
        Double total = Double.parseDouble(txtTotalItens.getText());
        totalpagar = (total - (total * 0.1));
        String totalpg = String.valueOf(totalpagar);
        txtTotalPagar.setText(totalpg);
    }//GEN-LAST:event_rbAvistaActionPerformed

    private void rbDebitoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbDebitoActionPerformed
        double totalpagar;
        Double total = Double.parseDouble(txtTotalItens.getText());
        totalpagar = (total - (total * 0.05));
        String totalpg = String.valueOf(totalpagar);
        txtTotalPagar.setText(totalpg);
    }//GEN-LAST:event_rbDebitoActionPerformed

    private void btnFinalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFinalizarActionPerformed
        if (!(cbxClienteVenda.getSelectedIndex() == 0)) {
            if (criarXml()) {
                JOptionPane.showMessageDialog(null, "DADOS SALVOS COM SUCESSO!", "Sucesso",
                        JOptionPane.INFORMATION_MESSAGE);
                criarNotaFiscal(numeroDaUltimaNota);
                cbxClienteVenda.setSelectedIndex(0);
                txtIdVenda.setText("");
                txtNomeProdutoVenda.setText("");
                txtPrecoUnit.setText("");
                txtQtdeVenda.setText("");
                txtTotalPagar.setText("");
                txtTotalItens.setText("");
                editarQuantidadeProduto();
                model3.setNumRows(0);
                grupoPagamento.setSelected(null, true);
            } else {
                JOptionPane.showMessageDialog(null, "ERRO NA CRIAO DA NOTA!", "ERRO",
                        JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null, "SELECIONE UM CLIENTE!", "ERRO", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnFinalizarActionPerformed

    private void cbxNumParcelaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxNumParcelaActionPerformed
        String numparc = (String) cbxNumParcela.getSelectedItem();
        double totalpagar;
        if (!(numparc.equals("---"))) {
            double total = Double.parseDouble(txtTotalItens.getText());
            int parcela = Integer.parseInt(numparc);
            totalpagar = total / parcela;
            String totalpg = String.valueOf(totalpagar);
            lblQuantParcela.setText(numparc);
            txtTotalPagar.setText(totalpg);
        } else {
            JOptionPane.showMessageDialog(null, "SELECIONE O PARCELAMENTO", "ERRO", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_cbxNumParcelaActionPerformed

    private void tabProdutoBuscaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabProdutoBuscaMouseClicked
        linhaTabela = tabProdutoBusca.getSelectedRow();
        String idaux = (String) tabProdutoBusca.getValueAt(linhaTabela, 0);
        String nomeprodaux = (String) tabProdutoBusca.getValueAt(linhaTabela, 1);
        String precoaux = (String) tabProdutoBusca.getValueAt(linhaTabela, 3);

        String dados[] = new String[5];
        if (evt.getClickCount() == 2) {
            if (nomeprodaux.equals("")) {
                JOptionPane.showMessageDialog(null, "CAMPOS EM BRANCO!", "ERRO", JOptionPane.ERROR_MESSAGE);
            } else {
                String quantidade = JOptionPane.showInputDialog("INFORME A QUANTIDADE:");
                String preco = precoaux.replaceAll(",", ".");
                Double precodouble = Double.parseDouble(preco);
                int quantidadeint = Integer.parseInt(quantidade);
                double totaldouble = precodouble * quantidadeint;
                String total = String.valueOf(totaldouble);
                dados[0] = idaux;
                dados[1] = nomeprodaux;
                dados[2] = preco;
                dados[3] = quantidade;
                dados[4] = total;
                model3.addRow(new Object[] { dados[0], dados[1], dados[2], dados[3], dados[4] });
                JOptionPane.showMessageDialog(null, "PRODUTO ADICIONADO!", "Sucesso",
                        JOptionPane.INFORMATION_MESSAGE);
                model4.setNumRows(0);
                double totale = 0;
                String totalaux;
                for (int i = 0; i < model3.getRowCount(); i++) {
                    totalaux = (String) tabItensVenda.getValueAt(i, 4);
                    totale += Double.parseDouble(totalaux);
                }
                totalaux = String.valueOf(totale);
                txtTotalItens.setText(totalaux);
                txtTotalPagar.setText(totalaux);

            }
        }
    }//GEN-LAST:event_tabProdutoBuscaMouseClicked

    private void tabClienteBuscaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabClienteBuscaMouseClicked
        linhaTabela = tabClienteBusca.getSelectedRow();
        String nomeclidaux = (String) tabClienteBusca.getValueAt(linhaTabela, 1);
        if (evt.getClickCount() == 2) {
            if (nomeclidaux.equals("")) {
                JOptionPane.showMessageDialog(null, "CAMPOS EM BRANCO!", "ERRO", JOptionPane.ERROR_MESSAGE);
            } else {
                cbxClienteVenda.setSelectedItem(nomeclidaux);
                JOptionPane.showMessageDialog(null, "CLIENTE SELECIONADO!", "Sucesso",
                        JOptionPane.INFORMATION_MESSAGE);
                model5.setNumRows(0);
            }
        }
    }//GEN-LAST:event_tabClienteBuscaMouseClicked

    private void txtNomeClienteBuscaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNomeClienteBuscaKeyReleased
        procurarCliente(txtNomeClienteBusca.getText().toUpperCase());
    }//GEN-LAST:event_txtNomeClienteBuscaKeyReleased

    private void txtNomeProdutoBuscaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNomeProdutoBuscaKeyReleased
        procurarProduto(txtNomeProdutoBusca.getText().toUpperCase());
    }//GEN-LAST:event_txtNomeProdutoBuscaKeyReleased

    private int idverificador() {
        int numAtual = 1;
        Document aux;
        File diretorio = new File("Bancos de Dados");
        File arquivo = new File(diretorio, "BancoNotas.xml");
        SAXBuilder carrega = new SAXBuilder();
        if (!(arquivo.exists())) {
            root = new Element("BancoNotas");
            doc = new Document(root);
            return numAtual = 1;
        }
        try {
            aux = carrega.build(arquivo);
            root = (Element) aux.getRootElement();
            List<Element> notas = root.getChildren();
            for (int i = 0; i < notas.size(); i++) {
                Element nota = notas.get(i);
                numAtual = Integer.parseInt(nota.getAttributeValue("Numeracao"));
            }
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return numAtual + 1;
    }

    Document doc2;
    Element root2;
    String numeroDaUltimaNota = "";//Enviar Para Nota Fiscal

    public boolean criarXml() {
        File diretorio2 = new File("Bancos de Dados");
        File arquivo2 = new File(diretorio2, "BancoNotas.xml");

        String cpf = lblCpfClienteVenda.getText().replaceAll("CPF:", " ");
        String nome = lblNomeClienteVenda.getText().replaceAll("Nome: ", " ");
        String telefone = lblFoneClienteVenda.getText().replaceAll("Fone:", " ");
        String data = formatarData();
        String valorNota = txtTotalItens.getText();
        String valorPagar = txtTotalPagar.getText();
        String tipoPagamento = "";

        if (rbAvista.isSelected())
            tipoPagamento = "? Vista";
        else if (rbDebito.isSelected())
            tipoPagamento = "Dbito";
        else if (rbCredito.isSelected())
            tipoPagamento = "Crdito Parcelado de " + cbxNumParcela.getSelectedItem() + "x";

        int numeracao = 1;

        if (!(arquivo2.exists())) {
            root2 = new Element("BancoNotas");
            doc2 = new Document(root2);
            numeracao = 1;
            numeroDaUltimaNota = "" + numeracao;
        }
        /*Se existir faz uma copia do original e acrescenta novas informaes*/
        try {
            if (arquivo2.exists()) {
                SAXBuilder builder = new SAXBuilder();
                doc2 = builder.build(arquivo2);
                root2 = (Element) doc2.getRootElement();
                numeracao = idverificador();
                numeroDaUltimaNota = "" + numeracao;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        }

        Element nota = new Element("Nota");

        Attribute numer = new Attribute("Numeracao", "" + numeracao);
        nota.setAttribute(numer);
        Attribute date = new Attribute("Data", "" + data);
        nota.setAttribute(date);

        root2.addContent(nota);

        Element cliente = new Element("Cliente");

        Element name = new Element("Nome");
        name.setText(nome);

        Element fone = new Element("Telefone");
        fone.setText(telefone);

        Element cPf = new Element("CPF");
        cPf.setText(cpf);

        cliente.addContent(name);
        cliente.addContent(fone);
        cliente.addContent(cPf);

        nota.addContent(cliente);

        Element produto = new Element("Produto");

        for (int i = 0; i < tabItensVenda.getRowCount(); i++) {

            Element iten = new Element("Item");
            String auxitem = (String) tabItensVenda.getValueAt(i, 1);
            iten.setText(auxitem);

            String auxquant = (String) tabItensVenda.getValueAt(i, 3);
            Attribute qtde = new Attribute("Quantidade", "" + auxquant);
            iten.setAttribute(qtde);

            String auxprecouni = (String) tabItensVenda.getValueAt(i, 2);
            Attribute precoUni = new Attribute("PrecoUnitario", "" + auxprecouni);
            iten.setAttribute(precoUni);

            String auxtotalpecas = (String) tabItensVenda.getValueAt(i, 4);
            Attribute pecastotal = new Attribute("PrecoTotal", "" + auxtotalpecas);
            iten.setAttribute(pecastotal);

            produto.addContent(iten);
        }
        nota.addContent(produto);

        Element pagament = new Element("Pagamento");

        Element valort = new Element("ValorTotal");
        valort.setText(valorPagar);

        Element valornot = new Element("ValorTotalNota");
        valornot.setText(valorNota);

        Element modoPag = new Element("ModoPagamento");
        modoPag.setText(tipoPagamento);

        pagament.addContent(valort);
        pagament.addContent(valornot);
        pagament.addContent(modoPag);

        nota.addContent(pagament);

        try {
            XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
            OutputStream out = new FileOutputStream(new File(diretorio2, "BancoNotas.xml"));
            xout.output(doc2, out);
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return (true);
    }

    Document doc3;
    Element root3;

    private void editarQuantidadeProduto() {
        File diretorio2 = new File("Bancos de Dados");
        File arquivoProd = new File(diretorio2, "BancoProduto.xml");
        String id = txtIdVenda.getText();
        String qtde = txtQtdeVenda.getText();
        //int quantidadeLinhas = tabItensVenda.getRowCount();
        try {
            SAXBuilder builder = new SAXBuilder();
            doc3 = builder.build(arquivoProd);
            root3 = doc3.getRootElement();
            List<Element> produtos = root3.getChildren();
            for (int j = 0; j < tabItensVenda.getRowCount(); j++) {
                for (int i = 0; i < produtos.size(); i++) {
                    Element produto = produtos.get(i);
                    if (produto.getAttributeValue("id").equals(tabItensVenda.getValueAt(j, 0))) {
                        int subtrairItem = Integer.parseInt((String) tabItensVenda.getValueAt(j, 3));
                        int qtdeAtual = Integer.parseInt(produto.getChildText("qtde"));
                        int novaQtde = qtdeAtual - subtrairItem;
                        produto.getChild("qtde").setText("" + novaQtde);
                    }
                }
                XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
                OutputStream out = new FileOutputStream(new File(diretorio2, "BancoProduto.xml"));
                xout.output(doc3, out);
                out.close();
            }
        } catch (JDOMException | IOException e) {
        }
    }

    Document doc4;
    Element root4;

    public void procurarCliente(String nome) {
        File diretorio4 = new File("Bancos de Dados");
        File arquivo4 = new File(diretorio4, "BancoCLiente.xml");
        model5.setNumRows(0);
        try {
            SAXBuilder builder = new SAXBuilder();
            doc4 = builder.build(arquivo4);
            root4 = (Element) doc4.getRootElement();
            List<Element> clientes = root4.getChildren();
            String dados[] = new String[6];
            for (int i = 0; i < clientes.size(); i++) {
                Element cliente = clientes.get(i);
                dados[0] = cliente.getAttributeValue("id");
                dados[1] = cliente.getChildText("nome").toUpperCase();
                dados[2] = cliente.getChildText("sexo");
                dados[3] = cliente.getChildText("idade");
                dados[4] = cliente.getChildText("telefone");
                dados[5] = cliente.getChildText("cpf");
                if ((nome.equals("")) || //Mostar todos se forem nulls
                        (!nome.equals("") && dados[1].contains(nome))) { //Mostrar pelo nome

                    model5.addRow(new Object[] { dados[0], dados[1], dados[2], dados[3], dados[4], dados[5] });
                }

            }
        } catch (JDOMException | IOException e) {
        }
    }

    Document doc5;
    Element root5;

    public void procurarProduto(String nome) {
        File diretorio5 = new File("Bancos de Dados");
        File arquivo5 = new File(diretorio5, "BancoProduto.xml");
        model4.setNumRows(0);
        try {
            SAXBuilder builder = new SAXBuilder();
            doc5 = builder.build(arquivo5);
            root5 = (Element) doc5.getRootElement();
            List<Element> produtos = root5.getChildren();
            String dados[] = new String[4];
            for (int i = 0; i < produtos.size(); i++) {
                Element produto = produtos.get(i);
                dados[0] = produto.getAttributeValue("id");
                dados[1] = produto.getChildText("nome").toUpperCase();
                dados[2] = produto.getChild("nome").getAttributeValue("tipo");
                dados[3] = produto.getChildText("preoUnitrio");
                if (nome.equals("") || !nome.equals("") && dados[1].contains(nome)) {

                    model4.addRow(new Object[] { dados[0], dados[1], dados[2], dados[3] });
                }
            }

        } catch (JDOMException | IOException e) {
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdicionarItem;
    private javax.swing.JButton btnCancelarVenda;
    private javax.swing.JButton btnFinalizar;
    private javax.swing.JButton btnRemoverItem;
    private javax.swing.JButton btnRemoverTodosItens;
    private javax.swing.JComboBox cbxClienteVenda;
    private javax.swing.JComboBox cbxNumParcela;
    private javax.swing.ButtonGroup grupoPagamento;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblClienteVenda;
    private javax.swing.JLabel lblCpfClienteVenda;
    private javax.swing.JLabel lblData;
    private javax.swing.JLabel lblFoneClienteVenda;
    private javax.swing.JLabel lblIdClienteVenda;
    private javax.swing.JLabel lblIdVenda;
    private javax.swing.JLabel lblIdadeClienteVenda;
    private javax.swing.JLabel lblNomeClienteBusca;
    private javax.swing.JLabel lblNomeClienteVenda;
    private javax.swing.JLabel lblNomeProdutoBusca;
    private javax.swing.JLabel lblNomeProdutoVenda;
    private javax.swing.JLabel lblNumeroParcela;
    private javax.swing.JLabel lblPrecoUnit;
    private javax.swing.JLabel lblQtdeVenda;
    private javax.swing.JLabel lblQuantParcela;
    private javax.swing.JLabel lblSexoClienteVenda;
    private javax.swing.JLabel lblTotalItens;
    private javax.swing.JLabel lblTotalPagar;
    private javax.swing.JLabel lblVenda;
    private javax.swing.JLabel lblX;
    private javax.swing.JPanel pnlBusca;
    private javax.swing.JPanel pnlControle;
    private javax.swing.JRadioButton rbAvista;
    private javax.swing.JRadioButton rbCredito;
    private javax.swing.JRadioButton rbDebito;
    private javax.swing.JSpinner spnData;
    private javax.swing.JTable tabClienteBusca;
    private javax.swing.JTabbedPane tabGuiasVenda;
    private javax.swing.JTable tabItensVenda;
    private javax.swing.JTable tabProdutoBusca;
    private javax.swing.JTextField txtIdVenda;
    private javax.swing.JTextField txtNomeClienteBusca;
    private javax.swing.JTextField txtNomeProdutoBusca;
    private javax.swing.JTextField txtNomeProdutoVenda;
    private javax.swing.JTextField txtPrecoUnit;
    private javax.swing.JTextField txtQtdeVenda;
    private javax.swing.JTextField txtTotalItens;
    private javax.swing.JTextField txtTotalPagar;
    // End of variables declaration//GEN-END:variables

    private DefaultTableModel model3 = new DefaultTableModel(new Object[][] {},
            new String[] { "ID", "Nome", "Preo Unit", "Qtde", "Total" }) {

        public boolean isCellEditable(int row, int col) {
            return false;
        }
    };
    private DefaultTableModel model4 = new DefaultTableModel(new Object[][] {},
            new String[] { "ID", "Nome", "Tipo", "Preo Unit" }) {

        public boolean isCellEditable(int row, int col) {
            return false;
        }
    };
    private DefaultTableModel model5 = new DefaultTableModel(new Object[][] {},
            new String[] { "ID", "Nome", "Sexo", "Idade", "Fone", "CPF" }) {

        public boolean isCellEditable(int row, int col) {
            return false;
        }
    };
}