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.FileInputStream;
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.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.PDType1Font;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
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.reallinkbilling.DAO.ItemWiseStockDetailsDAO;
import com.reallinkbilling.db.GetDBConnection;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DownloadStockReport extends ActionSupport implements SessionAware,ServletRequestAware,ServletResponseAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2231894051386352138L;

		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 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 SessionMap<String,Object> sessionMap;

	private HttpServletRequest request = null;
	private HttpServletResponse response = null;
	
	private String stockType = "ALL";
	
	private List<ItemWiseStockDetailsDAO> productStockList = new ArrayList<ItemWiseStockDetailsDAO>();
	public void downloadStockReport() throws Exception {
		
		//System.out.println("Bill Number : "+billNumber);
		
				Connection connection = null;
				PreparedStatement preparedStatement= null;
				ResultSet resultSet = null;
				
				String sql = "";
				
				Map<String, Object> session = ActionContext.getContext().getSession();
				
				String db = (String) session.get("db");
				try
	        	{
					HashMap<String, String> settings = (HashMap<String, String>) this.getSessionMap().get("shop_settings");
			        boolean addQuantity2 = false;
		        	if(settings.get("add_quntity2").equals("TRUE"))
		        	{
		        		addQuantity2 = true;
		        	}
		        
	        	
					DecimalFormat df = new DecimalFormat("##");
		        	float totalCustSaleVal = 0.0F;
		        	float totalPurchaseVal = 0.0F;
		        	float totalDealerSaleVal = 0.0F;
		        	float totalRetailSaleVal = 0.0F;
		        	
					String shopId = (String) this.getSessionMap().get("shopId");
		    		String shopCode = (String) this.getSessionMap().get("shopCode");
		    		
		    		connection = GetDBConnection.getDBConnection(db);
		    	
		    		sql = "SELECT "
		    		        + "product_name_eng,"
		    		        + "product_name_mar,"
		    		        + "product_alias,"
		    		        + "low_stock_count,"
		    		        + "in_stock_quantity,"
		    		        + "in_stock_quantity_2,"
		    		        + "purchase_rate,"
		    		        + "customer_sales_price,"
		    		        + "dealer_sales_price,"
		    		        + "retailer_sales_price,"
		    		        + "stock_conversion_ratio "
		    		        + "FROM shop_product_master "
		    		        + "WHERE shop_id = ? "
		    		        + "AND shop_code = ? ";



if ("AVAILABLE STOCK".equalsIgnoreCase(stockType)) {
    sql += "AND in_stock_quantity > 0 ";
} else if ("ZERO STOCK".equalsIgnoreCase(stockType)) {
    sql += "AND in_stock_quantity = 0 ";
}


					sql += "ORDER BY product_name_eng";


		    		//System.out.println("Download Stock Type: " + stockType);
		    		//System.out.println("Download Stock SQL: " + sql);

		    		preparedStatement = connection.prepareStatement(sql);
		    		preparedStatement.setString(1, shopId);
		    		preparedStatement.setString(2, shopCode);

		    		resultSet = preparedStatement.executeQuery();
		    		
					
		    		
		    		while (resultSet.next()) {

		    		    ItemWiseStockDetailsDAO detailsDAO =
		    		            new ItemWiseStockDetailsDAO();

		    		    float inStockQty = resultSet.getFloat(5);
		    		    float stockConversionRatio = resultSet.getFloat(11);
		    		    float purchaseRate = resultSet.getFloat(7);

		    		    float purchaseValue = 0.0F;

		    		    if (stockConversionRatio != 0) {
		    		        purchaseValue =
		    		                (inStockQty / stockConversionRatio) * purchaseRate;
		    		    }

		    		    detailsDAO.setProductNameEn(resultSet.getString(1));
		    		    detailsDAO.setProductNameMr(resultSet.getString(2));
		    		    detailsDAO.setAlias(resultSet.getString(3));
		    		    detailsDAO.setLowStockCount(resultSet.getString(4));
		    		    detailsDAO.setStock(df.format(inStockQty));
		    		    detailsDAO.setStock2(df.format(resultSet.getFloat(6)));
		    		    detailsDAO.setPurchaseValue(df.format(purchaseValue));

		    		    detailsDAO.setCustSaleValue(
		    		            df.format(inStockQty * resultSet.getFloat(8)));

		    		    detailsDAO.setDealerSaleValue(
		    		            df.format(inStockQty * resultSet.getFloat(9)));

		    		    detailsDAO.setRetailerSaleValue(
		    		            df.format(inStockQty * resultSet.getFloat(10)));

		    		    totalPurchaseVal += purchaseValue;
		    		    totalCustSaleVal += inStockQty * resultSet.getFloat(8);
		    		    totalDealerSaleVal += inStockQty * resultSet.getFloat(9);
		    		    totalRetailSaleVal += inStockQty * resultSet.getFloat(10);

		    		    productStockList.add(detailsDAO);
		    		}
		    		
					
					
					if(this.getProductStockList().size()>0)
					{ 
						if(!addQuantity2)
						{
						
						try
						{
							String name = (String) this.getSessionMap().get("shopnameenglish");
							String address = (String) this.getSessionMap().get("shopaddresseng");
							String path= this.getRequest().getSession().getServletContext().getRealPath("/images/");
							
							
							 java.util.Date date = new java.util.Date();
				        	SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
				        	String reportDate = dateFormat.format(date);
						
				        	
				       	     long t = date.getTime();	        	     
				       	     java.sql.Time time = new java.sql.Time(t);
				       	     
							PDDocument document = new PDDocument(); 
							PDPage page = null;
							
							 page = new PDPage(PDRectangle.A4);
							
							 
							 document.addPage(page);
							 
							 
					         PDPageContentStream contentStream = new PDPageContentStream(document, page);
							 
					         int pageCount = 1;
					         
					         Table header;
					         
					         
					         
					         
					         TableBuilder tableHeaderBuilder = Table.builder()
						                .addColumnsOfWidth(550)				                
						                .font(HELVETICA)
						                .borderColor(Color.BLACK)
						                .horizontalAlignment(CENTER);
					         
					         Row dairyNameHeader = Row.builder()
						                .add(TextCell.builder().text(name).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
						                .backgroundColor(WHITE)
						                .textColor(Color.BLACK)
						                .font(PDType1Font.HELVETICA_BOLD).fontSize(16)				              
						                .horizontalAlignment(CENTER)
						                .build();
					         
					         Row addressRow = Row.builder()
						                .add(TextCell.builder().text(address).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
						                .backgroundColor(WHITE)
						                .textColor(Color.BLACK)
						                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)				              
						                .horizontalAlignment(CENTER)
						                .build();
					         
					         
					         Row reportInfo1 = Row.builder()
						                .add(TextCell.builder()
						                		.text("Stock Details Report On : "+reportDate+" "+time)
						                		.horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
						                .backgroundColor(WHITE)
						                .textColor(Color.BLACK)
						                .font(PDType1Font.HELVETICA_BOLD).fontSize(12)				              
						                .horizontalAlignment(CENTER)
						                .build();
					         
					         
					         
					         
					         tableHeaderBuilder.addRow(dairyNameHeader);
					         tableHeaderBuilder.addRow(addressRow);
					         tableHeaderBuilder.addRow(reportInfo1);
					        
					         
					         header = tableHeaderBuilder.build();
					         
					         float startY = page.getMediaBox().getHeight() - PADDING20;
					         TableDrawer.builder()
				                .contentStream(contentStream)
				                .table(header)
				                .startX(PADDING30)
				                .startY(startY)
				                .build()
				                .draw();
							
					         
					         Table stockInfoRowTable = null;
					         
					         TableBuilder tableStockInfoBuilder = Table.builder()
						                .addColumnsOfWidth(40,200,65,65,65,65,65)				                
						                .font(HELVETICA)
						                .borderWidth(0.5F)
						                .borderColor(Color.BLACK)
						                .horizontalAlignment(CENTER);
					         
					         
					         Row stockTableHeader = Row.builder()
					        		  	.add(TextCell.builder().text("SrNo").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())					              
						                .add(TextCell.builder().text("Product Name").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())				               
						                .add(TextCell.builder().text("In Stock").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())
							            .add(TextCell.builder().text("Purchase Value").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
						                .add(TextCell.builder().text("Customer Sale Value").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())
						                .add(TextCell.builder().text("Retailer Sale Value").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())
						                .add(TextCell.builder().text("Dealer Sale Value").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())
						                .backgroundColor(Color.LIGHT_GRAY)
						                .textColor(Color.BLACK)
						                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)				              
						                .horizontalAlignment(CENTER)
						                .build();
					         
					         tableStockInfoBuilder.addRow(stockTableHeader);
					        
					         stockInfoRowTable = tableStockInfoBuilder.build();
					        		
					        		
					        		 float currentY =  75.0F;
					        		 
					        		 int srNo = 1;
					        		 
					        		 for(ItemWiseStockDetailsDAO detailsDAO :  this.getProductStockList())
					        		 {

					        			
							        	 
							        	 
								         if(currentY > 750)
								         {
								        	 
								        	 stockInfoRowTable = tableStockInfoBuilder.build();
						        			 
						        			 
						        			 TableDrawer.builder()
								                .contentStream(contentStream)
								                .table(stockInfoRowTable)
								                .startX(PADDING20)
								                .startY(startY-50)
								                .build()
								                .draw();
							         
						        			 stockInfoRowTable = null;
						        			 tableStockInfoBuilder = null;
						        			 tableStockInfoBuilder = Table.builder()
										                .addColumnsOfWidth(40,200,65,65,65,65,65)				                
										                .font(HELVETICA)
										                .borderWidth(0.5F)
										                .borderColor(Color.BLACK)
										                .horizontalAlignment(CENTER);
						        			 
						        			 
								        	 
								        	 pageCount++;
								        	 contentStream.close();
								        	PDPage newPage = new PDPage(PDRectangle.A4);
								        	 contentStream = new PDPageContentStream(document,newPage);
								        	currentY = 20.0F;
								             document.addPage( newPage );
								             
								             
								             
								             Table headerOtherPage;
								             
								             
								             TableBuilder tableHeaderBuilderOtherPage = Table.builder()
										                .addColumnsOfWidth(550)				                
										                .font(HELVETICA)
										                .borderColor(Color.BLACK)
										                .horizontalAlignment(CENTER);
									         
									         Row dairyNameHeaderOtherPage = Row.builder()
										                .add(TextCell.builder().text(name).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
										                .backgroundColor(WHITE)
										                .textColor(Color.BLACK)
										                .font(PDType1Font.HELVETICA_BOLD).fontSize(16)				              
										                .horizontalAlignment(CENTER)
										                .build();
									         
									         Row addressRowOtherPage = Row.builder()
										                .add(TextCell.builder().text(address).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
										                .backgroundColor(WHITE)
										                .textColor(Color.BLACK)
										                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)				              
										                .horizontalAlignment(CENTER)
										                .build();
									         
									         
									         Row reportInfo1OtherPage = Row.builder()
										                .add(TextCell.builder()
										                		.text("Stock Details Report On : "+reportDate+" "+time)
										                		.horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
										                .backgroundColor(WHITE)
										                .textColor(Color.BLACK)
										                .font(PDType1Font.HELVETICA_BOLD).fontSize(12)				              
										                .horizontalAlignment(CENTER)
										                .build();
									         
									         
									         
									         
									         tableHeaderBuilderOtherPage.addRow(dairyNameHeaderOtherPage);
									         tableHeaderBuilderOtherPage.addRow(addressRowOtherPage);
									         tableHeaderBuilderOtherPage.addRow(reportInfo1OtherPage);
									        
									         
									         headerOtherPage = tableHeaderBuilderOtherPage.build();
									         
									         TableDrawer.builder()
								                .contentStream(contentStream)
								                .table(headerOtherPage)
								                .startX(PADDING30)
								                .startY(startY)
								                .build()
								                .draw();
									         
									         tableStockInfoBuilder.addRow(stockTableHeader);
									         
											
								         }
					        			 
								         
       
								         
							        	 Row collectionTableInfoRow = Row.builder()
									                .add(TextCell.builder().text(""+srNo).horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(0.5F).build())				               
									                .add(TextCell.builder().text(detailsDAO.getProductNameEn().replace('\u2010', ' ')).fontSize(9).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(0.5F).build())
									                .add(TextCell.builder().text(detailsDAO.getStock()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
									                .add(TextCell.builder().text(detailsDAO.getPurchaseValue()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
									                .add(TextCell.builder().text(detailsDAO.getCustSaleValue()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
									                .add(TextCell.builder().text(detailsDAO.getRetailerSaleValue()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
									                .add(TextCell.builder().text(detailsDAO.getDealerSaleValue()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
										            .backgroundColor(Color.WHITE)
									                .textColor(Color.BLACK)
									                .font(PDType1Font.HELVETICA).fontSize(9)				              
									                .horizontalAlignment(CENTER)
									                .height(25F)
									                .build();
								         
							        	 tableStockInfoBuilder.addRow(collectionTableInfoRow);
								         
								       
								         currentY = (float) (currentY + 25.0F);
								         
								         srNo++;
								         
					        		 }
					        		 

						        	 Row totalTableInfoRow = Row.builder()
								                .add(TextCell.builder().text("Total:").colSpan(3).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())				               
								                .add(TextCell.builder().text(df.format(totalPurchaseVal)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
								                .add(TextCell.builder().text(df.format(totalCustSaleVal)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
								                .add(TextCell.builder().text(df.format(totalDealerSaleVal)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
								                .add(TextCell.builder().text(df.format(totalRetailSaleVal)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
									            .backgroundColor(Color.WHITE)
								                .textColor(Color.BLACK)
								                .font(PDType1Font.HELVETICA_BOLD).fontSize(9)				              
								                .horizontalAlignment(CENTER)
								                .height(25F)
								                .build();
							         
						        	 tableStockInfoBuilder.addRow(totalTableInfoRow);
					        		 
					        		 stockInfoRowTable = tableStockInfoBuilder.build();
					        		 startY = page.getMediaBox().getHeight() - PADDING20;
					        		 
					        		  TableDrawer.builder()
						                .contentStream(contentStream)
						                .table(stockInfoRowTable)
						                .startX(PADDING20)
						                .startY(startY-50)
						                .build()
						                .draw();
					        		 
					        		 contentStream.close();
									 	document.save(path+"/stock-report.pdf");
								        document.close();
								        
								        

										response.setHeader("Content-disposition", "inline; filename=stock-report.pdf");
										response.setContentType("application/pdf");
										FileInputStream fileInputStream = new FileInputStream(path+"/stock-report.pdf");
										PrintWriter out = response.getWriter();  
										int bytes;
										while ((bytes = fileInputStream.read()) != -1) {
											out.write(bytes);
										}
										fileInputStream.close();
										out.flush();
										out.close();
						}
						catch(Exception e)
						{
							e.printStackTrace();
						}
						}
						else
						{
							

							
							try
							{
								String name = (String) this.getSessionMap().get("shopnameenglish");
								String address = (String) this.getSessionMap().get("shopaddresseng");
								String path= this.getRequest().getSession().getServletContext().getRealPath("/images/");
								
								
								 java.util.Date date = new java.util.Date();
					        	SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
					        	String reportDate = dateFormat.format(date);
								
								
					       	     long t = date.getTime();	        	     
					       	     java.sql.Time time = new java.sql.Time(t);
					       	     
								PDDocument document = new PDDocument(); 
								PDPage page = null;
								
								 page = new PDPage(PDRectangle.A4);
								
								 
								 document.addPage(page);
								 
								 
						         PDPageContentStream contentStream = new PDPageContentStream(document, page);
								 
						         int pageCount = 1;
						         
						         Table header;
						         
						         
						         
						         
						         TableBuilder tableHeaderBuilder = Table.builder()
							                .addColumnsOfWidth(580)				                
							                .font(HELVETICA)
							                .borderColor(Color.BLACK)
							                .horizontalAlignment(CENTER);
						         
						         Row dairyNameHeader = Row.builder()
							                .add(TextCell.builder().text(name).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
							                .backgroundColor(WHITE)
							                .textColor(Color.BLACK)
							                .font(PDType1Font.HELVETICA_BOLD).fontSize(16)				              
							                .horizontalAlignment(CENTER)
							                .build();
						         
						         Row addressRow = Row.builder()
							                .add(TextCell.builder().text(address).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
							                .backgroundColor(WHITE)
							                .textColor(Color.BLACK)
							                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)				              
							                .horizontalAlignment(CENTER)
							                .build();
						         
						         
						         Row reportInfo1 = Row.builder()
							                .add(TextCell.builder()
							                		.text("Stock Details Report On : "+reportDate+" "+time)
							                		.horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
							                .backgroundColor(WHITE)
							                .textColor(Color.BLACK)
							                .font(PDType1Font.HELVETICA_BOLD).fontSize(12)				              
							                .horizontalAlignment(CENTER)
							                .build();
						         
						         
						         
						         
						         tableHeaderBuilder.addRow(dairyNameHeader);
						         tableHeaderBuilder.addRow(addressRow);
						         tableHeaderBuilder.addRow(reportInfo1);
						        
						         
						         header = tableHeaderBuilder.build();
						         
						         float startY = page.getMediaBox().getHeight() - PADDING20;
						         TableDrawer.builder()
					                .contentStream(contentStream)
					                .table(header)
					                .startX(PADDING30)
					                .startY(startY)
					                .build()
					                .draw();
								
						         
						         Table stockInfoRowTable = null;
						         
						         TableBuilder tableStockInfoBuilder = Table.builder()
							                .addColumnsOfWidth(40,170,40,40,65,65,65,65)				                
							                .font(HELVETICA)
							                .borderWidth(0.5F)
							                .borderColor(Color.BLACK)
							                .horizontalAlignment(CENTER);
						         
						         
						         Row stockTableHeader = Row.builder()
						        		  	.add(TextCell.builder().text("SrNo").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())					              
							                .add(TextCell.builder().text("Product Name").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())				               
							                .add(TextCell.builder().text("In Stock").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())
							                .add(TextCell.builder().text("In Nos").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())
									          .add(TextCell.builder().text("Purchase Value").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
							                .add(TextCell.builder().text("Customer Sale Value").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())
							                .add(TextCell.builder().text("Retailer Sale Value").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())
							                .add(TextCell.builder().text("Dealer Sale Value").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())
							                .backgroundColor(Color.LIGHT_GRAY)
							                .textColor(Color.BLACK)
							                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)				              
							                .horizontalAlignment(CENTER)
							                .build();
						         
						         tableStockInfoBuilder.addRow(stockTableHeader);
						        
						         stockInfoRowTable = tableStockInfoBuilder.build();
						        		
						        		
						        		 float currentY =  75.0F;
						        		 
						        		 int srNo = 1;
						        		 
						        		 for(ItemWiseStockDetailsDAO detailsDAO :  this.getProductStockList())
						        		 {

						        			
								        	 
								        	 
									         if(currentY > 750)
									         {
									        	 
									        	 stockInfoRowTable = tableStockInfoBuilder.build();
							        			 
							        			 
							        			 TableDrawer.builder()
									                .contentStream(contentStream)
									                .table(stockInfoRowTable)
									                .startX(PADDING20)
									                .startY(startY-50)
									                .build()
									                .draw();
								         
							        			 stockInfoRowTable = null;
							        			 tableStockInfoBuilder = null;
							        			 tableStockInfoBuilder = Table.builder()
											                .addColumnsOfWidth(40,170,40,40,65,65,65,65)				                
											                .font(HELVETICA)
											                .borderWidth(0.5F)
											                .borderColor(Color.BLACK)
											                .horizontalAlignment(CENTER);
							        			 
							        			 
									        	 
									        	 pageCount++;
									        	 contentStream.close();
									        	PDPage newPage = new PDPage(PDRectangle.A4);
									        	 contentStream = new PDPageContentStream(document,newPage);
									        	currentY = 20.0F;
									             document.addPage( newPage );
									             
									             
									             
									             Table headerOtherPage;
									             
									             
									             TableBuilder tableHeaderBuilderOtherPage = Table.builder()
											                .addColumnsOfWidth(580)				                
											                .font(HELVETICA)
											                .borderColor(Color.BLACK)
											                .horizontalAlignment(CENTER);
										         
										         Row dairyNameHeaderOtherPage = Row.builder()
											                .add(TextCell.builder().text(name).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
											                .backgroundColor(WHITE)
											                .textColor(Color.BLACK)
											                .font(PDType1Font.HELVETICA_BOLD).fontSize(16)				              
											                .horizontalAlignment(CENTER)
											                .build();
										         
										         Row addressRowOtherPage = Row.builder()
											                .add(TextCell.builder().text(address).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
											                .backgroundColor(WHITE)
											                .textColor(Color.BLACK)
											                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)				              
											                .horizontalAlignment(CENTER)
											                .build();
										         
										         
										         Row reportInfo1OtherPage = Row.builder()
											                .add(TextCell.builder()
											                		.text("Stock Details Report On : "+reportDate+" "+time)
											                		.horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
											                .backgroundColor(WHITE)
											                .textColor(Color.BLACK)
											                .font(PDType1Font.HELVETICA_BOLD).fontSize(12)				              
											                .horizontalAlignment(CENTER)
											                .build();
										         
										         
										         
										         
										         tableHeaderBuilderOtherPage.addRow(dairyNameHeaderOtherPage);
										         tableHeaderBuilderOtherPage.addRow(addressRowOtherPage);
										         tableHeaderBuilderOtherPage.addRow(reportInfo1OtherPage);
										        
										         
										         headerOtherPage = tableHeaderBuilderOtherPage.build();
										         
										         TableDrawer.builder()
									                .contentStream(contentStream)
									                .table(headerOtherPage)
									                .startX(PADDING30)
									                .startY(startY)
									                .build()
									                .draw();
										         
										         tableStockInfoBuilder.addRow(stockTableHeader);
										         
												
									         }
						        			 
									         

								        	 Row collectionTableInfoRow = Row.builder()
										                .add(TextCell.builder().text(""+srNo).horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(0.5F).build())				               
										                .add(TextCell.builder().text(detailsDAO.getProductNameEn()).fontSize(9).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(0.5F).build())
										                .add(TextCell.builder().text(detailsDAO.getStock()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
										                .add(TextCell.builder().text(detailsDAO.getStock2()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
											           .add(TextCell.builder().text(detailsDAO.getPurchaseValue()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
										                .add(TextCell.builder().text(detailsDAO.getCustSaleValue()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
										                .add(TextCell.builder().text(detailsDAO.getRetailerSaleValue()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
										                .add(TextCell.builder().text(detailsDAO.getDealerSaleValue()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
											            .backgroundColor(Color.WHITE)
										                .textColor(Color.BLACK)
										                .font(PDType1Font.HELVETICA).fontSize(10)				              
										                .horizontalAlignment(CENTER)
										                .height(25F)
										                .build();
									         
								        	 tableStockInfoBuilder.addRow(collectionTableInfoRow);
									         
									       
									         currentY = (float) (currentY + 25.0F);
									         
									         srNo++;
									         
						        		 }
						        		 

							        	 Row totalTableInfoRow = Row.builder()
									                .add(TextCell.builder().text("Total:").colSpan(4).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())				               
									                .add(TextCell.builder().text(df.format(totalPurchaseVal)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
									                .add(TextCell.builder().text(df.format(totalCustSaleVal)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
									                .add(TextCell.builder().text(df.format(totalDealerSaleVal)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
									                .add(TextCell.builder().text(df.format(totalRetailSaleVal)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
										            .backgroundColor(Color.WHITE)
									                .textColor(Color.BLACK)
									                .font(PDType1Font.HELVETICA_BOLD).fontSize(9)				              
									                .horizontalAlignment(CENTER)
									                .height(25F)
									                .build();
								         
							        	 tableStockInfoBuilder.addRow(totalTableInfoRow);
						        		 
						        		 stockInfoRowTable = tableStockInfoBuilder.build();
						        		 startY = page.getMediaBox().getHeight() - PADDING20;
						        		 
						        		  TableDrawer.builder()
							                .contentStream(contentStream)
							                .table(stockInfoRowTable)
							                .startX(PADDING20)
							                .startY(startY-50)
							                .build()
							                .draw();
						        		 
						        		 contentStream.close();
										 	document.save(path+"/stock-report.pdf");
									        document.close();
									        
									        

											response.setHeader("Content-disposition", "inline; filename=stock-report.pdf");
											response.setContentType("application/pdf");
											FileInputStream fileInputStream = new FileInputStream(path+"/stock-report.pdf");
											PrintWriter out = response.getWriter();  
											int bytes;
											while ((bytes = fileInputStream.read()) != -1) {
												out.write(bytes);
											}
											fileInputStream.close();
											out.flush();
											out.close();
							}
							catch(Exception e)
							{
								e.printStackTrace();
							}
							
							
						}
					}
					
					
	        	}catch (SQLException e) {
					e.printStackTrace();
				}
				catch (Exception e) {
					e.printStackTrace();
				}finally {
					if(resultSet != null)
    				{
    					try {
    						resultSet.close();resultSet = null;
    					} catch (SQLException e) {
    						
    					}
    				}
					if(preparedStatement != null)
					{
						try {preparedStatement.close(); preparedStatement = null;} catch (SQLException e) {}
					}
					if(connection != null)
					{
						
						try {connection.close(); connection = null;}catch (SQLException e) {}
					}
					
					
	        	}
	}

	 
	public void setSession(Map<String, Object> map) {  
	    sessionMap=(SessionMap<String, Object>)map;  
	}

	public SessionMap<String, Object> getSessionMap() {
		return sessionMap;
	}

	public void setSessionMap(SessionMap<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}
	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 void setServletResponse(HttpServletResponse httpServletResponse) {
		this.response = httpServletResponse;
	}

	
	public void setServletRequest(HttpServletRequest httpServletRequest) {
		this.request = httpServletRequest;
	}

	
	public List<ItemWiseStockDetailsDAO> getProductStockList() {
		return productStockList;
	}

	public void setProductStockList(List<ItemWiseStockDetailsDAO> productStockList) {
		this.productStockList = productStockList;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	public String getStockType() {
		return stockType;
	}


	public void setStockType(String stockType) {
		this.stockType = stockType;
	}
	
	
}
