Dialogos.RetiroDialogo.java Source code

Java tutorial

Introduction

Here is the source code for Dialogos.RetiroDialogo.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 Dialogos;

import Controladores.exceptions.NonexistentEntityException;
import GUI.Conection;
import Impresion.PrintNow;
import Negocio.CobroDiario;
import Negocio.Configuraciones;
import Negocio.Cupo;
import Negocio.Locker;
import Negocio.UsuarioDiario;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.joda.time.Instant;
import org.joda.time.Interval;
import org.joda.time.Period;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;

/**
 *
 * @author santiago pc
 */
public class RetiroDialogo extends javax.swing.JDialog {
    Cupo cupo = null;
    CobroDiario cobroDiario = null;

    /**
     * Creates new form RetiroDialogo
     */
    public RetiroDialogo(java.awt.Frame parent, boolean modal, Cupo cupo) {
        super(parent, modal);
        initComponents();
        this.cupo = cupo;
        this.setLocationRelativeTo(parent);
        placa.setText(cupo.getPlaca().getPlaca());
        Interval intervalo = new Interval(new Instant(cupo.getCupoPK().getIngreso().getTime()),
                new Instant(cupo.getSalida().getTime()));
        Period period = intervalo.toPeriod();
        PeriodFormatter minutesAndSeconds = new PeriodFormatterBuilder().printZeroAlways().appendHours()
                .appendSeparator(":").appendMinutes().toFormatter();
        String result = minutesAndSeconds.print(period);
        tiempo.setText(result);
        cobroFinal.setText(String.valueOf(cupo.getCobroSugerido()));
        if (cupo.getLocker() != null) {
            locker.setText(String.format("%s : %d", cupo.getLocker().getIdentificador(),
                    cupo.getLocker().getAlojamiento()));
        } else {
            locker.setText("Ninguno");
        }
        observaciones.setText(cupo.getPlaca().getUsuario().getObservacion());
        this.setVisible(true);
    }

    public RetiroDialogo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(parent);
        this.setVisible(true);
    }

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

        placa = new javax.swing.JLabel();
        tiempo = new javax.swing.JLabel();
        cobroFinal = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        placa1 = new javax.swing.JLabel();
        locker = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        observaciones = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Retiro de vehiculo del sistema.");
        setIconImage(null);
        setIconImages(null);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        placa.setFont(new java.awt.Font("Arial", 0, 48)); // NOI18N
        placa.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        placa.setText("$placa");

        tiempo.setFont(new java.awt.Font("Arial", 0, 48)); // NOI18N
        tiempo.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        tiempo.setText("$tiempo");

        cobroFinal.setFont(new java.awt.Font("Arial", 0, 48)); // NOI18N
        cobroFinal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        cobroFinal.setText("0");
        cobroFinal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cobroFinalActionPerformed(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Arial", 0, 48)); // NOI18N
        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        placa1.setFont(new java.awt.Font("Arial", 0, 48)); // NOI18N
        placa1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        placa1.setText("Locker");

        locker.setFont(new java.awt.Font("Arial", 0, 48)); // NOI18N
        locker.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        locker.setText("A1");

        jButton2.setFont(new java.awt.Font("Arial", 0, 48)); // NOI18N
        jButton2.setText("Imprimir");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        observaciones.setColumns(20);
        observaciones.setFont(new java.awt.Font("Arial", 0, 36)); // NOI18N
        observaciones.setForeground(new java.awt.Color(255, 51, 0));
        observaciones.setLineWrap(true);
        observaciones.setRows(5);
        observaciones.setWrapStyleWord(true);
        jScrollPane1.setViewportView(observaciones);

        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(jScrollPane1)
                        .addComponent(cobroFinal)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(placa1, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(locker, javax.swing.GroupLayout.PREFERRED_SIZE, 236,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                layout.createSequentialGroup()
                                        .addComponent(placa, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(tiempo, javax.swing.GroupLayout.PREFERRED_SIZE, 236,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup().addComponent(jButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 230,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(placa).addComponent(tiempo))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(placa1).addComponent(locker))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cobroFinal, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 54,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 54,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap()));

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

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

    private void cobroFinalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cobroFinalActionPerformed
        retiroDiarioFinal(cupo);
        this.dispose();
    }//GEN-LAST:event_cobroFinalActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        cobroFinal.selectAll();
    }//GEN-LAST:event_formWindowOpened

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        retiroDiarioFinal(cupo);
        PrintNow.imprimirReciboSalida(cupo, cobroDiario.getCobro());
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @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(RetiroDialogo.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RetiroDialogo.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RetiroDialogo.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RetiroDialogo.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() {
                RetiroDialogo dialog = new RetiroDialogo(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);
            }
        });
    }

    private void retiroDiarioFinal(Cupo cupoActual) {
        Configuraciones consecutivo = Conection.getConfiguraciones().findConfiguraciones("consecutivo");
        CobroDiario cobro = new CobroDiario();
        UsuarioDiario usuario = cupo.getPlaca();
        cobro.setConsecutivo(Long.parseLong(consecutivo.getValor()));
        cobro.setFecha(new Date());
        cobro.setCobro(Long.parseLong(cobroFinal.getText()));
        cobro.setCupo(cupoActual);
        usuario.setEntradas(usuario.getEntradas() + 1);
        usuario.setMinutosRegistrados(usuario.getMinutosRegistrados() + cupo.getHoras() * 60 + cupo.getMinutos());
        usuario.setCobroTotal(usuario.getCobroTotal() + cobro.getCobro());
        try {
            Conection.getCupo().edit(cupoActual);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(RetiroDialogo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(RetiroDialogo.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Conection.getUsuarioDiario().edit(usuario);
            Conection.getCobroDiaro().create(cobro);
        } catch (Exception ex) {
            Logger.getLogger(RetiroDialogo.class.getName()).log(Level.SEVERE, null, ex);
        }
        consecutivo.setValor(String.valueOf(Integer.parseInt(consecutivo.getValor()) + 1));
        try {
            Conection.getConfiguraciones().edit(consecutivo);
        } catch (Exception ex) {
            Logger.getLogger(RetiroDialogo.class.getName()).log(Level.SEVERE, null, ex);
        }
        Locker locker = cupo.getLocker();
        if (locker != null) {
            locker.setAlojamiento(0);
            try {
                Conection.getLocker().edit(locker);
            } catch (Exception ex) {
                Logger.getLogger(RetiroDialogo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        cobroDiario = cobro;
    }

    private void cancelar(Cupo cupoActual) {
        cupo.setSalida(null);
        cupo.setCobroSugerido(0);
        cupo.setHoras(0);
        cupo.setMinutos(0);
        try {
            Conection.getCupo().edit(cupo);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(RetiroDialogo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(RetiroDialogo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField cobroFinal;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel locker;
    private javax.swing.JTextArea observaciones;
    private javax.swing.JLabel placa;
    private javax.swing.JLabel placa1;
    private javax.swing.JLabel tiempo;
    // End of variables declaration//GEN-END:variables
}