<?php 
ob_start();
session_start();
include("../inc/config.php");
include("../inc/session.php");
$allPermissions = $_SESSION['permission'];
?>
<?php


function xss_clean($data)
    {
        $data = trim($data);
        // Fix &entity\n;
        $data = str_replace(array('&amp;', '&lt;', '&gt;'), array('&amp;amp;', '&amp;lt;', '&amp;gt;'), $data);
        $data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data);
        $data = preg_replace('/(&#x*[0-9A-F]+);*/iu', '$1;', $data);
        $data = html_entity_decode($data, ENT_COMPAT, 'UTF-8');

        // Remove any attribute starting with "on" or xmlns
        $data = preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data);

        // Remove javascript: and vbscript: protocols
        $data = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2nojavascript...', $data);
        $data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2novbscript...', $data);
        $data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', '$1=$2nomozbinding...', $data);

        // Only works in IE: <span style="width: expression(alert('Ping!'));"></span>
        $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
        $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
        $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu', '$1>', $data);

        // Remove namespaced elements (we do not need them)
        $data = preg_replace('#</*\w+:\w[^>]*+>#i', '', $data);

        do {
            // Remove really unwanted tags
            $old_data = $data;
            $data = preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', '', $data);
        } while ($old_data !== $data);

        // we are done...
        return $data;
    }


$requestData= $_REQUEST;

$condition = '';

    $from_date=$_POST['from_date'] ?? date('Y-m-d');
    $to_date=$_POST['to_date'] ?? date('Y-m-d');
    $new_client = $_POST['new_client'] ?? 0;
    $branch_id=$_POST['branch_id'] ?? 0;
    $client_id=$_POST['client_id'] ?? 0;
    $courier_id=$_POST['courier_id'] ?? 0;
    $customer_id=$_POST['customer_id'] ?? 0;
    $status=$_POST['status'] ?? 0;
    

    $condition.=" and pickup_entry.date_added BETWEEN '$from_date' AND '$to_date'  ";
    if($branch_id!='all' && $branch_id!=''){
        $condition.=" and pickup_entry.branch_id='".$branch_id."'";
    }
    
    if($new_client!='all' && $new_client!=''){
        $condition.=" and pickup_entry.new_client_id='".$new_client."'";
    }
    if($client_id!='all' && $client_id!=''){
        $condition.=" and pickup_entry.client_id='".$client_id."'";
    }
    if($courier_id!='all' && $courier_id!=''){
        $condition.=" and pickup_entry.courier_id='".$courier_id."'";
    }
    if($customer_id!='all' && $customer_id!=''){
        $condition.=" and pickup_entry.customer_id='".$customer_id."'";
    }
    if($status!='all'){
        $condition.=" and pickup_entry.status_code='".$status."'";
    }
    
    if($session_client_ids==0){
        $condition.="";
    }else{
        $condition.=" and tbl_client_new.id IN ($session_client_ids)";
    }
    
     if($session['id']!=1 && $session_branch_ids!='0'){
        $condition.= " and tbl_branch.id IN ($session_branch_ids)";
    }
    

/*Fetch Database column Details*/
        $columns = array( 
            0 =>'id', 
            1 =>'client_id',
            2 =>'courier_id',
            3 =>'cscs_awb',
            4 =>'3pl_awb',
            5 =>'ref_no',
            6 =>'status',
            7 =>'status_code',
            8 =>'order_type',
            9 =>'collectable_amount',
            10 =>'business_mode',
            11 =>'business_type',
            12 =>'shipper_name',
            13 =>'shipper_address',
            14 =>'shipper_city',
            15 =>'shipper_state',
            16 =>'shipper_pincode',
            17 =>'shipper_phone',
            18 =>'consignee_name',
            19 =>'consignee_address',
            20 =>'consignee_city',
            21 =>'consignee_state',
            22 =>'consignee_pincode',
            23 =>'consignee_phone',
            24 =>'product_description',
            25 =>'quantity',
            26 =>'boxes',
            27 =>'invoice_value',
            28 =>'taxable_value',
            29 =>'hsn_code',
            30 =>'discount',
            31 =>'gst_amount',
            32 =>'mrp',
            33 =>'ewaybill',
            34 =>'rto_name',
            35 =>'rto_address',
            36 =>'rto_city',
            37 =>'rto_state',
            38 =>'rto_pincode',
            39 =>'rto_phone',
            40 =>'a_weight',
            41 =>'vw_length',
            42 =>'vw_breadth',
            43 =>'vw_height',
            44 =>'v_weight',
            45 =>'c_weight',
            46 =>'gst_rate',
            47 =>'buyer_gst',
            48 =>'seller_gst',
            49 =>'manifest_id',
            50 =>'delete_status',
            51 =>'date_added',
            52 =>'updated_at',
            53 =>'client_name'
        );
