<?
include_once('includes/top.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$user_level=$_SESSION['user_level'];
include_once("opendb.php");
$sea=$_REQUEST['sea'];
$fromdt=$_REQUEST['fromdt'];
$todt=$_REQUEST['todt'];
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" />
<link rel="stylesheet" href="cal/jquery-ui.css" />
<script src="cal/jquery-1.9.1.js"></script>
<script src="cal/jquery-ui1.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />




   <script type="text/javascript">
$(function() {
$( "#ref_date" ).datepicker("destroy");
     var fyr=document.getElementById('f_yr').value;
     var splv=fyr.explode("-");
     var syr=splv[0];
     var eyr='20'.concat(splv[1]);
     var syr1='01-04-';
     var eyr1='31-03-';
     var stfnyr=syr1.concat(syr);
     var efnyr=eyr1.concat(eyr);
     $("#ref_date").datepicker({ dateFormat: 'dd-mm-yy', minDate: stfnyr, maxDate: efnyr});

$( "#ref_date1" ).datepicker("destroy");
     var fyr=document.getElementById('f_yr').value;
     var splv=fyr.explode("-");
     var syr=splv[0];
     var eyr='20'.concat(splv[1]);
     var syr1='01-04-';
     var eyr1='31-03-';
     var stfnyr=syr1.concat(syr);
     var efnyr=eyr1.concat(eyr);
     $("#ref_date1").datepicker({ dateFormat: 'dd-mm-yy', minDate: stfnyr, maxDate: efnyr});
        });
</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($fromdt > 0)
{
	
	print "<form name='f1' action='$PHP_SELF'><tr><td>Date from : <input type='text' id='ref_date'  name='fromdt' value='$fromdt' readonly></td></tr>";
	print "<tr><td>To &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <input type='text' id='ref_date1' value='$todt' name='todt' readonly><input type=submit value='Show' name='show'></td></tr></form>";
	}
else
{
	$yeardat=explode('-',$fin_year);
	$startingyr=$yeardat[0];
	$fromdt ="01-04-".$startingyr;
	$todt =date('d-m-Y');
	print "<form name='f1' action='$PHP_SELF'><tr><td>Date from : <input type=hidden name='f_yr' id='f_yr' value='$fin_year'><input type='text' id='ref_date'  name='fromdt' value='$fromdt' readonly></td></tr>";
	print "<tr><td>To &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <input type='text' id='ref_date1' value='$todt' name='todt'  readonly><input type=submit value='Show' name='show'></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=implode('-', array_reverse(explode('-', $fromdt)));
	$dt=implode('-', array_reverse(explode('-', $todt)));
	
        $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];
 
            }
			$sql="select * from prod_invoice where brcode = '$brcode' and customer='$row[3]' and staffdisc>0 and pmode='CASH' group by jvno";
	$sqrs=mysqli_query($db,$sql);
	while($sqrow=mysqli_fetch_array($sqrs))
	{
		$mjour = "select sum(dramt),sum(cramt) from journal2 where jvno='$sqrow[jvno]' and date >= '$df' and date <= '$dt' and j_type !='BR' ";
		//echo $mjour;
		//echo "<br>";
		$jrow = mysqli_fetch_array(mysqli_query($db,$mjour));
		$mdramt1 = $jrow[0];
		
		$mcramt1 = $jrow[1];
		//$mcl_bal = $mcl_bal+$mdramt1;
	}
	$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' ";
	 
	$jrow1 = mysqli_fetch_array(mysqli_query($db,$mjour));
         
	$mdramt = $jrow1[0];
	$mcramt = $jrow1[1];
	$mdramt=$mdramt+$mdramt1;
	
	$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 dfrom=document.f1.fromdt.value;	
	 var dto=document.f1.todt.value;	
     document.location="listdebtorsprn.php?dfrom="+dfrom+"&&dto="+dto,true;
    }
    </script>