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

<head>
	<meta charset="UTF-8">
	<title>Add Order DownPayment</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: 10px;
			width: 830px;
			padding: 5px;
			color: crimson;
			background: #ddf;
			border: 2px solid rgba(186, 186, 186, 1.00);
		}

		#bg p {
			font-size: 20px;
		}
	</style>
</head>

<body>
<?php
error_reporting(0);

include "functions/dblink.php";

$values=$_GET['no'];//=>QNO

$serch="SELECT * FROM ordr001 WHERE OQNO = $values LIMIT 1";
$qSerch=mysqli_query($con,$serch);
$fSerch=mysqli_fetch_assoc($qSerch);
$rslt=$fSerch['OLOST'];
//echo "<script>alert('Hello $rslt')</script>";

$strDate = date("y"); //2 ตัวหลังของปีปัจจุบัน
$sql="SELECT * FROM ordr001 WHERE YEAR(ODATE)=YEAR(CURDATE())";	//ทั้งหมดในปีปัจจุบัน
$query=mysqli_query($con,$sql);
$rows=mysqli_num_rows($query);

$sql = "SELECT * FROM ordr001 
				WHERE year(`ODATE`)=DATE_FORMAT(curdate(),'%Y') 
				ORDER BY ONO DESC LIMIT 1";//Record สุดท้ายของปีนี้
$query=mysqli_query($con,$sql);
$result=mysqli_fetch_assoc($query);	
$data=substr($result['ONO'],4,6);//หมายเลข Order สุดท้ายของปีนี้

//หมายเลข Order สุดท้ายของปีนี้ บวก 1 เป็น Record ใหม่	
if($data>0){$num=$data+1;}else{$num=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;	

//เพิ่ม Downpayment
if($rslt=='Dwn'){
		echo "<script>
				alert('ทำรายการ Down Payment ไปแล้ว กด OK เพื่อนกลับ Page เดิม');
				window.location='actionController.php?action=seller/retrieveOrderAction';
			  </script>";			
}else{//เพิ่มข้อมูล
	$sql="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,
				OQSUMDISC,OQSUM,OQSMFNAME,OQSMLNAME,OQPRCV,OQSENT,
				OQTERM1,OQTERM2,OQAFTER,OQSPEC,OQSPECNAME,OQNOTE,OPDATE,OLOCK,OLOST)
			SELECT 
				$x,curdate(),QNO,QDATE,QC_NO,QC_SUR,QC_NAME,QC_CONTRCT,
				QC_TAX_NO,QC_DISC,QC_ADRS,QC_DSTRT,QC_PRVN,QC_TEL,QC_FAX,QPJNAME,
				QPDNO1,QPDNAME1,QQTY1,QSLEPRC1,QPDDISC1,
				QPDNO2,QPDNAME2,QQTY2,QSLEPRC2,QPDDISC2,
				QPDNO3,QPDNAME3,QQTY3,QSLEPRC3,QPDDISC3,
				QPDNO4,QPDNAME4,QQTY4,QSLEPRC4,QPDDISC4,
				QPDNO5,QPDNAME5,QQTY5,QSLEPRC5,QPDDISC5,
				QPDNO6,QPDNAME6,QQTY6,QSLEPRC6,QPDDISC6,
				QPDNO7,QPDNAME7,QQTY7,QSLEPRC7,QPDDISC7,
				QPDNO8,QPDNAME8,QQTY8,QSLEPRC8,QPDDISC8,
				QPDNO9,QPDNAME9,QQTY9,QSLEPRC9,QPDDISC9,
				QPDNO10,QPDNAME10,QQTY10,QSLEPRC10,QPDDISC10,
				QSUMDISC,QSUM,QSMFNAME,QSMLNAME,QPRCV,QSENT,
				QTERM1,QTERM2,QAFTER,QSPEC,QSPECNAME,QNOTE,curdate(),'','Dwn'
			FROM quot002 
			WHERE quot002.QNO='$values' ";
	$insert=mysqli_query($con,$sql);
	echo "<script>
			alert('ข้อมูลถูกบันทึกแล้ว');
			window.location='actionController.php?action=seller/retrieveOrderAction';
		  </script>";
	echo '</body></html>';
	back();			
}

function back()
{
	global $con;
	mysqli_close($con);
	header('location:actionController.php?action=seller/retrieveOrderAction');
	echo '</body></html>';//->orderList.php
	//exit("<p><a href='actionController.php?action=seller/retrieveOrderAction'>ย้อนกลับ</a></p></body></html>");
}

mysqli_close($con);

?>

</body>
</html>