<?php include '_session_login.php' ?>
<!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>
	<link rel="stylesheet" href="css/style.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<title></title>
<style type="text/css">
@media print{
.print_text {
	line-height: 1.0em;
	font-size:16px;
	font-family: "AngsanaUPC";
}
}
select {
	text-align: center;
	text-align-last: center;
  border:none;
}
option {
	text-align: left;
}
</style>

<style>
#p2{
	font-size:0.7em;
}
.p2{
	font-size:0.7em;
}
table {
  border-collapse: collapse;
}
select {
  border: 0 !important;
  -webkit-appearance: none;
  -moz-appearance: none;
  background: url('dropdown_arrow.png') no-repeat;
  background-position: 0px 0px;
  text-indent: 0px;
  text-overflow: "";
  color: black;
	text-align:center;
}
.td {
	border: 1px solid black;
	padding: -1px;
	line-height: 0.9em;
}
@media print {
  @page {
		margin: 0;
	}
  body {
		margin: 1.6cm;
	}
}
</style>

<style type="text/css" media="print">
.inputprint{
    display:none;
}
@page { size: landscape; }
body{
	line-height: 7pt;
}
</style>

<style>
.div {
    width: 40%;
    padding: 15px;
    border: 15px solid DarkOrange;
    margin: 15px;
}
</style>

</head>
<body>
<?php
$_month_name = array("01"=>"มกราคม",  "02"=>"กุมภาพันธ์",  "03"=>"มีนาคม",
	 "04"=>"เมษายน",  "05"=>"พฤษภาคม",  "06"=>"มิถุนายน",
	 "07"=>"กรกฎาคม",  "08"=>"สิงหาคม",  "09"=>"กันยายน",
	 "10"=>"ตุลาคม", "11"=>"พฤศจิกายน",  "12"=>"ธันวาคม");
$m = date('m');
$Y1 = date('Y')+543;
$MM = $_month_name[$m];
include '_connect.php';
$DMYs1 = date('Y-m-d')." 00:00:00.000";
$DMY2 = date('d')." &nbsp;&nbsp;".$MM." &nbsp;&nbsp;".$Y1;

function convert($number){
$txtnum1 = array('ศูนย์','หนึ่ง','สอง','สาม','สี่','ห้า','หก','เจ็ด','แปด','เก้า','สิบ');
$txtnum2 = array('','สิบ','ร้อย','พัน','หมื่น','แสน','ล้าน','สิบ','ร้อย','พัน','หมื่น','แสน','ล้าน');
$number = str_replace(",","",$number);
$number = str_replace(" ","",$number);
$number = str_replace("บาท","",$number);
$number = explode(".",$number);
if(sizeof($number)>2){
return '';
exit;
}
$strlen = strlen($number[0]);
$convert = '';
for($i=0;$i<$strlen;$i++){
	$n = substr($number[0], $i,1);
	if($n!=0){
		if($i==($strlen-1) AND $n==1){ $convert .= 'เอ็ด'; }
		elseif($i==($strlen-2) AND $n==2){  $convert .= 'ยี่'; }
		elseif($i==($strlen-2) AND $n==1){ $convert .= ''; }
		else{ $convert .= $txtnum1[$n]; }
		$convert .= $txtnum2[$strlen-$i-1];
	}
}
$convert .= 'บาท';
if($number[1]=='0' OR $number[1]=='00' OR
$number[1]==''){
$convert .= 'ถ้วน';
}else{
$strlen = strlen($number[1]);
for($i=0;$i<$strlen;$i++){
$n = substr($number[1], $i,1);
	if($n!=0){
	if($i==($strlen-1) AND $n==1){$convert
	.= 'เอ็ด';}
	elseif($i==($strlen-2) AND
	$n==2){$convert .= 'ยี่';}
	elseif($i==($strlen-2) AND
	$n==1){$convert .= '';}
	else{ $convert .= $txtnum1[$n];}
	$convert .= $txtnum2[$strlen-$i-1];
	}
}
$convert .= 'สตางค์';
}
return $convert;
}
// วิธีใช้งาน

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

