<?
session_start();
include("opendb.php");
include("calstock.php");

if(strlen($brcode)<=0)
{
 print "<script> alert('Invalid Login'); parent.document.location='../ind.php' </script>";
 return;
}

if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}

$qry=mysqli_query($db,"select * from invoice where invno='$inno'");
if(mysqli_num_rows($qry)<=0)
{
 print"<script>alert('invalid invoice number');document.location='excelinv1.php';</script>";
} 

$qry=mysqli_query($db,"select * from invoice where invno='$inno'");
$row=mysqli_fetch_array($qry);       
$date = $row[1];
$newDate = date("d-m-Y", strtotime($date));


$filename ="excelreport.xls";



 $contents =" Customer Name\t ".$row[2]."\t \n";
$contents .=" Register No\t ".$row[3]."\t \n";
$contents .=" Invoice No\t ".$row[0]."\t \n";
$contents .=" Invoice Date\t ".$newDate."\t \n";
$contents .=" Sales Person\t ".$row[12]."\t \n";


$res=mysqli_query($db,"select sum(qty),group1,sum(dis),sum(total) from invoice where invno='$inno' and flag='spares' group by group1");
if(mysqli_num_rows($res)>0)
{
 $contents .=" Spare\t Unit\t Quantity \t Total Amount \t Discount \t Net Amount \t \n";
while($res1=mysqli_fetch_array($res))
  {
   
$tots=$res1[2]+$res1[3];
$contents .=$res1[1]."\t";
$contents .="litre\t";
$contents .=$res1[0]."\t";
$contents .=$tots."\t";
$contents .=$res1[2]."\t";
$contents .=$res1[3]."\t \n";

  }
}

$res2=mysqli_query($db,"select * from invoice where invno='$inno' and flag='labour' ");
if(mysqli_num_rows($res2)>0)
{
$contents .=" Labour\t Unit\t Quantity \t Total Amount \t Discount \t Net Amount \t \n";
while($res3=mysqli_fetch_array($res2))
  {
   $totl=$res3[8]+$res3[9];
$contents .=$res3[4]."\t";
$contents .="--\t";
$contents .=$res3[6]."\t";
$contents .=$totl."\t";
$contents .=$res3[8]."\t";
$contents .=$res3[9]."\t \n";


   }
}

$contents = strip_tags($contents); 

header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);

echo $contents;

?>