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.FileNotFoundException;
import java.io.FileOutputStream;
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.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
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.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
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.DayWiseCollectionDAO;
import com.reallinkbilling.DAO.GSTPurchaseInvoiceInfoDAO;
import com.reallinkbilling.db.GetDBConnection;

public class DownloadCollectionReportDayWiseReport extends ActionSupport implements SessionAware{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HttpSession session;
	
	public HttpServletRequest request;
	public HttpServletResponse response;
	private SessionMap<String,Object> sessionMap;
	  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 String fromDate;
		private String toDate;
		
		private List<DayWiseCollectionDAO> collectionDAOList = new ArrayList<DayWiseCollectionDAO>();
		private String defaultValue;
		private Map<String,String> customerSearchOptionList = new HashMap<String,String>();
		private String customerSearchOption;
		
		private String totalBilling;
		private String totalCashCollected;
		private String totalCreditCollected;
		private String totalCreditGiven;
		private String totalCollection;
	 
		public void download(){
			
			this.setSession(ServletActionContext.getRequest().getSession(false)); 
		
	        if(this.getSession()==null || this.getSession().getAttribute("login")==null){ 
	        	addActionMessage("Please Login First !!!");
	         }  
	        else{
	        	
	        	response = ServletActionContext.getResponse();
				request = ServletActionContext.getRequest();
				
				String shopId = (String) this.getSessionMap().get("shopId");
	    		String shopCode = (String) this.getSessionMap().get("shopCode");
	        	
	        	Connection connection = null;
				PreparedStatement preparedStatement= null;
				ResultSet  resultSet = null;
				
				String sql = "";
				String db = (String)  this.getSessionMap().get("db");
				
				try
	    		{
	    			
	    			
					connection = GetDBConnection.getDBConnection(db);
	    			
	    			 Date fromD=new SimpleDateFormat("dd-MM-yyyy").parse(this.getFromDate());  
	    			 Date toD=new SimpleDateFormat("dd-MM-yyyy").parse(this.getToDate());  
	    			 DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
	    			 
	    			 long diff = toD.getTime() -fromD.getTime() ;
	    			
	    			 long fromDateMillies = fromD.getTime();
	    			 
	    			 
	    			 List<String> listOfDays = new ArrayList<String>();
	    			 
	    			
	    			 	listOfDays.add(this.getFromDate());
	    			 	
	    				long diffDays = diff / (24 * 60 * 60 * 1000);
	    				
	    				int intDays = Integer.parseInt(""+diffDays);
	    				
	    				for(int i = intDays;i>0;i--)
	    				{
	    					fromDateMillies = fromDateMillies+(24 * 60 * 60 * 1000);
	    					Date d = new Date(fromDateMillies);	    					
	    					listOfDays.add(dateFormat.format(d));    					
	    				}
	    				
	    				 float floatTotalBilling=0.0F;
	    				 float floatTotalCashCollected=0.0F;
	    				 float floatTotalCreditCollected=0.0F;
	    				 float floatTotalCreditGiven=0.0F;
	    				 float floatTotalCollection=0.0F;
	    				 
	    				
	    				
	    				for(int i = 0;i<listOfDays.size();i++)
	    				{
	    					
	    					
	    					DayWiseCollectionDAO collectionDAO = new DayWiseCollectionDAO();
	    					
	    					
	    					// cash collection 
	    	    			sql = "SELECT "
	    	    					+ "bill_date,"
	    	    					+ "bill_total_amount,"
	    	    					+ "bill_amount_received,"
	    	    					+ "bill_amount_retrurn "
	    	    					+ "FROM "
	    	    					+ "shop_bill_info_master "
	    	    					+ "WHERE "
	    	    					+ "shop_id = ? "
	    	    					+ "AND "
	    	    					+ "shop_code = ? "
	    	    					+ "AND "
	    	    					+ "bill_date = STR_TO_DATE(?, '%d-%m-%Y')";
	    	    			
	    	    			preparedStatement =connection.prepareStatement(sql);
	    	    			preparedStatement.setString(1, shopId);
	    	    			preparedStatement.setString(2, shopCode);
	    	    			preparedStatement.setString(3, listOfDays.get(i));
	    	    			resultSet = preparedStatement.executeQuery();
	    	    			
	    	    			float totalBillAmount = 0.0F;
	    	    			float totalCreditGiven = 0.0F;
	    	    			float totalAmountCollected = 0.0F;
	    	    			float totalCreditCollection =0.0F;
	    	    			while(resultSet.next())	    				
	    	    			{
	    	    				
    	    				if(resultSet.getString(2) != null && resultSet.getString(3) != null && resultSet.getString(4) != null)
    	    				{
	    	    				totalBillAmount = totalBillAmount + (Float.parseFloat(resultSet.getString(2)));
	    	    				
	    	    				float amtReceived = Float.parseFloat(resultSet.getString(3));
	    	    				float amountReturn  = Float.parseFloat(resultSet.getString(4));
	    	    				
	    	    				if(amountReturn > 0 )
	    	    				{
	    	    					totalAmountCollected = totalAmountCollected + Float.parseFloat(resultSet.getString(2));
	    	    				}
	    	    				else
	    	    				{
	    	    					totalAmountCollected = totalAmountCollected + amtReceived;
	    	    					totalCreditGiven = totalCreditGiven 
	    	    							+ (Float.parseFloat(resultSet.getString(2)) - amtReceived);
	    	    				}
	    	    				
	    	    				
	    	    			}
	    	    			
	    	    			
	    	    		}
	    	    			
	    	    			sql = "SELECT SUM(payment_amount) "
	    	    					+ "FROM "
	    	    					+ "shop_customer_payment_master "
	    	    					+ "WHERE "
	    	    					+ "shop_id = ? "
	    	    					+ "AND "
	    	    					+ "shop_code = ? "
	    	    					+ "AND "
	    	    					+ "entry_date = STR_TO_DATE(?, '%d-%m-%Y')";
	    	    			
	    	    			PreparedStatement ps = connection.prepareStatement(sql);
	    	    			
	    	    			ps.setString(1, shopId);
	    	    			ps.setString(2, shopCode);
	    	    			ps.setString(3, listOfDays.get(i));
	    	    			ResultSet rs = ps.executeQuery();
	    	    			
	    	    			
	    	    			if(rs.next())
	    	    			{
	    	    				try
	    	    				{
		    	    				if(rs.getString(1) != null)
		    	    				{
		    	    				
		    	    					totalCreditCollection = Float.parseFloat(rs.getString(1));
		    	    				}
		    	    				else
		    	    				{
		    	    					totalCreditCollection = 0.0F;
		    	    				}
	    	    				}catch(NumberFormatException nfe)
	    	    				{}
	    	    			}
	    	    			
	    	    			collectionDAO.setCollectionDate(listOfDays.get(i));
	    	    			collectionDAO.setTotalBilling(""+totalBillAmount);
	    	    			collectionDAO.setTotalCashCollection(""+totalAmountCollected);
	    	    			collectionDAO.setTotalCreditCollection(""+totalCreditCollection);
	    	    			collectionDAO.setTotalCreeditGiven(""+totalCreditGiven);
	    	    			collectionDAO.setTotalCollection(""+(totalCreditCollection+totalAmountCollected));
	    	    			
	    	    			floatTotalBilling = floatTotalBilling+ totalBillAmount;
	    	    			floatTotalCashCollected = floatTotalCashCollected + totalAmountCollected;
	    	    			floatTotalCreditCollected = floatTotalCreditCollected + totalCreditCollection;
	    	    			floatTotalCreditGiven = floatTotalCreditGiven + totalCreditGiven;
	    	    			floatTotalCollection = floatTotalCollection + (totalCreditCollection+totalAmountCollected);
	    	    			
	    	    			
	    	    			this.getCollectionDAOList().add(collectionDAO);	
	    	    			
	    	    			if(rs != null)
	    	    			{
	    	    				rs.close();
	    	    			}
	    	    			if(ps != null)
	    	    			{
	    	    				ps.close();
	    	    			}
	    				}
	    				
	    				
	    				this.setTotalBilling(""+floatTotalBilling);
	    				this.setTotalCashCollected(""+floatTotalCashCollected);
	    				this.setTotalCreditCollected(""+floatTotalCreditCollected);
	    				this.setTotalCreditGiven(""+floatTotalCreditGiven);
	    				this.setTotalCollection(""+floatTotalCollection);
	    				
	    				
	    				@SuppressWarnings("unchecked")
	        			String shopName = (String) this.getSessionMap().get("shopnameenglish");
	        		     
	        			
	    				
	    					
	    				
	        			
	        			String path=	request.getSession().getServletContext().getRealPath("/images/");
	        			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;
	    		    			 Table creditDetails;
	    		    			 
	    		    			
	    		    			  TableBuilder tableHeaderBuilder = Table.builder()
	    					                .addColumnsOfWidth(585)				                
	    					                .font(HELVETICA)
	    					                .borderColor(Color.BLACK)
	    					                .horizontalAlignment(CENTER);
	    				         
	    				          Row headerName = Row.builder()
	    					                .add(TextCell.builder().text(shopName).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(10F).build())				               
	    					                .backgroundColor(WHITE)
	    					                .textColor(Color.BLACK)
	    					                .font(PDType1Font.HELVETICA_BOLD).fontSize(18)				              
	    					                .horizontalAlignment(CENTER)
	    					                .build();
	    				          
	    				          
	    				          Row info1 = Row.builder()
	    					                .add(TextCell.builder().text("Collection Report DayWise "
	    					                			+"       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("From Date : "
	    					                		+ ""+this.getFromDate()
	    					                		+ " and To Date :"+this.getToDate() ).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();
	    				          
	    				          
	    				          TableBuilder tableDebtDetailsBuilder = Table.builder()
	    					                .addColumnsOfWidth(80, 100, 100, 100, 100,105)		                
	    					                .font(HELVETICA)
	    					                .borderWidth(0.5F)
	    					                .borderColor(Color.BLACK)
	    					                .horizontalAlignment(CENTER);
	    				          
	    				          Row tableHeader = Row.builder()
	    				        		  	.add(TextCell.builder().text("Date").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
	    					                .add(TextCell.builder().text("Billing Amount").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
	    					                .add(TextCell.builder().text("Cash Collected").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
	    					                .add(TextCell.builder().text("Credit Collected").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())									                
	    					                .add(TextCell.builder().text("Credit Given").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())									                
	    					                .add(TextCell.builder().text("Collection").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())									                
	    					                .backgroundColor(Color.LIGHT_GRAY)
	    					                .textColor(Color.BLACK)
	    					                .font(PDType1Font.HELVETICA_BOLD).fontSize(10)				              
	    					                .horizontalAlignment(CENTER)
	    					                .build();
	    				          tableDebtDetailsBuilder.addRow(tableHeader);
	    				       
	    				        
	    				     
	    				        	  int size = this.getCollectionDAOList().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 < 97)
	    				        		  {
	    				        			  pageCount++;
	    				        			  creditDetails = tableDebtDetailsBuilder.build();
	    				        			  TableDrawer.builder()
	    						                .contentStream(contentStream)
	    						                .table(creditDetails)
	    						                .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(shopName).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(10F).build())				               
	    									                .backgroundColor(WHITE)
	    									                .textColor(Color.BLACK)
	    									                .font(PDType1Font.HELVETICA_BOLD).fontSize(18)				              
	    									                .horizontalAlignment(CENTER)
	    									                .build();
	    								          
	    								          
	    								           info1 = Row.builder()
	    									                .add(TextCell.builder().text("Collection Report DayWise "
	    								                			+"       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("From Date : "
	    									                		+ ""+this.getFromDate()
	    									                		+ " and To Date :"+this.getToDate() ).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();
	    				        			  
	    				        			  creditDetails = null;
	    				        			   tableDebtDetailsBuilder = Table.builder()
	    				        					   .addColumnsOfWidth(80, 100, 100, 100, 100,105)
	    								                .font(HELVETICA)
	    								                .borderWidth(0.5F)
	    								                .borderColor(Color.BLACK)
	    								                .horizontalAlignment(CENTER);
	    							          
	    							           tableHeader = Row.builder()
	    							        		   .add(TextCell.builder().text("Date").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
	    								                .add(TextCell.builder().text("Billing Amount").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
	    								                .add(TextCell.builder().text("Cash Collected").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
	    								                .add(TextCell.builder().text("Credit Collected").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())									                
	    								                .add(TextCell.builder().text("Credit Given").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())									                
	    								                .add(TextCell.builder().text("Collection").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())									                
	    								                .backgroundColor(Color.LIGHT_GRAY)
	    								                .textColor(Color.BLACK)
	    								                .font(PDType1Font.HELVETICA_BOLD).fontSize(10)				              
	    								                .horizontalAlignment(CENTER)
	    								                .build();
	    							          tableDebtDetailsBuilder.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();
	    				        		  }
	    				        		  
	    				        		  DayWiseCollectionDAO dao2 = this.getCollectionDAOList().get(s);
	    				        		  
	    				        			Row supplierInfo = Row.builder()
	    				        					.add(TextCell.builder().text(dao2.getCollectionDate()).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
	    					        			  	.add(TextCell.builder().text(dao2.getTotalBilling()).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
	    					        			  	.add(TextCell.builder().text(dao2.getTotalCashCollection()).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
	    					        			  	.add(TextCell.builder().text(dao2.getTotalCreditCollection()).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
	    					        			  	.add(TextCell.builder().text(dao2.getTotalCreeditGiven()).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
	    					        			  	.add(TextCell.builder().text(dao2.getTotalCollection()).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
	    					        			  	.backgroundColor(Color.WHITE)
	    							                .textColor(Color.BLACK)
	    							                .font(PDType1Font.HELVETICA)
	    							                .fontSize(10)				              
	    							                .horizontalAlignment(CENTER)
	    							                .height(25F)					                
	    							                .build();
	    					        	  
	    					        	  tableDebtDetailsBuilder.addRow(supplierInfo);
	    					        	 
	    					        	  currentY = currentY -25;
	    				        	  }
	    					          
	    					    	  Row custInfo = Row.builder()
	    					    			   .add(TextCell.builder().text("Total").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())                
	    					    			   .add(TextCell.builder().text("" + this.getTotalBilling()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())                
	    					    			   .add(TextCell.builder().text("" + this.getTotalCashCollected()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())                
	    					    			   .add(TextCell.builder().text("" + this.getTotalCreditCollected()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())                
	    					    			   .add(TextCell.builder().text("" + this.getTotalCreditGiven()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())                
	    					    			   .add(TextCell.builder().text("" + this.getTotalCollection()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())                
	    					    			    .backgroundColor(Color.WHITE)
	    						                .textColor(Color.BLACK)
	    						                .font(PDType1Font.HELVETICA_BOLD)
	    						                .fontSize(10)				              
	    						                .horizontalAlignment(CENTER)
	    						                .height(15F)					                
	    						                .build();
	    				        	  
	    				        	  tableDebtDetailsBuilder.addRow(custInfo);
	    				        	  creditDetails = tableDebtDetailsBuilder.build();
	    				         
	    				          
	    				          TableDrawer.builder()
	    			                .contentStream(contentStream)
	    			                .table(creditDetails)
	    			                .startX(PADDING5)
	    			                .startY(startY-50)
	    			                .build()
	    			                .draw();
	    				          contentStream.close();
	    					          
	    					          
	    					          
	    					          document.save(path+"/collection-dayWise-fromDate-" + this.getFromDate() +"-ToDate-"+this.getToDate()+"-report.pdf");
	    						        document.close();
	    						        
	    						        response.setHeader("Content-disposition", "inline; filename=collection-dayWise-fromDate-" + this.getFromDate() +"-ToDate-"+this.getToDate()+"-report.pdf");
	    								response.setContentType("application/pdf");
	    								
	    								FileInputStream fileInputStream = new FileInputStream(path+"/collection-dayWise-fromDate-" + this.getFromDate() +"-ToDate-"+this.getToDate()+"-report.pdf");
	    								PrintWriter out = response.getWriter();  
	    								int bytes;
	    								while ((bytes = fileInputStream.read()) != -1) {
	    									out.write(bytes);
	    								}
	    								fileInputStream.close();
	    								out.flush();
	    								out.close();
	    								
	    				
	    				

	    		}
	    		 catch (SQLException e) {
		    			
		    			
		    			
		    			addActionError(e.getErrorCode()+" || "+e.getMessage() +" || Something Went Wrong!");
		    		} catch (IOException e) {
		    			
		    			
		    			addActionError(e.getMessage()+" || "+e.getCause()+" || Something Went Wrong!");
		    		} catch (ParseException e) {
		    			
		    			addActionError(e.getMessage()+" || "+e.getCause()+" || Error while parsing date! Something Went Wrong!");
					}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 List<DayWiseCollectionDAO> getCollectionDAOList() {
			return collectionDAOList;
		}



		public void setCollectionDAOList(List<DayWiseCollectionDAO> collectionDAOList) {
			this.collectionDAOList = collectionDAOList;
		}



		public String getDefaultValue() {
			return defaultValue;
		}



		public void setDefaultValue(String defaultValue) {
			this.defaultValue = defaultValue;
		}



		public Map<String, String> getCustomerSearchOptionList() {
			return customerSearchOptionList;
		}



		public void setCustomerSearchOptionList(Map<String, String> customerSearchOptionList) {
			this.customerSearchOptionList = customerSearchOptionList;
		}



		public String getCustomerSearchOption() {
			return customerSearchOption;
		}



		public void setCustomerSearchOption(String customerSearchOption) {
			this.customerSearchOption = customerSearchOption;
		}



		public HttpSession getSession() {
			return session;
		}



		public void setSession(HttpSession session) {
			this.session = session;
		}
	      


		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;  
		}



		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 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 getTotalBilling() {
			return totalBilling;
		}



		public void setTotalBilling(String totalBilling) {
			this.totalBilling = totalBilling;
		}



		public String getTotalCashCollected() {
			return totalCashCollected;
		}



		public void setTotalCashCollected(String totalCashCollected) {
			this.totalCashCollected = totalCashCollected;
		}



		public String getTotalCreditCollected() {
			return totalCreditCollected;
		}



		public void setTotalCreditCollected(String totalCreditCollected) {
			this.totalCreditCollected = totalCreditCollected;
		}



		public String getTotalCreditGiven() {
			return totalCreditGiven;
		}



		public void setTotalCreditGiven(String totalCreditGiven) {
			this.totalCreditGiven = totalCreditGiven;
		}



		public String getTotalCollection() {
			return totalCollection;
		}



		public void setTotalCollection(String totalCollection) {
			this.totalCollection = totalCollection;
		}
		

}
