diff --git a/application/controllers/mockup/mcuoffline/Preregisterappcponev3.php b/application/controllers/mockup/mcuoffline/Preregisterappcponev3.php index cfd5f2d..09adf57 100644 --- a/application/controllers/mockup/mcuoffline/Preregisterappcponev3.php +++ b/application/controllers/mockup/mcuoffline/Preregisterappcponev3.php @@ -1,18 +1,193 @@ db_onedev = $this->load->database("onedev", true); - } + public function __construct() + { + parent::__construct(); + $this->db_onedev = $this->load->database("onedev", true); + $this->db_dashboard = $this->load->database("cpone_dashboard", true); + } + + private function normalize_tanggal_mcu($rawDate) + { + $rawDate = trim((string)$rawDate); + if ($rawDate === '') { + return ''; + } + $ts = strtotime($rawDate); + if ($ts === false) { + return ''; + } + return date('Y-m-d', $ts); + } + + private function extract_tanggal_mcu($prm, $selectedPatient = array()) + { + $candidates = array(); + if (isset($prm['TANGGAL_MCU'])) $candidates[] = $prm['TANGGAL_MCU']; + if (isset($prm['tanggal_mcu'])) $candidates[] = $prm['tanggal_mcu']; + if (isset($selectedPatient['TANGGAL_MCU'])) $candidates[] = $selectedPatient['TANGGAL_MCU']; + if (isset($selectedPatient['tanggal_mcu'])) $candidates[] = $selectedPatient['tanggal_mcu']; + + foreach ($candidates as $candidate) { + $normalized = $this->normalize_tanggal_mcu($candidate); + if ($normalized !== '') { + return $normalized; + } + } + + return ''; + } + + private function sync_schedule_and_daily($preregisterID, $mgmMcuID, $tanggalMcu) + { + $preregisterID = intval($preregisterID); + $mgmMcuID = intval($mgmMcuID); + $tanggalMcu = $this->normalize_tanggal_mcu($tanggalMcu); + + if ($preregisterID <= 0 || $mgmMcuID <= 0 || $tanggalMcu === '') { + return; + } + + $sql = "INSERT INTO mcu_patient_schedule ( + Mcu_PatientSchedulePreregisterID, + Mcu_PatientScheduleDate, + Mcu_PatientScheduleIsActive, + Mcu_PatientScheduleSyncedAt + ) VALUES (?, ?, 'Y', NOW()) + ON DUPLICATE KEY UPDATE + Mcu_PatientScheduleIsActive = 'Y', + Mcu_PatientScheduleSyncedAt = NOW()"; + $query = $this->db_dashboard->query($sql, array($preregisterID, $tanggalMcu)); + if (!$query) { + return; + } + + $sql = "SELECT + s.Mcu_PatientScheduleDate AS schedule_date, + COUNT(*) AS total_participant + FROM mcu_patient_schedule s + JOIN cpone.mcu_preregister_patients p + ON p.Mcu_PreregisterPatientsID = s.Mcu_PatientSchedulePreregisterID + WHERE + s.Mcu_PatientScheduleIsActive = 'Y' + AND p.Mcu_PreregisterPatientsIsActive = 'Y' + AND p.Mcu_PreregisterPatientsMgm_McuID = ? + GROUP BY s.Mcu_PatientScheduleDate"; + $query = $this->db_dashboard->query($sql, array($mgmMcuID)); + if (!$query) { + return; + } + $rows = $query->result_array(); + if (count($rows) === 0) { + return; + } + + foreach ($rows as $row) { + $sql = "INSERT INTO mcu_participant_daily ( + Mcu_ParticipantDailyMcuID, + Mcu_ParticipantDailyDate, + Mcu_ParticipantDailyTotal, + Mcu_ParticipantDailyIsActive + ) VALUES (?, ?, ?, 'Y') + ON DUPLICATE KEY UPDATE + Mcu_ParticipantDailyTotal = VALUES(Mcu_ParticipantDailyTotal), + Mcu_ParticipantDailyIsActive = 'Y'"; + $this->db_dashboard->query($sql, array( + $mgmMcuID, + $row['schedule_date'], + intval($row['total_participant']) + )); + } + } + + private function sync_patient_dashboard($preregisterID) + { + $preregisterID = intval($preregisterID); + if ($preregisterID <= 0) { + return; + } + + $sql = "SELECT + Mcu_PreregisterPatientsID, + Mcu_PreregisterPatientsMgm_McuID, + Mcu_PreregisterPatientsPatientName, + Mcu_PreregisterPatientsNIP, + Mcu_PreregisterPatientsGender, + Mcu_PreregisterPatientsDOB, + Mcu_PreregisterPatientsDepartment, + Mcu_PreregisterPatientsDivisi, + Mcu_PreregisterPatientsPosisi, + Mcu_PreregisterPatientsIsRegistered, + Mcu_PreregisterPatientsT_OrderHeaderID + FROM mcu_preregister_patients + WHERE Mcu_PreregisterPatientsID = ? AND Mcu_PreregisterPatientsIsActive = 'Y' + LIMIT 1"; + $query = $this->db_onedev->query($sql, array($preregisterID)); + if (!$query || $query->num_rows() === 0) { + return; + } + $row = $query->row_array(); + + $orderID = null; + if ( + isset($row['Mcu_PreregisterPatientsIsRegistered']) && + $row['Mcu_PreregisterPatientsIsRegistered'] === 'Y' && + intval($row['Mcu_PreregisterPatientsT_OrderHeaderID']) > 0 + ) { + $orderID = intval($row['Mcu_PreregisterPatientsT_OrderHeaderID']); + } + + $sql = "INSERT INTO mcu_patient ( + Mcu_PatientPreregisterID, + Mcu_PatientMcuID, + Mcu_PatientName, + Mcu_PatientNIP, + Mcu_PatientGender, + Mcu_PatientDOB, + Mcu_PatientDepartment, + Mcu_PatientDivision, + Mcu_PatientPosisi, + Mcu_PatientIsRegistered, + Mcu_PatientOrderID, + Mcu_PatientIsActive, + Mcu_PatientSyncedAt + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'Y', NOW()) + ON DUPLICATE KEY UPDATE + Mcu_PatientMcuID = VALUES(Mcu_PatientMcuID), + Mcu_PatientName = VALUES(Mcu_PatientName), + Mcu_PatientNIP = VALUES(Mcu_PatientNIP), + Mcu_PatientGender = VALUES(Mcu_PatientGender), + Mcu_PatientDOB = VALUES(Mcu_PatientDOB), + Mcu_PatientDepartment = VALUES(Mcu_PatientDepartment), + Mcu_PatientDivision = VALUES(Mcu_PatientDivision), + Mcu_PatientPosisi = VALUES(Mcu_PatientPosisi), + Mcu_PatientIsRegistered = VALUES(Mcu_PatientIsRegistered), + Mcu_PatientOrderID = VALUES(Mcu_PatientOrderID), + Mcu_PatientIsActive = 'Y', + Mcu_PatientSyncedAt = NOW()"; + $this->db_dashboard->query($sql, array( + intval($row['Mcu_PreregisterPatientsID']), + intval($row['Mcu_PreregisterPatientsMgm_McuID']), + $row['Mcu_PreregisterPatientsPatientName'], + $row['Mcu_PreregisterPatientsNIP'], + $row['Mcu_PreregisterPatientsGender'], + $row['Mcu_PreregisterPatientsDOB'], + $row['Mcu_PreregisterPatientsDepartment'], + $row['Mcu_PreregisterPatientsDivisi'], + $row['Mcu_PreregisterPatientsPosisi'], + $row['Mcu_PreregisterPatientsIsRegistered'], + $orderID + )); + } public function getsetup() { try { @@ -465,8 +640,10 @@ class Preregisterappcponev3 extends MY_Controller $this->sys_error($message); exit; } - $last_id_x = $this->db_onedev->insert_id(); - if (intval($v['Mcu_PreregisterPatientsM_PatientID']) == 0) { + $last_id_x = $this->db_onedev->insert_id(); + $tanggalMcu = $this->extract_tanggal_mcu($prm); + $this->sync_schedule_and_daily($last_id_x, $setup['Mgm_McuID'], $tanggalMcu); + if (intval($v['Mcu_PreregisterPatientsM_PatientID']) == 0) { // { // "Mcu_PreregisterPatientsID": 0, // "Mcu_PreregisterPatientsNIK": "", @@ -598,10 +775,11 @@ class Preregisterappcponev3 extends MY_Controller // $ptn = json_encode($ptn); //$this->db_onedev->query("CALL one_log.log_me('PATIENT', 'PATIENT.ADDR_ADD', '{$ptn_addr}', $userid)"); - } - - - $result = array( + } + $this->sync_patient_dashboard($last_id_x); + + + $result = array( "total" => 1, "records" => array('status' => 'OK') ); @@ -912,7 +1090,7 @@ class Preregisterappcponev3 extends MY_Controller WHERE Mcu_PreregisterPatientsID = ?"; //echo $query; - $rows = $this->db_onedev->query($query, [ + $rows = $this->db_onedev->query($query, [ $v['Mcu_PreregisterPatientsM_PatientID'], $v['Mcu_PreregisterPatientsKTP'], $v['Mcu_PreregisterPatientsNIP'], @@ -935,12 +1113,15 @@ class Preregisterappcponev3 extends MY_Controller $userid, $v['Mcu_PreregisterPatientsID'] ]); - if (!$rows) { - $message = $this->db_onedev->error(); - $message['qry'] = $this->db_onedev->last_query(); - $this->sys_error($message); - exit; - } + if (!$rows) { + $message = $this->db_onedev->error(); + $message['qry'] = $this->db_onedev->last_query(); + $this->sys_error($message); + exit; + } + $tanggalMcu = $this->extract_tanggal_mcu($prm, $v); + $this->sync_schedule_and_daily($v['Mcu_PreregisterPatientsID'], $setup['Mgm_McuID'], $tanggalMcu); + $this->sync_patient_dashboard($v['Mcu_PreregisterPatientsID']); $sql_ktp = ''; if (isset($v['Mcu_PreregisterPatientsKTP']) && $v['Mcu_PreregisterPatientsKTP'] != '') {