1205 lines
40 KiB
PHP
1205 lines
40 KiB
PHP
<?php
|
||
class Api_his extends MY_Controller
|
||
{
|
||
var $intervalHour;
|
||
var $tmp_body;
|
||
function __construct()
|
||
{
|
||
parent::__construct();
|
||
$this->intervalHour = 12;
|
||
}
|
||
function param()
|
||
{
|
||
$body = file_get_contents("php://input");
|
||
$this->tmp_body = $body;
|
||
return json_decode($body, true);
|
||
}
|
||
|
||
function auth_token()
|
||
{
|
||
$headers = $this->input->request_headers();
|
||
|
||
if (!$headers['authorization']) {
|
||
echo json_encode(["status" => "ERR", "message" => "No Authorization Bearer [Auth003]"]);
|
||
exit;
|
||
}
|
||
$token = $headers["authorization"];
|
||
$token = trim(str_replace("Bearer", "", $token));
|
||
if ($token != "MYmIZGwkLQUjmFHiL2kgrOjhUyZnk507oBD2BDwM" ) {
|
||
echo json_encode(["status" => "ERR", "message" => "Token Salah"]);
|
||
exit;
|
||
}
|
||
// if ($param["expired"] < $xnow) {
|
||
// echo json_encode(["status" => "ERR", "message" => "Access Token expired [Auth003]"]);
|
||
// exit;
|
||
// }
|
||
return $param["branchCode"];
|
||
}
|
||
|
||
public function GetMapingLayanan()
|
||
{
|
||
try {
|
||
|
||
$sql = "
|
||
SELECT
|
||
HisMapingLayananID,
|
||
HisMapingLayananGroupLayananID,
|
||
HisMapingLayananLayananID,
|
||
HisMapingLayananNamaLayanan,
|
||
HisMapingLayananHarga
|
||
FROM
|
||
his_mapping_layanan
|
||
WHERE
|
||
HisMapingLayananIsActive = 1
|
||
";
|
||
|
||
$qry = $this->db->query($sql);
|
||
|
||
// Cek error query
|
||
if (!$qry) {
|
||
$db_error = $this->db->error();
|
||
|
||
echo json_encode([
|
||
"Status" => [
|
||
"OK" => false,
|
||
"Code" => 500,
|
||
"Messages" => $db_error['message']
|
||
],
|
||
"Data" => null
|
||
]);
|
||
return;
|
||
}
|
||
|
||
$rows = $qry->result_array();
|
||
|
||
// Validasi data kosong
|
||
if (empty($rows)) {
|
||
echo json_encode([
|
||
"Status" => [
|
||
"OK" => false,
|
||
"Code" => 404,
|
||
"Messages" => "Data layanan tidak ditemukan"
|
||
],
|
||
"Data" => []
|
||
]);
|
||
return;
|
||
}
|
||
|
||
$result = [];
|
||
|
||
foreach ($rows as $r) {
|
||
$result[] = [
|
||
"id" => $r["HisMapingLayananID"],
|
||
"code_jenis_pelayanan" => $r["HisMapingLayananCodeJenisPelayanan"],
|
||
"layanan_id" => $r["HisMapingLayananLayananID"],
|
||
"layanan_nama" => $r["HisMapingLayananNamaLayanan"],
|
||
"harga" => (float) $r["HisMapingLayananHarga"]
|
||
];
|
||
}
|
||
|
||
echo json_encode([
|
||
"Status" => [
|
||
"OK" => true,
|
||
"Code" => 200,
|
||
"Messages" => "SUCCESS"
|
||
],
|
||
"Data" => $result
|
||
]);
|
||
|
||
} catch (Exception $e) {
|
||
|
||
echo json_encode([
|
||
"Status" => [
|
||
"OK" => false,
|
||
"Code" => 500,
|
||
"Messages" => $e->getMessage()
|
||
],
|
||
"Data" => null
|
||
]);
|
||
}
|
||
}
|
||
|
||
// public function SendPendaftaranPasien()
|
||
// {
|
||
// $useDummy = true;
|
||
|
||
// $payload = [
|
||
// 'no_rekam_medis' => null,
|
||
// 'nama_pasien' => 'Tb Fajri Mulyana',
|
||
// 'nama_panggilan' => 'Fajri',
|
||
// 'jenis_kelamin' => 'L',
|
||
// 'golongan_darah' => null,
|
||
// 'agama' => null,
|
||
// 'pendidikan' => null,
|
||
// 'pekerjaan' => null,
|
||
// 'suku_bangsa' => null,
|
||
// 'bahasa_sehari' => null,
|
||
// 'tempat_lahir' => null,
|
||
// 'tanggal_lahir' => '2007-02-04',
|
||
// 'status_perkawinan'=> 'Menikah',
|
||
// 'nomor_identitas' => [
|
||
// 'jenis' => 'KITAS',
|
||
// 'nomor' => '360213412341412341',
|
||
// ],
|
||
// 'jenis_pasien' => 'jaminan_perusahaan',
|
||
// 'perusahaan_penanggung_code' => 'PTSAS',
|
||
// 'perusahaan_afiliasi_code' => 'PTPrimaya',
|
||
// 'group_tarif' => 'MCUSAS',
|
||
// 'provider' => null,
|
||
// 'no_polis' => 'SM1122334455',
|
||
// 'no_karyawan' => 'SM2023003',
|
||
// 'no_jaminan' => null,
|
||
// 'nama_karyawan' => null,
|
||
// 'kewarganegaraan' => 'Indonesia',
|
||
// 'negara' => 'Indonesia',
|
||
// 'provinsi' => 'Banten',
|
||
// 'kabupaten' => 'Lebak',
|
||
// 'kota' => 'Rangkasbitung',
|
||
// 'kelurahan' => 'rangkasbitung',
|
||
// 'alamat' => 'Btn ona',
|
||
// 'dispenser_paket_code' => 'DISP-260200004',
|
||
// 'poli_code' => 'MCU',
|
||
// 'dokter_dpjp_code' => 'dr_mcu'
|
||
// ];
|
||
|
||
// if ($useDummy) {
|
||
// $insert = [
|
||
// 'payload' => json_encode($payload),
|
||
// 'response' => null,
|
||
// 'status_sync' => 'DRAFT',
|
||
// 'created_at' => date('Y-m-d H:i:s')
|
||
// ];
|
||
|
||
// $this->db->insert('his_api_test', $insert);
|
||
|
||
// return [
|
||
// 'status' => true,
|
||
// 'message' => 'Payload berhasil disimpan sebagai DRAFT',
|
||
// 'insert_id' => $this->db->insert_id()
|
||
// ];
|
||
// } else {
|
||
// $url = "https://api-anda.com/pendaftaran-pasien";
|
||
|
||
// $ch = curl_init($url);
|
||
|
||
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
// curl_setopt($ch, CURLOPT_POST, true);
|
||
// curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||
// 'Content-Type: application/json',
|
||
// 'Authorization: Bearer YOUR_TOKEN_DISINI'
|
||
// ]);
|
||
// curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
|
||
|
||
// $response = curl_exec($ch);
|
||
// $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||
|
||
// if (curl_errno($ch)) {
|
||
// $error = curl_error($ch);
|
||
// log_message('error', 'API Error: ' . $error);
|
||
// curl_close($ch);
|
||
|
||
// return [
|
||
// 'status' => false,
|
||
// 'message' => $error
|
||
// ];
|
||
// }
|
||
|
||
// curl_close($ch);
|
||
|
||
// log_message('info', 'API Response: ' . $response);
|
||
|
||
// return [
|
||
// 'status' => $httpcode == 200,
|
||
// 'http_code' => $httpcode,
|
||
// 'response' => json_decode($response, true)
|
||
// ];
|
||
// }
|
||
// }
|
||
|
||
public function SendPendaftaranPasien()
|
||
{
|
||
// Ambil raw JSON
|
||
$input = json_decode(file_get_contents('php://input'), true);
|
||
|
||
if (empty($input['Mcu_PreregisterPatientsID'])) {
|
||
return $this->output
|
||
->set_content_type('application/json')
|
||
->set_output(json_encode([
|
||
'status' => false,
|
||
'message' => 'Mcu_PreregisterPatientsID wajib diisi'
|
||
]));
|
||
}
|
||
|
||
$id = $input['Mcu_PreregisterPatientsID'];
|
||
|
||
// 1 Query preregister
|
||
$data = $this->db
|
||
->select('
|
||
mp.Mcu_PreregisterPatientsID,
|
||
mp.Mcu_PreregisterPatientsIsRegistered,
|
||
p.*,
|
||
to.*,
|
||
c.*,
|
||
his_to.*
|
||
|
||
')
|
||
->from('mcu_preregister_patients mp')
|
||
->join('m_patient p', 'p.M_PatientID = mp.Mcu_PreregisterPatientsM_PatientID', 'left')
|
||
->join('t_orderheader to', 'to.T_OrderHeaderID = mp.Mcu_PreregisterPatientsT_OrderHeaderID', 'left')
|
||
->join('corporate c', 'c.CorporateID = to.T_OrderHeaderCorporateID', 'left')
|
||
->join('his_maping_t_orderheader his_to', 'his_to.HisMappingTOrderHeaderITOrderHeaderID = to.T_OrderHeaderID', 'left')
|
||
->where('mp.Mcu_PreregisterPatientsID', $id)
|
||
->get()
|
||
->row_array();
|
||
|
||
if (!$data) {
|
||
return $this->output
|
||
->set_content_type('application/json')
|
||
->set_output(json_encode([
|
||
'status' => false,
|
||
'message' => 'Data tidak ditemukan'
|
||
]));
|
||
}
|
||
|
||
// 2 Mapping payload
|
||
$payload = [
|
||
'no_rekam_medis' => null,
|
||
'nama_pasien' => $data['M_PatientName'],
|
||
'nama_panggilan' => $data['M_PatientPrefix'],
|
||
'jenis_kelamin' => $data['M_PatientGender'] == 'male' ? 'L' : 'P',
|
||
'golongan_darah' => $data['M_PatientBloodTypeCode'],
|
||
'agama' => $data['M_PatientReligionCode'],
|
||
'pendidikan' => $data['M_PatientEducationCode'],
|
||
'pekerjaan' => $data['M_PatientJob'],
|
||
'suku_bangsa' => $data['M_PatientEtnicCode'],
|
||
'bahasa_sehari' => null,
|
||
'tempat_lahir' => null,
|
||
'tanggal_lahir' => $data['M_PatientDOB'],
|
||
'status_perkawinan'=> null,
|
||
'nomor_identitas' => [
|
||
'jenis' => $data['M_PatientIdentifierCode'],
|
||
'nomor' => $data['M_PatientIdentifierValue'],
|
||
],
|
||
'jenis_pasien' => 'jaminan_perusahaan',
|
||
'perusahaan_penanggung_code' => $data['CorporateCode'],
|
||
'perusahaan_afiliasi_code' => $data['CorporateName'],
|
||
'group_tarif' => $data['HisMappingTOrderHeaderGroupPaket'],
|
||
'provider' => null,
|
||
'no_polis' => $data['Mcu_PreregisterPatientsIsRegistered'],
|
||
'no_karyawan' => $data['M_PatientNIP'],
|
||
'no_jaminan' => null,
|
||
'nama_karyawan' => null,
|
||
'kewarganegaraan' => 'Indonesia',
|
||
'negara' => 'Indonesia',
|
||
'provinsi' => $data['M_PatientAddressState'],
|
||
'kabupaten' => $data['M_PatientAddressVillage'],
|
||
'kota' => $data['M_PatientAddressCity'],
|
||
'kelurahan' => $data['M_PatientAddressVillage'],
|
||
'alamat' => $data['M_PatientAddress'],
|
||
'dispenser_paket_code' => $data['HisMappingTOrderHeaderPaketCode'],
|
||
'poli_code' => 'MCU',
|
||
'dokter_dpjp_code' => 'dr_mcu'
|
||
];
|
||
|
||
// 3 Simpan log dulu (biar aman kalau API mati)
|
||
$insert = [
|
||
'payload' => json_encode($payload),
|
||
'response' => null,
|
||
'status_sync' => 'PROCESS',
|
||
'created_at' => date('Y-m-d H:i:s')
|
||
];
|
||
|
||
$this->db->insert('his_api_test', $insert);
|
||
$logId = $this->db->insert_id();
|
||
echo $logId;
|
||
// // 4 Kirim ke HIS
|
||
// $url = "https://devcpone.aplikasi.web.id/one-api/api_his/SendPendaftaranPasien";
|
||
|
||
// $ch = curl_init($url);
|
||
|
||
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
// curl_setopt($ch, CURLOPT_POST, true);
|
||
// curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||
// 'Content-Type: application/json'
|
||
// ]);
|
||
// curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
|
||
|
||
// $response = curl_exec($ch);
|
||
// $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||
|
||
// curl_close($ch);
|
||
|
||
// // 5 Update log
|
||
// $this->db->where('id', $logId)->update('his_api_test', [
|
||
// 'response' => $response,
|
||
// 'status_sync' => $httpcode == 200 ? 'SUCCESS' : 'FAILED'
|
||
// ]);
|
||
|
||
// return $this->output
|
||
// ->set_content_type('application/json')
|
||
// ->set_output(json_encode([
|
||
// 'status' => $httpcode == 200,
|
||
// 'http_code' => $httpcode,
|
||
// 'response' => json_decode($response, true)
|
||
// ]));
|
||
}
|
||
|
||
public function SendMasterPerusahaan(){
|
||
$payload = [
|
||
'kode_perusahaan' => 'SAS',
|
||
'nama_perusahaan' => 'PT Shadana',
|
||
'jenis_perushaaan' => 'CPONE',
|
||
'opsi' => 'supplier,kerjasama,asuransi,provider',
|
||
'jaminan_cob' => true,
|
||
'mata_uang' => 'idr',
|
||
'nama_pic' => 'tbfajri',
|
||
'email_pic' => 'tbfajri7@gmail.com',
|
||
'phone_number_pic' => '081234567890',
|
||
'alamat_perusahaan' => 'test1234567',
|
||
'kota_perusahaan' => 'solo',
|
||
'propinsi_perusahaan' => 'jawa tengah',
|
||
'telepon_perusahaan' => '021 2345 2323',
|
||
'email_perusahaan' => 'custmer@sas.com',
|
||
];
|
||
}
|
||
|
||
public function SimpanPaket()
|
||
{
|
||
$use_dummy = false;
|
||
|
||
$error = null;
|
||
$url = null;
|
||
$params_log = [];
|
||
|
||
if ($use_dummy) {
|
||
|
||
$url = 'DUMMY_LOCAL_FILE';
|
||
$params_log = ['mode' => 'dummy'];
|
||
|
||
$response = file_get_contents(
|
||
'/home/one/project/one/one-api/application/controllers/test/dummy/paket_response.json'
|
||
);
|
||
|
||
$http_code = 200;
|
||
|
||
} else {
|
||
|
||
$host = "https://devone.aplikasi.web.id:6060";
|
||
$token = "MYmIZGwkLQUjmFHiL2kgrOjhUyZnk507oBD2BDwM";
|
||
|
||
$endpoint = "/api/cpone/paket";
|
||
// $queryStr = "?start_date=2025-01-01&end_date=2027-12-30";
|
||
|
||
$url = $host . $endpoint;
|
||
|
||
$params_log = [
|
||
'url' => $url,
|
||
'method' => 'GET'
|
||
];
|
||
|
||
$ch = curl_init($url);
|
||
|
||
curl_setopt_array($ch, [
|
||
CURLOPT_HTTPGET => true,
|
||
CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $token],
|
||
CURLOPT_RETURNTRANSFER => true,
|
||
CURLOPT_TIMEOUT => 30,
|
||
CURLOPT_SSL_VERIFYPEER => false,
|
||
CURLOPT_SSL_VERIFYHOST => false,
|
||
CURLOPT_FOLLOWLOCATION => true,
|
||
]);
|
||
|
||
$response = curl_exec($ch);
|
||
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||
$error = curl_error($ch);
|
||
|
||
curl_close($ch);
|
||
}
|
||
|
||
// =============================
|
||
// LOG API
|
||
// =============================
|
||
$this->db->query("
|
||
INSERT INTO api_log_his (
|
||
api_url,
|
||
api_params,
|
||
api_response,
|
||
api_status,
|
||
api_created
|
||
)
|
||
VALUES (?, ?, ?, ?, NOW())
|
||
", [
|
||
$url,
|
||
json_encode($params_log),
|
||
$response,
|
||
$http_code
|
||
]);
|
||
|
||
if ($error) {
|
||
echo "Curl Error: " . $error;
|
||
return false;
|
||
}
|
||
|
||
if ($http_code != 200) {
|
||
echo "API gagal. HTTP: {$http_code}";
|
||
return false;
|
||
}
|
||
|
||
$result = json_decode($response, true);
|
||
|
||
if (empty($result['data'])) {
|
||
echo "Data kosong";
|
||
return true;
|
||
}
|
||
|
||
// =============================
|
||
// BEGIN TRANSACTION
|
||
// =============================
|
||
$this->db->trans_start();
|
||
|
||
foreach ($result['data'] as $row) {
|
||
|
||
// =============================
|
||
// UPSERT MAPPING PAKET
|
||
// =============================
|
||
$sql = "
|
||
INSERT INTO his_mapping_paket (
|
||
PaketID,
|
||
KodeCorp,
|
||
Nama,
|
||
JenisPaket,
|
||
Harga,
|
||
RawatInap,
|
||
RawatJalan,
|
||
MultiVisit,
|
||
Keterangan,
|
||
Kelas,
|
||
TglDari,
|
||
TglSampai
|
||
)
|
||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?)
|
||
ON DUPLICATE KEY UPDATE
|
||
KodeCorp = VALUES(KodeCorp),
|
||
Nama = VALUES(Nama),
|
||
JenisPaket = VALUES(JenisPaket),
|
||
Harga = VALUES(Harga),
|
||
RawatInap = VALUES(RawatInap),
|
||
RawatJalan = VALUES(RawatJalan),
|
||
MultiVisit = VALUES(MultiVisit),
|
||
Keterangan = VALUES(Keterangan),
|
||
Kelas = VALUES(Kelas),
|
||
TglDari = VALUES(TglDari),
|
||
TglSampai = VALUES(TglSampai)
|
||
";
|
||
|
||
$this->db->query($sql, [
|
||
$row['PaketID'],
|
||
$row['KodeCorp'],
|
||
$row['Nama'],
|
||
$row['JenisPaket'],
|
||
$row['Harga'],
|
||
$row['RawatInap'],
|
||
$row['RawatJalan'],
|
||
$row['MultiVisit'],
|
||
$row['Keterangan'],
|
||
$row['Kelas'],
|
||
$row['TglDari'],
|
||
$row['TglSampai']
|
||
]);
|
||
}
|
||
|
||
$this->db->trans_complete();
|
||
|
||
if ($this->db->trans_status() === FALSE) {
|
||
echo "Transaksi gagal ❌";
|
||
return false;
|
||
}
|
||
|
||
echo "Sync paket HIS selesai 🚀";
|
||
return true;
|
||
}
|
||
|
||
public function SimpanPaketDetail($code)
|
||
{
|
||
|
||
$use_dummy = false; // ubah ke false kalau network sudah OK
|
||
|
||
if ($use_dummy) {
|
||
|
||
$response = file_get_contents('/home/one/project/one/one-api/application/controllers/test/dummy/paket_detail_response.json');
|
||
$http_code = 200;
|
||
|
||
} else {
|
||
|
||
// taruh block CURL asli lo di sini
|
||
$host = "https://devone.aplikasi.web.id:6060";
|
||
$token = "MYmIZGwkLQUjmFHiL2kgrOjhUyZnk507oBD2BDwM";
|
||
|
||
$endpoint = "/api/cpone/paket/" . $code;
|
||
|
||
$url = $host . $endpoint . $query;
|
||
|
||
$final_headers = array(
|
||
'Authorization: Bearer ' . $token
|
||
);
|
||
|
||
// =============================
|
||
// HIT API
|
||
// =============================
|
||
$ch = curl_init($url);
|
||
|
||
// 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);
|
||
|
||
// =============================
|
||
// LOG REQUEST & RESPONSE
|
||
// =============================
|
||
$params_log = [
|
||
'url' => $url,
|
||
'method' => 'GET'
|
||
];
|
||
}
|
||
|
||
$result = json_decode($response, true);
|
||
|
||
if (empty($result['data']['layanan'])) {
|
||
echo "Tidak ada layanan";
|
||
return true;
|
||
}
|
||
|
||
$paketID = $result['data']['PaketID'];
|
||
|
||
foreach ($result['data']['layanan'] as $row) {
|
||
|
||
// Bersihkan angka dari koma
|
||
$harga = floatval(str_replace(',', '', $row['Harga']));
|
||
$diskon = floatval(str_replace(',', '', $row['Diskon']));
|
||
$shareRS = floatval(str_replace(',', '', $row['ShareRS']));
|
||
$shareDokter = floatval(str_replace(',', '', $row['ShareDokter']));
|
||
$subtotal = floatval(str_replace(',', '', $row['Subtotal']));
|
||
|
||
$sql = "
|
||
INSERT INTO his_mapping_paket_detail_layanan (
|
||
HisMapingPaketID,
|
||
PaketID,
|
||
LayananID,
|
||
NamaLayanan,
|
||
NamaDepartemen,
|
||
Jumlah,
|
||
JumlahHari,
|
||
Harga,
|
||
Diskon,
|
||
TipeDiskon,
|
||
ShareRS,
|
||
ShareDokter,
|
||
Subtotal
|
||
)
|
||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)
|
||
ON DUPLICATE KEY UPDATE
|
||
Harga = VALUES(Harga),
|
||
Diskon = VALUES(Diskon),
|
||
Subtotal = VALUES(Subtotal)
|
||
";
|
||
|
||
$query = $this->db->query($sql, [
|
||
$paketID, // HisMapingPaketID (sementara pakai PaketID string)
|
||
$paketID,
|
||
$row['LayananID'],
|
||
$row['NamaLayanan'],
|
||
$row['NamaDepartemen'],
|
||
(int) $row['Jumlah'],
|
||
(int) $row['JumlahHari'],
|
||
$harga,
|
||
$diskon,
|
||
$row['TipeDiskon'],
|
||
$shareRS,
|
||
$shareDokter,
|
||
$subtotal
|
||
]);
|
||
|
||
if (!$query) {
|
||
echo "<pre>";
|
||
print_r($this->db->error());
|
||
die;
|
||
}
|
||
}
|
||
|
||
echo "Sync detail layanan selesai 🚀";
|
||
return true;
|
||
}
|
||
|
||
public function SimpanCorporate()
|
||
{
|
||
$branchCode = $this->auth_token();
|
||
$data = $this->param();
|
||
|
||
// 🧹 Basic sanitasi
|
||
$insert = [
|
||
'PerusahaanID' => $data['PerusahaanID'] ?? null,
|
||
'KodePerusahaan' => $data['KodePerusahaan'] ?? null,
|
||
'Nama' => $data['Nama'] ?? null,
|
||
'JenisPerusahaanID' => $data['JenisPerusahaanID'] ?? null,
|
||
'Penjamin' => $data['Penjamin'] ?? 'N',
|
||
'Asuransi' => $data['Asuransi'] ?? 'N',
|
||
'Provider' => $data['Provider'] ?? 'N',
|
||
'COB' => $data['COB'] ?? 'N',
|
||
'MataUangID' => $data['MataUangID'] ?? 'IDR',
|
||
'Penanggungjawab' => $data['Penanggungjawab'] ?? null,
|
||
'TeleponPenanggungjawab' => $data['TeleponPenanggungjawab'] ?? null,
|
||
'EmailPenanggungjawab' => $data['EmailPenanggungjawab'] ?? null,
|
||
'Alamat' => $data['Alamat'] ?? null,
|
||
'Kota' => $data['Kota'] ?? null,
|
||
'KodePos' => $data['KodePos'] ?? null,
|
||
'Propinsi' => $data['Propinsi'] ?? null,
|
||
'Negara' => $data['Negara'] ?? null,
|
||
'Telepon' => $data['Telepon'] ?? null,
|
||
'Fax' => $data['Fax'] ?? null,
|
||
'Email' => $data['Email'] ?? null,
|
||
'Website' => $data['Website'] ?? null,
|
||
'NamaBank' => $data['NamaBank'] ?? null,
|
||
'NamaRekening' => $data['NamaRekening'] ?? null,
|
||
'nomerRekening' => $data['nomerRekening'] ?? null,
|
||
'DefaultLamaPembayaran' => $data['DefaultLamaPembayaran'] ?? 0,
|
||
'DefaultDiskon' => $data['DefaultDiskon'] ?? 0,
|
||
'ppn' => $data['ppn'] ?? 11,
|
||
'Catatan' => $data['Catatan'] ?? null,
|
||
'NPWP' => $data['NPWP'] ?? null,
|
||
'FlagDiskon' => $data['FlagDiskon'] ?? 'N',
|
||
'VerifikasiBilling' => $data['VerifikasiBilling'] ?? 'N',
|
||
'PIC_Marketing' => $data['PIC_Marketing'] ?? null,
|
||
'NA' => $data['NA'] ?? 'N',
|
||
'CreatedAt' => date('Y-m-d H:i:s')
|
||
];
|
||
|
||
// 🧠 Validasi minimal
|
||
if (empty($insert['PerusahaanID']) || empty($insert['Nama'])) {
|
||
return $this->jsonResponse(false, 422, 'PerusahaanID dan Nama wajib diisi');
|
||
}
|
||
|
||
// 🚀 Insert
|
||
$this->db->insert('his_mapping_corporate', $insert);
|
||
|
||
if ($this->db->affected_rows() > 0) {
|
||
return $this->jsonResponse(true, 201, 'Corporate berhasil dibuat', $insert);
|
||
} else {
|
||
return $this->jsonResponse(false, 500, 'Gagal insert data');
|
||
}
|
||
|
||
}
|
||
|
||
public function simpanLayanan()
|
||
{
|
||
$use_dummy = true; // ubah ke false kalau network sudah OK
|
||
|
||
if ($use_dummy) {
|
||
|
||
$response = file_get_contents('/home/one/project/one/one-api/application/controllers/test/dummy/layanan_response.json');
|
||
$http_code = 200;
|
||
|
||
} else {
|
||
// taruh block CURL asli lo di sini
|
||
$host = "https://devone.aplikasi.web.id:6060";
|
||
$token = "MYmIZGwkLQUjmFHiL2kgrOjhUyZnk507oBD2BDwM";
|
||
|
||
$endpoint = "/api/cpone/layanan";
|
||
|
||
$url = $host . $endpoint;
|
||
|
||
$final_headers = array(
|
||
'Authorization: Bearer ' . $token
|
||
);
|
||
|
||
// =============================
|
||
// HIT API
|
||
// =============================
|
||
$ch = curl_init($url);
|
||
|
||
// 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);
|
||
|
||
// =============================
|
||
// LOG REQUEST & RESPONSE
|
||
// =============================
|
||
$params_log = [
|
||
'url' => $url,
|
||
'method' => 'GET'
|
||
];
|
||
}
|
||
|
||
$result = json_decode($response, true);
|
||
|
||
if (empty($result['data'])) {
|
||
echo "Data kosong";
|
||
return true;
|
||
}
|
||
|
||
foreach ($result['data'] as $row) {
|
||
|
||
$sql = "
|
||
INSERT INTO his_mapping_layanan (
|
||
HisMapingLayananGroupLayananID,
|
||
HisMapingLayananDepartemen,
|
||
HisMapingLayananLayananDepartemenID,
|
||
HisMapingLayananLayananID,
|
||
HisMapingLayananNamaLayanan,
|
||
HisMapingLayananHarga,
|
||
HisMapingLayananIsActive
|
||
)
|
||
VALUES (?,?,?,?,?,?,1)
|
||
ON DUPLICATE KEY UPDATE
|
||
HisMapingLayananGroupLayananID = VALUES(HisMapingLayananGroupLayananID),
|
||
HisMapingLayananDepartemen = VALUES(HisMapingLayananDepartemen),
|
||
HisMapingLayananLayananDepartemenID = VALUES(HisMapingLayananLayananDepartemenID),
|
||
HisMapingLayananNamaLayanan = VALUES(HisMapingLayananNamaLayanan),
|
||
HisMapingLayananHarga = VALUES(HisMapingLayananHarga),
|
||
HisMapingLayananIsActive = 1
|
||
";
|
||
|
||
$query = $this->db->query($sql, [
|
||
$row['GroupLayananID'],
|
||
$row['Departemen'],
|
||
$row['LayananDepartemenID'],
|
||
$row['LayananID'],
|
||
$row['NamaLayanan'],
|
||
(float) $row['Harga']
|
||
]);
|
||
|
||
if (!$query) {
|
||
echo "<pre>";
|
||
print_r($this->db->error());
|
||
die;
|
||
}
|
||
}
|
||
|
||
|
||
echo "Sync layanan HIS selesai 🚀";
|
||
return true;
|
||
}
|
||
|
||
public function mappingPaket()
|
||
{
|
||
$rows = $this->db
|
||
->where('HasMapping', 0)
|
||
->get('his_mapping_paket')
|
||
->result_array();
|
||
|
||
if (empty($rows)) {
|
||
echo json_encode([
|
||
"Status" => [
|
||
"OK" => true,
|
||
"Code" => 200,
|
||
"Messages" => "Tidak ada data perlu mapping"
|
||
]
|
||
]);
|
||
return;
|
||
}
|
||
|
||
$this->db->trans_begin();
|
||
|
||
foreach ($rows as $row) {
|
||
|
||
/**
|
||
* 1️⃣ DELETE HEADER + CHILD
|
||
*/
|
||
$existing = $this->db
|
||
->where('T_PriceHeaderCode', $row['PaketID'])
|
||
->get('t_priceheader')
|
||
->row();
|
||
|
||
if ($existing) {
|
||
$oldPriceID = $existing->T_PriceHeaderID;
|
||
|
||
$this->db->where('T_PriceT_PriceHeaderID', $oldPriceID)->delete('t_price');
|
||
$this->db->where('T_PacketT_PriceHeaderID', $oldPriceID)->delete('t_packet');
|
||
$this->db->where('T_PriceHeaderID', $oldPriceID)->delete('t_priceheader');
|
||
}
|
||
|
||
/**
|
||
* 2️⃣ INSERT HEADER
|
||
*/
|
||
$this->db->insert('t_priceheader', [
|
||
'T_PriceHeaderName' => $row['Nama'],
|
||
'T_PriceHeaderCode' => $row['PaketID'],
|
||
'T_PriceHeaderStartDate' => $row['TglDari'],
|
||
'T_PriceHeaderEndDate' => $row['TglSampai'],
|
||
]);
|
||
|
||
$priceID = $this->db->insert_id();
|
||
|
||
/**
|
||
* 3️⃣ AMBIL DETAIL
|
||
*/
|
||
$details = $this->db
|
||
->where('PaketID', $row['PaketID'])
|
||
->get('his_mapping_paket_detail_layanan')
|
||
->result_array();
|
||
|
||
if (empty($details)) continue;
|
||
|
||
$packetTotal = 0;
|
||
|
||
/**
|
||
* 4️⃣ INSERT PRICE + HITUNG TOTAL
|
||
*/
|
||
$packetDetails = [];
|
||
|
||
foreach ($details as $detail) {
|
||
|
||
// UPSERT TEST
|
||
$test = $this->db
|
||
->where('T_TestCode', $detail['LayananID'])
|
||
->get('t_test')
|
||
->row();
|
||
|
||
if ($test) {
|
||
$testID = $test->T_TestID;
|
||
|
||
$this->db->where('T_TestID', $testID)
|
||
->update('t_test', [
|
||
'T_TestName' => $detail['NamaLayanan'],
|
||
'T_TestShortName' => $detail['NamaLayanan'],
|
||
'T_TestShortNameBarcode' => $detail['NamaLayanan'],
|
||
]);
|
||
} else {
|
||
$this->db->insert('t_test', [
|
||
'T_TestCode' => $detail['LayananID'],
|
||
'T_TestName' => $detail['NamaLayanan'],
|
||
'T_TestShortName' => $detail['NamaLayanan'],
|
||
'T_TestShortNameBarcode' => $detail['NamaLayanan'],
|
||
]);
|
||
$testID = $this->db->insert_id();
|
||
}
|
||
|
||
// Hitung diskon
|
||
$harga = $detail['Harga'];
|
||
$diskonRp = ($detail['TipeDiskon'] === 'persen')
|
||
? $harga * ($detail['Diskon'] / 100)
|
||
: $detail['Diskon'];
|
||
|
||
$subTotal = $harga - $diskonRp;
|
||
$packetTotal += $subTotal;
|
||
|
||
// Insert t_price
|
||
$this->db->insert('t_price', [
|
||
'T_PriceT_PriceHeaderID' => $priceID,
|
||
'T_PriceT_TestID' => $testID,
|
||
'T_PriceAmount' => $harga,
|
||
'T_PriceDisc' => $diskonRp,
|
||
'T_PriceSubTotal' => $subTotal,
|
||
'T_PriceTotal' => $subTotal,
|
||
]);
|
||
|
||
// simpan untuk packetdetail
|
||
$packetDetails[] = [
|
||
'testID' => $testID,
|
||
'harga' => $harga,
|
||
'subtotal' => $subTotal
|
||
];
|
||
}
|
||
|
||
/**
|
||
* 5️⃣ INSERT PACKET
|
||
*/
|
||
$this->db->insert('t_packet', [
|
||
'T_PacketT_PriceHeaderID' => $priceID,
|
||
'T_PacketName' => $row['Nama'],
|
||
'T_PacketPrice' => $packetTotal,
|
||
'T_PacketOriginalPrice' => $packetTotal,
|
||
'T_PacketSasCode' => $row['PaketID'],
|
||
'T_PacketStartDate' => $row['TglDari'],
|
||
'T_PacketEndDate' => $row['TglSampai'],
|
||
]);
|
||
|
||
$tPacketID = $this->db->insert_id();
|
||
|
||
/**
|
||
* 6️⃣ INSERT PACKET DETAIL
|
||
*/
|
||
foreach ($packetDetails as $pd) {
|
||
$this->db->insert('t_packetdetail', [
|
||
'T_PacketDetailT_PacketID' => $tPacketID,
|
||
'T_PacketDetailT_TestID' => $pd['testID'],
|
||
'T_PacketDetailOriginalPrice' => $pd['harga'],
|
||
'T_PacketDetailPrice' => $pd['subtotal'],
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 7️⃣ UPDATE FLAG
|
||
*/
|
||
$this->db->where('HisMapingPaketID', $row['HisMapingPaketID'])
|
||
->update('his_mapping_paket', ['HasMapping' => 1]);
|
||
}
|
||
|
||
if ($this->db->trans_status() === FALSE) {
|
||
$this->db->trans_rollback();
|
||
return $this->jsonError("Transaksi gagal");
|
||
}
|
||
|
||
$this->db->trans_commit();
|
||
echo json_encode([
|
||
"Status" => [
|
||
"OK" => true,
|
||
"Code" => 200,
|
||
"Messages" => "Mapping paket berhasil 🚀"
|
||
]
|
||
]);
|
||
return;
|
||
}
|
||
|
||
public function mapCorporate($perusahaanID = null)
|
||
{
|
||
$branchCode = "NOAUTH";
|
||
$data = $this->param();
|
||
|
||
// 🔎 Ambil data dari his_mapping_corporate
|
||
$mapping = $this->db
|
||
->where('PerusahaanID', $perusahaanID)
|
||
->where('NA', 'N')
|
||
->get('his_mapping_corporate')
|
||
->row();
|
||
|
||
if (!$mapping) {
|
||
return $this->jsonResponse(false, 404, 'Data mapping tidak ditemukan');
|
||
}
|
||
|
||
// 🚫 Cek apakah sudah ada di corporate
|
||
$exist = $this->db
|
||
->where('CorporateCode', $mapping->KodePerusahaan)
|
||
->get('corporate')
|
||
->row();
|
||
|
||
if ($exist) {
|
||
return $this->jsonResponse(false, 409, 'Corporate sudah ada');
|
||
}
|
||
|
||
// 🔄 Transform mapping ke tabel corporate
|
||
$insert = [
|
||
'CorporateOldCompanyID' => $mapping->PerusahaanID,
|
||
'CorporateCorporateTypeID' => $mapping->JenisPerusahaanID,
|
||
'CorporateCode' => $mapping->KodePerusahaan,
|
||
'CorporateName' => $mapping->Nama,
|
||
|
||
'CorporateAddress' => $mapping->Alamat ?? '',
|
||
'CorporateAddressCity' => $mapping->Kota ?? '',
|
||
'CorporateAddressState' => $mapping->Propinsi ?? '',
|
||
'CorporateAddressCountry' => $mapping->Negara ?? '',
|
||
|
||
'CorporateEmail' => $mapping->Email,
|
||
'CorporatePhone' => $mapping->Telepon ?? '',
|
||
|
||
'CorporatePICName' => $mapping->Penanggungjawab ?? '',
|
||
'CorporatePICEmail' => $mapping->EmailPenanggungjawab,
|
||
'CorporatePICPhone' => $mapping->TeleponPenanggungjawab ?? '',
|
||
|
||
'CorporateDueDate' => $mapping->DefaultLamaPembayaran ?? 0,
|
||
|
||
'CorporateCreated' => date('Y-m-d H:i:s'),
|
||
'CorporateCreatedUserID' => 1,
|
||
'CorporateLastUpdated' => date('Y-m-d H:i:s'),
|
||
'CorporateLastUpdatedUserID' => 1,
|
||
|
||
'CorporateIsActive' => ($mapping->NA == 'N') ? 'Y' : 'N'
|
||
];
|
||
|
||
$this->db->trans_start();
|
||
|
||
$this->db->insert('corporate', $insert);
|
||
|
||
// OPTIONAL: Tandai sudah mapping
|
||
$this->db->where('PerusahaanID', $perusahaanID)
|
||
->update('his_mapping_corporate', [
|
||
'HasMapping' => 1
|
||
]);
|
||
|
||
$this->db->trans_complete();
|
||
|
||
if ($this->db->trans_status() === FALSE) {
|
||
return $this->jsonResponse(false, 500, 'Gagal mapping corporate');
|
||
}
|
||
|
||
return $this->jsonResponse(true, 201, 'Corporate berhasil dimapping', $insert);
|
||
}
|
||
|
||
/**
|
||
* Helper rollback
|
||
*/
|
||
private function rollbackWithError($msg)
|
||
{
|
||
$error = $this->db->error();
|
||
$this->db->trans_rollback();
|
||
|
||
echo json_encode([
|
||
"Status" => [
|
||
"OK" => false,
|
||
"Code" => 500,
|
||
"Messages" => $msg,
|
||
"DB_Code" => $error['code'],
|
||
"DB_Message" => $error['message'],
|
||
"Last_Query" => $this->db->last_query()
|
||
]
|
||
]);
|
||
return;
|
||
}
|
||
|
||
public function mappingPaketDetail($code)
|
||
{
|
||
|
||
$sql = "
|
||
SELECT
|
||
HisMapingPaketDetailID,
|
||
HisMapingPaketID,
|
||
PaketID,
|
||
LayananID,
|
||
NamaLayanan,
|
||
NamaDepartemen,
|
||
Jumlah,
|
||
JumlahHari,
|
||
Harga,
|
||
Diskon,
|
||
TipeDiskon,
|
||
ShareRS,
|
||
ShareDokter,
|
||
Subtotal,
|
||
FROM his_mapping_paket_detail_layanan
|
||
WHERE HisMapingPaketID = ".$code."
|
||
";
|
||
|
||
$qry = $this->db->query($sql);
|
||
|
||
if (!$qry) {
|
||
$db_error = $this->db->error();
|
||
|
||
echo json_encode([
|
||
"Status" => [
|
||
"OK" => false,
|
||
"Code" => 500,
|
||
"Messages" => $db_error['message']
|
||
],
|
||
"Data" => null
|
||
]);
|
||
return;
|
||
}
|
||
|
||
$rows = $qry->result_array();
|
||
|
||
if (empty($rows)) {
|
||
echo json_encode([
|
||
"Status" => [
|
||
"OK" => true,
|
||
"Code" => 200,
|
||
"Messages" => "Tidak ada data perlu mapping"
|
||
],
|
||
"Data" => []
|
||
]);
|
||
return;
|
||
}
|
||
|
||
$this->db->trans_start();
|
||
|
||
foreach ($rows as $row) {
|
||
|
||
// =============================
|
||
// UPSERT T_PRICEHEADER
|
||
// =============================
|
||
$sqlTpriceHeader = "
|
||
INSERT INTO t_priceheader (
|
||
T_PriceHeaderName,
|
||
T_PriceHeaderCode,
|
||
T_PriceHeaderStartDate,
|
||
T_PriceHeaderEndDate
|
||
)
|
||
VALUES (?,?,?,?)
|
||
ON DUPLICATE KEY UPDATE
|
||
T_PriceHeaderName = VALUES(T_PriceHeaderName),
|
||
T_PriceHeaderStartDate = VALUES(T_PriceHeaderStartDate),
|
||
T_PriceHeaderEndDate = VALUES(T_PriceHeaderEndDate)
|
||
";
|
||
|
||
$query = $this->db->query($sqlTpriceHeader, [
|
||
$row['Nama'],
|
||
$row['PaketID'],
|
||
$row['TglDari'],
|
||
$row['TglSampai'],
|
||
]);
|
||
|
||
if (!$query) {
|
||
continue; // skip kalau gagal
|
||
}
|
||
|
||
// =============================
|
||
// UPDATE HasMapping = 1
|
||
// =============================
|
||
$this->db->where('HisMapingPaketID', $row['HisMapingPaketID'])
|
||
->update('his_mapping_paket', [
|
||
'HasMapping' => 1
|
||
]);
|
||
}
|
||
|
||
$this->db->trans_complete();
|
||
|
||
if ($this->db->trans_status() === FALSE) {
|
||
echo json_encode([
|
||
"Status" => [
|
||
"OK" => false,
|
||
"Code" => 500,
|
||
"Messages" => "Transaksi gagal"
|
||
]
|
||
]);
|
||
return;
|
||
}
|
||
|
||
echo json_encode([
|
||
"Status" => [
|
||
"OK" => true,
|
||
"Code" => 200,
|
||
"Messages" => "Mapping paket berhasil 🚀"
|
||
]
|
||
]);
|
||
}
|
||
|
||
private function jsonResponse($ok, $code, $message, $data = [])
|
||
{
|
||
return $this->output
|
||
->set_content_type('application/json')
|
||
->set_status_header($code)
|
||
->set_output(json_encode([
|
||
"Status" => [
|
||
"OK" => $ok,
|
||
"Code" => $code,
|
||
"Messages" => $message
|
||
],
|
||
"Data" => $data
|
||
]));
|
||
}
|
||
} |