<?php
include "../dbconfig.php";
include "../pdf_demo/fpdf.php";
$sql=$_POST['query'];
//print_r($_POST);
//exit;
$pdf=new FPDF();
define('£',chr(163));

class PDF extends FPDF
{
// Page header
function Header()
{
    // Logo
    //$this->Image('logo.png',10,6,30);
    // Arial bold 15
    $this->SetFont('Arial','B',15);
    // Move to the right
    $this->Cell(80);
    // Title
    $this->Cell(30,10,'List of Invoices',1,0,'C');
    // Line break
    $this->Ln(20);
}

// Page footer
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,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}

//Disable automatic page break
$pdf->SetAutoPageBreak(false);

//Add first page
$pdf->AddPage('L');

//set initial y axis position per page
$y_axis_initial = 25;
$pdf->SetFillColor(255,0,255);
$pdf->SetTextColor(0,0,0);
$pdf->SetFont( 'Arial', '', 20 );
    // Move to the right
	$pdf->SetX(15);
    $pdf->Cell(150,10,'List of Invoices',1,10);
    // Title
   $pdf->SetY(-15);
    // Arial italic 8
    $pdf->SetFont('Arial','I',8);
    // Page number
    $pdf->Cell(0,10,'Page '.$pdf->PageNo().'/{nb}',0,0,'C'); 
//$pdf->Write(20, "List of Invoices" );
//print column titles for the actual page
$pdf->SetFillColor(232, 232, 232);
$pdf->SetFont('Arial', 'B', 12);
$pdf->SetY($y_axis_initial);
$pdf->SetX(15);
$pdf->Cell(20, 6, 'Sr.No.', 1, 0, 'L', 1);
        $pdf->Cell(30, 6, 'Invoice No.', 1, 0, 'L', 1);
        $pdf->Cell(100, 6, 'Company', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'Nett', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'VAT', 1, 0, 'L', 1);
        $pdf->Cell(30, 6, 'Gross', 1, 0, 'L', 1);
        $pdf->Cell(30, 6, 'Status', 1, 0, 'R', 1);

$row_height = 6;
$y_axis = 25;
$y_axis = $y_axis + $row_height;
//echo $sql;
//exit;
$report=mysqli_query($con,$sql);


//initialize counter
$i = 0;

//Set maximum rows per page
$max = 25;

//Set Row Height

 $No = 0;
while($row = mysqli_fetch_array($report))
{
    
	//If the current row is the last one, create new page and print column title
    if ($i == $max)
    {
        $pdf->AddPage('L');

        //print column titles for the current page
        $pdf->SetY($y_axis_initial);
        $pdf->SetX(25);
		$pdf->Cell(20, 6, 'Sr.No.', 1, 0, 'L', 1);
        $pdf->Cell(30, 6, 'Invoice No.', 1, 0, 'L', 1);
        $pdf->Cell(100, 6, 'Company', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'Nett', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'VAT', 1, 0, 'L', 1);
        $pdf->Cell(30, 6, 'Gross', 1, 0, 'L', 1);
        $pdf->Cell(30, 6, 'Status', 1, 0, 'R', 1);

        //Go to next row
        $y_axis = $y_axis + $row_height;

        //Set $i variable to 0 (first row)
        $i = 0;
    }
     $No = $No +1;
    $invoice_no = $row['invoice_no'];
	$company = $row['name'];
    $nett = number_format($row['nett'],2);
	$vat = number_format($row['vat'],2);
	
	$gross = number_format($row['gross'],2);
    $status = $row['status'];

    $pdf->SetY($y_axis);
    $pdf->SetX(15);
	$pdf->Cell(20, 6, $No, 1, 0, 'L', 1);
    $pdf->Cell(30, 6, $invoice_no, 1, 0, 'L', 1);
	$pdf->Cell(100, 6, $company, 1, 0, 'L', 1);
	$pdf->Cell(30, 6, £.$nett, 1, 0, 'R', 1);
	$pdf->Cell(30, 6, £.$vat, 1, 0, 'R', 1);
    $pdf->Cell(30, 6, £.$gross, 1, 0, 'R', 1);
    $pdf->Cell(30, 6, $status, 1, 0, 'R', 1);

    //Go to next row
    $y_axis = $y_axis + $row_height;
    $i = $i + 1;
	$total_net =number_format($total_net+$nett,2);
	$total_vat =number_format($total_vat+$vat,2);
	$total_gross =number_format($total_gross+$gross,2);
}
$pdf->SetFillColor(0,0,255);
$pdf->SetTextColor(255,255,255);
$pdf->SetFont( 'Arial', '', 15);
$pdf->SetXY(135,$y_axis);
$pdf->Cell(30, 6, "Totals", 1, 0, 'R', 1);
$pdf->SetXY(165,$y_axis);
$pdf->Cell(30, 6, £.$total_net, 1, 0, 'R', 1);
$pdf->SetXY(195,$y_axis);
$pdf->Cell(30, 6, £.$total_vat, 1, 0, 'R', 1);
$pdf->SetXY(225,$y_axis);
$pdf->Cell(30, 6, £.$total_gross, 1, 0, 'R', 1);

//Create file
ob_clean();
$pdf->Output('dd.pdf','D');

?>