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

	function get_item_id_media_print($item_name4)
{
$sql_query11="select * from consumable_creation where id='$item_name4' and item='Media' ";
$rs11=mysql_query($sql_query11);
 while($rsdata11=mysql_fetch_object($rs11))
 {
 $particulars=$rsdata11->particulars;

 } 
 return $particulars;
}

// $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'];
//  $jobtype=$_GET['jobtype'];
 $item_name4=get_item_id_media_print($item_id4);

 $from_date=$_GET['from_date'];
 $to_date=$_GET['to_date'];
 $cur_date=date("Y-m-d");
if($from_date!=""){ $from_date1= "DATE_FORMAT(media_date,'%Y-%m-%d')>='$from_date'"; }else{$from_date1="DATE_FORMAT(media_date,'%Y-%m-%d')='$cur_date'"; }
if($to_date!=""){ $to_date1 = "DATE_FORMAT(media_date,'%Y-%m-%d')<='$to_date'";}else{$to_date1="DATE_FORMAT(media_date,'%Y-%m-%d')='$cur_date'";}
//if($job_card_no!=""){ $job_card_no41 = "jobcard_no='$job_card_no'";}else{$job_card_no41="";}
if($buyer_name!=""){ $buyer_name41 = "in_cust_name='$buyer_name'";}else{$buyer_name41="";}
if($roll_size!=""){ $roll_size41 = "roll_name='$roll_size'";}else{$roll_size41="";}
//if($jobtype!=""){ $job_type = "job_types='$jobtype'";}else{$job_type="";}
//"@".$job_card_no41.."@".$job_type
$all_value10 = $from_date1."@".$to_date1."@".$buyer_name41."@".$roll_size41;
$all_array10 = explode('@',$all_value10);
foreach($all_array10 as $value10)
{ 
if($value10!='')
{
$get_query105 .= $value10." AND ";
}
}	  

if($from_date!=""){ $from_date2= $from_date; }else{$from_date2=$cur_date; }
if($to_date!=""){ $to_date2 = $to_date;}else{$to_date2=$cur_date;}
//if($job_card_no!=""){ $job_card_no1=$job_card_no;} else {$job_card_no1="ALL" ;} 
if($buyer_name!=""){ $buyer_name1=$buyer_name;} else {$buyer_name1="ALL" ;}
if($roll_size!=""){ $roll_size1=$roll_size;} else {$roll_size1="ALL" ;}
if($jobtype!=""){ $job_type1=$jobtype;} else {$job_type1="ALL" ;}


$blog=array('','Media Usage Console Report','','');
$blog1=array('','','Buyer Wise','','');

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

$output .="\n"; 
$output .="\n"; 
foreach($blog1 as $icon1)
{
$output		.= '"'.$icon1.'",';
}
$output .="\n"; 
$output .="\n"; 
$output .="From Date".",".$from_date2.","."To Date".",".$to_date2.","."Jobcard No".",".$job_card_no1.","."Buyer Name".",".$buyer_name1.","."Roll Size".",".$roll_size1."\n"; 
$output .="\n"; 

 

 
     $output .='"SNo","Buyer Name","Roll Size","Quantity","Meter","Sqr Meter",'."\n";
      $table_values_new=array(ItemGroupName);
	 $table_values=array(SNo,BuyerName,RollSize,Quantity,Meter,SqrMeter);
   
   //echo "select *  from media_sub_list where $get_query105 id!='' order by jobcard_no ASC ";
   $sql="select entrydates ,media_date ,job_types ,in_cust_name ,jobcard_no ,sum(plan_qty) as qty ,roll_name ,SUM(gap_mtr) as gmeter,SUM(tot_req_by) as meter from media_sub_list where $get_query105 id!='' group by  in_cust_name,roll_name order by roll_name ASC ";


    $rs=mysql_query($sql);
	 $rscount=mysql_num_rows($rs);
	if($rscount!='')
	{
	$s_no=0;
   		while($rsdata=mysql_fetch_array($rs))
		{
   				 $s_no=$s_no+1;
				 $from=$from+1;
                $media_date=$rsdata['media_date'];
         //	  $media_particular=$rsdata['media_particular'];
           $customer_name=$rsdata['in_cust_name'];
            $jobcard_no=$rsdata['jobcard_no'];
            $jobcard_no=$rsdata['jobcard_no'];
         //	 $width=$rsdata['width'];
            $roll_name=$rsdata['roll_name'];
         //	 $roll_size=$rsdata['roll_size'];
             $total_qty=$rsdata['qty'];
           //     $order_qty=$rsdata['order_qty'];
         //	 $height=$rsdata['height'];
         //	  $tot_req_by=$rsdata['tot_req_by'];
         //	   $total_qty=$rsdata['plan_qty'];
         //	 $operator_name=$rsdata['operator_name'];
         //	 $steps=$rsdata['steps'];
         //	$steps_used=$rsdata['steps_used'];
         $gap_mtr=$rsdata['meter'];
         $gap_mtrs = $rsdata['gmeter'];
         // $gap=$rsdata['gap'];
         //$total_steps=$rsdata['total_steps'];
         //$req_by=$rsdata['req_by'];
          $sqr_meter=(($roll_name*39.37)/1550)*$gap_mtr;
				
			  $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=='JobTypes'){$output.='"'.$job_types.'",';}
				// if($val=='Width'){$output.='"'.$width.'",';}
				// if($val=='Height'){$output.='"'.$height.'",';}
				// if($val=='OrderQty'){$output.='"'.number_format($order_qty,2).'",';}
				if($val=='RollSize'){$output.='"'.$roll_name.'",';}
				if($val=='Quantity'){$output.='"'.number_format($total_qty,2).'",';}
				// if($val=='ReqBy'){$output.='"'.$req_by.'",';}
				// if($val=='StepsUsed'){$output.='"'.$steps_used.'",';}
				// if($val=='Steps'){$output.='"'.$steps.'",';}
				// if($val=='TotalRepeats'){$output.='"'.$total_steps.'",';}
				// if($val=='Gap'){$output.='"'.$gap.'",';}
				if($val=='Meter'){$output.='"'.number_format($gap_mtr,2).'",';}
				if($val=='SqrMeter'){$output.='"'.number_format($sqr_meter,2).'",';}
				
				

if(($val!='SNo')&&($val!='BuyerName')&&($val!='RollSize')&&($val!='Quantity')&&($val!='Meter')&&($val!='SqrMeter')){$output .='"'.$val.'",';}
} 
 $output .="\n";
		
					
				
				 
	 $s1 += $sqr_meter;
	  $s2 += $total_qty;
	   $s3 += $gap_mtr;
 
}
 
$output .="".Total.",".",".",".$s2.",".$s3.",".$s1."\n\n";
}
$date=date('d-m-Y H:i:s');
$filename =  "Media Usage Console BuyerWise 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>