/*Fetch Database column Details*/        
        
        
    $statement = $pdo->prepare("SELECT pickup_entry.*,tbl_client_new.name as new_client_name,tbl_user.full_name as client_name,tbl_status.name as status_name FROM `pickup_entry` 
    left JOIN tbl_user ON pickup_entry.client_id=tbl_user.id 
    left join tbl_branch on pickup_entry.branch_id=tbl_branch.id 
    left JOIN tbl_status ON tbl_status.id=pickup_entry.status_code 
    left JOIN tbl_client_new ON tbl_client_new.id=pickup_entry.new_client_id
    WHERE 1=1  $condition  ORDER BY pickup_entry.id DESC");    
    $statement->execute();
	$query = $statement->fetchAll(PDO::FETCH_ASSOC);
	$totalData = $statement->rowCount();
    $totalFiltered = $totalData;   

/*Fetch Filter Data Start*/    
        $sql="SELECT pickup_entry.*,tbl_client_new.name as new_client_name,tbl_user.full_name as client_name,tbl_branch.name as branch_name,courier_company.company_name as courier_name,tbl_status.name as status_name FROM `pickup_entry`
        left JOIN tbl_user ON pickup_entry.client_id=tbl_user.id 
        left join tbl_branch on pickup_entry.branch_id=tbl_branch.id 
        left join courier_company on pickup_entry.courier_id=courier_company.id 
        left JOIN tbl_status ON tbl_status.id=pickup_entry.status_code  
        left JOIN tbl_client_new ON tbl_client_new.id=pickup_entry.new_client_id WHERE 1=1 $condition";
        if( !empty($requestData['search']['value']) ) 
        {   
        	$sql.=" AND ( pickup_entry.cscs_awb LIKE '%".$requestData['search']['value']."%' ";    
        	$sql.=" OR pickup_entry.3pl_awb LIKE '%".$requestData['search']['value']."%' ";
        	$sql.=" OR pickup_entry.ref_no LIKE '%".$requestData['search']['value']."%' ";
        	$sql.=" OR pickup_entry.status LIKE '%".$requestData['search']['value']."%' ";
        	$sql.=" OR pickup_entry.shipper_pincode LIKE '%".$requestData['search']['value']."%' ";
        	$sql.=" OR tbl_user.full_name LIKE '%".$requestData['search']['value']."%' )";
        }
    
        $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);
/*Fetch Filter Data End*/


