<?php
include("../inc/config.php");
$list=array();
$awb_number=$_POST['awb_number'];
$list=explode(",",$awb_number);

            	
	$now = gmdate("YmdHis");
		header('Content-Type: text/csv; charset=utf-8');  
		header('Content-Disposition: attachment; filename=Tracking-list-'.$now.'.csv');  
		$output = fopen("php://output", "w");  
		fputcsv($output, array('SL', 'AWB Number', 'Status', 'Location', 'Date', 'Client Name', 'Shipper Name', 'Shipper Address','Shipper City','Shipper State','Shipper Pincode','Shipper Phone','Consignee Name','Consignee Address','Consignee City','Consignee State','Consignee Pincode','Consignee Phone','Entry Created Date','Charged Weight'));  
        $i=1;
        
        
		foreach ($list as $awb)
		{
        	$statement = $pdo->prepare("SELECT pickup_entry.*,tbl_user.full_name as client_name,(SELECT location from tbl_tracking WHERE pickup_entry_id=pickup_entry.id ORDER by pickup_entry_id DESC LIMIT 1) tracking_location,(SELECT created_at from tbl_tracking WHERE pickup_entry_id=pickup_entry.id ORDER by pickup_entry_id DESC LIMIT 1) tracking_date FROM `pickup_entry` INNER JOIN tbl_user ON pickup_entry.client_id=tbl_user.id  WHERE pickup_entry.id='$awb'");	 
            $statement->execute();
            $result = $statement->fetchAll(PDO::FETCH_ASSOC);
            foreach($result as $row)
            {
                $date=date_create($row['tracking_date']);
			    $tracking_date=date_format($date,'d-m-Y');
			    $date1=date_create($row['date_added']);
			    $entry_date=date_format($date1,'d-m-Y');
                
                
                
            	fputcsv($output, array($i,$row['3pl_awb'],$row['status'],$row['tracking_location'],$tracking_date,$row['client_name'],$row['shipper_name'],$row['shipper_address'],$row['shipper_city'],$row['shipper_state'],$row['shipper_pincode'],$row['shipper_phone'],$row['consignee_name'],$row['consignee_address'],$row['consignee_city'],$row['consignee_state'],$row['consignee_pincode'],$row['consignee_phone'],$entry_date,$row['c_weight']));
    		    $i++;
            }
	    } 
		fclose($output);

?>