Files
2026-04-27 10:26:26 +07:00

131 lines
6.8 KiB
PHP

<?php
class Mr03 extends CI_Controller
{
function __construct() {
parent::__construct();
$this->db = $this->load->database('onedev', true);
}
function getRegionalIP() {
$sql = "select S_SystemIPAddressRegional from conf_systems";
$qry = $this->db->query($sql);
if (! $qry ) {
return "devone.aplikasi.web.id";
}
$rows = $qry->result_array();
if (count($rows) > 0 ) return $rows[0]["S_SystemIPAddressRegional"];
return "devone.aplikasi.web.id";
}
function do($prm_date) {
$sql = "delete from ssr_mr03 where date(SsrMr03T_OrderHeaderDate) = ?";
$qry = $this->db->query($sql, array($prm_date));
if (! $qry ) {
echo 'Err : ' . print_r($this->db->error(),true). '\n';
exit;
}
$sql = "select * from m_branch where M_BranchIsActive = 'Y' and M_BranchIsDefault = 'Y'";
$qry = $this->db->query($sql);
if (! $qry ) {
echo 'Err : ' . print_r($this->db->error(),true) . '\n';
exit;
}
$rows = $qry->result_array();
$branchCode = '';
if (count($rows) > 0 ) {
$branchCode = $rows[0]['M_BranchCode'];
}
if ($branchCode == '') {
echo 'Err : Invalid Branch\n';
exit;
}
$sql = " insert into ssr_mr03 (SsrMr03T_OrderHeaderID,
SsrMr03M_BranchCode, SsrMr03M_StaffNIK,
SsrMr03M_PatientName, SsrMr03M_CompanyID, SsrMr03M_DoctorOldCode,
SsrMr03T_OrderHeaderDate, SsrMr03T_OrderHeaderLabNumberExt, SsrMr03Total,
SsrMr03Reward, SsrMr03RoundReward, SsrMr03Nat_JpaGroupMaxJpaDisc, SsrMr03M_No_FormRev,
SsrMr03M_DoctorAddressOldCode, SsrMr03M_DoctorID, SsrMr03M_DoctorAddressID )
select T_OrderHeaderID,
?, M_DoctorM_StaffNIK,
CONCAT(M_TitleName,' ',M_PatientName) AS M_PatientName, M_CompanyID, M_DoctorOldCode,
T_OrderHeaderDate, T_OrderHeaderLabNumberExt,
sum(fn_mr_price(T_OrderDetailT_TestSasCode,T_OrderDetailTotal,T_OrderDetailT_TestID, T_OrderHeaderID)) as total,
sum(fn_mr_Price(T_OrderDetailT_TestSasCode,T_OrderDetailTotal,T_OrderDetailT_TestID, T_OrderHeaderID) *(Nat_JPADetailDiscount/100)) as reward,
round(sum(fn_mr_Price(T_OrderDetailT_TestSasCode,T_OrderDetailTotal,T_OrderDetailT_TestID, T_OrderHeaderID)*(Nat_JPADetailDiscount/100))/ 1000)*1000 as roundreward,
Nat_JpaGroupMaxJpaDisc, M_No_FormRev, M_DoctorAddressOldCode, M_DoctorID, M_DoctorAddressID
from
t_orderheader
join m_doctoraddress on T_OrderHeaderSenderM_DoctorAddressID = M_DoctorAddressID and M_DoctorAddressIsActive = 'Y'
join m_doctor on M_DoctorAddressM_DoctorID = M_DoctorID
join t_orderdetail ON T_OrderDetailT_OrderHeaderID = T_OrderHeaderID AND T_OrderDetailIsActive = 'Y' and T_OrderHeaderIsActive = 'Y'
join t_test ON T_OrderDetailT_TestID = T_TestID and T_TestIsPrice = 'Y' AND T_TestIsActive = 'Y'
join nat_test oN T_TestNat_TestID = Nat_TestID aND Nat_TestIsActive = 'Y'
join jpa_test on JPA_TestNat_TestID = Nat_TestID and JPA_TestIsActive = 'Y'
join nat_jpa on M_DoctorAddressNat_JpaID = Nat_JpaID and Nat_JpaIsActive = 'Y'
join nat_jpagroup on JPA_TestNat_JpaGroupID = Nat_JPAGroupID and Nat_JPAGroupIsActive = 'Y'
join nat_jpadetail on Nat_JPADetailNat_JPAID = Nat_JPAID and Nat_JPAGroupID = Nat_JPADetailNat_JPAGroupID and Nat_JPADetailIsActive = 'Y'
left join m_company ON T_OrderHeaderM_CompanyID = M_CompanyID AND M_CompanyIsActive = 'Y'
left join m_patient ON T_OrderHeaderM_PatientID = M_PatientID AND M_PatientIsActive = 'Y'
left join m_title ON M_PatientM_TitleID = M_TitleID AND M_TitleIsActive = 'Y'
-- left join m_staff on M_DoctorM_StaffID = M_StaffID
LEFT JOIN m_no_form on M_No_FormID > 0 and M_No_FormName = 'INV' and M_No_FormIsActive = 'Y'
left join conf_systems on S_SystemsID > 0
where
DATE(T_OrderHeaderDate) = ?
and T_OrderHeaderIsActive = 'Y'
and Nat_JpaGroupMaxJpaDisc >= Nat_JPADetailDiscount
and M_DoctorIsMarketingConfirm = 'Y'
group by T_OrderHeaderID
order by T_OrderHeaderID ";
$qry = $this->db->query($sql,array($branchCode,$prm_date));
if (!$qry) print_r($this->db->error());
$tot = $this->db->affected_rows();
echo "$prm_date : $tot records\n";
}
function upload($prm_date) {
$sql = "select * from ssr_mr03
where date(SsrMr03T_OrderHeaderDate) = ?
and SsrMr03IsSent = 'N'
and SsrMr03Retry < 10";
$qry = $this->db->query($sql,array($prm_date));
if (! $qry) {
echo "Err : " . print_r($this->db->error(),true);
exit;
}
$rows = $qry->result_array();
$data = json_encode($rows);
$md5 = md5($data);
$param = array("data" => $data, "md5" => $md5);
$jparam = json_encode($param);
$regionalIP = $this->getRegionaliP();
$url = "http://$regionalIP/one-api/tools/marketing/r_mr03";
echo "Uploading : $prm_date, total " . count($rows) . " records\n";
$j_result = $this->post($url,$jparam);
$result = json_decode($j_result,true);
$sql = "update ssr_mr03 set SsrMr03Retry = SsrMr03Retry + 1 where date(SsrMr03T_OrderHeaderDate) = ?";
$this->db->query($sql,array($prm_date));
if ($result["status"] == "OK") {
$sql = "update ssr_mr03 set SsrMr03IsSent = 'Y' where SsrMr03ID = ?";
foreach($result["SsrMr03ID"] as $id ) {
$this->db->query($sql, array($id));
}
echo $result["status"] . ", total " . count($result["SsrMr03ID"]) . "\n";
exit;
}
echo "ERR : " . $result["message"] . "\n";
}
function post($url,$data) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
$result = curl_exec($ch);
return $result;
}
}