com.app.caja.SobranteFaltanteCaja.java Source code

Java tutorial

Introduction

Here is the source code for com.app.caja.SobranteFaltanteCaja.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 com.app.caja;

import com.entidades.ArqueoCab;
import com.entidades.Caja;
import java.awt.HeadlessException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import util.HibernateUtil;

/**
 * @author Carlos Patio
 */
public class SobranteFaltanteCaja extends javax.swing.JDialog {

    /**
     * Creates new form AnularComprobante
     */
    private String usuario;
    private String origen = "";
    private String formulario;
    private ArqueoCab masterArqueo;
    private int posicion;
    private List<Object> ArrayPrimaryKey;

    public SobranteFaltanteCaja(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        hibernateSession();
        inicializarCampos();
        this.usuario = "karu";
        this.formulario = this.getClass().getSimpleName();
        arranque();
    }

    public SobranteFaltanteCaja(String user) {
        initComponents();
        hibernateSession();
        inicializarCampos();
        this.usuario = "karu";
        this.formulario = this.getClass().getSimpleName();
        arranque();
    }

    private Session st;
    private BigInteger masterNroOpe;

    private void hibernateSession() {
        st = HibernateUtil.getSessionFactory().openSession();
    }

    private void arranque() {
        this.cmdGuardar.setEnabled(false);
        this.cmdCancelar.setVisible(true);
        this.cmdCancelar.setEnabled(false);
        buscarRegistros();
    }

