<?php
include '../_connect.php';
$y1 = date('Y')+543;
$y = substr($y1,2,2);
$m = date('m');
// $DAYS = $Y1."-".$M1."-".$D1;
$DAYS = $_POST['DMY'];
$DAYS1 = $_POST['DMY1'];
$DAYS2 = $_POST['DMY2'];

$REG_PSERIAL_NO =  $_POST["REG_PSERIAL_NO"];
$query = "SELECT * FROM [EHONGDB].[dbo].[REG_BILLDATA] WHERE PSERIAL_NO = '$REG_PSERIAL_NO'";
$select=$conn->prepare($query);
$select->execute();
$row_21 = $select->fetch();
$xcorp_no = $row_21['xcorp_no'];

$query = "SELECT * FROM [EHONGDB].[dbo].[CROPORATION] WHERE CROP_NO = '$xcorp_no'";
$select=$conn->prepare($query);
$select->execute();
$row_2 = $select->fetch();
$CROP_NO = $row_2['CROP_NO'];

$query = "SELECT MAX(REG_NO) ID_NOs FROM [CONFIG].[dbo].[REG_BILLDATATRANSPORT]";
$select=$conn->prepare($query);
$select->execute();
$row_3 = $select->fetch();
// echo $query;
// echo $row_3['ID_NOs'];

$value = $row_3['ID_NOs']; //ค่าเดิมที่อ่านมา
// $value = '2003001';
$old = str_split($value,4);
$date = $old[0];
$number = $old[1];
$newdate = date("ym");
if($date!=$newdate)
{
    $number = 1;
    $number = sprintf("%03d",$number);
}else{
    $number = sprintf("%03d",++$number);;
}
$value = $newdate.$number;
// echo $number."<br>";
// echo $value."<br>";
// $REG_NO = $xcorp_no." ".$y."/".$m."/"."001";
$strSQL = "INSERT INTO CONFIG.dbo.REG_BILLDATATRANSPORT";
$strSQL .="(REG_NO, REG_DATE, REG_CROP, REG_BANDNAME, REG_PSERIAL_NO,
            REG_USERBREG, REG_BREG, REG_BILLBREG, REG_BILLBREGTRANSPORT ,REG_USERTRANSPORT1,
            REG_BILLUSER1, REG_USERTRANSPORT2, REG_BILLUSER2, REG_PROVINCE, REG_DIFFERENCE,
            REG_NOTE, REG_DATE1, REG_DATE2, REG_CORPNO , REG_CT_NO, REG_SERIAL_NO, REG_STATUS) ";
// $strSQL .="(REG_PSERIAL_NO) ";
$strSQL .="VALUES ";
$strSQL .="('$value','$DAYS','".$_POST["REG_CROP"]."','".$_POST["REG_BANDNAME"]."','".$_POST["REG_PSERIAL_NO"]."',
            '".$_POST["REG_USERBREG"]."','".$_POST["REG_BREG"]."','".$_POST["a1"]."','".$_POST["sss1"]."','".$_POST["REG_USERTRANSPORT1"]."',
            '".$_POST["a2"]."','".$_POST["REG_USERTRANSPORT2"]."','".$_POST["a3"]."','".$_POST["tsp_transport"]."','',
            '".$_POST["REG_NOTE"]."','$DAYS1','$DAYS2','$CROP_NO','".$_POST["REG_CT_NO"]."','".$_POST["REG_SERIAL_NO"]."','".$_POST["REG_STATUS"]."')";
$result = $conn->query($strSQL);
echo $strSQL;
if($result)
{
  header("location:../transport_print_1.php?REG_NO=$value");
}
else
{
  header("location:../transport_index_5.php");
}

?>
