Files
BE_CPONE/Patient.php
2026-04-27 10:26:26 +07:00

3988 lines
141 KiB
PHP

<?php
class Patient extends MY_Controller
{
var $db_onedev;
public function index()
{
echo "Resultentry API";
}
public function __construct()
{
parent::__construct();
$this->db_onedev = $this->load->database("onedev", true);
$this->db_log = $this->load->database("log", true);
$this->load->helper(array('form', 'url'));
}
public function error_log($data, $userid = 999)
{
$sql = "INSERT INTO ais_error_log (
AisErrorLogFnName,
AisErrorLogMessage,
AisErrorLogQuery,
AisErrorLogJson,
AisErrorLogUserID,
AisErrorLogCreated
)
VALUES(
?,
?,
?,
?,
?,
NOW()
)";
$qry = $this->db_log->query($sql, array($data['fn_name'], $data['message'], $data['query'], $data['json'], $userid));
//echo $this->db_log->last_query();
return true;
}
/**
* Generic POST request function
* @param string $url API endpoint URL
* @param array $data Request payload
* @param array $headers Custom headers (optional)
* @return array Response from API
*/
public function post_request($url, $data = array(), $headers = array())
{
// Default headers
$default_headers = array(
'Content-Type: application/json'
);
// Merge custom headers with default headers
$final_headers = array_merge($default_headers, $headers);
// Initialize cURL
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $final_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// Execute cURL request
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
// Close cURL
curl_close($ch);
// Prepare result
$result = array(
'success' => false,
'http_code' => $http_code,
'response' => null,
'error' => null
);
if ($error) {
$result['error'] = $error;
} else {
$result = json_decode($response, true);
if ($result['status'] == 200) {
$result['success'] = true;
} else {
$result['success'] = false;
}
}
return $result;
}
public function get_request($url, $headers = array())
{
// Retrieve configuration
$config = $this->get_config();
$token = $config['AisConfigAuthToken'] ?? 'default-token';
$header_token = $config['AisConfigHeaderToken'];
// Default headers based on the curl command
$default_headers = array(
'Header-Token: ' . $header_token,
'Authorization: Bearer ' . $token
);
// Merge custom headers with default headers
$final_headers = array_merge($default_headers, $headers);
// Initialize cURL
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $final_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// Execute cURL request
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
// Close cURL
curl_close($ch);
// Prepare result
$result = array(
'success' => false,
'http_code' => $http_code,
'response' => null,
'error' => null
);
if ($error) {
$result['error'] = $error;
} else {
$decoded = json_decode($response, true);
$result['response'] = $decoded;
// Check if response is successful based on http code
if ($http_code === 200) {
$result['success'] = true;
}
}
return $result;
}
function get_config()
{
$sql = "SELECT * FROM ais_config LIMIT 1";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
$this->error_log(array('fn_name' => 'get_config', 'message' => 'ais_config select', 'query' => $this->db_onedev->last_query(), 'json' => ''), 999);
exit;
}
$dt_config = $qry->row_array();
return $dt_config;
}
/**
* POST request to auth API
* @param string $username Username for authentication
* @param string $password Password for authentication
* @return array Response from API
*/
public function post_auth()
{
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/auth/auth.php';
$headers = array(
'Header-Token: ' . $dt_config['AisConfigHeaderToken']
);
$username = $dt_config['AisConfigUsername'];
$password = $dt_config['AisConfigPassword'];
$data = array(
'username' => $username,
'password' => $password
);
$result = $this->post_request($url, $data, $headers);
$sql = "INSERT INTO ais_login_log(
AisLoginLogUsername,
AisLoginLogPassword,
AisLoginLogHeaderToken,
AisLoginLogResult,
AisLoginLogCreated
)
VALUES(
?,
?,
?,
?,
NOW()
)";
$qry = $this->db_log->query($sql, array($username, $password, $dt_config['AisConfigHeaderToken'], json_encode($result)));
if (!$qry) {
$this->error_log(array('fn_name' => 'post_auth', 'message' => 'ais_login_log insert', 'query' => $this->db_log->last_query(), 'json' => json_encode($result)), 999);
exit;
}
//print_r($result);
//exit;
// Check if success
if (!$result['success']) {
$this->error_log(array('fn_name' => 'post_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($result)), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
} else {
// Update token
$token = $result['data']['token'];
;
$sql = "UPDATE ais_config SET AisConfigAuthToken = ? WHERE AisConfigID = 1";
$qry = $this->db_onedev->query($sql, array($token));
//echo $this->db_onedev->last_query();
//exit;
if (!$qry) {
$this->error_log(array('fn_name' => 'post_auth', 'message' => 'ais_config update', 'query' => $this->db_onedev->last_query(), 'json' => ''), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Update Token');
echo json_encode($errors);
exit;
}
//$success = array('status' => 'success', 'message' => 'Berhasil Login', 'token' => $token);
return $token;
}
}
function get_cost_center()
{
$userid = 999;
// Get id from query parameter
$id = $this->input->get('id') ?? null;
// if (empty($id)) {
// $errors = array('status' => 'error', 'message' => 'ID parameter is required');
// echo json_encode($errors);
// exit;
// }
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'get_cost_center_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/cost_center';
if (!empty($id)) {
$url .= '?id=' . $id;
}
$result = $this->get_request($url);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'get_cost_center', 'message' => 'failed get cost center', 'query' => '', 'json' => json_encode($result)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Get Layanan');
echo json_encode($errors);
exit;
}
$data = $result['response']['data'] ?? null;
// $data = json_encode($data);
$success = array('status' => 'success', 'message' => 'Berhasil Get Cost Center', 'data' => $data);
echo json_encode($success);
exit;
}
public function post_cost_center()
{
$userid = 999;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'post_cost_center', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/cost_center';
$headers = array(
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
);
// Get data
$sql = "SELECT * FROM nat_group WHERE Nat_GroupIsActive = 'Y'";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
$this->error_log(array('fn_name' => 'post_cost_center', 'message' => 'profit_center select', 'query' => $this->db_onedev->last_query(), 'json' => ''), 999);
exit;
}
$raw_data = $qry->result_array();
$data = array();
$errors = array();
$raw_data = array(
array(
'cost_center_code' => '1100',
'cost_center_name' => 'Laboratorium',
'cost_center_desc' => 'Laboratorium',
'status' => 'active'
),
array(
'cost_center_code' => '1200',
'cost_center_name' => 'Radiologi dan Elektromedis',
'cost_center_desc' => 'Radiologi dan Elektromedis',
'status' => 'active'
),
array(
'cost_center_code' => '1303',
'cost_center_name' => 'Layanan Lainnya',
'cost_center_desc' => 'Layanan Lainnya',
'status' => 'active'
)
);
foreach ($raw_data as $row) {
$data = $row;
$result = $this->post_request($url, $data, $headers);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'post_profit_center', 'message' => 'profit_center insert', 'query' => $this->db_onedev->last_query(), 'json' => json_encode($result)), 999);
$errors[] = array('cost_center_code' => $row['Nat_GroupID'], 'error' => $result['message']);
}
}
if(count($errors) > 0){
$success = array('status' => 'error', 'message' => 'Gagal Post Cost Center', 'errors' => $errors);
echo json_encode($success);
exit;
}else{
$success = array('status' => 'success', 'message' => 'Berhasil Post Cost Center');
echo json_encode($success);
exit;
}
}
public function post_profit_center()
{
$userid = 999;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'post_profit_center', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/profit_center';
$headers = array(
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
);
// Get data
$sql = "SELECT * FROM nat_group WHERE Nat_GroupIsActive = 'Y'";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
$this->error_log(array('fn_name' => 'post_profit_center', 'message' => 'profit_center select', 'query' => $this->db_onedev->last_query(), 'json' => ''), 999);
exit;
}
$raw_data = $qry->result_array();
$data = array();
$datas_response = array();
foreach ($raw_data as $row) {
$data = array(
'profit_center_code' => $row['Nat_GroupID'],
'profit_center_name' => $row['Nat_GroupName'],
'profit_center_desc' => $row['Nat_GroupName'],
'status' => 'active'
);
$result = $this->post_request($url, $data, $headers);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'post_profit_center', 'message' => 'profit_center insert', 'query' => $this->db_onedev->last_query(), 'json' => json_encode($result)), 999);
echo json_encode($result);
exit;
}
echo json_encode($result);
exit;
$datas_response[] = $result['response']['data'];
}
$success = array('data' => $datas_response, 'status' => 'success', 'message' => 'Berhasil Post Profit Center');
echo json_encode($success);
exit;
}
/**
* POST request to jenis layanan API
* @param array $data Array of jenis layanan data
* @return array Response from API
*/
public function post_jenis_layanan()
{
$userid = 999;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'post_jenis_layanan', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/jenislayanan';
$headers = array(
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
);
// Get data
$sql = "SELECT * FROM nat_group WHERE Nat_GroupIsActive = 'Y'";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
$this->error_log(array('fn_name' => 'post_jenis_layanan', 'message' => 'nat_group select', 'query' => $this->db_onedev->last_query(), 'json' => ''), 999);
exit;
}
$raw_data = $qry->result_array();
$data = array();
foreach ($raw_data as $row) {
$data = array(
'JenisLayananID' => $row['Nat_GroupID'],
'Nama' => $row['Nat_GroupName'],
'JenisLayananKode' => $row['Nat_GroupCode'],
'Keterangan' => '',
'LoginBuat' => 'system',
'TanggalBuat' => $row['Nat_GroupCreated']
);
$result = $this->post_request($url, $data, $headers);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'post_jenis_layanan', 'message' => 'jenislayanan insert', 'query' => $this->db_onedev->last_query(), 'json' => json_encode($result)), 999);
echo json_encode($result);
exit;
}
$sql = "INSERT INTO ais_post_jenis_layanan_log(
AisPostJenisLayananType,
AisPostJenisLayananUrl,
AisPostJenisLayananRefID,
AisPostJenisLayananPayload,
AisPostJenisLayananResponse,
AisPostJenisLayananUserID,
AisPostJenisLayananCreated
)
VALUES(
?,
?,
?,
?,
?,
?,
NOW()
)";
$qry = $this->db_log->query($sql, array(
'NEW',
$url,
$row['Nat_GroupID'],
json_encode($data),
json_encode($result),
$userid
));
if (!$qry) {
$this->error_log(array('fn_name' => 'post_jenis_layanan', 'message' => 'ais_post_jenis_layanan_log insert', 'query' => $this->db_log->last_query(), 'json' => json_encode($result)), 999);
exit;
}
}
$success = array('status' => 'success', 'message' => 'Berhasil Post Jenis Layanan');
echo json_encode($success);
exit;
}
public function post_tipe_bayar()
{
$userid = 999;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'post_tipe_bayar', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/tipebayar';
$headers = array(
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
);
// Get data
$sql = "SELECT * FROM m_paymenttype WHERE M_PaymentTypeIsActive = 'Y' AND M_PaymentTypeID = 1";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
$this->error_log(array('fn_name' => 'post_tipe_bayar', 'message' => 'payment_type select', 'query' => $this->db_onedev->last_query(), 'json' => ''), 999);
exit;
}
$raw_data_cash = $qry->row_array();
$datas = array();
$datas[] = array(
'TipeBayarID' => $raw_data_cash['M_PaymentTypeID'].'0',
'GroupBayar' => $raw_data_cash['M_PaymentTypeName'],
'Nama' => $raw_data_cash['M_PaymentTypeName'],
'NamaBank' => '',
'NomerAccount' => '',
'NamaAccount' => '',
'LoginBuat' => 'system',
'TanggalBuat' => $raw_data_cash['M_PaymentTypeCreated']
);
$sql ="SELECT
pt.M_PaymentTypeID,
pt.M_PaymentTypeCode,
pt.M_PaymentTypeName,
ba.M_BankAccountID,
ba.M_BankAccountNo,
ba.M_BankAccountName,
nb.Nat_BankID,
nb.Nat_BankCode,
nb.Nat_BankName
FROM m_paymenttype pt
CROSS JOIN m_bank_account ba
LEFT JOIN nat_bank nb ON ba.M_BankAccountNat_BankID = nb.Nat_BankID
WHERE pt.M_PaymentTypeCode != 'CASH'
AND pt.M_PaymentTypeIsActive = 'Y'
AND ba.M_BankAccountIsActive = 'Y'
ORDER BY pt.M_PaymentTypeID, ba.M_BankAccountID";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
$this->error_log(array('fn_name' => 'post_tipe_bayar', 'message' => 'payment_type select', 'query' => $this->db_onedev->last_query(), 'json' => ''), 999);
exit;
}
$raw_data = $qry->result_array();
foreach ($raw_data as $row) {
$datas[] = array(
'TipeBayarID' => $row['M_PaymentTypeID'].$row['M_BankAccountID'],
'GroupBayar' => $row['M_PaymentTypeName'],
'Nama' => $row['Nat_BankName'],
'NamaBank' => $row['Nat_BankName'],
'NomerAccount' => $row['M_BankAccountNo'],
'NamaAccount' => $row['M_BankAccountName'],
'LoginBuat' => 'system',
'TanggalBuat' => $row['M_PaymentTypeCreated']
);
}
$errors = array();
//echo json_encode($datas); exit;
foreach ($datas as $data) {
$result = $this->post_request($url, $data, $headers);
if (!$result['success']) {
$errors[] = array('TipeBayarID' => $data['TipeBayarID'], 'error' => $result['message']);
}
}
if(count($errors) > 0){
$success = array('status' => 'error', 'message' => 'Gagal Post Tipe Bayar', 'errors' => $errors);
echo json_encode($success);
exit;
}else{
$success = array('status' => 'success', 'message' => 'Berhasil Post Tipe Bayar');
echo json_encode($success);
exit;
}
}
function get_tipe_bayar()
{
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'get_tipe_bayar_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/tipebayar';
$result = $this->get_request($url);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'get_tipe_bayar', 'message' => 'failed get tipe bayar', 'query' => '', 'json' => json_encode($result)), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Get Tipe Bayar');
echo json_encode($errors);
exit;
}
$data = $result['response']['data'] ?? null;
// $data = json_encode($data);
$success = array('status' => 'success', 'message' => 'Berhasil Get Tipe Bayar', 'data' => $data);
echo json_encode($success);
exit;
}
function get_jenis_perusahaan()
{
// Get id from query parameter
$id = $this->input->get('id');
if (empty($id)) {
$errors = array('status' => 'error', 'message' => 'ID parameter is required');
echo json_encode($errors);
exit;
}
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'get_jenis_perusahaan_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/jenisperusahaan?id=' . $id;
$result = $this->get_request($url);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'get_jenis_perusahaan', 'message' => 'failed get jenis perusahaan', 'query' => '', 'json' => json_encode($result)), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Get Jenis Perusahaan');
echo json_encode($errors);
exit;
}
$data = $result['response']['data'] ?? null;
// $data = json_encode($data);
$success = array('status' => 'success', 'message' => 'Berhasil Get Jenis Perusahaan', 'data' => $data);
echo json_encode($success);
exit;
}
public function post_jenis_perusahaan()
{
$userid = 999;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'post_jenis_perusahaan_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/jenisperusahaan';
$headers = array(
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
);
// Get data
$sql = "SELECT * FROM corporate_type WHERE CorporateTypeIsActive = 'Y'";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
$this->error_log(array('fn_name' => 'post_jenis_perusahaan', 'message' => 'corporate_type select', 'query' => $this->db_onedev->last_query(), 'json' => ''), 999);
exit;
}
$raw_data = $qry->result_array();
$data = array();
foreach ($raw_data as $row) {
$data = array(
'JenisPerusahaanID' => intval($row['CorporateTypeID']),
'Nama' => $row['CorporateTypeName'],
'LoginBuat' => 'system',
'TanggalBuat' => $row['CorporateTypeCreated']
);
$result = $this->post_request($url, $data, $headers);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'post_jenis_perusahaan', 'message' => 'failed insert jenis perusahaan', 'query' => $this->db_onedev->last_query(), 'json' => json_encode($result)), 999);
echo json_encode($result);
exit;
}
}
$success = array('status' => 'success', 'message' => 'Berhasil Post Jenis Perusahaan');
echo json_encode($success);
exit;
}
public function post_pillar()
{
$userid = 999;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'post_pillar_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/pillar';
$headers = array(
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
);
// Get data
$sql = "SELECT * FROM corporate_type WHERE CorporateTypeIsActive = 'Y'";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
$this->error_log(array('fn_name' => 'post_pillar', 'message' => 'corporate_type select', 'query' => $this->db_onedev->last_query(), 'json' => ''), 999);
exit;
}
$raw_data = $qry->result_array();
$data = array();
foreach ($raw_data as $row) {
$data = array(
'pillar_code' => $row['CorporateTypeID'],
'pillar_name' => $row['CorporateTypeName'],
'pillar_desc' => '',
'status' => 'active'
);
$result = $this->post_request($url, $data, $headers);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'post_pillar', 'message' => 'failed insert pillar', 'query' => $this->db_onedev->last_query(), 'json' => json_encode($result)), 999);
echo json_encode($result);
exit;
}
}
$success = array('status' => 'success', 'message' => 'Berhasil Post Pillar');
echo json_encode($success);
exit;
}
// Get Group Layanan
function get_branch()
{
$userid = 999;
// Get id from query parameter
$id = $this->input->get('id') ?? null;
// if (empty($id)) {
// $errors = array('status' => 'error', 'message' => 'ID parameter is required');
// echo json_encode($errors);
// exit;
// }
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'get_branch_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/branch';
if (!empty($id)) {
$url .= '?id=' . $id;
}
$result = $this->get_request($url);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'get_branch', 'message' => 'failed get branch', 'query' => '', 'json' => json_encode($result)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Get Layanan');
echo json_encode($errors);
exit;
}
$data = $result['response']['data'] ?? null;
// $data = json_encode($data);
$success = array('status' => 'success', 'message' => 'Berhasil Get Branch', 'data' => $data);
echo json_encode($success);
exit;
}
public function post_branch()
{
$userid = 999;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'post_branch_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/branch';
$headers = array(
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
);
// Get data
$sql = "SELECT * FROM m_branch WHERE M_BranchIsActive = 'Y' ";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
$this->error_log(array('fn_name' => 'post_branch', 'message' => 'm_branch select', 'query' => $this->db_onedev->last_query(), 'json' => ''), 999);
exit;
}
$raw_data = $qry->result_array();
$data = array();
$errors = array();
foreach ($raw_data as $row) {
$data = array(
'branch_code' => $row['M_BranchCodeLab'],
'branch_name' => $row['M_BranchName'],
'branch_regional_code' => '',
'branch_desc' => $row['M_BranchName'],
'status' => 'active'
);
$result = $this->post_request($url, $data, $headers);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'post_pillar', 'message' => 'failed insert pillar', 'query' => $this->db_onedev->last_query(), 'json' => json_encode($result)), 999);
$errors[] = array('branch_code' => $row['M_BranchCodeLab'], 'error' => $result['message']);
}
}
if(count($errors) > 0){
$success = array('status' => 'error', 'message' => 'Gagal Post Branch', 'errors' => $errors);
echo json_encode($success);
exit;
}else{
$success = array('status' => 'success', 'message' => 'Berhasil Post Branch');
echo json_encode($success);
exit;
}
}
public function post_perusahaan_by_code()
{
$userid = 999;
$prm = $this->sys_input;
$corporate_code = $prm['corporate_code'] ?? null;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'post_perusahaan_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/master/perusahaan.php';
$headers = array(
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
);
// Get data
$sql = "SELECT *
FROM corporate
LEFT JOIN cpone_log.ais_corporate ON corporate.CorporateCode = ais_corporate.Ais_CorporateCorporateCode AND ais_corporate.Ais_CorporateIsActive = 'Y'
WHERE
CorporateIsActive = 'Y' AND CorporateCode = ? AND ais_corporate.Ais_CorporateID IS NULL
LIMIT 1";
$qry = $this->db_onedev->query($sql, [$corporate_code]);
if (!$qry) {
$this->error_log(array('fn_name' => 'post_perusahaan', 'message' => 'corporate select', 'query' => $this->db_onedev->last_query(), 'json' => ''), 999);
exit;
}
$raw_data = $qry->result_array();
$data = array();
$errors = array();
foreach ($raw_data as $row) {
$data = array(
"PerusahaanID" => $row['CorporateCode'],
"KodePerusahaan" => $row['CorporateCode'],
"Nama" => $row['CorporateName'],
"JenisPerusahaanID" => intval($row['CorporateCorporateTypeID']),
"Penjamin" => "N",
"Asuransi" => "N",
"Provider" => "N",
"Pabrik" => "N",
"Supplier" => "N",
"Manufaktur" => "N",
"Alamat" => $row['CorporateAddress'],
"Kota" => $row['CorporateAddressCity'],
"KodePos" => "",
"Propinsi" => $row['CorporateAddressState'],
"Negara" => $row['CorporateAddressCountry'],
"Telepon" => $row['CorporatePhone'],
"NamaKontak" => $row['CorporatePICName'],
"BankName" => "",
"RekeningNo" => "",
"RekeningName" => "",
"Website" => "",
"Email" => $row['CorporateEmail'],
"Fax" => "",
"LoginBuat" => "system",
"TanggalBuat" => $row['CorporateCreated']
);
$result = $this->post_request($url, $data, $headers);
if (!$result['success']) {
$errors[] = array('PerusahaanID' => $row['CorporateCode'], 'error' => $result['message']);
}
$sql = "INSERT INTO ais_corporate(
Ais_CorporateCorporateCode,
Ais_CorporateType,
Ais_CorporateJSON,
Ais_CorporateStatus,
Ais_CorporateResponse,
Ais_CorporateLastUpdate,
Ais_CorporateUserID
) VALUES (?, ?, ?, ?, ?, NOW(), ?)";
$qry = $this->db_log->query($sql, [
$row['CorporateCode'],
'ADD',
json_encode($data),
$result['success']?'SUCCESS':'FAILED',
json_encode($result),
$userid]);
if (!$qry) {
$this->error_log(array('fn_name' => 'post_perusahaan', 'message' => 'ais_corporate insert', 'query' => $this->db_log->last_query(), 'json' => ''), 999);
exit;
}
}
if(count($errors) > 0){
$success = array('status' => 'error', 'message' => 'Gagal Post Perusahaan', 'errors' => $errors);
echo json_encode($success);
exit;
}else{
$success = array('status' => 'success', 'message' => 'Berhasil Post Perusahaan');
echo json_encode($success);
exit;
}
}
public function re_post_perusahaan_by_code()
{
$userid = 999;
$prm = $this->sys_input;
$corporate_code = $prm['corporate_code'] ?? null;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 're_post_perusahaan_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/master/perusahaan.php';
$headers = array(
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
);
// Get data
$sql = "SELECT *
FROM corporate
LEFT JOIN cpone_log.ais_corporate ON corporate.CorporateCode = ais_corporate.Ais_CorporateCorporateCode AND ais_corporate.Ais_CorporateIsActive = 'Y'
WHERE
CorporateIsActive = 'Y' AND CorporateCode = ? LIMIT 1";
$qry = $this->db_onedev->query($sql, [$corporate_code]);
if (!$qry) {
$this->error_log(array('fn_name' => 're_post_perusahaan', 'message' => 'corporate select', 'query' => $this->db_onedev->last_query(), 'json' => ''), 999);
exit;
}
$raw_data = $qry->result_array();
$data = array();
$errors = array();
foreach ($raw_data as $row) {
$data = array(
"PerusahaanID" => $row['CorporateCode'],
"KodePerusahaan" => $row['CorporateCode'],
"Nama" => $row['CorporateName'],
"JenisPerusahaanID" => intval($row['CorporateCorporateTypeID']),
"Penjamin" => "N",
"Asuransi" => "N",
"Provider" => "N",
"Pabrik" => "N",
"Supplier" => "N",
"Manufaktur" => "N",
"Alamat" => $row['CorporateAddress'],
"Kota" => $row['CorporateAddressCity'],
"KodePos" => "",
"Propinsi" => $row['CorporateAddressState'],
"Negara" => $row['CorporateAddressCountry'],
"Telepon" => $row['CorporatePhone'],
"NamaKontak" => $row['CorporatePICName'],
"BankName" => "",
"RekeningNo" => "",
"RekeningName" => "",
"Website" => "",
"Email" => $row['CorporateEmail'],
"Fax" => "",
"LoginBuat" => "system",
"TanggalBuat" => $row['CorporateCreated']
);
$result = $this->put_request($url, $data, $headers);
if (!$result['success']) {
$errors[] = array('PerusahaanID' => $row['CorporateCode'], 'error' => $result['message']);
}
$sql = "INSERT INTO ais_corporate(
Ais_CorporateCorporateCode,
Ais_CorporateType,
Ais_CorporateJSON,
Ais_CorporateStatus,
Ais_CorporateResponse,
Ais_CorporateLastUpdate,
Ais_CorporateUserID
) VALUES (?, ?, ?, ?, ?, NOW(), ?)";
$qry = $this->db_log->query($sql, [
$row['CorporateCode'],
'ADD',
json_encode($data),
$result['success']?'SUCCESS':'FAILED',
json_encode($result),
$userid]);
if (!$qry) {
$this->error_log(array('fn_name' => 'post_perusahaan', 'message' => 'ais_corporate insert', 'query' => $this->db_log->last_query(), 'json' => ''), 999);
exit;
}
}
if(count($errors) > 0){
$success = array('status' => 'error', 'message' => 'Gagal Post Perusahaan', 'errors' => $errors);
echo json_encode($success);
exit;
}else{
$success = array('status' => 'success', 'message' => 'Berhasil Post Perusahaan');
echo json_encode($success);
exit;
}
}
public function post_perusahaan_all()
{
$userid = 999;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'post_perusahaan_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/master/perusahaan.php';
$headers = array(
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
);
// Get data
$sql = "SELECT *
FROM corporate
LEFT JOIN cpone_log.ais_corporate ON corporate.CorporateCode = ais_corporate.Ais_CorporateCorporateCode AND ais_corporate.Ais_CorporateIsActive = 'Y'
WHERE
CorporateIsActive = 'Y' AND ais_corporate.Ais_CorporateID IS NULL
LIMIT 20";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
$this->error_log(array('fn_name' => 'post_perusahaan', 'message' => 'corporate select', 'query' => $this->db_onedev->last_query(), 'json' => ''), 999);
exit;
}
$raw_data = $qry->result_array();
$data = array();
$errors = array();
foreach ($raw_data as $row) {
$data = array(
"PerusahaanID" => $row['CorporateCode'],
"KodePerusahaan" => $row['CorporateCode'],
"Nama" => $row['CorporateName'],
"JenisPerusahaanID" => intval($row['CorporateCorporateTypeID']),
"Penjamin" => "N",
"Asuransi" => "N",
"Provider" => "N",
"Pabrik" => "N",
"Supplier" => "N",
"Manufaktur" => "N",
"Alamat" => $row['CorporateAddress'],
"Kota" => $row['CorporateAddressCity'],
"KodePos" => "",
"Propinsi" => $row['CorporateAddressState'],
"Negara" => $row['CorporateAddressCountry'],
"Telepon" => $row['CorporatePhone'],
"NamaKontak" => $row['CorporatePICName'],
"BankName" => "",
"RekeningNo" => "",
"RekeningName" => "",
"Website" => "",
"Email" => $row['CorporateEmail'],
"Fax" => "",
"LoginBuat" => "system",
"TanggalBuat" => $row['CorporateCreated']
);
$result = $this->post_request($url, $data, $headers);
if (!$result['success']) {
$errors[] = array('PerusahaanID' => $row['CorporateCode'], 'error' => $result['message']);
}
$sql = "INSERT INTO ais_corporate(
Ais_CorporateCorporateCode,
Ais_CorporateType,
Ais_CorporateJSON,
Ais_CorporateStatus,
Ais_CorporateResponse,
Ais_CorporateLastUpdate,
Ais_CorporateUserID
) VALUES (?, ?, ?, ?, ?, NOW(), ?)";
$qry = $this->db_log->query($sql, [
$row['CorporateCode'],
'ADD',
json_encode($data),
$result['success']?'SUCCESS':'FAILED',
json_encode($result),
$userid]);
if (!$qry) {
$this->error_log(array('fn_name' => 'post_perusahaan', 'message' => 'ais_corporate insert', 'query' => $this->db_log->last_query(), 'json' => ''), 999);
exit;
}
}
if(count($errors) > 0){
$success = array('status' => 'error', 'message' => 'Gagal Post Perusahaan', 'errors' => $errors);
echo json_encode($success);
exit;
}else{
$success = array('status' => 'success', 'message' => 'Berhasil Post Perusahaan');
echo json_encode($success);
exit;
}
}
function get_perusahaan_by_code()
{
$corporate_code = $this->sys_input['corporate_code'] ?? null;
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'get_perusahaan_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . 'api/master/perusahaan.php?id='.$corporate_code;
$result = $this->get_request($url);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'get_perusahaan', 'message' => 'failed get perusahaan', 'query' => '', 'json' => json_encode($result)), 999);
$errors = array('status' => 'error', 'message' => 'Gagal Get Perusahaan');
echo json_encode($errors);
exit;
}
$data = $result['response']['data'] ?? null;
$count = is_array($data) ? count($data) : 0;
$data = json_encode($data);
$success = array('status' => 'success', 'message' => 'Berhasil Get Perusahaan', 'count' => $count, 'data' => $data);
echo json_encode($success);
}
public function post_perusahaan_bulk()
{
$userid = 999;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log([
'fn_name' => 'post_perusahaan_bulk_auth',
'message' => 'failed auth',
'query' => '',
'json' => json_encode($login)
], 999);
echo json_encode(['status' => 'error', 'message' => 'Gagal Login']);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/master/perusahaan.php';
$headers = [
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
];
// Get data corporate aktif
$sql = "SELECT
c.*
FROM
corporate c
LEFT JOIN
cpone_log.ais_corporate a
ON a.Ais_CorporateCorporateCode = c.CorporateCode
WHERE
c.CorporateIsActive = 'Y'
AND a.Ais_CorporateCorporateCode IS NULL
LIMIT 10;";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
$this->error_log([
'fn_name' => 'post_perusahaan_bulk',
'message' => 'corporate select',
'query' => $this->db_onedev->last_query(),
'json' => ''
], $userid);
exit;
}
$raw_data = $qry->result_array();
if (empty($raw_data)) {
echo json_encode(['status' => 'error', 'message' => 'Tidak ada data perusahaan aktif']);
exit;
}
$payload = ['data' => []];
$terlewati = [];
foreach ($raw_data as $row) {
$corporateCode = $row['CorporateCode'];
$searchurl = $baseUrl . '/api/master/perusahaan.php?id=' . $corporateCode;
// cek apakah data sudah ada di server AIS
$result = $this->get_request($searchurl);
if (!$result['success']) {
$this->error_log([
'fn_name' => 'get_perusahaan_existing',
'message' => 'failed get perusahaan existing',
'query' => '',
'json' => json_encode($result)
], $userid);
echo json_encode(['status' => 'error', 'message' => 'Gagal Get Data Perusahaan']);
exit;
}
// Normalisasi hasil
$existing_data = $result['response']['data'] ?? [];
// Jika API balikan object tunggal, ubah ke array
if (!empty($existing_data) && isset($existing_data['PerusahaanID'])) {
$existing_data = [$existing_data];
}
// Deteksi apakah sudah ada
$sudah_ada = false;
if (is_array($existing_data) && count($existing_data) > 0) {
foreach ($existing_data as $d) {
if (isset($d['PerusahaanID']) && $d['PerusahaanID'] == $corporateCode) {
$sudah_ada = true;
}
}
}
if ($sudah_ada) {
$terlewati[] = $corporateCode;
}
// Belum ada → masukkan ke payload
$payload['data'][] = [
"PerusahaanID" => $row['CorporateCode'],
"KodePerusahaan" => $row['CorporateCode'],
"Nama" => $row['CorporateName'],
"JenisPerusahaanID" => intval($row['CorporateCorporateTypeID']),
"Penjamin" => "N",
"Asuransi" => "N",
"Provider" => "N",
"Pabrik" => "N",
"Supplier" => "N",
"Manufaktur" => "N",
"Alamat" => $row['CorporateAddress'],
"Kota" => $row['CorporateAddressCity'],
"KodePos" => "",
"Propinsi" => $row['CorporateAddressState'],
"Negara" => $row['CorporateAddressCountry'],
"Telepon" => $row['CorporatePhone'],
"NamaKontak" => $row['CorporatePICName'],
"BankName" => "",
"RekeningNo" => "",
"RekeningName" => "",
"Website" => "",
"Email" => $row['CorporateEmail'],
"Fax" => "",
"LoginBuat" => "system",
"TanggalBuat" => $row['CorporateCreated']
];
}
// === Insert data terlewati ke tabel ais_corporate ===
if (!empty($terlewati)) {
foreach ($terlewati as $code) {
$sql = "INSERT INTO ais_corporate (
Ais_CorporateCorporateCode,
Ais_CorporateLastUpdate,
Ais_CorporateUserID
)
VALUES(?, NOW(), ?)";
$qry = $this->db_log->query($sql, array($code, $userid));
// Log ke db_one_log
$this->error_log([
'fn_name' => 'post_perusahaan_bulk_insert_log',
'message' => 'Insert ke tabel ais_corporate (data terlewati)',
'query' => $this->db_log->last_query()
], $userid);
}
}
// Kirim ke API
$result = $this->post_request($url, $payload, $headers);
if (!$result['success']) {
$this->error_log([
'fn_name' => 'post_perusahaan_bulk',
'message' => 'failed insert perusahaan bulk',
'query' => $this->db_onedev->last_query(),
'json' => json_encode($result)
], $userid);
echo json_encode([
'status' => 'error',
'message' => 'Gagal insert bulk perusahaan',
'response' => $result,
'terlewati' => $terlewati
]);
exit;
}
// === Insert ke tabel ais_corporate ===
foreach ($payload['data'] as $item) {
$sql = "INSERT INTO ais_corporate (
Ais_CorporateCorporateCode,
Ais_CorporateLastUpdate,
Ais_CorporateUserID
)
VALUES(
?,
NOW(),
?
)";
$qry = $this->db_log->query($sql, array($item['PerusahaanID'], $userid));
// Log ke db_one_log
$this->error_log([
'fn_name' => 'post_perusahaan_bulk_insert_log',
'message' => 'Insert ke tabel ais_corporate',
'query' => $this->db_log->last_query()
], $userid);
}
echo json_encode([
'status' => 'success',
'message' => 'Berhasil Post Perusahaan Baru',
'response' => $result,
'Sudah Terinsert' => $terlewati
]);
exit;
}
public function edit_perusahaan()
{
$userid = 999;
$prm = $this->sys_input;
$id = $prm['PerusahaanID'];
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log([
'fn_name' => 'post_perusahaan_bulk_auth',
'message' => 'failed auth',
'query' => '',
'json' => json_encode($login)
], $userid);
echo json_encode(['status' => 'error', 'message' => 'Gagal Login']);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/master/perusahaan.php';
$searchurl = $baseUrl . '/api/master/perusahaan.php';
if (!empty($id)) {
$searchurl .= '?id=' . $id;
}
$result = $this->get_request($searchurl);
$headers = [
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
];
// Get data
// $sql = "SELECT * FROM corporate WHERE CorporateIsActive = 'Y' AND CorporateCode = ? limit 1";
// $qry = $this->db_onedev->query($sql, [$prm['PerusahaanID']]);
$result = $this->get_request($searchurl);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'get_perusahaan', 'message' => 'failed get perusahaan', 'query' => '', 'json' => json_encode($result)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Get Perusahaan');
echo json_encode($errors);
exit;
}
$data = $result['response']['data'] ?? null;
$count = is_array($data) ? count($data) : 0;
$data = json_encode($data);
if ($data == null) {
} else {
$data = array(
"PerusahaanID" => $prm['PerusahaanID'],
"KodePerusahaan" => $prm['KodePerusahaan'],
"Nama" => $prm['Nama'],
"JenisPerusahaanID" => intval($prm['JenisPerusahaanID']),
"Penjamin" => $prm['Penjamin'],
"Asuransi" => $prm['Asuransi'],
"Provider" => $prm['Provider'],
"Pabrik" => $prm['Pabrik'],
"Supplier" => $prm['Supplier'],
"Manufaktur" => $prm['Manufaktur'],
"Alamat" => $prm['Alamat'],
"Kota" => $prm['Kota'],
"KodePos" => $prm['KodePos'],
"Propinsi" => $prm['Propinsi'],
"Negara" => $prm['Negara'],
"Telepon" => $prm['Telepon'],
"NamaKontak" => isset($prm['NamaKontak']) ? $prm['NamaKontak'] : "",
"BankName" => isset($prm['BankName']) ? $prm['BankName'] : "",
"RekeningNo" => isset($prm['RekeningNo']) ? $prm['RekeningNo'] : "",
"RekeningName" => isset($prm['RekeningName']) ? $prm['RekeningName'] : "",
"Website" => isset($prm['Website']) ? $prm['Website'] : "",
"Email" => isset($prm['Email']) ? $prm['Email'] : "",
"Fax" => $prm['Fax'],
"LoginBuat" => $prm['LoginBuat'],
"TanggalBuat" => $prm['TanggalBuat']
);
$result = $this->put_request($url, $data, $headers);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'put_perusahaan', 'message' => 'failed edit perusahaan', 'query' => $this->db_onedev->last_query(), 'json' => json_encode($result)), $userid);
echo json_encode($result);
exit;
}
}
$success = array('status' => 'success', 'message' => 'Berhasil Put Perusahaan');
echo json_encode($success);
exit;
}
// Get Jenis Layanan
function get_jenislayanan()
{
$userid = 999;
// Get id from query parameter
$id = $this->input->get('id') ?? null;
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'get_jenis_layanan_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/jenislayanan';
if (!empty($id)) {
$url .= '?id=' . $id;
}
$result = $this->get_request($url);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'get_jenis_layanan', 'message' => 'failed get jenis layanan', 'query' => '', 'json' => json_encode($result)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Get Jenis Layanan');
echo json_encode($errors);
exit;
}
$data = $result['response']['data'] ?? null;
// $data = json_encode($data);
$success = array('status' => 'success', 'message' => 'Berhasil Get Jenis Layanan', 'data' => $data);
echo json_encode($success);
exit;
}
// Post Jenis Layanan
public function post_jenislayanan()
{
$userid = 999;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'post_jenis_layanan_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/jenislayanan';
$headers = array(
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
);
// Get data nat_subgroup
$sql = "SELECT *
FROM nat_test n
LEFT JOIN cpone_log.ais_nattest a
ON n.Nat_TestCode = a.Ais_NatTestNatTestCode
WHERE n.Nat_TestIsActive = 'Y'
AND a.Ais_NatTestNatTestCode IS NULL
limit 1
";
$qry = $this->db_onedev->query($sql);
// print_r($qry->result_array());
// exit;
if (!$qry) {
$this->error_log(array('fn_name' => 'post_jenis_layanan', 'message' => 'jenis layanan select', 'query' => $this->db_onedev->last_query(), 'json' => ''), $userid);
exit;
}
$raw_data = $qry->result_array();
if(empty($raw_data)){
$errors = array('status' => 'error', 'message' => 'Tidak ada data jenis layanan yang belum dikirim');
echo json_encode($errors);
exit;
}
$data = array();
foreach ($raw_data as $row) {
$data = array(
"JenisLayananID" => intval($row['Nat_TestID']),
"Nama" => $row['Nat_TestName'],
"JenisLayananKode" => $row['Nat_TestCode'],
"Keterangan" => '',
"LoginBuat" => 'system',
"TanggalBuat" => $row['Nat_TestCreated']
);
$result = $this->post_request($url, $data, $headers);
echo json_encode($result);
exit;
if (!$result['success']) {
if ($result['status'] == 406) {
$sql = "INSERT INTO ais_nattest (
Ais_NatTestNatTestCode,
Ais_NatTestLastUpdate,
Ais_NatTestUserID
)
VALUES(
?,
NOW(),
?
)";
$qry = $this->db_log->query($sql, array($row['Nat_TestCode'], $userid));
// Log ke db_one_log
$this->error_log([
'fn_name' => 'post_jenis_layanan_insert_log',
'message' => 'Insert ke tabel ais_nattest',
'query' => $this->db_log->last_query()
], $userid);
// lanjut ke record berikutnya
continue;
}
$this->error_log(array('fn_name' => 'post_jenislayanan', 'message' => 'failed insert jenislayanan', 'query' => $this->db_onedev->last_query(), 'json' => json_encode($result)), $userid);
}
$sql = "INSERT INTO ais_nattest (
Ais_NatTestNatTestCode,
Ais_NatTestLastUpdate,
Ais_NatTestUserID
)
VALUES(
?,
NOW(),
?
)";
$qry = $this->db_log->query($sql, array($row['Nat_TestCode'], $userid));
// Log ke db_one_log
$this->error_log([
'fn_name' => 'post_jenis_layanan_insert_log',
'message' => 'Insert ke tabel ais_nattest',
'query' => $this->db_log->last_query()
], $userid);
}
$data = $result['response']['data'] ?? null;
// $data = json_encode($data);
$success = array('status' => 'success', 'message' => 'Berhasil Post Jenis Layanan', 'data' => $data);
echo json_encode($success);
exit;
}
// Get Layanan
function get_layanan()
{
$userid = 999;
// Get id from query parameter
$id = $this->input->get('id') ?? null;
// if (empty($id)) {
// $errors = array('status' => 'error', 'message' => 'ID parameter is required');
// echo json_encode($errors);
// exit;
// }
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'get_layanan_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/masterlayanan';
if (!empty($id)) {
$url .= '?id=' . $id;
}
$result = $this->get_request($url);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'get_layanan', 'message' => 'failed get layanan', 'query' => '', 'json' => json_encode($result)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Get Layanan');
echo json_encode($errors);
exit;
}
$data = $result['response']['data'] ?? null;
// $data = json_encode($data);
$success = array('status' => 'success', 'message' => 'Berhasil Get Layanan', 'data' => $data);
echo json_encode($success);
exit;
}
public function post_layanan_by_code()
{
$userid = 999;
$prm = $this->sys_input;
$layanan_code = $prm['layanan_code'] ?? null;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log([
'fn_name' => 'post_layanan_auth',
'message' => 'failed auth',
'query' => '',
'json' => json_encode($login)
], $userid);
echo json_encode(['status' => 'error', 'message' => 'Gagal Login']);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/masterlayanan';
$headers = [
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
];
// Ambil data layanan dari tabel t_test yang belum dikirim
$sql = "SELECT *
FROM t_test t
JOIN nat_test nt ON t.T_TestNat_TestID = nt.Nat_TestID
JOIN ais_map_jenislayanan ON Ais_MapJenisLayananNat_GroupID = nt.Nat_TestNat_GroupID
LEFT JOIN cpone_log.ais_test a
ON t.T_TestSasCode = a.Ais_TestTestSasCode
WHERE t.T_TestIsActive = 'Y' AND t.T_TestSasCode = ?
AND a.Ais_TestTestSasCode IS NULL
";
$qry = $this->db_onedev->query($sql, [$layanan_code]);
if (!$qry) {
$this->error_log([
'fn_name' => 'post_layanan',
'message' => 'layanan select failed',
'query' => $this->db_onedev->last_query(),
'json' => ''
], $userid);
exit;
}
$raw_data = $qry->result_array();
if(empty($raw_data)){
$errors = array('status' => 'error', 'message' => 'Tidak ada data layanan yang belum dikirim untuk kode layanan: ' . $layanan_code);
echo json_encode($errors);
exit;
}
$result_data = [];
foreach ($raw_data as $row) {
$data = [
"LayananID" => $row['T_TestSasCode'],
"JenisLayananID" => $row['Ais_MapJenisLayananJenisLayananID'],
"Nama" => $row['T_TestName'],
"LayananKode" => $row['T_TestSasCode'],
"GroupLayanan2ID" => $row['T_TestSasCode'],
"SubGroupLayanan2ID" => $row['T_TestSasCode']
];
$result = $this->post_request($url, $data, $headers);
if($result['error'] == null && $result['success'] == true){
$result_data[] = $data;
$sql = "INSERT INTO ais_test (
Ais_TestTestSasCode,
Ais_TestLastUpdate,
Ais_TestUserID
)
VALUES (?, NOW(), ?)";
$this->db_log->query($sql, [$row['T_TestSasCode'], $userid]);
}
else{
$this->error_log([
'fn_name' => 'post_layanan',
'message' => 'failed insert layanan ke AIS',
'query' => $this->db_onedev->last_query(),
'json' => json_encode($result)
], $userid);
}
}
$data = $result_data;
$success = [
'status' => 'success',
'message' => 'Berhasil Post Layanan',
'data' => $data
];
echo json_encode($success);
exit;
}
// Post Jenis Layanan
public function post_layanan_all()
{
$userid = 999;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log([
'fn_name' => 'post_layanan_auth',
'message' => 'failed auth',
'query' => '',
'json' => json_encode($login)
], $userid);
echo json_encode(['status' => 'error', 'message' => 'Gagal Login']);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/masterlayanan';
$headers = [
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
];
// Ambil data layanan dari tabel t_test yang belum dikirim
$sql = "SELECT *
FROM t_test t
JOIN nat_test nt ON t.T_TestNat_TestID = nt.Nat_TestID
JOIN ais_map_jenislayanan ON Ais_MapJenisLayananNat_GroupID = nt.Nat_TestNat_GroupID
LEFT JOIN cpone_log.ais_test a
ON t.T_TestSasCode = a.Ais_TestTestSasCode
WHERE t.T_TestIsActive = 'Y'
AND a.Ais_TestTestSasCode IS NULL
";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
$this->error_log([
'fn_name' => 'post_layanan',
'message' => 'layanan select failed',
'query' => $this->db_onedev->last_query(),
'json' => ''
], $userid);
exit;
}
$raw_data = $qry->result_array();
if(empty($raw_data)){
$errors = array('status' => 'error', 'message' => 'Tidak ada data layanan yang belum dikirim');
echo json_encode($errors);
exit;
}
$result_data = [];
foreach ($raw_data as $row) {
$data = [
"LayananID" => $row['T_TestSasCode'],
"JenisLayananID" => $row['Ais_MapJenisLayananJenisLayananID'],
"Nama" => $row['T_TestName'],
"LayananKode" => $row['T_TestSasCode'],
"GroupLayanan2ID" => $row['T_TestSasCode'],
"SubGroupLayanan2ID" => $row['T_TestSasCode']
];
$result = $this->post_request($url, $data, $headers);
if($result['error'] == null && $result['success'] == true){
$result_data[] = $data;
$sql = "INSERT INTO ais_test (
Ais_TestTestSasCode,
Ais_TestLastUpdate,
Ais_TestUserID
)
VALUES (?, NOW(), ?)";
$this->db_log->query($sql, [$row['T_TestSasCode'], $userid]);
}
else{
$this->error_log([
'fn_name' => 'post_layanan',
'message' => 'failed insert layanan ke AIS',
'query' => $this->db_onedev->last_query(),
'json' => json_encode($result)
], $userid);
}
}
$data = $result_data;
$success = [
'status' => 'success',
'message' => 'Berhasil Post Layanan',
'data' => $data
];
echo json_encode($success);
exit;
}
// Post Jenis Layanan
public function edit_layanan()
{
$userid = 999;
$prm = $this->sys_input;
$layanan_code = $prm['layanan_code'] ?? null;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log([
'fn_name' => 'post_layanan_auth',
'message' => 'failed auth',
'query' => '',
'json' => json_encode($login)
], $userid);
echo json_encode(['status' => 'error', 'message' => 'Gagal Login']);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/masterlayanan';
$headers = [
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
];
// Ambil data layanan dari tabel t_test yang belum dikirim
$sql = "SELECT *
FROM t_test t
JOIN nat_test nt ON t.T_TestNat_TestID = nt.Nat_TestID
JOIN ais_map_jenislayanan ON Ais_MapJenisLayananNat_GroupID = nt.Nat_TestNat_GroupID
WHERE t.T_TestIsActive = 'Y' AND t.T_TestSasCode = ?
LIMIT 1
";
$qry = $this->db_onedev->query($sql, [$layanan_code]);
if (!$qry) {
$this->error_log([
'fn_name' => 'edit_layanan',
'message' => 'layanan select failed',
'query' => $this->db_onedev->last_query(),
'json' => ''
], $userid);
exit;
}
$raw_data = $qry->result_array();
if(empty($raw_data)){
$errors = array('status' => 'error', 'message' => 'Tidak ada data layanan yang belum dikirim');
echo json_encode($errors);
exit;
}
$result_data = [];
foreach ($raw_data as $row) {
$data = [
"LayananID" => $row['T_TestSasCode'],
"JenisLayananID" => $row['Ais_MapJenisLayananJenisLayananID'],
"Nama" => $row['T_TestName'],
"LayananKode" => $row['T_TestSasCode'],
"GroupLayanan2ID" => $row['T_TestSasCode'],
"SubGroupLayanan2ID" => $row['T_TestSasCode']
];
$result = $this->put_request($url, $data, $headers);
if($result['error'] == null && $result['success'] == true){
$result_data[] = $data;
$sql = "INSERT INTO ais_test (
Ais_TestTestSasCode,
Ais_TestLastUpdate,
Ais_TestUserID
)
VALUES (?, NOW(), ?)";
$this->db_log->query($sql, [$row['T_TestSasCode'], $userid]);
}
else{
$this->error_log([
'fn_name' => 'post_layanan',
'message' => 'failed insert layanan ke AIS',
'query' => $this->db_onedev->last_query(),
'json' => json_encode($result)
], $userid);
}
}
$data = $result_data;
$success = [
'status' => 'success',
'message' => 'Berhasil Post Layanan',
'data' => $data
];
echo json_encode($success);
exit;
}
// Get Group Layanan
function get_grouplayanan()
{
$userid = 999;
// Get id from query parameter
$id = $this->input->get('id') ?? null;
// if (empty($id)) {
// $errors = array('status' => 'error', 'message' => 'ID parameter is required');
// echo json_encode($errors);
// exit;
// }
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'get_grouplayanan_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/grouplayanan';
if (!empty($id)) {
$url .= '?id=' . $id;
}
$result = $this->get_request($url);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'get_grouplayanan', 'message' => 'failed get group layanan', 'query' => '', 'json' => json_encode($result)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Get Layanan');
echo json_encode($errors);
exit;
}
$data = $result['response']['data'] ?? null;
// $data = json_encode($data);
$success = array('status' => 'success', 'message' => 'Berhasil Get GroupLayanan', 'data' => $data);
echo json_encode($success);
exit;
}
// Post Group Layanan
public function post_grouplayanan()
{
$userid = 999;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log([
'fn_name' => 'post_grouplayanan_auth',
'message' => 'failed auth',
'query' => '',
'json' => json_encode($login)
], $userid);
echo json_encode(['status' => 'error', 'message' => 'Gagal Login']);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/grouplayanan';
$headers = [
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
];
// Ambil data layanan dari tabel t_test yang belum dikirim
$sql = "SELECT *
FROM nat_group n
LEFT JOIN cpone_log.ais_natgroup a
ON n.Nat_GroupID = a.Ais_NatGroupNatGroupID
WHERE n.Nat_GroupIsActive = 'Y'
AND a.Ais_NatGroupNatGroupID IS NULL
LIMIT 10";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
$this->error_log([
'fn_name' => 'post_grouplayanan',
'message' => 'group layanan select failed',
'query' => $this->db_onedev->last_query(),
'json' => ''
], $userid);
exit;
}
$raw_data = $qry->result_array();
$result_data = [];
foreach ($raw_data as $row) {
$data = [
"GroupLayananID" => $row['Nat_GroupID'],
"Nama" => $row['Nat_GroupName'],
"GroupLayananKode" => $row['Nat_GroupCode'],
"DepartemenID" => 'LAB-WESTERINDO-01',
"JumlahLayanan" => 0,
"LoginBuat" => 'system',
"TanggalBuat" => $row['Nat_GroupCreated']
];
$result = $this->post_request($url, $data, $headers);
if (!$result['success']) {
if ($result['status'] == 406) {
$sql = "INSERT INTO ais_natgroup (
Ais_NatGroupNatGroupID,
Ais_NatGroupLastUpdated,
Ais_NatGroupUserID
)
VALUES (?, NOW(), ?)";
$this->db_log->query($sql, [$row['Nat_GroupID'], $userid]);
continue;
}
$this->error_log([
'fn_name' => 'post_grouplayanan',
'message' => 'failed insert layanan ke AIS',
'query' => $this->db_onedev->last_query(),
'json' => json_encode($result)
], $userid);
echo json_encode($result);
exit;
}
// Simpan log ke cpone_log
$sql = "INSERT INTO ais_natgroup (
Ais_NatGroupNatGroupID,
Ais_NatGroupLastUpdated,
Ais_NatGroupUserID
)
VALUES (?, NOW(), ?)";
$this->db_log->query($sql, [$row['Nat_GroupID'], $userid]);
// Simpan hasil dari response AIS (jika ada)
$result_data[] = $result['response']['data'] ?? $data;
}
$data = $result_data;
$success = [
'status' => 'success',
'message' => 'Berhasil Post Layanan',
'data' => $data
];
echo json_encode($success);
exit;
}
// Get Department
function get_departemen()
{
$userid = 999;
// Get id from query parameter
$id = $this->input->get('id') ?? null;
// if (empty($id)) {
// $errors = array('status' => 'error', 'message' => 'ID parameter is required');
// echo json_encode($errors);
// exit;
// }
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'get_departemen_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/departemen';
if (!empty($id)) {
$url .= '?id=' . $id;
}
$result = $this->get_request($url);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'get_departemen', 'message' => 'failed get departemen', 'query' => '', 'json' => json_encode($result)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Get Departemen');
echo json_encode($errors);
exit;
}
$data = $result['response']['data'] ?? null;
// $data = json_encode($data);
$success = array('status' => 'success', 'message' => 'Berhasil Get Departemen', 'data' => $data);
echo json_encode($success);
exit;
}
// Post Department
public function post_departemen()
{
$userid = 999;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log([
'fn_name' => 'post_departemen_auth',
'message' => 'failed auth',
'query' => '',
'json' => json_encode($login)
], $userid);
echo json_encode(['status' => 'error', 'message' => 'Gagal Login']);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/departemen';
$headers = [
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
];
// Ambil data layanan dari tabel t_test yang belum dikirim
// $sql = "SELECT *
// FROM nat_group n
// LEFT JOIN cpone_log.ais_natgroup a
// ON n.Nat_GroupID = a.Ais_NatGroupNatGroupID
// WHERE n.Nat_GroupIsActive = 'Y'
// AND a.Ais_NatGroupNatGroupID IS NULL
// LIMIT 10";
// $qry = $this->db_onedev->query($sql);
// if (!$qry) {
// $this->error_log([
// 'fn_name' => 'post_grouplayanan',
// 'message' => 'group layanan select failed',
// 'query' => $this->db_onedev->last_query(),
// 'json' => ''
// ], $userid);
// exit;
// }
// $raw_data = $qry->result_array();
$raw_data = [
[
"DepartemenNama" => "LABOLATORIUM KLINIS",
"DepartemenID" => "LAB-WESTERINDO-01",
"DepartemenKodeDepartemen" => "LAB-WESTERINDO-01",
"DepartemenTanggalBuat" => "0000-00-00 00:00:00"
]
];
$result_data = [];
foreach ($raw_data as $row) {
$data = [
"Nama" => $row['DepartemenNama'],
"DepartemenID" => $row['DepartemenID'],
"KodeDepartemen" => $row['DepartemenKodeDepartemen'],
"NamaModulExternal" => 'LABOLATORIUM',
"LoginBuat" => 'system',
"TanggalBuat" => $row['DepartemenTanggalBuat']
];
$result = $this->post_request($url, $data, $headers);
if (!$result['success']) {
if ($result['status'] == 406) {
$sql = "INSERT INTO ais_departemen (
Ais_DepartemenDepartemenID,
Ais_DepartemenLastUpdated,
Ais_DepartemenUserID
)
VALUES (?, NOW(), ?)";
$this->db_log->query($sql, [$row['DepartemenID'], $userid]);
continue;
}
$this->error_log([
'fn_name' => 'post_departemen',
'message' => 'failed insert Departemen ke AIS',
'query' => $this->db_onedev->last_query(),
'json' => json_encode($result)
], $userid);
echo json_encode($result);
exit;
}
// Simpan log ke cpone_log
$sql = "INSERT INTO ais_departemen (
Ais_DepartemenDepartemenID,
Ais_DepartemenLastUpdated,
Ais_DepartemenUserID
)
VALUES (?, NOW(), ?)";
$this->db_log->query($sql, [$row['DepartemenID'], $userid]);
// Simpan hasil dari response AIS (jika ada)
$result_data[] = $result['response']['data'] ?? $data;
}
$data = $result_data;
$success = [
'status' => 'success',
'message' => 'Berhasil Post Departmen',
'data' => $data
];
echo json_encode($success);
exit;
}
public function post_packet_by_code()
{
$userid = 999;
$prm = $this->sys_input;
$packet_code = $prm['packet_code'] ?? null;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log([
'fn_name' => 'post_packet_by_code_auth',
'message' => 'failed auth',
'query' => '',
'json' => json_encode($login)
], $userid);
echo json_encode(['status' => 'error', 'message' => 'Gagal Login']);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/paket';
$headers = [
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
];
$sql = "SELECT *
FROM t_packet
LEFT JOIN cpone_log ON T_PacketSasCode = Ais_PacketT_PacketSasCode AND
Ais_PacketStatus = 'SUCCESS'
WHERE
T_PacketIsActive = 'Y' AND T_PacketSasCode = ? AND Ais_PacketID IS NULL
LIMIT 1";
$qry = $this->db_onedev->query($sql, [$packet_code]);
if (!$qry) {
$this->error_log([
'fn_name' => 'post_packet_by_code',
'message' => 'packet select failed',
'query' => $this->db_onedev->last_query(),
'json' => ''
], $userid);
}
$raw_data = $qry->result_array();
if(empty($raw_data)){
$errors = array('status' => 'error', 'message' => 'Tidak ada data paket yang belum dikirim untuk kode: ' . $packet_code);
echo json_encode($errors);
exit;
}
foreach ($raw_data as $row) {
$data = [
"PaketID" => $row['T_PacketSasCode'],
"Nama" => $row['T_PacketName'],
"Harga" => $row['T_PacketOriginalPrice'],
"LoginBuat" => 'system',
"TanggalBuat" => $row['T_PacketCreated']
];
$result = $this->post_request($url, $data, $headers);
if (!$result['success']) {
if ($result['status'] == 406) {
$sql = "INSERT INTO ais_departemen (
Ais_DepartemenDepartemenID,
Ais_DepartemenLastUpdated,
Ais_DepartemenUserID
)
VALUES (?, NOW(), ?)";
$this->db_log->query($sql, [$row['DepartemenID'], $userid]);
continue;
}
$this->error_log([
'fn_name' => 'post_departemen',
'message' => 'failed insert Departemen ke AIS',
'query' => $this->db_onedev->last_query(),
'json' => json_encode($result)
], $userid);
echo json_encode($result);
exit;
}
// Simpan log ke cpone_log
$sql = "INSERT INTO ais_departemen (
Ais_DepartemenDepartemenID,
Ais_DepartemenLastUpdated,
Ais_DepartemenUserID
)
VALUES (?, NOW(), ?)";
$this->db_log->query($sql, [$row['DepartemenID'], $userid]);
// Simpan hasil dari response AIS (jika ada)
$result_data[] = $result['response']['data'] ?? $data;
}
$data = $result_data;
$success = [
'status' => 'success',
'message' => 'Berhasil Post Departmen',
'data' => $data
];
echo json_encode($success);
exit;
}
public function re_post_packet_by_code()
{
$userid = 999;
$prm = $this->sys_input;
$packet_code = $prm['packet_code'] ?? null;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log([
'fn_name' => 're_post_packet_by_code_auth',
'message' => 'failed auth',
'query' => '',
'json' => json_encode($login)
], $userid);
echo json_encode(['status' => 'error', 'message' => 'Gagal Login']);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/paket';
$headers = [
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
];
$sql = "SELECT *
FROM t_packet
LEFT JOIN cpone_log ON T_PacketSasCode = Ais_PacketT_PacketSasCode AND
Ais_PacketStatus = 'SUCCESS'
WHERE
T_PacketIsActive = 'Y' AND T_PacketSasCode = ? AND Ais_PacketID IS NULL
LIMIT 1";
$qry = $this->db_onedev->query($sql, [$packet_code]);
if (!$qry) {
$this->error_log([
'fn_name' => 'post_packet_by_code',
'message' => 'packet select failed',
'query' => $this->db_onedev->last_query(),
'json' => ''
], $userid);
}
$raw_data = $qry->result_array();
if(empty($raw_data)){
$errors = array('status' => 'error', 'message' => 'Tidak ada data paket yang belum dikirim untuk kode: ' . $packet_code);
echo json_encode($errors);
exit;
}
foreach ($raw_data as $row) {
$data = [
"PaketID" => $row['T_PacketSasCode'],
"Nama" => $row['T_PacketName'],
"Harga" => $row['T_PacketOriginalPrice'],
"LoginBuat" => 'system',
"TanggalBuat" => $row['T_PacketCreated']
];
$result = $this->put_request($url, $data, $headers);
if (!$result['success']) {
if ($result['status'] == 406) {
$sql = "INSERT INTO ais_departemen (
Ais_DepartemenDepartemenID,
Ais_DepartemenLastUpdated,
Ais_DepartemenUserID
)
VALUES (?, NOW(), ?)";
$this->db_log->query($sql, [$row['DepartemenID'], $userid]);
continue;
}
$this->error_log([
'fn_name' => 're_post_departemen',
'message' => 'failed insert Departemen ke AIS',
'query' => $this->db_onedev->last_query(),
'json' => json_encode($result)
], $userid);
echo json_encode($result);
exit;
}
// Simpan log ke cpone_log
$sql = "INSERT INTO ais_departemen (
Ais_DepartemenDepartemenID,
Ais_DepartemenLastUpdated,
Ais_DepartemenUserID
)
VALUES (?, NOW(), ?)";
$this->db_log->query($sql, [$row['DepartemenID'], $userid]);
// Simpan hasil dari response AIS (jika ada)
$result_data[] = $result['response']['data'] ?? $data;
}
$data = $result_data;
$success = [
'status' => 'success',
'message' => 'Berhasil Re-Post Departmen',
'data' => $data
];
echo json_encode($success);
exit;
}
public function post_doctor_all()
{
$userid = 555;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log([
'fn_name' => 'post_doctor_all_auth',
'message' => 'failed auth',
'query' => '',
'json' => json_encode($login)
], $userid);
echo json_encode(['status' => 'error', 'message' => 'Gagal Login']);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/dokter';
$headers = [
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
];
$sql = " SELECT * , IF(M_SpecialistID IS NOT NULL, 'Spesialis', 'Umum') AS M_DoctorType
FROM m_doctor
LEFT JOIN m_specialist ON M_DoctorM_SpecialistID = M_SpecialistID AND M_SpecialistIsActive = 'Y'
LEFT JOIN cpone_log.ais_doctor ON m_doctor.M_DoctorID = ais_doctor.Ais_DoctorM_DoctorID
WHERE M_DoctorIsActive = 'Y' AND ais_doctor.Ais_DoctorM_DoctorID IS NULL LIMIT 20";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
$this->error_log([
'fn_name' => 'post_doctor_all',
'message' => 'doctor select failed',
'query' => $this->db_onedev->last_query(),
'json' => ''
], $userid);
exit;
}
$raw_data = $qry->result_array();
$return_data = [];
foreach ($raw_data as $key => $row) {
$data = [
"Nama" => $row['M_DoctorName'],
"DokterID" => $row['M_DoctorCode'],
"Gelar" => $row['M_DoctorPrefix'],
"Gelar_belakang" => $row['M_DoctorSuffix'],
"Inisial" => $row['M_DoctorName'],
"NPWP" => '',
"NIK" => '',
"Tipe" => $row['M_DoctorType'],
"Kelamin" => $row['M_DoctorGender'] == 'male' ? 'L' : 'P',
"StatusKepegawaian" => '',
"NamaBank" => '',
"NamaRekening" => '',
"NomerRekening" => '',
"Alamat" => $row['M_DoctorAddress'],
"Email" => $row['M_DoctorEmail']
];
$result = $this->post_request($url, $data, $headers);
$status = $result['error'] == null ? 'success' : 'error';
$sql = "INSERT INTO ais_doctor (
Ais_DoctorM_DoctorCode,
Ais_DoctorM_DoctorID,
Ais_DoctorStatus,
Ais_DoctorJson,
Ais_DoctorResponse,
Ais_DoctorCreated
)
VALUES (
?,
?,
?,
?,
?,
NOW()
)";
$query = $this->db_log->query($sql, [$row['M_DoctorCode'], $row['M_DoctorID'], $status, json_encode($data), json_encode($result)]);
if (!$query) {
$this->error_log([
'fn_name' => 'post_doctor',
'message' => 'failed insert doctor ke AIS',
'query' => $this->db_log->last_query(),
'json' => json_encode($result)
], $userid);
}
$return_data[] = $data;
}
$success = [
'status' => 'success',
'message' => 'Berhasil Post Dokter',
'data' => $return_data
];
echo json_encode($success);
exit;
}
public function re_post_doctor_by_code()
{
$userid = 555;
$prm = $this->sys_input;
$doctor_code = $prm['doctor_code'] ?? null;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log([
'fn_name' => 're_post_doctor_by_code_auth',
'message' => 'failed auth',
'query' => '',
'json' => json_encode($login)
], $userid);
echo json_encode(['status' => 'error', 'message' => 'Gagal Login']);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/dokter';
$headers = [
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
];
$sql = " SELECT * , IF(M_SpecialistID IS NOT NULL, 'Spesialis', 'Umum') AS M_DoctorType
FROM m_doctor
LEFT JOIN m_specialist ON M_DoctorM_SpecialistID = M_SpecialistID AND M_SpecialistIsActive = 'Y'
LEFT JOIN cpone_log.ais_doctor ON m_doctor.M_DoctorID = ais_doctor.Ais_DoctorM_DoctorID
WHERE M_DoctorIsActive = 'Y' AND M_DoctorCode = ? LIMIT 1";
$qry = $this->db_onedev->query($sql, [$doctor_code]);
if (!$qry) {
$this->error_log([
'fn_name' => 're_post_doctor_by_code',
'message' => 'doctor select failed',
'query' => $this->db_onedev->last_query(),
'json' => ''
], $userid);
exit;
}
$raw_data = $qry->result_array();
$return_data = [];
foreach ($raw_data as $key => $row) {
$data = [
"Nama" => $row['M_DoctorName'],
"DokterID" => $row['M_DoctorCode'],
"Gelar" => $row['M_DoctorPrefix'],
"Gelar_belakang" => $row['M_DoctorSuffix'],
"Inisial" => $row['M_DoctorName'],
"NPWP" => '',
"NIK" => '',
"Tipe" => $row['M_DoctorType'],
"Kelamin" => $row['M_DoctorGender'] == 'male' ? 'L' : 'P',
"StatusKepegawaian" => '',
"NamaBank" => '',
"NamaRekening" => '',
"NomerRekening" => '',
"Alamat" => $row['M_DoctorAddress'],
"Email" => $row['M_DoctorEmail']
];
$result = $this->put_request($url, $data, $headers);
$status = $result['error'] == null ? 'success' : 'error';
$sql = "INSERT INTO ais_doctor (
Ais_DoctorM_DoctorCode,
Ais_DoctorM_DoctorID,
Ais_DoctorStatus,
Ais_DoctorJson,
Ais_DoctorResponse,
Ais_DoctorCreated
)
VALUES (
?,
?,
?,
?,
?,
NOW()
)";
$query = $this->db_log->query($sql, [$row['M_DoctorCode'], $row['M_DoctorID'], $status, json_encode($data), json_encode($result)]);
if (!$query) {
$this->error_log([
'fn_name' => 're_post_doctor',
'message' => 'failed insert doctor ke AIS',
'query' => $this->db_log->last_query(),
'json' => json_encode($result)
], $userid);
}
$return_data[] = $data;
}
$success = [
'status' => 'success',
'message' => 'Berhasil Re-Post Dokter',
'data' => $return_data
];
echo json_encode($success);
exit;
}
public function post_doctor_by_code()
{
$userid = 555;
$prm = $this->sys_input;
$doctor_code = $prm['doctor_code'] ?? null;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log([
'fn_name' => 'post_doctor_by_code_auth',
'message' => 'failed auth',
'query' => '',
'json' => json_encode($login)
], $userid);
echo json_encode(['status' => 'error', 'message' => 'Gagal Login']);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/dokter';
$headers = [
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
];
$sql = " SELECT * , IF(M_SpecialistID IS NOT NULL, 'Spesialis', 'Umum') AS M_DoctorType
FROM m_doctor
LEFT JOIN m_specialist ON M_DoctorM_SpecialistID = M_SpecialistID AND M_SpecialistIsActive = 'Y'
LEFT JOIN cpone_log.ais_doctor ON m_doctor.M_DoctorID = ais_doctor.Ais_DoctorM_DoctorID
WHERE M_DoctorIsActive = 'Y' AND ais_doctor.Ais_DoctorM_DoctorID IS NULL AND M_DoctorCode = ? LIMIT 1";
$qry = $this->db_onedev->query($sql, [$doctor_code]);
if (!$qry) {
$this->error_log([
'fn_name' => 'post_doctor_by_code',
'message' => 'doctor select failed',
'query' => $this->db_onedev->last_query(),
'json' => ''
], $userid);
exit;
}
$raw_data = $qry->result_array();
$return_data = [];
foreach ($raw_data as $key => $row) {
$data = [
"Nama" => $row['M_DoctorName'],
"DokterID" => $row['M_DoctorCode'],
"Gelar" => $row['M_DoctorPrefix'],
"Gelar_belakang" => $row['M_DoctorSuffix'],
"Inisial" => $row['M_DoctorName'],
"NPWP" => '',
"NIK" => '',
"Tipe" => $row['M_DoctorType'],
"Kelamin" => $row['M_DoctorGender'] == 'male' ? 'L' : 'P',
"StatusKepegawaian" => '',
"NamaBank" => '',
"NamaRekening" => '',
"NomerRekening" => '',
"Alamat" => $row['M_DoctorAddress'],
"Email" => $row['M_DoctorEmail']
];
$result = $this->post_request($url, $data, $headers);
$status = $result['error'] == null ? 'success' : 'error';
$sql = "INSERT INTO ais_doctor (
Ais_DoctorM_DoctorCode,
Ais_DoctorM_DoctorID,
Ais_DoctorStatus,
Ais_DoctorJson,
Ais_DoctorResponse,
Ais_DoctorCreated
)
VALUES (
?,
?,
?,
?,
?,
NOW()
)";
$query = $this->db_log->query($sql, [$row['M_DoctorCode'], $row['M_DoctorID'], $status, json_encode($data), json_encode($result)]);
if (!$query) {
$this->error_log([
'fn_name' => 'post_doctor',
'message' => 'failed insert doctor ke AIS',
'query' => $this->db_log->last_query(),
'json' => json_encode($result)
], $userid);
}
$return_data[] = $data;
}
$success = [
'status' => 'success',
'message' => 'Berhasil Post Dokter',
'data' => $return_data
];
echo json_encode($success);
exit;
}
function get_doctor_by_code()
{
$userid = 999;
// Get id from query parameter
$doctor_code = $this->sys_input['doctor_code'] ?? null;
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'get_doctor_by_code_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . 'api/dokter?id='.$doctor_code;
$result = $this->get_request($url);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'get_doctor_by_code', 'message' => 'failed get doctor', 'query' => '', 'json' => json_encode($result)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Get Dokter');
echo json_encode($errors);
exit;
}
$data = $result['response']['data'] ?? null;
// $data = json_encode($data);
$success = array('status' => 'success', 'message' => 'Berhasil Get Dokter', 'data' => $data);
echo json_encode($success);
exit;
}
// Get Medrec
function get_medrec_by_noreg()
{
$userid = 999;
// Get id from query parameter
$noreg = $this->sys_input['noreg'] ?? null;
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'get_medrec_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . 'api/medrec?id='.$noreg;
$result = $this->get_request($url);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'get_medrec', 'message' => 'failed get medrec', 'query' => '', 'json' => json_encode($result)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Get Layanan');
echo json_encode($errors);
exit;
}
$data = $result['response']['data'] ?? null;
// $data = json_encode($data);
$success = array('status' => 'success', 'message' => 'Berhasil Get Medrec', 'data' => $data);
echo json_encode($success);
exit;
}
// Post Medrec
public function post_medrec_by_noreg()
{
$userid = 999;
$prm = $this->sys_input;
$noreg = $prm['noreg'] ?? null;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log([
'fn_name' => 'post_medrec_auth',
'message' => 'failed auth',
'query' => '',
'json' => json_encode($login)
], $userid);
echo json_encode(['status' => 'error', 'message' => 'Gagal Login']);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/medrec';
$headers = [
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
];
// Ambil data layanan dari tabel t_test yang belum dikirim
$sql = "SELECT *
FROM m_patient n
LEFT JOIN cpone_log.ais_medrec a
ON n.M_PatientNoReg = a.Ais_MedrecPatientNoReg
WHERE n.M_PatientIsActive = 'Y'
AND a.Ais_MedrecPatientNoReg IS NULL AND n.M_PatientNoReg = ?
LIMIT 1";
$qry = $this->db_onedev->query($sql, [$noreg]);
if (!$qry) {
$this->error_log([
'fn_name' => 'post_medrec',
'message' => 'medrec select failed',
'query' => $this->db_onedev->last_query(),
'json' => ''
], $userid);
exit;
}
$raw_data = $qry->result_array();
if(empty($raw_data)){
$errors = array('status' => 'error', 'message' => 'Tidak ada data medrec yang belum dikirim untuk no reg: ' . $noreg);
echo json_encode($errors);
exit;
}
$result_data = [];
$errors = [];
foreach ($raw_data as $row) {
if ($row['M_PatientGender'] == 'MALE')
'L';
if ($row['M_PatientGender'] == 'FEMALE')
'P';
$data = [
"Nama" => $row['M_PatientName'],
"MEDRECID" => $row['M_PatientNoReg'],
"KodeRS" => "",
"PerusahaanID" => $row['M_PatientRegisteredByCorporateID'],
"NIK" => $row['M_PatientNIP'],
"Kelamin" => $row['M_PatientGender'],
"TanggalLahir" => $row['M_PatientDOB'],
"TempatLahir" => $row['M_PatientLocation'],
"Alamat" => $row['M_PatientAddress'],
"Couple" => 'N',
"CoupleID" => 0,
"LoginBuat" => 'system'
];
$result = $this->post_request($url, $data, $headers);
if ($result['error'] == null) {
$sql = "INSERT INTO ais_medrec (
Ais_MedrecPatientNoReg,
Ais_MedrecStatus,
Ais_MedrecJson,
Ais_MedrecResponse,
Ais_MedrecUserID,
Ais_MedrecCreated
)
VALUES (?, ?, ?, ?, ?, NOW())";
$this->db_log->query($sql, [
$row['M_PatientNoReg'],
$result['errror'] == null?'success':'error',
json_encode($data),
json_encode($result),
$userid
]);
$success = [
'status' => 'success',
'message' => 'Berhasil Post Medrec',
];
echo json_encode($success);
exit;
}else{
$success = [
'status' => 'error',
'message' => 'Gagal Post Medrec: '.implode(', ', $result['error']),
'errors' => $result['error']
];
echo json_encode($success);
exit;
}
}
}
public function re_post_medrec_by_noreg()
{
$userid = 999;
$prm = $this->sys_input;
$noreg = $prm['noreg'] ?? null;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log([
'fn_name' => 're_post_medrec_auth',
'message' => 'failed auth',
'query' => '',
'json' => json_encode($login)
], $userid);
echo json_encode(['status' => 'error', 'message' => 'Gagal Login']);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/medrec';
$headers = [
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
];
// Ambil data layanan dari tabel t_test yang belum dikirim
$sql = "SELECT *
FROM m_patient n
LEFT JOIN cpone_log.ais_medrec a
ON n.M_PatientNoReg = a.Ais_MedrecPatientNoReg
WHERE n.M_PatientIsActive = 'Y'
AND n.M_PatientNoReg = ?
LIMIT 1";
$qry = $this->db_onedev->query($sql, [$noreg]);
if (!$qry) {
$this->error_log([
'fn_name' => 'post_medrec',
'message' => 'medrec select failed',
'query' => $this->db_onedev->last_query(),
'json' => ''
], $userid);
exit;
}
$raw_data = $qry->result_array();
if(empty($raw_data)){
$errors = array('status' => 'error', 'message' => 'Tidak ada data medrec yang belum dikirim untuk no reg: ' . $noreg);
echo json_encode($errors);
exit;
}
$result_data = [];
$errors = [];
foreach ($raw_data as $row) {
if ($row['M_PatientGender'] == 'MALE')
'L';
if ($row['M_PatientGender'] == 'FEMALE')
'P';
$data = [
"Nama" => $row['M_PatientName'],
"MEDRECID" => $row['M_PatientNoReg'],
"KodeRS" => "",
"PerusahaanID" => $row['M_PatientRegisteredByCorporateID'],
"NIK" => $row['M_PatientNIP'],
"Kelamin" => $row['M_PatientGender'],
"TanggalLahir" => $row['M_PatientDOB'],
"TempatLahir" => $row['M_PatientLocation'],
"Alamat" => $row['M_PatientAddress'],
"Couple" => 'N',
"CoupleID" => 0,
"LoginBuat" => 'system'
];
$result = $this->put_request($url, $data, $headers);
if ($result['error'] == null) {
$sql = "INSERT INTO ais_medrec (
Ais_MedrecPatientNoReg,
Ais_MedrecStatus,
Ais_MedrecJson,
Ais_MedrecResponse,
Ais_MedrecUserID,
Ais_MedrecCreated
)
VALUES (?, ?, ?, ?, ?, NOW())";
$this->db_log->query($sql, [
$row['M_PatientNoReg'],
$result['errror'] == null?'success':'error',
json_encode($data),
json_encode($result),
$userid
]);
$success = [
'status' => 'success',
'message' => 'Berhasil Re-Post Medrec',
];
echo json_encode($success);
exit;
}else{
$success = [
'status' => 'error',
'message' => 'Gagal Re-Post Medrec: '.implode(', ', $result['error']),
'errors' => $result['error']
];
echo json_encode($success);
exit;
}
}
}
// Get Transaction
function get_transaction()
{
$userid = 999;
// Get id from query parameter
$id = $this->input->get('id') ?? null;
// if (empty($id)) {
// $errors = array('status' => 'error', 'message' => 'ID parameter is required');
// echo json_encode($errors);
// exit;
// }
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log(array('fn_name' => 'get_transaction_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($login)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Login');
echo json_encode($errors);
exit;
}
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/transaksi';
if (!empty($id)) {
$url .= '?id=' . $id;
}
$result = $this->get_request($url);
if (!$result['success']) {
$this->error_log(array('fn_name' => 'get_transaction', 'message' => 'failed get transaksi', 'query' => '', 'json' => json_encode($result)), $userid);
$errors = array('status' => 'error', 'message' => 'Gagal Get Transaksi', 'response' => $result);
echo json_encode($errors);
exit;
}
$data = $result['response']['data'] ?? null;
// $data = json_encode($data);
$success = array('status' => 'success', 'message' => 'Berhasil Get Transaksi', 'data' => $data);
echo json_encode($success);
exit;
}
// Post Transaction
public function post_transaction()
{
$userid = 999;
// Auth Login
$login = $this->post_auth();
if (!$login['success']) {
$this->error_log([
'fn_name' => 'post_transaction_auth',
'message' => 'failed auth',
'query' => '',
'json' => json_encode($login)
], $userid);
echo json_encode(['status' => 'error', 'message' => 'Gagal Login']);
exit;
}
// Get config
$dt_config = $this->get_config();
$baseUrl = $dt_config['AisConfigBaseUrl'];
$url = $baseUrl . '/api/transaksi';
$headers = [
'Header-Token: ' . $dt_config['AisConfigHeaderToken'],
'Authorization: Bearer ' . $dt_config['AisConfigAuthToken']
];
// Ambil parameter tanggal dan pagination
$date = $this->input->get('date') ?: date('Y-m-d'); // default hari ini
$limit = (int) ($this->input->get('limit') ?: 25); // default 50 record per halaman
$offset = (int) ($this->input->get('offset') ?: 0); // default halaman pertama
// Ambil data layanan dari tabel t_test yang belum dikirim
$sql = "SELECT
n.*
FROM t_orderheader n
LEFT JOIN m_doctor
ON T_OrderHeaderPjM_DoctorID = M_DoctorID
LEFT JOIN m_patient
ON T_OrderHeaderM_PatientID = M_PatientID
LEFT JOIN corporate
ON T_OrderHeaderCorporateID = CorporateID
LEFT JOIN corporate_type
ON CorporateCorporateTypeID = CorporateTypeID
WHERE n.T_OrderHeaderIsActive = 'Y'
AND DATE(n.T_OrderHeaderDate) = ?
-- AND (
-- -- belum pernah dikirim sama sekali
-- a.Ais_TransactionOrderHeaderLabNumber IS NULL
-- -- atau belum pernah ada status sukses/201 untuk labnumber ini
-- OR n.T_OrderHeaderLabNumber NOT IN (
-- SELECT DISTINCT Ais_TransactionOrderHeaderLabNumber
-- FROM cpone_log.ais_transaction
-- WHERE Ais_TransactionStatus IN ('Success', '201')
-- )
-- )
ORDER BY n.T_OrderHeaderDate ASC
LIMIT ? OFFSET ?;";
$qry = $this->db_onedev->query($sql, [$date, $limit, $offset]);
if (!$qry) {
$this->error_log([
'fn_name' => 'post_transaction',
'message' => 'Transaction select failed',
'query' => $this->db_onedev->last_query(),
'json' => ''
], $userid);
exit;
}
$raw_data = $qry->result_array();
$result_data = [];
$terlewati = [];
$Error = [];
foreach ($raw_data as $row) {
// $labnumber = 'P210500671';
$gender = $row['M_PatientGender'];
if (strtoupper($gender) === 'MALE') {
$gender = 'L';
} elseif (strtoupper($gender) === 'FEMALE') {
$gender = 'P';
} else {
$gender = ''; // kalau tidak diketahui
}
$namaLengkap = trim(
($row['M_PatientPrefix'] ?? '') . ' ' .
($row['M_PatientName'] ?? '') . ' ' .
($row['M_PatientSuffix'] ?? '')
);
// $namadokterLengkap = trim(
// ($row['M_DoctorPrefix'] ?? '') . ' ' .
// ($row['M_DoctorPrefix2'] ?? '') . ' ' .
// ($row['M_PatientName'] ?? '') . ' ' .
// ($row['M_PatientSuffix'] ?? '') . ' ' .
// ($row['M_DoctorSuffix2'] ?? '')
// );
$orderheaderid = $row['T_OrderHeaderID'];
$labnumber = $row['T_OrderHeaderLabNumber'];
$searchurl = $baseUrl . '/api/transaksi?id=' . $labnumber;
// cek apakah data sudah ada di server AIS
$result = $this->get_request($searchurl);
if (!$result['success'] && isset($result['response']['response']['message']) && $result['response']['response']['message'] !== "Data Empty") {
$this->error_log([
'fn_name' => 'get_transaction_existing',
'message' => 'failed get transaction existing',
'query' => '',
'json' => json_encode($result)
], $userid);
echo json_encode(['status' => 'error', 'message' => 'Gagal Get Data Transaction']);
exit;
}
// Normalisasi hasil
$existing_data = $result['response']['data'] ?? [];
// Jika object tunggal
if (!empty($existing_data) && isset($existing_data['RegID'])) {
$existing_data = [$existing_data];
}
$sudah_ada = false;
if (is_array($existing_data) && count($existing_data) > 0) {
foreach ($existing_data as $d) {
if (isset($d['RegID']) && $d['RegID'] == $labnumber) {
$sudah_ada = true;
}
}
}
if ($sudah_ada) {
// Encode jadi JSON string yang siap disimpan ke database
$status = "Success";
$response = "Already Insert $labnumber";
$json_data = json_encode($existing_data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$sql = "INSERT INTO ais_transaction (
Ais_TransactionOrderHeaderLabNumber,
Ais_TransactionJson,
Ais_TransactionStatus,
Ais_TransactionResponse,
Ais_TransactionUrl,
Ais_TransactionLastUpdated,
Ais_TransactionUserID
)
SELECT ?, ?, ?, ?, ?, NOW(), ?
FROM DUAL
WHERE NOT EXISTS (
SELECT 1
FROM ais_transaction
WHERE Ais_TransactionOrderHeaderLabNumber = ?
AND Ais_TransactionStatus IN ('Success', '201')
);";
$this->db_log->query($sql, [$labnumber, $json_data, $status, $response, $url, $userid, $labnumber]);
$terlewati[] = $existing_data;
continue;
}
// Cari trx bayar
// Ambil data layanan dari tabel t_test yang belum dikirim
$trxbayar = $this->get_payment($orderheaderid);
$trxlayanan = $this->get_trxlayanan($orderheaderid);
$regpas = $this->get_regpasnominal($orderheaderid);
$data = [
"RegID" => $row['T_OrderHeaderLabNumber'],
"MEDRECID" => $row['M_PatientNoReg'],
"Tanggal" => $row['T_OrderHeaderDate'],
"PulangTanggal" => $row['T_OrderHeaderDate'],
"PerusahaanID" => $row['CorporateCode'],
"AsuransiID" => "",
"GroupTarifID" => 0,
"Nama" => $namaLengkap ?? "",
"NIK" => $row['M_PatientIdentifierValue'],
"TglLahir" => $row['M_PatientDOB'],
"JnsKelamin" => $gender,
"NomorPolis" => "",
"NomerJaminan" => "",
"JenisRegID" => 0,
"JenisPasienID" => $row['CorporateTypeName'],
"DepartemenID" => 'LAB-WESTERINDO-01',
"Pulang" => "Y",
"BolehPulang" => "Y",
"Catatan" => $row['T_OrderHeaderFoNote'],
"Verified" => "Y",
"KelasID" => "",
"LoginBuat" => "system",
"TanggalBuat" => $row['T_OrderHeaderCreated'],
"TrxLayanan" => $trxlayanan,
"TrxItem" => [],
"TrxItemReturn" => [],
"RegpasNominal" => $regpas,
"TrxBayar" => $trxbayar,
"Trxtt" => null,
"TrxLain" => null,
"PaketDispenser" => null
];
$result = $this->post_request($url, $data, $headers);
if (!$result['success']) {
if (!$result['status'] == 201) {
$status = 'Success';
$this->error_log([
'fn_name' => 'post_transaction',
'message' => 'failed insert Transaction ke AIS',
'query' => $this->db_onedev->last_query(),
'json' => json_encode($result)
], $userid);
echo json_encode($result);
$sql = "INSERT INTO ais_transaction (
Ais_TransactionOrderHeaderLabNumber,
Ais_TransactionJson,
Ais_TransactionStatus,
Ais_TransactionResponse,
Ais_TransactionUrl,
Ais_TransactionLastUpdated,
Ais_TransactionUserID
)
VALUES (?,?,?,?,?, NOW(), ?)";
$this->db_log->query($sql, [$labnumber, json_encode($data), $status, json_encode($result['error']), $url, $userid]);
// Simpan error per labnumber di array PHP (buat summary nanti)
$Error[$labnumber] = [
'message' => $result['message']?? '',
'detail' => $result['error']?? ''
];
continue;
} else {
$status = $result['status'];
$sql = "INSERT INTO ais_transaction (
Ais_TransactionOrderHeaderLabNumber,
Ais_TransactionJson,
Ais_TransactionStatus,
Ais_TransactionResponse,
Ais_TransactionUrl,
Ais_TransactionLastUpdated,
Ais_TransactionUserID
)
VALUES (?,?,?,?,?, NOW(), ?)";
$this->db_log->query($sql, [$labnumber, json_encode($data), $status, json_encode($result['message']), $url, $userid]);
}
}
$status = 'Success';
// Simpan log ke cpone_log
$sql = "INSERT INTO ais_transaction (
Ais_TransactionOrderHeaderLabNumber,
Ais_TransactionJson,
Ais_TransactionStatus,
Ais_TransactionResponse,
Ais_TransactionUrl,
Ais_TransactionLastUpdated,
Ais_TransactionUserID
)
VALUES (?,?,?,?,?, NOW(), ?)";
$this->db_log->query($sql, [$labnumber, $json_data, $status, $response, $url, $userid]);
// Simpan hasil dari response AIS (jika ada)
$result_data[] = $result['response']['data'] ?? $data;
}
$data = $result_data;
$success = [
'status' => 'success',
'message' => 'Berhasil Post Transaction',
'data' => $data,
'already' => $terlewati,
'errordetail' => $Error
];
echo json_encode($success);
exit;
}
private function get_trxlayanan($id = null)
{
if ($id == null) {
return [];
}
$sql_layanan = "SELECT *
FROM t_orderheader
LEFT JOIN t_orderdetail ON T_OrderHeaderID = T_OrderDetailT_OrderHeaderID
AND T_OrderDetailIsActive = 'Y'
LEFT JOIN t_test ON T_OrderDetailT_TestID = T_TestID
AND T_TestIsActive = 'Y'
AND T_TestIsPrice = 'Y'
LEFT JOIN m_doctor ON T_OrderHeaderPjM_DoctorID = M_DoctorID
WHERE T_OrderHeaderID = ?
AND T_OrderHeaderIsActive = 'Y'
";
$qry = $this->db_onedev->query($sql_layanan, [$id]);
$result = $qry->result_array();
if (empty($result)) {
return [];
}
$data_layanan = [];
foreach ($result as $value) {
$data_layanan[] = [
"TrxLayananID" => $value['T_OrderDetailID'],
"TrxDepartemenID" => "LAB-WESTERINDO-01",
"TanggalBuat" => $value['T_OrderDetailCreated'],
"LayananID" => $value['T_TestSasCode'],
"DokterID" => $value['T_OrderHeaderPjM_DoctorID'],
"ShareRS" => 0,
"ShareDokter" => 0,
"ShareExternal" => 0,
"ShareLain2" => 0,
"PihakExternal" => "",
"PihakLain2" => "",
"ShareTipe" => "",
"Harga" => $value['T_OrderDetailTotal'],
"Jumlah" => 0,
"Rate" => 0,
"FOC" => "",
"Diskon" => 0,
"TipeDiskon" => "",
"DepartemenID" => "LAB-WESTERINDO-01",
"DitanggungPasien" => $value['T_OrderHeaderTotal'],
"DitanggungPenjamin" => 0,
"MarkUpCito" => 0,
"Cito" => $value['T_OrderDetailIsCito'],
"PaketID" => "",
"KelasID" => "",
"Tagihkan" => "",
"TrxLayananDetail" => null
];
}
return $data_layanan;
}
private function get_regpasnominal($id = null)
{
if ($id == null) {
return [];
}
// Ambil data layanan
$sql_regpas = "SELECT *
FROM t_orderheader
JOIN t_orderdetail
ON T_OrderHeaderID = T_OrderDetailT_OrderHeaderID
AND T_OrderDetailIsActive = 'Y'
JOIN t_test
ON T_OrderDetailT_TestID = T_TestID
AND T_TestIsActive = 'Y'
JOIN nat_test
ON T_TestNat_TestID = Nat_TestID
AND Nat_TestIsActive = 'Y'
WHERE
T_OrderHeaderID = ?
AND T_OrderHeaderIsActive = 'Y'
AND T_TestIsPrice = 'Y'
";
$qry = $this->db_onedev->query($sql_regpas, [$id]);
$result = $qry->result_array();
if (empty($result)) {
return [];
}
// Hitung total pembayaran langsung dengan SUM
$sql_pay = "SELECT COALESCE(SUM(F_PaymentTotal), 0) AS total_bayar
FROM f_payment
WHERE F_PaymentT_OrderHeaderID = ?
AND F_PaymentIsActive = 'Y'
";
$qry_pay = $this->db_onedev->query($sql_pay, [$id]);
$row_pay = $qry_pay->row_array();
$pay = floatval($row_pay['total_bayar'] ?? 0);
// Hitung nominal tiap kategori layanan
$JumlahLayananRadiologi = 0;
$JumlahLayananLaboratorium = 0;
$JumlahLayanan = 0;
$OrderHeaderTotal = floatval($result[0]['T_OrderHeaderTotal']);
$data_regpas = [];
foreach ($result as $value) {
$total = floatval($value['T_OrderDetailTotal']);
if ($value['Nat_TestNat_GroupID'] == 1) {
$JumlahLayananLaboratorium += $total;
} elseif ($value['Nat_TestNat_GroupID'] == 3) {
$JumlahLayananRadiologi += $total;
} else {
$JumlahLayanan += $total;
}
}
// Bentuk data akhir
$data_regpas[] = [
"BiayaParamedik" => 0,
"ParamedikDitanggungPasien" => 0,
"ParamedikDitanggungPenjamin" => 0,
"BiayaAdministrasi" => 0,
"AdmDitanggungPasien" => 0,
"AdmDitanggungPenjamin" => 0,
"BiayaMaterai" => 0,
"BiayaMateraiDitanggungPasien" => 0,
"BiayaMateraiDitanggungPenjamin" => 0,
"JumlahLayanan" => $JumlahLayanan,
"JumlahLayananRadiologi" => $JumlahLayananRadiologi,
"JumlahLayananLaboratorium" => $JumlahLayananLaboratorium,
"JumlahItem" => 0,
"JumlahItemRetur" => 0,
"JumlahTT" => 0,
"JumlahBiayaLain" => 0,
"JumlahBayar" => $pay,
"TipeDiskonGlobal" => "",
"DiskonGlobal" => 0,
"TotalDitanggungPasien" => $OrderHeaderTotal,
"TotalDitanggungPenjamin" => 0
];
return $data_regpas;
}
private function get_payment($id = null)
{
if ($id == null) {
return [];
}
$sql_payment = "SELECT *
FROM t_orderheader
JOIN m_patient ON T_OrderHeaderM_PatientID = M_PatientID
AND T_OrderHeaderIsActive = 'Y'
JOIN f_payment ON T_OrderHeaderID = F_PaymentT_OrderHeaderID
AND F_PaymentIsActive = 'Y'
JOIN f_paymentdetail ON F_PaymentID = F_PaymentDetailF_PaymentID
AND F_PaymentDetailIsActive = 'Y'
JOIN m_paymenttype ON F_PaymentDetailM_PaymentTypeID = M_PaymentTypeID
WHERE T_OrderHeaderID = ?
";
$qry = $this->db_onedev->query($sql_payment, [$id]);
$result = $qry->result_array();
if (empty($result)) {
return [];
}
$data_payment = [];
foreach ($result as $value) {
$data_payment[] = [
"MEDRECID" => $value['M_PatientNoReg'] ?? "",
"BayarID" => $value['F_PaymentID'] ?? "",
"Jumlah" => $value['F_PaymentTotal'] ?? "0.00",
"Tanggal" => $value['F_PaymentCreated'] ?? "",
"JenisBayarID" => $value['M_PaymentTypeCode'] ?? "",
"KwitansiID" => $value['F_PaymentNumber'] ?? "",
"Dibatalkan" => "N"
];
}
return $data_payment;
}
/**
* Generic PUT request function
* @param string $url API endpoint URL
* @param array $data Request payload
* @param array $headers Custom headers (optional)
* @return array Response from API
*/
public function put_request($url, $data = array(), $headers = array())
{
// Default headers
$default_headers = array(
'Content-Type: application/json'
);
// Merge custom headers with default headers
$final_headers = array_merge($default_headers, $headers);
// Initialize cURL
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $final_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// Execute cURL request
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
// Close cURL
curl_close($ch);
// Prepare result
$result = array(
'success' => false,
'http_code' => $http_code,
'response' => null,
'error' => null
);
if ($error) {
$result['error'] = $error;
} else {
$result = json_decode($response, true);
$result['success'] = true;
}
return $result;
}
function monitoring_patient()
{
$sql_base = "SELECT
oh.T_OrderHeaderID,
oh.T_OrderHeaderM_PatientID AS patient_id,
p.M_PatientName AS patient_name,
p.M_PatientNoReg AS patient_noreq,
p.M_PatientGender AS patient_gender,
p.M_PatientDOB AS patient_birthdate,
p.M_PatientHp AS patient_hp,
p.M_PatientEmail AS patient_email,
p.M_PatientIdentifierValue AS patient_identifier,
p.M_PatientNIP AS patient_nip,
p.M_PatientJob AS patient_job,
p.M_PatientPosisi AS patient_posisi,
p.M_PatientPosisi AS pasient_divisi,
am.Ais_MedrecStatus AS medrec_status,
-- STATUS AIS
IF(MAX(am.Ais_MedrecID) IS NULL, 'N', 'Y') AS medrec_status
FROM t_orderheader oh
JOIN m_patient p
ON p.M_PatientID = oh.T_OrderHeaderM_PatientID
LEFT JOIN ".$this->db_log->database.".ais_medrec am
ON am.Ais_MedrecPatientNoReg = p.M_PatientNoReg
";
// Pagination
$page = $this->input->get('page') ? intval($this->input->get('page')) : 1;
$limit = 10;
$offset = ($page - 1) * $limit;
// Filter
$patient_name = $this->input->get('patient_name');
$patient_noreq = $this->input->get('patient_noreq');
$medrec_status = $this->input->get('medrec_status');
$params = [];
$where_clauses = [];
if ($medrec_status === 'Y') {
$where_clauses[] = "am.Ais_MedrecID IS NOT NULL";
} elseif ($medrec_status === 'N') {
$where_clauses[] = "am.Ais_MedrecID IS NULL";
}
if ($patient_name) {
$where_clauses[] = "p.M_PatientName LIKE ?";
$params[] = "%$patient_name%";
}
if ($patient_noreq) {
$where_clauses[] = "p.M_PatientNoReg LIKE ?";
$params[] = "%$patient_noreq%";
}
// WHERE builder
$where_sql = "";
if (!empty($where_clauses)) {
$where_sql = " WHERE " . implode(" AND ", $where_clauses);
}
// Count total rows
$count_sql = "SELECT COUNT(DISTINCT oh.T_OrderHeaderID) AS total
FROM t_orderheader oh
JOIN m_patient p
ON p.M_PatientID = oh.T_OrderHeaderM_PatientID
LEFT JOIN ".$this->db_log->database.".ais_medrec am
ON am.Ais_MedrecPatientNoReg = p.M_PatientNoReg
$where_sql";
$qry_count = $this->db_onedev->query($count_sql, $params);
$total = $qry_count->row()->total ?? 0;
// Main query
$sql = $sql_base . $where_sql . "
GROUP BY oh.T_OrderHeaderID
LIMIT $limit OFFSET $offset";
$qry = $this->db_onedev->query($sql, $params);
if (!$qry) {
echo json_encode(['status' => 'error', 'message' => 'Gagal mengambil data pasien.']);
exit;
}
$data = $qry->result_array();
echo json_encode([
'status' => 'success',
'message' => 'Berhasil mengambil data pasien.',
'data' => $data,
'total' => $total,
'page' => $page,
'limit' => $limit,
'total_page' => ceil($total / $limit)
]);
exit;
}
function get_json_response()
{
$patient_noreg = $this->input->get('patient_noreg');
if (empty($patient_noreg)) {
return $this->output
->set_status_header(400)
->set_content_type('application/json')
->set_output(json_encode([
'status' => 'error',
'message' => 'patient_noreg parameter is required'
]));
}
$sql = "SELECT
Ais_MedrecPatientNoReg AS patient_noreg,
Ais_MedrecCreated AS medrec_created,
Ais_MedrecStatus AS medrec_json,
Ais_MedrecResponse AS medrec_response,
'N' as show_log
FROM ais_medrec
WHERE Ais_MedrecPatientNoReg = ?";
$qry = $this->db_log->query($sql, [$patient_noreg]);
// ❗ Jika query gagal → tampilkan error DB
if ($qry === false) {
return $this->output
->set_status_header(500)
->set_content_type('application/json')
->set_output(json_encode([
'status' => 'error',
'message' => 'SQL Error',
'db_error' => $this->db_log->error() // tampilkan pesan error DB
]));
}
// Ambil seluruh baris
$results = $qry->result_array();
if (!$results) {
return $this->output
->set_content_type('application/json')
->set_output(json_encode([
'status' => 'error',
'message' => 'Data not found for the given patient_noreg.'
]));
}
// Decode JSON
foreach ($results as &$row) {
$row['medrec_json'] = json_decode($row['medrec_json'], true);
$row['medrec_response'] = json_decode($row['medrec_response'], true);
}
unset($row);
return $this->output
->set_content_type('application/json')
->set_output(json_encode([
'status' => 'success',
'data' => $results
]));
}
}