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

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

		#all {
			margin-top: 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;
		}
	</style>
</head>

<body>
<?php
	error_reporting(0);
	include "functions/dblink.php";

	$values = $_GET['ono'];
	$olost=$_GET['olost'];

	$strDate = date("y"); //2 ตัวหลังของปีปัจจุบัน
	$sql = "SELECT * FROM delv001
			WHERE YEAR(DDATE)=YEAR(CURRENT_DATE())";
	$query = mysqli_query($con, $sql);
	$rows = mysqli_num_rows($query); //จำนวนแถว
	if ($rows > 0) {
		$num = $rows + 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;

	if($olost=='Dwn'){	
		$sqlDwn = "SELECT * FROM delv001
			       WHERE DONO=$values AND DLOST='Dwn'";
		$queryDwn = mysqli_query($con, $sqlDwn);
		$dataDwn = mysqli_fetch_assoc($queryDwn);
		
		if($dataDwn['DNO']==''){
			/*INSERT INTO Table1 (field1,field2,...) SELECT field1,field2,... FROM Table2 WHERE Condition ;*/
			$sql = "INSERT INTO delv001 
			(DNO,DDATE,DONO,DODATE,DOC_NO,DOC_SUR,DOC_NAME,DOC_CONTRCT,
			DOC_TAX_NO,DOC_DISC,DOC_ADRS,DOC_DSTRT,DOC_PRVN,DOC_TEL,DOC_FAX,DOPJNAME,
					DOPDNO1,DOPDNAME1,DOQTY1,DOSLEPRC1,DOPDDISC1,
					DOPDNO2,DOPDNAME2,DOQTY2,DOSLEPRC2,DOPDDISC2,
					DOPDNO3,DOPDNAME3,DOQTY3,DOSLEPRC3,DOPDDISC3,
					DOPDNO4,DOPDNAME4,DOQTY4,DOSLEPRC4,DOPDDISC4,
					DOPDNO5,DOPDNAME5,DOQTY5,DOSLEPRC5,DOPDDISC5,
					DOPDNO6,DOPDNAME6,DOQTY6,DOSLEPRC6,DOPDDISC6,
					DOPDNO7,DOPDNAME7,DOQTY7,DOSLEPRC7,DOPDDISC7,
					DOPDNO8,DOPDNAME8,DOQTY8,DOSLEPRC8,DOPDDISC8,
					DOPDNO9,DOPDNAME9,DOQTY9,DOSLEPRC9,DOPDDISC9,
					DOPDNO10,DOPDNAME10,DOQTY10,DOSLEPRC10,DOPDDISC10,
			DOSUM,DOSUMDISC,DOSMFNAME,DOSMLNAME,DOPRCV,DOSENT,DOTERM1,DOTERM2,
			DOAFTER,DOSPEC,DOSPECNAME,DONOTE,DPDATE,DLOCK,DLOST) 
			SELECT 
			$x,curdate(),ONO,ODATE,OQNO,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,curdate(),'','Dwn' 
			FROM ordr001 
			WHERE ordr001.ONO='$values' AND ordr001.OLOST='Dwn'";
			$insert = mysqli_query($con, $sql);

			if (!$insert) {
				//echo mysqli_error($con);
				echo "<script>alert('การบันทึกข้อมูลผิดพลาด!')</script>";
				back();
			} else { 
				$sqlq = "UPDATE ordr001 SET OLOCK='1' 
						 WHERE ordr001.ONO='$values' AND ordr001.OLOST='$olost' ";
				mysqli_query($con, $sqlq);
				echo "<script>alert('บันทึกข้อมูลเสร็จสิ้น.');</script>";
				back();
			}

				
		}else{
			echo "<script>
					alert('ทำรายการ Down Payment ไปแล้ว!\\nกด OK เพื่อนกลับ Page เดิม');
					window.location='actionController.php?action=seller/retrieveDeliveryAction';
			      </script>";
		}
	}else{
		$sqlDwn = "SELECT * FROM delv001
			       WHERE DONO=$values";//มี Downpayment หรือยัง
		$queryDwn = mysqli_query($con, $sqlDwn);
		$dataDwn = mysqli_fetch_assoc($queryDwn);
		$dno=$dataDwn['DNO'];
		$dlost=$dataDwn['DLOST'];

		if($dno!=''){//มี Downpayment แล้ว
			if($dlost='Dwn'){
				$x=$dno;
				$sql = "INSERT INTO delv001 
				(DNO,DDATE,DONO,DODATE,DOC_NO,DOC_SUR,DOC_NAME,DOC_CONTRCT,
				DOC_TAX_NO,DOC_DISC,DOC_ADRS,DOC_DSTRT,DOC_PRVN,DOC_TEL,DOC_FAX,DOPJNAME,
						DOPDNO1,DOPDNAME1,DOQTY1,DOSLEPRC1,DOPDDISC1,
						DOPDNO2,DOPDNAME2,DOQTY2,DOSLEPRC2,DOPDDISC2,
						DOPDNO3,DOPDNAME3,DOQTY3,DOSLEPRC3,DOPDDISC3,
						DOPDNO4,DOPDNAME4,DOQTY4,DOSLEPRC4,DOPDDISC4,
						DOPDNO5,DOPDNAME5,DOQTY5,DOSLEPRC5,DOPDDISC5,
						DOPDNO6,DOPDNAME6,DOQTY6,DOSLEPRC6,DOPDDISC6,
						DOPDNO7,DOPDNAME7,DOQTY7,DOSLEPRC7,DOPDDISC7,
						DOPDNO8,DOPDNAME8,DOQTY8,DOSLEPRC8,DOPDDISC8,
						DOPDNO9,DOPDNAME9,DOQTY9,DOSLEPRC9,DOPDDISC9,
						DOPDNO10,DOPDNAME10,DOQTY10,DOSLEPRC10,DOPDDISC10,
				DOSUM,DOSUMDISC,DOSMFNAME,DOSMLNAME,DOPRCV,DOSENT,DOTERM1,DOTERM2,
				DOAFTER,DOSPEC,DOSPECNAME,DONOTE,DPDATE,DLOCK,DLOST) 
				SELECT 
				$x,curdate(),ONO,ODATE,OQNO,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,curdate(),'','' 
				FROM ordr001 
				WHERE ordr001.ONO='$values' AND ordr001.OLOST=''";
				$insert = mysqli_query($con, $sql);

				$sqlq = "UPDATE ordr001 SET OLOCK='1' 
						 WHERE ordr001.ONO='$values' AND ordr001.OLOST=''";
					mysqli_query($con, $sqlq);
					back();
			}else{
					echo "<script>alert('มีการทำรายการนี้ไปแล้ว\\nกด OK เพื่อนกลับ Page เดิม');
								  window.location='actionController.php?action=seller/retrieveDeliveryAction';
						  </script>";
					back();
		
				}
		}else{
			echo "<script>
						alert('ต้องทำรายการ Down Payment ก่อน!\\nกด OK เพื่อนกลับ Page เดิม');
						window.location='actionController.php?action=seller/retrieveDeliveryAction';
		 	      </script>";
		}
	}						
	function back()
	{
		global $con;
		mysqli_close($con);
		header('location:actionController.php?action=seller/retrieveDeliveryAction');
		echo '</body></html>'; //->deliveryList.php
		//exit("<p><a href='actionController.php?action=seller/retrieveDeliveryActionForDelivery'>ย้อนกลับ</a></p></body></html>");
	}

	mysqli_close($con);		
?>		
</body>

</html>