<?php
if(isset($_POST['delv1']) AND isset($_POST['delv2'])){
	$delv1=$_POST['delv1'];
	$delv2=$_POST['delv2'];
	$sql = "SELECT * FROM delv001 WHERE (DNO BETWEEN '$delv1' AND '$delv2') AND DLOCK!='1'";
	$prev="href='deliveryExportFormNo.php'";
	$string="Delivery No. : $delv1 to $delv2";
}
if(isset($_POST['begin']) AND isset($_POST['finish'])){
	$begin=$_POST['begin'];
	$finish=$_POST['finish'];
	$sql = "SELECT * FROM delv001 WHERE (DDATE BETWEEN '$begin' AND '$finish') AND DLOCK!='1'";
	$prev="href='deliveryExportForm.php'";
	$string="Delivery Date : $begin to $finish";
}
if(isset($_POST['name'])){
	$name=$_POST['name'];
	$sql = "SELECT * FROM delv001 WHERE DOC_NAME LIKE '%$name%' AND DLOCK!='1'";
	$prev="href='deliveryExportFormName.php'";
	$string="ชื่อลูกค้า : $name";
}
if(isset($_POST['sell'])){
	$sell=$_POST['sell'];
	$sql = "SELECT * FROM delv001 WHERE DOSMFNAME LIKE '%$sell%' AND DLOCK!='1'";
	$prev="href='deliveryExportFormSell.php'";
	$string="ชื่อพนักงานขาย : $sell";
}

if(isset($_GET['act'])){
	if($_GET['act']== 'excel'){
		header("Content-Type: application/xls");
		header("Content-Disposition: attachment; filename=Delivery.xls");
		header("Pragma: no-cache");
		header("Expires: 0");
	}
}
?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Delivery 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">
					<p>
					<h4 style="width: 800px;">DB EXPORT TO EXCEL <?php echo $string; ?></h4>
					<div style="width: 500px;">
						<a href="?act=excel" class="btn btn-primary"> Delivery 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;">Delv.No.</th>
								<th style="text-align: center;">Ordr.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 delv001 WHERE ((DNO BETWEEN '$delv1' AND '$delv2') OR 
                                                                (DDATE BETWEEN '$begin' AND '$finish')) AND 
                                                                DLOCK!='1'";*/
                                                                                                     
							$result = mysqli_query($con,$sql) ;
							while($row = mysqli_fetch_assoc($result)){ ?>
								<tr>
                                    <td style="text-align: left;"><?php echo $row['DDATE']; ?></td>
									<td style="text-align: left;"><?php echo $row['DNO']; ?></td>
									<td style="text-align: left;"><?php echo $row['DONO']; ?></td>
                                    <td style="text-align: left;"><?php echo $row['DOC_SUR'].' '.$row['DOC_NAME']; ?></td>
									<td style="text-align: left;"><?php echo $row['DOSUMDISC']; ?></td>
									<td style="text-align: left;"><?php echo $row['DOPDNAME1']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['DOQTY1']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['DOPDNAME2']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['DOQTY2']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['DOPDNAME3']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['DOQTY3']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['DOPDNAME4']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['DOQTY4']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['DOPDNAME5']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['DOQTY5']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['DOPDNAME6']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['DOQTY6']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['DOPDNAME7']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['DOQTY7']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['DOPDNAME8']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['DOQTY8']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['DOPDNAME9']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['DOQTY9']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['DOPDNAME10']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['DOQTY10']); ?></td>
									<td style="text-align: right;"><?php echo number_format($row['DOSUM']); ?></td>
								</tr>
						<?php } ?>
					</table>
				</div>
			</div>
		</div>
	</body>
</html>