<?
include_once('includes/top.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$user_level=$_SESSION['user_level'];
include_once("opendb.php");
$sea=$_REQUEST['sea'];
$y=$_REQUEST['y'];
$m=$_REQUEST['m'];
$d=$_REQUEST['d'];
$y1=$_REQUEST['y1'];
$m1=$_REQUEST['m1'];
$d1=$_REQUEST['d1'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
} 

	$query1 = "SELECT * FROM khadistaff where brcode = '$brcode' order by cname asc";
	
?>
<head>

<title>Khadi</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>
<link href="css/pagination.css" rel="stylesheet" type="text/css" />
<link href="css/grey.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.paginate {
	font-family:Arial, Helvetica, sans-serif;
	padding: 3px;
	margin: 3px;
}

.paginate a {
	padding:2px 5px 2px 5px;
	margin:2px;
	border:1px solid #999;
	text-decoration:none;
	color: #666;
}
.paginate a:hover, .paginate a:active {
	border: 1px solid #999;
	color: #000;
}
.paginate span.current {
    margin: 2px;
	padding: 2px 5px 2px 5px;
		border: 1px solid #999;
		
		font-weight: bold;
		background-color: #999;
		color: #FFF;
	}
	.paginate span.disabled {
		padding:2px 5px 2px 5px;
		margin:2px;
		border:1px solid #eee;
		color:#DDD;
	}      
</style>
</head>
<body>
<table class="container">
<table border=0 width=30% class='form' align="center">
<?
if (date('m',time())<4)
	$myear = (date('Y',time())-1);
else
	$myear = (date('Y',time()));
if($d > 0)
{
	print "<form name='f1' action='$PHP_SELF'><tr><td>Date from : dd<input type=text name='d' size=2 value='".$d."'>mm<input type=text name='m' size=2 value='".$m."'>yy<input type=text name='y' size=4 value='".$y."'></td></tr>";
	print "<tr><td>To &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: dd<input type=text name='d1' size=2 value='".$d1."'> mm<input type=text name='m1' size=2 value='".$m1."'>yy<input type=text name='y1' size=4 value='".$y1."'<input type=submit value='Show' name='show'><input type=submit value='Show' name='prn' ></td></tr></form>";
	}
else
{
	print "<form name='f1' action='$PHP_SELF'><tr><td>Date from : dd<input type=text name='d' size=2 value='1'>mm<input type=text name='m' size=2 value='04'>yy<input type=text name='y' size=4 value='".$myear."'></td></tr>";
	print "<tr><td>To &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: dd<input type=text name='d1' size=2 value='".date('d',time())."'> mm<input type=text name='m1' size=2 value='".date('m',time())."'>yy<input type=text name='y1' size=4 value='".date('Y',time())."'<input type=submit value='Show' name='show'><input type=submit value='Show' name='prn' onclick='billexc()' ></td></tr></form>";
	}		
?>
</table>
    <br>
<table border=0 cellspacing=1 cellpadding=1 class='report' >
    <tr><td colspan="8" align="center"><b>Staff Sales & Collection Report</b></td></tr>
<tr><th width=5><center><b>No.</th><th width=300><center><b>Ledger Name </th><th><center><b>Op Balance<th>Purchase Amount</th><th>Collection Amount(By Cash)</th><th>Collection Amount(By Bank)</th><th>Closing Balance</th>
<?

$result = mysqli_query($db,$query1);
$i=1;
$mtop_bal=0;
$mtdramt=0;
$mtcramt=0;
$mtbnk_amt=0;
$mtcl_bal=0;
while($row=mysqli_fetch_array($result))
{
	$df=$y.'-'.$m.'-'.$d;
	$dt=$y1.'-'.$m1.'-'.$d1;
        $mjour1 = "select op,drcr from ledger where led = '$row[3]' and brcode='$brcode' ";
        $mrs1=mysqli_query($db,$mjour1);
        $mjr=mysqli_fetch_array($mrs1);
        if($mjr[1]=='Cr')
            {
                $opbal=$mjr[0]*(-1);

            }
        else
            {
                $opbal=$mjr[0];
 
            }
	$mjour = "select sum(dramt),sum(cramt) from journal2 where partic = '$row[3]' and date < '$df' ";
        
	$jrow = mysqli_fetch_array(mysqli_query($db,$mjour));
	$mdramt = $jrow[0];
	$mcramt = $jrow[1];
	$mop_bal = $opbal+$mdramt-$mcramt;
	$mjour = "select sum(dramt),sum(cramt) from journal2 where partic = '$row[3]' and date >= '$df' and date <= '$dt' and j_type = 'BR'";
	$jrow1 = mysqli_fetch_array(mysqli_query($db,$mjour));
        
	$mdramt = $jrow1[0];
	$mbnk_amt = $jrow1[1];
	$mcl_bal = $mop_bal+$mdramt-$mbnk_amt;
	$mjour = "select sum(dramt),sum(cramt) from journal2 where partic = '$row[3]' and date >= '$df' and date <= '$dt' and j_type !='BR' ";
	//echo $mjour;
	$jrow1 = mysqli_fetch_array(mysqli_query($db,$mjour));
         
	$mdramt = $jrow1[0];
	$mcramt = $jrow1[1];
	$mcl_bal = $mcl_bal+$mdramt-$mcramt;
	print"\n<form action='editledger.php'><input type=hidden name=ledg value='$row[3]'>";
	print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'>";
	print "<td>".$i++."</td><td><a href='accounts/vmdebtordt.php?cd=".$row[3]."&sea=$sea&df=$df&dt=$dt&mop=$mop_bal'>$row[3]</a></td><td align=right>".number_format($mop_bal,2,'.',',')."</td><td align=right>".number_format($mdramt,2,'.',',')."</td><td align=right>".number_format($mcramt,2,'.',',')."</td><td align='right'>".number_format($mbnk_amt,2,'.',',')."</td><td align=right>".number_format($mcl_bal,2,'.',',')."</td>";
	$mtop_bal+=$mop_bal;
	$mtdramt+=$mdramt;
	$mtcramt+=$mcramt;
	$mtbnk_amt+=$mbnk_amt;
	$mtcl_bal+=$mcl_bal;
	//print "<td>".$i++."</td><td><a href='vmledger-test.php?cd=".$row[0]."&sea=$sea'>$row[0]</a></td><td>$row[1]</td><td align=right>".putComa($row[2])."</td><td align=right>$row[3]</td>";
	print "</tr>";
}

print "<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'>";
print "<td></td><td><b>T O T A L</td><td align=right><b>".number_format($mtop_bal,2,'.',',')."</td><td align=right><b>".number_format($mtdramt,2,'.',',')."</td><td align=right><b>".number_format($mtcramt,2,'.',',')."</td><td align='right'><b>".number_format($mtbnk_amt,2,'.',',')."</td><td align=right><b>".number_format($mtcl_bal,2,'.',',')."</td>";

?>
</table>
<script>
function billexc()
    {
        var ddf=document.f1.d.value;
	 var mmf=document.f1.m.value;
	 var yyf=document.f1.y.value;
	 var dfrom=yyf+'-'+mmf+'-'+ddf;

	var ddt=document.f1.d1.value;
	var mmt=document.f1.m1.value;
	var yyt=document.f1.y1.value;
	 var dto=yyt+'-'+mmt+'-'+ddt;
	 
	 //var url="sale_summ_billwise.php?fromdt="+dfrom+"&&todt="+dto;
	 //alert(url);
          document.location="listdebtorsprn.php?dfrom="+dfrom+"&&dto="+dto,true;
    }
    </script>