<?php ob_start();
session_name('CAKEPHP'); session_start();
include("reportconfig.php");
$data='';
$data.="<table width='100%' border='0' cellspacing='2' cellpadding='2'>";
$data.="<tr>";
$data.="<th colspan='6'>Stock Valuation Report</th>";
$data.="</tr>";
$data.="<tr>";
$data.="<th >SR No.</th>";
$data.="<th >Item Code</th>";
$data.="<th >Item Description</th>";
$data.="<th >Rate</th>";
$data.="<th >Closing Stock Qty</th>";
$data.="<th >Closing Stock Value</th>";
$data.="</tr>"; 
$data5= $_SESSION['query1'];
  $result=mysqli_query($con, $data5);
			
		$num=mysqli_affected_rows($con);
		if($num>0)
	{
		$flag=1;
				$i=0;
			    $totalamount=0;
				while($item_row=mysqli_fetch_array($result))
				{					
				$sql=mysqli_query($con, "select item_rate from purchase_bill_details where item_code='".$item_row['itemcode']."' ORDER BY id DESC ");	
				$rowdata=mysqli_fetch_array($sql);
				if($rowdata['item_rate']!='')
				{
				$rate=$rowdata['item_rate'];	
				}
				else 
				{
				$rate=$item_row['itemprice'];	
				}
				$total=$item_row['stockbalance']*$rate;	 
					$i++;  
					 $totalamount=$totalamount+$total;
				
				$data.="<tr bgcolor='#FFFFFF'>";
    	          $data.="<td>".$i."</td>";
    	          $data.="<td>".$item_row['itemcode']."</td>";
    	          $data.="<td>".$item_row['description']."</td>";
    	          $data.="<td>".$rate."</td>";
    	          $data.="<td>".$item_row['stockbalance']."</td>";
    	          $data.="<td>".$total."</td>";
    	          $data.="</tr>";
              } 
              $data.="<tr bgcolor='#FFFFFF'>";
    	          $data.="<td ></td>";
    	         $data.="<td ></td>";
    	         $data.="<td ></td>";
    	         $data.="<td ></td>";
    	          $data.="<th>Total</th>";
    	          $data.="<td>".$totalamount."</td>";
    	          $data.="</tr>";  } else { 
              $data.="<tr bgcolor='#FFFFFF'>";
    	          $data.="<th colspan='6'>No Records</th></tr>";
  $i++;}
  $data.="</table>";
   //$header='';  		
 header("Content-type: application/x-msdownload"); 
header("Content-Disposition: attachment; filename=data.xls"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
print "$header\n$data";  
  
  ?>
  