Files
BE_CPONE/application/controllers/tgram/Greeting.php
2026-04-27 10:26:26 +07:00

157 lines
6.0 KiB
PHP

<?php
//ini_set('display_errors', '1');
//ini_set('display_startup_errors', '1');
//error_reporting(E_ALL);
class Greeting extends MY_Controller
{
function __construct() {
parent::__construct();
}
/* WA ------ */
function update_wa() {
$prm = $this->sys_input;
$headerID = $prm["headerID"];
$groupResultID = $prm["groupResultID"];
$deliveryID= $prm["deliveryID"];
$jsonText= $prm["jsonTxt"];
$note= $prm["note"];
$destination= $prm["destination"];
$sql = "insert into tx_whatsapp(Tx_WhatsappT_OrderHeaderID, Tx_WhatsappGroup_ResultID, Tx_WhatsappM_DeliveryID, Tx_WhatsappSentDate, Tx_WhatsappJson,
Tx_WhatsappNote,Tx_WhatsappIsSent,Tx_WhatsappDestination) values (?, ?, ?, now(), ?, ?,'Y',?) ";
$qry = $this->db->query($sql, array($headerID, $groupResultID, $deliveryID, $jsonText, $note,$destination));
if ($qry) {
$txId = $this->db->insert_id();
$this->create_wa_detail($txId,$jsonText);
$this->update_greeting($destination);
$result = array("status" => "OK", "message" => "");
} else {
$result = array("status" => "ERR", "message" => print_r($this->db->error(),true) );
}
echo json_encode($result);
}
function create_wa_detail($txid, $text) {
$p= json_decode($text,true);
$sql = "insert into tx_whatsappdetail(Tx_WhatsappDetailTx_WhatsappID,Tx_WhatsappDetailUuid, Tx_WhatsappDetailCreated,Tx_WhatsappDetailMessage)
values(?,?,now(),?)";
if(isset($p["data"]) && isset($p["data"]["uuid"]) ){
$uuid = $p["data"]["uuid"];
$message= $p["data"]["message"];
$this->db->query($sql, array($txid,$uuid,$message));
}
}
function update_greeting($hp) {
$sql = "insert into wa_greeting(WaGreetingHp) values(?)";
$qry = $this->db->query($sql,array($hp));
/*
if ($qry) {
//insert into tx_whatsapp
echo json_encode(array("status" => "OK" , "message" => ""));
} else {
echo json_encode(array("status" => "ERR" , "message" => print_r($this->db->error(),true)));
}
*/
}
function is_greet($phone) {
$sql = "select count(*) tot from wa_greeting where WaGreetingHp = ? ";
$qry = $this->db->query($sql, array($phone));
if($qry) {
$rows = $qry->result_array();
if (count($rows)> 0 ) {
if ($rows[0]["tot"] > 0 ) return true;
}
}
return false;
}
function list() {
$date = $this->sys_input["date"];
//hanya whatsapp pasien
$sql = "select group_concat(distinct T_OrderHeaderID) xids
from
t_orderheader
join t_orderheaderaddon on T_OrderHeaderID = T_OrderHeaderAddOnT_OrderHeaderID and T_OrderHeaderAddOnIsActive = 'Y'
and T_OrderHeaderAddOnFOVerification = 'Y'
join t_orderdelivery on T_OrderHeaderID = T_OrderDeliveryT_OrderHeaderID
and T_OrderHeaderIsActive = 'Y' and date(T_OrderHeaderDate) = ?
and T_OrderDeliveryIsActive = 'Y'
and T_OrderDeliveryM_DeliveryID in ( 6 )
";
$ids = "";
$query = $this->db->query($sql,array($date));
if ($query) {
$rows = $query->result_array();
$ids = $rows[0]["xids"];
} else {
$result = array( "status" => "ERR", "rows" => array(), "messge" => print_r($this->db->error(),true) );
echo json_encode($result);
exit;
}
if ($ids == "") {
$result = array( "status" => "OK", "rows" => array() );
echo json_encode($result);
exit;
}
$sql = "select * from m_emailconfig where M_EmailConfigIsActive = 'Y' limit 0,1";
$query = $this->db->query($sql);
$tpl_greeting = "";
if ($query) {
$rows = $query->result_array();
if(count($rows) > 0 ) {
$r = $rows[0];
$tpl_greeting = trim($r["M_EmailConfigWaGreetingAPS"]);
}
} else {
$result = array( "status" => "ERR", "rows" => array(), "messge" => print_r($this->db->error(),true) );
echo json_encode($result);
exit;
}
if ($ids == "") {
$result = array( "status" => "OK", "rows" => array(), "messge" => "No Greeting setup" );
echo json_encode($result);
exit;
}
$sql = "select distinct T_OrderHeaderID, T_OrderDeliveryDestination,
fn_get_patient_atribute(T_OrderHeaderM_PatientID) PatientName,
ifnull(Last_StatusPaymentIsLunas,'N') IsLunas
from t_orderheader
join t_orderdelivery on T_OrderHeaderID = T_OrderDeliveryT_OrderHeaderID and T_OrderDeliveryIsActive = 'Y'
and T_OrderDeliveryM_DeliveryID in ( 6 )
left join last_statuspayment ON Last_StatusPaymentT_OrderHeaderID = T_OrderHeaderID AND Last_StatusPaymentIsActive = 'Y'
where T_OrderHeaderID in ( $ids )
order by T_OrderHeaderID ";
$query = $this->db->query($sql,array($date));
$list_result = array();
if ($query) {
$rows = $query->result_array();
$max_count = 30;
$count = 0;
foreach($rows as $idx => $r) {
if ($count >= $max_count) break;
$headerID = $r["T_OrderHeaderID"];
$phone = $r["T_OrderDeliveryDestination"];
if ($this->is_greet($phone)) continue;
if ($r["isLunas"] == "N" ) continue ;
$count++;
$j_pasien = json_decode($r["PatientName"],true);
$xname = $j_pasien["patient_fullname"];
$greeting = str_replace("{PASIEN}",$xname,$tpl_greeting);
$list_result[] = array(
"T_OrderHeaderID" => $headerID,
"phone" => $phone,
"greeting" => $greeting
);
}
} else {
$result = array( "status" => "ERR", "rows" => array(), "messge" => print_r($this->db->error(),true) );
echo json_encode($result);
exit;
}
echo json_encode( array(
"status" => "OK",
"rows" => $list_result
));
}
}