<?php
  $date_xo = date('m');
  // echo $calcdate1;
  $calcdate1_xxxx1 = date('Y')-1;
  if ($date_xo == "01") {
    // $count = "COUNT(DISTINCT a1.ps_id) as ps_id_1";
    // $join = "full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_01]) a1 on (b.ps_id = a1.ps_id)";
    // // $rxs = $row['ps_id_1'];
    $count = "COUNT(DISTINCT a1.ps_id) as ps_id_1,
      COUNT(DISTINCT a2.ps_id) as ps_id_2,
      COUNT(DISTINCT a3.ps_id) as ps_id_3,
      COUNT(DISTINCT a4.ps_id) as ps_id_4,
      COUNT(DISTINCT a5.ps_id) as ps_id_5,
      COUNT(DISTINCT a6.ps_id) as ps_id_6,
      COUNT(DISTINCT a7.ps_id) as ps_id_7,
      COUNT(DISTINCT a8.ps_id) as ps_id_8,
      COUNT(DISTINCT a9.ps_id) as ps_id_9,
      COUNT(DISTINCT a10.ps_id) as ps_id_10,
      COUNT(DISTINCT a11.ps_id) as ps_id_11,
      COUNT(DISTINCT a12.ps_id) as sum_ps_id";
    $join = "full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1_xxxx1."_01]) a1 on (b.ps_id = a1.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1_xxxx1."_02]) a2 on (b.ps_id = a2.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1_xxxx1."_03]) a3 on (b.ps_id = a3.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1_xxxx1."_04]) a4 on (b.ps_id = a4.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1_xxxx1."_05]) a5 on (b.ps_id = a5.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1_xxxx1."_06]) a6 on (b.ps_id = a6.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1_xxxx1."_07]) a7 on (b.ps_id = a7.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1_xxxx1."_08]) a8 on (b.ps_id = a8.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1_xxxx1."_09]) a9 on (b.ps_id = a9.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1_xxxx1."_10]) a10 on (b.ps_id = a10.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1_xxxx1."_11]) a11 on (b.ps_id = a11.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1_xxxx1."_12]) a12 on (b.ps_id = a12.ps_id)";
    // $rxs = $row['ps_id_12'];
  }
  if ($date_xo == "02") {
    $count = "COUNT(DISTINCT a1.ps_id) as sum_ps_id,
    	COUNT(DISTINCT a2.ps_id) as ps_id_2";
    $join = "full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_01]) a1 on (b.ps_id = a1.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_02]) a2 on (b.ps_id = a2.ps_id)";
    // $rxs = $row['ps_id_1'];
  }
  if ($date_xo == "03") {
    $count = "COUNT(DISTINCT a1.ps_id) as ps_id_1,
    	COUNT(DISTINCT a2.ps_id) as sum_ps_id,
    	COUNT(DISTINCT a3.ps_id) as ps_id_3";
    $join = "full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_01]) a1 on (b.ps_id = a1.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_02]) a2 on (b.ps_id = a2.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_03]) a3 on (b.ps_id = a3.ps_id)";
    // $rxs = $row['ps_id_2'];
  }
  if ($date_xo == "04") {
    $count = "COUNT(DISTINCT a1.ps_id) as ps_id_1,
    	COUNT(DISTINCT a2.ps_id) as ps_id_2,
    	COUNT(DISTINCT a3.ps_id) as sum_ps_id,
    	COUNT(DISTINCT a4.ps_id) as ps_id_4";
    $join = "full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_01]) a1 on (b.ps_id = a1.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_02]) a2 on (b.ps_id = a2.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_03]) a3 on (b.ps_id = a3.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_04]) a4 on (b.ps_id = a4.ps_id)";
    // $rxs = $row['ps_id_3'];
  }
  if ($date_xo == "05") {
    $count = "COUNT(DISTINCT a1.ps_id) as ps_id_1,
    	COUNT(DISTINCT a2.ps_id) as ps_id_2,
    	COUNT(DISTINCT a3.ps_id) as ps_id_3,
    	COUNT(DISTINCT a4.ps_id) as sum_ps_id,
    	COUNT(DISTINCT a5.ps_id) as ps_id_5";
    $join = "full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_01]) a1 on (b.ps_id = a1.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_02]) a2 on (b.ps_id = a2.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_03]) a3 on (b.ps_id = a3.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_04]) a4 on (b.ps_id = a4.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_05]) a5 on (b.ps_id = a5.ps_id)";
    // $rxs = $row['ps_id_4'];
  }
  if ($date_xo == "06") {
    $count = "COUNT(DISTINCT a1.ps_id) as ps_id_1,
    	COUNT(DISTINCT a2.ps_id) as ps_id_2,
    	COUNT(DISTINCT a3.ps_id) as ps_id_3,
    	COUNT(DISTINCT a4.ps_id) as ps_id_4,
    	COUNT(DISTINCT a5.ps_id) as sum_ps_id,
      COUNT(DISTINCT a6.ps_id) as ps_id_6";
    $join = "full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_01]) a1 on (b.ps_id = a1.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_02]) a2 on (b.ps_id = a2.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_03]) a3 on (b.ps_id = a3.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_04]) a4 on (b.ps_id = a4.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_05]) a5 on (b.ps_id = a5.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_06]) a6 on (b.ps_id = a6.ps_id)";
    // $rxs = $row['ps_id_5'];
  }
  if ($date_xo == "07") {
    $count = "COUNT(DISTINCT a1.ps_id) as ps_id_1,
    	COUNT(DISTINCT a2.ps_id) as ps_id_2,
    	COUNT(DISTINCT a3.ps_id) as ps_id_3,
    	COUNT(DISTINCT a4.ps_id) as ps_id_4,
    	COUNT(DISTINCT a5.ps_id) as ps_id_5,
      COUNT(DISTINCT a6.ps_id) as sum_ps_id,
      COUNT(DISTINCT a7.ps_id) as ps_id_7";
    $join = "full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_01]) a1 on (b.ps_id = a1.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_02]) a2 on (b.ps_id = a2.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_03]) a3 on (b.ps_id = a3.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_04]) a4 on (b.ps_id = a4.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_05]) a5 on (b.ps_id = a5.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_06]) a6 on (b.ps_id = a6.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_07]) a7 on (b.ps_id = a7.ps_id)";
    // $rxs = $row['ps_id_6'];
  }
  if ($date_xo == "08") {
    $count = "COUNT(DISTINCT a1.ps_id) as ps_id_1,
      COUNT(DISTINCT a2.ps_id) as ps_id_2,
      COUNT(DISTINCT a3.ps_id) as ps_id_3,
      COUNT(DISTINCT a4.ps_id) as ps_id_4,
      COUNT(DISTINCT a5.ps_id) as ps_id_5,
      COUNT(DISTINCT a6.ps_id) as ps_id_6,
      COUNT(DISTINCT a7.ps_id) as sum_ps_id,
      COUNT(DISTINCT a8.ps_id) as ps_id_8";
    $join = "full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_01]) a1 on (b.ps_id = a1.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_02]) a2 on (b.ps_id = a2.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_03]) a3 on (b.ps_id = a3.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_04]) a4 on (b.ps_id = a4.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_05]) a5 on (b.ps_id = a5.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_06]) a6 on (b.ps_id = a6.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_07]) a7 on (b.ps_id = a7.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_08]) a8 on (b.ps_id = a8.ps_id)";
    // $rxs = $row['ps_id_7'];
  }
  if ($date_xo == "09") {
    $count = "COUNT(DISTINCT a1.ps_id) as ps_id_1,
      COUNT(DISTINCT a2.ps_id) as ps_id_2,
      COUNT(DISTINCT a3.ps_id) as ps_id_3,
      COUNT(DISTINCT a4.ps_id) as ps_id_4,
      COUNT(DISTINCT a5.ps_id) as ps_id_5,
      COUNT(DISTINCT a6.ps_id) as ps_id_6,
      COUNT(DISTINCT a7.ps_id) as ps_id_7,
      COUNT(DISTINCT a8.ps_id) as sum_ps_id,
      COUNT(DISTINCT a9.ps_id) as ps_id_9";
    $join = "full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_01]) a1 on (b.ps_id = a1.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_02]) a2 on (b.ps_id = a2.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_03]) a3 on (b.ps_id = a3.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_04]) a4 on (b.ps_id = a4.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_05]) a5 on (b.ps_id = a5.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_06]) a6 on (b.ps_id = a6.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_07]) a7 on (b.ps_id = a7.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_08]) a8 on (b.ps_id = a8.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_09]) a9 on (b.ps_id = a9.ps_id)";
    // $rxs = $row['ps_id_8'];
  }
  if ($date_xo == "10") {
    $count = "COUNT(DISTINCT a1.ps_id) as ps_id_1,
      COUNT(DISTINCT a2.ps_id) as ps_id_2,
      COUNT(DISTINCT a3.ps_id) as ps_id_3,
      COUNT(DISTINCT a4.ps_id) as ps_id_4,
      COUNT(DISTINCT a5.ps_id) as ps_id_5,
      COUNT(DISTINCT a6.ps_id) as ps_id_6,
      COUNT(DISTINCT a7.ps_id) as ps_id_7,
      COUNT(DISTINCT a8.ps_id) as ps_id_8,
      COUNT(DISTINCT a9.ps_id) as sum_ps_id,
      COUNT(DISTINCT a10.ps_id) as ps_id_10";
    $join = "full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_01]) a1 on (b.ps_id = a1.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_02]) a2 on (b.ps_id = a2.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_03]) a3 on (b.ps_id = a3.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_04]) a4 on (b.ps_id = a4.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_05]) a5 on (b.ps_id = a5.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_06]) a6 on (b.ps_id = a6.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_07]) a7 on (b.ps_id = a7.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_08]) a8 on (b.ps_id = a8.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_09]) a9 on (b.ps_id = a9.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_10]) a10 on (b.ps_id = a10.ps_id)";
    // $rxs = $row['ps_id_9'];
    }
  if ($date_xo == "11") {
    $count = "COUNT(DISTINCT a1.ps_id) as ps_id_1,
      COUNT(DISTINCT a2.ps_id) as ps_id_2,
      COUNT(DISTINCT a3.ps_id) as ps_id_3,
      COUNT(DISTINCT a4.ps_id) as ps_id_4,
      COUNT(DISTINCT a5.ps_id) as ps_id_5,
      COUNT(DISTINCT a6.ps_id) as ps_id_6,
      COUNT(DISTINCT a7.ps_id) as ps_id_7,
      COUNT(DISTINCT a8.ps_id) as ps_id_8,
      COUNT(DISTINCT a9.ps_id) as ps_id_9,
      COUNT(DISTINCT a10.ps_id) as sum_ps_id,
      COUNT(DISTINCT a11.ps_id) as ps_id_11";
    $join = "full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_01]) a1 on (b.ps_id = a1.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_02]) a2 on (b.ps_id = a2.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_03]) a3 on (b.ps_id = a3.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_04]) a4 on (b.ps_id = a4.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_05]) a5 on (b.ps_id = a5.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_06]) a6 on (b.ps_id = a6.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_07]) a7 on (b.ps_id = a7.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_08]) a8 on (b.ps_id = a8.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_09]) a9 on (b.ps_id = a9.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_10]) a10 on (b.ps_id = a10.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_11]) a11 on (b.ps_id = a11.ps_id)";
    // $rxs = $row['ps_id_10'];
  }
  if ($date_xo == "12") {
    $count = "COUNT(DISTINCT a1.ps_id) as ps_id_1,
      COUNT(DISTINCT a2.ps_id) as ps_id_2,
      COUNT(DISTINCT a3.ps_id) as ps_id_3,
      COUNT(DISTINCT a4.ps_id) as ps_id_4,
      COUNT(DISTINCT a5.ps_id) as ps_id_5,
      COUNT(DISTINCT a6.ps_id) as ps_id_6,
      COUNT(DISTINCT a7.ps_id) as ps_id_7,
      COUNT(DISTINCT a8.ps_id) as ps_id_8,
      COUNT(DISTINCT a9.ps_id) as ps_id_9,
      COUNT(DISTINCT a10.ps_id) as ps_id_10,
      COUNT(DISTINCT a11.ps_id) as sum_ps_id,
      COUNT(DISTINCT a12.ps_id) as ps_id_12";
    $join = "full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_01]) a1 on (b.ps_id = a1.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_02]) a2 on (b.ps_id = a2.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_03]) a3 on (b.ps_id = a3.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_04]) a4 on (b.ps_id = a4.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_05]) a5 on (b.ps_id = a5.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_06]) a6 on (b.ps_id = a6.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_07]) a7 on (b.ps_id = a7.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_08]) a8 on (b.ps_id = a8.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_09]) a9 on (b.ps_id = a9.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_10]) a10 on (b.ps_id = a10.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_11]) a11 on (b.ps_id = a11.ps_id)
      full outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$calcdate1."_12]) a12 on (b.ps_id = a12.ps_id)";
    // $rxs = $row['ps_id_11'];
  }
  // echo $count_5;
  // echo $join_5;
  // echo // $rxs;

  // สาขา
  $sql_sumline = "SELECT
  	a.br_id as line_br_id,
  	COUNT(DISTINCT a.ps_id) as sums,
    $count
    FROM [HPSDB].[dbo].[hps_ctmainremain_".$MY."] a
    left outer join(SELECT
    	  BR_ID, PS_ID, lineid
    FROM [EHONGDB].[dbo].[PERSON] GROUP BY BR_ID,PS_ID,lineid) b on (a.ps_id=b.PS_ID)
    $join
    WHERE a.BILL_DATE BETWEEN '$DMY1' AND '$DMY2'
    and (a.status<>'X')
    and (a.ct_no like '0%' or a.ct_no like '1%' or a.ct_no like '2%' or a.ct_no like '3%' or a.ct_no like '4%' or a.ct_no like '5%' or a.ct_no like '6%')
    and b.lineid is not null
    $BR_ID
    GROUP BY a.br_id
    ORDER BY a.br_id";
    $rs=$conn->prepare($sql_sumline);
    $rs->execute();
    $sumline_x1 = array();
      foreach($rs as $row){
    $sumline_x1[$row['line_br_id']]=$row['sum_ps_id'];
    };
    $sl_sx1 = 0;
    $sl_sx2 = 0;
    // echo $sql_sumline;

  //เขต
  $sql_sumline_1 = "SELECT
    a.SZONE_CODE as SZONE_CODE_1,
    $count
    FROM [EHONGDB].[dbo].[BRANCH] a
    left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$MY."]) b on (a.BR_ID=b.BR_ID)
    left outer join(SELECT
    	  BR_ID, PS_ID, lineid
    FROM [EHONGDB].[dbo].[PERSON] GROUP BY BR_ID,PS_ID,lineid) b2 on (b2.ps_id=b.PS_ID)
    $join
    left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b3 on (a.SZONE_CODE=b3.szone_code)
    WHERE b.BILL_DATE BETWEEN '$DMY1' AND '$DMY2'
    and (b.status<>'X')
    and (b.ct_no like '0%' or  b.ct_no like '1%' or  b.ct_no like '2%' or  b.ct_no like '3%' or  b.ct_no like '4%' or  b.ct_no like '5%' or  b.ct_no like '6%')
    and b2.lineid is not null
    $zone_code $BR_ID
    GROUP BY a.SZONE_CODE
    ORDER BY a.SZONE_CODE ASC";
    $rs=$conn->prepare($sql_sumline_1);
    $rs->execute();
    $sumline_c1 = array();
      foreach($rs as $row){
    $sumline_c1[$row['SZONE_CODE_1']]=$row['sum_ps_id'];
    };
    $sl_sc1 = 0;
    $sl_sc2 = 0;
    // echo $sql_sumline_1;

    // ภาค
    $sql_sumline_2 = "SELECT
      b3.zstaff_id as zstaff_id_1,
      $count
      FROM [EHONGDB].[dbo].[BRANCH] a
      left outer join(SELECT * FROM [HPSDB].[dbo].[hps_ctmainremain_".$MY."]) b on (a.BR_ID=b.BR_ID)
      left outer join(SELECT
          BR_ID, PS_ID, lineid
      FROM [EHONGDB].[dbo].[PERSON] GROUP BY BR_ID,PS_ID,lineid) b2 on (b2.ps_id=b.PS_ID)
      $join
      left outer join(SELECT * FROM [EHONGDB].[dbo].[branch_salezone]) b3 on (a.SZONE_CODE=b3.szone_code)
      WHERE b.BILL_DATE BETWEEN '$DMY1' AND '$DMY2'
      and (b.status<>'X')
      and (b.ct_no like '0%' or  b.ct_no like '1%' or  b.ct_no like '2%' or  b.ct_no like '3%' or  b.ct_no like '4%' or  b.ct_no like '5%' or  b.ct_no like '6%')
      and b2.lineid is not null
      $zstaff_id3 $BR_ID $zone_code
      GROUP BY b3.zstaff_id
      ORDER BY b3.zstaff_id ASC";
      $rs=$conn->prepare($sql_sumline_2);
      $rs->execute();
      $sumline_m1 = array();
        foreach($rs as $row){
      $sumline_m1[$row['zstaff_id_1']]=$row['sum_ps_id'];
      };
      $sl_sm1 = 0;
      $sl_sm2 = 0;
?>
