<?php
session_start();
 error_reporting(E_ALL);
ini_set('display_errors', '1'); 

$cmpmid=$_SESSION['linkarisecmpmid'];
include("connect.php");
include("removespecial.php");
$brmid=$_SESSION['linkarisebranchid'];
	$umid = $_SESSION['linkariseumid'];
	$invoicemid=$_GET['invoicemid'];
	$eve = "select * from res_invoice_master where invoicemid=$invoicemid and invtype='i'";
	$re = mysqli_query($conn, $eve);
	if(mysqli_num_rows($re) > 0)
	{
		while($rt = mysqli_fetch_assoc($re))
		{
			$_SESSION['invoicedate']=date("d-m-Y", strtotime($rt['invoicedate']));
			$_SESSION['invoicefordisplay']=$rt['invoicefordisplay'];
			$_SESSION['invoicemno']=$rt['invoicemno'];
			$_SESSION['invoicepdf']=$rt['invoicepdf'];
			$_SESSION['invoicerefno']=$rt['invoicerefno'];
			$_SESSION['invoicegrossamount']=$rt['invoicegrossamount'];
			$_SESSION['invoiceroundoffamount']=$rt['invoiceroundoffamount'];
			$_SESSION['invoicenetamount']=$rt['invoicenetamount'];
			$_SESSION['invdeliverylotno']=$rt['invdeliverylotno'];
			$_SESSION['invdestination']=removespecialchar($rt['invdestination']);
			$_SESSION['invdispatchthrough']=removespecialchar($rt['invdispatchthrough']);
			$_SESSION['invorderno']=$rt['invorderno'];
			$_SESSION['invoiceterms']=$rt['invoiceterms'];
			$_SESSION['invremarks']=$rt['invremarks'];
			$_SESSION['invshipto']=$rt['invshipto'];
			
			$_SESSION['invewaybillno']=$rt['invewaybillno'];
			$_SESSION['createdby']="";
			$_SESSION['kindatten']="";
			$custid=$rt['customermid'];
			$eve1 = "select * from res_party_master where partymid=$custid";
			$re1 = mysqli_query($conn, $eve1);
			while($rt1 = mysqli_fetch_assoc($re1))
			{
				$_SESSION['cgstno']=$rt1['partygstno'];
				$_SESSION['cname']=removespecialchar($rt1['partyname']);
				$_SESSION['email']=$rt1['partyemail'];
				$_SESSION['mobile']=$rt1['partymobile'];
				$_SESSION['address']=$rt1['partyaddress'];
			
				$_SESSION['city']=$rt1['partycity'];
				$_SESSION['cstate']=$rt1['partystate'];
			}
			 $evechk1 = "select sum(itemdiscamt) as tdiscamt from res_invoice_detail where invoicemid=$invoicemid";
			$rechk11 = mysqli_query($conn, $evechk1);
			while($rtchk11 = mysqli_fetch_assoc($rechk11))
			{
				$_SESSION['tdiscamt']=$rtchk11['tdiscamt'];
			}
			
			$eve_display= "select * from res_company_master where cmpmid=$cmpmid";
			$re_display = mysqli_query($conn, $eve_display);
			while($rt_display = mysqli_fetch_assoc($re_display))
			{
				$_SESSION['headerimg']=$rt_display['cmpheaderimg'];
				$_SESSION['footerimg']=$rt_display['cmpfooterimg'];
				$_SESSION['cmpstate']=$rt_display['cmpstate'];
				$_SESSION['cmpname']=$rt_display['cmpname'];
				$_SESSION['accountname']=$rt_display['accountname'];
				$_SESSION['accountno']=$rt_display['accountno'];
				$_SESSION['ifsccode']=$rt_display['ifsccode'];
				$_SESSION['bankbranchname']=$rt_display['bankbranchname'];
				$_SESSION['gstno']=$rt_display['cmpdetail2'];
				$_SESSION['panno']=$rt_display['cmpdetail1'];
				
			}
		}
	}
require('fpdf.php');

class PDF_MC_Table extends FPDF
{
	var $widths;
	var $aligns;

	function SetWidths($w)
	{
		//Set the array of column widths
		$this->widths=$w;
	}

	function SetAligns($a)
	{
		//Set the array of column alignments
		$this->aligns=$a;
	}

	function Row($data)
	{
		//Calculate the height of the row
		$nb=0;
		for($i=0;$i<count($data);$i++)
			$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
		$h=5*$nb;
		//Issue a page break first if needed
		$this->CheckPageBreak($h);
		//Draw the cells of the row
		for($i=0;$i<count($data);$i++)
		{
			$w=$this->widths[$i];
			$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
			//Save the current position
			$x=$this->GetX();
			$y=$this->GetY();
			//Draw the border
			$this->Rect($x,$y,$w,$h);
			//Print the text
			$this->MultiCell($w,5,$data[$i],0,$a);
			//Put the position to the right of the cell
			$this->SetXY($x+$w,$y);
		}
		//Go to the next line
		$this->Ln($h);
	}

	function CheckPageBreak($h)
	{
		//If the height h would cause an overflow, add a new page immediately
		if($this->GetY()+$h>$this->PageBreakTrigger)
			$this->AddPage($this->CurOrientation);
	}

