Files
BE_IBL/application/controllers/mockup/fo/registration_future/Order.php--040920
2026-04-15 15:24:34 +07:00

920 lines
35 KiB
Plaintext

<?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'];
$header_json = addslashes(str_replace('\n', '\\\n', json_encode($hdr)));
$detail_json = str_replace('\n', '\\\n', json_encode($prm['detail']));
$delivery_json = addslashes(str_replace('\n', '\\\n', json_encode($prm['delivery'])));
$req_json = json_encode($prm['req']);
$sql = "CALL sp_fo_register_save_v5('', '{$header_json}', '{$delivery_json}', '{$detail_json}', '{$req_json}', '{$this->sys_user['M_UserID']}');";
//echo $sql;
$query = $this->db_smartone->query($sql);
if ($query)
{
$rst = $query->row();
$this->clean_mysqli_connection($this->db_smartone->conn_id);
$rst_id = $rst->id;
$rst->data = json_decode($rst->data);
$rst_id = $rst->data->id;
if($rst_id){
$sql = "UPDATE t_orderheaderaddon
SET T_OrderHeaderAddOnAliasDoctorName = '{$hdr['alias_doctor']}',
T_OrderHeaderAddOnAliasDoctorAddress = '{$hdr['alias_doctor_address']}'
WHERE
T_OrderHeaderAddOnT_OrderHeaderID = {$rst_id}";
$this->db_smartone->query($sql);
$delivery_note = $prm['delivery'];
foreach($delivery_note as $k => $v){
//print_r($v);
if($v['noteplus'] != ''){
if($v['delivery_code'] == 'EMAIL' || $v['delivery_code'] == 'WHATSAPP' || $v['delivery_code'] == 'TELEGRAM'){
$sql = "UPDATE t_orderdelivery SET T_OrderDeliveryDestination = '{$v['noteplus']}'
WHERE
T_OrderDeliveryT_OrderHeaderID = {$rst_id} AND
T_OrderDeliveryM_DeliveryID = {$v['delivery_id']} AND
T_OrderDeliveryM_DeliveryTypeID = {$v['delivery_type_id']} AND
T_OrderDeliveryAddressID = {$v['address_id']} AND
T_OrderDeliveryIsActive = 'Y'";
$this->db_smartone->query($sql);
//echo $sql;
}
else{
$sql = "SELECT T_OrderDeliveryID as xdel_id
FROM t_orderdelivery
WHERE
T_OrderDeliveryT_OrderHeaderID = {$rst_id} AND
T_OrderDeliveryM_DeliveryID = {$v['delivery_id']} AND
T_OrderDeliveryM_DeliveryTypeID = {$v['delivery_type_id']} AND
T_OrderDeliveryAddressID = {$v['address_id']} AND
T_OrderDeliveryIsActive = 'Y'";
//echo $sql;
$xdel_id = $this->db_smartone->query($sql)->row()->xdel_id;
$sql = "INSERT INTO t_orderdeliverynote(
T_OrderDeliveryNoteT_OrderDeliveryID,
T_OrderDeliveryNoteValue,
T_OrderDeliveryNoteCreated,
T_OrderDeliveryNoteUserID
)
VALUES(
{$xdel_id},
'{$v['noteplus']}',
NOW(),
{$this->sys_user['M_UserID']}
)";
$this->db_smartone->query($sql);
//echo $sql;
}
}
}
$sql = "SELECT *
FROM t_orderheader
JOIN m_patient ON M_PatientID = T_OrderHeaderM_PatientID
WHERE
T_OrderHeaderID = {$rst_id}";
//echo $sql;
$x_header = $this->db_smartone->query($sql)->row_array();
$sql = "SELECT *
FROM t_orderdetail
WHERE
T_OrderDetailT_OrderHeaderID = {$rst_id} AND
T_OrderDetailT_TestIsPrice = 'Y' AND
T_OrderDetailIsActive = 'Y'";
$x_details = $this->db_smartone->query($sql)->result_array();
$x_details = json_encode($x_details);
$x_deliveries = json_encode($prm['delivery']);
$sql = "SELECT *
FROM t_orderpromise
WHERE
T_OrderPromiseT_OrderHeaderID = {$rst_id} AND T_OrderPromiseIsActive = 'Y'";
$x_promises = $this->db_smartone->query($sql)->result_array();
$x_promises = json_encode($x_promises);
$sql = "INSERT INTO order_log(
OrderLogT_OrderHeaderID,
OrderLogM_PatientDOB,
OrderLogM_CompanyID,
OrderLogM_MouID,
OrderLogM_DoctorSenderID,
OrderLogM_DoctorSenderAddressID,
orderLogT_OrderHeaderAddOnAliasDoctorName,
orderLogT_OrderHeaderAddOnAliasDoctorAddress,
OrderLogAge,
OrderLogFoNote,
OrderLogSubtotal,
OrderLogTotal,
OrderLogUserID,
OrderLogDetails,
OrderLogDeliveries,
OrderLogPromises
)
VALUES(
{$rst_id},
'{$x_header['M_PatientDOB']}',
{$x_header['T_OrderHeaderM_CompanyID']},
{$x_header['T_OrderHeaderM_MouID']},
{$x_header['T_OrderHeaderSenderM_DoctorID']},
{$x_header['T_OrderHeaderSenderM_DoctorAddressID']},
'{$hdr['alias_doctor']}',
'{$hdr['alias_doctor_address']}',
'{$x_header['T_OrderHeaderM_PatientAge']}',
'{$x_header['T_OrderHeaderFoNote']}',
'{$x_header['T_OrderHeaderSubTotal']}',
'{$x_header['T_OrderHeaderTotal']}',
{$this->sys_user['M_UserID']},
'{$x_details}',
'{$x_deliveries}',
'{$x_promises}'
)";
//echo $sql;
$this->db_smartone->query($sql);
if(isset($prm['order_id']) && intval($prm['order_id']) > 0){
$sql = "UPDATE t_orderpromise SET
T_OrderPromiseIsActive = 'N'
WHERE
T_OrderPromiseT_OrderHeaderID = {$rst_id}";
$this->db_smartone->query($sql);
$sql = "SELECT *
FROM mcu_preregister_patients
JOIN mcu_offline_prepare ON Mcu_PreregisterDetailsMcuOfflinePrepareID = McuOfflinePrepareID
WHERE
Mcu_PreregisterDetailsID = {$prm['order_id']}";
$row_new_promise = $this->db_smartone->query($sql)->row_array();
$new_promise = $row_new_promise['McuOfflinePreparePromiseDate'].' '.$row_new_promise['McuOfflinePreparePromiseTime'];
$sql = "INSERT INTO t_orderpromise (
T_OrderPromiseT_OrderHeaderID,
T_OrderPromiseDateTime
)
VALUES(
{$rst_id},
'{$new_promise}'
)";
$this->db_smartone->query($sql);
$last_promise_id = $this->db_smartone->insert_id();
$sql = "UPDATE mcu_preregister_patients SET
Mcu_PreregisterDetailsT_OrderHeaderID = {$rst_id}
WHERE
Mcu_PreregisterDetailsID = {$row_new_promise['Mcu_PreregisterDetailsID']}";
$this->db_smartone->query($sql);
$sql = "UPDATE t_orderdetail SET T_OrderDetailT_OrderPromiseID = {$last_promise_id}
WHERE
T_OrderDetailT_OrderHeaderID = {$rst_id}";
$this->db_smartone->query($sql);
}
}
$x = preg_match("/janji hasi/", $rst->message);
if ($x)
$rst->message = "Masih ada pemeriksaan yang BELUM DISET JANJI HASILNYA !";
$rst->message = ["text"=>$rst->message, "query"=>$this->db_smartone->last_query()];
// pe : add broadcast notification
$rst->order_header = $this->get_header($rst_id);
$rst->order_detail = $this->get_details($rst_id);
$rst->order_delivery = $this->get_delivery($rst_id);
$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 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}";
}
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);
}
}
foreach($rst['data_deliveries'] as $k_ordev => $v_ordev){
$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);
$deliveries = json_decode($prepare_deliveries);
//print_r($deliveries);
foreach($deliveries as $k => $v){
if(($v->kelurahan == $delivery['kelurahan']) && ($v->address_id == $delivery['address_id']) && ($v->delivery_type == $delivery['delivery_type']) && ($v->delivery_id == $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;
}
}
}