<?php
ob_start();
session_start();
include "../inc/config.php";
include "../inc/session.php";
?>

<?php


$now = gmdate("YmdHis");
header('Content-Type: text/csv; charset=utf-8');  
header('Content-Disposition: attachment; filename=Manifest-Report.csv');  
$output = fopen("php://output", "w"); 
	

$requestData= $_REQUEST;

$conditions='';

$to_branch=$_POST['to_branch'] ?? 0;
$from_branch=$_POST['from_branch'] ?? 0;
$coloader=$_POST['coloader'] ?? 0;
$from_date=$_POST['from_date'] ?? date('Y-m-d');
$to_date=$_POST['to_date'] ?? date('Y-m-d');
$manifest_status = $_POST['manifest_status'];

if($manifest_status!=""){
    $conditions.=" and manifest.manifest_status=".$manifest_status;
}

    if($to_branch!='all' && $to_branch!='' && $to_branch!='0'){
        $conditions.=" and manifest.destination='".$to_branch."'";
    }
    if($from_branch!='all' && $from_branch!='' && $from_branch!='0'){
        $conditions.=" and manifest.origin='".$from_branch."'";
    }
    if($coloader!='all' && $coloader!='' && $coloader!='0'){
        $conditions.=" and manifest.coloader_id='".$coloader."'";
    }
    
    $conditions.=" and manifest.entry_date BETWEEN '$from_date' AND '$to_date' ";
    
    if($session['id']!=1 && $session_branch_ids!='0'){
        $conditions.= " and manifest.origin IN ($session_branch_ids)";
    }
    
    if($session['id']!=1 && $session_branch_ids!='0'){
        $conditions.= " and manifest.destination IN ($session_branch_ids)";
    }

        
    	$statement = $pdo->prepare("SELECT manifest.*,
                                 (select name from tbl_branch where id=manifest.origin) as from_branch,
                                 (select name from tbl_branch where id=manifest.destination) as to_branch,
                                 (select name from tbl_coloader where id=manifest.coloader_id) as coloader_name FROM `manifest` where 1=1 ".$conditions);
		$statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalData = $statement->rowCount();
        $totalFiltered = $totalData;
        /*Fetch All Data End*/ 
        
        
        
        fputcsv($output, array('S NO','DRIVER NAME','DRIVER MOBILE NUMBER','VEHICLE NUMBER','FROM BRANCH','TO BRANCH','DATE','TOTAL SHIPMENT','TOTAL BOX','TOTAL WEIGHT','CO LOADER NAME','MANIFEST TYPE','MANIFEST STATUS'));
        
        $i =1;
        
        foreach($query as $row ) { 
            
            
            $bo=0;$wt=0;
     	    $statement = $pdo->prepare("SELECT manifest.*,
             (select sum(pickup_entry.boxes) from pickup_entry where pickup_entry.id in (".$row['ids'].")) as box,
             (select sum(a_weight) from pickup_entry where id in (".$row['ids'].")) as wt FROM `manifest` where id=".$row['id']);
            $statement->execute();
    		$qu = $statement->fetchAll(PDO::FETCH_ASSOC);
    		if($qu){
    		    $bo=$qu[0]['box'];
    		    $wt=$qu[0]['wt'];
    		}
     	
     	    if($row["manifest_type"]==1){	
                $manifest_type = " Incoming";
            }else{
                $manifest_type = "Outcoming";
            }
            
            
            if($row["manifest_status"]==0){	
                $manifest_status = " Incoming Not Verified";
            }elseif($row["manifest_status"]==1){
                $manifest_status = "Incoming Verified";
            }elseif($row["manifest_status"]==2){
                $manifest_status = "Incoming Partially Verified";
            }
            
            
            
            fputcsv($output, array($i,$row['driver_name'],$row['driver_mobile_no'],$row['vehicle_no'],$row['from_branch'],$row['to_branch'],date('d-m-Y',strtotime($row['entry_date'])),$row['no_of_entry'],$bo,$wt,$manifest_type,$manifest_status));
            
            $i++;
        }
   
   	fclose($output);

       

?>