<?php 
$http_origin = $_SERVER['HTTP_ORIGIN'];
function shapeSpace_check_https() {
	
	if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) {
		
		return true; 
	}
	return false;
}
if (shapeSpace_check_https() == "true")
{  
 header("Access-Control-Allow-Origin: $http_origin");
}
//header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json'); 
include 'connect61.php';
$text=trim($_REQUEST['text']);
$messages=array();
if(strlen($text)>8){
$sql="SELECT
	[ct_no]
	,[ps_id]
	,[ps_name]
	,[TI_NAME]		
	,[PS_FNAME]
	,[PS_LNAME]
	,[PS_PHONE]
	,[PS_MOBILE]
	,[CD_DATA]
	,[ct_date]
	,[br_id]
	,[sbr_id]
	,[price]
	,[payamnt]
	,[due_amnt]
	,[due_count]
	,[product_id]
	,[product_name]
	,[serial_no]
	,[status]
	,[cnsvrefno]
	,[cnsvrefcode]
	,[cd_no]
	,[cd_date]
	,[cd_paydate]
	,[ct_nscore]
	,[slowly_amnt]
FROM [View_To_Table].[dbo].[RCT_MAIN]  where 1=1 ";
if($text>0){
	$text=strtoupper($text);
    $sql.="and (ps_id = '".$text."' or PS_PHONE = '".$text."' or PS_MOBILE = '".$text."' or CD_DATA = '".$text."' or cnsvrefcode = '".$text."' or left(cnsvrefcode,LEN(cnsvrefcode)-2) = '".$text."')";
	//return "isnumeric ".$sql;
}else{
	trim($text);
	$isprefix=false;
	$prefix="";
	if(stripos($text,"นาย") !== false){
		$isprefix=true;
		$prefix="นาย";
		$text=str_replace('นาย', '', $text);
	}
	if(stripos($text,"นางสาว") !== false){
		$isprefix=true;
		$prefix="นางสาว";
		$text=str_replace('นางสาว', '', $text);
	}
	if(stripos($text,"นาง") !== false){
		$isprefix=true;
		$prefix="นาง";
		$text=str_replace('นาง', '', $text);
	}
	$arr=explode(" ",$text);
	foreach ($arr as $key => $value) {
		if($value==""){
			unset($arr[$key]);
		}
	}
	$arr=array_values($arr);
	if($isprefix){
		if (count($arr)!=2) {
			array_push($messages,'โปรดระบุชื่อ-นามสกุล ให้ถูกต้อง');
			goto end;
		}else{
			$sql.="and TI_NAME = '".$prefix."' and PS_FNAME = '".$arr[0]."' and PS_LNAME =    '".$arr[1]."'";
		}
	}else{
		if (count($arr)!=2) {
			array_push($messages,'โปรดระบุชื่อ-นามสกุล ให้ถูกต้อง');
			goto end;
		}else{
			$sql.="and PS_FNAME = '".$arr[0]."' and PS_LNAME = '".$arr[1]."'";
		}
	}
}
$stmt = $dbh->prepare($sql,array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute();
if($stmt->rowCount()>0){
	$messages=array("status"=>"พบ");
	$datalist=array();
	while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
		$ctno = $row['ct_no'];
		// $sql2="	SELECT top 1 ct_no, cd_no,cd_date, cd_paydate, ct_nscore, slowly_amnt 
  //               FROM [EHONGDB].[dbo].[hps_ctdue] 
  //               WHERE ct_no = '$ctno' AND cd_paydate is null or cd_paydate = '' order by cd_no";
		// -- คำนวณยอดที่ต้องชำระ -- //
// $sql_SD = "SELECT
//     a.ct_no
//     ,a.cd_no
//       ,a.cd_date
//       ,a.cd_paydate
//       ,a.ct_nscore
//       ,a.slowly_amnt
//       ,b.due_amnt
//       ,a.cd_payamnt
//       ,b.due_amnt-a.cd_payamnt+a.slowly_amnt as due_amntTotal
//       ,a.status
//       ,b.ct_no
//       ,b.ps_id
//       ,b.ps_name
//       ,b.ct_date
//       ,b.br_id
//       ,b.sbr_id
//       ,b.price
//       ,b.payamnt
//       ,b.due_count
//       ,b.product_id
//       ,b.product_name
//       ,b.serial_no
//       ,b.status
//     ,b.cnsvrefno
//       ,b.cnsvrefcode
//       ,c.TI_NAME
//       ,c.PS_FNAME
//       ,c.PS_LNAME
//       ,c.PS_PHONE
//       ,c.PS_MOBILE
//       ,c.CD_DATA
//   FROM [EHONGDB].[dbo].[hps_ctdue] a
//       left outer join(
//         SELECT
//             ct_no
//           ,ps_id
//           ,ps_name
//           ,ct_date
//           ,br_id
//           ,sbr_id
//           ,price
//           ,payamnt
//           ,due_amnt
//           ,due_count
//           ,product_id
//           ,product_name
//           ,serial_no
//           ,status
//         ,cnsvrefno
//           ,cnsvrefcode
//         FROM [EHONGDB].[dbo].[hps_ctmain] WHERE status = 'N') b on (a.ct_no=b.ct_no)
//         left outer join(
//         SELECT 
//           PS_ID
//             ,TI_NAME   
//             ,PS_FNAME
//             ,PS_LNAME
//             ,PS_PHONE
//             ,PS_MOBILE
//             ,CD_DATA
//             ,img_path
//         FROM [EHONGDB].[dbo].[PERSON]) c on (a.ps_id=c.PS_ID)
//   WHERE a.ct_no = '$ctno' and slowly_amnt is not null and slowly_amnt != '0' and a.status = 'D' order by cd_date asc";
//       $stmt_SD = $dbh->prepare($sql_SD);
//       $stmt_SD->execute();
//       $row_SD = $stmt_SD->fetch();
//       $due_amntTotal=$row_SD['due_amntTotal'];
//       $total_Camount=$Camount+$due_amntTotal;
                // -- END คำนวณยอดที่ต้องชำระ -- //
		$billerid='|010556020330003';
		$psid=$row['ps_id'];
        $dueamnt=$row['due_amnt'];
        $slowlyamnt=$row['slowly_amnt'];
        $amount=$dueamnt+$slowlyamnt."00";
        $Camount=$dueamnt+$slowlyamnt;

		$data=array();
		$data+=array("ct_no"=>$row['cnsvrefcode']);
		$data+=array("ct_nos"=>$row['ct_no']);
		$data+=array("product_name"=>$row['product_name']);
		$data+=array("due_amnt"=>$row['due_amnt']);
		$data+=array("cnsvrefcode"=>$row['cnsvrefcode']);
		$data+=array("ps_name"=>$row['TI_NAME'].$row['PS_FNAME']."  ".$row['PS_LNAME']);
		$data+=array("biller_id"=>"$billerid");
		$data+=array("Camount"=>"$Camount");
		//$data+=array("total_Camount"=>"$total_Camount");		
		array_push($datalist,$data);
	}
	$messages+=array("datalist"=>$datalist);
}else{
	$messages=array("status"=>"ไม่พบ");
}
}else{
	$messages=array("status"=>"ไม่พบ");
}
end:
echo json_encode($messages,JSON_UNESCAPED_UNICODE);
?>