/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frm_eleccioncaja.java
 *
 * Created on 09-ene-2010, 22:43:53
 */

package dia4g3java;

import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.ListSelectionModel;

/**
 *
 * @author pelujl
 */
public class frm_eleccioncaja extends javax.swing.JFrame {

    Empleado Auxusuario = new Empleado();
    Sucursal Auxsuc = new Sucursal();
    Caja Auxcaja = new Caja();
    String fecha = new String();

    int saldo_inicial=0;
    int nuevo_saldo_caja = 0;
    /** Creates new form frm_eleccioncaja */
    public frm_eleccioncaja(Sucursal suc,Empleado usuario) {
        initComponents();
        Auxusuario = usuario;
        Auxsuc = suc;

                           ArrayList<Caja> caj = Auxsuc.getCaja();
                           Iterator iterator_caja = caj.listIterator();
                           DefaultListModel dlm = new DefaultListModel();
                           jlstcaja.setModel(dlm);
                           jlstcaja.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

                           while (iterator_caja.hasNext()){
                               Caja mi_caja = (Caja) iterator_caja.next();
                               if (mi_caja.getLibre()){
                                   dlm.addElement(mi_caja.getId_caja());
                               }
                           }
    }

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

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jlstcaja = new javax.swing.JList();
        jbtn_seleccionarcaja = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jtxtsaldoinicial = new javax.swing.JTextField();
        jbtn_aceptosaldocaja = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jtxtsaldocaja = new javax.swing.JTextField();
        jlblmensaje = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Seleccione una caja:");

        jlstcaja.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jlstcaja);

        jbtn_seleccionarcaja.setText("Asignar caja");
        jbtn_seleccionarcaja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtn_seleccionarcajaActionPerformed(evt);
            }
        });

        jLabel2.setText("Saldo Inicial:");

        jbtn_aceptosaldocaja.setText("Acepto saldo de caja");
        jbtn_aceptosaldocaja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtn_aceptosaldocajaActionPerformed(evt);
            }
        });

        jButton2.setText("Abrir parte por diferencia");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel3.setText("Saldo caja:");

        jlblmensaje.setForeground(new java.awt.Color(255, 51, 51));

        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)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jlblmensaje, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jbtn_seleccionarcaja)
                        .addGap(89, 89, 89))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addContainerGap(345, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE))
                            .addComponent(jbtn_aceptosaldocaja)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jtxtsaldocaja, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)
                                    .addComponent(jtxtsaldoinicial, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)
                                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING))))
                        .addGap(30, 30, 30))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtn_seleccionarcaja)
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtsaldoinicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtn_aceptosaldocaja)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtxtsaldocaja, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jlblmensaje, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                .addContainerGap())
        );

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

    private void jbtn_seleccionarcajaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtn_seleccionarcajaActionPerformed
        // TODO add your handling code here:
        int cadena=(Integer) jlstcaja.getSelectedValue();
      //  String cadena=(String) jlstCuentas.getSelectedValue();
      

       System.out.println(cadena);
       Auxusuario.setCaja_asignada(cadena);

       ArrayList<Caja> caj = Auxsuc.getCaja();
       Iterator iterator_caja = caj.listIterator();

       while (iterator_caja.hasNext()){
           Caja mi_caja = (Caja) iterator_caja.next();
           if (mi_caja.getId_caja()==cadena){
                Auxcaja = mi_caja;
           }
       }

           System.out.println(Auxcaja.getSaldo());
           ArrayList <Arqueo> arq3 = Auxcaja.getArqueo();
           Iterator iterator_arq = arq3.listIterator();

               while (iterator_arq.hasNext()){
                   Arqueo mi_arqueo = (Arqueo) iterator_arq.next();
                   saldo_inicial = mi_arqueo.getSaldoFinal();
                    System.out.println(saldo_inicial);
               }

               String saldo = Integer.toString(saldo_inicial);
               jtxtsaldoinicial.setText(saldo);
               // System.out.println(saldo_inicial);
    }//GEN-LAST:event_jbtn_seleccionarcajaActionPerformed

    private void jbtn_aceptosaldocajaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtn_aceptosaldocajaActionPerformed
        // TODO add your handling code here:
            ArrayList<Arqueo> ar= new ArrayList<Arqueo>();

            Arqueo arq1 = new Arqueo();
            arq1.setSaldoInicial(saldo_inicial);
            arq1.setEmpleado(Auxusuario);
            
            java.util.Date date = new java.util.Date();
            java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("dd/MM/yyyy");
            fecha = sdf.format(date);
            
            arq1.setFecha(fecha);

            ar.add(arq1);

            this.dispose();
       // Auxcaja.setArqueo(val);

    }//GEN-LAST:event_jbtn_aceptosaldocajaActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
       if (jtxtsaldocaja.getText().compareTo("")==0){
            jlblmensaje.setText("INSERTE EL SALDO ACTUAL PARA EMITIR PARTE");
      }else{
            String saldocaja = jtxtsaldocaja.getText();
            nuevo_saldo_caja = Integer.parseInt (saldocaja);

            ArrayList<PartesDiferencias> PD = new ArrayList<PartesDiferencias>();
            PartesDiferencias p1 = new PartesDiferencias();
            p1.setFecha(fecha);
            p1.setId_caja(Auxcaja.getId_caja());
            p1.setSaldoArqueo(saldo_inicial);
            p1.setSaldoCaja(nuevo_saldo_caja);

            PD.add(p1);
            Auxusuario.setPartesDiferencias(PD);
            this.dispose();

      }


    }//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
  /*  public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frm_eleccioncaja().setVisible(true);
            }
        });
    }
*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtn_aceptosaldocaja;
    private javax.swing.JButton jbtn_seleccionarcaja;
    private javax.swing.JLabel jlblmensaje;
    private javax.swing.JList jlstcaja;
    private javax.swing.JTextField jtxtsaldocaja;
    private javax.swing.JTextField jtxtsaldoinicial;
    // End of variables declaration//GEN-END:variables

}
