package com.reallinkbilling.download;

import static java.awt.Color.WHITE;
import static org.apache.pdfbox.pdmodel.font.PDType1Font.HELVETICA;
import static org.vandeseer.easytable.settings.HorizontalAlignment.CENTER;
import static org.vandeseer.easytable.settings.HorizontalAlignment.LEFT;
import static org.vandeseer.easytable.settings.HorizontalAlignment.RIGHT;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.fontbox.ttf.TTFParser;
import org.apache.fontbox.ttf.TrueTypeFont;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;
import org.vandeseer.easytable.TableDrawer;
import org.vandeseer.easytable.structure.Row;
import org.vandeseer.easytable.structure.Table;
import org.vandeseer.easytable.structure.Table.TableBuilder;
import org.vandeseer.easytable.structure.cell.TextCell;

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.DAO.EmployeeAccountDetailsDAO;
import com.reallinkbilling.cipher.EncryptionDecryption;
import com.reallinkbilling.db.GetDBConnection;
import com.reallinkbilling.utils.GetEmpoyeeDetailsById;

public class DownloadEmployeeAccStmtReport extends ActionSupport implements SessionAware{

	

	/**
	 * 
	 */
	private static final long serialVersionUID = -625899965556423051L;
	public HttpServletRequest request;
	public HttpServletResponse response;

	private final static Color BLUE_DARK = new Color(76, 129, 190);
    private final static Color BLUE_LIGHT_1 = new Color(186, 206, 230);
    private final static Color BLUE_LIGHT_2 = new Color(218, 230, 242);

    private final static Color GRAY_LIGHT_1 = new Color(245, 245, 245);
    private final static Color GRAY_LIGHT_2 = new Color(240, 240, 240);
    private final static Color GRAY_LIGHT_3 = new Color(216, 216, 216);
    private static final float PADDING5 = 5f;
    private static final float PADDING10 = 10f;
    private static final float PADDING20 = 20f;
    private static final float PADDING30 = 30f;
    private static final float PADDING40 = 40f;
    private static final float PADDING50 = 50f;

	private HttpSession session;
	
    private String key;
	private String employeeName;
	private String employeeMobile;
	private String defaultValue;
	private String fromDate;
	private String toDate;
	private SessionMap<String,Object> sessionMap;
	
	private Float totalSalaryAmountBeforeDate;
	private Float totalSalaryPaidBeforeDate;
	private Float totalUnpaidSalaryAmountBeforeDate;
	private String hiddenKey;
	private Float totalSalaryAmountAllTheTime;
	private Float totalSalaryAmountPaidAllTheTime;
	private Float totalUnpaidAmountAllTheTime;
	
	private List<EmployeeAccountDetailsDAO> employeeAccountDetailsList = new ArrayList<EmployeeAccountDetailsDAO>();
	private List<EmployeeAccountDetailsDAO> employeeAccountDetailsSortedList = new ArrayList<EmployeeAccountDetailsDAO>();
	
	private Float employeeBalancePending = 0.0F;
	private Float employeeBalanceAdvance = 0.0f;
	private String opeingBalancUpdatedBy;
	private String opeingBalanceUpdateDate;

		
	
