Files
2026-04-15 15:23:57 +07:00

992 lines
35 KiB
PHP

<?php
class Order extends MY_Controller
{
var $db_smartone;
public function index()
{
echo "ORDER API";
}
public function __construct()
{
parent::__construct();
$this->db_smartone = $this->load->database("onedev", true);
}
function get_time_start(){
$rows = date("Y-m-d H:i:s");
$this->sys_ok(["records"=>$rows, "total"=>1, "q"=>$this->db_smartone->last_query()]);
}
function save()
{
$prm = $this->sys_input;
$hdr = $prm['header'];
$patient = $prm['patient'];
$s_test = $prm['checked_test'];
$header_json = addslashes(str_replace('\n', '\\\n', json_encode($hdr)));
$delivery_json = addslashes(str_replace('\n', '\\\n', json_encode($prm['delivery'])));
$sql = $this->db_smartone->query("SELECT M_BranchID as branchid FROM
m_branch
WHERE M_BranchIsActive = 'Y' AND M_BranchIsDefault = 'Y'")->row();
$branchid = $sql->branchid;
$sql = $this->db_smartone->query("SELECT HS_ConfigMinOrder as minorder FROM
one_hs.hs_config
WHERE HS_ConfigIsActive = 'Y'")->row();
$minorder = $sql->minorder;
$userid = $this->sys_user["M_UserID"];
$hsdate = date('Y-m-d', strtotime($hdr['hs_date']));
$hstime = $hdr['hs_time'];
$mouid = $hdr['mou_id'];
$sqlheader = "INSERT INTO one_hs.t_transaction
(T_TransactionNumbering,
T_TransactionM_BranchID,
T_TransactionM_MouID,
T_TransactionDiscount,
T_TransactionFee,
T_TransactionDate,
T_TransactionIsOnline,
T_TransactionUserID,
T_TransactionLastUpdated,
T_TransactionCreated)
VALUES(
one_hs.fn_numbering('TRL'),
$branchid,
$mouid,
'0',
'0',
now(),
'N',
$userid,
now(),
now())";
$queryheader = $this->db_smartone->query($sqlheader);
if($queryheader)
{
//echo $this->db_smartone->last_query();
$transaction_id = $this->db_smartone->insert_id();
$hs_price = $hdr['hs_price'];
$hs_priceother = $hdr['hs_priceother'];
$hs_priceminimal = $hdr['hs_priceminimal'];
$hs_addressid = $hdr['hs_addressid'];
$hs_name = $hdr['hs_name'];
$hs_hp = $hdr['hs_hp'];
$sqlheaderhs = "INSERT INTO one_hs.t_transactionHS
(t_transactionHST_TransactionID,
t_transactionHSOrderName,
t_transactionHSOrderNoHP,
t_transactionHSCost,
t_transactionHSCostOther,
t_transactionHSCostMinOrder,
t_transactionHSHS_AddressID,
t_transactionHSIsLocal,
t_transactionHSUserID,
t_transactionHSCreated,
t_transactionHSLastUpdated,
t_transactionHSOldID)
VALUES(
$transaction_id,
'{$hs_name}',
'{$hs_hp}',
$hs_price,
$hs_priceother,
$hs_priceminimal,
$hs_addressid,
'Y',
$userid,
now(),
now(),
0)";
$queryheaderhs = $this->db_smartone->query($sqlheaderhs);
//echo $this->db_smartone->last_query();
foreach($patient as $k => $p){
$pid = $p['patientidhs'];
$sqlorder = "INSERT INTO one_hs.t_order(
T_OrderT_TransactionID,
T_OrderNumber,
T_OrderM_BranchID,
T_OrderM_PatientID,
T_OrderDate,
T_OrderTime,
T_OrderUserID,
T_OrderLastUpdated)
VALUES(
$transaction_id,
one_hs.fn_numbering('RL'),
$branchid,
$pid,
'{$hsdate}',
'{$hstime}',
$userid,
now())";
//echo $sqlorder;
$queryorder = $this->db_smartone->query($sqlorder);
if($queryorder){
$order_id = $this->db_smartone->insert_id();
foreach($s_test as $x => $v){
if($v['patientid'] == $pid){
$sql = "INSERT INTO one_hs.t_orderdetail(
T_OrderDetailT_OrderID,
T_OrderDetailT_TestName,
T_OrderDetailT_TestID,
T_OrderDetailAmount,
T_OrderDetailDiscount,
T_OrderDetailDiscountRp,
T_OrderDetailSubtotal,
T_OrderDetailTotal,
T_OrderDetailUserID,
T_OrderDetailLastUpdated
)
VALUES(?,?,?,?,?,?,?,?,?,NOW())";
$order = $this->db_smartone->query($sql,
array(
$order_id,
$v['T_TestName'],
$v['T_TestID'],
$v['T_PriceAmount'],
$v['T_PriceDisc'],
$v['T_PriceDiscRp'],
$v['T_PriceSubTotal'],
$v['T_PriceTotal'],
$userid
)
);
}
}
foreach($prm['delivery'] as $kdel => $vdel){
$sql = "INSERT INTO one_hs.t_orderdeliveries(
T_OrderDeliveriesT_OrderID,
T_OrderDeliveriesM_DeliveryID,
T_OrderDeliveriesM_DeliveryTypeID,
T_OrderDeliveriesDestination,
T_OrderDeliveriesUserID,
T_OrderDeliveriesLastUpdated
)
VALUES(?,?,?,?,?,NOW())";
$order = $this->db_smartone->query($sql,
array(
$order_id,
$vdel['delivery_id'],
$vdel['delivery_type_id'],
$vdel['note'],
$userid
)
);
}
}
}
$r = $this->db_smartone->where('T_TransactionID', $transaction_id)
->get('one_hs.t_transaction')
->row();
$rst->data = array("id" => $transaction_id, 'noreg'=>$r->T_TransactionNumbering);
$this->broadcast("fo-register");
echo json_encode($rst);
}else
{
$this->sys_error_db("save order", $this->db_smartone);
exit;
}
}
function get_header($id){
$sql = "
SELECT T_OrderHeaderID as order_id,
T_OrderHeaderLabNumber as order_no,
T_OrderHeaderDate as order_date,
T_OrderHeaderSubTotal as order_subtotal,
T_OrderHeaderRounding as order_rounding,
T_OrderHeaderTotal as order_total,
concat(if(M_TitleID is null, '', concat(M_TitleName, ' ')),IFNULL(M_PatientPrefix,''),' ',M_PatientName,' ',IFNULL(M_PatientSuffix,'')) as patient_name,
M_PatientNoReg as patient_mr,
M_MouName as order_mou,
M_CompanyName as order_company,
fn_global_doctor_name(da.M_DoctorID) doctor_sender,
fn_global_doctor_name(db.M_DoctorID) doctor_pj,
fn_global_doctor_address(aa.M_DoctorAddressID, 1) doctor_sender_address,
M_MouIsBill M_CompanyIsBill, M_MouMinDP M_CompanyMinDP,
M_MouIsAgingOnHold M_CompanyIsAgingOnHold, M_MouIsAgingOnHoldNote M_CompanyIsAgingOnHoldNote
from t_orderheader
join m_patient on T_OrderHeaderM_PatientID = M_PatientID
join m_company on T_OrderHeaderM_CompanyID = M_CompanyID
join m_mou on T_OrderHeaderM_MouID = M_MouID
join m_doctor da on T_OrderHeaderSenderM_DoctorID = da.M_DoctorID
join m_doctoraddress aa on T_OrderHeaderSenderM_DoctorAddressID = aa.M_DoctorAddressID
join m_doctor db on T_OrderHeaderSenderM_DoctorID = db.M_DoctorID
left join m_title on m_patientm_titleid = m_titleid
where T_OrderHeaderID = {$id}";
//echo $sql;
$query = $this->db_smartone->query($sql);
$rows = $query->row();
//echo $this->db->last_query();
return $rows;
}
function get_delivery($id){
$sql = "CALL sp_fo_payment_get_delivery('{$id}')";
$rows = $this->db_smartone->query($sql)->row();
$this->clean_mysqli_connection($this->db_smartone->conn_id);
return json_decode($rows->delivery);
}
function search_latlong(){
$rows = [];
$sql = "SELECT HS_ConfigLat as lat,
HS_ConfigLong as lng,
HS_ConfigMaxDistance,
HS_ConfigMinOrder
FROM
one_hs.hs_config
WHERE HS_ConfigIsActive = 'Y'";
$rows = $this->db_smartone->query($sql)->result_array();
$sql = $this->db_smartone->query("SELECT HS_ConfigLat as lat FROM
one_hs.hs_config
WHERE HS_ConfigIsActive = 'Y'")->row();
$lat = $sql->lat;
$sql = $this->db_smartone->query("SELECT HS_ConfigLong as lng FROM
one_hs.hs_config
WHERE HS_ConfigIsActive = 'Y'")->row();
$lng = $sql->lng;
$result = array ("total" => count($rows), "records" => $rows, "lat" =>$lat,"lng"=>$lng);
$this->sys_ok($result);
}
function search_hsprice(){
$prm = $this->sys_input;
$lat = $prm['lat'];
$long = $prm['long'];
$userid = $this->sys_user["M_UserID"];
$sql = $this->db_smartone->query("SELECT M_BranchID as branchid FROM
m_branch
WHERE M_BranchIsActive = 'Y' AND M_BranchIsDefault = 'Y'")->row();
$branchid = $sql->branchid;
$sql = $this->db_smartone->query("SELECT HS_ConfigLat as branchlat FROM
one_hs.hs_config
WHERE HS_ConfigIsActive = 'Y'")->row();
$branchlat = $sql->branchlat;
$sql = $this->db_smartone->query("SELECT HS_ConfigLong as branchlong FROM
one_hs.hs_config
WHERE HS_ConfigIsActive = 'Y'")->row();
$branchlong = $sql->branchlong;
$sql = $this->db_smartone->query("SELECT ROUND(one_hs.distance_v2($lat,$long,$branchlat,$branchlong)/1000,1) as distance")->row();
$distance = $sql->distance;
$sql = $this->db_smartone->query("SELECT HS_PriceAmount as price
FROM one_hs.hs_price
WHERE
HS_PriceM_BranchID = $branchid AND
( $distance > HS_PriceMinDistance AND $distance <= HS_PriceMaxDistance ) AND
HS_PriceIsActive = 'Y'")->row();
//echo $this->db_smartone->last_query();
$price = $sql->price;
$result = array ("total" => count($rows), "records" => $price);
$this->sys_ok($result);
}
function get_details($id){
$sql = "
select T_OrderDetailID as d_id,
T_OrderDetailT_TestID as t_id,
IFNULL(T_OrderDetailT_TestName, T_PacketName) as t_name,
T_OrderDetailPrice as t_price,
T_OrderDetailDiscTotal as t_disctotal,
T_OrderDetailTotal as t_total
from t_orderdetail
join t_orderdetailaddon on T_OrderDetailAddOnT_OrderDetailID = T_OrderDetailID
left join t_test on t_orderdetailt_testid = t_testid
left join t_packet on t_orderdetailaddonispacket = 'Y' and t_orderdetailaddont_packetid = t_packetid
where T_OrderDetailT_OrderHeaderID = {$id}
and T_ORderDetailIsActive = 'Y'
and ((T_ORderDetailAddOnIsPacket = 'N' AND T_TestIsPrintNota = 'Y' AND T_OrderDetailT_TestIsPanelChildren = 'N')
OR (T_OrderDetailT_TestIsPanelChildren = 'Y' AND T_OrderDetailT_TestIsPanelChildrenPrintNota = 'Y')
OR (T_ORderDetailAddOnIsPacket = 'Y' AND T_PacketIsNOta = 'Y'))";
$query = $this->db_smartone->query($sql);
//echo $this->db->last_query();
$rows = $query->result_array();
return $rows;
}
function load_from_clinic()
{
$prm = $this->sys_input;
$sql = "CALL sp_fo_clinic_load('{$prm['queue']}');";
$query = $this->db_smartone->query($sql);
if ($query)
{
$rst = $query->row();
if ($rst->status == "OK")
{
$rst->data = json_decode($rst->data);
$rst->data->doctor->address = $rst->data->doctor_address;
unset($rst->data->doctor_address);
$rst->data->company->mou = [$rst->data->mou];
unset($rst->data->mou);
}
else
{
$this->sys_error("Tidak ada order");
exit;
}
echo json_encode($rst);
}
else
{
$this->sys_error_db("save order", $this->db_smartone);
exit;
}
}
function load()
{
$prm = $this->sys_input;
$sql = "CALL sp_fo_register_load('{$prm['id']}');";
$query = $this->db_smartone->query($sql);
if ($query)
{
$rst = $query->row();
if ($rst->status == "OK")
{
$rst->data = json_decode($rst->data);
$rst->data->doctor->address = $rst->data->doctor_address;
unset($rst->data->doctor_address);
$rst->data->company->mou = [$rst->data->mou];
unset($rst->data->mou);
}
else
{
$this->sys_error("Tidak ada order");
exit;
}
echo json_encode($rst);
}
else
{
$this->sys_error_db("save order", $this->db_smartone);
exit;
}
}
function search_deliveries($prm)
{
$type = $prm['type'];
$id = $prm['id'];
if($type == 'patient'){
$sql = "
SELECT 0 as kelurahan,
0 as address_id,
M_DeliveryM_DeliveryTypeID as delivery_type,
M_DeliveryID as delivery_id,
M_DeliveryName as delivery_name,
'' as description,
'N' as chex,
'' as note,
'origin' as typeform,
'{$type}' as type,
M_DeliveryTypeCode as delivery_code
FROM m_delivery
JOIN m_deliverytype ON M_DeliveryTypeCode = 'PICKUP' AND M_DeliveryM_DeliveryTypeID = M_DeliveryTypeID
WHERE
M_DeliverySource = '{$type}' AND M_DeliveryIsActive = 'Y'
UNION
SELECT M_PatientAddressM_KelurahanID as kelurahan,
M_PatientAddressID as address_id,
M_DeliveryM_DeliveryTypeID as delivery_type,
M_DeliveryID as delivery_id,
M_DeliveryName as delivery_name,
CONCAT(M_PatientAddressDescription,' ',M_KelurahanName,', ',M_DistrictName,', ',M_CityName) as description,
'N' as chex,
'' as note,
'origin' as typeform,
'{$type}' as type,
M_DeliveryTypeCode as delivery_code
FROM m_patient
JOIN m_patientaddress ON M_PatientAddressM_PatientID = M_PatientID AND M_PatientAddressIsActive = 'Y'
JOIN m_kelurahan ON M_PatientAddressM_KelurahanID = M_KelurahanID
JOIN m_district ON M_KelurahanM_DistrictID = M_DistrictID
JOIN m_city ON M_DistrictM_CityID = M_CityID
JOIN m_delivery ON M_DeliverySource = '{$type}' AND M_DeliveryIsActive = 'Y'
JOIN m_deliverytype ON M_DeliveryTypeCode = 'ADDRESS' AND M_DeliveryM_DeliveryTypeID = M_DeliveryTypeID
WHERE
M_PatientID = {$id}
GROUP BY M_PatientAddressID
UNION
SELECT 0 as kelurahan,
0 as address_id,
M_DeliveryM_DeliveryTypeID as delivery_type,
M_DeliveryID as delivery_id,
M_DeliveryName as delivery_name,
IFNULL(M_PatientEmail,'Belum ada email pasien') as description,
'N' as chex,
'' as note,
'origin' as typeform,
'{$type}' as type,
M_DeliveryTypeCode as delivery_code
FROM m_patient
JOIN m_delivery ON M_DeliverySource = '{$type}' AND M_DeliveryIsActive = 'Y'
JOIN m_deliverytype ON M_DeliveryTypeCode = 'EMAIL' AND M_DeliveryM_DeliveryTypeID = M_DeliveryTypeID
WHERE
M_PatientID = {$id}
UNION
SELECT 0 as kelurahan,
0 as address_id,
M_DeliveryM_DeliveryTypeID as delivery_type,
M_DeliveryID as delivery_id,
M_DeliveryName as delivery_name,
IFNULL(M_PatientEmail,'Belum ada email pasien') as description,
'N' as chex,
'' as note,
'origin' as typeform,
'{$type}' as type,
M_DeliveryTypeCode as delivery_code
FROM m_patient
JOIN m_delivery ON M_DeliverySource = '{$type}' AND M_DeliveryIsActive = 'Y'
JOIN m_deliverytype ON M_DeliveryTypeCode = 'EMAIL' AND M_DeliveryM_DeliveryTypeID = M_DeliveryTypeID
WHERE
M_PatientID = {$id}
UNION
SELECT 0 as kelurahan,
0 as address_id,
M_DeliveryM_DeliveryTypeID as delivery_type,
M_DeliveryID as delivery_id,
M_DeliveryName as delivery_name,
IFNULL(M_PatientHP,'Belum ada WA pasien') as description,
'N' as chex,
'' as note,
'origin' as typeform,
'{$type}' as type,
M_DeliveryTypeCode as delivery_code
FROM m_patient
JOIN m_delivery ON M_DeliverySource = '{$type}' AND M_DeliveryIsActive = 'Y'
JOIN m_deliverytype ON M_DeliveryTypeCode = 'WHATSAPP' AND M_DeliveryM_DeliveryTypeID = M_DeliveryTypeID
WHERE
M_PatientID = {$id}
UNION
SELECT 0 as kelurahan,
0 as address_id,
M_DeliveryM_DeliveryTypeID as delivery_type,
M_DeliveryID as delivery_id,
M_DeliveryName as delivery_name,
IFNULL(M_PatientHP,'Belum ada telegram pasien') as description,
'N' as chex,
'' as note,
'origin' as typeform,
'{$type}' as type,
M_DeliveryTypeCode as delivery_code
FROM m_patient
JOIN m_delivery ON M_DeliverySource = '{$type}' AND M_DeliveryIsActive = 'Y'
JOIN m_deliverytype ON M_DeliveryTypeCode = 'TELEGRAM' AND M_DeliveryM_DeliveryTypeID = M_DeliveryTypeID
WHERE
M_PatientID = {$id}";
//echo $sql;
}
if($type == 'mou'){
//echo $id;
$sql = "SELECT * FROM m_mou WHERE M_MouID = {$id}";
//echo $sql;
$data_mou = $this->db_onedev->query($sql)->row_array();
//print_r($data_mou);
$sql = "
SELECT M_CompanyM_KelurahanID as kelurahan,
M_CompanyID as address_id,
M_DeliveryM_DeliveryTypeID as delivery_type,
M_DeliveryID as delivery_id,
M_DeliveryName as delivery_name,
CONCAT(M_CompanyAddress,' ',M_KelurahanName,', ',M_DistrictName,', ',M_CityName) as description,
'N' as chex,
'' as note,
'origin' as typeform,
'{$type}' as type,
M_DeliveryTypeCode as delivery_code
FROM m_company
JOIN m_delivery ON M_DeliverySource = 'COMPANY' AND M_DeliveryIsActive = 'Y'
JOIN m_deliverytype ON M_DeliveryTypeCode = 'ADDRESS' AND M_DeliveryM_DeliveryTypeID = M_DeliveryTypeID
JOIN m_kelurahan ON M_CompanyM_KelurahanID = M_KelurahanID
JOIN m_district ON M_KelurahanM_DistrictID = M_DistrictID
JOIN m_city ON M_DistrictM_CityID = M_CityID
WHERE
M_CompanyID = {$data_mou['M_MouM_CompanyID']}
UNION
SELECT 0 as kelurahan,
0 as address_id,
M_DeliveryM_DeliveryTypeID as delivery_type,
M_DeliveryID as delivery_id,
M_DeliveryName as delivery_name,
IFNULL(M_MouEmail,'Belum ada email agreement') as description,
IFNULL(M_MouEmailIsDefault,'N') as chex,
'' as note,
'origin' as typeform,
'{$type}' as type,
M_DeliveryTypeCode as delivery_code
FROM m_mou
JOIN m_delivery ON M_DeliverySource = '{$type}' AND M_DeliveryIsActive = 'Y'
JOIN m_deliverytype ON M_DeliveryTypeCode = 'EMAIL' AND M_DeliveryM_DeliveryTypeID = M_DeliveryTypeID
WHERE
M_MouID = {$id}";
//echo $sql;
}
if($type == 'doctor'){
$sql = "
SELECT M_DoctorAddressM_KelurahanID as kelurahan,
M_DoctorAddressID as address_id,
M_DeliveryM_DeliveryTypeID as delivery_type,
M_DeliveryID as delivery_id,
M_DeliveryName as delivery_name,
CONCAT(M_DoctorAddressDescription,' ',M_KelurahanName,', ',M_DistrictName,', ',M_CityName) as description,
M_DoctorAddressDeliveryDefault as chex,
'' as note,
'origin' as typeform,
'{$type}' as type,
M_DeliveryTypeCode as delivery_code
FROM m_doctor
JOIN m_doctoraddress ON M_DoctorAddressM_DoctorID = M_DoctorID AND M_DoctorAddressIsActive = 'Y'
JOIN m_kelurahan ON M_DoctorAddressM_KelurahanID = M_KelurahanID
JOIN m_district ON M_KelurahanM_DistrictID = M_DistrictID
JOIN m_city ON M_DistrictM_CityID = M_CityID
JOIN m_delivery ON M_DeliverySource = '{$type}' AND M_DeliveryIsActive = 'Y'
JOIN m_deliverytype ON M_DeliveryTypeCode = 'ADDRESS' AND M_DeliveryM_DeliveryTypeID = M_DeliveryTypeID
WHERE
M_DoctorID = {$id}
GROUP BY M_DoctorAddressID
UNION
SELECT 0 as kelurahan,
0 as address_id,
M_DeliveryM_DeliveryTypeID as delivery_type,
M_DeliveryID as delivery_id,
M_DeliveryName as delivery_name,
IF(ISNULL(M_DoctorEmail) OR M_DoctorEmail = '','Belum ada email pengirim',M_DoctorEmail) as description,
IF(M_DoctorEmailIsDefault = '','N',IFNULL(M_DoctorEmailIsDefault,'N')) as chex,
'' as note,
'origin' as typeform,
'{$type}' as type,
M_DeliveryTypeCode as delivery_code
FROM m_doctor
JOIN m_delivery ON M_DeliverySource = '{$type}' AND M_DeliveryIsActive = 'Y'
JOIN m_deliverytype ON M_DeliveryTypeCode = 'EMAIL' AND M_DeliveryM_DeliveryTypeID = M_DeliveryTypeID
WHERE
M_DoctorID = {$id}
UNION
SELECT 0 as kelurahan,
0 as address_id,
M_DeliveryM_DeliveryTypeID as delivery_type,
M_DeliveryID as delivery_id,
M_DeliveryName as delivery_name,
IF(ISNULL(M_DoctorHP) OR M_DoctorHP = '','Belum ada WA pengirim',M_DoctorHP) as description,
IF(M_DoctorEmailIsDefault = '','N',IFNULL(M_DoctorEmailIsDefault,'N')) as chex,
'' as note,
'origin' as typeform,
'{$type}' as type,
M_DeliveryTypeCode as delivery_code
FROM m_doctor
JOIN m_delivery ON M_DeliverySource = '{$type}' AND M_DeliveryIsActive = 'Y'
JOIN m_deliverytype ON M_DeliveryTypeCode = 'WHATSAPP' AND M_DeliveryM_DeliveryTypeID = M_DeliveryTypeID
WHERE
M_DoctorID = {$id}
UNION
SELECT 0 as kelurahan,
0 as address_id,
M_DeliveryM_DeliveryTypeID as delivery_type,
M_DeliveryID as delivery_id,
M_DeliveryName as delivery_name,
IF(ISNULL(M_DoctorHP) OR M_DoctorHP = '','Belum ada telegram pengirim',M_DoctorHP) as description,
IF(M_DoctorEmailIsDefault = '','N',IFNULL(M_DoctorEmailIsDefault,'N')) as chex,
'' as note,
'origin' as typeform,
'{$type}' as type,
M_DeliveryTypeCode as delivery_code
FROM m_doctor
JOIN m_delivery ON M_DeliverySource = '{$type}' AND M_DeliveryIsActive = 'Y'
JOIN m_deliverytype ON M_DeliveryTypeCode = 'TELEGRAM' AND M_DeliveryM_DeliveryTypeID = M_DeliveryTypeID
WHERE
M_DoctorID = {$id}";
}
//echo $sql;
$query = $this->db_onedev->query($sql);
if ($query) {
$rows= $query->result_array();
//$data = json_decode($rows->x);
if($rows){
foreach($rows as $k => $v){
$xval = $v['chex'] === 'N'?false:true;
//$rows[$k]['chex'] = $xval;
}
}
return $rows;
} else {
return array();
}
}
function load_preregister(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$tests = $row_header['Mcu_PreregisterDetailsTests'];
if($prm['id'] != -1){
$sql = "SELECT *
FROM mcu_preregister_patients
JOIN mcu_offline_prepare ON Mcu_PreregisterDetailsMcuOfflinePrepareID = McuOfflinePrepareID
WHERE
Mcu_PreregisterDetailsID = {$prm['id']}";
}
else{
$sql = "SELECT *
FROM mcu_offline_prepare
WHERE McuOfflinePrepareIsDownloaded = 'Y'
ORDER BY McuOfflinePrepareID DESC
LIMIT 1";
}
$row_header = $this->db_onedev->query($sql)->row_array();
$patient = array();
$sql = "SELECT 'N' divider,M_PatientID, M_PatientNoReg,M_PatientEmail,M_PatientPrefix,M_PatientSuffix,
concat(IFNULL(M_TitleName,''),' ',IFNULL(M_PatientPrefix,''),' ',M_PatientName,' ',IFNULL(M_PatientSuffix,'')) M_PatientName,
M_PatientName M_PatientRealName, M_TitleID, M_TitleName, M_SexID, M_SexName,
M_PatientHP, M_PatientPOB, M_PatientDOB, DATE_FORMAT(M_PatientDOB,'%d-%m-%Y') as dob_ina,
'' M_PatientAddress,
M_PatientAddressDescription, M_PatientM_IdTypeID, M_PatientIDNumber,
IFNULL(M_PatientNote, '') M_PatientNote, M_PatientPhoto, IF(M_PatientPhone IS NULL OR M_PatientPhone = '', M_PatientHP, M_PatientPhone) hp,
'' info,
M_PatientAddressM_KelurahanID M_KelurahanID, 0 M_DistrictID, 0 M_CityID, 0 M_ProvinceID, M_PatientM_ReligionID,
IFNULL(M_ReligionName, '-') M_ReligionName
from
m_patient
left join m_title on M_PatientM_TitleID = M_TitleID
join m_sex on M_PatientM_SexID = M_SexID
left join m_patientaddress on M_PatientAddressM_PatientID = M_PatientID and M_PatientAddressIsActive = 'Y'
left join m_religion on m_patientm_religionid = m_religionid
where M_PatientID = {$row_header['Mcu_PreregisterDetailsM_PatientID']}
group by M_PatientID";
//echo $sql;
$query = $this->db_onedev->query($sql);
if ($query && $prm['id'] != -1) {
$patient = $query->row_array();
if($patient['M_KelurahanID']){
$patient['M_PatientName'] = stripslashes($patient['M_PatientName']);
$sql = "SELECT *, concat('{$patient['M_PatientAddressDescription']}', '\n\n',
m_kelurahanname, ', ',
m_districtname,'\n',
m_cityname, ', ',
m_provincename) as xaddress
FROM m_kelurahan
JOIN m_district ON M_KelurahanM_DistrictID = M_DistrictID
JOIN m_city ON M_DistrictM_CityID = M_CityID
JOIN m_province ON M_CityM_ProvinceID = M_ProvinceID
WHERE
M_KelurahanID = {$patient['M_KelurahanID']} ";
//echo $sql;
$row_address = $this->db_onedev->query($sql)->row_array();
$patient['M_PatientAddress'] = stripslashes($row_address['xaddress']);
$patient['M_DistrictID'] = $row_address['M_DistrictID'];
$patient['M_CityID'] = $row_address['M_CityID'];
$patient['M_ProvinceID'] = $row_address['M_ProvinceID'];
$info = $this->db_onedev->query("SELECT fn_fo_patient_visit(?) info", [$patient['M_PatientID']])->row();
$patient['info'] = json_decode($info->info);
$rst['patient'] = $patient ;
}else{
$patient['M_PatientAddress'] = '';
$patient['M_DistrictID'] = 0;
$patient['M_CityID'] = 0;
$patient['M_ProvinceID'] = 0;
$info = $this->db_onedev->query("SELECT fn_fo_patient_visit(?) info", [$patient['M_PatientID']])->row();
$patient['info'] = json_decode($info->info);
$rst['patient'] = $patient ;
}
}
else{
$rst['patient'] = array() ;
}
if($row_header['Mcu_PreregisterDetailsAgreement'] == ''){
$sql = "SELECT M_MouStatus,M_MouEmail, M_MouEmailIsDefault, M_MouEndDate, M_MouID, M_MouIsBill, M_MouIsDefault, M_MouName, M_MouNote, M_MouStartDate, '' delivery_email_code
FROM m_mou
WHERE
M_MouID = {$row_header['McuOfflinePrepareM_MouDefaultID']}";
}
else{
$sql = "SELECT M_MouStatus,M_MouEmail, M_MouEmailIsDefault, M_MouEndDate, M_MouID, M_MouIsBill, M_MouIsDefault, M_MouName, M_MouNote, M_MouStartDate, '' delivery_email_code
FROM m_mou
WHERE
M_MouNumber = '{$row_header['Mcu_PreregisterDetailsAgreement']}'";
}
//echo $sql;
$row_mou = $this->db_onedev->query($sql)->row_array();
$rst['data_deliveries'] = array();
$new_delivery = array();
$new_delivery = $this->search_deliveries(array('id'=>$row_header['Mcu_PreregisterDetailsM_PatientID'],'type'=>'patient'));
if(count($new_delivery) > 0){
foreach($new_delivery as $knd => $vnd){
array_push($rst['data_deliveries'],$vnd);
}
}
/*data doctor*/
$sql = "SELECT M_DoctorID, M_DoctorIsDefault, 'N' M_DoctorIsPJ,
CONCAT('[ ',M_DoctorCode,' ] ',M_DoctorPrefix, M_DoctorPrefix2,' ',M_DoctorName,' ',M_DoctorSufix, M_DoctorSufix2, M_DoctorSufix3) as M_DoctorName, fn_fo_delivery_code('DOCTOR', 'EMAIL', 0) as delivery_email_code,
IF(M_DoctorEmail IS NULL OR M_DoctorEmail = '', 'N', M_DoctorEmailIsDefault) email_default,
IFNULL( concat('[', group_concat(JSON_OBJECT('description',M_DoctorAddressNote,'M_DoctorAddressDescription', M_DoctorAddressDescription, 'M_DoctorAddressID', M_DoctorAddressID, 'delivery_default', M_DoctorAddressDeliveryDefault, 'delivery_code', fn_fo_delivery_code('DOCTOR', 'ADDRESS', M_DoctorAddressID)) SEPARATOR ','), ']'), '[]') as address,
M_DoctorNote
FROM mcu_offline_prepare
JOIN m_doctor ON McuOfflinePrepareM_DoctorDefaultID = M_DoctorID AND M_DoctorIsActive = 'Y'
JOIN m_doctoraddress ON M_DoctorAddressM_DoctorID = M_DoctorID AND M_DoctorAddressIsActive = 'Y'
WHERE
McuOfflinePrepareID = {$row_header['McuOfflinePrepareID']}
group by M_DoctorID";
//echo $sql;
$v = $this->db_onedev->query($sql)->row_array();
if($v){
$v['address'] = json_decode($v['address']);
$rst['selected_doctor'] = $v;
$sql = "SELECT M_DoctorAddressDescription, M_DoctorAddressID, M_DoctorAddressDeliveryDefault delivery_default, fn_fo_delivery_code('DOCTOR', 'ADDRESS', M_DoctorAddressID) delivery_code
FROM mcu_offline_prepare
JOIN m_doctor ON McuOfflinePrepareM_DoctorDefaultID = M_DoctorID AND M_DoctorIsActive = 'Y'
JOIN m_doctoraddress ON McuOfflinePrepareM_DoctorAddressDefaultID = M_DoctorAddressID AND M_DoctorAddressIsActive = 'Y'
WHERE
McuOfflinePrepareID = {$row_header['McuOfflinePrepareID']}";
$raddr = $this->db_onedev->query($sql)->row_array();
foreach($v['address'] as $kaddr => $vaddr){
$v['address'][$kaddr]->description = $v['address'][$kaddr]->M_DoctorAddressNote." : ".$v['address'][$kaddr]->M_DoctorAddressDescription;
if(intval($vaddr->M_DoctorAddressID) == intval($raddr['M_DoctorAddressID']))
$rst['selected_address'] = $v['address'][$kaddr];
}
//$rst['selected_address'] = $vaddr;
}
if(!$rst['selected_address']){
$rst['selected_address'] = $rst['address_doctor'][0];
}
$new_delivery = array();
$new_delivery = $this->search_deliveries(array('id'=>$rst['selected_doctor']['M_DoctorID'],'type'=>'doctor'));
if(count($new_delivery) > 0){
foreach($new_delivery as $knd => $vnd){
array_push($rst['data_deliveries'],$vnd);
}
}
/*data company mou*/
$sql = "SELECT m_company.*, '' mou
FROM m_company
WHERE
M_CompanyID = {$row_header['McuOfflinePrepareM_CompanyID']}";
$row_company = $this->db_onedev->query($sql)->row_array();
$sql = "SELECT M_MouStatus,M_MouEmail, M_MouEmailIsDefault, M_MouEndDate, M_MouID, M_MouIsBill, M_MouIsDefault, M_MouName, M_MouNote, M_MouStartDate, '' delivery_email_code
FROM m_mou
WHERE
M_MouM_CompanyID = {$row_company['M_CompanyID']} AND M_MouStatus = 'R' AND M_MouIsActive = 'Y'";
//echo $sql;
$row_company['mou'] = $this->db_onedev->query($sql)->result_array();
//print_r($row_company);
$rst['selected_company'] = $row_company;
$rst['selected_mou'] = $row_mou;
$rst['companies'] = array();
array_push($rst['companies'],$rst['selected_company']);
$new_delivery = array();
//echo $rst['selected_mou']['M_MouID'];
$new_delivery = $this->search_deliveries(array('id'=>$row_mou['M_MouID'],'type'=>'mou'));
if(count($new_delivery) > 0){
foreach($new_delivery as $knd => $vnd){
array_push($rst['data_deliveries'],$vnd);
}
}
//echo 'dsada';
foreach($rst['data_deliveries'] as $k_ordev => $v_ordev){
//echo $row_header['McuOfflinePrepareDeliveries'];
$check_centang_dev = $this->checked_delivery($row_header['McuOfflinePrepareDeliveries'],$v_ordev);
if($check_centang_dev){
$rst['data_deliveries'][$k_ordev] = $check_centang_dev ;
}
}
$rst['tests'] = array();
$explode_tests = explode(',',$row_header['Mcu_PreregisterDetailsTests']);
if(!empty($explode_tests)){
//print_r($explode_tests);
foreach($explode_tests as $ktest => $vtest){
$r_test = $this->search_test_by_code(trim($vtest),$row_mou['M_MouID']);
if($r_test){
array_push($rst['tests'],$r_test);
}
}
}
$xrst = array('status'=>$status,'data'=>$rst);
$result = array("records" => $rst);
$this->sys_ok($result);
exit;
}
function checked_delivery($prepare_deliveries,$delivery){
$rtn = false;
//print_r($delivery);
//print_r($prepare_deliveries);
$prepare_deliveries = preg_replace("/\\n/", "___n___", $prepare_deliveries);
$deliveries = json_decode($prepare_deliveries);
//print_r($deliveries);
foreach($deliveries as $k => $v){
if((intval($v->kelurahan) == intval($delivery['kelurahan'])) && (intval($v->address_id) == intval($delivery['address_id'])) && ($v->delivery_type == $delivery['delivery_type']) && (intval($v->delivery_id) == intval($delivery['delivery_id'])) && $v->chex == 'Y' ){
$rtn = $v;
}
}
return $rtn;
}
function search_test_by_code($xcode,$mouid)
{
$is_packet = 'N';
$px_type = 'PX';
if(substr($xcode, 0, 2) == 'PN' || substr($xcode, 0, 2) == 'PR'){
$is_packet = 'Y';
$px_type = substr($xcode, 0, 2);
$sql = "SELECT T_PacketID as T_TestID FROM t_packet WHERE T_PacketSasCode = '{$xcode}' AND T_PacketIsActive = 'Y'";
}
else{
$sql = "SELECT T_TestID FROM t_test WHERE T_TestSasCode = '{$xcode}' AND T_TestIsActive = 'Y'";
}
//echo $sql;
$xrow_test = $this->db_smartone->query($sql)->row_array();
$sql = "select *
from
ss_price_mou
where
Ss_PriceMouM_MouID = {$mouid} AND
T_TestID = {$xrow_test['T_TestID']} AND
px_type = '{$px_type}' AND
is_packet = '{$is_packet}' AND
T_PriceIsCito <> 'Y';";
// echo $sql;
$query = $this->db_smartone->query($sql);
if ($query)
{
$row = $query->row_array();
$id_to_remove = [];
// var_dump($rows);
$row['requirement'] = [];
if($row['px_type'] == "PX"){
$x = $this->db_smartone->query("SELECT fn_fo_requirement_get('{$row['T_TestID']}') x")
->row();
if ($x->x != null)
$row['requirement'] = json_decode($x->x);
}
//print_r($row);
// Janji Hasil per PX
//$x = $this->db_smartone->query("SELECT fn_fo_find_promise_by_one_px('{$row['T_TestID']}') x")->row();
if($row['is_packet'] == 'N'){
$tests = $row['T_PriceT_TestID'];
$panels = '';
}
else{
$tests = '';
$panels = $row['T_PriceT_TestID'];
}
$sql_param = array($tests, $panels);
$sql = "select fn_fo_find_promise_by_px(?, ?) as x";
$x = $this->db_smartone->query($sql, $sql_param)->row();
if ($x->x != null)
$row['promise'] = $x->x;
$row['nat_test'] = json_decode($row['nat_test']);
$row['child_test'] = json_decode($row['child_test']);
// IF PROFILE
if ($row['px_type'] == "PR" || $row['px_type'] == "PXR") {
foreach ($row['child_test'] as $l => $w) {
$row['child_test'][$l]->requirement = [];
$row['child_test'][$l]->nat_test = json_decode($w->nat_test);
$row['child_test'][$l]->promise = null;
//echo "SELECT fn_fo_requirement_get('{$w->T_TestID}')";
$x = $this->db_smartone->query("SELECT fn_fo_requirement_get('{$w->T_TestID}') x")->row();
if ($x->x != null)
$row['child_test'][$l]->requirement = json_decode($x->x);
// Janji Hasil per PX
$x = $this->db_smartone->query("SELECT fn_fo_find_promise_by_one_px('{$w->T_TestID}') x")
->row();
if ($x->x != null)
$row['child_test'][$l]->promise = $x->x;
}
}
return $row;
}
}
}