<?php 
ob_start();
session_start();
include "../config/config.php";
$allPermissions = $_SESSION['permission'];

$requestData= $_REQUEST;


/*Fetch Database column Details*/
        $columns = array( 
            0 =>'id', 
            1 =>'name', 
            2 =>'email',
            3 =>'password',
            4 =>'phone',
            5 =>'address',
            6 =>'role',
            7 =>'role_id',
            8 =>'id',
            9 =>'id'
           
        );
        
$conditions="";

$status = $_POST['status'];
$customer_id = $_POST['customer_id'];
$scheme_id = $_POST['scheme_id'];
$payment_month = $_POST['payment_month'];
$year = $_POST['year'];
$payment_status = $_POST['payment_status'];
$cus_scheme_id = $_POST['cus_scheme_id'];
$payment_type = $_POST['payment_type'];


if($status!=""){
    $conditions.=" and tbl_customer_scheme.status=".$status;
}
if($customer_id!=""){
    $conditions.=" and tbl_customer_scheme.customer_id=".$customer_id;
}
if($payment_month!=""){
    $conditions.=" and t.month=".$payment_month;
}
if($scheme_id!=""){
    $conditions.=" and tbl_schemes.id=".$scheme_id;
}
if($year!=""){
    $conditions.=" and t.year='".$year."'";
}
if($payment_type!=""){
    $conditions.=" and t.payment_type=".$payment_type;
}
if($cus_scheme_id!=""){
    $conditions.=" and tbl_customer_scheme.id=".$cus_scheme_id;
}        


$sql_query = "SELECT t.*,
    	tbl_customer_scheme.scheme_no,
    	tbl_customer_scheme.id AS 'scheme_row_id',
    	tbl_customers.customer_name, tbl_customers.mobile_no,
    	tbl_schemes.name as scheme_name,
    	tbl_payment_type.name as payment_type_name
    	FROM `tbl_payments`AS t 
        LEFT JOIN tbl_customer_scheme ON tbl_customer_scheme.id=t.scheme_id
        LEFT JOIN tbl_customers ON tbl_customers.id=tbl_customer_scheme.customer_id
        LEFT JOIN tbl_schemes ON tbl_schemes.id=tbl_customer_scheme.scheme_id
        LEFT JOIN tbl_payment_type ON tbl_payment_type.id=t.payment_type";

/*Fetch All Data Start  */        
        
    	$statement = $pdo->prepare("$sql_query

    	WHERE t.id!=0 ".$conditions." ORDER BY t.id DESC");
		$statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalData = $statement->rowCount();
        $totalFiltered = $totalData;
/*Fetch All Data End*/       
        
/*Fetch Filter Data Start*/    
         $sql = "$sql_query
    	WHERE t.id!=0 ".$conditions;
         
        if( !empty($_REQUEST['search']) ) {   
        	$sql.=" AND ( tbl_customers.customer_name LIKE '%".$_REQUEST['search']."%' ";  
        	$sql.=" OR tbl_customer_scheme.scheme_no LIKE '%".$_REQUEST['search']."%' ";
        	$sql.=" OR tbl_payment_type.name LIKE '%".$_REQUEST['search']."%' ) ";
        }
        
/*Fetch Filter Data End*/       
        


         $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
       	
       	error_log($sql);
        $statement = $pdo->prepare($sql);
        $statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);

$data = array();
$i=1;
foreach( $query as $row ) {  // preparing an array
	$nestedData=array(); 
	$action= "<div class='text-center'><div class='btn-group'>";
	 
	 
		                
		                 $action.="<a style='padding: 6px;margin-right: 3px;' target='_blank' href='scheme-payment-print.php?id=".$row["scheme_row_id"]."' title='Print Scheme Payment' class='tip btn btn-info btn-xs'>
		                    <i class='fa fa-print'></i>
		                </a> ";
	           
		                

		                
	$action.="</div></div>";
	
	if(!empty($row["scheme_months"]) && $row["payment_paid_month_counts"]){
	    $payment_pending_months = $row["scheme_months"]-$row["payment_paid_month_counts"];
	}
	
	
	$nestedData[] = $action;	

    $month = $row['month'];

            if($month==1){
                $payment_month = "January";
            }else if($month==2){
                $payment_month = "February";
            }else if($month==3){
                $payment_month = "March";
            }else if($month==4){
                $payment_month = "April";
            }else if($month==5){
                $payment_month = "May";
            }else if($month==6){
                $payment_month = "June";
            }else if($month==7){
                $payment_month = "July";
            }else if($month==8){
                $payment_month = "August";
            }else if($month==9){
                $payment_month = "September";
            }else if($month==10){
                $payment_month = "October";
            }else if($month==11){
                $payment_month = "November";
            }else if($month==12){
                $payment_month = "December";
            }

	$nestedData[] = "<div class='text-center'>".$i."</div>";
	$nestedData[] = "<div class='text-center'>".$row["customer_name"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["mobile_no"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row['scheme_no']."</div>";
	$nestedData[] = "<div class='text-center'><span class='btn btn-primary btn-xs'>".$row["scheme_name"]."</span></div>";
	$nestedData[] = "<div class='text-center'><span class='btn btn-warning btn-xs amount_span'><i class='fa fa-inr'></i> ".number_format($row['amount'],2)."</span></div>";
	$nestedData[] = "<div class='text-center'>".$payment_month." - ".$row['year']."</div>";
	$nestedData[] = "<div class='text-center'>".$row["payment_type_name"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["reference_no"]."</div>";
	$nestedData[] = "<div class='text-center'>".date('d-m-Y h:i A', strtotime($row["created_at"]))."</div>";
	

        	
        	
	$data[] = $nestedData;
	$i++;
	

}

$json_data = array(
			"draw"            => intval( $requestData['draw'] ),  
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ), 
			"data"            => $data   
			);

echo json_encode($json_data); 




?>