	function NbLines($w,$txt)
	{
		//Computes the number of lines a MultiCell of width w will take
		$cw=&$this->CurrentFont['cw'];
		if($w==0)
			$w=$this->w-$this->rMargin-$this->x;
		$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
		$s=str_replace("\r",'',$txt);
		$nb=strlen($s);
		if($nb>0 and $s[$nb-1]=="\n")
			$nb--;
		$sep=-1;
		$i=0;
		$j=0;
		$l=0;
		$nl=1;
		while($i<$nb)
		{
			$c=$s[$i];
			if($c=="\n")
			{
				$i++;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
				continue;
			}
			if($c==' ')
				$sep=$i;
			$l+=$cw[$c];
			if($l>$wmax)
			{
				if($sep==-1)
				{
					if($i==$j)
						$i++;
				}
				else
					$i=$sep+1;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
			}
			else
				$i++;
		}
		return $nl;
	}
}
function GenerateWord()
{
    //Get a random word
    $nb=rand(3,10);
    $w='';
    for($i=1;$i<=$nb;$i++)
        $w.=chr(rand(ord('a'),ord('z')));
    return $w;
}

function GenerateSentence()
{
    //Get a random sentence
    $nb=rand(1,10);
    $s='';
    for($i=1;$i<=$nb;$i++)
        $s.=GenerateWord().' ';
    return substr($s,0,-1);
}

/*
$pdf=new PDF_MC_Table();
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
$pdf->SetWidths(array(30,50,30,40));
srand(microtime()*1000000);
for($i=0;$i<20;$i++)
    $pdf->Row(array(GenerateSentence(),GenerateSentence(),GenerateSentence(),GenerateSentence()));
$pdf->Output();

return;
*/


class PDF extends FPDF
{


var $widths;
	var $aligns;

	function SetWidths($w)
	{
		//Set the array of column widths
		$this->widths=$w;
	}

	function SetAligns($a)
	{
		//Set the array of column alignments
		$this->aligns=$a;
	}

	function Row($data)
	{
		//Calculate the height of the row
		$nb=0;
		for($i=0;$i<count($data);$i++)
			$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
		$h=5*$nb;
		//Issue a page break first if needed
		$this->CheckPageBreak($h);
		//Draw the cells of the row
		for($i=0;$i<count($data);$i++)
		{
			$w=$this->widths[$i];
			$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
			//Save the current position
			$x=$this->GetX();
			$y=$this->GetY();
			//Draw the border
			$this->Rect($x,$y,$w,$h);
			//Print the text
			$this->MultiCell($w,5,$data[$i],0,$a);
			//Put the position to the right of the cell
			$this->SetXY($x+$w,$y);
		}
		//Go to the next line
		$this->Ln($h);
	}

	function CheckPageBreak($h)
	{
		//If the height h would cause an overflow, add a new page immediately
		if($this->GetY()+$h>$this->PageBreakTrigger)
			$this->AddPage($this->CurOrientation);
	}

