<!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;
$no=$_GET['no'];
$values=$_GET['dno'];//2401000006
$dlost=$_GET['dlost'];//...
$doterm1=$_GET['doterm1'];

//$sql = "SELECT * FROM `invc001` ORDER BY NO DESC LIMIT 1";//Record สุดท้าย
$sql = "SELECT * FROM `invc001` WHERE YEAR(`IDATE`)=YEAR(CURDATE()) ORDER BY INO DESC LIMIT 1";//ONO Record สุดท้าย
$query=mysqli_query($con,$sql);
$result=mysqli_fetch_assoc($query);	
$data=substr($result['INO'],4,6);//000015 ;หมายเลข Invoice สุดท้ายของปีนี้
$year=substr($result['INO'],0,2);//24 ;ปี Record สุดท้ายของปีนี้
//echo 'dno  ='.$values.'<br>';//2401000006
//echo 'dlost='.$dlost.'<br>';//...
//echo 'data ='.$data.'<br>';//000015
//echo 'year ='.$year.'<br>';//24

//หมายเลข Invoice สุดท้ายของปีนี้ บวก 1 เป็น Record ใหม่
$sqlIno = "SELECT * FROM invc001 WHERE IDNO=$values AND ILOST='Dwn' LIMIT 1";//Record ที่ทำ Downpayment ไปแล้ว
$queryIno=mysqli_query($con,$sqlIno);
$resultIno=mysqli_fetch_assoc($queryIno);

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="";}			
$x=$substr.$k.$num;
	
//echo 'x ='.$x.'<br>';//2401000016
//ตรวจสอบว่ามีการออก Invoice ไปหรือยัง ถ้า $idno='' แปลว่ายังไม่มีการออก Invoice
//ถ้า 
$sql = "SELECT * FROM invc001 WHERE IDNO='$values'";
	$query = mysqli_query($con, $sql);
	$data = mysqli_fetch_assoc($query);
	$idno=$data['IDNO'];//..
	$ilost=$data['ILOST'];
	$idterm1=$data['IDTERM1'];
	//echo '$idno ='.$idno.'<br>';//2401000006

if(empty($idno) or (($idno=$values) and ($ilost='Dwn'))){
	$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(),DLOCK,DLOST
	FROM delv001 
	WHERE delv001.NO='$no'";
	$insert=mysqli_query($con,$sql);

	if(!$insert){
		echo "<script>
				alert('การบันทึกข้อมูล Invoice ผิดพลาด!\\n\\nกด OK เพื่อนกลับ Invoice List Page');
				window.location='actionController.php?action=seller/retrieveInvoiceAction';
			  </script>";
		//back();
	}else{
		$sqlq = "UPDATE delv001 SET DLOCK='1' WHERE delv001.NO=$no";
		mysqli_query($con, $sqlq);
		echo "<script>
				alert('บันทึกข้อมูล Invoice เสร็จสิ้น.\\n\\nกด OK เพื่อนกลับ Invoice List Page');
				window.location='actionController.php?action=seller/retrieveInvoiceAction';
			  </script>";
		//back();
	}
}

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>