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

2190 lines
96 KiB
PHP

<?php
/*
alter table mcu_map_order
add key(Mcu_MapOrderT_OrderHeaderID),
add key(Mcu_MapOrderT_SampleTypeID),
add key(Mcu_MapOrderT_OrderHeaderNewID);
*/
class Mcu_proses_upload extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
function dump_data($id)
{
$sql = "select * from mcu_offline_upload_v2
where McuOfflineUploadID = ?";
$qry = $this->db->query($sql, [$id]);
if (!$qry) {
echo $this->log(
"\nError : " .
$this->db->error()["message"] .
"|\n" .
$this->db->last_query()
);
exit();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
echo "No Data";
exit();
}
$full_filename = $rows[0]["McuOfflineUploadFullName"];
echo "Full File : \n$full_filename\n";
$raw = gzdecode(file_get_contents($full_filename));
$j_data = json_decode($raw, true);
if (!$j_data) {
$this->log("Corrupt Json File : " . json_last_error_msg());
exit();
}
print_r($j_data);
}
public function index()
{
$target_db = "one";
$this->db->trans_begin();
$WAIT_BETWEEN_ORDER = 3;
$sql = "select *,
TIMESTAMPDIFF(HOUR,McuOfflineUploadCreated,now()) hour_diff
from mcu_offline_upload_v2
where McuOfflineUploadStatus = 'R'
order by McuOfflineUploadCreated desc
limit 0,1";
$rows = $this->get_rows($sql, [], "Check If Process Running");
if ($rows === false) {
exit();
} elseif (count($rows) > 0) {
$hour_diff = $rows[0]["hour_diff"];
if ($hour_diff <= 1) {
$code = $rows[0]["McuOfflineUploadMcuOfflinePrepareCode"];
$filename = $rows[0]["McuOfflineUploadName"];
$created = $rows[0]["McuOfflineUploadCreated"];
$this->log(
"\nMcu Upload Process still processing $code : $filename at $created ( $hour_diff )"
);
exit();
}
}
$sql = "select * from mcu_offline_upload_v2
where McuOfflineUploadStatus = 'N'
order by McuOfflineUploadStatus";
$qry = $this->db->query($sql);
if (!$qry) {
echo $this->log(
"\nError : " .
$this->db->error()["message"] .
"|\n" .
$this->db->last_query()
);
exit();
}
$rows = $qry->result_array();
$this->log("\nStart Mcu Process Upload " . count($rows) . " files.");
foreach ($rows as $r) {
$uploadID = $r["McuOfflineUploadID"];
$code = $r["McuOfflineUploadMcuOfflinePrepareCode"];
$filename = $r["McuOfflineUploadName"];
$full_filename = $r["McuOfflineUploadFullName"];
//update McuOfflineUploadStatus to R
$this->db->where("McuOfflineUploadID", $uploadID);
$upload_data = [
"McuOfflineUploadStatus" => "R",
"McuOfflineUploadCreated" => date("Y-m-d H:i:s"),
];
$this->db->update("mcu_offline_upload_v2", $upload_data);
$this->db->trans_commit();
$this->db->trans_begin();
$this->log("Processing : MCU $code : $filename");
$mcu = $this->get_mcu_by_code($code);
if (!$mcu) {
continue;
}
// DeCompress file
$raw = gzdecode(file_get_contents($full_filename));
$j_data = json_decode($raw, true);
if (!$j_data) {
$this->log("Corrupt Json File : " . json_last_error_msg());
continue;
}
//$mcu_session_code = date("YmdHis") . "-" . $code;
$mcu_session_code = basename($filename);
$x_id = $j_data["mcu_offline_prepare"]["McuOfflinePrepareID"];
$mcu_session_id = $this->create_mcu_session(
$x_id,
$mcu_session_code
);
if (!$mcu_session_id) {
$this->log("Session ID is blank $mcu_session_id");
continue;
}
$this->log(
"Session ID : $mcu_session_id => " .
count($j_data["order"]) .
" order."
);
//comit the session creation
$this->db->trans_commit();
$this->db->trans_begin();
$is_partial = false;
$total_order = count($j_data["order"]);
$total_order_imported = 0;
//Get Max Upload
//gak jadi di pakai
/*
$sql = "select max(Mcu_MapOrderDate) maxDate
from mcu_map_order";
$rows = $this->get_rows($sql,[],"Get MaxMapOrderDate");
$maxMapOrderDate = "";
if($rows && count($rows) > 0) {
$maxMapOrderDate = $rows[0]["maxDate"];
}
*/
// Per T_OrderHeader
foreach ($j_data["order"] as $j_order) {
//Generate Order
$order = array_filter(
$j_order,
function ($k) {
if (substr($k, 0, 13) == "T_OrderHeader") {
return true;
}
return false;
},
ARRAY_FILTER_USE_KEY
);
$this->log(
"\tStart Import : " . $order["T_OrderHeaderLabNumber"]
);
$mcu_T_OrderHeaderID = $order["T_OrderHeaderID"];
unset($order["T_OrderHeaderID"]);
$sql = "SELECT T_OrderHeaderID, T_OrderHeaderM_PatientID
FROM {$target_db}.mcu_map_order
JOIN {$target_db}.t_orderheader one_order ON one_order.T_OrderHeaderID = Mcu_MapOrderT_OrderHeaderNewID
WHERE
Mcu_MapOrderT_OrderHeaderID = ?";
$rows = $this->get_rows(
$sql,
[$mcu_T_OrderHeaderID],
"Get From Map Order"
);
if ($rows === false) {
continue;
}
$flag_existing_order = false;
if (count($rows) == 0) {
$qry = $this->db->insert("{$target_db}.t_orderheader", $order);
if (!$qry) {
$this->log(
"\tError Insert t_orderheader " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
continue;
}
$new_T_OrderHeaderID = $this->db->insert_id();
if ($new_T_OrderHeaderID > 0) {
// Get | Update M_Patient
$oldPatientID = $order["T_OrderHeaderM_PatientID"];
$new_M_PatientID = 0;
if (
true ||
$oldPatientID >
$j_data["last_master"]["M_PatientID"]
) {
$j_patient_data = array_filter(
$j_data["patient"],
function ($v, $k) use ($oldPatientID) {
if ($v["M_PatientID"] == $oldPatientID) {
return true;
}
},
ARRAY_FILTER_USE_BOTH
);
$have_patient_data = false;
$patient_data = [];
foreach ($j_patient_data as $pat) {
$patient_data = $pat;
$have_patient_data = true;
}
if ($have_patient_data) {
$x_patient_data = $patient_data;
unset($patient_data["address"]);
unset($patient_data["M_PatientID"]);
$this->log(
"\tNew Patient : " .
$patient_data["M_PatientName"]
);
$qry = $this->db->insert(
"{$target_db}.m_patient",
$patient_data
);
if (!$qry) {
$this->log(
"\tError Insert New Patient " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
continue;
}
$new_M_PatientID = $this->db->insert_id();
$have_address_error = false;
foreach (
$x_patient_data["address"]
as $address
) {
unset($address["M_PatientAddressID"]);
$address[
"M_PatientAddressM_PatientID"
] = $new_M_PatientID;
$qry = $this->db->insert(
"{$target_db}.m_patientaddress",
$address
);
if (!$qry) {
$this->log(
"\tError Insert New Patient Address " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$is_partial = true;
$have_address_error = true;
break;
}
}
if ($have_address_error) {
continue;
}
}
} else {
$j_patient_data = array_filter(
$j_data["patient"],
function ($v, $k) use ($oldPatientID) {
if ($v["M_PatientID"] == $oldPatientID) {
return true;
}
},
ARRAY_FILTER_USE_BOTH
);
$have_patient_data = false;
$patient_data = [];
foreach ($j_patient_data as $pat) {
$patient_data = $pat;
$have_patient_data = true;
}
if ($have_patient_data) {
unset($patient_data["address"]);
unset($patient_data["M_PatientID"]);
$this->log(
"\tUpdate Patient: " .
$patient_data["M_PatientName"]
);
$this->db->where("M_PatientID", $oldPatientID);
$this->db->set($patient_data);
$qry = $this->db->update("{$target_db}.m_patient");
if (!$qry) {
$this->log(
"\tError Update Patient" .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
continue;
}
}
}
$status = $this->update_header(
$new_T_OrderHeaderID,
$order["T_OrderHeaderLabNumber"],
$order["T_OrderHeaderLabNumberExt"],
$new_M_PatientID
);
if (!$status) {
$this->db->trans_rollback();
$this->db->trans_begin();
continue;
}
//Add t_orderheaderaddon
$sql = "DELETE FROM {$target_db}.t_orderheaderaddon
WHERE T_OrderHeaderAddOnT_OrderHeaderID = ?";
if (
!$this->exec_query(
$sql,
"\tDelete T_OrderHeaderAddOn",
[$new_T_OrderHeaderID]
)
) {
$this->db->trans_rollback();
$this->db->trans_begin();
$is_partial = true;
continue;
}
$have_addon_error = false;
foreach ($j_order["addon"] as $addon) {
unset($addon["T_OrderHeaderAddOnID"]);
$addon[
"T_OrderHeaderAddOnT_OrderHeaderID"
] = $new_T_OrderHeaderID;
$qry = $this->db->insert(
"{$target_db}.t_orderheaderaddon",
$addon
);
if (!$qry) {
$this->log(
"\tError Insert t_orderheaderaddon " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_addon_error = true;
break;
}
}
if ($have_addon_error) {
continue;
}
//TODO : NEW
//Add t_orderreq
$have_orderreq_error = false;
foreach ($j_order["t_orderreq"] as $o_req) {
unset($o_req["T_OrderReqID"]);
$o_req[
"T_OrderReqT_OrderHeaderID"
] = $new_T_OrderHeaderID;
$qry = $this->db->insert("{$target_db}.t_orderreq", $o_req);
if (!$qry) {
$this->log(
"\tError Insert t_orderreq" .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_orderreq_error = true;
break;
}
}
if ($have_orderreq_error) {
continue;
}
// fo_status
$sql = "DELETE FROM {$target_db}.fo_status
WHERE Fo_StatusT_OrderHeaderID = ?";
if (
!$this->exec_query($sql, "\tDelete fo_status", [
$new_T_OrderHeaderID,
])
) {
$this->db->trans_rollback();
$this->db->trans_begin();
continue;
}
$have_fo_status_error = false;
foreach ($j_order["fo_status"] as $fo_status) {
unset($fo_status["Fo_StatusID"]);
$fo_status[
"Fo_StatusT_OrderHeaderID"
] = $new_T_OrderHeaderID;
$qry = $this->db->insert(
"{$target_db}.fo_status",
$fo_status
);
if (!$qry) {
$this->log(
"\tError Insert fo_status " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_fo_status_error = true;
break;
}
}
if ($have_fo_status_error) {
continue;
}
// TODO UPDATE T_OrderDetailT_PromiseID
// order promise
$have_promise_error = false;
$map_promise = [];
foreach ($j_order["t_orderpromise"] as $detail) {
$old_promise_id = $detail["T_OrderPromiseID"];
unset($detail["T_OrderPromiseID"]);
$detail[
"T_OrderPromiseT_OrderHeaderID"
] = $new_T_OrderHeaderID;
$qry = $this->db->insert(
"{$target_db}.t_orderpromise",
$detail
);
if (!$qry) {
$this->log(
"\tError Insert t_orderpromise " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_promise_error = true;
break;
}
$map_promise[
$old_promise_id
] = $this->db->insert_id();
}
if ($have_promise_error) {
continue;
}
//order detail
$have_detail_error = false;
$map_order_detail = [];
foreach ($j_order["detail"] as $j_detail) {
$detail = array_filter(
$j_detail,
function ($k) {
if (substr($k, 0, 13) == "T_OrderDetail") {
return true;
}
return false;
},
ARRAY_FILTER_USE_KEY
);
$old_T_OrderDetailID = $detail["T_OrderDetailID"];
unset($detail["T_OrderDetailID"]);
//TODO :
$sql = "select T_OrderDetailID
from t_orderdetail
where T_OrderDetailT_OrderHeaderID = ?
and T_OrderDetailT_TestID = ?";
$check_rows = $this->get_rows(
$sql,
[
$new_T_OrderHeaderID,
$detail["T_OrderDetailT_TestID"],
],
"Check Detail"
);
if ($check_rows) {
if (count($check_rows) > 0) {
$this->log(
"Order Detail Already Exits : " .
$detail["T_OrderDetailT_TestName"]
);
continue;
}
} else {
//continue;
}
$detail[
"T_OrderDetailT_OrderHeaderID"
] = $new_T_OrderHeaderID;
if ($detail["T_OrderDetailT_OrderPromiseID"] > 0) {
$x_promise_id =
$detail["T_OrderDetailT_OrderPromiseID"];
if (isset($map_promise[$x_promise_id])) {
$detail["T_OrderDetailT_OrderPromiseID"] =
$map_promise[$x_promise_id];
}
}
$qry = $this->db->insert(
"{$target_db}.t_orderdetail",
$detail
);
if (!$qry) {
$this->log(
"\tError Insert t_orderdetail " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_detail_error = true;
break;
}
$new_T_OrderDetailID = $this->db->insert_id();
$map_order_detail[
$old_T_OrderDetailID
] = $new_T_OrderDetailID;
//addon
//tidak usah di naikan karena sudah di trigger
//yg di bawah data non esential
if (false) {
$j_addon = $j_detail["addon"];
foreach ($j_addon as $addon) {
unset($addon["T_OrderDetailAddOnID"]);
$addon[
"T_OrderDetailAddOnT_OrderDetailID"
] = $new_T_OrderDetailID;
$sql = "select T_OrderDetailAddOnID from t_orderdetailaddon where T_OrderDetailAddOnT_OrderDetailID = ?
and T_OrderDetailAddOnIsActive = 'Y'";
$rows = $this->get_rows($sql, [
$new_T_OrderDetailID,
],"Check TOrderDetailAddOn");
if (count($rows) == 0) {
$qry = $this->db->insert(
"{$target_db}.t_orderdetailaddon",
$addon
);
if (!$qry) {
$this->log(
"\tError Insert t_orderdetailaddon " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_detail_error = true;
break;
}
}
}
}
if ($have_detail_error) {
break;
}
}
if ($have_detail_error) {
continue;
}
//delivery
$have_delivery_error = false;
foreach ($j_order["t_orderdelivery"] as $delivery) {
$old_T_OrderDeliveryID =
$delivery["T_OrderDeliveryID"];
unset($delivery["T_OrderDeliveryID"]);
$delivery[
"T_OrderDeliveryT_OrderHeaderID"
] = $new_T_OrderHeaderID;
$qry = $this->db->insert(
"{$target_db}.t_orderdelivery",
$delivery
);
if (!$qry) {
$this->log(
"\tError Insert t_orderdelivery " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_delivery_error = true;
break;
}
$new_T_OrderDeliveryID = $this->db->insert_id();
if ($new_T_OrderDeliveryID > 0) {
$j_note = $j_order["t_orderdeliverynote"];
$del_notes = array_filter(
$j_note,
function ($v, $k) use (
$old_T_OrderDeliveryID
) {
if (
$v[
"T_OrderDeliveryNoteT_OrderDeliveryID"
] == $old_T_OrderDeliveryID
) {
return true;
}
},
ARRAY_FILTER_USE_BOTH
);
foreach ($del_notes as $note) {
unset($note["T_OrderDeliveryNoteID"]);
$note[
"T_OrderDeliveryNoteT_OrderDeliveryID"
] = $new_T_OrderDeliveryID;
$qry = $this->db->insert(
"{$target_db}.t_orderdeliverynote",
$note
);
$this->log(
"\tError Insert t_orderdeliverynote " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_delivery_error = true;
break;
}
if ($have_delivery_error) {
break;
}
}
}
if ($have_delivery_error) {
continue;
}
// order log
$have_order_log_error = false;
foreach ($j_order["order_log"] as $detail) {
unset($detail["OrderLogID"]);
$detail[
"OrderLogT_OrderHeaderID"
] = $new_T_OrderHeaderID;
$qry = $this->db->insert("{$target_db}.order_log", $detail);
if (!$qry) {
$this->log(
"\tError Insert order_log " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_order_log_error = true;
break;
}
}
if ($have_order_log_error) {
continue;
}
// order px
$have_order_px_error = false;
foreach ($j_order["order_px"] as $detail) {
unset($detail["OrderPxID"]);
$detail[
"OrderPxT_OrderHeaderID"
] = $new_T_OrderHeaderID;
$qry = $this->db->insert("{$target_db}.order_px", $detail);
if (!$qry) {
$this->log(
"\tError Insert order_px " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_order_px_error = true;
break;
}
}
}
} else {
$new_T_OrderHeaderID = $rows[0]["T_OrderHeaderID"];
$oldPatientID = $rows[0]["T_OrderHeaderM_PatientID"];
$new_M_PatientID = $oldPatientID;
$flag_existing_order = true;
}
if ($new_T_OrderHeaderID == 0) {
$this->log(
"\tError Insert OrderHeader : New T_OrderHeaderID = 0"
);
$this->db->trans_rollback();
$this->db->trans_begin();
continue;
}
// update existing pasien
// only when order created
if (false) {
$this->log(
"Old Patient : $oldPatientID | $new_M_PatientID"
);
if ($oldPatientID <= $new_M_PatientID) {
//update patient data
$j_patient_data = array_filter(
$j_data["patient"],
function ($v, $k) use ($oldPatientID) {
if ($v["M_PatientID"] == $oldPatientID) {
return true;
}
},
ARRAY_FILTER_USE_BOTH
);
$have_patient_data = false;
$patient_data = [];
foreach ($j_patient_data as $pat) {
$patient_data = $pat;
$have_patient_data = true;
}
if ($have_patient_data) {
unset($patient_data["address"]);
unset($patient_data["M_PatientID"]);
$this->log(
"\tUpdate Patient : " .
$patient_data["M_PatientName"]
);
$this->db->where("M_PatientID", $oldPatientID);
$this->db->set($patient_data);
$qry = $this->db->update("{$target_db}.m_patient");
if (!$qry) {
$this->log(
"\tError Update Patient " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
continue;
}
}
}
}
//
// Checking per sample type
// mcu_map_order
// t_barcodelab
// sample_by_step
// t_ordersample
// t_ordersamplereq
// t_sampling_queue_last_status
// t_sampling_queue_by_actio
$this->log("\tStep 1 Order created : {$new_T_OrderHeaderID}");
// mcu_map_order
// from screening for this mcu_T_OrderHeaderID
$j_screenings = $j_data["mcu_screening"];
$screenings = array_filter(
$j_screenings,
function ($v, $k) use ($mcu_T_OrderHeaderID) {
if (
$v["Mcu_ScreeningT_OrderHeaderID"] ==
$mcu_T_OrderHeaderID
) {
return true;
}
},
ARRAY_FILTER_USE_BOTH
);
$have_map_mcu_order_error = false;
$new_sample_type_id = [];
$last_sample_type_id_created = "";
foreach ($screenings as $sc) {
if ($sc["Mcu_ScreeningT_SampleTypeID"] == 0) {
continue;
}
//check di mcu_map_order
$sql = "select * from mcu_map_order
where Mcu_MapOrderT_OrderHeaderID = ?
and Mcu_MapOrderT_SampleTypeID = ?
and Mcu_MapOrderT_TestID = ?
and Mcu_MapOrderT_OrderHeaderNewID =?";
$prm = [
$mcu_T_OrderHeaderID,
$sc["Mcu_ScreeningT_SampleTypeID"],
$sc["Mcu_ScreeningT_TestID"],
$new_T_OrderHeaderID,
];
$rows = $this->get_rows(
$sql,
$prm,
"\tGet Mcu_Map_Orer $mcu_T_OrderHeaderID => $new_T_OrderHeaderID"
);
//Order baru di buat
if ($rows !== false && count($rows) == 0) {
//get sample name and test name
$new_sample_type_id[] =
$sc["Mcu_ScreeningT_SampleTypeID"];
$last_sample_type_id_created = date("Y-m-d H:i:s");
list(
$sc_TestName,
$sc_SampleTypeName,
) = $this->get_screening_name($sc);
$dt_mcu = [
"prepare_code" => $code,
"mcu_nolab" => $order["T_OrderHeaderLabNumber"],
"one_nolab" => $order["T_OrderHeaderLabNumber"],
"test_name" => $sc_TestName,
"sample_name" => $sc_SampleTypeName,
"session_code" => $mcu_session_code,
];
$data_mcu_map_order = [
"Mcu_MapOrderMcuOfflinePrepareCode" => $code,
"Mcu_MapOrderT_OrderHeaderID" => $mcu_T_OrderHeaderID,
"Mcu_MapOrderT_SampleTypeID" =>
$sc["Mcu_ScreeningT_SampleTypeID"],
"Mcu_MapOrderT_TestID" =>
$sc["Mcu_ScreeningT_TestID"],
"Mcu_MapOrderIsNonLab" =>
$sc["Mcu_ScreeningIsNonLab"],
"Mcu_MapOrderT_OrderHeaderNewID" => $new_T_OrderHeaderID,
"Mcu_MapOrderMcu_SessionID" => $mcu_session_id,
"Mcu_MapOrderMcu_SessionCode" => $mcu_session_code,
"Mcu_MapOrderInfo" => json_encode($dt_mcu),
];
$qry = $this->db->insert(
"{$target_db}.mcu_map_order",
$data_mcu_map_order
);
if (!$qry) {
$this->log(
"\tError Insert Mcu Map Order : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_map_mcu_order_error = true;
break;
}
$this->log(
"\t\tMcu Map Order Inserted " .
$this->db->insert_id()
);
}
}
if ($have_map_mcu_order_error) {
continue;
}
//Tambahan jika ada sample type id baru
$flag_is_empty_lab = false;
if (count($new_sample_type_id) == 0) {
$this->log("\tMcu Screening No New Sample Type ID");
$flag_is_empty_lab = true;
}
// only if having new sample type
if (count($new_sample_type_id) > 0) {
//t_barcodelab
$j_barcodelabs =
$j_data["fo_verif"]["data"]["t_barcodelab"];
$barcodelabs = array_filter(
$j_barcodelabs,
function ($v, $k) use ($mcu_T_OrderHeaderID) {
if (
$v["T_BarcodeLabT_OrderHeaderID"] ==
$mcu_T_OrderHeaderID
) {
return true;
}
},
ARRAY_FILTER_USE_BOTH
);
$have_barcodelab_error = false;
$new_barcode_lab = [];
//WIP: check again sampleTypeID
foreach ($barcodelabs as $b) {
$old_bc_id = $b["T_BarcodeLabID"];
unset($b["T_BarcodeLabID"]);
$b[
"T_BarcodeLabT_OrderHeaderID"
] = $new_T_OrderHeaderID;
if (
!in_array(
$b["T_BarcodeLabT_SampleTypeID"],
$new_sample_type_id
)
) {
continue;
}
$qry = $this->db->insert("{$target_db}.t_barcodelab", $b);
if (!$qry) {
$this->log(
"\tError Insert t_barcodelab : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_barcodelab_error = true;
break;
}
$new_barcode_lab[$old_bc_id] = $this->db->insert_id();
}
if ($have_barcodelab_error) {
continue;
}
//t_ordersample
$j_ordersamples =
$j_data["fo_verif"]["data"]["t_ordersample"];
$order_samples = array_filter(
$j_ordersamples,
function ($v, $k) use (
$mcu_T_OrderHeaderID,
$screenings
) {
if (
$v["T_OrderSampleT_OrderHeaderID"] ==
$mcu_T_OrderHeaderID
) {
foreach ($screenings as $sc) {
if (
$v["T_OrderSampleT_SampleTypeID"] ==
$sc["Mcu_ScreeningT_SampleTypeID"]
) {
return true;
}
}
}
},
ARRAY_FILTER_USE_BOTH
);
$have_ordersample_error = false;
$map_new_order_sample = [];
//WIP: check again sampleTypeID
foreach ($order_samples as $os) {
if (
!in_array(
$os["T_OrderSampleT_SampleTypeID"],
$new_sample_type_id
)
) {
continue;
}
//delete existing
$this->db->where(
"T_OrderSampleT_OrderHeaderID",
$new_T_OrderHeaderID
);
$this->db->where(
"T_OrderSampleT_SampleTypeID",
$os["T_OrderSampleT_SampleTypeID"]
);
$qry = $this->db->delete("t_ordersample");
if (!$qry) {
$this->log(
"\tError Delete t_ordersample : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_ordersample_error = true;
break;
}
$os[
"T_OrderSampleT_OrderHeaderID"
] = $new_T_OrderHeaderID;
$old_T_OrderSampleID = $os["T_OrderSampleID"];
unset($os["T_OrderSampleID"]);
$old_bc_id = $os["T_OrderSampleT_BarcodeLabID"];
if (!isset($new_barcode_lab[$old_bc_id])) {
$this->log(
"\tError T_BarcodeLabID not found ! = $old_bc_id "
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_ordersample_error = true;
break;
}
$os["T_OrderSampleT_BarcodeLabID"] =
$new_barcode_lab[$old_bc_id];
$qry = $this->db->insert("t_ordersample", $os);
if (!$qry) {
$this->log(
"\tError Insert t_ordersample : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_ordersample_error = true;
break;
}
$map_new_order_sample[
$old_T_OrderSampleID
] = $this->db->insert_id();
}
if ($have_ordersample_error) {
continue;
}
//t_ordersamplereq
$j_ordersamplereqs =
$j_data["fo_verif"]["data"]["t_ordersamplereq"];
$order_samplereqs = array_filter(
$j_ordersamplereqs,
function ($v, $k) use (
$mcu_T_OrderHeaderID,
$map_new_order_sample
) {
if (
$v["T_OrderSampleReqT_OrderHeaderID"] ==
$mcu_T_OrderHeaderID
) {
foreach ($map_new_order_sample as $xk => $xv) {
if (
$v["T_OrderSampleReqT_OrderSampleID"] ==
$xk
) {
return true;
}
}
}
},
ARRAY_FILTER_USE_BOTH
);
$have_ordersamplereq_error = false;
$map_new_order_sample = [];
foreach ($order_samplereqs as $osr) {
$osr[
"T_OrderSampleReqT_OrderHeaderID"
] = $new_T_OrderHeaderID;
unset($osr["T_OrderSampleReqID"]);
if (
!isset(
$map_new_order_sample[
$osr["T_OrderSampleReqT_OrderSampleID"]
]
)
) {
$this->log(
"\tError T_OrderSampleID not found ! = " .
$osr["T_OrderSampleReqT_OrderSampleID"]
);
continue;
//$this->db->trans_rollback();
//$this->db->trans_begin();
//$have_ordersamplereq_error = true;
//break;
}
$osr["T_OrderSampleReqT_OrderSampleID"] =
$map_new_order_sample[
$osr["T_OrderSampleReqT_OrderSampleID"]
];
//check if exists
$sql =
"select T_OrderSampleT_SampleTypeID from t_ordersample where T_OrderSampleReqT_OrderSampleID=?";
$rows = $this->get_rows(
$sql,
[$osr["T_OrderSampleReqT_OrderSampleID"]],
"Get SampleTypeID from t_ordersample"
);
if ($rows && count($rows) > 0) {
$x_sampleTypeID =
$rows[0]["T_OrderSampleT_SampleTypeID"];
if (
!in_array($x_sampleTypeID, $new_sample_type_id)
) {
continue;
}
}
$qry = $this->db->insert("t_ordersamplereq", $osr);
if (!$qry) {
$this->log(
"\tError Insert t_ordersamplereq : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_ordersamplereq_error = true;
break;
}
}
if ($have_ordersamplereq_error) {
continue;
}
//TODO : up to here
//sample_by_step
$j_sample_by_steps =
$j_data["fo_verif"]["data"]["sample_by_step"];
$sample_by_steps = array_filter(
$j_sample_by_steps,
function ($v, $k) use (
$mcu_T_OrderHeaderID,
$new_barcode_lab
) {
if (
$v["SampleByStepT_OrderHeaderID"] ==
$mcu_T_OrderHeaderID
) {
foreach ($new_barcode_lab as $xk => $xv) {
if (
$v["SampleByStepT_BarcodeLabID"] == $xk
) {
return true;
}
}
}
},
ARRAY_FILTER_USE_BOTH
);
$have_sample_by_step_error = false;
foreach ($sample_by_steps as $ss) {
//delete existing
if (
!isset(
$new_barcode_lab[
$ss["SampleByStepT_BarcodeLabID"]
]
)
) {
$this->log(
"\tError SampleByStepT_BarcodeLabID not found ! = " .
$ss["SampleByStepT_BarcodeLabID"]
);
//$this->db->trans_rollback();
//$this->db->trans_begin();
//$have_sample_by_step_error = true;
//break;
continue;
}
$newT_BarcodeLabID =
$new_barcode_lab[$ss["SampleByStepT_BarcodeLabID"]];
//check sampletypeID
$sql = "select T_BarcodeLabT_SampleTypeID from t_barcodelab
where T_BarcodeLabID = ?";
$rows = $this->get_rows(
$sql,
[$newT_BarcodeLabID],
"Get SampleTypeID from t_barcodelab"
);
if ($rows && count($rows) > 0) {
$x_sampleTypeID =
$rows[0]["T_BarcodeLabT_SampleTypeID"];
if (
!in_array($x_sampleTypeID, $new_sample_type_id)
) {
continue;
}
}
$this->db->where(
"SampleByStepT_OrderHeaderID",
$new_T_OrderHeaderID
);
$this->db->where(
"SampleByStepT_BarcodeLabID",
$newT_BarcodeLabID
);
$qry = $this->db->delete("sample_by_step");
if (!$qry) {
$this->log(
"\tError Delete sample_by_step : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_sample_by_step_error = true;
break;
}
$ss[
"SampleByStepT_OrderHeaderID"
] = $new_T_OrderHeaderID;
unset($ss["SampleByStepID"]);
$ss["SampleByStepT_BarcodeLabID"] = $newT_BarcodeLabID;
$qry = $this->db->insert("sample_by_step", $ss);
if (!$qry) {
$this->log(
"\tError Insert sample_by_step : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_sample_by_step_error = true;
break;
}
}
if ($have_sample_by_step_error) {
continue;
}
// TODO
// commit dulu ?
//t_sampling_queue_last_status
//t_sampling_queue_by_action
$have_sampling_queue_error = false;
foreach ($screenings as $sc) {
$sampleTypeID = $sc["Mcu_ScreeningT_SampleTypeID"];
if ($sampleTypeID == 0) {
continue;
}
if (!in_array($sampleTypeID, $new_sample_type_id)) {
continue;
}
$sql = "SELECT T_BahanT_SampleStationID as station_id
FROM {$target_db}.t_sampletype
JOIN {$target_db}.t_bahan ON T_SampleTypeT_BahanID = T_BahanID AND T_BahanIsActive = 'Y'
WHERE
T_SampleTypeID = ? LIMIT 1";
$rows = $this->get_rows(
$sql,
[$sampleTypeID],
"\tGet StationID"
);
if ($rows === false) {
$have_sampling_queue_error = true;
break;
}
if (count($rows) == 0) {
$this->log(
"\tTidak ada Station Sample Type ID : $sampleTypeID"
);
continue;
}
$stationID = $rows[0]["station_id"];
//t_sampling_queue_last_status
$j_sampling_queue_last_status =
$j_data["spec_col"]["data"][
"t_sampling_queue_last_status"
];
$sampling_queue_last_status = array_filter(
$j_sampling_queue_last_status,
function ($v, $k) use (
$mcu_T_OrderHeaderID,
$stationID
) {
if (
$v[
"T_SamplingQueueLastStatusT_OrderHeaderID"
] == $mcu_T_OrderHeaderID &&
$v[
"T_SamplingQueueLastStatusT_SamplingQueueStatusID"
] == 5 &&
$v[
"T_SamplingQueueLastStatusT_SampleStationID"
] == $stationID
) {
return true;
}
},
ARRAY_FILTER_USE_BOTH
);
foreach ($sampling_queue_last_status as $sqq) {
$this->db->where(
"T_SamplingQueueLastStatusT_OrderHeaderID",
$new_T_OrderHeaderID
);
$this->db->where(
"T_SamplingQueueLastStatusT_SampleStationID",
$stationID
);
$qry = $this->db->delete(
"t_sampling_queue_last_status"
);
if (!$qry) {
$this->log(
"\tError Delete t_sampling_queue_last_status : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_sampling_queue_error = true;
break;
}
unset($sqq["T_SamplingQueueLastStatusID"]);
$sqq[
"T_SamplingQueueLastStatusT_OrderHeaderID"
] = $new_T_OrderHeaderID;
$qry = $this->db->insert(
"t_sampling_queue_last_status",
$sqq
);
if (!$qry) {
$this->log(
"\tError Insert t_sampling_queue_last_status A : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_sampling_queue_error = true;
break;
}
}
if ($have_sampling_queue_error) {
continue;
}
//t_sampling_queue_by_action
$j_sampling_queue_by_action =
$j_data["spec_col"]["data"][
"t_sampling_queue_by_action"
];
$sampling_queue_by_action = array_filter(
$j_sampling_queue_by_action,
function ($v, $k) use (
$mcu_T_OrderHeaderID,
$stationID
) {
if (
$v[
"T_SamplingQueueByActionT_OrderHeaderID"
] == $mcu_T_OrderHeaderID &&
$v[
"T_SamplingQueueByActionT_SampleStationID"
] == $stationID
) {
return true;
}
},
ARRAY_FILTER_USE_BOTH
);
foreach ($sampling_queue_by_action as $sqa) {
$this->db->where(
"T_SamplingQueueByActionT_OrderHeaderID",
$new_T_OrderHeaderID
);
$this->db->where(
"T_SamplingQueueByActionT_SampleStationID",
$stationID
);
$qry = $this->db->delete(
"t_sampling_queue_by_action"
);
if (!$qry) {
$this->log(
"\tError Delete t_sampling_queue_by_action : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_sampling_queue_error = true;
break;
}
unset($sqa["T_SamplingQueueByActionID"]);
$sqa[
"T_SamplingQueueByActionT_OrderHeaderID"
] = $new_T_OrderHeaderID;
$qry = $this->db->insert(
"t_sampling_queue_by_action",
$sqa
);
if (!$qry) {
$this->log(
"\tError Insert t_sampling_queue_by_action : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_sampling_queue_error = true;
break;
}
}
if ($have_sampling_queue_error) {
break;
}
}
if ($have_sampling_queue_error) {
continue;
}
}
//TODO
//commit dulu
// HERE ...
// Non Lab
// t_samplingso
// so_resultentry
// t_samplingso_requirement
// t_samplingso_form
// t_samplingso_film
// t_sampling_queue_last_status
// t_sampling_queue_by_action
$this->log("\t\tStarting NonLab Process");
$new_nonlab_test_id = [];
$flag_is_empty_nonlab = true;
foreach ($screenings as $sc) {
if ($sc["Mcu_ScreeningT_TestID"] == 0) {
continue;
}
if ($sc["Mcu_ScreeningIsNonLab"] == "") {
continue;
}
//check di mcu_map_order
$sql = "select * from mcu_map_order
where Mcu_MapOrderT_OrderHeaderID = ?
and Mcu_MapOrderT_SampleTypeID = ?
and Mcu_MapOrderT_TestID = ?
and Mcu_MapOrderT_OrderHeaderNewID =?";
$prm = [
$mcu_T_OrderHeaderID,
$sc["Mcu_ScreeningT_SampleTypeID"],
$sc["Mcu_ScreeningT_TestID"],
$new_T_OrderHeaderID,
];
$rows = $this->get_rows(
$sql,
$prm,
"\tGet Mcu_Map_Order $mcu_T_OrderHeaderID => $new_T_OrderHeaderID"
);
//Order baru di buat
if ($rows !== false && count($rows) == 0) {
//get sample name and test name
$new_nonlab_test_id[] = $sc["Mcu_ScreeningT_TestID"];
list(
$sc_TestName,
$sc_SampleTypeName,
) = $this->get_screening_name($sc);
$dt_mcu = [
"prepare_code" => $code,
"mcu_nolab" => $order["T_OrderHeaderLabNumber"],
"one_nolab" => $order["T_OrderHeaderLabNumber"],
"test_name" => $sc_TestName,
"sample_name" => $sc_SampleTypeName,
"session_code" => $mcu_session_code,
];
$data_mcu_map_order = [
"Mcu_MapOrderMcuOfflinePrepareCode" => $code,
"Mcu_MapOrderT_OrderHeaderID" => $mcu_T_OrderHeaderID,
"Mcu_MapOrderT_SampleTypeID" =>
$sc["Mcu_ScreeningT_SampleTypeID"],
"Mcu_MapOrderT_TestID" =>
$sc["Mcu_ScreeningT_TestID"],
"Mcu_MapOrderIsNonLab" =>
$sc["Mcu_ScreeningIsNonLab"],
"Mcu_MapOrderT_OrderHeaderNewID" => $new_T_OrderHeaderID,
"Mcu_MapOrderMcu_SessionID" => $mcu_session_id,
"Mcu_MapOrderMcu_SessionCode" => $mcu_session_code,
"Mcu_MapOrderInfo" => json_encode($dt_mcu),
];
$qry = $this->db->insert(
"{$target_db}.mcu_map_order",
$data_mcu_map_order
);
if (!$qry) {
$this->log(
"\tError Insert Mcu Map Order : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_map_mcu_order_error = true;
break;
}
$this->log(
"\t\tMcu Map Order Inserted " .
$this->db->insert_id()
);
}
}
if ($have_map_mcu_order_error) {
continue;
}
$have_nonlab_error = false;
$pair_ids = [];
foreach ($screenings as $sc) {
if ($sc["Mcu_ScreeningT_TestID"] == 0) {
continue;
}
if (
!in_array(
$sc["Mcu_ScreeningT_TestID"],
$new_nonlab_test_id
)
) {
$this->log(
"\t\tNot New Mcu_Screening_TestID : " .
$sc["Mcu_ScreeningT_TestID"]
);
continue;
}
$flag_is_empty_nonlab = false;
$sql = "SELECT T_BahanT_SampleStationID as station_id
FROM {$target_db}.t_test
JOIN {$target_db}.t_sampletype ON T_TestT_SampleTypeID = T_SampleTypeID
JOIN {$target_db}.t_bahan ON T_SampleTypeT_BahanID = T_BahanID
WHERE
T_TestID = ? LIMIT 1";
$rows = $this->get_rows(
$sql,
[$sc["Mcu_ScreeningT_TestID"]],
"\tGet NonLab Station ID"
);
if ($rows === false) {
$have_nonlab_error = true;
break;
}
if (count($rows) == 0) {
$this->log(
"\tTidak ada Station Test ID : " .
$sc["Mcu_ScreeningT_TestID"]
);
continue;
}
$stationID = $rows[0]["station_id"];
//so_resultentry
$j_so_resultentries =
$j_data["rad_pasien_handling"]["data"][
"so_resultentry"
];
$so_resultentries = array_filter(
$j_so_resultentries,
function ($v, $k) use ($mcu_T_OrderHeaderID) {
if (
$v["So_ResultEntryT_OrderHeaderID"] ==
$mcu_T_OrderHeaderID
) {
return true;
}
},
ARRAY_FILTER_USE_BOTH
);
$have_so_result_entry_error = false;
foreach ($so_resultentries as $so_result) {
$old_So_ResultEntryID = $so_result["So_ResultEntryID"];
$so_result[
"So_ResultEntryT_OrderHeaderID"
] = $new_T_OrderHeaderID;
$so_result["So_ResultEntryT_OrderDetailID"] =
$map_order_detail[
$so_result["So_ResultEntryT_OrderDetailID"]
];
$xids =
$new_T_OrderHeaderID .
"-" .
$so_result["So_ResultEntryT_OrderDetailID"];
unset($so_result["So_ResultEntryID"]);
if (in_array($xids, $pair_ids)) {
$this->db->where(
"So_ResultEntryT_OrderHeaderID",
$new_T_OrderHeaderID
);
$this->db->where(
"So_ResultEntryT_OrderDetailID",
$so_result["So_ResultEntryT_OrderDetailID"]
);
$qry = $this->db->update(
"so_resultentry",
$so_result
);
} else {
$pair_ids[] = $xids;
$qry = $this->db->insert(
"so_resultentry",
$so_result
);
}
if (!$qry) {
$this->log(
"\tError Insert so_resultentry : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_so_result_entry_error = true;
break;
}
$new_So_ResultEntryID = $this->db->insert_id();
//detail
//so_resultentrydetail
$j_so_details =
$j_data["rad_pasien_handling"]["data"][
"so_resultentrydetail"
];
$so_details = array_filter(
$j_so_details,
function ($v, $k) use ($old_So_ResultEntryID) {
if (
$v[
"So_ResultEntryDetailSo_ResultEntryID"
] == $old_So_ResultEntryID
) {
return true;
}
},
ARRAY_FILTER_USE_BOTH
);
foreach ($so_details as $det) {
unset($det["So_ResultEntryDetailID"]);
$det[
"So_ResultEntryDetailSo_ResultEntryID"
] = $new_So_ResultEntryID;
$qry = $this->db->insert(
"so_resultentrydetail",
$det
);
if (!$qry) {
$this->log(
"\tError Insert so_resultentrydetail : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_so_result_entry_error = true;
break;
}
}
}
if ($have_so_result_entry_error) {
continue;
}
//t_samplingso
$j_sampling_sos =
$j_data["rad_pasien_handling"]["data"]["t_samplingso"];
$sampling_sos = array_filter(
$j_sampling_sos,
function ($v, $k) use (
$mcu_T_OrderHeaderID,
$stationID
) {
if (
$v["T_SamplingSoT_OrderHeaderID"] ==
$mcu_T_OrderHeaderID &&
$v["T_SamplingSoT_SampleStationID"] ==
$stationID
) {
return true;
}
},
ARRAY_FILTER_USE_BOTH
);
foreach ($sampling_sos as $sso) {
$this->db->where(
"T_SamplingSoT_OrderHeaderID",
$new_T_OrderHeaderID
);
$this->db->where(
"T_SamplingSoT_SampleStationID",
$stationID
);
$qry = $this->db->delete("t_samplingso");
if (!$qry) {
$this->log(
"\tError Delete t_samplingso : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_nonlab_error = true;
break;
}
$old_T_SamplingSoID = $sso["T_SamplingSoID"];
unset($sso["T_SamplingSoID"]);
$sso[
"T_SamplingSoT_OrderHeaderID"
] = $new_T_OrderHeaderID;
$qry = $this->db->insert("t_samplingso", $sso);
if (!$qry) {
$this->log(
"\tError Insert t_samplingso : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_nonlab_error = true;
break;
}
$new_T_SamplingSoID = $this->db->insert_id();
// t_samplingso_requirement
$j_sampling_so_reqs =
$j_data["rad_pasien_handling"]["data"][
"t_samplingso_requirement"
];
$sampling_so_reqs = array_filter(
$j_sampling_so_reqs,
function ($v, $k) use (
$mcu_T_OrderHeaderID,
$old_T_SamplingSoID
) {
if (
$v[
"T_SamplingSoRequirementT_OrderHeaderID"
] == $mcu_T_OrderHeaderID &&
$v[
"T_SamplingSoRequirementT_SamplingSoID"
] == $old_T_SamplingSoID
) {
return true;
}
},
ARRAY_FILTER_USE_BOTH
);
foreach ($sampling_so_reqs as $so_req) {
$so_req[
"T_SamplingSoRequirementT_OrderHeaderID"
] = $new_T_OrderHeaderID;
unset($so_req["T_SamplingSoRequirementID"]);
$so_req[
"T_SamplingSoRequirementT_SamplingSoID"
] = $new_T_SamplingSoID;
$qry = $this->db->insert(
"{$target_db}.t_samplingso_requirement",
$so_req
);
if (!$qry) {
$this->log(
"\tError Insert t_samplingso_requirement : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_nonlab_error = true;
break;
}
}
if ($have_nonlab_error) {
break;
}
// t_samplingso_form
//$j_sampling_sos = $j_data["rad_pasien_handling"]["data"]["t_samplingso"];
$j_sampling_so_form =
$j_data["rad_pasien_handling"]["data"][
"t_samplingso_form"
];
$sampling_so_form = array_filter(
$j_sampling_so_form,
function ($v, $k) use (
$stationID,
$old_T_SamplingSoID
) {
if (
$v["T_SamplingSoFormT_SampleStationID"] ==
$stationID &&
$v["T_SamplingSoFormT_SamplingSOID"] ==
$old_T_SamplingSoID
) {
return true;
}
},
ARRAY_FILTER_USE_BOTH
);
foreach ($sampling_so_form as $so_form) {
unset($so_form["T_SamplingSoFormID"]);
$so_form[
"T_SamplingSoFormT_SamplingSOID"
] = $new_T_SamplingSoID;
$qry = $this->db->insert(
"{$target_db}.t_samplingso_form",
$so_form
);
if (!$qry) {
$this->log(
"\tError Insert t_samplingso_form : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_nonlab_error = true;
break;
}
}
if ($have_nonlab_error) {
break;
}
// t_samplingso_film
$j_sampling_so_film =
$j_data["rad_image_verif"]["t_samplingso_film"];
$sampling_so_film = array_filter(
$j_sampling_so_film,
function ($v, $k) use ($old_T_SamplingSoID) {
if (
$v["T_SamplingSoFilmT_SamplingSoID"] ==
$old_T_SamplingSoID
) {
return true;
}
},
ARRAY_FILTER_USE_BOTH
);
foreach ($sampling_so_film as $so_film) {
unset($so_film["T_SamplingSoFilmID"]);
$so_film[
"T_SamplingSoFilmT_SamplingSoID"
] = $new_T_SamplingSoID;
$qry = $this->db->insert(
"{$target_db}.t_samplingso_film",
$so_film
);
if (!$qry) {
$this->log(
"\tError Insert t_samplingso_film : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_nonlab_error = true;
break;
}
}
if ($have_nonlab_error) {
break;
}
}
if ($have_nonlab_error) {
break;
}
// t_sampling_queue_last_status
$j_sampling_queue_last_status =
$j_data["spec_col"]["data"][
"t_sampling_queue_last_status"
];
$sampling_queue_last_status = array_filter(
$j_sampling_queue_last_status,
function ($v, $k) use (
$mcu_T_OrderHeaderID,
$stationID
) {
if (
$v[
"T_SamplingQueueLastStatusT_OrderHeaderID"
] == $mcu_T_OrderHeaderID &&
$v[
"T_SamplingQueueLastStatusT_SamplingQueueStatusID"
] == 5 &&
$v[
"T_SamplingQueueLastStatusT_SampleStationID"
] == $stationID
) {
return true;
}
},
ARRAY_FILTER_USE_BOTH
);
foreach ($sampling_queue_last_status as $sqq) {
$this->db->where(
"T_SamplingQueueLastStatusT_OrderHeaderID",
$new_T_OrderHeaderID
);
$this->db->where(
"T_SamplingQueueLastStatusT_SampleStationID",
$stationID
);
$qry = $this->db->delete(
"t_sampling_queue_last_status"
);
if (!$qry) {
$this->log(
"\tError Delete t_sampling_queue_last_status : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_nonlab_error = true;
break;
}
unset($sqq["T_SamplingQueueLastStatusID"]);
$sqq[
"T_SamplingQueueLastStatusT_OrderHeaderID"
] = $new_T_OrderHeaderID;
$qry = $this->db->insert(
"t_sampling_queue_last_status",
$sqq
);
if (!$qry) {
$this->log(
"\tError Insert t_sampling_queue_last_status B : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_nonlab_error = true;
break;
}
}
if ($have_nonlab_error) {
break;
}
// t_sampling_queue_by_action
$j_sampling_queue_by_action =
$j_data["spec_col"]["data"][
"t_sampling_queue_by_action"
];
$sampling_queue_by_action = array_filter(
$j_sampling_queue_by_action,
function ($v, $k) use (
$mcu_T_OrderHeaderID,
$stationID
) {
if (
$v["T_SamplingQueueByActionT_OrderHeaderID"] ==
$mcu_T_OrderHeaderID &&
$v[
"T_SamplingQueueByActionT_SampleStationID"
] == $stationID
) {
return true;
}
},
ARRAY_FILTER_USE_BOTH
);
foreach ($sampling_queue_by_action as $sqa) {
$this->db->where(
"T_SamplingQueueByActionT_OrderHeaderID",
$new_T_OrderHeaderID
);
$this->db->where(
"T_SamplingQueueByActionT_SampleStationID",
$stationID
);
$qry = $this->db->delete("t_sampling_queue_by_action");
if (!$qry) {
$this->log(
"\tError Delete t_sampling_queue_by_action : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_nonlab_error = true;
break;
}
unset($sqa["T_SamplingQueueByActionID"]);
$sqa[
"T_SamplingQueueByActionT_OrderHeaderID"
] = $new_T_OrderHeaderID;
$qry = $this->db->insert(
"t_sampling_queue_by_action",
$sqa
);
if (!$qry) {
$this->log(
"\tError Insert t_sampling_queue_by_action : " .
$this->get_db_error()
);
$this->db->trans_rollback();
$this->db->trans_begin();
$have_nonlab_error = true;
break;
}
}
if ($have_nonlab_error) {
break;
}
}
if ($have_nonlab_error) {
continue;
}
$this->db->trans_commit();
$this->db->trans_begin();
$this->log(
"\tEnd Import : " .
$order["T_OrderHeaderLabNumber"] .
" | $new_T_OrderHeaderID"
);
if ($flag_is_empty_lab && $flag_is_empty_nonlab) {
$this->log("\tNo Wait empty lab and non lab");
} else {
$this->log("\tWait {$WAIT_BETWEEN_ORDER} second");
sleep($WAIT_BETWEEN_ORDER);
}
$total_order_imported++;
}
if ($total_order <= $total_order_imported) {
$this->db->where("McuOfflineUploadID", $uploadID);
$update_data = ["McuOfflineUploadStatus" => "D"];
$this->db->update("mcu_offline_upload_v2", $update_data);
$this->db->trans_commit();
$this->db->trans_begin();
} else {
if (($total_order > 0) & ($total_order_imported > 0)) {
$this->db->where("McuOfflineUploadID", $uploadID);
$update_data = ["McuOfflineUploadStatus" => "P"];
$this->db->update("mcu_offline_upload_v2", $update_data);
$this->db->trans_commit();
$this->db->trans_begin();
} elseif ($total_order == 0) {
$this->db->where("McuOfflineUploadID", $uploadID);
$update_data = ["McuOfflineUploadStatus" => "X"];
$this->db->update("mcu_offline_upload_v2", $update_data);
$this->db->trans_commit();
$this->db->trans_begin();
} else {
$this->db->where("McuOfflineUploadID", $uploadID);
$update_data = ["McuOfflineUploadStatus" => "Z"];
$this->db->update("mcu_offline_upload_v2", $update_data);
$this->db->trans_commit();
$this->db->trans_begin();
}
}
$this->log(
"End Orocessing MCU $code : $filename {$total_order_imported} of {$total_order}"
);
}
}
public function get_screening_name($sc)
{
$testName = "";
$sampleTypeName = "";
if ($sc["Mcu_ScreeningT_TestID"] > 0) {
$row = $this->get_one_row(
"select T_TestName from t_test where T_TestID=?",
[$sc["Mcu_ScreeningT_TestID"]],
"Get TestName"
);
if ($row) {
$testName = $row["T_TestName"];
}
}
if ($sc["Mcu_ScreeningT_SampleTypeID"] > 0) {
$row = $this->get_one_row(
"select T_SampleTypeName from t_sampletype where T_SampleTypeID=?",
[$sc["Mcu_ScreeningT_SampleTypeID"]],
"Get SampleType Name"
);
if ($row) {
$sampleTypeName = $row["T_SampleTypeName"];
}
}
return [$testName, $sampleTypeName];
}
public function get_db_error()
{
return $this->db->error()["message"] . "|\n" . $this->db->last_query();
}
public function exec_query($sql, $msg, $param = false)
{
if (is_array($param)) {
$qry = $this->db->query($sql, $param);
} else {
$qry = $this->db->query($sql);
}
if (!$qry) {
$this->log(
"Error $msg" .
"|" .
$this->db->error()["message"] .
"|\n" .
$this->db->last_query()
);
return false;
}
return true;
}
public function update_header(
$orderID,
$labNumber,
$labNumberExt,
$new_M_PatientID = 0
) {
$target_db = "one";
if ($new_M_PatientID > 0) {
$data = [
"T_OrderHeaderLabNumber" => $labNumber,
"T_OrderHeaderLabNumberExt" => $labNumberExt,
"T_OrderHeaderM_PatientID" => $new_M_PatientID,
];
} else {
$data = [
"T_OrderHeaderLabNumber" => $labNumber,
"T_OrderHeaderLabNumberExt" => $labNumberExt,
];
}
$this->db->where("T_OrderHeaderID", $orderID);
$qry = $this->db->update("{$target_db}.t_orderheader", $data);
if (!$qry) {
$this->log(
"Error Update LabNumber : " .
$this->db->error()["message"] .
"|\n" .
$this->db->last_query()
);
return false;
}
return true;
}
public function create_mcu_session($id, $code)
{
$parts = pathinfo($code);
$code = str_replace(".json", "", $parts["filename"]);
$this->db->where("McuSessionCode", $code);
$this->db->where("McuSessionMcuOfflinePrepareID", $id);
$qry = $this->db->get("mcu_session.mcu_session");
if ($qry) {
$rows = $qry->result_array();
if (count($rows) > 0) {
return $rows[0]["McuSessionID"];
}
}
$data = [
"McuSessionCode" => $code,
"McuSessionMcuOfflinePrepareID" => $id,
"McuSessionUserID" => 3,
];
$qry = $this->db->insert("mcu_session.mcu_session", $data);
if (!$qry) {
$this->log(
"Error Create MCU Session : " .
$this->db->error()["message"] .
"|\n" .
$this->db->last_query()
);
return false;
}
return $this->db->insert_id();
}
public function log($message)
{
echo date("Y-m-d H:i:s ") . $message . "\n";
}
public function get_mcu_by_code($code)
{
$sql = "select McuOfflinePrepareID,McuOfflinePrepareM_CompanyID
from mcu_offline_prepare
where McuOfflinePrepareCode = ?";
return $this->get_one_row($sql, [$code], " Get Mcu by Code $code");
}
public function get_rows($sql, $param, $msg)
{
if (is_array($param)) {
$qry = $this->db->query($sql, $param);
} else {
$qry = $this->db->query($sql);
}
if (!$qry) {
$this->log(
"Error $msg : " .
$this->db->error()["message"] .
"|\n" .
$this->db->last_query()
);
return false;
}
$rows = $qry->result_array();
return $rows;
}
public function get_one_row($sql, $param, $msg)
{
if (is_array($param)) {
$qry = $this->db->query($sql, $param);
} else {
$qry = $this->db->query($sql);
}
if (!$qry) {
$this->log(
"Error $msg : " .
$this->db->error()["message"] .
"|" .
$this->db->last_query()
);
return false;
}
$rows = $qry->result_array();
if (count($rows) == 0) {
$this->log("Error $msg : Emoty Record ");
return false;
}
return $rows[0];
}
}