$value = $row_3['REG_NO']; //ค่าเดิมที่อ่านมา
$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;

for($i=0;$i<count($_POST["chkPrint"]);$i++)
{

		$BILL_NO = substr($_POST["chkPrint"][$i],0,-2);
    $FEE_NO1S = substr($_POST["chkPrint"][$i],16);

    $sql = "SELECT a.*,  a.CHK_BERG as cb_date, a.BG_PROV as prov_no1, a.FEE_COST as FEE_COST1,
    b.*,
    c.*,
    d.*,
    e.*,
    f.*
    FROM EHONGDB.dbo.REG_BILLDATADOCUMENT a
    LEFT OUTER JOIN (SELECT * FROM [EHONGDB].[dbo].[PROV]) b ON (a.BG_PROV = b.PR_CODE)
    LEFT OUTER JOIN (SELECT * FROM [CONFIG].[dbo].[REG_BILLDATA_V1]) c ON (a.BILL_NO=c.BILL_NO) AND (a.FEE_NO=c.FEE_NO)
    LEFT OUTER JOIN (SELECT * FROM [EHONGDB].[dbo].[REG_BILL]) d ON (a.BILL_NO=d.BILL_NO)
    LEFT OUTER JOIN (SELECT * FROM [CONFIG].[dbo].[tsp_transport]) e ON (a.BG_PROV=e.tsp_no)
    LEFT OUTER JOIN (SELECT * FROM [EHONGDB].[dbo].[REG_FEEDATA]) f ON (a.FEE_NO=f.FEE_NO)
    WHERE a.BILL_NO = '$BILL_NO' and a.FEE_NO = $FEE_NO1S and (a.status<>'V') and (a.chkdate2 is not null and a.chkstatus2='C') and (a.acpdate is not null) and (a.chk_berg is not null)
    ORDER BY b.PR_CODE,a.CHK_BERG";
    $select=$conn->prepare($sql);
    $select->execute();
    $fetch=$select->fetch();
    // echo $sql."<br>";
		$x = $fetch['FEE_COST1'];

		$CHK_BERG1 = substr($fetch['cb_date'],0,4)+543; $CHK_BERG2 = substr($fetch['cb_date'],5,2); $CHK_BERG3 = substr($fetch['cb_date'],8,2);
		$CHK_BERG = $CHK_BERG3." &nbsp;&nbsp;". $_month_name[$CHK_BERG2]."&nbsp;&nbsp; ".$CHK_BERG1;

		$query = "SELECT * FROM [CONFIG].[dbo].[tsp_ehongmd] WHERE tsp_transport = '$fetch[PR_NAME]'";
		$select_1 = $conn->prepare($query);
		$select_1 -> execute();
		$row_1 = $select_1->fetch();

		$query = "SELECT * FROM [CONFIG].[dbo].[tsp_transport] WHERE tsp_transport = '$fetch[PR_NAME]'";
		$select_2 = $conn->prepare($query);
		$select_2 -> execute();
		$row_2 = $select_2->fetch();

		$query = "SELECT * FROM [EHONGDB].[dbo].[CROPORATION] WHERE CROP_NAME = '$fetch[CROP_NAME]'";
		$select_3 = $conn->prepare($query);
		$select_3 -> execute();
		$row_3 = $select_3->fetch();

		$CROP_NO1 = $row_3["CROP_NO"];
		$REG_NO1 = $value+$i;
		$IDNO1 = $CROP_NO1." ".$REG_NO1;

    $FEE_COST1 = $_POST['FEE_COST1'][$i];
    $SUM1 = $_POST['SUM1'][$i];
    $SUM2 = $_POST['SUM2'][$i];
    $SUM3 = $_POST['SUM3'][$i];
    $SUM4 = $_POST['SUM4'][$i];

		// echo $_POST['SUM1'][$i].$i;
		// echo $FEE_COST1."<br>";
		// echo $SUM1."<br>";
		// echo $SUM2."<br>";
		// echo $SUM3."<br>";
		// echo $SUM4."<br>";
		//
    // $strSQL = "INSERT INTO CONFIG.dbo.REG_BILLDATA_V1";
    // $strSQL .="(BILL_NO, FEE_NO, REG_NO, REG_DATE, REG_CROP,
    //             REG_CT_NO, REG_BANDNAME, REG_SERIAL_NO, 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_STATUS, REG_CROPNO) ";
    // $strSQL .="VALUES ";
    // $strSQL .="('$BILL_NO','$FEE_NO1S','$REG_NO1','".$fetch['cb_date']."','".$fetch['CROP_NAME']."',
    //             '".$fetch['CT_NO']."','".$fetch['BRAND_NAME']."','".$fetch['SERIAL_NO']."','".$fetch['PSERIAL_NO']."','".$row_1['tsp_open']."',
    //             '$FEE_COST1','$SUM1','$SUM4','".$row_2["tsp_registrar1"]."','$SUM2',
    //             '".$row_2["tsp_registrar"]."','$SUM3','".$fetch['prov_no1']."','','',
		// 						'$DMYs1','Y','$CROP_NO1')";
    // $result = $conn->query($strSQL);
    // echo $strSQL;
?>
<page size="A4">
<div class="div">
<form class="" name="frm" method="post" action="" target="_blank" id="form-search" >
<table border="0" width="100%" class="print_text" id="my-text">
  <tr>
    <td></td>
    <td align="right"> <label id="p2">เลขที่ <?php echo $IDNO1; ?></label>&nbsp; &nbsp;</td>
  </tr>
  <tr>
    <td width="15%">
      <img src="dist/img/logo_1.png" alt="" width="100%" height="auto">
    </td>
    <td><center><p>
				<?php echo $fetch['CROP_NAME']; ?><br>
        <!-- <input type="text" name="REG_CROP" class="" id="cop1" style="border:none; font-size:16px;font-family:AngsanaUPC; text-align: center;" size="50" value=""> <br> -->
        <b>ใบรับรองแทนใบเสร็จรับเงิน</b>
    </p></center></td>
  </tr>
  <tr>
    <td ></td>
    <td align="right"> <label id="p2">วันที่ <?php echo $CHK_BERG; ?></label>&nbsp; &nbsp;</td>
  </tr>
</table>
<table border="0" width="100%" class="print_text">
  <tr>
    <td><label id="p2">&nbsp; ผู้รับเงินจากบริษัท</label></td>
    <td width="80%"><label id="p2" style="color:#1E90FF;">1. <?php echo $row_2['tsp_registrar1']; ?> &nbsp; &nbsp;&nbsp; &nbsp;(หัวหน้าห้องตรวจ)</label></td>
  </tr>
  <tr>
    <td></td>
    <td><label id="p2" style="color:#1E90FF;">2. <?php echo $row_2['tsp_registrar']; ?> &nbsp; &nbsp;&nbsp; &nbsp;(หัวหน้าทะเบียน)</label></td>
  </tr>
  <tr>
    <td><label id="p2">&nbsp; ขนส่งจังหวัด</label></td>
    <td><label id="p2" style="color:#1E90FF;"><?php echo $row_2['tsp_transport']; ?> </label></td>
  </tr>
</table>
 <br>
<table border="1" width="100%" class="print_text">
  <tr>
    <td align="center" width="10%"><label id="p2"><b>ลำดับ</b></label></td>
    <td align="center" width="50%"><label id="p2"><b>รายละเอียดรายจ่าย</b></label></td>
    <td align="center" width="10%"><label id="p2"><b>จำนวนเงิน</b></label></td>
    <td align="center" width="10%"><label id="p2"><b>หมายเหตุ</b></label></td>
  </tr>
  <tr>
    <td align="center"><label id="p2" style="color:#1E90FF;">1.</label></td>
    <td><label id="p2" style="color:#1E90FF;">&nbsp; ค่าธรรมเนียมกรมขนส่ง <?php echo $row_2['tsp_transport']; ?></label></td>
    <td align="right"><label id="p2" style="color:#1E90FF;"><?php echo $SUM1; ?></label>&nbsp;</td>
    <td><label id="p2" style="color:#1E90FF;">&nbsp; </label></td>
  </tr>
  <tr>
    <td align="center"><label id="p2" style="color:#1E90FF;">2.</label></td>
    <td><label id="p2" style="color:#1E90FF;">&nbsp; ค่าตรวจ <?php echo $row_2['tsp_registrar1']; ?> </label></td>
    <td align="right"><label id="p2"  style="color:#1E90FF;"><?php echo $SUM3; ?></label>&nbsp;</td>
    <td><label id="p2"></label></td>
  </tr>
  <tr>
    <td align="center"><label id="p2" style="color:#1E90FF;">3.</label></td>
    <td><label id="p2" style="color:#1E90FF;">&nbsp; ค่าบริการ <?php echo $row_2['tsp_registrar']; ?> </label></td>
    <td align="right"><label id="p2"  style="color:#1E90FF;"><?php echo $SUM2; ?></label>&nbsp;</td>
    <td><label id="p2"></label></td>
  </tr>
  <tr>
    <td align="center" width="10%"><label id="p2"><b></b></label> <br> </td>
    <td align="center" width="50%"><label id="p2"><b></b></label> <br> </td>
    <td align="center" width="10%"><label id="p2"><b></b></label> <br> </td>
    <td align="center" width="10%"><label id="p2"><b></b></label> <br> </td>
  </tr>
	<tr>
		<td align="center"><label id="p2" style="color:#1E90FF;"></label></td>
		<td><label id="p2" style="color:#1E90FF;">&nbsp; เลขที่สัญญา <?php echo $fetch['CT_NO']; ?> </label></td>
		<td><label id="p2"></label></td>
		<td><label id="p2"></label></td>
	</tr>
  <tr>
    <td align="center"></td>
    <td><label id="p2" style="color:#1E90FF;">&nbsp; ยี่ห้อรถ <?php echo $fetch['BRAND_NAME']; ?> </label> </td>
    <td><label id="p2"></label></td>
    <td><label id="p2"></label></td>
  </tr>
	<tr>
		<td align="center"></td>
		<td><label id="p2" style="color:#1E90FF;">&nbsp; เลขที่เครื่องรถ <?php echo $fetch['SERIAL_NO']; ?> </label></td>
		<td><label id="p2"></label></td>
		<td><label id="p2"></label></td>
	</tr>
  <tr>
    <td align="center"></td>
    <td><label id="p2" style="color:#1E90FF;">&nbsp; เลขที่ตัวถังรถ <?php echo $fetch['PSERIAL_NO']; ?> </label></td>
    <td><label id="p2"></label></td>
    <td><label id="p2"></label></td>
  </tr>
</table>
<table border="1" width="100%" class="print_text" id="my-text">
  <tr>
    <br>
    <td align="center" width="50%" colspan="2"> <label id="p2" style="color:#1E90FF;"> <?php echo  "(" .convert($x).")"; ?></label> </td>
    <td align="center" width="10%"><label id="p2">รวมทั้งสิ้น</label></td>
    <td align="right" width="10%"><label id="p2" style="color:#1E90FF;"><?php echo number_format($x,2); ?></label> &nbsp;</td>
    <td align="center" width="10%"><label id="p2"></label></td>
  </tr>
</table>
<br>
<table border="0" width="100%" class="print_text" id="my-text">
  <tr>
    <td colspan="4">
      <label id="p2">ข้าพเจ้า &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <?php echo $row_1['tsp_open']; ?>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
      <label id="p2">(ผู้เบิกจ่าย) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ตำแหน่ง &nbsp;&nbsp;&nbsp;&nbsp; เจ้าหน้าที่</label> <!-- <input type="text" name="" class="width-dynamic"  id="p2" style="border:none; font-family:Angsana New;" value="ตำแหน่ง..">  --> <br>
      <label id="p2">ขอรับรองว่า รายจ่ายข้างต้นไม่อาจเรียกเก็บใบเสร็จรับเงินจากผู้รับได้ และข้าพเจ้าได้จ่ายไปในงานของทาง</label>
      <br>
      <label id="p2">บริษัทอีฮง มดแดง โดยแท้ ตั้งแต่วันที่ &nbsp;&nbsp;&nbsp; <?php echo $CHK_BERG; ?></label>&nbsp;&nbsp;&nbsp;
      <label id="p2"> ถึงวันที่ &nbsp;&nbsp;&nbsp; <?php echo $DMY2; ?></label>
    </td>
  </tr>
</table>
<table border="0" width="100%" class="print_text" id="my-text">
  <tr>
    <td  width="60%"></td>
    <td align="center"><br>
      <!-- <div id="tag7"></div> -->

			<label id="p2">ลงชื่อ</label> &nbsp;&nbsp;&nbsp;&nbsp;<img src="img/<?php echo $row_1['tsp_img']; ?>" alt="" width="40%">&nbsp;&nbsp;&nbsp;&nbsp; <label id="p2">(ผู้เบิกจ่าย)</label><br>
      <!-- <label id="p2">ลงชื่อ &nbsp;&nbsp;<?php echo $row_1['REG_USERBREG']; ?>&nbsp;&nbsp;(ผู้เบิกจ่าย)</label><br> -->
      <label id="p2"><?php echo "(".$row_1['tsp_open'].")"; ?></label> <br>
      <label id="p2">ลงชื่อ</label> &nbsp;&nbsp;&nbsp;&nbsp;<img src="img/S4.jpg" alt="" width="40%">&nbsp;&nbsp;&nbsp;&nbsp; <label id="p2">(ผู้อนุมัติ)</label><br>
      <label id="p2">(นางสาวจิรัชญา ภูสิม)</label>
    </td>
  </tr>
</table>
<br>
<!-- <input type="hidden" name="sss1" id="sss1" value="180.00"> -->
</form>
</page>
<div style='page-break-after: always'></div>
</div>
<br><br><br><br>

<script language="JavaScript">
function startCalc(){
	interval = setInterval("calc()",1);
}
function chk(){
	var a1 = parseFloat(document.frm.a1.value.replace(/,/g, ''));
	var a2 = parseFloat(document.frm.a2.value.replace(/,/g, ''));
	var a3 = parseFloat(document.frm.a3.value.replace(/,/g, ''));
	result1 = (a1 * 1) + (a2 * 1) + (a3 * 1);
	if (isNaN(result1)) { document.frm.a1sum.value = ""; }
	else {
    document.frm.a1sum.value = result1.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  }
	result2 = (a2 * 1) + (a3 * 1);
	document.frm.sss1.value = result2.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
	}
	function stopCalc(){
		clearInterval(interval);
	}
</script>

<script>
 	function autofill(){
 		var tagc = $("#tag").val();
 		$.ajax({
 			url : '_autofill_1.php',
 			data : 'tagc='+tagc
 		}).success(function(data){
 			var json = data,
 			obj = JSON.parse(json);
 			$("#tag1").val(obj.id1);
      $("#tag2").val(obj.id2);
      $("#tag3").val(obj.id3);
      $("#tag4").val(obj.id4);
      $("#tag5").val(obj.id5);
      $("#tag6").val(obj.id6);
      $("#tag7").html('<img src="img/'+obj.id7+'" width="40%">')
			$("#tag8").val(obj.id8);
 		});

    var tagco = $("#auto").val();
    $.ajax({
      url : '_autocomplete_1.php',
      data : 'tagco='+tagco
    }).success(function(data){
      var json = data,
      obj = JSON.parse(json);
      $("#cop1").val(obj.cop1);
			$("#cop2").val(obj.cop2);
			$("#cop3").val(obj.cop3);
			$("#cop4").val(obj.cop4);
			$("#cop5").val(obj.cop5);
    });
 	}
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js'>
</script><script  src="script.js"></script>
<script>
// function myFunction() {
//   window.print();
// }
// function() {
  // window.print();
	// window.exit();
// }
</script>
<?php } ?>
</body>
</html>
