<?php 

// Instanciation of inherited class
	if(isset($_GET['accid'])){
		require('fpdf/fpdf.php');
		
        // $this->Cell(50, 30, 'Order #'.$this->orderNumber);
       
		require 'connection.php';

		$acId = $_GET['accid'];
		$from = $_GET['from'];
		$to = $_GET['to'];

		$qry = "SELECT * from accreg where accid = '$acId'";
        $run = mysqli_query($connect, $qry);
        $row = mysqli_fetch_array($run);
        $accId = $row['accid'];
        $name = $row['accname'];
       

		$qry1 = "SELECT min(dated) as mindate , max(dated) as maxdate from broilertransfer where dated>='$from' and dated<='$to' and supid='$accId' or  dated>='$from' and dated<='$to' and accid='$accId'";
        $run1 = mysqli_query($connect, $qry1);
        $row1 = mysqli_fetch_array($run1);
        $mindated = $row1['mindate'];
        $maxdated = $row1['maxdate'];
      	
       

        $fdate = $mindated;
         $dt1= date_create($fdate);
         $fdt=date_format($dt1,"d-m-Y");

        $ldate = $maxdated;
         $dt2= date_create($ldate);
         $ldt=date_format($dt2,"d-m-Y");

// Page header


// Instanciation of inherited class
        // end of opening balance
		// end data
		$pdf = new FPDF('P','mm','letter');
		
		$pdf->AliasNbPages();
	
		// page settings start
		//add new page
		$pdf->AddPage();

		  $pdf->SetLeftMargin('5');
		 $pdf->SetRightMargin('5');
		//set font to arial, bold, 14pt

		$pdf->SetFont('Arial','B',20);

		//Cell(width , height , text , border , end line , [align] )
		$pdf->Cell(28, 5);
		$pdf->Cell(120 ,5,'Broiler Report',0,0,'C');

		$pdf->SetFont('Arial','B',10);
		$pdf->Cell(95 ,5,'',0,0);
		// $pdf->Cell(50 ,5,'From: '.$fdt.' To: '.$ldt,0,0);
		$pdf->Cell(59 ,5,'',0,1);//end of line

		//set font to arial, regular, 12pt
		$pdf->SetFont('Arial','B',10);

		//contact detail
		$pdf->Cell(10,2,'',0,1);//end line
	
		//contact detail
		
		//contact detail
		$pdf->Cell(130,3,'',0,0);//end line
		// $pdf->Cell(20 ,3,'Phone: '.$row['phone'],0,0,'L');
		$pdf->Cell(59 ,3,'',0,1);//end of line
		$pdf->Cell(80,3,'',0,1);//end line
		$pdf->Cell(5,3,'',0,0);//end line
		// $pdf->Cell(20,5,'Account Name:',0);
		//delivery detail
		$pdf->SetFont('Arial','B',10);
		// $pdf->Cell(40,3,'',0,1);//end line
		// $pdf->Cell(60,3);
		$pdf->Cell(130 ,5,$row['accname'],10,0,'L ');
		$pdf->SetFont('Arial','B',8);

		$pdf->Cell(15 ,5,'From  ',10,0,'L ');
		$pdf->Cell(20 ,5,$fdt,10,0,'L ');
		$pdf->Cell(10 ,5,'To  ',10,0,'L ');

		$pdf->Cell(20 ,5,$ldt,10,0,'L ');

		$pdf->Cell(59 ,4,'',0,1);//end of line

		//set font to arial, regular, 12pt
		$pdf->SetFont('Arial','',12);
		$pdf->SetTextColor(255,255,255);
		//contact detail
		// $pdf->Cell(130,5,'',0,1);//end line
		$pdf->Cell(130 ,3,'Account ID: '.$accId,0,0,'C');
		$pdf->Cell(59 ,3,'',0,1);//end of line

		$pdf->SetFont('Arial','',12);
		$pdf->SetTextColor(0,0,0);
		// code to fetch detail from db

		//billing address
		$pdf->SetFont('Arial','',8);
		// $pdf->Cell(80, 5,'Page '.$pdf->PageNo().' of '.$pdf->PageNo(), 0,0,'L');
		// $pdf->Cell(150 ,5,'*********Opening Balance*********',0,0,'C');//end of line
		// $pdf->Cell(50 ,5,round($cashOp,0),0,1,'R');

		// //make a dummy empty cell as a vertical spacer
		// $pdf->Cell(189 ,2,'',0,1);//end of line

		//invoice contents
		$pdf->SetFont('Arial','',8);

		$pdf->Cell(15 ,5,'Date','B,T,L',0,'C');
		$pdf->Cell(15 ,5,'V.No','B,T',0,'C');
		$pdf->Cell(40 ,5,'Description','B,T',0,'C');//end of line
		$pdf->Cell(30 ,5,'Qty','B,T',0,'R');//end of line
		$pdf->Cell(20 ,5,'Kat','B,T',0,'R');//end of line
		$pdf->Cell(20 ,5,'Rate','B,T',0,'R');//end of line
		$pdf->Cell(35 ,5,'Amount','B,T',0,'R');//end of line
		$pdf->Cell(30 ,5,'Remarks','B,T,R',1,'R');//end of line

		$pdf->SetFont('Arial','',8);

		// fetch data
		$qtysum = 0;
		$amtsum = 0;
		$rates = 0;
		$acidtype=$acId;
		
		$amts = 0;
		
		$qry = "SELECT * from broilertransfer where supid = '$acId' and dated >= '$from' and dated <= '$to' OR accid = '$acId' and dated >= '$from' and dated <= '$to' order by dated,trid asc";
        $run_qry = mysqli_query($connect, $qry);
        while($row = mysqli_fetch_array($run_qry)){
        	if($row['supid']==$acidtype){
        	$rates=$row['vrate'];
        	$amts=$rates*$row['qty'];
        	}
     		if($row['accid']==$acidtype){
        	$rates=$row['crate'];
        	$amts=$rates*$row['qty'];

        	}

        	$qtysum = $qtysum+$row['qty'];
        	$amtsum = $amtsum+$amts;
			//Numbers are right-aligned so we give 'R' after new line parameter
			$rundate = $row['dated'];
         $dat= date_create($rundate);
         $dated=date_format($dat,"d-m-Y");

			$pdf->Cell(15 ,6,$dated,'B,L',0,'J');
			$pdf->Cell(15 ,6,$row["vno"],'B',0,'C');
			 $pdf->Cell(40 ,6,'Ref No. '.$row["refno"],'B',0,'J');
			$pdf->Cell(30 ,6,$row["qty"],'B',0,'R');
			$pdf->Cell(20 ,6,$row["kat"],'B',0,'R');//end of line
			$pdf->Cell(20 ,6,$rates,'B',0,'R');//end of line
			$pdf->Cell(35 ,6,round($amts,3),'B',0,'R');//end of line
			$pdf->Cell(30 ,6,'','BR',1,'C');//end of line

			// $pdf->Cell(20 ,6,$bal = $bal+($row['debit']-($row['credit'])),'B,R',1,'R');//end of line
			//Numbers are right-aligned so we give 'R' after new line parameter
		}
		$pdf->SetFont('Arial', '', 8);
		$pdf->Cell(10 ,2,"",'T',0,'C');
		$pdf->Cell(2 ,2,'','T',0,'C');
		$pdf->Cell(25 ,2,"",'T',0,'C');
		$pdf->Cell(31 ,2,"",'T',0,'C');

		$pdf->Cell(38 ,5,$qtysum,'T',0,'R');
		$pdf->Cell(70 ,5,$amtsum,'T',0,'R');
		// ($final ,2);
		if($amtsum>0){
			$pdf->Cell(21 ,5,number_format($amtsum/$qtysum,4),'T',0,'R');
		}
		
		// $pdf->Cell(20 ,5,$debitT-$creditT,'T',1,'R');
		
		//summary
		$pdf->SetFont('Arial', 'B', 12);
		$pdf->Cell(50,2,'',0);
		$pdf->Cell(50 ,2,'',0,0);
		$pdf->Cell(25 ,2,"",0,1,'C');//end of line
		// summary
		$pdf->Cell(84 ,2,'',0,0);
		$pdf->Cell(36 ,6,'',0,0);
		// $pdf->Cell(80 ,8,$closingB,0,1,'R');//end of line

		// footer
		$pdf->SetFont('Arial', 'B', 12);
		// $pdf->Cell();
		// $pdf->SetFillColor(0,0,0);
		$pdf->SetTextColor(255,255,255);
	

		$pdf->SetTextColor(0,0,0);
		//make a dummy empty cell as a vertical spacer
		// $pdf->Cell(159 ,0,'',0,1);//end of line
		// $pdf->Cell(0, 5,'Page '.$pdf->PageNo().' of '.$pdf->PageNo(), 0,0,'L');
// $pdf->Cell(0, 5, "Page " . $pdf->PageNo() . "/{totalPages}", 0, 1);
		// page settings end

		$pdf->Output();
	}
?>
