<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="com.csc.library.utility.DHMConvert"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Vector"%>
<%@page import="java.util.*"%>
<%@page import="com.csc.library.report.ReportUDF"%>
<%@page import="javax.servlet.jsp.jstl.sql.Result"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="com.csc.library.utilities.MyLog"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="java.text.DecimalFormat"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.text.SimpleDateFormat" %>
<%@page import="java.util.Locale" %>
<%@page import="java.util.Date" %>
<%@page import="java.util.GregorianCalendar" %>
<%@page import="com.csc.library.database.DataSet"%>
<%@page import="com.csc.library.session.InitialInquiry"%>
<%@page import="com.csc.library.session.InitialRecord"%>
<%@page import="com.csc.library.utilities.UProfile"%>
<%@page import="com.csc.library.utilities.CscCalendar"%>
<%@page import="com.csc.library.session.DbInquiry"%>
<%@page import="com.csc.library.session.DbRecord"%>
<%@page import="com.csc.library.utilities.CheckNull"%>
<%@page import="com.csc.library.entry.HelpEntry"%>
<%@page import="com.itextpdf.text.*"%>
<%@page import="com.itextpdf.text.pdf.*"%>
<%@page import="java.io.ByteArrayOutputStream" %>
<%@page import="com.csc.library.session.InitialEnvironment" %>
<jsp:useBean id="EMV" class="com.csc.library.system.Task" scope="page" />
<jsp:setProperty name="EMV" property="appname" value="EMPVIEW" />
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
      <title>Report</title>
    </head>
    <body>
      Not found data
      <%
      EMV.setChannel(request, response);
      HelpEntry screen = (HelpEntry)EMV.process("HelpEntry", "MCOMPANYHELP");
      screen.setMaxLine(-1);
      CheckNull chk = new CheckNull();
      setHelpEntry(screen);
      screen.process();

      DbInquiry op = new InitialInquiry(screen.getUProfile()).getDbInquiry("COLOPTION");
      op.setColumn("*");
      op.setFilter("optionid = '22' ");
      op.refresh();
      op.next();

      Map<Integer, Integer> linkmap = new LinkedHashMap<Integer, Integer>();
      for(int i=1;i<=6;i++){
        String chknum = op.getString("chk"+i);
        if(chknum.equals("1")){
          linkmap.put(i,Integer.valueOf(op.getString("col"+i)));
        }
      }

      ByteArrayOutputStream baos = new ByteArrayOutputStream();//72
      Document document = new Document(PageSize.A4.rotate(), 20, 22, 146, 36);

      if(linkmap.size()==0){
        document = new Document(PageSize.A4.rotate(), 20, 22, 134, 36);
        sizeColumn = new float[] {0.6f,1.0f,2.6f,2.0f,2.0f,2.0f,1.5f};
      }else if(linkmap.size()==1){
        sizeColumn = new float[] {0.6f,1.0f,2.6f,2.0f,2.0f,2.0f,1.5f,1.5f};
      }else if(linkmap.size()==2){
        sizeColumn = new float[] {0.6f,1.0f,2.6f,2.0f,2.0f,2.0f,1.5f,1.5f,1.5f};
      }else if(linkmap.size()==3){
        sizeColumn = new float[] {0.6f,1.0f,2.6f,2.0f,2.0f,2.0f,1.5f,1.5f,1.5f,1.5f};
      }else if(linkmap.size()==4){
        sizeColumn = new float[] {0.6f,1.0f,2.6f,2.0f,2.0f,2.0f,1.5f,1.5f,1.5f,1.5f,1.5f};
      }else if(linkmap.size()==5){
        sizeColumn = new float[] {0.6f,1.0f,2.6f,2.0f,2.0f,2.0f,1.5f,1.5f,1.5f,1.5f,1.5f,1.5f};
      }else if(linkmap.size()==6){
        sizeColumn = new float[] {0.6f,1.0f,2.6f,2.0f,2.0f,2.0f,1.5f,1.5f,1.5f,1.5f,1.5f,1.5f,1.5f};
      }

      PdfWriter.getInstance(document, baos);
      document.open();
      document.addTitle("Document Title");

      BaseFont bf = BaseFont.createFont(application.getRealPath("FONTS/Cordia.ttf"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
      BaseFont bfHead = BaseFont.createFont(application.getRealPath("FONTS/Cordia.ttf"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true);
      Font font = new Font(bf, 10);
      Font fontHead = new Font(bfHead, 10);
      //fontHead.setStyle(Font.BOLD);
      Font fontContent = new Font(bfHead, 10);
      Font fontTitle = new Font(bfHead, 16);
      fontTitle.setStyle(Font.BOLD);

      // set vlign middle
      Phrase content = new Phrase("Blah blah blah", font);
      Float fontSize = content.getFont().getSize();
      Float capHeight = content.getFont().getBaseFont().getFontDescriptor(BaseFont.CAPHEIGHT, fontSize);
      Float padding = 5f;
      boolean statusData = false;

      CscCalendar cc = new CscCalendar();
      String branch = chk.chkNullString(request.getParameter("branch"));
      String branchDesc = chk.chkNullString(request.getParameter("MBRANCH@TDESC"));
      String startdate = chk.chkNullString(request.getParameter("startdate"), cc.getDDMMYYYY());
      String enddate = chk.chkNullString(request.getParameter("enddate"), cc.getDDMMYYYY());
      String allbranch = chk.chkNullString(request.getParameter("allbranch"));
      String filter = chk.chkNullString(request.getParameter("__filter"),"1=1");
      String bu1 = chk.chkNullString(request.getParameter("__bu1"));
      String bu2 = chk.chkNullString(request.getParameter("__bu2"));
      String bu3 = chk.chkNullString(request.getParameter("__bu3"));
      String bu4 = chk.chkNullString(request.getParameter("__bu4"));
      String bu5 = chk.chkNullString(request.getParameter("__bu5"));
      String workarea = chk.chkNullString(request.getParameter("__workarea"));
      CscCalendar st = new CscCalendar(startdate);
      CscCalendar en = new CscCalendar(enddate);
      if(!allbranch.equals("")){
        filter += allbranch;
      }

      if(!branch.equals("")){
        filter += " and branch = '"+branch+"'";
      }

      if(!bu1.equals("")){
        filter += " and bu1 = '"+bu1+"'";
      }

      if(!bu2.equals("")){
        filter += " and bu2 = '"+bu2+"'";
      }

      if(!bu3.equals("")){
        filter += " and bu3 = '"+bu3+"'";
      }

      if(!bu4.equals("")){
        filter += " and bu4 = '"+bu4+"'";
      }

      if(!bu5.equals("")){
        filter += " and bu5 = '"+bu5+"'";
      }

      if(!workarea.equals("")){
        filter += " and workarea = '"+workarea+"'";
      }
      if(!startdate.equals("")){
        filter += " and startdate between '"+st.getYYYYMMDD()+"' and '"+en.getYYYYMMDD()+"'";
      }

      boolean hasData = false;
      if(screen.getInquiry().recCount() > 0) {
        try{
          String DbType = screen.getUProfile().getDbType().toUpperCase();
          String strJoiner = "+";
          if(DbType.equals("PGSQL") || DbType.equals("ORACLE")){
            strJoiner = "||";
          }

          String sql="";
          if(screen.getUProfile().get("lang").equals("eng")){
      			 sql = "select me.EMPLOYEEID,pre.EDESC"+strJoiner+"me.EFNAME"+strJoiner+"' '"+strJoiner+"me.ELNAME as fullname,b1.EDESC as bu1,b2.EDESC as bu2,b3.EDESC as bu3,STARTDATE from MEMPLOYEE me left join MPREFIX pre on me.EMP_PREFIX = pre.PREFIXID left join MBU3 b3 on me.BU3 = b3.BU3ID left join MBU2 b2 on me.BU2 = b2.BU2ID left join MBU1 b1 on me.BU1 = b1.BU1ID where "+filter+" ORDER BY me.bu1,me.bu2,me.bu3,me.bu4,me.bu5";
      		}else{
      			 sql = "select me.EMPLOYEEID,pre.TDESC"+strJoiner+"me.FNAME"+strJoiner+"' '"+strJoiner+"me.LNAME as fullname,b1.TDESC as bu1,b2.TDESC as bu2,b3.TDESC as bu3,STARTDATE from MEMPLOYEE me left join MPREFIX pre on me.EMP_PREFIX = pre.PREFIXID left join MBU3 b3 on me.BU3 = b3.BU3ID left join MBU2 b2 on me.BU2 = b2.BU2ID left join MBU1 b1 on me.BU1 = b1.BU1ID where "+filter+" ORDER BY me.bu1,me.bu2,me.bu3,me.bu4,me.bu5";
      		}

          DataSet ds = new DataSet(sql.toString(),screen.getUProfile().get("dbname"),"Memployee");
          ds.initConnection(screen.getUProfile());
          int no = 1;
          int span = linkmap.size()+7;
          while (ds.next()) {
            hasData = true;
            CscCalendar stDate = new CscCalendar(chk.chkNullString(ds.getString("startdate")));
            CscCalendar test1 = new CscCalendar(chk.chkNullString(ds.getString("startdate")));
            CscCalendar test2 = new CscCalendar(chk.chkNullString(ds.getString("startdate")));
            CscCalendar test3 = new CscCalendar(chk.chkNullString(ds.getString("startdate")));
            CscCalendar test4 = new CscCalendar(chk.chkNullString(ds.getString("startdate")));
            String testWork1 = test1.incDate(60).getDDMMYYYY() ;
            String testWork2 = test2.incDate(85).getDDMMYYYY() ;
            String testWork3 = test3.incDate(90).getDDMMYYYY() ;
            String testWork4 = test4.incDate(119).getDDMMYYYY() ;

            PdfPTable detail = new PdfPTable(span);
            detail.setWidthPercentage(100);
            detail.setWidths(sizeColumn);
            PdfPCell cellDetail;

            cellDetail = new PdfPCell(new Phrase(String.valueOf(no), font));
            cellDetail.setPadding(padding);
            cellDetail.setPaddingTop(capHeight - fontSize + padding);
            cellDetail.setHorizontalAlignment(Element.ALIGN_CENTER);
            detail.addCell(cellDetail);

            cellDetail = new PdfPCell(new Phrase(ds.getString("employeeid"), font));
            cellDetail.setPadding(padding);
            cellDetail.setPaddingTop(capHeight - fontSize + padding);
            cellDetail.setHorizontalAlignment(Element.ALIGN_CENTER);
            detail.addCell(cellDetail);

            cellDetail = new PdfPCell(new Phrase(ds.getString("fullname"), font));
            cellDetail.setPadding(padding);
            cellDetail.setPaddingTop(capHeight - fontSize + padding);
            cellDetail.setHorizontalAlignment(Element.ALIGN_LEFT);
            detail.addCell(cellDetail);

            cellDetail = new PdfPCell(new Phrase(ds.getString("bu1"), font));
            cellDetail.setPadding(padding);
            cellDetail.setPaddingTop(capHeight - fontSize + padding);
            cellDetail.setHorizontalAlignment(Element.ALIGN_LEFT);
            detail.addCell(cellDetail);

            cellDetail = new PdfPCell(new Phrase(ds.getString("bu2"), font));
            cellDetail.setPadding(padding);
            cellDetail.setPaddingTop(capHeight - fontSize + padding);
            cellDetail.setHorizontalAlignment(Element.ALIGN_LEFT);
            detail.addCell(cellDetail);

            cellDetail = new PdfPCell(new Phrase(ds.getString("bu3"), font));
            cellDetail.setPadding(padding);
            cellDetail.setPaddingTop(capHeight - fontSize + padding);
            cellDetail.setHorizontalAlignment(Element.ALIGN_LEFT);
            detail.addCell(cellDetail);

            cellDetail = new PdfPCell(new Phrase(stDate.getDDMMYYYY(), font));
            cellDetail.setPadding(padding);
            cellDetail.setPaddingTop(capHeight - fontSize + padding);
            cellDetail.setHorizontalAlignment(Element.ALIGN_CENTER);
            detail.addCell(cellDetail);

            for (Integer key : linkmap.keySet()){
              cellDetail = new PdfPCell(new Phrase(new CscCalendar(chk.chkNullString(ds.getString("startdate"))).incDate(linkmap.get(key)).getDDMMYYYY(), font));
              cellDetail.setPadding(padding);
              cellDetail.setPaddingTop(capHeight - fontSize + padding);
              cellDetail.setHorizontalAlignment(Element.ALIGN_CENTER);
              detail.addCell(cellDetail);
            }

            document.add(detail);

            no++;
          }
          ds.closeConnection();
        } catch(Exception e) {
          e.printStackTrace();
        } finally {
          if(hasData){
            document.close();
          }
        }
      }


      if(hasData){
        OutputStream os = response.getOutputStream();
        // Create a reader
        PdfReader reader = new PdfReader(baos.toByteArray());
        // Create a stamper
        PdfStamper stamper = new PdfStamper(reader,baos);
        // Loop over the pages and add a header to each page

        int n = reader.getNumberOfPages();
        for (int i = 1; i <= n; i++) {
          getHeaderTable(i, n, font, fontHead, fontTitle, fontContent, startdate, enddate).writeSelectedRows(0, -1, 20, 550, stamper.getOverContent(i));  //ไม่รู้ ซ้าย ขวา ล่าง
        }

        // Close the stamper
        stamper.close();
        reader.close();

        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control","must-revalidate, post-check=0 , pre-check=0");
        response.setHeader("Pragma", "public");
        // setting the content type
        response.setContentType("application/pdf");
        // the contentlength
        response.setContentLength(baos.size());
        // write ByteArrayOutputStream to the ServletOutputStream

        baos.writeTo(os);

        os.flush();
        os.close();
      }

      %>

      <%!
      String startdate = "";
      String enddate = "";
      String buDesc = "";

      private void setStartdate(String startdate) {
        this.startdate = startdate;
      }

      private void setEnddate(String enddate) {
        this.enddate = enddate;
      }

      private void setBuDesc(String buDesc) {
        this.buDesc = buDesc;
      }

      float[] sizeColumn = new float[] {0.6f,1.0f,2.6f,2.0f,2.0f,2.0f,1.5f,1.5f,1.5f,1.5f,1.5f};
      private PdfPTable getHeaderTable(int x, int y,Font font,Font fontHead,Font fontTitle,Font fontContent,String startdate,String enddate) {
        try{
          Phrase content = new Phrase("Blah blah blah", font);
          Float fontSize = content.getFont().getSize();
          Float capHeight = content.getFont().getBaseFont().getFontDescriptor(BaseFont.CAPHEIGHT, fontSize);
          Float padding = 3f;
          InitialEnvironment en = new InitialEnvironment("GLOBAL");
          String pathImage = en.getValue("EXCEL-DIR");

          DbRecord emp_rec = new InitialRecord(screen.getUProfile()).getDbRecord("memployee");
          DbInquiry op = new InitialInquiry(screen.getUProfile()).getDbInquiry("COLOPTION");
          op.setColumn("*");
          op.setFilter("optionid = '22' ");
          op.refresh();
          op.next();

          Map<Integer, Integer> linkmap = new LinkedHashMap<Integer, Integer>();
          for(int i=1;i<=6;i++){
            String chknum = op.getString("chk"+i);
            if(chknum.equals("1")){
              linkmap.put(i,Integer.valueOf(op.getString("col"+i)));
            }
          }
          int span = linkmap.size()+7;
          int srefer = linkmap.size() > 4 ? 2 : 1;
          CscCalendar calendar = new CscCalendar();
          SimpleDateFormat df = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
          GregorianCalendar gc= new GregorianCalendar();

          boolean langthai = screen.getUProfile().get("lang").equalsIgnoreCase("THA");
          String reportname = "", perioddate = "", printdate = "", printby = "", refer = "", page = "";
          if(langthai){
            reportname = "รายงาน :                รายงานรายชื่อพนักงานเข้าใหม่และการคำนวณ Probation";
            perioddate = "ช่วงวันที่ :               "+startdate+" ถึง "+enddate;
            printdate = "วันที่พิมพ์ :             วัน"+calendar.getLongThiDOW()+"ที่ "+calendar.getDate()+ " " + calendar.getThaiLongMonth() + " พ.ศ. "+ calendar.getThaiYYYY() +
            " " +calendar.getHHMM() + " น.";
            printby = "พิมพ์โดย :              "+screen.getUProfile().get("tfullname");
            refer = "อ้างอิง #    RPI2100";
            page = "หน้า          "+x+" / "+y;
          } else {
            reportname = "Report Name :         New Employee List And Probation Calulation Report";
            perioddate = "Period Date :           "+startdate+" To "+enddate;
            printdate = "Print Date :              "+calendar.getEngFull()+ " " + df.format((Date)gc.getTime());
            printby = "Print By :                 "+screen.getUProfile().get("efullname");
            refer = "Ref #    RPI2100";
            page = "Page          "+x+" / "+y;
          }

          PdfPTable detail = new PdfPTable(span);
          PdfPCell cellDetail;
          detail.setTotalWidth(800);
          detail.setWidths(sizeColumn);
          detail.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);

          // ------------------ title detail ---------------------

          cellDetail = new PdfPCell(new Phrase(String.format(screen.getUProfile().get("companyname"), x, y),fontTitle));
          cellDetail.setHorizontalAlignment(Element.ALIGN_LEFT);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          cellDetail.setColspan(span);
          cellDetail.setBorder(Rectangle.NO_BORDER);
          detail.addCell(cellDetail);

          // --------------- new line -----------------

          cellDetail = new PdfPCell(new Phrase(String.format(reportname, x, y),fontHead));
          cellDetail.setHorizontalAlignment(Element.ALIGN_LEFT);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          cellDetail.setColspan(span);
          cellDetail.setBorder(Rectangle.NO_BORDER);
          detail.addCell(cellDetail);

          // --------------- new line -----------------

          cellDetail = new PdfPCell(new Phrase(String.format(perioddate, x, y),fontHead));
          cellDetail.setHorizontalAlignment(Element.ALIGN_LEFT);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          cellDetail.setColspan(span);
          cellDetail.setBorder(Rectangle.NO_BORDER);
          detail.addCell(cellDetail);

          // --------------- new line -----------------

          cellDetail = new PdfPCell(new Phrase(String.format(printdate, x, y),fontHead));
          cellDetail.setHorizontalAlignment(Element.ALIGN_LEFT);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          cellDetail.setColspan(span-srefer);
          cellDetail.setBorder(Rectangle.NO_BORDER);
          detail.addCell(cellDetail);

          cellDetail = new PdfPCell(new Phrase(String.format(refer, x, y),fontHead));
          cellDetail.setHorizontalAlignment(Element.ALIGN_RIGHT);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          cellDetail.setColspan(srefer);
          cellDetail.setBorder(Rectangle.NO_BORDER);
          detail.addCell(cellDetail);

          // --------------- new line -----------------

          Float paddingbottom = 7.5f;

          cellDetail = new PdfPCell(new Phrase(String.format(printby, x, y),fontHead));
          cellDetail.setHorizontalAlignment(Element.ALIGN_LEFT);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          cellDetail.setPaddingBottom(paddingbottom);
          cellDetail.setColspan(span-srefer);
          cellDetail.setBorder(Rectangle.NO_BORDER);
          detail.addCell(cellDetail);

          cellDetail = new PdfPCell(new Phrase(String.format(page, x, y),fontHead));
          cellDetail.setHorizontalAlignment(Element.ALIGN_RIGHT);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          cellDetail.setPaddingBottom(paddingbottom);
          cellDetail.setColspan(srefer);
          cellDetail.setBorder(Rectangle.NO_BORDER);
          detail.addCell(cellDetail);

          // --------------- new line -----------------

          // ------------------ head detail ----------------------

          cellDetail = new PdfPCell(new Phrase(String.format(langthai?"ลำดับ":"No.", x, y),fontHead));
          cellDetail.setHorizontalAlignment(Element.ALIGN_CENTER);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          cellDetail.setRowspan(2);
          detail.addCell(cellDetail);

          cellDetail = new PdfPCell(new Phrase(String.format(langthai?"รหัสพนักงาน":"Employee ID", x, y),fontHead));
          cellDetail.setHorizontalAlignment(Element.ALIGN_CENTER);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          cellDetail.setRowspan(2);
          detail.addCell(cellDetail);

          cellDetail = new PdfPCell(new Phrase(String.format(langthai?"ชื่อ-นามสกุล":"Name-Surname", x, y),fontHead));
          cellDetail.setHorizontalAlignment(Element.ALIGN_CENTER);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          cellDetail.setRowspan(2);
          detail.addCell(cellDetail);

          cellDetail = new PdfPCell(new Phrase(String.format(emp_rec.getField("bu1").getDescription(), x, y),fontHead));
          cellDetail.setHorizontalAlignment(Element.ALIGN_CENTER);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          cellDetail.setRowspan(2);
          detail.addCell(cellDetail);

          cellDetail = new PdfPCell(new Phrase(String.format(emp_rec.getField("bu2").getDescription(), x, y),fontHead));
          cellDetail.setHorizontalAlignment(Element.ALIGN_CENTER);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          cellDetail.setRowspan(2);
          detail.addCell(cellDetail);

          cellDetail = new PdfPCell(new Phrase(String.format(emp_rec.getField("bu3").getDescription(), x, y),fontHead));
          cellDetail.setHorizontalAlignment(Element.ALIGN_CENTER);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          cellDetail.setRowspan(2);
          detail.addCell(cellDetail);

          cellDetail = new PdfPCell(new Phrase(String.format(langthai?"วันที่เริ่มงาน":"Start Date", x, y),fontHead));
          cellDetail.setHorizontalAlignment(Element.ALIGN_CENTER);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          cellDetail.setRowspan(2);
          detail.addCell(cellDetail);

          int span2 = linkmap.size();

          cellDetail = new PdfPCell(new Phrase(String.format(langthai?"วันที่ครบกำหนดทดลองงาน":"Probation Date", x, y),fontHead));
          cellDetail.setHorizontalAlignment(Element.ALIGN_CENTER);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          cellDetail.setColspan(span2);
          detail.addCell(cellDetail);

          // หัวตาราง
          for (Integer key : linkmap.keySet()){
            String headcol = langthai?"ครบ "+linkmap.get(key)+" วัน":linkmap.get(key)+" Day(s)";

            cellDetail = new PdfPCell(new Phrase(String.format(headcol, x, y),fontHead));
            cellDetail.setHorizontalAlignment(Element.ALIGN_CENTER);
            cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
            cellDetail.setPadding(padding);
            cellDetail.setPaddingTop(capHeight - fontSize + padding);
            detail.addCell(cellDetail);
          }
          /*

          cellDetail = new PdfPCell(new Phrase(String.format("ครบ 85 วัน", x, y),fontHead));
          cellDetail.setHorizontalAlignment(Element.ALIGN_CENTER);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          detail.addCell(cellDetail);

          cellDetail = new PdfPCell(new Phrase(String.format("ครบ 90 วัน", x, y),fontHead));
          cellDetail.setHorizontalAlignment(Element.ALIGN_CENTER);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          detail.addCell(cellDetail);

          cellDetail = new PdfPCell(new Phrase(String.format("ครบ 119 วัน", x, y),fontHead));
          cellDetail.setHorizontalAlignment(Element.ALIGN_CENTER);
          cellDetail.setVerticalAlignment (Element.ALIGN_MIDDLE);
          cellDetail.setPadding(padding);
          cellDetail.setPaddingTop(capHeight - fontSize + padding);
          detail.addCell(cellDetail);
          */
          // --------------- new line -----------------

          return detail;
        }catch(Exception e){
          e.printStackTrace();
        }
        return null;
      }
      %>

      <%!
      HelpEntry screen;
      private void setHelpEntry(HelpEntry screen) {
        this.screen = screen;
      }

      private boolean chkBlank(String val) {
        return val.equals("")?true:false;
      }

      private boolean chkZero(String val) {
        return val.equals("0000000")?true:false;
      }

      private String getDescBu(String table, String bu, String buid) {
        String val = "";
        try {
          DbInquiry mbu = new InitialInquiry(screen.getUProfile()).getDbInquiry(table);
          mbu.setColumn("tdesc");
          mbu.clearOldCondition();
          mbu.setFilter(bu + "='"+ buid +"'");
          mbu.refresh();
          if (mbu.next()) {
            val = mbu.getString("tdesc");
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
        return val;
      }

      private String notZero(String val) {
        return Double.parseDouble(val) > 0? val: "";
      }

      private String notZeroTime(String val) {
        return val.equals("00:00:00")? "": val;
      }
      private String getDescPosition(String table, String po) {
        String val = "";
        try {
          DbInquiry mbu = new InitialInquiry(screen.getUProfile()).getDbInquiry(table);
          mbu.setColumn("tdesc");
          mbu.clearOldCondition();
          mbu.setFilter("positionid='"+ po +"'");
          mbu.refresh();
          if (mbu.next()) {
            val = mbu.getString("tdesc");
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
        return val;
      }
      %>
    </body>
  </html>