vista.ventas.DiagOrdenesDeCompra.java Source code

Java tutorial

Introduction

Here is the source code for vista.ventas.DiagOrdenesDeCompra.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 vista.ventas;

import entidades.Sucursal;
import entidades.articulo.Articulo;
import entidades.articulo.PrecioArticulo;
import entidades.cliente.Cliente;
import entidades.cliente.Organismo;
import entidades.cliente.Persona;
import entidades.usuario.Usuario;
import entidades.venta.OrdenDeCompra;
import entidades.venta.OrdenDeCompraArticulo;
import facade.ArticuloFacade;
import facade.ClienteFacade;
import facade.OrdenDeCompraFacade;
import facade.PrecioArticuloFacade;
import includes.Comunes;
import includes.ModeloTablaNoEditable;
import includes.SuperDialog;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import org.apache.commons.validator.routines.BigDecimalValidator;
import org.apache.commons.validator.routines.BigIntegerValidator;
import vista.articulos.DiagBuscarArticulo;

/**
 *
 * @author franco
 */
public class DiagOrdenesDeCompra extends SuperDialog {

    private Cliente cliente;
    private Articulo articulo;
    private PrecioArticulo precioArticulo;
    private List<OrdenDeCompraArticulo> listaArticulos = new ArrayList<>();
    private OrdenDeCompraArticulo ordenDeCompraArticulo;
    private ModeloTablaNoEditable modeloTablaArticulos;
    private BigDecimal subTotal = new BigDecimal(0.0);
    private BigDecimal total = new BigDecimal(0.0);
    private OrdenDeCompra ordenDeCompra;
    private Usuario usuario;
    private Sucursal sucursal;
    private String tipoOperacion;
    private boolean modificacionOrden;
    private int selectedRow;

    /**
     * Creates new form DiagOrdenesDeCompra
     */
    public DiagOrdenesDeCompra(java.awt.Frame parent, boolean modal, Usuario usuario, Sucursal sucursal,
            String tipoOperacion) {
        super(parent, modal);
        this.usuario = usuario;
        this.sucursal = sucursal;
        this.tipoOperacion = tipoOperacion;
        initComponents();
        inicializarComponentes();

    }