    /**
     * 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();
        cmdNuevo = new javax.swing.JButton();
        cmdGuardar = new javax.swing.JButton();
        cmdEditar = new javax.swing.JButton();
        cmdCancelar = new javax.swing.JButton();
        cmdBuscar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtCodCaja = new javax.swing.JTextField();
        txtNomCaja = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtNroOpe = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtImporte = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        txtFecha = new javax.swing.JFormattedTextField();
        jLabel5 = new javax.swing.JLabel();
        cboTipoAjuste = new javax.swing.JComboBox();
        jPanel7 = new javax.swing.JPanel();
        btnUltimo = new javax.swing.JButton();
        btnSiguiente = new javax.swing.JButton();
        btnAnterior = new javax.swing.JButton();
        btnPrimero = new javax.swing.JButton();
        cmdCerrar = new javax.swing.JButton();
        lblInfoPie = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Sobrante / Faltante Caja - KARU SGCG v1.0");
        setResizable(false);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        cmdNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imagenes/New.png"))); // NOI18N
        cmdNuevo.setToolTipText("Nuevo");
        cmdNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdNuevoActionPerformed(evt);
            }
        });

        cmdGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imagenes/Save.png"))); // NOI18N
        cmdGuardar.setToolTipText("Guardar");
        cmdGuardar.setEnabled(false);
        cmdGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdGuardarActionPerformed(evt);
            }
        });

        cmdEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imagenes/edit.png"))); // NOI18N
        cmdEditar.setToolTipText("Editar");
        cmdEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEditarActionPerformed(evt);
            }
        });

        cmdCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imagenes/cancel32.png"))); // NOI18N
        cmdCancelar.setToolTipText("Cancelar");
        cmdCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancelarActionPerformed(evt);
            }
        });

        cmdBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imagenes/Search (2).png"))); // NOI18N
        cmdBuscar.setToolTipText("Buscar");
        cmdBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdBuscarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout
                .setHorizontalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup().addGap(3, 3, 3)
                                .addComponent(cmdNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 33,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 33,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 33,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 33,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 33,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addGap(3, 3, 3).addGroup(jPanel2Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(cmdNuevo, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cmdBuscar, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                jPanel2Layout.createSequentialGroup().addGroup(jPanel2Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(cmdEditar, javax.swing.GroupLayout.Alignment.LEADING,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(cmdGuardar, javax.swing.GroupLayout.Alignment.LEADING,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(cmdCancelar, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap()));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Caja");

        txtCodCaja.setBackground(new java.awt.Color(255, 255, 255));
        txtCodCaja.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCodCajaFocusLost(evt);
            }
        });

        txtNomCaja.setEditable(false);
        txtNomCaja.setBackground(new java.awt.Color(255, 255, 255));
        txtNomCaja.setFocusable(false);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setText("Nro. Op.");

        txtNroOpe.setBackground(new java.awt.Color(204, 255, 255));
        txtNroOpe.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        txtNroOpe.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtNroOpe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNroOpeActionPerformed(evt);
            }
        });
        txtNroOpe.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtNroOpeFocusLost(evt);
            }
        });
        txtNroOpe.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNroOpeKeyTyped(evt);
            }
        });

        jLabel4.setText("Importe");

        txtImporte.setBackground(new java.awt.Color(255, 255, 204));
        txtImporte.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(
                new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.##"))));
        txtImporte.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtImporte.setToolTipText("Monto de Apertura para la caja");
        txtImporte.setValue(0);
        txtImporte.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtImporteFocusLost(evt);
            }
        });
        txtImporte.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtImporteKeyTyped(evt);
            }
        });

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Fecha");

        txtFecha.setEditable(false);
        txtFecha.setBackground(new java.awt.Color(255, 255, 255));
        txtFecha.setFormatterFactory(
                new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));
        txtFecha.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtFecha.setToolTipText("Fecha del Arqueo");

        jLabel5.setText("Tipo Ajuste");

        cboTipoAjuste.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Faltante", "Sobrante" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 55,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 56,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 42,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup().addGap(9, 9, 9)
                                                .addGroup(jPanel1Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                                .addComponent(txtNroOpe,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 73,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(jLabel2,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 61,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                .addComponent(txtFecha,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 94,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                                .addGap(1, 1, 1)
                                                                .addComponent(txtCodCaja,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 64,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGap(1, 1, 1).addComponent(txtNomCaja,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 218,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE))))
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addGroup(jPanel1Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addComponent(cboTipoAjuste,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 89,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(txtImporte,
                                                                javax.swing.GroupLayout.Alignment.LEADING,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 91,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel3)
                        .addComponent(txtNroOpe, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2).addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1)
                                .addComponent(txtCodCaja, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtNomCaja, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel5).addComponent(cboTipoAjuste,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel4).addComponent(txtImporte,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        jPanel7.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel7.setToolTipText("Panel de Navegacin");

        btnUltimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imagenes/ULTIMO.png"))); // NOI18N
        btnUltimo.setToolTipText("ltimo Registro");
        btnUltimo.setMaximumSize(new java.awt.Dimension(24, 24));
        btnUltimo.setMinimumSize(new java.awt.Dimension(24, 24));
        btnUltimo.setPreferredSize(new java.awt.Dimension(24, 24));
        btnUltimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUltimoActionPerformed(evt);
            }
        });

        btnSiguiente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imagenes/SIGUIENTE.png"))); // NOI18N
        btnSiguiente.setToolTipText("Registro Siguiente");
        btnSiguiente.setMaximumSize(new java.awt.Dimension(24, 24));
        btnSiguiente.setMinimumSize(new java.awt.Dimension(24, 24));
        btnSiguiente.setPreferredSize(new java.awt.Dimension(24, 24));
        btnSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSiguienteActionPerformed(evt);
            }
        });

        btnAnterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imagenes/ANTERIOR.png"))); // NOI18N
        btnAnterior.setToolTipText("Registro Anterior");
        btnAnterior.setMaximumSize(new java.awt.Dimension(24, 24));
        btnAnterior.setMinimumSize(new java.awt.Dimension(24, 24));
        btnAnterior.setPreferredSize(new java.awt.Dimension(24, 24));
        btnAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnteriorActionPerformed(evt);
            }
        });

        btnPrimero.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imagenes/primero.png"))); // NOI18N
        btnPrimero.setToolTipText("Primer Registro");
        btnPrimero.setMaximumSize(new java.awt.Dimension(24, 24));
        btnPrimero.setMinimumSize(new java.awt.Dimension(24, 24));
        btnPrimero.setPreferredSize(new java.awt.Dimension(24, 24));
        btnPrimero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrimeroActionPerformed(evt);
            }
        });

        cmdCerrar.setText("Cerrar");
        cmdCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCerrarActionPerformed(evt);
            }
        });

        lblInfoPie.setText("Karu");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(jPanel7Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel7Layout.createSequentialGroup().addContainerGap()
                        .addComponent(btnPrimero, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(3, 3, 3)
                        .addComponent(btnAnterior, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(3, 3, 3)
                        .addComponent(btnSiguiente, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(3, 3, 3)
                        .addComponent(btnUltimo, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblInfoPie, javax.swing.GroupLayout.PREFERRED_SIZE, 171,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cmdCerrar)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        jPanel7Layout.setVerticalGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel7Layout.createSequentialGroup().addGap(3, 3, 3).addGroup(jPanel7Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnPrimero, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnAnterior, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnSiguiente, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnUltimo, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cmdCerrar).addComponent(lblInfoPie)).addGap(3, 3, 3)));

        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(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 43,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

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

    private void cmdNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdNuevoActionPerformed
        nuevo();
        this.txtNroOpe.grabFocus();
    }//GEN-LAST:event_cmdNuevoActionPerformed

    private void cmdGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdGuardarActionPerformed
        ArqueoCab arq = masterArqueo;

        if (arq != null) {
            guardarEdicion();
            deshabilitarCampos();
            buscarRegistros();
        }
    }//GEN-LAST:event_cmdGuardarActionPerformed

    private void cmdCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancelarActionPerformed
        inicializarCampos();
        deshabilitarCampos();
        origen = "";
    }//GEN-LAST:event_cmdCancelarActionPerformed

    private void cmdBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdBuscarActionPerformed
        try {
            /*if (!this.txtNroOpe.getText().isEmpty()) {
            int cod = Integer.parseInt(this.txtNroOpe.getText());
            buscarArqueo(cod);
                
            if (masterArqueo != null) {
                cargarDetalle(masterArqueo);
            }
            } else {
            JOptionPane.showMessageDialog(null, "Ingrese nro. de arqueo a buscar ",
                    "Sobrante / Faltante de Caja", JOptionPane.INFORMATION_MESSAGE);
            this.txtNroOpe.grabFocus();
            }*/
            buscarRegistros();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_cmdBuscarActionPerformed

