<!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 Payment</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";

$no=$_GET['no'];
$values=$_GET['qno'];//=>QNO มาจาก quotationListForOrder.php
$qterm1=$_GET['qterm1'];//=>QTERM1 มาจาก quotationListForOrder.php
//echo '$qterm1 = '.$qterm1.'<br>';

//$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";//INO Record สุดท้าย
$query=mysqli_query($con,$sql);
$result=mysqli_fetch_assoc($query);
$ono=$result['ONO'];//เลข Order ที่เข้ามา	
$data=substr($result['ONO'],4,6);//หมายเลข Order สุดท้ายของปีนี้
$year=substr($result['ONO'],0,2);//ปี Record สุดท้าย
//echo '$ono = '.$ono.'<br>';
//echo '$data = '.$data.'<br>';
//echo '$year = '.$year.'<br>';

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="";}
//$ono=$substr.$k.$num;	
$x=$substr.$k.$num;
//echo '$x = '.$x.'<br>';

$dwn="SELECT * FROM ordr001 WHERE OQNO='$values' AND `OLOST`='Dwn' ";
$queryDwn=mysqli_query($con,$dwn);
$resultDwn=mysqli_fetch_assoc($queryDwn);
$olost=$resultDwn['OLOST'];
//echo '$olost = '.$olost.'<br>';
if($resultDwn==false){//ใน Order ไม่มี Downpayment แล้ว
	if($qterm1>0){//Quotation ใบนี้มี Downpayment
		echo "<script>
			alert('รายการ Quotation ใบนี้มี Downpayment ต้องกลับไปทำ Downpayment ก่อน.\\n\\nกด OK เพื่อนกลับ Order List Page');
			window.location='actionController.php?action=seller/retrieveOrderAction';
			</script>";
	}else{
	//เพิ่ม Order
	$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(),'',''
			FROM quot002 
			WHERE quot002.NO='$no'";
			$insert=mysqli_query($con,$sql);
			if (!$insert){
				echo "<script>
						alert('การบันทึกข้อมูล Order เกิดข้อผิดพลาด.\\n\\nกด OK เพื่อนกลับ Order List Page');
						window.location='actionController.php?action=seller/retrieveOrderAction';
					  </script>";
			}else{
					$sqlq = "UPDATE quot002 SET quot002.QLOCK=1 WHERE quot002.NO='$no'";
					mysqli_query($con, $sqlq);
					echo "<script>
							alert('บันทึกข้อมูล Order แล้ว.\\n\\nกด OK เพื่อนกลับ Order List Page');
							window.location='actionController.php?action=seller/retrieveOrderAction';
						  </script>";
					echo '</body></html>';
				 }
	}			 

}else{//Quotation ใบนี้มี Downpayment
	//ตรวจสอบ Order: ใบนี้มีการทำ Downpayment หรือยัง ?
		$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 
		$ono,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(),'',''
	FROM quot002 
	WHERE quot002.NO='$no'";
	$insert=mysqli_query($con,$sql);
	if (!$insert){
		echo "<script>
				alert('การบันทึกข้อมูล Order เกิดข้อผิดพลาด.\\n\\nกด OK เพื่อนกลับ Order List Page');
				window.location='actionController.php?action=seller/retrieveOrderAction';
			</script>";
	}else{
			$sqlq = "UPDATE quot002 SET quot002.QLOCK=1 WHERE quot002.NO='$no'";
			mysqli_query($con, $sqlq);
			echo "<script>
					alert('บันทึกข้อมูล Order แล้ว.\\n\\nกด OK เพื่อนกลับ Order List Page');
					window.location='actionController.php?action=seller/retrieveOrderAction';
				</script>";
			echo '</body></html>';
		}

	}

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>