<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
	<meta charset="UTF-8">
	<title>Order Form(Add Data)</title>
	<style>
		* {
			font: 12px tahoma;
		}

		#all {
			margin: auto;
			width: 833px;
			padding: 5px;
			background: #ddf;
		}

		body {
			background: url(images/bg.jpg);
			min-width: 700px;
		}

		select {
			background: #eee none repeat scroll 0 0;
			border: 1px solid gray;
		}

		input {
			background: #eee none repeat scroll 0 0;
		}

		#bg {
			margin-top: 10px;
			width: 830px;
			padding: 5px;
			color: crimson;
			background: #ddf;
			border: 2px solid rgba(186, 186, 186, 1.00);
		}

		#bg p {
			font-size: 20px;
		}

		.rt {
			text-align: right;
		}
	</style>
</head>

<script language="JavaScript" type="text/javascript">
	var HttPRequest = false;

	function nextbox(e, id) {
		// อ่าน keycode (cross browser)
		var keycode = e.which || e.keyCode;
		// ตรวจสอบ keycode (13 คือ กด enter)
		if (keycode == 13) {
			// ย้ายโฟกัสไปยัง input ที่ id
			document.getElementById(id).focus();
			// return false เพื่อยกเลิกการ submit form
			return false;
		}
	}
</script>

