<?
session_start();

if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); document.location='ind.php' </script>";
  return;
}

include("test.php");
include ("opendb.php");
include('includes/top.php'); 
//print "<center>$branch";

?>
<br><br>
<center><font color='white'><b>Debtors Transactions</b></font> </center>

<style>
 td{font-family:'arial narrow';font-size:10;color:black;}
// th{font-family:'arial narrow';font-size:14;color:white;background-color:#5b5858}
// body{background-color:ivory}
</style>

<script>
function show()
{
window.open("prndrfull.php?df=<?print$df?>&dt=<?print$dt?>");
}
</script>
<center>


<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<link type="text/css" href="css/form1.css" rel="stylesheet" />
<?
$res_led=mysqli_query($db,"select * from ledger where grp like 'sundry cred%'  and brcode='$brcode' order by led");
while($led_row=mysqli_fetch_array($res_led))
{
$tr=0;
$tp=0;
$sql="select * from ledger where led='$led_row[led]' and brcode='$brcode'";
//print $sql;
?>


<?
$re=mysqli_query($db,$sql);
$opb=mysqli_fetch_array($re)or die('Err1'.mysqli_error($db));

 $sq="select sum(dramt),sum(cramt) from journal2 where partic='$led_row[led]' and brcode='$brcode'";
// print $sq;
  $chktr=mysqli_query($db,$sq);
  $chktrrow=mysqli_fetch_row($chktr);
  if($chktrrow[0]==0 && $chktrrow[1]==0 && $opb[op]==0)
    {print "</table><br>"; continue;}

?>
<br>
<table  cellspacing=0 cellpadding=2 width=100%>
<th colspan=12><font face='arial narrow' size=2 ><b><? print $led_row[led]?> </font>
</table><hr>
<table  cellspacing=0 cellpadding=2 width=100%>

<tr>
<th>|<th width><font size=2 color='white'>Date <th>|<th width><font size=2 color='white'>Vouch/Rec. No. <th>|<th><font size=2 color='white'>Debit <th>|<th><font size=2 color='white'>Credit <th>|<th><font size=2 color='white' >Ref.<th>| 
<?


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

}

$sql="select * from journal2 where brcode='$brcode' and partic='$led_row[led]' 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)
    {
 print "\n<tr bgcolor='#ffffff' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>|</td><td>".datetohtml($row[date])."</td><td>|</td><td> &nbsp;&nbsp;  To $row[narr]-($row[j_type])</td><td>|</td><td align=right></td><td>|</td><td align=right>".putComa($row[cramt])."</td><td>|</td> <td>$row[against]:$row[no]</td><td>|</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)
  {
 print "\n<tr bgcolor='#ffffff' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>|</td><td>".datetohtml($row[date])."</td><td>|</td><td> By $row[narr]-($row[j_type])</td><td>|</td><td align=right>".putComa($row[dramt])."</td><td>|</td><td align=right></td> <td>|</td><td>$row[against]:$row[no]</td><td>|</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>|</td><td colspan=3 align=center><font color=white><B>Total</td><td>|</td>";
print "<td align=right><font color=white>".putComa($tr)."</td><td>|</td>";
print "<td align=right><font color=white>".putComa(abs($tp))."</td><td colspan=2></td><td>|</td></tr>";

$bal=diff($tr,$tp);

//print "$tr-$tp = bal=".$bal." <br>- ".($tr-$tp);
//*******************************************************
// if(strtoupper($opb[drcr])=="DR" && $opb[op]<0)
// 	$bal*=-1;
  //$bal=number_format($bal,2,'','');
//$bal=number_format($bal,2,'.',','); //,2,'','');
//********************************************************
if($bal>0)
print "\n<tr><td>|</td><td colspan=3 align=center><b><font color=white>Balance</td><td>|</td><td align=right><font color=white><b>".putcoma(abs($bal))."</td><td>|</td><td><font color=white><b> Debit </td><td>|</td><td colspan=1></td><td>|</td></tr>";
else
print "\n<tr><td>|</td><td colspan=3 align=center><b><font color=white>Balance</td><td>|</td><td align=right><font color=white><b>".putcoma(abs($bal))."</td><td>|</td><td><font color=white> <b>Credit </td><td>|</td><td colspan=1></td><td>|</td></tr>";

print "</table><hr>";

}


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


function diff($x,$y)
{
 $difres=mysqli_query($db,"select ($x-$y)");
 $difrow=mysqli_fetch_row($difres);
 return $difrow[0];
}
?>
-END-
