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

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

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

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




$sql_query = "SELECT ad.*, c.customer_name, c.mobile_no,
p.hsn_code, pro.name as products_name, 
cu.name as created_user, uu.name as updated_user
FROM `tbl_advance_voucher` ad 
LEFT JOIN tbl_customers c ON c.id=ad.customer_id
LEFT JOIN tbl_product_variants p ON p.id=ad.product_variant_id
LEFT JOIN tbl_products_name pro ON pro.id=p.product_name
LEFT JOIN tbl_users cu ON cu.id=ad.created_by
LEFT JOIN tbl_users uu ON uu.id=ad.updated_by";

        
/*Fetch All Data Start*/        
        $statement = $pdo->prepare("$sql_query WHERE ad.id!=0 ".$conditions." ORDER BY ad.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 ad.id!=0 ".$conditions;
         
        if( !empty($_REQUEST['search'])){   
        	$sql.=" AND ( c.customer_name LIKE '%".$_REQUEST['search']."%' ";  
        // 	$sql.=" OR ad.receipt_id LIKE '%".$_REQUEST['search']."%' ";
        	$sql.=" OR ad.advance_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'>";
	 
	 
	                    $action.=" <a style='padding: 6px;margin-right: 3px;margin-top:1px;' 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;margin-top:1px;' href='advance-voucher-edit.php?id=".$row["id"]."' title='Edit Advance Voucher' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-edit'></i>
		                </a> ";
		                
		                
		                $action.="<a style='padding: 6px;margin-right: 3px;margin-top:1px;' href='modal/advance-payment-add-modal.php?id=".$row["id"]."' data-toggle='ajax-modal' title='Advance 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;margin-top:1px;' target='_blank' href='advance-voucher-print.php?id=".$row["id"]."' title='Print Advance Voucher' class='tip btn btn-info btn-xs'>
		                    <i class='fa fa-print'></i>
		                </a> ";
		                
		 
		                
		                
		       $action.=" <button style='padding: 6px;margin-right: 3px;margin-top:1px;' title='Delete Advance Voucher' onClick='delete_Details(".$row["id"].")' class='tip btn btn-danger btn-xs'>
		                    <i class='fa fa-trash-o'></i>
		                </button>";
		                
		                $action.="<a style='padding: 6px;margin-right: 3px;margin-top:1px;' target='_blank' href='advance-estimate-print.php?id=".$row["id"]."' title='Print Advance Estimate Bill' class='tip btn btn-warning btn-xs'>
		                    <i class='fa-solid fa-print'></i>
		                </a> ";
	           
		                

		                
	$action.="</div></div>";
	
	if($row['type']=="1"){
	    $product_type = "<span class='btn btn-info btn-xs'>Orders</span>";
	}else if($row['type']=="2"){
	    $product_type = "<span class='btn btn-warning btn-xs'>Stock</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'>".$row["customer_name"]." - ".$row["mobile_no"]."</div>";
	$nestedData[] = "<div class='text-center'>".date('d-m-Y', strtotime($row['date']))."</div>";
	$nestedData[] = "<div class='text-center'>".date('d-m-Y', strtotime($row['expected_date']))."</div>";
	$nestedData[] = "<div class='text-center'>".$product_type."</div>";
	$nestedData[] = "<div class='text-center'>".$row['advance_no']."</div>";
	$nestedData[] = "<div class='text-center'>".$row['advance_weight']."</div>";
	$nestedData[] = "<div class='text-center'>".number_format($row['per_gram_amount'],2)."</div>";
	$nestedData[] = "<div class='text-center'>".number_format($row['advance_amount'],2)."</div>";
	$nestedData[] = "<div class='text-center'>".number_format($row['total_amount'],2)."</div>";
	$nestedData[] = "<div class='text-center'><span class='btn btn-primary btn-xs'><i class='fa fa-user' style='font-size: 10px;'></i> ".$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); 




?>