<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>EHONGMD | รายงาน LINE ID</title>
  <?php
  include '_plugin.php';
  include '_connect.php';
  ?>
  <script src="bower_components/jquery/dist/jquery.min.js"></script>
  <script language="JavaScript">
	function ClickCheckAll(vol)
	{

		var i=1;
		for(i=1;i<=document.frmMain.hdnCount.value;i++)
		{
			if(vol.checked == true)
			{
				eval("document.frmMain.chkDel"+i+".checked=true");
			}
			else
			{
				eval("document.frmMain.chkDel"+i+".checked=false");
			}
		}
	}

	function onDelete()
	{
		if(confirm('ต้องการออกใบส่งเงินหรือไม่?')==true)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
  </script>
</head>
<body class="hold-transition skin-red sidebar-mini">

  <style media="screen">
  div.ex1 {
    /* height: 550px;
    overflow-y: scroll; */
  }
  th, td, tbody { white-space: nowrap; }
  </style>

<div class="wrapper">
<?php
include '_header.php';
include '_menu.php';
?>
  <div class="content-wrapper">
    <section class="content">
      <div class="row">
      <!-- ค้นหา -->
      <?php
      include '_connect.php';
      include 'line_search_1.php'

      ?>
      <?php
      $DMY1 = $_GET["DMY1"]; // วันที่
      $DMY2 = $_GET["DMY2"]; // ถึงวันที่
      $LINEX = $_GET['LINEX']; // sql
      $BR_ID = $_GET["BR_ID"]; // สาขา เขต
      // $pr_codex = $_POST["pr_code"]; // จังหวัด
      // $infofromx = $_POST["infofrom"]; // ช่องทางการรับรู้
      $BR_IDSX = $_GET['BR_IDSX'];
      $SZXONE = $_GET['SZXONE'];
      $ZSFID = $_GET['ZSFID'];

      // echo $LINEX;

      $_month_name = array("01"=>"มกราคม",  "02"=>"กุมภาพันธ์",  "03"=>"มีนาคม",
         "04"=>"เมษายน",  "05"=>"พฤษภาคม",  "06"=>"มิถุนายน",
         "07"=>"กรกฎาคม",  "08"=>"สิงหาคม",  "09"=>"กันยายน",
         "10"=>"ตุลาคม", "11"=>"พฤศจิกายน",  "12"=>"ธันวาคม");
      $m = date('m');
      //
      $calcdate1 = substr($DMY1,0,4); $calcdate2 = substr($DMY1,5,2); $calcdate3 = substr($DMY1,8,2);
      $calcdate = $calcdate3."/".$calcdate2."/".$calcdate1;
      $MY = $calcdate1."_".$calcdate2;
      // echo $MY;
      //
      $calcdate12 = substr($DMY2,0,4); $calcdate22 = substr($DMY2,5,2); $calcdate32 = substr($DMY2,8,2);
      $calcdate25 = $calcdate32."/".$calcdate22."/".$calcdate12;

      // if ($_GET["BR_ID"] != '') { $BR_ID = "$_GET[BR_ID]"; }
      // else { $BR_ID = ''; }
      // if ($_GET["szone_code"] != '') { $szone_code = "$_GET[szone_code]"; }
      // else { $szone_code = ''; }

      // if ($_GET["zone_code"] != '') { $zone_code = "AND c.szone_code = $zone_codex"; }
      // else { $zone_code = ''; }
      //
      // if ($_POST["pr_code"] != '') { $pr_code = "AND b.PR_CODE = $pr_codex"; }
      // else { $pr_code = ''; }
      //
      // if ($_POST["infofrom"] != '') { $infofrom = "AND a.infofrom = '$infofromx'"; }
      // else { $infofrom = ''; }
      // echo "$infofrom";
      if ($BR_IDSX != "") {
        $CBR_ID = "AND a.BR_ID = '$BR_IDSX'";
      }
      else {
        $CBR_ID = "";
      }
      if ($SZXONE != "") {
        $CSZXONE = "AND b.SZONE_CODE = '$SZXONE'";
      }
      else {
        $CSZXONE = "";
      }
      if ($ZSFID != "") {
        $CZSFID = "AND b.zstaff_id = '$ZSFID'";
      }
      else {
        $CZSFID = "";
      }
      // จัจักรยานยนต์ เขต
      if ($LINEX == '1') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] a
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) b on (a.ps_id=b.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[BRANCH]) c on (c.BR_ID=a.br_id)
        WHERE a.BR_ID = '$BR_ID' AND BILL_DATE BETWEEN '$DMY1' AND '$DMY2'
        and (status<>'X') and (product_id like 'มซ%') and (ct_no like '0%' or ct_no like '2%') AND lineid IS NOT NULL";
      }
      if ($LINEX == '2') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] a
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) b on (a.ps_id=b.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[BRANCH]) c on (c.BR_ID=a.br_id)
        WHERE a.BR_ID = '$BR_ID' AND BILL_DATE BETWEEN '$DMY1' AND '$DMY2'
        and (status<>'X') and (product_id like 'มซ%') and (ct_no like '0%' or ct_no like '2%') AND lineid IS NULL";
      }
      if ($LINEX == '3') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] a
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) b on (a.ps_id=b.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[BRANCH]) c on (c.BR_ID=a.br_id)
        WHERE a.BR_ID = '$BR_ID' AND BILL_DATE BETWEEN '$DMY1' AND '$DMY2'
        and (status<>'X') and (product_id like 'มซ%') and (ct_no like '0%' or ct_no like '2%')";
      }
      if ($LINEX == '5') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') and (b1.product_id like 'มซ%') and (b1.ct_no like '0%' or b1.ct_no like '2%') AND lineid IS NOT NULL $CBR_ID $CSZXONE $CZSFID";
      }
      if ($LINEX == '4') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') and (b1.product_id like 'มซ%') and (b1.ct_no like '0%' or b1.ct_no like '2%') AND lineid IS NULL $CBR_ID $CSZXONE $CZSFID";
      }
      if ($LINEX == '6') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') and (b1.product_id like 'มซ%') and (b1.ct_no like '0%' or b1.ct_no like '2%') $CBR_ID $CSZXONE $CZSFID";
      }
      // เครื่องใช้ไฟฟ้า สาขา
      if ($LINEX == '2a') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X')
        and (product_id not like 'มซ%') and (ct_no like '0%' or ct_no like '1%' or ct_no like '2%') and c.br_id = '$BR_ID'
        AND lineid IS NULL";
      }
      if ($LINEX == '1a') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X')
        and (product_id not like 'มซ%') and (ct_no like '0%' or ct_no like '1%' or ct_no like '2%') and c.br_id = '$BR_ID'
        AND lineid IS NOT NULL";
      }
      if ($LINEX == '3a') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X')
        and (product_id not like 'มซ%') and (ct_no like '0%' or ct_no like '1%' or ct_no like '2%') and c.br_id = '$BR_ID'";
      }
      if ($LINEX == '4a') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X')
        and (product_id not like 'มซ%') and (ct_no like '0%' or ct_no like '1%' or ct_no like '2%') $CBR_ID $CSZXONE $CZSFID
        AND lineid IS NULL";
      }
      if ($LINEX == '5a') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X')
        and (product_id not like 'มซ%') and (ct_no like '0%' or ct_no like '1%' or ct_no like '2%') $CBR_ID $CSZXONE $CZSFID
        AND lineid IS NOT NULL";
      }
      if ($LINEX == '6a') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X') $CBR_ID $CSZXONE $CZSFID
        and (product_id not like 'มซ%') and (ct_no like '0%' or ct_no like '1%' or ct_no like '2%')";
      }
      // จัดไฟแนนซ์ สาขา
      if ($LINEX == '2b') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X') and c.br_id = '$BR_ID'
        and (ct_no like '3%')
        AND lineid IS NULL";
      }
      if ($LINEX == '1b') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X') and c.br_id = '$BR_ID'
        and (ct_no like '3%')
        AND lineid IS NOT NULL";
      }
      if ($LINEX == '3b') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X') and c.br_id = '$BR_ID'
        and (ct_no like '3%')";
      }
      if ($LINEX == '4b') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X')
        and (ct_no like '3%')  $CBR_ID $CSZXONE $CZSFID
        AND lineid IS NULL";
      }
      if ($LINEX == '5b') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X')
        and (ct_no like '3%')  $CBR_ID $CSZXONE $CZSFID
        AND lineid IS NOT NULL";
      }
      if ($LINEX == '6b') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X')  $CBR_ID $CSZXONE $CZSFID
        and (ct_no like '3%')";
      }
      // ฮีโร่ สาขา
      if ($LINEX == '2cSW') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X')
        and (ct_no like '4%' or ct_no like '5%') and c.br_id = '$BR_ID'
        AND lineid IS NULL";
      }
      if ($LINEX == '1cSW') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X') and c.br_id = '$BR_ID'
        and (ct_no like '4%' or ct_no like '5%')
        AND lineid IS NOT NULL";
      }
      if ($LINEX == '3cSW') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X') and c.br_id = '$BR_ID'
        and (ct_no like '4%' or ct_no like '5%')";
      }
      if ($LINEX == '4c') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X')
        and (ct_no like '4%' or ct_no like '5%') $CBR_ID $CSZXONE $CZSFID
        AND lineid IS NULL";
      }
      if ($LINEX == '5c') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X')  $CBR_ID $CSZXONE $CZSFID
        and (ct_no like '4%' or ct_no like '5%')
        AND lineid IS NOT NULL";
      }
      if ($LINEX == '6c') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X')  $CBR_ID $CSZXONE $CZSFID
        and (ct_no like '4%' or ct_no like '5%')";
      }
      // จำนำ สาขา
      if ($LINEX == '2d') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X') and c.br_id = '$BR_ID'
        and (ct_no like '6%')
        AND lineid IS NULL";
      }
      if ($LINEX == '1d') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X') and c.br_id = '$BR_ID'
        and (ct_no like '6%')
        AND lineid IS NOT NULL";
      }
      if ($LINEX == '3d') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X') and c.br_id = '$BR_ID'
        and (ct_no like '6%')";
      }
      if ($LINEX == '4d') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X')
        and (ct_no like '6%')  $CBR_ID $CSZXONE $CZSFID
        AND lineid IS NULL";
      }
      if ($LINEX == '5d') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X')
        and (ct_no like '6%')  $CBR_ID $CSZXONE $CZSFID
        AND lineid IS NOT NULL";
      }
      if ($LINEX == '6d') {
        $sqlxline = "SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."] 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]) c on (c.BR_ID=a.br_id)
        WHERE BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (status<>'X')  $CBR_ID $CSZXONE $CZSFID
        and (ct_no like '6%')";
      }
      // จัักรยานยนต์ เขต
      if ($LINEX == '1x') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') and (b1.product_id like 'มซ%') and (b1.ct_no like '0%' or b1.ct_no like '2%') and a.SZONE_CODE = '$BR_ID' AND lineid IS NOT NULL $CBR_ID";
      }
      if ($LINEX == '2x') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') and (b1.product_id like 'มซ%') and (b1.ct_no like '0%' or b1.ct_no like '2%') and a.SZONE_CODE = '$BR_ID' AND lineid IS NULL $CBR_ID";
      }
      if ($LINEX == '3x') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') and (b1.product_id like 'มซ%') and (b1.ct_no like '0%' or b1.ct_no like '2%') and a.SZONE_CODE = '$BR_ID' $CBR_ID";
      }
      // เครื่เครื่องใช้ไฟฟ้า เขต
      if ($LINEX == '1ax') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X')
        and ( b1.product_id not like 'มซ%') and ( b1.ct_no like '0%' or  b1.ct_no like '1%' or  b1.ct_no like '2%') $CBR_ID
        and a.SZONE_CODE = '$BR_ID' AND lineid IS NOT NULL";
      }
      if ($LINEX == '2ax') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X')
        and ( b1.product_id not like 'มซ%') and ( b1.ct_no like '0%' or  b1.ct_no like '1%' or  b1.ct_no like '2%') $CBR_ID
        and a.SZONE_CODE = '$BR_ID' AND lineid IS NULL";
      }
      if ($LINEX == '3ax') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X')
        and ( b1.product_id not like 'มซ%') and ( b1.ct_no like '0%' or  b1.ct_no like '1%' or  b1.ct_no like '2%') $CBR_ID
        and a.SZONE_CODE = '$BR_ID'";
      }
      // จัจัดไฟแนนซ์ เขต
      if ($LINEX == '1bx') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID
        and (b1.ct_no like '3%')
        and a.SZONE_CODE = '$BR_ID' AND lineid IS NOT NULL";
      }
      if ($LINEX == '2bx') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID
        and (b1.ct_no like '3%')
        and a.SZONE_CODE = '$BR_ID' AND lineid IS NULL";
      }
      if ($LINEX == '3bx') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID
        and (b1.ct_no like '3%')
        and a.SZONE_CODE = '$BR_ID'";
      }
      // ฮีโร่ เขต
      if ($LINEX == '1cx') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID
        and (b1.ct_no like '4%' or b1.ct_no like '5%')
        and a.SZONE_CODE = '$BR_ID' AND lineid IS NOT NULL";
      }
      if ($LINEX == '2cx') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID
        and (b1.ct_no like '4%' or b1.ct_no like '5%')
        and a.SZONE_CODE = '$BR_ID' AND lineid IS NULL";
      }
      if ($LINEX == '3cx') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID
        and (b1.ct_no like '4%' or b1.ct_no like '5%')
        and a.SZONE_CODE = '$BR_ID'";
      }
      // จำนำ เขต
      if ($LINEX == '1dx') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID
        and (b1.ct_no like '6%')
        and a.SZONE_CODE = '$BR_ID' AND lineid IS NOT NULL";
      }
      if ($LINEX == '2dx') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID
        and (b1.ct_no like '6%')
        and a.SZONE_CODE = '$BR_ID' AND lineid IS NULL";
      }
      if ($LINEX == '3dx') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID ,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID
        and (b1.ct_no like '6%')
        and a.SZONE_CODE = '$BR_ID'";
      }
      // จักรยานยนต์ ภาค
      if ($LINEX == '1c') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime
        FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X')
        and (b1.product_id like 'มซ%') and (b1.ct_no like '0%' or b1.ct_no like '2%') $CBR_ID $CSZXONE
        AND b.zstaff_id = '$BR_ID' AND lineid IS NOT NULL";
      }
      if ($LINEX == '2c') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime
        FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X')
        and (b1.product_id like 'มซ%') and (b1.ct_no like '0%' or b1.ct_no like '2%') $CBR_ID $CSZXONE
        AND b.zstaff_id = '$BR_ID' AND lineid IS NULL";
      }
      if ($LINEX == '3c') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime
        FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X')
        and (b1.product_id like 'มซ%') and (b1.ct_no like '0%' or b1.ct_no like '2%') $CBR_ID $CSZXONE
        AND b.zstaff_id = '$BR_ID'";
      }
      // เครื่องใช้ไฟฟ้า ภาค
      if ($LINEX == '1ac') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime
        FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X')
        and (product_id not like 'มซ%') and (ct_no like '0%' or ct_no like '1%' or ct_no like '2%') $CBR_ID $CSZXONE
        AND b.zstaff_id = '$BR_ID' AND lineid IS NOT NULL";
      }
      if ($LINEX == '2ac') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime
        FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X')
        and (product_id not like 'มซ%') and (ct_no like '0%' or ct_no like '1%' or ct_no like '2%') $CBR_ID $CSZXONE
        AND b.zstaff_id = '$BR_ID' AND lineid IS NULL";
      }
      if ($LINEX == '3ac') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime
        FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X')
        and (product_id not like 'มซ%') and (ct_no like '0%' or ct_no like '1%' or ct_no like '2%') $CBR_ID $CSZXONE
        AND b.zstaff_id = '$BR_ID'";
      }
      // จัจัดไฟแนนซ์ ภาค
      if ($LINEX == '1bc') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime
        FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID $CSZXONE
        and (b1.ct_no like '3%')
        AND b.zstaff_id = '$BR_ID' AND lineid IS NOT NULL";
      }
      if ($LINEX == '2bc') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime
        FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID $CSZXONE
        and (b1.ct_no like '3%')
        AND b.zstaff_id = '$BR_ID' AND lineid IS NULL";
      }
      if ($LINEX == '3bc') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime
        FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID $CSZXONE
        and (b1.ct_no like '3%')
        AND b.zstaff_id = '$BR_ID'";
      }
      // ฮีโร่ ภาค
      if ($LINEX == '1cc') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime
        FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID $CSZXONE
        and (b1.ct_no like '4%' or b1.ct_no like '5%')
        AND b.zstaff_id = '$BR_ID' AND lineid IS NOT NULL";
      }
      if ($LINEX == '2cc') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime
        FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID $CSZXONE
        and (b1.ct_no like '4%' or b1.ct_no like '5%')
        AND b.zstaff_id = '$BR_ID' AND lineid IS NULL";
      }
      if ($LINEX == '3cc') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime
        FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID $CSZXONE
        and (b1.ct_no like '4%' or b1.ct_no like '5%')
        AND b.zstaff_id = '$BR_ID'";
      }
      // จำนำ ภาค
      if ($LINEX == '1dc') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime
        FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID $CSZXONE
        and (b1.ct_no like '6%')
        AND b.zstaff_id = '$BR_ID' AND lineid IS NOT NULL";
      }
      if ($LINEX == '2dc') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime
        FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID $CSZXONE
        and (b1.ct_no like '6%')
        AND b.zstaff_id = '$BR_ID' AND lineid IS NULL";
      }
      if ($LINEX == '3dc') {
        $sqlxline = "SELECT a.BR_ID,a.BR_NAME,b1.PS_ID,TI_NAME,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime, b1.ct_date
        FROM [EHONGDB].[dbo].[BRANCH] a
        left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_".$MY."]) b1 on (a.BR_ID=b1.BR_ID)
        left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_MOBILE, lineid, linename, emailaddr, lineaddtime
        FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
        left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
        WHERE b1.BILL_DATE BETWEEN '$DMY1' AND '$DMY2' and (b1.status<>'X') $CBR_ID $CSZXONE
        and (b1.ct_no like '6%')
        AND b.zstaff_id = '$BR_ID'";
      }

      // echo $LINEX;
      // echo "$LINEX";
      // echo $sqlxline;
      ?>
      <!-- ตาราง -->
      <div class="col-md-12">
        <div class="box box-success">
          <div class="box-header">
            <h3 class="box-title"><p class="text-success">รายงาน LINE ID กลุ่มลูกค้า</p></h3>
            <p class="text-warning">วันที่ <?php echo $calcdate; ?> ถึงวันที่ <?php echo $calcdate25; ?></p>
          </div>
          <div class="box-body">
            <div class="form-group">
              <!-- ตาราง -->
              <table id="example" class="table table-striped table-bordered" style="width:100%">
                <thead>
                  <tr>
                    <th style="background-color:black; color:#fff; text-align:center;" width="" rowspan="" ><b>No.</b></th>
                    <th style="background-color:black; color:#fff; text-align:center;" width="" rowspan="" ><b>วันที่</b></th>
                    <th style="background-color:black; color:#fff; text-align:center;" width="" rowspan="" ><b>สาขา</b></th>
                    <th style="background-color:black; color:#fff; text-align:center;" width="" rowspan="" ><b>รหัสลูกค้า</b></th>
                    <th style="background-color:black; color:#fff; text-align:center;" width="" rowspan="" ><b>ชื่อ-นามสกุล</b></th>
                    <th style="background-color:black; color:#fff; text-align:center;" width="" rowspan="" ><b>เบอร์โทรศัทพ์</b></th>
                    <th style="background-color:black; color:#fff; text-align:center;" width="" rowspan="" ><b>LINE ID</b></th>
                    <th style="background-color:black; color:#fff; text-align:center;" width="" rowspan="" ><b>LINE NAME</b></th>
                    <th style="background-color:black; color:#fff; text-align:center;" width="" rowspan="" ><b>E-MAIL</b></th>
                    <th style="background-color:black; color:#fff; text-align:center;" width="" rowspan="" ><b>วันที่ลูกค้าแอดไลน์</b></th>
                  </tr>
                </thead>
                <tbody style="white-space: nowrap">
                  <?php
                  $sql = "$sqlxline";
                  // echo "$sql";
                  $select=$conn->prepare($sql);
                  $select->execute();
                  $item = 1;
                  while($fetch=$select->fetch()) {
                    if ($fetch['ct_date'] != '') {
                      $CHK_BERG1 = substr($fetch['ct_date'],0,4)+543; $CHK_BERG2 = substr($fetch['ct_date'],5,2); $CHK_BERG3 = substr($fetch['ct_date'],8,2);
                      $CHK_BERG = $CHK_BERG3."/".$CHK_BERG2."/".$CHK_BERG1;
                    } else {
                      $CHK_BERG = '';
                    }
                    if ($fetch['lineaddtime'] != '') {
                      $lineaddtime1 = substr($fetch['lineaddtime'],0,4)+543; $lineaddtime2 = substr($fetch['lineaddtime'],5,2); $lineaddtime3 = substr($fetch['lineaddtime'],8,2);
                      $lineaddtime = $lineaddtime3."/".$lineaddtime2."/".$lineaddtime1;
                    } else {
                      $lineaddtime = '';
                    }
                    $linkx = "http://192.168.10.51/xehong-linux/index.php?sid=person&fid=maindata&ps_id=".$fetch['PS_ID']."&vcmd=load#";
                  ?>
                  <tr>
                    <td style="background-color:; color:; text-align:center;"><?= number_format($item)."."?></td>
                    <td style="background-color:; color:;"><?php echo $CHK_BERG; ?> </td>
                    <!-- ." : ".$fetch['BR_NAME'] -->
                    <td style="background-color:; color:;"><?php echo $fetch['BR_ID']." : ".$fetch['BR_NAME']; ?> </td>
                    <td style="background-color:; color:;"> <a href="<?php echo $linkx; ?>" style="color:blue" target="_blank"> <?php echo $fetch['PS_ID']; ?> </a></td>
                    <td style="background-color:; color:;"><?php echo $fetch['TI_NAME'].$fetch['PS_FNAME']." ".$fetch['PS_LNAME']; ?> </td>
                    <td style="background-color:; color:;"><?php echo $fetch['PS_MOBILE']; ?> </td>
                    <td style="background-color:; color:;"><?php echo $fetch['lineid']; ?> </td>
                    <td style="background-color:; color:;"><?php echo $fetch['linename']; ?> </td>
                    <td style="background-color:; color:;"><?php echo $fetch['emailaddr']; ?> </td>
                    <td style="background-color:; color:;"><?php echo $lineaddtime; ?> </td>
                  </tr>
                   <?php $item++; } ?>
                </tbody>
                <!-- <tfoot>
                  <td></td>
                </tfoot> -->
            </table>
            </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'; ?>
