<?
session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='inde.php'; </script>";
 return;
}
$fromdt=$_REQUEST['dfrom'];
$todt=$_REQUEST['dto'];
$type=$_REQUEST['gname'];
$redtype=$_REQUEST['redtp'];


$qry=mysqli_query($db,"select gname,invdate,sum(total),sum(mba),sum(strbtnml),sum(strbtspl),sum(redct),sum(befrtx),sum(taxamt),sum(nettot),min(invno),max(invno) from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' and gname='$type' group by gname,invdate");
$filename ="rebtrpt_grws.xls";
$contents="Date \t From Bill \t To Bill \t Amount \t MDA \t State Rebate Normal \t State Rebate Spl \t Total \t Remarks \n";


$mfrom_date = $fromdt;
	$mto_date = $todt;

//print "<tr><th COLSPAN=9>Rebate Report for the period from <$mfrom_date> to <$mto_date></th>";
	
        $colspan=8;
	$mstr[]='';
	$i=1;
	$mgname = '.';
	$mtot_total = 0;
	$mtot_mba= 0;
	$mtot_strbtnml = 0;
	$mtot_strbtspl = 0;
	$mtot_redct = 0;
	$mtot_befrtx = 0;
	$mtot_taxamt = 0;
	$mtot_nettot = 0;

	$mgr_total = 0;
	$mgr_mba= 0;
	$mgr_strbtnml = 0;
	$mgr_strbtspl = 0;
	$mgr_redct = 0;
	$mgr_befrtx = 0;
	$mgr_taxamt = 0;
	$mgr_nettot = 0;
	$ctr=0;
  	while($row=mysqli_fetch_array($qry))
  	{
		if($mgname <> $row[0])
		{
			if($mgname <> '.')
			{

$contents.=$mgname." Total \t";
$contents.=$mgr_total."\t";
$contents.=$mgr_mba."\t";
$contents.=$mgr_strbtnml."\t";
$contents.=$mgr_strbtspl."\t";
$contents.=$mgr_redct."\t";
$contents.=$mgr_befrtx."\t";
$contents.=$mgr_taxamt."\t";
$contents.=$mgr_nettot."\n";
$mgr_total = 0;
				$mgr_mba= 0;
				$mgr_strbtnml = 0;
				$mgr_strbtspl = 0;
				$mgr_redct = 0;
				$mgr_befrtx = 0;
				$mgr_taxamt = 0;
				$mgr_nettot = 0;
			}
   $contents.=$row[0]."\t";
$contents.="\t";
$contents.="\t";
$contents.="\t";
$contents.="\t";
$contents.="\t";
$contents.="\t";
$contents.="\t";
$contents.="\n";
$mgname = $row[0];
		}

$date = $row[1];
if($redtype=='mda')
{
$contents.=$date."\t";
$contents.="'".$row[10]."\t";
$contents.=$row[11]."\t";
$contents.=$row[2]."\t";
$contents.=$row[3]."\t";
$contents.="\t";
$contents.="\t";
$contents.=$row[3]."\t";
$contents.="\n"; 
}

if($redtype=='strbtnml')
{
$contents.=$date."\t";
$contents.=$row[10]."\t";
$contents.=$row[11]."\t";
$contents.=$row[2]."\t";
$contents.="\t";
$contents.=$row[4]."\t";
$contents.="\t";
$contents.=$row[4]."\t";
$contents.="\n"; 

   
}
if($redtype=='strbtspl')
{
$contents.=$date."\t";
$contents.=$row[10]."\t";
$contents.=$row[11]."\t";
$contents.=$row[2]."\t";
$contents.="\t";
$contents.="\t";
$contents.=$row[5]."\t";
$contents.=$row[5]."\t";
$contents.="\n"; 


    
}
		$mgr_total += $row[2];
		$mgr_mba += $row[3];
		$mgr_strbtnml += $row[4];
		$mgr_strbtspl += $row[5];
		$mgr_redct += $row[6];
		$mgr_befrtx += $row[7];
		$mgr_taxamt += $row[8];
		$mgr_nettot += $row[9];

		$mtot_total += $row[2];
		$mtot_mba += $row[3];
		$mtot_strbtnml += $row[4];
		$mtot_strbtspl += $row[5];
		$mtot_redct += $row[6];
		$mtot_befrtx += $row[7];
		$mtot_taxamt += $row[8];
		$mtot_nettot += $row[9];
		$i++;
	}


if($redtype=='mda')
{

    $contents.="Grand Total \t";
$contents.="\t";
$contents.="\t";
$contents.=number_format($mtot_total,2)."\t";
$contents.=number_format($mtot_mba,2)."\t";
$contents.="\t";
$contents.="\t";
$contents.=number_format($mtot_mba,2)."\t";
$contents.="\n";


}

if($redtype=='strbtnml')
{
 $contents.="Grand Total \t";
$contents.="\t";
$contents.="\t";
$contents.=number_format($mtot_total,2)."\t";
$contents.="\t";
$contents.=number_format($mtot_strbtnml,2)."\t";
$contents.="\t";
$contents.=number_format($mtot_strbtnml,2)."\t";
$contents.="\n";

}
if($redtype=='strbtspl')
{
$contents.="Grand Total \t";
$contents.="\t";
$contents.="\t";
$contents.=number_format($mtot_total,2)."\t";
$contents.="\t";
$contents.="\t";
$contents.=number_format($mtot_strbtspl,2)."\t";
$contents.=number_format($mtot_strbtspl,2)."\t";
$contents.="\n";


  }
$contents = strip_tags($contents);
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
print $contents;
	
	?>

