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

/*
 * jfrmReintegr.java
 *
 * Created on 23-ene-2010, 14:10:17
 */

package Vistas;

import Modelo.*;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
 *
 * @author Administrador
 */
public class jfrmReintegr extends javax.swing.JFrame implements Observer{

    JFrame auxJf;
    Sucursal sucAux = new Sucursal();
    /** Creates new form jfrmReintegr */
    public jfrmReintegr(Sucursal suc,JFrame jf) {
        auxJf=jf;
        initComponents();
        sucAux = suc;
        jtxtNumCuentaReintegro.setText(sucAux.getStCCOpera());
        
        jtxtSaldoActualCaja.setText(String.valueOf(sucAux.saldoCaja(sucAux.dameDNIEmpleado())));
        Caja c = sucAux.busquedaCajaSucursal(sucAux.dameIdCaja());
        c.addObserver(this);
    }

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

        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        label1 = new java.awt.Label();
        jlblInformacion = new javax.swing.JLabel();
        jbtAceptarReintegro = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jtxtConceptoReintegro = new javax.swing.JTextField();
        jtxtImporteReintegro = new javax.swing.JTextField();
        jtxtNumCuentaReintegro = new javax.swing.JTextField();
        jbtnCambiarCuenta = new javax.swing.JButton();
        jtxtRemitenteReintegro = new javax.swing.JTextField();
        jtxtSaldoActualCaja = new javax.swing.JTextField();
        jlblDoc1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vistas/EstafadorLogo2.JPG"))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Comic Sans MS", 1, 18));
        jLabel1.setText("Reintegro");

        label1.setFont(new java.awt.Font("Comic Sans MS", 1, 48));
        label1.setText("SwindlerBank");

        jbtAceptarReintegro.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        jbtAceptarReintegro.setText("Aceptar");
        jbtAceptarReintegro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtAceptarReintegroActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
        jLabel2.setText("Nº Cuenta:");

        jLabel3.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
        jLabel3.setText("Importe:");

        jLabel4.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
        jLabel4.setText("Concepto:");

        jLabel5.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
        jLabel5.setText("Remitente:");

        jtxtConceptoReintegro.setText("Reintegro");
        jtxtConceptoReintegro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtConceptoReintegroActionPerformed(evt);
            }
        });

        jbtnCambiarCuenta.setText("...");
        jbtnCambiarCuenta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCambiarCuentaActionPerformed(evt);
            }
        });

        jtxtSaldoActualCaja.setEditable(false);
        jtxtSaldoActualCaja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtSaldoActualCajaActionPerformed(evt);
            }
        });

        jlblDoc1.setFont(new java.awt.Font("Comic Sans MS", 0, 10)); // NOI18N
        jlblDoc1.setText("Saldo Caja:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 200, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtSaldoActualCaja, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jlblDoc1))
                        .addGap(11, 11, 11))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(jlblInformacion, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(jbtAceptarReintegro))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(53, 53, 53)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtConceptoReintegro, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtxtImporteReintegro, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtxtRemitenteReintegro, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jtxtNumCuentaReintegro, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(37, 37, 37)
                                    .addComponent(jbtnCambiarCuenta))))))
                .addGap(49, 49, 49))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(22, 22, 22)
                        .addComponent(jLabel1))
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jtxtRemitenteReintegro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtxtNumCuentaReintegro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnCambiarCuenta))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtxtImporteReintegro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jtxtConceptoReintegro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jbtAceptarReintegro)
                    .addComponent(jlblInformacion, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24))
            .addGroup(layout.createSequentialGroup()
                .addGap(133, 133, 133)
                .addComponent(jlblDoc1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jtxtSaldoActualCaja, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(256, Short.MAX_VALUE))
        );

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

    private void jbtAceptarReintegroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtAceptarReintegroActionPerformed
        // TODO add your handling code here:


        Op_Basica op = new Op_Basica();
        op.setStRemitente(jtxtRemitenteReintegro.getText());
        op.setStCuentaCorriente(jtxtNumCuentaReintegro.getText());
        op.setStConcBreve(jtxtConceptoReintegro.getText());
        

        if((jtxtRemitenteReintegro.getText().equals("")) || (jtxtNumCuentaReintegro.getText().equals("")) || (jtxtConceptoReintegro.getText().equals("")) || (jtxtImporteReintegro.getText().equals("")))
        {
            jlblInformacion.setText("Debe rellenar todos los campos");
        }

        else 
        {
            String stImporte = jtxtImporteReintegro.getText();
            DecimalFormat df = new DecimalFormat("#######,##");
            Number n = null;

            try {
                n = df.parse(stImporte);
            } catch (ParseException ex) {
                Logger.getLogger(jfrmIngreso.class.getName()).log(Level.SEVERE, null, ex);
            }
            op.setDImporte(n.doubleValue() * (-1));
            Date d = new Date();
            op.setDatOpe(d);

            if(sucAux.saldoCaja(sucAux.dameDNIEmpleado())<n.doubleValue())
            {
                jlblInformacion.setText("ERROR. Importe mayor que en Caja");
            }
            else
            {
                Producto auxCta = sucAux.devuelveProducto(op.getStCuentaCorriente());

                if(n.doubleValue() > auxCta.getDSaldo())
                {
                    jlblInformacion.setText("ERROR. Importe mayor que en Cuenta Corriente");
                }

                else
                {
                    sucAux.decrementoCaja(n.doubleValue(),sucAux.dameIdCaja());
                    sucAux.RegistraMovimiento(op);
                    jlblInformacion.setText("Reintegro realizado correctamente");
                }
            }
        }
}//GEN-LAST:event_jbtAceptarReintegroActionPerformed

    private void jbtnCambiarCuentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCambiarCuentaActionPerformed
        new jfrmSeleccionarCuentaCliente(sucAux,this,jtxtNumCuentaReintegro,true).setVisible(true);
        this.setEnabled(false);
}//GEN-LAST:event_jbtnCambiarCuentaActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        auxJf.setEnabled(true);
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosing

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

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

    /**
    * @param args the command line arguments
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new jfrmReintegr().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    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.JLabel jLabel6;
    private javax.swing.JButton jbtAceptarReintegro;
    private javax.swing.JButton jbtnCambiarCuenta;
    private javax.swing.JLabel jlblDoc1;
    private javax.swing.JLabel jlblInformacion;
    private javax.swing.JTextField jtxtConceptoReintegro;
    private javax.swing.JTextField jtxtImporteReintegro;
    private javax.swing.JTextField jtxtNumCuentaReintegro;
    private javax.swing.JTextField jtxtRemitenteReintegro;
    private javax.swing.JTextField jtxtSaldoActualCaja;
    private java.awt.Label label1;
    // End of variables declaration//GEN-END:variables

    public void update(Observable o, Object arg) {
        jtxtSaldoActualCaja.setText(String.valueOf(sucAux.saldoCaja(sucAux.dameDNIEmpleado())));
    }

}
