<?php 
session_start();
require('../../fpdf/fpdf.php');

class PDF_result extends FPDF {
	 	var $totalval;
		var $mrpval;
		var $qtyval;
		var $excessval;
		var $getYaxis;
		var $heady;
		var $totalcgst;
		var $totalsgst;
		var $totaligst;
		var $gtotal;
	function __construct ($orientation = 'P', $unit = 'pt', $format = 'A4', $margin = 10) {
		$this->FPDF($orientation, $unit, $format);
		$this->SetTopMargin(125);
		$this->SetLeftMargin($margin);
		$this->SetRightMargin($margin);
		
		$this->SetAutoPageBreak(true, $margin);
	}
	

	
 function Footer()
{
    //Position at 1.5 cm from bottom
    $this->SetY(-15);
    //times italic 8
    $this->SetFont('times','I',8);
    //Page number
//    $this->Cell(0,10,'This is a computer generated Invoice, No Signature Required',0,0,'C');
}



function numtowords($number){ 
$no = round($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);
  
 

    return $result . "Rupees  " ; 
} 


	
function Generate_Table(&$billdet_res_row,$con) {
	$billno=$_POST['orderid'];
	 $this->SetY($this->GetY() + 20);
	$this->heady = $this->GetY();
	$bill_res=mysqli_query($con,"SELECT * FROM   sinvoiceregdet WHERE orderid =  '$billno' ORDER BY srno");
	
	$w=array(20,60,100,220,27,37,48,75,16,11);
	$this->SetFont('times', 'B', 10);
	$this->Cell($w[0], 25, "S.N.", '1', 0, 'C');
	$this->Cell($w[1], 25, "PROD CODE", '1', 0, 'C');
	$this->Cell($w[3], 25, "PARTICULARS-HSNCODE", '1', 0, 'C');
//	$this->Cell($w[5], 25, "HSN", '1', 0, 'C');
	$this->Cell($w[4], 25, "QTY", '1', 0, 'C');
	$this->Cell($w[5], 25, "RATE", '1', 0, 'C');
	$this->Cell($w[6], 25, "AMOUNT", '1', 0, 'C');
	$this->Cell($w[5], 25, "CGST", '1', 0, 'C');
	$this->Cell($w[5], 25, "SGST", '1', 0, 'C');
	$this->Cell($w[5], 25, "IGST", '1', 0, 'C');
	$this->Cell($w[6], 25, "TOTAL", '1', 1, 'C');
	
	/*
	$wd='10';
	$this->SetXY($w[0]+$w[1]+10,$this->GetY()-23);
	$this->SetFont('Arial', 'B', 6);
	$this->Cell($w[9], $wd, "28", '0', 0, 'C');
	$this->Cell($w[9], $wd, "30", '0', 0, 'C');
	$this->Cell($w[9], $wd, "32", '0', 0, 'C');
	$this->Cell($w[9], $wd, "34", '0', 0, 'C');
	$this->Cell($w[8], $wd, "36", '0', 0, 'C');
	$this->Cell($w[8], $wd, "38", '0', 0, 'C');
	$this->Cell($w[8], $wd, "40", '0', 0, 'C');
	$this->Cell($w[8], $wd, "42", '0', 0, 'C');
	$this->Cell($w[8], $wd, "44", '0', 0, 'C');
	$this->Cell($w[8], $wd, "46", '0', 0, 'C');
	$this->Cell($w[8], $wd, "48", '0', 0, 'C');
	$this->Cell($w[8], $wd, "50", '0', 0, 'C');
	$this->Cell($w[8], $wd, "52", '0', 0, 'C');
	$this->Cell($w[8], $wd, "54", '0', 0, 'C');
	$this->Cell($w[8], $wd, "56", '0', 1, 'C');
	
	$this->SetXY($w[0]+$w[1]+10,$this->GetY());
	$this->SetFont('Arial', 'B', 6);
	$this->Cell($w[9], $wd, "S", '0', 0, 'C');
	$this->Cell($w[9], $wd, "M", '0', 0, 'C');
	$this->Cell($w[9], $wd, "L", '0', 0, 'C');
	$this->Cell($w[9], $wd, "XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "2XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "3XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "4XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "5XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "6XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "7XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "8XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "9XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "10XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "11XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "12XL", '0', 1, 'C');
	$this->Ln(5);
	 */
	$this->SetFont('times', '',10);
	$fill = false;
		$x = $this->GetX();
		$y = $this->GetY();
	while($row=mysqli_fetch_array($bill_res)) {
		if($c>600)
		{
			$this->AddPage();
			$this->bill_header($billdet_res_row);
			$c=0;
			$this->SetFont('times', 'B', 10);
			$this->Cell($w[0], 25, "S.N.", '1', 0, 'C');
        	$this->Cell($w[1], 25, "PROD CODE", '1', 0, 'C');
        	$this->Cell($w[3], 25, "PARTICULARS-HSNCODE", '1', 0, 'C');
        //	$this->Cell($w[5], 25, "HSN", '1', 0, 'C');
        	$this->Cell($w[4], 25, "QTY", '1', 0, 'C');
        	$this->Cell($w[5], 25, "RATE", '1', 0, 'C');
        	$this->Cell($w[6], 25, "AMOUNT", '1', 0, 'C');
        	$this->Cell($w[5], 25, "CGST", '1', 0, 'C');
        	$this->Cell($w[5], 25, "SGST", '1', 0, 'C');
        	$this->Cell($w[5], 25, "IGST", '1', 0, 'C');
        	$this->Cell($w[6], 25, "TOTAL", '1', 1, 'C');
			$this->SetFont('times', '');
			$this->Ln(5);
	
		}
			
			$this->SetFont('times', '', 10);
			$this->Cell($w[0], 12,$row['srno'], 0, 0, 'L');
			$x = $this->GetX();
			$y1 = $this->GetY();
			$this->MultiCell($w[1], 10, $row['prodcode'],0,'L');	
			
			$y2 = $this->GetY();
			$yH = $y2 - $y1;
			$this->SetXY($x + $w[1], $this->GetY() - $yH);
			
			/*
			$x = $this->GetX();
			$y1 = $this->GetY();
            $this->SetFont('Arial', '', 7);
			$jobvar_res=mysqli_query($con,"SELECT * FROM   sinvoicevar WHERE job_id =  '$billno' and prodname = '$row[prodcode]' and srno = '$row[srno]'  ORDER BY entryid");
			$ival=1;
			$iinc=0;
			while($jobvarrow=mysqli_fetch_array($jobvar_res)) {
				if($ival>=3){
					$iinc=2;
					}
			$this->Cell($w[9]+2+$iinc,  $yH,  $jobvarrow['qnty'],0,0,'L');
				
			 //$qty = $jobvarrow['qnty']."--".$qty;
			 $ival++;
		     } 
		$this->SetX($x + $w[3]);
			*/
			
		$this->Cell($w[3], $yH, $row['cat']."-".$row['colorcode']."-".$row['hsncode'],0,'L');	
	//	$this->Cell($w[5], $yH, '', 0, 0, 'R');
		$this->Cell($w[4], $yH, $row['qty'], 0, 0, 'R');
		$this->Cell($w[5], $yH, $row['pcost'], 0, 0, 'R');
		$this->Cell($w[6], $yH, $row['pamt'], 0, 0, 'R');
		$this->Cell($w[5], $yH,  $row['cgst'], 0, 0, 'R');
		$this->Cell($w[5], $yH,  $row['sgst'], 0, 0, 'R');
		$this->Cell($w[5], $yH,  $row['igst'], 0, 0, 'R');
		$this->Cell($w[6], $yH,  $row['gtotal'], 0, 1, 'R');
		
		$c=$this->GetY();
		$this->totalval += $row['pamt'];
		$this->mrpval += $row['mrp'];
		$this->qtyval += $row['qty'];
		$this->totalcgst += $row['cgst'];
		$this->totalsgst += $row['sgst'];
		$this->totaligst += $row['igst'];
		$this->gtotal += $row['gtotal'];
		$this->excessval += $row['qty'] *$row['mrp'];
		
			}
		$y=$this->GetY();
	
	
	$this->bill_footer($billdet_res_row);	
}//end of Generate_Table()	



function bill_header(&$billdet_res_row,$con){
/*
  $this->SetFont('times', 'B', 20);
  $this->Cell(0, 30, $_SESSION['SESS_CMP'], 0, 1, 'C', false);
  $this->SetFont('times', '', 11);
  $this->SetX(60);
  $this->MultiCell(500, 13, $_SESSION['SESS_CMPADD1'],'C','C');
  $this->Cell(0, 16,$_SESSION['SESS_CMPADD1_1'], 0, 1, 'C', false);
*/
  $this->SetFont('times', 'B', 14);
  $this->Cell(0, 30, "TAX INVOICE", 0, 1, 'C', false);
  $this->SetFont('times', '', 10);
  $ty=$this->GetY();
  $this->make_border($this->GetY(),750);	

		



$this->SetFont('times', 'B');
$this->Cell(70, 13, "Reverse Charge",'0',0);
$this->SetFont('times', '');
$this->Cell(220, 13, ": No",'0',1);
$this->SetFont('times', 'B');
$this->Cell(70, 13, 'Invoice No.','0',0 ,'L');
$this->SetFont('times', '');
$this->Cell(220, 13, ": $billdet_res_row[orderid]",'0',1 ,'L');	
$this->SetFont('times', 'B');
$this->Cell(70, 13, 'Invoice Date','0',0 ,'L');
$this->SetFont('times', '');
$this->Cell(220, 13, ": $billdet_res_row[orderdate]",'0',1 ,'L');
$this->SetFont('times', 'B');
$this->Cell(70, 13, 'Gstn','0',0 ,'L');
$this->SetFont('times', '');
$this->Cell(100, 13, ": 27AALCG4637M1Z1",'0',0,'L');
$this->Cell(50, 13, 'State Code',0,0 ,'L');
$this->SetFont('times', '');
$this->Cell(40, 13, ": 27",0,0 ,'L');
$this->Cell(10, 13, " ",'0',1 ,'L');

$this->Line(10,$this->GetY(),580,$this->GetY());
$this->SetFont('times', 'B');
$this->Cell(290, 15, 'Details of Receiver | Billed to: ','0',1 ,'C');
$this->Line(10,$this->GetY(),580,$this->GetY());



$this->Cell(40, 13, 'Name ','0',0 ,'L');
$this->SetFont('times', '');
$this->Cell(250, 13, ": $billdet_res_row[name]",'0',1 ,'L');
	
	
$this->SetFont('times', 'B');
$this->Cell(40, 13, 'Address ','0',0 ,'L');	
$this->SetFont('times', '', 8);		
$this->MultiCell(250, 13, ": $billdet_res_row[address] ". "$billdet_res_row[cmpcity]",'0','L');	


$this->SetFont('times', '',9);
$this->Cell(40, 13, 'State','0',0 ,'L');
$this->SetFont('times', '');
$this->Cell(160, 13, ": $billdet_res_row[state]",'0',0 ,'L');

$this->Cell(40, 13, 'Pin Code',0,0,'L');
$this->SetFont('times', '');
$this->Cell(10, 13, ": ",0,0 ,'L');
$this->Cell(10, 13, "$billdet_res_row[pincode]",'0',1 ,'L');

$this->SetFont('times', '',9);
$this->Cell(40, 13, 'GSTIN ','0',0 ,'L');
$this->SetFont('times', '');
$this->Cell(250, 13, ": $billdet_res_row[gstin]",'0',1 ,'L');

//$this->Line(10,$this->GetY(),580,$this->GetY());


/****** ******/
$this->SetXY(310,$ty);
$this->SetFont('times', 'B');
$this->Cell(90, 13, "Transportation Mode",'0',0);
$this->SetFont('times', '');
$this->Cell(200, 13, ": $billdet_res_row[shipvia]",'0',1,'L');

$this->SetX(310);
$this->SetFont('times', 'B');
$this->Cell(90, 13, 'AWB / LR No','0',0 ,'L');
$this->SetFont('times', '');
$this->Cell(200, 13, ": ",'0',1 ,'L');

$this->SetX(310);
$this->SetFont('times', 'B');
$this->Cell(90, 13, "Date of Dispatch",'0',0);
$this->SetFont('times', '');
$this->Cell(200, 13, ": $billdet_res_row[lrdate]",'0',1);

$this->SetX(310);
$this->SetFont('times', 'B');
$this->Cell(90, 13, "Place of Supply",'0',0);
$this->SetFont('times', '');
$this->Cell(200, 13, ": $billdet_res_row[pos]",'0',1);

$this->SetX(310);
$this->SetFont('times', 'B');
$this->Cell(290, 13, "Details of Consignee | Shipped to:",'0',1, 'C');



$this->SetX(310);
$this->Cell(40, 13, 'Name ','0',0 ,'L');
$this->SetFont('times', '');
$this->Cell(250, 13, ": $billdet_res_row[name]",'0',1 ,'L');
	
$this->SetX(310);	
$this->SetFont('times', 'B');
$this->Cell(40, 13, 'Address ','0',0 ,'L');	
$this->SetFont('times', '', 8);		
$this->MultiCell(240, 13, ": $billdet_res_row[address]". "$billdet_res_row[cmpcity]",'0','L');	

$this->SetX(310);
$this->SetFont('times', '');
$this->Cell(40, 13, 'State','0',0 ,'L');
$this->SetFont('times', '');
$this->Cell(130, 13, ": $billdet_res_row[state]",'0',0 ,'L');
$this->Cell(40, 13, 'Pin Code',0,0 ,'L');
$this->SetFont('times', '');
$this->Cell(10, 13, ": ",0,0 ,'L');
$this->Cell(10, 13, "$billdet_res_row[pincode]",'0',1 ,'L');

$this->SetX(310);
$this->SetFont('times', '');
$this->Cell(40, 13, 'GSTIN ','0',0 ,'L');
$this->SetFont('times', '');
$this->Cell(250, 13, ": $billdet_res_row[gstin]",'0',1 ,'L');


$this->line(310,$ty,310,$this->GetY()+20);
$this->SetFont('times', '');
}//end of bill_header()


function bill_footer(&$billdet_res_row){
	$y=600;
	$this->Line(10,$y,580,$y);
	//$this->Line(10,$fotty,10,$this->GetY());
	
	$this->SetY($y+5);
	
	
	
	$this->SetFont('times', 'B');
	$this->MultiCell(400, 10,"Total Invoice Value in Rupees:
	
	(".$this->numtowords($billdet_res_row[grtotal]).")
	",'0','L');
	
// $this->Line(10,$this->GetY(),580,$this->GetY());
	
	$term=$this->GetY()+5;
	$this->SetY($term);
	$this->SetFont('times', 'B');
	$this->MultiCell(285, 9," Bank Details : IndusInd Bank
	Branch Name : Andheri(East), Mumbai
	A/C No. 259833162127
	RTGS/NEFT IFSC : INDB0000018",0, 'L');
	
	$this->line(374,$y,374,$this->GetY()+5); // Vertical Line
	$this->Line(10,$this->GetY()+5,580,$this->GetY()+5); // Horizontal Line
	
	$this->SetY($this->GetY()+10);
	$this->SetFont('times', '');
	$this->MultiCell(285, 9," + Tems & Conditions +
	
	* Subject to Mumbai Jurisdiction.
	* Goods once sold will be not be taken back.
	* Payment Terms - Immediate
	",0);
    $this->SetY($this->GetY()+5);
	$this->Cell(0,10,'This is a computer generated Invoice, No Signature Required',0,0,'C');
	
	$t=$this->GetY();
	$this->SetFont('times', 'B');
	
	$this->SetXY(430,$y);
		// Vericle Lines for Items Table
		$this->line(30,$this->heady,30,$this->GetY());
		$this->line(90,$this->heady,90,$this->GetY());
		$this->line(310,$this->heady,310,$this->GetY());
//		$this->line(310,$this->heady,310,$this->GetY());
		$this->line(337,$this->heady,337,$this->GetY());
		$this->line(374,$this->heady,374,$this->GetY());
		$this->line(422,$this->heady,422,$this->GetY());
		$this->line(459,$this->heady,459,$this->GetY());
		$this->line(496,$this->heady,496,$this->GetY());
		$this->line(533,$this->heady,533,$this->GetY());
//		$this->line(580,$this->heady,580,$this->GetY());
		
	$this->SetFont('times', 'B');
	$this->SetXY(430,$y+5);
	$this->Cell(50, 10, "Total Amount ",0, 0);
	$this->Cell(50, 10, "",0, 0);
	$this->SetFont('times', 'B');
	$this->Cell(50, 10,  $this->totalval.".00", 0, 1, 'R');
	
	if ($billdet_res_row[pdiscount] > 0){
	$this->SetX(430);
	$this->Cell(50, 10, "Discount",0, 0);
	$this->Cell(50, 10, " $billdet_res_row[pdiscount] %",0, 0);
	$this->SetFont('times', 'B');
	$this->Cell(50, 10,  "$billdet_res_row[disamt]", 0, 1, 'R');

	$this->SetX(430);
	$this->Cell(50, 10, "Net Amount",0, 0);
	$this->Cell(50, 10, "",0, 0);
	$this->SetFont('times', 'B');
	$this->Cell(50, 10,  "$billdet_res_row[pnamount]", 0, 1, 'R');}
    
	$this->SetX(430);
	$this->Cell(50, 10, "Add : CGST",0, 0);
	$this->Cell(50, 10, "",0, 0);
	$this->SetFont('times', 'B');
	$this->Cell(50, 10,  $this->totalcgst, 0, 1, 'R');
	
	$this->SetX(430);
	$this->Cell(50, 10, "Add : SGST",0, 0);
	$this->Cell(50, 10, "",0, 0);
	$this->SetFont('times', 'B');
	$this->Cell(50, 10,  $this->totalsgst, 0, 1, 'R');
	
	$this->SetX(430);
	$this->Cell(50, 10, "Add : IGST",0, 0);
	$this->Cell(50, 10, "",0, 0);
	$this->SetFont('times', 'B');
	$this->Cell(50, 10,  $this->totaligst, 0, 1, 'R');
	
	$this->SetX(430);
	$this->Cell(50, 10, "Tax Amount : GST",0, 0);
	$this->Cell(50, 10, "",0, 0);
	$this->SetFont('times', 'B');
	$this->Cell(50, 10,  $this->totaligst+$this->totalsgst+$this->totalcgst, 0, 1, 'R');

	$this->SetX(430);
	$this->Cell(50, 13, "Grand Total",0, 0);
	$this->Cell(50, 13, "",0, 0);
	$this->SetFont('times', 'B');
	$this->Cell(50, 13, "$billdet_res_row[grtotal]", 0, 1, 'R'); //$this->gtotal
	
	/*$this->SetX(430);
	$this->Cell(50, 10, "",'L', 0);
	$this->Cell(50, 10, "",0, 0);
	$this->SetFont('times', 'B');
	$this->Cell(50, 10,  "", 0, 1, 'R');
	
	$this->SetX(430);
	$this->Cell(50, 10, " ",'LB', 0);
	$this->Cell(50, 10, " ",'B', 0);
	$this->SetFont('times', 'B');
	$this->Cell(50, 10,  " ", 'B', 1, 'R');*/
    
		$this->SetFont('times', '');
		$this->SetY($t);
		$t=$this->GetY();
		
		
		
		//$this->SetY($t+10);
		
	//$this->make_border($start_border,$end_border);
		//$this->Line(10,800,590,800);
		//$this->Line(10,802,590,802);
		//$this->SetY(810);
		//$this->Cell(75, 15, 'Prepared By:');
		//$this->Cell(350, 15, $billdet_res_row['prepby'],0,0,'L');
		//$this->Cell(75, 15, 'Checked By');
		//$this->Cell(0, 15, $billdet_res_row['checkby'],0,0,'L');
		//$this->Cell(0, 15, "SUBJECT TO MUMBAI JURISDICTION",0,0,'C');
	}// end of bill_footer()


	
	function make_border($start_border,$end_border)
	{
	//$this->SetDrawColor(0,80,180);
    //$this->SetFillColor(230,230,0);
    //$this->SetTextColor(220,50,50);
    // Thickness of frame (1 mm)
    $this->SetLineWidth(1);
		$this->Line(10,$start_border,580,$start_border);
			//$this->Line(16,$start_border,584,$start_border);
			
			
			$this->Line(10,$start_border,10,$end_border); 
			//$this->Line(16,$start_border,16,$end_border); 
			
			
			$this->Line(580,$start_border,580,$end_border);
			//$this->Line(586,$start_border,586,$end_border);
			
			$this->Line(10,$end_border,580,$end_border);
			//$this->Line(10,$end_border,586,$end_border);
		}

}//end of class


$pdf = new PDF_result();
$pdf->AddPage();


$billno=$_POST['orderid'];
include '../../db_connect.php';
	//to select the company's table in current session
include '../../db_table_select.php';


$billdet_res=mysqli_query($con,"SELECT `orderid`,   DATE_FORMAT(`orderdate`,'%d/%m/%Y') as `orderdate`, `ordertype`, `buyercode`, `billedto`, `refno`,  DATE_FORMAT(`dlvdate`,'%d/%m/%Y') as `dlvdate`, `ratetype`, `shipto`, `agentcode`, `distr`, `shipvia`, `pos`, `shippingpaid`, `totparcel`, `formtype`, `lrno`, DATE_FORMAT(`lrdate`,'%d/%m/%Y') as `lrdate`,`comm`, `comm_on`, `notes`, sinvoicereg.`vat` as vat ,`packinglistno`,disamt, `ptotal`, `pdiscount`, `pnamount`,  `grtotal`, `status`, commst.name, commst.address, commst.cmpcity, commst.state, commst.pincode, commst.vat as cmpvat , commst.gstin, commst.tin, commst.telephone
FROM  sinvoicereg INNER JOIN commst ON buyercode=compid 
WHERE orderid = '$billno'");

$billdet_res_row=mysqli_fetch_assoc($billdet_res);
$pdf->SetFont('times', 'B', 10);
$pdf->bill_header($billdet_res_row,$con);
	
$pdf->Generate_Table($billdet_res_row,$con);	
$pdf->Output('sinvoice_entry.pdf', 'F');



?>