$data = array();
$i=$requestData['start']+1;
foreach($query as $row ) {  
	$nestedData=array(); 
 	$action="";
if($session_id==1 || $allPermissions['transaction_booking']['update']=="1"){ 
 	$action="<a href='shipment-edit.php?id=".$row['id']."' class='btn btn-primary btn-xs'><i class='fa fa-edit'></i> Edit</a>&nbsp;";
}
    /*$action .= "<a href='awb_number_print.php?list_id=".$row['id']."' class='btn btn-info btn-xs'><i class='fa fa-print'></i> Print Docket</a>"; 
	$check_box = $i.". <input type='checkbox' class='checkbox' name='checkboxlist' style='margin:2px 5px;' value=".$row['id']." >";
	
	$_details='';
	    $_details.=$check_box;
	    $_details.="<span class='label_title'>Branch : </span><span style='font-weight:bold;'>".$row["branch_name"]."</span>";
        $_details.="<br><span class='label_title'>Client Name : </span><span style='font-weight:bold;'>".$row["new_client_name"]."</span>";
        $_details.="<br><span class='label_title'>AWB NO : </span><span style='font-weight:bold;'>".$row["cscs_awb"]."</span>";
       
        $_details.='<br>'.$action;
        
    $ship_address='';
        $ship_address.="<i class='fa fa-address-book'></i> <span style='font-weight:bold;'>".xss_clean($row["shipper_name"])."</span>";
        $ship_address.="<br><i class='fa fa-map-marker'></i> <span style='font-weight:bolsd;'>".xss_clean($row["shipper_address"])."</span>";
        $ship_address.="<br><i class='fa fa-map-pin'></i> <span style='font-weight:bolsd;'>".xss_clean($row["shipper_city"]).'-'.xss_clean($row["shipper_pincode"]).','.xss_clean($row["shipper_state"])."</span>";
        $ship_address.="<br><i class='fa fa-mobile'></i> <span style='font-weight:bolsd;'>".xss_clean($row["shipper_phone"])."</span>";      
        
        
        
        
    $day=date_create($row['date_added']);$created=date_format($day,'d-m-Y');
    $ship_info='';
        $ship_info.="<span class='label_title'>No of Box : </span><span style='font-weight:bold;'>".$row["boxes"]."</span>";
        $ship_info.="<br><span class='label_title'>Invoice Value : </span><span style='font-weight:bolsd;'>".xss_clean($row["invoice_value"])."</span>";
        $ship_info.="<br><span class='label_title'>Ewaybill : </span><span style='font-weight:bolsd;'>".xss_clean($row["ewaybill"])."</span>";
        $ship_info.="<br><span class='label_title'>A.Wt / C.Wt : </span><span style='font-weight:bolsd;'>".$row["a_weight"].' / '.$row["a_weight"]."</span>";
        $ship_info.="<br><span class='label_title'>Date : </span><span style='font-weight:bold;'>".$created."</span>";
        $ship_info.="<br><span class='label_title'>Curruent Status : </span><span class='btn btn-warning btn-xs' style='font-weight:bold;'>".strtoupper($row["status_name"])."</span>";
        
    $rec_address='';
        $rec_address.="<i class='fa fa-address-book'></i> <span style='font-weight:bold;'>".xss_clean($row["consignee_name"])."</span>";
        $rec_address.="<br><i class='fa fa-map-marker'></i> <span style='font-weight:bolsd;'>".xss_clean($row["consignee_address"])."</span>";
        $rec_address.="<br><i class='fa fa-map-pin'></i> <span style='font-weight:bolsd;'>".xss_clean($row["consignee_city"]).'-'.xss_clean($row["consignee_pincode"]).','.xss_clean($row["consignee_state"])."</span>";
        $rec_address.="<br><i class='fa fa-mobile'></i> <span style='font-weight:bolsd;'>".xss_clean($row["consignee_phone"])."</span>";  
        
        
	$rto_address='';
        $rto_address.="<i class='fa fa-address-book'></i> <span style='font-weight:bold;'>".$row["rto_name"]."</span>";
        $rto_address.="<br><i class='fa fa-map-marker'></i> <span style='font-weight:bolsd;'>".$row["rto_address"]."</span>";
        $rto_address.="<br><i class='fa fa-map-pin'></i> <span style='font-weight:bolsd;'>".$row["rto_city"].'-'.$row["rto_pincode"].','.$row["rto_state"]."</span>";
        $rto_address.="<br><i class='fa fa-mobile'></i> <span style='font-weight:bolsd;'>".$row["rto_phone"]."</span>";
    
    $pickup_address='';
        $pickup_address.="<i class='fa fa-address-book'></i> <span style='font-weight:bold;'>".$row["pickup_name"]."</span>";
        $pickup_address.="<br><i class='fa fa-map-marker'></i> <span style='font-weight:bolsd;'>".$row["pickup_address"]."</span>";
        $pickup_address.="<br><i class='fa fa-map-pin'></i> <span style='font-weight:bolsd;'>".$row["pickup_city"].'-'.$row["pickup_pincode"].','.$row["pickup_state"]."</span>";
        $pickup_address.="<br><i class='fa fa-mobile'></i> <span style='font-weight:bolsd;'>".$row["pickup_phone"]."</span>";
        
        
    $nestedData[] = $_details;
    $nestedData[] = $ship_info;
    $nestedData[] = $ship_address;
    $nestedData[] = $rec_address;
    $nestedData[] = $rto_address;*/
    
    $nestedData[] = $i;
    $day=date_create($row['date_added']);$created=date_format($day,'d M Y');
    $nestedData[] = ($created);
    $nestedData[] = xss_clean($row["cscs_awb"]);
    $nestedData[] = xss_clean($row["branch_name"]);
    $nestedData[] = xss_clean($row["new_client_name"]);
    
    $nestedData[] = xss_clean($row["shipper_name"]);
    $nestedData[] = xss_clean($row["shipper_location"]);
    $nestedData[] = xss_clean($row["shipper_pincode"]);
	$nestedData[] = xss_clean($row["shipper_city"]);
    $nestedData[] = xss_clean($row["shipper_state"]);
	$nestedData[] = xss_clean($row["shipper_phone"]);
	
	$nestedData[] = xss_clean($row["consignee_name"]);
	$nestedData[] = xss_clean($row["consignee_location"]);
	$nestedData[] = xss_clean($row["consignee_pincode"]);
	$nestedData[] = xss_clean($row["consignee_city"]);
    $nestedData[] = xss_clean($row["consignee_state"]);
	$nestedData[] = xss_clean($row["consignee_phone"]);


	$nestedData[] = xss_clean($row["boxes"]);
	$nestedData[] = xss_clean($row["a_weight"]);
	$nestedData[] = xss_clean($row["invoice_value"]);
	$nestedData[] = xss_clean($row["invoice_no"]);

	$nestedData[] = ucwords(xss_clean($row["business_mode"]));
	$nestedData[] = xss_clean($row["status_name"]);
	/*$nestedData[] = xss_clean($row["pod_status"]);*/

	
	
	$data[] = $nestedData;
	$i++;
}


	

$json_data = array(
			"draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
			"recordsTotal"    => intval( $totalData ),  // total number of records
			"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
			"data"            => $data   // total data array
			);

echo json_encode($json_data);  // send data as json format

?>
        