<?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_type=$_GET['jobtype'];
 $buyer_name=$_GET['buyer_name'];
  $roll_size=$_GET['roll_size'];
 $item_id4=$_GET['item_name'];
 $type_1=$_GET['type_1'];
 $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($job_type!=""){ $job_type41 = "job_types='$job_type'";}else{$job_type41="";}

$all_value10 = $from_date1."@".$to_date1."@".$buyer_name41."@".$roll_size41."@".$job_type41;
$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($job_type!=''){ $job_type1=$job_type;} else { $job_type1="All"; }

$blog=array('','Media Usage Analyze Report','','');


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

$output .="\n";
$output .="\n"; 

$output .="From Date".",".$from_date2.","."To Date".",".$to_date2.","."Job Type".",".$job_type1.","."Buyer Name".",".$buyer_name1.","."Roll Size".",".$roll_size1.","."\n"; 

// $output .="From Date".",".$from_date2.","."To Date".","."Job Type".",".$job_type1.",".$to_date2.","."Buyer Name".",".$buyer_name1.","."Roll Size".",".$roll_size1."\n"; 
$output .="\n"; 

     $output .='"SNo","Date","Job Type","Buyer Name","Jobcard No","Quantity","Roll Size","Meter","Sqr Meter",'."\n";
      $table_values_new=array(ItemGroupName);
	 $table_values=array(SNo,Date,JobType,BuyerName,JobcardNo,Quantity,RollSize,Meter,SqrMeter);
   
   //echo "select *  from media_sub_list where $get_query105 id!='' order by jobcard_no ASC ";
   if(($from_date!="") && ($type_1=="Buyer Wise")){
	$sql="select * from media_sub_list where $get_query105 id!='' order by in_cust_name ASC ";
  }elseif((($from_date!="") && $type_1=="Job Type Wise")){
	$sql="select * from media_sub_list where $get_query105 id!='' order by job_types ASC ";
  }elseif((($from_date!="") && $type_1=="Size Wise")){
	$sql="select * from media_sub_list where $get_query105 id!='' order by roll_name ASC ";
  }elseif(($from_date!="") && ($type_1=="Date Wise")){
	$sql="select * from media_sub_list where $get_query105 id!='' order by media_date ASC ";
	//echo "select * from media_sub_list where $get_query105 id!='' order by media_date ASC ";
  }else{
	$sql="select * from media_sub_list where DATE_FORMAT(media_date,'%Y-%m-%d')='$cur_date' and id!='' order by media_date ASC";  
  }
// echo $sql;
// exit();

//echo $sql;

    $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'];
            $job_types=$rsdata['job_types'];
            $customer_name=$rsdata['in_cust_name'];
                $jobcard_no=$rsdata['jobcard_no'];
           //    $width=$rsdata['width'];
           //  $height=$rsdata['height'];
                $roll_name=$rsdata['roll_name'];
                $sq_mtr = explode('-', $roll_name);  
         if(count($sq_mtr) == 2) {  
             $width = $sq_mtr[0];   
             $height = $sq_mtr[1]; 
             $squ = ($width * 2.54) / 100;
                        //  echo number_format($squ, 2);  // Display the square meters with 2 decimal places
         } 
         //	 $roll_size=$rsdata['roll_size'];
                $total_qty=$rsdata['plan_qty'];
         //	 $order_qty=$rsdata['order_qty'];
                $gap_mtr=$rsdata['gap_mtr'];
           //   $operator_name=$rsdata['operator_name'];
         //	$steps=$rsdata['steps'];
         //	$steps_used=$rsdata['steps_used'];
           $gap_mtrs = $rsdata['tot_req_by'];
           
         //	$gap=$rsdata['gap'];
         //	$total_steps=$rsdata['total_steps'];
         //	$req_by=$rsdata['req_by'];
         $squre_mettrs= $squ*$gap_mtrs;
				
			  $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=='JobType'){$output.='"'.$job_types.'",';}
				
				if($val=='BuyerName'){$output.='"'.$customer_name.'",';}
				if($val=='JobcardNo'){$output.='"'.$jobcard_no.'",';}
			//	if($val=='OrderQty'){$output.='"'.number_format($order_qty,2).'",';}
				if($val=='Quantity'){$output.='"'.number_format($total_qty,2).'",';}
				if($val=='RollSize'){$output.='"'.$roll_name.'",';}
				if($val=='Meter'){$output.='"'.number_format($gap_mtrs,2).'",';}
				if($val=='SqrMeter'){$output.='"'.number_format($squre_mettrs,2).'",';}
				// if($val=='TotalReq'){$output.='"'.number_format($sqr_meter,2).'",';}
				

if(($val!='SNo')&&($val!='Date')&&($val!='JobType')&&($val!='OB')&&($val!='BuyerName')&&($val!='JobcardNo')&&($val!='Quantity')&&($val!='RollSize')&&($val!='Meter')&&($val!='SqrMeter')){$output .='"'.$val.'",';}
} 
 $output .="\n";
		
					
				
				 
	 $s1 += $squre_mettrs;
	  $s2 += $roll_name;
	   $s3 += $gap_mtrs;
    //  $s4 += $sqr_meter;
}
 
$output .="".",".",".",".",".",".",".Total.",".$s3.",".$s1."\n\n";
}
$date=date('d-m-Y H:i:s');
$filename =  "Media Usage Analyze 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>