<!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 delv001 WHERE (DLOCK='0' OR DLOCK='' OR DLOCK IS NULL) ORDER BY DDATE DESC,DNO DESC";
		$result=mysqli_query($con,$sql);
		
		$rs = page_query($con, $sql, 19);
	
		$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>(Delivery List for Invoice)</h1>";
		echo "<span id=\"result\">รายการที่ : $first - $last จากทั้งหมด $total</span></caption>";				
					 
		echo "<tr id='headq' >";      //ส่วนหัวตาราง(ชื่อฟิลด์)
		echo "<th width='4%'>Act</th>";
		echo "<th width='9%'>Delv.No.</th>";
		echo "<th width='15%'>name</th>";
		echo "<th width='8%'>ส่วนลด</th>";
		echo "<th width='12%'>Proj. Name</th>";
		echo "<th width='25%'>รายการ</th>";
		echo "<th width='3%'>#</th>";
		echo "<th width='6%'>@</th>";
	    echo "<th width='8%'>รวม</th>";
		echo "<th width='10%'>ทั้งสิ้น</th>";
		echo "</tr>";		
	 
		while($data=mysqli_fetch_array($rs))
		{			
			echo "<tr>";
			//echo '<td><input type="checkbox" name="NO[]" value=" ' .$data['NO'].' "></td>';
			echo "<td><a href=\"actionController.php?
			                    action=seller/addInvoiceAction&dno={$data['DNO']}&
								actionIs=insert\">[เพิ่ม]</a>
					</td>";
			
				$t1=$data[DOQTY1]*$data[DOSLEPRC1];
				$t2=$data[DOQTY2]*$data[DOSLEPRC2];
				$t3=$data[DOQTY3]*$data[DOSLEPRC3];
				$t4=$data[DOQTY4]*$data[DOSLEPRC4];
				$t5=$data[DOQTY5]*$data[DOSLEPRC5];
				$t6=$data[DOQTY6]*$data[DOSLEPRC6];
				$t7=$data[DOQTY7]*$data[DOSLEPRC7];
				$t8=$data[DOQTY8]*$data[DOSLEPRC8];
				$t9=$data[DOQTY9]*$data[DOSLEPRC9];
				$t10=$data[DOQTY10]*$data[DOSLEPRC10];
				$total=$t1+$t2+$t3+$t4+$t5+$t6+$t7+$t8+$t9+$t10;

			if($data[DOC_DISC]<=100){	
				$s1=$t1-($t1*$data[DOC_DISC]/100);
				$s2=$t2-($t2*$data[DOC_DISC]/100);
				$s3=$t3-($t3*$data[DOC_DISC]/100);
				$s4=$t4-($t4*$data[DOC_DISC]/100);
				$s5=$t5-($t5*$data[DOC_DISC]/100);
				$s6=$t6-($t6*$data[DOC_DISC]/100);
				$s7=$t7-($t7*$data[DOC_DISC]/100);
				$s8=$t8-($t8*$data[DOC_DISC]/100);
				$s9=$t9-($t9*$data[DOC_DISC]/100);
				$s10=$t10-($t10*$data[DOC_DISC]/100);
				
				$final=$s1+$s2+$s3+$s4+$s5+$s6+$s7+$s8+$s9+$s10;
				
			}else{
				$final=$total-$data[DOC_DISC];
			}

			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[DOC_ADRS]</td>";
			echo "<td>$data[DOPJNAME]</td>";
			echo "<td>$data[DOPDNAME1]</td>";

	if($data['DLOST']=='Dwn'){
		echo "<td></td>";
		echo "<td>".number_format($data['DOTERM1'])."%</td>";
		echo "<td>DwnP</td>";
		echo "<td class=rtq style='color:red;'>".number_format($final*$data['DOTERM1']/100,2)."</td>";
	}else{	

			echo "<td class=rt>".number_format($data[DOQTY1])."</td>";
			echo "<td class=rt>".number_format($data[DOSLEPRC1])."</td>";
			if($data[DOC_DISC]<=100){
				echo "<td class=rt>".number_format($s1,2)."</td>";
			}else{
				echo "<td class=rt>".number_format($t1,2)."</td>";
			}
			
			echo "<td class=rt style='color:red; font-weight: bold;'>".number_format($final,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 "<td></td>";
					//echo "<td style='width:8%;'></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>";
					if($data[DOC_DISC]<=100){
						echo "<td class=rt>".number_format(${"s{$i}"},2)."</td>";
					}else{
						echo "<td class=rt>".number_format(${"t{$i}"},2)."</td>";
					}

					echo "<tr>";
				}
			}
			
			echo "<tr>";
			echo "<th width=' 4%'><hr color='#DBDBDB' size='.1'></th>";
			echo "<th width=' 9%'><hr color='#DBDBDB' size='.1'></th>";
			echo "<th width='15%'><hr color='#DBDBDB' size='.1'></th>";
			echo "<th width=' 8%'><hr color='#DBDBDB' size='.1'></th>";
			echo "<th width='12%'><hr color='#DBDBDB' size='.1'></th>";
			echo "<th width='25%'><hr color='#DBDBDB' size='.1'></th>";
			echo "<th width=' 3%'><hr color='#DBDBDB' size='.1'></th>";
			echo "<th width=' 6%'><hr color='#DBDBDB' size='.1'></th>";
			echo "<th width=' 8%'><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>