	function NbLines($w,$txt)
	{
		//Computes the number of lines a MultiCell of width w will take
		$cw=&$this->CurrentFont['cw'];
		if($w==0)
			$w=$this->w-$this->rMargin-$this->x;
		$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
		$s=str_replace("\r",'',$txt);
		$nb=strlen($s);
		if($nb>0 and $s[$nb-1]=="\n")
			$nb--;
		$sep=-1;
		$i=0;
		$j=0;
		$l=0;
		$nl=1;
		while($i<$nb)
		{
			$c=$s[$i];
			if($c=="\n")
			{
				$i++;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
				continue;
			}
			if($c==' ')
				$sep=$i;
			$l+=$cw[$c];
			if($l>$wmax)
			{
				if($sep==-1)
				{
					if($i==$j)
						$i++;
				}
				else
					$i=$sep+1;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
			}
			else
				$i++;
		}
		return $nl;
	}


function LoadData($file)
{
    // Read file lines
    $lines = file($file);
    $data = array();
    foreach($lines as $line)
        $data[] = explode(';',trim($line));
    return $data;
}
// Page header
function Header()
{
	
	$headerimg=$_SESSION['headerimg'];
	if(strlen($headerimg)>5){
		 $header = "images/$headerimg";
		$this->Image("$header",8,6,194,30);
	}
	 $this->Ln(20);
	 $this->Ln(8,6,194,30);
}

// Page footer
function Footer()
{
	$this->Ln(20);
	$this->Ln(10,270,190,15);
	$footerimg=$_SESSION['footerimg'];
	if(strlen($footerimg)>5){
		$footer = "images/$footerimg";
		$this->Image("$footer",10,270,190,15);
 	}
}


function ImprovedTable_3()
{
	$kindatten=$_SESSION['kindatten'];
		$w = array(95,95);
		$this->Ln(0);
		$this->SetFont('Arial','',12);
		if($kindatten=='')
		{
		}
		else
		{
			$this->Cell(190,5,"Kind Attn. $kindatten",0,0,'C');
		}
		$_SESSION['disc1']="";
		$disc1=strip_tags($_SESSION['disc1']);
		if($disc1=="")
		{
		}
		else
		{
			$this->SetFont('Arial','B',10);
			$this->Cell(10,5,'',0,0,'L');
			$this->Ln();
			$this->SetFont('Arial','',10);
			$this->MultiCell(0,5,"$disc1");
			$this->SetLineWidth(.3);
			$this->Ln();
		}
}

function ImprovedTable_9()
{
	$_SESSION['disc2']="";
	$w = array(95,95);
	$disc2=strip_tags($_SESSION['disc2']);
	if($disc2=="")
	{
	}
	else
	{
		$this->SetFont('Arial','',10);
		$this->MultiCell(0,5,"$disc2");
		$this->SetLineWidth(.3);
	}
}
function ImprovedTable_4()
{
	include "connect.php";
	$w = array(15,20,40,20,15,20,20,20,20);
	
	if($_SESSION['tdiscamt']>0)
	{
		$this->SetWidths(array(10,18,70,15,17,15,10,12,23));
		$this->SetAligns(array('C','C','C','C','C','C','C','C','C'));
		//$this->SetFillColor(128, 0, 0);
		$this->SetFont('Arial','B',10);
		$this->Row(array('SR. No.','HSN CODE ','PRODUCT DISCRIPTION','QTY','UNIT','RATE','GST (%)','DISC (%)','AMOUNT'));
		$this->SetFont('Arial','',10);

		$this->SetAligns(array('C','L','L','R','R','R','R','R','R'));
		}
		else{
		$this->SetWidths(array(10,18,80,14,15,20,10,23));
		$this->SetAligns(array('C','C','C','C','C','C','C','C'));
		//$this->SetFillColor(128, 0, 0);
		$this->SetFont('Arial','B',10);
		$this->Row(array('SR. No.','HSN CODE ','PRODUCT DISCRIPTION','QTY','UNIT','RATE','GST (%)','AMOUNT'));
		$this->SetFont('Arial','',10);
		$this->SetAligns(array('C','L','L','R','R','R','R','R'));
		}
		$prefinalamount=0;
		$ccount="0";
		$subtotal="0";
		$extra1charges="0";
		$extra2charges="0";
		$extra3charges="0";
		$taxtotal="0";
		$grandtotal="0";
		$taxper=[];
		$taxvalue=[];
		   $eve2 = "select * from res_invoice_detail where invoicemid=".$_GET['invoicemid'];
		
		$re2 = mysqli_query($conn, $eve2);
		while($rt2 = mysqli_fetch_assoc($re2))
		{
			$ccount++;
			$itemid=$rt2['itemid'];
			$itemname=removespecialchar($rt2['itemname']);
			$itemhsncode=$rt2['itemhsncode'];
			$itemiprice=number_format($rt2['itemiprice'],2,'.','');
			$itemamount=$rt2['itemamount'];
			$itemqty=$rt2['itemqty'];
			$itemdesc=removespecialchar($rt2['itemdesc']);
			$itemunit=$rt2['itemunit'];
			$itemdiscper=$rt2['itemdiscper'];
			$itemtotalamount=$rt2['itemtotalamount'];
			$itemgstper=$rt2['itemgstper'];
			$gst=$rt2['itemgstper'];
			$itemgstamount=$rt2['itemgstamount'];
			$itemunit="";
			$eve21 = "select * from res_item_master where itemid=".$itemid;
		$re21 = mysqli_query($conn, $eve21);
		while($rt21 = mysqli_fetch_assoc($re21))
		{
			$itemunit=$rt21['itemunit'];
		}
			if($itemgstper>0)
			{	$gstflag=0;
				if(isset($taxper))
				{
					for($j=0;$j<sizeof($taxper);$j++)
					{
						if($taxper[$j]==$itemgstper)
						{
							$gstflag=1;
							$taxvalue[$j]=$taxvalue[$j]+$itemgstamount;
						}
						
					}
				}
				if($gstflag==0)
				{
					$taxper[]=$itemgstper;
					$taxvalue[]=$itemgstamount;
				}
			}
			if($itemgstper>500)
			{
				if (empty($tclass["$gst"]))
				{
					$tclass["$gst"]=0;
					
					$evetaxclass = "select tcper from res_taxclass_master";
					$retaxclass = mysqli_query($conn, $evetaxclass);
					while($rttaxclass = mysqli_fetch_assoc($retaxclass))
					{
						if($gst==$rttaxclass['tcper'])
						{
							$tclass["$gst"]=$tclass["$gst"]+($itemgstamount);
						}
					}
				}
				else
				{
					$tclass["$gst"]=$tclass["$gst"]+($itemgstamount);
				}
			}
			$subtotal = $subtotal + $itemamount;
			if(strlen($itemdesc)>0)
			{
				$itemname=$itemname."\n".$itemdesc;
			}
			setlocale(LC_MONETARY, 'en_IN');			
			//$itemamount = money_format('%!i', $itemamount);
			$itemamount = number_format($itemamount,2,'.','');

			if($_SESSION['tdiscamt']>0)
			{
				

				$this->Row(array($ccount,$itemhsncode,$itemname,$itemqty,$itemunit,$itemiprice,$gst,$itemdiscper,$itemamount));
			}
			else{
				$this->Row(array($ccount,$itemhsncode,$itemname,$itemqty,$itemunit,$itemiprice,$gst,$itemamount));
			}
		}
		 /*for($i=1;$i<=10;$i++)
		{
			if($_SESSION['tdiscamt']>0)
			{
					$this->Cell(190,5,"",'LR',0,'R');
					$this->Ln();
			}
			else{
					$this->Cell(190,5,"",'LR',0,'R');
					$this->Ln();
				
			}
		} */
		$tgst=0;
		if(isset($taxper))
		{
		for($k=0;$k<sizeof($taxper);$k++)
		{
			$tgst=$tgst+$taxvalue[$k];									
		}}
	
			
		 
	$_SESSION['taxper'] = $taxper;
	$_SESSION['taxvalue'] = $taxvalue;
	
	$_SESSION['subtotal'] = $subtotal;
	$_SESSION['grandtotal'] = $subtotal +$tgst ;
    $this->Cell(array_sum($w),0,'','T');
	$this->Ln();
}
function ImprovedTable_2()
{
	$cgstno = $_SESSION['cgstno'];
	$cname  = $_SESSION['cname'];
	$customercmpname  = $_SESSION['cname'];
	$cmpname = $_SESSION['cmpname'];
	$customeremail  = $_SESSION['email'];
	$customerid = $_SESSION['invoicemno'];
	$city = $_SESSION['city'];
	$cstate = $_SESSION['cstate'];
	$address = $_SESSION['address']." ".$city.'-'.$cstate;
	//$address2 = $city.'-'.$cstate;
	$mobile = $_SESSION['mobile'];
	$customercontactpersonname = $_SESSION['cname'];
	$pdfheaderline1 = "";
	$pdfheaderline2 = "";
	$invoicedate=$_SESSION['invoicedate'];
	$invoicefordisplay=$_SESSION['invoicefordisplay'];
	$invoicerefno=$_SESSION['invoicerefno'];
	$invdestination=$_SESSION['invdestination'];
	$invdispatchthrough=$_SESSION['invdispatchthrough'];
	$invshipto=$_SESSION['invshipto'];
	$invewaybillno=$_SESSION['invewaybillno'];
	
	
	//$cmpperson = "";

	/* $invshipto1= substr($invshipto,1,40);
	$invshipto2= substr($invshipto,41,60);
	$invshipto3= substr($invshipto,61,100);
	$invshipto4= substr($invshipto,101,140); */
	if (preg_match('/^.{1,45}\b/s', $address, $match))
	{
		$line1=$match[0];
	}
	$tt = strlen($line1);
	$linesub = substr($address,$tt);
	$line2="";
	if (preg_match('/^.{1,45}\b/s', $linesub, $match2))
	{
		$line2=$match2[0];
	}
	/* echo "line2: ".$line2; */
	
	$tt2 = strlen($line2);
	$linesub2 = substr($address,$tt+$tt2);
	$line3="";
	if (preg_match('/^.{1,45}\b/s', $linesub2, $match3))
	{
		$line3=$match3[0];
	}
	
	/* $line3="";
	$line3 = substr($address,strlen($line1)+strlen($line2),50); */
	$line4="";
	$line4 = substr($address,strlen($line1)+strlen($line2)+strlen($line3),45);
	
	//destination
	$dline1='';
	if (preg_match('/^.{1,45}\b/s', $invdestination, $dmatch))
	{
		$dline1=$dmatch[0];
	}
	$dtt = strlen($dline1);
	$dlinesub = substr($invdestination,$dtt);
	$dline2="";
	if (preg_match('/^.{1,45}\b/s', $dlinesub, $dmatch2))
	{
		$dline2=$dmatch2[0];
	}
	$dtt2 = strlen($dline2);
	$dlinesub2 = substr($invdestination,$dtt+$dtt2);
	$dline3="";
	if (preg_match('/^.{1,45}\b/s', $dlinesub2, $dmatch3))
	{
		$dline3=$dmatch3[0];
	}

	/* if (preg_match('/^.{1,50}\b/s', $invdestination, $match1))
	{
		$line11=$match1[0];
	}
	$dtt1 = strlen($line11);
	$linesub1 = substr($invdestination,$dtt1);
	$line21="";
	if (preg_match('/^.{1,50}\b/s', $linesub1, $match21))
	{
		$line21=$match21[0];
	}
	$line31="";
	$line31 = substr($invdestination,strlen($line11)+strlen($line21),50); */
	
	//dispatch through
	$line13='';
	if (preg_match('/^.{1,45}\b/s', $invdispatchthrough, $match3))
	{
		$line13=$match3[0];
	}
	$tt3 = strlen($line13);
	$linesub3 = substr($invdispatchthrough,$tt3);
	$line23="";
	if (preg_match('/^.{1,45}\b/s', $linesub3, $match23))
	{
		$line23=$match23[0];
	}
	$line33="";
	$line33 = substr($invdispatchthrough,strlen($line13)+strlen($line23),45);
	
	
	//ship to
	$sline1='';
	if (preg_match('/^.{1,44}\b/s', $invshipto, $smatch))
	{
		$sline1=$smatch[0];
	}
	$stt = strlen($sline1);
	$slinesub = substr($invshipto,$stt);
	$sline2="";
	if (preg_match('/^.{1,44}\b/s', $slinesub, $smatch2))
	{
		$sline2=$smatch2[0];
	}
	
	$stt2 = strlen($sline2);
	$slinesub2 = substr($invshipto,$stt+$stt2);
	$sline3="";
	if (preg_match('/^.{1,44}\b/s', $slinesub2, $smatch3))
	{
		$sline3=$smatch3[0];
	}
	//echo "line2: ".$line2;
	$sline4 = substr($invshipto,strlen($sline1)+strlen($sline2)+strlen($sline3),44);
	
	
	
	
    $w = array(95,95);
	$this->SetFont('Arial','B',10);
	$this->SetLineWidth(.3);
	$this->Cell($w[0],6,"Bill To: ",'LRT');
	$this->SetFont('Arial','B',10);
	$this->Cell($w[1],6,"Invoice Number: $invoicefordisplay      Date: $invoicedate",'LRTB');

	$this->Ln();
	$this->SetFont('Arial','',10);
	$this->Cell($w[0],6,"$cname",'LR');
	$this->SetFont('Arial','B',10);
	$this->Cell($w[1],6,"Reference Number: $invoicerefno",'LR');
	$this->SetFont('Arial','',10);
	$this->Ln();
	$this->Cell($w[0],6,"$line1",'LR');
	/* $this->Cell($w[1],6,"$pdfheaderline1",'LR'); */
	$this->SetFont('Arial','B',10);
	$this->Cell($w[1],6,"Eway Bill Number: $invewaybillno",'LRB');
	$this->Ln();
	$this->SetFont('Arial','',10);
	$this->Cell($w[0],6,"$line2",'LR');
	$this->SetFont('Arial','B',10);
	$this->Cell($w[1],6,"Delivery Note No: $_SESSION[invdeliverylotno]",'LR');
	$this->Ln(6);

	$this->SetFont('Arial','',10);
	//$this->SetLineWidth(.3);
	//$this->Cell($w[0],6,"Order No.: $_SESSION[invorderno]",'LR');
	$this->Cell($w[0],6,"$line3",'LR');
	$this->SetFont('Arial','B',10);
	$this->Cell($w[1],6,"Dispatch Through: ",'LRT');

	$this->Ln();
	$this->SetFont('Arial','',10);
	//$this->Cell($w[0],6,"Destination: ",'LR');
	$this->Cell($w[0],6,"$line4",'LR');
	$this->SetFont('Arial','',10);
	$this->Cell($w[1],6,"$line13",'LR');
	$this->SetFont('Arial','',10);
	
	$this->Ln();
	$this->SetFont('Arial','B',10);
	//$this->Cell($w[0],6,"$line11",'LR');
	$this->Cell($w[0],6,"GSTN: $cgstno",'LR');
	$this->SetFont('Arial','',10);
	$this->Cell($w[1],6,"$line23",'LRB');
	$this->SetFont('Arial','',10);
	
	$this->Ln();
	$this->SetFont('Arial','',10);
	//$this->Cell($w[0],6,"$line21",'LR');
	$this->Cell($w[0],6,"",'LR');
	$this->SetFont('Arial','B',10);
	$this->Cell($w[1],6,"Buyers Order No: $_SESSION[invorderno]",'LRB');
	$this->SetFont('Arial','',10);
	
	$this->Ln();
	$this->SetFont('Arial','B',10);
	$this->Cell($w[0],6,"Ship To: ",'LR');
	$this->SetFont('Arial','B',10);
	$this->Cell($w[1],6,"Terms Of Payment: ",'LRB');
	$this->SetFont('Arial','',10);
	
	$this->Ln();
	$this->SetFont('Arial','',10);
	$this->Cell($w[0],6,"$sline1",'LR');
	$this->SetFont('Arial','B',10);
	/* $this->Cell($w[1],6,"Terms Of Payment: 100% Advance against PI",'LRB'); */
	$this->Cell($w[1],6,"Destination:",'LR');
	$this->SetFont('Arial','',10);
	
	$this->Ln();
	$this->SetFont('Arial','',10);
	$this->Cell($w[0],6,"$sline2",'LR');
	$this->SetFont('Arial','',10);
	$this->Cell($w[1],6,"$dline1",'LR');
	$this->SetFont('Arial','',10);
	
	$this->Ln();
	$this->SetFont('Arial','',10);
	$this->Cell($w[0],6,"$sline3",'LR');
	$this->SetFont('Arial','',10);
	$this->Cell($w[1],6,"$dline2",'LR');
	$this->SetFont('Arial','',10);
	
	$this->Ln();
	$this->SetFont('Arial','',10);
	$this->Cell($w[0],6,"$sline4",'LRB');
	$this->SetFont('Arial','',10);
	$this->Cell($w[1],6,"$dline3",'LRB');
	$this->SetFont('Arial','',10);


	//$this->Ln(6);
	$this->Ln(15);
	
    $this->Cell(array_sum($w),0,'','T');
	$this->Ln();
}
function ImprovedTable_5()
{ $w = array(137,30,23);
		$_SESSION['cmpdetail5']="";
		$amount = $_SESSION['subtotal'];
		$cmpdetail5=$_SESSION['cmpdetail5'];
		setlocale(LC_MONETARY, 'en_IN');
		//$pckamt = money_format('%!i', $amount);
		$pckamt = number_format($amount,2,'.','');
		$this->SetFont('Arial','',10);
		$this->SetLineWidth(.3);
        $this->Cell($w[0],6,"$cmpdetail5",'L');
		$this->Cell($w[1],6,"Sub-Total",'LRTB',0,'R');
		$this->Cell($w[2],6,"$pckamt",'LRTB',0,'R');
        $this->Ln();
}

function ImprovedTable_6()
{ $w = array(137,30,23);
	if($_SESSION['cstate']==$_SESSION['cmpstate'])
	{
		$taxper=$_SESSION['taxper'];
		$taxvalue=$_SESSION['taxvalue'];
		if(isset($taxper))
		{
		for($k=0;$k<sizeof($taxper);$k++)
		{
		
			$key2=$taxper[$k]/2;
			$value1=$taxvalue[$k]/2;
			$this->SetFont('Arial','',10);
			$this->SetLineWidth(.3);
			$this->Cell($w[0],6,"",'L');
			setlocale(LC_MONETARY, 'en_IN');
			/* $value2 = money_format('%!i', $value1); */
			$value2 = $value1;
			$this->Cell($w[1],6,"CGST $key2%",'LRTB',0,'R');
			$this->Cell($w[2],6,"$value2",'LRTB',0,'R');
			$this->Ln();
			$this->SetFont('Arial','',10);
			$this->SetLineWidth(.3);
			$this->Cell($w[0],6,"",'L');
			$this->Cell($w[1],6,"SGST $key2%",'LRTB',0,'R');
			$this->Cell($w[2],6,"$value2",'LRTB',0,'R');
			$this->Ln();
		}
		}
	}
	else
	{
		//$tclass1=$_SESSION['tclass'];
		$taxper=$_SESSION['taxper'];
		$taxvalue=$_SESSION['taxvalue'];
		if(isset($taxper))
		{
		for($k=0;$k<sizeof($taxper);$k++)
		{
		
			$key=$taxper[$k];
			$value=$taxvalue[$k];
		
		$this->SetFont('Arial','',10);
		$this->SetLineWidth(.3);
		$this->Cell($w[0],6,"",'L');
		setlocale(LC_MONETARY, 'en_IN');
		//$value1 = money_format('%!i', $value);
		$value1 = $value;
		$this->Cell($w[1],6,"IGST $key%",'LRTB',0,'R');
		$this->Cell($w[2],6,"$value1",'LRTB',0,'R');
		$this->Ln();
		}
		}
	}
}

function ImprovedTable_7()
{ $w = array(137,30,23);

		$qroundoffamount= $_SESSION['invoiceroundoffamount'];
		
		$invoicenetamount= $_SESSION['invoicenetamount'];
		setlocale(LC_MONETARY, 'en_IN');
		//$gt1 = money_format('%!i', $invoicenetamount);
		$gt1 = number_format($invoicenetamount,2,'.','');
		//$qroundoffamount = money_format('%!i', $qroundoffamount);
		$qroundoffamount = $qroundoffamount;
		$amountinwords =  convert_number_to_words($invoicenetamount);
		
		/* if($qroundoffamount<>0)
		{
			if($qroundoffamount>0)
			{
				$qroundoffamount= "Rs.".'+'.number_format($qroundoffamount,2,'.','');
			}
			$this->SetFont('Arial','',10);
			$this->SetLineWidth(.3);
			$this->Cell($w[0],6,"",'LR',0,'R');
			$this->Cell($w[1],6,"Round Off",'LRTB',0,'R');
			$this->Cell($w[2],6,"$qroundoffamount",'LRTB',0,'R');
			$this->Ln();
		} */
		 $this->SetFont('Arial','',9);
		$this->SetLineWidth(.3);
		$this->SetFont('Arial','B',10);
		$this->Cell($w[0],6,"",'LRB',0,'R');
		$this->Cell($w[1],6,"Total Amount",'LRTB',0,'R');
		$this->Cell($w[2],6,"$gt1",'LRTB',0,'R');
		$this->SetFont('Arial','',10);
		$this->Ln();
		
		$w = array(190,0);

		$this->SetFont('Arial','B',10);
		$this->SetLineWidth(.3);
        $this->Cell($w[0],6,"Amount In Words",'LRT');
		$this->Ln();
		$this->SetFont('Arial','',10);
		$this->Cell($w[0],4,"$amountinwords",'LR');
		$this->Ln();
		
		$this->SetFont('Arial','',10);
		$this->Cell($w[0],4,"",'LR');
		$this->Ln();
		
		$w = array(100,90);
		
		$cmpname=$_SESSION['cmpname'];
		$accountno=$_SESSION['accountno'];
		$accountname=$_SESSION['accountname'];
		$ifsccode=$_SESSION['ifsccode'];
		$bankbranchname=$_SESSION['bankbranchname'];
		$createdby=$_SESSION['createdby'];
		$gstno=$_SESSION['gstno'];
		$panno=$_SESSION['panno'];
		
		$this->SetFont('Arial','B',10);
		$this->SetLineWidth(.3);
		$this->Cell($w[0],6,"GST No: $gstno",'L',0,'L');
		$this->Cell($w[1],6,"Account No: $accountno",'R',0,'L');
		$this->Ln();
		
		$this->SetFont('Arial','B',10);
		$this->SetLineWidth(.3);
		$this->Cell($w[0],6,"PAN No: $panno",'L',0,'L');
		$this->Cell($w[1],6,"Account Name: $accountname",'R',0,'L');
		$this->Ln();
		
		$this->SetFont('Arial','B',10);
		$this->SetLineWidth(.3);
		$this->Cell($w[0],6,"",'L',0,'L');
		$this->Cell($w[1],6,"IFSC Code: $ifsccode",'R',0,'L');
		$this->Ln();
		
		$this->SetFont('Arial','B',10);
		$this->SetLineWidth(.3);
		$this->Cell($w[0],6,"",'L',0,'L');
		$this->Cell($w[1],6,"Bank Name: $bankbranchname",'R',0,'L');
		$this->Ln();
		
		$this->SetFont('Arial','B',10);
		$this->SetLineWidth(.3);
		$this->Cell($w[0],6,"",'L',0,'L');
		$this->Cell($w[1],6,"",'R',0,'L');
		$this->Ln();
		
			$invremarks=$_SESSION['invremarks'];
		$invoiceterms=$_SESSION['invoiceterms'];

		if($invoiceterms==""){
		
		}
		else{
		$this->SetFont('Arial','B',10);
		$this->Cell(190,5,'Terms & Conditions','LR',0,'L');
		$this->Ln();
		$this->SetFont('Arial','',10);
		$this->MultiCell(0,5,"$invoiceterms",'LR');
		$this->SetLineWidth(.3);
		$this->SetFont('Arial','B',10);
		$this->Cell(190,5,'','LR',0,'C');
		$this->Ln();
		}
		if($invremarks==""){
		
		}
		else{
		$this->SetFont('Arial','B',10);
		$this->Cell(190,5,'Declaration','LR',0,'L');
		$this->Ln();
		$this->SetFont('Arial','',10);
		$this->MultiCell(0,5,"$invremarks",'LR');
		$this->SetLineWidth(.3);
		$this->SetFont('Arial','B',10);
		$this->Cell(190,5,'','LR',0,'C');
		$this->Ln();
		}
		$this->SetFont('Arial','B',10);
		$this->SetLineWidth(.3);
		$this->Cell($w[0],6,"",'L',0,'L');
		$this->Cell($w[1],6,"For, $cmpname",'R',0,'L');
		$this->Ln();
		 
		
		$this->SetFont('Arial','B',10);
		$this->SetLineWidth(.3);
		$this->Cell($w[0],6,"",'L',0,'L');
		$this->Cell($w[1],6,"Authorized Signature",'R',0,'L');
		$this->Ln();
		 
		
		/* if($invremarks=="")
		{
		}
		else
		{
			$this->SetFont('Arial','B',10);
			$this->SetLineWidth(.3);
			$this->Cell($w[0],6,"Declaration:",'L',0,'L');
			$this->Cell($w[1],6,"",'R',0,'L');
			$this->Ln();
			
			$this->SetFont('Arial','',10);
			$this->SetLineWidth(.3);
			$this->Cell($w[0],6,"$invremarks",'L',0,'L');
			$this->Cell($w[1],6,"",'R',0,'L');
			$this->Ln();
		} */
	/* 	$w = array(95,95);
		$this->SetFont('Arial','B',10);
		$this->Cell(190,5,'Declaration','LR',0,'L');
		$this->Ln();
		$this->SetFont('Arial','',10);
		$this->MultiCell(0,5,"$invremarks",'LR');
		$this->SetLineWidth(.3);
		$this->SetFont('Arial','B',10);
		$this->Cell(190,5,'','LR',0,'C');
		$this->Ln(); */
		$w = array(190,0);

		$this->SetFont('Arial','B',10);
		$this->SetLineWidth(.3);
        $this->Cell($w[0],6,"",'LR');
		$this->Ln();
		
		//$w = array(150,40);
		//$this->SetFont('Arial','B',10);
		//$this->SetLineWidth(.3);
        //$this->Cell($w[0],6,"Company GST No: 24ALIPV8745J1ZE",'L',0,'L');
		//$this->Cell($w[0],6,"",'LR',0,'R');
		//$this->Cell($w[1],6,"Round Off",'R',0,'R');
		//$this->Ln();
		
		
		$this->SetFont('Arial','',10);
		$this->Cell($w[0],4,"",'LRB');
		$this->Ln();
}
function ImprovedTable_1()
{
	
	$this->SetFont('Arial','B',15);
	$this->Cell(190,5,"Tax Invoice",0,0,'C');
    $this->Ln(6);

}
function ImprovedTable_8()
{
    $w = array(95,95);
	/* $invoiceterms=$_SESSION['invoiceterms'];
	$footertext=$_SESSION['footertext'];
	//$invremarks=$_SESSION['invremarks'];
	$invremarks="Certified that the perticulars given above are true and correct and the amount indicated represents the price actually charged and that there is no flow of additional consideration directly or indirectly from the buyer.";
	if($invoiceterms=="")
	{
	}
	else
	{
		$this->SetFont('Arial','B',10);
		$this->Cell(190,5,'Terms & Conditions','LRT',0,'L');
		$this->Ln();
		$this->SetFont('Arial','',10);
		$this->MultiCell(0,5,"$invoiceterms",'LR');
		//$this->Ln();
		$this->MultiCell(0,5,"",'LRB');
		$this->SetLineWidth(.3);
       // $this->Ln();
	} */
	
	/* $this->SetFont('Arial','B',10);
	$this->Cell(190,5,'Company Details','LR',0,'L');
	$this->Ln();
	$this->SetFont('Arial','',10);
	$this->MultiCell(0,5,"$footertext",'LRB');
	$this->SetLineWidth(.3);
	
	
	$this->SetFont('Arial','B',10);
	$this->Cell(190,5,'Declaration','LR',0,'L');
	$this->Ln();
	$this->SetFont('Arial','',10);
	$this->MultiCell(0,5,"$invremarks",'LR');
	$this->SetLineWidth(.3); */
	//$this->Ln();
	/* $this->SetFont('Arial','',10);
	$this->MultiCell(0,5,"$invoiceterms");
	$this->SetLineWidth(.3);
	$this->Ln(); */
}

function ImprovedTable_10()
{
    $w = array(95,95);
		/* $cmpname=$_SESSION['cmpname'];
		$createdby=$_SESSION['createdby'];
		$this->SetFont('Arial','B',10);
		$this->Cell(190,5,"For: $cmpname",'LR',0,'R');
		$this->Ln();
		$this->SetFont('Arial','',10);
		$this->Cell(190,5,"$createdby",'LRB',0,'R');
		$this->SetLineWidth(.3); */
}
}

