<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); document.location='ind.php' </script>";
  return;
}
echo "hi";
$cd=$_REQUEST['cd'];
$dt=$_REQUEST['dt'];
$df=$_REQUEST['df'];
include("test.php");
include ("opendb.php");
$cd=str_replace('~','&',$cd);

$re=mysqli_query($db,"select * from ledger where led='$cd' and brcode='$brcode'");
$opb=mysqli_fetch_array($re);
?>
<style>
 td{font-family:'verdana';font-size:14;color:black;}
 th{font-family:'verdana';font-size:14;color:white;background-color:#5b5858}
 body{background-color:ivory}
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
<font face='arial narrow' size=3 >Transaction Details of <b><? print $cd?> </font>
<table  cellspacing=1 cellpadding=1 width=100%>
<th width=80>Date <th>Vouch/Rec. No. <th>Debit <th>Credit <th>Balance<th>Ref. 

<?
$tr=0.0;$tp=0.0;$tr1=0.0;
$sql="select * from ledger where led='$cd' and brcode='$brcode'";
$re=mysqli_query($db,$sql);
$opb=mysqli_fetch_array($re);

if(strtoupper($opb[drcr])=="DR")
{
if($opb[op]>=0)
{
print "\n<tr style={background-color:silver}><td></td> <td>Opening balance</td><td align=right>".putComa($opb[op])."</td><td colspan=4></td></tr>";
$tr+=$opb[op];
$tr1+=$opb[op];
}
else
{
print "\n<tr style={background-color:silver}><td></td> <td>Opening balance</td><td></td><td align=right>".putComa(abs($opb[op]))."</td><td colspan=3></td></tr>";
$tp+=abs($opb[op]);
$tr1+=$opb[op];

}

}
else if(strtoupper($opb[drcr])=="CR")
{
if($opb[op]>=0)
{
print "\n<tr style={background-color:silver}><td></td> <td>Opening balance</td><td></td><td align=right>".putComa($opb[op])."</td><td colspan=4></td></tr>";
$tp+=$opb[op];
$tr1+=-($opb[op]);
}
else
{
print "\n<tr style={background-color:silver}><td></td> <td>Opening balance</td><td align=right>".putComa(abs($opb[op]))."</td><td colspan=3></td></tr>";
$tr+=abs($opb[op]);
$tr1+=$opb[op];
}

}
$sql="select * from journal2 where brcode='$brcode' and partic='$cd' and date>='$df' and date<='$dt' order by date,jvno ";
$result=mysqli_query($db,$sql) or die ('Error'.mysqli_error($db));
while($row=mysqli_fetch_array($result))
{
   if($row[type]=='Cr') //$row[cr]==$cd)
    {
$tr1-=$row[cramt];
 print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row[date])."</td><td>  <a href='multientry/viewtrans.php?code=$row[jvno]' title='Ref:$row[no]'> To $row[narr]-($row[j_type])</a></td><td align=right></td><td align=right>".putComa($row[cramt])."</td><td align=right>".number_format($tr1,2,".",",")."</td>";
// <td>$row[against]:$row[no]</td>";
 if($row[against]=='o_form')
  print "<td><a href='mis/vform.php?fno=$row[no]'>$row[against]:$row[no]</a></td>";
 else
print "<td>$row[no]</td>";
  

//<td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
print "</tr>";
$tp+=$row[cramt];
  }
 else //if($row[dr]==$cd)
  {
$tr1+=$row[dramt];
 print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row[date])."</td><td> <a href='multientry/viewtrans.php?code=$row[jvno]' title='Ref:$row[against] $row[no]'>By $row[narr]-($row[j_type])</a></td><td align=right>".putComa($row[dramt])."</td><td align=right></td><td align=right>".number_format($tr1,2,".",",")."</td>";
 if($row[against]=='o_form')
  print "<td><a href='mis/vform.php?fno=$row[no]'>$row[against]:$row[no]</a></td>";
 else
 print "<td>$row[against]:$row[no]</td>";

//<td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
print "</tr>";
$tr+=$row[dramt];
 }

}
print "<tr style={background-color:silver}><td colspan=2 align=center><font color=black><B>Total</td>";
print "<td align=right><font color=black>".putComa($tr)."</td>";
print "<td align=right><font color=black>".putComa(abs($tp))."</td><td colspan=2></td></tr>";

