<?php
include("../inc/config.php");



if(!empty($_POST['client-id']) && !empty($_POST['awb-number'])){
    $clientId=$_REQUEST['client-id'];
    $awb_no=$_REQUEST['awb-number'];
/*Fetch All Data Start*/     
        $sqls="SELECT pickup_entry.*,tbl_user.full_name as client_name FROM `pickup_entry` INNER JOIN tbl_user ON pickup_entry.client_id=tbl_user.id WHERE pickup_entry.status_code!=0 ";
        
        if(!empty($_POST['status'])){
            $sqls.=" and pickup_entry.status_code=".$_POST['status'];
        }
        $sqls.=" and pickup_entry.client_id=".$clientId." and pickup_entry.cscs_awb=".$awb_no." ORDER BY pickup_entry.id DESC";
    	$statement = $pdo->prepare($sqls);
		$statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalData = $statement->rowCount();
        $totalFiltered = $totalData;
/*Fetch All Data End*/       
        
/*Fetch Filter Data Start*/    
        $sql="SELECT pickup_entry.*,tbl_user.full_name as client_name FROM `pickup_entry` INNER JOIN tbl_user ON pickup_entry.client_id=tbl_user.id WHERE 1=1 ";
        if(!empty($_POST['status'])){
            $sql.=" and pickup_entry.status_code=".$_POST['status'];
        }
    	$sql.=" and pickup_entry.client_id=".$clientId." and pickup_entry.cscs_awb=".$awb_no." ORDER BY pickup_entry.id DESC";
    	$statement = $pdo->prepare($sql);
        $statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
        $totalFiltered = $statement->rowCount();
        
/*Fetch Filter Data End*/       
        $statement = $pdo->prepare($sql);
        $statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);

        $data = array();
        $i=0;
    foreach( $query as $row ) {  // preparing an array
        $get_sql="SELECT company_name FROM courier_company WHERE id=?";
        $getData = $pdo->prepare($get_sql);
        $getData->execute(array($row["courier_id"]));
		$query_getData = $getData->fetchAll(PDO::FETCH_ASSOC);
		foreach( $query_getData as $gD ) {
		   $company_name= $gD["company_name"];
		}
		$nestedData=array();
		if(empty($company_name)){ $company_name="";}
	$nestedData['awb-number'] = $row["cscs_awb"];
	$nestedData['ref-no'] = $row["ref_no"];
	$nestedData['order-type'] = $row["order_type"];
	$nestedData['order-type'] = $row["order_type"];
	$nestedData['shipper-address'] = $row["shipper_address"];
	$nestedData['shipper-city'] = $row["shipper_city"];
	$nestedData['shipper-state'] = $row["shipper_state"];
	$nestedData['shipper-phone'] = $row["shipper_phone"];
	$nestedData['shipper-pincode'] = $row["shipper_pincode"];
	$nestedData['consignee-name'] = $row["consignee_name"];
	$nestedData['consignee-address'] = $row["consignee_address"];
	$nestedData['consignee-city'] = $row["consignee_city"];
	$nestedData['consignee-state'] = $row["consignee_state"];
	$nestedData['consignee-phone'] = $row["consignee_phone"];
	$nestedData['consignee-pincode'] = $row["consignee_pincode"];
	$nestedData['v-weight'] = $row["v_weight"];
	$nestedData['vw-length'] = $row["vw_length"];
	$nestedData['vw-breadth'] = $row["vw_breadth"];
	$nestedData['vw-height'] = $row['vw_height']; 
	$nestedData['collectable-amount'] = $row['collectable_amount']; 
	$nestedData['product-description'] = $row['product_description']; 
	$nestedData['quantity'] = $row['quantity']; 
	$nestedData['boxes'] = $row['boxes']; 
	$nestedData['invoice-value'] = $row['invoice_value']; 
	$nestedData['taxable-value'] = $row['taxable_value']; 
	$nestedData['hsn-code'] = $row['hsn_code']; 
	$nestedData['discount'] = $row['discount']; 
	$nestedData['gst-amount'] = $row['gst_amount']; 
	$nestedData['mrp'] = $row['mrp']; 
	$nestedData['ewaybill'] = $row['ewaybill']; 
	$nestedData['a-weight'] = $row['a_weight']; 
	$nestedData['c-weight'] = $row['c_weight']; 
    if($row['gst_rate']=='gr_igst'){ $nestedData['gst-rate'] ="IGST";}elseif($row['gst_rate']=='gr_cgst'){ $nestedData['gst-rate'] ="CGST";} 
	$nestedData['buyer-gst'] = $row['buyer_gst']; 
	$nestedData['seller-gst'] = $row['seller_gst']; 
	$nestedData['rto-name'] = $row['rto_name']; 
	$nestedData['rto-address'] = $row['rto_address']; 
	$nestedData['rto-city'] = $row['rto_city']; 
	$nestedData['rto-state'] = $row['rto_state']; 
	$nestedData['rto-phone'] = $row['rto_phone']; 
	$nestedData['rto-pincode'] = $row['rto_pincode']; 
	$day=date_create($row['date_added']);$created=date_format($day,'d-m-Y');$nestedData['date'] =$created;
	$data[$i] = $nestedData;
	
	
$sql="SELECT * FROM `tbl_tracking` where pickup_entry_id=".$row['id']." ORDER BY t_id  DESC";	
$statement = $pdo->prepare($sql);
$statement->execute();
$tracking_data = $statement->fetchAll(PDO::FETCH_ASSOC);
$tracking_datas=array();
foreach($tracking_data as $res) {
    $tracking_column=array();
    $tracking_column['pickup-entry-status-update-location']=$res['location'];
    $tracking_column['status-code']=$res['status_code'];
    $tracking_column['status']=$res['status'];
    $tracking_column['description']=$res['description'];
    $tracking_column['receiver-name']=$res['receiver_name'];
    $tracking_column['relation']=$res['relation'];
    $tracking_column['uu-delivery-reason']=$res['reason'];
    $tracking_column['delivery-date']=$res['delivery_date'];
    $tracking_column['status-date']=$res['created_at'];
    $tracking_datas[]=$tracking_column;
}
    $data[$i]['tracking_details']=$tracking_datas;
	$i++;
}

if($totalData>0){
            $response['error'] = false;
            $response['message'] = "Pickup Entry retrieved successfully";
            $response['data'] = $data;
}else{
            $response['error'] = false;
            $response['message'] = "No Data Found!!!";
}


        
}else{
            $response['error'] = true;
            $response['message'] = "Client Id and Awb number is Required!!!";
            $response['data'] = $data;
  
}
print_r(json_encode($response));
?>