<body>
	<?php
	error_reporting(0);

	include "functions/dblink.php";
	$no = mysqli_query($con,"SELECT * FROM ordr001");
	/*$sql = "SELECT * FROM ordr001 
	        WHERE YEAR(ODATE)=YEAR(CURDATE())
			ORDER BY ODATE DESC LIMIT 1";*/
	// $sql = "SELECT * FROM `ordr001` ORDER BY NO DESC LIMIT 1";//Record สุดท้าย	
	$sql = "SELECT * FROM `ordr001` WHERE YEAR(`ODATE`)=YEAR(CURDATE()) ORDER BY ONO DESC LIMIT 1";	

	$query=mysqli_query($con,$sql);		
	$rows = mysqli_num_rows($no);//จำนวนแถว
	$strDate = date("Y")."<br/>";

	$result = mysqli_fetch_array($query);
	//$ono = $result['ONO'];
	//$year=$result['ODATE'];
	$data=substr($result['ONO'],4,6);//หมายเลข Order สุดท้าย
	$year=substr($result['ONO'],0,2);//ปี Record สุดท้าย

	//if (date("Y") == substr($year,0,4) OR $rows!=0) {$num = substr($ono, 4, 6) + 1;} 
	//else {$num = 1;}
	if(date('y')<>$year){$num=1;}
			   else {$num=$data+1;}		   
	
	$substr = DATE('y').DATE('m');
	if(strlen((string)$num)==1){$k="00000";}
				elseif(strlen((string)$num)==2){$k="0000";}
					elseif(strlen((string)$num)==3){$k="000";}
						elseif(strlen((string)$num)==4){$k="00";}
							elseif(strlen((string)$num)==5){$k="0";}
								else{$k="";}							
			
	$x=$substr.$k.$num;

	//if(isset($_POST['NO']))
	//{//นำข้อมูลจากตัวแปร $_POST ทีเหลือมาเรียงต่อเป็นสตริงเดียวกันโดยขั้นด้วย ' , '
	$values = $_GET['NO'];
	//$values=implode("','",$_GET);     //ลักษณะผลลัพธ์เช่น  a',  'b',  'c', 'd	
	//ปิดหัว ท้าย ด้วย ' เพื่อให้ครบคู่ ลักษณะผลลัพธ์จะเป็น 	'a',  'b',  'c', 'd'
	//$values="'".$values."'";
	$sql = "SELECT * FROM quot002 WHERE NO=$values";
	$result = mysqli_query($con, $sql);
	$num = mysqli_num_rows($result); // แสดงค่าออกมามันเห็นแค่แถวเดียวที่จริงน่าจะ 3 แถว
	$i = 0;
	while ($i < $num) {
		$data = mysqli_fetch_array($result);
		$i++;
	}
	$prc1 = $data["QQTY1"] * $data["QSLEPRC1"];
	$dsc1 = ($prc1 * $data["QC_DISC"] / 100);
	$tot1 = $prc1 - $dsc1;

	$prc2 = $data["QQTY2"] * $data["QSLEPRC2"];
	$dsc2 = ($prc2 * $data["QC_DISC"] / 100);
	$tot2 = $prc2 - $dsc2;

	$prc3 = $data["QQTY3"] * $data["QSLEPRC3"];
	$dsc3 = ($prc3 * $data["QC_DISC"] / 100);
	$tot3 = $prc3 - $dsc3;

	$prc4 = $data["QQTY4"] * $data["QSLEPRC4"];
	$dsc4 = ($prc4 * $data["QC_DISC"] / 100);
	$tot4 = $prc4 - $dsc4;

	$prc5 = $data["QQTY5"] * $data["QSLEPRC5"];
	$dsc5 = ($prc5 * $data["QC_DISC"] / 100);
	$tot5 = $prc5 - $dsc5;

	$prc6 = $data["QQTY6"] * $data["QSLEPRC6"];
	$dsc6 = ($prc6 * $data["QC_DISC"] / 100);
	$tot6 = $prc6 - $dsc6;

	$prc7 = $data["QQTY7"] * $data["QSLEPRC7"];
	$dsc7 = ($prc7 * $data["QC_DISC"] / 100);
	$tot7 = $prc7 - $dsc7;

	$prc8 = $data["QQTY8"] * $data["QSLEPRC8"];
	$dsc8 = ($prc8 * $data["QC_DISC"] / 100);
	$tot8 = $prc8 - $dsc8;

	$prc9 = $data["QQTY9"] * $data["QSLEPRC9"];
	$dsc9 = ($prc9 * $data["QC_DISC"] / 100);
	$tot9 = $prc9 - $dsc9;

	$prc10 = $data["QQTY10"] * $data["QSLEPRC10"];
	$dsc10 = ($prc10 * $data["QC_DISC"] / 100);
	$tot10 = $prc10 - $dsc10;

	for ($n = 1; $n <= 10; $n++) {
		$dsc = $dsc + $dsc["{$n}"];
		$tot = $tot + $tot["{$n}"];
	}

	$gtot = $data["QSUM"] - $data["QSUMDISC"];

	$sqli = "INSERT INTO ordr001 
	  (ONO,ODATE,OQNO,OQDATE,OQC_NO,OQC_SUR,OQC_NAME,OQC_CONTRCT,
	   OQC_TAX_NO,OQC_DISC,OQC_ADRS,OQC_DSTRT,OQC_PRVN,OQC_TEL,OQC_FAX,OQPJNAME,
						 OQPDNO1,OQPDNAME1,OQQTY1,OQSLEPRC1,OQPDDISC1,
						 OQPDNO2,OQPDNAME2,OQQTY2,OQSLEPRC2,OQPDDISC2,
						 OQPDNO3,OQPDNAME3,OQQTY3,OQSLEPRC3,OQPDDISC3,
						 OQPDNO4,OQPDNAME4,OQQTY4,OQSLEPRC4,OQPDDISC4,
						 OQPDNO5,OQPDNAME5,OQQTY5,OQSLEPRC5,OQPDDISC5,
						 OQPDNO6,OQPDNAME6,OQQTY6,OQSLEPRC6,OQPDDISC6,
						 OQPDNO7,OQPDNAME7,OQQTY7,OQSLEPRC7,OQPDDISC7,
						 OQPDNO8,OQPDNAME8,OQQTY8,OQSLEPRC8,OQPDDISC8,
						 OQPDNO9,OQPDNAME9,OQQTY9,OQSLEPRC9,OQPDDISC9,
						 OQPDNO10,OQPDNAME10,OQQTY10,OQSLEPRC10,OQPDDISC10,
	  OQSUM,OQSUMDISC,OQSMFNAME,OQSMLNAME,OQPRCV,OQSENT,OQTERM1,OQTERM2,
	  OQAFTER,OQSPEC,OQSPECNAME,OQNOTE,OPDATE,OLOCK,OLOST) 
		                VALUES 
						('$x','$odate','$oqno','$dt','$oqcno','$oqcsur','$oqcname','$oqccontrct',
						'$oqctaxno','$oqcdisc','$oqcadrs','$oqcdstrt','$oqcprvn','$tel','$fax','$pjname',
						'$no1','$name1','$qty1','$sleprc1','$disc1',
						'$no2','$name2','$qty2','$sleprc2','$disc2',
						'$no3','$name3','$qty3','$sleprc3','$disc3',
						'$no4','$name4','$qty4','$sleprc4','$disc4',
						'$no5','$name5','$qty5','$sleprc5','$disc5',
						'$no6','$name6','$qty6','$sleprc6','$disc6',
						'$no7','$name7','$qty7','$sleprc7','$disc7',
						'$no8','$name8','$qty8','$sleprc8','$disc8',
						'$no9','$name9','$qty9','$sleprc9','$disc9',
						'$no10','$name10','$qty10','$sleprc10','$disc10',
						'$sum','$sumd','$fn','$ln','$pv','$st','$tm1','$tm2',
						'$aft','$spc','$spcn','$nt','$dt','$lk','$lt')";

	$insert = mysqli_query($con, $sqli);

	if (!$insert) {
		echo mysqli_error($con);
	} else { ?>
		<div id="bg" align="center">
			<!-- <p>Username and Password Incorrect !</p> -->
			<p>--- ข้อมูลบันทึกแล้ว ---</p>
			<br>
		<?php
		back();
	} //ฟังค์ชั่นนี้อยู่ในขั้นตอน  1	
		?>
		</div>
		<?php
		function back()
		{
			global $con;
			mysqli_close($con);
			exit("<p><a href='actionController.php?action=seller/retrieveOrderAction'>ย้อนกลับ</a></p></body></html>");
		}
		mysqli_close($con);
		?>

		<div style='font-size:18px; color:red;'><?php echo "เพิ่ม Order"; ?></div>
		<div id="all" align="center">

			<form method="post" action="quotationListForOrder.php" name="addOrderForm">
				<table width="833">
					<tr>
						<td style='font-size:14px; color:blue;'><strong>รายละเอียด</strong></td>
					</tr>
					<tr>
						<td width="71"><input name="ONO" type="hidden" value="<?php echo $ono = $x; ?>"></td><!--$data["QNO"]-->
					</tr>

					<input type="hidden" name="OQDATE" id="OQDATE" size="19" value="<?php echo $dt = date("Y-m-d", strtotime($data["QDATE"])); ?>">
				</table>
				<table>
					<tr>
						<td width="71" style='text-align:right'>Order No. :</td>
						<td width="81"><input type="text" name="OQNO" size="10" value="<?php echo $oqno=$data["QNO"]; ?>" readonly></td>

						<td width="40" style='text-align:right'>วันที่ :</td>
						<td width="621"><input type="text" name="ODATE" id="ODATE" value="<?php echo $odate = date("d-m-Y"); ?>" onkeydown="return nextbox(event, 'OQC_NO');"></td>
					</tr>
				</table>

				<table width="830" height="29">
					<tr>
						<td class="rt" width="68">หมายเลข :</td>
						<td width="750"><input type="text" name="OQC_NO" id="OQC_NO" size="10" value="<?php echo $oqcno = $data["QC_NO"]; ?>" onkeydown="return nextbox(event, 'OQC_SUR');"></td>
					</tr>
				</table>

				<table>
					<tr>
						<td class="rt" width="69">Name :</td>
						<td width="52"><input type="text" name="OQC_SUR" id="OQC_SUR" size="8" value="<?php echo $oqcsur = $data["QC_SUR"]; ?>" onkeydown="return nextbox(event, 'OQC_NAME);"></td>

						<td width="696"><input type="text" name="OQC_NAME" id="OQC_NAME" size="50" value="<?php echo $oqcname = $data["QC_NAME"]; ?>" onkeydown="return nextbox(event,'OQC_CONTRCT');"></td>
					</tr>
				</table>

				<table>
					<tr>
						<td class="rt" width="75">ผู้ติดต่อ :</td>
						<td width="302"><input type="text" name="OQC_CONTRCT" id="OQC_CONTRCT" size="35" value="<?php echo $oqccontrct = $data["QC_CONTRCT"]; ?>" onkeydown="return nextbox(event,'OQC_TAX_NO');"></td>

						<td class="rt" width="59">Tax No. :</td>
						<td width="125"><input type="text" name="OQC_TAX_NO" id="OQC_TAX_NO" size="13" value="<?php echo $oqctaxno = $data["QC_TAX_NO"]; ?>" onkeydown="return nextbox(event,'oqc_disc');"></td>
					</tr>
					<tr>
						<td class="rt" width="75">ส่วนลด :</td>
						<td width="302"><input type="text" name="oqc_disc" id="oqc_disc" size="16" style="text-align: right" value="<?php echo $oqcdisc = $data["QC_DISC"]; ?>" onkeydown="return nextbox(event,'OQC_ADRS');"> % </td>
					</tr>
					<tr>
						<td class="rt" width="75">ที่อยู่ :</td>
						<td width="302"><input type="text" name="OQC_ADRS" id="OQC_ADRS" size="50" value="<?php echo $oqcadrs = $data["QC_ADRS"]; ?>" 
							onkeydown="return nextbox(event,'OQC_DSTRT');"></td>

						<td class="rt" width="59">อำเภอ :</td>
						<td width="125"><input type="text" name="OQC_DSTRT" id="OQC_DSTRT" size="20" value="<?php echo $oqcdstrt = $data["QC_DSTRT"]; ?>" 
							onkeydown="return nextbox(event,'OQC_PRVN');"></td>

						<td class="rt" width="56">จังหวัด :</td>
						<td width="188"><input type="text" name="OQC_PRVN" id="OQC_PRVN" size="25" value="<?php echo $oqcprvn = $data["QC_PRVN"]; ?>" 
							onkeydown="return nextbox(event,'OQC_TEL');"></td>
					</tr>
				</table>
				<table>
					<tr>
						<td class="rt" width="71">Tel. :</td>
						<td width="90"><input type="text" name="OQC_TEL" id="OQC_TEL" size="14" value="<?php echo $tel = $data["QC_TEL"]; ?>" 
							onkeydown="return nextbox(event,'OQC_FAX');"></td>

						<td class="rt" width="45">Fax. :</td>
						<td width="100"><input type="text" name="OQC_FAX" id="OQC_FAX" size="14" value="<?php echo $fax = $data["QC_FAX"]; ?>" 
							onkeydown="return nextbox(event,'OQPJNAME');"></td>

						<td class="rt" width="100">Proj. :</td>
						<td width="399"><input type="text" name="OQPJNAME" id="OQPJNAME" size="40" value="<?php echo $pjname = $data["QPJNAME"]; ?>" 
							onkeydown="return nextbox(event,'OPDNO1');"></td>
					</tr>
				</table>
				<table width="782">
					<tr align="center">
						<td width="10"></td>
						<td width="77">รายการ</td>
						<td width="220">ชื่อสินค้า</td>
						<td width="111">จำนวน</td>
						<td width="107">ราคาตั้ง/หน่วย</td>
						<td width="76">ส่วนลด</td>
						<td width="149">รวมขาย</td>
					</tr>
					<?php
					for ($i = 1; $i <= 10; $i++) { ?>
						<tr>
							<td><?php echo $i; ?>.</td>
							<td><input type="text" name="OQPDNO" <?php echo $i; ?> id="OQPDNO" <?php echo $i; ?> size="10" 
								value="<?php echo $no["{$i}"] = $data["QPDNO{$i}"]; ?>" 
								onkeydown="return nextbox(event,'OQPDNAME<?php echo $i; ?>');"></td>

							<td><input type="text" name="OQPDNAME" <?php echo $i; ?> id="OQPDNAME" <?php echo $i; ?> size="35" 
								value="<?php echo $name["{$i}"] = $data["QPDNAME{$i}"]; ?>" 
								onkeydown="return nextbox(event,'OQQTY<?php echo $i; ?>');"></td>

							<td><input type="text" name="OQQTY" <?php echo $i; ?> id="OQQTY" <?php echo $i; ?> size="17" style="text-align: right" 
								value="<?php echo $qty["{$i}"] = $data{"QQTY{$i}"}; ?>" 
								onkeydown="return nextbox(event,'<?php if ($i <= 9) {echo "QPDNO" . ($i + 1);} else {echo "OQSMFNAME";}?>');"></td>

							<td><input type="text" name="OQSLEPRC" <?php echo $i; ?> id="OQSLEPRC" <?php echo $i; ?> size="17" style="text-align: right" 
								value="<?php echo $sleprc["{$i}"] = number_format($data{"QSLEPRC{$i}"});?>"></td>

							<?php $disc["{$i}"] = $data{"QPDDISC{$i}"}; ?>
							<td><input type="text" name="tdisc" <?php $i; ?> id="tdisc" <?php $i; ?> size="12" style="text-align: right" 
								value="<?php echo number_format($dsc["{$i}"]); ?>"></td>
							<td><input type="text" name="tsle" <?php $i; ?> id="text" name="tsle" <?php $i; ?> style="text-align: right" disabled 
								value="<?php echo number_format($tot["{$i}"]); ?>"></td>
						</tr>
					<?php } ?>

					<td></td>
					<td></td>
					<td></td>
					<td width="129" style='text-align:right'>รวม :</td>

					<td width="117"><input type="text" name="oqsum" id="oqsum" size="17" style="text-align: right" 
						value="<?php echo $sum = number_format($data["QSUM"]);?>"></td>

					<td width="82"><input type="text" name="oqsumdisc" id="oqsumdisc" size="12" style="text-align: right" 
						value="<?php echo $sumd = number_format($data["QSUMDISC"]);?>"></td>

					<td width="149"><input type="text" name="tsle" id="tsle" style="text-align: right" disabled 
						value="<?php echo number_format($gtot); ?>"></td>
					</tr>
				</table>

				<table width="833">
					<tr>
						<td class="rt" width="75">ผู้ขาย :</td>
						<td width="146"><input type="text" name="OQSMFNAME" id="OQSMFNAME" size="20" value="<?php echo $fn = $data["QSMFNAME"]; ?>" 
							onkeydown="return nextbox(event,'OQSMLNAME');"></td>
						<td width="596"><input type="text" name="OQSMLNAME" id="OQSMLNAME" size="30" value="<?php echo $ln = $data["QSMLNAME"]; ?>" 
							onkeydown="return nextbox(event,'OQPRCV');"></td>
					</tr>
				</table>

				<table width="833">
					<tr>
						<td class="rt" width="75">ยืนราคา :</td>
						<td width="291"><input type="text" name="OQPRCV" id="OQPRCV" size="6" value="<?php echo $pv = $data["QPRCV"]; ?>" 
						    onkeydown="return nextbox(event,'OQSENT');"> วัน </td>
					</tr>
					<tr>
						<td class="rt" width="75">กำหนดส่ง :</td>
						<td width="291"><input type="text" name="OQSENT" id="OQSENT" size="6" value="<?php echo $st = $data["QSENT"]; ?>" 
							onkeydown="return nextbox(event,'OQTERM1');"> วัน </td>
					</tr>
					<tr>
						<td class="rt" width="75">ล่วงหน้า :</td>
						<td width="291"><input type="text" name="OQTERM1" id="OQTERM1" size="17" value="<?php echo $tm1 = $data["QTERM1"]; ?>" 
							onkeydown="return nextbox(event,'OQTERM2');"> บาท </td>
					</tr>
					<tr>
						<td class="rt" width="75">คงเหลือ :</td>
						<td width="291"><input type="text" name="OQTERM2" id="OQTERM2" size="17" value="<?php echo $tm2 = $data["QTERM2"]; ?>" 
							onkeydown="return nextbox(event,'OQAFTER');"> บาท </td>
					</tr>
					<tr>
						<td class="rt" width="75">กำหนดชำระ :</td>
						<td width="291"><input type="text" name="OQAFTER" id="OQAFTER" size="6" value="<?php echo $aft = $data["QAFTER"]; ?>" 
							onkeydown="return nextbox(event,'OQSPEC');"> วัน </td>
					</tr>
					<tr>
						<td class="rt" width="75">ค่าออกแบบ :</td>
						<td width="291"><input type="text" name="OQSPEC" id="OQSPEC" size="8" value="<?php echo $spc = $data["QSPEC"]; ?>" 
							onkeydown="return nextbox(event,'OQSPECNAME');"> บาท </td>
						<td width="222"></td>
						<td width="225"></td>
					</tr>

					<tr>
						<td class="rt" width="75">ผู้ออกแบบ :</td>
						<td width="291"><input type="text" name="OQSPECNAME" id="OQSPECNAME" size="30" value="<?php echo $spcn = $data["QSPECNAME"]; ?>" 
							onkeydown="return nextbox(event,'OQNOTE');"></td>
					</tr>
				</table>

				<table>

					<tr>
						<td class="rt" width="75">Note :</td>
						<td width="746"><input type="text" name="OQNOTE" id="OQNOTE" size="90" value="<?php echo $nt = $data["QNOTE"]; ?>" 
							onkeydown="return nextbox(event,'ODATE');"></td>
					</tr>

				</table>
				<table>
					<td><input name="OQPDATE" type="hidden" value="<?php echo date('Y-m-d'); ?>" </td>
					<td><input name="OQLOCK" type="hidden" value="<?php $lk = '';  ?>" </td>
					<td><input name="OQLOST" type="hidden" value="<?php $lt = '';  ?>" </td>
				</table>
				<td></td>
				<td><button>บันทึกข้อมูล(Save)</button> &nbsp;&nbsp;<a href='actionController.php?action=seller/retrieveOrderAction'>ย้อนกลับ</a></td>

			</form>
		</div>
</body>

</html>