<!doctype html>
<html>

<head>
	<meta charset="UTF-8">
	<title>Seller List</title>
	<style type="text/css">
		td,
		th {
			border-left: 1px solid #DBDBDB;
			vertical-align: top;
		}

		#heado {
			background: green;
			color: aliceblue;
		}

		/*div#menu { margin:5px auto; }*/

		.third-level-menu {
			position: absolute;
			top: 0;
			right: -150px;
			width: 150px;
			list-style: none;
			padding: 0;
			margin: 0;
			display: none;
		}

		.third-level-menu>li {
			height: 30px;
			background: #999999;
		}

		.third-level-menu>li:hover {
			background: #CCCCCC;
		}

		.second-level-menu {
			position: absolute;
			top: 30px;
			left: 0;
			width: 150px;
			list-style: none;
			padding: 0;
			margin: 0;
			display: none;
		}

		.second-level-menu>li {
			position: relative;
			height: 30px;
			background: #999999;
		}

		.second-level-menu>li:hover {
			background: #CCCCCC;
		}

		.top-level-menu {
			list-style: none;
			padding: 0;
			margin: 0;
		}

		.top-level-menu>li {
			position: relative;
			float: left;
			height: 30px;
			width: 150px;
			background: #999999;
		}

		.top-level-menu>li:hover {
			background: #CCCCCC;
		}

		.top-level-menu li:hover>ul {
			display: inline;
		}

		/* On hover, display the next level's menu */

		/* Menu Link Styles */
		/* Apply to all links inside the multi-level menu */
		.top-level-menu a {
			font: bold 14px Arial, Helvetica, sans-serif;
			color: #FFFFFF;
			text-decoration: none;
			padding: 0 0 0 10px;
			/* Make the link cover the entire list item-container */
			display: block;
			line-height: 30px;
			text-align: center;
		}

		.top-level-menu a:hover {
			color: #000000;
		}

		#search {
			position: absolute;
			left: 571px;
		}
		.wd{
			table-layout: fixed;
			width: 100%;
			margin-top: 50px;
		}
		td h4{
			color: blue;
			text-align: center;
		}
	</style>
</head>

