<?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');
    }
    $supplierid        = is_numeric($_GET['supplier']) ? $_GET['supplier'] : 0;
    
    $sqlsupplierQuery = mysqli_query($conn,"SELECT id, sname   FROM supplier_master WHERE id='$supplierid' ");
    $rowsupplier    = mysqli_fetch_array($sqlsupplierQuery,MYSQLI_ASSOC);
    $supplier_Fillter      = $rowsupplier['sname'];

   $sql = "SELECT a.id,a.scode,a.sname,a.mobile,sum(a.pamount) as pamount ,a.purchasetype,a.pdate
		FROM (
			SELECT sm.id,sm.scode,sm.sname,sm.mobile,pdi.amount as pamount, 0 as payamount, 'Internal Diesel Purchase' as purchasetype ,pdi.dieseldate as pdate
		
			FROM supplier_master as sm 
		
			LEFT JOIN purchase_dieselinvoice as pdi ON sm.id=pdi.supplierid 
		
			WHERE sm.isdelete=1 and pdi.isdelete=1 
		";
		 if($tripfromDate !=''  && $triptoDate !='' && $supplierid ==''  ) {
			$sql.=" and pdi.dieseldate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'";
   
		}  else if($tripfromDate !=''  && $triptoDate !='' && $supplierid !=''  ) {
		   $sql.=" and sm.id='".$supplierid."'  and   pdi.dieseldate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'  ";
   
		   }  else if($tripfromDate ==''  && $triptoDate =='' && $supplierid !='' )  {
			$sql.="  and sm.id='".$supplierid."' ";
   
		}

		$sql.="
			GROUP BY pdi.id
			
			UNION ALL
			
			SELECT sm.id,sm.scode,sm.sname,sm.mobile,(psh.amount) as pamount ,0 as payamount , 'spare' as purchasetype,psh.invoicedate as pdate
		
			FROM supplier_master as sm 
		
			LEFT JOIN purchase_spareinvoice_hdr as psh ON sm.id=psh.supplierdetails
		
			WHERE sm.isdelete=1 and psh.isdelete=1
		";

		if($tripfromDate !=''  && $triptoDate !='' && $supplierid ==''  ) {
			$sql.=" and psh.invoicedate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'";
   
		}  else if($tripfromDate !=''  && $triptoDate !='' && $supplierid !=''  ) {
		   $sql.=" and sm.id='".$supplierid."'  and   psh.invoicedate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'  ";
   
		   }  else if($tripfromDate ==''  && $triptoDate =='' && $supplierid !='' )  {
			$sql.="  and sm.id='".$supplierid."' ";
   
		}
		
		
		$sql.="
			GROUP BY psh.id
			
			UNION ALL
			
			SELECT sm.id,sm.scode,sm.sname,sm.mobile,(expl.amount) as pamount,0 as payamount ,'Explosive Prchase' as purchasetype,expl.invoicedate as pdate
		
			FROM supplier_master as sm 
		
			LEFT JOIN purchase_explosiveinvoice_hdr as expl ON sm.id=expl.supplierdetails
		
			WHERE sm.isdelete=1 and expl.isdelete=1
			";

			if($tripfromDate !=''  && $triptoDate !='' && $supplierid ==''  ) {
				$sql.=" and expl.invoicedate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'";
	   
			}  else if($tripfromDate !=''  && $triptoDate !='' && $supplierid !=''  ) {
			   $sql.=" and sm.id='".$supplierid."'  and   expl.invoicedate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'  ";
	   
			   }  else if($tripfromDate ==''  && $triptoDate =='' && $supplierid !='' )  {
				$sql.="  and sm.id='".$supplierid."' ";
	   
			}
		
		$sql.="
			GROUP BY expl.id
			
			UNION ALL
			
			SELECT sm.id,sm.scode,sm.sname,sm.mobile,(poh.amount) as pamount,0 as payamount ,'External Diesel Fill' as purchasetype,poh.dieseldate as pdate
		
			FROM supplier_master as sm 
		
			LEFT JOIN externalfill_diesel as poh ON sm.id=poh.supplierid
		
			WHERE sm.isdelete=1 and poh.isdelete=1
			";

			if($tripfromDate !=''  && $triptoDate !='' && $supplierid ==''  ) {
				$sql.=" and poh.dieseldate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'";
	   
			}  else if($tripfromDate !=''  && $triptoDate !='' && $supplierid !=''  ) {
			   $sql.=" and sm.id='".$supplierid."'  and   poh.dieseldate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'  ";
	   
			   }  else if($tripfromDate ==''  && $triptoDate =='' && $supplierid !='' )  {
				$sql.="  and sm.id='".$supplierid."' ";
	   
			}
		
		$sql.="
			GROUP BY poh.id
			
			
			UNION ALL
			
			
			SELECT sm.id,sm.scode,sm.sname,sm.mobile,(pth.amount) as pamount,0 as payamount ,'External Diesel Unfill' as purchasetype,pth.dieseldate as pdate
		
			FROM supplier_master as sm 
		
			LEFT JOIN externalpro_diesel as pth ON sm.id=pth.supplierid
		
			WHERE sm.isdelete=1 and pth.isdelete=1
			";

			if($tripfromDate !=''  && $triptoDate !='' && $supplierid ==''  ) {
				$sql.=" and pth.dieseldate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'";
	   
			}  else if($tripfromDate !=''  && $triptoDate !='' && $supplierid !=''  ) {
			   $sql.=" and sm.id='".$supplierid."'  and   pth.dieseldate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'  ";
	   
			   }  else if($tripfromDate ==''  && $triptoDate =='' && $supplierid !='' )  {
				$sql.="  and sm.id='".$supplierid."' ";
	   
			}
		
		$sql.="
			GROUP BY pth.id
		)
		a ";
        $sql.=" GROUP BY a.pdate,a.id,a.purchasetype";
    
    $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('Purchase Report');
$pdf->SetSubject('Purchase 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
Purchase 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>Supplier : $supplier_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>
        <td>Date</td>
        <td>Vehicle</td>
        <td>KM</td>
        <td>Hours</td>
        <td>Litres</td>
    </tr>
    


EOD;
$pdf->SetFont('helvetica', '', 8);
while($row = mysqli_fetch_array($query))
    {
        $triptdate  = new DateTime($row["pdate"]); 
        $tdate  = $triptdate->format('d-m-Y'); 
        
        $sname_arr  =   $row["sname"];
        $mobile_arr    =   $row["mobile"];
        $pamount_arr   =   $row["pamount"];
        $purchasetype_arr      =   $row["purchasetype"];
       

$tbl.= <<<EOD
    <tr>
        <td>$tdate</td>
        <td>$sname_arr</td>
        <td>$mobile_arr</td>
        <td>$pamount_arr</td>
        <td>$purchasetype_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');
   
?>