ManagementPackage.Setting.java Source code

Java tutorial

Introduction

Here is the source code for ManagementPackage.Setting.java

Source

package ManagementPackage;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Desktop;
import java.awt.Font;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.proteanit.sql.DbUtils;

/*
 * 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 KD
 */
public class Setting extends javax.swing.JFrame {

    private Connection con = null;
    private ResultSet rs = null;
    private PreparedStatement pst = null;

    private Connection con1 = null;
    private ResultSet rs1 = null;
    private PreparedStatement pst1 = null;

    float totalAdjustment;
    float totalTotalPaid;
    float total;

    String formatedDate = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
    String formatedDateTime = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.S").format(new Date());

    String month = formatedDate.substring(2, 6);
    String year = formatedDate.substring(6, 10);

    /**
     * Creates new form Setting
     */
    public Setting() {
        initComponents();
        con = SQLiteConnection.connectorDB();
        con1 = SQLiteConnection.connectorDB();
        productTable();
        //JOptionPane.showMessageDialog(null, year);
    }

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

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jPopupMenu2 = new javax.swing.JPopupMenu();
        jPopupMenu3 = new javax.swing.JPopupMenu();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        txtSearchProduct = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblSearchProduct = new javax.swing.JTable();
        txtAddNewProduct = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

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

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setBackground(new java.awt.Color(0, 0, 0));
        jPanel2.setPreferredSize(new java.awt.Dimension(813, 450));

