<?php
if(isset($_POST['invc1']) AND isset($_POST['invc2'])){
	$invc1=$_POST['invc1'];
	$invc2=$_POST['invc2'];
	$sql="SELECT * FROM invc001 WHERE (INO BETWEEN '$invc1' AND '$invc2') AND ILOCK!='1'";
	$prev="href='invoiceExportFormNo.php'";
	$string="Invoice No. : $invc1 to $invc2";
}	
if(isset($_POST['begin']) AND isset($_POST['finish'])){
	$begin=$_POST['begin'];
	$finish=$_POST['finish'];
	$sql="SELECT * FROM invc001 WHERE (IDATE BETWEEN '$begin' AND '$finish') AND ILOCK!='1'";
	$prev="href='invoiceExportForm.php'";
	$string="Invoice Date : $begin to $finish";
}
if(isset($_POST['name'])){
	$name=$_POST['name'];
	$sql="SELECT * FROM invc001 WHERE (IDC_NAME LIKE '%$name%') AND ILOCK!='1'";
	$prev="href='invoiceExportFormName.php'";
	$string="ชื่อลูกค้า : $name";
} 
if(isset($_POST['sell'])){
	$sell=$_POST['sell'];
	$sql="SELECT * FROM invc001 WHERE (IDSMFNAME LIKE '%$sell%') AND ILOCK!='1'";
	$prev="href='invoiceExportFormSell.php'";
	$string="ชื่อพนักงานขาย : $sell";
}

if(isset($_GET['act'])){
	if($_GET['act']== 'excel'){
		header("Content-Type: application/xls");
		header("Content-Disposition: attachment; filename=Invoice.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">
					<br /><br /><br />
					<h4 style="width: 800px;">DB EXPORT TO EXCEL <?php echo $string; ?></h4>
					<div style="width: 500px;">
						<a href="?act=excel" class="btn btn-primary"> Invoice 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;">Invc.No.</th>
								<th style="text-align: center;">Delv.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 invc001 WHERE ((INO BETWEEN '$invc1' AND '$invc2') OR 
                                                                (IDATE BETWEEN '$begin' AND '$finish')) AND 
                                                                ILOCK!='1'";*/
                                                                                                     
							$result = mysqli_query($con,$sql) ;
							while($row = mysqli_fetch_assoc($result)){ ?>
								<tr>
                                    <td style="text-align: left;"><?php echo $row['IDATE']; ?></td>
									<td style="text-align: left;"><?php echo $row['INO']; ?></td>
									<td style="text-align: left;"><?php echo $row['IDNO']; ?></td>
                                    <td style="text-align: left;"><?php echo $row['IDC_SUR'].' '.$row['IDC_NAME']; ?></td>
									<td style="text-align: left;"><?php echo $row['IDSUMDISC']; ?></td>
									<td style="text-align: left;"><?php echo $row['IDPDNAME1']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['IDQTY1']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['IDPDNAME2']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['IDQTY2']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['IDPDNAME3']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['IDQTY3']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['IDPDNAME4']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['IDQTY4']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['IDPDNAME5']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['IDQTY5']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['IDPDNAME6']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['IDQTY6']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['IDPDNAME7']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['IDQTY7']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['IDPDNAME8']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['IDQTY8']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['IDPDNAME9']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['IDQTY9']); ?></td>
                                    <td style="text-align: left;"><?php echo $row['IDPDNAME10']; ?></td>
									<td style="text-align: right;"><?php echo number_format($row['IDQTY10']); ?></td>
									<td style="text-align: right;"><?php echo number_format($row['IDSUM']); ?></td>
								</tr>
						<?php } ?>
					</table>
				</div>
			</div>
		</div>
	</body>
</html>