<?php
	ob_start();
	session_start();
?>

<?php
include("config.php");
//echo $_SESSION["adminname"];
	if(!isset($_SESSION["adminname"]))
	{       
		header("Location: index.php");
	}
?>
<html>
<head><title>BM BIO ENERGY</title>
<style type="text/css">
table, tr, td {
border: 1px solid white;
border-collapse: collapse;
padding: 8px;
font-size: 19px;
}
table {
margin: 0px auto;
width: 100%;
}
.table
{
margin: 29px;
width: 90%;
font-size: 23px;
}
.back
{
width: 8%;
background: #5CB85C none repeat scroll 0% 0%;
font-size: 20px;
color: #FFF;
text-decoration: none;
border: 1px solid #FFF;
margin-left: -371px;
position: absolute;
margin-top: -13px;
text-align: center;
}
table input[type=text]
{
width: 84%;
height: 37px;
border-radius: 4px;
border: 1px solid #000;
font-size: 19px;
padding: 5px;
}
</style>
</head>
<body style="background-color: rgb(0, 150, 136);">
<form method="post" enctype="multipart/form-data">
			<!--<div class="ser">
				<div class="don">
					<p class="seae"></p> <div class="box4"><input type="text" class="voice" name="invno" id="invno" />
					</div>
					
					<div style="clear:both;"></div>
				</div>
			</div>-->
			<table class="nostyle" style="width: 100%;">
							<tr>
								<td style="">From Date</td>
								<td style=""><input type="text" name="fromdate" id="fromdate" onmouseover='datepickvals(this.id);'   readonly /></td>
								<td style="">To Date</td>
								<td style=""><input type="text" name="todate" id="todate" onmouseover='datepickvals(this.id);' readonly /></td>
								<td style=""></td>
								<td style=""></td>
							</tr>
							<tr>
								<td colspan="4" class="t-right "><center ><input  type="submit" name="search"  id="search" value="Search" style="width: 13%; background: rgb(92, 184, 92) none repeat scroll 0% 0%; font-size: 20px; color: rgb(255, 255, 255); border: 1px solid rgb(255, 255, 255); font-family: times new" ></center></td><td><a href="viewreport.php" class="back" >Back</a></td>
								
							</tr>
						</table>
				
				
		</form>
		<a href="viewreport.php" style="width: 13%; background: rgb(92, 184, 92) none repeat scroll 0% 0%; font-size: 20px; color: rgb(255, 255, 255); border: 1px solid rgb(255, 255, 255); font-family: times new" >Invoice Report</a><br>
		<a href="collectionreport.php" style="width: 13%; background: rgb(92, 184, 92) none repeat scroll 0% 0%; font-size: 20px; color: rgb(255, 255, 255); border: 1px solid rgb(255, 255, 255); font-family: times new" >Collection Report</a><br>
		<a href="cancelreport.php" style="width: 13%; background: rgb(92, 184, 92) none repeat scroll 0% 0%; font-size: 20px; color: rgb(255, 255, 255); border: 1px solid rgb(255, 255, 255); font-family: times new" >Canceled Invoice Report</a><br>		
		<input type="button" style="float:right; color:green;" onclick="downlo('excelfile/Outstanding_Report.xls')" value="Download Excel" /><br>
		<!--input type="button" onclick="printDiv('printableArea')" value="Print" /-->
		<div id="printableArea">
		<div style="width:100%;" >
		<table >
		<tr>
		<td>S No</td>
		<td>Invoice No</td>
		<td>Invoice Amount</td>
		<td>Collected Amount</td>
		<td>Outstanding Amount</td>
		</tr>
		<?php 
		require_once 'excel/excelwriter.class.php';
		$excel=new ExcelWriter("excelfile/Outstanding_Report.xls");
		if($excel==false)	
		echo $excel->error;
		if(isset($_POST['search']))
		{
			$fromdate=$_POST['fromdate'];
			$ddate_from=date("Y-m-d", strtotime($fromdate));
			$todate=$_POST['todate'];
			$ddate_to=date("Y-m-d", strtotime($todate));
			$result=mysql_query("select * from report where  (STR_TO_DATE(date,'%d-%m-%Y') between '$ddate_from' AND '$ddate_to') or (STR_TO_DATE(date,'%m/%d/%Y') between '$ddate_from' AND '$ddate_to')");
			$myArr=array("","","","","Outstanding Report From ".$fromdate." TO ".$todate,"");
			$excel->writeLine($myArr);
		}
		else
		{
		$result=mysql_query("select * from report group by invoiceno,cdate order by id desc"); 
		$myArr=array("","","Outstanding Report");
		$excel->writeLine($myArr);
		}
		$myArr=array("S.no","Invoice No","Invoice Amount","Collected Amount","Outstanding Amount",);
		$excel->writeLine($myArr);
		$i=1;
		$taamount=0;$tcollected=0;$tpendingamount=0;
		while($row=mysql_fetch_array($result))
		{
			$id=$row['id'];
			$qry=mysql_query("select sum(collectionamount) as collected  from collection where refno='$id'"); 
			$rw=mysql_fetch_array($qry);
			if($rw['collected']<$row['aamount']){
				$pendingamount=$row['aamount']-$rw['collected'];
				echo '<tr>';
				echo '<td >'.$i.'</td>';
				echo '<td >'.$row['invoiceno'].'</td>';
				echo '<td style="text-align: center;">'.$row['aamount'].'</td>';
				echo '<td style="text-align: center;">'.$rw['collected'].'</td>';
				echo '<td style="text-align: center;">'.$pendingamount.'</td>';
				echo '</tr>';
				$myArr=array($i,$row['invoiceno'],$row['aamount'],$rw['collected'],$pendingamount);
				$excel->writeLine($myArr);
				$i++;
				$taamount+=$row['aamount'];$tcollected+=$rw['collected'];$tpendingamount+=$pendingamount;
			}
			
		}
		echo '<tr>';
				echo '<td ></td>';
				echo '<td >Total</td>';
				echo '<td style="text-align: center;">'.$taamount.'</td>';
				echo '<td style="text-align: center;">'.$tcollected.'</td>';
				echo '<td style="text-align: center;">'.$tpendingamount.'</td>';
				echo '</tr>';
		$myArr=array("","Total",$taamount,$tcollected,$tpendingamount);
		$excel->writeLine($myArr);
	?>
		</table>
		</div>			
		</div>			
		
</body>
</html>	

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="dp/js/jquery.timepicker.js"></script>
<script src="dp/lib/bootstrap-datepicker.js"></script>
 <link rel="stylesheet" type="text/css" href="dp/lib/bootstrap-datepicker.css" />
		<script>
			function datepickvals(a)
		{
			$('#'+a).datepicker({
				'format': 'dd-mm-yyyy',
				'autoclose': true
			});
		}
		</script>
		 <script>
                $('#basicExample .time').timepicker({
                    'showDuration': true,
                    'timeFormat': 'g:ia'
                });

                $('#basicExample .date').datepicker({
							'minDate': '0',
							'numberOfMonths': '1',
                    'format': 'dd-mm-yyyy',
                    'autoclose': true
                });

        </script>

<script type="text/javascript">
/*function ExportToExcel(img){
       var htmltable= document.getElementById('load');
       var html = htmltable.outerHTML;
       window.open('data:application/vnd.ms-excel,' + encodeURIComponent(html));
    }*/
function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}

	function downlo(a)
	{
		window.location=a;
	}
	</script>

