<?php 
	date_default_timezone_set('UTC');
	//require('fpdf/fpdf.php');
    require_once("config/configDB.php");
    require_once('tcpdf/config/tcpdf_config.php');
    require_once("tcpdf/tcpdf.php");

    $tripfromDate ='';
    if($_GET['startDate']) {
        $invoicedate_temp   = new DateTime(stripslashes($_GET['startDate']));
        $tripfromDate 		= $invoicedate_temp->format('Y-m-d');
    }
    $triptoDate ='';
    if($_GET['endDate']) {
        $invoicedate_temp1   = new DateTime(stripslashes($_GET['endDate']));
	    $triptoDate 		= $invoicedate_temp1->format('Y-m-d');
    }
    $tripvehicle        = is_numeric($_GET['vehicle']) ? $_GET['vehicle'] : 0;
    
    $sqlVehicleQuery = mysqli_query($conn,"SELECT id, vehicleno  FROM vehicle_master WHERE id='$tripvehicle' ");
    $rowvehicle    = mysqli_fetch_array($sqlVehicleQuery,MYSQLI_ASSOC);
    $tripvehicle_Fillter      = $rowvehicle['vehicleno'];

    $sql = "SELECT ish.invoicedate, ish.vehicleno,ish.kms,ish.hrs, ish.givenby, ish.receivedby,ish.remarks,ish.id,vm.vehicleno 
		FROM internalpro_spare_hdr as ish  
		LEFT JOIN  vehicle_master as vm ON vm.id=ish.vehicleno  
		WHERE ish.isdelete=1";

	if($tripfromDate !=''  && $triptoDate !='' && $tripvehicle ==0  ) {
	 	$sql.=" and ish.invoicedate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'";

	}  else if($tripfromDate !=''  && $triptoDate !='' && $tripvehicle >0  ) {
		$sql.=" and ish.vehicleno='".$tripvehicle."'  and   ish.invoicedate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'  ";

   	}  else if($tripfromDate ==''  && $triptoDate =='' && $tripvehicle >0 )  {
	 	$sql.="  and ish.vehicleno='".$tripvehicle."' ";

    }
     
    $query=mysqli_query($conn, $sql);
    
    class MYPDF extends TCPDF {

        //Page header
        public function Header() {
            // Logo
            //$image_file = K_PATH_IMAGES.'logo_example.jpg';
            //$this->Image($image_file, 10, 10, 15, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
            // Set font
            $this->SetY(12);
            $this->SetFont('helvetica', 'B', 24);
            // Title
            $this->Cell(0, 25, PDF_HEADER_TITLE, 0, false, 'C', 0, '', 0, false, 'M', 'M');
            $style3 = array('width' => 0.2, 'cap' => 'round', 'join' => 'round', 'dash' => 0, 'color' => array(0, 0, 0));
            //$this->Line(0, 22, 210, 22, $style3);
        }
    
        // Page footer
        public function Footer() {
            // Position at 15 mm from bottom
            $this->SetY(-15);
            // Set font
            $this->SetFont('helvetica', 'I', 8);
            // Page number
            $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');
        }
    }

    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('thinkofmind.com');
$pdf->SetTitle('Spare Out Report');
$pdf->SetSubject('Spare Out Report');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
//$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
	require_once(dirname(__FILE__).'/lang/eng.php');
	$pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set font
$pdf->SetFont('helvetica', '', 20);

// add a page
$pdf->AddPage();

$style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => '10,20,5,10', 'phase' => 10, 'color' => array(255, 0, 0));
$style2 = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(255, 0, 0));
$style3 = array('width' => 0.2, 'cap' => 'round', 'join' => 'round', 'dash' => 0, 'color' => array(0, 0, 0));
// Line
//$pdf->Line(5, 10, 5, 30, $style2);


// set some text to print
$txt = <<<EOD
Spare Out Reports

EOD;

// print a block of text using Write()
$pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0);

$pdf->SetFont('helvetica', '', 10);

$pdf->Write(2, '', '', 0, 'C', true, 0, false, false, 0);

$tbl = <<<EOD
<table cellspacing="0" cellpadding="1" >
    <tr>
        <td>From : $tripfromDate</td>
        <td>To : $triptoDate</td>
        <td>Vehicle : $tripvehicle_Fillter</td>
    </tr>
</table>           

EOD;

// print a block of text using Write()
$pdf->writeHTML($tbl, true, false, false, false, '');
//$pdf->Write(1, '', '', 0, 'C', true, 0, false, false, 0);

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


$tbl = <<<EOD
<table cellspacing="0" cellpadding="1" border="1">
    <tr>
    <th>Date</th>
    <th>Vehicle/Machinery</th>  
	<th>Kms</th>  
	<th>Hrs</th>                    
    <th>Spare Details</th>                    
    <th>Given By</th>
    <th>Received By</th>
    </tr>
    


EOD;
$pdf->SetFont('helvetica', '', 8);
while($row = mysqli_fetch_array($query))
    {
        $triptdate  = new DateTime($row["invoicedate"]); 
        $tdate  = $triptdate->format('d-m-Y'); 
        
		$edit_id  = $row["id"];
	$Details_Result  = mysqli_query($conn,"SELECT  ppsd.spareID, spm.productname as sparename ,ppsd.spareqty
FROM internalpro_spare_dtl as ppsd 
LEFT JOIN product_master as spm ON spm.id=ppsd.spareID

WHERE  ppsd.hdr_id='$edit_id' ");
	 $DetailsTable ='';
	 while($drow = mysqli_fetch_array($Details_Result))
	 {
	 	$DetailsTable .= $drow['sparename'].'-&nbsp&nbsp Qty&nbsp:&nbsp'.$drow['spareqty'].',&nbsp&nbsp&nbsp';	
	 }

        $vehicleno_arr  =   $row["vehicleno"];
		$kms  =   $row["kms"];
		$hrs  =   $row["hrs"];
        $DetailsTable_arr    =   $DetailsTable;
        $givenby_arr   =   $row["givenby"];
        $receivedby_arr   =   $row["receivedby"];

$tbl.= <<<EOD
    <tr>
        <td>$tdate</td>
        <td>$vehicleno_arr</td>
		<td>$kms</td>
		<td>$hrs</td>
        <td>$DetailsTable_arr</td>
        <td>$givenby_arr</td>
        <td>$receivedby_arr</td>
    </tr>
EOD;
    }
$tbl.= <<<EOD
    </table>
EOD;
$pdf->writeHTML($tbl, true, false, false, false, '');

//Close and output PDF document
$pdf->Output('example_048.pdf', 'I');
   
?>