<!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;}
	#headd {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;}
</style>
</head>

<body>

<ul class="top-level-menu">
    <li><a href="#">Add Delivery</a>
		<ul class="second-level-menu">	             
			<li><a href="actionController.php?
					             action=seller/retrieveOrderActionForDelivery">Order ทั้งใบ</a></li>
			<li><a href="actionController.php?
					             action=seller/retrieveOrderActionForDeliveryS">Order บางรายการ</a></li>		             	
		</ul>				             		            
	</li><li><a href="#">Delivery Report By</a>
        <ul class="second-level-menu">
			<li><a href="actionController.php?
					             action=seller/retrieveOrderActionForDeliveryD">Date</a></li> <!-- inputDateRangeD.php deliveryReport.php -->
            <li><a href="actionController.php?
					             action=seller/retrieveOrderActionForDeliveryDNo">Delivery No.</a></li><!-- inputDateRangeDNo.php deliveryReportByNo.php -->
            <li><a href="actionController.php?
					             action=seller/retrieveOrderActionForDeliveryDC">Contact</a></li><!-- inputDateRangeDC.php deliveryReportByContact.php -->
            <li><a href="actionController.php?
					             action=seller/retrieveOrderActionForDeliveryDN">Delivery Name</a></li><!-- inputDateRangeDN.php deliveryReportByName.php -->
            <li><a href="actionController.php?
					             action=seller/retrieveOrderActionForDeliveryDPj">Project Name</a></li><!-- inputDateRangeDPj.php deliveryReportByProject.php -->
            <li><a href="actionController.php?
					             action=seller/retrieveOrderActionForDeliveryDV">Deliv.Value(Desc.)</a></li><!-- inputDateRangeDV.php deliveryReportByValue.php -->
        </ul>
    </li>
    <li><a href="#"></a></li>
