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

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

	//$strDate = date("y"); //2 ตัวหลังของปีปัจจุบัน
	$sql = "SELECT * FROM `delv001` WHERE YEAR(`DDATE`)=YEAR(CURDATE()) ORDER BY DNO DESC LIMIT 1";//INO Record สุดท้าย
	$query=mysqli_query($con,$sql);
	$result=mysqli_fetch_assoc($query);
		
	$data=substr($result['DNO'],4,6);//หมายเลข Delivery สุดท้ายของปีนี้
	$year=substr($result['DNO'],0,2);//ปี Record สุดท้ายของปีนี้

	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;

	$dwn="SELECT * FROM delv001 WHERE DONO='$values' AND `DLOST`='Dwn'";
	$queryDwn=mysqli_query($con,$dwn);
	$resultDwn=mysqli_fetch_assoc($queryDwn);
	$dno=$resultDwn['DNO'];
	$olost=$resultDwn['DLOST'];
	//echo '$olost = '.$olost.'<br>';
	if($resultDwn==false){//ใน Order ไม่มี Downpayment แล้ว
		if($qterm1>0){//Quotation ใบนี้มี Downpayment
			echo "<script>
				alert('รายการ Order ใบนี้มี Downpayment ต้องกลับไปทำ Downpayment ก่อน.\\n\\nกด OK เพื่อนกลับ Order List Page');
				window.location='actionController.php?action=seller/retrieveOrderAction';
				</script>";
		}else{
			$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(),OLOCK,OLOST 
			FROM ordr001 
			WHERE ordr001.NO='$no'";
			$insert = mysqli_query($con, $sql);
			if($dataDwn==false){//กรณียังไม่มี Downpayment ใน Delivery File
				/*INSERT INTO Table1 (field1,field2,...) SELECT field1,field2,... FROM Table2 WHERE Condition ;*/	
				if (!$insert) {
					//echo mysqli_error($con);
					echo "<script>
							alert('การบันทึกข้อมูล Delivery ผิดพลาด!\\n\\nกด OK เพื่อนกลับ Delivery List Page');
							window.location='actionController.php?action=seller/retrieveDeliveryAction';
						  </script>";
					//back();
				}else{ 
					$sqlq = "UPDATE ordr001 SET ordr001.OLOCK=1 WHERE ordr001.NO='$no'";
					mysqli_query($con, $sqlq);
					echo "<script>
							alert('บันทึกข้อมูล Delivery เสร็จสิ้น.\\n\\nกด OK เพื่อนกลับ Delivery List Page');
							window.location='actionController.php?action=seller/retrieveDeliveryAction';
						  </script>";	  
					//back();
				}			
			}else{
				echo "<script>
						alert('ทำรายการ Down Payment ไปแล้ว!()\\n\\nกด OK เพื่อนกลับ Delivery List Page');
						window.location='actionController.php?action=seller/retrieveDeliveryAction';
					  </script>";
			}
		}
	}else{
		$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 
				$dno,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.NO='$no'";
				$insert = mysqli_query($con, $sql);
				if (!$insert) {
					//echo mysqli_error($con);
					echo "<script>
							alert('การบันทึกข้อมูล Delivery ผิดพลาด!\\n\\nกด OK เพื่อนกลับ Delivery List Page');
							window.location='actionController.php?action=seller/retrieveDeliveryAction';
						  </script>";
					//back();
				} else { 
					$sqlq = "UPDATE ordr001 SET ordr001.OLOCK=1 WHERE ordr001.NO='$no'";
					mysqli_query($con, $sqlq);
					echo "<script>
					    	alert('บันทึกข้อมูล Delivery เสร็จสิ้น.\\n\\nกด OK เพื่อนกลับ Delivery List Page');
							window.location='actionController.php?action=seller/retrieveDeliveryAction';
						  </script>";
					//back();
				}
	}
								
	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>