<?php 
session_start();
require('../fpdf/fpdf.php');
class PDF_result extends FPDF {
	var $totalamt ; 
	function __construct ($orientation = 'P', $unit = 'pt', $format = 'A4', $margin = 30) {
		$this->FPDF($orientation, $unit, $format);
		$this->SetTopMargin($margin);
		$this->SetLeftMargin(17);
		$this->SetRightMargin(13);
		
		$this->SetAutoPageBreak(true, $margin);
	}
	
	
	
 function Footer()
{
    //Position at 1.5 cm from bottom
    $this->SetY(-15);
    //Arial italic 8
    $this->SetFont('Arial','I',8);
    //Page number
    $this->Cell(0,10,'This is a computer generated Invoice'."(Page ".$this->PageNo().")",0,0,'C');	
}




	
function Generate_Table($con,$proddet,$billno) {
	//echo $billno;
	$bill_res=mysqli_query($con,"SELECT * FROM  proddet WHERE srno =  '$billno' ORDER BY srno");
	//echo "SELECT * FROM  $jobregdet WHERE jobid =  '$billno' ORDER BY srno";
	$this->SetY($this->GetY()+20);
	$w=array(30,75,100,345,30,40,45,70);
	$this->SetFont('Arial', 'B', 8);
	//$this->Cell($w[0], 25, "Job Card", '1', 0, 'L');
			$this->Cell($w[1], 25, "Product", '1', 0, 'L');
			//$this->Cell($w[2], 25, "Style", '1', 0, 'L');
			$this->Cell($w[3], 25, "Size wise Details", '1', 0, 'C');
			$this->Cell($w[4], 25, "Prod Qty", '1', 0, 'C');
			//$this->Cell($w[5], 25, "Shortage", '1', 0, 'R');
			$this->Cell($w[6], 25, "Prod Rate", '1', 0, 'C');
			$this->Cell($w[7], 25, "Prod Amt", '1', 1, 'C');
	$this->Ln(5);
	 
	$this->SetFont('Arial', '',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('Arial', '', 9);
			//$this->Cell($w[0], 25, "Job Card", '1', 0, 'L');
			$this->Cell($w[1], 25, "Product", '1', 0, 'L');
			//$this->Cell($w[2], 25, "Style", '1', 0, 'L');
			$this->Cell($w[3], 25, "Size wise Details", '1', 0, 'C');
			$this->Cell($w[4], 25, "Prod Qty", '1', 0, 'C');
			//$this->Cell($w[5], 25, "Shortage", '1', 0, 'R');
			$this->Cell($w[6], 25, "Prod Rate", '1', 0, 'C');
			$this->Cell($w[7], 25, "Prod Amt", '1', 1, 'C');
			$this->SetFont('Arial', '');
			$this->Ln(5);
	
		}
			$this->Ln(5);
			$this->SetFont('Arial', '', 9);
			//$this->Cell($w[0], 12,$row['srno'], 0, 0, 'L');
			$x = $this->GetX();
			$y1 = $this->GetY();
			$this->MultiCell($w[1], 12, $row['rmcode'].' - '.$row['skuid'],0,'L');	
			$y2 = $this->GetY();
			$yH = $y2 - $y1;
			$this->SetXY($x + $w[1], $this->GetY() - $yH);
			
			
			/*
			$x = $this->GetX();
			$y1 = $this->GetY();
			$this->MultiCell($w[1], $yH,  "", 0,'L');	
			$y2 = $this->GetY();
			$yH = $y2 - $y1;
			$this->SetXY($x + $w[1], $this->GetY() - $yH);*/
			
			$size='';
			/*$qty ='';*/
			
			$jobvar_res=mysqli_query($con,"SELECT sizesetcode FROM prodreg  where id=$billno");
		    while($jobvarrow=mysqli_fetch_array($jobvar_res)) {
			 $size = $this->Cell(10, 0,$jobvarrow['sizesetcode'], 0, 0, 'L').$size;
			 //$qty = $this->Cell(10, 20,$jobvarrow['qnty'], 0, 0, 'L').$qty;
		     }
			
			$x = $this->GetX();
			$y1 = $this->GetY();

			$this->Cell($w[3], 30, $size,0,1,'L');	
			$this->Cell($w[3], $yH, $qty,0,0,'L');
			$y2 = $this->GetY();
			$yH = $y2 - $y1;
			$this->SetXY(($w[3])+90, $this->GetY() - $yH);
		
		
		$this->Cell($w[4], $yH, $row['qty'].".00", 0, 0, 'R');
		$this->Cell($w[6], $yH, $row['psrate'].".00", 0, 0, 'R');
		$this->Cell($w[7], $yH, $row['pamount'].".00", 0, 1, 'R');
		$c=$this->GetY();
		
		$qtyvalue += $row['qty'];
		$pcostvalue += $row['psrate'];
		$pamtvalue += $row['pamount'];
			}
		$y=$this->GetY();
	   $this->totalamt =$pamtvalue;
	  		$w=array(30,75,100,345,30,40,45,70);
			$this->Ln(5);
			$this->SetFont('Arial', '', 9);
			$x = $this->GetX();
			$y1 = $this->GetY();
			$this->MultiCell($w[1], 12, "",'TB','L');	
			$y2 = $this->GetY();
			$yH = $y2 - $y1;
			$this->SetXY($x + $w[1], $this->GetY() - $yH);
			
