<!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;
		}
		.button{
			background-color: blue;
			/* border: 2px solid red; */
  			border-radius: 5px;	
			padding: 15px 25px;
			font-size: 15px;
		}
		.button a{
			height: 50px;
			font-size: 15px;
		}
		.fcc-btn{
			background-color: #199319;
  			color: white;
			padding: 15px 25px;
			border-radius: 5px;	
		}
	</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});
	
			}
		}
	</script>

	<body>
		<?php
		error_reporting(0);

		include "functions/dblink.php";

        if (isset($_GET['action'])) {
			$action = $_GET['action'];
			//ถ้าเป็นเพิ่มข้อมูล ก็ไม่ต้องทำอะไร เพื่อให้ฟอร์มนั้นว่างเปล่าสำหรับรับข้อมูลใหม่
			//if($action=="insert")
			if ($action == "seller/addDeliveryActionS") {
                $id = $_GET['no'];
				$h = "เพิ่มข้อมูลการจัดส่ง";
                $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 สุดท้าย
                //echo $data;
				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;
			}
		}

        $no = $_GET['no'];
		//echo "From $_GET = ".$no;
		$ono = $_GET['ONO'];
        $olost = $_GET['olost'];

		$sql = "SELECT * FROM ordr001 WHERE NO='$no'";
		$result = mysqli_query($con, $sql);
        $data = mysqli_fetch_array($result);

			$oqc_disc = $data['OQC_DISC'];
			//กรณี ลดเป็นก้อนทั้งใบคิดส่วนลดต่อ 1 หน่วยยอดขาย
			$disc_unit=$oqc_disc/$oqsum;

			$x1 = $data['X1'];
			$oqty1=$data['OQQTY1'];
			$prc1=$data['OQSLEPRC1'];
			$disc_unit1=$oqty1*$prc1*$disc_unit;

			$x2 = $data['X2'];
			$oqty2=$data['OQQTY2'];
			$prc2=$data['OQSLEPRC2'];
			$disc_unit2=$oqty2*$prc2*$disc_unit;

			$x3 = $data['X3'];
			$oqty3=$data['OQQTY3'];
			$prc3=$data['OQSLEPRC3'];
			$disc_unit3=$oqty3*$prc3*$disc_unit;

			$x4 = $data['X4'];
			$oqty4=$data['OQQTY4'];
			$prc4=$data['OQSLEPRC4'];
			$disc_unit4=$oqty4*$prc4*$disc_unit;

			$x5 = $data['X5'];
			$oqty5=$data['OQQTY5'];
			$prc5=$data['OQSLEPRC5'];
			$disc_unit5=$oqty5*$prc5*$disc_unit;

			$x6 = $data['X6'];
			$oqty6=$data['OQQTY6'];
			$prc6=$data['OQSLEPRC6'];
			$disc_unit6=$oqty6*$prc6*$disc_unit;

			$x7 = $data['X7'];
			$oqty7=$data['OQQTY7'];
			$prc7=$data['OQSLEPRC7'];
			$disc_unit7=$oqty7*$prc7*$disc_unit;

			$x8 = $data['X8'];
			$oqty8=$data['OQQTY8'];
			$prc8=$data['OQSLEPRC8'];
			$disc_unit8=$oqty8*$prc8*$disc_unit;

			$x9 = $data['X9'];
			$oqty9=$data['OQQTY9'];
			$prc9=$data['OQSLEPRC9'];
			$disc_unit9=$oqty9*$prc9*$disc_unit;

			$x10 = $data['X10'];
			$oqty10=$data['OQQTY10'];
			$prc10=$data['OQSLEPRC10'];
			$disc_unit10=$oqty10*$prc10*$disc_unit;

            $oqsum = $data['OQSUM'];
			$oqsumdisc = $data['OQSUMDISC'];
            $oqterm1 = $data['OQTERM1'];
			$term1=($oqsum-$oqsumdisc)*($oqterm1/100);
			$oqterm2 = $data['OQTERM2'];
			$term2=($oqsum-$oqsumdisc)-$term1;

        ?>    

        <form method="post" action="" name="frm">
			<!-- <form name="frm" onsubmit="return check();"> -->
			<div style='font-size:18px; color:red;' align='center'><?php echo $h; ?></div>
			<!-- <div id="all" align="center"></div>             -->
            <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>
            
            <!-- 4 DONO -->
				<tr>
					<td width="74" style='text-align:right'>Order No. :</td>
					<td><input type="text" name="DONO" id="DONO" value="<?php echo $ono ?>" readonly></td>
				</tr>	
			</table>            
			
            <table width="830" height="29">
                <tr>
                    <td class="rt" width="75">วันที่ Order :</td>
                    <?php $date = new DateTime($data['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 $data['ODATE']; ?>">

                    <td class="rt" width="90">Customer 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 $data['OQC_NO']; ?>"></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 $data['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 $data['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 $data['OQC_CONTRACT']; ?>" 
                                            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 $data['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($data['OQC_DISC'], 2); ?>" 
                                onclick="chk()" onkeydown="return nextbox(event, 'DOC_ADRS');"> % </td>
                    <input type="hidden" name="Disc" value="<?php echo $data['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 $data['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 $data['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 $data['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 $data['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 $data['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 $data["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 $data["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($data["OQQTY{$i}"]-$data["X{$i}"]<0){echo 0;}
                                                else{echo 0;} ?> 
                                    max=<?php if($data["OQQTY{$i}"]-$data["X{$i}"]>0){echo $data["OQQTY{$i}"]-$data["X{$i}"];} 
                                                else{echo 0;}?> 
                                    style="text-align: right" size="5" 
                                    value="<?php if (($data["OQQTY{$i}"]!=0 OR $data["OQQTY{$i}"]!="") AND $data["OQQTY{$i}"]-$data["X{$i}"]>0) 
                                                        {echo $XQTY{$i} = $data["OQQTY{$i}"]-$data["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 $data["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 ($data["OQSLEPRC{$i}"] != 0) {echo number_format($data["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="tsle" style="text-align: right" disabled 
                        onclick="chk()"></td>
            </table>

            <table width="833">
                <tr>
                    <td class="rt" width="70">ผู้ขาย :</td>
                    <!-- 70 DOSMFNAME -->
                    <td width="146"><input type="text" name="DOSMFNAME" id="DOSMFNAME" size="20" readonly 
                                            value="<?php echo $data['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 $data['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 $data['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 $data['OQSENT']; ?>" 
                                            onkeydown="return nextbox(event, 'DOAFTER');"> วัน </td>
                </tr>
                <tr>
                    <td class="rt" width="75">ล่วงหน้า :</td>
                    <!-- 74 DOTERM1-->
                    <input type="hidden" name="DOTERM1" readonly value="<?php echo $oqterm1; ?>" onclick="chk()">
                    <td width="350"><input type="text" name="DOTERM1" id="DOTERM1" size="15" readonly disabled 
                        value="<?php echo $data['OQTERM1']; ?>" 
                        onclick="chk()"><span style="color: red;"> % เป็นเงิน <?php echo number_format($term1,2); ?> บาท</span></td>

                </tr>
                <tr>
                    <td class="rt" width="75">คงเหลือ :</td>
                    <!-- 75 DOTERM2-->
                    <input type="hidden" name="DOTERM2" readonly value="<?php echo $oqterm2; ?>" onclick="chk()">
                    <td width="350"><input type="text" name="DOTERM2" id="DOTERM2" size="15" disabled readonly 
                        value="<?php echo $data['OQTERM2']; ?>" 
                        onclick="chk()"><span style="color: red;"> % เป็นเงิน <?php echo number_format($term2,2); ?> บาท</span></td>
                </tr>
                <tr>
                    <td class="rt" width="80">กำหนดชำระ :</td>
                    <!-- 76 DOAFTER-->
                    <td width="205"><input type="text" name="DOAFTER" id="DOAFTER" size="6" readonly 
                                            value="<?php echo $data['OQAFTER']; ?>" 
                                            onkeydown="return nextbox(event, 'DOSPEC');"> วัน </td>
                </tr>
                <tr>
                    <td class="rt" width="78">ค่าออกแบบ :</td>
                    <!-- 77 DOSPEC-->
                    <td width="205"><input type="text" name="DOSPEC" id="DOSPEC" size="8" readonly 
                                            value="<?php echo $data['OQSPEC']; ?>" 
                                            onkeydown="return nextbox(event, 'DOSPECNAME');"> บาท </td>
                    <td width="178"></td>
                    <td width="355"></td>
                </tr>
                <tr>
                    <td class="rt" width="76">ผู้ออกแบบ :</td>
                    <!--78 DOSPECNAME-->
                    <td width="205"><input type="text" name="DOSPECNAME" id="DOSPECNAME" size="30" readonly 
                                            value="<?php echo $data['OQSPECNAME']; ?>" 
                                            onkeydown="return nextbox(event, 'DONOTE');"></td>
                </tr>
            </table>
            <table>
                <tr>
                    <td class="rt" width="70">Note :</td>
                    <!-- 79 DONOTE-->
                    <td width="750"><input type="text" name="DONOTE" id="DONOTE" size="90" readonly 
                                            value="<?php echo $data['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=""></td><!-- 81 DLOCK-->
                <td><input type="hidden" name="DLOST" 
                            value="<?php echo ''; ?>"></td><!-- 82 DLOST-->
            </table>
            <td></td><br><br>
			<div class='btn' align='center'>
				<td><button class="button button" type="submit" value="Submit">บันทึกข้อมูล(save)</button> &nbsp;&nbsp;			
					<a style='font-size:17px' class="fcc-btn" href='actionController.php?action=seller/retrieveDeliveryAction'>ย้อนกลับ</a>
				</td>
			</div>
		</form>     
        
        <?php                
		
		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 {//echo "From $_GET = ".$no;
				echo "<script language='javascript'>
					alert('\\nDelivery No. $x ถูกบันทึกเรียบร้อย\\nFrom\\nOrder No. $ono\\n\\nกด OK เพื่อนกลับ Delivery List Page');
					window.location='actionController.php?action=seller/retrieveDeliveryAction';
				</script>";

				$dNo = $_POST['DNO'];

				$sqlNo = "SELECT * FROM delv001 WHERE DNO = '$dNo' ";
				$resNo = mysqli_query($con, $sqlNo); //$delivNo
				$dtQty = mysqli_fetch_assoc($resNo);
				//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']);
				//}
				//ส่วนลด

				//รวม
					$tot=(($x1+$xqty1)*$prc1)+(($x2+$xqty2)*$prc2)+(($x3+$xqty3)*$prc3)+
					     (($x4+$xqty4)*$prc4)+(($x5+$xqty5)*$prc5)+(($x6+$xqty6)*$prc6)+
						 (($x7+$xqty7)*$prc7)+(($x8+$xqty8)*$prc8)+(($x9+$xqty9)*$prc9)+
						 (($x10+$xqty10)*$prc10);
					if($oqsumdisc>=100){//ลดทีเดียวทั้งใบ คิดเป็นเงิน
						$dis_tot=$disc_unit*$tot;
					}else{//ลดเป็น % คิดเป็นเงิน
						$dis_tot=$oqc_disc*$tot/100;
					}
				mysqli_query($con, "UPDATE delv001 
				SET DOSUMDISC=$dis_tot ,DOSUM=$tot WHERE DNO = '$dNo'");

				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=OQSUMDISC-$dis_tot,OQSUM=OQSUM-$tot,OPDATE = curdate() 
				WHERE NO = $no ");
				
				back();
			 } //ฟังค์ชั่นนี้อยู่ในขั้นตอน  1
				?>
				</div>
			<?php

		}	

		//ถ้าเป็นการ Postback เพื่อส่งข้อมูลกลับเข้ามา
		function back()
		{
			global $con;
			mysqli_close($con);
			exit("<p><a style='font-size:17px' href='actionController.php?action=seller/retrieveOrderAction'>ย้อนกลับ</a>
					</p></body></html>");
		}
		mysqli_close($con);

			?>
	</body>

</html>