<?
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['df'];
  $todt=$_REQUEST['dt'];
  $tbl='brtransout'.$brcode.$user;
  $mfrom_date = $fromdt;
  $mto_date = $todt;
  $fromdt=implode('-', array_reverse(explode('-', $fromdt)));
  $todt=implode('-', array_reverse(explode('-', $todt)));
  mysqli_query($db,"delete from $tbl");
  mysqli_query($db,"create table $tbl(gname varchar(50),total double(14,2),mba double(14,2),strbtnml double(14,2),strbtspl double(14,2),redct double(14,2),befrtx double(14,2),taxamt double(14,2),nettot double(14,2),dbttot double(14,2))");
  $qry=mysqli_query($db,"select gname,invdate,sum(total),sum(mba),sum(comision),sum(mrgin),sum(pfchrgs),sum(othr_chrgs),sum(taxamt),invno,tax,customer from brtransferout where invdate between '$fromdt' and '$todt' and brcode='$brcode' group by invdate,invno");
 
$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);
$brnchnm= $brrow[1];
$filename ="stax_transout_excel.xls";
$contents="\t\t\t\t\tKERALA KHADI & VILLAGE INDUSTRIES BOARD \n";
$contents.= "\t\t\t\t\t\tCENTER NAME : ".$brnchnm." \n";
$contents.= "\t\t\t\t\tBranch Transfer Register for the period from  ".$mfrom_date."  to  ".$mto_date."\n";

$contents.= "\t GRN No\t Branch \t Entry Date \t Amount \t MDA \t Commission \t Margin \t PF Charges \t Other Charges \t Total Amount \n";
$colspan=6;
  $mstr[]='';
  $i=1;
  $mgr_total = 0;
  $mgr_mba = 0;
  $mgr_comision = 0;
  $mgr_mrgin = 0;
  $mgr_pfchrgs = 0;
  $mgr_othr_chrgs = 0;
  $ctr=0;
  $mgr_dbtrs=0; $mgr_dbtrs1=0;
  while($row=mysqli_fetch_array($qry))
    {
    $qry1=mysqli_query($db,"select brname from branch where brcode = '$row[11]'");
    $row1=mysqli_fetch_array($qry1);
    if ($row1)
      $mcust = $row1[0];
    else
      $mcust = $row[11];
//select gname,invdate,sum(total),sum(mba),sum(comision),sum(mrgin),sum(pfchrgs),sum(othr_chrgs),sum(taxamt),invno,tax,customer,refno,refdt from
    $mnet_tot = $row[2]-$row[3]-$row[4]-$row[5]+$row[6]+$row[7];
    $inv_dt=implode('-', array_reverse(explode('-', $row[1])));

		  $contents.= "\t".$row[9]."\t".$mcust." \t".$inv_dt."\t".$row[2]."\t".$row[3]." \t".$row[4]."\t".$row[5]."\t".$row[6]."\t".$row[7]."\t".number_format($mnet_tot,2,".",",")."\n"; 

         $mgr_total += $row[2];
    $mgr_mba += $row[3];
    $mgr_comision += $row[4];
    $mgr_mrgin += $row[5];
    $mgr_pfchrgs += $row[6];
    $mgr_othr_chrgs += $row[7];
    $i++;
  }
  $mnet_tot=$mgr_total-$mgr_mba-$mgr_comision-$mgr_mrgin+$mgr_pfchrgs+$mgr_othr_chrgs;
 
  $contents.= "\t<b> Total \t\t\t".$mgr_total." \t". $mgr_mba."\t". $mgr_comision."\t".$mgr_mrgin." \t". $rmgr_pfchrgs."\t". $mgr_othr_chrgs."\t".number_format($mnet_tot,2,".",",")."\n"; 

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