function convert_number_to_words($myno,$part2=1)
{
   $number = $myno;
   $no = (int)($number);
   $point = round($number - $no, 2) * 100;
   $hundred = null;
   $digits_1 = strlen($no);
   $i = 0;
   $str = array();
   $words = array('0' => '',
   '1' => 'One',
   '2' => 'Two',
    '3' => 'Three',
	'4' => 'Four',
	'5' => 'Five',
	'6' => 'Six',
    '7' => 'Seven',
	'8' => 'Eight',
	'9' => 'Nine',
    '10' => 'Ten',
	'11' => 'Eleven',
	'12' => 'Twelve',
    '13' => 'Thirteen',
	'14' => 'Fourteen',
    '15' => 'Fifteen',
	'16' => 'Sixteen',
	'17' => 'Seventeen',
    '18' => 'Eighteen',
	'19' =>'Nineteen',
	'20' => 'Twenty',
    '30' => 'Thirty',
	'40' => 'Forty',
	'50' => 'Fifty',
    '60' => 'Sixty',
	'70' => 'Seventy',
    '80' => 'Eighty',
	'90' => 'Ninety');
   $digits = array('', 'Hundred', 'Thousand', 'Lakh', 'Crore');
   while ($i < $digits_1) {
     $divider = ($i == 2) ? 10 : 100;
     $number = floor($no % $divider);
     $no = floor($no / $divider);
     $i += ($divider == 10) ? 1 : 2;
     if ($number) {
        $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
        $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
        $str [] = ($number < 21) ? $words[$number] .
            " " . $digits[$counter] . $plural . " " . $hundred
            :
            $words[floor($number / 10) * 10]
            . " " . $words[$number % 10] . " "
            . $digits[$counter] . $plural . " " . $hundred;
     } else $str[] = null;
  }
  $str = array_reverse($str);
  $result = implode('', $str);
  $points = ($point) ?
    "." . $words[$point / 10] . " " .
          $words[$point = $point % 10] : '';


	$tnumber = $myno;

	$whole = floor($tnumber);
	$fraction = ($tnumber - $whole)*100;

	if($fraction>0.00)
	{
		$pointvalue = convert_number_to_words( (int)$fraction,-7);

		return $result . "Rupees and ".$pointvalue."Paisa Only.";
	}
	else if($part2==-7)
	{
		return $result;
	}
	else
	{
		return $result."Rupees Only.";

	}
}

