/*
 * 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 interfacePharmacie;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import java.text.*;
import java.awt.print.*;
import javax.swing.JTable;

/**
 *
 * @author awn27
 */
public class DetailsAchat extends javax.swing.JFrame {

    /**
     * Creates new form DetailsAchat
     */
    public DetailsAchat() {
        initComponents();
        this.setLocationRelativeTo(this);
        this.setResizable(false);
    }

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

        txtNumAchat = new javax.swing.JTextField();
        btnRechercherAchat = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        lblClient = new javax.swing.JLabel();
        lblTelephone = new javax.swing.JLabel();
        lblAdresse = new javax.swing.JLabel();
        lblDate = new javax.swing.JLabel();
        lblTotalAchat = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lblEtat = new javax.swing.JLabel();
        lblDu = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbDetailsAchat = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        lblAvance = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        lblFacture = new javax.swing.JLabel();
        btnPrint = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txtNumAchat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNumAchatActionPerformed(evt);
            }
        });
        getContentPane().add(txtNumAchat, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 50, 176, 35));

        btnRechercherAchat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/loop.png"))); // NOI18N
        btnRechercherAchat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRechercherAchatActionPerformed(evt);
            }
        });
        getContentPane().add(btnRechercherAchat, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 50, 70, 40));

        jLabel1.setText("N° Achat");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(15, 46, 96, 35));

        lblClient.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lblClient.setText("Nom");
        getContentPane().add(lblClient, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 170, 230, -1));

        lblTelephone.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lblTelephone.setText("Téléphone");
        getContentPane().add(lblTelephone, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 210, -1, -1));

        lblAdresse.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lblAdresse.setText("Adresse");
        getContentPane().add(lblAdresse, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 260, -1, -1));

        lblDate.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lblDate.setText("Date");
        getContentPane().add(lblDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 160, 110, 40));

        lblTotalAchat.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lblTotalAchat.setText("Total");
        getContentPane().add(lblTotalAchat, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 240, 90, 20));

        jLabel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Informations Client", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 18))); // NOI18N
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 120, 360, 180));

        lblEtat.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lblEtat.setText("Etat");
        getContentPane().add(lblEtat, new org.netbeans.lib.awtextra.AbsoluteConstraints(740, 160, 120, 30));

        lblDu.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lblDu.setText("Du");
        getContentPane().add(lblDu, new org.netbeans.lib.awtextra.AbsoluteConstraints(800, 210, 50, -1));

        jtbDetailsAchat.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3"
            }
        ));
        jScrollPane1.setViewportView(jtbDetailsAchat);
        if (jtbDetailsAchat.getColumnModel().getColumnCount() > 0) {
            jtbDetailsAchat.getColumnModel().getColumn(1).setResizable(false);
            jtbDetailsAchat.getColumnModel().getColumn(2).setResizable(false);
        }

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 330, 530, 240));

        jLabel4.setFont(new java.awt.Font("Arial Black", 0, 14)); // NOI18N
        jLabel4.setText("NOM :");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 170, -1, -1));

        jLabel5.setFont(new java.awt.Font("Arial Black", 0, 14)); // NOI18N
        jLabel5.setText("TELEPHONE :");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 210, -1, -1));

        jLabel6.setFont(new java.awt.Font("Arial Black", 0, 14)); // NOI18N
        jLabel6.setText("ADRESSE :");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 260, -1, -1));

        lblAvance.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lblAvance.setText("Avance");
        getContentPane().add(lblAvance, new org.netbeans.lib.awtextra.AbsoluteConstraints(800, 240, 70, 30));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel3.setText("Date :");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 170, -1, -1));

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel7.setText("Total :");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 240, -1, -1));

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel8.setText("Avance :");
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(670, 240, -1, 30));

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel9.setText("Somme dûe :");
        getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(670, 210, -1, -1));

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel10.setText("Etat :");
        getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(670, 162, 60, 30));

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
        jLabel11.setText("CFA");
        getContentPane().add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(880, 240, -1, 30));

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
        jLabel12.setText("CFA");
        getContentPane().add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(580, 240, -1, -1));

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
        jLabel13.setText("CFA");
        getContentPane().add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(880, 210, -1, -1));

        jLabel14.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Informations Achat", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 18))); // NOI18N
        getContentPane().add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 130, 590, 170));

        jLabel15.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
        jLabel15.setText("N° Facture :");
        getContentPane().add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(850, 150, 110, 30));

        lblFacture.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lblFacture.setText("n°");
        getContentPane().add(lblFacture, new org.netbeans.lib.awtextra.AbsoluteConstraints(960, 150, 40, 30));

        btnPrint.setBackground(new java.awt.Color(255, 255, 204));
        btnPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/print.png"))); // NOI18N
        btnPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrintActionPerformed(evt);
            }
        });
        getContentPane().add(btnPrint, new org.netbeans.lib.awtextra.AbsoluteConstraints(790, 370, 80, -1));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ACCUEIL.png"))); // NOI18N
        jButton1.setContentAreaFilled(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(810, 30, 70, 60));

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

    private void txtNumAchatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNumAchatActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNumAchatActionPerformed
       
    BDPharmacie bd = new BDPharmacie();
        Connection connex;
        double avance;
    private void btnRechercherAchatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRechercherAchatActionPerformed
                avance = 0;
                 bd=new BDPharmacie();
                 connex=bd.connectToBD();
         String tab[]={"Médicament", "Quantité", "Montant"};
            DefaultTableModel dftm=new DefaultTableModel();
            
            dftm.setColumnIdentifiers(tab); 
    
    int idAchat=Integer.parseInt(txtNumAchat.getText());
    String sql="SELECT cl.prenom, cl.nom, cl.adresse, cl.telephone, ac.date, ac.montant AS total, ac.etat, ac.somme_du, m.nom_medicament, d.quantite, m.pu, (m.pu*d.quantite) AS somme\n" +
            "FROM detail_achat d, achat ac, client cl, medicament m \n" +
            "WHERE d.id_achat=ac.id_achat AND cl.id_client=ac.id_client AND m.id_medicament=d.id_medicament AND d.id_achat=? ";  
    
        try {
                BDPharmacie bd = new BDPharmacie();
                PreparedStatement stm=connex.prepareStatement(sql);
                stm.setInt(1, idAchat); 
                ResultSet rs=stm.executeQuery();           
                while(rs.next()){
                Vector v=new Vector();
           
           String prenom = rs.getString("cl.prenom");
           String nom = rs.getString("cl.nom");
           String date = rs.getString("ac.date");
           String telephone = rs.getString("telephone");
           String adresse = rs.getString("cl.adresse");
           String total = rs.getString("total");
           String medicament = rs.getString("m.nom_medicament");
           String somme_du = rs.getString("ac.somme_du");
           String quantite = rs.getString("d.quantite");
           String montant = rs.getString("m.pu");
           String etat = rs.getString("ac.etat");
           
           
           lblClient.setText(prenom+" "+nom);
           lblTelephone.setText(telephone);
           lblAdresse.setText(adresse);
           lblDate.setText(date);
           lblDu.setText(somme_du);
           lblTotalAchat.setText(total); 
           lblEtat.setText(etat);
          
           v.add(medicament);
           v.add(quantite);
           v.add(montant);
           dftm.addRow(v); 
           }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        jtbDetailsAchat.setModel(dftm); 
    String t = lblTotalAchat.getText();
    String d = lblDu.getText();
     
    avance = Double.parseDouble(t) - Double.parseDouble(d);
    lblAvance.setText(avance+" ");
    String a = String.valueOf(idAchat);
    lblFacture.setText(a);
         
    }//GEN-LAST:event_btnRechercherAchatActionPerformed

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

            MessageFormat header = new MessageFormat("Facture");
            MessageFormat footer = new MessageFormat("Pharmacie Abdoul - Mariama");
            try {
            jtbDetailsAchat.print(JTable.PrintMode.NORMAL, header, footer);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }//GEN-LAST:event_btnPrintActionPerformed
        
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

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

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DetailsAchat().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPrint;
    private javax.swing.JButton btnRechercherAchat;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    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.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtbDetailsAchat;
    private javax.swing.JLabel lblAdresse;
    private javax.swing.JLabel lblAvance;
    private javax.swing.JLabel lblClient;
    private javax.swing.JLabel lblDate;
    private javax.swing.JLabel lblDu;
    private javax.swing.JLabel lblEtat;
    private javax.swing.JLabel lblFacture;
    private javax.swing.JLabel lblTelephone;
    private javax.swing.JLabel lblTotalAchat;
    private javax.swing.JTextField txtNumAchat;
    // End of variables declaration//GEN-END:variables
}
