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

 $from = $_GET['from'];
    $to = $_GET['to'];
    $status = $_GET['status'];
    $role = $_GET['client_type'];
    $client_id = $_GET['client_id'];

 if($role=="Admin") 
    {
        if($status=='all' && $client_id=='all')
        {
           $condition='';  
        } 
        elseif($status!='all' && $client_id=='all')
        {
           $condition='WHERE pickup_entry.status_code="'.$status.'"';
        }
        elseif($status=='all' && $client_id!='all')
        {
           $condition='WHERE pickup_entry.client_id='.$client_id;
        }
        else
        {
            $condition='WHERE pickup_entry.status_code="'.$status.'" AND pickup_entry.client_id='.$client_id;
        }
        
        
        $statement = $pdo->prepare("SELECT pickup_entry.*,tbl_user.full_name as client_name,pickup_entry.date_added as added FROM pickup_entry INNER JOIN tbl_user on pickup_entry.client_id=tbl_user.id $condition order by pickup_entry.id ASC");   
     }
    else
    {
        
        if($status=='all')
        {
         $statement = $pdo->prepare("SELECT pickup_entry.*,tbl_user.full_name as client_name,pickup_entry.date_added as added FROM pickup_entry INNER JOIN tbl_user on pickup_entry.client_id=tbl_user.id where pickup_entry.client_id=$client_id order by pickup_entry.id ASC");   
        }
        else{
        
          $statement = $pdo->prepare("SELECT pickup_entry.*,tbl_user.full_name as client_name,pickup_entry.date_added as added FROM pickup_entry INNER JOIN tbl_user on pickup_entry.client_id=tbl_user.id where pickup_entry.status_code='$status' AND pickup_entry.client_id=$client_id order by pickup_entry.id ASC");   
        }
    }	
            	$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");  
		if($role=="Admin"){
		    fputcsv($output, array('SL','CSCS AWB','3PL AWB','3PL Name','Ref No','Status','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'));  
		}else{
		    fputcsv($output, array('SL','CSCS AWB','Ref No','Status','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) {
		    
		    
		$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"];
		}
		
		if(empty($company_name)){ $company_name="";}
		    
		    
		    if($row['gst_rate']=='gr_igst'){ $gst ="IGST";}elseif($row['gst_rate']=='gr_cgst'){ $gst ="CGST";} 
		    $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)
                    {
                    if($role=="Admin"){
		     fputcsv($output, array($i,$row['cscs_awb'],$row['3pl_awb'],$company_name,$row['ref_no'],strtoupper($row['status']),$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));
		}else{
		     fputcsv($output, array($i,$row['cscs_awb'],$row['ref_no'],strtoupper($row['status']),$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
                       {
                             if($role=="Admin"){
                		     fputcsv($output, array($i,$row['cscs_awb'],$row['3pl_awb'],$company_name,$row['ref_no'],strtoupper($row['status']),$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));
                		}else{
                		    fputcsv($output, array($i,$row['cscs_awb'],$row['ref_no'],strtoupper($row['status']),$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);




?>