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

$requestData= $_REQUEST;


/*Fetch Database column Details*/
        $columns = array( 
            0 =>'id', 
            1 =>'id', 
            2 =>'name',
            3 =>'emp_code',
            4 =>'role_name',
            5 =>'email',
            6 =>'id',
            7 =>'id',
            8 =>'id',
            9 =>'id'
           
        );
        
$conditions="";
$cond1="";
$cond2="";
$cond3="";

$status = $_POST['status'];
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];

if($status!=""){
    $conditions.=" and e.status=".$status;
}

if($start_date!="" && $end_date!=""){
    $cond1.=" and s.date BETWEEN '$start_date' and '$end_date'";
    $cond2.=" and ad.date BETWEEN '$start_date' and '$end_date'";
    $cond3.=" and cs.scheme_added_date BETWEEN '$start_date' and '$end_date'";
}

        
/*Fetch All Data Start*/        
$sql_query = "SELECT e.*, 
(SELECT COALESCE(SUM(s.grand_total),0) FROM tbl_sales s WHERE s.created_user_id=e.id $cond1) as sales_values,
(SELECT COUNT(s.id) FROM tbl_sales s WHERE s.created_user_id=e.id $cond1) as sales_count,
(SELECT COALESCE(SUM(ad.advance_amount),0) FROM tbl_advance_voucher ad WHERE ad.created_user_id=e.id $cond2) as advance_voucher_values,
(SELECT COALESCE(SUM(cs.scheme_amount),0) FROM tbl_customer_scheme cs WHERE cs.sales_man_id=e.id $cond3) as scheme_values
FROM tbl_users e ";

    	$statement = $pdo->prepare("$sql_query
    	where e.id!=0  ".$conditions." ORDER BY e.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 e.id!=0  ".$conditions;
         
        if( !empty($_REQUEST['search']) ) 
        {   
        	$sql.=" AND ( e.name LIKE '%".$_REQUEST['search']."%' ";    
        	$sql.=" OR e.email LIKE '%".$_REQUEST['search']."%' ) ";
        }
        
/*Fetch Filter Data End*/       
        


         $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
       	
        $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(); 
	
	$total_amount = $row["sales_values"]+$row["advance_voucher_values"]+$row["scheme_values"];
	
	$nestedData[] = "<div class='text-center'>".$i."</div>";
	$nestedData[] = "<div class='text-center'>".$row["name"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row['emp_code']."</div>";
	$nestedData[] = "<div class='text-center'>".$row['sales_count']."</div>";
	$nestedData[] = "<div class='text-center'>".$row["sales_values"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["advance_voucher_values"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["scheme_values"]."</div>";
	$nestedData[] = "<div class='text-center'>".number_format($total_amount,2)."</div>";
	
	if($row["status"]==1){
	    $nestedData[] ="<div class='text-center'><span class='btn btn-success btn-xs'><i class='fa fa-eye'></i> Active</span></div>";
	}else if($row["status"]==0){
	    $nestedData[] ="<div class='text-center'><span class='btn btn-danger btn-xs' onClick='DynamicActive(".$row["id"].")'><i class='fa fa-eye-slash'></i> In-Active</span></div>";
	}
        	
        	
	$data[] = $nestedData;
	$i++;
	

}

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

echo json_encode($json_data); 




?>