<?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'];

    $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 , sum(payamount) as payamount,a.purchasetype,a.pdate
		FROM (
			SELECT sm.id,sm.scode,sm.sname,sm.mobile,pdi.amount as pamount, 0 as payamount, 'Diesel' 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 	and pdi.status=''	
			GROUP BY pdi.id	
					
			UNION ALL			
			SELECT sm.id,sm.scode,sm.sname,sm.mobile,(psh.amount) as pamount ,0 as payamount , 'External Diesel Fill' as purchasetype,psh.dieseldate as pdate
		
			FROM supplier_master as sm 
		
			LEFT JOIN externalfill_diesel as psh ON sm.id=psh.supplierid
		
			WHERE sm.isdelete=1 and psh.isdelete=1 and psh.status=''
		
			GROUP BY psh.id
			
			UNION ALL		
			SELECT sm.id,sm.scode,sm.sname,sm.mobile,(poh.amount) as pamount,0 as payamount ,'External Diesel Unfill' as purchasetype,poh.dieseldate as pdate
		
			FROM supplier_master as sm 
		
			LEFT JOIN externalpro_diesel as poh ON sm.id=poh.supplierid
		
			WHERE sm.isdelete=1 and poh.isdelete=1 and poh.status=''
		
			GROUP BY poh.id	
			
			UNION ALL	
					
			SELECT sm.id,sm.scode,sm.sname,sm.mobile,(pth.total_cost) as pamount,0 as payamount ,'Compressor Contractor Work' as purchasetype,pth.utilizedate as pdate
		
			FROM supplier_master as sm 
		
			LEFT JOIN explosive_utiliz_hdr as pth ON sm.id=pth.contractor
		
			WHERE sm.isdelete=1 and pth.status=''
		
			GROUP BY pth.id	
			UNION ALL	
					
			SELECT sm.id,sm.scode,sm.sname,sm.mobile,0 as pamount,(sp.amount) as payamount ,'payment' as purchasetype,sp.invoicedate as pdate		
			FROM supplier_master as sm 		
			LEFT JOIN supplier_payment as sp ON sm.id=sp.supplierid		
			WHERE sm.isdelete=1 and sp.isdelete=1	 and sp.status=''	
			GROUP BY sp.id
		)
		a  where 1=1 ";

	 if($tripfromDate !=''  && $triptoDate !=''  && $supplierid==0 ) {
	 	$sql.=" and a.pdate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'";
	 } else if($tripfromDate !=''  && $triptoDate !=''  && $supplierid >0 ) {
		$sql.=" and a.id='".$supplierid."' and a.pdate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'  ";

   	 } else if($tripfromDate ==''  && $triptoDate ==''  && $supplierid>0 )  {
		$sql.="  and a.id='".$supplierid."' ";

	} 
	
    if($tripfromDate !=''  && $triptoDate !='' ) {
        $sql.="  GROUP BY a.pdate,a.id,a.purchasetype";     
    } else if($tripfromDate !=''  && $triptoDate !='' ) {      
        $sql.="  GROUP BY a.pdate,a.id,a.purchasetype";
    } else {
        $sql.="  GROUP BY a.id";    
    }
    
    $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('Account Report');
$pdf->SetSubject('Account 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
Account 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>
    <th> Scode</th>
    <th>Supplier Name</th>
    <th>Category</th>
    <th>Purchase Amount</th>
    <th>Paid Amount</th>
    <th>Total Balance</th>
    </tr>
    


EOD;
$pdf->SetFont('helvetica', '', 8);
while($row = mysqli_fetch_array($query))
    {
       
        if($tripfromDate !=''  && $triptoDate !='' ) {
            $triptdate  = new DateTime($row["pdate"]); 
            $tdate  = $triptdate->format('d-m-Y'); 
        } else {
            $tdate  = $row['scode'];
        }
        //$vehicleno_arr  =   $row["vehicleno"];
        $sname_arr    =   $row["sname"];
        $purchasetype_arr   =   $row["purchasetype"];
        $pamount_arr   =   $row["pamount"];
        $payamount_arr = $row["payamount"];

        if($tripfromDate !=''  && $triptoDate !='' ) {
    
            $amount_arr = '-';
        } else {
	       $amount_arr   =   $row["pamount"]-$row["payamount"];
       }
        

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