<?session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
$cd = $_REQUEST['cd'];?>
<link href="css/printline.css" type="text/css" rel="stylesheet" media="screen"  />
<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>
<center>
<font face='arial narrow' size=3 >Transaction Details of <b><? print $cd?> </font></center>
<table  cellspacing=1 cellpadding=1 width=100% class="grid">
<th width=80>Date <th>Vouch/Rec. No. <th>Actual amt <th>Outstanding Amt<th>Ref<th>Cleared On
<?
$opbbb=mysqli_fetch_array($re11);
$grp=$opbbb[grp];
$re111=mysqli_query($db,"select desig from bruser where username='$user' and branch='$brcode'");

$opbbb1=mysqli_fetch_array($re111);
$grp1=$opbbb1[desig];

$adj_amt=0;

if(($grp1!='ccn' )||($brcode != 'PSN236'))
{

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

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

	$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);

            $r=mysqli_query($db,"select sum(amt) from ledger_edit where brcode='$brcode' and partic='$cd' and sub_jvno='opb'");
               $re=mysqli_fetch_row($r);

               $r1=mysqli_query($db,"select sum(amt) from ledger_edit where brcode='$brcode' and partic='$cd' and jvno='opb'");
               $re1=mysqli_fetch_row($r1);  
                $adj_amt=$re1[0];
 
                $act_amt=$opb['op']-$r[0];

                $act_amt1=$act_amt-$re[0];
       print "\n<tr style={background-color:silver}><td></td> <td>Opening balance</td>";

	if(strtoupper($opb[drcr])=="DR")
	{
		
                     
                      
                      
                      $out_amt=$act_amt1-$adj_amt;

                      print "<td>".$act_amt."</td><td>".$out_amt."</td>";
	}
	else if(strtoupper($opb[drcr])=="CR")
	{
		        	
           
                       $out_amt=$adj_amt-$act_amt;

                      print "<td>-(".$act_amt.")</td><td>".$out_amt."</td>";
	}
       
         print "<td colspan='2'></td></tr>";
         
	$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))
	{
       
               $r=mysqli_query($db,"select sum(amt) from ledger_edit where brcode='$brcode' and partic='$cd' and sub_jvno='$row[jvno]'");
               $re=mysqli_fetch_row($r);

               $r1=mysqli_query($db,"select sum(amt) from ledger_edit where brcode='$brcode' and partic='$cd' and jvno='$row[jvno]'");
               $re1=mysqli_fetch_row($r1);  
                $adj_amt=$re1[0];

               print "<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".$row[date]."</td><td>  <a href='viewtrans.php?code=$row[jvno]' title='Ref:$row[no]'> To $row[narr]-($row[j_type])</a></td>";

  		 if($row[type]=='Cr') 
    	        {
			 
                     $act_amt=$row[cramt]-$r[0];
                      $act_amt1=$act_amt-$re[0];

                     $out_amt=$adj_amt-$act_amt1;
                
                     print "<td>-(".$act_amt.")</td><td>(".$out_amt.")</td>";
                     	
  		 }
 		else 
  		{
		       $act_amt=$row[dramt]-$r[0];	
                      $act_amt1=$act_amt-$re[0];
           
                      $out_amt=$act_amt1-$adj_amt;

                    print "<td>".$act_amt."</td><td>".$out_amt."</td>";
 		}

             
                   print "<td>$row[mode]</td><td>$row[narr]</td></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></td>";
	print "<td align=right><font color=black></td><td colspan=3></td></tr>"; */


	/* $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];
     		else
			$bal=($balrow[1]+abs($opb[op]))-$balrow[0]; 
 		}
 	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></table>";
 */
  }

?>

