<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Delivery 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;}*/
		.rt {
			text-align: right;
		}

		#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;
		}

		#readonly p {
			color: red;
		}
	</style>
</head>

<body>

	<script language="JavaScript">
		function nextbox(e, id) {
			// อ่าน keycode (cross browser)
			var keycode = e.which || e.keyCode;
			// ตรวจสอบ keycode (13 คือ กด enter)
			if (keycode == 13) {
				// ย้ายโฟกัสไปยัง input ที่ id
				document.getElementById(id).focus();
				// return false เพื่อยกเลิกการ submit form
				return false;
			}
		}

		function chk() {

			if (parseFloat(document.frm.Disc.value) !== "") {
				var disc = parseFloat(document.frm.Disc.value);
			} else {
				var disc = 0;
			}

			var qty1 = parseFloat(document.frm.Qty1.value);
			var prc1 = parseFloat(document.frm.Prc1.value);
			var sl1 = (qty1 * prc1);

			var qty2 = parseFloat(document.frm.Qty2.value);
			var prc2 = parseFloat(document.frm.Prc2.value);
			var sl2 = (qty2 * prc2);

			var qty3 = parseFloat(document.frm.Qty3.value);
			var prc3 = parseFloat(document.frm.Prc3.value);
			var sl3 = (qty3 * prc3);

			var qty4 = parseFloat(document.frm.Qty4.value);
			var prc4 = parseFloat(document.frm.Prc4.value);
			var sl4 = (qty4 * prc4);


			var qty5 = parseFloat(document.frm.Qty5.value);
			var prc5 = parseFloat(document.frm.Prc5.value);
			var sl5 = (qty5 * prc5);

			var qty6 = parseFloat(document.frm.Qty6.value);
			var prc6 = parseFloat(document.frm.Prc6.value);
			var sl6 = (qty6 * prc6);

			var qty7 = parseFloat(document.frm.Qty7.value);
			var prc7 = parseFloat(document.frm.Prc7.value);
			var sl7 = (qty7 * prc7);

			var qty8 = parseFloat(document.frm.Qty8.value);
			var prc8 = parseFloat(document.frm.Prc8.value);
			var sl8 = (qty8 * prc8);

			var qty9 = parseFloat(document.frm.Qty9.value);
			var prc9 = parseFloat(document.frm.Prc9.value);
			var sl9 = (qty9 * prc9);

			var qty10 = parseFloat(document.frm.Qty10.value);
			var prc10 = parseFloat(document.frm.Prc10.value);
			var sl10 = (qty10 * prc10);

			var sl = sl1 + sl2 + sl3 + sl4 + sl5 + sl6 + sl7 + sl8 + sl9 + sl10;
			var discperunit = disc / sl;
			var doterm1 = parseFloat(document.frm.DOTERM1.value);

			if (disc <= 100) {

				document.frm.tdisc1.value = sl1 * disc / 100;
				document.frm.tsle1.value = sl1 - (sl1 * disc / 100);
				if ((sl1 * disc / 100) != 0) {
					document.frm.tdiscd1.value = (sl1 * disc / 100).toLocaleString(undefined, {
						minimumFractionDigits: 2
					});
				} else {
					if (qty1 != 0) {
						document.frm.tdiscd1.value = '0.00';
					} else {
						document.frm.tdiscd1.value = "";
					}
				}
				document.frm.tsled1.value = (sl1 - (sl1 * disc / 100)).toLocaleString(undefined, {
					minimumFractionDigits: 2
				});

				document.frm.tdisc2.value = sl2 * disc / 100;
				document.frm.tsle2.value = sl2 - (sl2 * disc / 100);
				if ((sl2 * disc / 100) != 0) {
					document.frm.tdiscd2.value = (sl2 * disc / 100).
					toLocaleString(undefined, {
						minimumFractionDigits: 2
					});
				} else {
					if (qty2 != 0) {
						document.frm.tdiscd2.value = "0.00";
					} else {
						document.frm.tdiscd2.value = "";
					}
				}
				document.frm.tsled2.value = (sl2 - (sl2 * disc / 100)).toLocaleString(undefined, {
					minimumFractionDigits: 2
				});

				document.frm.tdisc3.value = sl3 * disc / 100;
				document.frm.tsle3.value = sl3 - (sl3 * disc / 100);
				if ((sl3 * disc / 100) != 0) {
					document.frm.tdiscd3.value = (sl3 * disc / 100).toLocaleString(undefined, {
						minimumFractionDigits: 2
					});
				} else {
					if (qty3 != 0) {
						document.frm.tdiscd3.value = "0.00";
					} else {
						document.frm.tdiscd3.value = "";
					}
				}
				document.frm.tsled3.value = (sl3 - (sl3 * disc / 100)).toLocaleString(undefined, {
					minimumFractionDigits: 2
				});

				document.frm.tdisc4.value = sl4 * disc / 100;
				document.frm.tsle4.value = sl4 - (sl4 * disc / 100);
				if ((sl4 * disc / 100) != 0) {
					document.frm.tdiscd4.value = (sl4 * disc / 100).toLocaleString(undefined, {
						minimumFractionDigits: 2
					});
				} else {
					if (qty4 != 0) {
						document.frm.tdiscd4.value = "0.00";
					} else {
						document.frm.tdiscd4.value = "";
					}
				}
				document.frm.tsled4.value = (sl4 - (sl4 * disc / 100)).
				toLocaleString(undefined, {
					minimumFractionDigits: 2
				});

				document.frm.tdisc5.value = sl5 * disc / 100;
				document.frm.tsle5.value = sl5 - (sl5 * disc / 100);
				if ((sl5 * disc / 100) != 0) {
					document.frm.tdiscd5.value = (sl5 * disc / 100).
					toLocaleString(undefined, {
						minimumFractionDigits: 2
					});
				} else {
					if (qty5 != 0) {
						document.frm.tdiscd5.value = "0.00";
					} else {
						document.frm.tdiscd5.value = "";
					}
				}
				document.frm.tsled5.value = (sl5 - (sl5 * disc / 100)).
				toLocaleString(undefined, {
					minimumFractionDigits: 2
				});

				document.frm.tdisc6.value = sl6 * disc / 100;
				document.frm.tsle6.value = sl6 - (sl6 * disc / 100);
				if ((sl6 * disc / 100) != 0) {
					document.frm.tdiscd6.value = (sl6 * disc / 100).
					toLocaleString(undefined, {
						minimumFractionDigits: 2
					});
				} else {
					if (qty6 != 0) {
						document.frm.tdiscd6.value = "0.00";
					} else {
						document.frm.tdiscd6.value = "";
					}
				}
				document.frm.tsled6.value = (sl6 - (sl6 * disc / 100)).
				toLocaleString(undefined, {
					minimumFractionDigits: 2
				});

				document.frm.tdisc7.value = sl7 * disc / 100;
				document.frm.tsle7.value = sl7 - (sl7 * disc / 100);
				if ((sl7 * disc / 100) != 0) {
					document.frm.tdiscd7.value = (sl7 * disc / 100).
					toLocaleString(undefined, {
						minimumFractionDigits: 2
					});
				} else {
					if (qty7 != 0) {
						document.frm.tdiscd7.value = "0.00";
					} else {
						document.frm.tdiscd7.value = "";
					}
				}
				document.frm.tsled7.value = (sl7 - (sl7 * disc / 100)).
				toLocaleString(undefined, {
					minimumFractionDigits: 2
				});

				document.frm.tdisc8.value = sl8 * disc / 100;
				document.frm.tsle8.value = sl8 - (sl8 * disc / 100);
				if ((sl8 * disc / 100) != 0) {
					document.frm.tdiscd8.value = (sl8 * disc / 100).
					toLocaleString(undefined, {
						minimumFractionDigits: 2
					});
				} else {
					if (qty8 != 0) {
						document.frm.tdiscd8.value = "0.00";
					} else {
						document.frm.tdiscd8.value = "";
					}
				}
				document.frm.tsled8.value = (sl8 - (sl8 * disc / 100)).
				toLocaleString(undefined, {
					minimumFractionDigits: 2
				});

				document.frm.tdisc9.value = sl9 * disc / 100;
				document.frm.tsle9.value = sl9 - (sl9 * disc / 100);
				if ((sl9 * disc / 100) != 0) {
					document.frm.tdiscd9.value = (sl9 * disc / 100).
					toLocaleString(undefined, {
						minimumFractionDigits: 2
					});
				} else {
					if (qty9 != 0) {
						document.frm.tdiscd9.value = "0.00";
					} else {
						document.frm.tdiscd9.value = "";
					}
				}
				document.frm.tsled9.value = (sl9 - (sl9 * disc / 100)).
				toLocaleString(undefined, {
					minimumFractionDigits: 2
				});

				document.frm.tdisc10.value = sl10 * disc / 100;
				document.frm.tsle10.value = sl10 - (sl10 * disc / 100);
				if ((sl10 * disc / 100) != 0) {
					document.frm.tdiscd10.value = (sl10 * disc / 100).
					toLocaleString(undefined, {
						minimumFractionDigits: 2
					});
				} else {
					if (qty10 != 0) {
						document.frm.tdiscd10.value = "0.00";
					} else {
						document.frm.tdiscd10.value = "";
					}
				}
				document.frm.tsled10.value = (sl10 - (sl10 * disc / 100)).
				toLocaleString(undefined, {
					minimumFractionDigits: 2
				});

				var sl = sl1 + sl2 + sl3 + sl4 + sl5 + sl6 + sl7 + sl8 + sl9 + sl10;
				var ts = ((sl1) - (sl1 * disc / 100)) + ((sl2) - (sl2 * disc / 100)) + ((sl3) - (sl3 * disc / 100)) + ((sl4) - (sl4 * disc / 100)) +
					((sl5) - (sl5 * disc / 100)) + ((sl6) - (sl6 * disc / 100)) + ((sl7) - (sl7 * disc / 100)) + ((sl8) - (sl8 * disc / 100)) +
					((sl9) - (sl9 * disc / 100)) + ((sl10) - (sl10 * disc / 100));
				var tdisc = sl * disc / 100;
				document.frm.tdiscd.value = (sl * disc / 100).
				toLocaleString(undefined, {minimumFractionDigits: 2});

				var tsle = ts;
				document.frm.tsled.value = ts.toLocaleString(undefined, {minimumFractionDigits: 2});

			} else {

				document.frm.tdisc1.value = discperunit * sl1;
				document.frm.tsle1.value = sl1 - (discperunit * sl1);

				if (sl1 != 0) {
					document.frm.tdiscd1.value = (+(discperunit * sl1).toFixed(2)).toLocaleString();
					document.frm.tsled1.value = (+(sl1 - (discperunit * sl1)).toFixed(2)).toLocaleString();
				} else {
					document.frm.tdiscd1.value = "";
					document.frm.tsled1.value = "";
				}

				//document.frm.tdisc2.value = discperunit*sl2;
				document.frm.tdisc2.value = discperunit * sl2;
				document.frm.tsle2.value = sl2 - (discperunit * sl2);
				if (sl2 != 0) {
					document.frm.tdiscd2.value = (+(discperunit * sl2).toFixed(2)).toLocaleString();
					document.frm.tsled2.value = (+(sl2 - (discperunit * sl2)).toFixed(2)).toLocaleString();

				} else {
					document.frm.tdiscd2.value = "";
					document.frm.tsled2.value = "";
				}

				document.frm.tdisc3.value = discperunit * sl3;
				document.frm.tsle3.value = sl3 - (discperunit * sl3);
				if (sl3 != 0) {
					document.frm.tdiscd3.value = (+(discperunit * sl3).toFixed(2)).toLocaleString();
					document.frm.tsled3.value = (+(sl3 - (discperunit * sl3)).toFixed(2)).toLocaleString();
				} else {
					document.frm.tdiscd3.value = "";
					document.frm.tsled3.value = "";
				}

				document.frm.tdisc4.value = discperunit * sl4;
				document.frm.tsle4.value = sl4 - (discperunit * sl4);
				if (sl4 != 0) {
					document.frm.tdiscd4.value = (+(discperunit * sl4).toFixed(2)).toLocaleString();
					document.frm.tsled4.value = (+(sl4 - (discperunit * sl4)).toFixed(2)).toLocaleString();
				} else {
					document.frm.tdiscd4.value = "";
					document.frm.tsled4.value = "";
				}

				document.frm.tdisc5.value = discperunit * sl5;
				document.frm.tsle5.value = sl5 - (discperunit * sl5);
				if (sl5 != 0) {
					document.frm.tdiscd5.value = (+(discperunit * sl5).toFixed(2)).toLocaleString();
					document.frm.tsled5.value = (+(sl5 - (discperunit * sl5)).toFixed(2)).toLocaleString();
				} else {
					document.frm.tdiscd5.value = "";
					document.frm.tsled5.value = "";
				}

				document.frm.tdisc6.value = discperunit * sl6;
				document.frm.tsle6.value = sl6 - (discperunit * sl6);
				if (sl6 != 0) {
					document.frm.tdiscd6.value = (+(discperunit * sl6).toFixed(2)).toLocaleString();
					document.frm.tsled6.value = (+(sl6 - (discperunit * sl6)).toFixed(2)).toLocaleString();
				} else {
					document.frm.tdiscd6.value = "";
					document.frm.tsled6.value = "";
				}

				document.frm.tdisc7.value = discperunit * sl7;
				document.frm.tsle7.value = sl7 - (discperunit * sl7);
				if (sl7 != 0) {
					document.frm.tdiscd7.value = (+(discperunit * sl7).toFixed(2)).toLocaleString();
					document.frm.tsled7.value = (+(sl7 - (discperunit * sl7)).toFixed(2)).toLocaleString();
				} else {
					document.frm.tdiscd7.value = "";
					document.frm.tsled7.value = "";
				}

				document.frm.tdisc8.value = discperunit * sl8;
				document.frm.tsle8.value = sl8 - (discperunit * sl8);
				if (sl8 != 0) {
					document.frm.tdiscd8.value = (+(discperunit * sl8).toFixed(2)).toLocaleString();
					document.frm.tsled8.value = (+(sl8 - (discperunit * sl8)).toFixed(2)).toLocaleString();
				} else {
					document.frm.tdiscd8.value = "";
					document.frm.tsled8.value = "";
				}

				document.frm.tdisc9.value = discperunit * sl9;
				document.frm.tsle9.value = sl9 - (discperunit * sl9);
				if (sl9 != 0) {
					document.frm.tdiscd9.value = (+(discperunit * sl9).toFixed(2)).toLocaleString();
					document.frm.tsled9.value = (+(sl9 - (discperunit * sl9)).toFixed(2)).toLocaleString();
				} else {
					document.frm.tdiscd9.value = "";
					document.frm.tsled9.value = "";
				}

				document.frm.tdisc10.value = discperunit * sl10;
				document.frm.tsle10.value = sl10 - (discperunit * sl10);
				if (sl10 != 0) {
					document.frm.tdiscd10.value = (+(discperunit * sl10).toFixed(2)).toLocaleString();
					document.frm.tsled10.value = (+(sl10 - (discperunit * sl10)).toFixed(2)).toLocaleString();
				} else {
					document.frm.tdiscd10.value = "";
					document.frm.tsled10.value = "";
				}

				//document.frm.tdisc.value=disc;
				var tdisc = disc;
				document.frm.tdiscd.value = disc.
				toLocaleString(undefined, {
					minimumFractionDigits: 2
				});

				var tsle = (sl1 + sl2 + sl3 + sl4 + sl5 + sl6 + sl7 + sl8 + sl9 + sl10) - disc;

				document.frm.tsled.value = ((sl1 + sl2 + sl3 + sl4 + sl5 + sl6 + sl7 + sl8 + sl9 + sl10) - disc).
				toLocaleString(undefined, {minimumFractionDigits: 2});

				var term1=<?php echo $oqterm1;?>;
			    document.frm.txterm1.value=(tsle*term1/100).toLocaleString(undefined,{minimumFractionDigits:2});
			}
		}
	</script>

	<body>
		<?php
		error_reporting(0);

		include "functions/dblink.php";

		$ono = $_GET['ONO'];
		$sql = "SELECT * FROM ordr001 WHERE ONO=$ono";
		$result = mysqli_query($con, $sql);
		while ($data = mysqli_fetch_array($result)) {
			$o_no = $data['ONO'];
			$odate = $data['ODATE'];
			$oqcno = $data['OQC_NO'];
			$oqno = $data['OQNO'];
			$oqc_sur = $data['OQC_SUR'];
			$oqc_name = $data['OQC_NAME'];
			$oqc_contrct = $data['OQC_CONTRCT'];
			$oqc_tax_no = $data['OQC_TAX_NO'];

			$oqc_disc = $data['OQC_DISC'];

			$oqc_adrs = $data['OQC_ADRS'];
			$oqc_dstrt = $data['OQC_DSTRT'];
			$oqc_prvn = $data['OQC_PRVN'];
			$oqc_tel = $data['OQC_TEL'];
			$oqc_fax = $data['OQC_FAX'];
			$oqpjname = $data['OQPJNAME'];

			$oqpdno1 = $data['OQPDNO1'];
			$oqpdname1 = $data['OQPDNAME1'];
			$x1 = $data['X1'];
			$oqqty1 = $data['OQQTY1'];
			$oqsleprc1 = $data['OQSLEPRC1'];
			//$tsx1 = ($oqqty1+$x1) * $oqsleprc1;
			$tsale1 = $oqqty1 * $oqsleprc1;

			$oqpdno2 = $data['OQPDNO2'];
			$oqpdname2 = $data['OQPDNAME2'];
			$x2 = $data['X2'];
			$oqqty2 = $data['OQQTY2'];
			$oqsleprc2 = $data['OQSLEPRC2'];
			//$tsx2 = ($oqqty2+$x2) * $oqsleprc2;
			$tsale2 = $oqqty2 * $oqsleprc2;

			$oqpdno3 = $data['OQPDNO3'];
			$oqpdname3 = $data['OQPDNAME3'];
			$x3 = $data['X3'];
			$oqqty3 = $data['OQQTY3'];
			$oqsleprc3 = $data['OQSLEPRC3'];
			//$tsx3 = ($oqqty1+$x3) * $oqsleprc3;
			$tsale3 = $oqqty3 * $oqsleprc3;

			$oqpdno4 = $data['OQPDNO4'];
			$oqpdname4 = $data['OQPDNAME4'];
			$x4 = $data['X4'];
			$oqqty4 = $data['OQQTY4'];
			$oqsleprc4 = $data['OQSLEPRC4'];
			//$tsx4 = ($oqqty4+$x4) * $oqsleprc4;
			$tsale4 = $oqqty4 * $oqsleprc4;

			$oqpdno5 = $data['OQPDNO5'];
			$oqpdname5 = $data['OQPDNAME5'];
			$x5 = $data['X5'];
			$oqqty5 = $data['OQQTY5'];
			$oqsleprc5 = $data['OQSLEPRC5'];
			//$tsx5 = ($oqqty5+$x5) * $oqsleprc5;
			$tsale5 = $oqqty5 * $oqsleprc5;

			$oqpdno6 = $data['OQPDNO6'];
			$oqpdname6 = $data['OQPDNAME6'];
			$x6 = $data['X6'];
			$oqqty6 = $data['OQQTY6'];
			$oqsleprc6 = $data['OQSLEPRC6'];
			//$tsx6 = ($oqqty6+$x6) * $oqsleprc6;
			$tsale6 = $oqqty6 * $oqsleprc6;

			$oqpdno7 = $data['OQPDNO7'];
			$oqpdname7 = $data['OQPDNAME7'];
			$x7 = $data['X7'];
			$oqqty7 = $data['OQQTY7'];
			$oqsleprc7 = $data['OQSLEPRC7'];
			//$tsx7 = ($oqqty7+$x7) * $oqsleprc7;
			$tsale7 = $oqqty7 * $oqsleprc7;

			$oqpdno8 = $data['OQPDNO8'];
			$oqpdname8 = $data['OQPDNAME8'];
			$x8 = $data['X8'];
			$oqqty8 = $data['OQQTY8'];
			$oqsleprc8 = $data['OQSLEPRC8'];
			//$tsx8 = ($oqqty8+$x8) * $oqsleprc8;
			$tsale8 = $oqqty8 * $oqsleprc8;

			$oqpdno9 = $data['OQPDNO9'];
			$oqpdname9 = $data['OQPDNAME9'];
			$x9 = $data['X9'];
			$oqqty9 = $data['OQQTY9'];
			$oqsleprc9 = $data['OQSLEPRC9'];
			//$tsx9 = ($oqqty9+$x9) * $oqsleprc9;
			$tsale9 = $oqqty9 * $oqsleprc9;

			$oqpdno10 = $data['OQPDNO10'];
			$oqpdname10 = $data['OQPDNAME10'];
			$x10 = $data['X10'];
			$oqqty10 = $data['OQQTY10'];
			$oqsleprc10 = $data['OQSLEPRC10'];
			//$tsx10 = ($oqqty10+$x10) * $oqsleprc10;
			$tsale10 = $oqqty10 * $oqsleprc10;

			$oqsum = $data['OQSUM'];
			$oqsumdisc = $data['OQSUMDISC'];
			//$tsx=$tsx1+$tsx2+$tsx3+$tsx4+$tsx5+$tsx6+$tsx7+$tsx8+$tsx9+$tsx10;//disc>100
			//$discPerUnit=$oqsumdisc/$tsx;//disc>100

			$oqsmfname = $data['OQSMFNAME'];
			$oqsmlname = $data['OQSMLNAME'];
			$oqprcv = $data['OQPRCV'];
			$oqsent = $data['OQSENT'];
			$oqterm1 = $data['OQTERM1'];
			$oqterm2 = $data['OQTERM2'];
			$oqafter = $data['OQAFTER'];
			$oqspec = $data['OQSPEC'];
			$oqspecname = $data['OQSPECNAME'];
			$oqnote = $data['OQNOTE'];
		}

		if (isset($_POST['DNO'])) { //นำข้อมูลจากตัวแปร $_POST ทีเหลือมาเรียงต่อเป็นสตริงเดียวกันโดยขั้นด้วย ' , '
			$values = implode("','", $_POST);     //ลักษณะผลลัพธ์เช่น  a',  'b',  'c', 'd	
			//ปิดหัว ท้าย ด้วย ' เพื่อให้ครบคู่ ลักษณะผลลัพธ์จะเป็น 	'a',  'b',  'c', 'd'
			$values = "'" . $values . "'";

			//นำข้อมูลนั้นมาสร้างเป็น  SQL ในแบบคำสั่ง REPLACE
			$sql = "INSERT INTO delv001 VALUES($values); ";
			$replace = mysqli_query($con, $sql);
			if (!$replace) {
				echo mysqli_error($con);
			} else { ?>
				<div id="bg" align="center">
					<p>--- ข้อมูลบันทึกแล้ว ---</p>
					<br>
				<?php

				$dNo = substr($values, 5, 10);
				$sqlNo = "SELECT * FROM delv001 WHERE DNO = $dNo";
				$resNo = mysqli_query($con, $sqlNo); //$delivNo
				while ($dtQty = mysqli_fetch_array($resNo)) {
					$xqty1 = number_format($dtQty['DOQTY1']);
					$xqty2 = number_format($dtQty['DOQTY2']);
					$xqty3 = number_format($dtQty['DOQTY3']);
					$xqty4 = number_format($dtQty['DOQTY4']);
					$xqty5 = number_format($dtQty['DOQTY5']);
					$xqty6 = number_format($dtQty['DOQTY6']);
					$xqty7 = number_format($dtQty['DOQTY7']);
					$xqty8 = number_format($dtQty['DOQTY8']);
					$xqty9 = number_format($dtQty['DOQTY9']);
					$xqty10 = number_format($dtQty['DOQTY10']);
				}

				echo "Delivery No. = " . $dNo . "<br>";
				//echo "DOQTY = ".$xqty1.$xqty2.$xqty3.$xqty4.$xqty5.$xqty6.$xqty7.$xqty8.$xqty9.$xqty10."<br>"; 
				echo "From" . "<br>";
				echo "Order No. = " . $ono;      

				if ($oqc_disc <= 100) {
					mysqli_query($con, "UPDATE ordr001 
					SET X1=$x1+$xqty1 ,
					    X2=$x2+$xqty2 ,
						X3=$x3+$xqty3 ,
						X4=$x4+$xqty4 ,
						X5=$x5+$xqty5 ,
						X6=$x6+$xqty6 ,
						X7=$x7+$xqty7 ,
						X8=$x8+$xqty8 ,
						X9=$x9+$xqty9 ,
						X10=$x10+$xqty10 ,
						OQSUMDISC=((OQQTY1*OQSLEPRC1)+(OQQTY2*OQSLEPRC2)+
								(OQQTY3*OQSLEPRC3)+(OQQTY4*OQSLEPRC4)+
								(OQQTY5*OQSLEPRC5)+(OQQTY6*OQSLEPRC6)+
								(OQQTY7*OQSLEPRC7)+(OQQTY8*OQSLEPRC8)+
								(OQQTY9*OQSLEPRC9)+(OQQTY10*OQSLEPRC10))*'$oqc_disc'/100,
						OQSUM=((OQQTY1*OQSLEPRC1)+(OQQTY2*OQSLEPRC2)+
								(OQQTY3*OQSLEPRC3)+(OQQTY4*OQSLEPRC4)+
								(OQQTY5*OQSLEPRC5)+(OQQTY6*OQSLEPRC6)+
								(OQQTY7*OQSLEPRC7)+(OQQTY8*OQSLEPRC8)+
								(OQQTY9*OQSLEPRC9)+(OQQTY10*OQSLEPRC10))-OQSUMDISC,
					OPDATE = curdate() WHERE ONO = '$ono'");
				} else {
					mysqli_query($con, "UPDATE ordr001 
					SET X1=$x1+$xqty1 ,
						X2=$x2+$xqty2 ,
						X3=$x3+$xqty3 ,
						X4=$x4+$xqty4 ,
						X5=$x5+$xqty5 ,
						X6=$x6+$xqty6 ,
						X7=$x7+$xqty7 ,
						X8=$x8+$xqty8 ,
						X9=$x9+$xqty9 ,
						X10=$x10+$xqty10 ,
                        OQSUMDISC=$oqc_disc,
                        OQSUM=((OQQTY1*OQSLEPRC1)+(OQQTY2*OQSLEPRC2)+
                                 (OQQTY3*OQSLEPRC3)+(OQQTY4*OQSLEPRC4)+
                                 (OQQTY5*OQSLEPRC5)+(OQQTY6*OQSLEPRC6)+
                                 (OQQTY7*OQSLEPRC7)+(OQQTY8*OQSLEPRC8)+
                                 (OQQTY9*OQSLEPRC9)+(OQQTY10*OQSLEPRC10))-OQSUMDISC,
                        OPDATE = curdate() WHERE ONO = '$ono'");
				}
				back();
			} //ฟังค์ชั่นนี้อยู่ในขั้นตอน  1
				?>
				</div>
			<?php

		} //ฟังค์ชั่นนี้อยู่ในขั้นตอน  1	

		//ถ้าเป็นการ Postback เพื่อส่งข้อมูลกลับเข้ามา
		if (isset($_GET['action'])) {
			$action = $_GET['action'];
			//ถ้าเป็นเพิ่มข้อมูล ก็ไม่ต้องทำอะไร เพื่อให้ฟอร์มนั้นว่างเปล่าสำหรับรับข้อมูลใหม่
			//if($action=="insert")
			if ($action == "seller/addDeliveryActionS") {
				$h = "เพิ่มข้อมูลการจัดส่ง";
				$strDate = substr(date("Y"), 2, 2) . "<br/>";
				//$sqlAll = mysqli_query($con, "SELECT * FROM delv001");
				$sql = "SELECT * FROM delv001 WHERE YEAR(DDATE)=YEAR(CURDATE())";
				$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;
			}
		}

		function back()
		{
			global $con;
			mysqli_close($con);
			exit("<p><a href='actionController.php?action=seller/retrieveOrderAction'>ย้อนกลับ</a>
		        </p></body></html>");
		}
		mysqli_close($con);

			?>

			<div style='font-size:18px; color:red;' align='center'><?php echo $h; ?></div>
			<div id="all" align="center">

				<form method="post" action="" name="frm">
					<!-- <form name="frm" onsubmit="return check();"> -->
					<table width="830">
						<tr>
							<td style='font-size:14px; color:blue;'><strong>รายละเอียด</strong></td>
						</tr>
						<!-- 1 NO Auto Increment -->
						<input type="hidden" name="NO" id="NO" value="0">
					</table>

					<table width="830">
						<tr>
							<td width="76" style='text-align:right'>Deliv.No. :</td>
							<!-- 2 DNO -->
							<td width="74"><input type="text" name="DNO" id="DNO" size="10" readonly 
							                      value="<?php echo $x; ?>"></td>
							<!-- 3 DDATE -->
							<td width="74" style='text-align:right'>วันที่ :</td>
							<td width="586"><input type="date" name="DDATE" id="DDATE" size="19" 
							                       value="<?php echo date("Y-m-d"); ?>" 
												   onkeydown="return nextbox(event, 'DOC_SUR');"></td>
						</tr>
					</table>
					<!-- 4 DONO -->
					<input type="hidden" name="DONO" id="DONO" value="<?php echo $o_no ?>">

					<table width="830" height="29">
						<tr>
							<td class="rt" width="75">วันที่ Order :</td>
							<?php $date = new DateTime($odate);
							//echo $date->format('d/m/Y');
							?>
							<!-- 5 DODATE -->
							<td width="154">
								<input type="text" name="DODATE" id="DODATE" readonly disabled 
								       value="<?php echo $date->format('d/m/Y'); ?>">
							</td>
							<input type="hidden" name="DODATE" value="<?php echo $odate; ?>">

							<td class="rt" width="87">Order No. :</td>
							<!-- 6 DOC_NO -- OQC_NO -->
							<td width="494"><input type="text" name="DOC_NO" id="DOC_NO" size="10" readonly 
							    value="<?php echo $oqcno; ?>"></td>
						</tr>
					</table>

					<table width="830" height="29">
						<tr>
							<td class="rt" width="76">Name :</td>
							<!-- 7 DOC_SUR -->
							<td width="57"><input type="text" name="DOC_SUR" id="DOC_SUR" size="8" readonly 
							                      value="<?php echo $oqc_sur; ?>" 
												  onkeydown="return nextbox(event, 'DOC_NAME');"></td>
							<!-- 8 DOC_NAME -->
							<td width="682"><input type="text" name="DOC_NAME" id="DOC_NAME" size="50" readonly 
							                       value="<?php echo $oqc_name; ?>" 
												   onkeydown="return nextbox(event, 'DOC_CONTRCT');"></td>
						</tr>
					</table>

					<table width="830" height="29">
						<tr>
							<td class="rt" width="76">ผู้ติดต่อ :</td>
							<!-- 9 DOC_CONTRCT -->
							<td width="231"><input type="text" name="DOC_CONTRCT" id="DOC_CONTRCT" size="35" readonly 
							                       value="<?php echo $oqc_contrct; ?>" 
												   onkeydown="return nextbox(event, 'DOC_TAX_NO');"></td>

							<td class="rt" width="170">Tax No. :</td>
							<!-- 10 DOC_TAX_NO -->
							<td><input type="text" name="DOC_TAX_NO" id="DOC_TAX_NO" size="15" readonly 
							           value="<?php echo $oqc_tax_no; ?>" 
									   onkeydown="return nextbox(event, 'txtDisc');"></td>

							<td width="55" class="rt">ส่วนลด :</td>
							<!-- 11 DOC_DISC -->
							<td><input type="text" name="txtDisc" id="txtDisc" readonly disabled size="16" style="text-align: right" 
							           value="<?php echo number_format($oqc_disc, 2); ?>" 
									   onclick="chk()" onkeydown="return nextbox(event, 'DOC_ADRS');"> % </td>
							<input type="hidden" name="Disc" value="<?php echo $oqc_disc; ?>">
						</tr>
					</table>

					<table width="830" height="29">
						<tr>
							<td class="rt" width="76">ที่อยู่ :</td>
							<!-- 12 DOC_ADRS -->
							<td width="305"><input type="text" name="DOC_ADRS" id="DOC_ADRS" size="50" readonly value="<?php echo $oqc_adrs; ?>" onkeydown="return nextbox(event, 'DOC_DSTRT');"></td>

							<td class="rt" width="93">อำเภอ/จังหวัด :</td>
							<!-- 13 DOC_DSTRT -->
							<td width="122"><input type="text" name="DOC_DSTRT" id="DOC_DSTRT" size="20" readonly value="<?php echo $oqc_dstrt; ?>" onkeydown="return nextbox(event, 'DOC_PRVN');"></td>
							<!-- 14 DOC_PRVN -->
							<td width="200"><input type="text" name="DOC_PRVN" id="DOC_PRVN" size="25" readonly value="<?php echo $oqc_prvn; ?>" onkeydown="return nextbox(event, 'DOC_TEL');"></td>
						</tr>
					</table>

					<table width="830" height="29">
						<tr>
							<td width="76" height="22" class="rt">Tel. :</td>
							<!-- 15 DOC_TEL -->
							<td><input type="text" name="DOC_TEL" id="DOC_TEL" size="14" readonly value="<?php echo $oqc_tel; ?>" onkeydown="return nextbox(event, 'DOC_FAX');"></td>

							<td width="46" class="rt">Fax. :</td>
							<!-- 16 DOC_FAX -->
							<td><input type="text" name="DOC_FAX" id="DOC_FAX" size="14" readonly value="<?php echo $oqc_fax; ?>" onkeydown="return nextbox(event, 'DOPJNAME');"></td>

							<td width="178" class="rt">Proj. :</td>
							<!-- 17 DOPJNAME -->
							<td><input type="text" name="DOPJNAME" id="DOPJNAME" size="40" readonly value="<?php echo $oqpjname; ?>" onkeydown="return nextbox(event, 'DOPDNO1');"></td>
						</tr>
					</table>
					<!-- ส่วนลด -->
					<!-- <input type="hidden" name="Disc" value="<?php echo $oqc_disc; ?>"> -->
					<table width="639">
						<tr align="center">
							<th></th>
							<th>รหัส</th>
							<th>ชื่อสินค้า</th>
							<th>จำนวน</th>
							<th>ต่อหน่วย</th>
							<th>ลด(บาท)</th>
							<th>รวมขาย</th>
						</tr>
						<tr>
							<?php
							for ($i = 1; $i <= 10; $i++) {
							?>
								<th><?php echo $i . "."; ?></td>
									<!-- DOPDNO -->
								<th><input type="text" name="DOPDNO<?php echo $i; ?>" id="DOPDNO<?php echo $i; ?>" size="10" readonly value="<?php echo ${"oqpdno{$i}"}; ?>" 
								           onclick="chk()" onkeydown="return nextbox(event, 'Qty<?php echo $i; ?>');"></th>
								<!-- DOPDNAME -->
								<th><input type="text" name="DOPDNAME<?php echo $i; ?>" id="DOPDNAME<?php echo $i; ?>" size="45" readonly value="<?php echo ${"oqpdname{$i}"}; ?>" 
								           onclick="chk()"></th>

								<!-- ตัวที่ 1 จำนวน -->
								<!-- <input type="hidden" name="Qty1"> -->
								<!-- DOQTY -->
								<th><input type="number" name="Qty<?php echo $i; ?>" id="Qty<?php echo $i; ?>" 
								           min=<?php if(${"oqqty{$i}"}-${"x{$i}"}<0){echo 0;}
										             else{echo 0;} ?> 
										   max=<?php if(${"oqqty{$i}"}-${"x{$i}"}>0){echo ${"oqqty{$i}"}-${"x{$i}"};} 
										             else{echo 0;}?> 
										   style="text-align: right" size="5" 
										   value="<?php if ((${"oqqty{$i}"}!=0 OR ${"oqqty{$i}"}!="") AND ${"oqqty{$i}"}-${"x{$i}"}>0) 
										                     {echo ${"xqty{$i}"} = ${"oqqty{$i}"}-${"x{$i}"};} 
										                else {echo ${"xqty{$i}"} = '0';} ?>"
										   onclick="chk()" 
										   onkeydown="return nextbox(event,'<?php if ($i <= 9) {echo 'DOPDNO' . ($i + 1);} 
										                                          else {echo 'DOSMFNAME';} ?>');">
								</th>

								<!-- ตัวที่ 2 ราคาต่อหน่วย-->
								<!-- DOSLEPRC -->
								<input type="hidden" name="Prc<?php echo $i; ?>" id="Prc<?php echo $i; ?>" 
								       value="<?php echo ${"oqsleprc{$i}"}; ?>">
								<th><input type="text" name="oqsleprc<?php echo $i; ?>" id="oqsleprc<?php echo $i; ?>" size="13" style="text-align: right" disabled 
								       value="<?php if (${"oqsleprc{$i}"} != 0) {echo number_format(${"oqsleprc{$i}"}, 2);} 
									                else {echo '';} ?>"></th>
								<!-- ผลลัพธ์ ส่วนลด-->
								<!-- DOPDDISC -->
								<input type="hidden" name="tdisc<?php echo $i; ?>" id="tdisc<?php echo $i; ?>" 
								       onclick="chk()">


								<th><input type="text" name="tdiscd<?php echo $i; ?>" id="tdiscd<?php echo $i; ?>" disabled style="text-align: right" size="12" 
								           onclick="chk()"></th>

								<!-- จำนวนเงิน(หักส่วนลด) -->
								<input type="hidden" name="tsle<?php echo $i; ?>" id="tsle<?php echo $i; ?>" disabled onclick="chk()">
								<th><input type="text" name="tsled<?php echo $i; ?>" id="tsled<?php echo $i; ?>" disabled style="text-align: right" 
								           onclick="chk()"></th>
						</tr>
					<?php } ?>
					<td></td>
					<td></td>
					<td style='text-align:right'>รวม :</td>
					<td></td>
					<td></td>
					<!-- 68 DOSUMDISC -->
					<input type="hidden" name="tdisc" id="tdisc" value="0.00" 
					       onClick="chr()">
					<td><input type="text" name="tdiscd" id="tdiscd" style="text-align: right" size="12" disabled 
					           onclick="chk()"></td>
					<!-- 69 DOSUM -->
					<input type="hidden" name="tsle" id="tsle" value="0.00" 
					       onClick="chr()">
					<td><input type="text" name="tsled" id="tsled" style="text-align: right" disabled 
					           onclick="chk()"></td>
					</table>

					<table width="833">
						<tr>
							<td class="rt" width="76">ผู้ขาย :</td>
							<!-- 70 DOSMFNAME -->
							<td width="146"><input type="text" name="DOSMFNAME" id="DOSMFNAME" size="20" readonly 
							                       value="<?php echo $oqsmfname; ?>" 
												   onkeydown="return nextbox(event, 'DOSMLNAME');"></td>
							<!-- 71 DOSMLNAME -->
							<td width="596"><input type="text" name="DOSMLNAME" id="DOSMLNAME" size="30" readonly 
							                       value="<?php echo $oqsmlname; ?>" 
												   onkeydown="return nextbox(event, 'DOPRCV');"></td>
						</tr>
					</table>

					<table width="833">
						<tr>
							<td class="rt" width="75">เครดิต :</td>
							<!-- 72 DOPRCV-->
							<td width="205"><input type="text" name="DOPRCV" id="DOPRCV" size="6" readonly 
							                       value="<?php echo $oqprcv; ?>" 
												   onkeydown="return nextbox(event, 'DOSENT');"> วัน </td>
						</tr>
						<tr>
							<td class="rt" width="75">กำหนดส่ง :</td>
							<!-- 73 DOSENT-->
							<td width="205"><input type="text" name="DOSENT" id="DOSENT" size="6" readonly 
							                       value="<?php echo $oqsent; ?>" 
												   onkeydown="return nextbox(event, 'DOAFTER');"> วัน </td>
						</tr>
						<tr>
							<td class="rt" width="75">ล่วงหน้า :</td>
							<!-- 74 DOTERM1-->
							<input type="hidden" name="DOTERM1" size="17" readonly value="<?php echo $oqterm1; ?>" onclick="chk()">
							<td width="205"><input type="text" name="DOTERM1" id="DOTERM1" readonly disabled 
							                       value="<?php echo $oqterm1; ?>" onclick="chk()"> % 
											<input type="text" name="txterm1" id="txterm1" readonly disable onclick="chk()">
							</td>	   

						</tr>
						<tr>
							<td class="rt" width="75">คงเหลือ :</td>
							<!-- 75 DOTERM2-->
							<input type="hidden" name="DOTERM2" size="17" readonly value="<?php echo $oqterm2; ?>" onclick="chk()">
							<td width="205"><input type="text" name="DOTERM2" id="DOTERM2" disabled readonly 
							                       value="<?php echo $oqterm2; ?>" onclick="chk()"> % </td>
						</tr>
						<tr>
							<td class="rt" width="75">กำหนดชำระ :</td>
							<!-- 76 DOAFTER-->
							<td width="205"><input type="text" name="DOAFTER" id="DOAFTER" size="6" readonly value="<?php echo $oqafter; ?>" onkeydown="return nextbox(event, 'DOSPEC');"> วัน </td>
						</tr>
						<tr>
							<td class="rt" width="75">ค่าออกแบบ :</td>
							<!-- 77 DOSPEC-->
							<td width="205"><input type="text" name="DOSPEC" id="DOSPEC" size="8" readonly value="<?php echo $oqspec; ?>" onkeydown="return nextbox(event, 'DOSPECNAME');"> บาท </td>
							<td width="178"></td>
							<td width="355"></td>
						</tr>
						<tr>
							<td class="rt" width="74">ผู้ออกแบบ :</td>
							<!--78 DOSPECNAME-->
							<td width="205"><input type="text" name="DOSPECNAME" id="DOSPECNAME" size="30" readonly value="<?php echo $oqspecname; ?>" onkeydown="return nextbox(event, 'DONOTE');"></td>
						</tr>
					</table>
					<table>
						<tr>
							<td class="rt" width="73">Note :</td>
							<!-- 79 DONOTE-->
							<td width="750"><input type="text" name="DONOTE" id="DONOTE" size="90" readonly value="<?php echo $oqnote; ?>" onkeydown="return nextbox(event, 'DDATE');"></td>
						</tr>
					</table>
					<table>
						<td><input type="hidden" name="DPDATE" value="<?php echo date('Y-m-d'); ?>"></td><!-- 80 PDDATE-->
						<td><input type="hidden" name="DLOCK" value="<?php echo $olock; ?>"></td><!-- 81 DLOCK-->
						<td><input type="hidden" name="DLOST" value="<?php echo ''; ?>"></td><!-- 82 DLOST-->
					</table>
					<td></td>
					<td><button>บันทึกข้อมูล(save)</button> &nbsp;&nbsp;<a href='actionController.php?action=seller/retrieveDeliveryAction'>ย้อนกลับ</a></td>
				</form>
	</body>

</html>