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

/*
 * JF_transferencia.java
 *
 * Created on 18-nov-2009, 19:59:44
 */

package interfaz;

import daial.*;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author David
 */
public class jfrmTransferencia extends javax.swing.JFrame {

    jfrmPrincipal p;
    Sucursal s;
    Producto ctaActiva;

    /** Creates new form JF_transferencia */
    public jfrmTransferencia(jfrmPrincipal principal, Sucursal suc, Producto cta) {
        initComponents();
        this.setLocationRelativeTo(null);
        p = principal;
        s = suc;
        ctaActiva = cta;
    }

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

        jBtn_transf = new javax.swing.JButton();
        jTxt_conc = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jTxt_cta = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTxt_import = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jBtn_atras = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();

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

        jBtn_transf.setText("Transferir");
        jBtn_transf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtn_transfActionPerformed(evt);
            }
        });

        jTxt_conc.setText("Tranferencia + Introduzca el concepto");
        jTxt_conc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTxt_concActionPerformed(evt);
            }
        });

        jLabel1.setText("Concepto:");

        jTxt_cta.setText("Introduzca cuenta destino");

        jLabel2.setText("Cuenta destino: ");

        jTxt_import.setText("Introduzca el importe");

        jLabel3.setText("Importe:");

        jBtn_atras.setText("Atrás");
        jBtn_atras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtn_atrasActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel4.setText("Tranferencia bancaria");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(76, 76, 76)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jBtn_transf)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(157, 157, 157)
                                .addComponent(jBtn_atras)))
                        .addGap(79, 79, 79))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(100, 100, 100)
                        .addComponent(jLabel4)))
                .addContainerGap(29, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(56, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTxt_conc, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTxt_import, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTxt_cta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(49, 49, 49))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addComponent(jLabel4)
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTxt_conc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxt_cta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxt_import, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtn_transf)
                    .addComponent(jBtn_atras))
                .addContainerGap(70, Short.MAX_VALUE))
        );

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

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

    private void jBtn_transfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtn_transfActionPerformed

        String concepto = jTxt_conc.getText();
        String cta_destino = jTxt_cta.getText();
        Double importe = Double.parseDouble(jTxt_import.getText());
        JOptionPane dialogo = new JOptionPane();

        if(s.buscarCuenta(cta_destino)==null)
        {
            JOptionPane.showMessageDialog(rootPane, "La cuenta destinatario no existe", "Aviso", JOptionPane.INFORMATION_MESSAGE);
        }else
        {
            ctaActiva.transferencia(importe,s.buscarCuenta(cta_destino));

            p.actualizar_info();
            OPTransferencia OPT = new OPTransferencia();
            OPT.setConcepto("Tranferencia " + concepto);
            OPT.setCtaDestino(cta_destino);
            Date d = new Date();
            OPT.setFechaOperacion(d);
            OPT.setImporte(importe);

            s.buscarCuenta(cta_destino).insertarMovimientoProducto(OPT);


            OPTransferencia OPT2 = new OPTransferencia();
            OPT2.setConcepto("Tranferencia " + concepto);
            OPT2.setCtaDestino(cta_destino);
            Date d2 = new Date();
            OPT2.setFechaOperacion(d2);
            OPT2.setImporte(-importe);
            ctaActiva.insertarMovimientoProducto(OPT2);

            JOptionPane.showMessageDialog(rootPane, "La transferencia se ha realizado con exito", "Aviso", JOptionPane.INFORMATION_MESSAGE);
            p.setEnabled(true);
            this.dispose();
        }
    }//GEN-LAST:event_jBtn_transfActionPerformed

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

    private void jBtn_atrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtn_atrasActionPerformed
        // TODO add your handling code here:
        p.setEnabled(true);
        this.dispose();
    }//GEN-LAST:event_jBtn_atrasActionPerformed

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtn_atras;
    private javax.swing.JButton jBtn_transf;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTxt_conc;
    private javax.swing.JTextField jTxt_cta;
    private javax.swing.JTextField jTxt_import;
    // End of variables declaration//GEN-END:variables

}
