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

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

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



        
/*Fetch All Data Start*/        
$sql_query = "SELECT rv.*,
c.customer_name, c.mobile_no,
p.name as products_name, 
sm.name as sales_man_name,
cu.name as created_user, uu.name as updated_user
FROM `tbl_repair_voucher` rv 
LEFT JOIN tbl_customers c ON c.id=rv.customer_id
LEFT JOIN tbl_ids_products p ON p.id=rv.product_ids
LEFT JOIN tbl_users sm ON sm.id=rv.sales_man
LEFT JOIN tbl_users cu ON cu.id=rv.created_by
LEFT JOIN tbl_users uu ON uu.id=rv.updated_by";
        
    	$statement = $pdo->prepare("$sql_query
    	WHERE rv.id!=0 ".$conditions." ORDER BY rv.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 rv.id!=0 ".$conditions;
         
        if( !empty($_REQUEST['search']) ) 
        {   
        	$sql.=" AND ( c.customer_name LIKE '%".$_REQUEST['search']."%' ";  
        	$sql.=" OR rv.bill_no LIKE '%".$_REQUEST['search']."%' ";
        	$sql.=" OR c.mobile_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'>";
	                    
	                    $repair_completed_date = $row['repair_completed_date'];
	                    $customer = base64_encode('customer');
	                    $company = base64_encode('company');
	                    $close = base64_encode('close');
	 
	                    $action.=" <a style='padding: 6px;margin-right: 3px;' href='modal/advance-voucher-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='repair-voucher.php?id=".$row["id"]."' title='Edit Repair Voucher' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-edit'></i>
		                </a> ";
		                
		                
		                $action.="<a style='padding: 6px;margin-right: 3px;' target='_blank' href='repair-voucher-print.php?id=".$row["id"]."&custom=".$customer."' title='Print Repair Customer Voucher' class='tip btn btn-info btn-xs'>
		                    <i class='fa fa-print'></i>
		                </a> ";
		                
		                
		                 $action.="<a style='padding: 6px;margin-right: 3px;' target='_blank' href='repair-voucher-print.php?id=".$row["id"]."&custom=".$company."' title='Print Repair Company Voucher' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-print'></i>
		                </a> ";
		                if($repair_completed_date != '0000-00-00' && !empty($repair_completed_date)){
    		                 $action.="<a style='padding: 6px;margin-right: 3px;' target='_blank' href='repair-voucher-print.php?id=".$row["id"]."&custom=".$close."' title='Print Repair Close Voucher' class='tip btn btn-danger btn-xs'>
    		                    <i class='fa fa-print'></i>
    		                </a> ";
		                }
		 
		                
		                
		       $action.=" <button style='padding: 6px;margin-right: 3px;' title='Delete Repair Voucher' 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['type']=="1"){
	    $product_type = "<span class='btn btn-info btn-xs'>Repair</span>";
	}else if($row['type']=="2"){
	    $product_type = "<span class='btn btn-warning btn-xs'>Soliding</span>";
	}else if($row['type']=="3"){
	    $product_type = "<span class='btn btn-primary btn-xs'>Others</span>";
	}
	
	if(!empty($row["scheme_months"]) && !empty($row["payment_paid_month_counts"])){
	    $payment_pending_months = $row["scheme_months"]-$row["payment_paid_month_counts"];
	}
	$nestedData[] = $action;	
	$nestedData[] = "<div class='text-center'>".$i."</div>";
	$nestedData[] = "<div class='text-center'>
	                    <b>Customer Name: ".$row["customer_name"]."</b></br>
	                    <b>Mobile No: ".$row["mobile_no"]."</b>
	                 </div>";
	$nestedData[] = "<div class='text-center'>".date('d-m-Y', strtotime($row['date']))."</div>";
	$nestedData[] = "<div class='text-center'>".$row["products_name"]."</div>";
	$nestedData[] = "<div class='text-center'>".$product_type."</div>";
	$nestedData[] = "<div class='text-center'>".$row['bill_no']."</div>";
	$nestedData[] = "<div class='text-center'>".$row['weight']."</div>";
	$nestedData[] = "<div class='text-center'><span class='btn btn-primary btn-xs'>".$row["sales_man_name"]."</span></div>";
	$nestedData[] = "<div class='text-center'><span class='btn btn-primary btn-xs'>".$row["created_user"]."</span></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); 




?>