<?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 =>'id',
            3 =>'id',
            4 =>'id',
            5 =>'id',
            6 =>'id',
            7 =>'id',
            8 =>'id',
            9 =>'id'
        );
        
$conditions="";

$supplier_id = $_POST['supplier_id'];
$created_user = $_POST['created_user'];
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];



        
/*Fetch All Data Start*/        
        
    	$statement = $pdo->prepare("SELECT p.*,
    	(SELECT SUM(tbl_payments.amount) FROM tbl_payments WHERE tbl_payments.purchase_id=p.id and accounts=2) as total_paid_amount,
    	tbl_suppliers.name as supplier_name, cu.name as created_user
FROM `tbl_purchases` p 
LEFT JOIN tbl_suppliers ON tbl_suppliers.id=p.supplier_id
LEFT JOIN tbl_users cu ON cu.id=p.created_by
    	WHERE p.id!=0 ".$conditions." ORDER BY p.id DESC");
		$statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalData = $statement->rowCount();
        $totalFiltered = $totalData;
/*Fetch All Data End*/       
        
/*Fetch Filter Data Start*/    
         $sql = "SELECT p.*,
         (SELECT SUM(tbl_payments.amount) FROM tbl_payments  WHERE tbl_payments.purchase_id=p.id and accounts=2) as total_paid_amount,
         tbl_suppliers.name as supplier_name, cu.name as created_user
FROM `tbl_purchases` p
LEFT JOIN tbl_suppliers ON tbl_suppliers.id=p.supplier_id
LEFT JOIN tbl_users cu ON cu.id=p.created_by
    	WHERE p.id!=0  ".$conditions;
         
        if( !empty($_REQUEST['search']) ) 
        {   
        	$sql.=" AND ( tbl_suppliers.name LIKE '%".$_REQUEST['search']."%' ";    
        	$sql.=" OR p.invoice_no 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='modal/purchase-view-modal.php?id=".$row["id"]."' data-toggle='ajax-modal' title='View Details' class='tip btn btn-primary btn-xs'>
		                    <i class='fa fa-eye'></i>
		                </a>";
		                
	 	               $action.="<a style='padding: 6px;margin-right: 3px;' href='purchases-edit.php?id=".$row["id"]."' title='Edit Purchases' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-edit'></i>
		                </a> ";
		                
		                $action.="<a style='padding: 6px;margin-right: 3px;' href='modal/purchase-list-modal.php?id=".$row["id"]."' id='".$row["id"]."' data-toggle='ajax-modal' title='Payment List' class='tip btn btn-info btn-xs'>
		                    <i class='fa fa-list' style=''></i>
		                </a> ";
		                
		                
		                
		                $action.="<a style='padding: 6px;margin-right: 3px;' href='modal/purchase-add-modal.php?id=".$row["id"]."' data-toggle='ajax-modal' title='Add Payment' class='tip btn btn-success btn-xs'>
		                    <i class='fa fa-inr' style='width: 14px;font-size: 12px;'></i>
		                </a> ";
		                
		                $action.="<a style='padding: 6px;margin-right: 3px;' href='purchases-view.php?id=".$row["id"]."' title='Print Purchases' class='tip btn btn-info btn-xs'>
		                    <i class='fa fa-print'></i>
		                </a> ";
		                
		                $action.="<a style='padding: 6px;margin-right: 3px;' href='purchases-edit.php?id=".$row["id"]."' title='Edit Purchases' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-edit'></i>
		                </a> ";
		                
		                
		                $action.=" <button style='padding: 6px;margin-right: 3px;' title='Delete Purchases' onClick='delete_Details(".$row["id"].")' class='tip btn btn-danger btn-xs'>
		                    <i class='fa fa-trash-o'></i>
		                </button>";
		                
	$action.="</div></div>";
	
	if($row["status"]==1){
	    $sales_status = "<span class='btn btn-warning btn-xs'>Purchased</span>";
	}else if($row["status"]==2){
	    $sales_status = "<span class='btn btn-primary btn-xs'>Quotation</span>";
	}
	
	$balance = $row['grand_total']-$row['total_paid_amount'];
	

	
	//$nestedData[] = $action;
	$nestedData[] = "<div class='text-center'>".$i."</div>";
	$nestedData[] = "<div class='text-center'>".date('d-m-Y', strtotime($row["date"]))."</div>";
	$nestedData[] = "<div class='text-center'>".$row["supplier_name"]."</div>";
	$nestedData[] = "<div class='text-center'><span class='btn btn-primary btn-xs amount_span'><i Class='fa fa-inr'></i> ".number_format($row['total_amount'],2)."</span></div>";
    $nestedData[] = "<div class='text-center'><b>".$row["gst_percentage"]." %</b></div>";
    
    
	$nestedData[] = "<div class='text-center'><span class='btn btn-primary btn-xs amount_span'><i Class='fa fa-inr'></i> ".number_format($row['grand_total'],2)."</span></div>";
	
	$nestedData[] = "<div class='text-center'><span class='btn btn-success btn-xs amount_span'><i Class='fa fa-inr'></i> ".number_format($row['total_paid_amount'],2)."</span></div>";
	
	$nestedData[] = "<div class='text-center'><span class='btn btn-danger btn-xs amount_span'><i Class='fa fa-inr'></i> ".number_format($balance,2)."</span></div>";
	
	
	
	
	$nestedData[] = "<div class='text-center'>".$sales_status."</div>";
	$nestedData[] = "<div class='text-center'>".$row["created_user"]."</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); 




?>