= ? and oneOrderDate <= ? join m_mou on T_OrderHeaderM_MouID = M_MouID join t_orderheaderaddon on T_OrderHeaderID = T_OrderHeaderAddonT_OrderHeaderID "; $qry = $this->db->query($sql, [$sdate, $edate]); $this->error_check($qry, "get order"); $rows = $qry->result_array(); $sql_i = "insert into one_health.diagnose ( diagnoseOneOrderID, diagnoseType, diagnoseText, diagnoseIsAPS, diagnoseIsPerusahaan, diagnoseIsDokter, diagnoseJson, diagnoseStatus) values (?,?, ?,?,?, ?, ?,? ) on duplicate key update diagnoseText = ? , diagnoseJson = ? "; $ctr_primay = 0; foreach ($rows as $r) { if ($r["isAPS"] == "Y" && $r["T_OrderHeaderDiagnose"] == "") { $json = $this->condition( $r["oneOrderEncounterID"], $r["encounterDisplay"], $r["oneOrderM_PatientIhsID"], $r["oneOrderM_PatientName"], $z08Code, $z08Display, $r["T_OrderHeaderAddonFoTimeEnd"] ); $status = "New"; } else { $json = ""; $status = "Hold"; } $qry = $this->db->query($sql_i, [ $r["oneOrderID"], "primary", $r["T_OrderHeaderDiagnose"], $r["isAPS"], $r["isPerusahaan"], $r["isDokter"], $json, $status, $r["T_OrderHeaderDiagnose"], $json ]); $this->error_check($qry, "insert primary"); $ctr_primay++; } echo "Date $sdate - $edate \n"; echo "Diagnose Primary $ctr_primay \n"; } /*function submit($sdate, $edate = "", $xid = "", $debug = "")*/ function submit($xid) { if ($edate == "") { $edate = $sdate; } $sdate = "$sdate 00:00:00"; $edate = "$edate 23:59:59"; if($xid != "") { $sql = "select oneOrderID, oneOrderDate, oneOrderT_OrderHeaderLabNumber, oneOrderM_PatientName,oneOrderEncounterID, oneOrderM_PatientIhsID, diagnose.* from one_health.diagnose join one_health.one_order on diagnoseOneOrderID = oneOrderID and diagnoseStatus = 'New' and diagnoseOneOrderID = ? "; $qry = $this->db->query($sql, [$xid]); } else { $sql = "select oneOrderID, oneOrderDate, oneOrderT_OrderHeaderLabNumber, oneOrderM_PatientName,oneOrderEncounterID, oneOrderM_PatientIhsID, diagnose.* from one_health.diagnose join one_health.one_order on diagnoseOneOrderID = oneOrderID and diagnoseStatus = 'New' and oneOrderDate >= ? and oneOrderDate <= ? "; $qry = $this->db->query($sql, [$sdate, $edate]); } if (!$qry) { echo $this->db->last_query(); exit; } $rows = $this->error_check($qry, "get diagnose New", true); if (count($rows) == 0) { echo "\t No Diagnose \n"; } if ($debug != "") { $xrows = []; $xrows[] = $rows[0]; $rows = $xrows; } $this->load->library("Satu_sehat"); header("Content-Type: text/plain"); $service = "/Condition"; $sql_u = "update one_health.diagnose set diagnoseReplay = ?, diagnoseSent = now(), diagnoseStatus =? where diagnoseID = ?"; $idx = 0; foreach ($rows as $r) { $number = $r["oneOrderT_OrderHeaderLabNumber"]; $patient = $r["oneOrderM_PatientName"]; $subject = $r["oneOrderM_PatientIhsID"]; $payload = json_decode($r["diagnoseJson"], true); $date = $r["oneOrderDate"]; $id = $r["diagnoseID"]; $type = $r["diagnoseType"]; $encounterID = $r["oneOrderEncounterID"]; $get_service = $service . "?subject=" . $subject . "&encounter=" . $encounterID; $resp = $this->satu_sehat->ss_get($get_service); echo "Submitting Diagnose {$type} for $number - $patient $date \n"; // echo "Check Service : $get_service\n"; $o_resp = $this->objToArray($resp); // print_r($payload); if ($o_resp["total"] > 0) { $resp_icd10 = $o_resp["entry"][0]["resource"]["code"]["coding"][0]["code"]; $payload_icd10 = $payload["code"]["coding"][0]["code"]; if ($resp_icd10 == $payload_icd10) { echo "Diagnose alreay found. $resp_icd10 | $payload_icd10"; $j_condition = json_encode($o_resp["entry"][0]); echo $j_condition; $qry = $this->db->query($sql_u, [$j_condition, "Sent", $id]); if (!$qry) { echo $this->db->last_query(); exit; } continue; } } $resp = $this->satu_sehat->ss_post($service, $payload); $o_resp = $this->objToArray($resp); // print_r($o_resp); $j_resp = json_encode($o_resp); // echo "Response : \n"; $qry = $this->db->query($sql_u, [$j_resp, "Sent", $id]); if (!$qry) { echo $this->db->last_query(); echo "\n"; print_r($this->db->error()); } else { echo "Uploaded ..."; } $idx++; if ($idx >= 10) { sleep(2); $idx = 0; } } } function condition( $encounterId, $encounterDisplay, $patientIhs, $patientName, $icdxCode, $icdxName, $theDate ) { $orgDate = $theDate; $theDate = substr($orgDate, 0, 10) . "T" . substr($orgDate, 11, 8) . "+07:00"; $param = [ "category" => [ [ "coding" => [ [ "code" => "encounter-diagnosis", "display" => "Encounter Diagnosis", "system" => "http://terminology.hl7.org/CodeSystem/condition-category" ] ] ] ], "clinicalStatus" => [ "coding" => [ [ "code" => "active", "display" => "Active", "system" => "http://terminology.hl7.org/CodeSystem/condition-clinical" ] ] ], "code" => [ "coding" => [ [ "code" => "$icdxCode", "display" => "$icdxName", "system" => "http://hl7.org/fhir/sid/icd-10" ] ] ], "encounter" => [ "display" => "$encounterDisplay", "reference" => "Encounter/$encounterId" ], "onsetDateTime" => "$theDate", "recordedDate" => "$theDate", "resourceType" => "Condition", "subject" => [ "display" => "$patientName", "reference" => "Patient/$patientIhs" ] ]; return json_encode($param, JSON_PRETTY_PRINT); } function error_check($qry, $stage, $check_empty = false) { if (!$qry) { echo json_encode( [ "status" => "ERR", "stage" => $stage, "message" => $this->db->error(), "sql" => $this->db->last_query() ] ); exit; } if ($check_empty) { $rows = $qry->result_array(); if (count($rows) == 0) { echo json_encode( [ "status" => "ERR", "stage" => $stage, "message" => "No record found", "sql" => $this->db->last_query() ] ); exit; } return $rows; } } function print_table($rows, $keys) { $this->print_table_style(); echo "
| $k | "; } echo "
| " . $r[$k] . " | "; } echo "