<?php
	ob_start();
	session_start();
?>
<?php
include("config.php");
//echo $_SESSION["adminname"];
	if(!isset($_SESSION["adminname"]) && !isset($_SESSION["username"]))	{
		header("Location: index.php");
	}
	if(isset($_GET['id'])){
		$id=$_GET['id'];
		$sqry=mysql_query("select * from report where id='$id'");
		$row=mysql_fetch_array($sqry);
		$sqry1=mysql_query("select sum(collectionamount) as collected from collection where refno='$id'");
		$row1=mysql_fetch_array($sqry1);
		if($row1['collected']==''){
			$collected=0;
		}else{
			$collected=$row1['collected'];
		}
		$out=$row['aamount']-$row1['collected'];
	}
?>
<html>
<head><title>BM BIO ENERGY</title>
<style type="text/css">
.view
{
padding: 2px;
font-weight: bold;
width: 11%;
background: #5CB85C none repeat scroll 0% 0%;
font-size: 24px;
color: #FFF;
text-decoration: none;
border: 1px solid #FFF;
text-align: center;
border-radius: 4px;
height: 27px;
position: absolute;
right: 416px;
top: 256px;
}
.pdf
{
padding: 2px;
font-weight: bold;
width: 11%;
background: #5CB85C none repeat scroll 0% 0%;
font-size: 24px;
color: #FFF;
text-decoration: none;
border: 1px solid #FFF;
text-align: center;
border-radius: 4px;
height: 27px;
position: absolute;
right: 350px;
top: 566px;
}
label{ 
font-size: 25px; 
width:29%;
float: left;
} 
.box1
{
margin-top: -36px;
width:100%;

}

.box2
{
width: 33%;
float: left;
margin-top: 30px;
}
.box22
{
width: 67%;
float: left;
margin-top: -311px;
margin-left: 418px;
}
.box33
{
width: 26%;
float: right;
margin-top: -357px;
margin-right: 73px;
}
.box44
{
width: 60%;
float: left;
margin-top: -212px;
margin-left: 1061px;
}
.box3
{
width: 66%;
float: left;
font-size: 24px;
}
.seae
{
width: 7%;
float: left;
font-size: 18px;
font-weight: bold;
}
.box4
{
width: 85%;
float: left;
margin-top: 13px;
}
.don
{
width: 100%;
float: left;
}
.inpu
{ 
width: 100%;
border: 1px solid #000;
height: 35px;
font-size: 18px;
border-radius: 4px;
margin-top: 0px;
}

.inpu1
{ 
width: 100%;
}
table, tr, td {
border: 1px solid white;
border-collapse: collapse;
padding: 8px;
font-size: 19px;
}
table  input[type=text]
{
height: 33px;
border: 1px solid #000;
border-radius: 4px;
font-size: 15px;
padding: 9px;
}

table {
width: 100%;
}
.table
{
margin: 29px;
width: 90%;
font-size: 23px;
}
.fool
{
width: 60%;
float: left;
margin-left: 12%;
}
.thala 
{
width: 100%;
color: #FFF;
margin-top: 0px;
}
.bo
{
margin-top: 12px;
width: 99%;
margin-left: 23px;
}
.se
{
margin-top: 11px;
width: 38%;
}
.out
{
background-color: #5CB85C;
text-decoration: none;
width: 12%;
height: 28px;
float: right;
text-align: center;
color: #FFF;
border-radius: 4px;
border: 1px solid #FFF;
top: 0px !important;
position: absolute;
margin-left: 40px;
}
.voice
{
width: 20%;
height: 28px;
border-radius: 4px;
border: 1px solid black;
font-size: 18px;
}
.sub
{
font-weight: bold;
width: 11%;
background: #5CB85C none repeat scroll 0% 0%;
font-size: 24px;
color: #FFF;
text-decoration: none;
border: 1px solid #FFF;
text-align: center;
border-radius: 4px;
height: 33px;
position: absolute;
right: 574px;
top: 256px;
font-family: times new;

}
.ser
{
width: 100%;
}
</head>
</style>


