256 lines
9.1 KiB
PHP
256 lines
9.1 KiB
PHP
<?php
|
|
|
|
class Member extends MY_Controller
|
|
{
|
|
var $db_onedev, $clientId, $secret, $baseUrl;
|
|
public function index()
|
|
{
|
|
echo "Member API";
|
|
}
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->db_onedev = $this->load->database("onedev", true);
|
|
$this->baseUrl = "https://bankpoint.pramita.co.id/one-api/point/api_ext";
|
|
$this->hostname = 'https://bankpoint.jala.my.id';
|
|
$this->clientId = "Mobile";
|
|
$this->secret = "15a9eee65bfec8ee27b2328dfd7811db";
|
|
}
|
|
function getToken()
|
|
{
|
|
$param = json_encode([
|
|
"client" => $this->clientId,
|
|
"secret" => $this->secret
|
|
]);
|
|
$resp = $this->post("{$this->baseUrl}/get_token", $param);
|
|
$respJ = json_decode($resp, true);
|
|
if ($respJ["status"] != "OK") {
|
|
return [false, print_r($respJ, true)];
|
|
}
|
|
return [true, $respJ["token"]];
|
|
}
|
|
function refreshToken()
|
|
{
|
|
list($status, $token) = $this->getToken();
|
|
if ($status) {
|
|
$resp = $this->get("{$this->baseUrl}/refresh_token", $token);
|
|
$respJ = json_decode($resp, true);
|
|
if ($respJ["status"] != "OK") {
|
|
return [false, print_r($respJ, true)];
|
|
}
|
|
return [true, $respJ["token"]];
|
|
}
|
|
}
|
|
function getPoint($nik = '', $page = 1, $limit = 20)
|
|
{
|
|
list($status, $token) = $this->getToken();
|
|
if ($status) {
|
|
$resp = $this->get("{$this->baseUrl}/point/{$nik}/{$page}/{$limit}", $token);
|
|
$respJ = json_decode($resp, true);
|
|
if ($respJ["status"] != "OK") {
|
|
return [false, print_r($respJ, true)];
|
|
}
|
|
return [true, $respJ["data"]];
|
|
}
|
|
return [false, $token];
|
|
}
|
|
function getTypeMember($nik = '')
|
|
{
|
|
$resp = $this->get("{$this->hostname}/one-api/tools/member/gethistorytype/get_history_by_nik/{$nik}");
|
|
$respJ = json_decode($resp, true);
|
|
if ($respJ["status"] != "OK") {
|
|
return [false, print_r($respJ, true)];
|
|
}
|
|
return $respJ["data"]["records"];
|
|
}
|
|
function get($url, $token = '')
|
|
{
|
|
$ch = curl_init($url);
|
|
if ($token != "") {
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_CUSTOMREQUEST => "GET",
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_CONNECTTIMEOUT => 10,
|
|
CURLOPT_TIMEOUT => 10,
|
|
CURLOPT_HTTPHEADER => [
|
|
"Content-Type: application/json",
|
|
"Authorization: Bearer " . $token
|
|
],
|
|
]);
|
|
} else {
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_CUSTOMREQUEST => "GET",
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_CONNECTTIMEOUT => 10,
|
|
CURLOPT_TIMEOUT => 10,
|
|
CURLOPT_HTTPHEADER => [
|
|
"Content-Type: application/json",
|
|
],
|
|
]);
|
|
}
|
|
$result = curl_exec($ch);
|
|
if (curl_error($ch) != "") {
|
|
echo json_encode([
|
|
"status" => "ERR",
|
|
"message" => "Http Error : " . curl_error($ch),
|
|
]);
|
|
curl_close($ch);
|
|
exit();
|
|
}
|
|
curl_close($ch);
|
|
return $result;
|
|
}
|
|
function post($url, $data = [], $token = '')
|
|
{
|
|
$ch = curl_init($url);
|
|
if ($token != "") {
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_CUSTOMREQUEST => "POST",
|
|
CURLOPT_POSTFIELDS => $data,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_CONNECTTIMEOUT => 10,
|
|
CURLOPT_TIMEOUT => 10,
|
|
CURLOPT_HTTPHEADER => [
|
|
"Content-Type: application/json",
|
|
"Authorization: Bearer " . $token
|
|
],
|
|
]);
|
|
} else {
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_CUSTOMREQUEST => "POST",
|
|
CURLOPT_POSTFIELDS => $data,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_CONNECTTIMEOUT => 10,
|
|
CURLOPT_TIMEOUT => 10,
|
|
CURLOPT_HTTPHEADER => [
|
|
"Content-Type: application/json",
|
|
],
|
|
]);
|
|
}
|
|
$result = curl_exec($ch);
|
|
if (curl_error($ch) != "") {
|
|
echo json_encode([
|
|
"status" => "ERR",
|
|
"message" => "Http Error : " . curl_error($ch),
|
|
]);
|
|
curl_close($ch);
|
|
exit();
|
|
}
|
|
curl_close($ch);
|
|
return $result;
|
|
}
|
|
function getPatientAddress($patientID = 0)
|
|
{
|
|
$sql = "SELECT CONCAT(M_PatientAddressDescription, ', ',
|
|
M_KelurahanName, ', ',
|
|
M_DistrictName, ', ',
|
|
M_CityName, ', ',
|
|
M_ProvinceName) AS patientAddress
|
|
FROM m_patientaddress
|
|
JOIN m_kelurahan ON M_PatientAddressM_KelurahanID = M_KelurahanID
|
|
JOIN m_district ON M_KelurahanM_DistrictID = M_DistrictID
|
|
JOIN m_city ON M_DistrictM_CityID = M_CityID
|
|
JOIN m_province ON M_CityM_ProvinceID = M_ProvinceID
|
|
WHERE M_PatientAddressM_PatientID = {$patientID}";
|
|
$qry = $this->db_onedev->query($sql);
|
|
if (!$qry) {
|
|
$this->sys_error_db("Error get data", $this->db_onedev);
|
|
exit;
|
|
}
|
|
return $qry->row_array()['patientAddress'];
|
|
}
|
|
function search()
|
|
{
|
|
if (!$this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$payload = $this->sys_input;
|
|
$fSearch = $payload["fSearch"];
|
|
|
|
$numberLimit = 10;
|
|
if ($fSearch !== '') {
|
|
$c_page = 1;
|
|
}else{
|
|
$c_page = $payload["currentPage"];
|
|
}
|
|
$numberOffset = ($c_page - 1) * $numberLimit;
|
|
|
|
$sqlCount = "SELECT count(*) AS total
|
|
FROM one_pointreward.member_national
|
|
LEFT JOIN one_pointreward.member_infonational ON MemberNIK = MemberInfoNasionalNIK
|
|
WHERE MemberIsActive = 'Y'";
|
|
$sql = "SELECT MemberID,
|
|
MemberM_PatientID,
|
|
MemberName,
|
|
MemberNumber,
|
|
MemberNIK,
|
|
MemberType,
|
|
MemberStartDate,
|
|
MemberEndDate,
|
|
MemberInfoNasionalPoint,
|
|
MemberInfoNasionalCountVisit
|
|
FROM one_pointreward.member_national
|
|
LEFT JOIN one_pointreward.member_infonational ON MemberNIK = MemberInfoNasionalNIK
|
|
WHERE MemberIsActive = 'Y'";
|
|
|
|
if ($fSearch !== '') {
|
|
$sqlCount .= " AND MemberNIK LIKE '%{$fSearch}%' OR MemberNumber LIKE '%{$fSearch}%' OR MemberName LIKE '%{$fSearch}%'";
|
|
$sql .= " AND MemberNIK LIKE '%{$fSearch}%' OR MemberNumber LIKE '%{$fSearch}%' OR MemberName LIKE '%{$fSearch}%'";
|
|
}
|
|
|
|
$totalCount = 0;
|
|
$totalPage = 0;
|
|
|
|
$qry = $this->db_onedev->query($sqlCount);
|
|
if ($qry) {
|
|
$totalCount = $qry->result_array()[0]["total"];
|
|
$totalPage = ceil($totalCount / $numberLimit);
|
|
} else {
|
|
$this->sys_error_db("Error count total data", $this->db_onedev);
|
|
exit;
|
|
}
|
|
|
|
$sql .= " ORDER BY MemberNIK ASC LIMIT {$numberLimit} OFFSET {$numberOffset}";
|
|
$qry = $this->db_onedev->query($sql);
|
|
if (!$qry) {
|
|
$this->sys_error_db("Error get data", $this->db_onedev);
|
|
exit;
|
|
}
|
|
$rows = $qry->result_array();
|
|
if ($rows) {
|
|
list($status, $points) = $this->getPoint($rows[$payload["lastIdx"]]["MemberNIK"], $payload["currentPage"]);
|
|
if (!$status) {
|
|
$this->sys_error($points);
|
|
exit;
|
|
}
|
|
$address = $this->getPatientAddress($rows[0]['MemberM_PatientID']);
|
|
$typemember = $this->getTypeMember($rows[$payload["lastIdx"]]["MemberNIK"]);
|
|
}
|
|
|
|
$result = array("total" => $totalPage, "records" => $rows, "sql" => $this->db_onedev->last_query(), "bankpoint" => $points, "address" => $address, "typemember" => $typemember);
|
|
$this->sys_ok($result);
|
|
exit;
|
|
}
|
|
function searchHistory()
|
|
{
|
|
if (!$this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$payload = $this->sys_input;
|
|
list($status, $points) = $this->getPoint($payload["NIK"], $payload["currentPage"]);
|
|
if (!$status) {
|
|
$this->sys_error($points);
|
|
exit;
|
|
}
|
|
$address = $this->getPatientAddress($payload["PatientID"]);
|
|
$typemember = $this->getTypeMember($payload["NIK"]);
|
|
|
|
$result = array("bankpoint" => $points, "address" => $address, "typemember" => $typemember);
|
|
$this->sys_ok($result);
|
|
}
|
|
}
|