<?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="">Search</td>
								<td style=""><input type="text" name="invno" id="invno"     /></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="outstandreport.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" >Outstanding 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/Collection_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>Reciept No</td>
		<td>Invoice No</td>
		<td>Date</td>
		<td>Invoice Amount</td>
		<td>Collected Amount</td>
		<td>Outstanding Amount</td>
		<td>Collection Amount</td>
		<td>Remarks</td>
		</tr>
		<?php 
		require_once 'excel/excelwriter.class.php';
		$excel=new ExcelWriter("excelfile/Collection_Report.xls");
		if($excel==false)	
		echo $excel->error;
		if(isset($_POST['search']))
		{
			$invno=$_POST['invno']	;
			$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 collection where (STR_TO_DATE(created_date,'%Y-%m-%d') between '$ddate_from' AND '$ddate_to') or  invoiceno='$invno'");
			$myArr=array("","","","","Collection Report From ".$fromdate." TO ".$todate,"");
			$excel->writeLine($myArr);
		}
		else
		{
			$result=mysql_query("select * from collection order by id desc"); 
			$myArr=array("","","","","Collection Report");
			$excel->writeLine($myArr);
		}
		
		$myArr=array("S.no","Reciept No","Invoice No","Date","Invoice Amount","Collected Amount","Outstanding Amount","Collection Amount");
		$excel->writeLine($myArr);
		$i=1;
		$ttotalamount=0;$tcollected=0;$tpendingamount=0;$tcollectionamount=0;
		while($row=mysql_fetch_array($result))
		{
			$id=$row['id'];
			echo '<tr>';
			echo '<td >'.$i.'</td>';
			echo '<td >'.$row['receiptno'].'</td>';
			echo '<td >'.$row['invoiceno'].'</td>';
			echo '<td >'.$date=date('d-m-Y',strtotime($row['created_date'])).'</td>';
			echo '<td style="text-align: center;">'.$row['totalamount'].'</td>';
			echo '<td style="text-align: center;">'.$row['collected'].'</td>';
			echo '<td style="text-align: center;">'.$row['pendingamount'].'</td>';
			echo '<td style="text-align: center;">'.$row['collectionamount'].'</td>';
			echo '<td style="text-align: center;">'.$row['remarks'].'</td>';
			echo '</tr>';
			$myArr=array($i,$row['receiptno'],$row['invoiceno'],$date,$row['totalamount'],$row['collected'],$row['pendingamount'],$row['collectionamount'],$row['remarks']);
			$excel->writeLine($myArr);
			$i++;
			$ttotalamount+=$row['totalamount'];$tcollected+=$row['collected'];$tpendingamount+=$row['pendingamount'];$tcollectionamount+=$row['collectionamount'];
		}
		echo '<tr><td colspan=7>Total</td><td style="text-align: center;">'.$tcollectionamount.'</td><td></td></tr>';
			$myArr=array("","","","","","","",$tcollectionamount,"");
			$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>

