<?
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;
}
include_once('testtot.php');
$df=$_REQUEST['dfrom'];
$dt=$_REQUEST['dto'];

$df=implode('-', array_reverse(explode('-', $df)));
$dt=implode('-', array_reverse(explode('-', $dt)));
$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);
$brnchnm= $brrow[1];
$query1 = "SELECT * FROM ledger where brcode = '$brcode' and grp = 'Sundry Debtors' order by led";
$result = mysqli_query($db,$query1);
$mfrom_date = $df;
  $mto_date= $dt;
  //$df = implode('-', array_reverse(explode('-', $df)));
 // $dt = implode('-', array_reverse(explode('-', $dt)));
$filename ="dbtrpt_grws.xls";
$contents.="Debtor Name \t Opening Balance \t Purchase Amount \t Collection Amount By Cash \t Collection Amount By Bank \t Closing Balance \n";

$i=1;
$mtop_bal=0;
$mtdramt=0;
$mtcramt=0;
$mtbnk_amt=0;
$mtcl_bal=0;
while($row=mysqli_fetch_array($result))
{
  $mjour = "select sum(dramt),sum(cramt) from journal2 where partic = '$row[0]' and date < '$df' ";
  
  $jrow = mysqli_fetch_array(mysqli_query($db,$mjour));
  $mdramt = $jrow[0];
  $mcramt = $jrow[1];
  $mop_bal = $row[2]+$mdramt-$mcramt;
  $mjour = "select sum(dramt),sum(cramt) from journal2 where partic = '$row[0]' and date >= '$df' and date <= '$dt' and j_type = 'BR'";
  
  $jrow1 = mysqli_fetch_array(mysqli_query($db,$mjour));
  $mdramt = $jrow1[0];
  $mbnk_amt = $jrow1[1];
  $mcl_bal = $mop_bal+$mdramt-$mbnk_amt;
  $mjour = "select sum(dramt),sum(cramt) from journal2 where partic = '$row[0]' and date >= '$df' and date <= '$dt' and j_type !='BR' ";
  //echo $mjour;
  //echo "<br>";
  $jrow1 = mysqli_fetch_array(mysqli_query($db,$mjour));
  $mdramt = $jrow1[0];
  $mcramt = $jrow1[1];
  $mcl_bal = $mcl_bal+$mdramt-$mcramt;
  /*
  echo $mop_bal;
  echo "<br>";
  echo $mdramt;
  echo "<br>";
  echo $mcramt;
  echo "<br>";
  echo $mbnk_amt;
  echo "<br>";
  echo $mcl_bal;
  echo "<br>";
  */
  if (abs($mop_bal)>0  || abs($mdramt)>0 || abs($mcramt)>0 || abs($mbnk_amt)>0 || abs($mcl_bal)>0)
  {
      //echo $row[0];
  //echo "<br>";
	  //$mnum = number_format($mop_bal, 2, '.', '');
	  
	  $contents.=$row[0]." \t".number_format($mop_bal, 2, '.', '')." \t ".number_format($mdramt, 2, '.', '')." \t".number_format($mcramt, 2, '.', '')."\t ".number_format($mbnk_amt, 2, '.', '')." \t ".number_format($mcl_bal, 2, '.', '')." \n";

           
  }
  $mtop_bal+=$mop_bal;
  $mtdramt+=$mdramt;
  $mtcramt+=$mcramt;
  $mtbnk_amt+=$mbnk_amt;
  $mtcl_bal+=$mcl_bal;
  //print "<td>".$i++."</td><td><a href='vmledger-test.php?cd=".$row[0]."&sea=$sea'>$row[0]</a></td><td>$row[1]</td><td align=right>".putComa($row[2])."</td><td align=right>$row[3]</td>";
  //print "</tr>";
}














	  
	  
	  
	  
	  









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

?>
