<?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="";

$exp_category = $_POST['exp_category'];
$payment_type = $_POST['payment_type'];
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];

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

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

if($start_date!="" && $end_date!=""){
    $conditions.=" and e.date BETWEEN '".$start_date."' and '".$end_date."'";
}



        
/*Fetch All Data Start*/        

$join = "LEFT JOIN tbl_exp_category ON tbl_exp_category.id=e.category
LEFT JOIN tbl_payment_type ON tbl_payment_type.id=e.payment_type
LEFT JOIN tbl_users cu ON cu.id=e.created_by
LEFT JOIN tbl_users uu ON uu.id=e.updated_by";
        
    	$statement = $pdo->prepare("SELECT e.*,tbl_exp_category.name as category_name,tbl_payment_type.name as payment_type_name,
cu.name as created_user, uu.name as updated_user
 FROM `tbl_expenses` e
$join
    	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*/       
        
        $statements = $pdo->prepare("SELECT COALESCE(SUM(amount),0) as total_expenses_amount FROM `tbl_expenses` e
                                    $join
    	                        WHERE e.id!=0 ".$conditions." ORDER BY e.id DESC");
    	$statements->execute();
		$querys = $statements->fetchAll(PDO::FETCH_ASSOC);
		$totalDatas = $statements->rowCount();
		$total_expenses_amount = $querys[0]['total_expenses_amount'];
		
		
		
/*Fetch Filter Data Start*/    
         $sql = "SELECT e.*,tbl_exp_category.name as category_name,tbl_payment_type.name as payment_type_name,
cu.name as created_user, uu.name as updated_user
 FROM `tbl_expenses` e
$join
    	WHERE e.id!=0 ".$conditions;
         
        if( !empty($_REQUEST['search']) ) 
        {   
        	$sql.=" AND ( e.name LIKE '%".$_REQUEST['search']."%' ";  
        	$sql.=" OR tbl_exp_category.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']."   ";
       	
        $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;' href='expenses_edit.php?id=".$row["id"]."' title='Edit Advance Voucher' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-edit'></i>
		                </a> ";

		                
		       $action.=" <button style='padding: 6px;margin-right: 3px;' title='Delete Expenses' onClick='delete_Details(".$row["id"].")' class='tip btn btn-danger btn-xs'>
		                    <i class='fa fa-trash-o'></i>
		                </button>";
	           
		                

		                
	$action.="</div></div>";
	
	
	$nestedData[] = $action;	
	$nestedData[] = "<div class='text-center'>".$i."</div>";
	$nestedData[] = "<div class='text-center'>".$row["name"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["category_name"]."</div>";
	$nestedData[] = "<div class='text-center'>".date('d-m-Y', strtotime($row['date']))."</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'>".$row['payment_type_name']."</div>";
	$nestedData[] = "<div class='text-center'>".$row['reference']."</div>";
	$nestedData[] = "<div class='text-center'>".$row['notes']."</div>";
	$nestedData[] = "<div class='text-center'><span class='btn btn-info btn-xs'>".$row["created_user"]."</span></div>";
	$nestedData[] = "<div class='text-center'>".$row["updated_user"]."</div>";
	$nestedData[] = "<div class='text-center'>".date('d-m-Y h:i A', strtotime($row['created_at']))."</div>";
	$nestedData[] = "<div class='text-center'>".date('d-m-Y h:i A', strtotime($row['updated_at']))."</div>";
	
		
	$data[] = $nestedData;
	$i++;
	

}






$nestedData=array(); 
$nestedData[] =  "";
$nestedData[] =  "";
$nestedData[] =  "";
$nestedData[] =  "";
$nestedData[] =  "";
$nestedData[] =  "<div class='text-center'><span class='btn btn-success btn-sm amount_span'><i class='fa fa-inr'></i> ".number_format($total_expenses_amount,2)."</span></div>";
$nestedData[] =  "";
$nestedData[] =  "";
$nestedData[] =  "";
$nestedData[] =  "";
$nestedData[] =  "";
$nestedData[] =  "";
$nestedData[] =  "";
$data[] = $nestedData;







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

echo json_encode($json_data); 




?>