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

	

	$cID    = $_GET['customerID'];
	$sDate  = $_GET['startDate'];
    $eDate  = $_GET['endDate'];	$sDate 	= new DateTime($sDate);		$eDate 	= new DateTime($eDate);		$sDate 	=$sDate->format('Y-m-d');		$eDate 	=$eDate->format('Y-m-d');

    //echo '--------'.$cID.' ----------'.$sDate.'-------------'.$eDate;
    $client_name  = '';
    if($sDate != '' && $eDate != '' && $cID>0) {

        $sql3 ="SELECT cc.id,cc.cashno, cc.collectiondate, cc.customerID,cc.payment_through, cc.pending, cc.paymenttype, cc.received_amount as amount, cc.paymentreference,cm.cname FROM customer_payment_header as cc LEFT JOIN customer_master as cm on cm.id = cc.customerID  WHERE cc.customerID ='".$cID."' AND cc.collectiondate BETWEEN '".$sDate."' AND '".$eDate."' and cc.status=''";

        $re2 = mysqli_query($conn,"select * from customer_master where id='".$cID."' "); 
		
		if($row2 = mysqli_fetch_array($re2))
		{
            $client_name= $row2['cname'];
        }
    } else if($sDate != '' && $eDate != '' ) {

        $sql3 ="SELECT cc.id,cc.cashno, cc.collectiondate, cc.customerID,cc.payment_through, cc.pending, cc.paymenttype, cc.received_amount as amount, cc.paymentreference,cm.cname FROM customer_payment_header as cc LEFT JOIN customer_master as cm on cm.id = cc.customerID  WHERE cc.collectiondate BETWEEN '".$sDate."' AND '".$eDate."'  and cc.status=''";

    } else {

        $sql3 ="SELECT cc.id,cc.cashno, cc.collectiondate, cc.customerID,cc.payment_through, cc.pending, cc.paymenttype, cc.received_amount as amount, cc.paymentreference,cm.cname FROM customer_payment_header as cc LEFT JOIN customer_master as cm on cm.id = cc.customerID where cc.status=''" ;

    }
	
	//echo $sql3;

	$pdf = new FPDF('P','mm','A4');
	$pdf->SetAutoPagebreak(true,5);
    $pdf->SetMargins(5,5);
	$pdf->AliasNbPages();
	$pdf->AddPage();
	$pdf->Line(5, 4, 207, 4);
	$pdf->Line(5, 36, 207, 36);
	$pdf->Line(5, 4, 5, 292);
	$pdf->Line(207, 4, 207, 292);
	//$pdf->Line(5, 292, 207, 292);
	//$pdf->Line(5, 284, 207, 284);
	//$pdf->Image('images/pdf-header.png',6,5,200,30);
	$pdf->ln(10);
	$pdf->SetFont('Arial','B',12);
	$pdf->Cell(202,10,'Sales Payment Report ',1,1,'C',false);
    $pdf->SetFont('Arial','',10);
    $pdf->Cell(19,12,'From Date ',1,0,'L',false);
    $pdf->Cell(30,12,$sDate,1,0,'L',false);
    $pdf->Cell(15,12,'To Date  ',1,0,'L',false);
    $pdf->Cell(30,12,$eDate,1,0,'L',false);
    $pdf->Cell(35,12,'supplier Name :',1,0,'L',false);
    $pdf->Cell(73,12,$client_name,1,1,'L',false);
	$pdf->SetX(5);
    $width_cell=array(15,60,35,30,32,30,30);
	$pdf->SetFont('Arial','B',8);
	$pdf->SetFillColor(193,229,252);
	$pdf->Cell($width_cell[0],10,'Invoice',1,0,'C',false);
	$pdf->Cell($width_cell[1],10,'Customer Name',1,0,'C',false); 
	$pdf->Cell($width_cell[2],10,'Date',1,0,'C',false); 
	$pdf->Cell($width_cell[3],10,'Amount From',1,0,'C',false);
	$pdf->Cell($width_cell[3],10,'Mode Of Payment',1,0,'C',false);
	$pdf->Cell($width_cell[4],10,'Amount',1,1,'C',false);
    
   

    $re3 = mysqli_query($conn,$sql3); 
    
    
	while($row3 = mysqli_fetch_array($re3))
    {
       	$pdf->SetFont('Arial','',8);
		$pdf->SetFillColor(193,229,252);
		// $pdf->Cell($width_cell[0],8,$ij,1,0,'C',false);
		// $pdf->Cell($width_cell[1],8,'',1,0,'C',false); 
		// $pdf->Cell($width_cell[2],8,'',1,0,'C',false); 
		// $pdf->Cell($width_cell[3],8,'',1,0,'C',false);
		// $pdf->Cell($width_cell[3],8,'',1,0,'C',false);
        // $pdf->Cell($width_cell[4],8,'',1,1,'C',false);
        //SELECT cc.id,cc.cashno, cc.collectiondate, cc.customerID, cc.pending, cc.paymenttype, cc.received_amount as amount, cm.cname
        $pdf->Cell($width_cell[0],8,$row3['cashno'],1,0,'C',false);
		$pdf->Cell($width_cell[1],8,$row3['cname'],1,0,'C',false); 
		$pdf->Cell($width_cell[2],8,$row3['collectiondate'],1,0,'C',false); 
		$pdf->Cell($width_cell[3],8,$row3['payment_through'],1,0,'C',false);
		$pdf->Cell($width_cell[3],8,$row3['paymenttype'].' '.$row3['paymentreference'],1,0,'C',false);
		$pdf->Cell($width_cell[4],8,number_format((float)$row3['amount'],2, '.', ''),1,1,'C',false);
    }
    
	
	$pdf->Output();
?>