package com.reallinkbilling.print;

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 static org.vandeseer.easytable.settings.VerticalAlignment.BOTTOM;
import static org.vandeseer.easytable.settings.VerticalAlignment.MIDDLE;
import static org.vandeseer.easytable.settings.VerticalAlignment.TOP;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
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 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.pdfbox.pdmodel.graphics.image.PDImageXObject;
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.BillInformationDAO;
import com.reallinkbilling.DAO.BillProductInfoDAO;
import com.reallinkbilling.DAO.ChallanInformationDAO;
import com.reallinkbilling.DAO.ChallanProductInfoDAO;
import com.reallinkbilling.cipher.EncryptionDecryption;
import com.reallinkbilling.db.GetDBConnection;
import com.reallinkbilling.resource.GetListResource;
import com.reallinkbilling.resource.NumberToWordsConverter;
import com.reallinkbilling.utils.GetCustomerCreditAmountById;

public class ChallanPrintOnlyType2 extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1414976236123269184L;

	
	private HttpSession session;
	private SessionMap<String,Object> sessionMap;
	private ChallanInformationDAO challanInfoDAO = null;
	private List<ChallanProductInfoDAO> productInfoList = null;
	public HttpServletRequest request;
	public HttpServletResponse response;
	private String challanNumber;
	private String challanPrintType;
	private String key;
	private String language;
	private String challanDateTime;
	
	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;
	
	
	public void printChallanOnly(){
		
		
		boolean isChallanNumberFound = false;
		
		//System.out.println("Bill Number : "+billNumber);
			DecimalFormat df =new DecimalFormat("##.##");
				Connection connection = null;
				PreparedStatement preparedStatement= null;
				ResultSet resultSet = null;
				
				String sql = "";
				String db = (String) this.getSessionMap().get("db");
				
				String shopId = (String) this.getSessionMap().get("shopId");
	    		String shopCode = (String) this.getSessionMap().get("shopCode");
	    		try
	    		{
	    			
	    		
	    		if(this.getKey() != null)
	    		{
	    			if(!this.getKey().equals(""))
	    			{
	    				String actualKey = EncryptionDecryption.decrypt(this.getKey());
	    				String [] keyData = actualKey.split("#");
	    				
	    				this.setChallanNumber(keyData[0]);
	    				shopId = keyData[1];
	    				shopCode = keyData[2];
	    				db = "reallinkshop";
	    				
	    			}
	    		}
	    		
	    		}catch(Exception e){}
			
				String billDateTime = "";
				try
	        	{
	        		
					challanInfoDAO = new ChallanInformationDAO();
					productInfoList = new ArrayList<ChallanProductInfoDAO>();
	        	      
	        
				connection = GetDBConnection.getDBConnection(db);
				
			
	        	
	        	sql = "SELECT "
	        			+ "DATE_FORMAT(challan_date,'%d-%m-%Y'),"
	        			+ "TIME_FORMAT(challan_time,'%h:%i %p'),"
	        			+ "payment_type,"
	        			+ "customer_id,"
	        			+ "customer_name,"
	        			+ "transportation_charges,"
	        			+ "comments,"
	        			+ "challan_total_amount,"
	        			+ "challan_amount_received,"
	        			+ "challan_by "
	        			+ "FROM "
	        			+ "shop_challan_info_master "
	        			+ "WHERE "	        		
	        			+ "challan_number = ? "
	        					+ "AND "
	        					+ "shop_id = ? "
	        					+ "AND "
	        					+ "shop_code = ? "
	        					+ "AND "
	        					+ "challan_date = STR_TO_DATE(?,'%d-%m-%Y') ";
	        	
	        	
	        	
	        	preparedStatement = connection.prepareStatement(sql);
	        	preparedStatement.setString(1, this.getChallanNumber());
	        	preparedStatement.setString(2, shopId);
	        	preparedStatement.setString(3, shopCode);
	        	preparedStatement.setString(4, this.getChallanDateTime());
	        	
	        	resultSet = preparedStatement.executeQuery();
	        	
	        	resultSet.last();
	        	int rowCount = resultSet.getRow();
	        	resultSet.beforeFirst();	        	
	        	
	        	if(rowCount>0)
	        	{
	        		Map<String,String> unitShortName = GetListResource.GetUnitShortEnglishName();
	        		
	        		
	        		isChallanNumberFound = true;
	        	
	        	if(resultSet.next())
	        	{
	        		
	        		billDateTime = resultSet.getString(1)+" "+resultSet.getString(2);
	        		this.getChallanInfoDAO().setChallanDate(resultSet.getString(1)+" "+resultSet.getString(2));
	        		this.getChallanInfoDAO().setChallanNumber(getChallanNumber());
	        		this.getChallanInfoDAO().setPaymentType(resultSet.getString(3));
	        		this.getChallanInfoDAO().setCustId(resultSet.getString(4));
	        		this.getChallanInfoDAO().setCustomerName(resultSet.getString(5).split("-")[1]); 
	        		this.getChallanInfoDAO().setCustomerMobile(resultSet.getString(5).split("-")[2]); 
	        		this.getChallanInfoDAO().setTotalChallanAmount(resultSet.getString(8));
	        		this.getChallanInfoDAO().setChallanBy(resultSet.getString(10));
	        		
	        	}
	        
	        
	        	
        	
		        	if(resultSet != null)
		        	{
		        		resultSet.close();
		        		resultSet = null;
		        	}
		        	if(preparedStatement != null)
		        	{
		        		preparedStatement.close();
		        		preparedStatement = null;
		        	
		        	}
			        	
			        	
			        	sql = "SELECT "
			        			+ "challanProductInfo.product_name,"
			        			+ "challanProductInfo.product_name_in_marathi,"
			        			+ "challanProductInfo.sales_unit,"
			        			+ "challanProductInfo.id_tax_table,"
			        			+ "challanProductInfo.sales_rate,"
			        			+ "challanProductInfo.product_quantity,"
			        			+ "challanProductInfo.discount_percentage,"
			        			+ "challanProductInfo.discount_ruppees,"
			        			+ "challanProductInfo.product_total_amount,"
			        			+ "challanProductInfo.product_total_amount_aft_discount,"
			        			+ "challanProductInfo.product_net_amount,"
			        			+ "challanProductInfo.product_igst_amount,"
			        			+ "challanProductInfo.product_cgst_amount,"
			        			+ "challanProductInfo.product_sgst_amount,"
			        			+ "challanProductInfo.making_labor_charge,"
			        			+ "productInfo.hsn_sac_code,"
			        			+ "productInfo.product_short_code,"
			        			+ "challanProductInfo.product_batch_id,"
			        			+ "challanProductInfo.product_expiry_date "
			        			+ "FROM "
			        			+ "shop_challan_product_info_master AS challanProductInfo "
			        			+ "INNER JOIN "
			        			+ "shop_product_master AS productInfo "
			        			+ "ON "
			        			+ "challanProductInfo.product_id = productInfo.product_id "
			        			+ "WHERE "			        			
			        			+ "challanProductInfo.challan_number = ? "
			        			+ "AND "
			        			+ "challanProductInfo.shop_id = ? "
			        			+ "AND "
			        			+ "challanProductInfo.shop_code = ? "
			        			+ "AND "
			        			+ "productInfo.shop_id = ? "
			        			+ "AND "
			        			+ "productInfo.shop_code = ? "
			        			+ "AND "
			        			+ "challanProductInfo.challan_date = STR_TO_DATE(?,'%d-%m-%Y') "
			        			+ "ORDER BY "
			        			+ "challanProductInfo.entry_id";
			        	
			        	
			        	
			        	
			        	
			        	preparedStatement = connection.prepareStatement(sql);
			        	
			        	preparedStatement.setString(1, this.getChallanNumber());
			        	preparedStatement.setString(2, shopId);
			        	preparedStatement.setString(3, shopCode);
			        	preparedStatement.setString(4, shopId);
			        	preparedStatement.setString(5, shopCode);
			        	preparedStatement.setString(6, this.getChallanDateTime());
			        	
			        	
			        	resultSet = preparedStatement.executeQuery();
			        	
			        	while(resultSet.next())
			        	{
			        		ChallanProductInfoDAO challanProductInfoDAO = new ChallanProductInfoDAO();
			        		challanProductInfoDAO.setProductName(resultSet.getString(1));
			        		challanProductInfoDAO.setProductNameMarathi(resultSet.getString(2));
			        		challanProductInfoDAO.setSalesUnit(resultSet.getString(3));
			        		challanProductInfoDAO.setTaxId(resultSet.getString(4));
			        		challanProductInfoDAO.setSalesRate(resultSet.getFloat(5));
			        		challanProductInfoDAO.setQuantity(resultSet.getFloat(6));
			        		challanProductInfoDAO.setDiscountPecentage(resultSet.getFloat(7));
			        		challanProductInfoDAO.setDiscountRupees(resultSet.getFloat(8));
			        		challanProductInfoDAO.setAmount(resultSet.getFloat(9));
			        		challanProductInfoDAO.setAmountAfterDiscount(resultSet.getFloat(10));
			        		challanProductInfoDAO.setGrossAmount(resultSet.getFloat(11));
			        		challanProductInfoDAO.setIgst(resultSet.getFloat(12));
			        		challanProductInfoDAO.setCgst(resultSet.getFloat(13));
			        		challanProductInfoDAO.setSgst(resultSet.getFloat(14));
			        		challanProductInfoDAO.setSalesUnitShortName(unitShortName.get(resultSet.getString(3)));
			        		challanProductInfoDAO.setMakingLaborCharge(resultSet.getFloat(15));
			        		challanProductInfoDAO.setHsnCode(resultSet.getString(16));
			        		challanProductInfoDAO.setProductCode(resultSet.getString(17));
			        		challanProductInfoDAO.setBatchId(resultSet.getString(18));
			        		challanProductInfoDAO.setExpiryDate(resultSet.getString(19));
			        		challanProductInfoDAO.setProductTotalDiscount(resultSet.getFloat(9)-resultSet.getFloat(10));
			        		
			        		this.getProductInfoList().add(challanProductInfoDAO);
			        		
			        		Map<String,String> taxInfo = GetListResource.GetTaxNamePercentage(resultSet.getString(4),this.getSessionMap());
				        	
							String taxName = taxInfo.get("TAX-NAME");
							String taxPercentage = taxInfo.get("TAX-PERCENTAGE");
							
							challanProductInfoDAO.setTaxName(taxName);
							challanProductInfoDAO.setTaxPercentage(Float.parseFloat(taxPercentage));
							
			        				
			        	}
			        	
	        	}
	        	
				
				if(isChallanNumberFound)
				{
					
					@SuppressWarnings("unchecked")
					HashMap<String,String> shopSettingsMap = (HashMap<String, String>) this.getSessionMap().get("shop_settings");
					
					
					String addDiscountColumnString = shopSettingsMap.get("add_discount_column_to_bill");
					String addQRCodeString = shopSettingsMap.get("add_QR_code_to_bill");
					String addGSTNumberString = shopSettingsMap.get("add_gst_number_to_bill");
					String addCustCreditAmtSettings = shopSettingsMap.get("add_credit_amount_to_bill");
					String billMessageFont = shopSettingsMap.get("bill_message_font");
					String showTotalQuantitySettings = shopSettingsMap.get("show_total_quantity");
					String addMakingLaborColumn = shopSettingsMap.get("add_making_charge_column");
					String addIntroLine =  shopSettingsMap.get("add_header_intro_line");
					String addPageHeaderString = shopSettingsMap.get("add_page_header_default");
				
					//
					 boolean addCustomerCreditAmountToBill = false;		        	
					 boolean addDiscountColumn = false;
					 boolean addQRCode = false;
			         boolean addShortUnit = false;
			         boolean addGSTNumber = false;
			         boolean showTotalQuantity = false;
			         boolean addPageHeader = false;
			         
			         
			         
			         if(addPageHeaderString.equals("TRUE"))
			         {
			        	 addPageHeader = true;
			         }
			         
			         if(showTotalQuantitySettings.equals("TRUE"))
			         {
			        	 showTotalQuantity = true;
			         }
			         
			         if(addCustCreditAmtSettings.equals("TRUE"))
			         {
			        	 addCustomerCreditAmountToBill = true;
			         }
					 if(addDiscountColumnString.equals("TRUE"))
					 {
						 addDiscountColumn = true;
					 }
					 if(addQRCodeString.equals("TRUE"))
					 {
						 addQRCode = true;
					 }
					 if(addGSTNumberString.equals("TRUE"))
					 {
						 addGSTNumber = true;
					 }
	        	
				String gstNum = (String)this.getSessionMap().get("shopgstnumber");
	        	
	        	
	        	float floatTotalAmount = 0.0F;
	        	float floatTotalDiscount = 0.0F;
	        	float floatTotalAmtAfterDiscount = 0.0F;
	        	float totalQuantity = 0.0F;
	        	int listSize = this.getProductInfoList().size();
	        	

	        	for(ChallanProductInfoDAO infoDAO : this.getProductInfoList())
	        	{
	        		//System.out.print("Amount after discount : "+infoDAO.getProductTotalDiscount());
	        		floatTotalAmount  = floatTotalAmount+ infoDAO.getAmount();
	        		floatTotalAmtAfterDiscount = floatTotalAmtAfterDiscount + infoDAO.getAmountAfterDiscount();
	        		floatTotalDiscount = floatTotalDiscount + (infoDAO.getQuantity() * infoDAO.getDiscountRupees());
	        		totalQuantity = totalQuantity + infoDAO.getQuantity();
	        	}
	        	
	        	/*System.out.println("Total Amount : "+floatTotalAmount);
	        	System.out.println("Total Discount : "+floatTotalDiscount);
	        	System.out.println("Total amount Payble : "+floatTotalAmtAfterDiscount);*/
	        	
	        	response = ServletActionContext.getResponse();
				request = ServletActionContext.getRequest();
				
				
				String path= this.getRequest().getSession().getServletContext().getRealPath("/images/");
				
			
				
				
				 
				  
				  
		         
		      
		        
		        
		        
		        if(this.getLanguage().equals("ENGLISH") && this.getChallanPrintType().equals("A5"))
		        {

		    		HashMap<String,String> bankDetails = (HashMap<String, String>) this.getSessionMap().get("bank_details");
		  	      
		        	
		        	
		        	String custMobile = "";
		        	String custAddress = "";
		        	float totalDiscountAmt = 0.0F;
		        	
		        	if(resultSet != null)
		        	{
		        		resultSet.close();
		        		resultSet = null;
		        	}
		        	
		        	if(preparedStatement != null)
		        	{
		        		preparedStatement.close();
		        		preparedStatement = null;
		        	}
		        	
		        	
		        	sql = "SELECT "
		        			+ "mobile_number,"
		        			+ "billing_address "
		        			+ "FROM "
		        			+ "shop_customer_master "
		        			+ "WHERE "
		        			+ "customer_name = ? "
		        			+ "AND "
		        			+ "customer_id = ? "
		        			+ "AND "
		        			+ "shop_id = ? "
		        			+ "AND "
		        			+ "shop_code = ?";
		        	
		        	preparedStatement = connection.prepareStatement(sql);
		        	preparedStatement.setString(1, this.getChallanInfoDAO().getCustomerName());
		        	preparedStatement.setString(2, this.getChallanInfoDAO().getCustId());
		        	preparedStatement.setString(3, shopId);
		        	preparedStatement.setString(4, shopCode);
		        	
		        	resultSet = preparedStatement.executeQuery();
		        	
		        	while(resultSet.next())
		        	{
		        		custMobile = resultSet.getString(1);
		        		custAddress = resultSet.getString(2);
		        	}
		        	
		        	
		        	PDDocument document = new PDDocument(); 
					PDPage page = null;
					
					int productListSize = this.getProductInfoList().size();
					
					
						page = new PDPage(new PDRectangle(420.9449f,595.2756f));
					
					 
					 document.addPage(page);
					 
						
						
					 File fontFile  =  new File( request.getSession().getServletContext().getRealPath("/fonts/Nirmala.ttf"));
					 File fontFileBold  =  new File( request.getSession().getServletContext().getRealPath("/fonts/nirmala-bold.ttf"));
					 
					 TrueTypeFont ttf1 = new TTFParser().parse(fontFile);
					 PDFont fontMarathi = PDType0Font.load(document, ttf1, true);
					 
					 TrueTypeFont ttf2 = new TTFParser().parse(fontFileBold);
					 PDFont fontMarathiBold = PDType0Font.load(document, ttf2, true);
					 
			         PDPageContentStream contentStream = new PDPageContentStream(document, page);
			         
			         
			         
			         PDImageXObject pdImageQRCode = null;
			         PDImageXObject pdImageLeftLogoImage = null;
			         PDImageXObject pdImageRightLogoImage = null;
			         Blob leftLogoImageBlob = null;
			         Blob rightLogoImageBlob = null;
			         InputStream leftLogoIS = null;
			         InputStream rightLogoIS = null;
			        		 
					 Blob blob = (Blob) this.getSessionMap().get("qr_image_bolb");
					 
					 //headintroline
					 
				
					 String headIntroLineContent = (String)this.getSessionMap().get("headintroline");
					 
					 if(shopSettingsMap.get("add_left_side_logo").equals("TRUE"))
					 {
						
							
							 byte[] leftLogoBytes =(byte[]) this.getSessionMap().get("left_logo_image_array");
							 pdImageLeftLogoImage = PDImageXObject.createFromByteArray(document, leftLogoBytes, "leftLogo");
						 
						
					 }
					 if(shopSettingsMap.get("add_right_side_logo").equals("TRUE"))
					 {

						 byte[] rightLogoBytes =(byte[]) this.getSessionMap().get("right_logo_image_array");
						 pdImageRightLogoImage = PDImageXObject.createFromByteArray(document, rightLogoBytes, "rightLogo");
					 }
					 
					 if(blob != null)
					 {
					 InputStream in = blob.getBinaryStream();
					 File qrFile = new File(path+"/"+shopId+"qr.jpeg");
					 FileOutputStream out = new FileOutputStream(qrFile);
					 byte[] buffer = new byte[1024];
					    int read;
					    while ((read = in.read(buffer)) != -1) {
					        out.write(buffer, 0, read);
					    }
					    
					    in.close();
					    out.close();
					    pdImageQRCode = PDImageXObject.createFromFile(path+"/"+shopId+"qr.jpeg", document);
					 }
	         
					 
					
	         Table header;
	         Table billInfoTable;
	         Table custInfoTable;
	         Table itemInfo;
	         
	         
	         TableBuilder tableHeaderBuilder = Table.builder()
		                .addColumnsOfWidth(380)				                
		                .font(HELVETICA)
		                .borderColor(Color.BLACK)
		                .horizontalAlignment(CENTER);
	         
	         Row billType = Row.builder()
		                .add(TextCell.builder().text("DELIVERY CHALLAN").fontSize(10).horizontalAlignment(CENTER).borderWidthTop(0.0F).borderWidthLeft(0.0F).borderWidthRight(0.0F).borderWidthBottom(0.5F).lineSpacing(1F).build())				               
		                .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         
	         Row headerRow  = Row.builder()
		                .add(TextCell.builder().text((String)this.getSessionMap().get("shopnameenglish")).fontSize(15).horizontalAlignment(CENTER).borderWidthLeft(0.5F).borderWidthRight(0.5F).borderWidthBottom(0.0F).lineSpacing(1F).build())				               
		                .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         
	         Row addressRow  = Row.builder()
		                .add(TextCell.builder().text((String)this.getSessionMap().get("shoponelineaddress")).fontSize(9).horizontalAlignment(CENTER).borderWidthLeft(0.5F).borderWidthRight(0.5F).borderWidthBottom(0.0F).lineSpacing(1F).build())				               
		                .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         
	         Row mobileRow  = Row.builder()
		                .add(TextCell.builder().text("Contact: "+(String)this.getSessionMap().get("shopcustcarenumber")).fontSize(9).horizontalAlignment(CENTER).borderWidthLeft(0.5F).borderWidthRight(0.5F).borderWidthBottom(0.5F).lineSpacing(1F).build())				               
		                .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         
	         
	         
	         
	         tableHeaderBuilder.addRow(billType);
	         
	         tableHeaderBuilder.addRow(headerRow);
	         if(addIntroLine.equals("TRUE"))
	         {
	        	 Row headerIntroRow  = Row.builder()
			                .add(TextCell.builder().text(headIntroLineContent).fontSize(9).horizontalAlignment(CENTER).borderWidthLeft(0.5F).borderWidthRight(0.5F).borderWidthBottom(0.0F).lineSpacing(1F).build())				               
			                .backgroundColor(WHITE)
			                .font(PDType1Font.HELVETICA_BOLD)	
			                .textColor(Color.BLACK)
			                .build();
	        	 
	        	 tableHeaderBuilder.addRow(headerIntroRow);
	        	 
	         }
	         tableHeaderBuilder.addRow(addressRow);
	         tableHeaderBuilder.addRow(mobileRow);
	         
	       
	         
	         header = tableHeaderBuilder.build();
	         
	         float startY = page.getMediaBox().getHeight() - PADDING20;
	         
	         if(addPageHeader)
	         {
		         TableDrawer.builder()
	                .contentStream(contentStream)
	                .table(header)
	                .startX(PADDING20)
	                .startY(startY)
	                .build()
	                .draw();
	         }
	         
	         TableBuilder tableBillInfoBuilder = Table.builder()
		                .addColumnsOfWidth(30,30,40,60)				                
		                .font(HELVETICA)
		                .borderColor(Color.BLACK)
		                .horizontalAlignment(CENTER);
	         
	         
	         Row billInfoRow1  = Row.builder()
		                .add(TextCell.builder().text("Challan No:"+this.getChallanNumber()).colSpan(2).fontSize(8).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text("Challan By").fontSize(6).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text(this.getChallanInfoDAO().getChallanBy()).fontSize(8).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         
	         
	         Row billInfoRow2  = Row.builder()
	        		 	.add(TextCell.builder().text("Date:"+this.getChallanInfoDAO().getChallanDate()).colSpan(4).fontSize(8).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
	        		 	.backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();

	         Row billInfoRow3  = Row.builder()
		                .add(TextCell.builder().text("Payment Type").colSpan(3).fontSize(8).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text(this.getChallanInfoDAO().getPaymentType()).fontSize(7).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
					    .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         
	         tableBillInfoBuilder.addRow(billInfoRow1);
	         tableBillInfoBuilder.addRow(billInfoRow2);
	         tableBillInfoBuilder.addRow(billInfoRow3);
	         
	         billInfoTable = tableBillInfoBuilder.build();
	         
	         
	         float currentY = startY - 70;
	         
	         if(addIntroLine.equals("TRUE"))
	         {
	        	 currentY = startY - 90; 
	         }
	          
	      	 if(!addPageHeader)
        	 {
	      		currentY = startY - 140;
        	 }
 	         
	         
	         TableDrawer.builder()
             .contentStream(contentStream)
             .table(billInfoTable)
             .startX(PADDING20 + 220F)
             .startY(currentY)
             .build()
             .draw();
	         
	         
	         TableBuilder tableCustInfoBuilder = Table.builder()
		                .addColumnsOfWidth(220);
	         
	         Row custInfoRow1 = Row.builder()
		                .add(TextCell.builder().text("Customer Name : "+this.getChallanInfoDAO().getCustomerName()).fontSize(8).horizontalAlignment(LEFT).borderWidthLeft(0.5F).borderWidthTop(0.5F).borderWidthRight(0.5F).lineSpacing(0.5F).build())				               
		                .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         Row custInfoRow2 = Row.builder()
		                .add(TextCell.builder().text("Mobile : "+custMobile).fontSize(8).horizontalAlignment(LEFT).borderWidthLeft(0.5F).borderWidthRight(0.5F).lineSpacing(0.5F).build())				               
		                .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         Row custInfoRow3 = Row.builder()
		                .add(TextCell.builder().text("Address : "+custAddress).fontSize(8).horizontalAlignment(LEFT).borderWidthLeft(0.5F).borderWidthRight(0.5F).borderWidthBottom(0.5F).lineSpacing(0.5F).build())				               
		                .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         
	    
	         
	         tableCustInfoBuilder.addRow(custInfoRow1);
	         tableCustInfoBuilder.addRow(custInfoRow2);
	         tableCustInfoBuilder.addRow(custInfoRow3);
	         
	         custInfoTable = tableCustInfoBuilder.build();
	         

	         
	         
	         TableDrawer.builder()
             .contentStream(contentStream)
             .table(custInfoTable)
             .startX(PADDING20)
             .startY(currentY)
             .build()
             .draw();
	         
	         TableBuilder tableItemInfoBuilder ;
	         
	         Row inteInfoHeaderRow;
	         
	         if(addMakingLaborColumn.equals("FALSE"))
	         {
	        	 if(addDiscountColumn)
	        	 {
			        	 tableItemInfoBuilder =  Table.builder()
		        		 .addColumnsOfWidth(25,155,45,45,45,65);
			        	 
			        	 inteInfoHeaderRow = Row.builder()
				                .add(TextCell.builder().text("Sr No").fontSize(6).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
				                .add(TextCell.builder().text("Items").fontSize(8).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
				                .add(TextCell.builder().text("Rate").fontSize(8).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
				                .add(TextCell.builder().text("Quantity").fontSize(8).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
				                .add(TextCell.builder().text("Discount").fontSize(8).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
					            .add(TextCell.builder().text("Amount").fontSize(8).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
				                .backgroundColor(GRAY_LIGHT_3)
				                .font(PDType1Font.HELVETICA_BOLD)	
				                .textColor(Color.BLACK)
				                .build();
			         
			         tableItemInfoBuilder.addRow(inteInfoHeaderRow);
	        	 }
	        	 else
	        	 {
	        		
	        			 tableItemInfoBuilder =  Table.builder()
				        		 .addColumnsOfWidth(25,165,55,55,80);
					        	 
					        	 inteInfoHeaderRow = Row.builder()
						                .add(TextCell.builder().text("Sr No").fontSize(6).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
						                .add(TextCell.builder().text("Items").fontSize(8).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
						                .add(TextCell.builder().text("Rate").fontSize(8).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
						                .add(TextCell.builder().text("Quantity").fontSize(8).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
							            .add(TextCell.builder().text("Amount").fontSize(8).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
						                .backgroundColor(GRAY_LIGHT_3)
						                .font(PDType1Font.HELVETICA_BOLD)	
						                .textColor(Color.BLACK)
						                .build();
					         
					         tableItemInfoBuilder.addRow(inteInfoHeaderRow); 
		        	
	        	 }
	         
	         }
	         else
	         {
	        	 if(addDiscountColumn)
	        	 {
	        		 tableItemInfoBuilder =  Table.builder()
		            		 .addColumnsOfWidth(25,120,45,45,45,45,55);
		    	        	 
		    	        	 inteInfoHeaderRow = Row.builder()
		    		                .add(TextCell.builder().text("Sr No").fontSize(6).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
		    		                .add(TextCell.builder().text("Items").fontSize(6).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
		    		                .add(TextCell.builder().text("Rate").fontSize(6).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
		    		                .add(TextCell.builder().text("Quantity").fontSize(6).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
		    		                .add(TextCell.builder().text("Making / Labor Charges").fontSize(5).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
		    		                .add(TextCell.builder().text("Discount").fontSize(6).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
		    		                .add(TextCell.builder().text("Amount").fontSize(6).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
		    		                .backgroundColor(GRAY_LIGHT_3)
		    		                .font(PDType1Font.HELVETICA_BOLD)	
		    		                .textColor(Color.BLACK)
		    		                .build();
		    	         
		    	         tableItemInfoBuilder.addRow(inteInfoHeaderRow);
	        	 }
	        	 else
	        	 {
	        	 tableItemInfoBuilder =  Table.builder()
	            		 .addColumnsOfWidth(25,135,55,55,55,55);
	    	        	 
	    	        	 inteInfoHeaderRow = Row.builder()
	    		                .add(TextCell.builder().text("Sr No").fontSize(7).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
	    		                .add(TextCell.builder().text("Items").fontSize(7).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
	    		                .add(TextCell.builder().text("Rate").fontSize(7).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
	    		                .add(TextCell.builder().text("Quantity").fontSize(7).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
	    		                .add(TextCell.builder().text("Making / Labor Charges").fontSize(6).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
		    			        .add(TextCell.builder().text("Amount").fontSize(7).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
	    		                .backgroundColor(GRAY_LIGHT_3)
	    		                .font(PDType1Font.HELVETICA_BOLD)	
	    		                .textColor(Color.BLACK)
	    		                .build();
	    	         
	    	         tableItemInfoBuilder.addRow(inteInfoHeaderRow);
	        	 }
	         }

	         int srNo = 1;
	         int pageCount = 1;
	         int listCount = this.getProductInfoList().size();
	         
	         if(listCount < 10)
	         {
	        	 listCount = 10;
	         }
	         
	     	for(int i =0;i<listCount;i++)
				{
	     		
	     		//System.out.println("Starting Current Y :"+currentY);
					Row itemDetails = null;
					
					if(currentY  < 250)
					{
						int colSpan = 5;
						 if(addDiscountColumn)
			        	 {
							 colSpan = 6;
			        	 }
						
						Row toBeContinue =  Row.builder()
			        		 	.add(TextCell.builder().text("Page No."+pageCount+", To be continued on next page...").colSpan(colSpan).fontSize(9).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
			        		 	.textColor(Color.BLACK)
				                .height(20F)
				                .font(fontMarathi)
				                .build();
						pageCount++;
						
						tableItemInfoBuilder.addRow(toBeContinue);
					
						itemInfo = tableItemInfoBuilder.build();
						
						float startY2 = page.getMediaBox().getHeight() - PADDING20;
						
			        	float currentY2 = startY2 - 135 ; 
			        	
			        	 if(addIntroLine.equals("TRUE"))
			 	         {
			 	        	 currentY = startY - 110; 
			 	         }
			        	 
			        	 if(!addPageHeader)
			        	 {
			        		 currentY2 = startY2 - 210;  
			        	 }
			        	 
			        	 if(addPageHeader)
				         {
				         
					         
					         if(pdImageLeftLogoImage != null)
				        	 {
					        	
				        		 contentStream.drawImage(pdImageLeftLogoImage, 30, 515 , 35, 35);
				        		
				        	 }
				
					         if(pdImageRightLogoImage != null)
				        	 {
					        	
				        		 contentStream.drawImage(pdImageRightLogoImage, 425, 435 , 35, 35);
				        		
				        	 }
				         }
						 
			 	         
			        	
						
						 TableDrawer.builder()
			             .contentStream(contentStream)
			             .table(itemInfo)
			             .startX(PADDING20)
			             .startY(currentY2)
			             .build()
			             .draw();
						
						 contentStream.close();
						 contentStream = null;
				        	PDPage newPage = new PDPage(new PDRectangle(420.9449f,595.2756f));
							
				        	 
				        	 contentStream = new PDPageContentStream(document,newPage);
				        	currentY = 20.0F;
				             document.addPage( newPage );
				             
					          startY = newPage.getMediaBox().getHeight() - PADDING20;
				             TableDrawer.builder()
				                .contentStream(contentStream)
				                .table(header)
				                .startX(PADDING20)
				                .startY(startY)
				                .build()
				                .draw();
				             
				             
					         currentY = startY - 80;
					         
					         if(addIntroLine.equals("TRUE"))
				 	         {
				 	        	 currentY = startY - 90; 
				 	         }
					         
					         
				        	 TableDrawer.builder()
				             .contentStream(contentStream)
				             .table(custInfoTable)
				             .startX(PADDING20)
				             .startY(currentY)
				             .build()
				             .draw();
				        	 
					         TableDrawer.builder()
				             .contentStream(contentStream)
				             .table(billInfoTable)
				             .startX(240)
				             .startY(currentY)
				             .build()
				             .draw();
					         
					         currentY = currentY - 50;
					         if(addIntroLine.equals("TRUE"))
				 	         {
				 	        	 currentY = currentY - 40; 
				 	         }
					       
					         itemInfo = null;
					         tableItemInfoBuilder = null;
					         if(addMakingLaborColumn.equals("FALSE"))
					         {
					        	 if(addDiscountColumn)
					        	 {
					        		 tableItemInfoBuilder = Table.builder()
					        				 .addColumnsOfWidth(25,155,45,45,45,65);
					        	 }
					        	 else
					        	 {
					        		 tableItemInfoBuilder = Table.builder()
					        				 .addColumnsOfWidth(25,165,55,55,80);
						        	 
					        	 }
					         }else
					         {
					        	 if(addDiscountColumn)
					        	 {
					        		 tableItemInfoBuilder =  Table.builder()
					        				 .addColumnsOfWidth(25,120,45,45,45,45,55);
					        	 }
					        	 else
					        	 {
					        		 tableItemInfoBuilder =  Table.builder()
					        				 .addColumnsOfWidth(25,135,55,55,55,55);
				    	        	 
					        	 }
					        	 
					         }
					       
					         
					         tableItemInfoBuilder.addRow(inteInfoHeaderRow);
					}
					
					
						ChallanProductInfoDAO informationDAO = null;
						
						try						
						{
							informationDAO = this.getProductInfoList().get(i);
						}catch (Exception e) {}
						if(informationDAO != null)
						{
						
							 if(addMakingLaborColumn.equals("FALSE"))
					         {
								
								 if(addDiscountColumn)
					        	 {
									 itemDetails = Row.builder()
							        		 	.add(TextCell.builder().text(""+srNo).fontSize(9).horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())				               
							        		 	.add(TextCell.builder().text(informationDAO.getProductName()).fontSize(8).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
								                .add(TextCell.builder().text(""+informationDAO.getSalesRate()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
								                .add(TextCell.builder().text(""+informationDAO.getQuantity()+" "+informationDAO.getSalesUnitShortName()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
								                .add(TextCell.builder().text(""+informationDAO.getDiscountRupees()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
										        .add(TextCell.builder().text(""+informationDAO.getAmountAfterDiscount()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
										        .backgroundColor(WHITE)
								                .textColor(Color.BLACK)
								                .height(20F)
								                .font(fontMarathi).fontSize(9)				              
								                .horizontalAlignment(CENTER)
								                .build();
							         srNo++;
							         
							         totalDiscountAmt = totalDiscountAmt + informationDAO.getDiscountRupees();
					        	 }
								 else
								 {
						         itemDetails = Row.builder()
						        		 	.add(TextCell.builder().text(""+srNo).fontSize(9).horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())				               
						        		 	.add(TextCell.builder().text(informationDAO.getProductName()).fontSize(8).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
							                .add(TextCell.builder().text(""+informationDAO.getSalesRate()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
							                .add(TextCell.builder().text(""+informationDAO.getQuantity()+" "+informationDAO.getSalesUnitShortName()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
								            .add(TextCell.builder().text(""+informationDAO.getAmountAfterDiscount()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
									        .backgroundColor(WHITE)
							                .textColor(Color.BLACK)
							                .height(20F)
							                .font(fontMarathi).fontSize(9)				              
							                .horizontalAlignment(CENTER)
							                .build();
						         srNo++;
								 }
					         }
							 else
							 {
								 itemDetails = Row.builder()
						        		 	.add(TextCell.builder().text(""+srNo).fontSize(9).horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())				               
						        		 	.add(TextCell.builder().text(informationDAO.getProductName()).fontSize(8).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
							                .add(TextCell.builder().text(""+informationDAO.getSalesRate()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
							                .add(TextCell.builder().text(""+informationDAO.getQuantity()+" "+informationDAO.getSalesUnitShortName()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
							                .add(TextCell.builder().text(""+informationDAO.getMakingLaborCharge()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
							                .add(TextCell.builder().text(""+informationDAO.getAmountAfterDiscount()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
									        .backgroundColor(WHITE)
							                .textColor(Color.BLACK)
							                .height(20F)
							                .font(fontMarathi).fontSize(9)				              
							                .horizontalAlignment(CENTER)
							                .build();
						         srNo++;
							 }
						}
						else
						{
							 if(addMakingLaborColumn.equals("FALSE"))
					         {
								 
								 if(addDiscountColumn)
					        	 {
									 itemDetails = Row.builder()
							        		 	.add(TextCell.builder().text("").fontSize(9).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
							        		 	.add(TextCell.builder().text("").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
							        			.add(TextCell.builder().text("").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
							        			.add(TextCell.builder().text("").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
										     	.add(TextCell.builder().text("").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
									            .add(TextCell.builder().text("").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
										        .backgroundColor(WHITE)
								                .textColor(Color.BLACK)
								                .height(20F)
								                .font(fontMarathi).fontSize(9)				              
								                .horizontalAlignment(CENTER)
								                .build();
									 
									 
					        	 }
								 else
								 {
						         itemDetails = Row.builder()
						        		 	.add(TextCell.builder().text("").fontSize(9).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
						        		 	.add(TextCell.builder().text("").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
						        			.add(TextCell.builder().text("").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
								     	    .add(TextCell.builder().text("").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
								            .add(TextCell.builder().text("").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
									        .backgroundColor(WHITE)
							                .textColor(Color.BLACK)
							                .height(20F)
							                .font(fontMarathi).fontSize(9)				              
							                .horizontalAlignment(CENTER)
							                .build();
								 }
					         }else
					         {
					        	 if(addDiscountColumn)
					        	 {
					        		 itemDetails = Row.builder()
							        		 	.add(TextCell.builder().text("").fontSize(9).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
							        		 	.add(TextCell.builder().text("").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
							        			.add(TextCell.builder().text("").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
									     	    .add(TextCell.builder().text("").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
									     	    .add(TextCell.builder().text("").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
										     	.add(TextCell.builder().text("").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
										        .add(TextCell.builder().text("").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
										        .backgroundColor(WHITE)
								                .textColor(Color.BLACK)
								                .height(20F)
								                .font(fontMarathi).fontSize(9)				              
								                .horizontalAlignment(CENTER)
								                .build();
					        	 }
					        	 else
					        	 {
					        		 
					        	 
						        	 itemDetails = Row.builder()
							        		 	.add(TextCell.builder().text("").fontSize(9).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
							        		 	.add(TextCell.builder().text("").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
							        			.add(TextCell.builder().text("").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
									     	    .add(TextCell.builder().text("").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
									     	    .add(TextCell.builder().text("").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
										        .add(TextCell.builder().text("").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
										        .backgroundColor(WHITE)
								                .textColor(Color.BLACK)
								                .height(20F)
								                .font(fontMarathi).fontSize(9)				              
								                .horizontalAlignment(CENTER)
								                .build();
					        	 }
					         }
						}
					
			         tableItemInfoBuilder.addRow(itemDetails);
			        
			         currentY = (float) (currentY - 20.0F);
					//System.out.println("Inner : "+currentY);
					
				}
				DecimalFormat df2 = new DecimalFormat("#.#");
	     	
			if(addCustomerCreditAmountToBill)
	         {
	     		float custCreditAmount = GetCustomerCreditAmountById.getCustomerCreditAmountById(this.getChallanInfoDAO().getCustId(), this.getSessionMap());
	        	int colSpanCount = 2;
	     		if(addMakingLaborColumn.equals("TRUE"))
		         {
	     			colSpanCount = 3;
		         }
	     		
	     		
	     		if(addDiscountColumn && addMakingLaborColumn.equals("TRUE"))
	        	 {
	     			colSpanCount = 4;
	        	 }
	     		
	     		if(addDiscountColumn)
	     		{
	     			colSpanCount = 3;
	     		}
	     		
				Row totalInfoRow = Row.builder()
							.add(TextCell.builder().text("Total OutStanding Amount: Rs. "+df2.format(custCreditAmount)).colSpan(2).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(0.5F).build())				                
			             	.add(TextCell.builder().text("Total Challan Amount (in Rs.)").colSpan(colSpanCount).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
		        		 	.add(TextCell.builder().text(df2.format(floatTotalAmtAfterDiscount)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
		        		 	.backgroundColor(WHITE)
			                .textColor(Color.BLACK)
			                .height(20F)
			                .font(fontMarathiBold).fontSize(8)				              
			                .horizontalAlignment(CENTER)
			                .build();
				tableItemInfoBuilder.addRow(totalInfoRow);
				
	         }
	     	 else
	     	 {
	     		int colSpanCount = 4;
	     		int colSpanAddTotalDisc = 3;
	     		
	     		if(addMakingLaborColumn.equals("TRUE"))
		         {
	     			colSpanCount = 5;
	     			colSpanAddTotalDisc = 4;
		         }
	     		
	     		if(addDiscountColumn && addMakingLaborColumn.equals("TRUE"))
	        	 {
	     			colSpanCount = 6;
	     			colSpanAddTotalDisc = 5;
	        	 }
	     		
	     		if(addDiscountColumn)
	     		{
	     			colSpanCount = 5;
	     			colSpanAddTotalDisc = 4;
	     		}
	     		
	     		
	     		Row totalMoneySavedInfoRow = Row.builder()
	        		 	.add(TextCell.builder().text("Total Savings:").colSpan(colSpanAddTotalDisc).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        		 	.add(TextCell.builder().text(df2.format(totalDiscountAmt)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        			.add(TextCell.builder().text("").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
		        		 .backgroundColor(WHITE)
		                .textColor(Color.BLACK)
		                .height(20F)
		                .font(fontMarathiBold).fontSize(9)				              
		                .horizontalAlignment(CENTER)
		                .build();
	     			tableItemInfoBuilder.addRow(totalMoneySavedInfoRow);
	     			
	     		Row totalInfoRow = Row.builder()
	        		 	.add(TextCell.builder().text("Total Bill Amount(in Rs.)").colSpan(colSpanCount).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        		 	.add(TextCell.builder().text(df2.format(floatTotalAmtAfterDiscount)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        		 	.backgroundColor(WHITE)
		                .textColor(Color.BLACK)
		                .height(20F)
		                .font(fontMarathiBold).fontSize(9)				              
		                .horizontalAlignment(CENTER)
		                .build();
	     			tableItemInfoBuilder.addRow(totalInfoRow);
	     	 }
				
				 
	     	int colSpanCountBottom = 5;
     		if(addMakingLaborColumn.equals("TRUE"))
	         {
     			colSpanCountBottom = 6;
	         }
     		
     		
     		if(addDiscountColumn && addMakingLaborColumn.equals("TRUE"))
	       	 {
	     			colSpanCountBottom = 7;
	       	 }
    		
    		if(addDiscountColumn)
    		{
    			colSpanCountBottom = 6;
    		}
				
				  String convertedAmount = new NumberToWordsConverter().getConverted(""+floatTotalAmtAfterDiscount);
		      
				  Row amtInwordsRow = Row.builder()
		        		 	.add(TextCell.builder().text("Amount in Words : "+convertedAmount).colSpan(colSpanCountBottom).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
		        		 	.backgroundColor(WHITE)
			                .textColor(Color.BLACK)
			                .height(20F)
			                .font(fontMarathi).fontSize(8)				              
			                .horizontalAlignment(CENTER)
			                .build();
				tableItemInfoBuilder.addRow(amtInwordsRow);
				String comm = this.getChallanInfoDAO().getComments();
				
				if(comm == null)
				{
					comm = "";
				}
				Row commentsRow = Row.builder()
	        		 	.add(TextCell.builder().text("Comments:"+comm).colSpan(colSpanCountBottom).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        		 	.backgroundColor(WHITE)
		                .textColor(Color.BLACK)
		                .height(15F)
		                .font(fontMarathi).fontSize(8)				              
		                .horizontalAlignment(LEFT)
		                .build();
			tableItemInfoBuilder.addRow(commentsRow);
	         
	         itemInfo = tableItemInfoBuilder.build();
	         
	         Table bankDetailsTable = null;
	         Table totalAmountDetails = null;
		       
		       
		       TableBuilder bankDetailsTableBuilder = Table.builder()
			                .addColumnsOfWidth(149,231)				                
			                .font(HELVETICA)
			                .borderWidth(1)
			                .borderColor(Color.BLACK);
		       
		       Row bankDetailsHeader = Row.builder()
		                .add(TextCell.builder().text("BANK DETAILS").colSpan(2).horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())				                
		                .backgroundColor(Color.WHITE)
		                .textColor(Color.BLACK)
		                .font(PDType1Font.HELVETICA_BOLD)
		                .fontSize(7)				              
		                .horizontalAlignment(CENTER)
		                .height(13F)	
		                .build();
		     //  bankDetailsTableBuilder.addRow(bankDetailsHeader);
		       
		       Row bankDetailsRow1 = Row.builder()
		                .add(TextCell.builder().text("BANKING NAME").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
		                .add(TextCell.builder().text(bankDetails.get("bankingname")).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
		                .backgroundColor(Color.WHITE)
		                .textColor(Color.BLACK)
		                .font(PDType1Font.HELVETICA)
		                .fontSize(7)				              
		                .horizontalAlignment(CENTER)
		                .height(13F)	
		                .build();
		       bankDetailsTableBuilder.addRow(bankDetailsRow1);
		       
		       Row bankDetailsRow2 = Row.builder()
		                .add(TextCell.builder().text("NAME OF THE BANK").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
		                .add(TextCell.builder().text(bankDetails.get("bankname")).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
		                .backgroundColor(Color.WHITE)
		                .textColor(Color.BLACK)
		                .font(PDType1Font.HELVETICA)
		                .fontSize(7)				              
		                .horizontalAlignment(CENTER)
		                .height(13F)	
		                .build();
		       bankDetailsTableBuilder.addRow(bankDetailsRow2);
		       	Row bankDetailsRow3 = Row.builder()
		                .add(TextCell.builder().text("BRANCH").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
		                .add(TextCell.builder().text(bankDetails.get("branchname")).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
		                .backgroundColor(Color.WHITE)
		                .textColor(Color.BLACK)
		                .font(PDType1Font.HELVETICA)
		                .fontSize(7)				              
		                .horizontalAlignment(CENTER)
		                .height(13F)	
		                .build();
		       	bankDetailsTableBuilder.addRow(bankDetailsRow3);
		       	Row bankDetailsRow4 = Row.builder()
		                .add(TextCell.builder().text("ACCOUNT NUMBER").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
		                .add(TextCell.builder().text(bankDetails.get("accnumber")).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
		                .backgroundColor(Color.WHITE)
		                .textColor(Color.BLACK)
		                .font(PDType1Font.HELVETICA)
		                .fontSize(7)				              
		                .horizontalAlignment(CENTER)
		                .height(13F)	
		                .build();
		       	bankDetailsTableBuilder.addRow(bankDetailsRow4);
		       	Row bankDetailsRow5 = Row.builder()
		                .add(TextCell.builder().text("IFSC CODE").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(10F).build())				                
		                .add(TextCell.builder().text(bankDetails.get("ifsccode")).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(10F).build())
		                .backgroundColor(Color.WHITE)
		                .textColor(Color.BLACK)
		                .font(PDType1Font.HELVETICA)
		                .fontSize(7)				              
		                .horizontalAlignment(CENTER)
		                .height(13F)	
		                .build();
		       	bankDetailsTableBuilder.addRow(bankDetailsRow5);
	        
		         bankDetailsTable = bankDetailsTableBuilder.build();
		         
	         
	         TableBuilder signatureBuiler = Table.builder()
		                .addColumnsOfWidth(145,90,145)				                
		                .font(HELVETICA)
		                .borderColor(Color.BLACK)
		                .horizontalAlignment(CENTER);
	         
	         Row signRow1 = Row.builder()
	        		 	.add(TextCell.builder().text("").verticalAlignment(BOTTOM).horizontalAlignment(CENTER).borderWidthTop(0.5F).borderWidthLeft(0.5F).borderWidthRight(0.5F).lineSpacing(0.5F).build())
		                .add(TextCell.builder().text("").verticalAlignment(BOTTOM).horizontalAlignment(CENTER).borderWidthTop(0.5F).borderWidthLeft(0.5F).borderWidthRight(0.5F).lineSpacing(0.5F).build())
		                .add(TextCell.builder().text("For "+(String)this.getSessionMap().get("shopnameenglish")).verticalAlignment(BOTTOM).horizontalAlignment(CENTER).borderWidthTop(0.5F).borderWidthLeft(0.5F).borderWidthRight(0.5F).lineSpacing(0.5F).build())					               
		                .backgroundColor(Color.WHITE)
		                .textColor(Color.BLACK)
		                .font(PDType1Font.HELVETICA_BOLD)
		                .fontSize(9)				              
		                .horizontalAlignment(CENTER)
		                .height(20F)	
		                .build();
	         signatureBuiler.addRow(signRow1);
	         
	         Row signRow2 = Row.builder()
	        		 	.add(TextCell.builder().text("").verticalAlignment(BOTTOM).horizontalAlignment(CENTER).borderWidthTop(0.0F).borderWidthLeft(0.5F).borderWidthRight(0.5F).borderWidthBottom(0.5F).lineSpacing(1F).build())
		                .add(TextCell.builder().text("Customer Signature").verticalAlignment(BOTTOM).horizontalAlignment(CENTER).borderWidthTop(0.0F).borderWidthLeft(0.5F).borderWidthRight(0.5F).borderWidthBottom(0.5F).lineSpacing(1F).build())
		                .add(TextCell.builder().text("Authorized Signature").verticalAlignment(BOTTOM).horizontalAlignment(CENTER).borderWidthTop(0.0F).borderWidthLeft(0.5F).borderWidthRight(0.5F).borderWidthBottom(0.5F).lineSpacing(1F).build())					               
		                .backgroundColor(Color.WHITE)
		                .textColor(Color.BLACK)
		                .font(PDType1Font.HELVETICA_BOLD)
		                .fontSize(9)				              
		                .horizontalAlignment(CENTER)
		                .height(60F)	
		                .build();
	         signatureBuiler.addRow(signRow2);
	       
	         
	         Table signature = signatureBuiler.build();   
	         
	      
	         itemInfo = tableItemInfoBuilder.build();
	         
        	float startY2 = page.getMediaBox().getHeight() - PADDING20;
        	float currentY2 = startY2 - 120 ; 
        	
        	
        	if(addIntroLine.equals("TRUE"))
 	         {
        		currentY2 = startY2 - 140; 
 	         }
        	
        	if(!addPageHeader)
        	{
        		currentY2 = startY2 - 185; 
        	}
        	 
      	 TableDrawer.builder()
             .contentStream(contentStream)
             .table(itemInfo)
             .startX(PADDING20)
             .startY(currentY2)
             .build()
             .draw();
			
         
         TableDrawer.builder()
         .contentStream(contentStream)
         .table(bankDetailsTable)
         .startX(PADDING20)
         .startY(currentY-122)
         .build()
         .draw();
         
	     
	         
	         TableDrawer.builder()
             .contentStream(contentStream)
             .table(signature)
             .startX(PADDING20)
             .startY(currentY-190)
             .build()
             .draw();
	         
	         
	         if(addPageHeader)
	         {
	         
		         
		         if(pdImageLeftLogoImage != null)
	        	 {
		        	
	        		 contentStream.drawImage(pdImageLeftLogoImage, 30, 515 , 35, 35);
	        		
	        	 }
	
		         if(pdImageRightLogoImage != null)
	        	 {
		        	
	        		 contentStream.drawImage(pdImageRightLogoImage, 425, 435 , 35, 35);
	        		
	        	 }
	         }
			 
	         	contentStream.close();
			 	document.save(path+"/challan.pdf");
		        document.close();
		        
		        

				response.setHeader("Content-disposition", "inline; filename=challan.pdf");
				response.setContentType("application/pdf");
				FileInputStream fileInputStream = new FileInputStream(path+"/challan.pdf");
				PrintWriter out = response.getWriter();  
				int bytes;
				while ((bytes = fileInputStream.read()) != -1) {
					out.write(bytes);
				}
				fileInputStream.close();
				out.flush();
				out.close();
				ttf1.close();
				ttf2.close();
			
	         
	        
				
		        }
		        
		        
		        // START: A4 PRINT WITH GST DETAILS
		        if(this.getLanguage().equals("ENGLISH") && this.getChallanPrintType().equals("A4"))
		        {

		    		HashMap<String,String> bankDetails = (HashMap<String, String>) this.getSessionMap().get("bank_details");
		  	      
		        	
		        	
		        	String custMobile = "";
		        	String custAddress = "";
		        	float totalDiscountAmt = 0.0F;
		        	
		        	if(resultSet != null)
		        	{
		        		resultSet.close();
		        		resultSet = null;
		        	}
		        	
		        	if(preparedStatement != null)
		        	{
		        		preparedStatement.close();
		        		preparedStatement = null;
		        	}
		        	
		        	
		        	sql = "SELECT "
		        			+ "mobile_number,"
		        			+ "billing_address "
		        			+ "FROM "
		        			+ "shop_customer_master "
		        			+ "WHERE "
		        			+ "customer_name = ? "
		        			+ "AND "
		        			+ "customer_id = ? "
		        			+ "AND "
		        			+ "shop_id = ? "
		        			+ "AND "
		        			+ "shop_code = ?";
		        	
		        	preparedStatement = connection.prepareStatement(sql);
		        	preparedStatement.setString(1, this.getChallanInfoDAO().getCustomerName());
		        	preparedStatement.setString(2, this.getChallanInfoDAO().getCustId());
		        	preparedStatement.setString(3, shopId);
		        	preparedStatement.setString(4, shopCode);
		        	
		        	resultSet = preparedStatement.executeQuery();
		        	
		        	while(resultSet.next())
		        	{
		        		custMobile = resultSet.getString(1);
		        		custAddress = resultSet.getString(2);
		        	}
		        	
		        	
		        	PDDocument document = new PDDocument(); 
					PDPage page = null;
					
					int productListSize = this.getProductInfoList().size();
					
					
						page = new PDPage(PDRectangle.A4);
					
					 
					 document.addPage(page);
					 
						
						
					 File fontFile  =  new File( request.getSession().getServletContext().getRealPath("/fonts/Nirmala.ttf"));
					 File fontFileBold  =  new File( request.getSession().getServletContext().getRealPath("/fonts/nirmala-bold.ttf"));
					 
					 TrueTypeFont ttf1 = new TTFParser().parse(fontFile);
					 PDFont fontMarathi = PDType0Font.load(document, ttf1, true);
					 
					 TrueTypeFont ttf2 = new TTFParser().parse(fontFileBold);
					 PDFont fontMarathiBold = PDType0Font.load(document, ttf2, true);
					 
			         PDPageContentStream contentStream = new PDPageContentStream(document, page);
			         
			         
			         
			         PDImageXObject pdImageQRCode = null;
			         PDImageXObject pdImageLeftLogoImage = null;
			         PDImageXObject pdImageRightLogoImage = null;
			         Blob leftLogoImageBlob = null;
			         Blob rightLogoImageBlob = null;
			         InputStream leftLogoIS = null;
			         InputStream rightLogoIS = null;
			        		 
					 Blob blob = (Blob) this.getSessionMap().get("qr_image_bolb");
					 
					 //headintroline
					 
				
					 String headIntroLineContent = (String)this.getSessionMap().get("headintroline");
					 
					 if(shopSettingsMap.get("add_left_side_logo").equals("TRUE"))
					 {
						
							
							 byte[] leftLogoBytes =(byte[]) this.getSessionMap().get("left_logo_image_array");
							 pdImageLeftLogoImage = PDImageXObject.createFromByteArray(document, leftLogoBytes, "leftLogo");
						 
						
					 }
					 if(shopSettingsMap.get("add_right_side_logo").equals("TRUE"))
					 {

						 byte[] rightLogoBytes =(byte[]) this.getSessionMap().get("right_logo_image_array");
						 pdImageRightLogoImage = PDImageXObject.createFromByteArray(document, rightLogoBytes, "rightLogo");
					 }
					 
					 if(blob != null)
					 {
					 InputStream in = blob.getBinaryStream();
					 File qrFile = new File(path+"/"+shopId+"qr.jpeg");
					 FileOutputStream out = new FileOutputStream(qrFile);
					 byte[] buffer = new byte[1024];
					    int read;
					    while ((read = in.read(buffer)) != -1) {
					        out.write(buffer, 0, read);
					    }
					    
					    in.close();
					    out.close();
					    pdImageQRCode = PDImageXObject.createFromFile(path+"/"+shopId+"qr.jpeg", document);
					 }
	         
					 
					
	         Table header;
	         Table billInfoTable;
	         Table custInfoTable;
	         Table itemInfo;
	         
	         
	         TableBuilder tableHeaderBuilder = Table.builder()
		                .addColumnsOfWidth(555)				                
		                .font(HELVETICA)
		                .borderColor(Color.BLACK)
		                .horizontalAlignment(CENTER);
	         
	         Row billType = Row.builder()
		                .add(TextCell.builder().text("DELIVERY CHALLAN").fontSize(12).horizontalAlignment(CENTER).borderWidthTop(0.0F).borderWidthLeft(0.0F).borderWidthRight(0.0F).borderWidthBottom(0.5F).lineSpacing(1F).build())				               
		                .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         
	         Row headerRow  = Row.builder()
		                .add(TextCell.builder().text((String)this.getSessionMap().get("shopnameenglish")).fontSize(15).horizontalAlignment(CENTER).borderWidthLeft(0.5F).borderWidthRight(0.5F).borderWidthBottom(0.0F).lineSpacing(1F).build())				               
		                .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         
	         Row addressRow  = Row.builder()
		                .add(TextCell.builder().text((String)this.getSessionMap().get("shoponelineaddress")).fontSize(10).horizontalAlignment(CENTER).borderWidthLeft(0.5F).borderWidthRight(0.5F).borderWidthBottom(0.0F).lineSpacing(1F).build())				               
		                .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         
	         Row mobileRow  = Row.builder()
		                .add(TextCell.builder().text("Contact: "+(String)this.getSessionMap().get("shopcustcarenumber")).fontSize(10).horizontalAlignment(CENTER).borderWidthLeft(0.5F).borderWidthRight(0.5F).borderWidthBottom(0.0F).lineSpacing(1F).build())				               
		                .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         
	         
	         String gstNumber = (String)this.getSessionMap().get("shopgstnumber");
	         if(gstNumber == null || gstNumber.equals(""))
	         {
	        	 gstNumber = "NA";
	         }
	         
	         Row gstRow  = Row.builder()
		                .add(TextCell.builder().text("GSTIN: "+gstNumber).fontSize(10).horizontalAlignment(CENTER).borderWidthLeft(0.5F).borderWidthRight(0.5F).borderWidthBottom(0.5F).lineSpacing(1F).build())				               
		                .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         
	         
	         tableHeaderBuilder.addRow(billType);
	         
	         tableHeaderBuilder.addRow(headerRow);
	         if(addIntroLine.equals("TRUE"))
	         {
	        	 Row headerIntroRow  = Row.builder()
			                .add(TextCell.builder().text(headIntroLineContent).fontSize(10).horizontalAlignment(CENTER).borderWidthLeft(0.5F).borderWidthRight(0.5F).borderWidthBottom(0.0F).lineSpacing(1F).build())				               
			                .backgroundColor(WHITE)
			                .font(PDType1Font.HELVETICA_BOLD)	
			                .textColor(Color.BLACK)
			                .build();
	        	 
	        	 tableHeaderBuilder.addRow(headerIntroRow);
	        	 
	         }
	         tableHeaderBuilder.addRow(addressRow);
	         tableHeaderBuilder.addRow(mobileRow);
	         tableHeaderBuilder.addRow(gstRow);
	         
	       
	         
	         header = tableHeaderBuilder.build();
	         
	         float startY = page.getMediaBox().getHeight() - PADDING20;
	         
	         if(addPageHeader)
	         {
		         TableDrawer.builder()
	                .contentStream(contentStream)
	                .table(header)
	                .startX(PADDING20)
	                .startY(startY)
	                .build()
	                .draw();
	         }
	         
	         TableBuilder tableBillInfoBuilder = Table.builder()
		                .addColumnsOfWidth(50,50,60,80)				                
		                .font(HELVETICA)
		                .borderColor(Color.BLACK)
		                .horizontalAlignment(CENTER);
	         
	         
	         Row billInfoRow1  = Row.builder()
		                .add(TextCell.builder().text("Challan No:"+this.getChallanNumber()).colSpan(2).fontSize(10).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text("Challan By").fontSize(10).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text(this.getChallanInfoDAO().getChallanBy()).fontSize(10).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         
	         
	         Row billInfoRow2  = Row.builder()
	        		 	.add(TextCell.builder().text("Date:"+this.getChallanInfoDAO().getChallanDate()).colSpan(4).fontSize(10).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
	        		 	.backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();

	         Row billInfoRow3  = Row.builder()
		                .add(TextCell.builder().text("Payment Type").colSpan(3).fontSize(10).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text(this.getChallanInfoDAO().getPaymentType()).fontSize(10).horizontalAlignment(LEFT).borderWidth(1.0F).lineSpacing(1F).build())				               
					    .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         
	         tableBillInfoBuilder.addRow(billInfoRow1);
	         tableBillInfoBuilder.addRow(billInfoRow2);
	         tableBillInfoBuilder.addRow(billInfoRow3);
	         
	         billInfoTable = tableBillInfoBuilder.build();
	         
	         
	         float currentY = startY - 90;
	         
	         if(addIntroLine.equals("TRUE"))
	         {
	        	 currentY = startY - 110; 
	         }
	          
	      	 if(!addPageHeader)
        	 {
	      		currentY = startY - 160;
        	 }
 	         
	         
	         TableDrawer.builder()
             .contentStream(contentStream)
             .table(billInfoTable)
             .startX(PADDING20 + 295F)
             .startY(currentY)
             .build()
             .draw();
	         
	         
	         TableBuilder tableCustInfoBuilder = Table.builder()
		                .addColumnsOfWidth(295);
	         
	         Row custInfoRow1 = Row.builder()
		                .add(TextCell.builder().text("Customer Name : "+this.getChallanInfoDAO().getCustomerName()).fontSize(10).horizontalAlignment(LEFT).borderWidthLeft(0.5F).borderWidthTop(0.5F).borderWidthRight(0.5F).lineSpacing(0.5F).build())				               
		                .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         Row custInfoRow2 = Row.builder()
		                .add(TextCell.builder().text("Mobile : "+custMobile).fontSize(10).horizontalAlignment(LEFT).borderWidthLeft(0.5F).borderWidthRight(0.5F).lineSpacing(0.5F).build())				               
		                .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         Row custInfoRow3 = Row.builder()
		                .add(TextCell.builder().text("Address : "+custAddress).fontSize(10).horizontalAlignment(LEFT).borderWidthLeft(0.5F).borderWidthRight(0.5F).borderWidthBottom(0.5F).lineSpacing(0.5F).build())				               
		                .backgroundColor(WHITE)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         
	    
	         
	         tableCustInfoBuilder.addRow(custInfoRow1);
	         tableCustInfoBuilder.addRow(custInfoRow2);
	         tableCustInfoBuilder.addRow(custInfoRow3);
	         
	         custInfoTable = tableCustInfoBuilder.build();
	         

	         
	         
	         TableDrawer.builder()
             .contentStream(contentStream)
             .table(custInfoTable)
             .startX(PADDING20)
             .startY(currentY)
             .build()
             .draw();
	         
	         TableBuilder tableItemInfoBuilder ;
	         
	         Row inteInfoHeaderRow;
	         Row inteInfoHeaderRow2;
	         
	         // First pass: Determine if it's IGST or CGST+SGST scenario
	         boolean isIGSTApplicable = false;
	         float checkTotalIGST = 0.0F;
	         float checkTotalCGST = 0.0F;
	         float checkTotalSGST = 0.0F;
	         
	         for(int i = 0; i < this.getProductInfoList().size(); i++)
	         {
	        	 ChallanProductInfoDAO checkDAO = this.getProductInfoList().get(i);
	        	 if(checkDAO != null)
	        	 {
	        		 checkTotalIGST += checkDAO.getIgst();
	        		 checkTotalCGST += checkDAO.getCgst();
	        		 checkTotalSGST += checkDAO.getSgst();
	        	 }
	         }
	         
	         // If CGST or SGST > 0, it's intra-state (CGST+SGST scenario)
	         // Otherwise if IGST > 0, it's inter-state (IGST scenario)
	       
	         // GST TABLE HEADERS - Conditional based on IGST or CGST+SGST
	         if(isIGSTApplicable)
	         {
	        	 // IGST Scenario - Inter-state
	        	 tableItemInfoBuilder =  Table.builder()
	        		 .addColumnsOfWidth(25,150,35,30,60,30,50,90);
	        	 
	        	 inteInfoHeaderRow = Row.builder()
		                .add(TextCell.builder().text("Sr\nNo").rowSpan(2).fontSize(7).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text("Description of Goods\nHSN Code").rowSpan(2).fontSize(7).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text("Unit\nPrice").rowSpan(2).fontSize(7).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text("Qty").rowSpan(2).fontSize(7).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text("Taxable\nAmount").rowSpan(2).fontSize(7).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
			            .add(TextCell.builder().text("IGST").colSpan(2).fontSize(7).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
			            .add(TextCell.builder().text("Total\nAmount").rowSpan(2).fontSize(7).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .backgroundColor(GRAY_LIGHT_3)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         
		         inteInfoHeaderRow2 = Row.builder()
		                .add(TextCell.builder().text("Rate\n%").fontSize(6).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text("Amount").fontSize(6).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .backgroundColor(GRAY_LIGHT_3)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         }
	         else
	         {
	        	 // CGST+SGST Scenario - Intra-state
	        	 tableItemInfoBuilder =  Table.builder()
	        		 .addColumnsOfWidth(25,140,35,30,55,25,80,25,80,60);
	        	 
	        	 inteInfoHeaderRow = Row.builder()
		                .add(TextCell.builder().text("Sr\nNo").rowSpan(2).fontSize(7).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text("Description of Goods\nHSN Code").rowSpan(2).fontSize(7).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text("Unit\nPrice").rowSpan(2).fontSize(7).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text("Qty").rowSpan(2).fontSize(7).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text("Taxable\nAmount").rowSpan(2).fontSize(7).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
			            .add(TextCell.builder().text("SGST").colSpan(2).fontSize(7).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
			            .add(TextCell.builder().text("CGST").colSpan(2).fontSize(7).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
			            .add(TextCell.builder().text("Total\nAmount").rowSpan(2).fontSize(7).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .backgroundColor(GRAY_LIGHT_3)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         
		         inteInfoHeaderRow2 = Row.builder()
		                .add(TextCell.builder().text("Rate\n%").fontSize(6).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text("Amount").fontSize(6).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text("Rate\n%").fontSize(6).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .add(TextCell.builder().text("Amount").fontSize(6).horizontalAlignment(CENTER).borderWidth(1.0F).lineSpacing(1F).build())				               
		                .backgroundColor(GRAY_LIGHT_3)
		                .font(PDType1Font.HELVETICA_BOLD)	
		                .textColor(Color.BLACK)
		                .build();
	         }
	         
	         tableItemInfoBuilder.addRow(inteInfoHeaderRow);
	         tableItemInfoBuilder.addRow(inteInfoHeaderRow2);
	         

	         int srNo = 1;
	         int pageCount = 1;
	         int listCount = this.getProductInfoList().size();
	         
	         float totalTaxableAmount = 0.0F;
	         float totalSGST = 0.0F;
	         float totalCGST = 0.0F;
	         float totalIGST = 0.0F;
	         
	         
	     	for(int i =0;i<listCount;i++)
				{
	     		
	     		//System.out.println("Starting Current Y :"+currentY);
					Row itemDetails = null;
					
				if(currentY  < 250)
				{
					int colSpan = isIGSTApplicable ? 8 : 10;  // Conditional column span
					
					Row toBeContinue =  Row.builder()
		        		 	.add(TextCell.builder().text("Page No."+pageCount+", To be continued on next page...").colSpan(colSpan).fontSize(9).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
		        		 	.textColor(Color.BLACK)
			                .height(20F)
			                .font(fontMarathi)
			                .build();
					pageCount++;
						
						tableItemInfoBuilder.addRow(toBeContinue);
					
						itemInfo = tableItemInfoBuilder.build();
						
						float startY2 = page.getMediaBox().getHeight() - PADDING20;
						
			        	float currentY2 = startY2 - 140 ; 
			        	
			        	 if(addIntroLine.equals("TRUE"))
			 	         {
			 	        	 currentY2 = startY2 - 160; 
			 	         }
			        	 
			        	 if(!addPageHeader)
			        	 {
			        		 currentY2 = startY2 - 210;  
			        	 }
			        	 
			        	 if(addPageHeader)
				         {
				         
					         
					         if(pdImageLeftLogoImage != null)
				        	 {
					        	
				        		 contentStream.drawImage(pdImageLeftLogoImage, 30, 750 , 35, 35);
				        		
				        	 }
				
					         if(pdImageRightLogoImage != null)
				        	 {
					        	
				        		 contentStream.drawImage(pdImageRightLogoImage, 540, 750 , 35, 35);
				        		
				        	 }
				         }
						 
			 	         
			        	
						
						 TableDrawer.builder()
			             .contentStream(contentStream)
			             .table(itemInfo)
			             .startX(PADDING20)
			             .startY(currentY2)
			             .build()
			             .draw();
						
						 contentStream.close();
						 contentStream = null;
				        	PDPage newPage = new PDPage(PDRectangle.A4);
							
				        	 
				        	 contentStream = new PDPageContentStream(document,newPage);
				        	currentY = 20.0F;
				             document.addPage( newPage );
				             
					          startY = newPage.getMediaBox().getHeight() - PADDING20;
					          
					          if(addPageHeader)
						         {
				             TableDrawer.builder()
				                .contentStream(contentStream)
				                .table(header)
				                .startX(PADDING20)
				                .startY(startY)
				                .build()
				                .draw();
						         }
				             
				             
					         currentY = startY - 100;
					         
					         if(addIntroLine.equals("TRUE"))
				 	         {
				 	        	 currentY = startY - 120; 
				 	         }
					         
					         if(!addPageHeader)
				        	 {
					      		currentY = startY - 170;
				        	 }
					         
				        	 TableDrawer.builder()
				             .contentStream(contentStream)
				             .table(custInfoTable)
				             .startX(PADDING20)
				             .startY(currentY)
				             .build()
				             .draw();
				        	 
					         TableDrawer.builder()
				             .contentStream(contentStream)
				             .table(billInfoTable)
				             .startX(315)
				             .startY(currentY)
				             .build()
				             .draw();
					         
				         currentY = currentY - 60;
				         
				       
				         itemInfo = null;
				         tableItemInfoBuilder = null;
				         
				         // Rebuild table with conditional columns
			         if(isIGSTApplicable)
				         {
				        	 tableItemInfoBuilder =  Table.builder()
				        		 .addColumnsOfWidth(25,150,35,30,60,30,50,90);
				         }
				         else
				         {
			        	 tableItemInfoBuilder =  Table.builder()
			        		 .addColumnsOfWidth(25,140,35,30,55,25,80,25,80,60);
				         }
				         
				         tableItemInfoBuilder.addRow(inteInfoHeaderRow);
				         tableItemInfoBuilder.addRow(inteInfoHeaderRow2);
				}
					
					
						ChallanProductInfoDAO informationDAO = null;
						
						try						
						{
							informationDAO = this.getProductInfoList().get(i);
						}catch (Exception e) {}
						if(informationDAO != null)
						{
						
							float halfTaxRate = informationDAO.getTaxPercentage() / 2.0F;
							
							String hsnCode = informationDAO.getHsnCode();
							if(hsnCode == null || hsnCode.equals(""))
							{
								hsnCode = "NA";
							}
							
					totalTaxableAmount = totalTaxableAmount + informationDAO.getGrossAmount();
						totalSGST = totalSGST + informationDAO.getSgst();
						totalCGST = totalCGST + informationDAO.getCgst();
						totalIGST = totalIGST + informationDAO.getIgst();
						
						// Build row conditionally based on IGST or CGST+SGST
						if(isIGSTApplicable)
						{
							// IGST Scenario - Inter-state
							float derivedUnitPrice = 0.0F;
							if(informationDAO.getQuantity() > 0)
							{
								derivedUnitPrice = informationDAO.getGrossAmount() / informationDAO.getQuantity();
							}
							itemDetails = Row.builder()
					        		 	.add(TextCell.builder().text(""+srNo).fontSize(8).horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())				               
					        		 	.add(TextCell.builder().text(informationDAO.getProductName()+"\n[ HSN Code - "+hsnCode+" ]").fontSize(7).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
						                .add(TextCell.builder().text(""+df.format(derivedUnitPrice)).fontSize(7).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
						                .add(TextCell.builder().text(""+df.format(informationDAO.getQuantity())).fontSize(7).horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())				               
						                .add(TextCell.builder().text(""+df.format(informationDAO.getGrossAmount())).fontSize(7).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
						                .add(TextCell.builder().text(""+df.format(informationDAO.getTaxPercentage())).fontSize(7).horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())				               
							            .add(TextCell.builder().text(""+df.format(informationDAO.getIgst())).fontSize(7).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
							            .add(TextCell.builder().text(""+df.format(informationDAO.getAmountAfterDiscount())).fontSize(7).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
							            .backgroundColor(WHITE)
						                .textColor(Color.BLACK)
						                .height(25F)
						                .font(fontMarathi).fontSize(8)				              
						                .horizontalAlignment(CENTER)
						                .build();
						}
						else
						{
							// CGST+SGST Scenario - Intra-state
							float derivedUnitPrice = 0.0F;
							if(informationDAO.getQuantity() > 0)
							{
								derivedUnitPrice = informationDAO.getGrossAmount() / informationDAO.getQuantity();
							}
							itemDetails = Row.builder()
					        		 	.add(TextCell.builder().text(""+srNo).fontSize(8).horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())				               
					        		 	.add(TextCell.builder().text(informationDAO.getProductName()+"\n[ HSN Code - "+hsnCode+" ]").fontSize(7).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
						                .add(TextCell.builder().text(""+df.format(derivedUnitPrice)).fontSize(7).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
						                .add(TextCell.builder().text(""+df.format(informationDAO.getQuantity())).fontSize(7).horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())				               
						                .add(TextCell.builder().text(""+df.format(informationDAO.getGrossAmount())).fontSize(7).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
						                .add(TextCell.builder().text(""+df.format(halfTaxRate)).fontSize(7).horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())				               
							            .add(TextCell.builder().text(""+df.format(informationDAO.getSgst())).fontSize(7).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
							            .add(TextCell.builder().text(""+df.format(halfTaxRate)).fontSize(7).horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())				               
							            .add(TextCell.builder().text(""+df.format(informationDAO.getCgst())).fontSize(7).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
							            .add(TextCell.builder().text(""+df.format(informationDAO.getAmountAfterDiscount())).fontSize(7).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
							            .backgroundColor(WHITE)
						                .textColor(Color.BLACK)
						                .height(25F)
						                .font(fontMarathi).fontSize(8)				              
						                .horizontalAlignment(CENTER)
						                .build();
						}
				         srNo++;
					         
						}
					
			         tableItemInfoBuilder.addRow(itemDetails);
			        
			         currentY = (float) (currentY - 25.0F);
					//System.out.println("Inner : "+currentY);
					
				}
				DecimalFormat df2 = new DecimalFormat("#.##");
	     	
			
	     	 // Add Customer Outstanding Amount if enabled
	     	 if(addCustomerCreditAmountToBill)
	         {
	     		float custCreditAmount = GetCustomerCreditAmountById.getCustomerCreditAmountById(this.getChallanInfoDAO().getCustId(), this.getSessionMap());
	        	
	     		// Row 1: Totals aligned with conditional columns
	     		Row totalInfoRowAfterOutstanding;
	     		if(isIGSTApplicable)
	     		{
	     			totalInfoRowAfterOutstanding = Row.builder()
	        		 	.add(TextCell.builder().text("Total:").colSpan(4).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())
	        		 	.add(TextCell.builder().text(df2.format(totalTaxableAmount)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())
	        		 	.add(TextCell.builder().text("").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())
	        		 	.add(TextCell.builder().text(df2.format(totalIGST)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())
	        		 	.add(TextCell.builder().text(df2.format(floatTotalAmtAfterDiscount)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())
	        		 	.backgroundColor(WHITE)
		                .textColor(Color.BLACK)
		                .height(20F)
		                .font(fontMarathiBold).fontSize(9)
		                .horizontalAlignment(CENTER)
		                .build();
	     		}
	     		else
	     		{
	     			totalInfoRowAfterOutstanding = Row.builder()
	        		 	.add(TextCell.builder().text("Total:").colSpan(4).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())
	        		 	.add(TextCell.builder().text(df2.format(totalTaxableAmount)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())
	        		 	.add(TextCell.builder().text("").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())
	        		 	.add(TextCell.builder().text(df2.format(totalSGST)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())
	        		 	.add(TextCell.builder().text("").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())
	        		 	.add(TextCell.builder().text(df2.format(totalCGST)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())
	        		 	.add(TextCell.builder().text(df2.format(floatTotalAmtAfterDiscount)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())
	        		 	.backgroundColor(WHITE)
		                .textColor(Color.BLACK)
		                .height(20F)
		                .font(fontMarathiBold).fontSize(9)
		                .horizontalAlignment(CENTER)
		                .build();
	     		}
	     		tableItemInfoBuilder.addRow(totalInfoRowAfterOutstanding);

	     		// Row 2: Outstanding amount below totals
	     		Row outstandingRow;
	     		if(isIGSTApplicable)
	     		{
	     			outstandingRow = Row.builder()
	        		 	.add(TextCell.builder().text("Total OutStanding Amount: Rs. "+df2.format(custCreditAmount)).colSpan(8).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
	        		 	.backgroundColor(WHITE)
		                .textColor(Color.BLACK)
		                .height(20F)
		                .font(fontMarathiBold).fontSize(9)
		                .horizontalAlignment(CENTER)
		                .build();
	     		}
	     		else
	     		{
	     			outstandingRow = Row.builder()
	        		 	.add(TextCell.builder().text("Total OutStanding Amount: Rs. "+df2.format(custCreditAmount)).colSpan(10).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
	        		 	.backgroundColor(WHITE)
		                .textColor(Color.BLACK)
		                .height(20F)
		                .font(fontMarathiBold).fontSize(9)
		                .horizontalAlignment(CENTER)
		                .build();
	     		}
	     		tableItemInfoBuilder.addRow(outstandingRow);
	         }
	     	 else
	     	 {
	     		Row totalInfoRow;
	     		if(isIGSTApplicable)
	     		{
	     			// IGST Scenario
	     			totalInfoRow = Row.builder()
	        		 	.add(TextCell.builder().text("Total:").colSpan(4).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        		 	.add(TextCell.builder().text(df2.format(totalTaxableAmount)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        		 	.add(TextCell.builder().text("").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        		 	.add(TextCell.builder().text(df2.format(totalIGST)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        		 	.add(TextCell.builder().text(df2.format(floatTotalAmtAfterDiscount)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        		 	.backgroundColor(WHITE)
		                .textColor(Color.BLACK)
		                .height(20F)
		                .font(fontMarathiBold).fontSize(9)				              
		                .horizontalAlignment(CENTER)
		                .build();
	     		}
	     		else
	     		{
	     			// CGST+SGST Scenario
	     			totalInfoRow = Row.builder()
	        		 	.add(TextCell.builder().text("Total:").colSpan(4).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        		 	.add(TextCell.builder().text(df2.format(totalTaxableAmount)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        		 	.add(TextCell.builder().text("").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        		 	.add(TextCell.builder().text(df2.format(totalSGST)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        		 	.add(TextCell.builder().text("").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        		 	.add(TextCell.builder().text(df2.format(totalCGST)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        		 	.add(TextCell.builder().text(df2.format(floatTotalAmtAfterDiscount)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        		 	.backgroundColor(WHITE)
		                .textColor(Color.BLACK)
		                .height(20F)
		                .font(fontMarathiBold).fontSize(9)				              
		                .horizontalAlignment(CENTER)
		                .build();
	     		}
     			tableItemInfoBuilder.addRow(totalInfoRow);
	     	 }
				
				 
	     	
				
			  String convertedAmount = new NumberToWordsConverter().getConverted(""+floatTotalAmtAfterDiscount);
	      
	      		  int colSpanForFullRow = isIGSTApplicable ? 8 : 10;  // Conditional column span
	      		  
			  Row amtInwordsRow = Row.builder()
	        		 	.add(TextCell.builder().text("Amount in Words : "+convertedAmount).colSpan(colSpanForFullRow).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
	        		 	.backgroundColor(WHITE)
		                .textColor(Color.BLACK)
		                .height(20F)
		                .font(fontMarathi).fontSize(9)				              
		                .horizontalAlignment(CENTER)
		                .build();
			tableItemInfoBuilder.addRow(amtInwordsRow);
			String comm = this.getChallanInfoDAO().getComments();
			
			if(comm == null)
			{
				comm = "";
			}
			Row commentsRow = Row.builder()
        		 	.add(TextCell.builder().text("Comments:"+comm).colSpan(colSpanForFullRow).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				               
        		 	.backgroundColor(WHITE)
		                .textColor(Color.BLACK)
		                .height(20F)
		                .font(fontMarathi).fontSize(8)				              
		                .horizontalAlignment(LEFT)
		                .build();
			tableItemInfoBuilder.addRow(commentsRow);
	         
	         itemInfo = tableItemInfoBuilder.build();
	         
	         Table bankDetailsTable = null;
		       
		       
		       TableBuilder bankDetailsTableBuilder = Table.builder()
			                .addColumnsOfWidth(149,149)				                
			                .font(HELVETICA)
			                .borderWidth(1)
			                .borderColor(Color.BLACK);
		       
		       Row bankDetailsHeader = Row.builder()
		                .add(TextCell.builder().text("BANK DETAILS").colSpan(2).horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())				                
		                .backgroundColor(Color.WHITE)
		                .textColor(Color.BLACK)
		                .font(PDType1Font.HELVETICA_BOLD)
		                .fontSize(9)				              
		                .horizontalAlignment(CENTER)
		                .height(15F)	
		                .build();
		       bankDetailsTableBuilder.addRow(bankDetailsHeader);
		       
		       Row bankDetailsRow1 = Row.builder()
		                .add(TextCell.builder().text("BANKING NAME").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
		                .add(TextCell.builder().text(bankDetails.get("bankingname")).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
		                .backgroundColor(Color.WHITE)
		                .textColor(Color.BLACK)
		                .font(PDType1Font.HELVETICA)
		                .fontSize(8)				              
		                .horizontalAlignment(CENTER)
		                .height(15F)	
		                .build();
		       bankDetailsTableBuilder.addRow(bankDetailsRow1);
		       
		       Row bankDetailsRow2 = Row.builder()
		                .add(TextCell.builder().text("NAME OF THE BANK").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
		                .add(TextCell.builder().text(bankDetails.get("bankname")).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
		                .backgroundColor(Color.WHITE)
		                .textColor(Color.BLACK)
		                .font(PDType1Font.HELVETICA)
		                .fontSize(8)				              
		                .horizontalAlignment(CENTER)
		                .height(15F)	
		                .build();
		       bankDetailsTableBuilder.addRow(bankDetailsRow2);
		       	Row bankDetailsRow3 = Row.builder()
		                .add(TextCell.builder().text("BRANCH").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
		                .add(TextCell.builder().text(bankDetails.get("branchname")).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
		                .backgroundColor(Color.WHITE)
		                .textColor(Color.BLACK)
		                .font(PDType1Font.HELVETICA)
		                .fontSize(8)				              
		                .horizontalAlignment(CENTER)
		                .height(15F)	
		                .build();
		       	bankDetailsTableBuilder.addRow(bankDetailsRow3);
		       	Row bankDetailsRow4 = Row.builder()
		                .add(TextCell.builder().text("ACCOUNT NUMBER").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())				                
		                .add(TextCell.builder().text(bankDetails.get("accnumber")).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
		                .backgroundColor(Color.WHITE)
		                .textColor(Color.BLACK)
		                .font(PDType1Font.HELVETICA)
		                .fontSize(8)				              
		                .horizontalAlignment(CENTER)
		                .height(15F)	
		                .build();
		       	bankDetailsTableBuilder.addRow(bankDetailsRow4);
		       	Row bankDetailsRow5 = Row.builder()
		                .add(TextCell.builder().text("IFSC CODE").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(10F).build())				                
		                .add(TextCell.builder().text(bankDetails.get("ifsccode")).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(10F).build())
		                .backgroundColor(Color.WHITE)
		                .textColor(Color.BLACK)
		                .font(PDType1Font.HELVETICA)
		                .fontSize(8)				              
		                .horizontalAlignment(CENTER)
		                .height(15F)	
		                .build();
		       	bankDetailsTableBuilder.addRow(bankDetailsRow5);
	        
		         bankDetailsTable = bankDetailsTableBuilder.build();
		         
		         // Build totals table to be shown beside Bank Details (right side)
		         TableBuilder totalDetailsBuilder = Table.builder()
		                 .addColumnsOfWidth(157,100)
		                 .font(HELVETICA)
		                 .borderWidth(1)
		                 .borderColor(Color.BLACK);
		         
		         Row totalDetailsRow1 = Row.builder()
		                 .add(TextCell.builder().text("Total Taxable Amount").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(10F).build())
		                 .add(TextCell.builder().text(df2.format(totalTaxableAmount)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(10F).build())
		                 .backgroundColor(Color.WHITE)
		                 .textColor(Color.BLACK)
		                 .font(PDType1Font.HELVETICA_BOLD)
		                 .fontSize(9)
		                 .horizontalAlignment(CENTER)
		                 .height(15F)
		                 .build();
		         totalDetailsBuilder.addRow(totalDetailsRow1);
		         
		         Row totalDetailsRow2 = Row.builder()
		                 .add(TextCell.builder().text("Total SGST Amount").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(10F).build())
		                 .add(TextCell.builder().text(df2.format(totalSGST)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(10F).build())
		                 .backgroundColor(Color.WHITE)
		                 .textColor(Color.BLACK)
		                 .font(PDType1Font.HELVETICA_BOLD)
		                 .fontSize(9)
		                 .horizontalAlignment(CENTER)
		                 .height(15F)
		                 .build();
		         totalDetailsBuilder.addRow(totalDetailsRow2);
		         
		         Row totalDetailsRow3 = Row.builder()
		                 .add(TextCell.builder().text("Total CGST Amount").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(10F).build())
		                 .add(TextCell.builder().text(df2.format(totalCGST)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(10F).build())
		                 .backgroundColor(Color.WHITE)
		                 .textColor(Color.BLACK)
		                 .font(PDType1Font.HELVETICA_BOLD)
		                 .fontSize(9)
		                 .horizontalAlignment(CENTER)
		                 .height(15F)
		                 .build();
		         totalDetailsBuilder.addRow(totalDetailsRow3);
		         
		         Row totalDetailsRow4 = Row.builder()
		                 .add(TextCell.builder().text("Total IGST Amount").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(10F).build())
		                 .add(TextCell.builder().text(df2.format(totalIGST)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(10F).build())
		                 .backgroundColor(Color.WHITE)
		                 .textColor(Color.BLACK)
		                 .font(PDType1Font.HELVETICA_BOLD)
		                 .fontSize(9)
		                 .horizontalAlignment(CENTER)
		                 .height(15F)
		                 .build();
		         totalDetailsBuilder.addRow(totalDetailsRow4);
		         
		         float totalTax = totalSGST + totalCGST + totalIGST;
		         Row totalDetailsRow5 = Row.builder()
		                 .add(TextCell.builder().text("Total TAX Amount").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(10F).build())
		                 .add(TextCell.builder().text(df2.format(totalTax)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(10F).build())
		                 .backgroundColor(Color.WHITE)
		                 .textColor(Color.BLACK)
		                 .font(PDType1Font.HELVETICA_BOLD)
		                 .fontSize(9)
		                 .horizontalAlignment(CENTER)
		                 .height(15F)
		                 .build();
		         totalDetailsBuilder.addRow(totalDetailsRow5);
		         
		         float roundOff = 0.0F;
		         float roundOffAmount = Math.round(floatTotalAmtAfterDiscount);
		         roundOff = roundOffAmount - floatTotalAmtAfterDiscount;
		         Row totalDetailsRow6 = Row.builder()
		                 .add(TextCell.builder().text("Round Off").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(10F).build())
		                 .add(TextCell.builder().text(df2.format(roundOff)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(10F).build())
		                 .backgroundColor(Color.WHITE)
		                 .textColor(Color.BLACK)
		                 .font(PDType1Font.HELVETICA_BOLD)
		                 .fontSize(9)
		                 .horizontalAlignment(CENTER)
		                 .height(15F)
		                 .build();
		         totalDetailsBuilder.addRow(totalDetailsRow6);
		         
		         Row totalDetailsRow7 = Row.builder()
		                 .add(TextCell.builder().text("Total Amount After TAX").horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(10F).build())
		                 .add(TextCell.builder().text(df2.format(floatTotalAmtAfterDiscount)).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(10F).build())
		                 .backgroundColor(Color.WHITE)
		                 .textColor(Color.BLACK)
		                 .font(PDType1Font.HELVETICA_BOLD)
		                 .fontSize(9)
		                 .horizontalAlignment(CENTER)
		                 .height(15F)
		                 .build();
		         totalDetailsBuilder.addRow(totalDetailsRow7);
		         
		         final Table totalAmountDetailsLocal = totalDetailsBuilder.build();
		         
	         
	         TableBuilder signatureBuiler = Table.builder()
		                .addColumnsOfWidth(185,185,185)				                
		                .font(HELVETICA)
		                .borderColor(Color.BLACK)
		                .horizontalAlignment(CENTER);
	         
	         Row signRow1 = Row.builder()
	        		 	.add(TextCell.builder().text("").verticalAlignment(BOTTOM).horizontalAlignment(CENTER).borderWidthTop(0.5F).borderWidthLeft(0.5F).borderWidthRight(0.5F).lineSpacing(0.5F).build())
		                .add(TextCell.builder().text("").verticalAlignment(BOTTOM).horizontalAlignment(CENTER).borderWidthTop(0.5F).borderWidthLeft(0.5F).borderWidthRight(0.5F).lineSpacing(0.5F).build())
		                .add(TextCell.builder().text("For "+(String)this.getSessionMap().get("shopnameenglish")).verticalAlignment(BOTTOM).horizontalAlignment(CENTER).borderWidthTop(0.5F).borderWidthLeft(0.5F).borderWidthRight(0.5F).lineSpacing(0.5F).build())					               
		                .backgroundColor(Color.WHITE)
		                .textColor(Color.BLACK)
		                .font(PDType1Font.HELVETICA_BOLD)
		                .fontSize(10)				              
		                .horizontalAlignment(CENTER)
		                .height(25F)	
		                .build();
	         signatureBuiler.addRow(signRow1);
	         
	         Row signRow2 = Row.builder()
	        		 	.add(TextCell.builder().text("").verticalAlignment(BOTTOM).horizontalAlignment(CENTER).borderWidthTop(0.0F).borderWidthLeft(0.5F).borderWidthRight(0.5F).borderWidthBottom(0.5F).lineSpacing(1F).build())
		                .add(TextCell.builder().text("Customer Signature").verticalAlignment(BOTTOM).horizontalAlignment(CENTER).borderWidthTop(0.0F).borderWidthLeft(0.5F).borderWidthRight(0.5F).borderWidthBottom(0.5F).lineSpacing(1F).build())
		                .add(TextCell.builder().text("Authorized Signature").verticalAlignment(BOTTOM).horizontalAlignment(CENTER).borderWidthTop(0.0F).borderWidthLeft(0.5F).borderWidthRight(0.5F).borderWidthBottom(0.5F).lineSpacing(1F).build())					               
		                .backgroundColor(Color.WHITE)
		                .textColor(Color.BLACK)
		                .font(PDType1Font.HELVETICA_BOLD)
		                .fontSize(10)				              
		                .horizontalAlignment(CENTER)
		                .height(60F)	
		                .build();
	         signatureBuiler.addRow(signRow2);
	       
	         
	         Table signature = signatureBuiler.build();   
	         
	      
	         itemInfo = tableItemInfoBuilder.build();
	         
        	float startY2 = page.getMediaBox().getHeight() - PADDING20;
        	float currentY2 = startY2 - 145 ; 
        	
        	
        	if(addIntroLine.equals("TRUE"))
 	         {
        		currentY2 = startY2 - 165; 
 	         }
        	
        	if(!addPageHeader)
        	{
        		currentY2 = startY2 - 215; 
        	}
        	 
         TableDrawer.builder()
            .contentStream(contentStream)
            .table(itemInfo)
            .startX(PADDING20)
            .startY(currentY2)
            .build()
            .draw();

        // Dynamically position following sections to avoid overlap
        float bankStartY = currentY2 - itemInfo.getHeight() - 12F;
        if (bankStartY < 80F) {
            bankStartY = 80F; // prevent drawing too close to page bottom
        }

       TableDrawer.builder()
       .contentStream(contentStream)
       .table(bankDetailsTable)
       .startX(PADDING20)
       .startY(bankStartY)
       .build()
       .draw();

       // Align totals table so its right edge matches the signature area width (185+185+185)
       final float totalAreaWidth = 185F + 185F + 185F;
       final float totalsStartX = PADDING20 + totalAreaWidth - totalAmountDetailsLocal.getWidth();
       TableDrawer.builder()
       .contentStream(contentStream)
       .table(totalAmountDetailsLocal)
       .startX(totalsStartX)
       .startY(bankStartY)
       .build()
       .draw();

       float signatureStartY = bankStartY - Math.max(bankDetailsTable.getHeight(), totalAmountDetailsLocal.getHeight()) - 12F;
        if (signatureStartY < 40F) {
            signatureStartY = 40F;
        }

        TableDrawer.builder()
            .contentStream(contentStream)
            .table(signature)
            .startX(PADDING20)
            .startY(signatureStartY)
            .build()
            .draw();
	         
	         
	         if(addPageHeader)
	         {
	         
		         
		         if(pdImageLeftLogoImage != null)
	        	 {
		        	
	        		 contentStream.drawImage(pdImageLeftLogoImage, 30, 750 , 35, 35);
	        		
	        	 }

		         if(pdImageRightLogoImage != null)
	        	 {
		        	
	        		 contentStream.drawImage(pdImageRightLogoImage, 540, 750 , 35, 35);
	        		
	        	 }
	         }
			 
	         	contentStream.close();
			 	document.save(path+"/challan.pdf");
		        document.close();
		        
		        

				response.setHeader("Content-disposition", "inline; filename=challan.pdf");
				response.setContentType("application/pdf");
				FileInputStream fileInputStream = new FileInputStream(path+"/challan.pdf");
				PrintWriter out = response.getWriter();  
				int bytes;
				while ((bytes = fileInputStream.read()) != -1) {
					out.write(bytes);
				}
				fileInputStream.close();
				out.flush();
				out.close();
				ttf1.close();
				ttf2.close();
			
	         
	        
				
		        }
		        // END: A4 PRINT WITH GST DETAILS
		        
		        
		      
		        
		        
		        
		       
	         
		         
	         	
				}
				
	        	}
	        	catch (SQLException e) {
	        		e.printStackTrace();
					
				} catch (IOException 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 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 ChallanInformationDAO getChallanInfoDAO() {
		return challanInfoDAO;
	}

	public void setChallanInfoDAO(ChallanInformationDAO challanInfoDAO) {
		this.challanInfoDAO = challanInfoDAO;
	}

	public List<ChallanProductInfoDAO> getProductInfoList() {
		return productInfoList;
	}

	public void setProductInfoList(List<ChallanProductInfoDAO> productInfoList) {
		this.productInfoList = productInfoList;
	}

	public String getChallanNumber() {
		return challanNumber;
	}

	public void setChallanNumber(String challanNumber) {
		this.challanNumber = challanNumber;
	}

	public String getChallanPrintType() {
		return challanPrintType;
	}

	public void setChallanPrintType(String challanPrintType) {
		this.challanPrintType = challanPrintType;
	}

	public String getChallanDateTime() {
		return challanDateTime;
	}

	public void setChallanDateTime(String challanDateTime) {
		this.challanDateTime = challanDateTime;
	}

	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 getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}
	
	
	

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public void setSession(Map<String, Object> map) {
		// TODO Auto-generated method stub
		 sessionMap=(SessionMap<String, Object>)map;  
		
	}

	


}