</ul>
	
	<?php
		error_reporting(0);

		include "functions/dblink.php";
		
		$sql = "SELECT * FROM delv001 WHERE (DLOCK='0' OR DLOCK='' OR DLOCK IS NULL) ORDER BY DDATE DESC,DNO DESC";
		$query = mysqli_query($con,$sql);
	?>
    
    <div id="search"><!-- echo $_SERVER['SCRIPT_NAME']; -->
	<form name="frmSearch" method="post" action="
				<?php echo 'actionController.php?action=seller/retrieveDeliveryAction
															&$strKeyword1&$strKeyword2';?>">
		<table>
		<tr>
			<td>พนักงานขาย
				<input type="text" name="txtSeller" id="txtSeller" size="51" 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 delv001 WHERE (DLOCK=0 OR DLOCK='' OR DLOCK IS NULL) AND 
							  DDATE>='$strKeyword1'  AND 
							  DDATE<='$strKeyword2' 
							  ORDER BY DDATE DESC,DNO DESC";
		}
	
		if(isset($_POST["txtSeller"])){
			$seller=$_POST["txtSeller"];
			if($seller==""){
				$sql = "SELECT * FROM delv001 WHERE (DLOCK=0 OR DLOCK='' OR DLOCK IS NULL) AND 
							  DDATE>='$strKeyword1'  AND 
							  DDATE<='$strKeyword2' 
							  ORDER BY DDATE DESC,DNO DESC";
			}else{
				$sql = "SELECT * FROM delv001 WHERE (DLOCK=0 OR DLOCK='' OR DLOCK IS NULL) AND 
							  DDATE>='$strKeyword1' AND 
							  DDATE<='$strKeyword2' AND
							  DOSMFNAME LIKE '%$seller%'
							  ORDER BY DDATE DESC,DNO 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>รายการใบส่งสินค้า (Delivery List)</h1>";
									
		echo "<span id=\"result\">รายการที่ : $first - $last จากทั้งหมด $total</span>
					 </caption>";
	
		echo "<tr id='headd' >";      //ส่วนหัวตาราง(ชื่อฟิลด์)
		echo "<th width='5%'>act.</th>";
	    echo "<th width='10%'>วันที่</th>";
		echo "<th width='10%'>Deliv.No.</th>";
	    echo "<th width='10%'>Ordr.No.</th>";
		echo "<th width='12%'>name</th>"; 
		echo "<th width='11%'>% ลด</th>";  
		echo "<th width='18%'>รายการสินค้า</th>";      
		echo "<th width='4%'>#</th>";              
		echo "<th width='8%'>@</th>"; 
	   echo "<th width='11%'>รวม(ไม่หัก Dwn)</th>";
		echo "</tr>";
	
		while($data=mysqli_fetch_array($rs,MYSQLI_ASSOC))
		{	
		   error_reporting(~E_NOTICE );
			echo "<tr>";
			echo "<td><a href=\"report-content/deliveryIndReport.php?
							                        no={$data['DNO']}\">[พิมพ์]</a></td>";//เวลานำไปใช้ $no=$_GET['no'];
			$s1=($data['DOQTY1']*$data['DOSLEPRC1']);
			$s2=($data['DOQTY2']*$data['DOSLEPRC2']);
			$s3=($data['DOQTY3']*$data['DOSLEPRC3']);
			$s4=($data['DOQTY4']*$data['DOSLEPRC4']);
			$s5=($data['DOQTY5']*$data['DOSLEPRC5']);
			$s6=($data['DOQTY6']*$data['DOSLEPRC6']);
			$s7=($data['DOQTY7']*$data['DOSLEPRC7']);
			$s8=($data['DOQTY8']*$data['DOSLEPRC8']);
			$s9=($data['DOQTY9']*$data['DOSLEPRC9']);
			$s10=($data['DOQTY10']*$data['DOSLEPRC10']);
			
			if($data['DOC_DISC']>100){
				$t1=$s1;$t2=$s2;$t3=$s3;$t4=$s4;$t5=$s5;$t6=$s6;$t7=$s7;$t8=$s8;$t9=$s9;$t10=$s10;
				$tot=($t1+$t2+$t3+$t4+$t5+$t6+$t7+$t8+$t9+$t10)-$data['DOC_DISC'];
			}else{
						$t1=$s1-($s1*$data['DOC_DISC']/100);
						$t2=$s2-($s2*$data['DOC_DISC']/100);
						$t3=$s3-($s3*$data['DOC_DISC']/100);
						$t4=$s4-($s4*$data['DOC_DISC']/100);
						$t5=$s5-($s5*$data['DOC_DISC']/100);
						$t6=$s6-($s6*$data['DOC_DISC']/100);
						$t7=$s7-($s7*$data['DOC_DISC']/100);
						$t8=$s8-($s8*$data['DOC_DISC']/100);
						$t9=$s9-($s9*$data['DOC_DISC']/100);
						$t10=$s10-($s10*$data['DOC_DISC']/100);
						$tot=$t1+$t2+$t3+$t4+$t5+$t6+$t7+$t8+$t9+$t10;
			       }
			
			echo "<td>".date_format(date_create("$data[DDATE]"),"d-m-Y")."</td>";
			echo "<td>$data[DNO]</td>";
			echo "<td>$data[DONO]</td>";
			echo "<td>$data[DOC_NAME]</td>";
			echo "<td class=rt>".number_format($data['DOC_DISC'],2)."</td>";
			echo "<td>$data[DOPDNAME1]</td>";

			$DwnP=$tot*$data['DOTERM1']/100;
			//$GTot=$tot-$DwnP;
			$GTot=$tot;

		if($data['DLOST']=='Dwn'){
			echo "<td>".number_format($data['DOTERM1'])."%</td>";
			echo "<td>DwnP</td>";
			echo "<td class=rtq style='color:red;'>".number_format($DwnP,2)."</td>";
		}else{	   
			echo "<td class=rt>".number_format($data['DOQTY1'])."</td>";
			echo "<td class=rt>".number_format($data['DOSLEPRC1'])."</td>";
			echo "<td class=rtq style='color:red;'>".number_format($GTot,2)."</td>";
			echo "</tr>";  
            
			for($i=2;$i<=10;$i++){ 
				$blank=NULL;        
				if ($data["DOPDNAME{$i}"] != "" OR $data["DOQTY{$i}"] != 0 OR $data["DOQTY{$i}"] != is_null($blank)){
             echo "<tr>";            
					echo "<td></td>";
					echo "<td></td>";
					echo "<td></td>";
					echo "<td></td>";	
					echo "<td></td>";
					echo "<td></td>";
					echo "<td>".$data["DOPDNAME{$i}"]."</td>";
					echo "<td class=rt>".number_format($data["DOQTY{$i}"])."</td>";
					echo "<td class=rt>".number_format($data["DOSLEPRC{$i}"])."</td>";
             echo "</tr>";        
					//echo "<td class=rt>".number_format($["t{$i}"])."</td>";		
				}
			}
			
			echo "<tr>";
			echo "<th width=' 5%'><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='10%'><hr color='#DBDBDB' size='.1'></th>";
			echo "<th width='12%'><hr color='#DBDBDB' size='.1'></th>"; 
			echo "<th width='11%'><hr color='#DBDBDB' size='.1'></th>";  
			echo "<th width='18%'><hr color='#DBDBDB' size='.1'></th>";      
			echo "<th width=' 4%'><hr color='#DBDBDB' size='.1'></th>";              
			echo "<th width=' 8%'><hr color='#DBDBDB' size='.1'></th>"; 
			echo "<th width='11%'><hr color='#DBDBDB' size='.1'></th>";
		}
		}
		echo "</table>";
		echo "</form>";
		
		//ต่อไปให้แสดงหมายเลขเพจเฉพาะเมื่อมีมากกว่า 1 เพจ 
		if(page_total() > 1)
	    { 	
			echo '<p id="pagenum">';
			page_echo_pagenums();
			echo '</p>';
	    }
		mysqli_close($con);
	?>
</body>
</html>