<?
//include_once('includes/top.php');
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['fromdt'];
  $todt=$_REQUEST['todt'];
  $mfrom_date = $fromdt;
  $mto_date = $todt;
  $fromdt = implode('-', array_reverse(explode('-', $fromdt)));
  $todt = implode('-', array_reverse(explode('-', $todt)));
$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);
$brnchnm= $brrow[1];
$filename ="branch_repo_exl.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 Report for the period from ".$mfrom_date."to".$mto_date."\n";
$mgrp = "";
  $mgrp1[]="";
  $mtotal1[]=0;

  //$qry=mysqli_query($db,"select invdate,refno,invno,customer,gname,sum(total),mba,comision,discount,taxamt,mrgin,pfchrgs,othr_chrgs from brtransferout where invdate between '$fromdt' and '$todt' and brcode='$brcode' group by invno,gname");
       $qry=mysqli_query($db,"select gname,invdate,sum(total),mba,comision,mrgin,pfchrgs,othr_chrgs,sum(taxamt),invno,tax,customer,refno,refdt,discount from brtransferout where invdate between '$fromdt' and '$todt' and brcode='$brcode' group by invdate,invno,gname");
//print "select gname,invdate,sum(total),mba,comision,mrgin,pfchrgs,othr_chrgs,sum(taxamt),invno,tax,customer,refno,refdt,discount from brtransferout where invdate between '$fromdt' and '$todt' and brcode='$brcode' group by invdate,invno";   

$contents.= "\t GRN No\tSupplier \tSupplier Inv No \t Entry Date \t Amount\t MDA\tCommission \tDiscount \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];

                        $marginamt=$row[2]*$row[5]/100;
                 $mnet_tot = $row[2]-$row[3]-$row[4]-$row[14]+$marginamt+$row[6]+$row[7];
    $dt = implode('-', array_reverse(explode('-', $row[1])));

	

$contents.= "\t". $row[9] ."\t".$mcust." \t". $row[12]."\t". $dt."\t". $row[2]."\t". $row[3]."\t ".$row[4]."\t". $row[14]." \t".$marginamt."\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_disc+=$row[14];
    $mgr_mrgin += $row[2]*$row[5]/100;
    $mgr_pfchrgs += $row[6];
    $mgr_othr_chrgs += $row[7];
    $i++;
  }

$contents.= "\t \tTOTAL \t\t\t".$mgr_total." \t". $mgr_mba."\t". $mgr_comision."\t". $mgr_disc."\t". $mgr_mrgin."\t ".$mgr_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');
?>