<?php
date_default_timezone_set("Asia/Kolkata");
 error_reporting(0);
ob_start();
session_start();
 
	include("../../include/db.php");

	

 $job_card_no=$_GET['job_card_no'];
 $buyer_name=$_GET['buyer_name'];
  $roll_size=$_GET['roll_size'];
 $item_id4=$_GET['item_name'];
 $type_1=$_GET['type_1'];
 

 $from_date=$_GET['from_date'];
 $to_date=$_GET['to_date'];
 $cur_date=date("Y-m-d");
if($from_date!=""){ $from_date1= "DATE_FORMAT(b.media_date,'%Y-%m-%d')>='$from_date'"; }else{$from_date1="DATE_FORMAT(b.media_date,'%Y-%m-%d')='$cur_date'"; }
if($to_date!=""){ $to_date1 = "DATE_FORMAT(b.media_date,'%Y-%m-%d')<='$to_date'";}else{$to_date1="DATE_FORMAT(b.media_date,'%Y-%m-%d')='$cur_date'";}
if($job_card_no!=""){ $job_card_no41 = "b.jobcard_no='$job_card_no'";}else{$job_card_no41="";}
if($buyer_name!=""){ $buyer_name41 = "b.in_cust_name='$buyer_name'";}else{$buyer_name41="";}


$all_value10 = $from_date1."@".$to_date1."@".$job_card_no41."@".$buyer_name41;
$all_array10 = explode('@',$all_value10);
foreach($all_array10 as $value10)
{ 
if($value10!='')
{
$get_query105 .= $value10." AND ";
}
}
if($from_date!=""){ $from_date2= "DATE_FORMAT(a.entry_time,'%Y-%m-%d')>='$from_date'"; }else{$from_date2="DATE_FORMAT(a.entry_time,'%Y-%m-%d')='$cur_date'"; }
if($to_date!=""){ $to_date2 = "DATE_FORMAT(a.entry_time,'%Y-%m-%d')<='$to_date'";}else{$to_date2="DATE_FORMAT(a.entry_time,'%Y-%m-%d')='$cur_date'";}
if($job_card_no!=""){ $job_card_no2 = "a.jobcard_no='$job_card_no'";}else{$job_card_no2="";}
if($buyer_name!=""){ $buyer_name2 = "a.customer_name='$buyer_name'";}else{$buyer_name2="";}


$all_value101 = $from_date2."@".$to_date2."@".$job_card_no2."@".$buyer_name2;
$all_array101 = explode('@',$all_value101);
foreach($all_array101 as $value101)
{ 
if($value101!='')
{
$get_query1051 .= $value101." AND ";
}
}

if($from_date!=""){ $from_date21= $from_date; }else{$from_date21=$cur_date; }
if($to_date!=""){ $to_date21 = $to_date;}else{$to_date21=$cur_date;}
if($job_card_no!=""){ $job_card_no11=$job_card_no;} else {$job_card_no11="ALL" ;} 
if($buyer_name!=""){ $buyer_name11=$buyer_name;} else {$buyer_name11="ALL" ;}


$blog=array('','Media Pending Report','','');


foreach($blog as $icon)
{
$output		.= '"'.$icon.'",';
}

$output .="\n";
$output .="\n"; 
$output .="From Date".",".$from_date21.","."To Date".",".$to_date21.","."Jobcard No".",".$job_card_no11.","."Buyer Name".",".$buyer_name11."\n"; 
$output .="\n"; 

 

 
     $output .='"SNo","Date","Jobcard No","Buyer Name","Particulars","Order Qty","Plan Qty","Pending Qty",'."\n";
      $table_values_new=array(ItemGroupName);
	 $table_values=array(SNo,Date,JobcardNo,BuyerName,Particulars,OrderQty,PlanQty,PendingQty);
   
   
   $sql="select a.entry_time as media_date,a.particulars as media_particular,a.customer_name as in_cust_name,a.jobcard_no as jobcard_no,a.qty as order_qty,a.jobcard_no as plan_qty from jobcard_sublist as a LEFT JOIN media_sub_list as b ON a.jobcard_no=b.jobcard_no where $get_query1051 b.jobcard_no IS NULL group by a.particulars 
UNION

select b.media_date as media_date,b.media_particular as media_particular,b.in_cust_name as in_cust_name,b.jobcard_no as jobcard_no,b.order_qty as order_qty,b.plan_qty as plan_qty from  media_sub_list as b  where $get_query105 b.id!='' group by b.media_particular ";


    $rs=mysql_query($sql);
	 $rscount=mysql_num_rows($rs);
	if($rscount!='')
	{
	$s_no=0;
   		while($rsdata=mysql_fetch_array($rs))
		{
   				 $media_date=$rsdata['media_date'];
		 $media_particular=$rsdata['media_particular'];
		 $customer_name=$rsdata['in_cust_name'];
         $jobcard_no=$rsdata['jobcard_no'];
					$order_qty=$rsdata['order_qty'];
					$plan_qty=$rsdata['plan_qty'];		
		 $pending_qty=$order_qty-$plan_qty;
		 if($pending_qty!=0){ 
			 $i=$i+1;
			 
			foreach ($table_values as $val) 
			{
				   
				        	
				if($val=='SNo'){$output.='"'.$i.'",';}
				if($val=='Date'){$output.='"'.date("d-m-Y",strtotime($media_date)).'",';}
				if($val=='JobcardNo'){$output.='"'.$jobcard_no.'",';}
				if($val=='BuyerName'){$output.='"'.$customer_name.'",';}
				if($val=='Particulars'){$output.='"'.$media_particular.'",';}
				if($val=='OrderQty'){$output.='"'.number_format($order_qty,2).'",';}
				if($val=='PlanQty'){$output.='"'.number_format($plan_qty,2).'",';}
				if($val=='PendingQty'){$output.='"'.number_format($pending_qty,2).'",';}
				
				   
				

if(($val!='SNo')&&($val!='Date')&&($val!='OB')&&($val!='JobcardNo')&&($val!='BuyerName')&&($val!='Particulars')&&($val!='OrderQty')&&($val!='PlanQty')&&($val!='PendingQty')){$output .='"'.$val.'",';}
} 
 $output .="\n";
		
					
				
				 
	 $sum_tot1+=$order_qty;
	  $sum_tot2+=$plan_qty;
	  $sum_tot3+=$pending_qty;
		 }		    
 
}
 
$output .="".",".",".",".",".Total.",".$sum_tot1.",".$sum_tot2.",".$sum_tot3."\n\n";
}
$date=date('d-m-Y H:i:s');
$filename =  "Media Pending Report". $date.".csv";
header('Content-type: application/xls');
header('Content-Disposition: attachment; filename='.$filename);
echo $output;



exit;
	


?>
<style>
br{
	
	mso-data-placement:same-cell;
	}
</style>