<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>EHONGMD</title>
  <?php
  include '_plugin.php';
  include '_connect.php';
  ?>
  <script src="bower_components/jquery/dist/jquery.min.js"></script>

</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
<?php
include '_header.php';
include '_menu.php';
?>
  <div class="content-wrapper">
    <section class="content">
      <div class="row">
      <!-- ค้นหา -->
      <?php
      $Search_No = $_POST['search'];
      $code = "a.pserial_no = '$Search_No' ";
      ?>
      <?php
      include '_connect.php';
      $_month_name = array("01"=>"มกราคม",  "02"=>"กุมภาพันธ์",  "03"=>"มีนาคม",
         "04"=>"เมษายน",  "05"=>"พฤษภาคม",  "06"=>"มิถุนายน",
         "07"=>"กรกฎาคม",  "08"=>"สิงหาคม",  "09"=>"กันยายน",
         "10"=>"ตุลาคม", "11"=>"พฤศจิกายน",  "12"=>"ธันวาคม");
      $m = date('m');
      include 'reg_search_1.2.php'
      ?>
      <!-- ตาราง -->
      <div class="col-md-12">
        <div class="box box-success">
          <div class="box-header">
            <h3 class="box-title"><p class="text-success">ค้นหา รายงานการออกใบเสร็จ</p></h3>
          </div>
          <div class="box-body">

            <div class="form-group" style="overflow-x:auto;">
              <table id="" class="table-bordered table-striped" width="180%" border="0">
                <tr>
                  <td width=""> <div align="center"><b>No.</div></td>
                  <td width=""> <div align="center"><b>พิมพ์ใบแทนรับเงิน</div></td>
                  <td width=""> <div align="center"><b>แก้ไข</div></td>
                  <td width=""> <div align="center"><b>วันที่เบิก</div></td>
                  <td width=""> <div align="center"><b>เลขที่ใบเบิก</div></td>
                  <td width=""> <div align="center"><b>#เบิก</div></td>
                  <td width=""> <div align="center"><b>ชื่อลูกค้า</div></td>
                  <td width=""> <div align="center"><b>รายการเบิก</div></td>
                  <td width=""> <div align="center"><b>บริษัทที่เบิก</div></td>
                  <td width=""> <div align="center"><b>ยี่ห้อ</div></td>
                  <td width=""> <div align="center"><b>เลขที่เครื่อง</div></td>
                  <td width=""> <div align="center"><b>เลขตัวถัง</div></td>
                  <td width=""> <div align="center"><b>ชื่อผู้เบิก</div></td>
                  <td width=""> <div align="center"><b>ค่าใช้จ่ายในการเบิก</div></td>
                  <td width=""> <div align="center"><b>ค่าใช้จ่ายที่มีใบเสร็จ (บาท)</div></td>
                  <td width=""> <div align="center"><b>ค่าใช้จ่ายที่จ่ายให้ขนส่ง</div></td>
                  <td width=""> <div align="center"><b>ชื่อเจ้าหน้าที่ให้ขนส่ง 1</div></td>
                  <td width=""> <div align="center"><b>มูลค่าที่ได้รับ (ค่าตรวจ)</div></td>
                  <td width=""> <div align="center"><b>ชื่อเจ้าหน้าที่ให้ขนส่ง 2</div></td>
                  <td width=""> <div align="center"><b>มูลค่าที่ได้รับ (ค่าบริการ)</div></td>
                  <td width=""> <div align="center"><b>จังหวัด</div></td>
                  <td width=""> <div align="center"><b>วันที่พิมพ์</div></td>
                  <td width=""> <div align="center"><b>ผู้พิมพ์</div></td>
                </tr>
                  <tr>
                  <?php
                   $sql = "SELECT DISTINCT a.BILL_NO, a.CHK_BERG as cb_date, a.BG_PROV as prov_no1, a.FEE_COST as FEE_COST1,
                   b.PR_NAME, b.PR_CODE,
                   c.REG_NO, c.REG_CROPNO, c.FEE_NO, c.REG_DATE1, c.REG_BREG, c.REG_BILLBREG, c.REG_BILLBREGTRANSPORT, c.REG_BILLUSER1, c.REG_BILLUSER2, c.REG_SAVEUSER, c.REG_STATUS,
                   d.BILL_NO, d.PS_NAME, d.CROP_NAME, d.BRAND_NAME, d.SERIAL_NO, d.PSERIAL_NO,
                   e.tsp_no, e.tsp_open, e.tsp_registrar1, e.tsp_registrar,
                   f.FEE_NO, f.FEE_NAME
                   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 $code 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 a.CHK_BERG ASC, b.PR_CODE ASC";
                   // ORDER BY b.PR_CODE,a.CHK_BERG
                   $select=$conn->prepare($sql);
                   $select->execute();
                   // echo $sql;
                   $FEE_COST = 0;
                   $REG_BILL1= 0;
                   $REG_BILL2= 0;
                   $REG_BILL3 = 0;
                   $REG_BILL4 = 0;
                   $item = 1;
                   $i = 0;
                    while($fetch=$select->fetch())
                    {
                      $i++;
                      if ($fetch['cb_date'] != '') {
                        $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." ". $_month_name[$CHK_BERG2]." ".$CHK_BERG1;
                      } else {
                        $CHK_BERG = '';
                      }
                      if ($fetch['REG_DATE1'] != '') {
                        $REG_DATE1 = substr($fetch['REG_DATE1'],0,4)+543; $REG_DATE2 = substr($fetch['REG_DATE1'],5,2); $REG_DATE3 = substr($fetch['REG_DATE1'],8,2);
                        $REG_DATE165 = $REG_DATE3." ". $_month_name[$REG_DATE2]." ".$REG_DATE1;
                      } else {
                        $REG_DATE165 = '';
                      }
                      include '_calculation.php';
                    ?>
                    <td style="text-align: center;"><?=$item."."?></td>
                    <td>
                      <?php if ($fetch['REG_STATUS'] != 'Y') { ?>
                      <form class="" action="reg_print_3.php?BILL_NO=<?php echo $fetch["BILL_NO"];?>&FEE_NO=<?php echo $fetch['FEE_NO']; ?>&1REG_NO=<?php echo $fetch['REG_NO']; ?>" method="POST" target="_blank">
                        <center><button id="myBtn" class="btn btn-warning" name="btnOpen">พิมพ์</button><center>
                      </form>
                      <?php } else {
                      ?>
                        <center><button id="myBtn" class="btn btn-success" name="btnOpen">พิมพ์แล้ว</button><center>
                      <?php } ?>
                    </td>
                    <td>
                      <?php if ($fetch['REG_STATUS'] == 'Y') { ?>
                      <form class="" action="reg_print_6.php?BILL_NO=<?php echo $fetch["BILL_NO"];?>&FEE_NO=<?php echo $fetch['FEE_NO']; ?>&1REG_NO=<?php echo $fetch['REG_NO']; ?>" method="POST" target="_blank">
                        <center><button id="myBtn" class="btn btn-danger" name="btnOpen">แก้ไข</button><center>
                      </form>
                      <?php } else { } ?>
                    </td>
                    <td><center><?php echo $CHK_BERG; ?></center></td>
                    <!-- <td><center><a href="#" class="text-green"><?php echo $fetch['REG_CROPNO']." ".$fetch['REG_NO'];?></a></center></td> -->
                    <td><center><a href="reg_print_5.php?BILL_NO=<?php echo $fetch["BILL_NO"];?>&FEE_NO=<?php echo $fetch['FEE_NO'];?>&1REG_NO=<?php echo $fetch['REG_NO'];?>" class="text-green" target="_blank"><?php echo $fetch['REG_CROPNO']." ".$fetch['REG_NO'];?></a></center></td>
                    <td><center><a><?php echo $fetch['BILL_NO']; ?></a></center></td>
                    <td><center><?php echo $fetch['PS_NAME']; ?></center></td>
                    <td><center><?php echo $fetch['FEE_NO'].":".$fetch['FEE_NAME']?></center></td>
                    <td><center><?php echo $fetch['CROP_NAME']; ?></center></td>
                    <td><center><?php echo $fetch['BRAND_NAME']; ?></center></td>
                    <td><center><?php echo $fetch['SERIAL_NO']; ?></center></td>
                    <td><center><?php echo $fetch['PSERIAL_NO']; ?></center></td>
                    <td><center><?php echo $fetch['tsp_open']; ?></center></td>
                    <td><center><a class="text-blue"><?php echo number_format($fetch['FEE_COST1'],2); ?></a></center></td>
                    <td><center><a class="text-blue"><?php echo number_format($SUM1,2); ?></a></center></td>
                    <td><center><a class="text-blue"><?php echo number_format($SUM4,2); ?></a></center></td>
                    <td><center><?php echo $fetch['tsp_registrar1']; ?></center></td>
                    <td><center><a class="text-blue"><?php echo number_format($SUM2,2); ?></a></p></center></td>
                    <td><center><?php echo $fetch['tsp_registrar']; ?></center></td>
                    <td><center><a class="text-blue"><?php echo number_format($SUM3,2); ?></a></center></p></td>
                    <td><center><?php echo $fetch['prov_no1'].":".$fetch['PR_NAME'];?></center></td>
                    <td><center><a class="text-green"><?php echo $REG_DATE165; ?></a></center></td>
                    <td><center><a class="text-green"><?php echo $fetch['REG_SAVEUSER']; ?></a></center></td>
                  </tr>
                  <?php
                  $FEE_COST += str_replace( ',', '', $fetch['FEE_COST1']);
                  $REG_BILL1+= str_replace( ',', '', $SUM1);
                  $REG_BILL4 += str_replace( ',', '', $SUM4);
                  $REG_BILL2 += str_replace( ',', '', $SUM2);
                  $REG_BILL3 += str_replace( ',', '', $SUM3);
                  $item++; }
                  ?>
              </table>
              <input type="hidden" name="hdnCount" value="<?php echo $i;?>">
              </form>
              <br>
            </div>
          </div>
        </div>
      </div>

      </div>
    </section>
  </div>
</div>

<?php
include '_footer.php';
include '_form.php';
?>
<div class="control-sidebar-bg"></div>
<?php include '_script_2.php'; ?>
</body>
</html>