			$x = $this->GetX();
			$y1 = $this->GetY();
			$this->Cell($w[3], $yH, "Sub Total     ",'TB',0,'R');
			$y2 = $this->GetY();
			$yH = $y2 - $y1;
			$this->SetXY($x + $w[3], $this->GetY() - $yH);
			
			$this->Cell($w[4], 12, $qtyvalue.".00", 'TB', 0, 'C');
			$this->Cell($w[6], 12, $pcostvalue.".00", 'TB', 0, 'R');
			$this->Cell($w[7], 12, $pamtvalue.".00", 'TB', 1, 'R');
	
      
	
}
	
	
function recurbill_print($con,$prodreg,$billno)
	{

	$billSQL = "SELECT  `id`, `cmp`, `code`, `gencode`, `color`, `stock`, `phystock`, `reorder`, `cat`, `ratio`, `ratiotot`, `sizesetcode`, `diffmrp`, `diffprice`, `sets`, `desc`, `unit`, `ptotal`, `pdiscount`, `pnamount`,`gstper`, `gstamt`, `grtotal`, `taxtype`, `status` FROM  $prodreg  WHERE id = '$billno'";
//		echo $billSQL;
if($billdet_res=mysqli_query($con,$billSQL))
echo mysqli_error($con);


/*	$jobcodeto = "SELECT jobcodeto, name , address    FROM  $jobreg INNER JOIN commst ON jobcodeto=compid  WHERE jobid = '$billno'";
     $jobcodeto_res=mysqli_query($con,$jobcodeto);
	 $jobcodeto_row = mysqli_fetch_assoc($jobcodeto_res);*/

//echo $jobcodeto;
while($billdet_res_row = mysqli_fetch_array($billdet_res))
{
$this->AddPage();
$this->SetFont('helvetica', 'B', 12);
$this->SetY(20);

$start_border=$this->GetY()-20;
$this->SetFont('helvetica', 'B');
$this->Cell(250, 13, "$_SESSION[SESS_CMP]");

$this->SetX(350);
$this->SetFont('helvetica', 'B');
$this->Cell(80, 13, "DOC NO");
$this->Cell(100, 13, ": ".$billdet_res_row['id'],0,1);
$this->SetX(30);
$set_y1=$this->GetY();
$this->SetFont('helvetica','',12);
$this->Cell(0, 13, "");

$set_y2=$this->GetY();
$this->SetXY(350,$set_y1);
$this->SetFont('helvetica', 'B');
$this->Cell(80, 13, "DATE");
$this->Cell(100, 13, ": ".$billdet_res_row['code'],0,1);

$set_y1=$this->GetY()+10;
$this->SetFont('helvetica','',10);
$this->MultiCell(200, 13, "$_SESSION[SESS_CMPADD1]" , 0);

$this->SetFont('helvetica','B',13);
$this->Cell(0, 13, "Process Transfer",'B',0,'C');

$this->SetFont('helvetica','B',10);
$this->SetXY(400,$this->GetY());

$statusArray = $_SESSION[SESS_STATUSARR]; 
$this->Cell(0, 13, "PROCESS : ".$billdet_res_row['status'],0,1,'L');


$this->SetY($this->GetY()+10);
$this->SetFont('helvetica','B',10);
$this->Cell(0, 13, "Recd From : ".$billdet_res_row['gencode'],0,0,'L');

$this->SetXY(330,$this->GetY());
$this->Cell(0, 13, "Send to : ",0,0,'L');
$this->SetXY(390,$this->GetY());
$this->Cell(0, 13, $jobcodeto_row['name'],0,0,'L');
$this->SetXY(390,$this->GetY()+10);
$this->SetFont('helvetica','',10);
$this->MultiCell(180, 13, $jobcodeto_row['address'] , 0);

//$this->Generate_Table($con,$jobreg,$proddet,$jobvar,$billdet_res_row['jobid']);
$this->Generate_Table($con,$proddet,$billno);
$set_y1=$this->GetY();
$table="";
		
	$taxonside=0;
	
	
		//if($billdet_res_row['incstax']==0)
		$taxonside=1;
		//}
	if($taxonside==1)
	$this->SetY(450);
	else
	$this->SetY(480);
	
	$this->Ln(29);





$this->SetY(710);

$this->SetFont('helvetica', 'B',13);
$this->Line(16,$this->GetY(),583,$this->GetY());
$this->Ln(10);
$this->Cell(0, 15, "Remark :  ".$billdet_res_row['cat'],0,0,'L');

$this->SetFont('helvetica', 'B',15);

$this->SetXY(350,$this->GetY());
$this->Cell(0, 13, "Total Amt Payable :   ",0,0,'L');
$this->SetXY(370,$this->GetY());
$this->Cell(0, 13, $this->totalamt.".00",0,1,'R');


$this->SetY($this->GetY()+50);
$this->SetFont('helvetica', 'B',12);
$this->Cell(50, 13, "Received By",0,0,'L');
$this->SetY($this->GetY());
$this->Cell(0, 13, "Checked By",0,0,'C');
$this->SetXY(600,$this->GetY());
$this->Cell(0, 13, "Autho.Sign",0,0,'R');

$this->Ln(10);
$end_border=$this->GetY();




	$this->make_border($start_border+10,$end_border+10);
	
	
}// while recurbill_print



	}	// function recurbill_print ends
	
	
	function make_border($start_border,$end_border)
	{
		$this->Line(14,$start_border,586,$start_border);
			$this->Line(16,$start_border+2,584,$start_border+2);
			
			
			$this->Line(14,$start_border,14,$end_border); 
			$this->Line(16,$start_border+2,16,$end_border-2); 
			
			
			$this->Line(584,$start_border+2,584,$end_border-2);
			$this->Line(586,$start_border,586,$end_border);
			
			$this->Line(16,$end_border,584,$end_border);
			$this->Line(14,$end_border+2,586,$end_border+2);
		}
	
}//class ends

		
$pdf = new PDF_result();	
//require('../../fpdf/dash.php');

$billno=$_POST['jobid'];

include "../db_connect.php";
include '../db_table_select.php';


$pdf->recurbill_print($con,$prodreg,$billno);
//$pdf->Generate_Table($con,$jobreg,$proddet,$jobvar,$billno);

$pdf->Output('product_det.pdf', 'F');



?>