/*print ($tp-$tr);
$bal=($tr-$tp);
//*******************************************************
// if(strtoupper($opb[drcr])=="DR" && $opb[op]<0)
// 	$bal*=-1;
  //$bal=number_format($bal,2,'','');
//$bal=number_format($bal,2,'.',','); //,2,'','');
//********************************************************
*/
$balres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where brcode='$brcode' and partic='$cd'");
 $balrow=mysqli_fetch_row($balres);

 if($opb[drcr]=='Dr')
 {
    if($opb[op]>=0)
	$bal=($balrow[0]+$opb[op])-$balrow[1]; //print '<br>bal'.$row[2];
     else
	$bal=($balrow[1]+abs($opb[op]))-$balrow[0]; //print '<br>bal'.$row[2];
 }
 else
{
    if($opb[op]>=0)
	 $bal=($balrow[1]+$opb[op])-$balrow[0];
    else
	 $bal=($balrow[0]+abs($opb[op]))-$balrow[1];
}

if($tr>$tp)
print "\n<tr><td colspan=2 align=center><b><font color=black>Balance</td><td align=right><font color=black><b>".putcoma(abs($bal))."</td><td><font color=black><b> Debit </td><td colspan=2></td></tr>";
else
print "\n<tr><td colspan=2 align=center><b><font color=black>Balance</td><td align=right><font color=black><b>".putcoma(abs($bal))."</td><td><font color=black> <b>Credit </td><td colspan=2></td></tr>";




//**************************************************************8888888888888888888888888



function xyz()
{
$sql="select * from actrans where brcode='$brcode' and(dr='$cd' or cr='$cd')  order by dt desc";
$result=mysqli_query($db,$sql) or die (mysqli_error($db));
$re=mysqli_query($db,"select * from ledger where led='$cd' and brcode='$brcode'");
$opb=mysqli_fetch_array($re);

?>
<center>
<font face='arial black'>Transaction Details of <? print $cd?> </font>
<table bgcolor=black cellspacing=1 cellpadding=1 width=100%>
<th>Date <th>Vouch/Rec. No. <th>Debit <th>Credit <th>Mode <th>Narration <th>Action
<?
$tr=0; $tp=0;
if(strtoupper($opb[drcr])=="DR")
{
print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td></td> <td>Opening balance</td><td align=right>$opb[op]</td><td colspan=4></td></tr>";
$tr+=$opb[op];
}
else if(strtoupper($opb[drcr])=="CR")
{
print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td></td> <td>Opening balance</td><td></td><td align=right>$opb[op]</td><td colspan=4></td></tr>";
$tp+=$opb[op];
}

while($row=mysqli_fetch_array($result))
{
if($row[cr]==$cd)
{
 print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row[dt])."</td><td>($row[vouch]) To $row[dr]</td><td align=right></td><td align=right>".putComa($row[amt])."</td> <td>$row[mode]</td><td>$row[narr]</td>";
//<td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
print "</tr>";
$tp+=$row[amt];
}
else if($row[dr]==$cd)
{
 print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row[dt])."</td><td>($row[vouch]) By $row[cr]</td><td align=right>".putComa($row[amt])."</td><td align=right></td> <td>$row[mode]</td><td>$row[narr]</td>";
// <td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
print "</tr>";
$tr+=$row[amt];
}
}
print "<tr><td colspan=2 align=center><font color=black><B>Total</td>";
print "<td align=right><font color=black>".putComa($tr)."</td>";
print "<td align=right><font color=black>".putComa($tp)."</td></tr>";

/*$bal=($tr-$tp);
//*******************************************************

      $bal*=-1;
	//$bal=number_format($bal,2,'.',',');
//********************************************************
*/


if($bal>0)
print "\n<tr><td colspan=2 align=center><b><font color=black>Balance</td><td align=right><font color=black>".putcoma($bal)."</td><td><font color=black> Debit </tr><td>";
else
print "<tr><td colspan=2 align=center><b><font color=black>Balance</td><td align=right><font color=black>".putcoma($bal)."</td><td><font color=black> Credit </tr></td>";
?>


</center>
<?
} // end fnct xyz
?>
</table><br>
<input type=button value="<- Back" onClick=history.back(1)>

 