    public DiagOrdenesDeCompra(java.awt.Frame parent, boolean modal, Usuario usuario, Sucursal sucursal,
            String tipoOperacion, OrdenDeCompra ordenDeCompra) {
        super(parent, modal);
        initComponents();
        this.usuario = usuario;
        this.sucursal = sucursal;
        this.tipoOperacion = tipoOperacion;
        this.ordenDeCompra = ordenDeCompra;
        inicializarComponentes();
    }

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

        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        ftfDocumento = new javax.swing.JFormattedTextField();
        jButton1 = new javax.swing.JButton();
        tfCliente = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        tfDescripcion = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        tfPrecioPactado = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        tfCantidad = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        tfSubTotalArticulo = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        tfCodigo = new javax.swing.JTextField();
        tfPrecioNormal = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblArticulos = new javax.swing.JTable();
        btnCancelarModificacion = new javax.swing.JButton();
        btnConfirmarPesada = new javax.swing.JButton();
        jLabel13 = new javax.swing.JLabel();
        tfTotal = new javax.swing.JTextField();
        btnEmitirTicket = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        tfNumero = new javax.swing.JTextField();
        jXDatePicker1 = new org.jdesktop.swingx.JXDatePicker();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle
                .getMessage(DiagOrdenesDeCompra.class, "DiagOrdenesDeCompra.jPanel2.border.title"))); // NOI18N

        jLabel3.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        jLabel3.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.jLabel3.text")); // NOI18N

        ftfDocumento.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(
                new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("########"))));
        ftfDocumento.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.ftfDocumento.text")); // NOI18N
        ftfDocumento.setFont(new java.awt.Font("Dialog", 0, 16)); // NOI18N
        ftfDocumento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ftfDocumentoActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/buscar2.png"))); // NOI18N
        jButton1.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.jButton1.text")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        tfCliente.setEditable(false);
        tfCliente.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
        tfCliente.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.tfCliente.text")); // NOI18N

        jLabel4.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        jLabel4.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.jLabel4.text")); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ftfDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, 160,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 46,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tfCliente).addContainerGap()));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jPanel2Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(tfCliente, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel4))
                        .addComponent(ftfDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, 34,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 34,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3)).addContainerGap(17, Short.MAX_VALUE)));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle
                .getMessage(DiagOrdenesDeCompra.class, "DiagOrdenesDeCompra.jPanel3.border.title"))); // NOI18N

        jLabel5.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        jLabel5.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.jLabel5.text")); // NOI18N

        tfDescripcion.setEditable(false);
        tfDescripcion.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
        tfDescripcion.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.tfDescripcion.text")); // NOI18N

        jLabel6.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        jLabel6.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.jLabel6.text")); // NOI18N

        tfPrecioPactado.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
        tfPrecioPactado.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.tfPrecioPactado.text")); // NOI18N
        tfPrecioPactado.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                tfPrecioPactadoFocusGained(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        jLabel7.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.jLabel7.text")); // NOI18N

        tfCantidad.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
        tfCantidad.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.tfCantidad.text")); // NOI18N
        tfCantidad.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfCantidadFocusLost(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        jLabel8.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.jLabel8.text")); // NOI18N

        tfSubTotalArticulo.setEditable(false);
        tfSubTotalArticulo.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
        tfSubTotalArticulo.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.tfSubTotalArticulo.text")); // NOI18N
        tfSubTotalArticulo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfSubTotalArticuloActionPerformed(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        jLabel9.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.jLabel9.text")); // NOI18N

        tfCodigo.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
        tfCodigo.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.tfCodigo.text")); // NOI18N
        tfCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfCodigoActionPerformed(evt);
            }
        });
        tfCodigo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                tfCodigoFocusGained(evt);
            }
        });

        tfPrecioNormal.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
        tfPrecioNormal.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.tfPrecioNormal.text")); // NOI18N
        tfPrecioNormal.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                tfPrecioNormalFocusGained(evt);
            }
        });

        jLabel12.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        jLabel12.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.jLabel12.text")); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(jPanel3Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel5).addComponent(tfCodigo,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 80,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel6).addComponent(tfDescripcion,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 237,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(tfPrecioNormal).addComponent(jLabel12,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(jPanel3Layout.createSequentialGroup().addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel8))
                                .addGroup(jPanel3Layout.createSequentialGroup().addComponent(tfPrecioPactado)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(tfCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 80,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel9).addComponent(tfSubTotalArticulo,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 94,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap()));
        jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap(13, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel5).addComponent(jLabel6).addComponent(jLabel7)
                                .addComponent(jLabel8).addComponent(jLabel9).addComponent(jLabel12))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(tfDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(tfPrecioPactado, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(tfCantidad, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(tfSubTotalArticulo, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(tfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(tfPrecioNormal, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))));

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle
                .getMessage(DiagOrdenesDeCompra.class, "DiagOrdenesDeCompra.jPanel4.border.title"))); // NOI18N

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

        btnCancelarModificacion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/back.png"))); // NOI18N
        btnCancelarModificacion.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.btnCancelarModificacion.text")); // NOI18N
        btnCancelarModificacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarModificacionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout
                .setHorizontalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel4Layout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 715,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnCancelarModificacion, javax.swing.GroupLayout.DEFAULT_SIZE, 135,
                                        Short.MAX_VALUE)));
        jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                        jPanel4Layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 85,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(317, 317, 317))
                .addGroup(jPanel4Layout.createSequentialGroup().addGap(37, 37, 37)
                        .addComponent(btnCancelarModificacion, javax.swing.GroupLayout.PREFERRED_SIZE, 45,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        btnConfirmarPesada.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/add.png"))); // NOI18N
        btnConfirmarPesada.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.btnConfirmarPesada.text")); // NOI18N
        btnConfirmarPesada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmarPesadaActionPerformed(evt);
            }
        });

        jLabel13.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        jLabel13.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.jLabel13.text")); // NOI18N

        tfTotal.setEditable(false);
        tfTotal.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
        tfTotal.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.tfTotal.text")); // NOI18N

        btnEmitirTicket.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/accept.png"))); // NOI18N
        btnEmitirTicket.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.btnEmitirTicket.text")); // NOI18N
        btnEmitirTicket.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEmitirTicketActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/back.png"))); // NOI18N
        jButton2.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.jButton2.text")); // NOI18N
        jButton2.setAlignmentX(0.5F);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        jLabel10.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.jLabel10.text")); // NOI18N

        jLabel11.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        jLabel11.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.jLabel11.text")); // NOI18N

        tfNumero.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
        tfNumero.setText(org.openide.util.NbBundle.getMessage(DiagOrdenesDeCompra.class,
                "DiagOrdenesDeCompra.tfNumero.text")); // NOI18N

        jXDatePicker1.setFormats("dd/MM/yyyy");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(btnConfirmarPesada,
                                                javax.swing.GroupLayout.Alignment.TRAILING,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 144,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout
                                                .createSequentialGroup().addComponent(jLabel13).addGap(12, 12, 12)
                                                .addComponent(tfTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 115,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(12, 12, 12).addComponent(btnEmitirTicket,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 144,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGroup(layout.createSequentialGroup().addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 80,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(104, 104, 104).addComponent(jLabel11).addGap(18, 18, 18)
                                .addComponent(jXDatePicker1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap())
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addGap(300, 300, 300)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 171,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jXDatePicker1, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel10).addComponent(jLabel11))
                                .addComponent(tfNumero, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 117,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnConfirmarPesada, javax.swing.GroupLayout.PREFERRED_SIZE, 37,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(tfTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 39,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnEmitirTicket, javax.swing.GroupLayout.PREFERRED_SIZE, 39,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup().addGap(10, 10, 10).addComponent(jLabel13)))
                        .addContainerGap(102, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
                        .createSequentialGroup().addContainerGap(481, Short.MAX_VALUE).addComponent(jButton2,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())));

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

    private void ftfDocumentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ftfDocumentoActionPerformed
        buscarClientePorDNI();
    }//GEN-LAST:event_ftfDocumentoActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        buscarCliente();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void tfCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfCodigoActionPerformed
        buscarArticulo();
    }//GEN-LAST:event_tfCodigoActionPerformed

    private void btnConfirmarPesadaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmarPesadaActionPerformed
        agregarArticulo();
        calcularTotal();
        tfCodigo.requestFocus();
    }//GEN-LAST:event_btnConfirmarPesadaActionPerformed

    private void btnEmitirTicketActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEmitirTicketActionPerformed
        realizarVenta();
    }//GEN-LAST:event_btnEmitirTicketActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        cerrar();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void tfSubTotalArticuloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfSubTotalArticuloActionPerformed
        btnConfirmarPesada.requestFocus();
    }//GEN-LAST:event_tfSubTotalArticuloActionPerformed

    private void tfPrecioPactadoFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfPrecioPactadoFocusGained
        tfPrecioPactado.selectAll();
    }//GEN-LAST:event_tfPrecioPactadoFocusGained

    private void tfCodigoFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfCodigoFocusGained
        tfCodigo.selectAll();

    }//GEN-LAST:event_tfCodigoFocusGained

    private void tfCantidadFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfCantidadFocusLost
        calcularSubtotalItem();
    }//GEN-LAST:event_tfCantidadFocusLost

    private void tfPrecioNormalFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfPrecioNormalFocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_tfPrecioNormalFocusGained

    private void tblArticulosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblArticulosMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() == 2) {
            seleccionarArticulo();
        }

    }//GEN-LAST:event_tblArticulosMouseClicked

    private void btnCancelarModificacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarModificacionActionPerformed
        // TODO add your handling code here:
        limpiarModificacion();
    }//GEN-LAST:event_btnCancelarModificacionActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelarModificacion;
    private javax.swing.JButton btnConfirmarPesada;
    private javax.swing.JButton btnEmitirTicket;
    private javax.swing.JFormattedTextField ftfDocumento;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane2;
    private org.jdesktop.swingx.JXDatePicker jXDatePicker1;
    private javax.swing.JTable tblArticulos;
    private javax.swing.JTextField tfCantidad;
    private javax.swing.JTextField tfCliente;
    private javax.swing.JTextField tfCodigo;
    private javax.swing.JTextField tfDescripcion;
    private javax.swing.JTextField tfNumero;
    private javax.swing.JTextField tfPrecioNormal;
    private javax.swing.JTextField tfPrecioPactado;
    private javax.swing.JTextField tfSubTotalArticulo;
    private javax.swing.JTextField tfTotal;
    // End of variables declaration//GEN-END:variables

    private void inicializarComponentes() {
        eventosDeTeclas();
        btnCancelarModificacion.setEnabled(false);
        modificacionOrden = false;
        jXDatePicker1.setDate(Comunes.obtenerFechaActualDesdeDB());
        botonesMultilinea();
        //maximizamos frame
        // this.setSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize()));
        //configuramos tabla
        modeloTablaArticulos = new ModeloTablaNoEditable();
        cargarEncabezadosTablaArticulos(modeloTablaArticulos);
        configurarTabla(tblArticulos);
        total = BigDecimal.ZERO;
        if (tipoOperacion.equals("Modificacin")) {
            cargarOrdenDeCompra();
        }
    }

    private void agregarArticulo() {

        if (validarArticulo()) {

            if (articulo != null) {
                if (!modificacionOrden) {
                    //ALTA DE ARTICULO EN LA LISTA
                    ordenDeCompraArticulo = new OrdenDeCompraArticulo();
                    ordenDeCompraArticulo.setArticulo(articulo);
                    //segun sea por peso o cantidad la venta
                    if (!tfCantidad.getText().isEmpty()
                            && BigDecimalValidator.getInstance().isValid(tfCantidad.getText())) {
                        ordenDeCompraArticulo
                                .setCantidadPeso(BigDecimal.valueOf(Double.parseDouble(tfCantidad.getText())));
                    } else {
                        tfCantidad.requestFocus();
                    }
                    ordenDeCompraArticulo.setPrecio(new BigDecimal(tfPrecioPactado.getText()));
                    //Lista para cargar tabla
                    listaArticulos.add(ordenDeCompraArticulo);
                    cargarTablaArticulos(listaArticulos);
                    //                cacularSubtotal();
                    //                calcularTotal();
                    limpiarCamposArticulo();
                    tfCodigo.requestFocus();
                } else {
                    //MODIFICACION DE ARTICULO DE LA LISTA
                    btnCancelarModificacion.setEnabled(false);
                    modificacionOrden = false;
                    //segun sea por peso o cantidad la venta
                    if (!tfCantidad.getText().isEmpty()
                            && BigDecimalValidator.getInstance().isValid(tfCantidad.getText())) {
                        ordenDeCompraArticulo
                                .setCantidadPeso(BigDecimal.valueOf(Double.parseDouble(tfCantidad.getText())));
                    } else {
                        tfCantidad.requestFocus();
                    }
                    ordenDeCompraArticulo.setPrecio(new BigDecimal(tfPrecioPactado.getText()));
                    //Lista para cargar tabla
                    ((ModeloTablaNoEditable) tblArticulos.getModel()).removeRow(selectedRow);
                    listaArticulos.set(selectedRow, ordenDeCompraArticulo);
                    cargarTablaArticulos(listaArticulos);
                    limpiarCamposArticulo();
                    tfCodigo.requestFocus();
                }

            }

        }
    }

    private boolean validarArticulo() {
        if (cliente == null) {
            JOptionPane.showMessageDialog(null, "Debe seleccionar un cliente", "Mensaje",
                    JOptionPane.ERROR_MESSAGE);
            ftfDocumento.requestFocus();
            return false;
        }
        if (!BigDecimalValidator.getInstance().isValid(tfPrecioPactado.getText())) {
            JOptionPane.showMessageDialog(null, "Ingrese el precio correctamente", "Mensaje",
                    JOptionPane.ERROR_MESSAGE);
            tfPrecioPactado.requestFocus();
            return false;
        }
        if (articulo == null) {
            JOptionPane.showMessageDialog(null, "Debe seleccionar un articulo", "Mensaje",
                    JOptionPane.ERROR_MESSAGE);
            tfCodigo.requestFocus();
            return false;
        }
        if (articulo.getId() != null) {
            if (!articulo.getUnidadMedida().isEsPeso()) {
                if (tfCantidad.getText().isEmpty()) {
                    JOptionPane.showMessageDialog(null, "Debe seleccionar cantidad y presionar Enter", "Mensaje",
                            JOptionPane.ERROR_MESSAGE);
                    tfCantidad.requestFocus();
                    return false;

                }
            }
        }
        if (tfSubTotalArticulo.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Debe seleccionar un articulo", "Mensaje",
                    JOptionPane.ERROR_MESSAGE);
            tfCodigo.requestFocus();
            return false;

        }

        return true;
    }

    private void cargarTablaArticulos(List<OrdenDeCompraArticulo> listaVentaArticulos) {
        modeloTablaArticulos = new ModeloTablaNoEditable();
        cargarEncabezadosTablaArticulos(modeloTablaArticulos);
        configurarTabla(tblArticulos);
        try {
            cargarArticulos(listaVentaArticulos);
        } catch (Exception ex) {
            Logger.getLogger(FrVentas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void cargarEncabezadosTablaArticulos(ModeloTablaNoEditable modeloTablaArticulos) {
        modeloTablaArticulos.addColumn("Id");
        modeloTablaArticulos.addColumn("Codigo");
        modeloTablaArticulos.addColumn("Articulo");
        modeloTablaArticulos.addColumn("Peso Neto/Cantidad");
        modeloTablaArticulos.addColumn("Precio pactado");
        modeloTablaArticulos.addColumn("SubTotal");
        tblArticulos.setModel(modeloTablaArticulos);
    }

    private void configurarTabla(JTable tbl) {
        JViewport scroll = (JViewport) tbl.getParent();
        int ancho = scroll.getWidth();
        int anchoColumna = 0;
        TableColumnModel modeloColumna = tbl.getColumnModel();
        TableColumn columnaTabla;
        for (int i = 0; i < tbl.getColumnCount(); i++) {
            columnaTabla = modeloColumna.getColumn(i);
            switch (i) {
            case 0:
                anchoColumna = (1 * ancho) / 100;
                break;
            case 1:
                anchoColumna = (20 * ancho) / 100;
                break;
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
            case 11:
            case 12:
                anchoColumna = (5 * ancho) / 100;
                break;
            }
            columnaTabla.setPreferredWidth(anchoColumna);
            tbl.setColumnModel(modeloColumna);
        }
        tbl.getTableHeader().setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 10));
        tbl.getTableHeader().setBackground(java.awt.Color.WHITE);
        tbl.getTableHeader().setForeground(Color.BLACK);
        tbl.getTableHeader().setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 18));
        //Si le queremos cambiar el tamao a la tablita
        tbl.setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 18));
    }

    private void cargarArticulos(List<OrdenDeCompraArticulo> listaVentaArticulos) {
        try {
            modeloTablaArticulos = new ModeloTablaNoEditable();
            cargarEncabezadosTablaArticulos(modeloTablaArticulos);
            for (OrdenDeCompraArticulo ventaArt : listaVentaArticulos) {
                cargarArticulos(ventaArt);
            }

            tblArticulos.setModel(modeloTablaArticulos);
            Comunes.setOcultarColumnasJTable(tblArticulos, 0);
        } catch (Exception ex) {
            Logger.getLogger(FrVentas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void cargarArticulos(OrdenDeCompraArticulo ventaArticulo) {

        Object[] fila = new Object[6];
        fila[0] = ventaArticulo.getId();
        fila[1] = ventaArticulo.getArticulo().getCodigoBarra();
        fila[2] = ventaArticulo.getArticulo().getDescripcion();
        fila[3] = ventaArticulo.getCantidadPeso();
        fila[4] = ventaArticulo.getPrecio();
        fila[5] = ventaArticulo.getCantidadPeso().multiply(ventaArticulo.getPrecio());
        modeloTablaArticulos.addRow(fila);
    }

    private void calcularTotal() {
        //        subTotal = BigDecimal.ZERO;
        //        for (OrdenDeCompraArticulo va : listaArticulos) {
        //            subTotal = subTotal.add(va.getPrecio());
        //        }
        ////        tfSubtotalGral.setText(subTotal.toString());
        //        tfTotal.setText(subTotal.toString());
        subTotal = BigDecimal.ZERO;
        for (OrdenDeCompraArticulo va : listaArticulos) {
            subTotal = subTotal.add(va.getPrecio().multiply(va.getCantidadPeso()));
        }
        //        subTotal = precioArticulo.getPrecio().multiply(BigDecimal.valueOf(Double.parseDouble(tfCantidad.getText())));
        total = subTotal;
        tfTotal.setText(total.toString());
    }

    //    private void cacularSubtotal2(){
    //     subTotal  = BigDecimal.ZERO;
    //     subTotal = precioArticulo.getPrecio().multiply(BigDecimal.valueOf(Double.parseDouble(tfCantidad.getText())));
    //     tfSubtotalGral.setText(subTotal.toString());
    //    }
    //    
    //    private void calcularTotal() {
    //        total.add(subTotal);
    //        tfTotal.setText(total.toString());
    //    }
    private void eventosDeTeclas() {
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.addKeyEventDispatcher(new KeyEventDispatcher() {

            public boolean dispatchKeyEvent(KeyEvent e) {
                if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow().getClass()
                        .getSimpleName().equals("DiagOrdenesDeCompra")) {
                    if (e.getID() == KeyEvent.KEY_PRESSED) {
                        if (e.getKeyCode() == KeyEvent.VK_F3) {
                            agregarArticulo();
                            calcularTotal();
                        }
                        if (e.getKeyCode() == KeyEvent.VK_F7) {
                            realizarVenta();
                        }
                        if (e.getKeyCode() == KeyEvent.VK_F4) {
                            cerrar();
                        }
                        if (e.getKeyCode() == KeyEvent.VK_DELETE && tblArticulos.getSelectedRow() != -1) {
                            quitarArticuloLista();
                        }

                    }
                }

                return false;
            }
        });
    }

    private void cerrar() {
        this.dispose();
    }

    private void limpiarCamposArticulo() {
        tfDescripcion.setText("");
        tfPrecioNormal.setText("");
        tfPrecioPactado.setText("");
        tfCantidad.setText("");
        tfSubTotalArticulo.setText("");
        ordenDeCompraArticulo = null;

    }

    private void botonesMultilinea() {
        //botones multilinea
        String htmlButton = "<html><div align=\"center\">Confirmar Item</div></html>";
        btnConfirmarPesada.setText(htmlButton);
        String htmlButton2 = "<html><div align=\"center\">Confirmar Orden</div></html>";
        btnEmitirTicket.setText(htmlButton2);
        String htmlButton3 = "<html><div align=\"center\">Cancelar Modificacin</div></html>";
        btnCancelarModificacion.setText(htmlButton3);
        //
    }

    private void realizarVenta() {
        if (validar()) {
            switch (tipoOperacion) {
            case "Alta": {
                ordenDeCompra = new OrdenDeCompra();
                ordenDeCompra.setFecha(jXDatePicker1.getDate());
                ordenDeCompra.setMonto(BigDecimal.valueOf(Double.valueOf(tfTotal.getText())));
                ordenDeCompra.setOrdenDeCompraArticulos(listaArticulos);
                ordenDeCompra.setNumeroOrden(tfNumero.getText());
                ordenDeCompra.setCliente(cliente);
                ordenDeCompra.setUsuario(usuario);
                ordenDeCompra.setSucursal(sucursal);
                OrdenDeCompraFacade.getInstance().alta(ordenDeCompra);
                JOptionPane.showMessageDialog(null, "Orden de Compra realizada!");
                limpiarCamposArticulo();
                limpiarValores();
                tfTotal.setText("");
                tfNumero.requestFocus();
                break;
            }
            case "Modificacin": {
                ordenDeCompra.setFecha(jXDatePicker1.getDate());
                ordenDeCompra.setMonto(BigDecimal.valueOf(Double.valueOf(tfTotal.getText())));
                ordenDeCompra.setOrdenDeCompraArticulos(listaArticulos);
                ordenDeCompra.setNumeroOrden(tfNumero.getText());
                ordenDeCompra.setCliente(cliente);
                ordenDeCompra.setUsuario(usuario);
                ordenDeCompra.setSucursal(sucursal);
                OrdenDeCompraFacade.getInstance().modificar(ordenDeCompra);
                JOptionPane.showMessageDialog(null, "Orden de Compra modificada!");
                limpiarCamposArticulo();
                limpiarValores();
                tfTotal.setText("");
                tfNumero.requestFocus();
                this.dispose();
                break;
            }
            }
        }
    }

    private boolean validar() {
        //LO COMENTO PORQUE PUEDE INGRESAR CUALQUIER CARACTER
        //        if (!BigIntegerValidator.getInstance().isValid(tfNumero.getText())) {
        //            JOptionPane.showMessageDialog(null, "Ingrese nmero de orden de compra");
        //            tfNumero.requestFocus();
        //            return false;
        //        }
        //        if (!OrdenDeCompraFacade.getInstance().existeOrdenDeCompraNumeroTicket(tfNumero.getText())) {
        //            JOptionPane.showMessageDialog(null, "Ya existe ese nmero de orden de compra");
        //            tfNumero.requestFocus();
        //            return false;
        //        }
        if (cliente == null) {
            JOptionPane.showMessageDialog(null, "Debe seleccionar un cliente");
            ftfDocumento.requestFocus();
            return false;
        }
        return true;
    }

    private void buscarArticulo() {
        articulo = ArticuloFacade.getInstance().buscarPorCodigo(tfCodigo.getText());
        if (articulo != null) {
            cargarArticulo();
            tfPrecioPactado.requestFocus();
        } else {
            DiagBuscarArticulo buscarArticulo = new DiagBuscarArticulo(null, true);
            buscarArticulo.setLocation(Comunes.centrarDialog(buscarArticulo));
            buscarArticulo.setVisible(true);
            if (buscarArticulo.getArticulo() != null) {
                articulo = buscarArticulo.getArticulo();
                cargarArticulo();
                tfPrecioPactado.requestFocus();
            }
        }

    }

    private void cargarArticulo() {
        tfCodigo.setText(articulo.getCodigoBarra());
        tfDescripcion.setText(articulo.getDescripcion());
        cargarPrecioArticulo();

        //Subtotal
        if (!tfCantidad.getText().isEmpty() && BigDecimalValidator.getInstance().isValid(tfCantidad.getText())) {
            //por cantidad
            String subTotalArticulo = precioArticulo.getPrecio()
                    .multiply(BigDecimal.valueOf(Double.parseDouble(tfCantidad.getText()))).toString();
            tfSubTotalArticulo.setText(subTotalArticulo);
        }

    }

    private void cargarPrecioArticulo() {
        try {
            precioArticulo = PrecioArticuloFacade.getInstance().get(articulo, cliente.getListaPrecio(), sucursal);
            tfPrecioNormal.setText(precioArticulo.getPrecio().toString());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "No se pudo cargar el precio del articulo");
        }
    }

    private void buscarClientePorDNI() {
        Persona persona = ClienteFacade.getInstance().getPersonaXDni(ftfDocumento.getText());
        if (persona != null) {
            tfCliente.setText(persona.toString());
            cliente = persona;
            tfCodigo.requestFocus();
            listaArticulos = new ArrayList<>();

        } else {
            tfCliente.setText("");
            buscarCliente();

        }

    }

    private void buscarCliente() {
        tfCliente.setText("");
        DiagBuscarCliente diagBuscarCliente = new DiagBuscarCliente(null, true);
        diagBuscarCliente.setLocation(Comunes.centrarDialog(diagBuscarCliente));
        diagBuscarCliente.setVisible(true);
        if (diagBuscarCliente.getCliente() != null) {
            cliente = diagBuscarCliente.getCliente();
            tfCliente.setText(cliente.toString());
            if (cliente.getClass() == Persona.class) {
                ftfDocumento.setText(((Persona) cliente).getDocumentoIdentidad().getNumero());
            } else {
                ftfDocumento.setText(((Organismo) cliente).getCUIT());
            }
            tfCodigo.requestFocus();
            listaArticulos = new ArrayList<>();
        }
    }

    private void calcularSubtotalItem() {
        if (BigDecimalValidator.getInstance().isValid(tfPrecioPactado.getText())
                && BigDecimalValidator.getInstance().isValid(tfCantidad.getText())) {
            BigDecimal precio = new BigDecimal(tfPrecioPactado.getText());
            BigDecimal cantidad = new BigDecimal(tfCantidad.getText());
            tfSubTotalArticulo.setText(precio.multiply(cantidad).toString());
        }
    }

    private void limpiarValores() {
        tfNumero.setText("");
        cliente = null;
        ordenDeCompra = null;
        listaArticulos.clear();
        ftfDocumento.setText("");
        tfCliente.setText("");
        modeloTablaArticulos = new ModeloTablaNoEditable();
        tblArticulos.setModel(modeloTablaArticulos);
        tfNumero.setText("");
        tfCodigo.setText("");
    }

    private void cargarOrdenDeCompra() {
        try {
            tfNumero.setText(ordenDeCompra.getNumeroOrden());
        } catch (Exception e) {

        }
        try {
            jXDatePicker1.setDate(ordenDeCompra.getFecha());
        } catch (Exception e) {

        }
        try {
            cliente = ordenDeCompra.getCliente();
            tfCliente.setText(cliente.toString());
        } catch (Exception e) {

        }

        if (cliente.getClass() == Persona.class) {
            ftfDocumento.setText(((Persona) cliente).getDocumentoIdentidad().getNumero());
        } else {
            ftfDocumento.setText(((Organismo) cliente).getCUIT());
        }
        try {
            listaArticulos = ordenDeCompra.getOrdenDeCompraArticulos();
            cargarTablaArticulos(listaArticulos);
        } catch (Exception e) {

        }
        try {
            tfTotal.setText(ordenDeCompra.getMonto().toString());
        } catch (Exception e) {

        }

    }

    public void quitarArticuloLista() {

        int i = JOptionPane.showConfirmDialog(null, "Desea eliminar el articulo seleccionado?",
                "Confirmar eliminacion", JOptionPane.YES_NO_OPTION);
        if (i == 0) {
            int filaSeleccionada = tblArticulos.getSelectedRow();
            listaArticulos.remove(filaSeleccionada);
            ((ModeloTablaNoEditable) tblArticulos.getModel()).removeRow(filaSeleccionada);
            calcularTotal();
            tfCodigo.requestFocus();
            tfCodigo.selectAll();
        }
    }

    private void seleccionarArticulo() {
        if (tblArticulos.getSelectedRow() != -1) {
            btnCancelarModificacion.setEnabled(true);
            modificacionOrden = true;
            selectedRow = tblArticulos.getSelectedRow();
            ordenDeCompraArticulo = listaArticulos.get(selectedRow);
            articulo = ordenDeCompraArticulo.getArticulo();
            //esto es
            cargarArticulo();
            tfPrecioPactado.setText(ordenDeCompraArticulo.getPrecio().toString());
            tfCantidad.setText(ordenDeCompraArticulo.getCantidadPeso().toString());
            tfPrecioPactado.requestFocus();
            calcularSubtotalItem();
            //Esto es

            //            ((ModeloTablaNoEditable) tblArticulos.getModel()).removeRow(selectedRow);
            //            calcularTotal();
            //            articulo 
        }

    }

    private void limpiarModificacion() {
        btnCancelarModificacion.setEnabled(false);
        modificacionOrden = false;
        limpiarCamposArticulo();
        tfCodigo.setText("");
        articulo = null;
    }

    private void modificarArticuloOrden() {

    }
}