<!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';
?>
</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">
      <!-- ค้นหา -->
      <!-- ตาราง -->
      <script src="bower_components/jquery/dist/jquery.min.js"></script>
      <div class="col-md-12">
        <div class="box box-warning">
          <div class="box-header with-border">
            <h3 class="box-title">รายงานภาค/เขต/สาขา</h3>
          </div>
          <?php
            if (isset($_POST['DMY1'])) {
              $DMY1 = $_POST["DMY1"]; // วันที่
              $DMY2 = $_POST["DMY2"]; // ถึงวันที่

              $calcdate1 = substr($DMY1,0,4); $calcdate2 = substr($DMY1,5,2); $calcdate3 = substr($DMY1,8,2);
              $calcdate = $calcdate3."/".$calcdate2."/".$calcdate1;

              $calcdate12 = substr($DMY2,0,4); $calcdate22 = substr($DMY2,5,2); $calcdate32 = substr($DMY2,8,2);
              $calcdate25 = $calcdate32."/".$calcdate22."/".$calcdate12;

              $MY = $calcdate1."_".$calcdate2;

              $day1 = $_POST["DMY1"];
              $day2 = $_POST["DMY2"];
              $BR_ID = $_POST['BR_ID'];
              $szone_code = $_POST['szone_code'];
              $zstaff_id = $_POST['zstaff_id'];

              if ($BR_ID != "") { $BR_ID_AND = "AND c.BR_ID = '$BR_ID'";
              } else {$BR_ID_AND = ""; }
              if ($szone_code != "") { $szone_code_AND = "AND d.szone_code = '$szone_code'";
              } else { $szone_code_AND = ""; }
              if ($zstaff_id != "") { $zstaff_id_AND = "AND d.zstaff_id = '$zstaff_id'";
              } else { $zstaff_id_AND = ""; }
            }
            else {
              $D = date('d');
              $Y = date('Y')+543;

              $Eday = date('d');
              $year = date('Y');
              $month2 = date('m');
              $Eday1 = date('Y-m-t',strtotime($year."-".$month2."-".'01'));
              $Eday2 = substr($Eday1,8,2);
              $day1 = $year."-".$month2."-01";
              $day2 = $year."-".$month2."-".$Eday;

              $BR_ID = "";
              $szone_code = "";
              $zstaff_id = "";
            }
          ?>
          <!-- target="_blank" info_2.php-->
          <form role="form" action="infofrom_2.php" method="POST" enctype="multipart/form-data" >
            <div class="box-body">
              <div class="row">
                <div class="col-xs-2">
                  <p>วันที่</p>
                  <input type="date" class="form-control" placeholder="" name="DMY1" value="<?php echo $day1; ?>">
                </div>
                <div class="col-xs-2">
                  <p>ถึงวันที่</p>
                  <input type="date" class="form-control" placeholder="" name="DMY2" value="<?php echo $day2; ?>">
                </div>
                <div class="col-xs-2">
                  <p>สาขา</p>
                  <select class="form-control" name="BR_ID" >
                    <?php
                    $sql = "SELECT * FROM EHONGDB.dbo.BRANCH WHERE BR_NAME NOT IN ('รวมทุกสาขา') AND BR_ID = '$BR_ID' ORDER BY BR_ID";
                    $select=$conn->prepare($sql);
                    $select->execute();
                    while($fetch=$select->fetch()) {
                    ?>
                    <option value="<?php echo $fetch['BR_ID'];?>"><?php echo $fetch['BR_ID']." : ".$fetch['BR_NAME'];?></option>
                    <?php } ?>
                    <option value="">-- ดูทั้งหมด --</option>
                    <?php
                    $sql = "SELECT * FROM EHONGDB.dbo.BRANCH WHERE BR_NAME NOT IN ('รวมทุกสาขา') ORDER BY BR_ID";
                    $select=$conn->prepare($sql);
                    $select->execute();
                    while($fetch=$select->fetch()) {
                    ?>
                    <option value="<?php echo $fetch['BR_ID'];?>"><?php echo $fetch['BR_ID']." : ".$fetch['BR_NAME'];?></option>
                    <?php } ?>
                  </select>
                </div>
                <div class="col-xs-2">
                  <p>เขต</p>
                  <select class="form-control" name="szone_code" placeholder="ผจก.เขต">
                    <?php
                    $sql = "SELECT * FROM EHONGDB.dbo.branch_salezone WHERE szone_code = '$szone_code' ORDER BY szone_code";
                    $select=$conn->prepare($sql);
                    $select->execute();
                    while($fetch=$select->fetch()) {
                    ?>
                    <option value="<?php echo $fetch['szone_code'];?>"><?php echo $fetch['szone_code']." : ".$fetch['staff_nick'];?></option>
                    <?php } ?>
                    <option value="">-- รวมทุกเขต --</option>
                    <?php
                    $sql = "SELECT * FROM EHONGDB.dbo.branch_salezone ORDER BY szone_code";
                    $select=$conn->prepare($sql);
                    $select->execute();
                    while($fetch=$select->fetch()) {
                    ?>
                    <option value="<?php echo $fetch['szone_code'];?>"><?php echo $fetch['szone_code']." : ".$fetch['staff_nick'];?></option>
                    <?php } ?>
                  </select>
                </div>
                <div class="col-xs-2">
                  <p>ภาค</p>
                  <select class="form-control" name="zstaff_id" placeholder="ผจก.เขต">
                    <?php
                    $sql = "SELECT DISTINCT zstaff_id,zstaff_name FROM EHONGDB.dbo.branch_salezone WHERE zstaff_id = '$zstaff_id' ORDER BY zstaff_id,zstaff_name";
                    $select=$conn->prepare($sql);
                    $select->execute();
                    while($fetch=$select->fetch()) {
                    ?>
                    <option value="<?php echo $fetch['zstaff_id'];?>"><?php echo $fetch['zstaff_id']." : ".$fetch['zstaff_name'];?></option>
                    <?php } ?>
                    <option value="">-- รวมทุกภาค --</option>
                    <?php
                    $sql = "SELECT DISTINCT zstaff_id,zstaff_name FROM EHONGDB.dbo.branch_salezone ORDER BY zstaff_id,zstaff_name";
                    $select=$conn->prepare($sql);
                    $select->execute();
                    while($fetch=$select->fetch()) {
                    ?>
                    <option value="<?php echo $fetch['zstaff_id'];?>"><?php echo $fetch['zstaff_id']." : ".$fetch['zstaff_name'];?></option>
                    <?php } ?>
                  </select>
                </div>
              </div>
            </div>

            <div class="box-footer">
              <input type="submit" class="btn btn-success" name="search" value="ดูรายงานรวม">
              <input type="submit" class="btn btn-warning" name="search_1" value="ดูรายงานสาขา">
              <input type="submit" class="btn btn-warning" name="search_2" value="ดูรายงานเขต">
              <input type="submit" class="btn btn-warning" name="search_3" value="ดูรายงานภาค">
            </div>
          </form>

        </div>
      </div>

      <?php
        if (isset($_POST['search_1']) || isset($_POST['search_2']) || isset($_POST['search_3'])) {
          if (isset($_POST['search_1']) != "") {
            $sll = "a.BR_ID,c.BR_NAME";
            $namesll = "สาขา";
          }
          if (isset($_POST['search_2']) != "") {
            $sll = "d.szone_code,d.szone_name";
            $namesll = "เขต";
          }
          if (isset($_POST['search_3']) != "") {
            $sll = "d.zstaff_id,d.zstaff_name";
            $namesll = "ภาค";
          }
      ?>
      <div class="col-md-12">
        <div class="box box-success">
          <div class="box-header">
            <h3 class="box-title"><p class="text-success">รายงานรวมช่องทางรู้จักร้าน - <?php echo $namesll; ?></p></h3>
            <p class="text-red">จากวันที่ <?php echo $calcdate; ?> ถึงวันที่ <?php echo $calcdate25; ?> </p>
          </div>
          <div class="box-body">
            <div class="form-group">
              <!-- example31 -->
              <div style="overflow-x:auto;">
                <table id="tabla2xx" class="table table-striped table-bordered" style="width:100%;">
                  <thead style="white-space: nowrap">
                    <tr >
                      <th rowspan="2" style="background-color:black; color:#fff; text-align:center;">NO.</th>
                      <th rowspan="2" style="background-color:black; color:#fff; text-align:center;"><?php echo $namesll; ?></th>
                      <th rowspan="2" style="background-color:yellow; color:#000; text-align:center;">รวม</th>
                      <th colspan="23" style="background-color:black; color:#fff; text-align:center;">ช่องทางที่รู้จักร้าน</th>
                    </tr>
                    <tr>
                      <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;">Facebook</th>
                      <th style="background-color:black; color:#fff; text-align:center;">Instragram</th>

                      <th style="background-color:black; color:#fff; text-align:center;">Line</th>
                      <th style="background-color:black; color:#fff; text-align:center;">Google(Website)</th>
                      <th style="background-color:black; color:#fff; text-align:center;">Tiktok</th>
                      <th style="background-color:black; color:#fff; text-align:center;">Twitter</th>
                      <th style="background-color:black; color:#fff; text-align:center;">Youtube</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
                    include '_connect.php';
                    $sql_1 = "SELECT
                      $sll
                      ,sum(t1.infosum) as infosum
                      ,sum(t1.info_1) as info_1
                      ,sum(t1.info_2) as info_2
                      ,sum(t1.info_3) as info_3
                      ,sum(t1.info_4) as info_4
                      ,sum(t1.info_5) as info_5
                      ,sum(t1.info_6) as info_6
                      ,sum(t1.info_7) as info_7
                      ,sum(t1.info_8) as info_8
                      ,sum(t1.info_9) as info_9
                      ,sum(t1.info_10) as info_10
                      ,sum(t1.info_11) as info_11
                      ,sum(t1.info_12) as info_12
                      ,sum(t1.info_13) as info_13
                      ,sum(t1.info_14) as info_14
                      ,sum(t1.info_15) as info_15
                      ,sum(t1.info_16) as info_16
                      ,sum(t1.info_17) as info_17
                      ,sum(t1.info_18) as info_18
                      ,sum(t1.info_19) as info_19
                      ,sum(t1.info_20) as info_20
                      ,sum(t1.info_21) as info_21
                      ,sum(t1.info_22) as info_22
                      ,sum(t1.info_23) as info_23
                    FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] a
                    left outer join(SELECT CT_NO,BR_ID,infofrom,infofromchk
                      ,count(infofromchk) as infosum
                      ,sum(case when (infofromchk is null) then 1 else 0 end) as info_1
                      ,sum(case when (infofromchk='ลูกค้าเก่า') then 1 else 0 end) as info_2
                      ,sum(case when (infofromchk='พนักงานสาขา') then 1 else 0 end) as info_3
                      ,sum(case when (infofromchk='ครอบครัว/ญาต') then 1 else 0 end) as info_4
                      ,sum(case when (infofromchk='เพื่อน/เพื่อนบ้าน') then 1 else 0 end) as info_5
                      ,sum(case when (infofromchk='ผู้นำชุมชน') then 1 else 0 end) as info_6
                      ,sum(case when (infofromchk='Facebook') then 1 else 0 end) as info_7
                      ,sum(case when (infofromchk='Instragram') then 1 else 0 end) as info_8
                      ,sum(case when (infofromchk='Line') then 1 else 0 end) as info_9
                      ,sum(case when (infofromchk='Google(Website)') then 1 else 0 end) as info_10
                      ,sum(case when (infofromchk='Tiktok') then 1 else 0 end) as info_11
                      ,sum(case when (infofromchk='Twitter') then 1 else 0 end) as info_12
                      ,sum(case when (infofromchk='Youtube') then 1 else 0 end) as info_13
                      ,sum(case when (infofromchk='หน้าร้านสาขา') then 1 else 0 end) as info_14
                      ,sum(case when (infofromchk='ป้ายโฆษณา') then 1 else 0 end) as info_15
                      ,sum(case when (infofromchk='รถแห่') then 1 else 0 end) as info_16
                      ,sum(case when (infofromchk='ใบปลิว') then 1 else 0 end) as info_17
                      ,sum(case when (infofromchk='ทีวี') then 1 else 0 end) as info_18
                      ,sum(case when (infofromchk='วิทยุ') then 1 else 0 end) as info_19
                      ,sum(case when (infofromchk='นิตยสาร/สิ่งพิมพ์') then 1 else 0 end) as info_20
                      ,sum(case when (infofromchk='บูธกิจกรรม') then 1 else 0 end) as info_21
                      ,sum(case when (infofromchk='ออกจดหมาย') then 1 else 0 end) as info_22
                      ,sum(case when (infofromchk='อื่นๆ') then 1 else 0 end) as info_23
                    FROM [EHONGDB].[dbo].[PRESALE_AUDITSALE_SUMMARY] GROUP BY CT_NO,BR_ID,infofrom,infofromchk) t1 on (a.CT_NO=t1.CT_NO)
                    left outer join(SELECT * FROM [EHONGDB].[dbo].[BRANCH]) c on (a.BR_ID=c.BR_ID)
                    left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) d on (c.SZONE_CODE=d.szone_code)
                    WHERE a.BILL_DATE BETWEEN '$DMY1 00:00:00' AND '$DMY2 00:00:00'
                     and (a.status<>'X') and (a.product_id like 'มซ%') and (a.ct_no like '0%' or a.ct_no like '2%')
                     $BR_ID_AND $szone_code_AND $zstaff_id_AND
                    GROUP BY $sll
                    ORDER BY $sll";
                    // echo $sql_1;
                    $select=$conn->prepare($sql_1);
                    $select->execute();
                    $item = 1;
                    $sum = 0;
                    $infosum_0 = 0;
                    $infosum_1 = 0;
                    $infosum_2 = 0;
                    $infosum_3 = 0;
                    $infosum_4 = 0;
                    $infosum_5 = 0;
                    $infosum_6 = 0;
                    $infosum_7 = 0;
                    $infosum_8 = 0;
                    $infosum_9 = 0;
                    $infosum_10 = 0;
                    $infosum_11 = 0;
                    $infosum_12 = 0;
                    $infosum_13 = 0;
                    $infosum_14 = 0;
                    $infosum_15 = 0;
                    $infosum_16 = 0;
                    $infosum_17 = 0;
                    $infosum_18 = 0;
                    $infosum_19 = 0;
                    $infosum_20 = 0;
                    $infosum_21 = 0;
                    $infosum_22 = 0;
                    $infosum_23 = 0;
                    while($fetch=$select->fetch()){
                      $infosum = $fetch['infosum']+$fetch['info_1'];
                    ?>
                    <tr>
                      <td style="background-color:; color:; text-align:;"><?php echo number_format($item); ?>.</td>

                      <?php
                      if (isset($_POST['search_1']) != "") {
                      ?>
                        <td style="background-color:; color:; text-align:;"><?php echo $fetch['BR_ID']." : ".$fetch['BR_NAME']; ?></td>
                      <?php
                      }
                      if (isset($_POST['search_2']) != "") {
                      ?>
                        <td style="background-color:; color:; text-align:;"><?php echo $fetch['szone_code']." : ".$fetch['szone_name']; ?></td>
                      <?php
                      }
                      if (isset($_POST['search_3']) != "") {
                      ?>
                        <td style="background-color:; color:; text-align:;"><?php echo $fetch['zstaff_id']." : ".$fetch['zstaff_name']; ?></td>
                      <?php
                      }
                      ?>

                      <th style="background-color:yellow; color:#000; text-align:center;"><?php echo $infosum; ?></th>

                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_1']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_2']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_3']; ?></th>

                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_4']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_5']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_6']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_7']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_8']; ?></th>

                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_9']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_10']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_11']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_12']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_13']; ?></th>

                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_14']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_15']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_16']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_17']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_18']; ?></th>

                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_19']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_20']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_21']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_22']; ?></th>
                      <th style="background-color:; color:; text-align:center;"><?php echo $fetch['info_23']; ?></th>
                    </tr>
                    <?php
                      $item++;
                      $infosum_0 += $infosum;
                      $infosum_1 += $fetch['info_1'];
                      $infosum_2 += $fetch['info_2'];
                      $infosum_3 += $fetch['info_3'];
                      $infosum_4 += $fetch['info_4'];
                      $infosum_5 += $fetch['info_5'];
                      $infosum_6 += $fetch['info_6'];
                      $infosum_7 += $fetch['info_7'];
                      $infosum_8 += $fetch['info_8'];
                      $infosum_9 += $fetch['info_9'];
                      $infosum_10 += $fetch['info_10'];
                      $infosum_11 += $fetch['info_11'];
                      $infosum_12 += $fetch['info_12'];
                      $infosum_13 += $fetch['info_13'];
                      $infosum_14 += $fetch['info_14'];
                      $infosum_15 += $fetch['info_15'];
                      $infosum_16 += $fetch['info_16'];
                      $infosum_17 += $fetch['info_17'];
                      $infosum_18 += $fetch['info_18'];
                      $infosum_19 += $fetch['info_19'];
                      $infosum_20 += $fetch['info_20'];
                      $infosum_21 += $fetch['info_21'];
                      $infosum_22 += $fetch['info_22'];
                      $infosum_23 += $fetch['info_23'];
                      }
                    ?>
                  </tbody>
                  <tfoot>
                    <th colspan="2" style="background-color:black; color:#fff; text-align:center;">รวม</th>
                    <th style="background-color:yellow; color:#000; text-align:center;"><?php echo number_format($infosum_0); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_1); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_2); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_3); ?></th>

                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_4); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_5); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_6); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_7); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_8); ?></th>

                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_9); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_10); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_11); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_12); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_13); ?></th>

                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_14); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_15); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_16); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_17); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_18); ?></th>

                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_19); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_20); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_21); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_22); ?></th>
                    <th style="background-color:black; color:#fff; text-align:center;"><?php echo number_format($infosum_23); ?></th>
                  </tfoot>
              </table>
            </div>
            </div>
          </div>
        </div>
      </div>
      <?php } ?>

      <?php
        if (isset($_POST['search'])) {
      ?>
      <div class="col-md-12">
        <div class="box box-success">
          <div class="box-header">
            <h3 class="box-title"><p class="text-success">รายงานรวมช่องทางรู้จักร้าน</p></h3>
            <p class="text-red">จากวันที่ <?php echo $calcdate; ?> ถึงวันที่ <?php echo $calcdate25; ?> </p>
          </div>
          <div class="box-body">
            <div class="form-group">
              <!-- example31 -->
              <div style="overflow-x:auto;">
                <table id="tabla2xx" class="table table-striped table-bordered" style="width:100%;">
                  <thead style="white-space: nowrap">
                    <tr >
                      <th rowspan="2" style="background-color:black; color:#fff; text-align:center;">NO.</th>
                      <th rowspan="2" style="background-color:black; color:#fff; text-align:center;">สาขา</th>
                      <th rowspan="2" style="background-color:black; color:#fff; text-align:center;">เขต</th>
                      <th rowspan="2" style="background-color:black; color:#fff; text-align:center;">ภาค</th>
                      <th rowspan="2" style="background-color:yellow; color:#000; text-align:center;">สัญญา</th>
                      <th rowspan="2" style="background-color:yellow; color:#000; text-align:center;">รหัสลูกค้า</th>
                      <th rowspan="2" style="background-color:yellow; color:#000; text-align:center;">ชื่อ-สกุล</th>
                      <th colspan="2" style="background-color:black; color:#fff; text-align:center;">ช่องทางที่รู้จักร้าน</th>
                    </tr>
                    <tr>
                      <td style="background-color:black; color:#fff; text-align:center;">สาขา</td>
                      <td style="background-color:black; color:#fff; text-align:center;">อนุมัติ</td>
                    </tr>
                  </thead>
                  <tbody style="white-space: nowrap">
                    <?php
                    include '_connect.php';
                    $sql = "SELECT
                      c.BR_ID,
                      c.BR_NAME,
                      d.szone_code,
                      d.szone_name,
                      d.zstaff_id,
                      d.zstaff_name,
                      a.CT_NO,
                      a.PS_ID,
                      a.PS_NAME,
                      infofrom,
                      infofromchk
                    FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] a
                    left outer join(SELECT * FROM [EHONGDB].[dbo].[PRESALE_AUDITSALE_SUMMARY]) b on (a.CT_NO=b.CT_NO)
                    left outer join(SELECT * FROM [EHONGDB].[dbo].[BRANCH]) c on (a.BR_ID=c.BR_ID)
                    left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) d on (c.SZONE_CODE=d.szone_code)
                    WHERE a.BILL_DATE BETWEEN '$DMY1 00:00:00' AND '$DMY2 00:00:00'
                    and (a.status<>'X') and (a.product_id like 'มซ%') and (a.ct_no like '0%' or a.ct_no like '2%')
                    $BR_ID_AND $szone_code_AND $zstaff_id_AND";
                    // echo $sql;
                    // echo $str;
                    $select=$conn->prepare($sql);
                    $select->execute();
                    $item = 1;
                    $sum = 0;
                    while($fetch=$select->fetch()){
                    ?>
                    <tr>
                      <td style="background-color:; color:; text-align:;"><?php echo number_format($item); ?>.</td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['BR_ID']." : ".$fetch['BR_NAME']; ?></td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['szone_code']." : ".$fetch['szone_name']; ?></td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['zstaff_id']." : ".$fetch['zstaff_name']; ?></th>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['CT_NO']; ?></td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['PS_ID']; ?></td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['PS_NAME']; ?></td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['infofrom']; ?></td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['infofromchk']; ?></td>
                    </tr>
                    <?php
                      $item++;
                      }
                    ?>
                  </tbody>
              </table>
            </div>
            </div>
          </div>
        </div>
      </div>
      <?php } ?>


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

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