protected static DefaultFontMapper getMapper() {
    if (mapper == null) {
      //      long t = System.currentTimeMillis();
      mapper = new DefaultFontMapper();

      if (PApplet.platform == PApplet.MACOSX) {
        try {
          String homeLibraryFonts = System.getProperty("user.home") + "/Library/Fonts";
          mapper.insertDirectory(homeLibraryFonts);
        } catch (Exception e) {
          // might be a security issue with getProperty() and user.home
          // if this sketch is running from the web
        }
        // add the system font paths
        mapper.insertDirectory("/System/Library/Fonts");
        mapper.insertDirectory("/Library/Fonts");

      } else if (PApplet.platform == PApplet.WINDOWS) {
        // how to get the windows fonts directory?
        // could be c:\winnt\fonts or c:\windows\fonts or not even c:
        // maybe do a Runtime.exec() on echo %WINDIR% ?
        // Runtime.exec solution might be a mess on systems where the
        // the backslash/colon characters not really used (i.e. JP)

        // find the windows fonts folder
        File roots[] = File.listRoots();
        for (int i = 0; i < roots.length; i++) {
          if (roots[i].toString().startsWith("A:")) {
            // Seems to be a problem with some machines that the A:
            // drive is returned as an actual root, even if not available.
            // This won't fix the issue if the same thing happens with
            // other removable drive devices, but should fix the
            // initial/problem as cited by the bug report:
            // http://dev.processing.org/bugs/show_bug.cgi?id=478
            // If not, will need to use the other fileExists() code below.
            continue;
          }

          File folder = new File(roots[i], "WINDOWS/Fonts");
          if (folder.exists()) {
            mapper.insertDirectory(folder.getAbsolutePath());
            break;
          }
          folder = new File(roots[i], "WINNT/Fonts");
          if (folder.exists()) {
            mapper.insertDirectory(folder.getAbsolutePath());
            break;
          }
        }
      }
      //      System.out.println("mapping " + (System.currentTimeMillis() - t));
    }
    return mapper;
  }
  public void generatePdf() {

    java.lang.Process wait_for_Pdf2Show;

    java.util.Calendar cal = java.util.Calendar.getInstance();

    java.util.Date dateStampPdf = cal.getTime();

    java.lang.String pdfDateStamp = dateStampPdf.toString();

    try {

      java.io.File tempFile =
          java.io.File.createTempFile("REP" + this.getDateLable() + "_", ".pdf");

      tempFile.deleteOnExit();

      java.lang.Runtime rt = java.lang.Runtime.getRuntime();

      java.lang.String debitTotal = null;

      java.lang.String creditTotal = null;

      com.lowagie.text.Document docPdf = new com.lowagie.text.Document();

      try {

        try {

          com.lowagie.text.pdf.PdfWriter.getInstance(
              docPdf, new java.io.FileOutputStream(tempFile));

          try {

            java.lang.Class.forName("org.postgresql.Driver");

          } catch (java.lang.ClassNotFoundException cnfExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), cnfExec.getMessage());
          }

          String compName = null;
          String date = null;
          try {

            //   java.sql.Connection conDb =
            // java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/sako","postgres","pilsiner");

            java.sql.Statement st3 = connectDB.createStatement();
            java.sql.Statement st4 = connectDB.createStatement();

            java.sql.ResultSet rset2 =
                st3.executeQuery("SELECT hospital_name from pb_hospitalprofile");
            java.sql.ResultSet rset4 = st4.executeQuery("SELECT date('now') as Date");
            while (rset2.next()) compName = rset2.getObject(1).toString();

            while (rset4.next()) date = rset4.getObject(1).toString();

            com.lowagie.text.HeaderFooter headerFoter =
                new com.lowagie.text.HeaderFooter(
                    new Phrase("" + compName + "", pFontHeader),
                    false); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
                            // Font.BOLDITALIC,java.awt.Color.blue)));
            headerFoter.setAlignment(com.lowagie.text.HeaderFooter.ALIGN_CENTER);
            headerFoter.setRight(5);
            docPdf.setHeader(headerFoter);

          } catch (java.sql.SQLException SqlExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), SqlExec.getMessage());
          }

          com.lowagie.text.HeaderFooter footer =
              new com.lowagie.text.HeaderFooter(
                  new Phrase("Journal List - Page: ", pFontHeader),
                  true); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 12,
                         // Font.BOLDITALIC,java.awt.Color.blue));

          docPdf.setFooter(footer);

          docPdf.open();

          try {

            com.lowagie.text.pdf.PdfPTable table = new com.lowagie.text.pdf.PdfPTable(6);

            int headerwidths[] = {10, 10, 8, 25, 15, 15};

            table.setWidths(headerwidths);

            table.setWidthPercentage((100));

            table.setHeaderRows(2);

            table.getDefaultCell().setBorder(Rectangle.BOTTOM);

            table.getDefaultCell().setColspan(6);

            Phrase phrase = new Phrase("", pFontHeader);
            double osBalance = 0;
            double osBalance1 = 0;
            //  try {
            //      java.text.DateFormat dateFormat =
            // java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM);//MEDIUM);
            //

            //                            java.util.Date endDate1 =
            // dateFormat.parse(endDate.toLocaleString());//dateInstance.toLocaleString());
            //                            java.util.Date endDate11 =
            // dateFormat.parse(beginDate.toLocaleString());//dateInstance.toLocaleString());

            //                            System.out.println(""+endDate1);
            //  phrase = new Phrase(bank +" Report: " +dateFormat.format(formattedDate),
            // pFontHeader);

            //  table.addCell(phrase);
            table.getDefaultCell().setColspan(4);

            phrase = new Phrase("Journal  No : " + Jno, pFontHeader);

            table.addCell(phrase);
            table.getDefaultCell().setColspan(2);
            table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

            phrase = new Phrase("Printed On  : " + date, pFontHeader);

            table.addCell(phrase);
            // } catch(java.text.ParseException psExec) {

            //     javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(),
            // psExec.getMessage());
            //
            //  }
            // Phrase phrase = new Phrase("Patients List As At:" +endDate, pFontHeader);

            // table.addCell(phrase);
            table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);

            table.getDefaultCell().setColspan(1);

            //    table.getDefaultCell().setBackgroundColor(java.awt.Color.LIGHT_GRAY);

            phrase = new Phrase("Date", pFontHeader);
            table.addCell(phrase);

            phrase = new Phrase("Activity Code", pFontHeader);
            table.addCell(phrase);
            phrase = new Phrase("journal No.", pFontHeader);
            table.addCell(phrase);
            phrase = new Phrase("Description", pFontHeader);
            table.addCell(phrase);

            table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

            phrase = new Phrase("Debit KShs", pFontHeader);
            table.addCell(phrase);

            phrase = new Phrase("Credit KShs", pFontHeader);
            table.addCell(phrase);

            table.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
            table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

            try {

              java.sql.Statement st = connectDB.createStatement();

              java.sql.Statement st2 = connectDB.createStatement();

              java.sql.ResultSet rset =
                  st.executeQuery(
                      "select input_date,activity_code ,journal_no,description,debit,credit from ac_journal WHERE journal_no BETWEEN '"
                          + Jno
                          + "' AND '"
                          + Jno1
                          + "' ORDER BY journal_no"); // tn,debit_note db WHERE tn.policy_no != ''
                                                      // and tn.policy_no = db.policy_no GROUP BY
                                                      // tn.policy_no,db.policy_class");

              while (rset.next()) {

                table.getDefaultCell().setColspan(1);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(rset.getObject(1).toString(), pFontHeader1);
                table.addCell(phrase);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(rset.getObject(2).toString(), pFontHeader1);

                table.addCell(phrase);
                phrase = new Phrase(rset.getObject(3).toString(), pFontHeader1);
                table.addCell(phrase);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(rset.getString(4).toString(), pFontHeader1);
                table.addCell(phrase);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset.getString(5)),
                        pFontHeader1);
                osBalance = osBalance + rset.getDouble(5);
                table.addCell(phrase);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset.getString(6)),
                        pFontHeader1);
                osBalance1 = osBalance1 + rset.getDouble(6);
                table.addCell(phrase);
              }
              table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

              table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);

              table.getDefaultCell().setColspan(4);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Total", pFontHeader);

              table.addCell(phrase);

              table.getDefaultCell().setColspan(1);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(osBalance)),
                      pFontHeader1);

              table.addCell(phrase);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(osBalance1)),
                      pFontHeader1);

              table.addCell(phrase);

              docPdf.add(table);

            } catch (java.sql.SQLException SqlExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }

          } catch (com.lowagie.text.BadElementException BadElExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

        } catch (java.io.FileNotFoundException fnfExec) {

          javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), fnfExec.getMessage());
        }
      } catch (com.lowagie.text.DocumentException lwDocexec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), lwDocexec.getMessage());
      }

      docPdf.close();

      try {

        if (System.getProperty("os.name").equalsIgnoreCase("Linux")) {

          System.out.println(tempFile);

          wait_for_Pdf2Show = rt.exec("kghostview " + tempFile + "");

          wait_for_Pdf2Show.waitFor();

        } else {

          wait_for_Pdf2Show =
              rt.exec("c:/Program Files/Adobe/Acrobat 5.0/Reader/AcroRd32.exe " + tempFile);

          wait_for_Pdf2Show.waitFor();
        }

      } catch (java.lang.InterruptedException intrExec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), intrExec.getMessage());
      }

    } catch (java.io.IOException IOexec) {

      javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), IOexec.getMessage());
    }
  }
  public void generatePdf(java.lang.String memNo) {

    java.lang.Process wait_for_Pdf2Show;

    java.util.Calendar cal = java.util.Calendar.getInstance();

    java.util.Date dateStampPdf = cal.getTime();

    java.lang.String pdfDateStamp = dateStampPdf.toString();

    try {

      java.io.File tempFile =
          java.io.File.createTempFile("REP" + this.getDateLable() + "_", ".pdf");

      tempFile.deleteOnExit();

      java.lang.Runtime rt = java.lang.Runtime.getRuntime();

      java.lang.String debitTotal = null;

      java.lang.String creditTotal = null;

      // com.lowagie.text.Document docPdf = new com.lowagie.text.Document();
      com.lowagie.text.Document docPdf = new com.lowagie.text.Document(PageSize.A4.rotate());
      double osBalance1 = 0.00;
      double osBalance = 0.00;
      double current = 0.00;
      double osBalancebf = 0.00;
      try {

        try {

          com.lowagie.text.pdf.PdfWriter.getInstance(
              docPdf, new java.io.FileOutputStream(tempFile));

          String compName = null;
          String date = null;
          String Messg = null;

          docPdf.open();

          try {

            java.util.Calendar calendar = java.util.Calendar.getInstance();

            long dateNow = calendar.getTimeInMillis();

            java.sql.Date datenowSql = new java.sql.Date(dateNow);

            System.out.println(datenowSql.toString());

            java.lang.Object listofStaffNos[] = this.getListofStaffNos();

            com.lowagie.text.pdf.PdfPTable table1 = new com.lowagie.text.pdf.PdfPTable(7);
            //  com.lowagie.text.Table table = new com.lowagie.text.Table(7);

            // table.endHeaders();

            int headerwidths[] = {15, 15, 30, 15, 15, 15, 15};

            table1.setWidths(headerwidths);
            //  if (docPdf.getPageNumber() > 1) {
            //  table1.setHeaderRows(4);
            //  }
            table1.setWidthPercentage((100));

            table1.getDefaultCell().setBorder(Rectangle.BOTTOM);

            table1.getDefaultCell().setColspan(7);

            Phrase phrase = new Phrase();

            //  table.addCell(phrase);

            table1.getDefaultCell().setColspan(1);
            table1.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
            table1.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

            try {
              java.sql.Statement st3 = connectDB.createStatement();
              java.sql.Statement st2x = connectDB.createStatement();

              java.sql.ResultSet rset2x =
                  st2x.executeQuery("SELECT rep_currency from pb_hospitalprofile");
              while (rset2x.next()) {
                ks = rset2x.getObject(1).toString();
              }
              java.sql.ResultSet rset3 = st3.executeQuery("select header_name from pb_header");
              while (rset3.next()) {
                table1.getDefaultCell().setColspan(7);

                table1.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
                phrase = new Phrase(rset3.getObject(1).toString(), pFontHeader11);
                table1.addCell(phrase);
              }

            } catch (java.sql.SQLException SqlExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }
            docPdf.add(table1);
          } catch (com.lowagie.text.BadElementException BadElExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

          //  } catch(java.io.FileNotFoundException fnfExec) {

          //  javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(),
          // fnfExec.getMessage());

          /// }
          try {

            java.util.Calendar calendar = java.util.Calendar.getInstance();

            long dateNow = calendar.getTimeInMillis();

            java.sql.Date datenowSql = new java.sql.Date(dateNow);

            System.out.println(datenowSql.toString());

            java.lang.Object listofStaffNos[] = this.getListofStaffNos();

            com.lowagie.text.pdf.PdfPTable table = new com.lowagie.text.pdf.PdfPTable(9);
            //  com.lowagie.text.Table table = new com.lowagie.text.Table(7);

            // table.endHeaders();

            int headerwidths[] = {12, 10, 28, 11, 14, 12, 14, 14, 15};

            table.setWidths(headerwidths);
            //  if (docPdf.getPageNumber() > 1) {
            table.setHeaderRows(8);
            //  }
            table.setWidthPercentage((100));

            table.getDefaultCell().setBorder(Rectangle.BOTTOM);

            table.getDefaultCell().setColspan(9);

            Phrase phrase = new Phrase();

            //  table.addCell(phrase);

            table.getDefaultCell().setColspan(1);
            table.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
            table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

            try {
              double Debit = 0.00;
              double Credit = 0.00;
              java.sql.Statement st22 = connectDB.createStatement();
              java.sql.Statement st11 = connectDB.createStatement();
              java.sql.Statement st = connectDB.createStatement();
              java.sql.Statement st1 = connectDB.createStatement();
              java.sql.Statement st2 = connectDB.createStatement();
              java.sql.Statement st3 = connectDB.createStatement();
              java.sql.Statement st4 = connectDB.createStatement();
              java.sql.Statement st41 = connectDB.createStatement();
              java.sql.Statement st5 = connectDB.createStatement();
              java.sql.Statement st22D = connectDB.createStatement();
              java.sql.ResultSet rset3 =
                  st3.executeQuery(
                      "select hospital_name,postal_code||' '||box_no||' '||town,main_telno||' '||other_telno,initcap(street),main_faxno,email,website,room_no from pb_hospitalprofile");
              java.sql.ResultSet rset4 =
                  st4.executeQuery(
                      "select scheme_name,account_no,tel_main,address from ac_schemes where account_no = '"
                          + memNo
                          + "'");
              java.sql.ResultSet rset41 =
                  st41.executeQuery(
                      "select payer_name from ac_schemes where account_no = '" + memNo + "'");
              //      java.sql.ResultSet rset1 = st1.executeQuery("select date,admission_no||'
              // '||cheque_no, upper(item),invoice_no ||'
              // '||receipt_no,debit,credit,credit_bal,balance from ac_debtors where account_no = '"
              // + memNo + "' AND date::date BETWEEN '" + beginDate + "' AND '" + endDate + "' group
              // by 1, 2, 3order by date,invoice_no");// UNION select
              // pd.date::date,initcap(pd.scheme_staff_no), (sh.first_name||' '||sh.second_name||'
              // '||sh.last_name) as name,pd.reference,sum(pd.credit),pd.patient_no from
              // hp_patient_card pd,hp_inpatient_register sh where pd.patient_no = sh.patient_no and
              // pd.isurer = '"+memNo+"' AND pd.date::date BETWEEN '"+beginDate+"' AND '"+endDate+"'
              // and pd.credit > 0 group by pd.date::date,pd.scheme_staff_no,
              // name,pd.reference,pd.invoice_no,pd.patient_no order by pd.invoice_no");
              java.sql.ResultSet rsetTotals2 =
                  st22.executeQuery(
                      "select sum(debit - credit) from ac_debtors where account_no = '"
                          + memNo
                          + "' AND date::date < '"
                          + beginDate
                          + "'");
              java.sql.ResultSet rsetTotals =
                  st2.executeQuery(
                      "select sum(debit),sum(credit) from ac_debtors where account_no = '"
                          + memNo
                          + "' AND date::date BETWEEN '"
                          + beginDate
                          + "' AND '"
                          + endDate
                          + "'");

              table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);
              table.getDefaultCell().setBorderWidth(Rectangle.TOP | Rectangle.BOTTOM);

              table.getDefaultCell().setColspan(9);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
              phrase = new Phrase("Statement of Account", pFontHeader11);
              table.addCell(phrase);

              while (rset4.next()) {
                table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);
                table.getDefaultCell().setColspan(9);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(rset4.getObject(1).toString(), pFontHeader1);
                table.addCell(phrase);

                while (rset41.next()) {
                  table.getDefaultCell().setColspan(9);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase(rset41.getString(1), pFontHeader1);
                  table.addCell(phrase);
                }
                table.getDefaultCell().setColspan(9);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase =
                    new Phrase(
                        "Account No : " + rset4.getObject(2).toString().toUpperCase(),
                        pFontHeader1);
                table.addCell(phrase);

                phrase =
                    new Phrase(
                        dbObject.getDBObject("Tel : " + rset4.getObject(3), "-"), pFontHeader1);
                table.addCell(phrase);
                phrase = new Phrase(dbObject.getDBObject(rset4.getObject(4), "-"), pFontHeader1);
                table.addCell(phrase);
                table.getDefaultCell().setColspan(6);
              }
              try {
                java.text.DateFormat dateFormat =
                    java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM); // MEDIUM);

                java.util.Date endDate1 =
                    dateFormat.parse(endDate.toLocaleString()); // dateInstance.toLocaleString());
                java.util.Date endDate11 =
                    dateFormat.parse(beginDate.toLocaleString()); // dateInstance.toLocaleString());

                System.out.println("" + endDate1);
                //  phrase = new Phrase(bank +" Report: " +dateFormat.format(formattedDate),
                // pFontHeader);

                //  table.addCell(phrase);

                phrase =
                    new Phrase(
                        "Period : "
                            + dateFormat.format(endDate11)
                            + " - "
                            + dateFormat.format(endDate1),
                        pFontHeader1);

                table.addCell(phrase);
              } catch (java.text.ParseException psExec) {

                javax.swing.JOptionPane.showMessageDialog(
                    new javax.swing.JFrame(), psExec.getMessage());
              }
              // table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              // phrase = new Phrase("Period : '"+beginDate+"' - '"+endDate+"'", pFontHeader1);
              // table.addCell(phrase);

              table.getDefaultCell().setColspan(3);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Date " + datenowSql, pFontHeader1);
              table.addCell(phrase);

              table.getDefaultCell().setColspan(3);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);
              table.getDefaultCell().setBorderWidth(Rectangle.TOP);
              table.getDefaultCell().setColspan(1);
              // phrase = new Phrase("Date", pFontHeader1);
              // table.addCell(phrase);
              table.getDefaultCell().setColspan(1);
              phrase = new Phrase("Staff No.", pFontHeader1);
              table.addCell(phrase);
              table.getDefaultCell().setColspan(2);
              phrase = new Phrase(" Staff Name", pFontHeader1);
              table.addCell(phrase);

              table.getDefaultCell().setColspan(1);
              phrase = new Phrase("", pFontHeader1);
              table.addCell(phrase);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              table.getDefaultCell().setColspan(1);
              phrase = new Phrase("", pFontHeader1);
              table.addCell(phrase);
              table.getDefaultCell().setColspan(1);
              phrase = new Phrase("", pFontHeader1);
              table.addCell(phrase);
              phrase = new Phrase("", pFontHeader1);
              table.addCell(phrase);
              phrase = new Phrase("Debit Bal. ", pFontHeader1);
              table.addCell(phrase);

              table.getDefaultCell().setColspan(1);
              phrase = new Phrase("Running Amt. " + ks, pFontHeader1);
              table.addCell(phrase);

              while (rsetTotals2.next()) {
                table.getDefaultCell().setColspan(5);

                table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase("BAL/BF", pFontHeader1);
                table.addCell(phrase);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
                table.getDefaultCell().setColspan(4);
                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rsetTotals2.getString(1)),
                        pFontHeader1);
                osBalancebf = osBalancebf + rsetTotals2.getDouble(1);
                table.addCell(phrase);
              }
              for (int i = 0; i < listofStaffNos.length; i++) {
                java.sql.ResultSet rset1 =
                    st1.executeQuery(
                        "select admission_no, upper(item), sum(debit - credit) from ac_debtors where account_no = '"
                            + memNo
                            + "' and admission_no = '"
                            + listofStaffNos[i]
                            + "' AND date::date BETWEEN '"
                            + beginDate
                            + "' AND '"
                            + endDate
                            + "' GROUP BY 1, 2");
                while (rset1.next()) {
                  table.getDefaultCell().setColspan(1);
                  table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase(dbObject.getDBObject(rset1.getObject(1), "-"), pFontHeader);

                  table.addCell(phrase);
                  table.getDefaultCell().setColspan(6);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase(dbObject.getDBObject(rset1.getObject(2), "-"), pFontHeader);

                  table.addCell(phrase);
                  table.getDefaultCell().setColspan(1);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  // phrase = new Phrase(dbObject.getDBObject(rset1.getObject(3), "-"),
                  // pFontHeader);

                  // table.addCell(phrase);

                  table.getDefaultCell().setColspan(1);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  // phrase = new Phrase(dbObject.getDBObject(rset1.getObject(4), "-"),
                  // pFontHeader);

                  // table.addCell(phrase);

                  table.getDefaultCell().setColspan(1);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
                  // phrase = new Phrase(new
                  // com.afrisoftech.sys.Format2Currency().Format2Currency(rset1.getString(5)),
                  // pFontHeader);
                  // Debit = Debit + rset1.getDouble(5);
                  // table.addCell(phrase);
                  // phrase = new Phrase(new
                  // com.afrisoftech.sys.Format2Currency().Format2Currency(rset1.getString(6)),
                  // pFontHeader);
                  // Credit = Credit + rset1.getDouble(6);
                  // table.addCell(phrase);

                  // phrase = new Phrase(new
                  // com.afrisoftech.sys.Format2Currency().Format2Currency(rset1.getString(7)),
                  // pFontHeader);
                  // osBalance1 = osBalance1 + rset1.getDouble(6);
                  // table.addCell(phrase);
                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(rset1.getString(3)),
                          pFontHeader);
                  osBalancebf = osBalancebf + rset1.getDouble(3);
                  // osBalancebf = osBalance1;
                  table.addCell(phrase);
                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(java.lang.String.valueOf(osBalancebf - osBalance1)),
                          pFontHeader);

                  table.addCell(phrase);
                }
              }
              table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

              table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);

              //  while (rsetTotals.next()) {

              table.getDefaultCell().setColspan(4);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Total", pFontHeader);

              table.addCell(phrase);

              table.getDefaultCell().setColspan(1);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
              phrase = new Phrase("", pFontHeader);

              table.addCell(phrase);
              phrase = new Phrase("", pFontHeader);

              table.addCell(phrase);
              phrase = new Phrase("", pFontHeader);

              table.addCell(phrase);
              table.getDefaultCell().setColspan(2);
              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(osBalancebf - osBalance1)),
                      pFontHeader);

              table.addCell(phrase);

              // phrase = new Phrase(" ");

              //     }
              try {

                java.sql.Statement st31 = connectDB.createStatement();
                // java.sql.Statement st4 = connectDB.createStatement();
                java.sql.ResultSet rset2 = st31.executeQuery("select name from pb_notice");

                // java.sql.ResultSet rset2 = st3.executeQuery("SELECT hospital_name from
                // pb_hospitalprofile");
                //   java.sql.ResultSet rset4 = st4.executeQuery("SELECT date('now') as Date");
                while (rset2.next()) {
                  Messg = rset2.getString(1);
                }
                com.lowagie.text.HeaderFooter footer =
                    new com.lowagie.text.HeaderFooter(
                        new Phrase("" + Messg + ""),
                        false); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
                // Font.BOLDITALIC,java.awt.Color.blue)));

                //  com.lowagie.text.HeaderFooter headerFoter = new
                // com.lowagie.text.HeaderFooter(new Phrase(""+compName+""),false);//
                // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
                // Font.BOLDITALIC,java.awt.Color.blue)));
                //  headerFoter.ALIGN_CENTER;
                //  headerFoter.setRight(5);
                docPdf.setFooter(footer);

              } catch (java.sql.SQLException SqlExec) {

                SqlExec.printStackTrace();
                javax.swing.JOptionPane.showMessageDialog(
                    new javax.swing.JFrame(), SqlExec.getMessage());
              }

              docPdf.add(table);

            } catch (java.sql.SQLException SqlExec) {

              SqlExec.printStackTrace();
              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }

            // }  // }

          } catch (com.lowagie.text.BadElementException BadElExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

        } catch (java.io.FileNotFoundException fnfExec) {

          javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), fnfExec.getMessage());
        }
      } catch (com.lowagie.text.DocumentException lwDocexec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), lwDocexec.getMessage());
      }

      try {

        if (System.getProperty("os.name").equalsIgnoreCase("Linux")) {

          System.out.println(tempFile);

          wait_for_Pdf2Show = rt.exec("xpdf " + tempFile + "");

          wait_for_Pdf2Show.waitFor();

        } else {

          wait_for_Pdf2Show =
              rt.exec("c:/Program Files/Adobe/Acrobat 5.0/Reader/AcroRd32.exe " + tempFile);

          wait_for_Pdf2Show.waitFor();
        }

      } catch (java.lang.InterruptedException intrExec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), intrExec.getMessage());
      }

    } catch (java.io.IOException IOexec) {

      javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), IOexec.getMessage());
    }
  }
  public void generatePdf() {
    java.sql.ResultSet rsetTotals1 = null;

    java.lang.Process wait_for_Pdf2Show;

    java.util.Calendar cal = java.util.Calendar.getInstance();

    java.util.Date dateStampPdf = cal.getTime();

    java.lang.String pdfDateStamp = dateStampPdf.toString();

    try {

      java.io.File tempFile =
          java.io.File.createTempFile("REP" + this.getDateLable() + "_", ".pdf");

      tempFile.deleteOnExit();

      java.lang.Runtime rt = java.lang.Runtime.getRuntime();

      java.lang.String debitTotal = null;

      java.lang.String creditTotal = null;

      com.lowagie.text.Document docPdf = new com.lowagie.text.Document();

      try {

        try {

          com.lowagie.text.pdf.PdfWriter.getInstance(
              docPdf, new java.io.FileOutputStream(tempFile));

          String compName = null;
          String date = null;
          try {

            java.sql.Statement st3 = connectDB.createStatement();
            java.sql.Statement st4 = connectDB.createStatement();

            java.sql.ResultSet rset2 =
                st3.executeQuery("SELECT hospital_name from pb_hospitalprofile");
            java.sql.ResultSet rset4 = st4.executeQuery("SELECT date('now') as Date");
            while (rset2.next()) compName = rset2.getObject(1).toString();

            while (rset4.next()) date = rset4.getObject(1).toString();

            com.lowagie.text.HeaderFooter headerFoter =
                new com.lowagie.text.HeaderFooter(
                    new Phrase("" + compName, pFontHeader),
                    false); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
                            // Font.BOLDITALIC,java.awt.Color.blue)));
            headerFoter.setAlignment(com.lowagie.text.HeaderFooter.ALIGN_CENTER);
            headerFoter.setRight(5);
            docPdf.setHeader(headerFoter);

          } catch (java.sql.SQLException SqlExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), SqlExec.getMessage());
          }

          com.lowagie.text.HeaderFooter footer =
              new com.lowagie.text.HeaderFooter(
                  new Phrase("Un Finalised Invoices List - Page: ", pFontHeader1),
                  true); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 12,
                         // Font.BOLDITALIC,java.awt.Color.blue));

          docPdf.setFooter(footer);

          docPdf.open();

          double balance = 0.00;

          try {

            com.lowagie.text.pdf.PdfPTable table = new com.lowagie.text.pdf.PdfPTable(4);

            int headerwidths[] = {15, 30, 25, 15};

            table.setWidths(headerwidths);

            table.setWidthPercentage((100));

            table.setHeaderRows(2);

            table.getDefaultCell().setBorder(Rectangle.BOTTOM);
            table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);

            table.getDefaultCell().setColspan(2);

            Phrase phrase =
                new Phrase(
                    "In Patient Unfinalised Inv : ",
                    com.lowagie.text.FontFactory.getFont(FontFactory.HELVETICA_BOLD, 10));

            table.addCell(phrase);

            table.getDefaultCell().setColspan(2);

            phrase =
                new Phrase(
                    "Printed On : " + date,
                    com.lowagie.text.FontFactory.getFont(FontFactory.HELVETICA_BOLD, 10));

            table.addCell(phrase);
            table.getDefaultCell().setColspan(1);

            // phrase = new Phrase("Name", pFontHeader);

            // table.addCell(phrase);

            phrase = new Phrase("Date", pFontHeader);

            table.addCell(phrase);

            phrase = new Phrase("No.", pFontHeader);

            table.addCell(phrase);

            phrase = new Phrase("Name", pFontHeader);

            table.addCell(phrase);

            phrase = new Phrase("Amount", pFontHeader);

            table.addCell(phrase);

            table.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
            table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

            try {

              // java.lang.Object[] listofAct = this.getListofActivities();
              java.lang.Object[] listofAct1 = this.getListofActivities1();

              java.sql.Statement st21 = connectDB.createStatement();

              //  java.sql.ResultSet rsetTotals = st21.executeQuery("SELECT sum(amount) from
              // op_unfinalised_inv  where trans_date BETWEEN '"+beginDate+"' AND '"+endDate+"'");

              //    java.sql.Connection conDb1 =
              // java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/sako","postgres","pilsiner");

              //  System.out.println(listofAct.length);
              java.sql.Statement st2 = connectDB.createStatement();
              java.sql.Statement st22 = connectDB.createStatement();

              for (int k = 0; k < listofAct1.length; k++) {
                java.sql.ResultSet rset1 =
                    st2.executeQuery(
                        "SELECT patient_no,sum(debit-credit) from hp_patient_card WHERE patient_no ILIKE '"
                            + listofAct1[k].toString()
                            + "%' and invoice_no NOT iLike 'O%' OR invoice_no NOT iLike 'I%' AND date BETWEEN '"
                            + beginDate
                            + "' AND '"
                            + endDate
                            + "' group by patient_no");
                java.sql.ResultSet rset2 =
                    st22.executeQuery(
                        "SELECT patient_name from hp_admission WHERE patient_no ILIKE '"
                            + listofAct1[k].toString()
                            + "%'");

                /*    table.getDefaultCell().setColspan(1);
                   table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                   phrase = new Phrase(" ",pFontHeader);
                   table.addCell(phrase);
                   table.getDefaultCell().setColspan(3);
                   table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  // phrase = new Phrase(listofAct1[k].toString(),pFontHeader);
                 //  table.addCell(phrase);

                   phrase = new Phrase("",pFontHeader);
                   table.addCell(phrase);
                */
                while (rset1.next()) {
                  while (rset2.next()) {
                    table.getDefaultCell().setColspan(1);

                    table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                    phrase = new Phrase(" ", pFontNum);
                    table.addCell(phrase);

                    table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                    phrase = new Phrase(rset1.getObject(1).toString(), pFontNum);
                    table.addCell(phrase);

                    table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                    phrase = new Phrase(rset2.getString(1), pFontNum);
                    table.addCell(phrase);

                    table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
                    phrase =
                        new Phrase(
                            new com.afrisoftech.sys.Format2Currency()
                                .Format2Currency(rset1.getString(2)),
                            pFontHeader1);
                    table.addCell(phrase);
                    balance = balance + rset1.getDouble(2);
                  }
                }
              }
              // while (rsetTotals.next()) {

              table.getDefaultCell().setColspan(2);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Total", pFontHeader);

              table.addCell(phrase);

              table.getDefaultCell().setColspan(2);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              //  phrase = new Phrase(new
              // com.afrisoftech.sys.Format2Currency().Format2Currency(rsetTotals.getString(1)),
              // pFontHeader);
              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(balance)),
                      pFontHeader);

              table.addCell(phrase);
              //  }

              // }

              docPdf.add(table);

            } catch (java.sql.SQLException SqlExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }

          } catch (com.lowagie.text.BadElementException BadElExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

        } catch (java.io.FileNotFoundException fnfExec) {

          javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), fnfExec.getMessage());
        }
      } catch (com.lowagie.text.DocumentException lwDocexec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), lwDocexec.getMessage());
      }

      docPdf.close();

      try {

        if (System.getProperty("os.name").equalsIgnoreCase("Linux")) {

          System.out.println(tempFile);

          wait_for_Pdf2Show = rt.exec("kghostview " + tempFile + "");

          wait_for_Pdf2Show.waitFor();

        } else {

          wait_for_Pdf2Show =
              rt.exec("c:/Program Files/Adobe/Acrobat 5.0/Reader/AcroRd32.exe " + tempFile);

          wait_for_Pdf2Show.waitFor();
        }

      } catch (java.lang.InterruptedException intrExec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), intrExec.getMessage());
      }

    } catch (java.io.IOException IOexec) {

      javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), IOexec.getMessage());
    }
  }
  public void generatePdf(java.lang.String memNo) {

    java.lang.Process wait_for_Pdf2Show;

    java.util.Calendar cal = java.util.Calendar.getInstance();

    java.util.Date dateStampPdf = cal.getTime();

    java.lang.String pdfDateStamp = dateStampPdf.toString();

    try {

      java.io.File tempFile =
          java.io.File.createTempFile("REP" + this.getDateLable() + "_", ".pdf");

      tempFile.deleteOnExit();

      java.lang.Runtime rt = java.lang.Runtime.getRuntime();

      java.lang.String debitTotal = null;

      java.lang.String creditTotal = null;

      com.lowagie.text.Document docPdf = new com.lowagie.text.Document();

      try {

        try {

          com.lowagie.text.pdf.PdfWriter.getInstance(
              docPdf, new java.io.FileOutputStream(tempFile));

          String compName = null;
          String date = null;
          /*   try {


                 java.sql.Statement st3 = connectDB.createStatement();
                 java.sql.Statement st4 = connectDB.createStatement();

                 java.sql.ResultSet rset2 = st3.executeQuery("SELECT hospital_name from pb_hospitalprofile");
                 java.sql.ResultSet rset4 = st4.executeQuery("SELECT date('now') as Date");
                 while(rset2.next())
                     compName = rset2.getObject(1).toString();

                 while(rset4.next())
                     date = rset4.getObject(1).toString();
          */
          com.lowagie.text.HeaderFooter headerFoter =
              new com.lowagie.text.HeaderFooter(
                  new Phrase("FINAL VOUCHER"),
                  false); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
          // Font.BOLDITALIC,java.awt.Color.blue)));

          //  com.lowagie.text.HeaderFooter headerFoter = new com.lowagie.text.HeaderFooter(new
          // Phrase(""+compName+""),false);//
          // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
          // Font.BOLDITALIC,java.awt.Color.blue)));
          headerFoter.setRight(5);
          docPdf.setHeader(headerFoter);

          docPdf.open();

          try {

            java.lang.Object listofStaffNos[] = this.getListofStaffNos();

            for (int j = 0; j < listofStaffNos.length; j++) {
              com.lowagie.text.pdf.PdfPTable table = new com.lowagie.text.pdf.PdfPTable(6);

              int headerwidths[] = {25, 25, 20, 15, 15, 15};

              table.setWidths(headerwidths);

              table.setWidthPercentage((100));

              table.getDefaultCell().setBorder(Rectangle.BOTTOM);

              table.getDefaultCell().setColspan(6);

              Phrase phrase = new Phrase();

              //  table.addCell(phrase);

              table.getDefaultCell().setColspan(1);
              table.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
              table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

              try {

                java.sql.Statement st = connectDB.createStatement();
                java.sql.Statement st1 = connectDB.createStatement();
                java.sql.Statement st2 = connectDB.createStatement();
                java.sql.Statement st3 = connectDB.createStatement();
                java.sql.Statement st4 = connectDB.createStatement();
                java.sql.Statement st5 = connectDB.createStatement();
                java.sql.ResultSet rset3 =
                    st3.executeQuery(
                        "select hospital_name,postal_code||' '||box_no||' '||town,main_telno||' '||other_telno,initcap(street),main_faxno,email,website,room_no from pb_hospitalprofile");
                java.sql.ResultSet rset4 =
                    st4.executeQuery("select dealer,description from ac.cash_book");
                // java.sql.ResultSet rset = st.executeQuery("select DISTINCT member_code,
                // member_name,date from shares_transactions order by member_code");
                java.sql.ResultSet rset =
                    st.executeQuery(
                        "select patient_no,initcap(first_name||' '||second_name||' '||last_name),address,residence,tel_no from hp_patient_register where patient_no = '"
                            + listofStaffNos[j]
                            + "'");
                java.sql.ResultSet rset5 =
                    st5.executeQuery(
                        "select staff_no,staff_name from hp_schemestaff sh, hp_patient_card pc where pc.patient_no = '"
                            + listofStaffNos[j]
                            + "' AND pc.scheme_staff_no = sh.staff_no");
                java.sql.ResultSet rset1 =
                    st1.executeQuery(
                        "select date::date,initcap(service) as service,dosage,reference,debit from hp_patient_card where patient_no = '"
                            + listofStaffNos[j]
                            + "' AND paid = true order by date");
                java.sql.ResultSet rsetTotals =
                    st2.executeQuery(
                        "select sum(debit) from hp_patient_card where patient_no = '"
                            + listofStaffNos[j]
                            + "' AND paid = true");

                while (rset3.next()) {
                  table.getDefaultCell().setColspan(6);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
                  phrase = new Phrase(rset3.getObject(1).toString(), pFontHeader1);
                  table.addCell(phrase);

                  table.getDefaultCell().setColspan(6);
                  table.getDefaultCell().setBorderColor(java.awt.Color.white);

                  table.getDefaultCell().setColspan(2);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase =
                      new Phrase("Address:" + "\t" + rset3.getObject(2).toString(), pFontHeader);
                  table.addCell(phrase);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase("Tel:" + "\t" + rset3.getObject(3).toString(), pFontHeader);

                  table.addCell(phrase);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase("Fax:" + "\t" + rset3.getObject(5).toString(), pFontHeader);

                  table.addCell(phrase);
                  table.getDefaultCell().setBorderColor(java.awt.Color.white);
                  table.getDefaultCell().setColspan(3);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase("Email:" + "\t" + rset3.getObject(6).toString(), pFontHeader);
                  table.addCell(phrase);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase =
                      new Phrase("Website:" + "\t" + rset3.getObject(7).toString(), pFontHeader);

                  table.addCell(phrase);
                  /// table.addCell("  ");

                }
                table.getDefaultCell().setColspan(6);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
                phrase = new Phrase("Voucher", pFontHeader1);
                table.addCell(phrase);

                while (rset4.next()) table.getDefaultCell().setColspan(6);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(rset4.getObject(1).toString(), pFontHeader1);
                table.addCell(phrase);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase("Address:  " + rset4.getObject(2).toString(), pFontHeader);
                table.addCell(phrase);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase("Tel:       " + rset4.getObject(3).toString(), pFontHeader);

                table.addCell(phrase);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase("Fax:       " + rset4.getObject(4).toString(), pFontHeader);

                table.addCell(phrase);
                /*    table.getDefaultCell().setBorderColor(java.awt.Color.white);
                  table.getDefaultCell().setColspan(3);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase("Email:"+"\t"+rset3.getObject(6).toString(), pFontHeader);
                  table.addCell(phrase);


                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase("Website:"+"\t" +rset3.getObject(7).toString(), pFontHeader);

                  table.addCell(phrase);
                  /// table.addCell("  ");
                */

                while (rset5.next()) {

                  table.getDefaultCell().setColspan(6);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase("Member No.  " + rset5.getObject(1).toString(), pFontHeader);
                  table.addCell(phrase);

                  table.getDefaultCell().setColspan(6);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase("Member Name  " + rset5.getObject(2).toString(), pFontHeader);
                  table.addCell(phrase);
                }
                while (rset.next()) {

                  table.getDefaultCell().setColspan(6);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase("Patient No.  " + rset.getObject(1).toString(), pFontHeader);
                  table.addCell(phrase);

                  table.getDefaultCell().setColspan(6);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase("Patient Name  " + rset.getObject(2).toString(), pFontHeader);
                  table.addCell(phrase);

                  /*
                  table.getDefaultCell().setColspan(2);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase("Address  "+rset.getObject(3).toString(), pFontHeader);
                  table.addCell(phrase);

                  table.getDefaultCell().setColspan(2);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase("Town  "+rset.getObject(4).toString(), pFontHeader);
                  table.addCell(phrase);
                  table.getDefaultCell().setColspan(2);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase("TEl No. "+rset.getObject(5).toString(), pFontHeader);
                  table.addCell(phrase);
                  */

                }
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);
                table.getDefaultCell().setBorderWidth(Rectangle.TOP);
                table.getDefaultCell().setColspan(1);
                phrase = new Phrase("Date", pFontHeader1);
                table.addCell(phrase);
                table.getDefaultCell().setColspan(2);
                phrase = new Phrase("Description", pFontHeader1);
                table.addCell(phrase);
                table.getDefaultCell().setColspan(1);
                phrase = new Phrase("Qty", pFontHeader1);
                table.addCell(phrase);

                table.getDefaultCell().setColspan(1);
                phrase = new Phrase("Ref", pFontHeader1);
                table.addCell(phrase);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                table.getDefaultCell().setColspan(1);
                phrase = new Phrase("Amount KShs", pFontHeader1);
                table.addCell(phrase);

                while (rset1.next()) {
                  table.getDefaultCell().setColspan(1);
                  table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase(rset1.getObject(1).toString(), pFontHeader);

                  table.addCell(phrase);
                  table.getDefaultCell().setColspan(2);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase(rset1.getObject(2).toString(), pFontHeader);

                  table.addCell(phrase);
                  table.getDefaultCell().setColspan(1);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase(rset1.getObject(3).toString(), pFontHeader);

                  table.addCell(phrase);

                  table.getDefaultCell().setColspan(1);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase(rset1.getObject(4).toString(), pFontHeader);

                  table.addCell(phrase);

                  table.getDefaultCell().setColspan(1);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(rset1.getString(5)),
                          pFontHeader);

                  table.addCell(phrase);
                }

                table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

                table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);

                while (rsetTotals.next()) {

                  table.getDefaultCell().setColspan(3);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase("Total", pFontHeader);

                  table.addCell(phrase);

                  table.getDefaultCell().setColspan(3);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(rsetTotals.getString(1)),
                          pFontHeader);

                  table.addCell(phrase);

                  // phrase = new Phrase(" ");

                }

                docPdf.add(table);

              } catch (java.sql.SQLException SqlExec) {

                javax.swing.JOptionPane.showMessageDialog(
                    new javax.swing.JFrame(), SqlExec.getMessage());
              }
            } // }

          } catch (com.lowagie.text.BadElementException BadElExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

        } catch (java.io.FileNotFoundException fnfExec) {

          javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), fnfExec.getMessage());
        }
      } catch (com.lowagie.text.DocumentException lwDocexec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), lwDocexec.getMessage());
      }

      docPdf.close();

      try {

        if (System.getProperty("os.name").equalsIgnoreCase("Linux")) {

          System.out.println(tempFile);

          wait_for_Pdf2Show = rt.exec("kghostview " + tempFile + "");

          wait_for_Pdf2Show.waitFor();

        } else {

          wait_for_Pdf2Show =
              rt.exec("c:/Program Files/Adobe/Acrobat 5.0/Reader/AcroRd32.exe " + tempFile);

          wait_for_Pdf2Show.waitFor();
        }

      } catch (java.lang.InterruptedException intrExec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), intrExec.getMessage());
      }

    } catch (java.io.IOException IOexec) {

      javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), IOexec.getMessage());
    }
  }
  public void generatePdf(java.lang.String memNo) {

    java.lang.Process wait_for_Pdf2Show;

    java.util.Calendar cal = java.util.Calendar.getInstance();

    java.util.Date dateStampPdf = cal.getTime();

    java.lang.String pdfDateStamp = dateStampPdf.toString();

    try {

      java.io.File tempFile =
          java.io.File.createTempFile("REP" + this.getDateLable() + "_", ".pdf");

      tempFile.deleteOnExit();

      java.lang.Runtime rt = java.lang.Runtime.getRuntime();

      java.lang.String debitTotal = null;

      java.lang.String creditTotal = null;

      com.lowagie.text.Document docPdf = new com.lowagie.text.Document();
      double osBalance1 = 0.00;
      double osBalance = 0.00;
      double current = 0.00;
      double osBalancebf = 0.00;
      try {

        try {

          com.lowagie.text.pdf.PdfWriter.getInstance(
              docPdf, new java.io.FileOutputStream(tempFile));

          String compName = null;
          String date = null;
          String Messg = null;

          com.lowagie.text.HeaderFooter footer =
              new com.lowagie.text.HeaderFooter(
                  new Phrase("Allocation - Page: ", pFontHeader),
                  true); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 12,
                         // Font.BOLDITALIC,java.awt.Color.blue));

          docPdf.setFooter(footer);

          docPdf.open();

          try {

            java.util.Calendar calendar = java.util.Calendar.getInstance();

            long dateNow = calendar.getTimeInMillis();

            java.sql.Date datenowSql = new java.sql.Date(dateNow);

            System.out.println(datenowSql.toString());

            java.lang.Object listofStaffNos[] = this.getListofStaffNos();

            com.lowagie.text.pdf.PdfPTable table1 = new com.lowagie.text.pdf.PdfPTable(6);
            //  com.lowagie.text.Table table = new com.lowagie.text.Table(7);

            // table.endHeaders();

            int headerwidths[] = {15, 15, 30, 15, 15, 15};

            table1.setWidths(headerwidths);
            //  if (docPdf.getPageNumber() > 1) {
            //  table1.setHeaderRows(4);
            //  }
            table1.setWidthPercentage((100));

            table1.getDefaultCell().setBorder(Rectangle.BOTTOM);

            table1.getDefaultCell().setColspan(7);

            Phrase phrase = new Phrase();

            //  table.addCell(phrase);

            table1.getDefaultCell().setColspan(1);
            table1.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
            table1.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

            try {
              java.sql.Statement st3 = connectDB.createStatement();
              java.sql.ResultSet rset3 = st3.executeQuery("select header_name from pb_header");
              while (rset3.next()) table1.getDefaultCell().setColspan(6);

              table1.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
              phrase = new Phrase(rset3.getObject(1).toString(), pFontHeader11);
              table1.addCell(phrase);

            } catch (java.sql.SQLException SqlExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }
            docPdf.add(table1);
          } catch (com.lowagie.text.BadElementException BadElExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

          //  } catch(java.io.FileNotFoundException fnfExec) {

          //  javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(),
          // fnfExec.getMessage());

          /// }
          try {

            java.util.Calendar calendar = java.util.Calendar.getInstance();

            long dateNow = calendar.getTimeInMillis();

            java.sql.Date datenowSql = new java.sql.Date(dateNow);

            System.out.println(datenowSql.toString());

            java.lang.Object listofStaffNos[] = this.getListofStaffNos();

            com.lowagie.text.pdf.PdfPTable table = new com.lowagie.text.pdf.PdfPTable(10);
            //  com.lowagie.text.Table table = new com.lowagie.text.Table(7);

            // table.endHeaders();

            int headerwidths[] = {12, 10, 23, 10, 12, 13, 8, 11, 10, 8};

            table.setWidths(headerwidths);
            //  if (docPdf.getPageNumber() > 1) {
            table.setHeaderRows(10);
            //  }
            table.setWidthPercentage((100));

            table.getDefaultCell().setBorder(Rectangle.BOTTOM);

            table.getDefaultCell().setColspan(10);

            Phrase phrase = new Phrase();

            //  table.addCell(phrase);

            table.getDefaultCell().setColspan(1);
            table.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
            table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

            try {
              java.sql.Statement st22 = connectDB.createStatement();
              java.sql.Statement st11 = connectDB.createStatement();
              java.sql.Statement st = connectDB.createStatement();
              java.sql.Statement st1 = connectDB.createStatement();
              java.sql.Statement st2 = connectDB.createStatement();
              java.sql.Statement st3 = connectDB.createStatement();
              java.sql.Statement st4 = connectDB.createStatement();
              java.sql.Statement st41 = connectDB.createStatement();
              java.sql.Statement st5 = connectDB.createStatement();
              // java.sql.ResultSet rset3 = st3.executeQuery("select hospital_name,postal_code||'
              // '||box_no||' '||town,main_telno||'
              // '||other_telno,initcap(street),main_faxno,email,website,room_no from
              // pb_hospitalprofile");
              //  java.sql.ResultSet rset4 = st4.executeQuery("select scheme_manager from
              // ac_scheme_providers where scheme_manager = '"+memNo+"'");
              java.sql.ResultSet rset4 =
                  st4.executeQuery(
                      "select scheme_name,account_no from ac_schemes where account_no = '"
                          + memNo
                          + "'");
              java.sql.ResultSet rset41 =
                  st41.executeQuery(
                      "select payer_name from ac_schemes where account_no = '" + memNo + "'");
              //   java.sql.ResultSet rset1 = st1.executeQuery("select ac.date,pc.scheme_staff_no,
              // ac.payee,ac.reference_no,sum(ac.debit),ac.admission_no from hp_patient_card
              // pc,ac_debtors ac where pc.patient_no = ac.admission_no and ac.dealer = '"+memNo+"'
              // and ac.dealer = pc.scheme AND ac.date::date BETWEEN '"+beginDate+"' AND
              // '"+endDate+"' and ac.debit > 0 group by ac.date,pc.scheme_staff_no,
              // ac.payee,ac.reference_no,ac.admission_no");// UNION select
              // pd.date::date,initcap(pd.scheme_staff_no), (sh.first_name||' '||sh.second_name||'
              // '||sh.last_name) as name,pd.reference,sum(pd.credit),pd.patient_no from
              // hp_patient_card pd,hp_inpatient_register sh where pd.patient_no = sh.patient_no and
              // pd.isurer = '"+memNo+"' AND pd.date::date BETWEEN '"+beginDate+"' AND '"+endDate+"'
              // and pd.credit > 0 group by pd.date::date,pd.scheme_staff_no,
              // name,pd.reference,pd.invoice_no,pd.patient_no order by pd.invoice_no");
              java.sql.ResultSet rset1 =
                  st1.executeQuery(
                      "select al.date,db.admission_no,db.item,al.inv_no,al.inv_date,db.debit, al.cheque_no,al.amount,al.amount_alloc,(db.debit-al.amount_alloc) from db_allocation al,ac_debtors db where al.acc_no = '"
                          + memNo
                          + "' and al.inv_no = db.invoice_no AND al.date::date BETWEEN '"
                          + beginDate
                          + "' AND '"
                          + endDate
                          + "' order by al.date"); // UNION select
                                                   // pd.date::date,initcap(pd.scheme_staff_no),
                                                   // (sh.first_name||' '||sh.second_name||'
                                                   // '||sh.last_name) as
                                                   // name,pd.reference,sum(pd.credit),pd.patient_no
                                                   // from hp_patient_card pd,hp_inpatient_register
                                                   // sh where pd.patient_no = sh.patient_no and
                                                   // pd.isurer = '"+memNo+"' AND pd.date::date
                                                   // BETWEEN '"+beginDate+"' AND '"+endDate+"' and
                                                   // pd.credit > 0 group by
                                                   // pd.date::date,pd.scheme_staff_no,
                                                   // name,pd.reference,pd.invoice_no,pd.patient_no
                                                   // order by pd.invoice_no");
              // java.sql.ResultSet rsetTotals2 = st22.executeQuery("select sum(debit),sum(balance)
              // from ac_debtors where account_no = '"+memNo+"' AND date::date < '"+beginDate+"'");

              //  java.sql.ResultSet rset1 = st1.executeQuery("select
              // pc.date::date,pc.scheme_staff_no, initcap(sh.first_name||' '||sh.second_name||'
              // '||sh.last_name) as name,pc.reference,sum(pc.credit),pc.patient_no from
              // hp_patient_card pc,hp_patient_register sh where pc.scheme_staff_no = sh.account_no
              // and pc.isurer = '"+memNo+"' AND pc.date::date BETWEEN '"+beginDate+"' AND
              // '"+endDate+"' and pc.credit > 0 group by pc.date::date,pc.scheme_staff_no,
              // name,pc.reference,pc.invoice_no,pc.patient_no");// UNION select
              // pd.date::date,initcap(pd.scheme_staff_no), (sh.first_name||' '||sh.second_name||'
              // '||sh.last_name) as name,pd.reference,sum(pd.credit),pd.patient_no from
              // hp_patient_card pd,hp_inpatient_register sh where pd.patient_no = sh.patient_no and
              // pd.isurer = '"+memNo+"' AND pd.date::date BETWEEN '"+beginDate+"' AND '"+endDate+"'
              // and pd.credit > 0 group by pd.date::date,pd.scheme_staff_no,
              // name,pd.reference,pd.invoice_no,pd.patient_no order by pd.invoice_no");
              //  java.sql.ResultSet rsetTotals = st2.executeQuery("select
              // sum(amount),sum(amount_alloc) from db_allocation where acc_no = '"+memNo+"' AND
              // date::date BETWEEN '"+beginDate+"' AND '"+endDate+"' and acc_no = '"+memNo+"'");

              table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);
              table.getDefaultCell().setBorderWidth(Rectangle.TOP | Rectangle.BOTTOM);

              table.getDefaultCell().setColspan(10);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
              phrase = new Phrase("Payment Allocation Report", pFontHeader11);
              table.addCell(phrase);

              while (rset4.next()) table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);
              table.getDefaultCell().setColspan(10);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase(rset4.getObject(1).toString(), pFontHeader1);
              table.addCell(phrase);
              table.getDefaultCell().setColspan(10);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Account No : " + rset4.getObject(2).toString(), pFontHeader1);
              table.addCell(phrase);
              while (rset41.next()) table.getDefaultCell().setColspan(10);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase(rset41.getString(1), pFontHeader1);
              table.addCell(phrase);

              //  while (rset4.next())

              table.getDefaultCell().setColspan(5);

              try {
                java.text.DateFormat dateFormat =
                    java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM); // MEDIUM);

                java.util.Date endDate1 =
                    dateFormat.parse(endDate.toLocaleString()); // dateInstance.toLocaleString());
                java.util.Date endDate11 =
                    dateFormat.parse(beginDate.toLocaleString()); // dateInstance.toLocaleString());

                System.out.println("" + endDate1);
                //  phrase = new Phrase(bank +" Report: " +dateFormat.format(formattedDate),
                // pFontHeader);

                //  table.addCell(phrase);

                table.getDefaultCell().setColspan(6);
                phrase =
                    new Phrase(
                        "Period : "
                            + dateFormat.format(endDate11)
                            + " - "
                            + dateFormat.format(endDate1),
                        pFontHeader1);

                table.addCell(phrase);
              } catch (java.text.ParseException psExec) {

                javax.swing.JOptionPane.showMessageDialog(
                    new javax.swing.JFrame(), psExec.getMessage());
              }
              // table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              // phrase = new Phrase("Period : '"+beginDate+"' - '"+endDate+"'", pFontHeader1);
              // table.addCell(phrase);

              table.getDefaultCell().setColspan(4);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase(" Date Printed " + datenowSql, pFontHeader1);
              table.addCell(phrase);
              table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

              table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              //  table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

              table.getDefaultCell().setColspan(1);
              phrase = new Phrase("Date", pFontHeader1);
              table.addCell(phrase);
              table.getDefaultCell().setColspan(1);
              phrase = new Phrase("Patient No.", pFontHeader1);
              table.addCell(phrase);
              phrase = new Phrase("Patient Name", pFontHeader1);
              table.addCell(phrase);
              table.getDefaultCell().setColspan(1);
              phrase = new Phrase("Inv. No.", pFontHeader1);
              table.addCell(phrase);
              phrase = new Phrase("Inv.Date", pFontHeader1);
              table.addCell(phrase);
              phrase = new Phrase("Inv. Amt.", pFontHeader1);
              table.addCell(phrase);

              phrase = new Phrase("Chq No. ", pFontHeader1);
              table.addCell(phrase);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              table.getDefaultCell().setColspan(1);

              table.getDefaultCell().setColspan(1);
              phrase = new Phrase("Chq. Amt. ", pFontHeader1);
              table.addCell(phrase);
              phrase = new Phrase("Amt.Alloc ", pFontHeader1);
              table.addCell(phrase);
              phrase = new Phrase("Inv. Bal. ", pFontHeader1);
              table.addCell(phrase);
              table.getDefaultCell().setBorderWidth(Rectangle.BOTTOM);
              // table

              while (rset1.next()) {
                table.getDefaultCell().setColspan(1);
                table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(dbObject.getDBObject(rset1.getObject(1), "-"), pFontHeader);

                table.addCell(phrase);
                table.getDefaultCell().setColspan(1);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(dbObject.getDBObject(rset1.getObject(2), "-"), pFontHeader);

                table.addCell(phrase);
                table.getDefaultCell().setColspan(1);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(dbObject.getDBObject(rset1.getObject(3), "-"), pFontHeader);

                table.addCell(phrase);

                table.getDefaultCell().setColspan(1);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(dbObject.getDBObject(rset1.getObject(4), "-"), pFontHeader);

                table.addCell(phrase);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(dbObject.getDBObject(rset1.getObject(5), "-"), pFontHeader);

                table.addCell(phrase);
                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset1.getString(6)),
                        pFontHeader);
                // osBalance1 = osBalance1 + rset1.getDouble(5);
                table.addCell(phrase);
                table.getDefaultCell().setColspan(1);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(dbObject.getDBObject(rset1.getObject(7), "-"), pFontHeader);

                table.addCell(phrase);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset1.getString(8)),
                        pFontHeader);
                // osBalance1 = osBalance1 + rset1.getDouble(5);
                table.addCell(phrase);
                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset1.getString(9)),
                        pFontHeader);
                osBalancebf = osBalancebf + rset1.getDouble(9);
                // osBalancebf = osBalance1;
                table.addCell(phrase);
                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset1.getString(10)),
                        pFontHeader);
                osBalance1 = osBalance1 + rset1.getDouble(10);
                table.addCell(phrase);
              }
              table.getDefaultCell().setColspan(3);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
              phrase = new Phrase("", pFontHeader);

              table.addCell(phrase);
              table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

              table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);

              //  while (rsetTotals.next()) {

              table.getDefaultCell().setColspan(5);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Total", pFontHeader);

              table.addCell(phrase);

              table.getDefaultCell().setColspan(1);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(osBalancebf)),
                      pFontHeader);

              table.addCell(phrase);
              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(osBalance1)),
                      pFontHeader);

              table.addCell(phrase);
              // phrase = new Phrase(" ");

              //     }
              /*   try {


                    java.sql.Statement st31 = connectDB.createStatement();
                    // java.sql.Statement st4 = connectDB.createStatement();
                    java.sql.ResultSet rset2 = st31.executeQuery("select name from pb_notice");

                    // java.sql.ResultSet rset2 = st3.executeQuery("SELECT hospital_name from pb_hospitalprofile");
                    //   java.sql.ResultSet rset4 = st4.executeQuery("SELECT date('now') as Date");
                    while(rset2.next())
                        Messg = rset2.getString(1);

                    com.lowagie.text.HeaderFooter footer = new com.lowagie.text.HeaderFooter(new Phrase(""+Messg+""),false);// FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14, Font.BOLDITALIC,java.awt.Color.blue)));

                    //  com.lowagie.text.HeaderFooter headerFoter = new com.lowagie.text.HeaderFooter(new Phrase(""+compName+""),false);// FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14, Font.BOLDITALIC,java.awt.Color.blue)));
                    //  headerFoter.ALIGN_CENTER;
                    //  headerFoter.setRight(5);
                    docPdf.setFooter(footer);




                } catch(java.sql.SQLException SqlExec) {

                    javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), SqlExec.getMessage());

                }

              */
              docPdf.add(table);

            } catch (java.sql.SQLException SqlExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }

            // }  // }

          } catch (com.lowagie.text.BadElementException BadElExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

        } catch (java.io.FileNotFoundException fnfExec) {

          javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), fnfExec.getMessage());
        }
      } catch (com.lowagie.text.DocumentException lwDocexec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), lwDocexec.getMessage());
      }

      docPdf.close();

      try {

        if (System.getProperty("os.name").equalsIgnoreCase("Linux")) {

          System.out.println(tempFile);

          wait_for_Pdf2Show = rt.exec("kghostview " + tempFile + "");

          wait_for_Pdf2Show.waitFor();

        } else {

          wait_for_Pdf2Show =
              rt.exec("c:/Program Files/Adobe/Acrobat 5.0/Reader/AcroRd32.exe " + tempFile);

          wait_for_Pdf2Show.waitFor();
        }

      } catch (java.lang.InterruptedException intrExec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), intrExec.getMessage());
      }

    } catch (java.io.IOException IOexec) {

      javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), IOexec.getMessage());
    }
  }
  public void generatePdf(java.lang.String memNo) {

    java.lang.Process wait_for_Pdf2Show;

    java.util.Calendar cal = java.util.Calendar.getInstance();

    java.util.Date dateStampPdf = cal.getTime();

    java.lang.String pdfDateStamp = dateStampPdf.toString();

    try {

      java.io.File tempFile =
          java.io.File.createTempFile("REP" + this.getDateLable() + "_", ".pdf");

      tempFile.deleteOnExit();

      java.lang.Runtime rt = java.lang.Runtime.getRuntime();

      java.lang.String debitTotal = null;

      java.lang.String creditTotal = null;

      com.lowagie.text.Document docPdf = new com.lowagie.text.Document();

      try {

        try {

          com.lowagie.text.pdf.PdfWriter.getInstance(
              docPdf, new java.io.FileOutputStream(tempFile));

          String compName = null;
          String date = null;
          String Messg = null;

          try {

            java.sql.Statement st3 = connectDB.createStatement();
            java.sql.Statement st4 = connectDB.createStatement();
            java.sql.ResultSet rset2 = st3.executeQuery("select name from interim_footer");

            // java.sql.ResultSet rset2 = st3.executeQuery("SELECT hospital_name from
            // pb_hospitalprofile");
            //   java.sql.ResultSet rset4 = st4.executeQuery("SELECT date('now') as Date");
            while (rset2.next()) Messg = rset2.getObject(1).toString();

            //  while(rset4.next())
            //    date = rset4.getObject(1).toString();

            com.lowagie.text.HeaderFooter footer =
                new com.lowagie.text.HeaderFooter(
                    new Phrase("" + Messg + ""),
                    false); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
            // Font.BOLDITALIC,java.awt.Color.blue)));

            //  com.lowagie.text.HeaderFooter headerFoter = new com.lowagie.text.HeaderFooter(new
            // Phrase(""+compName+""),false);//
            // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
            // Font.BOLDITALIC,java.awt.Color.blue)));
            //  headerFoter.ALIGN_CENTER;
            //  headerFoter.setRight(5);
            docPdf.setFooter(footer);

          } catch (java.sql.SQLException SqlExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), SqlExec.getMessage());
          }

          docPdf.open();

          try {

            com.lowagie.text.pdf.PdfPTable table = new com.lowagie.text.pdf.PdfPTable(6);

            int headerwidths[] = {25, 25, 20, 15, 15, 15};

            table.setWidths(headerwidths);
            // table..setWidths(headerwidths);
            table.setWidthPercentage((100));

            table.getDefaultCell().setBorder(Rectangle.BOTTOM);

            table.getDefaultCell().setColspan(6);

            Phrase phrase = new Phrase();

            //  table.addCell(phrase);

            table.getDefaultCell().setColspan(1);
            table.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
            table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

            try {
              java.sql.Statement stc = connectDB.createStatement();
              java.sql.Statement stb = connectDB.createStatement();
              java.sql.Statement sta = connectDB.createStatement();
              java.sql.Statement st3 = connectDB.createStatement();
              java.sql.Statement st11 = connectDB.createStatement();
              java.sql.Statement st21 = connectDB.createStatement();
              java.sql.Statement st22 = connectDB.createStatement();
              java.sql.Statement st = connectDB.createStatement();
              java.sql.Statement st1 = connectDB.createStatement();
              java.sql.Statement st2 = connectDB.createStatement();
              java.sql.Statement st32 = connectDB.createStatement();
              java.sql.ResultSet rsetc =
                  stc.executeQuery(
                      "select discharge from hp_admission where patient_no = '"
                          + memNo
                          + "' order by date_admitted desc LIMIT 1");

              java.sql.ResultSet rsetb =
                  stb.executeQuery(
                      "select (discharge_date - adm_date) from hp_inpatient_register where patient_no = '"
                          + memNo
                          + "'");

              java.sql.ResultSet rset3 =
                  st3.executeQuery(
                      "select hospital_name,postal_code||' '||box_no||' '||town,main_telno||' '||other_telno,initcap(street),main_faxno,email,website,room_no from pb_hospitalprofile");
              java.sql.ResultSet rset22 = st32.executeQuery("SELECT header_name from pb_header");
              java.sql.ResultSet rseta =
                  sta.executeQuery(
                      "select ad.ward,ad.bed_no,ad.doctor ,pr.adm_date,pr.discharge_date from hp_admission ad,hp_inpatient_register pr where pr.patient_no = '"
                          + memNo
                          + "' and pr.patient_no = ad.patient_no");

              //  java.sql.ResultSet rset = st.executeQuery("select DISTINCT member_code,
              // member_name,date from shares_transactions order by member_code");
              java.sql.ResultSet rset =
                  st.executeQuery(
                      "select patient_no,initcap(second_name||' '||first_name||' '||last_name),address,residence,tel_no,payer,description,category from hp_inpatient_register where patient_no = '"
                          + memNo
                          + "'");
              java.sql.ResultSet rset1 =
                  st1.executeQuery(
                      " select date::date,initcap(service) as service,dosage,reference,debit-credit from hp_patient_card where patient_no = '"
                          + memNo
                          + "' AND date::date >= '"
                          + beginDate
                          + "' AND (reference IS NULL OR reference != '') and service != 'N.H.I.F' AND service != 'Receipt' order by date::date"); // union select date::date,initcap(service) as service,dosage,reference,credit from hp_patient_card where patient_no = '"+memNo+"' and credit > 0 order by date");
              java.sql.ResultSet rsetTotals =
                  st2.executeQuery(
                      "select sum(debit - credit) from hp_patient_card where patient_no = '"
                          + memNo
                          + "' AND date::date >= '"
                          + beginDate
                          + "' and service != 'N.H.I.F' and service != 'Receipt'");

              java.sql.ResultSet rset11 =
                  st11.executeQuery(
                      " select date::date,initcap(service) as service,dosage,reference,credit-debit from hp_patient_card where patient_no = '"
                          + memNo
                          + "' AND date::date >= '"
                          + beginDate
                          + "' AND (service = 'N.H.I.F' OR service = 'Receipt') AND invoice_no NOT LIKE 'I%' AND invoice_no NOT LIKE 'O%' order by date::date"); // union select date::date,initcap(service) as service,dosage,reference,credit from hp_patient_card where patient_no = '"+memNo+"' and credit > 0 order by date");
              java.sql.ResultSet rsetTotals1 =
                  st21.executeQuery(
                      "select sum(credit-debit) from hp_patient_card where patient_no = '"
                          + memNo
                          + "' AND date::date >= '"
                          + beginDate
                          + "' and (service = 'N.H.I.F' OR service = 'Receipt') AND invoice_no NOT LIKE 'I%' AND invoice_no NOT LIKE 'O%'");

              while (rset22.next()) // {
              table.getDefaultCell().setColspan(6);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
              phrase = new Phrase(dbObject.getDBObject(rset22.getObject(1), "-"), pFontHeader11);
              table.addCell(phrase);

              /*
                    table.getDefaultCell().setColspan(6);
                    table.getDefaultCell().setBorderColor(java.awt.Color.white);

                    table.getDefaultCell().setColspan(2);

                    table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                    phrase = new Phrase("Address:"+"\t"+dbObject.getDBObject(rset3.getObject(2), "-"), pFontHeader);
                    table.addCell(phrase);

                    table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                    phrase = new Phrase("Tel:"+"\t"+dbObject.getDBObject(rset3.getObject(3), "-"), pFontHeader);

                    table.addCell(phrase);


                    table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                    phrase = new Phrase("Fax:"+"\t"+ dbObject.getDBObject(rset3.getObject(5), "-"), pFontHeader);

                    table.addCell(phrase);
                    table.getDefaultCell().setBorderColor(java.awt.Color.white);
                    table.getDefaultCell().setColspan(3);
                    table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                    phrase = new Phrase("Email:"+"\t"+dbObject.getDBObject(rset3.getObject(6), "-"), pFontHeader);
                    table.addCell(phrase);


                    table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                    phrase = new Phrase("Website:"+"\t" +dbObject.getDBObject(rset3.getObject(7), "-"), pFontHeader);

                    table.addCell(phrase);
                    /// table.addCell("  ");


                }
              */
              table.getDefaultCell().setColspan(6);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
              phrase = new Phrase("Interim Invoice", pFontHeader1);
              table.addCell(phrase);
              table.getDefaultCell().setBorder(Rectangle.BOTTOM);
              table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);
              while (rset.next()) table.getDefaultCell().setColspan(3);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Patient No:  " + memNo, pFontHeader1);
              table.addCell(phrase);

              while (rseta.next())
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase =
                  new Phrase(
                      "Ward: " + dbObject.getDBObject(rseta.getObject(1), "-"), pFontHeader1);
              table.addCell(phrase);
              while (rset.next()) table.getDefaultCell().setColspan(3);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase =
                  new Phrase(
                      "Patient Name: " + dbObject.getDBObject(rset.getObject(2), "-"),
                      pFontHeader1);
              table.addCell(phrase);

              while (rseta.next())
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase =
                  new Phrase(
                      "Bed No: " + dbObject.getDBObject(rseta.getObject(2), "-"), pFontHeader1);
              table.addCell(phrase);

              while (rset.next()) table.getDefaultCell().setColspan(3);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase =
                  new Phrase(
                      "Address: " + dbObject.getDBObject(rset.getObject(3), "-"), pFontHeader1);
              table.addCell(phrase);

              while (rseta.next())
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase =
                  new Phrase(
                      "Doctor: " + dbObject.getDBObject(rseta.getObject(3), "-"), pFontHeader1);
              table.addCell(phrase);
              while (rset.next()) table.getDefaultCell().setColspan(3);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase =
                  new Phrase("Town  " + dbObject.getDBObject(rset.getObject(4), "-"), pFontHeader1);
              table.addCell(phrase);

              while (rseta.next())
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase =
                  new Phrase(
                      "Adm Date: " + dbObject.getDBObject(rseta.getObject(4), "-"), pFontHeader1);
              table.addCell(phrase);

              table.getDefaultCell().setColspan(3);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase =
                  new Phrase(
                      "TEl No. " + dbObject.getDBObject(rset.getObject(5), "-"), pFontHeader1);
              table.addCell(phrase);

              while (rseta.next())
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase =
                  new Phrase(
                      "Discharge Date: " + dbObject.getDBObject(rseta.getObject(5), "-"),
                      pFontHeader1);
              table.addCell(phrase);
              while (rset.next()) table.getDefaultCell().setColspan(3);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase =
                  new Phrase(
                      "Payer : " + dbObject.getDBObject(rset.getObject(6), "-"), pFontHeader1);
              table.addCell(phrase);
              table.getDefaultCell().setColspan(3);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase =
                  new Phrase(
                      "Patient Category : " + dbObject.getDBObject(rset.getObject(8), "-"),
                      pFontHeader1);
              table.addCell(phrase);
              table.getDefaultCell().setColspan(3);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase =
                  new Phrase(
                      "Scheme Name : " + dbObject.getDBObject(rset.getObject(7), "-"),
                      pFontHeader1);
              table.addCell(phrase);

              //      if(rsetc.getBoolean(1) == true){
              while (rsetb.next()) table.getDefaultCell().setColspan(3);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase =
                  new Phrase(
                      "No.of Days : " + dbObject.getDBObject(rsetb.getObject(1), "-"),
                      pFontHeader1);
              table.addCell(phrase);
              //  }
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);
              table.getDefaultCell().setBorderWidth(Rectangle.TOP);
              table.getDefaultCell().setColspan(1);
              phrase = new Phrase("Date", pFontHeader1);
              table.addCell(phrase);
              table.getDefaultCell().setColspan(1);
              phrase = new Phrase("Description", pFontHeader1);
              table.addCell(phrase);
              table.getDefaultCell().setColspan(1);
              phrase = new Phrase("Qty", pFontHeader1);
              table.addCell(phrase);

              table.getDefaultCell().setColspan(1);
              phrase = new Phrase("Ref", pFontHeader1);
              table.addCell(phrase);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              table.getDefaultCell().setColspan(1);
              phrase = new Phrase("Amt. KShs", pFontHeader1);
              table.addCell(phrase);
              phrase = new Phrase("Bal. KShs", pFontHeader1);
              table.addCell(phrase);

              while (rset1.next()) {
                table.getDefaultCell().setColspan(1);
                table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(dbObject.getDBObject(rset1.getObject(1), "-"), pFontHeader);

                table.addCell(phrase);
                table.getDefaultCell().setColspan(1);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(dbObject.getDBObject(rset1.getObject(2), "-"), pFontHeader);

                table.addCell(phrase);
                table.getDefaultCell().setColspan(1);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(dbObject.getDBObject(rset1.getObject(3), "-"), pFontHeader);

                table.addCell(phrase);

                table.getDefaultCell().setColspan(1);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(dbObject.getDBObject(rset1.getObject(4), "-"), pFontHeader);

                table.addCell(phrase);

                table.getDefaultCell().setColspan(1);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset1.getString(5)),
                        pFontHeader);

                table.addCell(phrase);

                osBalance = osBalance + rset1.getDouble(5);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(java.lang.String.valueOf(osBalance)),
                        pFontHeader);
                //   current = current + osBalance;

                table.addCell(phrase);
              }

              table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

              table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);

              while (rsetTotals.next()) {

                table.getDefaultCell().setColspan(3);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase("Invoice Amount", pFontHeader1);

                table.addCell(phrase);

                table.getDefaultCell().setColspan(3);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rsetTotals.getString(1)),
                        pFontHeader);

                // table.addCell(phrase);
                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(java.lang.String.valueOf(osBalance)),
                        pFontHeader1);

                table.addCell(phrase);

                // phrase = new Phrase(" ");

              }

              while (rset11.next()) {
                table.getDefaultCell().setColspan(1);
                table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(dbObject.getDBObject(rset11.getObject(1), "-"), pFontHeader);

                table.addCell(phrase);
                table.getDefaultCell().setColspan(1);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(dbObject.getDBObject(rset11.getObject(2), "-"), pFontHeader);

                table.addCell(phrase);
                table.getDefaultCell().setColspan(1);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(" ", pFontHeader);

                table.addCell(phrase);

                table.getDefaultCell().setColspan(1);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(dbObject.getDBObject(rset11.getObject(4), "-"), pFontHeader);

                table.addCell(phrase);

                table.getDefaultCell().setColspan(1);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset11.getString(5)),
                        pFontHeader);

                table.addCell(phrase);

                osBalance1 = osBalance1 + rset11.getDouble(5);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(java.lang.String.valueOf(osBalance1)),
                        pFontHeader);
                //   current = current + osBalance;

                table.addCell(phrase);
              }

              table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

              table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);
              while (rsetTotals1.next()) {

                table.getDefaultCell().setColspan(2);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(" ", pFontHeader);

                table.addCell(phrase);
                table.getDefaultCell().setColspan(2);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase("Total Receipts", pFontHeader1);

                table.addCell(phrase);

                table.getDefaultCell().setColspan(2);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rsetTotals1.getString(1)),
                        pFontHeader);

                // table.addCell(phrase);
                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(java.lang.String.valueOf(osBalance1)),
                        pFontHeader1);

                table.addCell(phrase);

                // phrase = new Phrase(" ");

              }

              //  while (rsetTotals.next()) {
              table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

              table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);
              table.getDefaultCell().setColspan(2);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase(" ", pFontHeader);

              table.addCell(phrase);
              table.getDefaultCell().setColspan(2);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Net Amount", pFontHeader1);

              table.addCell(phrase);

              table.getDefaultCell().setColspan(2);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              //  phrase = new Phrase(new
              // com.afrisoftech.sys.Format2Currency().Format2Currency(rsetTotals.getString(1)),pFontHeader);

              // table.addCell(phrase);
              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(osBalance - osBalance1)),
                      pFontHeader1);

              table.addCell(phrase);

              // phrase = new Phrase(" ");

              // }
              docPdf.add(table);

            } catch (java.sql.SQLException SqlExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }

            // }

          } catch (com.lowagie.text.BadElementException BadElExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

        } catch (java.io.FileNotFoundException fnfExec) {

          javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), fnfExec.getMessage());
        }
      } catch (com.lowagie.text.DocumentException lwDocexec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), lwDocexec.getMessage());
      }

      docPdf.close();

      try {

        if (System.getProperty("os.name").equalsIgnoreCase("Linux")) {

          System.out.println(tempFile);

          wait_for_Pdf2Show = rt.exec("kghostview " + tempFile + "");

          wait_for_Pdf2Show.waitFor();

        } else {

          wait_for_Pdf2Show =
              rt.exec("c:/Program Files/Adobe/Acrobat 5.0/Reader/AcroRd32.exe " + tempFile);

          wait_for_Pdf2Show.waitFor();
        }

      } catch (java.lang.InterruptedException intrExec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), intrExec.getMessage());
      }

    } catch (java.io.IOException IOexec) {

      javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), IOexec.getMessage());
    }
  }
  public void generatePdf() {

    java.lang.Object[][] rangeDates = ageingSeries.getAgeingDateSeries();

    // ageingDates = ageingSeries.getAgeingDateSeries();

    double columnTotals[] = new double[rangeDates.length];

    java.lang.Process wait_for_Pdf2Show;

    java.util.Calendar cal = java.util.Calendar.getInstance();

    java.util.Date dateStampPdf = cal.getTime();

    java.lang.String pdfDateStamp = dateStampPdf.toString();

    int interval = 0;

    try {

      java.io.File tempFile =
          java.io.File.createTempFile("REP" + this.getDateLable() + "_", ".pdf");

      tempFile.deleteOnExit();

      java.lang.Runtime rt = java.lang.Runtime.getRuntime();

      java.lang.String debitTotal = null;

      java.lang.String creditTotal = null;

      com.lowagie.text.Document docPdf = new com.lowagie.text.Document();

      java.util.Calendar calendar = java.util.Calendar.getInstance();

      long dateNow = calendar.getTimeInMillis();

      java.sql.Date datenowSql = new java.sql.Date(dateNow);

      System.out.println(datenowSql.toString());

      try {

        try {

          com.lowagie.text.pdf.PdfWriter.getInstance(
              docPdf, new java.io.FileOutputStream(tempFile));

          String compName = null;
          String date = null;
          try {

            //   java.sql.Connection conDb =
            // java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/sako","postgres","pilsiner");

            java.sql.Statement st3 = connectDB.createStatement();
            java.sql.Statement st4 = connectDB.createStatement();

            java.sql.ResultSet rset2 =
                st3.executeQuery("SELECT hospital_name from pb_hospitalprofile");
            java.sql.ResultSet rset4 = st4.executeQuery("SELECT date('now') as Date");
            while (rset2.next()) compName = rset2.getObject(1).toString();

            while (rset4.next()) date = rset4.getObject(1).toString();

            com.lowagie.text.HeaderFooter headerFoter =
                new com.lowagie.text.HeaderFooter(
                    new Phrase("" + compName, pFontHeader2),
                    false); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
                            // Font.BOLDITALIC,java.awt.Color.blue)));

            //  com.lowagie.text.HeaderFooter headerFoter = new com.lowagie.text.HeaderFooter(new
            // Phrase(""+compName+""),false);//
            // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
            // Font.BOLDITALIC,java.awt.Color.blue)));
            headerFoter.setAlignment(com.lowagie.text.HeaderFooter.ALIGN_CENTER);
            docPdf.setHeader(headerFoter);

          } catch (java.sql.SQLException SqlExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), SqlExec.getMessage());
          }

          com.lowagie.text.HeaderFooter footer =
              new com.lowagie.text.HeaderFooter(
                  new Phrase("Ageing  Page: ", pFontHeader),
                  true); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 12,
                         // Font.BOLDITALIC,java.awt.Color.blue));

          docPdf.setFooter(footer);

          docPdf.open();

          double Totals = 0.00;
          double OS = 0.00;
          try {

            com.lowagie.text.pdf.PdfPTable table =
                new com.lowagie.text.pdf.PdfPTable(rangeDates.length + 3);

            String headerWidths = null;

            java.util.Vector headerVector = new java.util.Vector(1, 1);

            int z = rangeDates.length;

            int headerwidths[] = {35, 15, 15, 10, 10};

            table.setWidths(headerwidths);

            table.setWidthPercentage((107));

            table.setHeaderRows(2);

            table.getDefaultCell().setBorder(Rectangle.BOTTOM);

            table.getDefaultCell().setColspan(5);
            Phrase phrase = new Phrase("");
            table.getDefaultCell().setColspan(3);
            try {
              java.text.DateFormat dateFormat =
                  java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM); // MEDIUM);

              java.util.Date endDate2 = dateFormat.parse(endDate.toLocaleString());

              phrase =
                  new Phrase("Weekly Sales Comparison " + dateFormat.format(endDate2), pFontHeader);

              table.addCell(phrase);
            } catch (java.text.ParseException psExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), psExec.getMessage());
            }

            table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
            table.getDefaultCell().setColspan(2);
            phrase = new Phrase("Printed on : " + date, pFontHeader);
            table.addCell(phrase);

            table.getDefaultCell().setColspan(1);

            table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
            phrase = new Phrase("DESCRIPTION", pFontHeader);
            table.addCell(phrase);
            table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
            for (int x = 0; x < rangeDates.length; x++) {

              int days = 1;
              try {

                // Date parser

                java.text.SimpleDateFormat dateFormat =
                    new java.text.SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
                com.afrisoftech.lib.DateFormatter dateFormatter =
                    new com.afrisoftech.lib.DateFormatter(
                        dateFormat.parse(rangeDates[x][0].toString().trim()), "w");
                com.afrisoftech.lib.DateFormatter yearFormatterCurrent =
                    new com.afrisoftech.lib.DateFormatter(endDate, "yy");

                java.lang.String monthString = dateFormatter.getDateString();
                java.lang.String yearStringCurrent = yearFormatterCurrent.getDateString();
                // int days = 1;
                //  if (x < 1) {

                com.afrisoftech.lib.DateFormatter dateFormatterCurrent =
                    new com.afrisoftech.lib.DateFormatter(endDate, "w");
                com.afrisoftech.lib.DateFormatter yearFormatterCurrent1 =
                    new com.afrisoftech.lib.DateFormatter(endDate, "yy");

                java.lang.String monthStringCurrent = dateFormatterCurrent.getDateString();
                java.lang.String yearStringCurrent1 = yearFormatterCurrent1.getDateString();

                //      phrase = new Phrase("Week ["+monthStringCurrent+"/"+yearStringCurrent1+"]"
                // ,pFontHeader);
                //  } else {
                //       phrase = new Phrase("Week ["+monthString+"/"+yearStringCurrent+"]",
                // pFontHeader);
                //                                phrase = new Phrase("+ "+x*days +"
                // Month",pFontHeader);
                interval = x;
                //  }

                table.addCell(phrase);

                // Catch java.text.parse exception.

              } catch (java.text.ParseException prs) {
                prs.printStackTrace();
              }
            }
            //  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
            phrase = new Phrase("Change % (+/-)", pFontHeader);
            table.addCell(phrase);
            phrase = new Phrase("Change In Reporting Rate", pFontHeader);
            table.addCell(phrase);

            table.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
            // table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

            try {

              double GrandTotal = 0.00;
              double Over120Total = 0.00;
              double TurnOver = 0.00;
              java.lang.Object[] listofAct = this.getListofActivities();

              //    java.sql.Connection conDb1 =
              // java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/sako","postgres","pilsiner");

              System.out.println(listofAct.length);
              double Over120 = 0.00;
              for (int i = 0; i < listofAct.length; i++) {

                double TotalCount = 0.00;

                table.getDefaultCell().setColspan(1);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                table.getDefaultCell().setColspan(1);
                java.sql.Statement stmta1 = connectDB.createStatement();
                java.sql.PreparedStatement pSeta1 =
                    connectDB.prepareStatement(
                        "SELECT count(activity_code) FROM ac_ledger where  activity_code = '"
                            + listofAct[i]
                            + "'");
                java.sql.PreparedStatement pset22 =
                    connectDB.prepareStatement(
                        "select activity from pb_activity WHERE code = ?"); // < '"+endDate+"'::date
                                                                            // and date >
                                                                            // '"+endDate+"'::date -
                                                                            // 30 group by dealer");
                // java.sql.PreparedStatement pset22 = connectDB.prepareStatement("select
                // description from ac_ledger WHERE activity_code = ?");//< '"+endDate+"'::date and
                // date > '"+endDate+"'::date - 30 group by dealer");

                pset22.setString(1, listofAct[i].toString().toUpperCase());
                java.sql.ResultSet rSeta1 = pSeta1.executeQuery();
                while (rSeta1.next()) {
                  name = rSeta1.getInt(1);
                }
                if (name > 0) {
                  java.sql.ResultSet rset22 = pset22.executeQuery();

                  while (rset22.next()) {

                    table.getDefaultCell().setColspan(1);
                    //  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
                    phrase =
                        new Phrase(
                            dbObject.getDBObject(rset22.getObject(1).toString().toUpperCase(), "-"),
                            pFontHeader1);
                    table.addCell(phrase);
                  }

                  java.sql.Statement st2 = connectDB.createStatement();
                  java.sql.Statement st21 = connectDB.createStatement();
                  java.sql.Statement st22 = connectDB.createStatement();
                  java.sql.Statement st23 = connectDB.createStatement();
                  java.sql.Statement st211 = connectDB.createStatement();
                  java.sql.Statement st221 = connectDB.createStatement();
                  java.sql.Statement st231 = connectDB.createStatement();
                  java.sql.Statement st2A = connectDB.createStatement();
                  java.sql.Statement st2B = connectDB.createStatement();
                  java.sql.Statement st2C = connectDB.createStatement();
                  java.sql.Statement stc = connectDB.createStatement();
                  System.out.println("Dealer Is : [" + listofAct[i] + "].");

                  java.sql.PreparedStatement pset1 =
                      connectDB.prepareStatement(
                          "select sum(credit-debit) from ac_ledger WHERE activity_code = ?  AND date = '"
                              + rangeDates[rangeDates.length - 1][0]
                              + "'"); // AND '"+rangeDates[rangeDates.length - 1][1]+"'");//<
                                      // '"+endDate+"'::date and date > '"+endDate+"'::date - 30
                                      // group by dealer");
                  pset1.setString(1, listofAct[i].toString());
                  java.sql.ResultSet rset1 = pset1.executeQuery();

                  //    java.sql.PreparedStatement pset111 = connectDB.prepareStatement("select
                  // sum(credit-debit) from ac_ledger WHERE activity_code = ?  AND date <
                  // '"+endDate+"'");//< '"+endDate+"'::date and date > '"+endDate+"'::date - 30
                  // group by dealer");
                  //  pset111.setString(1,listofAct[i].toString());
                  //   java.sql.ResultSet rset111 = pset111.executeQuery();
                  //   java.sql.Statement st02 = connectDB.createStatement();

                  //   java.sql.PreparedStatement pset112 = connectDB.prepareStatement("select
                  // sum(credit-debit) from ac_ledger WHERE activity_code = ? AND date <
                  // '"+endDate+"'");//< '"+endDate+"'::date and date > '"+endDate+"'::date - 30
                  // group by dealer");
                  //   pset112.setString(1,listofAct[i].toString());
                  //   java.sql.ResultSet rset112 = pset112.executeQuery();

                  for (int t = 0; t < rangeDates.length; t++) {
                    java.sql.Statement st01 = connectDB.createStatement();
                    java.sql.PreparedStatement pset =
                        connectDB.prepareStatement(
                            "select sum(credit-debit) from ac_ledger WHERE activity_code = ?  AND date between '"
                                + rangeDates[t][0]
                                + "' AND '"
                                + rangeDates[t][1]
                                + "'"); // < '"+endDate+"'::date and date > '"+endDate+"'::date - 30
                                        // group by dealer");
                    pset.setString(1, listofAct[i].toString().toUpperCase());
                    java.sql.ResultSet rset = pset.executeQuery();

                    while (rset.next()) {

                      table.getDefaultCell().setColspan(1);
                      table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
                      phrase =
                          new Phrase(
                              new com.afrisoftech.sys.Format2Currency()
                                  .Format2Currency(rset.getString(1)),
                              pFontHeader1);
                      table.addCell(phrase);

                      columnTotals[t] = columnTotals[t] + rset.getDouble(1);
                      TotalCount = TotalCount + rset.getDouble(1);
                      TurnOver = rset.getDouble(1);
                    }
                  }
                  // phrase = new Phrase(new
                  // com.afrisoftech.sys.Format2Currency().Format2Currency(java.lang.String.valueOf(TotalCount)), pFontHeader);
                  //  table.addCell(phrase);
                  while (rset1.next()) {
                    Over120 = rset1.getDouble(1);

                    if (Over120 > 0) {

                      phrase =
                          new Phrase(
                              new com.afrisoftech.sys.Format2Currency()
                                  .Format2Currency(
                                      java.lang.String.valueOf((Over120 / TurnOver) * 100)),
                              pFontHeader);
                      table.addCell(phrase);
                      phrase = new Phrase("+", pFontHeader);
                      table.addCell(phrase);

                    } else {

                      phrase = new Phrase(java.lang.String.valueOf(0.00), pFontHeader);
                      table.addCell(phrase);
                      phrase = new Phrase("", pFontHeader);
                      table.addCell(phrase);
                    }
                  }
                  /*                                     }else{
                      if (TotalCount <= 0 && Over120 > 0){

                          phrase = new Phrase(java.lang.String.valueOf((Over120/TurnOver)*100), pFontHeader);
                          table.addCell(phrase);
                          phrase = new Phrase("+", pFontHeader);
                          table.addCell(phrase);

                      }else{
                          if (TotalCount > 0 && Over120 < 0){

                              phrase = new Phrase(java.lang.String.valueOf((Over120/TurnOver)*100), pFontHeader);
                              table.addCell(phrase);
                              phrase = new Phrase("-", pFontHeader);
                              table.addCell(phrase);
                          }else{
                              if (TotalCount > 0 && Over120 == 0){

                                  phrase = new Phrase(java.lang.String.valueOf((Over120/TurnOver)*100), pFontHeader);
                                  table.addCell(phrase);
                                  phrase = new Phrase("-", pFontHeader);
                                  table.addCell(phrase);
                              }else{
                                  if (TotalCount < 0 && Over120 < 0){

                                      phrase = new Phrase(java.lang.String.valueOf((Over120/TurnOver)*100), pFontHeader);
                                      table.addCell(phrase);
                                      phrase = new Phrase("-", pFontHeader);
                                      table.addCell(phrase);
                                  }else{

                                      phrase = new Phrase(java.lang.String.valueOf(0.00), pFontHeader);
                                      table.addCell(phrase);
                                      phrase = new Phrase("", pFontHeader);
                                      table.addCell(phrase);
                                  }
                              }
                          }
                      }
                  }*/
                  // }
                }
              }

              table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

              table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);

              // while (rsetTotals.next()) {

              table.getDefaultCell().setColspan(1);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Total", pFontHeader);

              table.addCell(phrase);

              table.getDefaultCell().setColspan(1);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              // phrase = new Phrase(new
              // com.afrisoftech.sys.Format2Currency().Format2Currency(rsetTotals.getString(1)),
              // pFontHeader);
              for (int x = 0; x < rangeDates.length; x++) {
                // phrase = new Phrase("Current"+2*x,pFontHeader);
                // table.addCell(phrase);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(java.lang.String.valueOf(columnTotals[x])),
                        pFontHeader);

                table.addCell(phrase);
              }
              phrase = new Phrase(" ", pFontHeader);
              table.addCell(phrase);
              table.addCell(phrase);
              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(Over120Total)),
                      pFontHeader);

              // table.addCell(phrase);
              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(OS)),
                      pFontHeader);

              //  table.addCell(phrase);

              docPdf.add(table);

            } catch (java.sql.SQLException SqlExec) {

              SqlExec.printStackTrace();

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }

          } catch (com.lowagie.text.BadElementException BadElExec) {

            // Bad

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

        } catch (java.io.FileNotFoundException fnfExec) {

          javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), fnfExec.getMessage());
        }
      } catch (com.lowagie.text.DocumentException lwDocexec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), lwDocexec.getMessage());
      }

      docPdf.close();

      try {

        if (System.getProperty("os.name").equalsIgnoreCase("Linux")) {

          System.out.println(tempFile);

          wait_for_Pdf2Show = rt.exec("kghostview " + tempFile + "");

          wait_for_Pdf2Show.waitFor();

        } else {

          wait_for_Pdf2Show =
              rt.exec("c:/Program Files/Adobe/Acrobat 5.0/Reader/AcroRd32.exe " + tempFile);

          wait_for_Pdf2Show.waitFor();
        }

      } catch (java.lang.InterruptedException intrExec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), intrExec.getMessage());
      }

    } catch (java.io.IOException IOexec) {

      javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), IOexec.getMessage());
    }
  }
  public void generatePdf() {

    java.lang.Process wait_for_Pdf2Show;

    java.util.Calendar cal = java.util.Calendar.getInstance();

    java.util.Date dateStampPdf = cal.getTime();

    java.lang.String pdfDateStamp = dateStampPdf.toString();

    try {

      java.io.File tempFile =
          java.io.File.createTempFile("REP" + this.getDateLable() + "_", ".pdf");

      tempFile.deleteOnExit();

      java.lang.Runtime rt = java.lang.Runtime.getRuntime();

      java.lang.String debitTotal = null;

      java.lang.String creditTotal = null;

      //            com.lowagie.text.Document docPdf = new com.lowagie.text.Document();
      com.lowagie.text.Document docPdf = new com.lowagie.text.Document();

      try {

        try {

          com.lowagie.text.pdf.PdfWriter.getInstance(
              docPdf, new java.io.FileOutputStream(tempFile));

          double osBalancebf = 0.00;
          double osBalance = 0.00;
          double osBalanceb1 = 0.00;

          String compName = null;
          String date = null;

          docPdf.open();

          try {

            java.util.Calendar calendar = java.util.Calendar.getInstance();

            long dateNow = calendar.getTimeInMillis();

            java.sql.Date datenowSql = new java.sql.Date(dateNow);

            System.out.println(datenowSql.toString());

            //  java.lang.Object listofStaffNos[] = this.getListofStaffNos();

            com.lowagie.text.pdf.PdfPTable table1 = new com.lowagie.text.pdf.PdfPTable(7);
            //  com.lowagie.text.Table table = new com.lowagie.text.Table(7);

            // table.endHeaders();

            int headerwidths[] = {15, 15, 30, 15, 15, 15, 15};

            table1.setWidths(headerwidths);
            //  if (docPdf.getPageNumber() > 1) {
            //  table1.setHeaderRows(4);
            //  }
            table1.setWidthPercentage((100));

            table1.getDefaultCell().setBorder(Rectangle.BOTTOM);

            table1.getDefaultCell().setColspan(7);

            Phrase phrase = new Phrase();

            //  table.addCell(phrase);

            table1.getDefaultCell().setColspan(1);
            table1.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
            table1.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

            try {
              java.sql.Statement st3 = connectDB.createStatement();
              java.sql.ResultSet rset3 = st3.executeQuery("select header_name from pb_header");
              while (rset3.next()) table1.getDefaultCell().setColspan(7);

              table1.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
              phrase = new Phrase(rset3.getObject(1).toString(), pFontHeader1);
              table1.addCell(phrase);

            } catch (java.sql.SQLException SqlExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }
            docPdf.add(table1);
          } catch (com.lowagie.text.BadElementException BadElExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

          com.lowagie.text.HeaderFooter footer =
              new com.lowagie.text.HeaderFooter(
                  new Phrase("Debtors Statement - Page: ", pFontHeader),
                  true); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 12,
                         // Font.BOLDITALIC,java.awt.Color.blue));

          docPdf.setFooter(footer);

          try {

            java.util.Calendar calendar = java.util.Calendar.getInstance();

            long dateNow = calendar.getTimeInMillis();

            java.sql.Date datenowSql = new java.sql.Date(dateNow);

            System.out.println(datenowSql.toString());

            com.lowagie.text.pdf.PdfPTable table = new com.lowagie.text.pdf.PdfPTable(6);

            int headerwidths[] = {10, 30, 15, 15, 15, 20};

            table.setWidths(headerwidths);

            table.setWidthPercentage((100));

            table.setHeaderRows(2);

            table.getDefaultCell().setBorder(Rectangle.BOTTOM);

            Phrase phrase = new Phrase();
            //  table.getDefaultCell().setColspan(8);

            table.getDefaultCell().setColspan(4);

            try {
              java.text.DateFormat dateFormat =
                  java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM); // MEDIUM);

              java.util.Date endDate1 =
                  dateFormat.parse(endDate.toLocaleString()); // dateInstance.toLocaleString());
              java.util.Date endDate11 =
                  dateFormat.parse(beginDate.toLocaleString()); // dateInstance.toLocaleString());

              System.out.println("" + endDate1);
              //  phrase = new Phrase(bank +" Report: " +dateFormat.format(formattedDate),
              // pFontHeader);

              //  table.addCell(phrase);

              phrase =
                  new Phrase(
                      Debtor
                          + " Period : "
                          + dateFormat.format(endDate11)
                          + " - "
                          + dateFormat.format(endDate1),
                      pFontHeader1);

              table.addCell(phrase);
            } catch (java.text.ParseException psExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), psExec.getMessage());
            }
            // table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
            // phrase = new Phrase("Period : '"+beginDate+"' - '"+endDate+"'", pFontHeader1);
            // table.addCell(phrase);

            table.getDefaultCell().setColspan(2);

            table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
            phrase = new Phrase("Date " + datenowSql, pFontHeader1);
            table.addCell(phrase);

            table.getDefaultCell().setColspan(3);
            table.getDefaultCell().setColspan(1);

            //    table.getDefaultCell().setBackgroundColor(java.awt.Color.LIGHT_GRAY);

            phrase = new Phrase("Acc No", pFontHeader);
            table.addCell(phrase);

            phrase = new Phrase("Scheme", pFontHeader);
            table.addCell(phrase);

            table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
            phrase = new Phrase("Debit", pFontHeader);
            table.addCell(phrase);

            phrase = new Phrase("Credit", pFontHeader);
            table.addCell(phrase);

            phrase = new Phrase("Balance ", pFontHeader);
            table.addCell(phrase);

            phrase = new Phrase("Running Total", pFontHeader);
            table.addCell(phrase);

            table.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
            // table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

            try {

              java.sql.Statement st = connectDB.createStatement();
              java.sql.Statement st1 = connectDB.createStatement();

              java.sql.Statement st2 = connectDB.createStatement();

              java.sql.ResultSet rset =
                  st.executeQuery(
                      "select db.account_no,db.payee,sum(db.debit),sum(credit),SUM(db.balance) from ac_debtors db WHERE db.date BETWEEN '"
                          + beginDate
                          + "' AND '"
                          + endDate
                          + "' AND db.dealer ILIKE '"
                          + Debtor
                          + "' GROUP BY db.payee,db.account_no ORDER BY db.payee");
              java.sql.ResultSet rset1 =
                  st1.executeQuery(
                      "select SUM(db.balance) from ac_debtors db WHERE db.date < '"
                          + beginDate
                          + "' AND db.dealer ILIKE '"
                          + Debtor
                          + "' GROUP BY db.account_no");

              while (rset1.next()) {
                bfwd = rset1.getDouble(1);
                bfcd = bfcd + rset1.getDouble(1);
              }
              while (rset.next()) {

                table.getDefaultCell().setColspan(1);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(dbObject.getDBObject(rset.getObject(1), "-"), pFontHeader);

                table.addCell(phrase);
                table.getDefaultCell().setColspan(1);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(dbObject.getDBObject(rset.getObject(2), "-"), pFontHeader);

                table.addCell(phrase);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset.getString(3)),
                        pFontHeader1);
                osBalance = osBalance + rset.getDouble(3);
                table.addCell(phrase);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset.getString(4)),
                        pFontHeader1);
                osBalanceb1 = osBalanceb1 + rset.getDouble(4);
                table.addCell(phrase);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset.getString(5)),
                        pFontHeader);
                osBalancebf = osBalancebf + rset.getDouble(5);
                // osBalancebf = osBalance1;
                table.addCell(phrase);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(java.lang.String.valueOf(osBalancebf)),
                        pFontHeader);

                table.addCell(phrase);
              }
              // }
              table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

              table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);

              //   while (rsetTotals.next()) {

              table.getDefaultCell().setColspan(2);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Total", pFontHeader);

              table.addCell(phrase);

              table.getDefaultCell().setColspan(1);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(osBalance)),
                      pFontHeader);

              table.addCell(phrase);

              // table.getDefaultCell().setColspan(2);

              // table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(osBalanceb1)),
                      pFontHeader);

              table.addCell(phrase);

              table.getDefaultCell().setColspan(2);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(osBalancebf)),
                      pFontHeader);

              table.addCell(phrase);

              docPdf.add(table);

            } catch (java.sql.SQLException SqlExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }

          } catch (com.lowagie.text.BadElementException BadElExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

        } catch (java.io.FileNotFoundException fnfExec) {

          javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), fnfExec.getMessage());
        }
      } catch (com.lowagie.text.DocumentException lwDocexec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), lwDocexec.getMessage());
      }

      docPdf.close();

      try {

        if (System.getProperty("os.name").equalsIgnoreCase("Linux")) {

          System.out.println(tempFile);

          //  wait_for_Pdf2Show = rt.exec("kghostview "+tempFile+"");

          wait_for_Pdf2Show = rt.exec("kghostview " + tempFile + "");

          wait_for_Pdf2Show.waitFor();

        } else {

          wait_for_Pdf2Show =
              rt.exec("c:/Program Files/Adobe/Acrobat 5.0/Reader/AcroRd32.exe " + tempFile);

          wait_for_Pdf2Show.waitFor();
        }

      } catch (java.lang.InterruptedException intrExec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), intrExec.getMessage());
      }

    } catch (java.io.IOException IOexec) {

      javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), IOexec.getMessage());
    }
  }
  public void generatePdf() {

    java.lang.Object[][] ageingDates = ageingSeries.getAgeingDateSeries();

    ageingDates = ageingSeries.getAgeingDateSeries();

    double columnTotals[] = new double[ageingDates.length];

    java.lang.Process wait_for_Pdf2Show;

    java.util.Calendar cal = java.util.Calendar.getInstance();

    java.util.Date dateStampPdf = cal.getTime();

    java.lang.String pdfDateStamp = dateStampPdf.toString();

    try {

      java.io.File tempFile =
          java.io.File.createTempFile("REP" + this.getDateLable() + "_", ".pdf");

      tempFile.deleteOnExit();

      java.lang.Runtime rt = java.lang.Runtime.getRuntime();

      java.lang.String debitTotal = null;

      java.lang.String creditTotal = null;

      //  com.lowagie.text.Document docPdf = new com.lowagie.text.Document();
      com.lowagie.text.Document docPdf = new com.lowagie.text.Document(PageSize.A4.rotate());

      try {

        try {

          com.lowagie.text.pdf.PdfWriter.getInstance(
              docPdf, new java.io.FileOutputStream(tempFile));

          String compName = null;
          String date = null;
          try {

            //   java.sql.Connection conDb =
            // java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/medic","postgres","pilsiner");

            java.sql.Statement st3 = connectDB.createStatement();
            java.sql.Statement st4 = connectDB.createStatement();

            java.sql.ResultSet rset2 =
                st3.executeQuery("SELECT hospital_name from pb_hospitalprofile");
            java.sql.ResultSet rset4 = st4.executeQuery("SELECT date('now') as Date");
            while (rset2.next()) compName = rset2.getObject(1).toString();

            while (rset4.next()) date = rset4.getObject(1).toString();

            com.lowagie.text.HeaderFooter headerFoter =
                new com.lowagie.text.HeaderFooter(
                    new Phrase("" + compName + ""),
                    false); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
                            // Font.BOLDITALIC,java.awt.Color.blue)));
            headerFoter.setAlignment(com.lowagie.text.HeaderFooter.ALIGN_CENTER);

            headerFoter.setRight(5);
            docPdf.setHeader(headerFoter);

          } catch (java.sql.SQLException SqlExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), SqlExec.getMessage());
          }

          com.lowagie.text.HeaderFooter footer =
              new com.lowagie.text.HeaderFooter(
                  new Phrase("Extendend Trial Balance  - Page: ", pFontHeader),
                  true); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 12,
                         // Font.BOLDITALIC,java.awt.Color.blue));

          docPdf.setFooter(footer);

          docPdf.open();

          docPdf.open();

          try {

            com.lowagie.text.pdf.PdfPTable table = new com.lowagie.text.pdf.PdfPTable(8);

            int headerwidths[] = {8, 20, 12, 12, 12, 12, 12, 12};

            table.setWidths(headerwidths);

            table.setWidthPercentage((100));

            table.setHeaderRows(3);

            table.getDefaultCell().setBorder(Rectangle.BOTTOM);

            Phrase phrase = new Phrase("", pFontHeader);

            try {

              double GrandTotal = 0.00;
              double Over120Total = 0.00;

              java.text.DateFormat dateFormat =
                  java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM); // MEDIUM);

              java.util.Date endDate1 =
                  dateFormat.parse(endDate.toLocaleString()); // dateInstance.toLocaleString());
              java.util.Date endDate11 =
                  dateFormat.parse(beginDate.toLocaleString()); // dateInstance.toLocaleString());

              System.out.println("" + endDate1);
              //  phrase = new Phrase(bank +" Report: " +dateFormat.format(formattedDate),
              // pFontHeader);

              //  table.addCell(phrase);
              table.getDefaultCell().setColspan(5);

              phrase =
                  new Phrase(
                      "Extended Trial Balance : As at " + dateFormat.format(endDate1), pFontHeader);

              table.addCell(phrase);
              table.getDefaultCell().setColspan(3);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              phrase = new Phrase("Printed On  :" + date, pFontHeader);

              table.addCell(phrase);
            } catch (java.text.ParseException psExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), psExec.getMessage());
            }
            // Phrase phrase = new Phrase("Patients List As At:" +endDate, pFontHeader);

            // table.addCell(phrase);
            try {
              java.text.SimpleDateFormat dateFormat =
                  new java.text.SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
              com.afrisoftech.lib.DateFormatter dateFormatter =
                  new com.afrisoftech.lib.DateFormatter(
                      dateFormat.parse(endDate.toString().trim()), "MMMM yyyy");

              java.lang.String monthString = dateFormatter.getDateString();

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);

              table
                  .getDefaultCell()
                  .setBorder(Rectangle.BOTTOM | Rectangle.TOP | Rectangle.LEFT | Rectangle.RIGHT);

              table.getDefaultCell().setColspan(2);

              phrase = new Phrase("  ", pFontHeader);
              // table.addCell(phrase);

              phrase = new Phrase("  ", pFontHeader);
              table.addCell(phrase);

              table.getDefaultCell().setColspan(2);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
              phrase = new Phrase("Prior Balances", pFontHeader);
              table.addCell(phrase);

              phrase = new Phrase(monthString + " Movement", pFontHeader);
              table.addCell(phrase);

              phrase = new Phrase("Ending Balance", pFontHeader);
              table.addCell(phrase);

              table.getDefaultCell().setColspan(1);

              phrase = new Phrase("GL. Code", pFontHeader);
              table.addCell(phrase);

              phrase = new Phrase("Description", pFontHeader);
              table.addCell(phrase);

              //  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
              phrase = new Phrase("Debit", pFontHeader);
              table.addCell(phrase);

              phrase = new Phrase("Credit", pFontHeader);
              table.addCell(phrase);

              phrase = new Phrase("Debit", pFontHeader);
              table.addCell(phrase);

              phrase = new Phrase("Credit", pFontHeader);
              table.addCell(phrase);

              phrase = new Phrase("Debit", pFontHeader);
              table.addCell(phrase);

              phrase = new Phrase("Credit", pFontHeader);
              table.addCell(phrase);

            } catch (java.text.ParseException prs) {
              prs.printStackTrace();
            }
            table.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
            // table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

            java.lang.Object[] listofAct = this.getListofActivities();

            //   for (int j = 0; j < listofStaffNos.length; j++) {
            try {

              int name = 0;
              double TurnOver = 0.00;
              double Over120 = 0.00;
              double TotalCount = 0.00;
              double current = 0.00;

              for (int i = 0; i < listofAct.length; i++) {

                java.sql.Statement stmta1 = connectDB.createStatement();
                java.sql.PreparedStatement pSeta1 =
                    connectDB.prepareStatement(
                        "SELECT count(code) FROM ac_trial_balance where  code = '"
                            + listofAct[i]
                            + "'");
                java.sql.ResultSet rSeta1 = pSeta1.executeQuery();
                while (rSeta1.next()) {
                  name = rSeta1.getInt(1);
                }

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                if (name > 0) {

                  java.sql.PreparedStatement pset22 =
                      connectDB.prepareStatement(
                          "select distinct code,initcap(activity) from pb_activity WHERE code = ? GROUP BY code,activity"); // < '"+endDate+"'::date and date > '"+endDate+"'::date - 30 group by dealer");

                  pset22.setString(1, listofAct[i].toString());

                  java.sql.ResultSet rset22 = pset22.executeQuery();
                  while (rset22.next()) {

                    table.getDefaultCell().setColspan(1);
                    //  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
                    phrase =
                        new Phrase(
                            dbObject.getDBObject(rset22.getObject(1).toString(), "-"),
                            pFontHeader1);
                    table.addCell(phrase);

                    phrase =
                        new Phrase(
                            dbObject.getDBObject(rset22.getObject(2).toString(), "-"),
                            pFontHeader1);
                    table.addCell(phrase);

                    table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                    // for (int t = ageingDates.length - 1; t >= 0; t--) {
                    double amount1 = 0.00;
                    double amounta = 0.00;
                    double amount1a = 0.00;

                    for (int t = 0; t < ageingDates.length; t++) {
                      double amount = 0.00;
                      double amount1b = 0.00;

                      System.out.println("Checking dates : " + ageingDates[t][1].toString());

                      java.sql.Statement st01 = connectDB.createStatement();
                      // java.sql.PreparedStatement pset = connectDB.prepareStatement("select
                      // sum(mtd_debit-mtd_credit) from ac_trial_balance WHERE code = ?  AND date
                      // between '"+ageingDates[t][0]+"' AND '"+ageingDates[t][1]+"'");
                      java.sql.PreparedStatement pset =
                          connectDB.prepareStatement(
                              "select sum(mtd_debit-mtd_credit) from ac_trial_balance WHERE code = ?  AND date <= '"
                                  + ageingDates[t][1]
                                  + "'");

                      pset.setString(1, listofAct[i].toString().toUpperCase());

                      java.sql.ResultSet rset = pset.executeQuery();

                      while (rset.next()) {
                        amount = rset.getDouble(1);
                        columnTotals[t] = columnTotals[t] + amount;
                      }

                      if (t == 0) {
                        if (amount > 0) {
                          phrase =
                              new Phrase(
                                  new com.afrisoftech.sys.Format2Currency()
                                      .Format2Currency(java.lang.String.valueOf(amount)),
                                  pFontHeader1);
                          table.addCell(phrase);
                          phrase =
                              new Phrase(
                                  new com.afrisoftech.sys.Format2Currency()
                                      .Format2Currency(java.lang.String.valueOf(0.00)),
                                  pFontHeader1);
                          table.addCell(phrase);
                          totalDebits = totalDebits + amount;
                          amount1 = amount;
                        } else {
                          phrase =
                              new Phrase(
                                  new com.afrisoftech.sys.Format2Currency()
                                      .Format2Currency(java.lang.String.valueOf(0.00)),
                                  pFontHeader1);
                          table.addCell(phrase);
                          phrase =
                              new Phrase(
                                  new com.afrisoftech.sys.Format2Currency()
                                      .Format2Currency(java.lang.String.valueOf(amount * -1)),
                                  pFontHeader1);
                          table.addCell(phrase);
                          totalCredits = totalCredits + (amount * -1);
                          amount1 = amount;
                        }
                      } else {

                        java.sql.PreparedStatement pset1 =
                            connectDB.prepareStatement(
                                "select sum(mtd_debit),sum(mtd_credit) from ac_trial_balance WHERE code = ?  AND date between '"
                                    + ageingDates[t][0]
                                    + "' AND '"
                                    + ageingDates[t][1]
                                    + "'");

                        pset1.setString(1, listofAct[i].toString().toUpperCase());

                        java.sql.ResultSet rset1 = pset1.executeQuery();

                        while (rset1.next()) {
                          amount = rset1.getDouble(1);
                          amount1b = rset1.getDouble(2);
                          columnTotals[t] = columnTotals[t] + amount;
                        }

                        // if (amount > 0){
                        phrase =
                            new Phrase(
                                new com.afrisoftech.sys.Format2Currency()
                                    .Format2Currency(java.lang.String.valueOf(amount)),
                                pFontHeader1);
                        table.addCell(phrase);
                        phrase =
                            new Phrase(
                                new com.afrisoftech.sys.Format2Currency()
                                    .Format2Currency(java.lang.String.valueOf(amount1b)),
                                pFontHeader1);
                        table.addCell(phrase);
                        totalDebits1 = totalDebits1 + amount;
                        totalCredits1 = totalCredits1 + amount1b;
                        amounta = amount - amount1b;
                        /*  }else{
                            phrase = new Phrase(new com.afrisoftech.sys.Format2Currency().Format2Currency(java.lang.String.valueOf(0.00)),pFontHeader1);
                            table.addCell(phrase);
                            phrase = new Phrase(new com.afrisoftech.sys.Format2Currency().Format2Currency(java.lang.String.valueOf(amount*-1)),pFontHeader1);
                            table.addCell(phrase);
                            totalCredits1 = totalCredits1 + (amount*-1);
                            amounta = amount;
                        }*/
                      }
                    }

                    balance = amounta + amount1;

                    table.getDefaultCell().setColspan(1);
                    table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                    if (balance > 0) {
                      phrase =
                          new Phrase(
                              new com.afrisoftech.sys.Format2Currency()
                                  .Format2Currency(java.lang.String.valueOf(balance)),
                              pFontHeader1);
                      table.addCell(phrase);
                      phrase =
                          new Phrase(
                              new com.afrisoftech.sys.Format2Currency()
                                  .Format2Currency(java.lang.String.valueOf(0.00)),
                              pFontHeader1);
                      table.addCell(phrase);
                      totalDebits11 = totalDebits11 + balance;
                    } else {
                      phrase =
                          new Phrase(
                              new com.afrisoftech.sys.Format2Currency()
                                  .Format2Currency(java.lang.String.valueOf(0.00)),
                              pFontHeader1);
                      table.addCell(phrase);
                      phrase =
                          new Phrase(
                              new com.afrisoftech.sys.Format2Currency()
                                  .Format2Currency(java.lang.String.valueOf(balance * -1)),
                              pFontHeader1);
                      table.addCell(phrase);
                      totalCredits11 = totalCredits11 + balance;
                    }
                  }
                }
              }

              // table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

              // table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);

              // while (rsetTotals.next()) {

              table.getDefaultCell().setColspan(2);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Total", pFontHeader);

              table.addCell(phrase);

              table.getDefaultCell().setColspan(1);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              // for (int x = ageingDates.length - 1; x >= 0; x--) {

              for (int t = 0; t < ageingDates.length; t++) {
                if (t == 0) {
                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(java.lang.String.valueOf(totalDebits)),
                          pFontHeader);

                  table.addCell(phrase);

                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(java.lang.String.valueOf(totalCredits)),
                          pFontHeader);

                  table.addCell(phrase);
                } else {

                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(java.lang.String.valueOf(totalDebits1)),
                          pFontHeader);

                  table.addCell(phrase);

                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(java.lang.String.valueOf(totalCredits1)),
                          pFontHeader);

                  table.addCell(phrase);

                  // totalDebits = columnTotals[x] + columnTotals1[x];
                }
              }
              /*  phrase = new Phrase(new com.afrisoftech.sys.Format2Currency().Format2Currency(java.lang.String.valueOf(totalDebits1+totalDebits-)), pFontHeader);

              table.addCell(phrase);
              phrase = new Phrase(new com.afrisoftech.sys.Format2Currency().Format2Currency(java.lang.String.valueOf(totalCredits1+totalCredits)), pFontHeader);

              table.addCell(phrase);*/

              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(totalDebits11)),
                      pFontHeader);

              table.addCell(phrase);
              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(totalCredits11 * -1)),
                      pFontHeader);

              table.addCell(phrase);

              docPdf.add(table);

            } catch (java.sql.SQLException SqlExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }

          } catch (com.lowagie.text.BadElementException BadElExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

        } catch (java.io.FileNotFoundException fnfExec) {

          javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), fnfExec.getMessage());
        }
      } catch (com.lowagie.text.DocumentException lwDocexec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), lwDocexec.getMessage());
      }

      docPdf.close();

      try {

        if (System.getProperty("os.name").equalsIgnoreCase("Linux")) {

          System.out.println(tempFile);

          wait_for_Pdf2Show = rt.exec("xpdf " + tempFile + "");

          wait_for_Pdf2Show.waitFor();

        } else {

          wait_for_Pdf2Show =
              rt.exec("c:/Program Files/Adobe/Acrobat 5.0/Reader/AcroRd32.exe " + tempFile);

          wait_for_Pdf2Show.waitFor();
        }

      } catch (java.lang.InterruptedException intrExec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), intrExec.getMessage());
      }

    } catch (java.io.IOException IOexec) {

      javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), IOexec.getMessage());
    }
  }
  public void generatePdf() {

    java.lang.Process wait_for_Pdf2Show;

    java.util.Calendar cal = java.util.Calendar.getInstance();

    java.util.Date dateStampPdf = cal.getTime();

    java.lang.String pdfDateStamp = dateStampPdf.toString();

    try {

      java.io.File tempFile =
          java.io.File.createTempFile("REP" + this.getDateLable() + "_", ".pdf");

      tempFile.deleteOnExit();

      java.lang.Runtime rt = java.lang.Runtime.getRuntime();

      java.lang.String debitTotal = null;

      java.lang.String creditTotal = null;

      com.lowagie.text.Document docPdf = new com.lowagie.text.Document();

      try {

        try {

          com.lowagie.text.pdf.PdfWriter.getInstance(
              docPdf, new java.io.FileOutputStream(tempFile));

          try {

            java.lang.Class.forName("org.postgresql.Driver");

          } catch (java.lang.ClassNotFoundException cnfExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), cnfExec.getMessage());
          }

          double osBalancet = 0.00;
          String compName = null;
          String date = null;
          double osBalancebf = 0.00;
          double price = 0.00;
          double qty = 0.00;
          try {

            //   java.sql.Connection conDb =
            // java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/sako","postgres","pilsiner");

            java.sql.Statement st3 = connectDB.createStatement();
            java.sql.Statement st4 = connectDB.createStatement();

            java.sql.ResultSet rset2 =
                st3.executeQuery("SELECT hospital_name from pb_hospitalprofile");
            java.sql.ResultSet rset4 =
                st4.executeQuery(
                    "SELECT TO_CHAR(current_timestamp(0),'FMDay FMDD/ MM/ YY HH12:MI')");
            while (rset2.next()) {
              compName = rset2.getObject(1).toString();
            }
            while (rset4.next()) {
              date = rset4.getObject(1).toString();
            }
            com.lowagie.text.HeaderFooter headerFoter =
                new com.lowagie.text.HeaderFooter(
                    new Phrase("" + compName + "", pFontHeader),
                    false); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
            // Font.BOLDITALIC,java.awt.Color.blue)));
            headerFoter.setAlignment(com.lowagie.text.HeaderFooter.ALIGN_CENTER);
            headerFoter.setRight(5);
            docPdf.setHeader(headerFoter);

          } catch (java.sql.SQLException SqlExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), SqlExec.getMessage());
          }

          com.lowagie.text.HeaderFooter footer =
              new com.lowagie.text.HeaderFooter(
                  new Phrase("Stocks By Dep. Cost - Page: ", pFontHeader),
                  true); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 12,
          // Font.BOLDITALIC,java.awt.Color.blue));

          docPdf.setFooter(footer);

          docPdf.open();

          try {

            com.lowagie.text.pdf.PdfPTable table = new com.lowagie.text.pdf.PdfPTable(5);

            int headerwidths[] = {25, 10, 10, 15, 10};

            table.setWidths(headerwidths);

            table.setWidthPercentage((100));

            table.setHeaderRows(2);

            table.getDefaultCell().setBorder(Rectangle.BOTTOM);

            table.getDefaultCell().setColspan(5);

            Phrase phrase = new Phrase("", pFontHeader);

            try {
              java.text.DateFormat dateFormat =
                  java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM); // MEDIUM);

              java.util.Date endDate1 =
                  dateFormat.parse(endDate.toLocaleString()); // dateInstance.toLocaleString());
              java.util.Date endDate11 =
                  dateFormat.parse(beginDate.toLocaleString()); // dateInstance.toLocaleString());

              System.out.println("" + endDate1);
              //  phrase = new Phrase(bank +" Report: " +dateFormat.format(formattedDate),
              // pFontHeader);

              //  table.addCell(phrase);
              table.getDefaultCell().setColspan(3);

              phrase =
                  new Phrase(
                      "Items cost Report "
                          + StoreName
                          + ":      Period : From "
                          + dateFormat.format(endDate11)
                          + " To "
                          + dateFormat.format(endDate1),
                      pFontHeader);

              table.addCell(phrase);
              table.getDefaultCell().setColspan(2);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              phrase = new Phrase("Printed On  : " + date, pFontHeader);

              table.addCell(phrase);
            } catch (java.text.ParseException psExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), psExec.getMessage());
            }
            // Phrase phrase = new Phrase("Patients List As At:" +endDate, pFontHeader);

            // table.addCell(phrase);
            table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);

            table.getDefaultCell().setColspan(1);

            //    table.getDefaultCell().setBackgroundColor(java.awt.Color.LIGHT_GRAY);
            table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
            phrase = new Phrase("ITEM", pFontHeader);
            table.addCell(phrase);
            table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

            phrase = new Phrase("Units", pFontHeader);
            table.addCell(phrase);

            phrase = new Phrase("Bp", pFontHeader);
            table.addCell(phrase);
            phrase = new Phrase("qty", pFontHeader);
            table.addCell(phrase);
            phrase = new Phrase("Cost", pFontHeader);
            table.addCell(phrase);

            // table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

            //  phrase = new Phrase("AMOUNT",pFontHeader);
            //  table.addCell(phrase);

            //   phrase = new Phrase("TOTAL KShs",pFontHeader);
            //   table.addCell(phrase);

            //    phrase = new Phrase("PROFIT",pFontHeader);
            //    table.addCell(phrase);
            table.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
            table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

            try {

              java.lang.Object[] listofAct = this.getListofActivities();

              //    java.sql.Connection conDb1 =
              // java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/sako","postgres","pilsiner");

              System.out.println(listofAct.length);

              //  java.sql.Statement pSet1 = connectDB.createStatement();
              java.sql.Statement st = connectDB.createStatement();
              //    java.sql.ResultSet rsetTotals = st.executeQuery("select sum((ph.quantity *
              // st.transfer_price)::numeric(10,2)), sum(amount)::numeric(10,2),sum(amount -
              // ph.quantity * st.transfer_price)::numeric(10,2) from hp_pharmacy ph,st_stock_prices
              // st WHERE st.department = 'Pharmacy' AND ph.date_prescribed BETWEEN '"+beginDate+"'
              // AND '"+endDate+"' AND ph.description = st.product");

              for (int i = 0; i < listofAct.length; i++) {

                System.out.println("item" + listofAct[i]);
                /*  double opStock = 0.00;
                 double supplies = 0.00;
                 double sales = 0.00;
                 double closing = 0.00;
                */

                java.sql.Statement st1 = connectDB.createStatement();
                java.sql.Statement st2 = connectDB.createStatement();
                java.sql.Statement st3 = connectDB.createStatement();
                java.sql.Statement st21 = connectDB.createStatement();
                java.sql.Statement st31 = connectDB.createStatement();
                java.sql.Statement st41 = connectDB.createStatement();
                java.sql.Statement st32 = connectDB.createStatement();
                java.sql.Statement st2e = connectDB.createStatement();

                java.sql.ResultSet rset =
                    st21.executeQuery(
                        "select sp.product,sp.units,sp.transfer_price from st_stock_prices sp where sp.product ILIKE '"
                            + listofAct[i]
                            + "%' AND sp.department ILIKE  '"
                            + StoreName
                            + "%'");
                java.sql.ResultSet rset31 =
                    st31.executeQuery(
                        "select SUM(ph.quantity) from hp_pharmacy ph where ph.description ILIKE '"
                            + listofAct[i]
                            + "%' AND ph.main_service ILIKE  '"
                            + StoreName
                            + "%' and ph.date_prescribed BETWEEN '"
                            + beginDate
                            + "' AND '"
                            + endDate
                            + "'");

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                // phrase = new Phrase(dbObject.getDBObject(rset.getObject(1), "-"), pFontHeader1);
                table.getDefaultCell().setColspan(1);

                phrase = new Phrase(listofAct[i].toString().toUpperCase(), pFontHeader1);

                table.addCell(phrase);

                while (rset.next()) {

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase(dbObject.getDBObject(rset.getObject(2), "-"), pFontHeader1);

                  table.addCell(phrase);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(rset.getString(3)),
                          pFontHeader1);
                  price = rset.getDouble(3);
                  table.addCell(phrase);
                }
                while (rset31.next()) {

                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(rset31.getString(1)),
                          pFontHeader1);
                  qty = rset31.getDouble(1);
                  table.addCell(phrase);
                  osBalancebf = qty * price;
                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(java.lang.String.valueOf(osBalancebf)),
                          pFontHeader);
                  osBalancet = osBalancebf + osBalancet;
                  table.addCell(phrase);
                }
              }
              table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

              table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);

              table.getDefaultCell().setColspan(3);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Total", pFontHeader);

              table.addCell(phrase);

              table.getDefaultCell().setColspan(2);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(osBalancet)),
                      pFontHeader);

              table.addCell(phrase);
              docPdf.add(table);

            } catch (java.sql.SQLException SqlExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }

          } catch (com.lowagie.text.BadElementException BadElExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

        } catch (java.io.FileNotFoundException fnfExec) {

          javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), fnfExec.getMessage());
        }
      } catch (com.lowagie.text.DocumentException lwDocexec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), lwDocexec.getMessage());
      }

      try {

        if (System.getProperty("os.name").equalsIgnoreCase("Linux")) {

          System.out.println(tempFile);

          wait_for_Pdf2Show = rt.exec("kghostview " + tempFile + "");

          wait_for_Pdf2Show.waitFor();

        } else {

          wait_for_Pdf2Show =
              rt.exec("c:/Program Files/Adobe/Acrobat 5.0/Reader/AcroRd32.exe " + tempFile);

          wait_for_Pdf2Show.waitFor();
        }

      } catch (java.lang.InterruptedException intrExec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), intrExec.getMessage());
      }

    } catch (java.io.IOException IOexec) {

      javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), IOexec.getMessage());
    }
  }
  public void generatePdf() {

    java.lang.Process wait_for_Pdf2Show;

    java.util.Calendar cal = java.util.Calendar.getInstance();

    java.util.Date dateStampPdf = cal.getTime();

    java.lang.String pdfDateStamp = dateStampPdf.toString();

    try {

      java.io.File tempFile =
          java.io.File.createTempFile("REP" + this.getDateLable() + "_", ".pdf");

      tempFile.deleteOnExit();

      java.lang.Runtime rt = java.lang.Runtime.getRuntime();

      java.lang.String debitTotal = null;

      java.lang.String creditTotal = null;

      com.lowagie.text.Document docPdf = new com.lowagie.text.Document();

      try {

        try {

          com.lowagie.text.pdf.PdfWriter.getInstance(
              docPdf, new java.io.FileOutputStream(tempFile));

          /*    try {

                java.lang.Class.forName("org.postgresql.Driver");

            } catch(java.lang.ClassNotFoundException cnfExec) {

                javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), cnfExec.getMessage());

            }
          */

          String compName = null;
          String date = null;
          try {

            java.sql.Statement st3 = connectDB.createStatement();
            java.sql.Statement st4 = connectDB.createStatement();

            java.sql.ResultSet rset2 =
                st3.executeQuery("SELECT hospital_name from pb_hospitalprofile");
            java.sql.ResultSet rset4 = st4.executeQuery("SELECT date('now') as Date");
            while (rset2.next()) compName = rset2.getObject(1).toString();

            while (rset4.next()) date = rset4.getObject(1).toString();

            com.lowagie.text.HeaderFooter headerFoter =
                new com.lowagie.text.HeaderFooter(
                    new Phrase(
                        ""
                            + compName
                            + "                                                        Printed On: "
                            + date
                            + ""),
                    false); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
                            // Font.BOLDITALIC,java.awt.Color.blue)));

            //  com.lowagie.text.HeaderFooter headerFoter = new com.lowagie.text.HeaderFooter(new
            // Phrase(""+compName+""),false);//
            // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
            // Font.BOLDITALIC,java.awt.Color.blue)));
            headerFoter.setRight(5);
            docPdf.setHeader(headerFoter);

          } catch (java.sql.SQLException SqlExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), SqlExec.getMessage());
          }

          com.lowagie.text.HeaderFooter footer =
              new com.lowagie.text.HeaderFooter(
                  new Phrase("Dept - Page: "),
                  true); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 12,
                         // Font.BOLDITALIC,java.awt.Color.blue));

          docPdf.setFooter(footer);

          docPdf.open();

          try {

            com.lowagie.text.pdf.PdfPTable table = new com.lowagie.text.pdf.PdfPTable(4);

            int headerwidths[] = {
              20, 10, 40, 30,
            };

            table.setWidths(headerwidths);

            table.setWidthPercentage((100));

            table.setHeaderRows(2);

            table.getDefaultCell().setBorder(Rectangle.BOTTOM);

            table.getDefaultCell().setColspan(4);

            Phrase phrase;
            try {
              java.text.DateFormat dateFormat =
                  java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG); // MEDIUM);

              java.util.Date formattedDate =
                  dateFormat.parse(endDate); // dateInstance.toLocaleString());
              java.util.Date formattedDate1 = dateFormat.parse(beginDate);

              phrase =
                  new Phrase(
                      bank
                          + "    Gross Pay "
                          + dateFormat.format(formattedDate)
                          + " --"
                          + dateFormat.format(formattedDate),
                      pFontHeader);

              table.addCell(phrase);
            } catch (java.text.ParseException psExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), psExec.getMessage());
            }

            table.getDefaultCell().setColspan(1);

            //    table.getDefaultCell().setBackgroundColor(java.awt.Color.LIGHT_GRAY);

            phrase = new Phrase("No", pFontHeader);
            table.addCell(phrase);
            phrase = new Phrase("Staff No", pFontHeader);
            table.addCell(phrase);

            phrase = new Phrase("Staff Name", pFontHeader);
            table.addCell(phrase);
            table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
            phrase = new Phrase("Gross Pay", pFontHeader);
            table.addCell(phrase);

            // table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

            table.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
            // table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);
            double balance = 0.00;

            try {

              java.sql.Statement st = connectDB.createStatement();

              java.sql.Statement st2 = connectDB.createStatement();
              //  java.sql.ResultSet rset11 = st22.executeQuery("SELECT sum(total_gross_pay) from
              // tax_card WHERE AND date BETWEEN '"+beginDate+"' AND '"+endDate+"'");

              java.sql.ResultSet rset =
                  st.executeQuery(
                      "select tc.staff_no,upper(mf.first_name||' '||mf.middle_name),sum(tc.amount) from posting tc,master_file mf where tc.staff_no =mf.employee_no AND tc.date BETWEEN '"
                          + beginDate
                          + "' AND '"
                          + endDate
                          + "' AND mf.department ILIKE '"
                          + bank
                          + "%' and tc.allowance_deduction ilike 'Earn%' group by tc.staff_no,mf.first_name||' '||mf.middle_name,mf.department order by tc.staff_no"); // tn,debit_note db WHERE tn.policy_no != '' and tn.policy_no = db.policy_no GROUP BY tn.policy_no,db.policy_class");

              // java.sql.ResultSet rset1 = st2.executeQuery("SELECT sum(tc.total_gross_pay) from
              // tax_card tc,master_file mf where tc.staff_no = mf.employee_no AND tc.date BETWEEN
              // '"+beginDate+"' AND '"+endDate+"' AND mf.department ILIKE '"+bank+"%'");
              // java.sql.ResultSet rset3 = st3.executeQuery("select sum(amount) from posting WHERE
              // date BETWEEN '"+beginDate+"' AND '"+endDate+"' AND description = '"+bank+"'");//
              // tn,debit_note db WHERE tn.policy_no != '' and tn.policy_no = db.policy_no GROUP BY
              // tn.policy_no,db.policy_class");

              while (rset.next()) {
                table.getDefaultCell().setColspan(1);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);

                numberSeq = numberSeq + 1;

                phrase = new Phrase("" + numberSeq + "   ", pFontHeader1);
                table.addCell(phrase);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(rset.getObject(1).toString(), pFontHeader1);

                table.addCell(phrase);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(rset.getObject(2).toString(), pFontHeader1);

                table.addCell(phrase);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset.getString(3)),
                        pFontHeader1);
                balance = balance + rset.getDouble(3);

                table.addCell(phrase);
              }

              table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);
              table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);
              // while (rset1.next()){
              table.getDefaultCell().setColspan(3);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Totals", pFontHeader);

              table.addCell(phrase);

              table.getDefaultCell().setColspan(1);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(balance)),
                      pFontHeader);

              table.addCell(phrase);

              docPdf.add(table);

            } catch (java.sql.SQLException SqlExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }

          } catch (com.lowagie.text.BadElementException BadElExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

        } catch (java.io.FileNotFoundException fnfExec) {

          javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), fnfExec.getMessage());
        }
      } catch (com.lowagie.text.DocumentException lwDocexec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), lwDocexec.getMessage());
      }

      docPdf.close();

      try {

        if (System.getProperty("os.name").equalsIgnoreCase("Linux")) {

          System.out.println(tempFile);

          wait_for_Pdf2Show = rt.exec("kghostview " + tempFile + "");

          wait_for_Pdf2Show.waitFor();

        } else {

          wait_for_Pdf2Show =
              rt.exec("c:/Program Files/Adobe/Acrobat 5.0/Reader/AcroRd32.exe " + tempFile);

          wait_for_Pdf2Show.waitFor();
        }

      } catch (java.lang.InterruptedException intrExec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), intrExec.getMessage());
      }

    } catch (java.io.IOException IOexec) {

      javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), IOexec.getMessage());
    }
  }
