<?php
if(isset($_POST['begin']) AND isset($_POST['finish'])){
	$begin=$_POST['begin'];
	$finish=$_POST['finish'];
	$string="Order Date : $begin to $finish";
	$sql = "SELECT * FROM ordr001 WHERE (ODATE BETWEEN '$begin' AND '$finish') AND OLOCK!='1'";
	$prev="href='orderExportForm.php'";
}
if(isset($_POST['ordr1']) AND isset($_POST['ordr2'])){
	$ordr1=$_POST['ordr1'];
	$ordr2=$_POST['ordr2'];
	$string="Order No. : $ordr1 to $ordr2";
	$sql = "SELECT * FROM ordr001 WHERE (ONO BETWEEN '$ordr1' AND '$ordr2') AND OLOCK!='1'";
	$prev="href='orderExportFormNo.php'";
}
if(isset($_POST['name'])){
	$name=$_POST['name'];
	$string="ชื่อลูกค้า : $anme";
	$sql = "SELECT * FROM ordr001 WHERE OQC_NAME LIKE '%$name%' ";
	$prev="href='orderExportFormName.php'";
}
if(isset($_POST['sell'])){
	$sell=$_POST['sell'];
	$string="พนักงานขาย : $sell";
	$sql = "SELECT * FROM ordr001 WHERE OQSMFNAME LIKE '%$sell%' ";
	$prev="href='orderExportFormSell.php'";
}
if(isset($_GET['act'])){
	if($_GET['act']== 'excel'){
		header("Content-Type: application/xls");
		header("Content-Disposition: attachment; filename=Order.xls");
		header("Pragma: no-cache");
		header("Expires: 0");
	}
}
?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Order DB Export To Excel</title>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" 
		      integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" 
			  crossorigin="anonymous">
	</head>
	<body>
		<div class="container">
			<div class="row">
				<div class="col-md-5">
					<br /><br /><br />
					<h4 style="width: 800px;">DB EXPORT TO EXCEL for <?php echo $string; ?></h4>
					<div style="width: 500px;">
						<a href="?act=excel" class="btn btn-primary"> Order Export->Excel </a>
						<a <?php echo $prev; ?> class="btn btn-primary"> Previous Menu </a>
                        <?php session_start();
                            if($_SESSION['Status']=='ADMIN'){ ?>
                                <a href="../home.php" class="btn btn-primary"> Home </a>
                            <?php }else{ ?>
                                <a href="../homeOther.php" class="btn btn-primary"> Home </a>
                            <?php } ?>   
						<a href="../logout.php" class="btn btn-primary"> Logout </a>
					</p>
					</div>
					
					<table border="1" class="table table-hover">
						<thead>
							<tr>
								<th style="text-align: center;">Date</th>
                                <th style="text-align: center;">Ordr.No.</th>
								<th style="text-align: center;">Quot.No.</th>
								<th style="text-align: center;">Cust.Name</th>
								<th style="text-align: center;">ส่วนลด</th>
								<th style="text-align: center;">สินค้า1</th>
                                <th style="text-align: center;">จำนวน</th>
                                <th style="text-align: center;">สินค้า2</th>
                                <th style="text-align: center;">จำนวน</th>
                                <th style="text-align: center;">สินค้า3</th>
                                <th style="text-align: center;">จำนวน</th>
                                <th style="text-align: center;">สินค้า4</th>
                                <th style="text-align: center;">จำนวน</th>
                                <th style="text-align: center;">สินค้า5</th>
                                <th style="text-align: center;">จำนวน</th>
                                <th style="text-align: center;">สินค้า6</th>
                                <th style="text-align: center;">จำนวน</th>
                                <th style="text-align: center;">สินค้า7</th>
                                <th style="text-align: center;">จำนวน</th>
                                <th style="text-align: center;">สินค้า8</th>
                                <th style="text-align: center;">จำนวน</th>
                                <th style="text-align: center;">สินค้า9</th>
                                <th style="text-align: center;">จำนวน</th>
                                <th style="text-align: center;">สินค้า10</th>
                                <th style="text-align: center;">จำนวน</th>
								<th style="text-align: center;">เงิน(บาท)</th>									
							</tr>
						</thead>
						<?php
							include "../functions/dblink.php" ;
                            /*$sql = "SELECT * FROM ordr001 WHERE ((ONO BETWEEN '$ordr1' AND '$ordr2') OR 
                                                                (ODATE BETWEEN '$begin' AND '$finish')) AND 
                                                                OLOCK!='1'";*/
                                                                                                     
							$result = mysqli_query($con,$sql) ;
							while($row = mysqli_fetch_assoc($result)){ ?>
								<tr>
                                    <td style="text-align: left;"><?php echo $row['ODATE']; ?></td>
									<td style="text-align: left;"><?php echo $row['ONO']; ?></td>
									<td style="text-align: left;"><?php echo $row['OQNO']; ?></td>
                                    <td style="text-align: left;"><?php echo $row['OQC_NAME']; ?></td>
									<td style="text-align: left;"><?php echo $row['OQSUMDISC']; ?></td>
									<td style="text-align: left;"><?php echo $row['OQPDNAME1']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['OQQTY1']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['OQPDNAME2']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['OQQTY2']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['OQPDNAME3']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['OQQTY3']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['OQPDNAME4']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['OQQTY4']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['OQPDNAME5']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['OQQTY5']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['OQPDNAME6']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['OQQTY6']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['OQPDNAME7']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['OQQTY7']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['OQPDNAME8']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['OQQTY8']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['OQPDNAME9']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['OQQTY9']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['OQPDNAME10']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['OQQTY10']); ?></td>
									<td style="text-align: right;"><?php echo number_format($row['OQSUM']); ?></td>
								</tr>
						<?php } ?>
					</table>
				</div>
			</div>
		</div>
	</body>
</html>