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

772 lines
30 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'];
$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_v8('', '{$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);
}
$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;
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'
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(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 join m_title on M_PatientM_TitleID = M_TitleID
join m_sex on M_PatientM_SexID = M_SexID
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();
$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{
$rst['patient'] = 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_MouID = {$row_header['McuOfflinePrepareM_MouDefaultID']}";
$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('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){
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'=>$rst['selected_mou']['M_MouID'],'type'=>'mou'));
if(count($new_delivery) > 0){
foreach($new_delivery as $knd => $vnd){
array_push($rst['data_deliveries'],$vnd);
}
}
/*data deliveries*/
/*$sql = "SELECT *
FROM t_orderdelivery
JOIN m_deliverytype ON T_OrderDeliveryM_DeliveryTypeID = M_DeliveryTypeID
JOIN m_delivery ON T_OrderDeliveryM_DeliveryID = M_DeliveryID
WHERE
T_OrderDeliveryT_OrderHeaderID = {$row_header['T_OrderHeaderID']} AND T_OrderDeliveryIsActive = 'Y'";
$order_deliveries = $this->db_onedev->query($sql)->result_array();
foreach($rst['data_deliveries'] as $kd => $vd){
$check_exist = $this->exist_delivery($order_deliveries,$vd);
//print_r($check_exist);
if($check_exist['idx'] != -1){
$rst['data_deliveries'][$kd]['chex'] = 'Y';
$idx = $check_exist['idx'];
if($vd['delivery_code'] == 'PICKUP' || $vd['delivery_code'] == 'ADDRESS'){
$sql = "SELECT *
FROM t_orderdeliverynote
WHERE
T_OrderDeliveryNoteT_OrderDeliveryID = {$order_deliveries[$idx]['T_OrderDeliveryID']} AND
T_OrderDeliveryNoteIsActive = 'Y'";
//echo $sql;
$dt_note = $this->db_onedev->query($sql)->row_array();
if($dt_note){
$rst['data_deliveries'][$kd]['note'] = $dt_note['T_OrderDeliveryNoteValue'];
}
}
if($vd['delivery_code'] !== 'PICKUP' && $vd['delivery_code'] != 'ADDRESS'){
if($vd['description'] != $order_deliveries[$idx]['T_OrderDeliveryDestination']){
//echo 'YYYYYYYYY';
$rst['data_deliveries'][$kd]['note'] = $order_deliveries[$idx]['T_OrderDeliveryDestination'];
}
}
}
}
*/
$xrst = array('status'=>$status,'data'=>$rst);
$result = array("records" => $rst);
$this->sys_ok($result);
exit;
}
}