Example #13
0
  public void generatePdf(java.lang.String memNo) {

    java.lang.Process wait_for_Pdf2Show;

    java.util.Calendar cal = java.util.Calendar.getInstance();

    java.util.Date dateStampPdf = cal.getTime();

    java.lang.String pdfDateStamp = dateStampPdf.toString();

    try {

      java.io.File tempFile =
          java.io.File.createTempFile("REP" + this.getDateLable() + "_", ".pdf");

      tempFile.deleteOnExit();

      java.lang.Runtime rt = java.lang.Runtime.getRuntime();

      java.lang.String debitTotal = null;

      java.lang.String creditTotal = null;

      com.lowagie.text.Document docPdf =
          new com.lowagie.text.Document(
              new Rectangle(
                  java.lang.Float.parseFloat(System.getProperty("papersize_width")),
                  java.lang.Float.parseFloat(System.getProperty("papersize_legnth"))),
              java.lang.Float.parseFloat(System.getProperty("receiptPageMargin")),
              java.lang.Float.parseFloat(System.getProperty("receiptPageMargin")),
              java.lang.Float.parseFloat(System.getProperty("receiptPageMargin")),
              java.lang.Float.parseFloat(System.getProperty("receiptPageMargin")));

      try {

        try {

          com.lowagie.text.pdf.PdfWriter.getInstance(
              docPdf, new java.io.FileOutputStream(tempFile));

          String compName = null;
          String date = null;
          /* try {


              java.sql.Statement st3 = connectDB.createStatement();
              java.sql.Statement st4 = connectDB.createStatement();

              java.sql.ResultSet rset2 = st3.executeQuery("SELECT hospital_name from pb_hospitalprofile");
              java.sql.ResultSet rset4 = st4.executeQuery("SELECT date('now') as Date");
              while(rset2.next())
                  compName = rset2.getObject(1).toString();

              while(rset4.next())
                  date = rset4.getObject(1).toString();

              com.lowagie.text.HeaderFooter headerFoter = new com.lowagie.text.HeaderFooter(new Phrase(""+compName+"                                                        Printed On: "+date+""),false);// FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14, Font.BOLDITALIC,java.awt.Color.blue)));

              //  com.lowagie.text.HeaderFooter headerFoter = new com.lowagie.text.HeaderFooter(new Phrase(""+compName+""),false);// FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14, Font.BOLDITALIC,java.awt.Color.blue)));
              headerFoter.setRight(5);
              docPdf.setHeader(headerFoter);

          } catch(java.sql.SQLException SqlExec) {

              javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), SqlExec.getMessage());

          }
          */
          //   com.lowagie.text.HeaderFooter footer = new com.lowagie.text.HeaderFooter(new
          // Phrase("Member Statements - Page: "), true);//
          // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 12,
          // Font.BOLDITALIC,java.awt.Color.blue));

          //   docPdf.setFooter(footer);

          docPdf.open();

          try {

            com.lowagie.text.pdf.PdfPTable table = new com.lowagie.text.pdf.PdfPTable(6);
            //   com.lowagie.text.rtf.RtfTable table = new com.lowagie.text.rtf.RtfTable();
            //  Table table = new Table(6);

            int headerwidths[] = {15, 20, 20, 15, 15, 15};

            //  table.setWidths(headerwidths);

            table.setWidthPercentage((100));

            // table.getDefaultCell().setBorder(Rectangle.BOTTOM);

            //  table.getDefaultCell().setColspan(6);

            Phrase phrase = new Phrase("");

            // table.addCell(phrase);
            table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);
            table.getDefaultCell().setColspan(1);
            table.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
            table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

            try {

              java.sql.Statement st = connectDB.createStatement();
              java.sql.Statement st1 = connectDB.createStatement();
              java.sql.Statement st2 = connectDB.createStatement();
              java.sql.Statement st5 = connectDB.createStatement();
              java.sql.Statement st6 = connectDB.createStatement();

              java.sql.Statement st3 = connectDB.createStatement();
              java.sql.ResultSet rset3 =
                  st3.executeQuery(
                      "select hospital_name,postal_code||' '||box_no||' '||town,main_telno||' '||other_telno,initcap(street),main_faxno,email,website,room_no from pb_hospitalprofile");
              //       java.sql.ResultSet rset1 = st1.executeQuery("select
              // cb.dealer,cb.receipt_no,CURRENT_TIMESTAMP(0),cb.patient_no from ac_cash_collection
              // cb, hp_patient_card pc where cb.patient_no = '"+MNo+"' AND cb.date::date =
              // current_date AND cb.transaction_type = 'Receipts' AND pc.paid = false");

              java.sql.ResultSet rset1 = st1.executeQuery("select CURRENT_TIMESTAMP(0)");
              //     java.sql.ResultSet rsetTotals = st2.executeQuery("select sum(pc.credit) as
              // debit from hp_patient_card pc where pc.patient_no = '"+MNo+"' AND
              // pc.transaction_type = 'Receipts' AND pc.paid = false  AND pc.date::date =
              // current_date");
              //  java.sql.ResultSet rs = st.executeQuery("select cb.receipt_no from
              // ac_cash_collection cb, hp_patient_card pc where cb.patient_no = '"+MNo+"' AND
              // pc.paid = false  AND pc.date::date = current_date");
              java.sql.ResultSet rset5 =
                  st5.executeQuery(
                      "select description,sum(quantity),sum(debit) from ac_cash_collection where patient_no = '"
                          + MNo
                          + "' AND date::date = current_date and receipt_no = '"
                          + Receipt
                          + "' group by description");
              java.sql.ResultSet rset6 =
                  st6.executeQuery(
                      "select distinct user_name,cash_point from ac_cash_collection where patient_no = '"
                          + MNo
                          + "' AND date = current_date");
              System.out.println(MNo);

              while (rset3.next()) {
                table.getDefaultCell().setColspan(6);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
                phrase = new Phrase(rset3.getObject(1).toString(), pFontHeader1);
                table.addCell(phrase);

                table.getDefaultCell().setColspan(6);
                table.getDefaultCell().setBorderColor(java.awt.Color.white);

                table.getDefaultCell().setColspan(2);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase("Address:" + "\t" + rset3.getObject(2).toString(), pFontHeader);
                table.addCell(phrase);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase("Tel:  " + rset3.getObject(3).toString(), pFontHeader);

                table.addCell(phrase);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase("Fax:  " + rset3.getObject(5).toString(), pFontHeader);

                table.addCell(phrase);
                table.getDefaultCell().setBorderColor(java.awt.Color.white);
                table.getDefaultCell().setColspan(3);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase("Email: " + rset3.getObject(6).toString(), pFontHeader);
                table.addCell(phrase);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase("Website: " + rset3.getObject(7).toString(), pFontHeader);

                table.addCell(phrase);
                /// table.addCell("  ");

              }
              //  table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);
              //  table.getDefaultCell().setBorderWidth(Rectangle.TOP);
              while (rset1.next()) table.getDefaultCell().setColspan(2);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              //  phrase = new Phrase("Receipt No. "+ rset1.getString(2), pFontHeader);

              phrase = new Phrase("Receipt No. " + Receipt, pFontHeader);

              table.addCell(phrase);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
              table.getDefaultCell().setColspan(4);
              phrase = new Phrase("RECEIPT ", pFontHeader1);

              table.addCell(phrase);
              table.getDefaultCell().setColspan(2);

              table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              // phrase = new Phrase("Patient No. "+ rset1.getObject(4).toString(), pFontHeader);
              phrase = new Phrase("Patient No. " + MNo, pFontHeader);

              table.addCell(phrase);
              //  phrase = new Phrase("Name "+ rset1.getObject(1).toString(), pFontHeader);
              phrase = new Phrase("Name " + Name, pFontHeader);

              table.addCell(phrase);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Date " + rset1.getObject(1).toString(), pFontHeader);

              table.addCell(phrase);

              table.getDefaultCell().setColspan(3);

              phrase = new Phrase("Description", pFontHeader);
              table.addCell(phrase);
              table.getDefaultCell().setColspan(1);
              phrase = new Phrase("Qty", pFontHeader);
              table.addCell(phrase);
              //  table.addCell("Description");

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
              table.getDefaultCell().setColspan(2);
              phrase = new Phrase("Amount KShs.", pFontHeader);
              table.addCell(phrase);

              while (rset5.next()) {

                table.getDefaultCell().setColspan(3);

                table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(rset5.getObject(1).toString(), pFontHeader);

                table.addCell(phrase);
                table.getDefaultCell().setColspan(1);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase(rset5.getObject(2).toString(), pFontHeader);

                table.addCell(phrase);

                table.getDefaultCell().setColspan(2);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset5.getString(3)),
                        pFontHeader);
                table.addCell(phrase);
              }
              /*              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                           phrase = new Phrase(new com.afrisoftech.sys.Format2Currency().Format2Currency(rset1.getString(5)),pFontHeader);

                           table.addCell(phrase);

                           table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                           phrase = new Phrase(new com.afrisoftech.sys.Format2Currency().Format2Currency(rset1.getString(6)),pFontHeader);

                           table.addCell(phrase);

                       }
              */
              // table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

              // table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);

              //     while (rsetTotals.next()) {

              table.getDefaultCell().setColspan(4);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Total", pFontHeader);

              table.addCell(phrase);

              table.getDefaultCell().setColspan(2);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);

              // phrase = new Phrase(new
              // com.afrisoftech.sys.Format2Currency().Format2Currency(rsetTotals.getString(1)),pFontHeader);

              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency().Format2Currency(Amount),
                      pFontHeader);

              table.addCell(phrase);

              /*   table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
                phrase = new Phrase(new com.afrisoftech.sys.Format2Currency().Format2Currency(rsetTotals.getString(2)),pFontHeader);

                //phrase = new Phrase(" ");

                table.addCell(phrase);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                phrase = new Phrase(new com.afrisoftech.sys.Format2Currency().Format2Currency(rsetTotals.getString(3)),pFontHeader);

                table.addCell(phrase);


              */
              //    }

              while (rset6.next()) table.getDefaultCell().setColspan(3);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Payment Mode : " + Paymode, pFontHeader);

              table.addCell(phrase);

              table.getDefaultCell().setColspan(3);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Served By : " + rset6.getString(1), pFontHeader);
              table.addCell(phrase);
              table.getDefaultCell().setColspan(3);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Cash Point : " + rset6.getString(2).toString(), pFontHeader);
              table.addCell(phrase);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              table.getDefaultCell().setColspan(3);
              phrase = new Phrase("Thank You. We wish You quick recovery", pFontHeader);
              table.addCell(phrase);

              docPdf.add(table);
              //  java.sql.PreparedStatement pstmt4 = connectDB.prepareStatement("UPDATE
              // hp_patient_card SET paid ='true' WHERE patient_no = '"+MNo+"' AND date::date =
              // current_date");
              //  pstmt4.executeUpdate();
            } catch (java.sql.SQLException SqlExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }

            // }

          } catch (com.lowagie.text.BadElementException BadElExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

        } catch (java.io.FileNotFoundException fnfExec) {

          javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), fnfExec.getMessage());
        }
      } catch (com.lowagie.text.DocumentException lwDocexec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), lwDocexec.getMessage());
      }

      try {

        if (System.getProperty("os.name").equalsIgnoreCase("Linux")) {

          System.out.println(tempFile);

          //                    wait_for_Pdf2Show = rt.exec("kghostview "+tempFile+"");
          wait_for_Pdf2Show = rt.exec("kword " + tempFile + "");

          //                    wait_for_Pdf2Show = rt.exec("cat kghostview "+tempFile+"");

          wait_for_Pdf2Show.waitFor();

        } else if (System.getProperty("os.name").equalsIgnoreCase("Windows 98")) {

          wait_for_Pdf2Show = rt.exec("command.exe /C AcroRd32 /p /h " + tempFile);

          wait_for_Pdf2Show.waitFor();
          // wait_for_Pdf2Show = rt.exec("c:/Program Files/Adobe/Acrobat 5.0/Reader/AcroRd32.exe
          // "+tempFile);

          // wait_for_Pdf2Show.waitFor();

        } else {

          wait_for_Pdf2Show = rt.exec("cmd.exe /C AcroRd32 /p /h " + tempFile);

          wait_for_Pdf2Show.waitFor();
          // wait_for_Pdf2Show = rt.exec("c:/Program Files/Adobe/Acrobat 5.0/Reader/AcroRd32.exe
          // "+tempFile);

          // wait_for_Pdf2Show.waitFor();

        }

      } catch (java.lang.InterruptedException intrExec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), intrExec.getMessage());
      }

    } catch (java.io.IOException IOexec) {

      javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), IOexec.getMessage());
    }
  }
  public void generatePdf() {

    java.util.Calendar calLast = java.util.Calendar.getInstance();

    calLast.roll(java.util.Calendar.YEAR, -1);

    dateLastyear = calLast.getTime().toString();

    System.out.println(this.dateLastyear);

    java.lang.Process wait_for_Pdf2Show;

    java.util.Calendar cal = java.util.Calendar.getInstance();

    java.util.Date dateStampPdf = cal.getTime();

    java.lang.String pdfDateStamp = dateStampPdf.toString();

    try {

      java.io.File tempFile =
          java.io.File.createTempFile("REP" + this.getDateLable() + "_", ".pdf");

      tempFile.deleteOnExit();

      java.lang.Runtime rt = java.lang.Runtime.getRuntime();

      java.lang.String debitTotal = null;

      java.lang.String creditTotal = null;

      com.lowagie.text.Document docPdf = new com.lowagie.text.Document();

      try {

        try {

          com.lowagie.text.pdf.PdfWriter.getInstance(
              docPdf, new java.io.FileOutputStream(tempFile));

          /*        try {

                     java.lang.Class.forName("org.postgresql.Driver");

                 } catch(java.lang.ClassNotFoundException cnfExec) {

                     javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), cnfExec.getMessage());

                 }
          */

          String compName = null;
          String date = null;
          try {

            //    java.sql.Connection conDb =
            // java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/sako","postgres","pilsiner");

            java.sql.Statement st3 = connectDB.createStatement();
            java.sql.Statement st4 = connectDB.createStatement();

            java.sql.ResultSet rset2 = st3.executeQuery("SELECT company_name from company_profile");
            java.sql.ResultSet rset4 = st4.executeQuery("SELECT date('now') as Date");
            while (rset2.next()) compName = rset2.getObject(1).toString();

            while (rset4.next()) date = rset4.getObject(1).toString();

            com.lowagie.text.HeaderFooter headerFoter =
                new com.lowagie.text.HeaderFooter(
                    new Phrase(
                        ""
                            + compName
                            + "                                                        Printed On: "
                            + date
                            + ""),
                    false); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
                            // Font.BOLDITALIC,java.awt.Color.blue)));

            //  com.lowagie.text.HeaderFooter headerFoter = new com.lowagie.text.HeaderFooter(new
            // Phrase(""+compName+""),false);//
            // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
            // Font.BOLDITALIC,java.awt.Color.blue)));
            headerFoter.setRight(5);
            docPdf.setHeader(headerFoter);

          } catch (java.sql.SQLException SqlExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), SqlExec.getMessage());
          }

          com.lowagie.text.HeaderFooter footer =
              new com.lowagie.text.HeaderFooter(
                  new Phrase("Balance Sheet - Page: "),
                  true); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 12,
                         // Font.BOLDITALIC,java.awt.Color.blue));

          docPdf.setFooter(footer);

          docPdf.open();

          try {

            com.lowagie.text.pdf.PdfPTable table = new com.lowagie.text.pdf.PdfPTable(3);

            int headerwidths[] = {30, 40, 30};

            table.setWidths(headerwidths);

            table.setWidthPercentage((100));

            table.getDefaultCell().setBorder(Rectangle.BOTTOM);

            table.getDefaultCell().setColspan(3);

            Phrase phrase = new Phrase("Balance Sheet as at : " + beginDate, pFontHeader);

            table.addCell(phrase);

            table.getDefaultCell().setColspan(1);

            table.getDefaultCell().setBackgroundColor(java.awt.Color.LIGHT_GRAY);

            table.addCell("");

            table.addCell("");

            table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

            table.addCell("Amount KShs.");

            table.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
            table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

            try {

              java.lang.Object[] listofAct = this.getListofActivities();

              java.lang.Object[] listofAct1 = this.getListofActivities1();
              java.lang.Object[] listofAct2 = this.getListofActivities2();
              //    java.sql.Connection conDb1 =
              // java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/sako","postgres","pilsiner");

              System.out.println(listofAct.length);
              java.sql.Statement st4 = connectDB.createStatement();
              java.sql.ResultSet rset4 =
                  st4.executeQuery(
                      "SELECT SUM(amount) from assets_view WHERE date BETWEEN '"
                          + beginDate
                          + "' AND '"
                          + endDate
                          + "'");

              for (int i = 0; i < listofAct.length; i++) {

                java.sql.Statement st = connectDB.createStatement();

                java.sql.Statement st2 = connectDB.createStatement();

                java.sql.Statement st3 = connectDB.createStatement();

                java.sql.ResultSet rset3 =
                    st3.executeQuery(
                        "select DISTINCT at.description from accounts_setup at,balance_sheet_view bv,sub_activities sa,activity ac where at.main_code = '"
                            + listofAct[i].toString()
                            + "' and bv.gl_code = ac.code and ac.sub_code = sa.sub_code and sa.main_code = at.main_code and at.class = 'ba'");

                java.sql.ResultSet rset =
                    st.executeQuery(
                        "select bv.gl_code,initcap(bv.description),sum(bv.amount) from balance_sheet_view bv,activity ac,accounts_setup at,sub_activities sa where at.main_code = '"
                            + listofAct[i].toString()
                            + "' and sa.main_code = '"
                            + listofAct[i].toString()
                            + "' and ac.sub_code = sa.sub_code and bv.gl_code = ac.code and at.class = 'ba' AND bv.date BETWEEN '"
                            + beginDate
                            + "' AND '"
                            + endDate
                            + "' group by bv.gl_code,bv.description order by bv.gl_code");

                java.sql.ResultSet rsetTotals =
                    st2.executeQuery(
                        "SELECT SUM(amount) from balance_sheet_view bv,activity ac,sub_activities sa,accounts_setup at where sa.main_code = '"
                            + listofAct[i].toString()
                            + "' and sa.main_code = at.main_code and sa.sub_code = ac.sub_code and bv.gl_code = ac.code and at.class = 'ba' AND bv.date BETWEEN '"
                            + beginDate
                            + "' AND '"
                            + endDate
                            + "' group by sa.main_code");

                // rsetTotals1 = st3.executeQuery("SELECT SUM(debit),SUM(credit) from");
                table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

                table.getDefaultCell().setColspan(3);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                table.addCell("  ");

                table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);

                while (rset3.next()) {

                  table.getDefaultCell().setColspan(3);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase(rset3.getObject(1).toString(), pFontHeader1);

                  table.addCell(phrase);
                }

                while (rset.next()) {

                  table.getDefaultCell().setColspan(1);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase(rset.getObject(1).toString(), pFontHeader1);
                  table.addCell(phrase);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase(rset.getObject(2).toString(), pFontHeader1);

                  table.addCell(phrase);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(rset.getString(3)),
                          pFontHeader1);

                  table.addCell(phrase);
                }

                while (rsetTotals.next()) {

                  table.getDefaultCell().setColspan(2);
                  table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);
                  table.getDefaultCell().setBorder(Rectangle.TOP | Rectangle.BOTTOM);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase("Sub Total", pFontHeader);

                  table.addCell(phrase);

                  table.getDefaultCell().setColspan(1);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(rsetTotals.getString(1)),
                          pFontHeader);

                  table.addCell(phrase);
                }
              }

              table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);
              table.getDefaultCell().setColspan(3);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              table.addCell("  ");

              while (rset4.next()) {

                table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);
                table.getDefaultCell().setBorder(Rectangle.BOTTOM);

                table.getDefaultCell().setColspan(2);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase("Total Assets", pFontHeader);

                table.addCell(phrase);
                table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);
                table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);
                table.getDefaultCell().setBorder(Rectangle.BOTTOM);

                table.getDefaultCell().setColspan(1);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset4.getString(1)),
                        pFontHeader);

                table.addCell(phrase);
              }

              table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);
              java.sql.Statement st5 = connectDB.createStatement();
              java.sql.ResultSet rset5 =
                  st5.executeQuery(
                      "SELECT SUM(amount) from liabilities_view WHERE date BETWEEN '"
                          + beginDate
                          + "' AND '"
                          + endDate
                          + "'");

              for (int i = 0; i < listofAct1.length; i++) {

                java.sql.Statement st6 = connectDB.createStatement();

                java.sql.Statement st7 = connectDB.createStatement();

                java.sql.Statement st8 = connectDB.createStatement();

                java.sql.ResultSet rset8 =
                    st8.executeQuery(
                        "select DISTINCT at.description from accounts_setup at,balance_sheet_view bv,sub_activities sa,activity ac where at.main_code = '"
                            + listofAct1[i].toString()
                            + "' and bv.gl_code = ac.code and ac.sub_code = sa.sub_code and sa.main_code = at.main_code and at.class = 'bl'");

                java.sql.ResultSet rset6 =
                    st6.executeQuery(
                        "select bv.gl_code,bv.description,sum(bv.amount) from balance_sheet_view bv,activity ac,accounts_setup at,sub_activities sa where at.main_code = '"
                            + listofAct1[i].toString()
                            + "' and sa.main_code = '"
                            + listofAct1[i].toString()
                            + "' and ac.sub_code = sa.sub_code and bv.gl_code = ac.code and at.class = 'bl' AND bv.date BETWEEN '"
                            + beginDate
                            + "' AND '"
                            + endDate
                            + "' group by bv.gl_code,bv.description order by bv.gl_code");

                java.sql.ResultSet rset7 =
                    st7.executeQuery(
                        "SELECT SUM(amount) from balance_sheet_view bv,activity ac,sub_activities sa,accounts_setup at where sa.main_code = '"
                            + listofAct1[i].toString()
                            + "' and sa.main_code = at.main_code and sa.sub_code = ac.sub_code and bv.gl_code = ac.code and at.class = 'bl' AND bv.date BETWEEN '"
                            + beginDate
                            + "' AND '"
                            + endDate
                            + "' group by sa.main_code");

                // rsetTotals1 = st3.executeQuery("SELECT SUM(debit),SUM(credit) from");
                table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

                table.getDefaultCell().setColspan(3);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                table.addCell("  ");

                table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);

                while (rset8.next()) {
                  table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);
                  table.getDefaultCell().setColspan(3);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase(rset8.getObject(1).toString(), pFontHeader1);

                  table.addCell(phrase);
                }

                while (rset6.next()) {

                  table.getDefaultCell().setColspan(1);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase(rset6.getObject(1).toString(), pFontHeader);
                  table.addCell(phrase);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase(rset6.getObject(2).toString(), pFontHeader1);

                  table.addCell(phrase);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(rset6.getString(3)),
                          pFontHeader1);

                  table.addCell(phrase);
                }

                while (rset7.next()) {

                  table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);
                  table.getDefaultCell().setColspan(2);
                  table.getDefaultCell().setBorder(Rectangle.TOP);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase("Sub Total", pFontHeader);

                  table.addCell(phrase);

                  table.getDefaultCell().setColspan(1);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(rset7.getString(1)),
                          pFontHeader);

                  table.addCell(phrase);
                }

                table.getDefaultCell().setColspan(3);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                table.addCell("  ");
                table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);
              }
              while (rset5.next()) {

                table.getDefaultCell().setColspan(2);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                phrase = new Phrase("Total Liabilities", pFontHeader);
                table.getDefaultCell().setBorder(Rectangle.BOTTOM);
                table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

                table.addCell(phrase);

                table.getDefaultCell().setColspan(1);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset5.getString(1)),
                        pFontHeader);

                table.addCell(phrase);
              }
              float num_a = 0;
              float num_b = 0;
              float num_c = 0;
              float totals = 0;
              int j = 0;
              java.sql.Statement st13 = connectDB.createStatement();
              java.sql.ResultSet rset13 =
                  st13.executeQuery(
                      " SELECT description,sub_code,(select sum(amount) from pl_account_view WHERE date BETWEEN '"
                          + beginDate
                          + "' AND '"
                          + endDate
                          + "') from sub_activities where identifier Like 'pl%'");
              java.sql.Statement st9 = connectDB.createStatement();
              java.sql.ResultSet rset9 =
                  st9.executeQuery(
                      "SELECT SUM(amount) from equity_view WHERE date BETWEEN '"
                          + beginDate
                          + "' AND '"
                          + endDate
                          + "'");

              java.sql.Statement st14 = connectDB.createStatement();
              java.sql.ResultSet rset14 =
                  st14.executeQuery(
                      "select(select sum(amount) from pl_account_view WHERE date BETWEEN '"
                          + beginDate
                          + "' AND '"
                          + endDate
                          + "') + (select sum(amount) from equity_view WHERE date BETWEEN '"
                          + beginDate
                          + "' AND '"
                          + endDate
                          + "')");
              java.sql.Statement st16 = connectDB.createStatement();
              java.sql.ResultSet rset16 =
                  st16.executeQuery(
                      "select(select sum(amount) from pl_account_view WHERE date BETWEEN '"
                          + beginDate
                          + "' AND '"
                          + endDate
                          + "') + (select sum(amount) from equity_view WHERE date BETWEEN '"
                          + beginDate
                          + "' AND '"
                          + endDate
                          + "') + (select sum(amount) from liabilities_view WHERE date BETWEEN '"
                          + beginDate
                          + "' AND '"
                          + endDate
                          + "')");

              for (int i = 0; i < listofAct2.length; i++) {

                java.sql.Statement st10 = connectDB.createStatement();

                java.sql.Statement st8 = connectDB.createStatement();

                java.sql.Statement st11 = connectDB.createStatement();

                //   java.sql.ResultSet rset10 = st10.executeQuery("select * from equity_view");

                java.sql.ResultSet rset11 =
                    st11.executeQuery(
                        "select DISTINCT at.description from accounts_setup at,balance_sheet_view bv,sub_activities sa,activity ac where at.main_code = '"
                            + listofAct2[i].toString()
                            + "' and bv.gl_code = ac.code and ac.sub_code = sa.sub_code and sa.main_code = at.main_code and at.class = 'bli'");

                java.sql.Statement st15 = connectDB.createStatement();
                java.sql.ResultSet rset15 =
                    st15.executeQuery(
                        "select bv.gl_code,bv.description,sum(bv.amount) from balance_sheet_view bv,activity ac,accounts_setup at,sub_activities sa where at.main_code = '"
                            + listofAct2[i].toString()
                            + "' and sa.main_code = '"
                            + listofAct2[i].toString()
                            + "' and ac.sub_code = sa.sub_code and bv.gl_code = ac.code and at.class = 'bli' AND bv.date BETWEEN '"
                            + beginDate
                            + "' AND '"
                            + endDate
                            + "' group by bv.gl_code,bv.description order by bv.gl_code");

                table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);
                table.getDefaultCell().setColspan(3);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                table.addCell("  ");

                while (rset11.next()) {
                  table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);
                  table.getDefaultCell().setColspan(3);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase(rset11.getObject(1).toString(), pFontHeader1);
                  System.out.println(rset11.getObject(1).toString());

                  table.addCell(phrase);
                }

                while (rset15.next()) {
                  table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);
                  table.getDefaultCell().setColspan(1);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase(rset15.getObject(1).toString(), pFontHeader1);
                  table.addCell(phrase);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  phrase = new Phrase(rset15.getObject(2).toString(), pFontHeader1);
                  System.out.println(rset15.getObject(2).toString());
                  table.addCell(phrase);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
                  // table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);
                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(rset15.getString(3)),
                          pFontHeader1);

                  table.addCell(phrase);
                }
                //            }
                while (rset13.next()) // {

                  // table.getDefaultCell().setColspan(2);

                  //  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  // table.getDefaultCell().
                  //  phrase = new Phrase("Profit & Loss", pFontHeader);

                  table.getDefaultCell().setColspan(1);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);

                phrase = new Phrase(rset13.getObject(2).toString(), pFontHeader1);

                table.addCell(phrase);

                table.getDefaultCell().setColspan(1);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);

                phrase = new Phrase(rset13.getObject(1).toString(), pFontHeader1);

                table.addCell(phrase);

                // table.addCell(phrase);

                table.getDefaultCell().setColspan(1);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset13.getString(3)),
                        pFontHeader1);

                table.addCell(phrase);

                // }

                while (rset14.next()) {

                  table.getDefaultCell().setColspan(2);
                  table.getDefaultCell().setBorder(Rectangle.TOP);
                  table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  // table.getDefaultCell().
                  phrase = new Phrase("Total Equity", pFontHeader);

                  table.addCell(phrase);

                  table.getDefaultCell().setColspan(1);
                  table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(rset14.getString(1)),
                          pFontHeader1);

                  table.addCell(phrase);
                }

                table.getDefaultCell().setColspan(3);
                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                table.addCell("  ");
                table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

                while (rset16.next()) {

                  table.getDefaultCell().setColspan(2);

                  table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);
                  table.getDefaultCell().setBorder(Rectangle.BOTTOM);

                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                  // table.getDefaultCell().
                  phrase = new Phrase("Total Liabilities + Equity", pFontHeader);

                  table.addCell(phrase);

                  table.getDefaultCell().setColspan(1);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
                  float totalLiabEq = rset16.getFloat(1);
                  if (totalLiabEq != 0) {

                    //  phrase = new Phrase(totalLiabEq);

                    phrase =
                        new Phrase(
                            new com.afrisoftech.sys.Format2Currency()
                                .Format2Currency(rset16.getString(1)),
                            pFontHeader1);
                    table.addCell(phrase);
                  } else {
                    phrase =
                        new Phrase(
                            new com.afrisoftech.sys.Format2Currency()
                                .Format2Currency(rset13.getString(3)),
                            pFontHeader1);

                    table.addCell(phrase);
                  }
                }
              }

              docPdf.add(table);

            } catch (java.sql.SQLException SqlExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }

          } catch (com.lowagie.text.BadElementException BadElExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

        } catch (java.io.FileNotFoundException fnfExec) {

          javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), fnfExec.getMessage());
        }
      } catch (com.lowagie.text.DocumentException lwDocexec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), lwDocexec.getMessage());
      }

      docPdf.close();

      try {

        if (System.getProperty("os.name").equalsIgnoreCase("Linux")) {

          System.out.println(tempFile);

          wait_for_Pdf2Show = rt.exec("kghostview " + tempFile + "");

          wait_for_Pdf2Show.waitFor();

        } else {

          wait_for_Pdf2Show =
              rt.exec("c:/Program Files/Adobe/Acrobat 5.0/Reader/AcroRd32.exe " + tempFile);

          wait_for_Pdf2Show.waitFor();
        }

      } catch (java.lang.InterruptedException intrExec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), intrExec.getMessage());
      }

    } catch (java.io.IOException IOexec) {

      javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), IOexec.getMessage());
    }
  }
  public void generatePdf() {

    java.lang.Process wait_for_Pdf2Show;

    java.util.Calendar cal = java.util.Calendar.getInstance();

    java.util.Date dateStampPdf = cal.getTime();

    java.lang.String pdfDateStamp = dateStampPdf.toString();

    try {

      java.io.File tempFile =
          java.io.File.createTempFile("REP" + this.getDateLable() + "_", ".pdf");

      tempFile.deleteOnExit();

      java.lang.Runtime rt = java.lang.Runtime.getRuntime();

      java.lang.String debitTotal = null;

      java.lang.String creditTotal = null;

      com.lowagie.text.Document docPdf = new com.lowagie.text.Document(PageSize.A4.rotate());

      try {

        try {

          com.lowagie.text.pdf.PdfWriter.getInstance(
              docPdf, new java.io.FileOutputStream(tempFile));

          String compName = null;
          String date = null;

          com.lowagie.text.HeaderFooter headerFoter =
              new com.lowagie.text.HeaderFooter(
                  new Phrase(
                      "KENYA REVENUE AUTHORITY "
                          + "\n INCOME TAX DEPARTMENT "
                          + "\n INCOME TAX DEDUCTION CARD YEAR "
                          + endDate,
                      pFontHeader3),
                  false); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
          // Font.BOLDITALIC,java.awt.Color.blue)));

          //  com.lowagie.text.HeaderFooter headerFoter = new com.lowagie.text.HeaderFooter(new
          // Phrase(""+compName+""),false);//
          // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
          // Font.BOLDITALIC,java.awt.Color.blue)));
          headerFoter.setAlignment(com.lowagie.text.HeaderFooter.ALIGN_CENTER);
          headerFoter.setBorderColor(java.awt.Color.WHITE);
          docPdf.setHeader(headerFoter);

          //         com.lowagie.text.HeaderFooter footer = new com.lowagie.text.HeaderFooter(new
          // Phrase("P9A \t\t\t\t\t (See back of this card for further information required by the
          // Department)."), true);// FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA,
          // 12, Font.BOLDITALIC,java.awt.Color.blue));

          //         docPdf.setFooter(footer);

          docPdf.open();

          try {
            //  java.lang.Object listofStaffNos[] = this.getListofStaffNos();

            //  for (int j = 0; j < listofStaffNos.length; j++) {

            com.lowagie.text.pdf.PdfPTable table = new com.lowagie.text.pdf.PdfPTable(8);

            int headerwidths[] = {12, 25, 10, 10, 10, 12, 10, 12};

            table.setWidths(headerwidths);

            table.setWidthPercentage((100));

            table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);
            table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

            table.getDefaultCell().setColspan(8);

            Phrase phrase = new Phrase("", pFontHeader);

            // table.addCell(phrase);

            table
                .getDefaultCell()
                .setBorder(Rectangle.BOTTOM | Rectangle.LEFT | Rectangle.RIGHT | Rectangle.TOP);

            table.getDefaultCell().setColspan(1);
            try {
              //  table.getDefaultCell().setBackgroundColor(java.awt.Color.LIGHT_GRAY);
              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
              phrase = new Phrase("YEAR", pFontHeader);
              table.addCell(phrase);

              phrase = new Phrase("DESCRIPTION", pFontHeader);
              table.addCell(phrase);

              phrase = new Phrase("DEBIT", pFontHeader);
              table.addCell(phrase);

              phrase = new Phrase("CREDIT", pFontHeader);
              table.addCell(phrase);

              phrase = new Phrase("TAX/DIVINDED", pFontHeader);
              table.addCell(phrase);
              table.getDefaultCell().setColspan(1);
              phrase = new Phrase("DATE PAID", pFontHeader);
              table.addCell(phrase);

              table.getDefaultCell().setColspan(1);

              phrase = new Phrase("TAX REFUND", pFontHeader);
              table.addCell(phrase);

              phrase = new Phrase("DATE REFUNDED", pFontHeader);
              table.addCell(phrase);

              table.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
              table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

              java.sql.Statement st = connectDB.createStatement();

              java.sql.Statement st11b = connectDB.createStatement();
              java.sql.ResultSet rset1 =
                  st.executeQuery(
                      "select tax_year,item_description,debit,credit,tax_divided,case when (date_paid is null) then 'now' else date_paid end as date_paid,tax_refunded,case when (date_refunded is null) then 'now' else date_refunded end as date_refunded from ac_corporation_tax where tax_year BETWEEN '"
                          + beginDate
                          + "' AND '"
                          + endDate
                          + "'");

              while (rset1.next()) {

                table.getDefaultCell().setColspan(1);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);

                phrase = new Phrase(rset1.getObject(1).toString(), pFontHeader1);
                table.addCell(phrase);

                phrase = new Phrase(rset1.getObject(2).toString().toUpperCase(), pFontHeader1);
                table.addCell(phrase);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset1.getString(3)),
                        pFontHeader1);
                table.addCell(phrase);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset1.getString(4)),
                        pFontHeader1);
                table.addCell(phrase);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset1.getString(5)),
                        pFontHeader1);
                table.addCell(phrase);

                //  phrase = new Phrase(new
                // com.afrisoftech.sys.Format2Currency().Format2Currency(rset1.getString(6)),
                // pFontHeader1);
                //  table.addCell(phrase);

                phrase = new Phrase(rset1.getObject(6).toString(), pFontHeader1);
                table.addCell(phrase);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(rset1.getString(7)),
                        pFontHeader1);
                table.addCell(phrase);

                phrase = new Phrase(rset1.getObject(8).toString(), pFontHeader1);
                table.addCell(phrase);
              }

              docPdf.add(table);

              /* } catch(com.lowagie.text.BadElementException BadElExec) {

                  javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), BadElExec.getMessage());

              }*/
              System.out.println("Status of new page : " + docPdf.getPageNumber());

              boolean boolNewPage = docPdf.newPage();
            } catch (java.sql.SQLException SqlExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }

            // }

          } catch (com.lowagie.text.BadElementException BadElExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }
          //

        } catch (java.io.FileNotFoundException fnfExec) {

          javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), fnfExec.getMessage());
        }
      } catch (com.lowagie.text.DocumentException lwDocexec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), lwDocexec.getMessage());
      }

      docPdf.close();

      try {

        if (System.getProperty("os.name").equalsIgnoreCase("Linux")) {

          System.out.println(tempFile);
          //      wait_for_Pdf2Show = rt.exec("kghostview "+tempFile+"");

          wait_for_Pdf2Show = rt.exec("xpdf " + tempFile + "");

          wait_for_Pdf2Show.waitFor();

        } else {

          wait_for_Pdf2Show =
              rt.exec("c:/Program Files/Adobe/Acrobat 5.0/Reader/AcroRd32.exe " + tempFile);

          wait_for_Pdf2Show.waitFor();
        }

      } catch (java.lang.InterruptedException intrExec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), intrExec.getMessage());
      }

    } catch (java.io.IOException IOexec) {

      javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), IOexec.getMessage());
    }

    // }
    /*   public java.lang.Object[] getListofStaffNos() {

        java.lang.Object[] listofStaffNos = null;

        java.util.Vector listStaffNoVector = new java.util.Vector(1,1);


        try {

            //    java.sql.Connection connDB = java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/sako","postgres","pilsiner");

            java.sql.Statement stmt1 = connectDB.createStatement();

            java.sql.ResultSet rSet1 = stmt1.executeQuery("SELECT DISTINCT staff_no FROM tax_card where date BETWEEN '"+beginDate+"' AND '"+endDate+"' order by staff_no");

            while (rSet1.next()) {

                listStaffNoVector.addElement(rSet1.getObject(1).toString());

            }

        }catch (java.sql.SQLException sqlExec) {

            javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), sqlExec.getMessage());

        }

        listofStaffNos = listStaffNoVector.toArray();
        System.out.println("Done list of Staff Nos ...");
        return listofStaffNos;

    }

    public java.lang.Object[] getListofStaffNos1() {

        java.lang.Object[] listofStaffNos1 = null;

        java.util.Vector listStaffNoVector1 = new java.util.Vector(1,1);


        try {

            //    java.sql.Connection connDB = java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/sako","postgres","pilsiner");

            java.sql.Statement stmt1 = connectDB.createStatement();

            java.sql.ResultSet rSet1 = stmt1.executeQuery("SELECT DISTINCT month FROM tax_card where date BETWEEN '"+beginDate+"' AND '"+endDate+"' order by month");

            while (rSet1.next()) {

                listStaffNoVector1.addElement(rSet1.getObject(1).toString());

            }

        }catch (java.sql.SQLException sqlExec) {

            javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), sqlExec.getMessage());

        }

        listofStaffNos1 = listStaffNoVector1.toArray();
        System.out.println("Done list of Staff Nos ...");
        return listofStaffNos1;

    }*/
  }
  public void generatePdf() {

    // java.lang.Object[][] ageingDates = periodicDates.getMonthlyDates();

    ageingDates = ageingSeries.getAgeingDateSeries();

    double columnTotals[] = new double[ageingDates.length];

    java.lang.Process wait_for_Pdf2Show;

    java.util.Calendar cal = java.util.Calendar.getInstance();

    java.util.Date dateStampPdf = cal.getTime();

    java.lang.String pdfDateStamp = dateStampPdf.toString();

    int interval = 0;

    try {

      java.io.File tempFile =
          java.io.File.createTempFile("REP" + this.getDateLable() + "_", ".pdf");

      tempFile.deleteOnExit();

      java.lang.Runtime rt = java.lang.Runtime.getRuntime();

      java.lang.String debitTotal = null;

      java.lang.String creditTotal = null;

      com.lowagie.text.Document docPdf = new com.lowagie.text.Document();

      java.util.Calendar calendar = java.util.Calendar.getInstance();

      long dateNow = calendar.getTimeInMillis();

      java.sql.Date datenowSql = new java.sql.Date(dateNow);

      System.out.println(datenowSql.toString());

      try {

        try {

          com.lowagie.text.pdf.PdfWriter.getInstance(
              docPdf, new java.io.FileOutputStream(tempFile));

          String compName = null;
          String date = null;
          try {

            //   java.sql.Connection conDb =
            // java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/sako","postgres","pilsiner");

            java.sql.Statement st3 = connectDB.createStatement();
            java.sql.Statement st4 = connectDB.createStatement();

            java.sql.ResultSet rset2 =
                st3.executeQuery("SELECT hospital_name from pb_hospitalprofile");
            java.sql.ResultSet rset4 = st4.executeQuery("SELECT date('now') as Date");
            while (rset2.next()) compName = rset2.getObject(1).toString();

            while (rset4.next()) date = rset4.getObject(1).toString();

            com.lowagie.text.HeaderFooter headerFoter =
                new com.lowagie.text.HeaderFooter(
                    new Phrase("" + compName, pFontHeader2),
                    false); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
            // Font.BOLDITALIC,java.awt.Color.blue)));

            //  com.lowagie.text.HeaderFooter headerFoter = new com.lowagie.text.HeaderFooter(new
            // Phrase(""+compName+""),false);//
            // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14,
            // Font.BOLDITALIC,java.awt.Color.blue)));
            headerFoter.setAlignment(com.lowagie.text.HeaderFooter.ALIGN_CENTER);
            docPdf.setHeader(headerFoter);

          } catch (java.sql.SQLException SqlExec) {

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), SqlExec.getMessage());
          }

          com.lowagie.text.HeaderFooter footer =
              new com.lowagie.text.HeaderFooter(
                  new Phrase(Debtor + ": Ageing  Page: ", pFontHeader),
                  true); // FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 12,
          // Font.BOLDITALIC,java.awt.Color.blue));

          docPdf.setFooter(footer);

          docPdf.open();

          double Totals = 0.00;
          double OS = 0.00;
          try {

            com.lowagie.text.pdf.PdfPTable table =
                new com.lowagie.text.pdf.PdfPTable(ageingDates.length + 4);

            String headerWidths = null;

            java.util.Vector headerVector = new java.util.Vector(1, 1);

            int z = ageingDates.length;

            int headerwidths[] = {22, 13, 13, 13, 13, 13, 13, 13}; // ,13,13};

            table.setWidths(headerwidths);

            table.setWidthPercentage((100));

            table.setHeaderRows(2);

            table.getDefaultCell().setBorder(Rectangle.BOTTOM);

            table.getDefaultCell().setColspan(8);
            Phrase phrase = new Phrase("");
            table.getDefaultCell().setColspan(5);
            try {
              java.text.DateFormat dateFormat =
                  java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM); // MEDIUM);

              //                            java.util.Date endDate1 =
              // dateFormat.parse(beginDate.toLocaleString());//dateInstance.toLocaleString());
              java.util.Date endDate2 = dateFormat.parse(endDate.toLocaleString());
              //  System.out.println(""+endDate1);
              //  phrase = new Phrase(bank +" Report: " +dateFormat.format(formattedDate),
              // pFontHeader);

              //  table.addCell(phrase);

              phrase =
                  new Phrase(
                      Debtor + ": Payer  Ageing as at " + dateFormat.format(endDate2), pFontHeader);

              table.addCell(phrase);
            } catch (java.text.ParseException psExec) {

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), psExec.getMessage());
            }

            table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
            table.getDefaultCell().setColspan(3);
            phrase = new Phrase("Printed on : " + date, pFontHeader);
            table.addCell(phrase);

            table.getDefaultCell().setColspan(1);

            table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
            phrase = new Phrase("Name", pFontHeader);
            table.addCell(phrase);
            // table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

            //                 phrase = new Phrase("O/S Balance",pFontHeader);
            //                 table.addCell(phrase);

            for (int x = 0; x < ageingDates.length; x++) {

              int days = 30;
              if (x < 1) {
                phrase = new Phrase("Current", pFontHeader);
              } else {
                phrase = new Phrase("Over " + x * days + " Days", pFontHeader);
                interval = x;
              }

              table.addCell(phrase);
            }
            //  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
            phrase = new Phrase("Over " + ((interval + 1) * 30) + " Days", pFontHeader);
            table.addCell(phrase);

            //      table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
            phrase = new Phrase("UnAlloc.", pFontHeader);
            table.addCell(phrase);

            phrase = new Phrase("O/S", pFontHeader);
            table.addCell(phrase);

            table.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE);
            // table.getDefaultCell().setBorderColor(java.awt.Color.WHITE);

            try {

              double GrandTotal = 0.00;
              double Over120Total = 0.00;
              java.lang.Object[] listofAct = this.getListofActivities();

              //    java.sql.Connection conDb1 =
              // java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/sako","postgres","pilsiner");

              System.out.println(listofAct.length);

              for (int i = 0; i < listofAct.length; i++) {
                double TurnOver = 0.00;
                double Over120 = 0.00;
                double TotalCount = 0.00;

                table.getDefaultCell().setColspan(1);

                table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
                java.sql.PreparedStatement pset10 =
                    connectDB.prepareStatement(
                        "select account_no||' '||scheme_name from ac_schemes WHERE account_no = ?"); // < '"+endDate+"'::date and date > '"+endDate+"'::date - 30 group by dealer");

                // java.sql.PreparedStatement pset10 = connectDB.prepareStatement("select distinct
                // initcap(scheme_manager) as dealer from ac_scheme_providers WHERE scheme_manager =
                // ?");//< '"+endDate+"'::date and date > '"+endDate+"'::date - 30 group by
                // dealer");
                pset10.setString(1, listofAct[i].toString());
                java.sql.ResultSet rset10 = pset10.executeQuery();
                while (rset10.next()) table.getDefaultCell().setColspan(1);
                //  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
                phrase = new Phrase(dbObject.getDBObject(rset10.getObject(1), "-"), pFontHeader1);
                table.addCell(phrase);
                // columnTotals[t] = columnTotals[t] + rset.getDouble(1);

                // table.addCell(phrase);

                java.sql.Statement st2 = connectDB.createStatement();
                java.sql.Statement st21 = connectDB.createStatement();
                java.sql.Statement st22 = connectDB.createStatement();
                java.sql.Statement st23 = connectDB.createStatement();
                java.sql.Statement st211 = connectDB.createStatement();
                java.sql.Statement st221 = connectDB.createStatement();
                java.sql.Statement st231 = connectDB.createStatement();
                java.sql.Statement st2A = connectDB.createStatement();
                java.sql.Statement st2B = connectDB.createStatement();
                java.sql.Statement st2C = connectDB.createStatement();
                java.sql.Statement stc = connectDB.createStatement();
                System.out.println("Dealer Is : [" + listofAct[i] + "].");

                java.sql.PreparedStatement pset1 =
                    connectDB.prepareStatement(
                        "select sum(balance) from ac_debtors WHERE account_no = ? and dealer = '"
                            + Debtor
                            + "'  AND date < '"
                            + ageingDates[0][0]
                            + "'"); // < '"+endDate+"'::date and date > '"+endDate+"'::date - 30
                // group by dealer");
                // java.sql.PreparedStatement pset1 = connectDB.prepareStatement("select
                // sum(balance) from ac_debtors WHERE account_no = ? and dealer = '"+Debtor+"'  AND
                // date < '"+ageingDates[ageingDates.length - 1][0]+"'");//< '"+endDate+"'::date and
                // date > '"+endDate+"'::date - 30 group by dealer");

                pset1.setString(1, listofAct[i].toString());
                java.sql.ResultSet rset1 = pset1.executeQuery();

                java.sql.PreparedStatement pset111 =
                    connectDB.prepareStatement(
                        "select sum(balance-credit_bal) from ac_debtors WHERE account_no = ? and dealer = '"
                            + Debtor
                            + "'  AND date < '"
                            + endDate
                            + "'"); // < '"+endDate+"'::date and date > '"+endDate+"'::date - 30
                // group by dealer");
                pset111.setString(1, listofAct[i].toString());
                java.sql.ResultSet rset111 = pset111.executeQuery();
                java.sql.Statement st02 = connectDB.createStatement();

                java.sql.PreparedStatement pset112 =
                    connectDB.prepareStatement(
                        "select sum(credit_bal) from ac_debtors WHERE account_no = ? and dealer = '"
                            + Debtor
                            + "' AND date < '"
                            + endDate
                            + "'"); // < '"+endDate+"'::date and date > '"+endDate+"'::date - 30
                // group by dealer");
                pset112.setString(1, listofAct[i].toString());
                java.sql.ResultSet rset112 = pset112.executeQuery();

                for (int t = ageingDates.length - 1; t >= 0; t--) {
                  // for (int t = 0; t < ageingDates.length; t++) {
                  java.sql.Statement st01 = connectDB.createStatement();

                  java.sql.PreparedStatement pset =
                      connectDB.prepareStatement(
                          "select sum(balance) from ac_debtors WHERE account_no = ? and dealer = '"
                              + Debtor
                              + "'  AND date between '"
                              + ageingDates[t][0]
                              + "' AND '"
                              + ageingDates[t][1]
                              + "'");
                  pset.setString(1, listofAct[i].toString().toUpperCase());
                  java.sql.ResultSet rset = pset.executeQuery();

                  while (rset.next()) {

                    table.getDefaultCell().setColspan(1);
                    table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
                    phrase =
                        new Phrase(
                            new com.afrisoftech.sys.Format2Currency()
                                .Format2Currency(rset.getString(1)),
                            pFontHeader1);
                    table.addCell(phrase);
                    columnTotals[t] = columnTotals[t] + rset.getDouble(1);
                  }
                  TotalCount = TotalCount + rset.getDouble(1);
                }

                while (rset1.next()) {
                  System.out.println("This Result " + rset1.getString(1));
                  table.getDefaultCell().setColspan(1);
                  table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);
                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(rset1.getString(1)),
                          pFontHeader1);
                  // phrase = new Phrase(new
                  // com.afrisoftech.sys.Format2Currency().Format2Currency("0"),pFontHeader1);

                  Over120 = rset1.getDouble(1);
                  table.addCell(phrase);
                  Over120Total = Over120Total + rset1.getDouble(1);
                  TurnOver = TotalCount + Over120;
                }

                while (rset112.next()) {
                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(rset112.getString(1)),
                          pFontHeader1);
                  table.addCell(phrase);
                  OS = OS + rset112.getDouble(1);
                }

                while (rset111.next()) {
                  phrase =
                      new Phrase(
                          new com.afrisoftech.sys.Format2Currency()
                              .Format2Currency(rset111.getString(1)),
                          pFontHeader1);
                  table.addCell(phrase);
                  Totals = Totals + rset111.getDouble(1);
                }
              }

              table.getDefaultCell().setBorderColor(java.awt.Color.BLACK);

              table.getDefaultCell().setBorder(Rectangle.BOTTOM | Rectangle.TOP);

              // while (rsetTotals.next()) {

              table.getDefaultCell().setColspan(1);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
              phrase = new Phrase("Total", pFontHeader);

              table.addCell(phrase);

              table.getDefaultCell().setColspan(1);

              table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT);

              // phrase = new Phrase(new
              // com.afrisoftech.sys.Format2Currency().Format2Currency(rsetTotals.getString(1)),
              // pFontHeader);
              // for (int x = 0; x < ageingDates.length; x++) {
              for (int x = ageingDates.length - 1; x >= 0; x--) {
                // phrase = new Phrase("Current"+2*x,pFontHeader);
                // table.addCell(phrase);

                phrase =
                    new Phrase(
                        new com.afrisoftech.sys.Format2Currency()
                            .Format2Currency(java.lang.String.valueOf(columnTotals[x])),
                        pFontHeader);

                table.addCell(phrase);
              }

              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(Over120Total)),
                      pFontHeader);

              table.addCell(phrase);
              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(OS)),
                      pFontHeader);

              table.addCell(phrase);

              phrase =
                  new Phrase(
                      new com.afrisoftech.sys.Format2Currency()
                          .Format2Currency(java.lang.String.valueOf(Totals)),
                      pFontHeader);

              table.addCell(phrase);

              docPdf.add(table);

            } catch (java.sql.SQLException SqlExec) {

              SqlExec.printStackTrace();

              javax.swing.JOptionPane.showMessageDialog(
                  new javax.swing.JFrame(), SqlExec.getMessage());
            }

          } catch (com.lowagie.text.BadElementException BadElExec) {

            // Bad

            javax.swing.JOptionPane.showMessageDialog(
                new javax.swing.JFrame(), BadElExec.getMessage());
          }

        } catch (java.io.FileNotFoundException fnfExec) {

          javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), fnfExec.getMessage());
        }
      } catch (com.lowagie.text.DocumentException lwDocexec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), lwDocexec.getMessage());
      }

      docPdf.close();

      try {

        if (System.getProperty("os.name").equalsIgnoreCase("Linux")) {

          System.out.println(tempFile);

          wait_for_Pdf2Show = rt.exec("kghostview " + tempFile + "");

          wait_for_Pdf2Show.waitFor();

        } else {

          wait_for_Pdf2Show =
              rt.exec("c:/Program Files/Adobe/Acrobat 5.0/Reader/AcroRd32.exe " + tempFile);

          wait_for_Pdf2Show.waitFor();
        }

      } catch (java.lang.InterruptedException intrExec) {

        javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), intrExec.getMessage());
      }

    } catch (java.io.IOException IOexec) {

      javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), IOexec.getMessage());
    }
  }