<?php
session_start();
$clientId=$_SESSION['user']['id'];
$clientLocation=$_SESSION['user']['location'];
$clientType=$_SESSION['user']['role'];
include("../inc/config.php");
            	
	$now = gmdate("YmdHis");
		header('Content-Type: text/csv; charset=utf-8');  
		header('Content-Disposition: attachment; filename=Picked-Pickup-list-'.$now.'.csv');  
		$output = fopen("php://output", "w");  
		fputcsv($output, array('SL','CSCS AWB','3PL AWB','Ref No','Order Type','Shipper Name','Shipper Address','Shipper City','Shipper State','Shipper Phone','Shipper Pincode','Consignee Name','Consignee Address','Consignee City','Consignee State','Consignee Phone','Consignee Pincode','Volumetric Weight','Volumetric Length','Volumetric Breadth','Volumetric Height','Collectable Amount','Product Description','Quantity','Invoice Value','Taxable Value','Hsn Code','Discount','Gst Amount','MRP','Ewaybill','Actual Weight','Charged Weight','Gst Type','Buyer Gst','Seller Gst','RTO Name','RTO Address','RTO City','RTO State','RTO Phone','RTO Pincode','Date Added'));  
        $i=1;
        	if($clientType=="Admin")
						 { 
							$statement = $pdo->prepare("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='picked' ORDER BY pickup_entry.id DESC");
						 }
						 else
						 {
						     $statement = $pdo->prepare("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='picked' AND and pickup_entry.client_id=$clientId ORDER BY pickup_entry.id DESC");
						 }
            $statement->execute();
            $result = $statement->fetchAll(PDO::FETCH_ASSOC);
            foreach($result as $row)
            {
                if($row['gst_rate']=='gr_igst'){ $type="IGST";}elseif($row['gst_rate']=='gr_cgst'){ $type="CGST";}
                	$day=date_create($row['date_added']);
					$created=date_format($day,'d-m-Y');
            	fputcsv($output, array($i,$row['cscs_awb'],$row['3pl_awb'],$row['ref_no'],$row['order_type'],$row['shipper_name'],$row['shipper_address'],$row['shipper_city'],	$row['shipper_state'],$row['shipper_phone'],$row['shipper_pincode'],$row['consignee_name'],	$row['consignee_address'],$row['consignee_city'],$row['consignee_state'],$row['consignee_phone'],$row['consignee_pincode'],	$row['v_weight'],$row['vw_length'],$row['vw_breadth'],$row['vw_height'],$row['collectable_amount'],$row['product_description'],$row['quantity'],$row['invoice_value'],$row['taxable_value'],$row['hsn_code'],$row['discount'],$row['gst_amount'],$row['mrp'],$row['ewaybill'],$row['a_weight'],$row['c_weight'],$type,$row['buyer_gst'],$row['seller_gst'],$row['rto_name'],$row['rto_address'],$row['rto_city'],$row['rto_state'],$row['rto_phone'],$row['rto_pincode'],$created ));
    		    $i++;
            }
		fclose($output);
?>