<?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;
}
$buyer_name=$_GET['buyer_name'];
 $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($item_name4!=""){ $item_name41 = "particulars='$item_name4'";}else{$item_name41="";}
if($buyer_name!=""){ $buyer_name41 = "supplier_name='$buyer_name'";}else{$buyer_name41="";}

$all_value10 = $from_date1."@".$to_date1."@".$item_name41."@".$buyer_name41;
$all_array10 = explode('@',$all_value10);
foreach($all_array10 as $value10)
{ 
if($value10!='')
{
$get_query102 .= $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($item_name4!=""){ $item_name3=$item_name4;} else {$item_name3="ALL" ;} 
if($buyer_name!=""){ $buyer_name1=$buyer_name;} else {$buyer_name1="ALL" ;}



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


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

$output .="\n";
$output .="\n"; 
$output .="From Date".",".$from_date2.","."To Date".",".$to_date2.","."Item Name".",".$item_name3.","."Buyer Name".",".$buyer_name1."\n"; 
$output .="\n"; 

 

 
     $output .='"SNo","Date","Invoice No","Supplier","Particulars","Qty","Mtr Qty"'."\n";
      $table_values_new=array(ItemGroupName);
	 $table_values=array(SNo,Date,InvoiceNo,Supplier,Particulars,Qty,MtrQty);
   
   //echo "select *  from media_sub_list where $get_query105 id!='' order by jobcard_no ASC ";
  $sql="select sum(qty) as qty,entrydates,invoice_no,supplier_name,particulars,sum(mtr_roll) as mtr_roll from media_pur_sublist where $get_query102 id!=''  group by invoice_no,supplier_name,particulars,entrydates,mtr_roll order by particulars ASC ";


    $rs=mysql_query($sql);
	$rscount1=mysql_num_rows($rs);
	$s_no=0;
	if($rscount1!='')
	{
   		while($rsdata=mysql_fetch_array($rs))
  			{
   				 $s_no=$s_no+1;
				 $from=$from+1;
                 $id=$rsdata['id'];
                 $ink_pur_no=$rsdata['ink_pur_no'];
				 $invoice_no=$rsdata['invoice_no'];
				 $supplier_name=$rsdata['supplier_name'];
				 $particulars=$rsdata['particulars'];
				 $qty=$rsdata['qty'];
				 $mtr_roll=$rsdata['mtr_roll'];
			     $entrydates=$rsdata['entrydates'];
		 $tot_mtr=$rsdata['tot_mtr'];
		  
			 $sno=$sno+1;
			foreach ($table_values as $val) 
			{
				
				if($val=='SNo'){$output.='"'.$sno.'",';}
				if($val=='Date'){$output.='"'.date("d-m-Y",strtotime($entrydates)).'",';}
				if($val=='InvoiceNo'){$output.='"'.$invoice_no.'",';}
				if($val=='Supplier'){$output.='"'.$supplier_name.'",';}
				if($val=='Particulars'){$output.='"'.$particulars.'",';}
				if($val=='Qty'){$output.='"'.number_format($qty,2).'",';}
				if($val=='MtrQty'){$output.='"'.number_format($mtr_roll*$qty,2).'",';}
				
				
				

if(($val!='SNo')&&($val!='Date')&&($val!='InvoiceNo')&&($val!='Supplier')&&($val!='Particulars')&&($val!='Qty')&&($val!='MtrQty')){$output .='"'.$val.'",';}
} 
 $output .="\n";
		
					
				
				 
	 $sum_tot+=$mtr_roll*$qty;
 
}
 
$output .="".",".",".",".",".",".Total.",".$sum_tot."\n\n";
}
$date=date('d-m-Y H:i:s');
$filename =  "Media Purchase 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>