<?php
function fun(){
$sqry=mysql_query("select * from collection order by id desc limit 0,1");
$srw=mysql_fetch_array($sqry);
$rr=mysql_num_rows($sqry);	
$ss=1;
$d=date("d");
$m=date("m");
if($m=='04' )
{
	$dbd=date('d-m-Y',strtotime($srw['created_date']));
	$rcindate_array=explode("-",$dbd);
	$dbdate=$rcindate_array[0];
	$dbmnth=$rcindate_array[1];
	if($d!=$dbdate && $m!=$dbmnth)
	{
		$ss=0;
	}
	else
	{
		$ss=1;
	}
}

if($rr==0 || $ss==0)
{
	$receiptno=1;
}
else
{
	$dcn=$srw['receiptno'];
	$receiptno=$dcn+1;
}
$in=3;
$receiptno = str_pad($receiptno, $in, "0", STR_PAD_LEFT);
return $receiptno;
}
$receiptno=fun();
if(isset($_POST['submit']))
{
$receiptno=fun();
$invoiceno=$_POST['invoiceno'];
$totalamount=$_POST['totalamount'];
$collected=$_POST['collected'];
$pendingamount=$_POST['pendingamount'];
$collectionamount=$_POST['collectionamount'];
$remarks=$_POST['remarks'];
$created_date=date('Y-m-d H:i:s');
 $sql="insert into collection set refno='{$id}',receiptno='{$receiptno}',invoiceno='{$invoiceno}',totalamount='{$totalamount}',collected='{$collected}',pendingamount='{$pendingamount}',collectionamount='{$collectionamount}',remarks='{$remarks}',created_date='{$created_date}',updated_date='{$created_date}'";
 mysql_query($sql)or die (mysql_error());
 $qry1=mysql_query("update report set status='1' where id='$id'");
 $qry=mysql_query("select sum(collectionamount) as collected  from collection where refno='$id'");
 $rw=mysql_fetch_array($qry);
 $collected=$rw['collected'];
 if($collected==$totalamount){
		$qry1=mysql_query("update report set status='2' where id='$id'");
 }
 if($sql)
  {
	header("Location: viewreport.php");
  }
 }
?>

<body style="background-color: rgb(0, 150, 136);">
<div class="thala">
<center><h1>BM BIO ENERGY</h1></center><h2 style="margin-left:85%; ">
<a class="out" href="logout.php">Logout</a></h2>
</div>
<form method="post" enctype="multipart/form-data" autocomplete='off'>
				<div class="box1">
			<div class="box2">
				<label>Receipt No:</label>
				<div class="box3">
					<input type="text" placeholder="Receipt No" class="inpu" name="receiptno" id="receiptno"  value="<?=$receiptno;?>"  readonly />
				</div>
			</div>
			<div class="box2">
				<label>Invoice No:</label>
				<div class="box3">
					<input type="text" class="inpu" name="invoiceno" id="invoiceno"  readonly value="<?=$row['invoiceno'];?>" />
				</div>
			</div>
			<div class="box2">
				<label>Total Amount:</label>
				<div class="box3">
					<input type="text" placeholder="Total Amount" class="inpu" name="totalamount" id="totalamount" value="<?=$row['aamount'];?>"  readonly/>
				</div>
			</div>
		</div>
		<div style="clear:both;"></div>
         <div class="box1">
			<div class="box2">
				<label>Collected:</label>
				<div class="box3">
					<input type="text" placeholder="Collected" class="inpu" name="collected" id="collected"  value="<?=$collected;?>" readonly>
				</div>
			</div>
			<div class="box2">
				<label>Outstanding Amount:</label>
				<div class="box3">
					<input type="text" placeholder="Pending Amount" class="inpu" name="pendingamount" id="pendingamount" value="<?=$out;?>" readonly>
				</div>
			</div>
			<div class="box2">
				<label> Collection Amount:</label>
				<div class="box3">
					<input type="text" placeholder="Collection Amount" class="inpu" name="collectionamount" required id="collectionamount" onkeypress='fnamt(this.id);' onchange="check(this.value,<?=$out;?>);"   />
				</div>
			</div>
		</div>
		<div style="clear:both;"></div>
         <div class="box1">
			<div class="box2">
				<label>Remarks:</label>
				<div class="box3">
					<input type="text" placeholder="Remarks" class="inpu" name="remarks" id="remarks" required >
				</div>
			</div>
		</div>
		<div style="clear:both;"></div>
		<div class="fool">
			<center>
			<input type="submit" name="submit" id="submit" class="sub"  value="Submit">
			<a href="viewreport.php" class="view">Bill View</a>
			</center>	
		</div>
		
</body>
</html>
<script>
function check(coll,pend){
	if(parseFloat(pend)<parseFloat(coll)){
		alert("Please enter the collection amount less then or equal to Outstanding amount")
		document.getElementById("collectionamount").value='';
	}
}
function fnamt(id){
var foo = document.getElementById(id);
foo.addEventListener('input', function (prev) {
    return function (evt) {
        if (!/^\d{0,6}(?:\.\d{0,2})?$/.test(this.value)) {
          this.value = prev;
        }
        else {
          prev = this.value;
        }
    };
}(foo.value), false);
}	
</script>