<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Invoice List</title>
<style type="text/css">
	td, th {border-left: 1px solid #DBDBDB; vertical-align: top;}
	#headi {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;}
a:link{text-decoration: none;}
.wd{table-layout: fixed; width: 100%; margin-top: 50px;}
</style>

</head>

<body>

<ul class="top-level-menu">
    <li><a href="actionController.php?
					             action=seller/retrieveDeliveryActionForInvoice">Add Invoice</a>					            
	</li>
    <li><a href="#">Invoice Report By</a>
        <ul class="second-level-menu">
            <li><a href="actionController.php?
					             action=seller/retrieveDeliveryActionForInvoiceReport">All Invoice</a></li> <!-- inputDateIA.php invoiceReport.php -->
            <li><a href="actionController.php?
					             action=seller/retrieveDeliveryActionForInvoiceReportD">Date</a></li><!-- inputDateID.php invoiceReportByDate.php -->
            <li><a href="actionController.php?
					             action=seller/retrieveDeliveryActionForInvoiceReportN">Invoice No.</a></li><!-- inputDateINo.php invoiceReportByNo.php -->
            <li><a href="actionController.php?
					             action=seller/retrieveDeliveryActionForInvoiceReportC">Contact</a></li><!-- inputDateIC.php invoiceReportByContact.php -->
            <li><a href="actionController.php?
					             action=seller/retrieveDeliveryActionForInvoiceReportNm">Name</a></li><!-- inputDateINm.php invoiceReportByName.php -->
			<li><a href="actionController.php?
					             action=seller/retrieveDeliveryActionForInvoiceReportSl">Seller</a></li><!-- inputDateISl.php invoiceReportBySeller.php -->					 
            <li><a href="actionController.php?
					             action=seller/retrieveDeliveryActionForInvoiceReportPj">Project Name</a></li><!-- inputDateIPj.php invoiceReportByProject.php -->
            <li><a href="actionController.php?
					             action=seller/retrieveDeliveryActionForInvoiceReportV">Value(Desc.)</a></li><!-- inputDateIV.php invoiceReportByValue.php -->
        </ul>
    </li>
    <li><a href="#"></a></li>
</ul>
	
<?php
error_reporting(0);

		include "functions/dblink.php";  
		$sql = "SELECT * FROM invc001 WHERE (ILOCK='0' OR ILOCK='') ORDER BY IDATE DESC,IDNO 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/retrieveInvoiceAction
															&$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);

		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 invc001 WHERE (ILOCK='0' OR ILOCK='' OR ILOCK IS NULL) AND 
		                      IDATE>='$strKeyword1'  AND 
		                      IDATE<='$strKeyword2' 
                              ORDER BY IDATE DESC,INO DESC";
		}

		if(isset($_POST["txtSeller"])){
			$seller=$_POST["txtSeller"];
			if($seller==""){
				$sql = "SELECT * FROM invc001 WHERE (ILOCK='0' OR ILOCK='' OR ILOCK IS NULL) AND 
		                      IDATE>='$strKeyword1'  AND 
		                      IDATE<='$strKeyword2' 
                              ORDER BY IDATE DESC,INO DESC";
			}else{
				$sql = "SELECT * FROM invc001 WHERE (ILOCK='0' OR ILOCK='' OR ILOCK IS NULL) AND 
		                      IDATE>='$strKeyword1'  AND 
		                      IDATE<='$strKeyword2'  AND
							  IDSMFNAME LIKE '%$seller%'
                              ORDER BY IDATE DESC,INO DESC";
			}
		}

		$perpage=20;
		$find = "ทั้งหมด";
		$condition = 1;	
		
		$result=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>รายการใบแจ้งหนี้ (Invoice List)</h1>";
	
		echo "<span id=\"result\">รายการที่ : $first - $last จากทั้งหมด $total</span>
					 </caption>";
	
		echo "<tr id='headi' >";      //ส่วนหัวตาราง(ชื่อฟิลด์)
		echo "<th width='5%'>act</th>";
	    echo "<th width='9%'>วันที่</th>";
		echo "<th width='9%'>Inv.No.</th>";
        echo "<th width='9%'>Delv.No.</th>"; 
		echo "<th width='15%'>name</th>";
        echo "<th width='9%'>ส่วนลด</th>";
        echo "<th width='22%'>รายการสินค้า</th>";	 
        echo "<th width='4%'>Qty</th>";
		echo "<th width='8%'>@</th>";
		echo "<th width='10%'>รวมทั้งสิ้น</th>";
		echo "</tr>";
	
		while($data=mysqli_fetch_array($rs))
		{	
			error_reporting(~E_NOTICE );
			echo "<tr>";			
			echo "<td>
					<a href=\"report-content/invoiceIndReport.php?no={$data['NO']}&ino={$data['INO']}&ilost={$data['ILOST']}\">[พิมพ์]</a>
				  </td>";//เวลานำไปใช้ $no=$_GET['no'];
			if($data['IDC_DISC']>100){
				$t1=($data['IDQTY1']*$data['IDSLEPRC1']);
				$t2=($data['IDQTY2']*$data['IDSLEPRC2']);
				$t3=($data['IDQTY3']*$data['IDSLEPRC3']);
				$t4=($data['IDQTY4']*$data['IDSLEPRC4']);
				$t5=($data['IDQTY5']*$data['IDSLEPRC5']);
				$t6=($data['IDQTY6']*$data['IDSLEPRC6']);
				$t7=($data['IDQTY7']*$data['IDSLEPRC7']);
				$t8=($data['IDQTY8']*$data['IDSLEPRC8']);
				$t9=($data['IDQTY9']*$data['IDSLEPRC9']);
				$t10=($data['IDQTY10']*$data['IDSLEPRC10']);
				$tot=($t1+$t2+$t3+$t4+$t5+$t6+$t7+$t8+$t9+$t10)-($data['IDC_DISC']);
			}else {
						$t1=($data['IDQTY1']*$data['IDSLEPRC1'])-(($data['IDQTY1']*$data['IDSLEPRC1'])*$data['IDC_DISC']/100);
						$t2=($data['IDQTY2']*$data['IDSLEPRC2'])-(($data['IDQTY2']*$data['IDSLEPRC2'])*$data['IDC_DISC']/100);
						$t3=($data['IDQTY3']*$data['IDSLEPRC3'])-(($data['IDQTY3']*$data['IDSLEPRC3'])*$data['IDC_DISC']/100);
						$t4=($data['IDQTY4']*$data['IDSLEPRC4'])-(($data['IDQTY4']*$data['IDSLEPRC4'])*$data['IDC_DISC']/100);
						$t5=($data['IDQTY5']*$data['IDSLEPRC5'])-(($data['IDQTY5']*$data['IDSLEPRC5'])*$data['IDC_DISC']/100);
						$t6=($data['IDQTY6']*$data['IDSLEPRC6'])-(($data['IDQTY6']*$data['IDSLEPRC6'])*$data['IDC_DISC']/100);
						$t7=($data['IDQTY7']*$data['IDSLEPRC7'])-(($data['IDQTY7']*$data['IDSLEPRC7'])*$data['IDC_DISC']/100);
						$t8=($data['IDQTY8']*$data['IDSLEPRC8'])-(($data['IDQTY8']*$data['IDSLEPRC8'])*$data['IDC_DISC']/100);
						$t9=($data['IDQTY9']*$data['IDSLEPRC9'])-(($data['IDQTY9']*$data['IDSLEPRC9'])*$data['IDC_DISC']/100);
						$t10=($data['IDQTY10']*$data['IDSLEPRC10'])-(($data['IDQTY10']*$data['IDSLEPRC10'])*$data['IDC_DISC']/100);
						$tot=($t1+$t2+$t3+$t4+$t5+$t6+$t7+$t8+$t9+$t10);
			}
					
			echo "<td>".date_format(date_create("$data[IDATE]"),"d-m-Y")."</td>";
			echo "<td>$data[INO]</td>";
        	echo "<td>$data[IDNO]</td>";    
			echo "<td>$data[IDC_NAME]</td>";
        	echo "<td class=rt>".number_format($data['IDC_DISC'],2)."</td>";    
        	echo "<td>$data[IDPDNAME1]</td>"; 
		
		$DwnP=$tot*$data['IDTERM1']/100;
		$GTot=$tot-$DwnP;	
			
		if($data['ILOST']=='Dwn'){
			echo "<td>".number_format($data['IDTERM1'])."%</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['IDQTY1'])."</td>";
			echo "<td class=rt>".number_format($data['IDSLEPRC1'])."</td>";
			echo "<td color:red; class=rtq>".number_format($GTot,2)."</td>";
			echo "<tr>";
			
			for($i=2;$i<=10;$i++){
				$blank=NULL;
				if ($data["IDPDNAME{$i}"] != "" OR $data["IDQTY{$i}"] != 0 OR $data["IDQTY{$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["IDPDNAME{$i}"]."</td>";
					echo "<td class=rt>".number_format($data["IDQTY{$i}"])."</td>";
					echo "<td class=rt>".number_format($data["IDSLEPRC{$i}"])."</td>";
					echo "<tr>";
				}
			}
			
			
		}
			echo "<tr>";
			echo "<th width=' 5%'><hr color='#DBDBDB' size='.1'></th>";
			echo "<th width=' 9%'><hr color='#DBDBDB' size='.1'></th>";
			echo "<th width=' 9%'><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=' 9%'><hr color='#DBDBDB' size='.1'></th>";
			echo "<th width='22%'><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='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>