<body>

	<ul class="top-level-menu">
		<li><a href="actionController.php?
					   action=seller/retrieveQuotationActionForOrder">Add Order</a></li>
		<li><a href="#">Order Report By</a>
			<ul class="second-level-menu">
				<li><a href="actionController.php?
									 action=seller/retrieveQuotationActionForOrderQA">Date(A->Z)</a></li>
				<li><a href="actionController.php?
									 action=seller/retrieveQuotationActionForOrderQD">Date(Z->A)</a></li>
				<li><a href="actionController.php?
									 action=seller/retrieveQuotationActionForOrderQONo">Order No.</a></li>
				<li><a href="actionController.php?
									 action=seller/retrieveQuotationActionForOrderQC">Contact</a></li>
				<li><a href="actionController.php?
									 action=seller/retrieveQuotationActionForOrderQN">Name</a></li>
				<li><a href="actionController.php?
									 action=seller/retrieveQuotationActionForOrderQSl">Seller</a></li>					 
				<li><a href="actionController.php?
									 action=seller/retrieveQuotationActionForOrderQPj">Project Name</a></li>
				<li><a href="actionController.php?
									 action=seller/retrieveQuotationActionForOrderQV">Value(Desc.)</a></li>
			</ul>
		</li>
		<li><a href="#">All Report</a>
			<ul class="second-level-menu">
				<li><a href="actionController.php?
					                action=seller/retrieveOrderActionOA">Order(All)</a></li>
				<li><a href="actionController.php?
					                action=seller/retrieveOrderActionOC">Order Canceled</a></li>
				<li><a href="actionController.php?
					                action=seller/retrieveOrderActionOR">Order(Received)</a></li>
			</ul>
	</ul>

	<?php
	include "functions/dblink.php";
	$sql = "SELECT * FROM ordr001 WHERE OLOCK=0 ORDER BY OQDATE DESC,ONO DESC";
	
	$query = mysqli_query($con, $sql);
	?>

	<div id="search">
		<form name="frmSearch" method="post" action="
			  <?php echo 'actionController.php?action=seller/retrieveOrderAction
															&$strKeyword1&$strKeyword2'; ?>">
			<table>
				<tr>
					<td>พนักงานขาย
						<input type="text" name="txtSeller" id="txtSeller" size="49" placeholder="ค้นหา - ทั้งหมด">
					</td>	
				</tr>
				<tr>
					<td>วันที่
						<input name="txtKeyword1" type="date" id="txtKeyword1" value="<?php echo $strKeyword1; ?>"> -
						<input name="txtKeyword2" type="date" id="txtKeyword2" value="<?php echo $strKeyword2; ?>">
						<input type="submit" value="ค้นหา">
					</td>
				</tr>
			</table>
		</form>
	</div>

	<?php

	error_reporting(0);
	include "pagination.php";
	include "functions/dblink.php";

	ini_set('display_errors', 1);
	error_reporting(~0);
	
	$find = "ทั้งหมด";
	$condition = 1;
	
	if (isset($_POST["txtKeyword1"]) and isset($_POST["txtKeyword2"])) {
		if($_POST["txtKeyword1"]==""){
			$strKeyword1 = date('Y-m-d', strtotime('1900-01-01'));
		}else{
			$strKeyword1 = $_POST["txtKeyword1"];
		}
		if($_POST["txtKeyword2"]==""){
			$strKeyword2 = date('Y-m-d');
		}else{
			$strKeyword2 = $_POST["txtKeyword2"];
		}	
	}else{
		$strKeyword1 = date('Y-m-d', strtotime('1900-01-01'));
		$strKeyword2 = date('Y-m-d');
		$sql = "SELECT * FROM ordr001 WHERE (OLOCK=0 OR OLOCK='' OR OLOCK IS NULL) AND 
		                  ODATE>='$strKeyword1'  AND 
		                  ODATE<='$strKeyword2' 
						  ORDER BY ODATE DESC,ONO DESC";
	}

	if(isset($_POST["txtSeller"])){
		$seller=$_POST["txtSeller"];
		if($seller==""){
			$sql = "SELECT * FROM ordr001 WHERE (OLOCK=0 OR OLOCK='' OR OLOCK IS NULL) AND 
		                  ODATE>='$strKeyword1'  AND 
		                  ODATE<='$strKeyword2' 
						  ORDER BY ODATE DESC,ONO DESC";
		}else{
			$sql = "SELECT * FROM ordr001 WHERE (OLOCK=0 OR OLOCK='' OR OLOCK IS NULL) AND 
		                  ODATE>='$strKeyword1' AND 
		                  ODATE<='$strKeyword2' AND
						  OQSMFNAME LIKE '%$seller%'
						  ORDER BY ODATE DESC,ONO DESC";
		}
		
	}
	
	$perpage=20;
	$query = mysqli_query($con, $sql);
	$num=mysqli_num_rows($query);

	if (isset($_POST["txtKeyword1"]) and isset($_POST["txtKeyword2"])) {
		$perpage=$num;
	}

	$rs = page_query($con, $sql, $perpage);

	$first = page_start_row();
	$last = page_stop_row();
	$total = page_total_rows();
	if ($total == 0) {
		$first = 0;
	}

	//<a href='form.php?action=insert'>เพิ่มข้อมูล</a>
	echo '<form method="post">';
	echo "<table class='wd'><caption>";     //อ่านข้อมูล result set มาแสดงในรูปแบบตาราง
	echo "<h1>รายการใบสั่งซื้อ (Order List)</h1>";

	echo "<span id=\"result\">รายการที่ : $first - $last จากทั้งหมด $total</span>
					 </caption>";

	echo "<tr id='heado' >";      //ส่วนหัวตาราง(ชื่อฟิลด์)
	echo "<th width='10%'>action</th>";
	echo "<th width='9%'>Ord.Date</th>";     //ODATE
	echo "<th width='10%'>Order No.</th>";
	echo "<th width='10%'>Quot.No.</th>";
	echo "<th width='13%''>name</th>";        //OQC_NAME
	echo "<th width='9%'>% ลด</th>";        //OQC_DISC
	//echo "<th style='width:100px;'>ที่อยู่</th>";
	echo "<th width='13%'>รายการสินค้า</th>";
	echo "<th width='3%'>Qty</th>";       //OQQTY1...10
	echo "<th width='9%'>เงิน(บาท)</th>";    //OQSLEPRC1...10
	echo "<th width='10%'>รวมทั้งสิ้น</th>";
	echo "</tr>";
	
	while ($data = mysqli_fetch_array($rs, MYSQLI_ASSOC)) {
		error_reporting(~E_NOTICE);
		echo "<tr>";

		echo "<td><a href=\"actionController.php?
			                action=seller/editOrderAction&ONO={$data['ONO']}&
							actionIs=update\">[เรียกดู]</a>
			                <a href=\"report-content/orderIndReport.php?
							        no={$data['ONO']}\">[พิมพ์]</a></td>"; //เวลานำไปใช้ $no=$_GET['no'];

		if($data['OQQTY1']-$data['X1']>0){
			$tot1=($data['OQQTY1']-$data['X1'])*$data['OQSLEPRC1'];}
			else{$tot1=0;}
		if($data['OQQTY2']-$data['X2']>0){
			$tot2=($data['OQQTY2']-$data['X2'])*$data['OQSLEPRC2'];}
			else{$tot2=0;}
		if($data['OQQTY3']-$data['X3']>0){
			$tot3=($data['OQQTY3']-$data['X3'])*$data['OQSLEPRC3'];}
			else{$tot3=0;}
		if($data['OQQTY4']-$data['X4']>0){
			$tot4=($data['OQQTY4']-$data['X4'])*$data['OQSLEPRC4'];}
			else{$tot4=0;}
		if($data['OQQTY5']-$data['X5']>0){
			$tot5=($data['OQQTY5']-$data['X5'])*$data['OQSLEPRC5'];}
			else{$tot5=0;}
		if($data['OQQTY6']-$data['X6']>0){
			$tot6=($data['OQQTY6']-$data['X6'])*$data['OQSLEPRC6'];}
			else{$tot6=0;}
		if($data['OQQTY7']-$data['X7']>0){
			$tot7=($data['OQQTY7']-$data['X7'])*$data['OQSLEPRC7'];}
			else{$tot7=0;}
		if($data['OQQTY8']-$data['X8']>0){
			$tot8=($data['OQQTY8']-$data['X8'])*$data['OQSLEPRC8'];}
			else{$tot8=0;}
		if($data['OQQTY9']-$data['X9']>0){
			$tot9=($data['OQQTY9']-$data['X9'])*$data['OQSLEPRC9'];}
			else{$tot9=0;}
		if($data['OQQTY10']-$data['X10']>0){
			$tot10=($data['OQQTY10']-$data['X10'])*$data['OQSLEPRC10'];}
			else{$tot10=0;}
	
		if($data['OQC_DISC']<=100){	
			$s1=($tot1)-($tot1*$data['OQC_DISC']/100);
			$s2=($tot2)-($tot2*$data['OQC_DISC']/100);
			$s3=($tot3)-($tot3*$data['OQC_DISC']/100);
			$s4=($tot4)-($tot4*$data['OQC_DISC']/100);
			$s5=($tot5)-($tot5*$data['OQC_DISC']/100);
			$s6=($tot6)-($tot6*$data['OQC_DISC']/100);
			$s7=($tot7)-($tot7*$data['OQC_DISC']/100);
			$s8=($tot8)-($tot8*$data['OQC_DISC']/100);
			$s9=($tot9)-($tot9*$data['OQC_DISC']/100);
			$s10=($tot10)-($tot10*$data['OQC_DISC']/100);
			$tot=$s1+$s2+$s3+$s4+$s5+$s6+$s7+$s8+$s9+$s10;
		}else{
			$s=$tot1+$tot2+$tot3+$tot4+$tot5+$tot6+$tot7+$tot8+$tot9+$tot10;
			$tot=$s-$data['OQC_DISC'];
		}
		
		echo "<td>" . date_format(date_create("$data[ODATE]"), "d-m-Y") . "</td>";
		echo "<td>$data[ONO]</td>";
		echo "<td>$data[OQNO]</td>"; //หมายเลข Quotation
		echo "<td>$data[OQC_NAME]</td>";
		echo "<td class=rt>" . number_format($data['OQC_DISC'], 2) . "</td>";
		//echo "<td>$data[OQC_ADRS] $data[OQC_DSTRT] $data[OQC_PRVN]</td>";
		echo "<td>$data[OQPDNAME1]</td>";
	
	if($data['OLOST']=='Dwn'){
		echo "<td>".number_format($data['OQTERM1'])."%</td>";
		echo "<td>DwnP</td>";
		echo "<td class=rt>".number_format(($tot * $data['OQTERM1']/100),2). "</td>";
	}else{
		if($data['OQQTY1']-$data['X1']>0){
			$num=$data['OQQTY1']-$data['X1'];
			echo "<td class=rt>" . number_format($num) . "</td>";
			echo "<td class=rt>" . number_format(($data['OQSLEPRC1'] * $num), 2) . "</td>";
		}else{
			echo "<td></td>";
			echo "<td>"."<h4>Done</h4>"."</td>";
		}
		echo "<td class=rtq>" . number_format($tot, 2) . "</td>";	
		echo "<tr>";

		//$data{"QPDNAME{$i}"}; ${"tsle{$i}"}
		for ($i = 2; $i <= 10; $i++) {
			$blank=NULL;
			if ($data{"OQPDNAME{$i}"} != "" OR $data{"OQQTY{$i}"} != 0 OR $data{"OQQTY{$i}"} != is_null($blank)){
				echo "<td></td>";
				echo "<td></td>";
				echo "<td></td>";
				echo "<td></td>";
				echo "<td></td>";
				echo "<td></td>";
				echo "<td>" . $data{"OQPDNAME{$i}"} . "</td>";
				$num=$data{"OQQTY{$i}"}-$data{"X{$i}"};
				echo "<td class=rt>" . number_format($num) . "</td>";
				echo "<td class=rt>".number_format(($data{"OQSLEPRC{$i}"} * $num), 2) . "</td>";
				echo "<tr>";
			}

		}

	}
	
		echo "<tr>";
		echo "<th width='10%'><hr color='#DBDBDB' size='.1'></th>";
		echo "<th width=' 9%'><hr color='#DBDBDB' size='.1'></th>";
		echo "<th width='10%'><hr color='#DBDBDB' size='.1'></th>";
		echo "<th width='10%'><hr color='#DBDBDB' size='.1'></th>";
		echo "<th width='13%'><hr color='#DBDBDB' size='.1'></th>"; 
		echo "<th width=' 9%'><hr color='#DBDBDB' size='.1'></th>"; 
		echo "<th width='13%'><hr color='#DBDBDB' size='.1'></th>";
		echo "<th width=' 3%'><hr color='#DBDBDB' size='.1'></th>"; 
		echo "<th width=' 9%'><hr color='#DBDBDB' size='.1'></th>";
		echo "<th width='10%'><hr color='#DBDBDB' size='.1'></th>";
		echo "</tr>";

	}	
		
	
	echo "</table>";
	echo "</form>";

	//ต่อไปให้แสดงหมายเลขเพจเฉพาะเมื่อมีมากกว่า 1 เพจ 
	if (page_total() > 1) {
		echo '<p id="pagenum">';
		page_echo_pagenums();
		echo '</p>';
	}
	mysqli_close($con);
	?>
</body>

</html>