<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
   print "<script> alert('Invalid User'); document.location='inde.php' </script>";
   return;
}
include("opendb.php");
include("test.php");
?>
<link href="css/form.css" type="text/css" rel="stylesheet" media="screen" />
<? include('includes/top.php'); ?>
<link rel="stylesheet" href="cal/jquery-ui.css" />
    <script src="cal/jquery-1.9.1.js"></script>
    <script src="cal/jquery-ui1.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css" />

<body bgcolor='lavender'>
<form name='f1' id='f1' action='<?print $PHP_SELF?>' method='POST' onSubmit="return valid()">
<br>
<CENTER>
<table border=1 cellspacing=1 cellpadding=1 class=report>
<tr bgColor='c1d7bb'><td colspan=7 align=center bgcolor='8A8D89'>
</TD></TR>
<tr><td bgcolor="8A8D89" align='left'><font color='ivory'><b></b></td><td bgcolor="8A8D89" align='left'><font color='ivory'><b></b></td></tr>
<?
$qry=mysqli_query($db,"select distinct grp from  ledger order by grp ASC");
?>
<tr>
<td>Ledger Group</td>
<td><select name='jtype'>
<option value='select'>-select-</option>
<?
while($row=mysqli_fetch_array($qry))
{
?>
       <option value='<? echo $row['grp'] ?>'><? echo $row['grp'] ?></option>
<?
}
?>
</select>
</td></tr>
<tr><td> To </td>
    <td>dd<input type=text name=d1 size=1 value='<?print date("d",time())?>'>
  mm<input type=text name=m1 size=1 value='<?print date("m",time())?>'>
  yyyy<input type=text name=y1 size=3 value='<?print date("Y",time())?>'>
    </td>
</tr>
<tr><td colspan=6 align=center bgcolor='8A8D89'>
<input type=submit value='SHOW' name='sprint'>
<input type=reset value='CANCEL'></td></tr>
</td></tr>
</table>
</center>
<?
if(isset($_POST['sprint']))

{
    $y1=$_REQUEST['y1'];
$m1=$_REQUEST['m1'];
$d1=$_REQUEST['d1'];

 
 $dt=$y1."-".$m1."-".$d1;
 
   $jtype = $_REQUEST['jtype'];
   $tblgrpbal=$user."grpbal".$brcode;
mysqli_query($db,"drop table $tblgrpbal");
   mysqli_query($db,"CREATE TABLE $tblgrpbal select * from ledger where grp='$jtype' ");
   print" <table border=0 cellspacing=1 cellpadding=1 bgcolor=black align=center >
<tr bgcolor='silver'><td width=5><b>No.</td><td width=300><b>LEDGER </td><td><b>GROUP</td><td>Plant</td><td><b>Op Bal</td><td><b>Closing Bal</td><td><b>Type</td><td><b>Branch</td><td>Code</td><td>LedgerBalance</td><td>Balance</td></tr>";
$i=1;
  $brres=mysqli_query($db,"select * from branch  order by brname ");
 while($brrow=mysqli_fetch_array($brres))
 {
    // print "<center>Branch : $brrow[1]";
  

$result=mysqli_query($db,"select distinct led,grp,drcr,op from $tblgrpbal where grp='$jtype' and brcode='$brrow[0]' order by led")or die(mysqli_error($db));



while($row=mysqli_fetch_array($result))
{
print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'>";

$balres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where brcode='$brrow[0]' and partic='$row[0]' and date<='$dt' ");
		$balrow=mysqli_fetch_row($balres);
		if($row['drcr']=='Dr')
 		{
			$bal=($balrow[0]+$row[3])-$balrow[1]; //print '<br>bal'.$row[2];
		}
 		else
 		{
 			$bal=$balrow[1]+$row[3]-$balrow[0];
 		}

$r=mysqli_query($db,"select brcode from branch where brled='$row[0]'")or die(mysqli_error($db));
$ro=mysqli_fetch_array($r);

if(mysqli_num_rows($r)>0)
{
  
   
    
 
     
    $re=mysqli_query($db,"select brled from branch where brcode='$brrow[0]'")or die(mysqli_error($db));
  $roe=mysqli_fetch_array($re);


  $rww=mysqli_query($db,"select distinct led,grp,drcr,op from $tblgrpbal where brcode='$ro[0]' and led='$roe[0]'")or die(mysqli_error($db));
    $rtt=mysqli_fetch_array($rww);
    

$balree=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where brcode='$ro[0]' and partic='$roe[0]' and date<='$dt' ");

		$balroo=mysqli_fetch_row($balree);

		if($rtt['drcr']=='Dr')
 		{
			$bal2=($balroo[0]+$rtt[3])-$balroo[1]; //print '<br>bal'.$row[2];
		}
 		else
 		{
 			$bal2=$balroo[1]+$rtt[3]-$balroo[0];
 			
 		}
 		$bal2=-$bal2;
 		$balance=$bal-$bal2;
}


?>



<td><? echo $i++?></td><td><a href='vmledgerdet2.php?cd=<? echo str_replace('&','-',$row[0]) ?>'><?print $row[0] ?></a></td><td><? print $row[1]?></td><td><? print $brrow[0]?></td><td align=right><? print $row[3]?></td><td align=right><? echo number_format($bal,2,'.',',') ?></td><td><? print $row[2]?></td><td><? print $brrow[1]?></td><td><? print $ro[0]?></td><td align=right><? echo number_format($bal2,2,'.',',') ?></td><td align=right><? echo number_format($balance,2,'.',',') ?></td></tr>
<?
$bal2=0;
$balance=0;

//<td><input type=button value='Edit' onClick=show('".str_replace(' ','-',$row[0])."')></tr>";
}
}
print "</table><br>";
 
} // end save

?>
