<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta charset="UTF-8">
<title>Invoice 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;}
</style>
</head>

<body>
<?php
error_reporting(0);

include "functions/dblink.php";

//echo $x;
$values=$_GET['dno'];
$dlost=$_GET['dlost'];

//$no = mysqli_query($con,"SELECT * FROM invc001");
$sql="SELECT * FROM invc001 WHERE YEAR(IDATE)=YEAR(CURRENT_DATE())";//ทั้งหมดในปีปัจจุบัน
//ORDER BY IDATE DESC LIMIT 1;
$query=mysqli_query($con,$sql);
$rows=mysqli_num_rows($query);//จำนวนแถว
//echo $rows.'<br>';

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

//หมายเลข Invoice สุดท้ายของปีนี้ บวก 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;

if($dlost=='Dwn'){
	$sqlDwn = "SELECT * FROM invc001
			   WHERE IDNO=$values AND ILOST='Dwn'";
	$queryDwn = mysqli_query($con, $sqlDwn);
	$dataDwn = mysqli_fetch_assoc($queryDwn);

	if($dataDwn['INO']==''){
	/*INSERT INTO Table1 (field1,field2,...) SELECT field1,field2,... FROM Table2 WHERE Condition ;*/
	$sql="INSERT INTO invc001 
	(INO,IDATE,IDNO,IDDATE,IDONO,IDC_NO,IDC_SUR,IDC_NAME, IDC_CONTRCT,
	IDC_TAX_NO,IDC_DISC,IDC_ADRS,IDC_DSTRT,IDC_PRVN,IDC_TEL,IDC_FAX,IDPJNAME,
		IDPDNO1,IDPDNAME1,IDQTY1,IDSLEPRC1,IDPDDISC1,
		IDPDNO2,IDPDNAME2,IDQTY2,IDSLEPRC2,IDPDDISC2,
		IDPDNO3,IDPDNAME3,IDQTY3,IDSLEPRC3,IDPDDISC3,
		IDPDNO4,IDPDNAME4,IDQTY4,IDSLEPRC4,IDPDDISC4,
		IDPDNO5,IDPDNAME5,IDQTY5,IDSLEPRC5,IDPDDISC5,
		IDPDNO6,IDPDNAME6,IDQTY6,IDSLEPRC6,IDPDDISC6,
		IDPDNO7,IDPDNAME7,IDQTY7,IDSLEPRC7,IDPDDISC7,
		IDPDNO8,IDPDNAME8,IDQTY8,IDSLEPRC8,IDPDDISC8,
		IDPDNO9,IDPDNAME9,IDQTY9,IDSLEPRC9,IDPDDISC9,
		IDPDNO10,IDPDNAME10,IDQTY10,IDSLEPRC10,IDPDDISC10,
	IDSUMDISC,IDSUM,IDSMFNAME,IDSMLNAME,IDPRCV,IDSENT,IDTERM1,IDTERM2,
	IDAFTER,IDSPEC,IDSPECNAME,IDNOTE,IDPDATE,ILOCK,ILOST) 
	SELECT 
	$x,curdate(),DNO,DDATE,DONO,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,
	DOSUMDISC,DOSUM,DOSMFNAME,DOSMLNAME,DOPRCV,DOSENT,DOTERM1,DOTERM2,
	DOAFTER,DOSPEC,DOSPECNAME,DONOTE,curdate(),'','Dwn' 
	FROM delv001 
	WHERE delv001.DNO='$values' AND delv001.DLOST='Dwn'";
	$insert=mysqli_query($con,$sql);
	
	if(!$insert){
			//echo mysqli_error($con);
			echo "<script>alert('การบันทึกข้อมูลผิดพลาด!')</script>";
			back();
		}
		else{
           $sqlq="UPDATE delv001 SET DLOCK=1 WHERE delv001.DNO='$values' AND delv001.DLOST='Dwn'";
    	   mysqli_query($con,$sqlq);
		   echo "<script>alert('บันทึกข้อมูลเสร็จสิ้น.');</script>";
           back();
		}
	}else{
		echo "<script>
					alert('ทำรายการ Down Payment ไปแล้ว!\\nกด OK เพื่อนกลับ Page เดิม');
					window.location='actionController.php?action=seller/retrieveInvoiceAction';
			  </script>";
	}		   
	
}else{
	$sqlDwn = "SELECT * FROM invc001
			   WHERE IDNO=$values";//มี Downpayment หรือยัง
	$queryDwn = mysqli_query($con, $sqlDwn);
	$dataDwn = mysqli_fetch_assoc($queryDwn);
	$ino=$dataDwn['INO'];
	$ilost=$dataDwn['ILOST'];

	if($ino!=''){//มี Downpayment แล้ว
		if($ilost='Dwn'){
			$x=$ino;
			$sql = "INSERT INTO invc001 
			(INO,IDATE,IDNO,IDDATE,IDONO,IDC_NO,IDC_SUR,IDC_NAME, IDC_CONTRCT,
			IDC_TAX_NO,IDC_DISC,IDC_ADRS,IDC_DSTRT,IDC_PRVN,IDC_TEL,IDC_FAX,IDPJNAME,
				IDPDNO1,IDPDNAME1,IDQTY1,IDSLEPRC1,IDPDDISC1,
				IDPDNO2,IDPDNAME2,IDQTY2,IDSLEPRC2,IDPDDISC2,
				IDPDNO3,IDPDNAME3,IDQTY3,IDSLEPRC3,IDPDDISC3,
				IDPDNO4,IDPDNAME4,IDQTY4,IDSLEPRC4,IDPDDISC4,
				IDPDNO5,IDPDNAME5,IDQTY5,IDSLEPRC5,IDPDDISC5,
				IDPDNO6,IDPDNAME6,IDQTY6,IDSLEPRC6,IDPDDISC6,
				IDPDNO7,IDPDNAME7,IDQTY7,IDSLEPRC7,IDPDDISC7,
				IDPDNO8,IDPDNAME8,IDQTY8,IDSLEPRC8,IDPDDISC8,
				IDPDNO9,IDPDNAME9,IDQTY9,IDSLEPRC9,IDPDDISC9,
				IDPDNO10,IDPDNAME10,IDQTY10,IDSLEPRC10,IDPDDISC10,
			IDSUMDISC,IDSUM,IDSMFNAME,IDSMLNAME,IDPRCV,IDSENT,IDTERM1,IDTERM2,
			IDAFTER,IDSPEC,IDSPECNAME,IDNOTE,IDPDATE,ILOCK,ILOST) 
			SELECT  
			$x,curdate(),DNO,DDATE,DONO,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,
			DOSUMDISC,DOSUM,DOSMFNAME,DOSMLNAME,DOPRCV,DOSENT,DOTERM1,DOTERM2,
			DOAFTER,DOSPEC,DOSPECNAME,DONOTE,curdate(),'','' 
			FROM delv001 
			WHERE delv001.DNO='$values' AND delv001.DLOST=''";
			$insert = mysqli_query($con, $sql);

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

mysqli_close($con);

?>

</body>
</html>