<?
session_start();
$brcode = $_SESSION['brcode'];
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); document.location='ind.php' </script>";
  return;
}
$cd = $_REQUEST['ldegr'];
$df = $_REQUEST['dfrom'];
$dt = $_REQUEST['dto'];
include("test.php");
include ("opendb.php");
$id = array();
	$id = $_REQUEST['chk'];
	
    if (count($id) > 0)
  	{
  	    $ids= implode(",", $id);
  	}
//echo $ids;
$re=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brow=mysqli_fetch_array($re);
$df1 = implode('-', array_reverse(explode('-',$df)));
$dt1 = implode('-', array_reverse(explode('-',$dt)));
?>
<body>
<style>
 body,td{font-family:'arial narrow';font-size:14;color:black;background-color:white}
 th{font-family:'arial narrow';font-size:14;color:white;background-color:#5b5858}
// body{background-color:ivory}
</style>
<center>
    <table border='0' class='printable' width='100%'>
	  
	   <tr><td colspan=6 align=center style="font-size: 22px;"<b>PSN AUTOMOBILES PVT LTD</td></tr>
 	   <tr><td colspan=6 align=center><b>GST NO : <?echo $brow['gst']?></td></tr>
	   <tr><td colspan=6 align=center><b><?echo $brow['braddr']?></td></tr>
	   <tr><td colspan=6 align=center><b><?echo $brow['brcity']?></td></tr>
	   <tr><td colspan=6 align=center><b><?echo $brow['brphone']?></td></tr>
	   </table>
<font face='arial narrow' size=3><?print $branch?><br>Transaction Details of <b><? print $cd?> </font>
<?
print " </b><center>Transactions from ".$df1.' to '.$dt1." <br>.</center>";
?>

<table  cellspacing=1 cellpadding=3 border = 1>
<th width=80>Date <th>Vouch/Rec. No. <th>Debit <th>Credit <th>Balance<th>Ref. <th>Jv No.
<?
$tr=0;$tp=0;
$ssq="select sum(dramt)-sum(cramt) from journal2 where brcode='$brcode' and date<'$df' and (partic='$cd')";

$res2=mysqli_query($db,$ssq)or die('jour err:'.mysqli_error());
$r2=mysqli_fetch_row($res2);
$re=mysqli_query($db,"select * from ledger where led='$cd' and brcode='$brcode'");
$opb=mysqli_fetch_array($re);
//print 'Type :'.$opb['drcr'];
if($ids=='0')
{
    $openbal=0;
}
else
{
    
if($opb['drcr']=='Cr' and $r2[0]<0)
{
	$openbal=$opb['op']+abs($r2[0]);
	print "<tr><td></td><td> Opening Balance</td><td></td><td align=right>".putComa($openbal)."</td><td colspan=2></td>";
	$tp+=$openbal;
}
else if($opb['drcr']=='Cr' and $r2[0]>=0)
{
	$openbal=$opb['op']-$r2[0];
	if($openbal>0)
	{
		print "<tr><td></td><td> Opening Balance</td><td></td><td align=right>".putComa($openbal)."</td><td colspan=2></td>";
		$tp+=$openbal;
	}
	else
	{
		print "<tr><td></td><td> Opening Balance</td><td align=right>".putComa(abs($openbal))."</td><td colspan=2></td>";
	$tr+=$openbal;
	}
}


if($opb['drcr']=='Dr' and $r2[0]>=0)
{
	if($opb['op']>=0)
		$openbal= $opb['op'] +abs($r2[0]);
	else
		$openbal= $opb['op']+$r2[0];
	if($openbal>0)
	{
		print "<tr><td></td><td> Opening Balance</td><td align=right>".putComa($openbal)."</td><td colspan=3></td>";
		$tr+=$openbal;
	}
	else
	{
		print "<tr><td></td><td> Opening Balance</td><td>&nbsp;</td><td align=right>".putComa(abs($openbal))."</td><td colspan=3></td>";
		$tp+=abs($openbal);
	}
}
if($opb['drcr']=='Dr' and $r2[0]<0)
{
	if($opb['op']>=0)
		$openbal= $opb['op']+ $r2[0];
	else
		$openbal= $opb['op']+$r2[0];
	print $opb['op'].'-'.$r2[0];
	if($openbal>=0)
    	{
    		print "<tr><td></td><td> Opening Balance</td><td align=right>".putComa($openbal)."</td><td colspan=3></td>";
     		$tr+=$openbal;
     	}
 	else
    	{
    		print "<tr><td></td><td> Opening Balance</td><td></td><td align=right>".putComa(abs($openbal))."</td><td colspan=3></td>";
     		$tp+=$openbal;
     	}

}
}
$mop_bal = abs($tr)- abs($tp);
$sql="select * from journal2 where brcode='$brcode' and partic='$cd' and date>='$df' and date<='$dt'  and jvno NOT IN($ids) order by date,jvno ";
//echo $sql;
$result=mysqli_query($db,$sql) or die (mysqli_error());
//$mop_bal = $openbal;
while($row=mysqli_fetch_array($result))
{
   if($row['type']=='Cr') //$row[cr]==$cd)
    {
	$mop_bal = $mop_bal -  $row['cramt'];
	if($mop_bal<0)
		$mstr = "(".number_format(abs($mop_bal), 2, '.', ',').")";
	else
		$mstr = number_format($mop_bal, 2, '.', ',');
 	print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".implode('-', array_reverse(explode('-',$row['date'])))."</td><td> &nbsp;&nbsp; <a href='multientry/viewtrans.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>".$mstr."</td> <td>$row[against]:$row[no]</td><td>$row[jvno]</td>";
 	
	//<td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
	print "</tr>";
	$totcramt+=$row['cramt'];
  }
  else //if($row[dr]==$cd)
  {

	$mop_bal = $mop_bal +  $row['dramt'];
	if($mop_bal<0)
		$mstr = "(".number_format(abs($mop_bal), 2, '.', ',').")";
	else
		$mstr = number_format($mop_bal, 2, '.', ',');
	$mstr1 = "";
	if($row['j_type']=="V sale")
	{
	    
	    $rech=mysqli_query($db,"select chasis from veh_invoice where invno ='$row[tr_no]'");
        $chbrow=mysqli_fetch_array($rech);
        $mstr1 = "Chasis No. ".$chbrow[0];
	}
 	print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".implode('-', array_reverse(explode('-',$row['date'])))."</td><td> <a href='multientry/viewtrans.php?code=$row[jvno]' title='Ref:$row[against] $row[no]'>To $row[narr]-($row[j_type])$mstr1</a></td><td align=right>".putComa($row['dramt'])."</td><td align=right></td><td align=right>".$mstr."</td> <td>$row[against]:$row[no]</td><td>$row[jvno]</td>";
 

	print "</tr>";
	$totdramt+=$row['dramt'];
  }
}
print "<tr style={background-color:silver}><td colspan=2 align=center><font color=black><B>Total</td>"; 

 $totdramt+=abs($tr);

 $totcramt+=abs($tp);

print "<td align=right><font color=black>".putComa($totdramt)."</td>";
print "<td align=right><font color=black>".putComa(abs($totcramt))."</td><td colspan=3></td></tr>";


if(abs($totdramt)>abs($totcramt))
{
$bal=(abs($totdramt)-abs($totcramt));
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 if(abs($totdramt)<abs($totcramt))
{$bal=(abs($totcramt)-abs($totdramt)); 

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


//*******************************************************
// 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($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></td><td align=right><font color=black><b>".putcoma(abs($bal))."</td><td><font color=black> <b>Credit </td> </tr>";


//**************************************************************8888888888888888888888888
?>
</table><br>

<?
//} // end main if
?>