    private void cmdEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEditarActionPerformed
        origen = "Editar";
        habilitarCampos();
    }//GEN-LAST:event_cmdEditarActionPerformed

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

    private void txtCodCajaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCodCajaFocusLost

    }//GEN-LAST:event_txtCodCajaFocusLost

    private void txtImporteFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtImporteFocusLost
        if (this.txtImporte.getText().equals("")) {
            this.txtImporte.setValue(0);
        }
    }//GEN-LAST:event_txtImporteFocusLost

    private void txtNroOpeFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNroOpeFocusLost
        ArqueoCab arq = null;
        try {
            if (!this.txtNroOpe.getText().isEmpty()) {
                int cod = Integer.parseInt(this.txtNroOpe.getText());
                arq = buscarArqueo(cod);

                if (arq != null) {
                    masterArqueo = arq;
                    cargarDetalle(arq);
                }
                //if (masterArqueo != null) {
                // cargarDetalle(masterArqueo);
                // }
            }
        } catch (NumberFormatException e) {
        }
    }//GEN-LAST:event_txtNroOpeFocusLost

    private void txtImporteKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtImporteKeyTyped
        char car = evt.getKeyChar();
        if (txtImporte.getText().length() >= 10) {
            evt.consume();
        }

        if (car == ',' || car == '0' || car == '1' || car == '2' || car == '3' || car == '4' || car == '5'
                || car == '6' || car == '7' || car == '8' || car == '9') {
            //no hace nada, deja pasar
        } else {
            evt.consume();
        }
    }//GEN-LAST:event_txtImporteKeyTyped

    private void txtNroOpeKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNroOpeKeyTyped
        char car = evt.getKeyChar();
        if (txtNroOpe.getText().length() >= 10) {
            evt.consume();
        }
        if ((car < '0' || car > '9')) {
            evt.consume();
        }
    }//GEN-LAST:event_txtNroOpeKeyTyped

    private void btnUltimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUltimoActionPerformed
        ultimo();
    }//GEN-LAST:event_btnUltimoActionPerformed

    private void btnSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSiguienteActionPerformed
        siguiente();
    }//GEN-LAST:event_btnSiguienteActionPerformed

    private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnteriorActionPerformed
        anterior();
    }//GEN-LAST:event_btnAnteriorActionPerformed

    private void btnPrimeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrimeroActionPerformed
        primero();
    }//GEN-LAST:event_btnPrimeroActionPerformed

    private void cmdCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCerrarActionPerformed
        this.st.close();
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_cmdCerrarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SobranteFaltanteCaja.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SobranteFaltanteCaja.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SobranteFaltanteCaja.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SobranteFaltanteCaja.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SobranteFaltanteCaja dialog = new SobranteFaltanteCaja(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnterior;
    private javax.swing.JButton btnPrimero;
    private javax.swing.JButton btnSiguiente;
    private javax.swing.JButton btnUltimo;
    private javax.swing.JComboBox cboTipoAjuste;
    private javax.swing.JButton cmdBuscar;
    private javax.swing.JButton cmdCancelar;
    private javax.swing.JButton cmdCerrar;
    private javax.swing.JButton cmdEditar;
    private javax.swing.JButton cmdGuardar;
    private javax.swing.JButton cmdNuevo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JLabel lblInfoPie;
    private javax.swing.JTextField txtCodCaja;
    private javax.swing.JFormattedTextField txtFecha;
    private javax.swing.JFormattedTextField txtImporte;
    private javax.swing.JTextField txtNomCaja;
    private javax.swing.JTextField txtNroOpe;
    // End of variables declaration//GEN-END:variables

    private void habilitarCampos() {
        this.cmdGuardar.setEnabled(true);
        this.cmdCancelar.setEnabled(true);
        this.cmdNuevo.setEnabled(false);
        this.cmdEditar.setEnabled(false);
    }

    private void deshabilitarCampos() {
        this.cmdGuardar.setEnabled(false);
        this.cmdCancelar.setEnabled(false);
        this.cmdNuevo.setEnabled(true);
        this.cmdEditar.setEnabled(true);
    }

    private void inicializarCampos() {
        this.txtFecha.setText("");
        this.txtImporte.setValue(0);
        this.txtCodCaja.setText("");
        this.txtNomCaja.setText("");
        this.txtNroOpe.setText("");
    }

    private void nuevo() {
        habilitarCampos();
        inicializarCampos();
        this.origen = "Nuevo";
    }

    private void guardarEdicion() {
        Date fechaMod = new Date();
        ArqueoCab arq = new ArqueoCab();

        try {

            st.beginTransaction();

            BigDecimal factor = BigDecimal.ONE;

            if ((this.cboTipoAjuste.getSelectedIndex()) == 0) {
                factor = new BigDecimal("-1");
            } else {
                factor = BigDecimal.ONE;
            }

            arq = masterArqueo;
            arq.setCodAjuste(this.cboTipoAjuste.getSelectedIndex() + 1);
            arq.setImporteAjuste((new BigDecimal(this.txtImporte.getValue().toString())).multiply(factor));
            arq.setFechaMod(fechaMod);
            arq.setAccionMod("Cierre");
            arq.setUsuarioMod(usuario);

            st.update(arq);
            st.getTransaction().commit();

        } catch (HibernateException ex) {
            st.getTransaction().rollback();
            JOptionPane.showMessageDialog(null,
                    "Error al actualizar registro. " + "\n" + ex.getMessage() + "\n" + ex.getCause(),
                    "Sobrante / Faltante de Caja", JOptionPane.INFORMATION_MESSAGE);
            ex.printStackTrace();
        }
    }

    private Caja obtenerCaja(String cod) {
        Caja caja = null;
        try {
            caja = (Caja) st.get(Caja.class, cod);
            if (caja == null) {
                JOptionPane.showMessageDialog(null, "Cdigo de caja buscado no existe.",
                        "Sobrante / Faltante de Caja", JOptionPane.INFORMATION_MESSAGE);
            } else {
                this.txtNomCaja.setText(caja.getNomCaja());
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Ocurrio un error al recuperar Caja. " + "\n" + e.getMessage(),
                    "Sobrante / Faltante de Caja", JOptionPane.ERROR_MESSAGE);
        }
        return caja;
    }

    private ArqueoCab buscarArqueo(int cod) {
        ArqueoCab var = null;
        try {
            var = obtenerHabilitacion(cod);

            if (var != null) {
                masterArqueo = var;
                //    cargarDetalle(var);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    "Error al realizar bsqueda de comprobante. " + "\n" + e.getMessage(),
                    "Error - Sobrante / Faltante de Caja", JOptionPane.ERROR_MESSAGE);
        }
        return var;
    }

    private ArqueoCab obtenerHabilitacion(int oper) {
        ArqueoCab arq = null;

        try {
            //arq = (ArqueoCab) st.get(ArqueoCab.class, oper);
            if (origen.equals("Nuevo")) {
                Query query = st.createQuery("From ArqueoCab a Where a.nroOperacion = ? and a.estado = 'A'");
                query.setParameter(0, oper);

                arq = (ArqueoCab) query.uniqueResult();

            } else {
                arq = (ArqueoCab) st.get(ArqueoCab.class, oper);
            }
            if (arq == null && origen.equals("Nuevo")) {
                JOptionPane.showMessageDialog(null, "El arqueo no existe o ya fue cerrado.",
                        "Sobrante / Faltante de Caja", JOptionPane.INFORMATION_MESSAGE);
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Ocurrio un error al realizar consulta. " + "\n" + e.getMessage(),
                    "Error - Sobrante / Faltante de Caja", JOptionPane.ERROR_MESSAGE);
        }
        return arq;
    }

    private void cargarDetalle(ArqueoCab arq) {
        try {
            this.txtNroOpe.setText(String.valueOf(arq.getNroOperacion()));
            this.txtFecha.setValue(arq.getFechaDesde());
            this.txtCodCaja.setText(arq.getCodCaja());
            this.txtNomCaja.setText((obtenerCaja(arq.getCodCaja()).getNomCaja()));
            this.cboTipoAjuste.setSelectedIndex(arq.getCodAjuste() - 1);
            this.txtImporte.setValue(arq.getImporteAjuste());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void primero() {
        if (posicion > 0 && ArrayPrimaryKey.size() > 0) {
            posicion = 0;
            recuperarRegistros(posicion);
            String mensaje = "Registro " + String.valueOf(posicion + 1).trim() + " de "
                    + String.valueOf(ArrayPrimaryKey.size()).trim();
            this.lblInfoPie.setText(mensaje);
        } else {

        }
    }

    private void anterior() {
        if (posicion > 0 && ArrayPrimaryKey.size() > 0) {
            posicion--;
            recuperarRegistros(posicion);
            String mensaje = "Registro " + String.valueOf(posicion + 1).trim() + " de "
                    + String.valueOf(ArrayPrimaryKey.size()).trim();
            this.lblInfoPie.setText(mensaje);
        } else {

        }
    }

    private void siguiente() {
        if (posicion < ArrayPrimaryKey.size() - 1 && ArrayPrimaryKey.size() > 0) {
            posicion++;
            recuperarRegistros(posicion);
            String mensaje = "Registro " + String.valueOf(posicion + 1).trim() + " de "
                    + String.valueOf(ArrayPrimaryKey.size()).trim();
            this.lblInfoPie.setText(mensaje);
        } else {

        }
    }

    private void ultimo() {
        if (posicion < ArrayPrimaryKey.size() - 1 && ArrayPrimaryKey.size() > 0) {
            posicion = ArrayPrimaryKey.size() - 1;
            recuperarRegistros(posicion);
            String mensaje = "Registro " + String.valueOf(posicion + 1).trim() + " de "
                    + String.valueOf(ArrayPrimaryKey.size()).trim();
            this.lblInfoPie.setText(mensaje);
        } else {

        }
    }

    private void buscarRegistros() {
        try {
            String SqlQuery = "select nro_operacion from arqueo_cab where nro_operacion > 0 ";

            if (!this.txtNroOpe.getText().trim().isEmpty()) {
                SqlQuery = SqlQuery + " and nro_operacion = " + this.txtNroOpe.getText();
            }
            if (!this.txtCodCaja.getText().trim().isEmpty()) {
                SqlQuery = SqlQuery + " and cod_caja = '" + this.txtCodCaja.getText().trim() + "'";
            }

            SqlQuery = SqlQuery + " order by nro_operacion";

            Query consulta = st.createSQLQuery(SqlQuery);

            ArrayPrimaryKey = consulta.list();

            if (ArrayPrimaryKey.size() > 1) {
                posicion = 0;
                recuperarRegistros(posicion);
                this.btnPrimero.setEnabled(true);
                this.btnAnterior.setEnabled(true);
                this.btnSiguiente.setEnabled(true);
                this.btnUltimo.setEnabled(true);

                String mensaje = "Registro " + String.valueOf(posicion + 1).trim() + " de "
                        + String.valueOf(ArrayPrimaryKey.size()).trim();
                this.lblInfoPie.setText(mensaje);

            } else if (ArrayPrimaryKey.size() == 1) {
                posicion = 0;
                recuperarRegistros(posicion);

                String mensaje = "Registro " + String.valueOf(posicion + 1).trim() + " de "
                        + String.valueOf(ArrayPrimaryKey.size()).trim();
                this.lblInfoPie.setText(mensaje);
            } else {
                JOptionPane.showMessageDialog(null, "No existen registros para mostrar.", "Buscar",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (HeadlessException | HibernateException e) {
            e.printStackTrace();
        }
    }

    public void recuperarRegistros(int indice) {
        inicializarCampos();
        int nro_operacion = Integer.parseInt(ArrayPrimaryKey.get(indice).toString());
        //obtenerRegistro(nro_operacion);
        cargarDetalle(buscarArqueo(nro_operacion));
    }

}