<?
session_start();
include_once('includes/top.php');
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$cd=$_REQUEST['cd'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
?>
<html>
<body>
<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 type="text/css" href="includes/style1.css" rel="stylesheet" />
<link type="text/css" href="css/form1.css" rel="stylesheet" />
<center>
<font face='arial narrow' size=3 >Transaction Details of <b><? print $cd?> </font></center>
<form action='<?print $PHP_SELF?>'>

<input type=hidden name=cd value='<?print $cd?>'>
<input type=button value="<- Back" onClick=history.back(1)>


<table  cellspacing=1 cellpadding=1 width=100% class="grid">
<th width=80>Date <th>Vouch/Rec. No. <th>Debit <th>Credit <th>Balance

<?
include("test.php");
include ("opendb.php");
$re11=mysqli_query($db,"select * from spledgermaster where led='$cd' and brcode='$brcode' ");
$opbbb=mysqli_fetch_array($re11);
$grp=$opbbb['grp'];
$re111=mysqli_query($db,"select desig from bruser where username='$user' and brcode='$brcode'");

$opbbb1=mysqli_fetch_array($re111);
$grp1=$opbbb1['desig'];
//print $grp1;
if(($grp1!='ccn' )||($brcode != 'PSN236'))
{

$cd=str_replace('~','&',$cd);

$re=mysqli_query($db,"select * from spledgermaster where led='$cd' and brcode='$brcode' ");
$opb=mysqli_fetch_array($re);

$tr=0.0;$tp=0.0;$tr1=0.0;
 $sql="select * from spledgermaster 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>".number_format($opb['op'],2,".",",")."</td><td colspan=3></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>".(number_format(abs($opb['op']),2,".",","))."</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>".number_format($opb['op'],2,".",",")."</td><td colspan=3></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>".(number_format(abs($opb['op']),2,".",","))."</td><td colspan=3></td></tr>";
$tr+=abs($opb['op']);
$tr1+=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)
    {
$tr1-=$row['cramt'];
$tp+=$row['cramt'];

$s=$tr-$tp;
 print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row['date'])."</td><td>  <a href='viewtranssap.php?code=$row[jvno]' title='Ref:$row[no]'> By $row[narr]-($row[j_type])</a></td><td align=right></td><td align=right>".putComa($row['cramt'])."</td><td align=right>".number_format($s,2,".",",")."</td>";
// <td>$row[against]:$row[no]</td>";
$sql="select * from breco where brcode='$brcode' and jvno='$row[jvno]'";
$resu=mysqli_query($db,$sql) or die ('Error'.mysqli_error($db));
$r=mysqli_fetch_array($resu);

/*
 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>";
print "<td>".datetohtml($r[2])."</td>";
  */

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

$s=$tr-$tp;
if($brcode!='37881')
{
    $mnarr = $row['narr'];
   /* if(substr($mnarr,0,2)=='C.')
    {
        $sql22="select narr from journal2 where brcode='$brcode' and jvno='$row[jvno]' and left(narr,2) != 'C.' limit 1";
        $resu22=mysqli_query($db,$sql22) or die ('Error'.mysqli_error($db));
        $r22=mysqli_fetch_array($resu22);
        $mnarr = $r22[0];    
    }*/
    
    /*if(preg_match('/BR:/',$mnarr))
    {
        $sql22="select narr from journal2 where brcode='$brcode' and jvno='$row[jvno]' and type='cr' limit 1";
        $resu22=mysqli_query($db,$sql22) or die ('Error'.mysqli_error($db));
        $r22=mysqli_fetch_array($resu22);
        $mnarr = $r22[0];    
    }*/
    print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row['date'])."</td><td> <a href='viewtranssap.php?code=$row[jvno]' title='Ref:$row[against] $row[no]'>To $mnarr-($row[j_type])</a></td><td align=right>".putComa($row['dramt'])."</td><td align=right></td><td align=right>".number_format($s,2,".",",")."</td>";
}
else   
{
    print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row['date'])."</td><td> <a href='viewtranssap.php?code=$row[jvno]' title='Ref:$row[against] $row[no]'>To $row[narr]-($row[j_type])</a></td><td align=right>".putComa($row['dramt'])."</td><td align=right></td><td align=right>".number_format($s,2,".",",")."</td>";
}    
$sql="select * from breco where brcode='$brcode' and jvno='$row[jvno]'";
$resu=mysqli_query($db,$sql) or die ('Error'.mysqli_error($db));
$r=mysqli_fetch_array($resu); 
/*
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>";
print "<td>".datetohtml($r[2])."</td>";
*/
//<td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
print "</tr>";
 }

}
print "<tr style={background-color:silver}><td colspan=2 align=center><font color=black><B>Total</td>";
print "<td align=right><font color=black>".number_format($tr,2,".",",")."</td>";
print "<td align=right><font color=black>".number_format(abs($tp),2,".",",")."</td><td colspan=3></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>".number_format(abs($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>".number_format(abs($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 spledgermaster 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>".($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>".($tr)."</td>";
print "<td align=right><font color=black>".($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)>

<?
} // end fnct xyz
elseif($grp=='Sundry Debtors')

{
$cd=str_replace('~','&',$cd);

$re=mysqli_query($db,"select * from spledgermaster where led='$cd' and brcode='$brcode'");
$opb=mysqli_fetch_array($re);

$tr=0.0;$tp=0.0;$tr1=0.0;
$sql="select * from spledgermaster 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>5Opening balance</td><td align=right>".(round($opb['op'],3))."</td><td colspan=4></td></tr>";
$tr+=$opb['op'];
$tr1+=$opb['op'];
}
else
{
print "\n<tr style={background-color:silver}><td></td> <td>2Opening balance</td><td></td><td align=right>".(abs(round($opb['op'],3)))."</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>4Opening balance</td><td></td><td align=right>".(round($opb['op'],3))."</td><td colspan=3></td></tr>";
$tp+=$opb['op'];
$tr1+=-($opb['op']);
}
else
{
print "\n<tr style={background-color:silver}><td></td> <td>1Opening balance</td><td align=right>".(abs(round($opb['op'],3)))."</td><td colspan=3></td></tr>";
$tr+=abs($opb['op']);
$tr1+=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)
    {
$tr1-=$row['cramt'];
$tp+=$row['cramt'];

$s=$tr-$tp;
 print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row['date'])."</td><td>  <a href='viewtranssap.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($s,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>";
  }
 else //if($row[dr]==$cd)
  {
$tr1+=$row['dramt'];
$tr+=$row['dramt'];

$s=$tr-$tp;
print "oooo";
 print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row['date'])."</td><td> <a href='viewtranssap.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($s,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>";
 }

}
print "<tr style={background-color:silver}><td colspan=2 align=center><font color=black><B>Total</td>";
print "<td align=right><font color=black>".($tr)."</td>";
print "<td align=right><font color=black>".(abs($tp))."</td><td colspan=3></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,'','');
//******************************************************** s200-24819
*/
$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>".round(abs($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>".round(abs($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 spledgermaster 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>".($tr)."</td>";
print "<td align=right><font color=black>".($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>".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>".round($bal,2)."</td><td><font color=black> Credit </tr></td>";
?>


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

<?

}
?>

 </form>
