sistemaVendas.CancelarVenda.java Source code

Java tutorial

Introduction

Here is the source code for sistemaVendas.CancelarVenda.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.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.table.DefaultTableModel;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;

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

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

    public CancelarVenda(JDesktopPane tela) {
        telas = tela;
        initComponents();
        model6.setNumRows(0);
        tabBuscaVendas.setModel(model6);
        spnDataCancelarVenda.setEditor(new JSpinner.DateEditor(spnDataCancelarVenda, "dd/MM/yyyy"));
        preencherComboBox();
    }

    /**
     * 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() {

        lblCancelarVenda = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabBuscaVendas = new javax.swing.JTable();
        lblBuscarVenda = new javax.swing.JLabel();
        lblNumeroNota = new javax.swing.JLabel();
        lblDataCancelarVenda = new javax.swing.JLabel();
        lblClientesCancelarVenda = new javax.swing.JLabel();
        cbxClientesCancelarVenda = new javax.swing.JComboBox();
        spnDataCancelarVenda = new javax.swing.JSpinner();
        txtNumeroNota = new javax.swing.JTextField();
        btnCancelarCancelamentoVenda = new javax.swing.JButton();
        btnDeletarVenda = new javax.swing.JButton();

        setClosable(true);
        setTitle("Cancelamento de Vendas");

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

        tabBuscaVendas
                .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" }));
        tabBuscaVendas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabBuscaVendasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabBuscaVendas);

        lblBuscarVenda.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblBuscarVenda.setText("Buscar--->");

        lblNumeroNota.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblNumeroNota.setText("Nmero Da Nota:");

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

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

        cbxClientesCancelarVenda
                .setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-----Selecione Cliente----" }));
        cbxClientesCancelarVenda.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbxClientesCancelarVendaItemStateChanged(evt);
            }
        });
        cbxClientesCancelarVenda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxClientesCancelarVendaActionPerformed(evt);
            }
        });

        spnDataCancelarVenda.setModel(new javax.swing.SpinnerDateModel());
        spnDataCancelarVenda.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spnDataCancelarVendaStateChanged(evt);
            }
        });

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

        btnCancelarCancelamentoVenda.setText("Cancelar");
        btnCancelarCancelamentoVenda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarCancelamentoVendaActionPerformed(evt);
            }
        });

        btnDeletarVenda.setText("Deletar Venda");
        btnDeletarVenda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeletarVendaActionPerformed(evt);
            }
        });

        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(239, 239, 239).addComponent(
                                        lblCancelarVenda, javax.swing.GroupLayout.PREFERRED_SIZE, 193,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup().addContainerGap()
                                        .addComponent(lblBuscarVenda))
                                .addGroup(layout
                                        .createSequentialGroup().addGap(291, 291, 291)
                                        .addComponent(btnDeletarVenda)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(47, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                                javax.swing.GroupLayout.Alignment.TRAILING,
                                layout.createSequentialGroup().addComponent(lblNumeroNota)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtNumeroNota, javax.swing.GroupLayout.PREFERRED_SIZE, 83,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(42, 42, 42).addComponent(lblDataCancelarVenda)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(spnDataCancelarVenda, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(45, 45, 45).addComponent(lblClientesCancelarVenda)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cbxClientesCancelarVenda,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 143,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(42, 42, 42))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        layout.createSequentialGroup().addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(btnCancelarCancelamentoVenda)
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        558, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGap(63, 63, 63)))));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(lblCancelarVenda)
                                .addGap(19, 19, 19).addComponent(lblBuscarVenda)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                                        .addComponent(lblNumeroNota)
                                        .addComponent(txtNumeroNota, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblDataCancelarVenda)
                                        .addComponent(spnDataCancelarVenda, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblClientesCancelarVenda).addComponent(
                                                cbxClientesCancelarVenda, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(49, 49, 49)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 182,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnDeletarVenda)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40,
                                        Short.MAX_VALUE)
                                .addComponent(btnCancelarCancelamentoVenda).addContainerGap()));

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

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

    private void txtNumeroNotaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNumeroNotaKeyReleased
        procurar(txtNumeroNota.getText(), "", "");
    }//GEN-LAST:event_txtNumeroNotaKeyReleased

    private void spnDataCancelarVendaStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spnDataCancelarVendaStateChanged
        String aux = formatarData();
        procurar("", aux, "");
    }//GEN-LAST:event_spnDataCancelarVendaStateChanged

    Document doc7;
    Element root7;

    private void cbxClientesCancelarVendaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbxClientesCancelarVendaItemStateChanged
        if (cbxClientesCancelarVenda.getSelectedItem().equals("--Selecionar Cliente--")) {
            model6.setNumRows(0);
        }
        /*Faz a Busca e modifica as Labels*/
        File diretorio = new File("Bancos de Dados");
        File arquivo = new File(diretorio, "BancoNotas.xml");
        try {
            SAXBuilder builder = new SAXBuilder();
            doc7 = builder.build(arquivo);
            root7 = (Element) doc7.getRootElement();
            List<Element> notas = root7.getChildren();
            for (int i = 0; i < notas.size(); i++) {
                Element nota = notas.get(i);
                if (nota.getChild("Cliente").getChildText("Nome")
                        .equals((String) cbxClientesCancelarVenda.getSelectedItem())) {
                    String aux = (String) cbxClientesCancelarVenda.getSelectedItem();
                    procurar("", "", aux);
                }
            }
        } catch (JDOMException | IOException e) {
        }
    }//GEN-LAST:event_cbxClientesCancelarVendaItemStateChanged

    private void cbxClientesCancelarVendaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxClientesCancelarVendaActionPerformed
        String aux = (String) cbxClientesCancelarVenda.getSelectedItem();
        procurar("", "", aux);
    }//GEN-LAST:event_cbxClientesCancelarVendaActionPerformed

    int linhatabela;

    private void tabBuscaVendasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabBuscaVendasMouseClicked
        linhatabela = tabBuscaVendas.getSelectedRow();
    }//GEN-LAST:event_tabBuscaVendasMouseClicked

    Document doc9;
    Element root9;

    private void btnDeletarVendaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeletarVendaActionPerformed
        File diretorio = new File("Bancos de Dados");
        File arquivo = new File(diretorio, "BancoNotas.xml");
        String id = (String) tabBuscaVendas.getValueAt(linhatabela, 0);
        try {
            SAXBuilder builder = new SAXBuilder();
            doc9 = builder.build(arquivo);
            root9 = (Element) doc9.getRootElement();
            List<Element> notas = root9.getChildren();
            for (int i = 0; i < notas.size(); i++) {
                Element nota = notas.get(i);
                if (nota.getAttributeValue("Numeracao").equals(id)) {
                    nota.getParent().removeContent(nota);
                }
            }
            XMLOutputter xout = new XMLOutputter();
            OutputStream out = new FileOutputStream(new File(diretorio, "BancoNotas.xml"));
            xout.output(doc9, out);
            out.close();
        } catch (JDOMException | IOException e) {
            e.printStackTrace();
        }
        JOptionPane.showMessageDialog(null, "VENDA CANCELADA COM SUCESSO!", "Sucesso",
                JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_btnDeletarVendaActionPerformed

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

    Document doc6;
    Element root6;

    public void procurar(String idnota, String data, String nomecliente) {
        model6.setNumRows(0);
        // String totalcompra;
        try {
            File diretorio = new File("Bancos de Dados");
            File arquivo = new File(diretorio, "BancoNotas.xml");
            SAXBuilder builder = new SAXBuilder();
            doc6 = builder.build(arquivo);
            root6 = (Element) doc6.getRootElement();
            List<Element> notas = root6.getChildren();
            String dados[] = new String[4];
            for (int i = 0; i < notas.size(); i++) {
                Element nota = notas.get(i);
                dados[0] = nota.getAttributeValue("Numeracao");
                dados[1] = nota.getAttributeValue("Data");
                dados[2] = nota.getChild("Cliente").getChildText("Nome");
                dados[3] = nota.getChild("Pagamento").getChildText("ValorTotal");
                if (idnota.equals("") && nomecliente.equals("") && data.equals("") || //Mostar todos se forem nulls
                        !idnota.equals("") && !nomecliente.equals("") && !data.equals("") && dados[0].equals(idnota)
                                && dados[2].contains(nomecliente) && dados[1].contains(data)
                        || //Mostrar pelos 3 campos juntos
                        !idnota.equals("") && !nomecliente.equals("") && dados[0].equals(idnota)
                                && dados[2].contains(nomecliente)
                        || //Mostrar pelo id e nome
                        !nomecliente.equals("") && !data.equals("") && dados[2].contains(nomecliente)
                                && dados[1].contains(data)
                        || //Mostrar pelo nome e data
                        !idnota.equals("") && dados[0].equals(idnota) || //Mostrar pelo id
                        !nomecliente.equals("") && dados[2].contains(nomecliente) || //Mostrar pelo nome
                        !data.equals("") && dados[1].contains(data)) { //Mostrar pelo cpf                    

                    model6.addRow(new Object[] { dados[0], dados[1], dados[2], dados[3] });
                }
            }
        } catch (JDOMException | IOException e) {
        }
    }

    Document doc8;
    Element root8;

    public void preencherComboBox() {
        File diretorio = new File("Bancos de Dados");
        File bancoTipos = new File(diretorio, "BancoCLiente.xml");
        try {
            SAXBuilder builder = new SAXBuilder();
            doc8 = builder.build(bancoTipos);
            root8 = (Element) doc8.getRootElement();
            List<Element> clientes = root8.getChildren();
            for (int i = 0; i < clientes.size(); i++) {
                Element cliente = clientes.get(i);
                cbxClientesCancelarVenda.addItem(cliente.getChildText("nome"));
            }
        } catch (JDOMException | IOException e) {
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelarCancelamentoVenda;
    private javax.swing.JButton btnDeletarVenda;
    private javax.swing.JComboBox cbxClientesCancelarVenda;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblBuscarVenda;
    private javax.swing.JLabel lblCancelarVenda;
    private javax.swing.JLabel lblClientesCancelarVenda;
    private javax.swing.JLabel lblDataCancelarVenda;
    private javax.swing.JLabel lblNumeroNota;
    private javax.swing.JSpinner spnDataCancelarVenda;
    private javax.swing.JTable tabBuscaVendas;
    private javax.swing.JTextField txtNumeroNota;
    // End of variables declaration//GEN-END:variables

    private DefaultTableModel model6 = new DefaultTableModel(new Object[][] {},
            new String[] { "Numero da Nota", "Data", "Cliente", "Total da Compra" }) {

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