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

<?php
include("config.php");
	if(!isset($_SESSION["adminname"]) && !isset($_SESSION["username"]))
	{       
		header("Location: index.php");
	}
	if(isset($_GET['cid'])){
		$id=$_GET['cid'];
		$result=mysql_query("update report set status='3' where id='$id'"); 
		if($result){
			header("Location: viewreport.php?x");
		}
	}
?>
<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="report1.php" class="back" >Back</a></td>
							</tr>
						</table>
				
				
		</form>
		
		<a href="logout.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" >Logout</a><br>
		<?php
			if(isset($_GET['x'])){
				echo "<center><h2>Invoice Canceled Succesfully</h2></center>";
			}
		if(isset($_SESSION["adminname"])){?>
		<a href="passchange.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" >User List</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="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>
		<?php } ?>
		<input type="button" style="float:right; color:green;" onclick="downlo('excelfile/Invoice_Report.xls')" value="Download Excel" /><br>
		<!--input type="button" onclick="printDiv('printableArea')" value="Print" /-->
		<div id="printableArea">
		<div style="width:100%;" >
		<table >
		<tr>
		<td >Invoice No</td>
		<td>Date</td>
		<td>Address 1</td>
		<td >1L</td>
		<td >5L</td>
		<td>50L</td>
		<td>Qty/litres</td>
		<td>Unit Price</td>
		<td>Amount</td>
		<td style="width: 10%;">Cancel</td>
		<td style="width: 10%;">Collection Entry</td>
		<td style="width: 10%;">Pdf</td>
		<td style="width: 10%;"> Dc Pdf</td>
		</tr>
		<?php 
		require_once 'excel/excelwriter.class.php';
		$excel=new ExcelWriter("excelfile/Invoice_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 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') or invoiceno='$invno'");
			$myArr=array("","","","","Invoice Report From ".$fromdate." TO ".$todate,"");
			$excel->writeLine($myArr);
		}
		else
		{
			$result=mysql_query("select * from report order by id desc"); 
			$myArr=array("","","","","Invoice Report");
			$excel->writeLine($myArr);
		}
		$ta1l=0;$ta5l=0;$ta50l=0;$taaquantity=0;$taamount=0;
		$myArr=array("S.no","Invoice No","Date","Address","1L","5L","50L","Qty/litres","Unit Price","Amount");
		$excel->writeLine($myArr);
		$i=1;
		while($row=mysql_fetch_array($result))
		{
			$id=$row['id'];
			echo '<tr>';
			echo '<td >'.$row['invoiceno'].'</td>';
			echo '<td >'.$row['date'].'</td>';
			echo '<td >'.$row['address1'].'</td>';
			echo '<td style="text-align: center;">'.$row['a1l'].'</td>';
			echo '<td style="text-align: center;">'.$row['a5l'].'</td>';
			echo '<td style="text-align: center;">'.$row['a50l'].'</td>';
			echo '<td style="text-align: center;">'.$row['aquantity'].'</td>';
			echo '<td style="text-align: right;">'.$row['aunitprice'].'</td>';
			echo '<td style="text-align: right;">'.$row['aamount'].'</td>';
			if($row['status']=='0'){
			echo '<td><a href="viewreport.php?cid='.$id.'" title="Cancel Invoice">Cancel Invoice</a></td>';
			}elseif($row['status']=='3'){ echo '<td>Canceled</td>';}else{ echo '<td></td>';}
			if($row['status']=='1' || $row['status']=='0'){
			echo '<td><a href="collection.php?id='.$id.'" title="Collection Entry"><img style="width: 24%;" src="img/edit.png"><img></a></td>';
			}else{ echo '<td></td>';}
			
			
			//	echo '<td><a target="_blank" href="'.$row['pdf'].'"><img src="img/pdf.png" style="width: 24%;"></img>';
			echo '<td><a target="_blank" href="invoiceprint.php?id='.$row['id'].'"><img src="img/pdf.png" style="width: 24%;"></img>';
			
			echo '<td><a target="_blank" href="dcprint.php?id='.$row['id'].'"><img src="img/pdf.png" style="width: 24%;"></img>';
			
		//	echo '<td><a target="_blank" href="pdf/'.$row['dcno'].'.pdf"><img src="img/pdf.png" style="width: 24%;"></img>';
			//echo '<td ><a  href="editreport.php?id='.$row['id'].'">Edit</a></td>';
			
			echo '</tr>';
			$ta1l+=$row['a1l'];$ta5l=$row['a5l'];$ta50l+=$row['a50l'];$taaquantity+=$row['aquantity'];$taamount+=$row['aamount'];
			$myArr=array($i,$row['invoiceno'],$row['date'],$row['address1'],$row['a1l'],$row['a5l'],$row['a50l'],$row['aquantity'],$row['aunitprice'],$row['aamount']);
			$excel->writeLine($myArr);
			$i++;
		}
			echo '<tr><td colspan="3">Total</td>';
			echo '<td style="text-align: center;">'.$ta1l.'</td>';
			echo '<td style="text-align: center;">'.$ta5l.'</td>';
			echo '<td style="text-align: center;">'.$ta50l.'</td>';
			echo '<td style="text-align: center;">'.$taaquantity.'</td>';
			echo '<td style="text-align: right;"></td>';
			echo '<td style="text-align: right;">'.$taamount.'</td>';
			echo '<td colspan=3></td>';
			echo '</tr>';
			$myArr=array("","","","Total",$ta1l,$ta5l,$ta50l,$taaquantity,"",$taamount);
			$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>

