<?php 
ob_start();
session_start();
include "../config/config.php";
include "../config/session.php";

$requestData = $_REQUEST;

if($requestData['length']==-1){
    $requestData['length'] = 100000;
}




/*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="";

$from_location_id = $_POST['from_location_id'];
$to_location_id = $_POST['to_location_id'];
$created_user = $_POST['created_user'];
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];

if($from_location_id!=""){
    $conditions.=" and st.from_location=".$from_location_id;
}
if($to_location_id!=""){
    $conditions.=" and st.to_location=".$to_location_id;
}
if($created_user!=""){
    $conditions.=" and st.created_by=".$created_user;
}

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



$sql_query = "SELECT st.*,
                fb.business_name as from_business_name,
                tb.business_name as to_business_name,
                cu.name as created_user, uu.name as updated_user
                FROM `tbl_stock_transfer` st
                LEFT JOIN tbl_business_branch fb ON fb.id=st.from_location
                LEFT JOIN tbl_business_branch tb ON tb.id=st.to_location
                LEFT JOIN tbl_users cu ON cu.id=st.created_by
                LEFT JOIN tbl_users uu ON uu.id=st.updated_by";



    
/*Fetch All Data Start*/        
        
    	$statement = $pdo->prepare("$sql_query
    	WHERE st.id!=0 ".$conditions." ORDER BY st.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 st.id!=0  ".$conditions;
         
        if( !empty($_REQUEST['search'])){   
        	$sql.=" AND ( fb.business_name LIKE '%".$_REQUEST['search']."%' ";    
        	$sql.=" OR tb.business_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='modal/sales-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>";
		                
		                
		 if($row["status"]==2){
	 	               $action.="<a style='padding: 6px;margin-right: 3px;' href='pos-edit.php?id=".$row["id"]."' title='Edit Sales' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-edit'></i>
		                </a> ";
		 }
		                
		                
		                
		if($row["status"]==1){                
                $action.="<a style='padding: 6px;margin-right: 3px;' href='modal/payment-add-modal.php?id=".$row["id"]."' data-toggle='ajax-modal' title='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='modal/sales-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='sales-view.php?id=".$row["id"]."' title='Print Sales' class='tip btn btn-info btn-xs'>
		                    <i class='fa fa-print'></i>
		                </a> ";
		                
		                
		                $action.=" <button style='padding: 6px; margin-right: 3px;' title='Delete Stock Transfer' 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' style='font-size: 12px; font-weight: 700; font-family: system-ui;'><i class='fa fa-check'></i> Sales</span>";
	}else if($row["status"]==2){
	    $sales_status = "<span class='btn btn-primary btn-xs' style='font-size: 12px; font-weight: 700; font-family: system-ui;'>Quotation</span>";
	}
	
	$balance = $row['grand_total']-($row['paid']+$row['discount_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["from_business_name"]."</div>";
	
	$nestedData[] = "<div class='text-center'>".$row["to_business_name"]."</div>";
	
	$nestedData[] = "<div class='text-center'>
	                    <span class='btn btn-primary btn-xs' style='font-size: 12px; font-weight: 700; font-family: system-ui;'><i class='fa fa-inr'></i> ".number_format($row['grand_total'],2)."</span>
	                 </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); 




?>