<!-- <script>
    var myApp = new function () {
        this.printTable = function () {
            var tab = document.getElementById('tab');
            var win = window.open('', '', 'height=700,width=1200');
            win.document.write(tab.outerHTML);
            win.document.close();
            win.print();
            // tab.style.font = "bold 2px arial,serif";
            // tab.style.backgroundColor = "#FF0000";
            // win.close();
        }
    }
</script> -->
</body>
</html>
<?php include 'scroll.php' ?>


<!-- SELECT a.BR_ID,a.BR_NAME,b1.PS_ID,TI_NAME,PS_FNAME ,PS_LNAME ,PS_PHONE, lineid, linename, emailaddr, b1.ct_date
FROM [EHONGDB].[dbo].[BRANCH] a
left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmain_2020_08]) b1 on (a.BR_ID=b1.BR_ID)
left outer join(SELECT BR_ID ,PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_PHONE, lineid, linename, emailaddr
 FROM [EHONGDB].[dbo].[PERSON]) t1 on (b1.ps_id=t1.PS_ID)
 left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b on (a.SZONE_CODE=b.szone_code)
 WHERE b1.BILL_DATE BETWEEN '2020-08-01' AND '2020-08-31' and (b1.status<>'X') and (b1.product_id like 'มซ%') and (b1.ct_no like '0%' or b1.ct_no like '2%') AND b.zstaff_id = '02' -->