	public void download() throws ParseException
	{
		session=ServletActionContext.getRequest().getSession(false); 
		 
        if(session==null || session.getAttribute("login")==null){ 
        	addActionMessage("Please Login First !!!");
        	
        }  
        else{
        	
        	response = ServletActionContext.getResponse();
			request = ServletActionContext.getRequest();


        	Connection connection = null;
			PreparedStatement preparedStatement= null;
			ResultSet resultSet = null;
			
			String sql = "";
			String db = (String) this.getSession().getAttribute("db");
        	
			try
			{
				String shopId = (String) this.getSessionMap().get("shopId");
	    		String shopCode = (String) this.getSessionMap().get("shopCode");
	    		
			
	    		connection = GetDBConnection.getDBConnection(db);
       			
				String employeeId = EncryptionDecryption.decrypt(getKey());
		        
	        	
       			
				PreparedStatement getPendingAdvBalPSMT = null;
   				ResultSet getPendingAdvBalRs = null;
   				
   				String getPendingAdvBalSQL = "SELECT "
   						+ "opening_bal_type,"
   						+ "opening_balance_amount "
   						+ "FROM "
   						+ "shop_employee_salary_details "
   						+ "WHERE "
   						+ "employee_id = ? "
   						+ "AND "
   						+ "shop_id = ? "
   						+ "AND "
   						+ "shop_code = ? ";
   				getPendingAdvBalPSMT = connection.prepareStatement(getPendingAdvBalSQL);
   				
   				getPendingAdvBalPSMT.setString(1, employeeId);
   				getPendingAdvBalPSMT.setString(2, shopId);
   				getPendingAdvBalPSMT.setString(3, shopCode);
   				
   				
   				getPendingAdvBalRs = getPendingAdvBalPSMT.executeQuery();
   				
   				if(getPendingAdvBalRs.next())
   				{
   					if(getPendingAdvBalRs.getString(1).equals("ADVANCE"))
   					{
   						this.setEmployeeBalanceAdvance(getPendingAdvBalRs.getFloat(2));
   						this.setEmployeeBalancePending(0.0F);
   					}
   					else
   					{
   						this.setEmployeeBalanceAdvance(0.0F);
   						this.setEmployeeBalancePending(getPendingAdvBalRs.getFloat(2));
   					}
   					
   				
   				}
   				else
   				{
   					this.setEmployeeBalanceAdvance(0.0F);
						this.setEmployeeBalancePending(0.0F);
   				}
   				if(getPendingAdvBalRs != null)
   				{
   					getPendingAdvBalRs.close();
   					getPendingAdvBalRs = null;
   				}
   				if(getPendingAdvBalPSMT != null)
   				{
   					getPendingAdvBalPSMT.close();
   					getPendingAdvBalPSMT = null;
   				}
   				
       			
       			
       			sql = "SELECT "
       					+ "SUM(payment_amount) "
       					+ "FROM "
       					+ "shop_employee_payment_details "
       					+ "WHERE "
       					+ "employee_id = ? "
       					+ "and "
       					+ "payment_date < STR_TO_DATE(?, '%d-%m-%Y') "
       					+ "AND "
       					+ "shop_id = ? "
       					+ "AND "
       					+ "shop_code = ? ";
       			
       			preparedStatement = connection.prepareStatement(sql);
       			preparedStatement.setString(1, employeeId);
       			preparedStatement.setString(2, this.getFromDate());
       			preparedStatement.setString(3, shopId);
       			preparedStatement.setString(4, shopCode);
       			
       			resultSet = preparedStatement.executeQuery();
       			
       			if(resultSet.next())
       			{
       				if(resultSet.getString(1) != null)
       				{
       					this.setTotalSalaryPaidBeforeDate(resultSet.getFloat(1));
       				}
       				else
       				{
       					this.setTotalSalaryPaidBeforeDate(0.0F);
       				}
       			}
       			else
       			{
       				this.setTotalSalaryPaidBeforeDate(0.0F);
       			}
       			
       			if(resultSet != null)
       			{
       				resultSet.close();
       				resultSet = null;
       			}
       			if(preparedStatement != null)
       			{
       				preparedStatement.close();
       				preparedStatement = null;
       			}
       			
       			
       			float floatSalaryAmt = 0.0F;
   				
   				float totalSalaryAmountPaid = this.getTotalSalaryPaidBeforeDate();
   				float totalSalaryAmountBeforeDate = 0.0F;
   				
   				
   				sql = "SELECT "
   						+ "total_calculated_salary_amount "
   						+ "FROM "
   						+ "shop_employee_salary_calculation_details "
   						+ "WHERE "
       					+ "employee_id = ? "
       					+ "AND "
       					+ "salary_to_date < STR_TO_DATE(?, '%d-%m-%Y') "
	       				+ "AND "
	   					+ "shop_id = ? "
	   					+ "AND "
	   					+ "shop_code = ? ";
       			
       			preparedStatement = connection.prepareStatement(sql);
       			preparedStatement.setString(1, employeeId);
       			preparedStatement.setString(2, this.getFromDate());
       			preparedStatement.setString(3, shopId);
       			preparedStatement.setString(4, shopCode);
       			
       			resultSet = preparedStatement.executeQuery();
       			
       			while(resultSet.next())
       			{
       				
       				try
       				{
       					floatSalaryAmt = resultSet.getFloat(1);
       					totalSalaryAmountBeforeDate = totalSalaryAmountBeforeDate + floatSalaryAmt;
       					
       				}
       				catch(Exception e)
       				{
       					
       					addActionError("One Of the Entry invoice could not be processed!");
       				}
       				
       			}
       			if(!hasActionErrors())
       			{
       				this.setTotalSalaryAmountBeforeDate(totalSalaryAmountBeforeDate);
       			
       				if(resultSet != null)
       				{
       					resultSet.close();
       					resultSet = null;
       				}
       				
       				if(preparedStatement != null)
       				{
       					preparedStatement.close();
       					preparedStatement = null;
       				}
       				
       				
       				sql = "SELECT "
       						+ "entry_id,"
       						+ "employee_id,"
       						+ "DATE_FORMAT(payment_date, '%d/%m/%Y'),"
       					    + "payment_amount,"
       			            + "payment_details "
       						+ "FROM "
       						+ "shop_employee_payment_details "
       						+ "WHERE "
       						+ "employee_id = ?"
		       				+ "AND "
	       					+ "(payment_date BETWEEN "
		   					+ "STR_TO_DATE(?, '%d-%m-%Y') "
		   					+ "AND "
		   					+ "STR_TO_DATE(?, '%d-%m-%Y')) "
		   					+ "AND "
		   					+ "shop_id = ? "
		   					+ "AND "
		   					+ "shop_code = ? ";
       				
       				preparedStatement = connection.prepareStatement(sql);
       				
       				preparedStatement.setString(1, employeeId);
       				preparedStatement.setString(2, this.getFromDate());
       				preparedStatement.setString(3, this.getToDate());
       				preparedStatement.setString(4, shopId);
	       			preparedStatement.setString(5, shopCode);
       			
       				resultSet = preparedStatement.executeQuery();
       				
       				while(resultSet.next())
       				{
       					Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(resultSet.getString(3)); 
    	       			
       				
       					EmployeeAccountDetailsDAO dao = new EmployeeAccountDetailsDAO();
       					dao.setEntryId(EncryptionDecryption.encrypt(resultSet.getString(1)));
       					dao.setDate(resultSet.getString(3));
       					dao.setEntryDate(date1);
       					dao.setSalaryAmount(0.0F);
       					dao.setAmount(resultSet.getFloat(4));	       					
       					dao.setAmountType("PAYMENT");
       					dao.setDueAmount(0.0F);
       				    dao.setComments(resultSet.getString(5));
       					dao.setSalaryFromDate("-");
       					dao.setSalaryToDate("-");
       					this.getEmployeeAccountDetailsList().add(dao);
       					
       				}
       				
       				if(resultSet != null)
       				{
       					resultSet.close();
       					resultSet = null;
       				}
       				
       				if(preparedStatement != null)
       				{
       					preparedStatement.close();
       					preparedStatement = null;
       				}
       				
       				sql = "SELECT "
       						+ "entry_id,"
       						+ "DATE_FORMAT(salary_from_date,'%d/%m/%Y'),"
       						+ "DATE_FORMAT(salary_to_date,'%d/%m/%Y'),"
       						+ "total_calculated_salary_amount,"
       						+ "comments "
       						+ "FROM "
       						+ "shop_employee_salary_calculation_details "
       						+ "WHERE "
       						+ "employee_id = ? "
       						+ "AND "
	       					+ "(salary_to_date BETWEEN "
		   					+ "STR_TO_DATE(?, '%d-%m-%Y') "
		   					+ "AND "
		   					+ "STR_TO_DATE(?, '%d-%m-%Y')) "
		   					+ "AND "
		   					+ "shop_id = ? "
		   					+ "AND "
		   					+ "shop_code = ? ";
       				
       				preparedStatement = connection.prepareStatement(sql);
       				
       				preparedStatement.setString(1, employeeId);
       				preparedStatement.setString(2, this.getFromDate());
       				preparedStatement.setString(3, this.getToDate());
       				preparedStatement.setString(4, shopId);
       				preparedStatement.setString(5, shopCode);
       				
       				
       				resultSet = preparedStatement.executeQuery();
       				while(resultSet.next())
       				{
       					Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(resultSet.getString(3)); 
    	       			
	       				
       					EmployeeAccountDetailsDAO dao = new EmployeeAccountDetailsDAO();
       					dao.setEntryId(EncryptionDecryption.encrypt(resultSet.getString(1)));
       					dao.setDate(resultSet.getString(2) + " - TO -"+resultSet.getString(3));
       					dao.setSalaryFromDate(resultSet.getString(2));
       					dao.setSalaryToDate(resultSet.getString(3));
       					dao.setEntryDate(date1);
       					dao.setSalaryAmount(resultSet.getFloat(4));
       					dao.setAmount(0.0F);	       					
       					dao.setAmountType("SALARY");
       					dao.setComments(resultSet.getString(5));
       					dao.setDueAmount(0.0F);
       					this.getEmployeeAccountDetailsList().add(dao);
       					
       				}
       				
       				
       		
       				if(!hasActionErrors())
       				{
       					

						 
						 Collections.sort(this.getEmployeeAccountDetailsList(), new Comparator<EmployeeAccountDetailsDAO>() {
							  public int compare(EmployeeAccountDetailsDAO o1, EmployeeAccountDetailsDAO o2) {
							      if (o1.getEntryDate() == null || o2.getEntryDate() == null)
							        return 0;
							      return o1.getEntryDate().compareTo(o2.getEntryDate());
							  }
							});
						 
						
						 float dueAmountBeforeDates = this.getTotalSalaryAmountBeforeDate() - this.getTotalSalaryPaidBeforeDate();
						 float currentSalaryAmtDue = 0.0F;
					
						this.setTotalUnpaidSalaryAmountBeforeDate(dueAmountBeforeDates);
					if(this.getEmployeeBalanceAdvance() > 0)
					{
						currentSalaryAmtDue = dueAmountBeforeDates - this.getEmployeeBalanceAdvance();
					} 
					else if(this.getEmployeeBalancePending() > 0 )
					{
						currentSalaryAmtDue = dueAmountBeforeDates + this.getEmployeeBalancePending(); 
					}
					 else
					 {
						currentSalaryAmtDue = dueAmountBeforeDates;
					 }
   						      
						for(EmployeeAccountDetailsDAO dao : this.getEmployeeAccountDetailsList())
       				{
							if(dao.getAmountType().equals("SALARY"))
							{
								dao.setDueAmount(currentSalaryAmtDue + dao.getSalaryAmount());
								
								currentSalaryAmtDue = currentSalaryAmtDue + dao.getSalaryAmount();
							}
							else if(dao.getAmountType().equals("PAYMENT"))
							{
								dao.setDueAmount(currentSalaryAmtDue - dao.getAmount());
								currentSalaryAmtDue = currentSalaryAmtDue -  dao.getAmount();
							}
       				}
						
		       			HashMap<String, String> employeeDetails = GetEmpoyeeDetailsById.getEmployeeDetails(employeeId, this.getSessionMap(), connection);
	       				
		       			
					
					DecimalFormat df = new DecimalFormat("##.##");
					
	    			String path=	request.getSession().getServletContext().getRealPath("/images/");
	    			PDDocument document = new PDDocument(); 
					PDPage page = null;
					
					
					Date date = new Date();
					SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-YYYY");
		        	
		        	
					String dateToday = sdf.format(date); 
						 page = new PDPage(PDRectangle.A4);
					
					
				        document.addPage(page);
				         PDPageContentStream contentStream = new PDPageContentStream(document, page);
				         int pageCount = 1;
				         
				         
				    	 File fontFile  =  new File( request.getSession().getServletContext().getRealPath("/fonts/Nirmala.ttf"));
						 
						 TrueTypeFont ttf1 = new TTFParser().parse(fontFile);
						 PDFont fontMarathi = PDType0Font.load(document, ttf1, true);
					 
			    			 Table header;
			    			
			    			  TableBuilder tableHeaderBuilder = Table.builder()
						                .addColumnsOfWidth(585)				                
						                .font(HELVETICA)
						                .borderColor(Color.BLACK)
						                .horizontalAlignment(CENTER);
					         
					          Row headerName = Row.builder()
						                .add(TextCell.builder().text((String)this.getSessionMap().get("shopnameenglish")).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(10F).build())				               
						                .backgroundColor(WHITE)
						                .textColor(Color.BLACK)
						                .font(PDType1Font.HELVETICA_BOLD).fontSize(20)				              
						                .horizontalAlignment(CENTER)
						                .build();
					          
					          
					          Row info1 = Row.builder()
						                .add(TextCell.builder().text("Employee Account Statment: From Date: "
						                			+this.getFromDate()+" To Date : "+this.getToDate() +"       Page No . : "+pageCount).borderWidthBottom(0.0F).horizontalAlignment(CENTER).build())				               
						                .backgroundColor(WHITE)
						                .textColor(Color.BLACK)
						                .font(PDType1Font.HELVETICA_BOLD).fontSize(10)
						                .horizontalAlignment(CENTER)
						                .build();
					          
					          Row info2 = Row.builder()
						                .add(TextCell.builder().text("Employee Name: "
						                		+employeeDetails.get("EMPLOYEENAMEENG")
						                		+", Generated On : "+dateToday).borderWidthBottom(0.0F).horizontalAlignment(CENTER).build())				               
						                .backgroundColor(WHITE)
						                .textColor(Color.BLACK)
						                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)
						                .horizontalAlignment(CENTER)
						                .build();
					          
					          
					          
					          Row beforeDateRow = Row.builder()
						                .add(TextCell.builder().text("Salary Before date: "
						                		+this.getTotalSalaryAmountBeforeDate()
						                		+", Salary Paid Before Date : "+this.getTotalSalaryPaidBeforeDate()
						                		+", Salary balance Before Date : "+this.getTotalUnpaidSalaryAmountBeforeDate()).borderWidthBottom(0.0F).horizontalAlignment(CENTER).build())				               
						                .backgroundColor(WHITE)
						                .textColor(Color.BLACK)
						                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)
						                .horizontalAlignment(CENTER)
						                .build();
					         
					          
					          Row pendingAdvRow = Row.builder()
						                .add(TextCell.builder().text("Peding Amount "
						                		+this.getEmployeeBalancePending()
						                		+", Advance Amount : "+this.getEmployeeBalanceAdvance()).borderWidthBottom(0.0F).horizontalAlignment(CENTER).build())				               
						                .backgroundColor(WHITE)
						                .textColor(Color.BLACK)
						                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)
						                .horizontalAlignment(CENTER)
						                .build();
					         
					          
					          tableHeaderBuilder.addRow(headerName);
					          tableHeaderBuilder.addRow(info1);
					          tableHeaderBuilder.addRow(info2);
					          tableHeaderBuilder.addRow(beforeDateRow);
					          tableHeaderBuilder.addRow(pendingAdvRow);
					          header = tableHeaderBuilder.build();
					          
					          
					          Table stmtDetails;
				    			 
				    			
					          
					          TableBuilder tableStmtDetailsBuilder = Table.builder()
					        		  	.addColumnsOfWidth(65,65,65,80,80,80,150)				                
						                .font(HELVETICA)
						                .borderWidth(0.5F)
						                .borderColor(Color.BLACK)
						                .horizontalAlignment(CENTER);
					          
					          Row tableHeader = Row.builder()
					        		  	.add(TextCell.builder().text("Entry Date").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
						                .add(TextCell.builder().text("From Date").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
						                .add(TextCell.builder().text("TO Date").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
						                .add(TextCell.builder().text("Salary").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
						                .add(TextCell.builder().text("Amount Paid").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
						                .add(TextCell.builder().text("Balance/Due").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
						                .add(TextCell.builder().text("Comments").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())									                
						                .backgroundColor(Color.LIGHT_GRAY)
						                .textColor(Color.BLACK)
						                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)				              
						                .horizontalAlignment(CENTER)
						                .build();
					          tableStmtDetailsBuilder.addRow(tableHeader);
					       
					        
					     
					        	  int size = this.getEmployeeAccountDetailsList().size();
					        	  
					        	  
						          float startY = page.getMediaBox().getHeight() - PADDING20;
						          float currentY = startY;
						          TableDrawer.builder()
					                .contentStream(contentStream)
					                .table(header)
					                .startX(PADDING30)
					                .startY(startY)
					                .build()
					                .draw();
						          
						          for(int s=0;s<size;s++)
					        	  {
					        		  
					        		  if(currentY < 120)
					        		  {
					        			  
					        			  pageCount++;
					        			  stmtDetails = tableStmtDetailsBuilder.build();
					        			  TableDrawer.builder()
							                .contentStream(contentStream)
							                .table(stmtDetails)
							                .startX(PADDING5)
							                .startY(startY-50)
							                .build()
							                .draw();
					        			  
					        			  contentStream.close();
					        			  
					        			  header = null;
					        			  
					        			 
							    			 
							    			
							    			  tableHeaderBuilder = Table.builder()
										                .addColumnsOfWidth(585)				                
										                .font(HELVETICA)
										                .borderColor(Color.BLACK)
										                .horizontalAlignment(CENTER);
									         
									           headerName = Row.builder()
										                .add(TextCell.builder().text((String)this.getSessionMap().get("shopnameenglish")).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(10F).build())				               
										                .backgroundColor(WHITE)
										                .textColor(Color.BLACK)
										                .font(PDType1Font.HELVETICA_BOLD).fontSize(20)				              
										                .horizontalAlignment(CENTER)
										                .build();
									          
									          
										          
									           		info1 = Row.builder()
										                .add(TextCell.builder().text("Employee Account Statment: From Date: "
										                			+this.getFromDate()+" To Date : "+this.getToDate() +"       Page No . : "+pageCount).borderWidthBottom(0.0F).horizontalAlignment(CENTER).build())				               
										                .backgroundColor(WHITE)
										                .textColor(Color.BLACK)
										                .font(PDType1Font.HELVETICA_BOLD).fontSize(10)
										                .horizontalAlignment(CENTER)
										                .build();
										          
									           		info2 = Row.builder()
											                .add(TextCell.builder().text("Employee Name: "
											                		+employeeDetails.get("EMPLOYEENAMEENG")
											                		+", Generated On : "+dateToday).borderWidthBottom(0.0F).horizontalAlignment(CENTER).build())				               
											                .backgroundColor(WHITE)
											                .textColor(Color.BLACK)
											                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)
											                .horizontalAlignment(CENTER)
											                .build();
										         
										         
									          
									          tableHeaderBuilder.addRow(headerName);
									          tableHeaderBuilder.addRow(info1);
									          tableHeaderBuilder.addRow(info2);
									          header = tableHeaderBuilder.build();
					        			  
					        			  stmtDetails = null;
					        			   tableStmtDetailsBuilder = Table.builder()
					        					    .addColumnsOfWidth(65,65,65,80,80,80,150)				                
									                .font(HELVETICA)
									                .borderWidth(0.5F)
									                .borderColor(Color.BLACK)
									                .horizontalAlignment(CENTER);
					        			      
					        			   tableHeader = Row.builder()
					        					 	.add(TextCell.builder().text("Entry Date").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
									                .add(TextCell.builder().text("From Date").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
									                .add(TextCell.builder().text("T0 Date").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
									                .add(TextCell.builder().text("Salary").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
									                .add(TextCell.builder().text("Amount Paid").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
									                .add(TextCell.builder().text("Balance/Due").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
									                .add(TextCell.builder().text("Comments").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())									                
									                .backgroundColor(Color.LIGHT_GRAY)
										                .textColor(Color.BLACK)
										                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)				              
										                .horizontalAlignment(CENTER)
										                .build();
									          tableStmtDetailsBuilder.addRow(tableHeader);
								          
					        			  
					        				PDPage newPage = new PDPage(PDRectangle.A4);
								        	 contentStream = new PDPageContentStream(document,newPage);
								        	currentY =  newPage.getMediaBox().getHeight() - PADDING20;
								             document.addPage( newPage );
								             
								             TableDrawer.builder()
								                .contentStream(contentStream)
								                .table(header)
								                .startX(PADDING30)
								                .startY(currentY)
								                .build()
								                .draw();
					        		  }
					        		  
					        		  
					        		  EmployeeAccountDetailsDAO dao = this.getEmployeeAccountDetailsList().get(s);
					        		
					        		  if(dao.getComments() == null)
					        		  {
					        			  dao.setComments("");
					        		  }
					        		  
							          Row infoRow = Row.builder()
							        		  	.add(TextCell.builder().text(dao.getDate()).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
								                .add(TextCell.builder().text(dao.getSalaryFromDate()).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
								                .add(TextCell.builder().text(dao.getSalaryToDate()).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
								                .add(TextCell.builder().text(df.format(dao.getSalaryAmount())).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				                
								                .add(TextCell.builder().text(df.format(dao.getAmount())).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())
								                .add(TextCell.builder().text(df.format(dao.getDueAmount())).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())
								                .add(TextCell.builder().text(dao.getComments()).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())									                
								                .backgroundColor(Color.WHITE)
								                .textColor(Color.BLACK)
								                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)				              
								                .horizontalAlignment(CENTER)
								                .build();
							      	  
						        	  tableStmtDetailsBuilder.addRow(infoRow);
						        	 
						        	  currentY = currentY -20;
					        	  }
						          
						   
						          stmtDetails = tableStmtDetailsBuilder.build();
						          
						          TableDrawer.builder()
					                .contentStream(contentStream)
					                .table(stmtDetails)
					                .startX(PADDING5)
					                .startY(startY-90)
					                .build()
					                .draw();
						          contentStream.close();
							          
						          
						          document.save(path+"/labour-acc-stmt.pdf");
							        document.close();
							        
							        response.setHeader("Content-disposition", "inline; filename=emp-acc-stmt"+employeeDetails.get("EMPLOYEENAMEENG")+".pdf");
									response.setContentType("application/pdf");
									
									FileInputStream fileInputStream = new FileInputStream(path+"/labour-acc-stmt.pdf");
									PrintWriter out = response.getWriter();  
									int bytes;
									while ((bytes = fileInputStream.read()) != -1) {
										out.write(bytes);
									}
									fileInputStream.close();
									out.flush();
									out.close();
									ttf1.close();
       				}
       			}
						          
				
			}catch (SQLException e) {
				
				addActionError(e.getErrorCode()+" || "+e.getMessage() +" || Account details not found!");
			} catch (IOException e) {
				
				addActionError(e.getMessage()+" || "+e.getCause()+" || Account details not found !");
			}finally {
				if(resultSet != null)
				{
					try {resultSet.close();} catch (SQLException e) {}
				}
				if(preparedStatement != null)
				{
					try {preparedStatement.close();} catch (SQLException e) {}
				}
				if(connection != null)
				{
					
					try {connection.close();}catch (SQLException e) {}
				}
				
			}
        }
	}


	public HttpServletRequest getRequest() {
		return request;
	}


	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}


	public HttpServletResponse getResponse() {
		return response;
	}


	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}


	public HttpSession getSession() {
		return session;
	}


	public void setSession(HttpSession session) {
		this.session = session;
	}


	public String getFromDate() {
		return fromDate;
	}


	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}


	public String getToDate() {
		return toDate;
	}


	public void setToDate(String toDate) {
		this.toDate = toDate;
	}


	


	public String getKey() {
		return key;
	}


	public void setKey(String key) {
		this.key = key;
	}


	


	public String getDefaultValue() {
		return defaultValue;
	}


	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}


	public Float getTotalSalaryAmountBeforeDate() {
		return totalSalaryAmountBeforeDate;
	}


	public void setTotalSalaryAmountBeforeDate(Float totalSalaryAmountBeforeDate) {
		this.totalSalaryAmountBeforeDate = totalSalaryAmountBeforeDate;
	}


	public Float getTotalSalaryPaidBeforeDate() {
		return totalSalaryPaidBeforeDate;
	}


	public void setTotalSalaryPaidBeforeDate(Float totalSalaryPaidBeforeDate) {
		this.totalSalaryPaidBeforeDate = totalSalaryPaidBeforeDate;
	}


	public Float getTotalUnpaidSalaryAmountBeforeDate() {
		return totalUnpaidSalaryAmountBeforeDate;
	}


	public void setTotalUnpaidSalaryAmountBeforeDate(Float totalUnpaidSalaryAmountBeforeDate) {
		this.totalUnpaidSalaryAmountBeforeDate = totalUnpaidSalaryAmountBeforeDate;
	}


	public String getHiddenKey() {
		return hiddenKey;
	}


	public void setHiddenKey(String hiddenKey) {
		this.hiddenKey = hiddenKey;
	}


	public Float getTotalSalaryAmountAllTheTime() {
		return totalSalaryAmountAllTheTime;
	}


	public void setTotalSalaryAmountAllTheTime(Float totalSalaryAmountAllTheTime) {
		this.totalSalaryAmountAllTheTime = totalSalaryAmountAllTheTime;
	}


	public Float getTotalSalaryAmountPaidAllTheTime() {
		return totalSalaryAmountPaidAllTheTime;
	}


	public void setTotalSalaryAmountPaidAllTheTime(Float totalSalaryAmountPaidAllTheTime) {
		this.totalSalaryAmountPaidAllTheTime = totalSalaryAmountPaidAllTheTime;
	}


	public Float getTotalUnpaidAmountAllTheTime() {
		return totalUnpaidAmountAllTheTime;
	}


	public void setTotalUnpaidAmountAllTheTime(Float totalUnpaidAmountAllTheTime) {
		this.totalUnpaidAmountAllTheTime = totalUnpaidAmountAllTheTime;
	}


	

	public String getEmployeeName() {
		return employeeName;
	}


	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}


	public String getEmployeeMobile() {
		return employeeMobile;
	}


	public void setEmployeeMobile(String employeeMobile) {
		this.employeeMobile = employeeMobile;
	}


	public List<EmployeeAccountDetailsDAO> getEmployeeAccountDetailsList() {
		return employeeAccountDetailsList;
	}


	public void setEmployeeAccountDetailsList(List<EmployeeAccountDetailsDAO> employeeAccountDetailsList) {
		this.employeeAccountDetailsList = employeeAccountDetailsList;
	}


	public List<EmployeeAccountDetailsDAO> getEmployeeAccountDetailsSortedList() {
		return employeeAccountDetailsSortedList;
	}


	public void setEmployeeAccountDetailsSortedList(List<EmployeeAccountDetailsDAO> employeeAccountDetailsSortedList) {
		this.employeeAccountDetailsSortedList = employeeAccountDetailsSortedList;
	}


	public Float getEmployeeBalancePending() {
		return employeeBalancePending;
	}


	public void setEmployeeBalancePending(Float employeeBalancePending) {
		this.employeeBalancePending = employeeBalancePending;
	}


	public Float getEmployeeBalanceAdvance() {
		return employeeBalanceAdvance;
	}


	public void setEmployeeBalanceAdvance(Float employeeBalanceAdvance) {
		this.employeeBalanceAdvance = employeeBalanceAdvance;
	}


	public String getOpeingBalancUpdatedBy() {
		return opeingBalancUpdatedBy;
	}


	public void setOpeingBalancUpdatedBy(String opeingBalancUpdatedBy) {
		this.opeingBalancUpdatedBy = opeingBalancUpdatedBy;
	}


	public String getOpeingBalanceUpdateDate() {
		return opeingBalanceUpdateDate;
	}


	public void setOpeingBalanceUpdateDate(String opeingBalanceUpdateDate) {
		this.opeingBalanceUpdateDate = opeingBalanceUpdateDate;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}


public SessionMap<String, Object> getSessionMap() {
return sessionMap;
}

public void setSessionMap(SessionMap<String, Object> sessionMap) {
this.sessionMap = sessionMap;
}

public void setSession(Map<String, Object> map) {  
sessionMap=(SessionMap<String, Object>)map;  
}
	
	
}

