<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Seller List</title>
<style>
	td, th {border-left: 1px solid #DBDBDB;
	        vertical-align: top;}
	#headq {background: green; color: aliceblue;}
	.wd{
			table-layout: fixed;
			width: 100%;
		}
</style>
</head>

<body>
	<?php
 		
		error_reporting(0);
 		include "pagination.php";   
		include "functions/dblink.php";	   

		$find = "ทั้งหมด";
		$condition = 1;	
		
		//กรณี chkbox แล้วโพสกลับขึ้นมา	
		
		$sql="SELECT * FROM ordr001 WHERE (OLOCK=0 OR OLOCK='' OR OLOCK IS NULL OR OLOST!='Dwn') 
		                            ORDER BY ODATE DESC,ONO DESC";
		$result=mysqli_query($con,$sql);
		$rs = page_query($con, $sql, 20);
	
		$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>รายการใบสั่งซื้อ(สำหรับออกใบส่งสินค้า)</h1>";
		echo "<h1>(Order List for Delivery)</h1>";
		echo "<span id=\"result\">รายการที่ : $first - $last จากทั้งหมด $total</span></caption>";				
					 
		echo "<tr id='headq'>";      //ส่วนหัวตาราง(ชื่อฟิลด์)
		echo "<th width='4%'>Act.</th>";
		echo "<th width='9%'>ว้นที่</th>";
		echo "<th width='10%'>Ord.No.</th>";
	    echo "<th width='12%'>name</th>";
	    echo "<th width='8%'>ส่วนลด</th>";
		//echo "<th>ที่อยู่</th>";
		echo "<th width='12%'>Proj. Name</th>";
		echo "<th width='14%'>รายการ</th>";
		echo "<th width='3%'>#</th>";
		echo "<th width='8%'>@</th>";
	    echo "<th width='9%'>บาท</th>";
		echo "<th width='10%'>รวม(หัก Dwn)</th>";
		echo "</tr>";		
	 
		while($data=mysqli_fetch_array($rs))
		{			
			echo "<tr>";
			echo "<td><a href=\"actionController.php?action=seller/addDeliveryActionS&ONO={$data['ONO']}&
								no={$data['NO']}&olost={$data['OLOST']}&actionIs=insert\">[เพิ่ม]</a>
					</td>";
			//จำนวนในแต่ละรายการ * ราคา/หน่วย		
            if($data['OQQTY1']-$data['X1']>0){
				$t1=($data['OQQTY1']-$data['X1'])*$data['OQSLEPRC1'];
			}else{
				$t1=0;
			}
			if($data['OQQTY2']-$data['X2']>0){	
				$t2=($data['OQQTY2']-$data['X2'])*$data['OQSLEPRC2'];
			}else{
				$t2=0;
			}
			if($data['OQQTY3']-$data['X3']>0){
				$t3=($data['OQQTY3']-$data['X3'])*$data['OQSLEPRC3'];
			}else{
				$t3=0;
			}
			if($data['OQQTY4']-$data['X4']>0){
				$t4=($data['OQQTY4']-$data['X4'])*$data['OQSLEPRC4'];
			}else{
				$t4=0;
			}
			if($data['OQQTY5']-$data['X5']>0){
				$t5=($data['OQQTY5']-$data['X5'])*$data['OQSLEPRC5'];
			}else{
				$t5=0;
			}
			if($data['OQQTY6']-$data['X6']>0){
				$t6=($data['OQQTY6']-$data['X6'])*$data['OQSLEPRC6'];
			}else{
				$t6=0;
			}
			if($data['OQQTY7']-$data['X7']>0){
				$t7=($data['OQQTY7']-$data['X7'])*$data['OQSLEPRC7'];
			}else{
				$t7=0;
			}
			if($data['OQQTY8']-$data['X8']>0){
				$t8=($data['OQQTY8']-$data['X8'])*$data['OQSLEPRC8'];
			}else{
				$t8=0;
			}
			if($data['OQQTY9']-$data['X9']>0){
				$t9=($data['OQQTY9']-$data['X9'])*$data['OQSLEPRC9'];
			}else{
				$t9=0;
			}
			if($data['OQQTY10']-$data['X10']>0){
				$t10=($data['OQQTY10']-$data['X10'])*$data['OQSLEPRC10'];
			}else{
				$t10=0;
			}
		//หักส่วนลด	
		if($data['OQC_DISC']<=100){	
			$s1=($t1)-($t1*$data['OQC_DISC']/100);
			$s2=($t2)-($t2*$data['OQC_DISC']/100);
			$s3=($t3)-($t3*$data['OQC_DISC']/100);
			$s4=($t4)-($t4*$data['OQC_DISC']/100);
			$s5=($t5)-($t5*$data['OQC_DISC']/100);
			$s6=($t6)-($t6*$data['OQC_DISC']/100);
			$s7=($t7)-($t7*$data['OQC_DISC']/100);
			$s8=($t8)-($t8*$data['OQC_DISC']/100);
			$s9=($t9)-($t9*$data['OQC_DISC']/100);
			$s10=($t10)-($t10*$data['OQC_DISC']/100);
			$total=$s1+$s2+$s3+$s4+$s5+$s6+$s7+$s8+$s9+$s10;//ทั้งสิ้นหักส่วนลด(%)
		}else{
			$s=$t1+$t2+$t3+$t4+$t5+$t6+$t7+$t8+$t9+$t10;
			$total=$s-$data['OQC_DISC'];//ทั้งสิ้นหักส่วนลด(ทั้งใบ)
		}
			
				echo "<td>$data[ODATE]</td>";
				echo "<td>$data[ONO]</td>";
				echo "<td>$data[OQC_NAME]</td>";
				echo "<td class=rt>".number_format($data['OQC_DISC'],2)."</td>";
				echo "<td>$data[OQPJNAME]</td>";
				echo "<td>$data[OQPDNAME1]</td>";
			if($data['OQQTY1']-$data['X1']>0){
				echo "<td class=rt>".number_format($data['OQQTY1']-$data['X1'])."</td>";//จำนวน		
			}else{
				echo "<td class=rt>"."0"."</td>";
			}
				echo "<td class=rt>".number_format($data['OQSLEPRC1'])."</td>";	
				echo "<td class=rt>".number_format($s1,2)."</td>";
				echo "<td class=rt>".number_format($total-($total*($data['OQTERM1']/100)),2)."</td>";
				echo "<tr>";
			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>";
					echo "<td class=rt>".number_format($data["OQQTY{$i}"]-$data["X{$i}"])."</td>";//จำนวน
					echo "<td class=rt>".number_format($data["OQSLEPRC{$i}"])."</td>";
					echo "<td class=rt>".number_format($s["{$i}"],2)."</td>";
					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>