<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); parent.document.location='../ind.php' </script>";
  return;
}
include_once("../test.php");
$bnk=$_REQUEST['bnk'];
 $y=$_REQUEST['y'];
$m=$_REQUEST['m'];
$d=$_REQUEST['d'];
$y1=$_REQUEST['y1'];
$m1=$_REQUEST['m1'];
$d1=$_REQUEST['d1'];

 $df=$y."-".$m."-".$d;
 $dt=$y1."-".$m1."-".$d1;
?>


<style>
 td{font-family:arial narrow;font-size:14;color:black;}
 th{font-family:arial narrow;font-size:14;color:white;background-color:#5b5858}
 body{background-color:ivory;}
 select
  {border-style:ridge;
   border-color:black;
   height:25px;
   border-size=1;
   font-family:verdana;
   color:black;
  }
 input
  {border-style:ridge;
   border-color:#777777;
   background-color:white;
   height:25px;
   font-weight:bold;
   border-size=1;
   font-family:sans-serif;
   color:BLACK;
  }
</style>

<script>
function show()
{
window.open("prnbbook.php?df=<?print$df?>&dt=<?print$dt?>&bnk=<?print $bnk?>");
}
</script>
<title>Ampl</title>
    
<script type="text/javascript" src="../scripts/jquery-latest.js"></script>
	<link type="text/css" href="../includes/style1.css" rel="stylesheet" />
	<link type="text/css" href="../css/form.css" rel="stylesheet" />
    <script type="text/javascript">
        $(function() {
          if ($.browser.msie && $.browser.version.substr(0,1)<7)
          {
			$('li').has('ul').mouseover(function(){
				$(this).children('ul').show();
				}).mouseout(function(){
				$(this).children('ul').hide();
				})
          }
        });   
        

    </script>
    
    <body background="../back_ground.jpg">
<?php include_once('top.php'); ?>

<br><br>
<font face=arial narrow size=3>
    <table align=center>
       <tr><td align=center>TDS PAYABLE BALANCE <font color=blue> <?print " </font><br>from  ".$df." to ".$dt?></td></tr> 
        
    </table>
    
<table  border='1' width=30% align=center>
<tr font face=arial narrow size=3  ><th font face=arial narrow size=3 width=60><center><b>Branch</th> <th width=30><center><b>Credit </th> <th width=30><center><b>Debit</th> <th width=30><center><b>Balance</th></tr>

<?
 include_once("../opendb.php");
	
$tr=0.00;
$tp=0.00;

//print putcoma(abs($tr-$tp));


$newdate = strtotime ( '+7 day' , strtotime ( $dt ) ) ;
$dt7 = date ( 'Y-m-d' , $newdate );


$trantbl=mysqli_query($db,"SELECT brcode,brname FROM branch where brcode in(select brcode from ledger where led='T D S PAYABLE')  order by brcode");

while($tran=mysqli_fetch_array($trantbl))
{
    
    $ssq11="select sum(cramt) from journal2 where brcode='$tran[0]' and date>='$df' and date<='$dt' and (partic='T D S PAYABLE')";

$result11=mysqli_query($db,$ssq11)or die(mysqli_error($db));

$sq22="select sum(dramt) from journal2 where brcode='$tran[0]' and date>='$df' and date<='$dt7' and partic='T D S PAYABLE' ";

$result122=mysqli_query($db,$sq22)or die(mysqli_error($db));
$row=mysqli_fetch_row($result11);
//print $opbal;
$row1=mysqli_fetch_row($result122);

  //new  op add
$tr=0.00;
$tp=0.00;  
    $sq="select op,drcr from ledger where brcode='$tran[0]' and led='T D S PAYABLE'";
   // print $sq;
$re=mysqli_query($db,$sq) or die(mysqli_error($db));
$opb=mysqli_fetch_array($re) or die(mysqli_error($db));
$opbval=$opb[0];
$drorcr=$opb[1];

$res=mysqli_query($db,"select acfrom from acperiod");
$r1=mysqli_fetch_row($res);

$ssq="select sum(dramt)-sum(cramt) from journal2 where brcode='$tran[0]' and date>='$r1[0]' and date<'$df' and (partic='T D S PAYABLE')";

$res2=mysqli_query($db,$ssq)or die(mysqli_error($db));
$r2=mysqli_fetch_row($res2);

if($drorcr=='Cr' and $r2[0]<0)
{
 $opbal=($opbval+abs($r2[0]));
 $ttopb=$row[0]+$opbal;
}
else if($drorcr=='Cr' and $r2[0]>0)
 {
     $opbal=($opbval-$r2[0]);
 $ttopb=$row[0]+$opbal;
 }
else if($drorcr=='Dr' and $r2[0]>0)
{
 $opbal=($opbval+$r2[0]);
 $ttopb=$row[0]+$opbal;
}
else if($drorcr=='Dr' and $r2[0]<0)
{
 $opbal=($opbval+$r2[0]);
 $ttopb=$row[0]+$opbal;
}
else
 {$opbal=$opbval;
 $ttopb=$row[0]+$opbal;
 }
$sq="select distinct date from journal2 where brcode='$tran[0]' and date>='$df' and date<='$dt' and partic='T D S PAYABLE' order by date";

$result1=mysqli_query($db,$sq)or die(mysqli_error($db));

$head=1;
$actype='';

if($opbal>0 && strtoupper($opb[drcr])=="DR")
{
//print "<tr ><td></td><td colspan=2><font color=green>To Opening Balance</td><td align=right><font color=green>".number_format(abs($opbal),2,".",",")."</td><td></td><td></td></tr>";
$tr+=$opbal;
$actype='DR';
}
else if($opbal>0 && strtoupper($opb[drcr])=="CR")
{
//print "<tr ><td></td><td colspan=2><font color=green>To Opening Balance</td>";
//print "<td></td><td align=right><font color=red>".number_format(abs($opbal),2,".",",")."</td><td></td><td></td></tr>";
$tp+=$opbal;
$actype='CR';
}
else if($opbal<0 && strtoupper($opb[drcr])=="CR")
{
//print "<tr ><td></td><td colspan=2><font color=green>To Opening Balance</td>";
//print "<td align=right><font color=red>".number_format(abs($opbal),2,".",",")."</td><td></td><td></td><td></td></tr>";
$tr+=abs($opbal);
$actype='CR';
}
else if($opbal<0 && strtoupper($opb[drcr])=="DR")
{
//print "<tr bgcolor=white><td></td><td colspan=2><font color=green>To Opening Balance</td><td></td><td align=right><font color=red>".number_format(abs($opbal),2,".",",")."</td><td></td><td></td></tr>";
$tp+=abs($opbal);
$actype='DR';
}
$opbal=0;
//  neww end  ##########################



//$baltds=$row[0]-$row1[0];
$baltds=$ttopb-$row1[0];

 print "<tr><td>$tran[1]</td><td align=right><font color=blue>".number_format($ttopb,2,".",",")."</td><td align=right><font color=blue>".number_format($row1[0],2,".",",")."</td><td align=right><font color=blue>".number_format($baltds,2,".",",")."</td></tr>";

}
$tr=0;
$tp=0;
print"</table>";


//$opbal=$closebal;



?>

