<?php // error_reporting(0); ?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>EHONGMD | รายงานทะเบียนตรวจสอบ - ใบสั่งทำ</title>
  <script src="bower_components/jquery/dist/jquery.min.js"></script>
  <link rel="shortcut icon" href="dist/img/logo_1.png" />
  <?php
    include '_plugin.php';
    include '_connect.php';
  ?>
</head>
<body class="hold-transition skin-red sidebar-mini">
<div class="wrapper">
<?php
  include '_header.php';
  include '_menu.php';
?>
  <div class="content-wrapper">
    <section class="content">
      <div class="row">
      <!-- ค้นหา -->
      <!-- ตาราง -->
      <?php
      $BR_IDS = $_REQUEST['BR_IDS'];
      $ZSONE_CODES = $_REQUEST['ZSONE_CODES'];
      $ZSTAFFS = $_REQUEST['ZSTAFFS'];
      $ST = $_REQUEST['ST'];
      $MONTH = $_REQUEST['MONTH'];
      $YEAR = $_REQUEST['YEAR'];
      $MY = $YEAR."_".$MONTH;

      if ($ST == "SE" || $ST == "SSE") {
        // ยอดขาย
        $STRS = "";
      }
      if ($ST == "YM" || $ST == "SYM") {
        // รถจดทะเบียน
        $STRS = "AND a.BRAND_NAME NOT IN ('AIMA','MODDAENG','EM','SAGASONIC') AND a.model_name NOT IN ('RK110ZH-S')";
      }
      if ($ST == "NM" || $ST == "SNM") {
        // ไม่ต้องจดทะเบียน - ยังไม่เสร็จ
        $STRS = "AND a.BRAND_NAME IN ('AIMA','MODDAENG','EM','SAGASONIC')
          UNION
          SELECT
            a.ct_no,
            a.BILL_NO,
            a.BILL_DATE,
            a.regbill_no,
            a.BR_ID,
            aa1.BR_NAME,
            b.PS_ID,
            a.PS_NAME,
            a.BRAND_NAME,
            a.model_name,
            a.serial_no,
            a.pserial_no,
            a.product_id,
            a.product_name
          FROM [EHONGDB].[dbo].[hps_ctmain] a
          LEFT OUTER JOIN(SELECT * FROM [EHONGDB].[dbo].[PERSON]) b on (a.ps_id=b.PS_ID)
          LEFT OUTER JOIN(SELECT * FROM [EHONGDB].[dbo].[BRANCH]) aa1 on (aa1.BR_ID=a.BR_ID)
          LEFT OUTER JOIN(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) aa2 on (aa2.SZONE_CODE=aa1.SZONE_CODE)
          WHERE MONTH(a.BILL_DATE) = $MONTH AND YEAR(a.BILL_DATE) = $YEAR
          AND (a.status<>'X') AND (a.product_id like 'มซ%') AND (a.ct_no like '0%' or a.ct_no like '2%')
          AND a.BR_ID LIKE '%$BR_IDS%' AND aa1.szone_code LIKE '%$ZSONE_CODES%' AND aa2.zstaff_id LIKE '%$ZSTAFFS%'
          AND a.model_name = 'RK110ZH-S'";
      }
      if ($ST == "YRG" || $ST == "SYRG") {
        // รถจดทะเบียน มีใบเสร็จ
        $STRS = "AND a.BRAND_NAME NOT IN ('AIMA','MODDAENG','EM','SAGASONIC') AND a.model_name NOT IN ('RK110ZH-S') AND a.regbill_no IS NOT NULL";
      }
      if ($ST == "NRG" || $ST == "SNRG") {
        // รถจดทะเบียน ยังไม่มีใบเสร็จ
        $STRS = "AND a.BRAND_NAME NOT IN ('AIMA','MODDAENG','EM','SAGASONIC') AND a.model_name NOT IN ('RK110ZH-S') AND a.regbill_no IS NULL";
      }
      // echo $STRS;
      ?>
      <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">
              <!-- example31 -->
              <div style="overflow-x:auto;">
                <table id="example" class="table table-striped table-bordered" style="width:100%;">
                  <thead style="white-space: nowrap">
                    <tr >
                      <th style="background-color:black; color:#fff; text-align:center;">NO.</th>
                      <th style="background-color:black; color:#fff; text-align:center;">วันที่ขาย</th>
                      <th style="background-color:black; color:#fff; text-align:center;">เลขสัญญา</th>
                      <th style="background-color:black; color:#fff; text-align:center;">เลขบิลขาย</th>
                      <th style="background-color:black; color:#fff; text-align:center;">เลขบิลใบสร็จ</th>
                      <th style="background-color:black; color:#fff; text-align:center;">สาขที่ทำสัญญา</th>
                      <th style="background-color:black; color:#fff; text-align:center;">รหัสลูกค้า</th>
                      <th style="background-color:black; color:#fff; text-align:center;">ชื่อลูกค้า</th>
                      <th style="background-color:black; color:#fff; text-align:center;">รหัสสินค้า</th>
                      <th style="background-color:black; color:#fff; text-align:center;">ยี่ห้อ</th>
                      <th style="background-color:black; color:#fff; text-align:center;">รุ่น</th>
                      <th style="background-color:black; color:#fff; text-align:center;">ชื่อรุ่น</th>
                      <th style="background-color:black; color:#fff; text-align:center;">เลขตัวเครื่อง</th>
                      <th style="background-color:black; color:#fff; text-align:center;">เลขตัวถัง</th>
                    </tr>
                  </thead>
                  <tbody style="white-space: nowrap">
                    <?php
                      $sql = "SELECT
                        a.ct_no,
                        a.BILL_NO,
                        a.BILL_DATE,
                        a.regbill_no,
                        a.BR_ID,
                        aa1.BR_NAME,
                        b.PS_ID,
                        a.PS_NAME,
                        a.BRAND_NAME,
                        a.model_name,
                        a.serial_no,
                        a.pserial_no,
                        a.product_id,
                        a.product_name
                      FROM [EHONGDB].[dbo].[hps_ctmain] a
                      LEFT OUTER JOIN(SELECT * FROM [EHONGDB].[dbo].[PERSON]) b on (a.ps_id=b.PS_ID)
                      LEFT OUTER JOIN(SELECT * FROM [EHONGDB].[dbo].[BRANCH]) aa1 on (aa1.BR_ID=a.BR_ID)
                      LEFT OUTER JOIN(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) aa2 on (aa2.SZONE_CODE=aa1.SZONE_CODE)
                      WHERE MONTH(a.BILL_DATE) = $MONTH AND YEAR(a.BILL_DATE) = $YEAR
                      AND (a.status<>'X') AND (a.product_id like 'มซ%') AND (a.ct_no like '0%' or a.ct_no like '2%')
                      AND a.BR_ID LIKE '%$BR_IDS%' AND aa1.szone_code LIKE '%$ZSONE_CODES%' AND aa2.zstaff_id LIKE '%$ZSTAFFS%'
                      $STRS
                      ORDER BY a.BILL_DATE";
                      // echo $sql;
                      $select=$conn->prepare($sql);
                      $select->execute();
                      $no = 0;
                      while($fetch=$select->fetch()){
                        $no++;
                    ?>
                    <tr>
                      <td style="background-color:; color:; text-align:;"><?php echo number_format($no); ?>.</td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['BILL_DATE']; ?></td>
                      <td style="background-color:; color:; text-align:;"><a href="http://192.168.10.53/xehong-linux/index.php?sid=hps&fid=view&presaleno=&mo_no=&ct_no=<?php echo $fetch['ct_no']; ?>#" target="_blank"><?php echo $fetch['ct_no']; ?></a></td>
                      <td style="background-color:; color:; text-align:;"><a href="http://192.168.10.53/xehong-linux/index.php?sid=sale&fid=savesale&sale_no=<?php echo $fetch['BILL_NO']; ?>&vcmd=load&flgmenu=0#" target="_blank"><?php echo $fetch['BILL_NO']; ?></a></td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['regbill_no']; ?></td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['BR_ID']." : ".$fetch['BR_NAME']; ?></td>
                      <td style="background-color:; color:; text-align:;"><a href="http://192.168.10.53/xehong-linux/index.php?sid=person&fid=maindata&ps_id=<?php echo $fetch['PS_ID']; ?>&vcmd=load&typemenu=2#" target="_blank"><?php echo $fetch['PS_ID']; ?></a></td>
                      <td style="background-color:; color:; text-align:;"><a href="http://192.168.10.53/xehong-linux/index.php?sid=person&fid=maindata&ps_id=<?php echo $fetch['PS_ID']; ?>&vcmd=load&typemenu=2#" target="_blank"><?php echo $fetch['PS_NAME']; ?></a></td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['product_id']; ?></td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['BRAND_NAME']; ?></td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['model_name']; ?></td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['product_name']; ?></td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['serial_no']; ?></td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['pserial_no']; ?></td>
                    </tr>
                    <?php
                      }
                    ?>
                  </tbody>
              </table>
            </div>
            </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';
  include 'scroll0.php';
?>
</body>
</html>