function convert_number_to_words2($number) {

    $hyphen      = '-';
    $conjunction = ' and ';
    $separator   = ' ';
    $negative    = 'negative ';
    $decimal     = ' and ';
    $dictionary  = array(
        0                   => 'Zero',
        1                   => 'One',
        2                   => 'Two',
        3                   => 'Three',
        4                   => 'Four',
        5                   => 'Five',
        6                   => 'Six',
        7                   => 'Seven',
        8                   => 'Eight',
        9                   => 'Nine',
        10                  => 'Ten',
        11                  => 'Eleven',
        12                  => 'Twelve',
        13                  => 'Thirteen',
        14                  => 'Fourteen',
        15                  => 'Fifteen',
        16                  => 'Sixteen',
        17                  => 'Seventeen',
        18                  => 'Eighteen',
        19                  => 'Nineteen',
        20                  => 'Twenty',
        30                  => 'Thirty',
        40                  => 'Fourty',
        50                  => 'Fifty',
        60                  => 'Sixty',
        70                  => 'Seventy',
        80                  => 'Eighty',
        90                  => 'Ninety',
        100                 => 'Hundred',
        1000                => 'Thousand',
        1000000             => 'Million',
        1000000000          => 'Billion',
        1000000000000       => 'Trillion',
        1000000000000000    => 'Quadrillion',
        1000000000000000000 => 'Quintillion'
    );

    if (!is_numeric($number)) {
        return false;
    }

    if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
        // overflow
        trigger_error(
            'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
            E_USER_WARNING
        );
        return false;
    }

    if ($number < 0) {
        return $negative . convert_number_to_words2(abs($number));
    }

    $string = $fraction = null;

    if (strpos($number, '.') !== false) {
        list($number, $fraction) = explode('.', $number);
    }

    switch (true) {
        case $number < 21:
            $string = $dictionary[$number];
            break;
        case $number < 100:
            $tens   = ((int) ($number / 10)) * 10;
            $units  = $number % 10;
            $string = $dictionary[$tens];
            if ($units) {
                $string .= $hyphen . $dictionary[$units];
            }
            break;
        case $number < 1000:
            $hundreds  = $number / 100;
            $remainder = $number % 100;
            $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
            if ($remainder) {
                $string .= $conjunction . convert_number_to_words2($remainder);
            }
            break;
        default:
            $baseUnit = pow(1000, floor(log($number, 1000)));
            $numBaseUnits = (int) ($number / $baseUnit);
            $remainder = $number % $baseUnit;
            $string = convert_number_to_words2($numBaseUnits) . ' ' . $dictionary[$baseUnit];
            if ($remainder) {
                $string .= $remainder < 100 ? $conjunction : $separator;
                $string .= convert_number_to_words2($remainder);
            }
            break;
    }

    if (null !== $fraction && is_numeric($fraction)) {
        $string .= $decimal;
        $words = array();
        foreach (str_split((string) $fraction) as $number) {
            $words[] = $dictionary[$number];
        }
        $string .= implode(' ', $words);
    }

    return $string;
}


// Instanciation of inherited class
$pdf = new PDF();
$header = array();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->ImprovedTable_1();
$pdf->ImprovedTable_2();
$pdf->ImprovedTable_3();
$pdf->ImprovedTable_4();
$pdf->ImprovedTable_5();
$pdf->ImprovedTable_6();
$pdf->ImprovedTable_7();
$pdf->ImprovedTable_8();
$pdf->ImprovedTable_9();
$pdf->ImprovedTable_10();
$pdf->SetFont('Times','',12);
//$pdf->Output();
//$settingtype = $_SESSION['settingtype'];
$invoicepdf = $_SESSION['invoicepdf'];
$qmid2 = $_GET['invoicemid'];
$pdf->Output('F',"download/$invoicepdf");

//return;

header("Location: invoicestatus.php?invoicemid=$qmid2");




?>
