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

 $from = $_POST['fromm'];
    $to = $_POST['too'];
    $status = $_POST['set_statuss'];
    $client_id = $_POST['client_ids'];

   if($status=='all')
    {
         $statement = $pdo->prepare("SELECT pickup_entry.*,tbl_user.full_name as client_name,pickup_entry.created_at as added,tbl_customer.cust_code as cust_code,
    	       material_desc.full_desc as content,tbl_customer.cust_name as cust_name FROM pickup_entry INNER JOIN tbl_user INNER JOIN tbl_customer INNER JOIN material_desc 
    	       on pickup_entry.client_id=tbl_user.id AND pickup_entry.content_desc=material_desc.id AND pickup_entry.customer_id=tbl_customer.cust_id 
    	       where pickup_entry.client_id='$client_id' order by pickup_entry.id DESC");   
    }
    else{
          $statement = $pdo->prepare("SELECT pickup_entry.*,tbl_user.full_name as client_name,pickup_entry.created_at as added,tbl_customer.cust_code as cust_code,
    	       material_desc.full_desc as content,tbl_customer.cust_name as cust_name FROM pickup_entry INNER JOIN tbl_user INNER JOIN tbl_customer INNER JOIN material_desc 
    	       on pickup_entry.client_id=tbl_user.id AND pickup_entry.content_desc=material_desc.id AND pickup_entry.customer_id=tbl_customer.cust_id 
    	       where pickup_entry.client_id='$client_id' and pickup_entry.status='$status' order by pickup_entry.id DESC");   
    }
        
            	
            	
            	
            	
            	$statement->execute();
            	$result = $statement->fetchAll(PDO::FETCH_ASSOC);	
            	




	$now = gmdate("YmdHis");
		header('Content-Type: text/csv; charset=utf-8');  
		header('Content-Disposition: attachment; filename=Pickup_list_'.$now.'.csv');  
		$output = fopen("php://output", "w");  
// 		fputcsv($output, array('SL', 'AWB Number', 'Ref Number', 'status', 'Company Name', 'Address', 'Pincode', 'Mobile', 'Actual Weight', 'Charged Weight', 'No.Of Pics', 'Date Added', 'Customer Name'));  
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','No.Of Boxes','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;
		foreach ($result as $row) {
		    $date=date_create($row['date_added']);
			 $day=date_format($date,'d-m-Y');
			 
			  
			   $created=date_create($row['date_added']);
                     $da=date_format($created,'Y-m-d');
                     if($from!='' && $to!=''){
                     if ($from <= $da && $to >= $da)
                    {
        			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['boxes'],$row['invoice_value'],$row['taxable_value'],$row['hsn_code'],$row['discount'],$row['gst_amount'],$row['mrp'],$row['ewaybill'],$row['a_weight'],$row['c_weight'],$gst,$row['buyer_gst'],$row['seller_gst'],$row['rto_name'],$row['rto_address'],$row['rto_city'],$row['rto_state'],$row['rto_phone'],$row['rto_pincode'],$day));
        		    $i++;
                    }
                     }
                     else
                      {
        			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['boxes'],$row['invoice_value'],$row['taxable_value'],$row['hsn_code'],$row['discount'],$row['gst_amount'],$row['mrp'],$row['ewaybill'],$row['a_weight'],$row['c_weight'],$gst,$row['buyer_gst'],$row['seller_gst'],$row['rto_name'],$row['rto_address'],$row['rto_city'],$row['rto_state'],$row['rto_phone'],$row['rto_pincode'],$day));
        		    $i++;
                    }
		} 
		fclose($output);




?>