<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
   print "<script> alert('Invalid User'); parent.document.location='../index.html' </script>";
  return;
}

include("test.php");
include ("opendb.php");
$cd=$_REQUEST['cd'];
$sea=$_REQUEST['sea'];
$mode=$_REQUEST['mode'];
$re=mysqli_query($db,"select * from ledger where led='$cd' and brcode='$brcode'");
$opb=mysqli_fetch_array($re);
?>


<font face='arial narrow' size=3 >Transaction Details of <b><? print $cd?> </font>
<form name='form1' method='POST'>
<table  cellspacing=1 cellpadding=1 width=100%>
<th width=80>Date <th>Vouch/Rec. No. <th>Debit <th>Credit <th>Ref. 
<input type='hidden' name='partynm' value='<? echo $cd ?>'><input type='hidden' name='srchnm' value='<? echo $sea ?>'>
<input type='hidden' name='modvl' value='<? echo $mode ?>'>

<?
$tr=0;$tp=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>$opb[op]</td><td colspan=4></td></tr>";
$tr+=$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]);
}

}
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>$opb[op]</td><td colspan=4></td></tr>";
$tp+=$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]);
}

}
$sql="select * from journal2 where brcode='$brcode' and partic='$cd' 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)
    {
$sql="select * from journal2 where brcode='$brcode'  and jvno='$row[jvno]' and type='Dr'";
//  print $sql;
   $drres1=mysqli_query($db,$sql)or die(mysqli_error($db));
    $drrow1=mysqli_fetch_array($drres1);
 print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row[date])."</td><td> &nbsp;&nbsp; <a href='multientry/viewtrans.php?code=$row[jvno]&&sea=$sea&&mode=$mode&&cd=$cd' title='Ref:$row[no]'><b>$drrow1[partic]</b><br> &nbsp;&nbsp; To $row[narr]-($row[j_type])</a></td><td align=right></td><td align=right>".putComa($row[cramt])."</td> <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>";
$tp+=$row[cramt];
  }
 else //if($row[dr]==$cd)
  {
$sql="select * from journal2 where brcode='$brcode'  and jvno='$row[jvno]' and type='Cr'";

   $drres11=mysqli_query($db,$sql)or die(mysqli_error($db));
    $drrow1=mysqli_fetch_array($drres11);

 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]&&sea=$sea&&mode=$mode&&cd=$cd' title='Ref:$row[against] $row[no]'>By $row[narr]-($row[j_type])<br>&nbsp;&nbsp;<b> ($drrow1[partic])</b></a></td><td align=right>".putComa($row[dramt])."</td><td align=right></td> <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>";
$bal=($tr-$tp);
//*******************************************************
// if(strtoupper($opb[drcr])=="DR" && $opb[op]<0)
// 	$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><b>".putcoma(abs(round($bal,2)))."</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(round($bal,2)))."</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(round($bal,2))."</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(round($bal,2))."</td><td><font color=black> Credit </tr></td>";
?>
</center>
<?
} // end fnct xyz
?>
</table><br>
<input type=button value="<- Back" onClick="prvsrcldg()">
</form>
<script type="text/javascript">
function prvsrcldg()
{
var part=document.form1.partynm.value;
var mdvl=document.form1.modvl.value;
var sn=document.form1.srchnm.value;
var srchvl='Search';
document.location="sealedge.php?cd="+part+"&&sea="+sn+"&&mode="+mdvl+"&&sealed="+srchvl,true;
}
</script>