<?php
include '_connect.php';
$code = $_POST['code'];
$code_2 = $_POST['code_2'];
$_month_name = array("01"=>"มกราคม",  "02"=>"กุมภาพันธ์",  "03"=>"มีนาคม",
   "04"=>"เมษายน",  "05"=>"พฤษภาคม",  "06"=>"มิถุนายน",
   "07"=>"กรกฎาคม",  "08"=>"สิงหาคม",  "09"=>"กันยายน",
   "10"=>"ตุลาคม", "11"=>"พฤศจิกายน",  "12"=>"ธันวาคม");
$m = date('m');
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>รายงานการบันทึกใบเสร็จ</title>
    <style type="text/css" media="print">
      body {
        font-size: 1pt;
      }
      table {
        border-collapse: collapse;
        font-size: 1%;
      }
      table, th, td {
        border: 1px solid black;
      }
      @media print {
        @page {
          /* margin: 0; */
          margin: 1cm;
          size: landscape;
        }
      }
      /* @page {
        margin: 1cm;
        size: landscape;
      } */
    </style>
    <style media="screen">
    body {
      font-size: 5pt;
    }
    table {
      border-collapse: collapse;
    }
    table, th, td {
      border: 1px solid black;
    }
    </style>
  </head>
  <body>
    <!-- <p><?php echo $code_2; ?></p><br> -->
    <table id="tab" class="" width="100%" border="1">
      <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>หัวหน้าทะเบียน</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> -->
      </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,
          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,
          g.xcorp_no
          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)
          LEFT OUTER JOIN (SELECT * FROM [EHONGDB].[dbo].[REG_BILLDATA]) g ON (a.BILL_NO=g.BILL_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 c.REG_NO ASC, b.PR_CODE ASC";
          $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."/".$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."/".$REG_DATE2."/".$REG_DATE1;
            } else {
              $REG_DATE165 = '';
            }

            if ($fetch['REG_BREG'] == '') {
              $FEE_COST_10 = '0.00';
            } else {
              $FEE_COST_10 = $fetch['REG_BREG'];
            }

            if ($fetch['REG_BILLBREG'] == '') {
              $REG_BILL1_10 = '0.00';
            } else {
              $REG_BILL1_10 = $fetch['REG_BILLBREG'];
            }

            if ($fetch['REG_BILLBREGTRANSPORT'] == '') {
              $REG_BILL4_10 = '0.00';
            } else {
              $REG_BILL4_10 = $fetch['REG_BILLBREGTRANSPORT'];
            }

            if ($fetch['REG_BILLUSER1'] == '') {
              $REG_BILL2_10 = '0.00';
            } else {
              $REG_BILL2_10 = $fetch['REG_BILLUSER1'];
            }

            if ($fetch['REG_BILLUSER2'] == '') {
              $REG_BILL3_10 = '0.00';
            } else {
              $REG_BILL3_10 = $fetch['REG_BILLUSER2'];
            }
          ?>
          <td style="text-align: center;"><?=$item."."?></td>
          <td><center><?php echo $CHK_BERG; ?></center></td>
          <td><center><?php echo $fetch['REG_CROPNO']." ".$fetch['REG_NO'];?></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><p class="text-blue"><?php echo $REG_BILL1_10; ?></p></center></td>
          <?php
          $sum23R = $REG_BILL2_10+$REG_BILL3_10;
          ?>

          <td><center><p class="text-blue"><?php echo $FEE_COST_10; ?></p></center></td>
          <td><center><p class="text-blue"><?php echo $REG_BILL4_10; ?></p></center></td>
          <!-- <td><center><p class="text-blue"><?php echo number_format($sum23R,2); ?></p></center></td> -->
          <td><center><?php echo $fetch['tsp_registrar1']; ?></center></td>
          <td><center><p class="text-blue"><?php echo $REG_BILL2_10; ?></center></p></td>
          <td><center><?php echo $fetch['tsp_registrar']; ?></center></td>
          <td><center><p class="text-blue"><?php echo $REG_BILL3_10; ?></p></center></td>
          <td><center><?php echo $fetch['PR_NAME'];?></center></td>
          <!-- <td><center></center></td>
          <td><center></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( ',', '', $FEE_COST_10);
          $REG_BILL1+= str_replace( ',', '', $REG_BILL1_10);
          $REG_BILL4 += str_replace( ',', '', $REG_BILL4_10);
          $REG_BILL2 += str_replace( ',', '', $REG_BILL2_10);
          $REG_BILL3 += str_replace( ',', '', $REG_BILL3_10);
          $item++; }
        ?>
        <tr>
          <td><center><b>รวม</center></td>
          <!-- <td><center></center></td> -->
          <!-- <td><center></center></td> -->
          <!-- <td><center></center></td> -->
          <!-- <td><center></center></td> -->
          <td><center></center></td>
          <td><center></center></td>
          <td><center></center></td>
          <td><center></center></td>
          <td><center></center></td>
          <td><center></center></td>
          <td><center><b><?php echo number_format($REG_BILL1,2); ?></center></td>
          <td><center><b><?php echo number_format($FEE_COST,2); ?></center></td>
          <td><center><b><?php echo number_format($REG_BILL4,2); ?></center></td>
          <td><center></center></td>
          <td><center><b><?php echo number_format($REG_BILL2,2); ?></center></td>
          <td><center></td>
          <td><center><b><?php echo number_format($REG_BILL3,2); ?></center></center></td>
          <td><center></center></td>
          <td><center></center></td>
          <!-- <td><center></center></td>
          <td><center></center></td>
          <td><center></center></td> -->
        </tr>
    </table>
  </body>
</html>
<script type="text/javascript">
  window.print();
</script>