        txtSearchProduct.setBackground(new java.awt.Color(0, 0, 0));
        txtSearchProduct.setFont(new java.awt.Font("Segoe UI Light", 0, 18)); // NOI18N
        txtSearchProduct.setForeground(new java.awt.Color(255, 255, 255));
        txtSearchProduct.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSearchProductKeyReleased(evt);
            }
        });

        tblSearchProduct.setBackground(new java.awt.Color(0, 0, 0));
        tblSearchProduct.setFont(new java.awt.Font("Segoe UI Light", 0, 18)); // NOI18N
        tblSearchProduct.setForeground(new java.awt.Color(255, 255, 255));
        tblSearchProduct.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 } },
                new String[] { "Title 1", "Product Name", "Buying Rate", "Selling Rate", "Amount" }) {
            boolean[] canEdit = new boolean[] { false, false, false, false, false };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        tblSearchProduct.setGridColor(new java.awt.Color(153, 153, 153));
        tblSearchProduct.setRowHeight(29);
        tblSearchProduct.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblSearchProductMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblSearchProduct);

        txtAddNewProduct.setBackground(new java.awt.Color(0, 0, 0));
        txtAddNewProduct.setFont(new java.awt.Font("Segoe UI Light", 0, 18)); // NOI18N
        txtAddNewProduct.setForeground(new java.awt.Color(255, 255, 255));
        txtAddNewProduct.setText("Add New Product");
        txtAddNewProduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtAddNewProductActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ManagementPackage/3.jpg"))); // NOI18N

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ManagementPackage/4.jpg"))); // NOI18N
        jLabel2.setText("jLabel2");

        jLabel5.setBackground(new java.awt.Color(0, 0, 0));
        jLabel5.setFont(new java.awt.Font("Segoe UI Light", 0, 36)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(51, 102, 255));
        jLabel5.setText("Shop Management System");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel1).addGroup(jPanel2Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(txtSearchProduct, javax.swing.GroupLayout.Alignment.TRAILING,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 693,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(txtAddNewProduct).addComponent(jScrollPane1,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 693,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel2).addGap(20, 20, 20))
                        .addGroup(jPanel2Layout.createSequentialGroup().addGap(151, 151, 151).addComponent(jLabel5)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                        jPanel2Layout.createSequentialGroup().addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtAddNewProduct).addGap(40, 40, 40))
                .addGroup(jPanel2Layout.createSequentialGroup().addGap(13, 13, 13).addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(txtSearchProduct, javax.swing.GroupLayout.PREFERRED_SIZE, 33,
                                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, 320,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(91, 91, 91))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                        jPanel2Layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel2).addGap(28, 28, 28)));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jPanel2,
                        javax.swing.GroupLayout.PREFERRED_SIZE, 1133, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)));
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel2,
                        javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE));

        jMenuBar1.setBackground(new java.awt.Color(0, 0, 0));
        jMenuBar1.setForeground(new java.awt.Color(255, 255, 255));

        jMenu1.setText("Extras");

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A,
                java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem3.setText("Add New Product");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C,
                java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Create User");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D,
                java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem7.setText("Delete User");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem7);

        jMenu2.setText("Reports");

        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R,
                java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem4.setText("Daily Report");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem4);

        jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M,
                java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem5.setText("Monthly Report");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem5);

        jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Y,
                java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem6.setText("Yearly Report");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem6);

        jMenu1.add(jMenu2);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L,
                java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Logout");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

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

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

    private void txtAddNewProductActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtAddNewProductActionPerformed
        // TODO add your handling code here:
        AddNewProduct anp = new AddNewProduct();
        anp.setVisible(true);
        dispose();
    }//GEN-LAST:event_txtAddNewProductActionPerformed

    private void txtSearchProductKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSearchProductKeyReleased
        // TODO add your handling code here:
        String query = "select id, product_name, buying_rate, selling_rate, qty from products_list where product_name like ?";
        try {
            String tsp = txtSearchProduct.getText() + '%';
            pst = con.prepareStatement(query);
            pst.setString(1, tsp);
            rs = pst.executeQuery();
            tblSearchProduct.setModel(DbUtils.resultSetToTableModel(rs));

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }//GEN-LAST:event_txtSearchProductKeyReleased

    private void tblSearchProductMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblSearchProductMouseClicked
        // TODO add your handling code here:
        int row = tblSearchProduct.getSelectedRow();
        String tableClick = tblSearchProduct.getModel().getValueAt(row, 0).toString();
        String query = "select * from products_list where id = '" + tableClick + "'";

        try {
            pst = con.prepareStatement(query);
            rs = pst.executeQuery();

            if (rs.next()) {
                String id = rs.getString("id");
                String pName = rs.getString("product_name");
                String bRate = rs.getString("buying_rate");
                String sRate = rs.getString("Selling_rate");
                String qty = rs.getString("qty");

                //JOptionPane.showMessageDialog(null, pName);
                EditNewProduct enp = new EditNewProduct(id, pName, bRate, sRate, qty);
                con.close();
                rs.close();
                pst.close();

                enp.setVisible(true);
                dispose();
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }

    }//GEN-LAST:event_tblSearchProductMouseClicked

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

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        // TODO add your handling code here:
        totalAdjustment = 0;
        totalTotalPaid = 0;
        total = 0;

        Document doc = new Document();
        try {
            long time = new Date().getTime();
            PdfWriter.getInstance(doc, new FileOutputStream("Reports\\Daily" + time + " " + formatedDate + ".pdf"));
            doc.open();

            PdfPTable table = new PdfPTable(5);
            PdfPCell cell1 = new PdfPCell(new Paragraph("Shop Management System \n\n",
                    FontFactory.getFont(FontFactory.TIMES_BOLD, 20, Font.BOLD, BaseColor.WHITE)));
            cell1.setColspan(10);
            cell1.setPadding(10);
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell1.setBackgroundColor(BaseColor.BLACK);
            table.addCell(cell1);

            PdfPCell cell21 = new PdfPCell(new Paragraph("\n\n"));
            cell21.setColspan(10);
            cell21.setBorder(2);
            cell21.setBorderColorLeft(BaseColor.WHITE);
            table.addCell(cell21);

            PdfPCell cell2 = new PdfPCell(new Paragraph("Daily Report\n"));
            cell2.setColspan(10);
            cell2.setPadding(10);
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell2.setBackgroundColor(BaseColor.LIGHT_GRAY);
            table.addCell(cell2);

            PdfPCell cell4 = new PdfPCell(new Paragraph("Date: " + formatedDateTime));
            cell4.setColspan(10);
            cell4.setPaddingBottom(10);
            cell4.setPaddingTop(10);
            cell4.setBorder(2);
            cell4.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cell4.setBorderColorRight(BaseColor.WHITE);
            table.addCell(cell4);

            table.addCell("Transaction No");
            table.addCell("Billed By");
            table.addCell("Product Name");
            table.addCell("Qty");
            table.addCell("Amount (TK)");

            String PrevTransNo = " ";
            String query = "select trans_no, product_name, qty, amount, trans_by from trans_details where date = '"
                    + formatedDate + "'";
            try {
                pst = con.prepareStatement(query);
                rs = pst.executeQuery();
                while (rs.next()) {
                    String NewTransNo = rs.getString("trans_no");
                    String product_name = rs.getString("product_name");
                    String qty = rs.getString("qty");
                    String amount = rs.getString("amount");
                    String trans_by = rs.getString("trans_by");

                    total = total + Float.parseFloat(amount);

                    if (PrevTransNo.equals(NewTransNo))
                        table.addCell(" ");
                    else {
                        table.addCell(NewTransNo);
                        String query1 = "select adjustment, total_paid from paid_amount where trans_no = '"
                                + NewTransNo + "'";
                        pst1 = con1.prepareStatement(query1);
                        rs1 = pst1.executeQuery();
                        String adjustment = rs1.getString("adjustment");
                        String total_paid = rs1.getString("total_paid");

                        totalAdjustment = totalAdjustment + Float.parseFloat(adjustment);
                        totalTotalPaid = totalTotalPaid + Float.parseFloat(total_paid);

                        rs1.close();
                        pst1.close();
                    }

                    PrevTransNo = rs.getString("trans_no");
                    table.addCell(trans_by);
                    table.addCell(product_name);
                    table.addCell(qty);

                    PdfPCell cellAmount = new PdfPCell(new Paragraph(amount));
                    cellAmount.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cellAmount);

                }
                rs.close();
                pst.close();

            } catch (SQLException ex) {
                Logger.getLogger(ServiceEnd.class.getName()).log(Level.SEVERE, null, ex);
            }

            PdfPCell cellb = new PdfPCell(new Paragraph(" "));
            cellb.setColspan(10);
            cellb.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(cellb);

            //
            PdfPCell celltxtTotal = new PdfPCell(new Paragraph("Total"));
            celltxtTotal.setColspan(3);
            celltxtTotal.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtTotal);

            PdfPCell celltxtTotal1 = new PdfPCell(new Paragraph("" + total));
            celltxtTotal1.setColspan(2);
            celltxtTotal1.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtTotal1);

            PdfPCell celltxtAdjust = new PdfPCell(new Paragraph("Adjustment"));
            celltxtAdjust.setColspan(3);
            celltxtAdjust.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtAdjust);

            PdfPCell celltxtAdjust1 = new PdfPCell(new Paragraph("" + totalAdjustment));
            celltxtAdjust1.setColspan(2);
            celltxtAdjust1.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtAdjust1);

            PdfPCell celltxtTotalPaid = new PdfPCell(new Paragraph("Total Paid"));
            celltxtTotalPaid.setColspan(3);
            celltxtTotalPaid.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtTotalPaid);

            PdfPCell celltxtTotalPaid1 = new PdfPCell(new Paragraph("" + totalTotalPaid));
            celltxtTotalPaid1.setColspan(2);
            celltxtTotalPaid1.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtTotalPaid1);

            doc.add(table);

            doc.close();

            //JOptionPane.showMessageDialog(null, "Report Created!");
            // open PDF file
            File file = new File("Reports\\Daily" + time + " " + formatedDate + ".pdf");
            if (file.toString().endsWith(".pdf"))
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file);
            else {
                Desktop desktop = Desktop.getDesktop();
                desktop.open(file);
            }

        } catch (DocumentException ex) {
            Logger.getLogger(ServiceEnd.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ServiceEnd.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ServiceEnd.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jMenuItem4ActionPerformed

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

        // TODO add your handling code here:
        totalAdjustment = 0;
        totalTotalPaid = 0;
        total = 0;

        Document doc = new Document();
        try {
            long time = new Date().getTime();
            PdfWriter.getInstance(doc,
                    new FileOutputStream("Reports\\Monthly" + time + " " + formatedDate + ".pdf"));
            doc.open();

            PdfPTable table = new PdfPTable(5);
            PdfPCell cell1 = new PdfPCell(new Paragraph("Shop Management System \n\n",
                    FontFactory.getFont(FontFactory.TIMES_BOLD, 20, Font.BOLD, BaseColor.WHITE)));
            cell1.setColspan(10);
            cell1.setPadding(10);
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell1.setBackgroundColor(BaseColor.BLACK);
            table.addCell(cell1);

            PdfPCell cell21 = new PdfPCell(new Paragraph("\n\n"));
            cell21.setColspan(10);
            cell21.setBorder(2);
            cell21.setBorderColorLeft(BaseColor.WHITE);
            table.addCell(cell21);

            PdfPCell cell2 = new PdfPCell(new Paragraph("Monthly Report\n"));
            cell2.setColspan(10);
            cell2.setPadding(10);
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell2.setBackgroundColor(BaseColor.LIGHT_GRAY);
            table.addCell(cell2);

            PdfPCell cell4 = new PdfPCell(new Paragraph("Date: " + formatedDateTime));
            cell4.setColspan(10);
            cell4.setPaddingBottom(10);
            cell4.setPaddingTop(10);
            cell4.setBorder(2);
            cell4.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cell4.setBorderColorRight(BaseColor.WHITE);
            table.addCell(cell4);

            table.addCell("Transaction No");
            table.addCell("Billed By");
            table.addCell("Product Name");
            table.addCell("Qty");
            table.addCell("Amount (TK)");

            String PrevTransNo = " ";
            String query = "select trans_no, product_name, qty, amount, trans_by from trans_details where date like '%"
                    + month + "%'";
            try {
                pst = con.prepareStatement(query);
                rs = pst.executeQuery();
                while (rs.next()) {
                    String NewTransNo = rs.getString("trans_no");
                    String product_name = rs.getString("product_name");
                    String qty = rs.getString("qty");
                    String amount = rs.getString("amount");
                    String trans_by = rs.getString("trans_by");

                    total = total + Float.parseFloat(amount);

                    if (PrevTransNo.equals(NewTransNo))
                        table.addCell(" ");
                    else {
                        table.addCell(NewTransNo);
                        String query1 = "select adjustment, total_paid from paid_amount where trans_no = '"
                                + NewTransNo + "'";
                        pst1 = con1.prepareStatement(query1);
                        rs1 = pst1.executeQuery();
                        String adjustment = rs1.getString("adjustment");
                        String total_paid = rs1.getString("total_paid");

                        totalAdjustment = totalAdjustment + Float.parseFloat(adjustment);
                        totalTotalPaid = totalTotalPaid + Float.parseFloat(total_paid);

                        rs1.close();
                        pst1.close();
                    }

                    PrevTransNo = rs.getString("trans_no");
                    table.addCell(trans_by);
                    table.addCell(product_name);
                    table.addCell(qty);

                    PdfPCell cellAmount = new PdfPCell(new Paragraph(amount));
                    cellAmount.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cellAmount);

                }
                rs.close();
                pst.close();

            } catch (SQLException ex) {
                Logger.getLogger(ServiceEnd.class.getName()).log(Level.SEVERE, null, ex);
            }

            PdfPCell cellb = new PdfPCell(new Paragraph(" "));
            cellb.setColspan(10);
            cellb.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(cellb);

            //
            PdfPCell celltxtTotal = new PdfPCell(new Paragraph("Total"));
            celltxtTotal.setColspan(3);
            celltxtTotal.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtTotal);

            PdfPCell celltxtTotal1 = new PdfPCell(new Paragraph("" + total));
            celltxtTotal1.setColspan(2);
            celltxtTotal1.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtTotal1);

            PdfPCell celltxtAdjust = new PdfPCell(new Paragraph("Adjustment"));
            celltxtAdjust.setColspan(3);
            celltxtAdjust.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtAdjust);

            PdfPCell celltxtAdjust1 = new PdfPCell(new Paragraph("" + totalAdjustment));
            celltxtAdjust1.setColspan(2);
            celltxtAdjust1.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtAdjust1);

            PdfPCell celltxtTotalPaid = new PdfPCell(new Paragraph("Total Paid"));
            celltxtTotalPaid.setColspan(3);
            celltxtTotalPaid.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtTotalPaid);

            PdfPCell celltxtTotalPaid1 = new PdfPCell(new Paragraph("" + totalTotalPaid));
            celltxtTotalPaid1.setColspan(2);
            celltxtTotalPaid1.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtTotalPaid1);

            doc.add(table);

            doc.close();

            //JOptionPane.showMessageDialog(null, "Report Created!");
            // open PDF file
            File file = new File("Reports\\Monthly" + time + " " + formatedDate + ".pdf");
            if (file.toString().endsWith(".pdf"))
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file);
            else {
                Desktop desktop = Desktop.getDesktop();
                desktop.open(file);
            }

        } catch (DocumentException ex) {
            Logger.getLogger(ServiceEnd.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ServiceEnd.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ServiceEnd.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jMenuItem5ActionPerformed

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

        // TODO add your handling code here:
        totalAdjustment = 0;
        totalTotalPaid = 0;
        total = 0;

        Document doc = new Document();
        try {
            long time = new Date().getTime();
            PdfWriter.getInstance(doc,
                    new FileOutputStream("Reports\\Yearly" + time + " " + formatedDate + ".pdf"));
            doc.open();

            PdfPTable table = new PdfPTable(5);
            PdfPCell cell1 = new PdfPCell(new Paragraph("Shop Management System \n\n",
                    FontFactory.getFont(FontFactory.TIMES_BOLD, 20, Font.BOLD, BaseColor.WHITE)));
            cell1.setColspan(10);
            cell1.setPadding(10);
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell1.setBackgroundColor(BaseColor.BLACK);
            table.addCell(cell1);

            PdfPCell cell21 = new PdfPCell(new Paragraph("\n\n"));
            cell21.setColspan(10);
            cell21.setBorder(2);
            cell21.setBorderColorLeft(BaseColor.WHITE);
            table.addCell(cell21);

            PdfPCell cell2 = new PdfPCell(new Paragraph("Yearly Report\n"));
            cell2.setColspan(10);
            cell2.setPadding(10);
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell2.setBackgroundColor(BaseColor.LIGHT_GRAY);
            table.addCell(cell2);

            PdfPCell cell4 = new PdfPCell(new Paragraph("Date: " + formatedDateTime));
            cell4.setColspan(10);
            cell4.setPaddingBottom(10);
            cell4.setPaddingTop(10);
            cell4.setBorder(2);
            cell4.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cell4.setBorderColorRight(BaseColor.WHITE);
            table.addCell(cell4);

            table.addCell("Transaction No");
            table.addCell("Billed By");
            table.addCell("Product Name");
            table.addCell("Qty");
            table.addCell("Amount (TK)");

            String PrevTransNo = " ";
            String query = "select trans_no, product_name, qty, amount, trans_by from trans_details where date like '%"
                    + year + "%'";
            try {
                pst = con.prepareStatement(query);
                rs = pst.executeQuery();
                while (rs.next()) {
                    String NewTransNo = rs.getString("trans_no");
                    String product_name = rs.getString("product_name");
                    String qty = rs.getString("qty");
                    String amount = rs.getString("amount");
                    String trans_by = rs.getString("trans_by");

                    total = total + Float.parseFloat(amount);

                    if (PrevTransNo.equals(NewTransNo))
                        table.addCell(" ");
                    else {
                        table.addCell(NewTransNo);
                        String query1 = "select adjustment, total_paid from paid_amount where trans_no = '"
                                + NewTransNo + "'";
                        pst1 = con1.prepareStatement(query1);
                        rs1 = pst1.executeQuery();
                        String adjustment = rs1.getString("adjustment");
                        String total_paid = rs1.getString("total_paid");

                        totalAdjustment = totalAdjustment + Float.parseFloat(adjustment);
                        totalTotalPaid = totalTotalPaid + Float.parseFloat(total_paid);

                        rs1.close();
                        pst1.close();
                    }

                    PrevTransNo = rs.getString("trans_no");
                    table.addCell(trans_by);
                    table.addCell(product_name);
                    table.addCell(qty);

                    PdfPCell cellAmount = new PdfPCell(new Paragraph(amount));
                    cellAmount.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(cellAmount);

                }
                rs.close();
                pst.close();

            } catch (SQLException ex) {
                Logger.getLogger(ServiceEnd.class.getName()).log(Level.SEVERE, null, ex);
            }

            PdfPCell cellb = new PdfPCell(new Paragraph(" "));
            cellb.setColspan(10);
            cellb.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(cellb);

            //
            PdfPCell celltxtTotal = new PdfPCell(new Paragraph("Total"));
            celltxtTotal.setColspan(3);
            celltxtTotal.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtTotal);

            PdfPCell celltxtTotal1 = new PdfPCell(new Paragraph("" + total));
            celltxtTotal1.setColspan(2);
            celltxtTotal1.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtTotal1);

            PdfPCell celltxtAdjust = new PdfPCell(new Paragraph("Adjustment"));
            celltxtAdjust.setColspan(3);
            celltxtAdjust.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtAdjust);

            PdfPCell celltxtAdjust1 = new PdfPCell(new Paragraph("" + totalAdjustment));
            celltxtAdjust1.setColspan(2);
            celltxtAdjust1.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtAdjust1);

            PdfPCell celltxtTotalPaid = new PdfPCell(new Paragraph("Total Paid"));
            celltxtTotalPaid.setColspan(3);
            celltxtTotalPaid.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtTotalPaid);

            PdfPCell celltxtTotalPaid1 = new PdfPCell(new Paragraph("" + totalTotalPaid));
            celltxtTotalPaid1.setColspan(2);
            celltxtTotalPaid1.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(celltxtTotalPaid1);

            doc.add(table);

            doc.close();

            //JOptionPane.showMessageDialog(null, "Report Created!");
            // open PDF file
            File file = new File("Reports\\Yearly" + time + " " + formatedDate + ".pdf");
            if (file.toString().endsWith(".pdf"))
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file);
            else {
                Desktop desktop = Desktop.getDesktop();
                desktop.open(file);
            }

        } catch (DocumentException ex) {
            Logger.getLogger(ServiceEnd.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ServiceEnd.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ServiceEnd.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
        dispose();
        new Login().setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        new CreateUser().setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
        AddNewProduct anp = new AddNewProduct();
        anp.setVisible(true);
        dispose();
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
        // TODO add your handling code here:
        new DeleteUser().setVisible(true);
    }//GEN-LAST:event_jMenuItem7ActionPerformed

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

    //setting table output
    public void productTable() {
        String query = "select id, product_name, buying_rate, selling_rate, qty from products_list";
        try {
            pst = con.prepareStatement(query);
            rs = pst.executeQuery();
            tblSearchProduct.setModel(DbUtils.resultSetToTableModel(rs));

            rs.close();
            pst.close();

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JPopupMenu jPopupMenu2;
    private javax.swing.JPopupMenu jPopupMenu3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblSearchProduct;
    private javax.swing.JButton txtAddNewProduct;
    private javax.swing.JTextField txtSearchProduct;
    // End of variables declaration//GEN-END:variables
}