PRCR_Checkroll_Amalgamation.java Source code

Java tutorial

Introduction

Here is the source code for PRCR_Checkroll_Amalgamation.java

Source

import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ItemEvent;
import java.awt.event.KeyEvent;
import java.io.FileOutputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.*;
import java.awt.print.*;
import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

/*
 * 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.
 */
/**
 *
 * @author Dhananjaya
 */
public class PRCR_Checkroll_Amalgamation extends javax.swing.JFrame {

    /**
     * Creates new form PRCR_Checkroll_Amalgamation
     */
    Date_Handler datehandler = new Date_Handler();
    DateChooser_text datechooser = new DateChooser_text();
    DatabaseManager dbm = DatabaseManager.getDbCon();
    PRCR_Checkroll_Amalgamation_Report rep = new PRCR_Checkroll_Amalgamation_Report();

    public PRCR_Checkroll_Amalgamation() {
        initComponents();
        try {
            this.setIconImage(new ImageIcon(getClass().getResource("Iconpng.png")).getImage());
        } catch (Exception e) {

        }
        jTable1.setAutoCreateRowSorter(true);
        jTable1.getTableHeader().setFont(new Font("Arial", Font.BOLD, 10));

    }

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

        jPanel1 = new javax.swing.JPanel();
        datepanel1 = new javax.swing.JPanel();
        monthfield2 = new javax.swing.JTextField();
        yearfield2 = new javax.swing.JTextField();
        datePick2 = new com.michaelbaranov.microba.calendar.DatePicker();
        jLabel2 = new javax.swing.JLabel();
        division_jc = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        workDays = new javax.swing.JTextField();
        workDaysPay = new javax.swing.JTextField();
        OTHours = new javax.swing.JTextField();
        OThoursPay = new javax.swing.JTextField();
        COins = new javax.swing.JTextField();
        GrandTotal = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        datepanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        monthfield2.setText(datehandler.get_today_month());
        monthfield2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                monthfield2KeyPressed(evt);
            }
        });

        yearfield2.setText(datehandler.get_today_year());
        yearfield2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                yearfield2KeyPressed(evt);
            }
        });

        datePick2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                datePick2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout datepanel1Layout = new javax.swing.GroupLayout(datepanel1);
        datepanel1.setLayout(datepanel1Layout);
        datepanel1Layout
                .setHorizontalGroup(datepanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(datepanel1Layout.createSequentialGroup().addGap(23, 23, 23)
                                .addComponent(monthfield2, javax.swing.GroupLayout.PREFERRED_SIZE, 43,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(yearfield2, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(24, 24, 24)
                                .addComponent(datePick2, javax.swing.GroupLayout.PREFERRED_SIZE, 42,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        datepanel1Layout.setVerticalGroup(datepanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(datepanel1Layout.createSequentialGroup().addGap(0, 0, 0).addGroup(datepanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(datePick2, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(datepanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(monthfield2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(yearfield2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        jLabel2.setText("Division");

        division_jc.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
        division_jc.setEditable(true);
        division_jc.setModel(new javax.swing.DefaultComboBoxModel(dbm.getStringArray("division_details", "code")));
        division_jc.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                division_jcItemStateChanged(evt);
            }
        });
        division_jc.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                division_jcKeyPressed(evt);
            }
        });

        jTable1.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null },
                        { null, null, null, null, null, null, null, null } },
                new String[] { "Work Code", "Section", "Work Days", "Work Days Pay", "OT Hours", "OT Hours Pay",
                        "Coins", "Grand Total" }) {
            Class[] types = new Class[] { java.lang.Object.class, java.lang.Object.class, java.lang.Integer.class,
                    java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Object.class,
                    java.lang.Double.class };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);
        if (jTable1.getColumnModel().getColumnCount() > 0) {
            jTable1.getColumnModel().getColumn(0).setPreferredWidth(70);
            jTable1.getColumnModel().getColumn(1).setPreferredWidth(150);
            jTable1.getColumnModel().getColumn(2).setPreferredWidth(55);
            jTable1.getColumnModel().getColumn(3).setPreferredWidth(70);
            jTable1.getColumnModel().getColumn(4).setPreferredWidth(55);
            jTable1.getColumnModel().getColumn(5).setPreferredWidth(70);
            jTable1.getColumnModel().getColumn(6).setPreferredWidth(40);
        }

        jButton1.setText("View");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        workDays.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                workDaysActionPerformed(evt);
            }
        });

        workDaysPay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                workDaysPayActionPerformed(evt);
            }
        });

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

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
                                false)
                        .addGroup(jPanel1Layout.createSequentialGroup().addGap(41, 41, 41).addComponent(jLabel2)
                                .addGap(34, 34, 34)
                                .addComponent(division_jc, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(70, 70, 70)
                                .addComponent(datepanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(108, 108, 108)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 75,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton2))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(
                                        jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 595,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout
                                        .createSequentialGroup().addGap(299, 299, 299)
                                        .addComponent(workDays, javax.swing.GroupLayout.PREFERRED_SIZE, 87,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(workDaysPay, javax.swing.GroupLayout.PREFERRED_SIZE, 79,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(OTHours, javax.swing.GroupLayout.PREFERRED_SIZE, 79,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(OThoursPay, javax.swing.GroupLayout.PREFERRED_SIZE, 79,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(COins, javax.swing.GroupLayout.PREFERRED_SIZE, 79,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(GrandTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 87,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(42, Short.MAX_VALUE)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup().addGap(53, 53, 53)
                                        .addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel2).addComponent(division_jc,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(jPanel1Layout.createSequentialGroup().addGap(43, 43, 43)
                                        .addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(jPanel1Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jButton1).addComponent(jButton2))
                                                .addComponent(datepanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        40, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(27, 27, 27)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(workDays, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(workDaysPay, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(OTHours, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(OThoursPay, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(COins, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(GrandTotal, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(43, Short.MAX_VALUE)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)));

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

    private void monthfield2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_monthfield2KeyPressed
        if (monthfield2.getText().equals("Jan")) {
            if (evt.getKeyCode() == KeyEvent.VK_UP) {
                monthfield2.setText("Dec");
                int yr = Integer.parseInt(yearfield2.getText());

                yearfield2.setText("" + (yr - 1));
                monthfield2.selectAll();

            }
            if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                monthfield2.setText("Feb");
                monthfield2.selectAll();
            }

        } else if (monthfield2.getText().equals("Feb")) {
            if (evt.getKeyCode() == KeyEvent.VK_UP) {
                monthfield2.setText("Jan");
                int yr = Integer.parseInt(yearfield2.getText());
                monthfield2.selectAll();

            }
            if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                monthfield2.setText("Mar");
                monthfield2.selectAll();
            }

        } else if (monthfield2.getText().equals("Mar")) {
            if (evt.getKeyCode() == KeyEvent.VK_UP) {
                monthfield2.setText("Feb");
                int yr = Integer.parseInt(yearfield2.getText());
                monthfield2.selectAll();
            }
            if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                monthfield2.setText("Apr");
                monthfield2.selectAll();
            }

        } else if (monthfield2.getText().equals("Apr")) {
            if (evt.getKeyCode() == KeyEvent.VK_UP) {
                monthfield2.setText("Mar");
                int yr = Integer.parseInt(yearfield2.getText());
                monthfield2.selectAll();
            }
            if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                monthfield2.setText("May");
                monthfield2.selectAll();
            }

        } else if (monthfield2.getText().equals("May")) {
            if (evt.getKeyCode() == KeyEvent.VK_UP) {
                monthfield2.setText("Apr");
                int yr = Integer.parseInt(yearfield2.getText());
                monthfield2.selectAll();

            }
            if (evt.getKeyCode() == KeyEvent.VK_DOWN) {

                monthfield2.setText("Jun");
                monthfield2.selectAll();
            }

        } else if (monthfield2.getText().equals("Jun")) {
            if (evt.getKeyCode() == KeyEvent.VK_UP) {
                monthfield2.setText("May");
                int yr = Integer.parseInt(yearfield2.getText());
                monthfield2.selectAll();

            }
            if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                monthfield2.setText("Jul");
                monthfield2.selectAll();
            }

        } else if (monthfield2.getText().equals("Jul")) {
            if (evt.getKeyCode() == KeyEvent.VK_UP) {
                monthfield2.setText("Jun");
                int yr = Integer.parseInt(yearfield2.getText());
                monthfield2.selectAll();

            }
            if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                monthfield2.setText("Aug");
                monthfield2.selectAll();
            }

        } else if (monthfield2.getText().equals("Aug")) {
            if (evt.getKeyCode() == KeyEvent.VK_UP) {
                monthfield2.setText("Jul");
                int yr = Integer.parseInt(yearfield2.getText());
                monthfield2.selectAll();

            }
            if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                monthfield2.setText("Sep");
                monthfield2.selectAll();
            }

        } else if (monthfield2.getText().equals("Sep")) {
            if (evt.getKeyCode() == KeyEvent.VK_UP) {
                monthfield2.setText("Aug");
                int yr = Integer.parseInt(yearfield2.getText());
                monthfield2.selectAll();

            }
            if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                monthfield2.setText("Oct");
                monthfield2.selectAll();
            }

        } else if (monthfield2.getText().equals("Oct")) {
            if (evt.getKeyCode() == KeyEvent.VK_UP) {
                monthfield2.setText("Sep");
                int yr = Integer.parseInt(yearfield2.getText());
                monthfield2.selectAll();

            }
            if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                monthfield2.setText("Nov");
                monthfield2.selectAll();
            }

        } else if (monthfield2.getText().equals("Nov")) {
            if (evt.getKeyCode() == KeyEvent.VK_UP) {
                monthfield2.setText("Oct");
                int yr = Integer.parseInt(yearfield2.getText());
                monthfield2.selectAll();

            }
            if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                monthfield2.setText("Dec");
                monthfield2.selectAll();
            }

        } else if (monthfield2.getText().equals("Dec")) {
            if (evt.getKeyCode() == KeyEvent.VK_UP) {
                monthfield2.setText("Nov");
                int yr = Integer.parseInt(yearfield2.getText());
                monthfield2.selectAll();

            }
            if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                monthfield2.setText("Jan");
                int yr = Integer.parseInt(yearfield2.getText());

                yearfield2.setText("" + (yr + 1));
                monthfield2.selectAll();
            }

        }
        if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
            //dayfield2.requestFocus();
            //dayfield2.selectAll();
        }
        if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
            yearfield2.requestFocus();
            yearfield2.selectAll();
        }

        if (evt.getKeyCode() == KeyEvent.VK_ENTER) { ////// ChaNGE  focus on enter////////////////
            //category_code.requestFocus();

        }
    }//GEN-LAST:event_monthfield2KeyPressed

    private void yearfield2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_yearfield2KeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
            yearfield2.setText("" + (Integer.parseInt(yearfield2.getText()) + 1));
            yearfield2.selectAll();
        }
        if (evt.getKeyCode() == KeyEvent.VK_UP) {
            yearfield2.setText("" + (Integer.parseInt(yearfield2.getText()) - 1));
            yearfield2.selectAll();
        }
        if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
            monthfield2.requestFocus();
            monthfield2.selectAll();
        }

        if (evt.getKeyCode() == KeyEvent.VK_ENTER) { ////// ChaNGE  focus on enter////////////////
            //  category_code.requestFocus();

        }
    }//GEN-LAST:event_yearfield2KeyPressed

    private void datePick2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_datePick2ActionPerformed
        java.sql.Date datef = new java.sql.Date(datePick2.getDate().getTime());

        // dayfield2.setText(datehandler.get_day(datef));
        monthfield2.setText(datehandler.get_month(datef));
        yearfield2.setText(datehandler.get_year(datef));
        // category_code.requestFocus();
    }//GEN-LAST:event_datePick2ActionPerformed

    private void division_jcItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_division_jcItemStateChanged
        DatabaseManager dbma = DatabaseManager.getDbCon();
        String Name = null;
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            String item = evt.getItem().toString();

            try {
                ResultSet query = dbma.query("SELECT * FROM division_details WHERE code =" + item + "");
                while (query.next()) {
                    Name = query.getString("division");

                }
            } catch (SQLException ex) {
                System.out.println("error");
            }

            // division_lb.setText("" + Name);
        }

        //empCode_JC.requestFocus();
    }//GEN-LAST:event_division_jcItemStateChanged

    private void division_jcKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_division_jcKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) { ////// ChaNGE  focus on enter////////////////
            //  empCode_JC.requestFocus();

        } // TODO add your handling code here:
    }//GEN-LAST:event_division_jcKeyPressed

    public void ClearTable(int columnsize) {
        int rows = 0;
        int column = 0;

        for (int i = 0; i < columnsize; i++) {
            for (column = 0; column < 8; column++) {
                jTable1.setValueAt(null, rows, column);
            }
            rows++;

        }
        // this.rows=0;
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // ClearTable();

        //createNewDatabaseTableforAmalgamation();

        jTable1.setBackground(Color.WHITE);
        jTable1.setForeground(Color.BLACK);

        JTableHeader header = jTable1.getTableHeader();
        header.setBackground(Color.white);
        header.setForeground(Color.black);

        jTable1.getTableHeader().setFont(new Font("Arial", Font.PLAIN, 7));
        int columnsize = getColumnsize("workcode_details", "code");

        String workCode[] = new String[columnsize];
        String Section[] = new String[columnsize];
        int j = 0;

        workCode = getStringArray("workcode_details", "code");
        Section = getStringArray("workcode_details", "work");
        // ((DefaultTableModel) jTable1.getModel()).setNumRows(columnsize + 2);
        // ClearTable(columnsize + 2);
        double normaldays = 0;
        double sundays = 0;
        double otnighthrs = 0;
        double otdayhrs = 0;
        double normalDaysrate = Double
                .parseDouble(dbm.checknReturnData("checkroll_pay_info", "checkroll", 1, "normalday_rate"));
        double sundayrate = Double
                .parseDouble(dbm.checknReturnData("checkroll_pay_info", "checkroll", 1, "sunday_rate"));
        double otDayrate = Double
                .parseDouble(dbm.checknReturnData("checkroll_pay_info", "checkroll", 1, "otrate_before"));
        double otNightRate = Double
                .parseDouble(dbm.checknReturnData("checkroll_pay_info", "checkroll", 1, "otrate_after"));

        double workdaysT = 0;
        double workdayspayT = 0;
        double othoursT = 0;
        double othourspayT = 0;
        double coinsT = 0;
        double grandtotalT = 0;

        /*   int k=checknReturnNoOfData("prcr_checkroll_workentry", "date", "2014-03",
         "division", "BG", "work_code", "ABVF", "normalday_or_sunday", "n");
         System.out.println(k);*/
        for (int i = 0; i < columnsize; i++) {

            normaldays = checknReturnNoOfData("prcr_checkroll_workentry", "date",
                    getST(monthfield2.getText(), yearfield2.getText()), "division",
                    division_jc.getSelectedItem().toString(), "work_code", workCode[i], "normalday_or_sunday", "n");

            sundays = checknReturnNoOfData("prcr_checkroll_workentry", "date",
                    getST(monthfield2.getText(), yearfield2.getText()), "division",
                    division_jc.getSelectedItem().toString(), "work_code", workCode[i], "normalday_or_sunday", "s");
            otdayhrs = checknReturnTotal("prcr_checkroll_workentry", "date",
                    getST(monthfield2.getText(), yearfield2.getText()), "division",
                    division_jc.getSelectedItem().toString(), "work_code", workCode[i], "ot_day");
            otnighthrs = checknReturnTotal("prcr_checkroll_workentry", "date",
                    getST(monthfield2.getText(), yearfield2.getText()), "division",
                    division_jc.getSelectedItem().toString(), "work_code", workCode[i], "ot_night");

            if (normaldays != 0 || sundays != 0 || otdayhrs != 0 || otnighthrs != 0) {

                j++;

            }
        }

        ((DefaultTableModel) jTable1.getModel()).setNumRows(j + 3);
        ClearTable(j + 3);
        j = 0;

        for (int i = 0; i < columnsize; i++) {

            normaldays = checknReturnNoOfData("prcr_checkroll_workentry", "date",
                    getST(monthfield2.getText(), yearfield2.getText()), "division",
                    division_jc.getSelectedItem().toString(), "work_code", workCode[i], "normalday_or_sunday", "n");

            sundays = checknReturnNoOfData("prcr_checkroll_workentry", "date",
                    getST(monthfield2.getText(), yearfield2.getText()), "division",
                    division_jc.getSelectedItem().toString(), "work_code", workCode[i], "normalday_or_sunday", "s");
            otdayhrs = checknReturnTotal("prcr_checkroll_workentry", "date",
                    getST(monthfield2.getText(), yearfield2.getText()), "division",
                    division_jc.getSelectedItem().toString(), "work_code", workCode[i], "ot_day");
            otnighthrs = checknReturnTotal("prcr_checkroll_workentry", "date",
                    getST(monthfield2.getText(), yearfield2.getText()), "division",
                    division_jc.getSelectedItem().toString(), "work_code", workCode[i], "ot_night");

            if (normaldays != 0 || sundays != 0 || otdayhrs != 0 || otnighthrs != 0) {

                jTable1.setValueAt(workCode[i], j, 0);
                jTable1.setValueAt(Section[i], j, 1);

                jTable1.setValueAt(normaldays + sundays, j, 2);
                workdaysT = workdaysT + (normaldays + sundays);
                jTable1.setValueAt(normaldays * normalDaysrate + sundays * sundayrate, j, 3);
                workdayspayT = workdayspayT + (normaldays * normalDaysrate + sundays * sundayrate);

                jTable1.setValueAt(otdayhrs + otnighthrs, j, 4);
                othoursT = othoursT + (otdayhrs + otnighthrs);
                jTable1.setValueAt(otdayhrs * otDayrate + otnighthrs * otNightRate, j, 5);
                othourspayT = othourspayT + (otdayhrs * otDayrate + otnighthrs * otNightRate);

                jTable1.setValueAt(normaldays * normalDaysrate + sundays * sundayrate + otdayhrs * otDayrate
                        + otnighthrs * otNightRate, j, 7);
                grandtotalT = grandtotalT + (normaldays * normalDaysrate + sundays * sundayrate
                        + otdayhrs * otDayrate + otnighthrs * otNightRate);
                j++;
                //write data in to database table for amalgamation to generate reports
                //                 try {
                //                     double workdayss=normaldays + sundays;
                //                     double workdayspayy=normaldays * normalDaysrate + sundays * sundayrate;
                //                     double othourss=otdayhrs + otnighthrs;
                //                     double othourspayy=otdayhrs * otDayrate + otnighthrs * otNightRate;
                //                     double grandtotall=normaldays * normalDaysrate + sundays * sundayrate + otdayhrs * otDayrate + otnighthrs * otNightRate;
                //                    dbm.insert("INSERT INTO prcr_checkroll_amalgamation_report(work_code,section,work_days,work_days_pay,ot_hours,ot_hours_pay,grand_total) VALUES('" + workCode[i]+ "','" + Section[i] + "','" + workdayss + "','" + workdayspayy + "','" + othourss + "','" + othourspayy + "','"+grandtotall+"')");
                //                } catch (SQLException ex) {
                //                     System.out.println("error-couldnt write data in to amalgamation database table");
                //                }

            }
        }

        jTable1.setValueAt(workdaysT, j + 2, 2);
        jTable1.setValueAt(workdayspayT, j + 2, 3);
        jTable1.setValueAt(othoursT, j + 2, 4);
        jTable1.setValueAt(othourspayT, j + 2, 5);
        jTable1.setValueAt(grandtotalT, j + 2, 7);

        workDays.setText(String.valueOf(workdaysT));
        workDaysPay.setText(String.valueOf(workdayspayT));
        OTHours.setText(String.valueOf(othoursT));
        OThoursPay.setText(String.valueOf(othourspayT));
        GrandTotal.setText(String.valueOf(grandtotalT));
        /*  double workdays=0;
         double workdayspay=0;
         double othours=0;
         double othourspay=0;
         double coins=0;
         double grandtotal=0;
         int k=0;
         while(jTable1.getValueAt(k, 0)!=null){
         workdays=workdays+Double.parseDouble(jTable1.getValueAt(k,2).toString());
         workdayspay=workdayspay+Double.parseDouble(jTable1.getValueAt(k, 3).toString());
         othours=othours+Double.parseDouble(jTable1.getValueAt(k,4).toString());
         othourspay=othourspay+Double.parseDouble(jTable1.getValueAt(k,5).toString());
         // coins=coins+Double.parseDouble(jTable1.getValueAt(k,6).toString());
         grandtotal=grandtotal+Double.parseDouble(jTable1.getValueAt(k,7).toString());
         }
            
         workDays.setText(String.valueOf(workdays));
         workDaysPay.setText(String.valueOf(workdayspay));
         OTHours.setText(String.valueOf(othours));
         OThoursPay.setText(String.valueOf(othourspay));
         GrandTotal.setText(String.valueOf(grandtotal)); */

    }//GEN-LAST:event_jButton1ActionPerformed
    //    public void createNewDatabaseTableforAmalgamation(){
    //       
    //     try {
    //         dbm.DeleteTable("prcr_checkroll_amalgamation_report");
    //            //use new_1 and new_2 if any other deduction type is needd to be added
    //            dbm.insert("CREATE TABLE prcr_checkroll_amalgamation_report(work_code VARCHAR(30),"
    //                    + "section VARCHAR(50)," + "work_days DOUBLE,"
    //                    + "work_days_pay DOUBLE," + "ot_hours DOUBLE," + "ot_hours_pay DOUBLE,"
    //                    + "coins DOUBLE," + "grand_total DOUBLE);");
    //        } catch (SQLException ex) {
    //            //Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
    //            System.err.println(ex.getMessage());
    //        }
    //    
    //    }

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

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

    private void print() {
        Document document = new Document(PageSize.A4.rotate());
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:/Amalgamation.pdf"));

            document.open();
            PdfContentByte cb = writer.getDirectContent();

            cb.saveState();
            Graphics2D g2 = cb.createGraphicsShapes(1800, 750);

            Shape oldClip = g2.getClip();
            g2.clipRect(0, 0, 1800, 750);

            jTable1.print(g2);
            g2.setClip(oldClip);

            g2.dispose();
            cb.restoreState();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        document.close();
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        //       print(); 
        rep.CreateReport(division_jc.getSelectedItem().toString(),
                getST(monthfield2.getText(), yearfield2.getText()),
                getST(monthfield2.getText(), yearfield2.getText()));
    }//GEN-LAST:event_jButton2ActionPerformed
    // Date column is the column in your database which dates are entered
    // String month is the month you want to check. Give month as a string ex:- 01 for January, 09 for September
    // String column_need_to_check is the column that you need to filter in your case the column that has n's and s's :)
    // Object element is the element you need to check in that column. Ex:- Either "s" or "n"

    public int checknReturnNoOfData(String table_name, String date_column, String st, String divCodeColumn,
            String divCode, String workCodeColumn, String workCode, String column_need_to_check,
            Object element_to_check) {
        DatabaseManager dbm = DatabaseManager.getDbCon();
        int count = 0;
        try {
            ResultSet query = dbm.query("SELECT * FROM " + table_name + "");
            while (query.next()) {
                //                 System.out.println(query.getString(date_column).substring(0,7)+"  "+st);
                //                 System.out.println(query.getString(divCodeColumn)+" "+divCode);
                //                 System.out.println(query.getString(workCodeColumn)+"  "+workCode);
                //                 System.out.println(query.getString(column_need_to_check)+"  "+element_to_check);
                //                 System.out.println(query.getString(date_column).substring(0, 7).equals(st) && query.getString(divCodeColumn).equals(divCode) && query.getString(workCodeColumn).equals(workCode) && query.getString(column_need_to_check).equals(element_to_check));
                //                 
                //System.out.println("gssgsdg   gsdg "+division_jc.getSelectedItem().toString());
                if (query.getString(date_column).substring(0, 7).equals(st)
                        && query.getString(divCodeColumn).equals(divCode)
                        && query.getString(workCodeColumn).equals(workCode)
                        && query.getString(column_need_to_check).equals(element_to_check)) {
                    count++;

                }
            }
            return count;
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());

        }
        return count;
    }

    // column_need_to_get_total is Either Day OT hour column or Night OT hour column.. All other parameters are similar to the above method
    public double checknReturnTotal(String table_name, String date_column, String st, String divCodeColumn,
            String divCode, String workCodeColumn, String workCode, String column_need_to_get_total) {
        DatabaseManager dbm = DatabaseManager.getDbCon();
        double tot = 0;
        try {
            ResultSet query = dbm.query("SELECT * FROM " + table_name + "");
            while (query.next()) {
                if (query.getString(date_column).substring(0, 7).equals(st)
                        && query.getString(divCodeColumn).equals(divCode)
                        && query.getString(workCodeColumn).equals(workCode)) {
                    tot = tot + query.getDouble(column_need_to_get_total);
                }
            }
            return tot;
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());

        }
        return tot;
    }

    public String getST(String month, String year) {
        String st;
        if (datehandler.return_index(month) < 10) {
            st = year + "-0" + datehandler.return_index(month);
        } else {
            st = year + "-" + datehandler.return_index(month);
        }

        return st;
    }

    public String[] getStringArray(String table_name, String column_name) {

        int count = 0;
        DatabaseManager dbm = DatabaseManager.getDbCon();
        try {
            ResultSet query = dbm.query("SELECT " + column_name + " FROM " + table_name + "");
            while (query.next()) {
                count++;
            }
            String[] array = new String[count];
            count = 0;
            ResultSet query2 = dbm.query("SELECT " + column_name + " FROM " + table_name + "");
            while (query2.next()) {
                array[count] = query2.getString(column_name);
                count++;
            }
            return array;
        } catch (SQLException ex) {

        }
        return null;

    }

    public int getColumnsize(String table_name, String column_name) {

        int count = 0;
        DatabaseManager dbm = DatabaseManager.getDbCon();
        try {
            ResultSet query = dbm.query("SELECT " + column_name + " FROM " + table_name + "");
            while (query.next()) {
                count++;
            }

        } catch (SQLException ex) {

        }
        return count;
        //return null;

    }

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField COins;
    private javax.swing.JTextField GrandTotal;
    private javax.swing.JTextField OTHours;
    private javax.swing.JTextField OThoursPay;
    private com.michaelbaranov.microba.calendar.DatePicker datePick2;
    private javax.swing.JPanel datepanel1;
    private javax.swing.JComboBox division_jc;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField monthfield2;
    private javax.swing.JTextField workDays;
    private javax.swing.JTextField workDaysPay;
    private javax.swing.JTextField yearfield2;
    // End of variables declaration//GEN-END:variables
}